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 { "irq_exits", VCPU_STAT(irq_exits
) },
95 { "host_state_reload", VCPU_STAT(host_state_reload
) },
96 { "efer_reload", VCPU_STAT(efer_reload
) },
97 { "fpu_reload", VCPU_STAT(fpu_reload
) },
98 { "insn_emulation", VCPU_STAT(insn_emulation
) },
99 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
100 { "irq_injections", VCPU_STAT(irq_injections
) },
101 { "nmi_injections", VCPU_STAT(nmi_injections
) },
102 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
103 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
104 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
105 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
106 { "mmu_flooded", VM_STAT(mmu_flooded
) },
107 { "mmu_recycled", VM_STAT(mmu_recycled
) },
108 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
109 { "mmu_unsync", VM_STAT(mmu_unsync
) },
110 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
111 { "largepages", VM_STAT(lpages
) },
115 unsigned long segment_base(u16 selector
)
117 struct descriptor_table gdt
;
118 struct desc_struct
*d
;
119 unsigned long table_base
;
125 asm("sgdt %0" : "=m"(gdt
));
126 table_base
= gdt
.base
;
128 if (selector
& 4) { /* from ldt */
131 asm("sldt %0" : "=g"(ldt_selector
));
132 table_base
= segment_base(ldt_selector
);
134 d
= (struct desc_struct
*)(table_base
+ (selector
& ~7));
135 v
= d
->base0
| ((unsigned long)d
->base1
<< 16) |
136 ((unsigned long)d
->base2
<< 24);
138 if (d
->s
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
139 v
|= ((unsigned long)((struct ldttss_desc64
*)d
)->base3
) << 32;
143 EXPORT_SYMBOL_GPL(segment_base
);
145 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
147 if (irqchip_in_kernel(vcpu
->kvm
))
148 return vcpu
->arch
.apic_base
;
150 return vcpu
->arch
.apic_base
;
152 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
154 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
156 /* TODO: reserve bits check */
157 if (irqchip_in_kernel(vcpu
->kvm
))
158 kvm_lapic_set_base(vcpu
, data
);
160 vcpu
->arch
.apic_base
= data
;
162 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
164 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
166 WARN_ON(vcpu
->arch
.exception
.pending
);
167 vcpu
->arch
.exception
.pending
= true;
168 vcpu
->arch
.exception
.has_error_code
= false;
169 vcpu
->arch
.exception
.nr
= nr
;
171 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
173 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
176 ++vcpu
->stat
.pf_guest
;
178 if (vcpu
->arch
.exception
.pending
) {
179 if (vcpu
->arch
.exception
.nr
== PF_VECTOR
) {
180 printk(KERN_DEBUG
"kvm: inject_page_fault:"
181 " double fault 0x%lx\n", addr
);
182 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
183 vcpu
->arch
.exception
.error_code
= 0;
184 } else if (vcpu
->arch
.exception
.nr
== DF_VECTOR
) {
185 /* triple fault -> shutdown */
186 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
190 vcpu
->arch
.cr2
= addr
;
191 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
194 void kvm_inject_nmi(struct kvm_vcpu
*vcpu
)
196 vcpu
->arch
.nmi_pending
= 1;
198 EXPORT_SYMBOL_GPL(kvm_inject_nmi
);
200 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
202 WARN_ON(vcpu
->arch
.exception
.pending
);
203 vcpu
->arch
.exception
.pending
= true;
204 vcpu
->arch
.exception
.has_error_code
= true;
205 vcpu
->arch
.exception
.nr
= nr
;
206 vcpu
->arch
.exception
.error_code
= error_code
;
208 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
210 static void __queue_exception(struct kvm_vcpu
*vcpu
)
212 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
213 vcpu
->arch
.exception
.has_error_code
,
214 vcpu
->arch
.exception
.error_code
);
218 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
219 * a #GP and return false.
221 bool kvm_require_cpl(struct kvm_vcpu
*vcpu
, int required_cpl
)
223 if (kvm_x86_ops
->get_cpl(vcpu
) <= required_cpl
)
225 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
228 EXPORT_SYMBOL_GPL(kvm_require_cpl
);
231 * Load the pae pdptrs. Return true is they are all valid.
233 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
235 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
236 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
239 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
241 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
242 offset
* sizeof(u64
), sizeof(pdpte
));
247 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
248 if (is_present_pte(pdpte
[i
]) &&
249 (pdpte
[i
] & vcpu
->arch
.mmu
.rsvd_bits_mask
[0][2])) {
256 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
261 EXPORT_SYMBOL_GPL(load_pdptrs
);
263 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
265 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
269 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
272 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
275 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
281 void kvm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
283 if (cr0
& CR0_RESERVED_BITS
) {
284 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
285 cr0
, vcpu
->arch
.cr0
);
286 kvm_inject_gp(vcpu
, 0);
290 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
291 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
292 kvm_inject_gp(vcpu
, 0);
296 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
297 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
298 "and a clear PE flag\n");
299 kvm_inject_gp(vcpu
, 0);
303 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
305 if ((vcpu
->arch
.shadow_efer
& EFER_LME
)) {
309 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
310 "in long mode while PAE is disabled\n");
311 kvm_inject_gp(vcpu
, 0);
314 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
316 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
317 "in long mode while CS.L == 1\n");
318 kvm_inject_gp(vcpu
, 0);
324 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
325 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
327 kvm_inject_gp(vcpu
, 0);
333 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
334 vcpu
->arch
.cr0
= cr0
;
336 kvm_mmu_reset_context(vcpu
);
339 EXPORT_SYMBOL_GPL(kvm_set_cr0
);
341 void kvm_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
343 kvm_set_cr0(vcpu
, (vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f));
344 KVMTRACE_1D(LMSW
, vcpu
,
345 (u32
)((vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f)),
348 EXPORT_SYMBOL_GPL(kvm_lmsw
);
350 void kvm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
352 unsigned long old_cr4
= vcpu
->arch
.cr4
;
353 unsigned long pdptr_bits
= X86_CR4_PGE
| X86_CR4_PSE
| X86_CR4_PAE
;
355 if (cr4
& CR4_RESERVED_BITS
) {
356 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
357 kvm_inject_gp(vcpu
, 0);
361 if (is_long_mode(vcpu
)) {
362 if (!(cr4
& X86_CR4_PAE
)) {
363 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
365 kvm_inject_gp(vcpu
, 0);
368 } else if (is_paging(vcpu
) && (cr4
& X86_CR4_PAE
)
369 && ((cr4
^ old_cr4
) & pdptr_bits
)
370 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
371 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
372 kvm_inject_gp(vcpu
, 0);
376 if (cr4
& X86_CR4_VMXE
) {
377 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
378 kvm_inject_gp(vcpu
, 0);
381 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
382 vcpu
->arch
.cr4
= cr4
;
383 vcpu
->arch
.mmu
.base_role
.cr4_pge
= (cr4
& X86_CR4_PGE
) && !tdp_enabled
;
384 kvm_mmu_reset_context(vcpu
);
386 EXPORT_SYMBOL_GPL(kvm_set_cr4
);
388 void kvm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
390 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
391 kvm_mmu_sync_roots(vcpu
);
392 kvm_mmu_flush_tlb(vcpu
);
396 if (is_long_mode(vcpu
)) {
397 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
398 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
399 kvm_inject_gp(vcpu
, 0);
404 if (cr3
& CR3_PAE_RESERVED_BITS
) {
406 "set_cr3: #GP, reserved bits\n");
407 kvm_inject_gp(vcpu
, 0);
410 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
411 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
413 kvm_inject_gp(vcpu
, 0);
418 * We don't check reserved bits in nonpae mode, because
419 * this isn't enforced, and VMware depends on this.
424 * Does the new cr3 value map to physical memory? (Note, we
425 * catch an invalid cr3 even in real-mode, because it would
426 * cause trouble later on when we turn on paging anyway.)
428 * A real CPU would silently accept an invalid cr3 and would
429 * attempt to use it - with largely undefined (and often hard
430 * to debug) behavior on the guest side.
432 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
433 kvm_inject_gp(vcpu
, 0);
435 vcpu
->arch
.cr3
= cr3
;
436 vcpu
->arch
.mmu
.new_cr3(vcpu
);
439 EXPORT_SYMBOL_GPL(kvm_set_cr3
);
441 void kvm_set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
443 if (cr8
& CR8_RESERVED_BITS
) {
444 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
445 kvm_inject_gp(vcpu
, 0);
448 if (irqchip_in_kernel(vcpu
->kvm
))
449 kvm_lapic_set_tpr(vcpu
, cr8
);
451 vcpu
->arch
.cr8
= cr8
;
453 EXPORT_SYMBOL_GPL(kvm_set_cr8
);
455 unsigned long kvm_get_cr8(struct kvm_vcpu
*vcpu
)
457 if (irqchip_in_kernel(vcpu
->kvm
))
458 return kvm_lapic_get_cr8(vcpu
);
460 return vcpu
->arch
.cr8
;
462 EXPORT_SYMBOL_GPL(kvm_get_cr8
);
464 static inline u32
bit(int bitno
)
466 return 1 << (bitno
& 31);
470 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
471 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
473 * This list is modified at module load time to reflect the
474 * capabilities of the host cpu.
476 static u32 msrs_to_save
[] = {
477 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
480 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
482 MSR_IA32_TIME_STAMP_COUNTER
, MSR_KVM_SYSTEM_TIME
, MSR_KVM_WALL_CLOCK
,
483 MSR_IA32_PERF_STATUS
, MSR_IA32_CR_PAT
, MSR_VM_HSAVE_PA
486 static unsigned num_msrs_to_save
;
488 static u32 emulated_msrs
[] = {
489 MSR_IA32_MISC_ENABLE
,
492 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
494 if (efer
& efer_reserved_bits
) {
495 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
497 kvm_inject_gp(vcpu
, 0);
502 && (vcpu
->arch
.shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
503 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
504 kvm_inject_gp(vcpu
, 0);
508 if (efer
& EFER_FFXSR
) {
509 struct kvm_cpuid_entry2
*feat
;
511 feat
= kvm_find_cpuid_entry(vcpu
, 0x80000001, 0);
512 if (!feat
|| !(feat
->edx
& bit(X86_FEATURE_FXSR_OPT
))) {
513 printk(KERN_DEBUG
"set_efer: #GP, enable FFXSR w/o CPUID capability\n");
514 kvm_inject_gp(vcpu
, 0);
519 if (efer
& EFER_SVME
) {
520 struct kvm_cpuid_entry2
*feat
;
522 feat
= kvm_find_cpuid_entry(vcpu
, 0x80000001, 0);
523 if (!feat
|| !(feat
->ecx
& bit(X86_FEATURE_SVM
))) {
524 printk(KERN_DEBUG
"set_efer: #GP, enable SVM w/o SVM\n");
525 kvm_inject_gp(vcpu
, 0);
530 kvm_x86_ops
->set_efer(vcpu
, efer
);
533 efer
|= vcpu
->arch
.shadow_efer
& EFER_LMA
;
535 vcpu
->arch
.shadow_efer
= efer
;
537 vcpu
->arch
.mmu
.base_role
.nxe
= (efer
& EFER_NX
) && !tdp_enabled
;
538 kvm_mmu_reset_context(vcpu
);
541 void kvm_enable_efer_bits(u64 mask
)
543 efer_reserved_bits
&= ~mask
;
545 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits
);
549 * Writes msr value into into the appropriate "register".
550 * Returns 0 on success, non-0 otherwise.
551 * Assumes vcpu_load() was already called.
553 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
555 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
559 * Adapt set_msr() to msr_io()'s calling convention
561 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
563 return kvm_set_msr(vcpu
, index
, *data
);
566 static void kvm_write_wall_clock(struct kvm
*kvm
, gpa_t wall_clock
)
569 struct pvclock_wall_clock wc
;
570 struct timespec now
, sys
, boot
;
577 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
580 * The guest calculates current wall clock time by adding
581 * system time (updated by kvm_write_guest_time below) to the
582 * wall clock specified here. guest system time equals host
583 * system time for us, thus we must fill in host boot time here.
585 now
= current_kernel_time();
587 boot
= ns_to_timespec(timespec_to_ns(&now
) - timespec_to_ns(&sys
));
589 wc
.sec
= boot
.tv_sec
;
590 wc
.nsec
= boot
.tv_nsec
;
591 wc
.version
= version
;
593 kvm_write_guest(kvm
, wall_clock
, &wc
, sizeof(wc
));
596 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
599 static uint32_t div_frac(uint32_t dividend
, uint32_t divisor
)
601 uint32_t quotient
, remainder
;
603 /* Don't try to replace with do_div(), this one calculates
604 * "(dividend << 32) / divisor" */
606 : "=a" (quotient
), "=d" (remainder
)
607 : "0" (0), "1" (dividend
), "r" (divisor
) );
611 static void kvm_set_time_scale(uint32_t tsc_khz
, struct pvclock_vcpu_time_info
*hv_clock
)
613 uint64_t nsecs
= 1000000000LL;
618 tps64
= tsc_khz
* 1000LL;
619 while (tps64
> nsecs
*2) {
624 tps32
= (uint32_t)tps64
;
625 while (tps32
<= (uint32_t)nsecs
) {
630 hv_clock
->tsc_shift
= shift
;
631 hv_clock
->tsc_to_system_mul
= div_frac(nsecs
, tps32
);
633 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
634 __func__
, tsc_khz
, hv_clock
->tsc_shift
,
635 hv_clock
->tsc_to_system_mul
);
638 static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz
);
640 static void kvm_write_guest_time(struct kvm_vcpu
*v
)
644 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
646 unsigned long this_tsc_khz
;
648 if ((!vcpu
->time_page
))
651 this_tsc_khz
= get_cpu_var(cpu_tsc_khz
);
652 if (unlikely(vcpu
->hv_clock_tsc_khz
!= this_tsc_khz
)) {
653 kvm_set_time_scale(this_tsc_khz
, &vcpu
->hv_clock
);
654 vcpu
->hv_clock_tsc_khz
= this_tsc_khz
;
656 put_cpu_var(cpu_tsc_khz
);
658 /* Keep irq disabled to prevent changes to the clock */
659 local_irq_save(flags
);
660 kvm_get_msr(v
, MSR_IA32_TIME_STAMP_COUNTER
,
661 &vcpu
->hv_clock
.tsc_timestamp
);
663 local_irq_restore(flags
);
665 /* With all the info we got, fill in the values */
667 vcpu
->hv_clock
.system_time
= ts
.tv_nsec
+
668 (NSEC_PER_SEC
* (u64
)ts
.tv_sec
);
670 * The interface expects us to write an even number signaling that the
671 * update is finished. Since the guest won't see the intermediate
672 * state, we just increase by 2 at the end.
674 vcpu
->hv_clock
.version
+= 2;
676 shared_kaddr
= kmap_atomic(vcpu
->time_page
, KM_USER0
);
678 memcpy(shared_kaddr
+ vcpu
->time_offset
, &vcpu
->hv_clock
,
679 sizeof(vcpu
->hv_clock
));
681 kunmap_atomic(shared_kaddr
, KM_USER0
);
683 mark_page_dirty(v
->kvm
, vcpu
->time
>> PAGE_SHIFT
);
686 static int kvm_request_guest_time_update(struct kvm_vcpu
*v
)
688 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
690 if (!vcpu
->time_page
)
692 set_bit(KVM_REQ_KVMCLOCK_UPDATE
, &v
->requests
);
696 static bool msr_mtrr_valid(unsigned msr
)
699 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR
- 1:
700 case MSR_MTRRfix64K_00000
:
701 case MSR_MTRRfix16K_80000
:
702 case MSR_MTRRfix16K_A0000
:
703 case MSR_MTRRfix4K_C0000
:
704 case MSR_MTRRfix4K_C8000
:
705 case MSR_MTRRfix4K_D0000
:
706 case MSR_MTRRfix4K_D8000
:
707 case MSR_MTRRfix4K_E0000
:
708 case MSR_MTRRfix4K_E8000
:
709 case MSR_MTRRfix4K_F0000
:
710 case MSR_MTRRfix4K_F8000
:
711 case MSR_MTRRdefType
:
712 case MSR_IA32_CR_PAT
:
720 static bool valid_pat_type(unsigned t
)
722 return t
< 8 && (1 << t
) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
725 static bool valid_mtrr_type(unsigned t
)
727 return t
< 8 && (1 << t
) & 0x73; /* 0, 1, 4, 5, 6 */
730 static bool mtrr_valid(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
734 if (!msr_mtrr_valid(msr
))
737 if (msr
== MSR_IA32_CR_PAT
) {
738 for (i
= 0; i
< 8; i
++)
739 if (!valid_pat_type((data
>> (i
* 8)) & 0xff))
742 } else if (msr
== MSR_MTRRdefType
) {
745 return valid_mtrr_type(data
& 0xff);
746 } else if (msr
>= MSR_MTRRfix64K_00000
&& msr
<= MSR_MTRRfix4K_F8000
) {
747 for (i
= 0; i
< 8 ; i
++)
748 if (!valid_mtrr_type((data
>> (i
* 8)) & 0xff))
754 return valid_mtrr_type(data
& 0xff);
757 static int set_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
759 u64
*p
= (u64
*)&vcpu
->arch
.mtrr_state
.fixed_ranges
;
761 if (!mtrr_valid(vcpu
, msr
, data
))
764 if (msr
== MSR_MTRRdefType
) {
765 vcpu
->arch
.mtrr_state
.def_type
= data
;
766 vcpu
->arch
.mtrr_state
.enabled
= (data
& 0xc00) >> 10;
767 } else if (msr
== MSR_MTRRfix64K_00000
)
769 else if (msr
== MSR_MTRRfix16K_80000
|| msr
== MSR_MTRRfix16K_A0000
)
770 p
[1 + msr
- MSR_MTRRfix16K_80000
] = data
;
771 else if (msr
>= MSR_MTRRfix4K_C0000
&& msr
<= MSR_MTRRfix4K_F8000
)
772 p
[3 + msr
- MSR_MTRRfix4K_C0000
] = data
;
773 else if (msr
== MSR_IA32_CR_PAT
)
774 vcpu
->arch
.pat
= data
;
775 else { /* Variable MTRRs */
776 int idx
, is_mtrr_mask
;
779 idx
= (msr
- 0x200) / 2;
780 is_mtrr_mask
= msr
- 0x200 - 2 * idx
;
783 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].base_lo
;
786 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].mask_lo
;
790 kvm_mmu_reset_context(vcpu
);
794 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
798 set_efer(vcpu
, data
);
800 case MSR_IA32_MC0_STATUS
:
801 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
804 case MSR_IA32_MCG_STATUS
:
805 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
808 case MSR_IA32_MCG_CTL
:
809 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
812 case MSR_IA32_DEBUGCTLMSR
:
814 /* We support the non-activated case already */
816 } else if (data
& ~(DEBUGCTLMSR_LBR
| DEBUGCTLMSR_BTF
)) {
817 /* Values other than LBR and BTF are vendor-specific,
818 thus reserved and should throw a #GP */
821 pr_unimpl(vcpu
, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
824 case MSR_IA32_UCODE_REV
:
825 case MSR_IA32_UCODE_WRITE
:
826 case MSR_VM_HSAVE_PA
:
828 case 0x200 ... 0x2ff:
829 return set_msr_mtrr(vcpu
, msr
, data
);
830 case MSR_IA32_APICBASE
:
831 kvm_set_apic_base(vcpu
, data
);
833 case MSR_IA32_MISC_ENABLE
:
834 vcpu
->arch
.ia32_misc_enable_msr
= data
;
836 case MSR_KVM_WALL_CLOCK
:
837 vcpu
->kvm
->arch
.wall_clock
= data
;
838 kvm_write_wall_clock(vcpu
->kvm
, data
);
840 case MSR_KVM_SYSTEM_TIME
: {
841 if (vcpu
->arch
.time_page
) {
842 kvm_release_page_dirty(vcpu
->arch
.time_page
);
843 vcpu
->arch
.time_page
= NULL
;
846 vcpu
->arch
.time
= data
;
848 /* we verify if the enable bit is set... */
852 /* ...but clean it before doing the actual write */
853 vcpu
->arch
.time_offset
= data
& ~(PAGE_MASK
| 1);
855 vcpu
->arch
.time_page
=
856 gfn_to_page(vcpu
->kvm
, data
>> PAGE_SHIFT
);
858 if (is_error_page(vcpu
->arch
.time_page
)) {
859 kvm_release_page_clean(vcpu
->arch
.time_page
);
860 vcpu
->arch
.time_page
= NULL
;
863 kvm_request_guest_time_update(vcpu
);
867 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n", msr
, data
);
872 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
876 * Reads an msr value (of 'msr_index') into 'pdata'.
877 * Returns 0 on success, non-0 otherwise.
878 * Assumes vcpu_load() was already called.
880 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
882 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
885 static int get_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
887 u64
*p
= (u64
*)&vcpu
->arch
.mtrr_state
.fixed_ranges
;
889 if (!msr_mtrr_valid(msr
))
892 if (msr
== MSR_MTRRdefType
)
893 *pdata
= vcpu
->arch
.mtrr_state
.def_type
+
894 (vcpu
->arch
.mtrr_state
.enabled
<< 10);
895 else if (msr
== MSR_MTRRfix64K_00000
)
897 else if (msr
== MSR_MTRRfix16K_80000
|| msr
== MSR_MTRRfix16K_A0000
)
898 *pdata
= p
[1 + msr
- MSR_MTRRfix16K_80000
];
899 else if (msr
>= MSR_MTRRfix4K_C0000
&& msr
<= MSR_MTRRfix4K_F8000
)
900 *pdata
= p
[3 + msr
- MSR_MTRRfix4K_C0000
];
901 else if (msr
== MSR_IA32_CR_PAT
)
902 *pdata
= vcpu
->arch
.pat
;
903 else { /* Variable MTRRs */
904 int idx
, is_mtrr_mask
;
907 idx
= (msr
- 0x200) / 2;
908 is_mtrr_mask
= msr
- 0x200 - 2 * idx
;
911 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].base_lo
;
914 (u64
*)&vcpu
->arch
.mtrr_state
.var_ranges
[idx
].mask_lo
;
921 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
926 case 0xc0010010: /* SYSCFG */
927 case 0xc0010015: /* HWCR */
928 case MSR_IA32_PLATFORM_ID
:
929 case MSR_IA32_P5_MC_ADDR
:
930 case MSR_IA32_P5_MC_TYPE
:
931 case MSR_IA32_MC0_CTL
:
932 case MSR_IA32_MCG_STATUS
:
933 case MSR_IA32_MCG_CAP
:
934 case MSR_IA32_MCG_CTL
:
935 case MSR_IA32_MC0_MISC
:
936 case MSR_IA32_MC0_MISC
+4:
937 case MSR_IA32_MC0_MISC
+8:
938 case MSR_IA32_MC0_MISC
+12:
939 case MSR_IA32_MC0_MISC
+16:
940 case MSR_IA32_MC0_MISC
+20:
941 case MSR_IA32_UCODE_REV
:
942 case MSR_IA32_EBL_CR_POWERON
:
943 case MSR_IA32_DEBUGCTLMSR
:
944 case MSR_IA32_LASTBRANCHFROMIP
:
945 case MSR_IA32_LASTBRANCHTOIP
:
946 case MSR_IA32_LASTINTFROMIP
:
947 case MSR_IA32_LASTINTTOIP
:
948 case MSR_VM_HSAVE_PA
:
949 case MSR_P6_EVNTSEL0
:
950 case MSR_P6_EVNTSEL1
:
951 case MSR_K7_EVNTSEL0
:
952 case MSR_K8_INT_PENDING_MSG
:
956 data
= 0x500 | KVM_NR_VAR_MTRR
;
958 case 0x200 ... 0x2ff:
959 return get_msr_mtrr(vcpu
, msr
, pdata
);
960 case 0xcd: /* fsb frequency */
963 case MSR_IA32_APICBASE
:
964 data
= kvm_get_apic_base(vcpu
);
966 case MSR_IA32_MISC_ENABLE
:
967 data
= vcpu
->arch
.ia32_misc_enable_msr
;
969 case MSR_IA32_PERF_STATUS
:
970 /* TSC increment by tick */
973 data
|= (((uint64_t)4ULL) << 40);
976 data
= vcpu
->arch
.shadow_efer
;
978 case MSR_KVM_WALL_CLOCK
:
979 data
= vcpu
->kvm
->arch
.wall_clock
;
981 case MSR_KVM_SYSTEM_TIME
:
982 data
= vcpu
->arch
.time
;
985 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
991 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
994 * Read or write a bunch of msrs. All parameters are kernel addresses.
996 * @return number of msrs set successfully.
998 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
999 struct kvm_msr_entry
*entries
,
1000 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1001 unsigned index
, u64
*data
))
1007 down_read(&vcpu
->kvm
->slots_lock
);
1008 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
1009 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
1011 up_read(&vcpu
->kvm
->slots_lock
);
1019 * Read or write a bunch of msrs. Parameters are user addresses.
1021 * @return number of msrs set successfully.
1023 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
1024 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
1025 unsigned index
, u64
*data
),
1028 struct kvm_msrs msrs
;
1029 struct kvm_msr_entry
*entries
;
1034 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
1038 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
1042 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
1043 entries
= vmalloc(size
);
1048 if (copy_from_user(entries
, user_msrs
->entries
, size
))
1051 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
1056 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
1067 int kvm_dev_ioctl_check_extension(long ext
)
1072 case KVM_CAP_IRQCHIP
:
1074 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
1075 case KVM_CAP_SET_TSS_ADDR
:
1076 case KVM_CAP_EXT_CPUID
:
1077 case KVM_CAP_CLOCKSOURCE
:
1079 case KVM_CAP_NOP_IO_DELAY
:
1080 case KVM_CAP_MP_STATE
:
1081 case KVM_CAP_SYNC_MMU
:
1082 case KVM_CAP_REINJECT_CONTROL
:
1083 case KVM_CAP_IRQ_INJECT_STATUS
:
1084 case KVM_CAP_ASSIGN_DEV_IRQ
:
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 #define F(x) bit(X86_FEATURE_##x)
1303 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1304 u32 index
, int *nent
, int maxnent
)
1306 unsigned f_nx
= is_efer_nx() ? F(NX
) : 0;
1307 #ifdef CONFIG_X86_64
1308 unsigned f_lm
= F(LM
);
1314 const u32 kvm_supported_word0_x86_features
=
1315 F(FPU
) | F(VME
) | F(DE
) | F(PSE
) |
1316 F(TSC
) | F(MSR
) | F(PAE
) | F(MCE
) |
1317 F(CX8
) | F(APIC
) | 0 /* Reserved */ | F(SEP
) |
1318 F(MTRR
) | F(PGE
) | F(MCA
) | F(CMOV
) |
1319 F(PAT
) | F(PSE36
) | 0 /* PSN */ | F(CLFLSH
) |
1320 0 /* Reserved, DS, ACPI */ | F(MMX
) |
1321 F(FXSR
) | F(XMM
) | F(XMM2
) | F(SELFSNOOP
) |
1322 0 /* HTT, TM, Reserved, PBE */;
1323 /* cpuid 0x80000001.edx */
1324 const u32 kvm_supported_word1_x86_features
=
1325 F(FPU
) | F(VME
) | F(DE
) | F(PSE
) |
1326 F(TSC
) | F(MSR
) | F(PAE
) | F(MCE
) |
1327 F(CX8
) | F(APIC
) | 0 /* Reserved */ | F(SYSCALL
) |
1328 F(MTRR
) | F(PGE
) | F(MCA
) | F(CMOV
) |
1329 F(PAT
) | F(PSE36
) | 0 /* Reserved */ |
1330 f_nx
| 0 /* Reserved */ | F(MMXEXT
) | F(MMX
) |
1331 F(FXSR
) | F(FXSR_OPT
) | 0 /* GBPAGES */ | 0 /* RDTSCP */ |
1332 0 /* Reserved */ | f_lm
| F(3DNOWEXT
) | F(3DNOW
);
1334 const u32 kvm_supported_word4_x86_features
=
1335 F(XMM3
) | 0 /* Reserved, DTES64, MONITOR */ |
1336 0 /* DS-CPL, VMX, SMX, EST */ |
1337 0 /* TM2 */ | F(SSSE3
) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
1338 0 /* Reserved */ | F(CX16
) | 0 /* xTPR Update, PDCM */ |
1339 0 /* Reserved, DCA */ | F(XMM4_1
) |
1340 F(XMM4_2
) | 0 /* x2APIC */ | F(MOVBE
) | F(POPCNT
) |
1341 0 /* Reserved, XSAVE, OSXSAVE */;
1342 /* cpuid 0x80000001.ecx */
1343 const u32 kvm_supported_word6_x86_features
=
1344 F(LAHF_LM
) | F(CMP_LEGACY
) | F(SVM
) | 0 /* ExtApicSpace */ |
1345 F(CR8_LEGACY
) | F(ABM
) | F(SSE4A
) | F(MISALIGNSSE
) |
1346 F(3DNOWPREFETCH
) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5
) |
1347 0 /* SKINIT */ | 0 /* WDT */;
1349 /* all calls to cpuid_count() should be made on the same cpu */
1351 do_cpuid_1_ent(entry
, function
, index
);
1356 entry
->eax
= min(entry
->eax
, (u32
)0xb);
1359 entry
->edx
&= kvm_supported_word0_x86_features
;
1360 entry
->ecx
&= kvm_supported_word4_x86_features
;
1362 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1363 * may return different values. This forces us to get_cpu() before
1364 * issuing the first command, and also to emulate this annoying behavior
1365 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1367 int t
, times
= entry
->eax
& 0xff;
1369 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1370 entry
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
1371 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
1372 do_cpuid_1_ent(&entry
[t
], function
, 0);
1373 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1378 /* function 4 and 0xb have additional index. */
1382 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1383 /* read more entries until cache_type is zero */
1384 for (i
= 1; *nent
< maxnent
; ++i
) {
1385 cache_type
= entry
[i
- 1].eax
& 0x1f;
1388 do_cpuid_1_ent(&entry
[i
], function
, i
);
1390 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1398 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1399 /* read more entries until level_type is zero */
1400 for (i
= 1; *nent
< maxnent
; ++i
) {
1401 level_type
= entry
[i
- 1].ecx
& 0xff00;
1404 do_cpuid_1_ent(&entry
[i
], function
, i
);
1406 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1412 entry
->eax
= min(entry
->eax
, 0x8000001a);
1415 entry
->edx
&= kvm_supported_word1_x86_features
;
1416 entry
->ecx
&= kvm_supported_word6_x86_features
;
1424 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
1425 struct kvm_cpuid_entry2 __user
*entries
)
1427 struct kvm_cpuid_entry2
*cpuid_entries
;
1428 int limit
, nent
= 0, r
= -E2BIG
;
1431 if (cpuid
->nent
< 1)
1433 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
1434 cpuid
->nent
= KVM_MAX_CPUID_ENTRIES
;
1436 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
1440 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
1441 limit
= cpuid_entries
[0].eax
;
1442 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1443 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1444 &nent
, cpuid
->nent
);
1446 if (nent
>= cpuid
->nent
)
1449 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
1450 limit
= cpuid_entries
[nent
- 1].eax
;
1451 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1452 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1453 &nent
, cpuid
->nent
);
1455 if (nent
>= cpuid
->nent
)
1459 if (copy_to_user(entries
, cpuid_entries
,
1460 nent
* sizeof(struct kvm_cpuid_entry2
)))
1466 vfree(cpuid_entries
);
1471 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
1472 struct kvm_lapic_state
*s
)
1475 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
1481 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
1482 struct kvm_lapic_state
*s
)
1485 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1486 kvm_apic_post_state_restore(vcpu
);
1492 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1493 struct kvm_interrupt
*irq
)
1495 if (irq
->irq
< 0 || irq
->irq
>= 256)
1497 if (irqchip_in_kernel(vcpu
->kvm
))
1501 kvm_queue_interrupt(vcpu
, irq
->irq
, false);
1508 static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu
*vcpu
)
1511 kvm_inject_nmi(vcpu
);
1517 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
1518 struct kvm_tpr_access_ctl
*tac
)
1522 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
1526 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1527 unsigned int ioctl
, unsigned long arg
)
1529 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1530 void __user
*argp
= (void __user
*)arg
;
1532 struct kvm_lapic_state
*lapic
= NULL
;
1535 case KVM_GET_LAPIC
: {
1536 lapic
= kzalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
1541 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, lapic
);
1545 if (copy_to_user(argp
, lapic
, sizeof(struct kvm_lapic_state
)))
1550 case KVM_SET_LAPIC
: {
1551 lapic
= kmalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
1556 if (copy_from_user(lapic
, argp
, sizeof(struct kvm_lapic_state
)))
1558 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, lapic
);
1564 case KVM_INTERRUPT
: {
1565 struct kvm_interrupt irq
;
1568 if (copy_from_user(&irq
, argp
, sizeof irq
))
1570 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1577 r
= kvm_vcpu_ioctl_nmi(vcpu
);
1583 case KVM_SET_CPUID
: {
1584 struct kvm_cpuid __user
*cpuid_arg
= argp
;
1585 struct kvm_cpuid cpuid
;
1588 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1590 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
1595 case KVM_SET_CPUID2
: {
1596 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1597 struct kvm_cpuid2 cpuid
;
1600 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1602 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
1603 cpuid_arg
->entries
);
1608 case KVM_GET_CPUID2
: {
1609 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1610 struct kvm_cpuid2 cpuid
;
1613 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1615 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
1616 cpuid_arg
->entries
);
1620 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1626 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
1629 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
1631 case KVM_TPR_ACCESS_REPORTING
: {
1632 struct kvm_tpr_access_ctl tac
;
1635 if (copy_from_user(&tac
, argp
, sizeof tac
))
1637 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
1641 if (copy_to_user(argp
, &tac
, sizeof tac
))
1646 case KVM_SET_VAPIC_ADDR
: {
1647 struct kvm_vapic_addr va
;
1650 if (!irqchip_in_kernel(vcpu
->kvm
))
1653 if (copy_from_user(&va
, argp
, sizeof va
))
1656 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
1667 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
1671 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
1673 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
1677 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
1678 u32 kvm_nr_mmu_pages
)
1680 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
1683 down_write(&kvm
->slots_lock
);
1684 spin_lock(&kvm
->mmu_lock
);
1686 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
1687 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
1689 spin_unlock(&kvm
->mmu_lock
);
1690 up_write(&kvm
->slots_lock
);
1694 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
1696 return kvm
->arch
.n_alloc_mmu_pages
;
1699 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1702 struct kvm_mem_alias
*alias
;
1704 for (i
= 0; i
< kvm
->arch
.naliases
; ++i
) {
1705 alias
= &kvm
->arch
.aliases
[i
];
1706 if (gfn
>= alias
->base_gfn
1707 && gfn
< alias
->base_gfn
+ alias
->npages
)
1708 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1714 * Set a new alias region. Aliases map a portion of physical memory into
1715 * another portion. This is useful for memory windows, for example the PC
1718 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
1719 struct kvm_memory_alias
*alias
)
1722 struct kvm_mem_alias
*p
;
1725 /* General sanity checks */
1726 if (alias
->memory_size
& (PAGE_SIZE
- 1))
1728 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
1730 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
1732 if (alias
->guest_phys_addr
+ alias
->memory_size
1733 < alias
->guest_phys_addr
)
1735 if (alias
->target_phys_addr
+ alias
->memory_size
1736 < alias
->target_phys_addr
)
1739 down_write(&kvm
->slots_lock
);
1740 spin_lock(&kvm
->mmu_lock
);
1742 p
= &kvm
->arch
.aliases
[alias
->slot
];
1743 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
1744 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
1745 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
1747 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
1748 if (kvm
->arch
.aliases
[n
- 1].npages
)
1750 kvm
->arch
.naliases
= n
;
1752 spin_unlock(&kvm
->mmu_lock
);
1753 kvm_mmu_zap_all(kvm
);
1755 up_write(&kvm
->slots_lock
);
1763 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1768 switch (chip
->chip_id
) {
1769 case KVM_IRQCHIP_PIC_MASTER
:
1770 memcpy(&chip
->chip
.pic
,
1771 &pic_irqchip(kvm
)->pics
[0],
1772 sizeof(struct kvm_pic_state
));
1774 case KVM_IRQCHIP_PIC_SLAVE
:
1775 memcpy(&chip
->chip
.pic
,
1776 &pic_irqchip(kvm
)->pics
[1],
1777 sizeof(struct kvm_pic_state
));
1779 case KVM_IRQCHIP_IOAPIC
:
1780 memcpy(&chip
->chip
.ioapic
,
1781 ioapic_irqchip(kvm
),
1782 sizeof(struct kvm_ioapic_state
));
1791 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1796 switch (chip
->chip_id
) {
1797 case KVM_IRQCHIP_PIC_MASTER
:
1798 memcpy(&pic_irqchip(kvm
)->pics
[0],
1800 sizeof(struct kvm_pic_state
));
1802 case KVM_IRQCHIP_PIC_SLAVE
:
1803 memcpy(&pic_irqchip(kvm
)->pics
[1],
1805 sizeof(struct kvm_pic_state
));
1807 case KVM_IRQCHIP_IOAPIC
:
1808 memcpy(ioapic_irqchip(kvm
),
1810 sizeof(struct kvm_ioapic_state
));
1816 kvm_pic_update_irq(pic_irqchip(kvm
));
1820 static int kvm_vm_ioctl_get_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1824 memcpy(ps
, &kvm
->arch
.vpit
->pit_state
, sizeof(struct kvm_pit_state
));
1828 static int kvm_vm_ioctl_set_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1832 memcpy(&kvm
->arch
.vpit
->pit_state
, ps
, sizeof(struct kvm_pit_state
));
1833 kvm_pit_load_count(kvm
, 0, ps
->channels
[0].count
);
1837 static int kvm_vm_ioctl_reinject(struct kvm
*kvm
,
1838 struct kvm_reinject_control
*control
)
1840 if (!kvm
->arch
.vpit
)
1842 kvm
->arch
.vpit
->pit_state
.pit_timer
.reinject
= control
->pit_reinject
;
1847 * Get (and clear) the dirty memory log for a memory slot.
1849 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1850 struct kvm_dirty_log
*log
)
1854 struct kvm_memory_slot
*memslot
;
1857 down_write(&kvm
->slots_lock
);
1859 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1863 /* If nothing is dirty, don't bother messing with page tables. */
1865 spin_lock(&kvm
->mmu_lock
);
1866 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
1867 spin_unlock(&kvm
->mmu_lock
);
1868 kvm_flush_remote_tlbs(kvm
);
1869 memslot
= &kvm
->memslots
[log
->slot
];
1870 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1871 memset(memslot
->dirty_bitmap
, 0, n
);
1875 up_write(&kvm
->slots_lock
);
1879 long kvm_arch_vm_ioctl(struct file
*filp
,
1880 unsigned int ioctl
, unsigned long arg
)
1882 struct kvm
*kvm
= filp
->private_data
;
1883 void __user
*argp
= (void __user
*)arg
;
1886 * This union makes it completely explicit to gcc-3.x
1887 * that these two variables' stack usage should be
1888 * combined, not added together.
1891 struct kvm_pit_state ps
;
1892 struct kvm_memory_alias alias
;
1896 case KVM_SET_TSS_ADDR
:
1897 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
1901 case KVM_SET_MEMORY_REGION
: {
1902 struct kvm_memory_region kvm_mem
;
1903 struct kvm_userspace_memory_region kvm_userspace_mem
;
1906 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
1908 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
1909 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
1910 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
1911 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
1912 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1917 case KVM_SET_NR_MMU_PAGES
:
1918 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1922 case KVM_GET_NR_MMU_PAGES
:
1923 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1925 case KVM_SET_MEMORY_ALIAS
:
1927 if (copy_from_user(&u
.alias
, argp
, sizeof(struct kvm_memory_alias
)))
1929 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &u
.alias
);
1933 case KVM_CREATE_IRQCHIP
:
1935 kvm
->arch
.vpic
= kvm_create_pic(kvm
);
1936 if (kvm
->arch
.vpic
) {
1937 r
= kvm_ioapic_init(kvm
);
1939 kfree(kvm
->arch
.vpic
);
1940 kvm
->arch
.vpic
= NULL
;
1945 r
= kvm_setup_default_irq_routing(kvm
);
1947 kfree(kvm
->arch
.vpic
);
1948 kfree(kvm
->arch
.vioapic
);
1952 case KVM_CREATE_PIT
:
1953 mutex_lock(&kvm
->lock
);
1956 goto create_pit_unlock
;
1958 kvm
->arch
.vpit
= kvm_create_pit(kvm
);
1962 mutex_unlock(&kvm
->lock
);
1964 case KVM_IRQ_LINE_STATUS
:
1965 case KVM_IRQ_LINE
: {
1966 struct kvm_irq_level irq_event
;
1969 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1971 if (irqchip_in_kernel(kvm
)) {
1973 mutex_lock(&kvm
->lock
);
1974 status
= kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
,
1975 irq_event
.irq
, irq_event
.level
);
1976 mutex_unlock(&kvm
->lock
);
1977 if (ioctl
== KVM_IRQ_LINE_STATUS
) {
1978 irq_event
.status
= status
;
1979 if (copy_to_user(argp
, &irq_event
,
1987 case KVM_GET_IRQCHIP
: {
1988 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1989 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
1995 if (copy_from_user(chip
, argp
, sizeof *chip
))
1996 goto get_irqchip_out
;
1998 if (!irqchip_in_kernel(kvm
))
1999 goto get_irqchip_out
;
2000 r
= kvm_vm_ioctl_get_irqchip(kvm
, chip
);
2002 goto get_irqchip_out
;
2004 if (copy_to_user(argp
, chip
, sizeof *chip
))
2005 goto get_irqchip_out
;
2013 case KVM_SET_IRQCHIP
: {
2014 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
2015 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
2021 if (copy_from_user(chip
, argp
, sizeof *chip
))
2022 goto set_irqchip_out
;
2024 if (!irqchip_in_kernel(kvm
))
2025 goto set_irqchip_out
;
2026 r
= kvm_vm_ioctl_set_irqchip(kvm
, chip
);
2028 goto set_irqchip_out
;
2038 if (copy_from_user(&u
.ps
, argp
, sizeof(struct kvm_pit_state
)))
2041 if (!kvm
->arch
.vpit
)
2043 r
= kvm_vm_ioctl_get_pit(kvm
, &u
.ps
);
2047 if (copy_to_user(argp
, &u
.ps
, sizeof(struct kvm_pit_state
)))
2054 if (copy_from_user(&u
.ps
, argp
, sizeof u
.ps
))
2057 if (!kvm
->arch
.vpit
)
2059 r
= kvm_vm_ioctl_set_pit(kvm
, &u
.ps
);
2065 case KVM_REINJECT_CONTROL
: {
2066 struct kvm_reinject_control control
;
2068 if (copy_from_user(&control
, argp
, sizeof(control
)))
2070 r
= kvm_vm_ioctl_reinject(kvm
, &control
);
2083 static void kvm_init_msr_list(void)
2088 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
2089 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
2092 msrs_to_save
[j
] = msrs_to_save
[i
];
2095 num_msrs_to_save
= j
;
2099 * Only apic need an MMIO device hook, so shortcut now..
2101 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
2102 gpa_t addr
, int len
,
2105 struct kvm_io_device
*dev
;
2107 if (vcpu
->arch
.apic
) {
2108 dev
= &vcpu
->arch
.apic
->dev
;
2109 if (dev
->in_range(dev
, addr
, len
, is_write
))
2116 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
2117 gpa_t addr
, int len
,
2120 struct kvm_io_device
*dev
;
2122 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
, len
, is_write
);
2124 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
, len
,
2129 static int kvm_read_guest_virt(gva_t addr
, void *val
, unsigned int bytes
,
2130 struct kvm_vcpu
*vcpu
)
2133 int r
= X86EMUL_CONTINUE
;
2136 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2137 unsigned offset
= addr
& (PAGE_SIZE
-1);
2138 unsigned toread
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
2141 if (gpa
== UNMAPPED_GVA
) {
2142 r
= X86EMUL_PROPAGATE_FAULT
;
2145 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, toread
);
2147 r
= X86EMUL_UNHANDLEABLE
;
2159 static int kvm_write_guest_virt(gva_t addr
, void *val
, unsigned int bytes
,
2160 struct kvm_vcpu
*vcpu
)
2163 int r
= X86EMUL_CONTINUE
;
2166 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2167 unsigned offset
= addr
& (PAGE_SIZE
-1);
2168 unsigned towrite
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
2171 if (gpa
== UNMAPPED_GVA
) {
2172 r
= X86EMUL_PROPAGATE_FAULT
;
2175 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, data
, towrite
);
2177 r
= X86EMUL_UNHANDLEABLE
;
2190 static int emulator_read_emulated(unsigned long addr
,
2193 struct kvm_vcpu
*vcpu
)
2195 struct kvm_io_device
*mmio_dev
;
2198 if (vcpu
->mmio_read_completed
) {
2199 memcpy(val
, vcpu
->mmio_data
, bytes
);
2200 vcpu
->mmio_read_completed
= 0;
2201 return X86EMUL_CONTINUE
;
2204 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2206 /* For APIC access vmexit */
2207 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
2210 if (kvm_read_guest_virt(addr
, val
, bytes
, vcpu
)
2211 == X86EMUL_CONTINUE
)
2212 return X86EMUL_CONTINUE
;
2213 if (gpa
== UNMAPPED_GVA
)
2214 return X86EMUL_PROPAGATE_FAULT
;
2218 * Is this MMIO handled locally?
2220 mutex_lock(&vcpu
->kvm
->lock
);
2221 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
, bytes
, 0);
2223 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
2224 mutex_unlock(&vcpu
->kvm
->lock
);
2225 return X86EMUL_CONTINUE
;
2227 mutex_unlock(&vcpu
->kvm
->lock
);
2229 vcpu
->mmio_needed
= 1;
2230 vcpu
->mmio_phys_addr
= gpa
;
2231 vcpu
->mmio_size
= bytes
;
2232 vcpu
->mmio_is_write
= 0;
2234 return X86EMUL_UNHANDLEABLE
;
2237 int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
2238 const void *val
, int bytes
)
2242 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
2245 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
, 1);
2249 static int emulator_write_emulated_onepage(unsigned long addr
,
2252 struct kvm_vcpu
*vcpu
)
2254 struct kvm_io_device
*mmio_dev
;
2257 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2259 if (gpa
== UNMAPPED_GVA
) {
2260 kvm_inject_page_fault(vcpu
, addr
, 2);
2261 return X86EMUL_PROPAGATE_FAULT
;
2264 /* For APIC access vmexit */
2265 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
2268 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
2269 return X86EMUL_CONTINUE
;
2273 * Is this MMIO handled locally?
2275 mutex_lock(&vcpu
->kvm
->lock
);
2276 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
, bytes
, 1);
2278 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
2279 mutex_unlock(&vcpu
->kvm
->lock
);
2280 return X86EMUL_CONTINUE
;
2282 mutex_unlock(&vcpu
->kvm
->lock
);
2284 vcpu
->mmio_needed
= 1;
2285 vcpu
->mmio_phys_addr
= gpa
;
2286 vcpu
->mmio_size
= bytes
;
2287 vcpu
->mmio_is_write
= 1;
2288 memcpy(vcpu
->mmio_data
, val
, bytes
);
2290 return X86EMUL_CONTINUE
;
2293 int emulator_write_emulated(unsigned long addr
,
2296 struct kvm_vcpu
*vcpu
)
2298 /* Crossing a page boundary? */
2299 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
2302 now
= -addr
& ~PAGE_MASK
;
2303 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
2304 if (rc
!= X86EMUL_CONTINUE
)
2310 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
2312 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
2314 static int emulator_cmpxchg_emulated(unsigned long addr
,
2318 struct kvm_vcpu
*vcpu
)
2320 static int reported
;
2324 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
2326 #ifndef CONFIG_X86_64
2327 /* guests cmpxchg8b have to be emulated atomically */
2334 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2336 if (gpa
== UNMAPPED_GVA
||
2337 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
2340 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
2345 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
2347 kaddr
= kmap_atomic(page
, KM_USER0
);
2348 set_64bit((u64
*)(kaddr
+ offset_in_page(gpa
)), val
);
2349 kunmap_atomic(kaddr
, KM_USER0
);
2350 kvm_release_page_dirty(page
);
2355 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
2358 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
2360 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
2363 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
2365 kvm_mmu_invlpg(vcpu
, address
);
2366 return X86EMUL_CONTINUE
;
2369 int emulate_clts(struct kvm_vcpu
*vcpu
)
2371 KVMTRACE_0D(CLTS
, vcpu
, handler
);
2372 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
& ~X86_CR0_TS
);
2373 return X86EMUL_CONTINUE
;
2376 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
2378 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
2382 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
2383 return X86EMUL_CONTINUE
;
2385 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __func__
, dr
);
2386 return X86EMUL_UNHANDLEABLE
;
2390 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
2392 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
2395 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
2397 /* FIXME: better handling */
2398 return X86EMUL_UNHANDLEABLE
;
2400 return X86EMUL_CONTINUE
;
2403 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
2406 unsigned long rip
= kvm_rip_read(vcpu
);
2407 unsigned long rip_linear
;
2409 if (!printk_ratelimit())
2412 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
2414 kvm_read_guest_virt(rip_linear
, (void *)opcodes
, 4, vcpu
);
2416 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2417 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
2419 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
2421 static struct x86_emulate_ops emulate_ops
= {
2422 .read_std
= kvm_read_guest_virt
,
2423 .read_emulated
= emulator_read_emulated
,
2424 .write_emulated
= emulator_write_emulated
,
2425 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
2428 static void cache_all_regs(struct kvm_vcpu
*vcpu
)
2430 kvm_register_read(vcpu
, VCPU_REGS_RAX
);
2431 kvm_register_read(vcpu
, VCPU_REGS_RSP
);
2432 kvm_register_read(vcpu
, VCPU_REGS_RIP
);
2433 vcpu
->arch
.regs_dirty
= ~0;
2436 int emulate_instruction(struct kvm_vcpu
*vcpu
,
2437 struct kvm_run
*run
,
2443 struct decode_cache
*c
;
2445 kvm_clear_exception_queue(vcpu
);
2446 vcpu
->arch
.mmio_fault_cr2
= cr2
;
2448 * TODO: fix x86_emulate.c to use guest_read/write_register
2449 * instead of direct ->regs accesses, can save hundred cycles
2450 * on Intel for instructions that don't read/change RSP, for
2453 cache_all_regs(vcpu
);
2455 vcpu
->mmio_is_write
= 0;
2456 vcpu
->arch
.pio
.string
= 0;
2458 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
2460 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
2462 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
2463 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
2464 vcpu
->arch
.emulate_ctxt
.mode
=
2465 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
2466 ? X86EMUL_MODE_REAL
: cs_l
2467 ? X86EMUL_MODE_PROT64
: cs_db
2468 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
2470 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2472 /* Reject the instructions other than VMCALL/VMMCALL when
2473 * try to emulate invalid opcode */
2474 c
= &vcpu
->arch
.emulate_ctxt
.decode
;
2475 if ((emulation_type
& EMULTYPE_TRAP_UD
) &&
2476 (!(c
->twobyte
&& c
->b
== 0x01 &&
2477 (c
->modrm_reg
== 0 || c
->modrm_reg
== 3) &&
2478 c
->modrm_mod
== 3 && c
->modrm_rm
== 1)))
2479 return EMULATE_FAIL
;
2481 ++vcpu
->stat
.insn_emulation
;
2483 ++vcpu
->stat
.insn_emulation_fail
;
2484 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2485 return EMULATE_DONE
;
2486 return EMULATE_FAIL
;
2490 if (emulation_type
& EMULTYPE_SKIP
) {
2491 kvm_rip_write(vcpu
, vcpu
->arch
.emulate_ctxt
.decode
.eip
);
2492 return EMULATE_DONE
;
2495 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2496 shadow_mask
= vcpu
->arch
.emulate_ctxt
.interruptibility
;
2499 kvm_x86_ops
->set_interrupt_shadow(vcpu
, shadow_mask
);
2501 if (vcpu
->arch
.pio
.string
)
2502 return EMULATE_DO_MMIO
;
2504 if ((r
|| vcpu
->mmio_is_write
) && run
) {
2505 run
->exit_reason
= KVM_EXIT_MMIO
;
2506 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
2507 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
2508 run
->mmio
.len
= vcpu
->mmio_size
;
2509 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
2513 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2514 return EMULATE_DONE
;
2515 if (!vcpu
->mmio_needed
) {
2516 kvm_report_emulation_failure(vcpu
, "mmio");
2517 return EMULATE_FAIL
;
2519 return EMULATE_DO_MMIO
;
2522 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
2524 if (vcpu
->mmio_is_write
) {
2525 vcpu
->mmio_needed
= 0;
2526 return EMULATE_DO_MMIO
;
2529 return EMULATE_DONE
;
2531 EXPORT_SYMBOL_GPL(emulate_instruction
);
2533 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
2535 void *p
= vcpu
->arch
.pio_data
;
2536 gva_t q
= vcpu
->arch
.pio
.guest_gva
;
2540 bytes
= vcpu
->arch
.pio
.size
* vcpu
->arch
.pio
.cur_count
;
2541 if (vcpu
->arch
.pio
.in
)
2542 ret
= kvm_write_guest_virt(q
, p
, bytes
, vcpu
);
2544 ret
= kvm_read_guest_virt(q
, p
, bytes
, vcpu
);
2548 int complete_pio(struct kvm_vcpu
*vcpu
)
2550 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2557 val
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
2558 memcpy(&val
, vcpu
->arch
.pio_data
, io
->size
);
2559 kvm_register_write(vcpu
, VCPU_REGS_RAX
, val
);
2563 r
= pio_copy_data(vcpu
);
2570 delta
*= io
->cur_count
;
2572 * The size of the register should really depend on
2573 * current address size.
2575 val
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
2577 kvm_register_write(vcpu
, VCPU_REGS_RCX
, val
);
2583 val
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
2585 kvm_register_write(vcpu
, VCPU_REGS_RDI
, val
);
2587 val
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
2589 kvm_register_write(vcpu
, VCPU_REGS_RSI
, val
);
2593 io
->count
-= io
->cur_count
;
2599 static void kernel_pio(struct kvm_io_device
*pio_dev
,
2600 struct kvm_vcpu
*vcpu
,
2603 /* TODO: String I/O for in kernel device */
2605 mutex_lock(&vcpu
->kvm
->lock
);
2606 if (vcpu
->arch
.pio
.in
)
2607 kvm_iodevice_read(pio_dev
, vcpu
->arch
.pio
.port
,
2608 vcpu
->arch
.pio
.size
,
2611 kvm_iodevice_write(pio_dev
, vcpu
->arch
.pio
.port
,
2612 vcpu
->arch
.pio
.size
,
2614 mutex_unlock(&vcpu
->kvm
->lock
);
2617 static void pio_string_write(struct kvm_io_device
*pio_dev
,
2618 struct kvm_vcpu
*vcpu
)
2620 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2621 void *pd
= vcpu
->arch
.pio_data
;
2624 mutex_lock(&vcpu
->kvm
->lock
);
2625 for (i
= 0; i
< io
->cur_count
; i
++) {
2626 kvm_iodevice_write(pio_dev
, io
->port
,
2631 mutex_unlock(&vcpu
->kvm
->lock
);
2634 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
2635 gpa_t addr
, int len
,
2638 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
, len
, is_write
);
2641 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2642 int size
, unsigned port
)
2644 struct kvm_io_device
*pio_dev
;
2647 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2648 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2649 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2650 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2651 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= 1;
2652 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2653 vcpu
->arch
.pio
.in
= in
;
2654 vcpu
->arch
.pio
.string
= 0;
2655 vcpu
->arch
.pio
.down
= 0;
2656 vcpu
->arch
.pio
.rep
= 0;
2658 if (vcpu
->run
->io
.direction
== KVM_EXIT_IO_IN
)
2659 KVMTRACE_2D(IO_READ
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2662 KVMTRACE_2D(IO_WRITE
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2665 val
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
2666 memcpy(vcpu
->arch
.pio_data
, &val
, 4);
2668 pio_dev
= vcpu_find_pio_dev(vcpu
, port
, size
, !in
);
2670 kernel_pio(pio_dev
, vcpu
, vcpu
->arch
.pio_data
);
2676 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
2678 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2679 int size
, unsigned long count
, int down
,
2680 gva_t address
, int rep
, unsigned port
)
2682 unsigned now
, in_page
;
2684 struct kvm_io_device
*pio_dev
;
2686 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2687 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2688 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2689 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2690 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= count
;
2691 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2692 vcpu
->arch
.pio
.in
= in
;
2693 vcpu
->arch
.pio
.string
= 1;
2694 vcpu
->arch
.pio
.down
= down
;
2695 vcpu
->arch
.pio
.rep
= rep
;
2697 if (vcpu
->run
->io
.direction
== KVM_EXIT_IO_IN
)
2698 KVMTRACE_2D(IO_READ
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2701 KVMTRACE_2D(IO_WRITE
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2705 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2710 in_page
= PAGE_SIZE
- offset_in_page(address
);
2712 in_page
= offset_in_page(address
) + size
;
2713 now
= min(count
, (unsigned long)in_page
/ size
);
2718 * String I/O in reverse. Yuck. Kill the guest, fix later.
2720 pr_unimpl(vcpu
, "guest string pio down\n");
2721 kvm_inject_gp(vcpu
, 0);
2724 vcpu
->run
->io
.count
= now
;
2725 vcpu
->arch
.pio
.cur_count
= now
;
2727 if (vcpu
->arch
.pio
.cur_count
== vcpu
->arch
.pio
.count
)
2728 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2730 vcpu
->arch
.pio
.guest_gva
= address
;
2732 pio_dev
= vcpu_find_pio_dev(vcpu
, port
,
2733 vcpu
->arch
.pio
.cur_count
,
2734 !vcpu
->arch
.pio
.in
);
2735 if (!vcpu
->arch
.pio
.in
) {
2736 /* string PIO write */
2737 ret
= pio_copy_data(vcpu
);
2738 if (ret
== X86EMUL_PROPAGATE_FAULT
) {
2739 kvm_inject_gp(vcpu
, 0);
2742 if (ret
== 0 && pio_dev
) {
2743 pio_string_write(pio_dev
, vcpu
);
2745 if (vcpu
->arch
.pio
.count
== 0)
2749 pr_unimpl(vcpu
, "no string pio read support yet, "
2750 "port %x size %d count %ld\n",
2755 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2757 static void bounce_off(void *info
)
2762 static unsigned int ref_freq
;
2763 static unsigned long tsc_khz_ref
;
2765 static int kvmclock_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
2768 struct cpufreq_freqs
*freq
= data
;
2770 struct kvm_vcpu
*vcpu
;
2771 int i
, send_ipi
= 0;
2774 ref_freq
= freq
->old
;
2776 if (val
== CPUFREQ_PRECHANGE
&& freq
->old
> freq
->new)
2778 if (val
== CPUFREQ_POSTCHANGE
&& freq
->old
< freq
->new)
2780 per_cpu(cpu_tsc_khz
, freq
->cpu
) = cpufreq_scale(tsc_khz_ref
, ref_freq
, freq
->new);
2782 spin_lock(&kvm_lock
);
2783 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
2784 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
2785 vcpu
= kvm
->vcpus
[i
];
2788 if (vcpu
->cpu
!= freq
->cpu
)
2790 if (!kvm_request_guest_time_update(vcpu
))
2792 if (vcpu
->cpu
!= smp_processor_id())
2796 spin_unlock(&kvm_lock
);
2798 if (freq
->old
< freq
->new && send_ipi
) {
2800 * We upscale the frequency. Must make the guest
2801 * doesn't see old kvmclock values while running with
2802 * the new frequency, otherwise we risk the guest sees
2803 * time go backwards.
2805 * In case we update the frequency for another cpu
2806 * (which might be in guest context) send an interrupt
2807 * to kick the cpu out of guest context. Next time
2808 * guest context is entered kvmclock will be updated,
2809 * so the guest will not see stale values.
2811 smp_call_function_single(freq
->cpu
, bounce_off
, NULL
, 1);
2816 static struct notifier_block kvmclock_cpufreq_notifier_block
= {
2817 .notifier_call
= kvmclock_cpufreq_notifier
2820 int kvm_arch_init(void *opaque
)
2823 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
2826 printk(KERN_ERR
"kvm: already loaded the other module\n");
2831 if (!ops
->cpu_has_kvm_support()) {
2832 printk(KERN_ERR
"kvm: no hardware support\n");
2836 if (ops
->disabled_by_bios()) {
2837 printk(KERN_ERR
"kvm: disabled by bios\n");
2842 r
= kvm_mmu_module_init();
2846 kvm_init_msr_list();
2849 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2850 kvm_mmu_set_base_ptes(PT_PRESENT_MASK
);
2851 kvm_mmu_set_mask_ptes(PT_USER_MASK
, PT_ACCESSED_MASK
,
2852 PT_DIRTY_MASK
, PT64_NX_MASK
, 0);
2854 for_each_possible_cpu(cpu
)
2855 per_cpu(cpu_tsc_khz
, cpu
) = tsc_khz
;
2856 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
)) {
2857 tsc_khz_ref
= tsc_khz
;
2858 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block
,
2859 CPUFREQ_TRANSITION_NOTIFIER
);
2868 void kvm_arch_exit(void)
2870 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC
))
2871 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block
,
2872 CPUFREQ_TRANSITION_NOTIFIER
);
2874 kvm_mmu_module_exit();
2877 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
2879 ++vcpu
->stat
.halt_exits
;
2880 KVMTRACE_0D(HLT
, vcpu
, handler
);
2881 if (irqchip_in_kernel(vcpu
->kvm
)) {
2882 vcpu
->arch
.mp_state
= KVM_MP_STATE_HALTED
;
2885 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
2889 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
2891 static inline gpa_t
hc_gpa(struct kvm_vcpu
*vcpu
, unsigned long a0
,
2894 if (is_long_mode(vcpu
))
2897 return a0
| ((gpa_t
)a1
<< 32);
2900 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
2902 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
2905 nr
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
2906 a0
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
2907 a1
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
2908 a2
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
2909 a3
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
2911 KVMTRACE_1D(VMMCALL
, vcpu
, (u32
)nr
, handler
);
2913 if (!is_long_mode(vcpu
)) {
2921 if (kvm_x86_ops
->get_cpl(vcpu
) != 0) {
2927 case KVM_HC_VAPIC_POLL_IRQ
:
2931 r
= kvm_pv_mmu_op(vcpu
, a0
, hc_gpa(vcpu
, a1
, a2
), &ret
);
2938 kvm_register_write(vcpu
, VCPU_REGS_RAX
, ret
);
2939 ++vcpu
->stat
.hypercalls
;
2942 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
2944 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
2946 char instruction
[3];
2948 unsigned long rip
= kvm_rip_read(vcpu
);
2952 * Blow out the MMU to ensure that no other VCPU has an active mapping
2953 * to ensure that the updated hypercall appears atomically across all
2956 kvm_mmu_zap_all(vcpu
->kvm
);
2958 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
2959 if (emulator_write_emulated(rip
, instruction
, 3, vcpu
)
2960 != X86EMUL_CONTINUE
)
2966 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
2968 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
2971 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2973 struct descriptor_table dt
= { limit
, base
};
2975 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2978 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2980 struct descriptor_table dt
= { limit
, base
};
2982 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2985 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
2986 unsigned long *rflags
)
2988 kvm_lmsw(vcpu
, msw
);
2989 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2992 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
2994 unsigned long value
;
2996 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2999 value
= vcpu
->arch
.cr0
;
3002 value
= vcpu
->arch
.cr2
;
3005 value
= vcpu
->arch
.cr3
;
3008 value
= vcpu
->arch
.cr4
;
3011 value
= kvm_get_cr8(vcpu
);
3014 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
3017 KVMTRACE_3D(CR_READ
, vcpu
, (u32
)cr
, (u32
)value
,
3018 (u32
)((u64
)value
>> 32), handler
);
3023 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
3024 unsigned long *rflags
)
3026 KVMTRACE_3D(CR_WRITE
, vcpu
, (u32
)cr
, (u32
)val
,
3027 (u32
)((u64
)val
>> 32), handler
);
3031 kvm_set_cr0(vcpu
, mk_cr_64(vcpu
->arch
.cr0
, val
));
3032 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
3035 vcpu
->arch
.cr2
= val
;
3038 kvm_set_cr3(vcpu
, val
);
3041 kvm_set_cr4(vcpu
, mk_cr_64(vcpu
->arch
.cr4
, val
));
3044 kvm_set_cr8(vcpu
, val
& 0xfUL
);
3047 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
3051 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
3053 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
3054 int j
, nent
= vcpu
->arch
.cpuid_nent
;
3056 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
3057 /* when no next entry is found, the current entry[i] is reselected */
3058 for (j
= i
+ 1; ; j
= (j
+ 1) % nent
) {
3059 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
3060 if (ej
->function
== e
->function
) {
3061 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
3065 return 0; /* silence gcc, even though control never reaches here */
3068 /* find an entry with matching function, matching index (if needed), and that
3069 * should be read next (if it's stateful) */
3070 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
3071 u32 function
, u32 index
)
3073 if (e
->function
!= function
)
3075 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
3077 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
3078 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
3083 struct kvm_cpuid_entry2
*kvm_find_cpuid_entry(struct kvm_vcpu
*vcpu
,
3084 u32 function
, u32 index
)
3087 struct kvm_cpuid_entry2
*best
= NULL
;
3089 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
3090 struct kvm_cpuid_entry2
*e
;
3092 e
= &vcpu
->arch
.cpuid_entries
[i
];
3093 if (is_matching_cpuid_entry(e
, function
, index
)) {
3094 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
3095 move_to_next_stateful_cpuid_entry(vcpu
, i
);
3100 * Both basic or both extended?
3102 if (((e
->function
^ function
) & 0x80000000) == 0)
3103 if (!best
|| e
->function
> best
->function
)
3109 int cpuid_maxphyaddr(struct kvm_vcpu
*vcpu
)
3111 struct kvm_cpuid_entry2
*best
;
3113 best
= kvm_find_cpuid_entry(vcpu
, 0x80000008, 0);
3115 return best
->eax
& 0xff;
3119 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
3121 u32 function
, index
;
3122 struct kvm_cpuid_entry2
*best
;
3124 function
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3125 index
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
3126 kvm_register_write(vcpu
, VCPU_REGS_RAX
, 0);
3127 kvm_register_write(vcpu
, VCPU_REGS_RBX
, 0);
3128 kvm_register_write(vcpu
, VCPU_REGS_RCX
, 0);
3129 kvm_register_write(vcpu
, VCPU_REGS_RDX
, 0);
3130 best
= kvm_find_cpuid_entry(vcpu
, function
, index
);
3132 kvm_register_write(vcpu
, VCPU_REGS_RAX
, best
->eax
);
3133 kvm_register_write(vcpu
, VCPU_REGS_RBX
, best
->ebx
);
3134 kvm_register_write(vcpu
, VCPU_REGS_RCX
, best
->ecx
);
3135 kvm_register_write(vcpu
, VCPU_REGS_RDX
, best
->edx
);
3137 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
3138 KVMTRACE_5D(CPUID
, vcpu
, function
,
3139 (u32
)kvm_register_read(vcpu
, VCPU_REGS_RAX
),
3140 (u32
)kvm_register_read(vcpu
, VCPU_REGS_RBX
),
3141 (u32
)kvm_register_read(vcpu
, VCPU_REGS_RCX
),
3142 (u32
)kvm_register_read(vcpu
, VCPU_REGS_RDX
), handler
);
3144 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
3147 * Check if userspace requested an interrupt window, and that the
3148 * interrupt window is open.
3150 * No need to exit to userspace if we already have an interrupt queued.
3152 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
3153 struct kvm_run
*kvm_run
)
3155 return (!irqchip_in_kernel(vcpu
->kvm
) && !kvm_cpu_has_interrupt(vcpu
) &&
3156 kvm_run
->request_interrupt_window
&&
3157 kvm_arch_interrupt_allowed(vcpu
));
3160 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
3161 struct kvm_run
*kvm_run
)
3163 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
3164 kvm_run
->cr8
= kvm_get_cr8(vcpu
);
3165 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
3166 if (irqchip_in_kernel(vcpu
->kvm
))
3167 kvm_run
->ready_for_interrupt_injection
= 1;
3169 kvm_run
->ready_for_interrupt_injection
=
3170 kvm_arch_interrupt_allowed(vcpu
) &&
3171 !kvm_cpu_has_interrupt(vcpu
) &&
3172 !kvm_event_needs_reinjection(vcpu
);
3175 static void vapic_enter(struct kvm_vcpu
*vcpu
)
3177 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3180 if (!apic
|| !apic
->vapic_addr
)
3183 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
3185 vcpu
->arch
.apic
->vapic_page
= page
;
3188 static void vapic_exit(struct kvm_vcpu
*vcpu
)
3190 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
3192 if (!apic
|| !apic
->vapic_addr
)
3195 down_read(&vcpu
->kvm
->slots_lock
);
3196 kvm_release_page_dirty(apic
->vapic_page
);
3197 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
3198 up_read(&vcpu
->kvm
->slots_lock
);
3201 static void update_cr8_intercept(struct kvm_vcpu
*vcpu
)
3205 if (!kvm_x86_ops
->update_cr8_intercept
)
3208 if (!vcpu
->arch
.apic
)
3211 if (!vcpu
->arch
.apic
->vapic_addr
)
3212 max_irr
= kvm_lapic_find_highest_irr(vcpu
);
3219 tpr
= kvm_lapic_get_cr8(vcpu
);
3221 kvm_x86_ops
->update_cr8_intercept(vcpu
, tpr
, max_irr
);
3224 static void inject_pending_irq(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3226 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
3227 kvm_x86_ops
->set_interrupt_shadow(vcpu
, 0);
3229 /* try to reinject previous events if any */
3230 if (vcpu
->arch
.nmi_injected
) {
3231 kvm_x86_ops
->set_nmi(vcpu
);
3235 if (vcpu
->arch
.interrupt
.pending
) {
3236 kvm_x86_ops
->set_irq(vcpu
);
3240 /* try to inject new event if pending */
3241 if (vcpu
->arch
.nmi_pending
) {
3242 if (kvm_x86_ops
->nmi_allowed(vcpu
)) {
3243 vcpu
->arch
.nmi_pending
= false;
3244 vcpu
->arch
.nmi_injected
= true;
3245 kvm_x86_ops
->set_nmi(vcpu
);
3247 } else if (kvm_cpu_has_interrupt(vcpu
)) {
3248 if (kvm_x86_ops
->interrupt_allowed(vcpu
)) {
3249 kvm_queue_interrupt(vcpu
, kvm_cpu_get_interrupt(vcpu
),
3251 kvm_x86_ops
->set_irq(vcpu
);
3256 static int vcpu_enter_guest(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3259 bool req_int_win
= !irqchip_in_kernel(vcpu
->kvm
) &&
3260 kvm_run
->request_interrupt_window
;
3263 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
3264 kvm_mmu_unload(vcpu
);
3266 r
= kvm_mmu_reload(vcpu
);
3270 if (vcpu
->requests
) {
3271 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
3272 __kvm_migrate_timers(vcpu
);
3273 if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE
, &vcpu
->requests
))
3274 kvm_write_guest_time(vcpu
);
3275 if (test_and_clear_bit(KVM_REQ_MMU_SYNC
, &vcpu
->requests
))
3276 kvm_mmu_sync_roots(vcpu
);
3277 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
3278 kvm_x86_ops
->tlb_flush(vcpu
);
3279 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
3281 kvm_run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
3285 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
)) {
3286 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
3294 kvm_x86_ops
->prepare_guest_switch(vcpu
);
3295 kvm_load_guest_fpu(vcpu
);
3297 local_irq_disable();
3299 clear_bit(KVM_REQ_KICK
, &vcpu
->requests
);
3300 smp_mb__after_clear_bit();
3302 if (vcpu
->requests
|| need_resched() || signal_pending(current
)) {
3309 if (vcpu
->arch
.exception
.pending
)
3310 __queue_exception(vcpu
);
3312 inject_pending_irq(vcpu
, kvm_run
);
3314 /* enable NMI/IRQ window open exits if needed */
3315 if (vcpu
->arch
.nmi_pending
)
3316 kvm_x86_ops
->enable_nmi_window(vcpu
);
3317 else if (kvm_cpu_has_interrupt(vcpu
) || req_int_win
)
3318 kvm_x86_ops
->enable_irq_window(vcpu
);
3320 if (kvm_lapic_enabled(vcpu
)) {
3321 update_cr8_intercept(vcpu
);
3322 kvm_lapic_sync_to_vapic(vcpu
);
3325 up_read(&vcpu
->kvm
->slots_lock
);
3329 get_debugreg(vcpu
->arch
.host_dr6
, 6);
3330 get_debugreg(vcpu
->arch
.host_dr7
, 7);
3331 if (unlikely(vcpu
->arch
.switch_db_regs
)) {
3332 get_debugreg(vcpu
->arch
.host_db
[0], 0);
3333 get_debugreg(vcpu
->arch
.host_db
[1], 1);
3334 get_debugreg(vcpu
->arch
.host_db
[2], 2);
3335 get_debugreg(vcpu
->arch
.host_db
[3], 3);
3338 set_debugreg(vcpu
->arch
.eff_db
[0], 0);
3339 set_debugreg(vcpu
->arch
.eff_db
[1], 1);
3340 set_debugreg(vcpu
->arch
.eff_db
[2], 2);
3341 set_debugreg(vcpu
->arch
.eff_db
[3], 3);
3344 KVMTRACE_0D(VMENTRY
, vcpu
, entryexit
);
3345 kvm_x86_ops
->run(vcpu
, kvm_run
);
3347 if (unlikely(vcpu
->arch
.switch_db_regs
)) {
3349 set_debugreg(vcpu
->arch
.host_db
[0], 0);
3350 set_debugreg(vcpu
->arch
.host_db
[1], 1);
3351 set_debugreg(vcpu
->arch
.host_db
[2], 2);
3352 set_debugreg(vcpu
->arch
.host_db
[3], 3);
3354 set_debugreg(vcpu
->arch
.host_dr6
, 6);
3355 set_debugreg(vcpu
->arch
.host_dr7
, 7);
3357 set_bit(KVM_REQ_KICK
, &vcpu
->requests
);
3363 * We must have an instruction between local_irq_enable() and
3364 * kvm_guest_exit(), so the timer interrupt isn't delayed by
3365 * the interrupt shadow. The stat.exits increment will do nicely.
3366 * But we need to prevent reordering, hence this barrier():
3374 down_read(&vcpu
->kvm
->slots_lock
);
3377 * Profile KVM exit RIPs:
3379 if (unlikely(prof_on
== KVM_PROFILING
)) {
3380 unsigned long rip
= kvm_rip_read(vcpu
);
3381 profile_hit(KVM_PROFILING
, (void *)rip
);
3385 kvm_lapic_sync_from_vapic(vcpu
);
3387 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
3393 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3397 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
)) {
3398 pr_debug("vcpu %d received sipi with vector # %x\n",
3399 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
3400 kvm_lapic_reset(vcpu
);
3401 r
= kvm_arch_vcpu_reset(vcpu
);
3404 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
3407 down_read(&vcpu
->kvm
->slots_lock
);
3412 if (vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
)
3413 r
= vcpu_enter_guest(vcpu
, kvm_run
);
3415 up_read(&vcpu
->kvm
->slots_lock
);
3416 kvm_vcpu_block(vcpu
);
3417 down_read(&vcpu
->kvm
->slots_lock
);
3418 if (test_and_clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
))
3420 switch(vcpu
->arch
.mp_state
) {
3421 case KVM_MP_STATE_HALTED
:
3422 vcpu
->arch
.mp_state
=
3423 KVM_MP_STATE_RUNNABLE
;
3424 case KVM_MP_STATE_RUNNABLE
:
3426 case KVM_MP_STATE_SIPI_RECEIVED
:
3437 clear_bit(KVM_REQ_PENDING_TIMER
, &vcpu
->requests
);
3438 if (kvm_cpu_has_pending_timer(vcpu
))
3439 kvm_inject_pending_timer_irqs(vcpu
);
3441 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
3443 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
3444 ++vcpu
->stat
.request_irq_exits
;
3446 if (signal_pending(current
)) {
3448 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
3449 ++vcpu
->stat
.signal_exits
;
3451 if (need_resched()) {
3452 up_read(&vcpu
->kvm
->slots_lock
);
3454 down_read(&vcpu
->kvm
->slots_lock
);
3458 up_read(&vcpu
->kvm
->slots_lock
);
3459 post_kvm_run_save(vcpu
, kvm_run
);
3466 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3473 if (vcpu
->sigset_active
)
3474 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
3476 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
)) {
3477 kvm_vcpu_block(vcpu
);
3478 clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
3483 /* re-sync apic's tpr */
3484 if (!irqchip_in_kernel(vcpu
->kvm
))
3485 kvm_set_cr8(vcpu
, kvm_run
->cr8
);
3487 if (vcpu
->arch
.pio
.cur_count
) {
3488 r
= complete_pio(vcpu
);
3492 #if CONFIG_HAS_IOMEM
3493 if (vcpu
->mmio_needed
) {
3494 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
3495 vcpu
->mmio_read_completed
= 1;
3496 vcpu
->mmio_needed
= 0;
3498 down_read(&vcpu
->kvm
->slots_lock
);
3499 r
= emulate_instruction(vcpu
, kvm_run
,
3500 vcpu
->arch
.mmio_fault_cr2
, 0,
3501 EMULTYPE_NO_DECODE
);
3502 up_read(&vcpu
->kvm
->slots_lock
);
3503 if (r
== EMULATE_DO_MMIO
) {
3505 * Read-modify-write. Back to userspace.
3512 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
)
3513 kvm_register_write(vcpu
, VCPU_REGS_RAX
,
3514 kvm_run
->hypercall
.ret
);
3516 r
= __vcpu_run(vcpu
, kvm_run
);
3519 if (vcpu
->sigset_active
)
3520 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
3526 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
3530 regs
->rax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3531 regs
->rbx
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
3532 regs
->rcx
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
3533 regs
->rdx
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
3534 regs
->rsi
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
3535 regs
->rdi
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
3536 regs
->rsp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
3537 regs
->rbp
= kvm_register_read(vcpu
, VCPU_REGS_RBP
);
3538 #ifdef CONFIG_X86_64
3539 regs
->r8
= kvm_register_read(vcpu
, VCPU_REGS_R8
);
3540 regs
->r9
= kvm_register_read(vcpu
, VCPU_REGS_R9
);
3541 regs
->r10
= kvm_register_read(vcpu
, VCPU_REGS_R10
);
3542 regs
->r11
= kvm_register_read(vcpu
, VCPU_REGS_R11
);
3543 regs
->r12
= kvm_register_read(vcpu
, VCPU_REGS_R12
);
3544 regs
->r13
= kvm_register_read(vcpu
, VCPU_REGS_R13
);
3545 regs
->r14
= kvm_register_read(vcpu
, VCPU_REGS_R14
);
3546 regs
->r15
= kvm_register_read(vcpu
, VCPU_REGS_R15
);
3549 regs
->rip
= kvm_rip_read(vcpu
);
3550 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
3553 * Don't leak debug flags in case they were set for guest debugging
3555 if (vcpu
->guest_debug
& KVM_GUESTDBG_SINGLESTEP
)
3556 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
3563 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
3567 kvm_register_write(vcpu
, VCPU_REGS_RAX
, regs
->rax
);
3568 kvm_register_write(vcpu
, VCPU_REGS_RBX
, regs
->rbx
);
3569 kvm_register_write(vcpu
, VCPU_REGS_RCX
, regs
->rcx
);
3570 kvm_register_write(vcpu
, VCPU_REGS_RDX
, regs
->rdx
);
3571 kvm_register_write(vcpu
, VCPU_REGS_RSI
, regs
->rsi
);
3572 kvm_register_write(vcpu
, VCPU_REGS_RDI
, regs
->rdi
);
3573 kvm_register_write(vcpu
, VCPU_REGS_RSP
, regs
->rsp
);
3574 kvm_register_write(vcpu
, VCPU_REGS_RBP
, regs
->rbp
);
3575 #ifdef CONFIG_X86_64
3576 kvm_register_write(vcpu
, VCPU_REGS_R8
, regs
->r8
);
3577 kvm_register_write(vcpu
, VCPU_REGS_R9
, regs
->r9
);
3578 kvm_register_write(vcpu
, VCPU_REGS_R10
, regs
->r10
);
3579 kvm_register_write(vcpu
, VCPU_REGS_R11
, regs
->r11
);
3580 kvm_register_write(vcpu
, VCPU_REGS_R12
, regs
->r12
);
3581 kvm_register_write(vcpu
, VCPU_REGS_R13
, regs
->r13
);
3582 kvm_register_write(vcpu
, VCPU_REGS_R14
, regs
->r14
);
3583 kvm_register_write(vcpu
, VCPU_REGS_R15
, regs
->r15
);
3587 kvm_rip_write(vcpu
, regs
->rip
);
3588 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
3591 vcpu
->arch
.exception
.pending
= false;
3598 void kvm_get_segment(struct kvm_vcpu
*vcpu
,
3599 struct kvm_segment
*var
, int seg
)
3601 kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
3604 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
3606 struct kvm_segment cs
;
3608 kvm_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
3612 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
3614 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
3615 struct kvm_sregs
*sregs
)
3617 struct descriptor_table dt
;
3621 kvm_get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3622 kvm_get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3623 kvm_get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3624 kvm_get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3625 kvm_get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3626 kvm_get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3628 kvm_get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3629 kvm_get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3631 kvm_x86_ops
->get_idt(vcpu
, &dt
);
3632 sregs
->idt
.limit
= dt
.limit
;
3633 sregs
->idt
.base
= dt
.base
;
3634 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
3635 sregs
->gdt
.limit
= dt
.limit
;
3636 sregs
->gdt
.base
= dt
.base
;
3638 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3639 sregs
->cr0
= vcpu
->arch
.cr0
;
3640 sregs
->cr2
= vcpu
->arch
.cr2
;
3641 sregs
->cr3
= vcpu
->arch
.cr3
;
3642 sregs
->cr4
= vcpu
->arch
.cr4
;
3643 sregs
->cr8
= kvm_get_cr8(vcpu
);
3644 sregs
->efer
= vcpu
->arch
.shadow_efer
;
3645 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
3647 memset(sregs
->interrupt_bitmap
, 0, sizeof sregs
->interrupt_bitmap
);
3649 if (vcpu
->arch
.interrupt
.pending
&& !vcpu
->arch
.interrupt
.soft
)
3650 set_bit(vcpu
->arch
.interrupt
.nr
,
3651 (unsigned long *)sregs
->interrupt_bitmap
);
3658 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
3659 struct kvm_mp_state
*mp_state
)
3662 mp_state
->mp_state
= vcpu
->arch
.mp_state
;
3667 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
3668 struct kvm_mp_state
*mp_state
)
3671 vcpu
->arch
.mp_state
= mp_state
->mp_state
;
3676 static void kvm_set_segment(struct kvm_vcpu
*vcpu
,
3677 struct kvm_segment
*var
, int seg
)
3679 kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
3682 static void seg_desct_to_kvm_desct(struct desc_struct
*seg_desc
, u16 selector
,
3683 struct kvm_segment
*kvm_desct
)
3685 kvm_desct
->base
= seg_desc
->base0
;
3686 kvm_desct
->base
|= seg_desc
->base1
<< 16;
3687 kvm_desct
->base
|= seg_desc
->base2
<< 24;
3688 kvm_desct
->limit
= seg_desc
->limit0
;
3689 kvm_desct
->limit
|= seg_desc
->limit
<< 16;
3691 kvm_desct
->limit
<<= 12;
3692 kvm_desct
->limit
|= 0xfff;
3694 kvm_desct
->selector
= selector
;
3695 kvm_desct
->type
= seg_desc
->type
;
3696 kvm_desct
->present
= seg_desc
->p
;
3697 kvm_desct
->dpl
= seg_desc
->dpl
;
3698 kvm_desct
->db
= seg_desc
->d
;
3699 kvm_desct
->s
= seg_desc
->s
;
3700 kvm_desct
->l
= seg_desc
->l
;
3701 kvm_desct
->g
= seg_desc
->g
;
3702 kvm_desct
->avl
= seg_desc
->avl
;
3704 kvm_desct
->unusable
= 1;
3706 kvm_desct
->unusable
= 0;
3707 kvm_desct
->padding
= 0;
3710 static void get_segment_descriptor_dtable(struct kvm_vcpu
*vcpu
,
3712 struct descriptor_table
*dtable
)
3714 if (selector
& 1 << 2) {
3715 struct kvm_segment kvm_seg
;
3717 kvm_get_segment(vcpu
, &kvm_seg
, VCPU_SREG_LDTR
);
3719 if (kvm_seg
.unusable
)
3722 dtable
->limit
= kvm_seg
.limit
;
3723 dtable
->base
= kvm_seg
.base
;
3726 kvm_x86_ops
->get_gdt(vcpu
, dtable
);
3729 /* allowed just for 8 bytes segments */
3730 static int load_guest_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3731 struct desc_struct
*seg_desc
)
3734 struct descriptor_table dtable
;
3735 u16 index
= selector
>> 3;
3737 get_segment_descriptor_dtable(vcpu
, selector
, &dtable
);
3739 if (dtable
.limit
< index
* 8 + 7) {
3740 kvm_queue_exception_e(vcpu
, GP_VECTOR
, selector
& 0xfffc);
3743 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, dtable
.base
);
3745 return kvm_read_guest(vcpu
->kvm
, gpa
, seg_desc
, 8);
3748 /* allowed just for 8 bytes segments */
3749 static int save_guest_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3750 struct desc_struct
*seg_desc
)
3753 struct descriptor_table dtable
;
3754 u16 index
= selector
>> 3;
3756 get_segment_descriptor_dtable(vcpu
, selector
, &dtable
);
3758 if (dtable
.limit
< index
* 8 + 7)
3760 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, dtable
.base
);
3762 return kvm_write_guest(vcpu
->kvm
, gpa
, seg_desc
, 8);
3765 static gpa_t
get_tss_base_addr(struct kvm_vcpu
*vcpu
,
3766 struct desc_struct
*seg_desc
)
3770 base_addr
= seg_desc
->base0
;
3771 base_addr
|= (seg_desc
->base1
<< 16);
3772 base_addr
|= (seg_desc
->base2
<< 24);
3774 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, base_addr
);
3777 static u16
get_segment_selector(struct kvm_vcpu
*vcpu
, int seg
)
3779 struct kvm_segment kvm_seg
;
3781 kvm_get_segment(vcpu
, &kvm_seg
, seg
);
3782 return kvm_seg
.selector
;
3785 static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu
*vcpu
,
3787 struct kvm_segment
*kvm_seg
)
3789 struct desc_struct seg_desc
;
3791 if (load_guest_segment_descriptor(vcpu
, selector
, &seg_desc
))
3793 seg_desct_to_kvm_desct(&seg_desc
, selector
, kvm_seg
);
3797 static int kvm_load_realmode_segment(struct kvm_vcpu
*vcpu
, u16 selector
, int seg
)
3799 struct kvm_segment segvar
= {
3800 .base
= selector
<< 4,
3802 .selector
= selector
,
3813 kvm_x86_ops
->set_segment(vcpu
, &segvar
, seg
);
3817 int kvm_load_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3818 int type_bits
, int seg
)
3820 struct kvm_segment kvm_seg
;
3822 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
))
3823 return kvm_load_realmode_segment(vcpu
, selector
, seg
);
3824 if (load_segment_descriptor_to_kvm_desct(vcpu
, selector
, &kvm_seg
))
3826 kvm_seg
.type
|= type_bits
;
3828 if (seg
!= VCPU_SREG_SS
&& seg
!= VCPU_SREG_CS
&&
3829 seg
!= VCPU_SREG_LDTR
)
3831 kvm_seg
.unusable
= 1;
3833 kvm_set_segment(vcpu
, &kvm_seg
, seg
);
3837 static void save_state_to_tss32(struct kvm_vcpu
*vcpu
,
3838 struct tss_segment_32
*tss
)
3840 tss
->cr3
= vcpu
->arch
.cr3
;
3841 tss
->eip
= kvm_rip_read(vcpu
);
3842 tss
->eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3843 tss
->eax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3844 tss
->ecx
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
3845 tss
->edx
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
3846 tss
->ebx
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
3847 tss
->esp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
3848 tss
->ebp
= kvm_register_read(vcpu
, VCPU_REGS_RBP
);
3849 tss
->esi
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
3850 tss
->edi
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
3851 tss
->es
= get_segment_selector(vcpu
, VCPU_SREG_ES
);
3852 tss
->cs
= get_segment_selector(vcpu
, VCPU_SREG_CS
);
3853 tss
->ss
= get_segment_selector(vcpu
, VCPU_SREG_SS
);
3854 tss
->ds
= get_segment_selector(vcpu
, VCPU_SREG_DS
);
3855 tss
->fs
= get_segment_selector(vcpu
, VCPU_SREG_FS
);
3856 tss
->gs
= get_segment_selector(vcpu
, VCPU_SREG_GS
);
3857 tss
->ldt_selector
= get_segment_selector(vcpu
, VCPU_SREG_LDTR
);
3860 static int load_state_from_tss32(struct kvm_vcpu
*vcpu
,
3861 struct tss_segment_32
*tss
)
3863 kvm_set_cr3(vcpu
, tss
->cr3
);
3865 kvm_rip_write(vcpu
, tss
->eip
);
3866 kvm_x86_ops
->set_rflags(vcpu
, tss
->eflags
| 2);
3868 kvm_register_write(vcpu
, VCPU_REGS_RAX
, tss
->eax
);
3869 kvm_register_write(vcpu
, VCPU_REGS_RCX
, tss
->ecx
);
3870 kvm_register_write(vcpu
, VCPU_REGS_RDX
, tss
->edx
);
3871 kvm_register_write(vcpu
, VCPU_REGS_RBX
, tss
->ebx
);
3872 kvm_register_write(vcpu
, VCPU_REGS_RSP
, tss
->esp
);
3873 kvm_register_write(vcpu
, VCPU_REGS_RBP
, tss
->ebp
);
3874 kvm_register_write(vcpu
, VCPU_REGS_RSI
, tss
->esi
);
3875 kvm_register_write(vcpu
, VCPU_REGS_RDI
, tss
->edi
);
3877 if (kvm_load_segment_descriptor(vcpu
, tss
->ldt_selector
, 0, VCPU_SREG_LDTR
))
3880 if (kvm_load_segment_descriptor(vcpu
, tss
->es
, 1, VCPU_SREG_ES
))
3883 if (kvm_load_segment_descriptor(vcpu
, tss
->cs
, 9, VCPU_SREG_CS
))
3886 if (kvm_load_segment_descriptor(vcpu
, tss
->ss
, 1, VCPU_SREG_SS
))
3889 if (kvm_load_segment_descriptor(vcpu
, tss
->ds
, 1, VCPU_SREG_DS
))
3892 if (kvm_load_segment_descriptor(vcpu
, tss
->fs
, 1, VCPU_SREG_FS
))
3895 if (kvm_load_segment_descriptor(vcpu
, tss
->gs
, 1, VCPU_SREG_GS
))
3900 static void save_state_to_tss16(struct kvm_vcpu
*vcpu
,
3901 struct tss_segment_16
*tss
)
3903 tss
->ip
= kvm_rip_read(vcpu
);
3904 tss
->flag
= kvm_x86_ops
->get_rflags(vcpu
);
3905 tss
->ax
= kvm_register_read(vcpu
, VCPU_REGS_RAX
);
3906 tss
->cx
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
3907 tss
->dx
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
3908 tss
->bx
= kvm_register_read(vcpu
, VCPU_REGS_RBX
);
3909 tss
->sp
= kvm_register_read(vcpu
, VCPU_REGS_RSP
);
3910 tss
->bp
= kvm_register_read(vcpu
, VCPU_REGS_RBP
);
3911 tss
->si
= kvm_register_read(vcpu
, VCPU_REGS_RSI
);
3912 tss
->di
= kvm_register_read(vcpu
, VCPU_REGS_RDI
);
3914 tss
->es
= get_segment_selector(vcpu
, VCPU_SREG_ES
);
3915 tss
->cs
= get_segment_selector(vcpu
, VCPU_SREG_CS
);
3916 tss
->ss
= get_segment_selector(vcpu
, VCPU_SREG_SS
);
3917 tss
->ds
= get_segment_selector(vcpu
, VCPU_SREG_DS
);
3918 tss
->ldt
= get_segment_selector(vcpu
, VCPU_SREG_LDTR
);
3919 tss
->prev_task_link
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
3922 static int load_state_from_tss16(struct kvm_vcpu
*vcpu
,
3923 struct tss_segment_16
*tss
)
3925 kvm_rip_write(vcpu
, tss
->ip
);
3926 kvm_x86_ops
->set_rflags(vcpu
, tss
->flag
| 2);
3927 kvm_register_write(vcpu
, VCPU_REGS_RAX
, tss
->ax
);
3928 kvm_register_write(vcpu
, VCPU_REGS_RCX
, tss
->cx
);
3929 kvm_register_write(vcpu
, VCPU_REGS_RDX
, tss
->dx
);
3930 kvm_register_write(vcpu
, VCPU_REGS_RBX
, tss
->bx
);
3931 kvm_register_write(vcpu
, VCPU_REGS_RSP
, tss
->sp
);
3932 kvm_register_write(vcpu
, VCPU_REGS_RBP
, tss
->bp
);
3933 kvm_register_write(vcpu
, VCPU_REGS_RSI
, tss
->si
);
3934 kvm_register_write(vcpu
, VCPU_REGS_RDI
, tss
->di
);
3936 if (kvm_load_segment_descriptor(vcpu
, tss
->ldt
, 0, VCPU_SREG_LDTR
))
3939 if (kvm_load_segment_descriptor(vcpu
, tss
->es
, 1, VCPU_SREG_ES
))
3942 if (kvm_load_segment_descriptor(vcpu
, tss
->cs
, 9, VCPU_SREG_CS
))
3945 if (kvm_load_segment_descriptor(vcpu
, tss
->ss
, 1, VCPU_SREG_SS
))
3948 if (kvm_load_segment_descriptor(vcpu
, tss
->ds
, 1, VCPU_SREG_DS
))
3953 static int kvm_task_switch_16(struct kvm_vcpu
*vcpu
, u16 tss_selector
,
3954 u16 old_tss_sel
, u32 old_tss_base
,
3955 struct desc_struct
*nseg_desc
)
3957 struct tss_segment_16 tss_segment_16
;
3960 if (kvm_read_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_16
,
3961 sizeof tss_segment_16
))
3964 save_state_to_tss16(vcpu
, &tss_segment_16
);
3966 if (kvm_write_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_16
,
3967 sizeof tss_segment_16
))
3970 if (kvm_read_guest(vcpu
->kvm
, get_tss_base_addr(vcpu
, nseg_desc
),
3971 &tss_segment_16
, sizeof tss_segment_16
))
3974 if (old_tss_sel
!= 0xffff) {
3975 tss_segment_16
.prev_task_link
= old_tss_sel
;
3977 if (kvm_write_guest(vcpu
->kvm
,
3978 get_tss_base_addr(vcpu
, nseg_desc
),
3979 &tss_segment_16
.prev_task_link
,
3980 sizeof tss_segment_16
.prev_task_link
))
3984 if (load_state_from_tss16(vcpu
, &tss_segment_16
))
3992 static int kvm_task_switch_32(struct kvm_vcpu
*vcpu
, u16 tss_selector
,
3993 u16 old_tss_sel
, u32 old_tss_base
,
3994 struct desc_struct
*nseg_desc
)
3996 struct tss_segment_32 tss_segment_32
;
3999 if (kvm_read_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_32
,
4000 sizeof tss_segment_32
))
4003 save_state_to_tss32(vcpu
, &tss_segment_32
);
4005 if (kvm_write_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_32
,
4006 sizeof tss_segment_32
))
4009 if (kvm_read_guest(vcpu
->kvm
, get_tss_base_addr(vcpu
, nseg_desc
),
4010 &tss_segment_32
, sizeof tss_segment_32
))
4013 if (old_tss_sel
!= 0xffff) {
4014 tss_segment_32
.prev_task_link
= old_tss_sel
;
4016 if (kvm_write_guest(vcpu
->kvm
,
4017 get_tss_base_addr(vcpu
, nseg_desc
),
4018 &tss_segment_32
.prev_task_link
,
4019 sizeof tss_segment_32
.prev_task_link
))
4023 if (load_state_from_tss32(vcpu
, &tss_segment_32
))
4031 int kvm_task_switch(struct kvm_vcpu
*vcpu
, u16 tss_selector
, int reason
)
4033 struct kvm_segment tr_seg
;
4034 struct desc_struct cseg_desc
;
4035 struct desc_struct nseg_desc
;
4037 u32 old_tss_base
= get_segment_base(vcpu
, VCPU_SREG_TR
);
4038 u16 old_tss_sel
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
4040 old_tss_base
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, old_tss_base
);
4042 /* FIXME: Handle errors. Failure to read either TSS or their
4043 * descriptors should generate a pagefault.
4045 if (load_guest_segment_descriptor(vcpu
, tss_selector
, &nseg_desc
))
4048 if (load_guest_segment_descriptor(vcpu
, old_tss_sel
, &cseg_desc
))
4051 if (reason
!= TASK_SWITCH_IRET
) {
4054 cpl
= kvm_x86_ops
->get_cpl(vcpu
);
4055 if ((tss_selector
& 3) > nseg_desc
.dpl
|| cpl
> nseg_desc
.dpl
) {
4056 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
4061 if (!nseg_desc
.p
|| (nseg_desc
.limit0
| nseg_desc
.limit
<< 16) < 0x67) {
4062 kvm_queue_exception_e(vcpu
, TS_VECTOR
, tss_selector
& 0xfffc);
4066 if (reason
== TASK_SWITCH_IRET
|| reason
== TASK_SWITCH_JMP
) {
4067 cseg_desc
.type
&= ~(1 << 1); //clear the B flag
4068 save_guest_segment_descriptor(vcpu
, old_tss_sel
, &cseg_desc
);
4071 if (reason
== TASK_SWITCH_IRET
) {
4072 u32 eflags
= kvm_x86_ops
->get_rflags(vcpu
);
4073 kvm_x86_ops
->set_rflags(vcpu
, eflags
& ~X86_EFLAGS_NT
);
4076 /* set back link to prev task only if NT bit is set in eflags
4077 note that old_tss_sel is not used afetr this point */
4078 if (reason
!= TASK_SWITCH_CALL
&& reason
!= TASK_SWITCH_GATE
)
4079 old_tss_sel
= 0xffff;
4081 /* set back link to prev task only if NT bit is set in eflags
4082 note that old_tss_sel is not used afetr this point */
4083 if (reason
!= TASK_SWITCH_CALL
&& reason
!= TASK_SWITCH_GATE
)
4084 old_tss_sel
= 0xffff;
4086 if (nseg_desc
.type
& 8)
4087 ret
= kvm_task_switch_32(vcpu
, tss_selector
, old_tss_sel
,
4088 old_tss_base
, &nseg_desc
);
4090 ret
= kvm_task_switch_16(vcpu
, tss_selector
, old_tss_sel
,
4091 old_tss_base
, &nseg_desc
);
4093 if (reason
== TASK_SWITCH_CALL
|| reason
== TASK_SWITCH_GATE
) {
4094 u32 eflags
= kvm_x86_ops
->get_rflags(vcpu
);
4095 kvm_x86_ops
->set_rflags(vcpu
, eflags
| X86_EFLAGS_NT
);
4098 if (reason
!= TASK_SWITCH_IRET
) {
4099 nseg_desc
.type
|= (1 << 1);
4100 save_guest_segment_descriptor(vcpu
, tss_selector
,
4104 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
| X86_CR0_TS
);
4105 seg_desct_to_kvm_desct(&nseg_desc
, tss_selector
, &tr_seg
);
4107 kvm_set_segment(vcpu
, &tr_seg
, VCPU_SREG_TR
);
4111 EXPORT_SYMBOL_GPL(kvm_task_switch
);
4113 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
4114 struct kvm_sregs
*sregs
)
4116 int mmu_reset_needed
= 0;
4117 int pending_vec
, max_bits
;
4118 struct descriptor_table dt
;
4122 dt
.limit
= sregs
->idt
.limit
;
4123 dt
.base
= sregs
->idt
.base
;
4124 kvm_x86_ops
->set_idt(vcpu
, &dt
);
4125 dt
.limit
= sregs
->gdt
.limit
;
4126 dt
.base
= sregs
->gdt
.base
;
4127 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
4129 vcpu
->arch
.cr2
= sregs
->cr2
;
4130 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
4131 vcpu
->arch
.cr3
= sregs
->cr3
;
4133 kvm_set_cr8(vcpu
, sregs
->cr8
);
4135 mmu_reset_needed
|= vcpu
->arch
.shadow_efer
!= sregs
->efer
;
4136 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
4137 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
4139 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
4141 mmu_reset_needed
|= vcpu
->arch
.cr0
!= sregs
->cr0
;
4142 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
4143 vcpu
->arch
.cr0
= sregs
->cr0
;
4145 mmu_reset_needed
|= vcpu
->arch
.cr4
!= sregs
->cr4
;
4146 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
4147 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
4148 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
4150 if (mmu_reset_needed
)
4151 kvm_mmu_reset_context(vcpu
);
4153 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
4154 pending_vec
= find_first_bit(
4155 (const unsigned long *)sregs
->interrupt_bitmap
, max_bits
);
4156 if (pending_vec
< max_bits
) {
4157 kvm_queue_interrupt(vcpu
, pending_vec
, false);
4158 pr_debug("Set back pending irq %d\n", pending_vec
);
4159 if (irqchip_in_kernel(vcpu
->kvm
))
4160 kvm_pic_clear_isr_ack(vcpu
->kvm
);
4163 kvm_set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
4164 kvm_set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
4165 kvm_set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
4166 kvm_set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
4167 kvm_set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
4168 kvm_set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
4170 kvm_set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
4171 kvm_set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
4173 /* Older userspace won't unhalt the vcpu on reset. */
4174 if (vcpu
->vcpu_id
== 0 && kvm_rip_read(vcpu
) == 0xfff0 &&
4175 sregs
->cs
.selector
== 0xf000 && sregs
->cs
.base
== 0xffff0000 &&
4176 !(vcpu
->arch
.cr0
& X86_CR0_PE
))
4177 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
4184 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
4185 struct kvm_guest_debug
*dbg
)
4191 if ((dbg
->control
& (KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
)) ==
4192 (KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
)) {
4193 for (i
= 0; i
< KVM_NR_DB_REGS
; ++i
)
4194 vcpu
->arch
.eff_db
[i
] = dbg
->arch
.debugreg
[i
];
4195 vcpu
->arch
.switch_db_regs
=
4196 (dbg
->arch
.debugreg
[7] & DR7_BP_EN_MASK
);
4198 for (i
= 0; i
< KVM_NR_DB_REGS
; i
++)
4199 vcpu
->arch
.eff_db
[i
] = vcpu
->arch
.db
[i
];
4200 vcpu
->arch
.switch_db_regs
= (vcpu
->arch
.dr7
& DR7_BP_EN_MASK
);
4203 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
4205 if (dbg
->control
& KVM_GUESTDBG_INJECT_DB
)
4206 kvm_queue_exception(vcpu
, DB_VECTOR
);
4207 else if (dbg
->control
& KVM_GUESTDBG_INJECT_BP
)
4208 kvm_queue_exception(vcpu
, BP_VECTOR
);
4216 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
4217 * we have asm/x86/processor.h
4228 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
4229 #ifdef CONFIG_X86_64
4230 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
4232 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
4237 * Translate a guest virtual address to a guest physical address.
4239 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
4240 struct kvm_translation
*tr
)
4242 unsigned long vaddr
= tr
->linear_address
;
4246 down_read(&vcpu
->kvm
->slots_lock
);
4247 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, vaddr
);
4248 up_read(&vcpu
->kvm
->slots_lock
);
4249 tr
->physical_address
= gpa
;
4250 tr
->valid
= gpa
!= UNMAPPED_GVA
;
4258 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
4260 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
4264 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
4265 fpu
->fcw
= fxsave
->cwd
;
4266 fpu
->fsw
= fxsave
->swd
;
4267 fpu
->ftwx
= fxsave
->twd
;
4268 fpu
->last_opcode
= fxsave
->fop
;
4269 fpu
->last_ip
= fxsave
->rip
;
4270 fpu
->last_dp
= fxsave
->rdp
;
4271 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
4278 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
4280 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
4284 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
4285 fxsave
->cwd
= fpu
->fcw
;
4286 fxsave
->swd
= fpu
->fsw
;
4287 fxsave
->twd
= fpu
->ftwx
;
4288 fxsave
->fop
= fpu
->last_opcode
;
4289 fxsave
->rip
= fpu
->last_ip
;
4290 fxsave
->rdp
= fpu
->last_dp
;
4291 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
4298 void fx_init(struct kvm_vcpu
*vcpu
)
4300 unsigned after_mxcsr_mask
;
4303 * Touch the fpu the first time in non atomic context as if
4304 * this is the first fpu instruction the exception handler
4305 * will fire before the instruction returns and it'll have to
4306 * allocate ram with GFP_KERNEL.
4309 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
4311 /* Initialize guest FPU by resetting ours and saving into guest's */
4313 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
4315 kvm_fx_save(&vcpu
->arch
.guest_fx_image
);
4316 kvm_fx_restore(&vcpu
->arch
.host_fx_image
);
4319 vcpu
->arch
.cr0
|= X86_CR0_ET
;
4320 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
4321 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
4322 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
4323 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
4325 EXPORT_SYMBOL_GPL(fx_init
);
4327 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
4329 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
4332 vcpu
->guest_fpu_loaded
= 1;
4333 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
4334 kvm_fx_restore(&vcpu
->arch
.guest_fx_image
);
4336 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
4338 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
4340 if (!vcpu
->guest_fpu_loaded
)
4343 vcpu
->guest_fpu_loaded
= 0;
4344 kvm_fx_save(&vcpu
->arch
.guest_fx_image
);
4345 kvm_fx_restore(&vcpu
->arch
.host_fx_image
);
4346 ++vcpu
->stat
.fpu_reload
;
4348 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
4350 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
4352 if (vcpu
->arch
.time_page
) {
4353 kvm_release_page_dirty(vcpu
->arch
.time_page
);
4354 vcpu
->arch
.time_page
= NULL
;
4357 kvm_x86_ops
->vcpu_free(vcpu
);
4360 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
4363 return kvm_x86_ops
->vcpu_create(kvm
, id
);
4366 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
4370 /* We do fxsave: this must be aligned. */
4371 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
4373 vcpu
->arch
.mtrr_state
.have_fixed
= 1;
4375 r
= kvm_arch_vcpu_reset(vcpu
);
4377 r
= kvm_mmu_setup(vcpu
);
4384 kvm_x86_ops
->vcpu_free(vcpu
);
4388 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
4391 kvm_mmu_unload(vcpu
);
4394 kvm_x86_ops
->vcpu_free(vcpu
);
4397 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
4399 vcpu
->arch
.nmi_pending
= false;
4400 vcpu
->arch
.nmi_injected
= false;
4402 vcpu
->arch
.switch_db_regs
= 0;
4403 memset(vcpu
->arch
.db
, 0, sizeof(vcpu
->arch
.db
));
4404 vcpu
->arch
.dr6
= DR6_FIXED_1
;
4405 vcpu
->arch
.dr7
= DR7_FIXED_1
;
4407 return kvm_x86_ops
->vcpu_reset(vcpu
);
4410 void kvm_arch_hardware_enable(void *garbage
)
4412 kvm_x86_ops
->hardware_enable(garbage
);
4415 void kvm_arch_hardware_disable(void *garbage
)
4417 kvm_x86_ops
->hardware_disable(garbage
);
4420 int kvm_arch_hardware_setup(void)
4422 return kvm_x86_ops
->hardware_setup();
4425 void kvm_arch_hardware_unsetup(void)
4427 kvm_x86_ops
->hardware_unsetup();
4430 void kvm_arch_check_processor_compat(void *rtn
)
4432 kvm_x86_ops
->check_processor_compatibility(rtn
);
4435 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
4441 BUG_ON(vcpu
->kvm
== NULL
);
4444 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
4445 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
4446 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
4448 vcpu
->arch
.mp_state
= KVM_MP_STATE_UNINITIALIZED
;
4450 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
4455 vcpu
->arch
.pio_data
= page_address(page
);
4457 r
= kvm_mmu_create(vcpu
);
4459 goto fail_free_pio_data
;
4461 if (irqchip_in_kernel(kvm
)) {
4462 r
= kvm_create_lapic(vcpu
);
4464 goto fail_mmu_destroy
;
4470 kvm_mmu_destroy(vcpu
);
4472 free_page((unsigned long)vcpu
->arch
.pio_data
);
4477 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
4479 kvm_free_lapic(vcpu
);
4480 down_read(&vcpu
->kvm
->slots_lock
);
4481 kvm_mmu_destroy(vcpu
);
4482 up_read(&vcpu
->kvm
->slots_lock
);
4483 free_page((unsigned long)vcpu
->arch
.pio_data
);
4486 struct kvm
*kvm_arch_create_vm(void)
4488 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
4491 return ERR_PTR(-ENOMEM
);
4493 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
4494 INIT_LIST_HEAD(&kvm
->arch
.assigned_dev_head
);
4496 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
4497 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID
, &kvm
->arch
.irq_sources_bitmap
);
4499 rdtscll(kvm
->arch
.vm_init_tsc
);
4504 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
4507 kvm_mmu_unload(vcpu
);
4511 static void kvm_free_vcpus(struct kvm
*kvm
)
4516 * Unpin any mmu pages first.
4518 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
4520 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
4521 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
4522 if (kvm
->vcpus
[i
]) {
4523 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
4524 kvm
->vcpus
[i
] = NULL
;
4530 void kvm_arch_sync_events(struct kvm
*kvm
)
4532 kvm_free_all_assigned_devices(kvm
);
4535 void kvm_arch_destroy_vm(struct kvm
*kvm
)
4537 kvm_iommu_unmap_guest(kvm
);
4539 kfree(kvm
->arch
.vpic
);
4540 kfree(kvm
->arch
.vioapic
);
4541 kvm_free_vcpus(kvm
);
4542 kvm_free_physmem(kvm
);
4543 if (kvm
->arch
.apic_access_page
)
4544 put_page(kvm
->arch
.apic_access_page
);
4545 if (kvm
->arch
.ept_identity_pagetable
)
4546 put_page(kvm
->arch
.ept_identity_pagetable
);
4550 int kvm_arch_set_memory_region(struct kvm
*kvm
,
4551 struct kvm_userspace_memory_region
*mem
,
4552 struct kvm_memory_slot old
,
4555 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
4556 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
4558 /*To keep backward compatibility with older userspace,
4559 *x86 needs to hanlde !user_alloc case.
4562 if (npages
&& !old
.rmap
) {
4563 unsigned long userspace_addr
;
4565 down_write(¤t
->mm
->mmap_sem
);
4566 userspace_addr
= do_mmap(NULL
, 0,
4568 PROT_READ
| PROT_WRITE
,
4569 MAP_PRIVATE
| MAP_ANONYMOUS
,
4571 up_write(¤t
->mm
->mmap_sem
);
4573 if (IS_ERR((void *)userspace_addr
))
4574 return PTR_ERR((void *)userspace_addr
);
4576 /* set userspace_addr atomically for kvm_hva_to_rmapp */
4577 spin_lock(&kvm
->mmu_lock
);
4578 memslot
->userspace_addr
= userspace_addr
;
4579 spin_unlock(&kvm
->mmu_lock
);
4581 if (!old
.user_alloc
&& old
.rmap
) {
4584 down_write(¤t
->mm
->mmap_sem
);
4585 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
4586 old
.npages
* PAGE_SIZE
);
4587 up_write(¤t
->mm
->mmap_sem
);
4590 "kvm_vm_ioctl_set_memory_region: "
4591 "failed to munmap memory\n");
4596 spin_lock(&kvm
->mmu_lock
);
4597 if (!kvm
->arch
.n_requested_mmu_pages
) {
4598 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
4599 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
4602 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
4603 spin_unlock(&kvm
->mmu_lock
);
4604 kvm_flush_remote_tlbs(kvm
);
4609 void kvm_arch_flush_shadow(struct kvm
*kvm
)
4611 kvm_mmu_zap_all(kvm
);
4612 kvm_reload_remote_mmus(kvm
);
4615 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
4617 return vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
4618 || vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
4619 || vcpu
->arch
.nmi_pending
;
4622 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
4625 int cpu
= vcpu
->cpu
;
4627 if (waitqueue_active(&vcpu
->wq
)) {
4628 wake_up_interruptible(&vcpu
->wq
);
4629 ++vcpu
->stat
.halt_wakeup
;
4633 if (cpu
!= me
&& (unsigned)cpu
< nr_cpu_ids
&& cpu_online(cpu
))
4634 if (!test_and_set_bit(KVM_REQ_KICK
, &vcpu
->requests
))
4635 smp_send_reschedule(cpu
);
4639 int kvm_arch_interrupt_allowed(struct kvm_vcpu
*vcpu
)
4641 return kvm_x86_ops
->interrupt_allowed(vcpu
);