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>
41 #include <asm/uaccess.h>
46 #define MAX_IO_MSRS 256
47 #define CR0_RESERVED_BITS \
48 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
49 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
50 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
51 #define CR4_RESERVED_BITS \
52 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
53 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
54 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
55 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
57 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
59 * - enable syscall per default because its emulated by KVM
60 * - enable LME and LMA per default on 64 bit KVM
63 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffafeULL
;
65 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffffeULL
;
68 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
69 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
71 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
72 struct kvm_cpuid_entry2 __user
*entries
);
73 struct kvm_cpuid_entry2
*kvm_find_cpuid_entry(struct kvm_vcpu
*vcpu
,
74 u32 function
, u32 index
);
76 struct kvm_x86_ops
*kvm_x86_ops
;
77 EXPORT_SYMBOL_GPL(kvm_x86_ops
);
79 struct kvm_stats_debugfs_item debugfs_entries
[] = {
80 { "pf_fixed", VCPU_STAT(pf_fixed
) },
81 { "pf_guest", VCPU_STAT(pf_guest
) },
82 { "tlb_flush", VCPU_STAT(tlb_flush
) },
83 { "invlpg", VCPU_STAT(invlpg
) },
84 { "exits", VCPU_STAT(exits
) },
85 { "io_exits", VCPU_STAT(io_exits
) },
86 { "mmio_exits", VCPU_STAT(mmio_exits
) },
87 { "signal_exits", VCPU_STAT(signal_exits
) },
88 { "irq_window", VCPU_STAT(irq_window_exits
) },
89 { "nmi_window", VCPU_STAT(nmi_window_exits
) },
90 { "halt_exits", VCPU_STAT(halt_exits
) },
91 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
92 { "hypercalls", VCPU_STAT(hypercalls
) },
93 { "request_irq", VCPU_STAT(request_irq_exits
) },
94 { "request_nmi", VCPU_STAT(request_nmi_exits
) },
95 { "irq_exits", VCPU_STAT(irq_exits
) },
96 { "host_state_reload", VCPU_STAT(host_state_reload
) },
97 { "efer_reload", VCPU_STAT(efer_reload
) },
98 { "fpu_reload", VCPU_STAT(fpu_reload
) },
99 { "insn_emulation", VCPU_STAT(insn_emulation
) },
100 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
101 { "irq_injections", VCPU_STAT(irq_injections
) },
102 { "nmi_injections", VCPU_STAT(nmi_injections
) },
103 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
104 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
105 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
106 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
107 { "mmu_flooded", VM_STAT(mmu_flooded
) },
108 { "mmu_recycled", VM_STAT(mmu_recycled
) },
109 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
110 { "mmu_unsync", VM_STAT(mmu_unsync
) },
111 { "mmu_unsync_global", VM_STAT(mmu_unsync_global
) },
112 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
113 { "largepages", VM_STAT(lpages
) },
117 unsigned long segment_base(u16 selector
)
119 struct descriptor_table gdt
;
120 struct desc_struct
*d
;
121 unsigned long table_base
;
127 asm("sgdt %0" : "=m"(gdt
));
128 table_base
= gdt
.base
;
130 if (selector
& 4) { /* from ldt */
133 asm("sldt %0" : "=g"(ldt_selector
));
134 table_base
= segment_base(ldt_selector
);
136 d
= (struct desc_struct
*)(table_base
+ (selector
& ~7));
137 v
= d
->base0
| ((unsigned long)d
->base1
<< 16) |
138 ((unsigned long)d
->base2
<< 24);
140 if (d
->s
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
141 v
|= ((unsigned long)((struct ldttss_desc64
*)d
)->base3
) << 32;
145 EXPORT_SYMBOL_GPL(segment_base
);
147 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
149 if (irqchip_in_kernel(vcpu
->kvm
))
150 return vcpu
->arch
.apic_base
;
152 return vcpu
->arch
.apic_base
;
154 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
156 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
158 /* TODO: reserve bits check */
159 if (irqchip_in_kernel(vcpu
->kvm
))
160 kvm_lapic_set_base(vcpu
, data
);
162 vcpu
->arch
.apic_base
= data
;
164 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
166 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
168 WARN_ON(vcpu
->arch
.exception
.pending
);
169 vcpu
->arch
.exception
.pending
= true;
170 vcpu
->arch
.exception
.has_error_code
= false;
171 vcpu
->arch
.exception
.nr
= nr
;
173 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
175 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
178 ++vcpu
->stat
.pf_guest
;
180 if (vcpu
->arch
.exception
.pending
) {
181 if (vcpu
->arch
.exception
.nr
== PF_VECTOR
) {
182 printk(KERN_DEBUG
"kvm: inject_page_fault:"
183 " double fault 0x%lx\n", addr
);
184 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
185 vcpu
->arch
.exception
.error_code
= 0;
186 } else if (vcpu
->arch
.exception
.nr
== DF_VECTOR
) {
187 /* triple fault -> shutdown */
188 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
192 vcpu
->arch
.cr2
= addr
;
193 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
196 void kvm_inject_nmi(struct kvm_vcpu
*vcpu
)
198 vcpu
->arch
.nmi_pending
= 1;
200 EXPORT_SYMBOL_GPL(kvm_inject_nmi
);
202 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
204 WARN_ON(vcpu
->arch
.exception
.pending
);
205 vcpu
->arch
.exception
.pending
= true;
206 vcpu
->arch
.exception
.has_error_code
= true;
207 vcpu
->arch
.exception
.nr
= nr
;
208 vcpu
->arch
.exception
.error_code
= error_code
;
210 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
212 static void __queue_exception(struct kvm_vcpu
*vcpu
)
214 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
215 vcpu
->arch
.exception
.has_error_code
,
216 vcpu
->arch
.exception
.error_code
);
220 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
221 * a #GP and return false.
223 bool kvm_require_cpl(struct kvm_vcpu
*vcpu
, int required_cpl
)
225 if (kvm_x86_ops
->get_cpl(vcpu
) <= required_cpl
)
227 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
230 EXPORT_SYMBOL_GPL(kvm_require_cpl
);
233 * Load the pae pdptrs. Return true is they are all valid.
235 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
237 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
238 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
241 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
243 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
244 offset
* sizeof(u64
), sizeof(pdpte
));
249 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
250 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
257 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
262 EXPORT_SYMBOL_GPL(load_pdptrs
);
264 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
266 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
270 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
273 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
276 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
282 void kvm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
284 if (cr0
& CR0_RESERVED_BITS
) {
285 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
286 cr0
, vcpu
->arch
.cr0
);
287 kvm_inject_gp(vcpu
, 0);
291 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
292 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
293 kvm_inject_gp(vcpu
, 0);
297 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
298 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
299 "and a clear PE flag\n");
300 kvm_inject_gp(vcpu
, 0);
304 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
306 if ((vcpu
->arch
.shadow_efer
& EFER_LME
)) {
310 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
311 "in long mode while PAE is disabled\n");
312 kvm_inject_gp(vcpu
, 0);
315 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
317 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
318 "in long mode while CS.L == 1\n");
319 kvm_inject_gp(vcpu
, 0);
325 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
326 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
328 kvm_inject_gp(vcpu
, 0);
334 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
335 vcpu
->arch
.cr0
= cr0
;
337 kvm_mmu_sync_global(vcpu
);
338 kvm_mmu_reset_context(vcpu
);
341 EXPORT_SYMBOL_GPL(kvm_set_cr0
);
343 void kvm_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
345 kvm_set_cr0(vcpu
, (vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f));
346 KVMTRACE_1D(LMSW
, vcpu
,
347 (u32
)((vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f)),
350 EXPORT_SYMBOL_GPL(kvm_lmsw
);
352 void kvm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
354 unsigned long old_cr4
= vcpu
->arch
.cr4
;
355 unsigned long pdptr_bits
= X86_CR4_PGE
| X86_CR4_PSE
| X86_CR4_PAE
;
357 if (cr4
& CR4_RESERVED_BITS
) {
358 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
359 kvm_inject_gp(vcpu
, 0);
363 if (is_long_mode(vcpu
)) {
364 if (!(cr4
& X86_CR4_PAE
)) {
365 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
367 kvm_inject_gp(vcpu
, 0);
370 } else if (is_paging(vcpu
) && (cr4
& X86_CR4_PAE
)
371 && ((cr4
^ old_cr4
) & pdptr_bits
)
372 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
373 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
374 kvm_inject_gp(vcpu
, 0);
378 if (cr4
& X86_CR4_VMXE
) {
379 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
380 kvm_inject_gp(vcpu
, 0);
383 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
384 vcpu
->arch
.cr4
= cr4
;
385 vcpu
->arch
.mmu
.base_role
.cr4_pge
= (cr4
& X86_CR4_PGE
) && !tdp_enabled
;
386 kvm_mmu_sync_global(vcpu
);
387 kvm_mmu_reset_context(vcpu
);
389 EXPORT_SYMBOL_GPL(kvm_set_cr4
);
391 void kvm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
393 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
394 kvm_mmu_sync_roots(vcpu
);
395 kvm_mmu_flush_tlb(vcpu
);
399 if (is_long_mode(vcpu
)) {
400 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
401 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
402 kvm_inject_gp(vcpu
, 0);
407 if (cr3
& CR3_PAE_RESERVED_BITS
) {
409 "set_cr3: #GP, reserved bits\n");
410 kvm_inject_gp(vcpu
, 0);
413 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
414 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
416 kvm_inject_gp(vcpu
, 0);
421 * We don't check reserved bits in nonpae mode, because
422 * this isn't enforced, and VMware depends on this.
427 * Does the new cr3 value map to physical memory? (Note, we
428 * catch an invalid cr3 even in real-mode, because it would
429 * cause trouble later on when we turn on paging anyway.)
431 * A real CPU would silently accept an invalid cr3 and would
432 * attempt to use it - with largely undefined (and often hard
433 * to debug) behavior on the guest side.
435 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
436 kvm_inject_gp(vcpu
, 0);
438 vcpu
->arch
.cr3
= cr3
;
439 vcpu
->arch
.mmu
.new_cr3(vcpu
);
442 EXPORT_SYMBOL_GPL(kvm_set_cr3
);
444 void kvm_set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
446 if (cr8
& CR8_RESERVED_BITS
) {
447 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
448 kvm_inject_gp(vcpu
, 0);
451 if (irqchip_in_kernel(vcpu
->kvm
))
452 kvm_lapic_set_tpr(vcpu
, cr8
);
454 vcpu
->arch
.cr8
= cr8
;
456 EXPORT_SYMBOL_GPL(kvm_set_cr8
);
458 unsigned long kvm_get_cr8(struct kvm_vcpu
*vcpu
)
460 if (irqchip_in_kernel(vcpu
->kvm
))
461 return kvm_lapic_get_cr8(vcpu
);
463 return vcpu
->arch
.cr8
;
465 EXPORT_SYMBOL_GPL(kvm_get_cr8
);
467 static inline u32
bit(int bitno
)
469 return 1 << (bitno
& 31);
473 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
474 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
476 * This list is modified at module load time to reflect the
477 * capabilities of the host cpu.
479 static u32 msrs_to_save
[] = {
480 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
483 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
485 MSR_IA32_TIME_STAMP_COUNTER
, MSR_KVM_SYSTEM_TIME
, MSR_KVM_WALL_CLOCK
,
486 MSR_IA32_PERF_STATUS
, MSR_IA32_CR_PAT
, MSR_VM_HSAVE_PA
489 static unsigned num_msrs_to_save
;
491 static u32 emulated_msrs
[] = {
492 MSR_IA32_MISC_ENABLE
,
495 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
497 if (efer
& efer_reserved_bits
) {
498 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
500 kvm_inject_gp(vcpu
, 0);
505 && (vcpu
->arch
.shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
506 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
507 kvm_inject_gp(vcpu
, 0);
511 if (efer
& EFER_FFXSR
) {
512 struct kvm_cpuid_entry2
*feat
;
514 feat
= kvm_find_cpuid_entry(vcpu
, 0x80000001, 0);
515 if (!feat
|| !(feat
->edx
& bit(X86_FEATURE_FXSR_OPT
))) {
516 printk(KERN_DEBUG
"set_efer: #GP, enable FFXSR w/o CPUID capability\n");
517 kvm_inject_gp(vcpu
, 0);
522 if (efer
& EFER_SVME
) {
523 struct kvm_cpuid_entry2
*feat
;
525 feat
= kvm_find_cpuid_entry(vcpu
, 0x80000001, 0);
526 if (!feat
|| !(feat
->ecx
& bit(X86_FEATURE_SVM
))) {
527 printk(KERN_DEBUG
"set_efer: #GP, enable SVM w/o SVM\n");
528 kvm_inject_gp(vcpu
, 0);
533 kvm_x86_ops
->set_efer(vcpu
, efer
);
536 efer
|= vcpu
->arch
.shadow_efer
& EFER_LMA
;
538 vcpu
->arch
.shadow_efer
= efer
;
540 vcpu
->arch
.mmu
.base_role
.nxe
= (efer
& EFER_NX
) && !tdp_enabled
;
541 kvm_mmu_reset_context(vcpu
);
544 void kvm_enable_efer_bits(u64 mask
)
546 efer_reserved_bits
&= ~mask
;
548 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits
);
552 * Writes msr value into into the appropriate "register".
553 * Returns 0 on success, non-0 otherwise.
554 * Assumes vcpu_load() was already called.
556 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
558 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
562 * Adapt set_msr() to msr_io()'s calling convention
564 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
566 return kvm_set_msr(vcpu
, index
, *data
);
569 static void kvm_write_wall_clock(struct kvm
*kvm
, gpa_t wall_clock
)
572 struct pvclock_wall_clock wc
;
573 struct timespec now
, sys
, boot
;
580 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
583 * The guest calculates current wall clock time by adding
584 * system time (updated by kvm_write_guest_time below) to the
585 * wall clock specified here. guest system time equals host
586 * system time for us, thus we must fill in host boot time here.
588 now
= current_kernel_time();
590 boot
= ns_to_timespec(timespec_to_ns(&now
) - timespec_to_ns(&sys
));
592 wc
.sec
= boot
.tv_sec
;
593 wc
.nsec
= boot
.tv_nsec
;
594 wc
.version
= version
;
596 kvm_write_guest(kvm
, wall_clock
, &wc
, sizeof(wc
));
599 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
602 static uint32_t div_frac(uint32_t dividend
, uint32_t divisor
)
604 uint32_t quotient
, remainder
;
606 /* Don't try to replace with do_div(), this one calculates
607 * "(dividend << 32) / divisor" */
609 : "=a" (quotient
), "=d" (remainder
)
610 : "0" (0), "1" (dividend
), "r" (divisor
) );
614 static void kvm_set_time_scale(uint32_t tsc_khz
, struct pvclock_vcpu_time_info
*hv_clock
)
616 uint64_t nsecs
= 1000000000LL;
621 tps64
= tsc_khz
* 1000LL;
622 while (tps64
> nsecs
*2) {
627 tps32
= (uint32_t)tps64
;
628 while (tps32
<= (uint32_t)nsecs
) {
633 hv_clock
->tsc_shift
= shift
;
634 hv_clock
->tsc_to_system_mul
= div_frac(nsecs
, tps32
);
636 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
637 __func__
, tsc_khz
, hv_clock
->tsc_shift
,
638 hv_clock
->tsc_to_system_mul
);
641 static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz
);
643 static void kvm_write_guest_time(struct kvm_vcpu
*v
)
647 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
650 if ((!vcpu
->time_page
))
654 if (unlikely(vcpu
->hv_clock_tsc_khz
!= __get_cpu_var(cpu_tsc_khz
))) {
655 kvm_set_time_scale(__get_cpu_var(cpu_tsc_khz
), &vcpu
->hv_clock
);
656 vcpu
->hv_clock_tsc_khz
= __get_cpu_var(cpu_tsc_khz
);
660 /* Keep irq disabled to prevent changes to the clock */
661 local_irq_save(flags
);
662 kvm_get_msr(v
, MSR_IA32_TIME_STAMP_COUNTER
,
663 &vcpu
->hv_clock
.tsc_timestamp
);
665 local_irq_restore(flags
);
667 /* With all the info we got, fill in the values */
669 vcpu
->hv_clock
.system_time
= ts
.tv_nsec
+
670 (NSEC_PER_SEC
* (u64
)ts
.tv_sec
);
672 * The interface expects us to write an even number signaling that the
673 * update is finished. Since the guest won't see the intermediate
674 * state, we just increase by 2 at the end.
676 vcpu
->hv_clock
.version
+= 2;
678 shared_kaddr
= kmap_atomic(vcpu
->time_page
, KM_USER0
);
680 memcpy(shared_kaddr
+ vcpu
->time_offset
, &vcpu
->hv_clock
,
681 sizeof(vcpu
->hv_clock
));
683 kunmap_atomic(shared_kaddr
, KM_USER0
);
685 mark_page_dirty(v
->kvm
, vcpu
->time
>> PAGE_SHIFT
);
688 static int kvm_request_guest_time_update(struct kvm_vcpu
*v
)
690 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
692 if (!vcpu
->time_page
)
694 set_bit(KVM_REQ_KVMCLOCK_UPDATE
, &v
->requests
);
698 static bool msr_mtrr_valid(unsigned msr
)
701 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR
- 1:
702 case MSR_MTRRfix64K_00000
:
703 case MSR_MTRRfix16K_80000
:
704 case MSR_MTRRfix16K_A0000
:
705 case MSR_MTRRfix4K_C0000
:
706 case MSR_MTRRfix4K_C8000
:
707 case MSR_MTRRfix4K_D0000
:
708 case MSR_MTRRfix4K_D8000
:
709 case MSR_MTRRfix4K_E0000
:
710 case MSR_MTRRfix4K_E8000
:
711 case MSR_MTRRfix4K_F0000
:
712 case MSR_MTRRfix4K_F8000
:
713 case MSR_MTRRdefType
:
714 case MSR_IA32_CR_PAT
:
722 static bool valid_pat_type(unsigned t
)
724 return t
< 8 && (1 << t
) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
727 static bool valid_mtrr_type(unsigned t
)
729 return t
< 8 && (1 << t
) & 0x73; /* 0, 1, 4, 5, 6 */
732 static bool mtrr_valid(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
736 if (!msr_mtrr_valid(msr
))
739 if (msr
== MSR_IA32_CR_PAT
) {
740 for (i
= 0; i
< 8; i
++)
741 if (!valid_pat_type((data
>> (i
* 8)) & 0xff))
744 } else if (msr
== MSR_MTRRdefType
) {
747 return valid_mtrr_type(data
& 0xff);
748 } else if (msr
>= MSR_MTRRfix64K_00000
&& msr
<= MSR_MTRRfix4K_F8000
) {
749 for (i
= 0; i
< 8 ; i
++)
750 if (!valid_mtrr_type((data
>> (i
* 8)) & 0xff))
756 return valid_mtrr_type(data
& 0xff);
759 static int set_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
761 u64
*p
= (u64
*)&vcpu
->arch
.mtrr_state
.fixed_ranges
;
763 if (!mtrr_valid(vcpu
, msr
, data
))
766 if (msr
== MSR_MTRRdefType
) {
767 vcpu
->arch
.mtrr_state
.def_type
= data
;
768 vcpu
->arch
.mtrr_state
.enabled
= (data
& 0xc00) >> 10;
769 } else if (msr
== MSR_MTRRfix64K_00000
)
771 else if (msr
== MSR_MTRRfix16K_80000
|| msr
== MSR_MTRRfix16K_A0000
)
772 p
[1 + msr
- MSR_MTRRfix16K_80000
] = data
;
773 else if (msr
>= MSR_MTRRfix4K_C0000
&& msr
<= MSR_MTRRfix4K_F8000
)
774 p
[3 + msr
- MSR_MTRRfix4K_C0000
] = data
;
775 else if (msr
== MSR_IA32_CR_PAT
)
776 vcpu
->arch
.pat
= data
;
777 else { /* Variable MTRRs */
778 int idx
, is_mtrr_mask
;
781 idx
= (msr
- 0x200) / 2;
782 is_mtrr_mask
= msr
- 0x200 - 2 * idx
;
785 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].base_lo
;
788 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].mask_lo
;
792 kvm_mmu_reset_context(vcpu
);
796 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
800 set_efer(vcpu
, data
);
802 case MSR_IA32_MC0_STATUS
:
803 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
806 case MSR_IA32_MCG_STATUS
:
807 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
810 case MSR_IA32_MCG_CTL
:
811 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
814 case MSR_IA32_DEBUGCTLMSR
:
816 /* We support the non-activated case already */
818 } else if (data
& ~(DEBUGCTLMSR_LBR
| DEBUGCTLMSR_BTF
)) {
819 /* Values other than LBR and BTF are vendor-specific,
820 thus reserved and should throw a #GP */
823 pr_unimpl(vcpu
, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
826 case MSR_IA32_UCODE_REV
:
827 case MSR_IA32_UCODE_WRITE
:
828 case MSR_VM_HSAVE_PA
:
830 case 0x200 ... 0x2ff:
831 return set_msr_mtrr(vcpu
, msr
, data
);
832 case MSR_IA32_APICBASE
:
833 kvm_set_apic_base(vcpu
, data
);
835 case MSR_IA32_MISC_ENABLE
:
836 vcpu
->arch
.ia32_misc_enable_msr
= data
;
838 case MSR_KVM_WALL_CLOCK
:
839 vcpu
->kvm
->arch
.wall_clock
= data
;
840 kvm_write_wall_clock(vcpu
->kvm
, data
);
842 case MSR_KVM_SYSTEM_TIME
: {
843 if (vcpu
->arch
.time_page
) {
844 kvm_release_page_dirty(vcpu
->arch
.time_page
);
845 vcpu
->arch
.time_page
= NULL
;
848 vcpu
->arch
.time
= data
;
850 /* we verify if the enable bit is set... */
854 /* ...but clean it before doing the actual write */
855 vcpu
->arch
.time_offset
= data
& ~(PAGE_MASK
| 1);
857 vcpu
->arch
.time_page
=
858 gfn_to_page(vcpu
->kvm
, data
>> PAGE_SHIFT
);
860 if (is_error_page(vcpu
->arch
.time_page
)) {
861 kvm_release_page_clean(vcpu
->arch
.time_page
);
862 vcpu
->arch
.time_page
= NULL
;
865 kvm_request_guest_time_update(vcpu
);
869 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n", msr
, data
);
874 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
878 * Reads an msr value (of 'msr_index') into 'pdata'.
879 * Returns 0 on success, non-0 otherwise.
880 * Assumes vcpu_load() was already called.
882 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
884 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
887 static int get_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
889 u64
*p
= (u64
*)&vcpu
->arch
.mtrr_state
.fixed_ranges
;
891 if (!msr_mtrr_valid(msr
))
894 if (msr
== MSR_MTRRdefType
)
895 *pdata
= vcpu
->arch
.mtrr_state
.def_type
+
896 (vcpu
->arch
.mtrr_state
.enabled
<< 10);
897 else if (msr
== MSR_MTRRfix64K_00000
)
899 else if (msr
== MSR_MTRRfix16K_80000
|| msr
== MSR_MTRRfix16K_A0000
)
900 *pdata
= p
[1 + msr
- MSR_MTRRfix16K_80000
];
901 else if (msr
>= MSR_MTRRfix4K_C0000
&& msr
<= MSR_MTRRfix4K_F8000
)
902 *pdata
= p
[3 + msr
- MSR_MTRRfix4K_C0000
];
903 else if (msr
== MSR_IA32_CR_PAT
)
904 *pdata
= vcpu
->arch
.pat
;
905 else { /* Variable MTRRs */
906 int idx
, is_mtrr_mask
;
909 idx
= (msr
- 0x200) / 2;
910 is_mtrr_mask
= msr
- 0x200 - 2 * idx
;
913 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].base_lo
;
916 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].mask_lo
;
923 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
928 case 0xc0010010: /* SYSCFG */
929 case 0xc0010015: /* HWCR */
930 case MSR_IA32_PLATFORM_ID
:
931 case MSR_IA32_P5_MC_ADDR
:
932 case MSR_IA32_P5_MC_TYPE
:
933 case MSR_IA32_MC0_CTL
:
934 case MSR_IA32_MCG_STATUS
:
935 case MSR_IA32_MCG_CAP
:
936 case MSR_IA32_MCG_CTL
:
937 case MSR_IA32_MC0_MISC
:
938 case MSR_IA32_MC0_MISC
+4:
939 case MSR_IA32_MC0_MISC
+8:
940 case MSR_IA32_MC0_MISC
+12:
941 case MSR_IA32_MC0_MISC
+16:
942 case MSR_IA32_MC0_MISC
+20:
943 case MSR_IA32_UCODE_REV
:
944 case MSR_IA32_EBL_CR_POWERON
:
945 case MSR_IA32_DEBUGCTLMSR
:
946 case MSR_IA32_LASTBRANCHFROMIP
:
947 case MSR_IA32_LASTBRANCHTOIP
:
948 case MSR_IA32_LASTINTFROMIP
:
949 case MSR_IA32_LASTINTTOIP
:
950 case MSR_VM_HSAVE_PA
:
951 case MSR_P6_EVNTSEL0
:
952 case MSR_P6_EVNTSEL1
:
953 case MSR_K7_EVNTSEL0
:
957 data
= 0x500 | KVM_NR_VAR_MTRR
;
959 case 0x200 ... 0x2ff:
960 return get_msr_mtrr(vcpu
, msr
, pdata
);
961 case 0xcd: /* fsb frequency */
964 case MSR_IA32_APICBASE
:
965 data
= kvm_get_apic_base(vcpu
);
967 case MSR_IA32_MISC_ENABLE
:
968 data
= vcpu
->arch
.ia32_misc_enable_msr
;
970 case MSR_IA32_PERF_STATUS
:
971 /* TSC increment by tick */
974 data
|= (((uint64_t)4ULL) << 40);
977 data
= vcpu
->arch
.shadow_efer
;
979 case MSR_KVM_WALL_CLOCK
:
980 data
= vcpu
->kvm
->arch
.wall_clock
;
982 case MSR_KVM_SYSTEM_TIME
:
983 data
= vcpu
->arch
.time
;
986 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
992 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
995 * Read or write a bunch of msrs. All parameters are kernel addresses.
997 * @return number of msrs set successfully.
999 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
1000 struct kvm_msr_entry
*entries
,
1001 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1002 unsigned index
, u64
*data
))
1008 down_read(&vcpu
->kvm
->slots_lock
);
1009 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
1010 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
1012 up_read(&vcpu
->kvm
->slots_lock
);
1020 * Read or write a bunch of msrs. Parameters are user addresses.
1022 * @return number of msrs set successfully.
1024 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
1025 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1026 unsigned index
, u64
*data
),
1029 struct kvm_msrs msrs
;
1030 struct kvm_msr_entry
*entries
;
1035 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
1039 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
1043 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
1044 entries
= vmalloc(size
);
1049 if (copy_from_user(entries
, user_msrs
->entries
, size
))
1052 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
1057 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
1068 int kvm_dev_ioctl_check_extension(long ext
)
1073 case KVM_CAP_IRQCHIP
:
1075 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
1076 case KVM_CAP_SET_TSS_ADDR
:
1077 case KVM_CAP_EXT_CPUID
:
1078 case KVM_CAP_CLOCKSOURCE
:
1080 case KVM_CAP_NOP_IO_DELAY
:
1081 case KVM_CAP_MP_STATE
:
1082 case KVM_CAP_SYNC_MMU
:
1083 case KVM_CAP_REINJECT_CONTROL
:
1084 case KVM_CAP_IRQ_INJECT_STATUS
:
1087 case KVM_CAP_COALESCED_MMIO
:
1088 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
1091 r
= !kvm_x86_ops
->cpu_has_accelerated_tpr();
1093 case KVM_CAP_NR_VCPUS
:
1096 case KVM_CAP_NR_MEMSLOTS
:
1097 r
= KVM_MEMORY_SLOTS
;
1099 case KVM_CAP_PV_MMU
:
1113 long kvm_arch_dev_ioctl(struct file
*filp
,
1114 unsigned int ioctl
, unsigned long arg
)
1116 void __user
*argp
= (void __user
*)arg
;
1120 case KVM_GET_MSR_INDEX_LIST
: {
1121 struct kvm_msr_list __user
*user_msr_list
= argp
;
1122 struct kvm_msr_list msr_list
;
1126 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
1129 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
1130 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
1133 if (n
< msr_list
.nmsrs
)
1136 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
1137 num_msrs_to_save
* sizeof(u32
)))
1139 if (copy_to_user(user_msr_list
->indices
+ num_msrs_to_save
,
1141 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
1146 case KVM_GET_SUPPORTED_CPUID
: {
1147 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1148 struct kvm_cpuid2 cpuid
;
1151 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1153 r
= kvm_dev_ioctl_get_supported_cpuid(&cpuid
,
1154 cpuid_arg
->entries
);
1159 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1171 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1173 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
1174 kvm_request_guest_time_update(vcpu
);
1177 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
1179 kvm_x86_ops
->vcpu_put(vcpu
);
1180 kvm_put_guest_fpu(vcpu
);
1183 static int is_efer_nx(void)
1185 unsigned long long efer
= 0;
1187 rdmsrl_safe(MSR_EFER
, &efer
);
1188 return efer
& EFER_NX
;
1191 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
1194 struct kvm_cpuid_entry2
*e
, *entry
;
1197 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
1198 e
= &vcpu
->arch
.cpuid_entries
[i
];
1199 if (e
->function
== 0x80000001) {
1204 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
1205 entry
->edx
&= ~(1 << 20);
1206 printk(KERN_INFO
"kvm: guest NX capability removed\n");
1210 /* when an old userspace process fills a new kernel module */
1211 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
1212 struct kvm_cpuid
*cpuid
,
1213 struct kvm_cpuid_entry __user
*entries
)
1216 struct kvm_cpuid_entry
*cpuid_entries
;
1219 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
1222 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
1226 if (copy_from_user(cpuid_entries
, entries
,
1227 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
1229 for (i
= 0; i
< cpuid
->nent
; i
++) {
1230 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
1231 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
1232 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
1233 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
1234 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
1235 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
1236 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
1237 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
1238 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
1239 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
1241 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
1242 cpuid_fix_nx_cap(vcpu
);
1246 vfree(cpuid_entries
);
1251 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
1252 struct kvm_cpuid2
*cpuid
,
1253 struct kvm_cpuid_entry2 __user
*entries
)
1258 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
1261 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
1262 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
1264 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
1271 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
1272 struct kvm_cpuid2
*cpuid
,
1273 struct kvm_cpuid_entry2 __user
*entries
)
1278 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
1281 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
1282 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
1287 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
1291 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1294 entry
->function
= function
;
1295 entry
->index
= index
;
1296 cpuid_count(entry
->function
, entry
->index
,
1297 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
1301 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1302 u32 index
, int *nent
, int maxnent
)
1304 const u32 kvm_supported_word0_x86_features
= bit(X86_FEATURE_FPU
) |
1305 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1306 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1307 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1308 bit(X86_FEATURE_MCE
) |
1309 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1310 bit(X86_FEATURE_SEP
) | bit(X86_FEATURE_MTRR
) |
1311 bit(X86_FEATURE_PGE
) | bit(X86_FEATURE_MCA
) |
1312 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PAT
) |
1313 bit(X86_FEATURE_PSE36
) |
1314 bit(X86_FEATURE_CLFLSH
) | bit(X86_FEATURE_MMX
) |
1315 bit(X86_FEATURE_FXSR
) | bit(X86_FEATURE_XMM
) |
1316 bit(X86_FEATURE_XMM2
) | bit(X86_FEATURE_SELFSNOOP
);
1317 const u32 kvm_supported_word1_x86_features
= bit(X86_FEATURE_FPU
) |
1318 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1319 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1320 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1321 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1322 bit(X86_FEATURE_PGE
) |
1323 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1324 bit(X86_FEATURE_MMX
) | bit(X86_FEATURE_FXSR
) |
1325 bit(X86_FEATURE_SYSCALL
) |
1326 (is_efer_nx() ? bit(X86_FEATURE_NX
) : 0) |
1327 #ifdef CONFIG_X86_64
1328 bit(X86_FEATURE_LM
) |
1330 bit(X86_FEATURE_FXSR_OPT
) |
1331 bit(X86_FEATURE_MMXEXT
) |
1332 bit(X86_FEATURE_3DNOWEXT
) |
1333 bit(X86_FEATURE_3DNOW
);
1334 const u32 kvm_supported_word3_x86_features
=
1335 bit(X86_FEATURE_XMM3
) | bit(X86_FEATURE_CX16
);
1336 const u32 kvm_supported_word6_x86_features
=
1337 bit(X86_FEATURE_LAHF_LM
) | bit(X86_FEATURE_CMP_LEGACY
) |
1338 bit(X86_FEATURE_SVM
);
1340 /* all calls to cpuid_count() should be made on the same cpu */
1342 do_cpuid_1_ent(entry
, function
, index
);
1347 entry
->eax
= min(entry
->eax
, (u32
)0xb);
1350 entry
->edx
&= kvm_supported_word0_x86_features
;
1351 entry
->ecx
&= kvm_supported_word3_x86_features
;
1353 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1354 * may return different values. This forces us to get_cpu() before
1355 * issuing the first command, and also to emulate this annoying behavior
1356 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1358 int t
, times
= entry
->eax
& 0xff;
1360 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1361 entry
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
1362 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
1363 do_cpuid_1_ent(&entry
[t
], function
, 0);
1364 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1369 /* function 4 and 0xb have additional index. */
1373 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1374 /* read more entries until cache_type is zero */
1375 for (i
= 1; *nent
< maxnent
; ++i
) {
1376 cache_type
= entry
[i
- 1].eax
& 0x1f;
1379 do_cpuid_1_ent(&entry
[i
], function
, i
);
1381 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1389 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1390 /* read more entries until level_type is zero */
1391 for (i
= 1; *nent
< maxnent
; ++i
) {
1392 level_type
= entry
[i
- 1].ecx
& 0xff00;
1395 do_cpuid_1_ent(&entry
[i
], function
, i
);
1397 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1403 entry
->eax
= min(entry
->eax
, 0x8000001a);
1406 entry
->edx
&= kvm_supported_word1_x86_features
;
1407 entry
->ecx
&= kvm_supported_word6_x86_features
;
1413 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
1414 struct kvm_cpuid_entry2 __user
*entries
)
1416 struct kvm_cpuid_entry2
*cpuid_entries
;
1417 int limit
, nent
= 0, r
= -E2BIG
;
1420 if (cpuid
->nent
< 1)
1423 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
1427 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
1428 limit
= cpuid_entries
[0].eax
;
1429 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1430 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1431 &nent
, cpuid
->nent
);
1433 if (nent
>= cpuid
->nent
)
1436 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
1437 limit
= cpuid_entries
[nent
- 1].eax
;
1438 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1439 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1440 &nent
, cpuid
->nent
);
1442 if (nent
>= cpuid
->nent
)
1446 if (copy_to_user(entries
, cpuid_entries
,
1447 nent
* sizeof(struct kvm_cpuid_entry2
)))
1453 vfree(cpuid_entries
);
1458 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
1459 struct kvm_lapic_state
*s
)
1462 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
1468 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
1469 struct kvm_lapic_state
*s
)
1472 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1473 kvm_apic_post_state_restore(vcpu
);
1479 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1480 struct kvm_interrupt
*irq
)
1482 if (irq
->irq
< 0 || irq
->irq
>= 256)
1484 if (irqchip_in_kernel(vcpu
->kvm
))
1488 set_bit(irq
->irq
, vcpu
->arch
.irq_pending
);
1489 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
1496 static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu
*vcpu
)
1499 kvm_inject_nmi(vcpu
);
1505 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
1506 struct kvm_tpr_access_ctl
*tac
)
1510 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
1514 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1515 unsigned int ioctl
, unsigned long arg
)
1517 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1518 void __user
*argp
= (void __user
*)arg
;
1520 struct kvm_lapic_state
*lapic
= NULL
;
1523 case KVM_GET_LAPIC
: {
1524 lapic
= kzalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
1529 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, lapic
);
1533 if (copy_to_user(argp
, lapic
, sizeof(struct kvm_lapic_state
)))
1538 case KVM_SET_LAPIC
: {
1539 lapic
= kmalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
1544 if (copy_from_user(lapic
, argp
, sizeof(struct kvm_lapic_state
)))
1546 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, lapic
);
1552 case KVM_INTERRUPT
: {
1553 struct kvm_interrupt irq
;
1556 if (copy_from_user(&irq
, argp
, sizeof irq
))
1558 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1565 r
= kvm_vcpu_ioctl_nmi(vcpu
);
1571 case KVM_SET_CPUID
: {
1572 struct kvm_cpuid __user
*cpuid_arg
= argp
;
1573 struct kvm_cpuid cpuid
;
1576 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1578 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
1583 case KVM_SET_CPUID2
: {
1584 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1585 struct kvm_cpuid2 cpuid
;
1588 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1590 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
1591 cpuid_arg
->entries
);
1596 case KVM_GET_CPUID2
: {
1597 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1598 struct kvm_cpuid2 cpuid
;
1601 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1603 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
1604 cpuid_arg
->entries
);
1608 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1614 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
1617 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
1619 case KVM_TPR_ACCESS_REPORTING
: {
1620 struct kvm_tpr_access_ctl tac
;
1623 if (copy_from_user(&tac
, argp
, sizeof tac
))
1625 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
1629 if (copy_to_user(argp
, &tac
, sizeof tac
))
1634 case KVM_SET_VAPIC_ADDR
: {
1635 struct kvm_vapic_addr va
;
1638 if (!irqchip_in_kernel(vcpu
->kvm
))
1641 if (copy_from_user(&va
, argp
, sizeof va
))
1644 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
1656 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
1660 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
1662 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
1666 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
1667 u32 kvm_nr_mmu_pages
)
1669 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
1672 down_write(&kvm
->slots_lock
);
1673 spin_lock(&kvm
->mmu_lock
);
1675 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
1676 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
1678 spin_unlock(&kvm
->mmu_lock
);
1679 up_write(&kvm
->slots_lock
);
1683 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
1685 return kvm
->arch
.n_alloc_mmu_pages
;
1688 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1691 struct kvm_mem_alias
*alias
;
1693 for (i
= 0; i
< kvm
->arch
.naliases
; ++i
) {
1694 alias
= &kvm
->arch
.aliases
[i
];
1695 if (gfn
>= alias
->base_gfn
1696 && gfn
< alias
->base_gfn
+ alias
->npages
)
1697 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1703 * Set a new alias region. Aliases map a portion of physical memory into
1704 * another portion. This is useful for memory windows, for example the PC
1707 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
1708 struct kvm_memory_alias
*alias
)
1711 struct kvm_mem_alias
*p
;
1714 /* General sanity checks */
1715 if (alias
->memory_size
& (PAGE_SIZE
- 1))
1717 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
1719 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
1721 if (alias
->guest_phys_addr
+ alias
->memory_size
1722 < alias
->guest_phys_addr
)
1724 if (alias
->target_phys_addr
+ alias
->memory_size
1725 < alias
->target_phys_addr
)
1728 down_write(&kvm
->slots_lock
);
1729 spin_lock(&kvm
->mmu_lock
);
1731 p
= &kvm
->arch
.aliases
[alias
->slot
];
1732 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
1733 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
1734 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
1736 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
1737 if (kvm
->arch
.aliases
[n
- 1].npages
)
1739 kvm
->arch
.naliases
= n
;
1741 spin_unlock(&kvm
->mmu_lock
);
1742 kvm_mmu_zap_all(kvm
);
1744 up_write(&kvm
->slots_lock
);
1752 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1757 switch (chip
->chip_id
) {
1758 case KVM_IRQCHIP_PIC_MASTER
:
1759 memcpy(&chip
->chip
.pic
,
1760 &pic_irqchip(kvm
)->pics
[0],
1761 sizeof(struct kvm_pic_state
));
1763 case KVM_IRQCHIP_PIC_SLAVE
:
1764 memcpy(&chip
->chip
.pic
,
1765 &pic_irqchip(kvm
)->pics
[1],
1766 sizeof(struct kvm_pic_state
));
1768 case KVM_IRQCHIP_IOAPIC
:
1769 memcpy(&chip
->chip
.ioapic
,
1770 ioapic_irqchip(kvm
),
1771 sizeof(struct kvm_ioapic_state
));
1780 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1785 switch (chip
->chip_id
) {
1786 case KVM_IRQCHIP_PIC_MASTER
:
1787 memcpy(&pic_irqchip(kvm
)->pics
[0],
1789 sizeof(struct kvm_pic_state
));
1791 case KVM_IRQCHIP_PIC_SLAVE
:
1792 memcpy(&pic_irqchip(kvm
)->pics
[1],
1794 sizeof(struct kvm_pic_state
));
1796 case KVM_IRQCHIP_IOAPIC
:
1797 memcpy(ioapic_irqchip(kvm
),
1799 sizeof(struct kvm_ioapic_state
));
1805 kvm_pic_update_irq(pic_irqchip(kvm
));
1809 static int kvm_vm_ioctl_get_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1813 memcpy(ps
, &kvm
->arch
.vpit
->pit_state
, sizeof(struct kvm_pit_state
));
1817 static int kvm_vm_ioctl_set_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1821 memcpy(&kvm
->arch
.vpit
->pit_state
, ps
, sizeof(struct kvm_pit_state
));
1822 kvm_pit_load_count(kvm
, 0, ps
->channels
[0].count
);
1826 static int kvm_vm_ioctl_reinject(struct kvm
*kvm
,
1827 struct kvm_reinject_control
*control
)
1829 if (!kvm
->arch
.vpit
)
1831 kvm
->arch
.vpit
->pit_state
.pit_timer
.reinject
= control
->pit_reinject
;
1836 * Get (and clear) the dirty memory log for a memory slot.
1838 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1839 struct kvm_dirty_log
*log
)
1843 struct kvm_memory_slot
*memslot
;
1846 down_write(&kvm
->slots_lock
);
1848 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1852 /* If nothing is dirty, don't bother messing with page tables. */
1854 spin_lock(&kvm
->mmu_lock
);
1855 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
1856 spin_unlock(&kvm
->mmu_lock
);
1857 kvm_flush_remote_tlbs(kvm
);
1858 memslot
= &kvm
->memslots
[log
->slot
];
1859 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1860 memset(memslot
->dirty_bitmap
, 0, n
);
1864 up_write(&kvm
->slots_lock
);
1868 long kvm_arch_vm_ioctl(struct file
*filp
,
1869 unsigned int ioctl
, unsigned long arg
)
1871 struct kvm
*kvm
= filp
->private_data
;
1872 void __user
*argp
= (void __user
*)arg
;
1875 * This union makes it completely explicit to gcc-3.x
1876 * that these two variables' stack usage should be
1877 * combined, not added together.
1880 struct kvm_pit_state ps
;
1881 struct kvm_memory_alias alias
;
1885 case KVM_SET_TSS_ADDR
:
1886 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
1890 case KVM_SET_MEMORY_REGION
: {
1891 struct kvm_memory_region kvm_mem
;
1892 struct kvm_userspace_memory_region kvm_userspace_mem
;
1895 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
1897 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
1898 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
1899 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
1900 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
1901 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1906 case KVM_SET_NR_MMU_PAGES
:
1907 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1911 case KVM_GET_NR_MMU_PAGES
:
1912 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1914 case KVM_SET_MEMORY_ALIAS
:
1916 if (copy_from_user(&u
.alias
, argp
, sizeof(struct kvm_memory_alias
)))
1918 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &u
.alias
);
1922 case KVM_CREATE_IRQCHIP
:
1924 kvm
->arch
.vpic
= kvm_create_pic(kvm
);
1925 if (kvm
->arch
.vpic
) {
1926 r
= kvm_ioapic_init(kvm
);
1928 kfree(kvm
->arch
.vpic
);
1929 kvm
->arch
.vpic
= NULL
;
1934 r
= kvm_setup_default_irq_routing(kvm
);
1936 kfree(kvm
->arch
.vpic
);
1937 kfree(kvm
->arch
.vioapic
);
1941 case KVM_CREATE_PIT
:
1942 mutex_lock(&kvm
->lock
);
1945 goto create_pit_unlock
;
1947 kvm
->arch
.vpit
= kvm_create_pit(kvm
);
1951 mutex_unlock(&kvm
->lock
);
1953 case KVM_IRQ_LINE_STATUS
:
1954 case KVM_IRQ_LINE
: {
1955 struct kvm_irq_level irq_event
;
1958 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1960 if (irqchip_in_kernel(kvm
)) {
1962 mutex_lock(&kvm
->lock
);
1963 status
= kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
,
1964 irq_event
.irq
, irq_event
.level
);
1965 mutex_unlock(&kvm
->lock
);
1966 if (ioctl
== KVM_IRQ_LINE_STATUS
) {
1967 irq_event
.status
= status
;
1968 if (copy_to_user(argp
, &irq_event
,
1976 case KVM_GET_IRQCHIP
: {
1977 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1978 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
1984 if (copy_from_user(chip
, argp
, sizeof *chip
))
1985 goto get_irqchip_out
;
1987 if (!irqchip_in_kernel(kvm
))
1988 goto get_irqchip_out
;
1989 r
= kvm_vm_ioctl_get_irqchip(kvm
, chip
);
1991 goto get_irqchip_out
;
1993 if (copy_to_user(argp
, chip
, sizeof *chip
))
1994 goto get_irqchip_out
;
2002 case KVM_SET_IRQCHIP
: {
2003 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
2004 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
2010 if (copy_from_user(chip
, argp
, sizeof *chip
))
2011 goto set_irqchip_out
;
2013 if (!irqchip_in_kernel(kvm
))
2014 goto set_irqchip_out
;
2015 r
= kvm_vm_ioctl_set_irqchip(kvm
, chip
);
2017 goto set_irqchip_out
;
2027 if (copy_from_user(&u
.ps
, argp
, sizeof(struct kvm_pit_state
)))
2030 if (!kvm
->arch
.vpit
)
2032 r
= kvm_vm_ioctl_get_pit(kvm
, &u
.ps
);
2036 if (copy_to_user(argp
, &u
.ps
, sizeof(struct kvm_pit_state
)))
2043 if (copy_from_user(&u
.ps
, argp
, sizeof u
.ps
))
2046 if (!kvm
->arch
.vpit
)
2048 r
= kvm_vm_ioctl_set_pit(kvm
, &u
.ps
);
2054 case KVM_REINJECT_CONTROL
: {
2055 struct kvm_reinject_control control
;
2057 if (copy_from_user(&control
, argp
, sizeof(control
)))
2059 r
= kvm_vm_ioctl_reinject(kvm
, &control
);
2072 static void kvm_init_msr_list(void)
2077 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
2078 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
2081 msrs_to_save
[j
] = msrs_to_save
[i
];
2084 num_msrs_to_save
= j
;
2088 * Only apic need an MMIO device hook, so shortcut now..
2090 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
2091 gpa_t addr
, int len
,
2094 struct kvm_io_device
*dev
;
2096 if (vcpu
->arch
.apic
) {
2097 dev
= &vcpu
->arch
.apic
->dev
;
2098 if (dev
->in_range(dev
, addr
, len
, is_write
))
2105 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
2106 gpa_t addr
, int len
,
2109 struct kvm_io_device
*dev
;
2111 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
, len
, is_write
);
2113 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
, len
,
2118 static int kvm_read_guest_virt(gva_t addr
, void *val
, unsigned int bytes
,
2119 struct kvm_vcpu
*vcpu
)
2122 int r
= X86EMUL_CONTINUE
;
2125 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2126 unsigned offset
= addr
& (PAGE_SIZE
-1);
2127 unsigned toread
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
2130 if (gpa
== UNMAPPED_GVA
) {
2131 r
= X86EMUL_PROPAGATE_FAULT
;
2134 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, toread
);
2136 r
= X86EMUL_UNHANDLEABLE
;
2148 static int kvm_write_guest_virt(gva_t addr
, void *val
, unsigned int bytes
,
2149 struct kvm_vcpu
*vcpu
)
2152 int r
= X86EMUL_CONTINUE
;
2155 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2156 unsigned offset
= addr
& (PAGE_SIZE
-1);
2157 unsigned towrite
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
2160 if (gpa
== UNMAPPED_GVA
) {
2161 r
= X86EMUL_PROPAGATE_FAULT
;
2164 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, data
, towrite
);
2166 r
= X86EMUL_UNHANDLEABLE
;
2179 static int emulator_read_emulated(unsigned long addr
,
2182 struct kvm_vcpu
*vcpu
)
2184 struct kvm_io_device
*mmio_dev
;
2187 if (vcpu
->mmio_read_completed
) {
2188 memcpy(val
, vcpu
->mmio_data
, bytes
);
2189 vcpu
->mmio_read_completed
= 0;
2190 return X86EMUL_CONTINUE
;
2193 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2195 /* For APIC access vmexit */
2196 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
2199 if (kvm_read_guest_virt(addr
, val
, bytes
, vcpu
)
2200 == X86EMUL_CONTINUE
)
2201 return X86EMUL_CONTINUE
;
2202 if (gpa
== UNMAPPED_GVA
)
2203 return X86EMUL_PROPAGATE_FAULT
;
2207 * Is this MMIO handled locally?
2209 mutex_lock(&vcpu
->kvm
->lock
);
2210 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
, bytes
, 0);
2212 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
2213 mutex_unlock(&vcpu
->kvm
->lock
);
2214 return X86EMUL_CONTINUE
;
2216 mutex_unlock(&vcpu
->kvm
->lock
);
2218 vcpu
->mmio_needed
= 1;
2219 vcpu
->mmio_phys_addr
= gpa
;
2220 vcpu
->mmio_size
= bytes
;
2221 vcpu
->mmio_is_write
= 0;
2223 return X86EMUL_UNHANDLEABLE
;
2226 int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
2227 const void *val
, int bytes
)
2231 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
2234 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
, 1);
2238 static int emulator_write_emulated_onepage(unsigned long addr
,
2241 struct kvm_vcpu
*vcpu
)
2243 struct kvm_io_device
*mmio_dev
;
2246 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2248 if (gpa
== UNMAPPED_GVA
) {
2249 kvm_inject_page_fault(vcpu
, addr
, 2);
2250 return X86EMUL_PROPAGATE_FAULT
;
2253 /* For APIC access vmexit */
2254 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
2257 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
2258 return X86EMUL_CONTINUE
;
2262 * Is this MMIO handled locally?
2264 mutex_lock(&vcpu
->kvm
->lock
);
2265 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
, bytes
, 1);
2267 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
2268 mutex_unlock(&vcpu
->kvm
->lock
);
2269 return X86EMUL_CONTINUE
;
2271 mutex_unlock(&vcpu
->kvm
->lock
);
2273 vcpu
->mmio_needed
= 1;
2274 vcpu
->mmio_phys_addr
= gpa
;
2275 vcpu
->mmio_size
= bytes
;
2276 vcpu
->mmio_is_write
= 1;
2277 memcpy(vcpu
->mmio_data
, val
, bytes
);
2279 return X86EMUL_CONTINUE
;
2282 int emulator_write_emulated(unsigned long addr
,
2285 struct kvm_vcpu
*vcpu
)
2287 /* Crossing a page boundary? */
2288 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
2291 now
= -addr
& ~PAGE_MASK
;
2292 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
2293 if (rc
!= X86EMUL_CONTINUE
)
2299 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
2301 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
2303 static int emulator_cmpxchg_emulated(unsigned long addr
,
2307 struct kvm_vcpu
*vcpu
)
2309 static int reported
;
2313 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
2315 #ifndef CONFIG_X86_64
2316 /* guests cmpxchg8b have to be emulated atomically */
2323 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2325 if (gpa
== UNMAPPED_GVA
||
2326 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
2329 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
2334 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
2336 kaddr
= kmap_atomic(page
, KM_USER0
);
2337 set_64bit((u64
*)(kaddr
+ offset_in_page(gpa
)), val
);
2338 kunmap_atomic(kaddr
, KM_USER0
);
2339 kvm_release_page_dirty(page
);
2344 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
2347 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
2349 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
2352 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
2354 kvm_mmu_invlpg(vcpu
, address
);
2355 return X86EMUL_CONTINUE
;
2358 int emulate_clts(struct kvm_vcpu
*vcpu
)
2360 KVMTRACE_0D(CLTS
, vcpu
, handler
);
2361 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
& ~X86_CR0_TS
);
2362 return X86EMUL_CONTINUE
;
2365 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
2367 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
2371 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
2372 return X86EMUL_CONTINUE
;
2374 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __func__
, dr
);
2375 return X86EMUL_UNHANDLEABLE
;
2379 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
2381 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
2384 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
2386 /* FIXME: better handling */
2387 return X86EMUL_UNHANDLEABLE
;
2389 return X86EMUL_CONTINUE
;
2392 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
2395 unsigned long rip
= kvm_rip_read(vcpu
);
2396 unsigned long rip_linear
;
2398 if (!printk_ratelimit())
2401 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
2403 kvm_read_guest_virt(rip_linear
, (void *)opcodes
, 4, vcpu
);
2405 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2406 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
2408 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
2410 static struct x86_emulate_ops emulate_ops
= {
2411 .read_std
= kvm_read_guest_virt
,
2412 .read_emulated
= emulator_read_emulated
,
2413 .write_emulated
= emulator_write_emulated
,
2414 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
2417 static void cache_all_regs(struct kvm_vcpu
*vcpu
)
2419 kvm_register_read(vcpu
, VCPU_REGS_RAX
);
2420 kvm_register_read(vcpu
, VCPU_REGS_RSP
);
2421 kvm_register_read(vcpu
, VCPU_REGS_RIP
);
2422 vcpu
->arch
.regs_dirty
= ~0;
2425 int emulate_instruction(struct kvm_vcpu
*vcpu
,
2426 struct kvm_run
*run
,
2432 struct decode_cache
*c
;
2434 kvm_clear_exception_queue(vcpu
);
2435 vcpu
->arch
.mmio_fault_cr2
= cr2
;
2437 * TODO: fix x86_emulate.c to use guest_read/write_register
2438 * instead of direct ->regs accesses, can save hundred cycles
2439 * on Intel for instructions that don't read/change RSP, for
2442 cache_all_regs(vcpu
);
2444 vcpu
->mmio_is_write
= 0;
2445 vcpu
->arch
.pio
.string
= 0;
2447 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
2449 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
2451 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
2452 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
2453 vcpu
->arch
.emulate_ctxt
.mode
=
2454 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
2455 ? X86EMUL_MODE_REAL
: cs_l
2456 ? X86EMUL_MODE_PROT64
: cs_db
2457 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
2459 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2461 /* Reject the instructions other than VMCALL/VMMCALL when
2462 * try to emulate invalid opcode */
2463 c
= &vcpu
->arch
.emulate_ctxt
.decode
;
2464 if ((emulation_type
& EMULTYPE_TRAP_UD
) &&
2465 (!(c
->twobyte
&& c
->b
== 0x01 &&
2466 (c
->modrm_reg
== 0 || c
->modrm_reg
== 3) &&
2467 c
->modrm_mod
== 3 && c
->modrm_rm
== 1)))
2468 return EMULATE_FAIL
;
2470 ++vcpu
->stat
.insn_emulation
;
2472 ++vcpu
->stat
.insn_emulation_fail
;
2473 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2474 return EMULATE_DONE
;
2475 return EMULATE_FAIL
;
2479 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2480 shadow_mask
= vcpu
->arch
.emulate_ctxt
.interruptibility
;
2483 kvm_x86_ops
->set_interrupt_shadow(vcpu
, shadow_mask
);
2485 if (vcpu
->arch
.pio
.string
)
2486 return EMULATE_DO_MMIO
;
2488 if ((r
|| vcpu
->mmio_is_write
) && run
) {
2489 run
->exit_reason
= KVM_EXIT_MMIO
;
2490 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
2491 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
2492 run
->mmio
.len
= vcpu
->mmio_size
;
2493 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
2497 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2498 return EMULATE_DONE
;
2499 if (!vcpu
->mmio_needed
) {
2500 kvm_report_emulation_failure(vcpu
, "mmio");
2501 return EMULATE_FAIL
;
2503 return EMULATE_DO_MMIO
;
2506 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
2508 if (vcpu
->mmio_is_write
) {
2509 vcpu
->mmio_needed
= 0;
2510 return EMULATE_DO_MMIO
;
2513 return EMULATE_DONE
;
2515 EXPORT_SYMBOL_GPL(emulate_instruction
);
2517 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
2519 void *p
= vcpu
->arch
.pio_data
;
2520 gva_t q
= vcpu
->arch
.pio
.guest_gva
;
2524 bytes
= vcpu
->arch
.pio
.size
* vcpu
->arch
.pio
.cur_count
;
2525 if (vcpu
->arch
.pio
.in
)
2526 ret
= kvm_write_guest_virt(q
, p
, bytes
, vcpu
);
2528 ret
= kvm_read_guest_virt(q
, p
, bytes
, vcpu
);
2532 int complete_pio(struct kvm_vcpu
*vcpu
)
2534 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2541 val
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
2542 memcpy(&val
, vcpu
->arch
.pio_data
, io
->size
);
2543 kvm_register_write(vcpu
, VCPU_REGS_RAX
, val
);
2547 r
= pio_copy_data(vcpu
);
2554 delta
*= io
->cur_count
;
2556 * The size of the register should really depend on
2557 * current address size.
2559 val
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
2561 kvm_register_write(vcpu
, VCPU_REGS_RCX
, val
);
2567 val
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
2569 kvm_register_write(vcpu
, VCPU_REGS_RDI
, val
);
2571 val
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
2573 kvm_register_write(vcpu
, VCPU_REGS_RSI
, val
);
2577 io
->count
-= io
->cur_count
;
2583 static void kernel_pio(struct kvm_io_device
*pio_dev
,
2584 struct kvm_vcpu
*vcpu
,
2587 /* TODO: String I/O for in kernel device */
2589 mutex_lock(&vcpu
->kvm
->lock
);
2590 if (vcpu
->arch
.pio
.in
)
2591 kvm_iodevice_read(pio_dev
, vcpu
->arch
.pio
.port
,
2592 vcpu
->arch
.pio
.size
,
2595 kvm_iodevice_write(pio_dev
, vcpu
->arch
.pio
.port
,
2596 vcpu
->arch
.pio
.size
,
2598 mutex_unlock(&vcpu
->kvm
->lock
);
2601 static void pio_string_write(struct kvm_io_device
*pio_dev
,
2602 struct kvm_vcpu
*vcpu
)
2604 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2605 void *pd
= vcpu
->arch
.pio_data
;
2608 mutex_lock(&vcpu
->kvm
->lock
);
2609 for (i
= 0; i
< io
->cur_count
; i
++) {
2610 kvm_iodevice_write(pio_dev
, io
->port
,
2615 mutex_unlock(&vcpu
->kvm
->lock
);
2618 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
2619 gpa_t addr
, int len
,
2622 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
, len
, is_write
);
2625 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2626 int size
, unsigned port
)
2628 struct kvm_io_device
*pio_dev
;
2631 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2632 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2633 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2634 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2635 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= 1;
2636 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2637 vcpu
->arch
.pio
.in
= in
;
2638 vcpu
->arch
.pio
.string
= 0;
2639 vcpu
->arch
.pio
.down
= 0;
2640 vcpu
->arch
.pio
.rep
= 0;
2642 if (vcpu
->run
->io
.direction
== KVM_EXIT_IO_IN
)
2643 KVMTRACE_2D(IO_READ
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2646 KVMTRACE_2D(IO_WRITE
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2649 val
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
2650 memcpy(vcpu
->arch
.pio_data
, &val
, 4);
2652 pio_dev
= vcpu_find_pio_dev(vcpu
, port
, size
, !in
);
2654 kernel_pio(pio_dev
, vcpu
, vcpu
->arch
.pio_data
);
2660 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
2662 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2663 int size
, unsigned long count
, int down
,
2664 gva_t address
, int rep
, unsigned port
)
2666 unsigned now
, in_page
;
2668 struct kvm_io_device
*pio_dev
;
2670 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2671 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2672 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2673 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2674 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= count
;
2675 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2676 vcpu
->arch
.pio
.in
= in
;
2677 vcpu
->arch
.pio
.string
= 1;
2678 vcpu
->arch
.pio
.down
= down
;
2679 vcpu
->arch
.pio
.rep
= rep
;
2681 if (vcpu
->run
->io
.direction
== KVM_EXIT_IO_IN
)
2682 KVMTRACE_2D(IO_READ
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2685 KVMTRACE_2D(IO_WRITE
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2689 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2694 in_page
= PAGE_SIZE
- offset_in_page(address
);
2696 in_page
= offset_in_page(address
) + size
;
2697 now
= min(count
, (unsigned long)in_page
/ size
);
2702 * String I/O in reverse. Yuck. Kill the guest, fix later.
2704 pr_unimpl(vcpu
, "guest string pio down\n");
2705 kvm_inject_gp(vcpu
, 0);
2708 vcpu
->run
->io
.count
= now
;
2709 vcpu
->arch
.pio
.cur_count
= now
;
2711 if (vcpu
->arch
.pio
.cur_count
== vcpu
->arch
.pio
.count
)
2712 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2714 vcpu
->arch
.pio
.guest_gva
= address
;
2716 pio_dev
= vcpu_find_pio_dev(vcpu
, port
,
2717 vcpu
->arch
.pio
.cur_count
,
2718 !vcpu
->arch
.pio
.in
);
2719 if (!vcpu
->arch
.pio
.in
) {
2720 /* string PIO write */
2721 ret
= pio_copy_data(vcpu
);
2722 if (ret
== X86EMUL_PROPAGATE_FAULT
) {
2723 kvm_inject_gp(vcpu
, 0);
2726 if (ret
== 0 && pio_dev
) {
2727 pio_string_write(pio_dev
, vcpu
);
2729 if (vcpu
->arch
.pio
.count
== 0)
2733 pr_unimpl(vcpu
, "no string pio read support yet, "
2734 "port %x size %d count %ld\n",
2739 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2741 static void bounce_off(void *info
)
2746 static unsigned int ref_freq
;
2747 static unsigned long tsc_khz_ref
;
2749 static int kvmclock_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
2752 struct cpufreq_freqs
*freq
= data
;
2754 struct kvm_vcpu
*vcpu
;
2755 int i
, send_ipi
= 0;
2758 ref_freq
= freq
->old
;
2760 if (val
== CPUFREQ_PRECHANGE
&& freq
->old
> freq
->new)
2762 if (val
== CPUFREQ_POSTCHANGE
&& freq
->old
< freq
->new)
2764 per_cpu(cpu_tsc_khz
, freq
->cpu
) = cpufreq_scale(tsc_khz_ref
, ref_freq
, freq
->new);
2766 spin_lock(&kvm_lock
);
2767 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
2768 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2769 vcpu
= kvm
->vcpus
[i
];
2772 if (vcpu
->cpu
!= freq
->cpu
)
2774 if (!kvm_request_guest_time_update(vcpu
))
2776 if (vcpu
->cpu
!= smp_processor_id())
2780 spin_unlock(&kvm_lock
);
2782 if (freq
->old
< freq
->new && send_ipi
) {
2784 * We upscale the frequency. Must make the guest
2785 * doesn't see old kvmclock values while running with
2786 * the new frequency, otherwise we risk the guest sees
2787 * time go backwards.
2789 * In case we update the frequency for another cpu
2790 * (which might be in guest context) send an interrupt
2791 * to kick the cpu out of guest context. Next time
2792 * guest context is entered kvmclock will be updated,
2793 * so the guest will not see stale values.
2795 smp_call_function_single(freq
->cpu
, bounce_off
, NULL
, 1);
2800 static struct notifier_block kvmclock_cpufreq_notifier_block
= {
2801 .notifier_call
= kvmclock_cpufreq_notifier
2804 int kvm_arch_init(void *opaque
)
2807 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
2810 printk(KERN_ERR
"kvm: already loaded the other module\n");
2815 if (!ops
->cpu_has_kvm_support()) {
2816 printk(KERN_ERR
"kvm: no hardware support\n");
2820 if (ops
->disabled_by_bios()) {
2821 printk(KERN_ERR
"kvm: disabled by bios\n");
2826 r
= kvm_mmu_module_init();
2830 kvm_init_msr_list();
2833 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2834 kvm_mmu_set_base_ptes(PT_PRESENT_MASK
);
2835 kvm_mmu_set_mask_ptes(PT_USER_MASK
, PT_ACCESSED_MASK
,
2836 PT_DIRTY_MASK
, PT64_NX_MASK
, 0, 0);
2838 for_each_possible_cpu(cpu
)
2839 per_cpu(cpu_tsc_khz
, cpu
) = tsc_khz
;
2840 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
)) {
2841 tsc_khz_ref
= tsc_khz
;
2842 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block
,
2843 CPUFREQ_TRANSITION_NOTIFIER
);
2852 void kvm_arch_exit(void)
2854 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
))
2855 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block
,
2856 CPUFREQ_TRANSITION_NOTIFIER
);
2858 kvm_mmu_module_exit();
2861 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
2863 ++vcpu
->stat
.halt_exits
;
2864 KVMTRACE_0D(HLT
, vcpu
, handler
);
2865 if (irqchip_in_kernel(vcpu
->kvm
)) {
2866 vcpu
->arch
.mp_state
= KVM_MP_STATE_HALTED
;
2869 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
2873 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
2875 static inline gpa_t
hc_gpa(struct kvm_vcpu
*vcpu
, unsigned long a0
,
2878 if (is_long_mode(vcpu
))
2881 return a0
| ((gpa_t
)a1
<< 32);
2884 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
2886 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
2889 nr
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
2890 a0
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
2891 a1
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
2892 a2
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
2893 a3
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
2895 KVMTRACE_1D(VMMCALL
, vcpu
, (u32
)nr
, handler
);
2897 if (!is_long_mode(vcpu
)) {
2905 if (kvm_x86_ops
->get_cpl(vcpu
) != 0) {
2911 case KVM_HC_VAPIC_POLL_IRQ
:
2915 r
= kvm_pv_mmu_op(vcpu
, a0
, hc_gpa(vcpu
, a1
, a2
), &ret
);
2922 kvm_register_write(vcpu
, VCPU_REGS_RAX
, ret
);
2923 ++vcpu
->stat
.hypercalls
;
2926 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
2928 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
2930 char instruction
[3];
2932 unsigned long rip
= kvm_rip_read(vcpu
);
2936 * Blow out the MMU to ensure that no other VCPU has an active mapping
2937 * to ensure that the updated hypercall appears atomically across all
2940 kvm_mmu_zap_all(vcpu
->kvm
);
2942 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
2943 if (emulator_write_emulated(rip
, instruction
, 3, vcpu
)
2944 != X86EMUL_CONTINUE
)
2950 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
2952 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
2955 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2957 struct descriptor_table dt
= { limit
, base
};
2959 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2962 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2964 struct descriptor_table dt
= { limit
, base
};
2966 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2969 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
2970 unsigned long *rflags
)
2972 kvm_lmsw(vcpu
, msw
);
2973 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2976 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
2978 unsigned long value
;
2980 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2983 value
= vcpu
->arch
.cr0
;
2986 value
= vcpu
->arch
.cr2
;
2989 value
= vcpu
->arch
.cr3
;
2992 value
= vcpu
->arch
.cr4
;
2995 value
= kvm_get_cr8(vcpu
);
2998 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
3001 KVMTRACE_3D(CR_READ
, vcpu
, (u32
)cr
, (u32
)value
,
3002 (u32
)((u64
)value
>> 32), handler
);
3007 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
3008 unsigned long *rflags
)
3010 KVMTRACE_3D(CR_WRITE
, vcpu
, (u32
)cr
, (u32
)val
,
3011 (u32
)((u64
)val
>> 32), handler
);
3015 kvm_set_cr0(vcpu
, mk_cr_64(vcpu
->arch
.cr0
, val
));
3016 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
3019 vcpu
->arch
.cr2
= val
;
3022 kvm_set_cr3(vcpu
, val
);
3025 kvm_set_cr4(vcpu
, mk_cr_64(vcpu
->arch
.cr4
, val
));
3028 kvm_set_cr8(vcpu
, val
& 0xfUL
);
3031 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
3035 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
3037 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
3038 int j
, nent
= vcpu
->arch
.cpuid_nent
;
3040 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
3041 /* when no next entry is found, the current entry[i] is reselected */
3042 for (j
= i
+ 1; ; j
= (j
+ 1) % nent
) {
3043 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
3044 if (ej
->function
== e
->function
) {
3045 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
3049 return 0; /* silence gcc, even though control never reaches here */
3052 /* find an entry with matching function, matching index (if needed), and that
3053 * should be read next (if it's stateful) */
3054 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
3055 u32 function
, u32 index
)
3057 if (e
->function
!= function
)
3059 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
3061 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
3062 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
3067 struct kvm_cpuid_entry2
*kvm_find_cpuid_entry(struct kvm_vcpu
*vcpu
,
3068 u32 function
, u32 index
)
3071 struct kvm_cpuid_entry2
*best
= NULL
;
3073 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
3074 struct kvm_cpuid_entry2
*e
;
3076 e
= &vcpu
->arch
.cpuid_entries
[i
];
3077 if (is_matching_cpuid_entry(e
, function
, index
)) {
3078 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
3079 move_to_next_stateful_cpuid_entry(vcpu
, i
);
3084 * Both basic or both extended?
3086 if (((e
->function
^ function
) & 0x80000000) == 0)
3087 if (!best
|| e
->function
> best
->function
)
3093 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
3095 u32 function
, index
;
3096 struct kvm_cpuid_entry2
*best
;
3098 function
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3099 index
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
3100 kvm_register_write(vcpu
, VCPU_REGS_RAX
, 0);
3101 kvm_register_write(vcpu
, VCPU_REGS_RBX
, 0);
3102 kvm_register_write(vcpu
, VCPU_REGS_RCX
, 0);
3103 kvm_register_write(vcpu
, VCPU_REGS_RDX
, 0);
3104 best
= kvm_find_cpuid_entry(vcpu
, function
, index
);
3106 kvm_register_write(vcpu
, VCPU_REGS_RAX
, best
->eax
);
3107 kvm_register_write(vcpu
, VCPU_REGS_RBX
, best
->ebx
);
3108 kvm_register_write(vcpu
, VCPU_REGS_RCX
, best
->ecx
);
3109 kvm_register_write(vcpu
, VCPU_REGS_RDX
, best
->edx
);
3111 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
3112 KVMTRACE_5D(CPUID
, vcpu
, function
,
3113 (u32
)kvm_register_read(vcpu
, VCPU_REGS_RAX
),
3114 (u32
)kvm_register_read(vcpu
, VCPU_REGS_RBX
),
3115 (u32
)kvm_register_read(vcpu
, VCPU_REGS_RCX
),
3116 (u32
)kvm_register_read(vcpu
, VCPU_REGS_RDX
), handler
);
3118 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
3121 * Check if userspace requested an interrupt window, and that the
3122 * interrupt window is open.
3124 * No need to exit to userspace if we already have an interrupt queued.
3126 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
3127 struct kvm_run
*kvm_run
)
3129 return (!vcpu
->arch
.irq_summary
&&
3130 kvm_run
->request_interrupt_window
&&
3131 vcpu
->arch
.interrupt_window_open
&&
3132 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
3135 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
3136 struct kvm_run
*kvm_run
)
3138 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
3139 kvm_run
->cr8
= kvm_get_cr8(vcpu
);
3140 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
3141 if (irqchip_in_kernel(vcpu
->kvm
))
3142 kvm_run
->ready_for_interrupt_injection
= 1;
3144 kvm_run
->ready_for_interrupt_injection
=
3145 (vcpu
->arch
.interrupt_window_open
&&
3146 vcpu
->arch
.irq_summary
== 0);
3149 static void vapic_enter(struct kvm_vcpu
*vcpu
)
3151 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3154 if (!apic
|| !apic
->vapic_addr
)
3157 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
3159 vcpu
->arch
.apic
->vapic_page
= page
;
3162 static void vapic_exit(struct kvm_vcpu
*vcpu
)
3164 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3166 if (!apic
|| !apic
->vapic_addr
)
3169 down_read(&vcpu
->kvm
->slots_lock
);
3170 kvm_release_page_dirty(apic
->vapic_page
);
3171 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
3172 up_read(&vcpu
->kvm
->slots_lock
);
3175 static int vcpu_enter_guest(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3180 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
3181 kvm_mmu_unload(vcpu
);
3183 r
= kvm_mmu_reload(vcpu
);
3187 if (vcpu
->requests
) {
3188 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
3189 __kvm_migrate_timers(vcpu
);
3190 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE
, &vcpu
->requests
))
3191 kvm_write_guest_time(vcpu
);
3192 if (test_and_clear_bit(KVM_REQ_MMU_SYNC
, &vcpu
->requests
))
3193 kvm_mmu_sync_roots(vcpu
);
3194 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
3195 kvm_x86_ops
->tlb_flush(vcpu
);
3196 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
3198 kvm_run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
3202 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
)) {
3203 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
3209 clear_bit(KVM_REQ_PENDING_TIMER
, &vcpu
->requests
);
3210 kvm_inject_pending_timer_irqs(vcpu
);
3214 kvm_x86_ops
->prepare_guest_switch(vcpu
);
3215 kvm_load_guest_fpu(vcpu
);
3217 local_irq_disable();
3219 if (vcpu
->requests
|| need_resched() || signal_pending(current
)) {
3226 vcpu
->guest_mode
= 1;
3228 * Make sure that guest_mode assignment won't happen after
3229 * testing the pending IRQ vector bitmap.
3233 if (vcpu
->arch
.exception
.pending
)
3234 __queue_exception(vcpu
);
3235 else if (irqchip_in_kernel(vcpu
->kvm
))
3236 kvm_x86_ops
->inject_pending_irq(vcpu
);
3238 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
3240 kvm_lapic_sync_to_vapic(vcpu
);
3242 up_read(&vcpu
->kvm
->slots_lock
);
3246 get_debugreg(vcpu
->arch
.host_dr6
, 6);
3247 get_debugreg(vcpu
->arch
.host_dr7
, 7);
3248 if (unlikely(vcpu
->arch
.switch_db_regs
)) {
3249 get_debugreg(vcpu
->arch
.host_db
[0], 0);
3250 get_debugreg(vcpu
->arch
.host_db
[1], 1);
3251 get_debugreg(vcpu
->arch
.host_db
[2], 2);
3252 get_debugreg(vcpu
->arch
.host_db
[3], 3);
3255 set_debugreg(vcpu
->arch
.eff_db
[0], 0);
3256 set_debugreg(vcpu
->arch
.eff_db
[1], 1);
3257 set_debugreg(vcpu
->arch
.eff_db
[2], 2);
3258 set_debugreg(vcpu
->arch
.eff_db
[3], 3);
3261 KVMTRACE_0D(VMENTRY
, vcpu
, entryexit
);
3262 kvm_x86_ops
->run(vcpu
, kvm_run
);
3264 if (unlikely(vcpu
->arch
.switch_db_regs
)) {
3266 set_debugreg(vcpu
->arch
.host_db
[0], 0);
3267 set_debugreg(vcpu
->arch
.host_db
[1], 1);
3268 set_debugreg(vcpu
->arch
.host_db
[2], 2);
3269 set_debugreg(vcpu
->arch
.host_db
[3], 3);
3271 set_debugreg(vcpu
->arch
.host_dr6
, 6);
3272 set_debugreg(vcpu
->arch
.host_dr7
, 7);
3274 vcpu
->guest_mode
= 0;
3280 * We must have an instruction between local_irq_enable() and
3281 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3282 * the interrupt shadow. The stat.exits increment will do nicely.
3283 * But we need to prevent reordering, hence this barrier():
3291 down_read(&vcpu
->kvm
->slots_lock
);
3294 * Profile KVM exit RIPs:
3296 if (unlikely(prof_on
== KVM_PROFILING
)) {
3297 unsigned long rip
= kvm_rip_read(vcpu
);
3298 profile_hit(KVM_PROFILING
, (void *)rip
);
3301 if (vcpu
->arch
.exception
.pending
&& kvm_x86_ops
->exception_injected(vcpu
))
3302 vcpu
->arch
.exception
.pending
= false;
3304 kvm_lapic_sync_from_vapic(vcpu
);
3306 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
3311 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3315 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
)) {
3316 pr_debug("vcpu %d received sipi with vector # %x\n",
3317 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
3318 kvm_lapic_reset(vcpu
);
3319 r
= kvm_arch_vcpu_reset(vcpu
);
3322 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
3325 down_read(&vcpu
->kvm
->slots_lock
);
3330 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
)
3331 r
= vcpu_enter_guest(vcpu
, kvm_run
);
3333 up_read(&vcpu
->kvm
->slots_lock
);
3334 kvm_vcpu_block(vcpu
);
3335 down_read(&vcpu
->kvm
->slots_lock
);
3336 if (test_and_clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
))
3337 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_HALTED
)
3338 vcpu
->arch
.mp_state
=
3339 KVM_MP_STATE_RUNNABLE
;
3340 if (vcpu
->arch
.mp_state
!= KVM_MP_STATE_RUNNABLE
)
3345 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
3347 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
3348 ++vcpu
->stat
.request_irq_exits
;
3350 if (signal_pending(current
)) {
3352 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
3353 ++vcpu
->stat
.signal_exits
;
3355 if (need_resched()) {
3356 up_read(&vcpu
->kvm
->slots_lock
);
3358 down_read(&vcpu
->kvm
->slots_lock
);
3363 up_read(&vcpu
->kvm
->slots_lock
);
3364 post_kvm_run_save(vcpu
, kvm_run
);
3371 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3378 if (vcpu
->sigset_active
)
3379 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
3381 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
)) {
3382 kvm_vcpu_block(vcpu
);
3383 clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
3388 /* re-sync apic's tpr */
3389 if (!irqchip_in_kernel(vcpu
->kvm
))
3390 kvm_set_cr8(vcpu
, kvm_run
->cr8
);
3392 if (vcpu
->arch
.pio
.cur_count
) {
3393 r
= complete_pio(vcpu
);
3397 #if CONFIG_HAS_IOMEM
3398 if (vcpu
->mmio_needed
) {
3399 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
3400 vcpu
->mmio_read_completed
= 1;
3401 vcpu
->mmio_needed
= 0;
3403 down_read(&vcpu
->kvm
->slots_lock
);
3404 r
= emulate_instruction(vcpu
, kvm_run
,
3405 vcpu
->arch
.mmio_fault_cr2
, 0,
3406 EMULTYPE_NO_DECODE
);
3407 up_read(&vcpu
->kvm
->slots_lock
);
3408 if (r
== EMULATE_DO_MMIO
) {
3410 * Read-modify-write. Back to userspace.
3417 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
)
3418 kvm_register_write(vcpu
, VCPU_REGS_RAX
,
3419 kvm_run
->hypercall
.ret
);
3421 r
= __vcpu_run(vcpu
, kvm_run
);
3424 if (vcpu
->sigset_active
)
3425 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
3431 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
3435 regs
->rax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3436 regs
->rbx
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
3437 regs
->rcx
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
3438 regs
->rdx
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
3439 regs
->rsi
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
3440 regs
->rdi
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
3441 regs
->rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
3442 regs
->rbp
= kvm_register_read(vcpu
, VCPU_REGS_RBP
);
3443 #ifdef CONFIG_X86_64
3444 regs
->r8
= kvm_register_read(vcpu
, VCPU_REGS_R8
);
3445 regs
->r9
= kvm_register_read(vcpu
, VCPU_REGS_R9
);
3446 regs
->r10
= kvm_register_read(vcpu
, VCPU_REGS_R10
);
3447 regs
->r11
= kvm_register_read(vcpu
, VCPU_REGS_R11
);
3448 regs
->r12
= kvm_register_read(vcpu
, VCPU_REGS_R12
);
3449 regs
->r13
= kvm_register_read(vcpu
, VCPU_REGS_R13
);
3450 regs
->r14
= kvm_register_read(vcpu
, VCPU_REGS_R14
);
3451 regs
->r15
= kvm_register_read(vcpu
, VCPU_REGS_R15
);
3454 regs
->rip
= kvm_rip_read(vcpu
);
3455 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
3458 * Don't leak debug flags in case they were set for guest debugging
3460 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
3461 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
3468 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
3472 kvm_register_write(vcpu
, VCPU_REGS_RAX
, regs
->rax
);
3473 kvm_register_write(vcpu
, VCPU_REGS_RBX
, regs
->rbx
);
3474 kvm_register_write(vcpu
, VCPU_REGS_RCX
, regs
->rcx
);
3475 kvm_register_write(vcpu
, VCPU_REGS_RDX
, regs
->rdx
);
3476 kvm_register_write(vcpu
, VCPU_REGS_RSI
, regs
->rsi
);
3477 kvm_register_write(vcpu
, VCPU_REGS_RDI
, regs
->rdi
);
3478 kvm_register_write(vcpu
, VCPU_REGS_RSP
, regs
->rsp
);
3479 kvm_register_write(vcpu
, VCPU_REGS_RBP
, regs
->rbp
);
3480 #ifdef CONFIG_X86_64
3481 kvm_register_write(vcpu
, VCPU_REGS_R8
, regs
->r8
);
3482 kvm_register_write(vcpu
, VCPU_REGS_R9
, regs
->r9
);
3483 kvm_register_write(vcpu
, VCPU_REGS_R10
, regs
->r10
);
3484 kvm_register_write(vcpu
, VCPU_REGS_R11
, regs
->r11
);
3485 kvm_register_write(vcpu
, VCPU_REGS_R12
, regs
->r12
);
3486 kvm_register_write(vcpu
, VCPU_REGS_R13
, regs
->r13
);
3487 kvm_register_write(vcpu
, VCPU_REGS_R14
, regs
->r14
);
3488 kvm_register_write(vcpu
, VCPU_REGS_R15
, regs
->r15
);
3492 kvm_rip_write(vcpu
, regs
->rip
);
3493 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
3496 vcpu
->arch
.exception
.pending
= false;
3503 void kvm_get_segment(struct kvm_vcpu
*vcpu
,
3504 struct kvm_segment
*var
, int seg
)
3506 kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
3509 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
3511 struct kvm_segment cs
;
3513 kvm_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
3517 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
3519 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
3520 struct kvm_sregs
*sregs
)
3522 struct descriptor_table dt
;
3527 kvm_get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3528 kvm_get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3529 kvm_get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3530 kvm_get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3531 kvm_get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3532 kvm_get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3534 kvm_get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3535 kvm_get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3537 kvm_x86_ops
->get_idt(vcpu
, &dt
);
3538 sregs
->idt
.limit
= dt
.limit
;
3539 sregs
->idt
.base
= dt
.base
;
3540 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
3541 sregs
->gdt
.limit
= dt
.limit
;
3542 sregs
->gdt
.base
= dt
.base
;
3544 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3545 sregs
->cr0
= vcpu
->arch
.cr0
;
3546 sregs
->cr2
= vcpu
->arch
.cr2
;
3547 sregs
->cr3
= vcpu
->arch
.cr3
;
3548 sregs
->cr4
= vcpu
->arch
.cr4
;
3549 sregs
->cr8
= kvm_get_cr8(vcpu
);
3550 sregs
->efer
= vcpu
->arch
.shadow_efer
;
3551 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
3553 if (irqchip_in_kernel(vcpu
->kvm
)) {
3554 memset(sregs
->interrupt_bitmap
, 0,
3555 sizeof sregs
->interrupt_bitmap
);
3556 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
3557 if (pending_vec
>= 0)
3558 set_bit(pending_vec
,
3559 (unsigned long *)sregs
->interrupt_bitmap
);
3561 memcpy(sregs
->interrupt_bitmap
, vcpu
->arch
.irq_pending
,
3562 sizeof sregs
->interrupt_bitmap
);
3569 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
3570 struct kvm_mp_state
*mp_state
)
3573 mp_state
->mp_state
= vcpu
->arch
.mp_state
;
3578 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
3579 struct kvm_mp_state
*mp_state
)
3582 vcpu
->arch
.mp_state
= mp_state
->mp_state
;
3587 static void kvm_set_segment(struct kvm_vcpu
*vcpu
,
3588 struct kvm_segment
*var
, int seg
)
3590 kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
3593 static void seg_desct_to_kvm_desct(struct desc_struct
*seg_desc
, u16 selector
,
3594 struct kvm_segment
*kvm_desct
)
3596 kvm_desct
->base
= seg_desc
->base0
;
3597 kvm_desct
->base
|= seg_desc
->base1
<< 16;
3598 kvm_desct
->base
|= seg_desc
->base2
<< 24;
3599 kvm_desct
->limit
= seg_desc
->limit0
;
3600 kvm_desct
->limit
|= seg_desc
->limit
<< 16;
3602 kvm_desct
->limit
<<= 12;
3603 kvm_desct
->limit
|= 0xfff;
3605 kvm_desct
->selector
= selector
;
3606 kvm_desct
->type
= seg_desc
->type
;
3607 kvm_desct
->present
= seg_desc
->p
;
3608 kvm_desct
->dpl
= seg_desc
->dpl
;
3609 kvm_desct
->db
= seg_desc
->d
;
3610 kvm_desct
->s
= seg_desc
->s
;
3611 kvm_desct
->l
= seg_desc
->l
;
3612 kvm_desct
->g
= seg_desc
->g
;
3613 kvm_desct
->avl
= seg_desc
->avl
;
3615 kvm_desct
->unusable
= 1;
3617 kvm_desct
->unusable
= 0;
3618 kvm_desct
->padding
= 0;
3621 static void get_segment_descriptor_dtable(struct kvm_vcpu
*vcpu
,
3623 struct descriptor_table
*dtable
)
3625 if (selector
& 1 << 2) {
3626 struct kvm_segment kvm_seg
;
3628 kvm_get_segment(vcpu
, &kvm_seg
, VCPU_SREG_LDTR
);
3630 if (kvm_seg
.unusable
)
3633 dtable
->limit
= kvm_seg
.limit
;
3634 dtable
->base
= kvm_seg
.base
;
3637 kvm_x86_ops
->get_gdt(vcpu
, dtable
);
3640 /* allowed just for 8 bytes segments */
3641 static int load_guest_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3642 struct desc_struct
*seg_desc
)
3645 struct descriptor_table dtable
;
3646 u16 index
= selector
>> 3;
3648 get_segment_descriptor_dtable(vcpu
, selector
, &dtable
);
3650 if (dtable
.limit
< index
* 8 + 7) {
3651 kvm_queue_exception_e(vcpu
, GP_VECTOR
, selector
& 0xfffc);
3654 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, dtable
.base
);
3656 return kvm_read_guest(vcpu
->kvm
, gpa
, seg_desc
, 8);
3659 /* allowed just for 8 bytes segments */
3660 static int save_guest_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3661 struct desc_struct
*seg_desc
)
3664 struct descriptor_table dtable
;
3665 u16 index
= selector
>> 3;
3667 get_segment_descriptor_dtable(vcpu
, selector
, &dtable
);
3669 if (dtable
.limit
< index
* 8 + 7)
3671 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, dtable
.base
);
3673 return kvm_write_guest(vcpu
->kvm
, gpa
, seg_desc
, 8);
3676 static u32
get_tss_base_addr(struct kvm_vcpu
*vcpu
,
3677 struct desc_struct
*seg_desc
)
3681 base_addr
= seg_desc
->base0
;
3682 base_addr
|= (seg_desc
->base1
<< 16);
3683 base_addr
|= (seg_desc
->base2
<< 24);
3685 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, base_addr
);
3688 static u16
get_segment_selector(struct kvm_vcpu
*vcpu
, int seg
)
3690 struct kvm_segment kvm_seg
;
3692 kvm_get_segment(vcpu
, &kvm_seg
, seg
);
3693 return kvm_seg
.selector
;
3696 static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu
*vcpu
,
3698 struct kvm_segment
*kvm_seg
)
3700 struct desc_struct seg_desc
;
3702 if (load_guest_segment_descriptor(vcpu
, selector
, &seg_desc
))
3704 seg_desct_to_kvm_desct(&seg_desc
, selector
, kvm_seg
);
3708 static int kvm_load_realmode_segment(struct kvm_vcpu
*vcpu
, u16 selector
, int seg
)
3710 struct kvm_segment segvar
= {
3711 .base
= selector
<< 4,
3713 .selector
= selector
,
3724 kvm_x86_ops
->set_segment(vcpu
, &segvar
, seg
);
3728 int kvm_load_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3729 int type_bits
, int seg
)
3731 struct kvm_segment kvm_seg
;
3733 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
))
3734 return kvm_load_realmode_segment(vcpu
, selector
, seg
);
3735 if (load_segment_descriptor_to_kvm_desct(vcpu
, selector
, &kvm_seg
))
3737 kvm_seg
.type
|= type_bits
;
3739 if (seg
!= VCPU_SREG_SS
&& seg
!= VCPU_SREG_CS
&&
3740 seg
!= VCPU_SREG_LDTR
)
3742 kvm_seg
.unusable
= 1;
3744 kvm_set_segment(vcpu
, &kvm_seg
, seg
);
3748 static void save_state_to_tss32(struct kvm_vcpu
*vcpu
,
3749 struct tss_segment_32
*tss
)
3751 tss
->cr3
= vcpu
->arch
.cr3
;
3752 tss
->eip
= kvm_rip_read(vcpu
);
3753 tss
->eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3754 tss
->eax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3755 tss
->ecx
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
3756 tss
->edx
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
3757 tss
->ebx
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
3758 tss
->esp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
3759 tss
->ebp
= kvm_register_read(vcpu
, VCPU_REGS_RBP
);
3760 tss
->esi
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
3761 tss
->edi
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
3762 tss
->es
= get_segment_selector(vcpu
, VCPU_SREG_ES
);
3763 tss
->cs
= get_segment_selector(vcpu
, VCPU_SREG_CS
);
3764 tss
->ss
= get_segment_selector(vcpu
, VCPU_SREG_SS
);
3765 tss
->ds
= get_segment_selector(vcpu
, VCPU_SREG_DS
);
3766 tss
->fs
= get_segment_selector(vcpu
, VCPU_SREG_FS
);
3767 tss
->gs
= get_segment_selector(vcpu
, VCPU_SREG_GS
);
3768 tss
->ldt_selector
= get_segment_selector(vcpu
, VCPU_SREG_LDTR
);
3769 tss
->prev_task_link
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
3772 static int load_state_from_tss32(struct kvm_vcpu
*vcpu
,
3773 struct tss_segment_32
*tss
)
3775 kvm_set_cr3(vcpu
, tss
->cr3
);
3777 kvm_rip_write(vcpu
, tss
->eip
);
3778 kvm_x86_ops
->set_rflags(vcpu
, tss
->eflags
| 2);
3780 kvm_register_write(vcpu
, VCPU_REGS_RAX
, tss
->eax
);
3781 kvm_register_write(vcpu
, VCPU_REGS_RCX
, tss
->ecx
);
3782 kvm_register_write(vcpu
, VCPU_REGS_RDX
, tss
->edx
);
3783 kvm_register_write(vcpu
, VCPU_REGS_RBX
, tss
->ebx
);
3784 kvm_register_write(vcpu
, VCPU_REGS_RSP
, tss
->esp
);
3785 kvm_register_write(vcpu
, VCPU_REGS_RBP
, tss
->ebp
);
3786 kvm_register_write(vcpu
, VCPU_REGS_RSI
, tss
->esi
);
3787 kvm_register_write(vcpu
, VCPU_REGS_RDI
, tss
->edi
);
3789 if (kvm_load_segment_descriptor(vcpu
, tss
->ldt_selector
, 0, VCPU_SREG_LDTR
))
3792 if (kvm_load_segment_descriptor(vcpu
, tss
->es
, 1, VCPU_SREG_ES
))
3795 if (kvm_load_segment_descriptor(vcpu
, tss
->cs
, 9, VCPU_SREG_CS
))
3798 if (kvm_load_segment_descriptor(vcpu
, tss
->ss
, 1, VCPU_SREG_SS
))
3801 if (kvm_load_segment_descriptor(vcpu
, tss
->ds
, 1, VCPU_SREG_DS
))
3804 if (kvm_load_segment_descriptor(vcpu
, tss
->fs
, 1, VCPU_SREG_FS
))
3807 if (kvm_load_segment_descriptor(vcpu
, tss
->gs
, 1, VCPU_SREG_GS
))
3812 static void save_state_to_tss16(struct kvm_vcpu
*vcpu
,
3813 struct tss_segment_16
*tss
)
3815 tss
->ip
= kvm_rip_read(vcpu
);
3816 tss
->flag
= kvm_x86_ops
->get_rflags(vcpu
);
3817 tss
->ax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3818 tss
->cx
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
3819 tss
->dx
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
3820 tss
->bx
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
3821 tss
->sp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
3822 tss
->bp
= kvm_register_read(vcpu
, VCPU_REGS_RBP
);
3823 tss
->si
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
3824 tss
->di
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
3826 tss
->es
= get_segment_selector(vcpu
, VCPU_SREG_ES
);
3827 tss
->cs
= get_segment_selector(vcpu
, VCPU_SREG_CS
);
3828 tss
->ss
= get_segment_selector(vcpu
, VCPU_SREG_SS
);
3829 tss
->ds
= get_segment_selector(vcpu
, VCPU_SREG_DS
);
3830 tss
->ldt
= get_segment_selector(vcpu
, VCPU_SREG_LDTR
);
3831 tss
->prev_task_link
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
3834 static int load_state_from_tss16(struct kvm_vcpu
*vcpu
,
3835 struct tss_segment_16
*tss
)
3837 kvm_rip_write(vcpu
, tss
->ip
);
3838 kvm_x86_ops
->set_rflags(vcpu
, tss
->flag
| 2);
3839 kvm_register_write(vcpu
, VCPU_REGS_RAX
, tss
->ax
);
3840 kvm_register_write(vcpu
, VCPU_REGS_RCX
, tss
->cx
);
3841 kvm_register_write(vcpu
, VCPU_REGS_RDX
, tss
->dx
);
3842 kvm_register_write(vcpu
, VCPU_REGS_RBX
, tss
->bx
);
3843 kvm_register_write(vcpu
, VCPU_REGS_RSP
, tss
->sp
);
3844 kvm_register_write(vcpu
, VCPU_REGS_RBP
, tss
->bp
);
3845 kvm_register_write(vcpu
, VCPU_REGS_RSI
, tss
->si
);
3846 kvm_register_write(vcpu
, VCPU_REGS_RDI
, tss
->di
);
3848 if (kvm_load_segment_descriptor(vcpu
, tss
->ldt
, 0, VCPU_SREG_LDTR
))
3851 if (kvm_load_segment_descriptor(vcpu
, tss
->es
, 1, VCPU_SREG_ES
))
3854 if (kvm_load_segment_descriptor(vcpu
, tss
->cs
, 9, VCPU_SREG_CS
))
3857 if (kvm_load_segment_descriptor(vcpu
, tss
->ss
, 1, VCPU_SREG_SS
))
3860 if (kvm_load_segment_descriptor(vcpu
, tss
->ds
, 1, VCPU_SREG_DS
))
3865 static int kvm_task_switch_16(struct kvm_vcpu
*vcpu
, u16 tss_selector
,
3867 struct desc_struct
*nseg_desc
)
3869 struct tss_segment_16 tss_segment_16
;
3872 if (kvm_read_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_16
,
3873 sizeof tss_segment_16
))
3876 save_state_to_tss16(vcpu
, &tss_segment_16
);
3878 if (kvm_write_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_16
,
3879 sizeof tss_segment_16
))
3882 if (kvm_read_guest(vcpu
->kvm
, get_tss_base_addr(vcpu
, nseg_desc
),
3883 &tss_segment_16
, sizeof tss_segment_16
))
3886 if (load_state_from_tss16(vcpu
, &tss_segment_16
))
3894 static int kvm_task_switch_32(struct kvm_vcpu
*vcpu
, u16 tss_selector
,
3896 struct desc_struct
*nseg_desc
)
3898 struct tss_segment_32 tss_segment_32
;
3901 if (kvm_read_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_32
,
3902 sizeof tss_segment_32
))
3905 save_state_to_tss32(vcpu
, &tss_segment_32
);
3907 if (kvm_write_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_32
,
3908 sizeof tss_segment_32
))
3911 if (kvm_read_guest(vcpu
->kvm
, get_tss_base_addr(vcpu
, nseg_desc
),
3912 &tss_segment_32
, sizeof tss_segment_32
))
3915 if (load_state_from_tss32(vcpu
, &tss_segment_32
))
3923 int kvm_task_switch(struct kvm_vcpu
*vcpu
, u16 tss_selector
, int reason
)
3925 struct kvm_segment tr_seg
;
3926 struct desc_struct cseg_desc
;
3927 struct desc_struct nseg_desc
;
3929 u32 old_tss_base
= get_segment_base(vcpu
, VCPU_SREG_TR
);
3930 u16 old_tss_sel
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
3932 old_tss_base
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, old_tss_base
);
3934 /* FIXME: Handle errors. Failure to read either TSS or their
3935 * descriptors should generate a pagefault.
3937 if (load_guest_segment_descriptor(vcpu
, tss_selector
, &nseg_desc
))
3940 if (load_guest_segment_descriptor(vcpu
, old_tss_sel
, &cseg_desc
))
3943 if (reason
!= TASK_SWITCH_IRET
) {
3946 cpl
= kvm_x86_ops
->get_cpl(vcpu
);
3947 if ((tss_selector
& 3) > nseg_desc
.dpl
|| cpl
> nseg_desc
.dpl
) {
3948 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
3953 if (!nseg_desc
.p
|| (nseg_desc
.limit0
| nseg_desc
.limit
<< 16) < 0x67) {
3954 kvm_queue_exception_e(vcpu
, TS_VECTOR
, tss_selector
& 0xfffc);
3958 if (reason
== TASK_SWITCH_IRET
|| reason
== TASK_SWITCH_JMP
) {
3959 cseg_desc
.type
&= ~(1 << 1); //clear the B flag
3960 save_guest_segment_descriptor(vcpu
, old_tss_sel
, &cseg_desc
);
3963 if (reason
== TASK_SWITCH_IRET
) {
3964 u32 eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3965 kvm_x86_ops
->set_rflags(vcpu
, eflags
& ~X86_EFLAGS_NT
);
3968 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
3970 if (nseg_desc
.type
& 8)
3971 ret
= kvm_task_switch_32(vcpu
, tss_selector
, old_tss_base
,
3974 ret
= kvm_task_switch_16(vcpu
, tss_selector
, old_tss_base
,
3977 if (reason
== TASK_SWITCH_CALL
|| reason
== TASK_SWITCH_GATE
) {
3978 u32 eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3979 kvm_x86_ops
->set_rflags(vcpu
, eflags
| X86_EFLAGS_NT
);
3982 if (reason
!= TASK_SWITCH_IRET
) {
3983 nseg_desc
.type
|= (1 << 1);
3984 save_guest_segment_descriptor(vcpu
, tss_selector
,
3988 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
| X86_CR0_TS
);
3989 seg_desct_to_kvm_desct(&nseg_desc
, tss_selector
, &tr_seg
);
3991 kvm_set_segment(vcpu
, &tr_seg
, VCPU_SREG_TR
);
3995 EXPORT_SYMBOL_GPL(kvm_task_switch
);
3997 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
3998 struct kvm_sregs
*sregs
)
4000 int mmu_reset_needed
= 0;
4001 int i
, pending_vec
, max_bits
;
4002 struct descriptor_table dt
;
4006 dt
.limit
= sregs
->idt
.limit
;
4007 dt
.base
= sregs
->idt
.base
;
4008 kvm_x86_ops
->set_idt(vcpu
, &dt
);
4009 dt
.limit
= sregs
->gdt
.limit
;
4010 dt
.base
= sregs
->gdt
.base
;
4011 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
4013 vcpu
->arch
.cr2
= sregs
->cr2
;
4014 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
4015 vcpu
->arch
.cr3
= sregs
->cr3
;
4017 kvm_set_cr8(vcpu
, sregs
->cr8
);
4019 mmu_reset_needed
|= vcpu
->arch
.shadow_efer
!= sregs
->efer
;
4020 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
4021 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
4023 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
4025 mmu_reset_needed
|= vcpu
->arch
.cr0
!= sregs
->cr0
;
4026 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
4027 vcpu
->arch
.cr0
= sregs
->cr0
;
4029 mmu_reset_needed
|= vcpu
->arch
.cr4
!= sregs
->cr4
;
4030 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
4031 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
4032 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
4034 if (mmu_reset_needed
)
4035 kvm_mmu_reset_context(vcpu
);
4037 if (!irqchip_in_kernel(vcpu
->kvm
)) {
4038 memcpy(vcpu
->arch
.irq_pending
, sregs
->interrupt_bitmap
,
4039 sizeof vcpu
->arch
.irq_pending
);
4040 vcpu
->arch
.irq_summary
= 0;
4041 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.irq_pending
); ++i
)
4042 if (vcpu
->arch
.irq_pending
[i
])
4043 __set_bit(i
, &vcpu
->arch
.irq_summary
);
4045 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
4046 pending_vec
= find_first_bit(
4047 (const unsigned long *)sregs
->interrupt_bitmap
,
4049 /* Only pending external irq is handled here */
4050 if (pending_vec
< max_bits
) {
4051 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
4052 pr_debug("Set back pending irq %d\n",
4055 kvm_pic_clear_isr_ack(vcpu
->kvm
);
4058 kvm_set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
4059 kvm_set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
4060 kvm_set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
4061 kvm_set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
4062 kvm_set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
4063 kvm_set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
4065 kvm_set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
4066 kvm_set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
4068 /* Older userspace won't unhalt the vcpu on reset. */
4069 if (vcpu
->vcpu_id
== 0 && kvm_rip_read(vcpu
) == 0xfff0 &&
4070 sregs
->cs
.selector
== 0xf000 && sregs
->cs
.base
== 0xffff0000 &&
4071 !(vcpu
->arch
.cr0
& X86_CR0_PE
))
4072 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
4079 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
4080 struct kvm_guest_debug
*dbg
)
4086 if ((dbg
->control
& (KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
)) ==
4087 (KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
)) {
4088 for (i
= 0; i
< KVM_NR_DB_REGS
; ++i
)
4089 vcpu
->arch
.eff_db
[i
] = dbg
->arch
.debugreg
[i
];
4090 vcpu
->arch
.switch_db_regs
=
4091 (dbg
->arch
.debugreg
[7] & DR7_BP_EN_MASK
);
4093 for (i
= 0; i
< KVM_NR_DB_REGS
; i
++)
4094 vcpu
->arch
.eff_db
[i
] = vcpu
->arch
.db
[i
];
4095 vcpu
->arch
.switch_db_regs
= (vcpu
->arch
.dr7
& DR7_BP_EN_MASK
);
4098 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
4100 if (dbg
->control
& KVM_GUESTDBG_INJECT_DB
)
4101 kvm_queue_exception(vcpu
, DB_VECTOR
);
4102 else if (dbg
->control
& KVM_GUESTDBG_INJECT_BP
)
4103 kvm_queue_exception(vcpu
, BP_VECTOR
);
4111 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
4112 * we have asm/x86/processor.h
4123 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
4124 #ifdef CONFIG_X86_64
4125 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
4127 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
4132 * Translate a guest virtual address to a guest physical address.
4134 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
4135 struct kvm_translation
*tr
)
4137 unsigned long vaddr
= tr
->linear_address
;
4141 down_read(&vcpu
->kvm
->slots_lock
);
4142 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, vaddr
);
4143 up_read(&vcpu
->kvm
->slots_lock
);
4144 tr
->physical_address
= gpa
;
4145 tr
->valid
= gpa
!= UNMAPPED_GVA
;
4153 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
4155 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
4159 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
4160 fpu
->fcw
= fxsave
->cwd
;
4161 fpu
->fsw
= fxsave
->swd
;
4162 fpu
->ftwx
= fxsave
->twd
;
4163 fpu
->last_opcode
= fxsave
->fop
;
4164 fpu
->last_ip
= fxsave
->rip
;
4165 fpu
->last_dp
= fxsave
->rdp
;
4166 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
4173 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
4175 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
4179 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
4180 fxsave
->cwd
= fpu
->fcw
;
4181 fxsave
->swd
= fpu
->fsw
;
4182 fxsave
->twd
= fpu
->ftwx
;
4183 fxsave
->fop
= fpu
->last_opcode
;
4184 fxsave
->rip
= fpu
->last_ip
;
4185 fxsave
->rdp
= fpu
->last_dp
;
4186 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
4193 void fx_init(struct kvm_vcpu
*vcpu
)
4195 unsigned after_mxcsr_mask
;
4198 * Touch the fpu the first time in non atomic context as if
4199 * this is the first fpu instruction the exception handler
4200 * will fire before the instruction returns and it'll have to
4201 * allocate ram with GFP_KERNEL.
4204 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
4206 /* Initialize guest FPU by resetting ours and saving into guest's */
4208 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
4210 kvm_fx_save(&vcpu
->arch
.guest_fx_image
);
4211 kvm_fx_restore(&vcpu
->arch
.host_fx_image
);
4214 vcpu
->arch
.cr0
|= X86_CR0_ET
;
4215 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
4216 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
4217 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
4218 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
4220 EXPORT_SYMBOL_GPL(fx_init
);
4222 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
4224 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
4227 vcpu
->guest_fpu_loaded
= 1;
4228 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
4229 kvm_fx_restore(&vcpu
->arch
.guest_fx_image
);
4231 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
4233 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
4235 if (!vcpu
->guest_fpu_loaded
)
4238 vcpu
->guest_fpu_loaded
= 0;
4239 kvm_fx_save(&vcpu
->arch
.guest_fx_image
);
4240 kvm_fx_restore(&vcpu
->arch
.host_fx_image
);
4241 ++vcpu
->stat
.fpu_reload
;
4243 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
4245 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
4247 if (vcpu
->arch
.time_page
) {
4248 kvm_release_page_dirty(vcpu
->arch
.time_page
);
4249 vcpu
->arch
.time_page
= NULL
;
4252 kvm_x86_ops
->vcpu_free(vcpu
);
4255 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
4258 return kvm_x86_ops
->vcpu_create(kvm
, id
);
4261 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
4265 /* We do fxsave: this must be aligned. */
4266 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
4268 vcpu
->arch
.mtrr_state
.have_fixed
= 1;
4270 r
= kvm_arch_vcpu_reset(vcpu
);
4272 r
= kvm_mmu_setup(vcpu
);
4279 kvm_x86_ops
->vcpu_free(vcpu
);
4283 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
4286 kvm_mmu_unload(vcpu
);
4289 kvm_x86_ops
->vcpu_free(vcpu
);
4292 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
4294 vcpu
->arch
.nmi_pending
= false;
4295 vcpu
->arch
.nmi_injected
= false;
4297 vcpu
->arch
.switch_db_regs
= 0;
4298 memset(vcpu
->arch
.db
, 0, sizeof(vcpu
->arch
.db
));
4299 vcpu
->arch
.dr6
= DR6_FIXED_1
;
4300 vcpu
->arch
.dr7
= DR7_FIXED_1
;
4302 return kvm_x86_ops
->vcpu_reset(vcpu
);
4305 void kvm_arch_hardware_enable(void *garbage
)
4307 kvm_x86_ops
->hardware_enable(garbage
);
4310 void kvm_arch_hardware_disable(void *garbage
)
4312 kvm_x86_ops
->hardware_disable(garbage
);
4315 int kvm_arch_hardware_setup(void)
4317 return kvm_x86_ops
->hardware_setup();
4320 void kvm_arch_hardware_unsetup(void)
4322 kvm_x86_ops
->hardware_unsetup();
4325 void kvm_arch_check_processor_compat(void *rtn
)
4327 kvm_x86_ops
->check_processor_compatibility(rtn
);
4330 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
4336 BUG_ON(vcpu
->kvm
== NULL
);
4339 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
4340 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
4341 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
4343 vcpu
->arch
.mp_state
= KVM_MP_STATE_UNINITIALIZED
;
4345 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
4350 vcpu
->arch
.pio_data
= page_address(page
);
4352 r
= kvm_mmu_create(vcpu
);
4354 goto fail_free_pio_data
;
4356 if (irqchip_in_kernel(kvm
)) {
4357 r
= kvm_create_lapic(vcpu
);
4359 goto fail_mmu_destroy
;
4365 kvm_mmu_destroy(vcpu
);
4367 free_page((unsigned long)vcpu
->arch
.pio_data
);
4372 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
4374 kvm_free_lapic(vcpu
);
4375 down_read(&vcpu
->kvm
->slots_lock
);
4376 kvm_mmu_destroy(vcpu
);
4377 up_read(&vcpu
->kvm
->slots_lock
);
4378 free_page((unsigned long)vcpu
->arch
.pio_data
);
4381 struct kvm
*kvm_arch_create_vm(void)
4383 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
4386 return ERR_PTR(-ENOMEM
);
4388 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
4389 INIT_LIST_HEAD(&kvm
->arch
.oos_global_pages
);
4390 INIT_LIST_HEAD(&kvm
->arch
.assigned_dev_head
);
4392 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
4393 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID
, &kvm
->arch
.irq_sources_bitmap
);
4395 rdtscll(kvm
->arch
.vm_init_tsc
);
4400 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
4403 kvm_mmu_unload(vcpu
);
4407 static void kvm_free_vcpus(struct kvm
*kvm
)
4412 * Unpin any mmu pages first.
4414 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
4416 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
4417 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
4418 if (kvm
->vcpus
[i
]) {
4419 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
4420 kvm
->vcpus
[i
] = NULL
;
4426 void kvm_arch_sync_events(struct kvm
*kvm
)
4428 kvm_free_all_assigned_devices(kvm
);
4431 void kvm_arch_destroy_vm(struct kvm
*kvm
)
4433 kvm_iommu_unmap_guest(kvm
);
4435 kfree(kvm
->arch
.vpic
);
4436 kfree(kvm
->arch
.vioapic
);
4437 kvm_free_vcpus(kvm
);
4438 kvm_free_physmem(kvm
);
4439 if (kvm
->arch
.apic_access_page
)
4440 put_page(kvm
->arch
.apic_access_page
);
4441 if (kvm
->arch
.ept_identity_pagetable
)
4442 put_page(kvm
->arch
.ept_identity_pagetable
);
4446 int kvm_arch_set_memory_region(struct kvm
*kvm
,
4447 struct kvm_userspace_memory_region
*mem
,
4448 struct kvm_memory_slot old
,
4451 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
4452 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
4454 /*To keep backward compatibility with older userspace,
4455 *x86 needs to hanlde !user_alloc case.
4458 if (npages
&& !old
.rmap
) {
4459 unsigned long userspace_addr
;
4461 down_write(¤t
->mm
->mmap_sem
);
4462 userspace_addr
= do_mmap(NULL
, 0,
4464 PROT_READ
| PROT_WRITE
,
4465 MAP_PRIVATE
| MAP_ANONYMOUS
,
4467 up_write(¤t
->mm
->mmap_sem
);
4469 if (IS_ERR((void *)userspace_addr
))
4470 return PTR_ERR((void *)userspace_addr
);
4472 /* set userspace_addr atomically for kvm_hva_to_rmapp */
4473 spin_lock(&kvm
->mmu_lock
);
4474 memslot
->userspace_addr
= userspace_addr
;
4475 spin_unlock(&kvm
->mmu_lock
);
4477 if (!old
.user_alloc
&& old
.rmap
) {
4480 down_write(¤t
->mm
->mmap_sem
);
4481 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
4482 old
.npages
* PAGE_SIZE
);
4483 up_write(¤t
->mm
->mmap_sem
);
4486 "kvm_vm_ioctl_set_memory_region: "
4487 "failed to munmap memory\n");
4492 spin_lock(&kvm
->mmu_lock
);
4493 if (!kvm
->arch
.n_requested_mmu_pages
) {
4494 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
4495 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
4498 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
4499 spin_unlock(&kvm
->mmu_lock
);
4500 kvm_flush_remote_tlbs(kvm
);
4505 void kvm_arch_flush_shadow(struct kvm
*kvm
)
4507 kvm_mmu_zap_all(kvm
);
4508 kvm_reload_remote_mmus(kvm
);
4511 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
4513 return vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
4514 || vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
4515 || vcpu
->arch
.nmi_pending
;
4518 static void vcpu_kick_intr(void *info
)
4521 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)info
;
4522 printk(KERN_DEBUG
"vcpu_kick_intr %p \n", vcpu
);
4526 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
4528 int ipi_pcpu
= vcpu
->cpu
;
4529 int cpu
= get_cpu();
4531 if (waitqueue_active(&vcpu
->wq
)) {
4532 wake_up_interruptible(&vcpu
->wq
);
4533 ++vcpu
->stat
.halt_wakeup
;
4536 * We may be called synchronously with irqs disabled in guest mode,
4537 * So need not to call smp_call_function_single() in that case.
4539 if (vcpu
->guest_mode
&& vcpu
->cpu
!= cpu
)
4540 smp_call_function_single(ipi_pcpu
, vcpu_kick_intr
, vcpu
, 0);