2 * Kernel-based Virtual Machine driver for Linux
4 * derived from drivers/kvm/kvm_main.c
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Amit Shah <amit.shah@qumranet.com>
14 * Ben-Ami Yassour <benami@il.ibm.com>
16 * This work is licensed under the terms of the GNU GPL, version 2. See
17 * the COPYING file in the top-level directory.
21 #include <linux/kvm_host.h>
26 #include "kvm_cache_regs.h"
29 #include <linux/clocksource.h>
30 #include <linux/interrupt.h>
31 #include <linux/kvm.h>
33 #include <linux/vmalloc.h>
34 #include <linux/module.h>
35 #include <linux/mman.h>
36 #include <linux/highmem.h>
37 #include <linux/iommu.h>
38 #include <linux/intel-iommu.h>
39 #include <linux/cpufreq.h>
40 #include <linux/user-return-notifier.h>
41 #include <trace/events/kvm.h>
42 #undef TRACE_INCLUDE_FILE
43 #define CREATE_TRACE_POINTS
46 #include <asm/debugreg.h>
47 #include <asm/uaccess.h>
53 #define MAX_IO_MSRS 256
54 #define CR0_RESERVED_BITS \
55 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
56 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
57 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
58 #define CR4_RESERVED_BITS \
59 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
60 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
61 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
62 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
64 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
66 #define KVM_MAX_MCE_BANKS 32
67 #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
70 * - enable syscall per default because its emulated by KVM
71 * - enable LME and LMA per default on 64 bit KVM
74 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
76 static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
79 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
80 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
82 static void update_cr8_intercept(struct kvm_vcpu *vcpu);
83 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
84 struct kvm_cpuid_entry2 __user *entries);
86 struct kvm_x86_ops *kvm_x86_ops;
87 EXPORT_SYMBOL_GPL(kvm_x86_ops);
90 module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
92 #define KVM_NR_SHARED_MSRS 16
94 struct kvm_shared_msrs_global {
96 u32 msrs[KVM_NR_SHARED_MSRS];
99 struct kvm_shared_msrs {
100 struct user_return_notifier urn;
102 struct kvm_shared_msr_values {
105 } values[KVM_NR_SHARED_MSRS];
108 static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
109 static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
111 struct kvm_stats_debugfs_item debugfs_entries[] = {
112 { "pf_fixed", VCPU_STAT(pf_fixed) },
113 { "pf_guest", VCPU_STAT(pf_guest) },
114 { "tlb_flush", VCPU_STAT(tlb_flush) },
115 { "invlpg", VCPU_STAT(invlpg) },
116 { "exits", VCPU_STAT(exits) },
117 { "io_exits", VCPU_STAT(io_exits) },
118 { "mmio_exits", VCPU_STAT(mmio_exits) },
119 { "signal_exits", VCPU_STAT(signal_exits) },
120 { "irq_window", VCPU_STAT(irq_window_exits) },
121 { "nmi_window", VCPU_STAT(nmi_window_exits) },
122 { "halt_exits", VCPU_STAT(halt_exits) },
123 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
124 { "hypercalls", VCPU_STAT(hypercalls) },
125 { "request_irq", VCPU_STAT(request_irq_exits) },
126 { "irq_exits", VCPU_STAT(irq_exits) },
127 { "host_state_reload", VCPU_STAT(host_state_reload) },
128 { "efer_reload", VCPU_STAT(efer_reload) },
129 { "fpu_reload", VCPU_STAT(fpu_reload) },
130 { "insn_emulation", VCPU_STAT(insn_emulation) },
131 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
132 { "irq_injections", VCPU_STAT(irq_injections) },
133 { "nmi_injections", VCPU_STAT(nmi_injections) },
134 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
135 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
136 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
137 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
138 { "mmu_flooded", VM_STAT(mmu_flooded) },
139 { "mmu_recycled", VM_STAT(mmu_recycled) },
140 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
141 { "mmu_unsync", VM_STAT(mmu_unsync) },
142 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
143 { "largepages", VM_STAT(lpages) },
147 static void kvm_on_user_return(struct user_return_notifier *urn)
150 struct kvm_shared_msrs *locals
151 = container_of(urn, struct kvm_shared_msrs, urn);
152 struct kvm_shared_msr_values *values;
154 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
155 values = &locals->values[slot];
156 if (values->host != values->curr) {
157 wrmsrl(shared_msrs_global.msrs[slot], values->host);
158 values->curr = values->host;
161 locals->registered = false;
162 user_return_notifier_unregister(urn);
165 static void shared_msr_update(unsigned slot, u32 msr)
167 struct kvm_shared_msrs *smsr;
170 smsr = &__get_cpu_var(shared_msrs);
171 /* only read, and nobody should modify it at this time,
172 * so don't need lock */
173 if (slot >= shared_msrs_global.nr) {
174 printk(KERN_ERR "kvm: invalid MSR slot!");
177 rdmsrl_safe(msr, &value);
178 smsr->values[slot].host = value;
179 smsr->values[slot].curr = value;
182 void kvm_define_shared_msr(unsigned slot, u32 msr)
184 if (slot >= shared_msrs_global.nr)
185 shared_msrs_global.nr = slot + 1;
186 shared_msrs_global.msrs[slot] = msr;
187 /* we need ensured the shared_msr_global have been updated */
190 EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
192 static void kvm_shared_msr_cpu_online(void)
196 for (i = 0; i < shared_msrs_global.nr; ++i)
197 shared_msr_update(i, shared_msrs_global.msrs[i]);
200 void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
202 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
204 if (((value ^ smsr->values[slot].curr) & mask) == 0)
206 smsr->values[slot].curr = value;
207 wrmsrl(shared_msrs_global.msrs[slot], value);
208 if (!smsr->registered) {
209 smsr->urn.on_user_return = kvm_on_user_return;
210 user_return_notifier_register(&smsr->urn);
211 smsr->registered = true;
214 EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
216 static void drop_user_return_notifiers(void *ignore)
218 struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
220 if (smsr->registered)
221 kvm_on_user_return(&smsr->urn);
224 unsigned long segment_base(u16 selector)
226 struct descriptor_table gdt;
227 struct desc_struct *d;
228 unsigned long table_base;
235 table_base = gdt.base;
237 if (selector & 4) { /* from ldt */
238 u16 ldt_selector = kvm_read_ldt();
240 table_base = segment_base(ldt_selector);
242 d = (struct desc_struct *)(table_base + (selector & ~7));
243 v = get_desc_base(d);
245 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
246 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
250 EXPORT_SYMBOL_GPL(segment_base);
252 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
254 if (irqchip_in_kernel(vcpu->kvm))
255 return vcpu->arch.apic_base;
257 return vcpu->arch.apic_base;
259 EXPORT_SYMBOL_GPL(kvm_get_apic_base);
261 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
263 /* TODO: reserve bits check */
264 if (irqchip_in_kernel(vcpu->kvm))
265 kvm_lapic_set_base(vcpu, data);
267 vcpu->arch.apic_base = data;
269 EXPORT_SYMBOL_GPL(kvm_set_apic_base);
271 #define EXCPT_BENIGN 0
272 #define EXCPT_CONTRIBUTORY 1
275 static int exception_class(int vector)
285 return EXCPT_CONTRIBUTORY;
292 static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
293 unsigned nr, bool has_error, u32 error_code)
298 if (!vcpu->arch.exception.pending) {
300 vcpu->arch.exception.pending = true;
301 vcpu->arch.exception.has_error_code = has_error;
302 vcpu->arch.exception.nr = nr;
303 vcpu->arch.exception.error_code = error_code;
307 /* to check exception */
308 prev_nr = vcpu->arch.exception.nr;
309 if (prev_nr == DF_VECTOR) {
310 /* triple fault -> shutdown */
311 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
314 class1 = exception_class(prev_nr);
315 class2 = exception_class(nr);
316 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
317 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
318 /* generate double fault per SDM Table 5-5 */
319 vcpu->arch.exception.pending = true;
320 vcpu->arch.exception.has_error_code = true;
321 vcpu->arch.exception.nr = DF_VECTOR;
322 vcpu->arch.exception.error_code = 0;
324 /* replace previous exception with a new one in a hope
325 that instruction re-execution will regenerate lost
330 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
332 kvm_multiple_exception(vcpu, nr, false, 0);
334 EXPORT_SYMBOL_GPL(kvm_queue_exception);
336 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
339 ++vcpu->stat.pf_guest;
340 vcpu->arch.cr2 = addr;
341 kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
344 void kvm_inject_nmi(struct kvm_vcpu *vcpu)
346 vcpu->arch.nmi_pending = 1;
348 EXPORT_SYMBOL_GPL(kvm_inject_nmi);
350 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
352 kvm_multiple_exception(vcpu, nr, true, error_code);
354 EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
357 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
358 * a #GP and return false.
360 bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
362 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
364 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
367 EXPORT_SYMBOL_GPL(kvm_require_cpl);
370 * Load the pae pdptrs. Return true is they are all valid.
372 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
374 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
375 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
378 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
380 ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
381 offset * sizeof(u64), sizeof(pdpte));
386 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
387 if (is_present_gpte(pdpte[i]) &&
388 (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
395 memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
396 __set_bit(VCPU_EXREG_PDPTR,
397 (unsigned long *)&vcpu->arch.regs_avail);
398 __set_bit(VCPU_EXREG_PDPTR,
399 (unsigned long *)&vcpu->arch.regs_dirty);
404 EXPORT_SYMBOL_GPL(load_pdptrs);
406 static bool pdptrs_changed(struct kvm_vcpu *vcpu)
408 u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
412 if (is_long_mode(vcpu) || !is_pae(vcpu))
415 if (!test_bit(VCPU_EXREG_PDPTR,
416 (unsigned long *)&vcpu->arch.regs_avail))
419 r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
422 changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
428 void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
430 if (cr0 & CR0_RESERVED_BITS) {
431 printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
432 cr0, vcpu->arch.cr0);
433 kvm_inject_gp(vcpu, 0);
437 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
438 printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
439 kvm_inject_gp(vcpu, 0);
443 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
444 printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
445 "and a clear PE flag\n");
446 kvm_inject_gp(vcpu, 0);
450 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
452 if ((vcpu->arch.shadow_efer & EFER_LME)) {
456 printk(KERN_DEBUG "set_cr0: #GP, start paging "
457 "in long mode while PAE is disabled\n");
458 kvm_inject_gp(vcpu, 0);
461 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
463 printk(KERN_DEBUG "set_cr0: #GP, start paging "
464 "in long mode while CS.L == 1\n");
465 kvm_inject_gp(vcpu, 0);
471 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
472 printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
474 kvm_inject_gp(vcpu, 0);
480 kvm_x86_ops->set_cr0(vcpu, cr0);
481 vcpu->arch.cr0 = cr0;
483 kvm_mmu_reset_context(vcpu);
486 EXPORT_SYMBOL_GPL(kvm_set_cr0);
488 void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
490 kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
492 EXPORT_SYMBOL_GPL(kvm_lmsw);
494 void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
496 unsigned long old_cr4 = kvm_read_cr4(vcpu);
497 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
499 if (cr4 & CR4_RESERVED_BITS) {
500 printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
501 kvm_inject_gp(vcpu, 0);
505 if (is_long_mode(vcpu)) {
506 if (!(cr4 & X86_CR4_PAE)) {
507 printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
509 kvm_inject_gp(vcpu, 0);
512 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
513 && ((cr4 ^ old_cr4) & pdptr_bits)
514 && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
515 printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
516 kvm_inject_gp(vcpu, 0);
520 if (cr4 & X86_CR4_VMXE) {
521 printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
522 kvm_inject_gp(vcpu, 0);
525 kvm_x86_ops->set_cr4(vcpu, cr4);
526 vcpu->arch.cr4 = cr4;
527 vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
528 kvm_mmu_reset_context(vcpu);
530 EXPORT_SYMBOL_GPL(kvm_set_cr4);
532 void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
534 if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
535 kvm_mmu_sync_roots(vcpu);
536 kvm_mmu_flush_tlb(vcpu);
540 if (is_long_mode(vcpu)) {
541 if (cr3 & CR3_L_MODE_RESERVED_BITS) {
542 printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
543 kvm_inject_gp(vcpu, 0);
548 if (cr3 & CR3_PAE_RESERVED_BITS) {
550 "set_cr3: #GP, reserved bits\n");
551 kvm_inject_gp(vcpu, 0);
554 if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
555 printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
557 kvm_inject_gp(vcpu, 0);
562 * We don't check reserved bits in nonpae mode, because
563 * this isn't enforced, and VMware depends on this.
568 * Does the new cr3 value map to physical memory? (Note, we
569 * catch an invalid cr3 even in real-mode, because it would
570 * cause trouble later on when we turn on paging anyway.)
572 * A real CPU would silently accept an invalid cr3 and would
573 * attempt to use it - with largely undefined (and often hard
574 * to debug) behavior on the guest side.
576 if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
577 kvm_inject_gp(vcpu, 0);
579 vcpu->arch.cr3 = cr3;
580 vcpu->arch.mmu.new_cr3(vcpu);
583 EXPORT_SYMBOL_GPL(kvm_set_cr3);
585 void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
587 if (cr8 & CR8_RESERVED_BITS) {
588 printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
589 kvm_inject_gp(vcpu, 0);
592 if (irqchip_in_kernel(vcpu->kvm))
593 kvm_lapic_set_tpr(vcpu, cr8);
595 vcpu->arch.cr8 = cr8;
597 EXPORT_SYMBOL_GPL(kvm_set_cr8);
599 unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
601 if (irqchip_in_kernel(vcpu->kvm))
602 return kvm_lapic_get_cr8(vcpu);
604 return vcpu->arch.cr8;
606 EXPORT_SYMBOL_GPL(kvm_get_cr8);
608 static inline u32 bit(int bitno)
610 return 1 << (bitno & 31);
614 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
615 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
617 * This list is modified at module load time to reflect the
618 * capabilities of the host cpu. This capabilities test skips MSRs that are
619 * kvm-specific. Those are put in the beginning of the list.
622 #define KVM_SAVE_MSRS_BEGIN 2
623 static u32 msrs_to_save[] = {
624 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
625 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
628 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
630 MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
633 static unsigned num_msrs_to_save;
635 static u32 emulated_msrs[] = {
636 MSR_IA32_MISC_ENABLE,
639 static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
641 if (efer & efer_reserved_bits) {
642 printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
644 kvm_inject_gp(vcpu, 0);
649 && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
650 printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
651 kvm_inject_gp(vcpu, 0);
655 if (efer & EFER_FFXSR) {
656 struct kvm_cpuid_entry2 *feat;
658 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
659 if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
660 printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
661 kvm_inject_gp(vcpu, 0);
666 if (efer & EFER_SVME) {
667 struct kvm_cpuid_entry2 *feat;
669 feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
670 if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
671 printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
672 kvm_inject_gp(vcpu, 0);
677 kvm_x86_ops->set_efer(vcpu, efer);
680 efer |= vcpu->arch.shadow_efer & EFER_LMA;
682 vcpu->arch.shadow_efer = efer;
684 vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
685 kvm_mmu_reset_context(vcpu);
688 void kvm_enable_efer_bits(u64 mask)
690 efer_reserved_bits &= ~mask;
692 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
696 * Writes msr value into into the appropriate "register".
697 * Returns 0 on success, non-0 otherwise.
698 * Assumes vcpu_load() was already called.
700 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
702 return kvm_x86_ops->set_msr(vcpu, msr_index, data);
706 * Adapt set_msr() to msr_io()'s calling convention
708 static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
710 return kvm_set_msr(vcpu, index, *data);
713 static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
716 struct pvclock_wall_clock wc;
717 struct timespec boot;
724 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
727 * The guest calculates current wall clock time by adding
728 * system time (updated by kvm_write_guest_time below) to the
729 * wall clock specified here. guest system time equals host
730 * system time for us, thus we must fill in host boot time here.
734 wc.sec = boot.tv_sec;
735 wc.nsec = boot.tv_nsec;
736 wc.version = version;
738 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
741 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
744 static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
746 uint32_t quotient, remainder;
748 /* Don't try to replace with do_div(), this one calculates
749 * "(dividend << 32) / divisor" */
751 : "=a" (quotient), "=d" (remainder)
752 : "0" (0), "1" (dividend), "r" (divisor) );
756 static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
758 uint64_t nsecs = 1000000000LL;
763 tps64 = tsc_khz * 1000LL;
764 while (tps64 > nsecs*2) {
769 tps32 = (uint32_t)tps64;
770 while (tps32 <= (uint32_t)nsecs) {
775 hv_clock->tsc_shift = shift;
776 hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
778 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
779 __func__, tsc_khz, hv_clock->tsc_shift,
780 hv_clock->tsc_to_system_mul);
783 static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
785 static void kvm_write_guest_time(struct kvm_vcpu *v)
789 struct kvm_vcpu_arch *vcpu = &v->arch;
791 unsigned long this_tsc_khz;
793 if ((!vcpu->time_page))
796 this_tsc_khz = get_cpu_var(cpu_tsc_khz);
797 if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
798 kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
799 vcpu->hv_clock_tsc_khz = this_tsc_khz;
801 put_cpu_var(cpu_tsc_khz);
803 /* Keep irq disabled to prevent changes to the clock */
804 local_irq_save(flags);
805 kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
807 monotonic_to_bootbased(&ts);
808 local_irq_restore(flags);
810 /* With all the info we got, fill in the values */
812 vcpu->hv_clock.system_time = ts.tv_nsec +
813 (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
816 * The interface expects us to write an even number signaling that the
817 * update is finished. Since the guest won't see the intermediate
818 * state, we just increase by 2 at the end.
820 vcpu->hv_clock.version += 2;
822 shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
824 memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
825 sizeof(vcpu->hv_clock));
827 kunmap_atomic(shared_kaddr, KM_USER0);
829 mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
832 static int kvm_request_guest_time_update(struct kvm_vcpu *v)
834 struct kvm_vcpu_arch *vcpu = &v->arch;
836 if (!vcpu->time_page)
838 set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
842 static bool msr_mtrr_valid(unsigned msr)
845 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
846 case MSR_MTRRfix64K_00000:
847 case MSR_MTRRfix16K_80000:
848 case MSR_MTRRfix16K_A0000:
849 case MSR_MTRRfix4K_C0000:
850 case MSR_MTRRfix4K_C8000:
851 case MSR_MTRRfix4K_D0000:
852 case MSR_MTRRfix4K_D8000:
853 case MSR_MTRRfix4K_E0000:
854 case MSR_MTRRfix4K_E8000:
855 case MSR_MTRRfix4K_F0000:
856 case MSR_MTRRfix4K_F8000:
857 case MSR_MTRRdefType:
858 case MSR_IA32_CR_PAT:
866 static bool valid_pat_type(unsigned t)
868 return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
871 static bool valid_mtrr_type(unsigned t)
873 return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
876 static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
880 if (!msr_mtrr_valid(msr))
883 if (msr == MSR_IA32_CR_PAT) {
884 for (i = 0; i < 8; i++)
885 if (!valid_pat_type((data >> (i * 8)) & 0xff))
888 } else if (msr == MSR_MTRRdefType) {
891 return valid_mtrr_type(data & 0xff);
892 } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
893 for (i = 0; i < 8 ; i++)
894 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
900 return valid_mtrr_type(data & 0xff);
903 static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
905 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
907 if (!mtrr_valid(vcpu, msr, data))
910 if (msr == MSR_MTRRdefType) {
911 vcpu->arch.mtrr_state.def_type = data;
912 vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
913 } else if (msr == MSR_MTRRfix64K_00000)
915 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
916 p[1 + msr - MSR_MTRRfix16K_80000] = data;
917 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
918 p[3 + msr - MSR_MTRRfix4K_C0000] = data;
919 else if (msr == MSR_IA32_CR_PAT)
920 vcpu->arch.pat = data;
921 else { /* Variable MTRRs */
922 int idx, is_mtrr_mask;
925 idx = (msr - 0x200) / 2;
926 is_mtrr_mask = msr - 0x200 - 2 * idx;
929 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
932 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
936 kvm_mmu_reset_context(vcpu);
940 static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
942 u64 mcg_cap = vcpu->arch.mcg_cap;
943 unsigned bank_num = mcg_cap & 0xff;
946 case MSR_IA32_MCG_STATUS:
947 vcpu->arch.mcg_status = data;
949 case MSR_IA32_MCG_CTL:
950 if (!(mcg_cap & MCG_CTL_P))
952 if (data != 0 && data != ~(u64)0)
954 vcpu->arch.mcg_ctl = data;
957 if (msr >= MSR_IA32_MC0_CTL &&
958 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
959 u32 offset = msr - MSR_IA32_MC0_CTL;
960 /* only 0 or all 1s can be written to IA32_MCi_CTL */
961 if ((offset & 0x3) == 0 &&
962 data != 0 && data != ~(u64)0)
964 vcpu->arch.mce_banks[offset] = data;
972 static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
974 struct kvm *kvm = vcpu->kvm;
975 int lm = is_long_mode(vcpu);
976 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
977 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
978 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
979 : kvm->arch.xen_hvm_config.blob_size_32;
980 u32 page_num = data & ~PAGE_MASK;
981 u64 page_addr = data & PAGE_MASK;
986 if (page_num >= blob_size)
989 page = kzalloc(PAGE_SIZE, GFP_KERNEL);
993 if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
995 if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
1004 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
1008 set_efer(vcpu, data);
1011 data &= ~(u64)0x40; /* ignore flush filter disable */
1013 pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
1018 case MSR_FAM10H_MMIO_CONF_BASE:
1020 pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
1025 case MSR_AMD64_NB_CFG:
1027 case MSR_IA32_DEBUGCTLMSR:
1029 /* We support the non-activated case already */
1031 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
1032 /* Values other than LBR and BTF are vendor-specific,
1033 thus reserved and should throw a #GP */
1036 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1039 case MSR_IA32_UCODE_REV:
1040 case MSR_IA32_UCODE_WRITE:
1041 case MSR_VM_HSAVE_PA:
1042 case MSR_AMD64_PATCH_LOADER:
1044 case 0x200 ... 0x2ff:
1045 return set_msr_mtrr(vcpu, msr, data);
1046 case MSR_IA32_APICBASE:
1047 kvm_set_apic_base(vcpu, data);
1049 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1050 return kvm_x2apic_msr_write(vcpu, msr, data);
1051 case MSR_IA32_MISC_ENABLE:
1052 vcpu->arch.ia32_misc_enable_msr = data;
1054 case MSR_KVM_WALL_CLOCK:
1055 vcpu->kvm->arch.wall_clock = data;
1056 kvm_write_wall_clock(vcpu->kvm, data);
1058 case MSR_KVM_SYSTEM_TIME: {
1059 if (vcpu->arch.time_page) {
1060 kvm_release_page_dirty(vcpu->arch.time_page);
1061 vcpu->arch.time_page = NULL;
1064 vcpu->arch.time = data;
1066 /* we verify if the enable bit is set... */
1070 /* ...but clean it before doing the actual write */
1071 vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
1073 vcpu->arch.time_page =
1074 gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
1076 if (is_error_page(vcpu->arch.time_page)) {
1077 kvm_release_page_clean(vcpu->arch.time_page);
1078 vcpu->arch.time_page = NULL;
1081 kvm_request_guest_time_update(vcpu);
1084 case MSR_IA32_MCG_CTL:
1085 case MSR_IA32_MCG_STATUS:
1086 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1087 return set_msr_mce(vcpu, msr, data);
1089 /* Performance counters are not protected by a CPUID bit,
1090 * so we should check all of them in the generic path for the sake of
1091 * cross vendor migration.
1092 * Writing a zero into the event select MSRs disables them,
1093 * which we perfectly emulate ;-). Any other value should be at least
1094 * reported, some guests depend on them.
1096 case MSR_P6_EVNTSEL0:
1097 case MSR_P6_EVNTSEL1:
1098 case MSR_K7_EVNTSEL0:
1099 case MSR_K7_EVNTSEL1:
1100 case MSR_K7_EVNTSEL2:
1101 case MSR_K7_EVNTSEL3:
1103 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1104 "0x%x data 0x%llx\n", msr, data);
1106 /* at least RHEL 4 unconditionally writes to the perfctr registers,
1107 * so we ignore writes to make it happy.
1109 case MSR_P6_PERFCTR0:
1110 case MSR_P6_PERFCTR1:
1111 case MSR_K7_PERFCTR0:
1112 case MSR_K7_PERFCTR1:
1113 case MSR_K7_PERFCTR2:
1114 case MSR_K7_PERFCTR3:
1115 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
1116 "0x%x data 0x%llx\n", msr, data);
1119 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
1120 return xen_hvm_config(vcpu, data);
1122 pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
1126 pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
1133 EXPORT_SYMBOL_GPL(kvm_set_msr_common);
1137 * Reads an msr value (of 'msr_index') into 'pdata'.
1138 * Returns 0 on success, non-0 otherwise.
1139 * Assumes vcpu_load() was already called.
1141 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1143 return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
1146 static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1148 u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
1150 if (!msr_mtrr_valid(msr))
1153 if (msr == MSR_MTRRdefType)
1154 *pdata = vcpu->arch.mtrr_state.def_type +
1155 (vcpu->arch.mtrr_state.enabled << 10);
1156 else if (msr == MSR_MTRRfix64K_00000)
1158 else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
1159 *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
1160 else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
1161 *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
1162 else if (msr == MSR_IA32_CR_PAT)
1163 *pdata = vcpu->arch.pat;
1164 else { /* Variable MTRRs */
1165 int idx, is_mtrr_mask;
1168 idx = (msr - 0x200) / 2;
1169 is_mtrr_mask = msr - 0x200 - 2 * idx;
1172 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
1175 (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
1182 static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1185 u64 mcg_cap = vcpu->arch.mcg_cap;
1186 unsigned bank_num = mcg_cap & 0xff;
1189 case MSR_IA32_P5_MC_ADDR:
1190 case MSR_IA32_P5_MC_TYPE:
1193 case MSR_IA32_MCG_CAP:
1194 data = vcpu->arch.mcg_cap;
1196 case MSR_IA32_MCG_CTL:
1197 if (!(mcg_cap & MCG_CTL_P))
1199 data = vcpu->arch.mcg_ctl;
1201 case MSR_IA32_MCG_STATUS:
1202 data = vcpu->arch.mcg_status;
1205 if (msr >= MSR_IA32_MC0_CTL &&
1206 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1207 u32 offset = msr - MSR_IA32_MC0_CTL;
1208 data = vcpu->arch.mce_banks[offset];
1217 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1222 case MSR_IA32_PLATFORM_ID:
1223 case MSR_IA32_UCODE_REV:
1224 case MSR_IA32_EBL_CR_POWERON:
1225 case MSR_IA32_DEBUGCTLMSR:
1226 case MSR_IA32_LASTBRANCHFROMIP:
1227 case MSR_IA32_LASTBRANCHTOIP:
1228 case MSR_IA32_LASTINTFROMIP:
1229 case MSR_IA32_LASTINTTOIP:
1232 case MSR_VM_HSAVE_PA:
1233 case MSR_P6_PERFCTR0:
1234 case MSR_P6_PERFCTR1:
1235 case MSR_P6_EVNTSEL0:
1236 case MSR_P6_EVNTSEL1:
1237 case MSR_K7_EVNTSEL0:
1238 case MSR_K7_PERFCTR0:
1239 case MSR_K8_INT_PENDING_MSG:
1240 case MSR_AMD64_NB_CFG:
1241 case MSR_FAM10H_MMIO_CONF_BASE:
1245 data = 0x500 | KVM_NR_VAR_MTRR;
1247 case 0x200 ... 0x2ff:
1248 return get_msr_mtrr(vcpu, msr, pdata);
1249 case 0xcd: /* fsb frequency */
1252 case MSR_IA32_APICBASE:
1253 data = kvm_get_apic_base(vcpu);
1255 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
1256 return kvm_x2apic_msr_read(vcpu, msr, pdata);
1258 case MSR_IA32_MISC_ENABLE:
1259 data = vcpu->arch.ia32_misc_enable_msr;
1261 case MSR_IA32_PERF_STATUS:
1262 /* TSC increment by tick */
1264 /* CPU multiplier */
1265 data |= (((uint64_t)4ULL) << 40);
1268 data = vcpu->arch.shadow_efer;
1270 case MSR_KVM_WALL_CLOCK:
1271 data = vcpu->kvm->arch.wall_clock;
1273 case MSR_KVM_SYSTEM_TIME:
1274 data = vcpu->arch.time;
1276 case MSR_IA32_P5_MC_ADDR:
1277 case MSR_IA32_P5_MC_TYPE:
1278 case MSR_IA32_MCG_CAP:
1279 case MSR_IA32_MCG_CTL:
1280 case MSR_IA32_MCG_STATUS:
1281 case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
1282 return get_msr_mce(vcpu, msr, pdata);
1285 pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
1288 pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
1296 EXPORT_SYMBOL_GPL(kvm_get_msr_common);
1299 * Read or write a bunch of msrs. All parameters are kernel addresses.
1301 * @return number of msrs set successfully.
1303 static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
1304 struct kvm_msr_entry *entries,
1305 int (*do_msr)(struct kvm_vcpu *vcpu,
1306 unsigned index, u64 *data))
1312 down_read(&vcpu->kvm->slots_lock);
1313 for (i = 0; i < msrs->nmsrs; ++i)
1314 if (do_msr(vcpu, entries[i].index, &entries[i].data))
1316 up_read(&vcpu->kvm->slots_lock);
1324 * Read or write a bunch of msrs. Parameters are user addresses.
1326 * @return number of msrs set successfully.
1328 static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
1329 int (*do_msr)(struct kvm_vcpu *vcpu,
1330 unsigned index, u64 *data),
1333 struct kvm_msrs msrs;
1334 struct kvm_msr_entry *entries;
1339 if (copy_from_user(&msrs, user_msrs, sizeof msrs))
1343 if (msrs.nmsrs >= MAX_IO_MSRS)
1347 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
1348 entries = vmalloc(size);
1353 if (copy_from_user(entries, user_msrs->entries, size))
1356 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
1361 if (writeback && copy_to_user(user_msrs->entries, entries, size))
1372 int kvm_dev_ioctl_check_extension(long ext)
1377 case KVM_CAP_IRQCHIP:
1379 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
1380 case KVM_CAP_SET_TSS_ADDR:
1381 case KVM_CAP_EXT_CPUID:
1382 case KVM_CAP_CLOCKSOURCE:
1384 case KVM_CAP_NOP_IO_DELAY:
1385 case KVM_CAP_MP_STATE:
1386 case KVM_CAP_SYNC_MMU:
1387 case KVM_CAP_REINJECT_CONTROL:
1388 case KVM_CAP_IRQ_INJECT_STATUS:
1389 case KVM_CAP_ASSIGN_DEV_IRQ:
1391 case KVM_CAP_IOEVENTFD:
1393 case KVM_CAP_PIT_STATE2:
1394 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
1395 case KVM_CAP_XEN_HVM:
1396 case KVM_CAP_ADJUST_CLOCK:
1397 case KVM_CAP_VCPU_EVENTS:
1400 case KVM_CAP_COALESCED_MMIO:
1401 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
1404 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
1406 case KVM_CAP_NR_VCPUS:
1409 case KVM_CAP_NR_MEMSLOTS:
1410 r = KVM_MEMORY_SLOTS;
1412 case KVM_CAP_PV_MMU: /* obsolete */
1419 r = KVM_MAX_MCE_BANKS;
1429 long kvm_arch_dev_ioctl(struct file *filp,
1430 unsigned int ioctl, unsigned long arg)
1432 void __user *argp = (void __user *)arg;
1436 case KVM_GET_MSR_INDEX_LIST: {
1437 struct kvm_msr_list __user *user_msr_list = argp;
1438 struct kvm_msr_list msr_list;
1442 if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
1445 msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
1446 if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
1449 if (n < msr_list.nmsrs)
1452 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
1453 num_msrs_to_save * sizeof(u32)))
1455 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
1457 ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
1462 case KVM_GET_SUPPORTED_CPUID: {
1463 struct kvm_cpuid2 __user *cpuid_arg = argp;
1464 struct kvm_cpuid2 cpuid;
1467 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
1469 r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
1470 cpuid_arg->entries);
1475 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
1480 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
1483 mce_cap = KVM_MCE_CAP_SUPPORTED;
1485 if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
1497 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1499 kvm_x86_ops->vcpu_load(vcpu, cpu);
1500 if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
1501 unsigned long khz = cpufreq_quick_get(cpu);
1504 per_cpu(cpu_tsc_khz, cpu) = khz;
1506 kvm_request_guest_time_update(vcpu);
1509 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1511 kvm_x86_ops->vcpu_put(vcpu);
1512 kvm_put_guest_fpu(vcpu);
1515 static int is_efer_nx(void)
1517 unsigned long long efer = 0;
1519 rdmsrl_safe(MSR_EFER, &efer);
1520 return efer & EFER_NX;
1523 static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
1526 struct kvm_cpuid_entry2 *e, *entry;
1529 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
1530 e = &vcpu->arch.cpuid_entries[i];
1531 if (e->function == 0x80000001) {
1536 if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
1537 entry->edx &= ~(1 << 20);
1538 printk(KERN_INFO "kvm: guest NX capability removed\n");
1542 /* when an old userspace process fills a new kernel module */
1543 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
1544 struct kvm_cpuid *cpuid,
1545 struct kvm_cpuid_entry __user *entries)
1548 struct kvm_cpuid_entry *cpuid_entries;
1551 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1554 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
1558 if (copy_from_user(cpuid_entries, entries,
1559 cpuid->nent * sizeof(struct kvm_cpuid_entry)))
1561 for (i = 0; i < cpuid->nent; i++) {
1562 vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
1563 vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
1564 vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
1565 vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
1566 vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
1567 vcpu->arch.cpuid_entries[i].index = 0;
1568 vcpu->arch.cpuid_entries[i].flags = 0;
1569 vcpu->arch.cpuid_entries[i].padding[0] = 0;
1570 vcpu->arch.cpuid_entries[i].padding[1] = 0;
1571 vcpu->arch.cpuid_entries[i].padding[2] = 0;
1573 vcpu->arch.cpuid_nent = cpuid->nent;
1574 cpuid_fix_nx_cap(vcpu);
1576 kvm_apic_set_version(vcpu);
1577 kvm_x86_ops->cpuid_update(vcpu);
1580 vfree(cpuid_entries);
1585 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
1586 struct kvm_cpuid2 *cpuid,
1587 struct kvm_cpuid_entry2 __user *entries)
1592 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1595 if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
1596 cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
1598 vcpu->arch.cpuid_nent = cpuid->nent;
1599 kvm_apic_set_version(vcpu);
1600 kvm_x86_ops->cpuid_update(vcpu);
1607 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
1608 struct kvm_cpuid2 *cpuid,
1609 struct kvm_cpuid_entry2 __user *entries)
1614 if (cpuid->nent < vcpu->arch.cpuid_nent)
1617 if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
1618 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
1623 cpuid->nent = vcpu->arch.cpuid_nent;
1627 static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1630 entry->function = function;
1631 entry->index = index;
1632 cpuid_count(entry->function, entry->index,
1633 &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
1637 #define F(x) bit(X86_FEATURE_##x)
1639 static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
1640 u32 index, int *nent, int maxnent)
1642 unsigned f_nx = is_efer_nx() ? F(NX) : 0;
1643 unsigned f_gbpages = kvm_x86_ops->gb_page_enable() ? F(GBPAGES) : 0;
1644 #ifdef CONFIG_X86_64
1645 unsigned f_lm = F(LM);
1651 const u32 kvm_supported_word0_x86_features =
1652 F(FPU) | F(VME) | F(DE) | F(PSE) |
1653 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1654 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
1655 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1656 F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
1657 0 /* Reserved, DS, ACPI */ | F(MMX) |
1658 F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
1659 0 /* HTT, TM, Reserved, PBE */;
1660 /* cpuid 0x80000001.edx */
1661 const u32 kvm_supported_word1_x86_features =
1662 F(FPU) | F(VME) | F(DE) | F(PSE) |
1663 F(TSC) | F(MSR) | F(PAE) | F(MCE) |
1664 F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
1665 F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
1666 F(PAT) | F(PSE36) | 0 /* Reserved */ |
1667 f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
1668 F(FXSR) | F(FXSR_OPT) | f_gbpages | 0 /* RDTSCP */ |
1669 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
1671 const u32 kvm_supported_word4_x86_features =
1672 F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
1673 0 /* DS-CPL, VMX, SMX, EST */ |
1674 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1675 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
1676 0 /* Reserved, DCA */ | F(XMM4_1) |
1677 F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
1678 0 /* Reserved, XSAVE, OSXSAVE */;
1679 /* cpuid 0x80000001.ecx */
1680 const u32 kvm_supported_word6_x86_features =
1681 F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
1682 F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
1683 F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
1684 0 /* SKINIT */ | 0 /* WDT */;
1686 /* all calls to cpuid_count() should be made on the same cpu */
1688 do_cpuid_1_ent(entry, function, index);
1693 entry->eax = min(entry->eax, (u32)0xb);
1696 entry->edx &= kvm_supported_word0_x86_features;
1697 entry->ecx &= kvm_supported_word4_x86_features;
1698 /* we support x2apic emulation even if host does not support
1699 * it since we emulate x2apic in software */
1700 entry->ecx |= F(X2APIC);
1702 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1703 * may return different values. This forces us to get_cpu() before
1704 * issuing the first command, and also to emulate this annoying behavior
1705 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1707 int t, times = entry->eax & 0xff;
1709 entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1710 entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
1711 for (t = 1; t < times && *nent < maxnent; ++t) {
1712 do_cpuid_1_ent(&entry[t], function, 0);
1713 entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
1718 /* function 4 and 0xb have additional index. */
1722 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1723 /* read more entries until cache_type is zero */
1724 for (i = 1; *nent < maxnent; ++i) {
1725 cache_type = entry[i - 1].eax & 0x1f;
1728 do_cpuid_1_ent(&entry[i], function, i);
1730 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1738 entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1739 /* read more entries until level_type is zero */
1740 for (i = 1; *nent < maxnent; ++i) {
1741 level_type = entry[i - 1].ecx & 0xff00;
1744 do_cpuid_1_ent(&entry[i], function, i);
1746 KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
1752 entry->eax = min(entry->eax, 0x8000001a);
1755 entry->edx &= kvm_supported_word1_x86_features;
1756 entry->ecx &= kvm_supported_word6_x86_features;
1764 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
1765 struct kvm_cpuid_entry2 __user *entries)
1767 struct kvm_cpuid_entry2 *cpuid_entries;
1768 int limit, nent = 0, r = -E2BIG;
1771 if (cpuid->nent < 1)
1773 if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
1774 cpuid->nent = KVM_MAX_CPUID_ENTRIES;
1776 cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
1780 do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
1781 limit = cpuid_entries[0].eax;
1782 for (func = 1; func <= limit && nent < cpuid->nent; ++func)
1783 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1784 &nent, cpuid->nent);
1786 if (nent >= cpuid->nent)
1789 do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
1790 limit = cpuid_entries[nent - 1].eax;
1791 for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
1792 do_cpuid_ent(&cpuid_entries[nent], func, 0,
1793 &nent, cpuid->nent);
1795 if (nent >= cpuid->nent)
1799 if (copy_to_user(entries, cpuid_entries,
1800 nent * sizeof(struct kvm_cpuid_entry2)))
1806 vfree(cpuid_entries);
1811 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
1812 struct kvm_lapic_state *s)
1815 memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
1821 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
1822 struct kvm_lapic_state *s)
1825 memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
1826 kvm_apic_post_state_restore(vcpu);
1827 update_cr8_intercept(vcpu);
1833 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
1834 struct kvm_interrupt *irq)
1836 if (irq->irq < 0 || irq->irq >= 256)
1838 if (irqchip_in_kernel(vcpu->kvm))
1842 kvm_queue_interrupt(vcpu, irq->irq, false);
1849 static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
1852 kvm_inject_nmi(vcpu);
1858 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
1859 struct kvm_tpr_access_ctl *tac)
1863 vcpu->arch.tpr_access_reporting = !!tac->enabled;
1867 static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
1871 unsigned bank_num = mcg_cap & 0xff, bank;
1874 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
1876 if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
1879 vcpu->arch.mcg_cap = mcg_cap;
1880 /* Init IA32_MCG_CTL to all 1s */
1881 if (mcg_cap & MCG_CTL_P)
1882 vcpu->arch.mcg_ctl = ~(u64)0;
1883 /* Init IA32_MCi_CTL to all 1s */
1884 for (bank = 0; bank < bank_num; bank++)
1885 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
1890 static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
1891 struct kvm_x86_mce *mce)
1893 u64 mcg_cap = vcpu->arch.mcg_cap;
1894 unsigned bank_num = mcg_cap & 0xff;
1895 u64 *banks = vcpu->arch.mce_banks;
1897 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
1900 * if IA32_MCG_CTL is not all 1s, the uncorrected error
1901 * reporting is disabled
1903 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
1904 vcpu->arch.mcg_ctl != ~(u64)0)
1906 banks += 4 * mce->bank;
1908 * if IA32_MCi_CTL is not all 1s, the uncorrected error
1909 * reporting is disabled for the bank
1911 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
1913 if (mce->status & MCI_STATUS_UC) {
1914 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
1915 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
1916 printk(KERN_DEBUG "kvm: set_mce: "
1917 "injects mce exception while "
1918 "previous one is in progress!\n");
1919 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1922 if (banks[1] & MCI_STATUS_VAL)
1923 mce->status |= MCI_STATUS_OVER;
1924 banks[2] = mce->addr;
1925 banks[3] = mce->misc;
1926 vcpu->arch.mcg_status = mce->mcg_status;
1927 banks[1] = mce->status;
1928 kvm_queue_exception(vcpu, MC_VECTOR);
1929 } else if (!(banks[1] & MCI_STATUS_VAL)
1930 || !(banks[1] & MCI_STATUS_UC)) {
1931 if (banks[1] & MCI_STATUS_VAL)
1932 mce->status |= MCI_STATUS_OVER;
1933 banks[2] = mce->addr;
1934 banks[3] = mce->misc;
1935 banks[1] = mce->status;
1937 banks[1] |= MCI_STATUS_OVER;
1941 static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
1942 struct kvm_vcpu_events *events)
1946 events->exception.injected = vcpu->arch.exception.pending;
1947 events->exception.nr = vcpu->arch.exception.nr;
1948 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
1949 events->exception.error_code = vcpu->arch.exception.error_code;
1951 events->interrupt.injected = vcpu->arch.interrupt.pending;
1952 events->interrupt.nr = vcpu->arch.interrupt.nr;
1953 events->interrupt.soft = vcpu->arch.interrupt.soft;
1955 events->nmi.injected = vcpu->arch.nmi_injected;
1956 events->nmi.pending = vcpu->arch.nmi_pending;
1957 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
1959 events->sipi_vector = vcpu->arch.sipi_vector;
1961 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
1962 | KVM_VCPUEVENT_VALID_SIPI_VECTOR);
1967 static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
1968 struct kvm_vcpu_events *events)
1970 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
1971 | KVM_VCPUEVENT_VALID_SIPI_VECTOR))
1976 vcpu->arch.exception.pending = events->exception.injected;
1977 vcpu->arch.exception.nr = events->exception.nr;
1978 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
1979 vcpu->arch.exception.error_code = events->exception.error_code;
1981 vcpu->arch.interrupt.pending = events->interrupt.injected;
1982 vcpu->arch.interrupt.nr = events->interrupt.nr;
1983 vcpu->arch.interrupt.soft = events->interrupt.soft;
1984 if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
1985 kvm_pic_clear_isr_ack(vcpu->kvm);
1987 vcpu->arch.nmi_injected = events->nmi.injected;
1988 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
1989 vcpu->arch.nmi_pending = events->nmi.pending;
1990 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
1992 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
1993 vcpu->arch.sipi_vector = events->sipi_vector;
2000 long kvm_arch_vcpu_ioctl(struct file *filp,
2001 unsigned int ioctl, unsigned long arg)
2003 struct kvm_vcpu *vcpu = filp->private_data;
2004 void __user *argp = (void __user *)arg;
2006 struct kvm_lapic_state *lapic = NULL;
2009 case KVM_GET_LAPIC: {
2011 if (!vcpu->arch.apic)
2013 lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2018 r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
2022 if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
2027 case KVM_SET_LAPIC: {
2029 if (!vcpu->arch.apic)
2031 lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
2036 if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
2038 r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
2044 case KVM_INTERRUPT: {
2045 struct kvm_interrupt irq;
2048 if (copy_from_user(&irq, argp, sizeof irq))
2050 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2057 r = kvm_vcpu_ioctl_nmi(vcpu);
2063 case KVM_SET_CPUID: {
2064 struct kvm_cpuid __user *cpuid_arg = argp;
2065 struct kvm_cpuid cpuid;
2068 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2070 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
2075 case KVM_SET_CPUID2: {
2076 struct kvm_cpuid2 __user *cpuid_arg = argp;
2077 struct kvm_cpuid2 cpuid;
2080 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2082 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
2083 cpuid_arg->entries);
2088 case KVM_GET_CPUID2: {
2089 struct kvm_cpuid2 __user *cpuid_arg = argp;
2090 struct kvm_cpuid2 cpuid;
2093 if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
2095 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
2096 cpuid_arg->entries);
2100 if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
2106 r = msr_io(vcpu, argp, kvm_get_msr, 1);
2109 r = msr_io(vcpu, argp, do_set_msr, 0);
2111 case KVM_TPR_ACCESS_REPORTING: {
2112 struct kvm_tpr_access_ctl tac;
2115 if (copy_from_user(&tac, argp, sizeof tac))
2117 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2121 if (copy_to_user(argp, &tac, sizeof tac))
2126 case KVM_SET_VAPIC_ADDR: {
2127 struct kvm_vapic_addr va;
2130 if (!irqchip_in_kernel(vcpu->kvm))
2133 if (copy_from_user(&va, argp, sizeof va))
2136 kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
2139 case KVM_X86_SETUP_MCE: {
2143 if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
2145 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2148 case KVM_X86_SET_MCE: {
2149 struct kvm_x86_mce mce;
2152 if (copy_from_user(&mce, argp, sizeof mce))
2154 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2157 case KVM_GET_VCPU_EVENTS: {
2158 struct kvm_vcpu_events events;
2160 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2163 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
2168 case KVM_SET_VCPU_EVENTS: {
2169 struct kvm_vcpu_events events;
2172 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
2175 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2186 static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
2190 if (addr > (unsigned int)(-3 * PAGE_SIZE))
2192 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
2196 static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
2199 kvm->arch.ept_identity_map_addr = ident_addr;
2203 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
2204 u32 kvm_nr_mmu_pages)
2206 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
2209 down_write(&kvm->slots_lock);
2210 spin_lock(&kvm->mmu_lock);
2212 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
2213 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
2215 spin_unlock(&kvm->mmu_lock);
2216 up_write(&kvm->slots_lock);
2220 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
2222 return kvm->arch.n_alloc_mmu_pages;
2225 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
2228 struct kvm_mem_alias *alias;
2230 for (i = 0; i < kvm->arch.naliases; ++i) {
2231 alias = &kvm->arch.aliases[i];
2232 if (gfn >= alias->base_gfn
2233 && gfn < alias->base_gfn + alias->npages)
2234 return alias->target_gfn + gfn - alias->base_gfn;
2240 * Set a new alias region. Aliases map a portion of physical memory into
2241 * another portion. This is useful for memory windows, for example the PC
2244 static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
2245 struct kvm_memory_alias *alias)
2248 struct kvm_mem_alias *p;
2251 /* General sanity checks */
2252 if (alias->memory_size & (PAGE_SIZE - 1))
2254 if (alias->guest_phys_addr & (PAGE_SIZE - 1))
2256 if (alias->slot >= KVM_ALIAS_SLOTS)
2258 if (alias->guest_phys_addr + alias->memory_size
2259 < alias->guest_phys_addr)
2261 if (alias->target_phys_addr + alias->memory_size
2262 < alias->target_phys_addr)
2265 down_write(&kvm->slots_lock);
2266 spin_lock(&kvm->mmu_lock);
2268 p = &kvm->arch.aliases[alias->slot];
2269 p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
2270 p->npages = alias->memory_size >> PAGE_SHIFT;
2271 p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
2273 for (n = KVM_ALIAS_SLOTS; n > 0; --n)
2274 if (kvm->arch.aliases[n - 1].npages)
2276 kvm->arch.naliases = n;
2278 spin_unlock(&kvm->mmu_lock);
2279 kvm_mmu_zap_all(kvm);
2281 up_write(&kvm->slots_lock);
2289 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2294 switch (chip->chip_id) {
2295 case KVM_IRQCHIP_PIC_MASTER:
2296 memcpy(&chip->chip.pic,
2297 &pic_irqchip(kvm)->pics[0],
2298 sizeof(struct kvm_pic_state));
2300 case KVM_IRQCHIP_PIC_SLAVE:
2301 memcpy(&chip->chip.pic,
2302 &pic_irqchip(kvm)->pics[1],
2303 sizeof(struct kvm_pic_state));
2305 case KVM_IRQCHIP_IOAPIC:
2306 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
2315 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
2320 switch (chip->chip_id) {
2321 case KVM_IRQCHIP_PIC_MASTER:
2322 spin_lock(&pic_irqchip(kvm)->lock);
2323 memcpy(&pic_irqchip(kvm)->pics[0],
2325 sizeof(struct kvm_pic_state));
2326 spin_unlock(&pic_irqchip(kvm)->lock);
2328 case KVM_IRQCHIP_PIC_SLAVE:
2329 spin_lock(&pic_irqchip(kvm)->lock);
2330 memcpy(&pic_irqchip(kvm)->pics[1],
2332 sizeof(struct kvm_pic_state));
2333 spin_unlock(&pic_irqchip(kvm)->lock);
2335 case KVM_IRQCHIP_IOAPIC:
2336 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
2342 kvm_pic_update_irq(pic_irqchip(kvm));
2346 static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2350 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2351 memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
2352 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2356 static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
2360 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2361 memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
2362 kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
2363 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2367 static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2371 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2372 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
2373 sizeof(ps->channels));
2374 ps->flags = kvm->arch.vpit->pit_state.flags;
2375 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2379 static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
2381 int r = 0, start = 0;
2382 u32 prev_legacy, cur_legacy;
2383 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2384 prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
2385 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
2386 if (!prev_legacy && cur_legacy)
2388 memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
2389 sizeof(kvm->arch.vpit->pit_state.channels));
2390 kvm->arch.vpit->pit_state.flags = ps->flags;
2391 kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
2392 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2396 static int kvm_vm_ioctl_reinject(struct kvm *kvm,
2397 struct kvm_reinject_control *control)
2399 if (!kvm->arch.vpit)
2401 mutex_lock(&kvm->arch.vpit->pit_state.lock);
2402 kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
2403 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
2408 * Get (and clear) the dirty memory log for a memory slot.
2410 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
2411 struct kvm_dirty_log *log)
2415 struct kvm_memory_slot *memslot;
2418 down_write(&kvm->slots_lock);
2420 r = kvm_get_dirty_log(kvm, log, &is_dirty);
2424 /* If nothing is dirty, don't bother messing with page tables. */
2426 spin_lock(&kvm->mmu_lock);
2427 kvm_mmu_slot_remove_write_access(kvm, log->slot);
2428 spin_unlock(&kvm->mmu_lock);
2429 memslot = &kvm->memslots[log->slot];
2430 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
2431 memset(memslot->dirty_bitmap, 0, n);
2435 up_write(&kvm->slots_lock);
2439 long kvm_arch_vm_ioctl(struct file *filp,
2440 unsigned int ioctl, unsigned long arg)
2442 struct kvm *kvm = filp->private_data;
2443 void __user *argp = (void __user *)arg;
2446 * This union makes it completely explicit to gcc-3.x
2447 * that these two variables' stack usage should be
2448 * combined, not added together.
2451 struct kvm_pit_state ps;
2452 struct kvm_pit_state2 ps2;
2453 struct kvm_memory_alias alias;
2454 struct kvm_pit_config pit_config;
2458 case KVM_SET_TSS_ADDR:
2459 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
2463 case KVM_SET_IDENTITY_MAP_ADDR: {
2467 if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
2469 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
2474 case KVM_SET_MEMORY_REGION: {
2475 struct kvm_memory_region kvm_mem;
2476 struct kvm_userspace_memory_region kvm_userspace_mem;
2479 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
2481 kvm_userspace_mem.slot = kvm_mem.slot;
2482 kvm_userspace_mem.flags = kvm_mem.flags;
2483 kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
2484 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
2485 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
2490 case KVM_SET_NR_MMU_PAGES:
2491 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
2495 case KVM_GET_NR_MMU_PAGES:
2496 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
2498 case KVM_SET_MEMORY_ALIAS:
2500 if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
2502 r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
2506 case KVM_CREATE_IRQCHIP: {
2507 struct kvm_pic *vpic;
2509 mutex_lock(&kvm->lock);
2512 goto create_irqchip_unlock;
2514 vpic = kvm_create_pic(kvm);
2516 r = kvm_ioapic_init(kvm);
2519 goto create_irqchip_unlock;
2522 goto create_irqchip_unlock;
2524 kvm->arch.vpic = vpic;
2526 r = kvm_setup_default_irq_routing(kvm);
2528 mutex_lock(&kvm->irq_lock);
2529 kfree(kvm->arch.vpic);
2530 kfree(kvm->arch.vioapic);
2531 kvm->arch.vpic = NULL;
2532 kvm->arch.vioapic = NULL;
2533 mutex_unlock(&kvm->irq_lock);
2535 create_irqchip_unlock:
2536 mutex_unlock(&kvm->lock);
2539 case KVM_CREATE_PIT:
2540 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
2542 case KVM_CREATE_PIT2:
2544 if (copy_from_user(&u.pit_config, argp,
2545 sizeof(struct kvm_pit_config)))
2548 down_write(&kvm->slots_lock);
2551 goto create_pit_unlock;
2553 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
2557 up_write(&kvm->slots_lock);
2559 case KVM_IRQ_LINE_STATUS:
2560 case KVM_IRQ_LINE: {
2561 struct kvm_irq_level irq_event;
2564 if (copy_from_user(&irq_event, argp, sizeof irq_event))
2566 if (irqchip_in_kernel(kvm)) {
2568 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2569 irq_event.irq, irq_event.level);
2570 if (ioctl == KVM_IRQ_LINE_STATUS) {
2571 irq_event.status = status;
2572 if (copy_to_user(argp, &irq_event,
2580 case KVM_GET_IRQCHIP: {
2581 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
2582 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
2588 if (copy_from_user(chip, argp, sizeof *chip))
2589 goto get_irqchip_out;
2591 if (!irqchip_in_kernel(kvm))
2592 goto get_irqchip_out;
2593 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
2595 goto get_irqchip_out;
2597 if (copy_to_user(argp, chip, sizeof *chip))
2598 goto get_irqchip_out;
2606 case KVM_SET_IRQCHIP: {
2607 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
2608 struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
2614 if (copy_from_user(chip, argp, sizeof *chip))
2615 goto set_irqchip_out;
2617 if (!irqchip_in_kernel(kvm))
2618 goto set_irqchip_out;
2619 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
2621 goto set_irqchip_out;
2631 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
2634 if (!kvm->arch.vpit)
2636 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
2640 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
2647 if (copy_from_user(&u.ps, argp, sizeof u.ps))
2650 if (!kvm->arch.vpit)
2652 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
2658 case KVM_GET_PIT2: {
2660 if (!kvm->arch.vpit)
2662 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
2666 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
2671 case KVM_SET_PIT2: {
2673 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
2676 if (!kvm->arch.vpit)
2678 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
2684 case KVM_REINJECT_CONTROL: {
2685 struct kvm_reinject_control control;
2687 if (copy_from_user(&control, argp, sizeof(control)))
2689 r = kvm_vm_ioctl_reinject(kvm, &control);
2695 case KVM_XEN_HVM_CONFIG: {
2697 if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
2698 sizeof(struct kvm_xen_hvm_config)))
2701 if (kvm->arch.xen_hvm_config.flags)
2706 case KVM_SET_CLOCK: {
2707 struct timespec now;
2708 struct kvm_clock_data user_ns;
2713 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
2722 now_ns = timespec_to_ns(&now);
2723 delta = user_ns.clock - now_ns;
2724 kvm->arch.kvmclock_offset = delta;
2727 case KVM_GET_CLOCK: {
2728 struct timespec now;
2729 struct kvm_clock_data user_ns;
2733 now_ns = timespec_to_ns(&now);
2734 user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
2738 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
2751 static void kvm_init_msr_list(void)
2756 /* skip the first msrs in the list. KVM-specific */
2757 for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
2758 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
2761 msrs_to_save[j] = msrs_to_save[i];
2764 num_msrs_to_save = j;
2767 static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
2770 if (vcpu->arch.apic &&
2771 !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
2774 return kvm_io_bus_write(&vcpu->kvm->mmio_bus, addr, len, v);
2777 static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
2779 if (vcpu->arch.apic &&
2780 !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
2783 return kvm_io_bus_read(&vcpu->kvm->mmio_bus, addr, len, v);
2786 static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
2787 struct kvm_vcpu *vcpu)
2790 int r = X86EMUL_CONTINUE;
2793 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2794 unsigned offset = addr & (PAGE_SIZE-1);
2795 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
2798 if (gpa == UNMAPPED_GVA) {
2799 r = X86EMUL_PROPAGATE_FAULT;
2802 ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
2804 r = X86EMUL_UNHANDLEABLE;
2816 static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
2817 struct kvm_vcpu *vcpu)
2820 int r = X86EMUL_CONTINUE;
2823 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2824 unsigned offset = addr & (PAGE_SIZE-1);
2825 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
2828 if (gpa == UNMAPPED_GVA) {
2829 r = X86EMUL_PROPAGATE_FAULT;
2832 ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
2834 r = X86EMUL_UNHANDLEABLE;
2847 static int emulator_read_emulated(unsigned long addr,
2850 struct kvm_vcpu *vcpu)
2854 if (vcpu->mmio_read_completed) {
2855 memcpy(val, vcpu->mmio_data, bytes);
2856 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
2857 vcpu->mmio_phys_addr, *(u64 *)val);
2858 vcpu->mmio_read_completed = 0;
2859 return X86EMUL_CONTINUE;
2862 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2864 /* For APIC access vmexit */
2865 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2868 if (kvm_read_guest_virt(addr, val, bytes, vcpu)
2869 == X86EMUL_CONTINUE)
2870 return X86EMUL_CONTINUE;
2871 if (gpa == UNMAPPED_GVA)
2872 return X86EMUL_PROPAGATE_FAULT;
2876 * Is this MMIO handled locally?
2878 if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
2879 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
2880 return X86EMUL_CONTINUE;
2883 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
2885 vcpu->mmio_needed = 1;
2886 vcpu->mmio_phys_addr = gpa;
2887 vcpu->mmio_size = bytes;
2888 vcpu->mmio_is_write = 0;
2890 return X86EMUL_UNHANDLEABLE;
2893 int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
2894 const void *val, int bytes)
2898 ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
2901 kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
2905 static int emulator_write_emulated_onepage(unsigned long addr,
2908 struct kvm_vcpu *vcpu)
2912 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2914 if (gpa == UNMAPPED_GVA) {
2915 kvm_inject_page_fault(vcpu, addr, 2);
2916 return X86EMUL_PROPAGATE_FAULT;
2919 /* For APIC access vmexit */
2920 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2923 if (emulator_write_phys(vcpu, gpa, val, bytes))
2924 return X86EMUL_CONTINUE;
2927 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
2929 * Is this MMIO handled locally?
2931 if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
2932 return X86EMUL_CONTINUE;
2934 vcpu->mmio_needed = 1;
2935 vcpu->mmio_phys_addr = gpa;
2936 vcpu->mmio_size = bytes;
2937 vcpu->mmio_is_write = 1;
2938 memcpy(vcpu->mmio_data, val, bytes);
2940 return X86EMUL_CONTINUE;
2943 int emulator_write_emulated(unsigned long addr,
2946 struct kvm_vcpu *vcpu)
2948 /* Crossing a page boundary? */
2949 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
2952 now = -addr & ~PAGE_MASK;
2953 rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
2954 if (rc != X86EMUL_CONTINUE)
2960 return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
2962 EXPORT_SYMBOL_GPL(emulator_write_emulated);
2964 static int emulator_cmpxchg_emulated(unsigned long addr,
2968 struct kvm_vcpu *vcpu)
2970 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
2971 #ifndef CONFIG_X86_64
2972 /* guests cmpxchg8b have to be emulated atomically */
2979 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
2981 if (gpa == UNMAPPED_GVA ||
2982 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
2985 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
2990 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
2992 kaddr = kmap_atomic(page, KM_USER0);
2993 set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
2994 kunmap_atomic(kaddr, KM_USER0);
2995 kvm_release_page_dirty(page);
3000 return emulator_write_emulated(addr, new, bytes, vcpu);
3003 static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
3005 return kvm_x86_ops->get_segment_base(vcpu, seg);
3008 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
3010 kvm_mmu_invlpg(vcpu, address);
3011 return X86EMUL_CONTINUE;
3014 int emulate_clts(struct kvm_vcpu *vcpu)
3016 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
3017 return X86EMUL_CONTINUE;
3020 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
3022 struct kvm_vcpu *vcpu = ctxt->vcpu;
3026 *dest = kvm_x86_ops->get_dr(vcpu, dr);
3027 return X86EMUL_CONTINUE;
3029 pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
3030 return X86EMUL_UNHANDLEABLE;
3034 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
3036 unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
3039 kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
3041 /* FIXME: better handling */
3042 return X86EMUL_UNHANDLEABLE;
3044 return X86EMUL_CONTINUE;
3047 void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
3050 unsigned long rip = kvm_rip_read(vcpu);
3051 unsigned long rip_linear;
3053 if (!printk_ratelimit())
3056 rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
3058 kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu);
3060 printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
3061 context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
3063 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
3065 static struct x86_emulate_ops emulate_ops = {
3066 .read_std = kvm_read_guest_virt,
3067 .read_emulated = emulator_read_emulated,
3068 .write_emulated = emulator_write_emulated,
3069 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3072 static void cache_all_regs(struct kvm_vcpu *vcpu)
3074 kvm_register_read(vcpu, VCPU_REGS_RAX);
3075 kvm_register_read(vcpu, VCPU_REGS_RSP);
3076 kvm_register_read(vcpu, VCPU_REGS_RIP);
3077 vcpu->arch.regs_dirty = ~0;
3080 int emulate_instruction(struct kvm_vcpu *vcpu,
3086 struct decode_cache *c;
3087 struct kvm_run *run = vcpu->run;
3089 kvm_clear_exception_queue(vcpu);
3090 vcpu->arch.mmio_fault_cr2 = cr2;
3092 * TODO: fix emulate.c to use guest_read/write_register
3093 * instead of direct ->regs accesses, can save hundred cycles
3094 * on Intel for instructions that don't read/change RSP, for
3097 cache_all_regs(vcpu);
3099 vcpu->mmio_is_write = 0;
3100 vcpu->arch.pio.string = 0;
3102 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
3104 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
3106 vcpu->arch.emulate_ctxt.vcpu = vcpu;
3107 vcpu->arch.emulate_ctxt.eflags = kvm_get_rflags(vcpu);
3108 vcpu->arch.emulate_ctxt.mode =
3109 (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
3110 ? X86EMUL_MODE_REAL : cs_l
3111 ? X86EMUL_MODE_PROT64 : cs_db
3112 ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
3114 r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
3116 /* Only allow emulation of specific instructions on #UD
3117 * (namely VMMCALL, sysenter, sysexit, syscall)*/
3118 c = &vcpu->arch.emulate_ctxt.decode;
3119 if (emulation_type & EMULTYPE_TRAP_UD) {
3121 return EMULATE_FAIL;
3123 case 0x01: /* VMMCALL */
3124 if (c->modrm_mod != 3 || c->modrm_rm != 1)
3125 return EMULATE_FAIL;
3127 case 0x34: /* sysenter */
3128 case 0x35: /* sysexit */
3129 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3130 return EMULATE_FAIL;
3132 case 0x05: /* syscall */
3133 if (c->modrm_mod != 0 || c->modrm_rm != 0)
3134 return EMULATE_FAIL;
3137 return EMULATE_FAIL;
3140 if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
3141 return EMULATE_FAIL;
3144 ++vcpu->stat.insn_emulation;
3146 ++vcpu->stat.insn_emulation_fail;
3147 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3148 return EMULATE_DONE;
3149 return EMULATE_FAIL;
3153 if (emulation_type & EMULTYPE_SKIP) {
3154 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
3155 return EMULATE_DONE;
3158 r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
3159 shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
3162 kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
3164 if (vcpu->arch.pio.string)
3165 return EMULATE_DO_MMIO;
3167 if ((r || vcpu->mmio_is_write) && run) {
3168 run->exit_reason = KVM_EXIT_MMIO;
3169 run->mmio.phys_addr = vcpu->mmio_phys_addr;
3170 memcpy(run->mmio.data, vcpu->mmio_data, 8);
3171 run->mmio.len = vcpu->mmio_size;
3172 run->mmio.is_write = vcpu->mmio_is_write;
3176 if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
3177 return EMULATE_DONE;
3178 if (!vcpu->mmio_needed) {
3179 kvm_report_emulation_failure(vcpu, "mmio");
3180 return EMULATE_FAIL;
3182 return EMULATE_DO_MMIO;
3185 kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
3187 if (vcpu->mmio_is_write) {
3188 vcpu->mmio_needed = 0;
3189 return EMULATE_DO_MMIO;
3192 return EMULATE_DONE;
3194 EXPORT_SYMBOL_GPL(emulate_instruction);
3196 static int pio_copy_data(struct kvm_vcpu *vcpu)
3198 void *p = vcpu->arch.pio_data;
3199 gva_t q = vcpu->arch.pio.guest_gva;
3203 bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
3204 if (vcpu->arch.pio.in)
3205 ret = kvm_write_guest_virt(q, p, bytes, vcpu);
3207 ret = kvm_read_guest_virt(q, p, bytes, vcpu);
3211 int complete_pio(struct kvm_vcpu *vcpu)
3213 struct kvm_pio_request *io = &vcpu->arch.pio;
3220 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3221 memcpy(&val, vcpu->arch.pio_data, io->size);
3222 kvm_register_write(vcpu, VCPU_REGS_RAX, val);
3226 r = pio_copy_data(vcpu);
3233 delta *= io->cur_count;
3235 * The size of the register should really depend on
3236 * current address size.
3238 val = kvm_register_read(vcpu, VCPU_REGS_RCX);
3240 kvm_register_write(vcpu, VCPU_REGS_RCX, val);
3246 val = kvm_register_read(vcpu, VCPU_REGS_RDI);
3248 kvm_register_write(vcpu, VCPU_REGS_RDI, val);
3250 val = kvm_register_read(vcpu, VCPU_REGS_RSI);
3252 kvm_register_write(vcpu, VCPU_REGS_RSI, val);
3256 io->count -= io->cur_count;
3262 static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
3264 /* TODO: String I/O for in kernel device */
3267 if (vcpu->arch.pio.in)
3268 r = kvm_io_bus_read(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
3269 vcpu->arch.pio.size, pd);
3271 r = kvm_io_bus_write(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
3272 vcpu->arch.pio.size, pd);
3276 static int pio_string_write(struct kvm_vcpu *vcpu)
3278 struct kvm_pio_request *io = &vcpu->arch.pio;
3279 void *pd = vcpu->arch.pio_data;
3282 for (i = 0; i < io->cur_count; i++) {
3283 if (kvm_io_bus_write(&vcpu->kvm->pio_bus,
3284 io->port, io->size, pd)) {
3293 int kvm_emulate_pio(struct kvm_vcpu *vcpu, int in, int size, unsigned port)
3297 vcpu->run->exit_reason = KVM_EXIT_IO;
3298 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
3299 vcpu->run->io.size = vcpu->arch.pio.size = size;
3300 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3301 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
3302 vcpu->run->io.port = vcpu->arch.pio.port = port;
3303 vcpu->arch.pio.in = in;
3304 vcpu->arch.pio.string = 0;
3305 vcpu->arch.pio.down = 0;
3306 vcpu->arch.pio.rep = 0;
3308 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3311 val = kvm_register_read(vcpu, VCPU_REGS_RAX);
3312 memcpy(vcpu->arch.pio_data, &val, 4);
3314 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
3320 EXPORT_SYMBOL_GPL(kvm_emulate_pio);
3322 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, int in,
3323 int size, unsigned long count, int down,
3324 gva_t address, int rep, unsigned port)
3326 unsigned now, in_page;
3329 vcpu->run->exit_reason = KVM_EXIT_IO;
3330 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
3331 vcpu->run->io.size = vcpu->arch.pio.size = size;
3332 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
3333 vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
3334 vcpu->run->io.port = vcpu->arch.pio.port = port;
3335 vcpu->arch.pio.in = in;
3336 vcpu->arch.pio.string = 1;
3337 vcpu->arch.pio.down = down;
3338 vcpu->arch.pio.rep = rep;
3340 trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
3344 kvm_x86_ops->skip_emulated_instruction(vcpu);
3349 in_page = PAGE_SIZE - offset_in_page(address);
3351 in_page = offset_in_page(address) + size;
3352 now = min(count, (unsigned long)in_page / size);
3357 * String I/O in reverse. Yuck. Kill the guest, fix later.
3359 pr_unimpl(vcpu, "guest string pio down\n");
3360 kvm_inject_gp(vcpu, 0);
3363 vcpu->run->io.count = now;
3364 vcpu->arch.pio.cur_count = now;
3366 if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
3367 kvm_x86_ops->skip_emulated_instruction(vcpu);
3369 vcpu->arch.pio.guest_gva = address;
3371 if (!vcpu->arch.pio.in) {
3372 /* string PIO write */
3373 ret = pio_copy_data(vcpu);
3374 if (ret == X86EMUL_PROPAGATE_FAULT) {
3375 kvm_inject_gp(vcpu, 0);
3378 if (ret == 0 && !pio_string_write(vcpu)) {
3380 if (vcpu->arch.pio.count == 0)
3384 /* no string PIO read support yet */
3388 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
3390 static void bounce_off(void *info)
3395 static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
3398 struct cpufreq_freqs *freq = data;
3400 struct kvm_vcpu *vcpu;
3401 int i, send_ipi = 0;
3403 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
3405 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
3407 per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
3409 spin_lock(&kvm_lock);
3410 list_for_each_entry(kvm, &vm_list, vm_list) {
3411 kvm_for_each_vcpu(i, vcpu, kvm) {
3412 if (vcpu->cpu != freq->cpu)
3414 if (!kvm_request_guest_time_update(vcpu))
3416 if (vcpu->cpu != smp_processor_id())
3420 spin_unlock(&kvm_lock);
3422 if (freq->old < freq->new && send_ipi) {
3424 * We upscale the frequency. Must make the guest
3425 * doesn't see old kvmclock values while running with
3426 * the new frequency, otherwise we risk the guest sees
3427 * time go backwards.
3429 * In case we update the frequency for another cpu
3430 * (which might be in guest context) send an interrupt
3431 * to kick the cpu out of guest context. Next time
3432 * guest context is entered kvmclock will be updated,
3433 * so the guest will not see stale values.
3435 smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
3440 static struct notifier_block kvmclock_cpufreq_notifier_block = {
3441 .notifier_call = kvmclock_cpufreq_notifier
3444 static void kvm_timer_init(void)
3448 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
3449 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
3450 CPUFREQ_TRANSITION_NOTIFIER);
3451 for_each_online_cpu(cpu) {
3452 unsigned long khz = cpufreq_get(cpu);
3455 per_cpu(cpu_tsc_khz, cpu) = khz;
3458 for_each_possible_cpu(cpu)
3459 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
3463 int kvm_arch_init(void *opaque)
3466 struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
3469 printk(KERN_ERR "kvm: already loaded the other module\n");
3474 if (!ops->cpu_has_kvm_support()) {
3475 printk(KERN_ERR "kvm: no hardware support\n");
3479 if (ops->disabled_by_bios()) {
3480 printk(KERN_ERR "kvm: disabled by bios\n");
3485 r = kvm_mmu_module_init();
3489 kvm_init_msr_list();
3492 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
3493 kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
3494 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
3495 PT_DIRTY_MASK, PT64_NX_MASK, 0);
3505 void kvm_arch_exit(void)
3507 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
3508 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
3509 CPUFREQ_TRANSITION_NOTIFIER);
3511 kvm_mmu_module_exit();
3514 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
3516 ++vcpu->stat.halt_exits;
3517 if (irqchip_in_kernel(vcpu->kvm)) {
3518 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
3521 vcpu->run->exit_reason = KVM_EXIT_HLT;
3525 EXPORT_SYMBOL_GPL(kvm_emulate_halt);
3527 static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
3530 if (is_long_mode(vcpu))
3533 return a0 | ((gpa_t)a1 << 32);
3536 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
3538 unsigned long nr, a0, a1, a2, a3, ret;
3541 nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
3542 a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
3543 a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
3544 a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
3545 a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
3547 trace_kvm_hypercall(nr, a0, a1, a2, a3);
3549 if (!is_long_mode(vcpu)) {
3557 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
3563 case KVM_HC_VAPIC_POLL_IRQ:
3567 r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
3574 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
3575 ++vcpu->stat.hypercalls;
3578 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
3580 int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
3582 char instruction[3];
3584 unsigned long rip = kvm_rip_read(vcpu);
3588 * Blow out the MMU to ensure that no other VCPU has an active mapping
3589 * to ensure that the updated hypercall appears atomically across all
3592 kvm_mmu_zap_all(vcpu->kvm);
3594 kvm_x86_ops->patch_hypercall(vcpu, instruction);
3595 if (emulator_write_emulated(rip, instruction, 3, vcpu)
3596 != X86EMUL_CONTINUE)
3602 static u64 mk_cr_64(u64 curr_cr, u32 new_val)
3604 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
3607 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3609 struct descriptor_table dt = { limit, base };
3611 kvm_x86_ops->set_gdt(vcpu, &dt);
3614 void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
3616 struct descriptor_table dt = { limit, base };
3618 kvm_x86_ops->set_idt(vcpu, &dt);
3621 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
3622 unsigned long *rflags)
3624 kvm_lmsw(vcpu, msw);
3625 *rflags = kvm_get_rflags(vcpu);
3628 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
3630 unsigned long value;
3634 value = vcpu->arch.cr0;
3637 value = vcpu->arch.cr2;
3640 value = vcpu->arch.cr3;
3643 value = kvm_read_cr4(vcpu);
3646 value = kvm_get_cr8(vcpu);
3649 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3656 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
3657 unsigned long *rflags)
3661 kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
3662 *rflags = kvm_get_rflags(vcpu);
3665 vcpu->arch.cr2 = val;
3668 kvm_set_cr3(vcpu, val);
3671 kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
3674 kvm_set_cr8(vcpu, val & 0xfUL);
3677 vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
3681 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
3683 struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
3684 int j, nent = vcpu->arch.cpuid_nent;
3686 e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
3687 /* when no next entry is found, the current entry[i] is reselected */
3688 for (j = i + 1; ; j = (j + 1) % nent) {
3689 struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
3690 if (ej->function == e->function) {
3691 ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
3695 return 0; /* silence gcc, even though control never reaches here */
3698 /* find an entry with matching function, matching index (if needed), and that
3699 * should be read next (if it's stateful) */
3700 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
3701 u32 function, u32 index)
3703 if (e->function != function)
3705 if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
3707 if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
3708 !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
3713 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
3714 u32 function, u32 index)
3717 struct kvm_cpuid_entry2 *best = NULL;
3719 for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
3720 struct kvm_cpuid_entry2 *e;
3722 e = &vcpu->arch.cpuid_entries[i];
3723 if (is_matching_cpuid_entry(e, function, index)) {
3724 if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
3725 move_to_next_stateful_cpuid_entry(vcpu, i);
3730 * Both basic or both extended?
3732 if (((e->function ^ function) & 0x80000000) == 0)
3733 if (!best || e->function > best->function)
3738 EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
3740 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
3742 struct kvm_cpuid_entry2 *best;
3744 best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
3746 return best->eax & 0xff;
3750 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
3752 u32 function, index;
3753 struct kvm_cpuid_entry2 *best;
3755 function = kvm_register_read(vcpu, VCPU_REGS_RAX);
3756 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3757 kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
3758 kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
3759 kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
3760 kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
3761 best = kvm_find_cpuid_entry(vcpu, function, index);
3763 kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
3764 kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
3765 kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
3766 kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
3768 kvm_x86_ops->skip_emulated_instruction(vcpu);
3769 trace_kvm_cpuid(function,
3770 kvm_register_read(vcpu, VCPU_REGS_RAX),
3771 kvm_register_read(vcpu, VCPU_REGS_RBX),
3772 kvm_register_read(vcpu, VCPU_REGS_RCX),
3773 kvm_register_read(vcpu, VCPU_REGS_RDX));
3775 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
3778 * Check if userspace requested an interrupt window, and that the
3779 * interrupt window is open.
3781 * No need to exit to userspace if we already have an interrupt queued.
3783 static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
3785 return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
3786 vcpu->run->request_interrupt_window &&
3787 kvm_arch_interrupt_allowed(vcpu));
3790 static void post_kvm_run_save(struct kvm_vcpu *vcpu)
3792 struct kvm_run *kvm_run = vcpu->run;
3794 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
3795 kvm_run->cr8 = kvm_get_cr8(vcpu);
3796 kvm_run->apic_base = kvm_get_apic_base(vcpu);
3797 if (irqchip_in_kernel(vcpu->kvm))
3798 kvm_run->ready_for_interrupt_injection = 1;
3800 kvm_run->ready_for_interrupt_injection =
3801 kvm_arch_interrupt_allowed(vcpu) &&
3802 !kvm_cpu_has_interrupt(vcpu) &&
3803 !kvm_event_needs_reinjection(vcpu);
3806 static void vapic_enter(struct kvm_vcpu *vcpu)
3808 struct kvm_lapic *apic = vcpu->arch.apic;
3811 if (!apic || !apic->vapic_addr)
3814 page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
3816 vcpu->arch.apic->vapic_page = page;
3819 static void vapic_exit(struct kvm_vcpu *vcpu)
3821 struct kvm_lapic *apic = vcpu->arch.apic;
3823 if (!apic || !apic->vapic_addr)
3826 down_read(&vcpu->kvm->slots_lock);
3827 kvm_release_page_dirty(apic->vapic_page);
3828 mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
3829 up_read(&vcpu->kvm->slots_lock);
3832 static void update_cr8_intercept(struct kvm_vcpu *vcpu)
3836 if (!kvm_x86_ops->update_cr8_intercept)
3839 if (!vcpu->arch.apic)
3842 if (!vcpu->arch.apic->vapic_addr)
3843 max_irr = kvm_lapic_find_highest_irr(vcpu);
3850 tpr = kvm_lapic_get_cr8(vcpu);
3852 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
3855 static void inject_pending_event(struct kvm_vcpu *vcpu)
3857 /* try to reinject previous events if any */
3858 if (vcpu->arch.exception.pending) {
3859 kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
3860 vcpu->arch.exception.has_error_code,
3861 vcpu->arch.exception.error_code);
3865 if (vcpu->arch.nmi_injected) {
3866 kvm_x86_ops->set_nmi(vcpu);
3870 if (vcpu->arch.interrupt.pending) {
3871 kvm_x86_ops->set_irq(vcpu);
3875 /* try to inject new event if pending */
3876 if (vcpu->arch.nmi_pending) {
3877 if (kvm_x86_ops->nmi_allowed(vcpu)) {
3878 vcpu->arch.nmi_pending = false;
3879 vcpu->arch.nmi_injected = true;
3880 kvm_x86_ops->set_nmi(vcpu);
3882 } else if (kvm_cpu_has_interrupt(vcpu)) {
3883 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
3884 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
3886 kvm_x86_ops->set_irq(vcpu);
3891 static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
3894 bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
3895 vcpu->run->request_interrupt_window;
3898 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
3899 kvm_mmu_unload(vcpu);
3901 r = kvm_mmu_reload(vcpu);
3905 if (vcpu->requests) {
3906 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
3907 __kvm_migrate_timers(vcpu);
3908 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
3909 kvm_write_guest_time(vcpu);
3910 if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
3911 kvm_mmu_sync_roots(vcpu);
3912 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
3913 kvm_x86_ops->tlb_flush(vcpu);
3914 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
3916 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
3920 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
3921 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
3929 kvm_x86_ops->prepare_guest_switch(vcpu);
3930 kvm_load_guest_fpu(vcpu);
3932 local_irq_disable();
3934 clear_bit(KVM_REQ_KICK, &vcpu->requests);
3935 smp_mb__after_clear_bit();
3937 if (vcpu->requests || need_resched() || signal_pending(current)) {
3938 set_bit(KVM_REQ_KICK, &vcpu->requests);
3945 inject_pending_event(vcpu);
3947 /* enable NMI/IRQ window open exits if needed */
3948 if (vcpu->arch.nmi_pending)
3949 kvm_x86_ops->enable_nmi_window(vcpu);
3950 else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
3951 kvm_x86_ops->enable_irq_window(vcpu);
3953 if (kvm_lapic_enabled(vcpu)) {
3954 update_cr8_intercept(vcpu);
3955 kvm_lapic_sync_to_vapic(vcpu);
3958 up_read(&vcpu->kvm->slots_lock);
3962 if (unlikely(vcpu->arch.switch_db_regs)) {
3964 set_debugreg(vcpu->arch.eff_db[0], 0);
3965 set_debugreg(vcpu->arch.eff_db[1], 1);
3966 set_debugreg(vcpu->arch.eff_db[2], 2);
3967 set_debugreg(vcpu->arch.eff_db[3], 3);
3970 trace_kvm_entry(vcpu->vcpu_id);
3971 kvm_x86_ops->run(vcpu);
3974 * If the guest has used debug registers, at least dr7
3975 * will be disabled while returning to the host.
3976 * If we don't have active breakpoints in the host, we don't
3977 * care about the messed up debug address registers. But if
3978 * we have some of them active, restore the old state.
3980 if (hw_breakpoint_active())
3981 hw_breakpoint_restore();
3983 set_bit(KVM_REQ_KICK, &vcpu->requests);
3989 * We must have an instruction between local_irq_enable() and
3990 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3991 * the interrupt shadow. The stat.exits increment will do nicely.
3992 * But we need to prevent reordering, hence this barrier():
4000 down_read(&vcpu->kvm->slots_lock);
4003 * Profile KVM exit RIPs:
4005 if (unlikely(prof_on == KVM_PROFILING)) {
4006 unsigned long rip = kvm_rip_read(vcpu);
4007 profile_hit(KVM_PROFILING, (void *)rip);
4011 kvm_lapic_sync_from_vapic(vcpu);
4013 r = kvm_x86_ops->handle_exit(vcpu);
4019 static int __vcpu_run(struct kvm_vcpu *vcpu)
4023 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
4024 pr_debug("vcpu %d received sipi with vector # %x\n",
4025 vcpu->vcpu_id, vcpu->arch.sipi_vector);
4026 kvm_lapic_reset(vcpu);
4027 r = kvm_arch_vcpu_reset(vcpu);
4030 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4033 down_read(&vcpu->kvm->slots_lock);
4038 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
4039 r = vcpu_enter_guest(vcpu);
4041 up_read(&vcpu->kvm->slots_lock);
4042 kvm_vcpu_block(vcpu);
4043 down_read(&vcpu->kvm->slots_lock);
4044 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
4046 switch(vcpu->arch.mp_state) {
4047 case KVM_MP_STATE_HALTED:
4048 vcpu->arch.mp_state =
4049 KVM_MP_STATE_RUNNABLE;
4050 case KVM_MP_STATE_RUNNABLE:
4052 case KVM_MP_STATE_SIPI_RECEIVED:
4063 clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
4064 if (kvm_cpu_has_pending_timer(vcpu))
4065 kvm_inject_pending_timer_irqs(vcpu);
4067 if (dm_request_for_irq_injection(vcpu)) {
4069 vcpu->run->exit_reason = KVM_EXIT_INTR;
4070 ++vcpu->stat.request_irq_exits;
4072 if (signal_pending(current)) {
4074 vcpu->run->exit_reason = KVM_EXIT_INTR;
4075 ++vcpu->stat.signal_exits;
4077 if (need_resched()) {
4078 up_read(&vcpu->kvm->slots_lock);
4080 down_read(&vcpu->kvm->slots_lock);
4084 up_read(&vcpu->kvm->slots_lock);
4085 post_kvm_run_save(vcpu);
4092 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4099 if (vcpu->sigset_active)
4100 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
4102 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
4103 kvm_vcpu_block(vcpu);
4104 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
4109 /* re-sync apic's tpr */
4110 if (!irqchip_in_kernel(vcpu->kvm))
4111 kvm_set_cr8(vcpu, kvm_run->cr8);
4113 if (vcpu->arch.pio.cur_count) {
4114 r = complete_pio(vcpu);
4118 if (vcpu->mmio_needed) {
4119 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
4120 vcpu->mmio_read_completed = 1;
4121 vcpu->mmio_needed = 0;
4123 down_read(&vcpu->kvm->slots_lock);
4124 r = emulate_instruction(vcpu, vcpu->arch.mmio_fault_cr2, 0,
4125 EMULTYPE_NO_DECODE);
4126 up_read(&vcpu->kvm->slots_lock);
4127 if (r == EMULATE_DO_MMIO) {
4129 * Read-modify-write. Back to userspace.
4135 if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
4136 kvm_register_write(vcpu, VCPU_REGS_RAX,
4137 kvm_run->hypercall.ret);
4139 r = __vcpu_run(vcpu);
4142 if (vcpu->sigset_active)
4143 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
4149 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4153 regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4154 regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4155 regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4156 regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4157 regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4158 regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4159 regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4160 regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4161 #ifdef CONFIG_X86_64
4162 regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
4163 regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
4164 regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
4165 regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
4166 regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
4167 regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
4168 regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
4169 regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
4172 regs->rip = kvm_rip_read(vcpu);
4173 regs->rflags = kvm_get_rflags(vcpu);
4180 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4184 kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
4185 kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
4186 kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
4187 kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
4188 kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
4189 kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
4190 kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
4191 kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
4192 #ifdef CONFIG_X86_64
4193 kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
4194 kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
4195 kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
4196 kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
4197 kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
4198 kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
4199 kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
4200 kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
4203 kvm_rip_write(vcpu, regs->rip);
4204 kvm_set_rflags(vcpu, regs->rflags);
4206 vcpu->arch.exception.pending = false;
4213 void kvm_get_segment(struct kvm_vcpu *vcpu,
4214 struct kvm_segment *var, int seg)
4216 kvm_x86_ops->get_segment(vcpu, var, seg);
4219 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4221 struct kvm_segment cs;
4223 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
4227 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
4229 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4230 struct kvm_sregs *sregs)
4232 struct descriptor_table dt;
4236 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4237 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4238 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4239 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4240 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4241 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
4243 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4244 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
4246 kvm_x86_ops->get_idt(vcpu, &dt);
4247 sregs->idt.limit = dt.limit;
4248 sregs->idt.base = dt.base;
4249 kvm_x86_ops->get_gdt(vcpu, &dt);
4250 sregs->gdt.limit = dt.limit;
4251 sregs->gdt.base = dt.base;
4253 sregs->cr0 = vcpu->arch.cr0;
4254 sregs->cr2 = vcpu->arch.cr2;
4255 sregs->cr3 = vcpu->arch.cr3;
4256 sregs->cr4 = kvm_read_cr4(vcpu);
4257 sregs->cr8 = kvm_get_cr8(vcpu);
4258 sregs->efer = vcpu->arch.shadow_efer;
4259 sregs->apic_base = kvm_get_apic_base(vcpu);
4261 memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
4263 if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
4264 set_bit(vcpu->arch.interrupt.nr,
4265 (unsigned long *)sregs->interrupt_bitmap);
4272 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4273 struct kvm_mp_state *mp_state)
4276 mp_state->mp_state = vcpu->arch.mp_state;
4281 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4282 struct kvm_mp_state *mp_state)
4285 vcpu->arch.mp_state = mp_state->mp_state;
4290 static void kvm_set_segment(struct kvm_vcpu *vcpu,
4291 struct kvm_segment *var, int seg)
4293 kvm_x86_ops->set_segment(vcpu, var, seg);
4296 static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
4297 struct kvm_segment *kvm_desct)
4299 kvm_desct->base = get_desc_base(seg_desc);
4300 kvm_desct->limit = get_desc_limit(seg_desc);
4302 kvm_desct->limit <<= 12;
4303 kvm_desct->limit |= 0xfff;
4305 kvm_desct->selector = selector;
4306 kvm_desct->type = seg_desc->type;
4307 kvm_desct->present = seg_desc->p;
4308 kvm_desct->dpl = seg_desc->dpl;
4309 kvm_desct->db = seg_desc->d;
4310 kvm_desct->s = seg_desc->s;
4311 kvm_desct->l = seg_desc->l;
4312 kvm_desct->g = seg_desc->g;
4313 kvm_desct->avl = seg_desc->avl;
4315 kvm_desct->unusable = 1;
4317 kvm_desct->unusable = 0;
4318 kvm_desct->padding = 0;
4321 static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
4323 struct descriptor_table *dtable)
4325 if (selector & 1 << 2) {
4326 struct kvm_segment kvm_seg;
4328 kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
4330 if (kvm_seg.unusable)
4333 dtable->limit = kvm_seg.limit;
4334 dtable->base = kvm_seg.base;
4337 kvm_x86_ops->get_gdt(vcpu, dtable);
4340 /* allowed just for 8 bytes segments */
4341 static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4342 struct desc_struct *seg_desc)
4344 struct descriptor_table dtable;
4345 u16 index = selector >> 3;
4347 get_segment_descriptor_dtable(vcpu, selector, &dtable);
4349 if (dtable.limit < index * 8 + 7) {
4350 kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
4353 return kvm_read_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
4356 /* allowed just for 8 bytes segments */
4357 static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4358 struct desc_struct *seg_desc)
4360 struct descriptor_table dtable;
4361 u16 index = selector >> 3;
4363 get_segment_descriptor_dtable(vcpu, selector, &dtable);
4365 if (dtable.limit < index * 8 + 7)
4367 return kvm_write_guest_virt(dtable.base + index*8, seg_desc, sizeof(*seg_desc), vcpu);
4370 static gpa_t get_tss_base_addr(struct kvm_vcpu *vcpu,
4371 struct desc_struct *seg_desc)
4373 u32 base_addr = get_desc_base(seg_desc);
4375 return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
4378 static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
4380 struct kvm_segment kvm_seg;
4382 kvm_get_segment(vcpu, &kvm_seg, seg);
4383 return kvm_seg.selector;
4386 static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
4388 struct kvm_segment *kvm_seg)
4390 struct desc_struct seg_desc;
4392 if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
4394 seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
4398 static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
4400 struct kvm_segment segvar = {
4401 .base = selector << 4,
4403 .selector = selector,
4414 kvm_x86_ops->set_segment(vcpu, &segvar, seg);
4418 static int is_vm86_segment(struct kvm_vcpu *vcpu, int seg)
4420 return (seg != VCPU_SREG_LDTR) &&
4421 (seg != VCPU_SREG_TR) &&
4422 (kvm_get_rflags(vcpu) & X86_EFLAGS_VM);
4425 static void kvm_check_segment_descriptor(struct kvm_vcpu *vcpu, int seg,
4428 /* NULL selector is not valid for CS and SS */
4429 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
4431 kvm_queue_exception_e(vcpu, TS_VECTOR, selector >> 3);
4434 int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
4435 int type_bits, int seg)
4437 struct kvm_segment kvm_seg;
4439 if (is_vm86_segment(vcpu, seg) || !(vcpu->arch.cr0 & X86_CR0_PE))
4440 return kvm_load_realmode_segment(vcpu, selector, seg);
4441 if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
4444 kvm_check_segment_descriptor(vcpu, seg, selector);
4445 kvm_seg.type |= type_bits;
4447 if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
4448 seg != VCPU_SREG_LDTR)
4450 kvm_seg.unusable = 1;
4452 kvm_set_segment(vcpu, &kvm_seg, seg);
4456 static void save_state_to_tss32(struct kvm_vcpu *vcpu,
4457 struct tss_segment_32 *tss)
4459 tss->cr3 = vcpu->arch.cr3;
4460 tss->eip = kvm_rip_read(vcpu);
4461 tss->eflags = kvm_get_rflags(vcpu);
4462 tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4463 tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4464 tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4465 tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4466 tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4467 tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4468 tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
4469 tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
4470 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4471 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4472 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4473 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4474 tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
4475 tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
4476 tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
4479 static int load_state_from_tss32(struct kvm_vcpu *vcpu,
4480 struct tss_segment_32 *tss)
4482 kvm_set_cr3(vcpu, tss->cr3);
4484 kvm_rip_write(vcpu, tss->eip);
4485 kvm_set_rflags(vcpu, tss->eflags | 2);
4487 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
4488 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
4489 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
4490 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
4491 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
4492 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
4493 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
4494 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
4496 if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
4499 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
4502 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
4505 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
4508 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
4511 if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
4514 if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
4519 static void save_state_to_tss16(struct kvm_vcpu *vcpu,
4520 struct tss_segment_16 *tss)
4522 tss->ip = kvm_rip_read(vcpu);
4523 tss->flag = kvm_get_rflags(vcpu);
4524 tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
4525 tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
4526 tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
4527 tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
4528 tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
4529 tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
4530 tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
4531 tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
4533 tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
4534 tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
4535 tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
4536 tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
4537 tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
4540 static int load_state_from_tss16(struct kvm_vcpu *vcpu,
4541 struct tss_segment_16 *tss)
4543 kvm_rip_write(vcpu, tss->ip);
4544 kvm_set_rflags(vcpu, tss->flag | 2);
4545 kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
4546 kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
4547 kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
4548 kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
4549 kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
4550 kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
4551 kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
4552 kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
4554 if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
4557 if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
4560 if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
4563 if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
4566 if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
4571 static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
4572 u16 old_tss_sel, u32 old_tss_base,
4573 struct desc_struct *nseg_desc)
4575 struct tss_segment_16 tss_segment_16;
4578 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4579 sizeof tss_segment_16))
4582 save_state_to_tss16(vcpu, &tss_segment_16);
4584 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
4585 sizeof tss_segment_16))
4588 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4589 &tss_segment_16, sizeof tss_segment_16))
4592 if (old_tss_sel != 0xffff) {
4593 tss_segment_16.prev_task_link = old_tss_sel;
4595 if (kvm_write_guest(vcpu->kvm,
4596 get_tss_base_addr(vcpu, nseg_desc),
4597 &tss_segment_16.prev_task_link,
4598 sizeof tss_segment_16.prev_task_link))
4602 if (load_state_from_tss16(vcpu, &tss_segment_16))
4610 static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
4611 u16 old_tss_sel, u32 old_tss_base,
4612 struct desc_struct *nseg_desc)
4614 struct tss_segment_32 tss_segment_32;
4617 if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4618 sizeof tss_segment_32))
4621 save_state_to_tss32(vcpu, &tss_segment_32);
4623 if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
4624 sizeof tss_segment_32))
4627 if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
4628 &tss_segment_32, sizeof tss_segment_32))
4631 if (old_tss_sel != 0xffff) {
4632 tss_segment_32.prev_task_link = old_tss_sel;
4634 if (kvm_write_guest(vcpu->kvm,
4635 get_tss_base_addr(vcpu, nseg_desc),
4636 &tss_segment_32.prev_task_link,
4637 sizeof tss_segment_32.prev_task_link))
4641 if (load_state_from_tss32(vcpu, &tss_segment_32))
4649 int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
4651 struct kvm_segment tr_seg;
4652 struct desc_struct cseg_desc;
4653 struct desc_struct nseg_desc;
4655 u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
4656 u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
4658 old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
4660 /* FIXME: Handle errors. Failure to read either TSS or their
4661 * descriptors should generate a pagefault.
4663 if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
4666 if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
4669 if (reason != TASK_SWITCH_IRET) {
4672 cpl = kvm_x86_ops->get_cpl(vcpu);
4673 if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
4674 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4679 if (!nseg_desc.p || get_desc_limit(&nseg_desc) < 0x67) {
4680 kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
4684 if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
4685 cseg_desc.type &= ~(1 << 1); //clear the B flag
4686 save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
4689 if (reason == TASK_SWITCH_IRET) {
4690 u32 eflags = kvm_get_rflags(vcpu);
4691 kvm_set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
4694 /* set back link to prev task only if NT bit is set in eflags
4695 note that old_tss_sel is not used afetr this point */
4696 if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
4697 old_tss_sel = 0xffff;
4699 if (nseg_desc.type & 8)
4700 ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
4701 old_tss_base, &nseg_desc);
4703 ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
4704 old_tss_base, &nseg_desc);
4706 if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
4707 u32 eflags = kvm_get_rflags(vcpu);
4708 kvm_set_rflags(vcpu, eflags | X86_EFLAGS_NT);
4711 if (reason != TASK_SWITCH_IRET) {
4712 nseg_desc.type |= (1 << 1);
4713 save_guest_segment_descriptor(vcpu, tss_selector,
4717 kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
4718 seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
4720 kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
4724 EXPORT_SYMBOL_GPL(kvm_task_switch);
4726 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4727 struct kvm_sregs *sregs)
4729 int mmu_reset_needed = 0;
4730 int pending_vec, max_bits;
4731 struct descriptor_table dt;
4735 dt.limit = sregs->idt.limit;
4736 dt.base = sregs->idt.base;
4737 kvm_x86_ops->set_idt(vcpu, &dt);
4738 dt.limit = sregs->gdt.limit;
4739 dt.base = sregs->gdt.base;
4740 kvm_x86_ops->set_gdt(vcpu, &dt);
4742 vcpu->arch.cr2 = sregs->cr2;
4743 mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
4744 vcpu->arch.cr3 = sregs->cr3;
4746 kvm_set_cr8(vcpu, sregs->cr8);
4748 mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
4749 kvm_x86_ops->set_efer(vcpu, sregs->efer);
4750 kvm_set_apic_base(vcpu, sregs->apic_base);
4752 mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
4753 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
4754 vcpu->arch.cr0 = sregs->cr0;
4756 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
4757 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
4758 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
4759 load_pdptrs(vcpu, vcpu->arch.cr3);
4760 mmu_reset_needed = 1;
4763 if (mmu_reset_needed)
4764 kvm_mmu_reset_context(vcpu);
4766 max_bits = (sizeof sregs->interrupt_bitmap) << 3;
4767 pending_vec = find_first_bit(
4768 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
4769 if (pending_vec < max_bits) {
4770 kvm_queue_interrupt(vcpu, pending_vec, false);
4771 pr_debug("Set back pending irq %d\n", pending_vec);
4772 if (irqchip_in_kernel(vcpu->kvm))
4773 kvm_pic_clear_isr_ack(vcpu->kvm);
4776 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
4777 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
4778 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
4779 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
4780 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
4781 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
4783 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
4784 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
4786 update_cr8_intercept(vcpu);
4788 /* Older userspace won't unhalt the vcpu on reset. */
4789 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
4790 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
4791 !(vcpu->arch.cr0 & X86_CR0_PE))
4792 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4799 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4800 struct kvm_guest_debug *dbg)
4802 unsigned long rflags;
4807 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
4809 if (vcpu->arch.exception.pending)
4811 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
4812 kvm_queue_exception(vcpu, DB_VECTOR);
4814 kvm_queue_exception(vcpu, BP_VECTOR);
4818 * Read rflags as long as potentially injected trace flags are still
4821 rflags = kvm_get_rflags(vcpu);
4823 vcpu->guest_debug = dbg->control;
4824 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
4825 vcpu->guest_debug = 0;
4827 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
4828 for (i = 0; i < KVM_NR_DB_REGS; ++i)
4829 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
4830 vcpu->arch.switch_db_regs =
4831 (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
4833 for (i = 0; i < KVM_NR_DB_REGS; i++)
4834 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
4835 vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
4838 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
4839 vcpu->arch.singlestep_cs =
4840 get_segment_selector(vcpu, VCPU_SREG_CS);
4841 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu);
4845 * Trigger an rflags update that will inject or remove the trace
4848 kvm_set_rflags(vcpu, rflags);
4850 kvm_x86_ops->set_guest_debug(vcpu, dbg);
4861 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
4862 * we have asm/x86/processor.h
4873 u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
4874 #ifdef CONFIG_X86_64
4875 u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
4877 u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
4882 * Translate a guest virtual address to a guest physical address.
4884 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4885 struct kvm_translation *tr)
4887 unsigned long vaddr = tr->linear_address;
4891 down_read(&vcpu->kvm->slots_lock);
4892 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
4893 up_read(&vcpu->kvm->slots_lock);
4894 tr->physical_address = gpa;
4895 tr->valid = gpa != UNMAPPED_GVA;
4903 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4905 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
4909 memcpy(fpu->fpr, fxsave->st_space, 128);
4910 fpu->fcw = fxsave->cwd;
4911 fpu->fsw = fxsave->swd;
4912 fpu->ftwx = fxsave->twd;
4913 fpu->last_opcode = fxsave->fop;
4914 fpu->last_ip = fxsave->rip;
4915 fpu->last_dp = fxsave->rdp;
4916 memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
4923 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4925 struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
4929 memcpy(fxsave->st_space, fpu->fpr, 128);
4930 fxsave->cwd = fpu->fcw;
4931 fxsave->swd = fpu->fsw;
4932 fxsave->twd = fpu->ftwx;
4933 fxsave->fop = fpu->last_opcode;
4934 fxsave->rip = fpu->last_ip;
4935 fxsave->rdp = fpu->last_dp;
4936 memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
4943 void fx_init(struct kvm_vcpu *vcpu)
4945 unsigned after_mxcsr_mask;
4948 * Touch the fpu the first time in non atomic context as if
4949 * this is the first fpu instruction the exception handler
4950 * will fire before the instruction returns and it'll have to
4951 * allocate ram with GFP_KERNEL.
4954 kvm_fx_save(&vcpu->arch.host_fx_image);
4956 /* Initialize guest FPU by resetting ours and saving into guest's */
4958 kvm_fx_save(&vcpu->arch.host_fx_image);
4960 kvm_fx_save(&vcpu->arch.guest_fx_image);
4961 kvm_fx_restore(&vcpu->arch.host_fx_image);
4964 vcpu->arch.cr0 |= X86_CR0_ET;
4965 after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
4966 vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
4967 memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
4968 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
4970 EXPORT_SYMBOL_GPL(fx_init);
4972 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
4974 if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
4977 vcpu->guest_fpu_loaded = 1;
4978 kvm_fx_save(&vcpu->arch.host_fx_image);
4979 kvm_fx_restore(&vcpu->arch.guest_fx_image);
4981 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
4983 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
4985 if (!vcpu->guest_fpu_loaded)
4988 vcpu->guest_fpu_loaded = 0;
4989 kvm_fx_save(&vcpu->arch.guest_fx_image);
4990 kvm_fx_restore(&vcpu->arch.host_fx_image);
4991 ++vcpu->stat.fpu_reload;
4993 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
4995 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
4997 if (vcpu->arch.time_page) {
4998 kvm_release_page_dirty(vcpu->arch.time_page);
4999 vcpu->arch.time_page = NULL;
5002 kvm_x86_ops->vcpu_free(vcpu);
5005 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
5008 return kvm_x86_ops->vcpu_create(kvm, id);
5011 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
5015 /* We do fxsave: this must be aligned. */
5016 BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
5018 vcpu->arch.mtrr_state.have_fixed = 1;
5020 r = kvm_arch_vcpu_reset(vcpu);
5022 r = kvm_mmu_setup(vcpu);
5029 kvm_x86_ops->vcpu_free(vcpu);
5033 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
5036 kvm_mmu_unload(vcpu);
5039 kvm_x86_ops->vcpu_free(vcpu);
5042 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
5044 vcpu->arch.nmi_pending = false;
5045 vcpu->arch.nmi_injected = false;
5047 vcpu->arch.switch_db_regs = 0;
5048 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
5049 vcpu->arch.dr6 = DR6_FIXED_1;
5050 vcpu->arch.dr7 = DR7_FIXED_1;
5052 return kvm_x86_ops->vcpu_reset(vcpu);
5055 int kvm_arch_hardware_enable(void *garbage)
5058 * Since this may be called from a hotplug notifcation,
5059 * we can't get the CPU frequency directly.
5061 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
5062 int cpu = raw_smp_processor_id();
5063 per_cpu(cpu_tsc_khz, cpu) = 0;
5066 kvm_shared_msr_cpu_online();
5068 return kvm_x86_ops->hardware_enable(garbage);
5071 void kvm_arch_hardware_disable(void *garbage)
5073 kvm_x86_ops->hardware_disable(garbage);
5074 drop_user_return_notifiers(garbage);
5077 int kvm_arch_hardware_setup(void)
5079 return kvm_x86_ops->hardware_setup();
5082 void kvm_arch_hardware_unsetup(void)
5084 kvm_x86_ops->hardware_unsetup();
5087 void kvm_arch_check_processor_compat(void *rtn)
5089 kvm_x86_ops->check_processor_compatibility(rtn);
5092 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
5098 BUG_ON(vcpu->kvm == NULL);
5101 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
5102 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
5103 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
5105 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
5107 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
5112 vcpu->arch.pio_data = page_address(page);
5114 r = kvm_mmu_create(vcpu);
5116 goto fail_free_pio_data;
5118 if (irqchip_in_kernel(kvm)) {
5119 r = kvm_create_lapic(vcpu);
5121 goto fail_mmu_destroy;
5124 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
5126 if (!vcpu->arch.mce_banks) {
5128 goto fail_free_lapic;
5130 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
5134 kvm_free_lapic(vcpu);
5136 kvm_mmu_destroy(vcpu);
5138 free_page((unsigned long)vcpu->arch.pio_data);
5143 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
5145 kfree(vcpu->arch.mce_banks);
5146 kvm_free_lapic(vcpu);
5147 down_read(&vcpu->kvm->slots_lock);
5148 kvm_mmu_destroy(vcpu);
5149 up_read(&vcpu->kvm->slots_lock);
5150 free_page((unsigned long)vcpu->arch.pio_data);
5153 struct kvm *kvm_arch_create_vm(void)
5155 struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
5158 return ERR_PTR(-ENOMEM);
5160 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
5161 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
5163 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
5164 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
5166 rdtscll(kvm->arch.vm_init_tsc);
5171 static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
5174 kvm_mmu_unload(vcpu);
5178 static void kvm_free_vcpus(struct kvm *kvm)
5181 struct kvm_vcpu *vcpu;
5184 * Unpin any mmu pages first.
5186 kvm_for_each_vcpu(i, vcpu, kvm)
5187 kvm_unload_vcpu_mmu(vcpu);
5188 kvm_for_each_vcpu(i, vcpu, kvm)
5189 kvm_arch_vcpu_free(vcpu);
5191 mutex_lock(&kvm->lock);
5192 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
5193 kvm->vcpus[i] = NULL;
5195 atomic_set(&kvm->online_vcpus, 0);
5196 mutex_unlock(&kvm->lock);
5199 void kvm_arch_sync_events(struct kvm *kvm)
5201 kvm_free_all_assigned_devices(kvm);
5204 void kvm_arch_destroy_vm(struct kvm *kvm)
5206 kvm_iommu_unmap_guest(kvm);
5208 kfree(kvm->arch.vpic);
5209 kfree(kvm->arch.vioapic);
5210 kvm_free_vcpus(kvm);
5211 kvm_free_physmem(kvm);
5212 if (kvm->arch.apic_access_page)
5213 put_page(kvm->arch.apic_access_page);
5214 if (kvm->arch.ept_identity_pagetable)
5215 put_page(kvm->arch.ept_identity_pagetable);
5219 int kvm_arch_set_memory_region(struct kvm *kvm,
5220 struct kvm_userspace_memory_region *mem,
5221 struct kvm_memory_slot old,
5224 int npages = mem->memory_size >> PAGE_SHIFT;
5225 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
5227 /*To keep backward compatibility with older userspace,
5228 *x86 needs to hanlde !user_alloc case.
5231 if (npages && !old.rmap) {
5232 unsigned long userspace_addr;
5234 down_write(¤t->mm->mmap_sem);
5235 userspace_addr = do_mmap(NULL, 0,
5237 PROT_READ | PROT_WRITE,
5238 MAP_PRIVATE | MAP_ANONYMOUS,
5240 up_write(¤t->mm->mmap_sem);
5242 if (IS_ERR((void *)userspace_addr))
5243 return PTR_ERR((void *)userspace_addr);
5245 /* set userspace_addr atomically for kvm_hva_to_rmapp */
5246 spin_lock(&kvm->mmu_lock);
5247 memslot->userspace_addr = userspace_addr;
5248 spin_unlock(&kvm->mmu_lock);
5250 if (!old.user_alloc && old.rmap) {
5253 down_write(¤t->mm->mmap_sem);
5254 ret = do_munmap(current->mm, old.userspace_addr,
5255 old.npages * PAGE_SIZE);
5256 up_write(¤t->mm->mmap_sem);
5259 "kvm_vm_ioctl_set_memory_region: "
5260 "failed to munmap memory\n");
5265 spin_lock(&kvm->mmu_lock);
5266 if (!kvm->arch.n_requested_mmu_pages) {
5267 unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
5268 kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
5271 kvm_mmu_slot_remove_write_access(kvm, mem->slot);
5272 spin_unlock(&kvm->mmu_lock);
5277 void kvm_arch_flush_shadow(struct kvm *kvm)
5279 kvm_mmu_zap_all(kvm);
5280 kvm_reload_remote_mmus(kvm);
5283 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
5285 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
5286 || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
5287 || vcpu->arch.nmi_pending ||
5288 (kvm_arch_interrupt_allowed(vcpu) &&
5289 kvm_cpu_has_interrupt(vcpu));
5292 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
5295 int cpu = vcpu->cpu;
5297 if (waitqueue_active(&vcpu->wq)) {
5298 wake_up_interruptible(&vcpu->wq);
5299 ++vcpu->stat.halt_wakeup;
5303 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
5304 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
5305 smp_send_reschedule(cpu);
5309 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
5311 return kvm_x86_ops->interrupt_allowed(vcpu);
5314 unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
5316 unsigned long rflags;
5318 rflags = kvm_x86_ops->get_rflags(vcpu);
5319 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
5320 rflags &= ~(unsigned long)(X86_EFLAGS_TF | X86_EFLAGS_RF);
5323 EXPORT_SYMBOL_GPL(kvm_get_rflags);
5325 void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
5327 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
5328 vcpu->arch.singlestep_cs ==
5329 get_segment_selector(vcpu, VCPU_SREG_CS) &&
5330 vcpu->arch.singlestep_rip == kvm_rip_read(vcpu))
5331 rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
5332 kvm_x86_ops->set_rflags(vcpu, rflags);
5334 EXPORT_SYMBOL_GPL(kvm_set_rflags);
5336 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
5337 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
5338 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
5339 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
5340 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
5341 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
5342 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
5343 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
5344 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
5345 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
5346 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);