2 * Kernel-based Virtual Machine driver for Linux
4 * derived from drivers/kvm/kvm_main.c
6 * Copyright (C) 2006 Qumranet, Inc.
9 * Avi Kivity <avi@qumranet.com>
10 * Yaniv Kamay <yaniv@qumranet.com>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
17 #include <linux/kvm_host.h>
23 #include <linux/clocksource.h>
24 #include <linux/kvm.h>
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28 #include <linux/mman.h>
29 #include <linux/highmem.h>
31 #include <asm/uaccess.h>
35 #define MAX_IO_MSRS 256
36 #define CR0_RESERVED_BITS \
37 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
38 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
39 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
40 #define CR4_RESERVED_BITS \
41 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
42 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
43 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
44 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
46 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
48 * - enable syscall per default because its emulated by KVM
49 * - enable LME and LMA per default on 64 bit KVM
52 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffafeULL
;
54 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffffeULL
;
57 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
58 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
60 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
61 struct kvm_cpuid_entry2 __user
*entries
);
63 struct kvm_x86_ops
*kvm_x86_ops
;
65 struct kvm_stats_debugfs_item debugfs_entries
[] = {
66 { "pf_fixed", VCPU_STAT(pf_fixed
) },
67 { "pf_guest", VCPU_STAT(pf_guest
) },
68 { "tlb_flush", VCPU_STAT(tlb_flush
) },
69 { "invlpg", VCPU_STAT(invlpg
) },
70 { "exits", VCPU_STAT(exits
) },
71 { "io_exits", VCPU_STAT(io_exits
) },
72 { "mmio_exits", VCPU_STAT(mmio_exits
) },
73 { "signal_exits", VCPU_STAT(signal_exits
) },
74 { "irq_window", VCPU_STAT(irq_window_exits
) },
75 { "nmi_window", VCPU_STAT(nmi_window_exits
) },
76 { "halt_exits", VCPU_STAT(halt_exits
) },
77 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
78 { "hypercalls", VCPU_STAT(hypercalls
) },
79 { "request_irq", VCPU_STAT(request_irq_exits
) },
80 { "irq_exits", VCPU_STAT(irq_exits
) },
81 { "host_state_reload", VCPU_STAT(host_state_reload
) },
82 { "efer_reload", VCPU_STAT(efer_reload
) },
83 { "fpu_reload", VCPU_STAT(fpu_reload
) },
84 { "insn_emulation", VCPU_STAT(insn_emulation
) },
85 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
86 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
87 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
88 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
89 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
90 { "mmu_flooded", VM_STAT(mmu_flooded
) },
91 { "mmu_recycled", VM_STAT(mmu_recycled
) },
92 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
93 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
94 { "largepages", VM_STAT(lpages
) },
99 unsigned long segment_base(u16 selector
)
101 struct descriptor_table gdt
;
102 struct desc_struct
*d
;
103 unsigned long table_base
;
109 asm("sgdt %0" : "=m"(gdt
));
110 table_base
= gdt
.base
;
112 if (selector
& 4) { /* from ldt */
115 asm("sldt %0" : "=g"(ldt_selector
));
116 table_base
= segment_base(ldt_selector
);
118 d
= (struct desc_struct
*)(table_base
+ (selector
& ~7));
119 v
= d
->base0
| ((unsigned long)d
->base1
<< 16) |
120 ((unsigned long)d
->base2
<< 24);
122 if (d
->s
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
123 v
|= ((unsigned long)((struct ldttss_desc64
*)d
)->base3
) << 32;
127 EXPORT_SYMBOL_GPL(segment_base
);
129 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
131 if (irqchip_in_kernel(vcpu
->kvm
))
132 return vcpu
->arch
.apic_base
;
134 return vcpu
->arch
.apic_base
;
136 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
138 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
140 /* TODO: reserve bits check */
141 if (irqchip_in_kernel(vcpu
->kvm
))
142 kvm_lapic_set_base(vcpu
, data
);
144 vcpu
->arch
.apic_base
= data
;
146 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
148 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
150 WARN_ON(vcpu
->arch
.exception
.pending
);
151 vcpu
->arch
.exception
.pending
= true;
152 vcpu
->arch
.exception
.has_error_code
= false;
153 vcpu
->arch
.exception
.nr
= nr
;
155 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
157 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
160 ++vcpu
->stat
.pf_guest
;
161 if (vcpu
->arch
.exception
.pending
) {
162 if (vcpu
->arch
.exception
.nr
== PF_VECTOR
) {
163 printk(KERN_DEBUG
"kvm: inject_page_fault:"
164 " double fault 0x%lx\n", addr
);
165 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
166 vcpu
->arch
.exception
.error_code
= 0;
167 } else if (vcpu
->arch
.exception
.nr
== DF_VECTOR
) {
168 /* triple fault -> shutdown */
169 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
173 vcpu
->arch
.cr2
= addr
;
174 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
177 void kvm_inject_nmi(struct kvm_vcpu
*vcpu
)
179 vcpu
->arch
.nmi_pending
= 1;
181 EXPORT_SYMBOL_GPL(kvm_inject_nmi
);
183 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
185 WARN_ON(vcpu
->arch
.exception
.pending
);
186 vcpu
->arch
.exception
.pending
= true;
187 vcpu
->arch
.exception
.has_error_code
= true;
188 vcpu
->arch
.exception
.nr
= nr
;
189 vcpu
->arch
.exception
.error_code
= error_code
;
191 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
193 static void __queue_exception(struct kvm_vcpu
*vcpu
)
195 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
196 vcpu
->arch
.exception
.has_error_code
,
197 vcpu
->arch
.exception
.error_code
);
201 * Load the pae pdptrs. Return true is they are all valid.
203 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
205 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
206 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
209 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
211 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
212 offset
* sizeof(u64
), sizeof(pdpte
));
217 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
218 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
225 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
230 EXPORT_SYMBOL_GPL(load_pdptrs
);
232 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
234 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
238 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
241 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
244 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
250 void kvm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
252 if (cr0
& CR0_RESERVED_BITS
) {
253 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
254 cr0
, vcpu
->arch
.cr0
);
255 kvm_inject_gp(vcpu
, 0);
259 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
260 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
261 kvm_inject_gp(vcpu
, 0);
265 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
266 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
267 "and a clear PE flag\n");
268 kvm_inject_gp(vcpu
, 0);
272 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
274 if ((vcpu
->arch
.shadow_efer
& EFER_LME
)) {
278 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
279 "in long mode while PAE is disabled\n");
280 kvm_inject_gp(vcpu
, 0);
283 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
285 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
286 "in long mode while CS.L == 1\n");
287 kvm_inject_gp(vcpu
, 0);
293 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
294 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
296 kvm_inject_gp(vcpu
, 0);
302 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
303 vcpu
->arch
.cr0
= cr0
;
305 kvm_mmu_reset_context(vcpu
);
308 EXPORT_SYMBOL_GPL(kvm_set_cr0
);
310 void kvm_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
312 kvm_set_cr0(vcpu
, (vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f));
313 KVMTRACE_1D(LMSW
, vcpu
,
314 (u32
)((vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f)),
317 EXPORT_SYMBOL_GPL(kvm_lmsw
);
319 void kvm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
321 unsigned long old_cr4
= vcpu
->arch
.cr4
;
322 unsigned long pdptr_bits
= X86_CR4_PGE
| X86_CR4_PSE
| X86_CR4_PAE
;
324 if (cr4
& CR4_RESERVED_BITS
) {
325 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
326 kvm_inject_gp(vcpu
, 0);
330 if (is_long_mode(vcpu
)) {
331 if (!(cr4
& X86_CR4_PAE
)) {
332 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
334 kvm_inject_gp(vcpu
, 0);
337 } else if (is_paging(vcpu
) && (cr4
& X86_CR4_PAE
)
338 && ((cr4
^ old_cr4
) & pdptr_bits
)
339 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
340 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
341 kvm_inject_gp(vcpu
, 0);
345 if (cr4
& X86_CR4_VMXE
) {
346 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
347 kvm_inject_gp(vcpu
, 0);
350 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
351 vcpu
->arch
.cr4
= cr4
;
352 kvm_mmu_reset_context(vcpu
);
354 EXPORT_SYMBOL_GPL(kvm_set_cr4
);
356 void kvm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
358 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
359 kvm_mmu_flush_tlb(vcpu
);
363 if (is_long_mode(vcpu
)) {
364 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
365 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
366 kvm_inject_gp(vcpu
, 0);
371 if (cr3
& CR3_PAE_RESERVED_BITS
) {
373 "set_cr3: #GP, reserved bits\n");
374 kvm_inject_gp(vcpu
, 0);
377 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
378 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
380 kvm_inject_gp(vcpu
, 0);
385 * We don't check reserved bits in nonpae mode, because
386 * this isn't enforced, and VMware depends on this.
391 * Does the new cr3 value map to physical memory? (Note, we
392 * catch an invalid cr3 even in real-mode, because it would
393 * cause trouble later on when we turn on paging anyway.)
395 * A real CPU would silently accept an invalid cr3 and would
396 * attempt to use it - with largely undefined (and often hard
397 * to debug) behavior on the guest side.
399 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
400 kvm_inject_gp(vcpu
, 0);
402 vcpu
->arch
.cr3
= cr3
;
403 vcpu
->arch
.mmu
.new_cr3(vcpu
);
406 EXPORT_SYMBOL_GPL(kvm_set_cr3
);
408 void kvm_set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
410 if (cr8
& CR8_RESERVED_BITS
) {
411 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
412 kvm_inject_gp(vcpu
, 0);
415 if (irqchip_in_kernel(vcpu
->kvm
))
416 kvm_lapic_set_tpr(vcpu
, cr8
);
418 vcpu
->arch
.cr8
= cr8
;
420 EXPORT_SYMBOL_GPL(kvm_set_cr8
);
422 unsigned long kvm_get_cr8(struct kvm_vcpu
*vcpu
)
424 if (irqchip_in_kernel(vcpu
->kvm
))
425 return kvm_lapic_get_cr8(vcpu
);
427 return vcpu
->arch
.cr8
;
429 EXPORT_SYMBOL_GPL(kvm_get_cr8
);
432 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
433 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
435 * This list is modified at module load time to reflect the
436 * capabilities of the host cpu.
438 static u32 msrs_to_save
[] = {
439 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
442 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
444 MSR_IA32_TIME_STAMP_COUNTER
, MSR_KVM_SYSTEM_TIME
, MSR_KVM_WALL_CLOCK
,
445 MSR_IA32_PERF_STATUS
,
448 static unsigned num_msrs_to_save
;
450 static u32 emulated_msrs
[] = {
451 MSR_IA32_MISC_ENABLE
,
454 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
456 if (efer
& efer_reserved_bits
) {
457 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
459 kvm_inject_gp(vcpu
, 0);
464 && (vcpu
->arch
.shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
465 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
466 kvm_inject_gp(vcpu
, 0);
470 kvm_x86_ops
->set_efer(vcpu
, efer
);
473 efer
|= vcpu
->arch
.shadow_efer
& EFER_LMA
;
475 vcpu
->arch
.shadow_efer
= efer
;
478 void kvm_enable_efer_bits(u64 mask
)
480 efer_reserved_bits
&= ~mask
;
482 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits
);
486 * Writes msr value into into the appropriate "register".
487 * Returns 0 on success, non-0 otherwise.
488 * Assumes vcpu_load() was already called.
490 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
492 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
496 * Adapt set_msr() to msr_io()'s calling convention
498 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
500 return kvm_set_msr(vcpu
, index
, *data
);
503 static void kvm_write_wall_clock(struct kvm
*kvm
, gpa_t wall_clock
)
506 struct pvclock_wall_clock wc
;
507 struct timespec now
, sys
, boot
;
514 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
517 * The guest calculates current wall clock time by adding
518 * system time (updated by kvm_write_guest_time below) to the
519 * wall clock specified here. guest system time equals host
520 * system time for us, thus we must fill in host boot time here.
522 now
= current_kernel_time();
524 boot
= ns_to_timespec(timespec_to_ns(&now
) - timespec_to_ns(&sys
));
526 wc
.sec
= boot
.tv_sec
;
527 wc
.nsec
= boot
.tv_nsec
;
528 wc
.version
= version
;
530 kvm_write_guest(kvm
, wall_clock
, &wc
, sizeof(wc
));
533 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
536 static uint32_t div_frac(uint32_t dividend
, uint32_t divisor
)
538 uint32_t quotient
, remainder
;
540 /* Don't try to replace with do_div(), this one calculates
541 * "(dividend << 32) / divisor" */
543 : "=a" (quotient
), "=d" (remainder
)
544 : "0" (0), "1" (dividend
), "r" (divisor
) );
548 static void kvm_set_time_scale(uint32_t tsc_khz
, struct pvclock_vcpu_time_info
*hv_clock
)
550 uint64_t nsecs
= 1000000000LL;
555 tps64
= tsc_khz
* 1000LL;
556 while (tps64
> nsecs
*2) {
561 tps32
= (uint32_t)tps64
;
562 while (tps32
<= (uint32_t)nsecs
) {
567 hv_clock
->tsc_shift
= shift
;
568 hv_clock
->tsc_to_system_mul
= div_frac(nsecs
, tps32
);
570 pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
571 __FUNCTION__
, tsc_khz
, hv_clock
->tsc_shift
,
572 hv_clock
->tsc_to_system_mul
);
575 static void kvm_write_guest_time(struct kvm_vcpu
*v
)
579 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
582 if ((!vcpu
->time_page
))
585 if (unlikely(vcpu
->hv_clock_tsc_khz
!= tsc_khz
)) {
586 kvm_set_time_scale(tsc_khz
, &vcpu
->hv_clock
);
587 vcpu
->hv_clock_tsc_khz
= tsc_khz
;
590 /* Keep irq disabled to prevent changes to the clock */
591 local_irq_save(flags
);
592 kvm_get_msr(v
, MSR_IA32_TIME_STAMP_COUNTER
,
593 &vcpu
->hv_clock
.tsc_timestamp
);
595 local_irq_restore(flags
);
597 /* With all the info we got, fill in the values */
599 vcpu
->hv_clock
.system_time
= ts
.tv_nsec
+
600 (NSEC_PER_SEC
* (u64
)ts
.tv_sec
);
602 * The interface expects us to write an even number signaling that the
603 * update is finished. Since the guest won't see the intermediate
604 * state, we just increase by 2 at the end.
606 vcpu
->hv_clock
.version
+= 2;
608 shared_kaddr
= kmap_atomic(vcpu
->time_page
, KM_USER0
);
610 memcpy(shared_kaddr
+ vcpu
->time_offset
, &vcpu
->hv_clock
,
611 sizeof(vcpu
->hv_clock
));
613 kunmap_atomic(shared_kaddr
, KM_USER0
);
615 mark_page_dirty(v
->kvm
, vcpu
->time
>> PAGE_SHIFT
);
618 static bool msr_mtrr_valid(unsigned msr
)
621 case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR
- 1:
622 case MSR_MTRRfix64K_00000
:
623 case MSR_MTRRfix16K_80000
:
624 case MSR_MTRRfix16K_A0000
:
625 case MSR_MTRRfix4K_C0000
:
626 case MSR_MTRRfix4K_C8000
:
627 case MSR_MTRRfix4K_D0000
:
628 case MSR_MTRRfix4K_D8000
:
629 case MSR_MTRRfix4K_E0000
:
630 case MSR_MTRRfix4K_E8000
:
631 case MSR_MTRRfix4K_F0000
:
632 case MSR_MTRRfix4K_F8000
:
633 case MSR_MTRRdefType
:
634 case MSR_IA32_CR_PAT
:
642 static int set_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
644 if (!msr_mtrr_valid(msr
))
647 vcpu
->arch
.mtrr
[msr
- 0x200] = data
;
651 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
655 set_efer(vcpu
, data
);
657 case MSR_IA32_MC0_STATUS
:
658 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
661 case MSR_IA32_MCG_STATUS
:
662 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
665 case MSR_IA32_MCG_CTL
:
666 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
669 case MSR_IA32_UCODE_REV
:
670 case MSR_IA32_UCODE_WRITE
:
672 case 0x200 ... 0x2ff:
673 return set_msr_mtrr(vcpu
, msr
, data
);
674 case MSR_IA32_APICBASE
:
675 kvm_set_apic_base(vcpu
, data
);
677 case MSR_IA32_MISC_ENABLE
:
678 vcpu
->arch
.ia32_misc_enable_msr
= data
;
680 case MSR_KVM_WALL_CLOCK
:
681 vcpu
->kvm
->arch
.wall_clock
= data
;
682 kvm_write_wall_clock(vcpu
->kvm
, data
);
684 case MSR_KVM_SYSTEM_TIME
: {
685 if (vcpu
->arch
.time_page
) {
686 kvm_release_page_dirty(vcpu
->arch
.time_page
);
687 vcpu
->arch
.time_page
= NULL
;
690 vcpu
->arch
.time
= data
;
692 /* we verify if the enable bit is set... */
696 /* ...but clean it before doing the actual write */
697 vcpu
->arch
.time_offset
= data
& ~(PAGE_MASK
| 1);
699 down_read(¤t
->mm
->mmap_sem
);
700 vcpu
->arch
.time_page
=
701 gfn_to_page(vcpu
->kvm
, data
>> PAGE_SHIFT
);
702 up_read(¤t
->mm
->mmap_sem
);
704 if (is_error_page(vcpu
->arch
.time_page
)) {
705 kvm_release_page_clean(vcpu
->arch
.time_page
);
706 vcpu
->arch
.time_page
= NULL
;
709 kvm_write_guest_time(vcpu
);
713 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n", msr
, data
);
718 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
722 * Reads an msr value (of 'msr_index') into 'pdata'.
723 * Returns 0 on success, non-0 otherwise.
724 * Assumes vcpu_load() was already called.
726 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
728 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
731 static int get_msr_mtrr(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
733 if (!msr_mtrr_valid(msr
))
736 *pdata
= vcpu
->arch
.mtrr
[msr
- 0x200];
740 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
745 case 0xc0010010: /* SYSCFG */
746 case 0xc0010015: /* HWCR */
747 case MSR_IA32_PLATFORM_ID
:
748 case MSR_IA32_P5_MC_ADDR
:
749 case MSR_IA32_P5_MC_TYPE
:
750 case MSR_IA32_MC0_CTL
:
751 case MSR_IA32_MCG_STATUS
:
752 case MSR_IA32_MCG_CAP
:
753 case MSR_IA32_MCG_CTL
:
754 case MSR_IA32_MC0_MISC
:
755 case MSR_IA32_MC0_MISC
+4:
756 case MSR_IA32_MC0_MISC
+8:
757 case MSR_IA32_MC0_MISC
+12:
758 case MSR_IA32_MC0_MISC
+16:
759 case MSR_IA32_MC0_MISC
+20:
760 case MSR_IA32_UCODE_REV
:
761 case MSR_IA32_EBL_CR_POWERON
:
765 data
= 0x500 | KVM_NR_VAR_MTRR
;
767 case 0x200 ... 0x2ff:
768 return get_msr_mtrr(vcpu
, msr
, pdata
);
769 case 0xcd: /* fsb frequency */
772 case MSR_IA32_APICBASE
:
773 data
= kvm_get_apic_base(vcpu
);
775 case MSR_IA32_MISC_ENABLE
:
776 data
= vcpu
->arch
.ia32_misc_enable_msr
;
778 case MSR_IA32_PERF_STATUS
:
779 /* TSC increment by tick */
782 data
|= (((uint64_t)4ULL) << 40);
785 data
= vcpu
->arch
.shadow_efer
;
787 case MSR_KVM_WALL_CLOCK
:
788 data
= vcpu
->kvm
->arch
.wall_clock
;
790 case MSR_KVM_SYSTEM_TIME
:
791 data
= vcpu
->arch
.time
;
794 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
800 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
803 * Read or write a bunch of msrs. All parameters are kernel addresses.
805 * @return number of msrs set successfully.
807 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
808 struct kvm_msr_entry
*entries
,
809 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
810 unsigned index
, u64
*data
))
816 down_read(&vcpu
->kvm
->slots_lock
);
817 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
818 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
820 up_read(&vcpu
->kvm
->slots_lock
);
828 * Read or write a bunch of msrs. Parameters are user addresses.
830 * @return number of msrs set successfully.
832 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
833 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
834 unsigned index
, u64
*data
),
837 struct kvm_msrs msrs
;
838 struct kvm_msr_entry
*entries
;
843 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
847 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
851 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
852 entries
= vmalloc(size
);
857 if (copy_from_user(entries
, user_msrs
->entries
, size
))
860 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
865 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
876 int kvm_dev_ioctl_check_extension(long ext
)
881 case KVM_CAP_IRQCHIP
:
883 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
884 case KVM_CAP_USER_MEMORY
:
885 case KVM_CAP_SET_TSS_ADDR
:
886 case KVM_CAP_EXT_CPUID
:
887 case KVM_CAP_CLOCKSOURCE
:
889 case KVM_CAP_NOP_IO_DELAY
:
890 case KVM_CAP_MP_STATE
:
891 case KVM_CAP_SYNC_MMU
:
894 case KVM_CAP_COALESCED_MMIO
:
895 r
= KVM_COALESCED_MMIO_PAGE_OFFSET
;
898 r
= !kvm_x86_ops
->cpu_has_accelerated_tpr();
900 case KVM_CAP_NR_VCPUS
:
903 case KVM_CAP_NR_MEMSLOTS
:
904 r
= KVM_MEMORY_SLOTS
;
917 long kvm_arch_dev_ioctl(struct file
*filp
,
918 unsigned int ioctl
, unsigned long arg
)
920 void __user
*argp
= (void __user
*)arg
;
924 case KVM_GET_MSR_INDEX_LIST
: {
925 struct kvm_msr_list __user
*user_msr_list
= argp
;
926 struct kvm_msr_list msr_list
;
930 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
933 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
934 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
937 if (n
< num_msrs_to_save
)
940 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
941 num_msrs_to_save
* sizeof(u32
)))
943 if (copy_to_user(user_msr_list
->indices
944 + num_msrs_to_save
* sizeof(u32
),
946 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
951 case KVM_GET_SUPPORTED_CPUID
: {
952 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
953 struct kvm_cpuid2 cpuid
;
956 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
958 r
= kvm_dev_ioctl_get_supported_cpuid(&cpuid
,
964 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
976 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
978 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
979 kvm_write_guest_time(vcpu
);
982 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
984 kvm_x86_ops
->vcpu_put(vcpu
);
985 kvm_put_guest_fpu(vcpu
);
988 static int is_efer_nx(void)
990 unsigned long long efer
= 0;
992 rdmsrl_safe(MSR_EFER
, &efer
);
993 return efer
& EFER_NX
;
996 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
999 struct kvm_cpuid_entry2
*e
, *entry
;
1002 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
1003 e
= &vcpu
->arch
.cpuid_entries
[i
];
1004 if (e
->function
== 0x80000001) {
1009 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
1010 entry
->edx
&= ~(1 << 20);
1011 printk(KERN_INFO
"kvm: guest NX capability removed\n");
1015 /* when an old userspace process fills a new kernel module */
1016 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
1017 struct kvm_cpuid
*cpuid
,
1018 struct kvm_cpuid_entry __user
*entries
)
1021 struct kvm_cpuid_entry
*cpuid_entries
;
1024 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
1027 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
1031 if (copy_from_user(cpuid_entries
, entries
,
1032 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
1034 for (i
= 0; i
< cpuid
->nent
; i
++) {
1035 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
1036 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
1037 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
1038 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
1039 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
1040 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
1041 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
1042 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
1043 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
1044 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
1046 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
1047 cpuid_fix_nx_cap(vcpu
);
1051 vfree(cpuid_entries
);
1056 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
1057 struct kvm_cpuid2
*cpuid
,
1058 struct kvm_cpuid_entry2 __user
*entries
)
1063 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
1066 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
1067 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
1069 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
1076 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
1077 struct kvm_cpuid2
*cpuid
,
1078 struct kvm_cpuid_entry2 __user
*entries
)
1083 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
1086 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
1087 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
1092 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
1096 static inline u32
bit(int bitno
)
1098 return 1 << (bitno
& 31);
1101 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1104 entry
->function
= function
;
1105 entry
->index
= index
;
1106 cpuid_count(entry
->function
, entry
->index
,
1107 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
1111 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1112 u32 index
, int *nent
, int maxnent
)
1114 const u32 kvm_supported_word0_x86_features
= bit(X86_FEATURE_FPU
) |
1115 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1116 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1117 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1118 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1119 bit(X86_FEATURE_SEP
) | bit(X86_FEATURE_PGE
) |
1120 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1121 bit(X86_FEATURE_CLFLSH
) | bit(X86_FEATURE_MMX
) |
1122 bit(X86_FEATURE_FXSR
) | bit(X86_FEATURE_XMM
) |
1123 bit(X86_FEATURE_XMM2
) | bit(X86_FEATURE_SELFSNOOP
);
1124 const u32 kvm_supported_word1_x86_features
= bit(X86_FEATURE_FPU
) |
1125 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1126 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1127 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1128 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1129 bit(X86_FEATURE_PGE
) |
1130 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1131 bit(X86_FEATURE_MMX
) | bit(X86_FEATURE_FXSR
) |
1132 bit(X86_FEATURE_SYSCALL
) |
1133 (bit(X86_FEATURE_NX
) && is_efer_nx()) |
1134 #ifdef CONFIG_X86_64
1135 bit(X86_FEATURE_LM
) |
1137 bit(X86_FEATURE_MMXEXT
) |
1138 bit(X86_FEATURE_3DNOWEXT
) |
1139 bit(X86_FEATURE_3DNOW
);
1140 const u32 kvm_supported_word3_x86_features
=
1141 bit(X86_FEATURE_XMM3
) | bit(X86_FEATURE_CX16
);
1142 const u32 kvm_supported_word6_x86_features
=
1143 bit(X86_FEATURE_LAHF_LM
) | bit(X86_FEATURE_CMP_LEGACY
);
1145 /* all func 2 cpuid_count() should be called on the same cpu */
1147 do_cpuid_1_ent(entry
, function
, index
);
1152 entry
->eax
= min(entry
->eax
, (u32
)0xb);
1155 entry
->edx
&= kvm_supported_word0_x86_features
;
1156 entry
->ecx
&= kvm_supported_word3_x86_features
;
1158 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1159 * may return different values. This forces us to get_cpu() before
1160 * issuing the first command, and also to emulate this annoying behavior
1161 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1163 int t
, times
= entry
->eax
& 0xff;
1165 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1166 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
1167 do_cpuid_1_ent(&entry
[t
], function
, 0);
1168 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1173 /* function 4 and 0xb have additional index. */
1177 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1178 /* read more entries until cache_type is zero */
1179 for (i
= 1; *nent
< maxnent
; ++i
) {
1180 cache_type
= entry
[i
- 1].eax
& 0x1f;
1183 do_cpuid_1_ent(&entry
[i
], function
, i
);
1185 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1193 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1194 /* read more entries until level_type is zero */
1195 for (i
= 1; *nent
< maxnent
; ++i
) {
1196 level_type
= entry
[i
- 1].ecx
& 0xff;
1199 do_cpuid_1_ent(&entry
[i
], function
, i
);
1201 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1207 entry
->eax
= min(entry
->eax
, 0x8000001a);
1210 entry
->edx
&= kvm_supported_word1_x86_features
;
1211 entry
->ecx
&= kvm_supported_word6_x86_features
;
1217 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
1218 struct kvm_cpuid_entry2 __user
*entries
)
1220 struct kvm_cpuid_entry2
*cpuid_entries
;
1221 int limit
, nent
= 0, r
= -E2BIG
;
1224 if (cpuid
->nent
< 1)
1227 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
1231 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
1232 limit
= cpuid_entries
[0].eax
;
1233 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1234 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1235 &nent
, cpuid
->nent
);
1237 if (nent
>= cpuid
->nent
)
1240 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
1241 limit
= cpuid_entries
[nent
- 1].eax
;
1242 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1243 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1244 &nent
, cpuid
->nent
);
1246 if (copy_to_user(entries
, cpuid_entries
,
1247 nent
* sizeof(struct kvm_cpuid_entry2
)))
1253 vfree(cpuid_entries
);
1258 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
1259 struct kvm_lapic_state
*s
)
1262 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
1268 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
1269 struct kvm_lapic_state
*s
)
1272 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1273 kvm_apic_post_state_restore(vcpu
);
1279 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1280 struct kvm_interrupt
*irq
)
1282 if (irq
->irq
< 0 || irq
->irq
>= 256)
1284 if (irqchip_in_kernel(vcpu
->kvm
))
1288 set_bit(irq
->irq
, vcpu
->arch
.irq_pending
);
1289 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
1296 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
1297 struct kvm_tpr_access_ctl
*tac
)
1301 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
1305 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1306 unsigned int ioctl
, unsigned long arg
)
1308 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1309 void __user
*argp
= (void __user
*)arg
;
1311 struct kvm_lapic_state
*lapic
= NULL
;
1314 case KVM_GET_LAPIC
: {
1315 lapic
= kzalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
1320 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, lapic
);
1324 if (copy_to_user(argp
, lapic
, sizeof(struct kvm_lapic_state
)))
1329 case KVM_SET_LAPIC
: {
1330 lapic
= kmalloc(sizeof(struct kvm_lapic_state
), GFP_KERNEL
);
1335 if (copy_from_user(lapic
, argp
, sizeof(struct kvm_lapic_state
)))
1337 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, lapic
);
1343 case KVM_INTERRUPT
: {
1344 struct kvm_interrupt irq
;
1347 if (copy_from_user(&irq
, argp
, sizeof irq
))
1349 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1355 case KVM_SET_CPUID
: {
1356 struct kvm_cpuid __user
*cpuid_arg
= argp
;
1357 struct kvm_cpuid cpuid
;
1360 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1362 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
1367 case KVM_SET_CPUID2
: {
1368 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1369 struct kvm_cpuid2 cpuid
;
1372 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1374 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
1375 cpuid_arg
->entries
);
1380 case KVM_GET_CPUID2
: {
1381 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1382 struct kvm_cpuid2 cpuid
;
1385 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1387 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
1388 cpuid_arg
->entries
);
1392 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1398 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
1401 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
1403 case KVM_TPR_ACCESS_REPORTING
: {
1404 struct kvm_tpr_access_ctl tac
;
1407 if (copy_from_user(&tac
, argp
, sizeof tac
))
1409 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
1413 if (copy_to_user(argp
, &tac
, sizeof tac
))
1418 case KVM_SET_VAPIC_ADDR
: {
1419 struct kvm_vapic_addr va
;
1422 if (!irqchip_in_kernel(vcpu
->kvm
))
1425 if (copy_from_user(&va
, argp
, sizeof va
))
1428 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
1440 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
1444 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
1446 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
1450 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
1451 u32 kvm_nr_mmu_pages
)
1453 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
1456 down_write(&kvm
->slots_lock
);
1458 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
1459 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
1461 up_write(&kvm
->slots_lock
);
1465 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
1467 return kvm
->arch
.n_alloc_mmu_pages
;
1470 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1473 struct kvm_mem_alias
*alias
;
1475 for (i
= 0; i
< kvm
->arch
.naliases
; ++i
) {
1476 alias
= &kvm
->arch
.aliases
[i
];
1477 if (gfn
>= alias
->base_gfn
1478 && gfn
< alias
->base_gfn
+ alias
->npages
)
1479 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1485 * Set a new alias region. Aliases map a portion of physical memory into
1486 * another portion. This is useful for memory windows, for example the PC
1489 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
1490 struct kvm_memory_alias
*alias
)
1493 struct kvm_mem_alias
*p
;
1496 /* General sanity checks */
1497 if (alias
->memory_size
& (PAGE_SIZE
- 1))
1499 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
1501 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
1503 if (alias
->guest_phys_addr
+ alias
->memory_size
1504 < alias
->guest_phys_addr
)
1506 if (alias
->target_phys_addr
+ alias
->memory_size
1507 < alias
->target_phys_addr
)
1510 down_write(&kvm
->slots_lock
);
1511 spin_lock(&kvm
->mmu_lock
);
1513 p
= &kvm
->arch
.aliases
[alias
->slot
];
1514 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
1515 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
1516 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
1518 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
1519 if (kvm
->arch
.aliases
[n
- 1].npages
)
1521 kvm
->arch
.naliases
= n
;
1523 spin_unlock(&kvm
->mmu_lock
);
1524 kvm_mmu_zap_all(kvm
);
1526 up_write(&kvm
->slots_lock
);
1534 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1539 switch (chip
->chip_id
) {
1540 case KVM_IRQCHIP_PIC_MASTER
:
1541 memcpy(&chip
->chip
.pic
,
1542 &pic_irqchip(kvm
)->pics
[0],
1543 sizeof(struct kvm_pic_state
));
1545 case KVM_IRQCHIP_PIC_SLAVE
:
1546 memcpy(&chip
->chip
.pic
,
1547 &pic_irqchip(kvm
)->pics
[1],
1548 sizeof(struct kvm_pic_state
));
1550 case KVM_IRQCHIP_IOAPIC
:
1551 memcpy(&chip
->chip
.ioapic
,
1552 ioapic_irqchip(kvm
),
1553 sizeof(struct kvm_ioapic_state
));
1562 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1567 switch (chip
->chip_id
) {
1568 case KVM_IRQCHIP_PIC_MASTER
:
1569 memcpy(&pic_irqchip(kvm
)->pics
[0],
1571 sizeof(struct kvm_pic_state
));
1573 case KVM_IRQCHIP_PIC_SLAVE
:
1574 memcpy(&pic_irqchip(kvm
)->pics
[1],
1576 sizeof(struct kvm_pic_state
));
1578 case KVM_IRQCHIP_IOAPIC
:
1579 memcpy(ioapic_irqchip(kvm
),
1581 sizeof(struct kvm_ioapic_state
));
1587 kvm_pic_update_irq(pic_irqchip(kvm
));
1591 static int kvm_vm_ioctl_get_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1595 memcpy(ps
, &kvm
->arch
.vpit
->pit_state
, sizeof(struct kvm_pit_state
));
1599 static int kvm_vm_ioctl_set_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1603 memcpy(&kvm
->arch
.vpit
->pit_state
, ps
, sizeof(struct kvm_pit_state
));
1604 kvm_pit_load_count(kvm
, 0, ps
->channels
[0].count
);
1609 * Get (and clear) the dirty memory log for a memory slot.
1611 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1612 struct kvm_dirty_log
*log
)
1616 struct kvm_memory_slot
*memslot
;
1619 down_write(&kvm
->slots_lock
);
1621 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1625 /* If nothing is dirty, don't bother messing with page tables. */
1627 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
1628 kvm_flush_remote_tlbs(kvm
);
1629 memslot
= &kvm
->memslots
[log
->slot
];
1630 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1631 memset(memslot
->dirty_bitmap
, 0, n
);
1635 up_write(&kvm
->slots_lock
);
1639 long kvm_arch_vm_ioctl(struct file
*filp
,
1640 unsigned int ioctl
, unsigned long arg
)
1642 struct kvm
*kvm
= filp
->private_data
;
1643 void __user
*argp
= (void __user
*)arg
;
1646 * This union makes it completely explicit to gcc-3.x
1647 * that these two variables' stack usage should be
1648 * combined, not added together.
1651 struct kvm_pit_state ps
;
1652 struct kvm_memory_alias alias
;
1656 case KVM_SET_TSS_ADDR
:
1657 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
1661 case KVM_SET_MEMORY_REGION
: {
1662 struct kvm_memory_region kvm_mem
;
1663 struct kvm_userspace_memory_region kvm_userspace_mem
;
1666 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
1668 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
1669 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
1670 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
1671 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
1672 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1677 case KVM_SET_NR_MMU_PAGES
:
1678 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1682 case KVM_GET_NR_MMU_PAGES
:
1683 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1685 case KVM_SET_MEMORY_ALIAS
:
1687 if (copy_from_user(&u
.alias
, argp
, sizeof(struct kvm_memory_alias
)))
1689 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &u
.alias
);
1693 case KVM_CREATE_IRQCHIP
:
1695 kvm
->arch
.vpic
= kvm_create_pic(kvm
);
1696 if (kvm
->arch
.vpic
) {
1697 r
= kvm_ioapic_init(kvm
);
1699 kfree(kvm
->arch
.vpic
);
1700 kvm
->arch
.vpic
= NULL
;
1706 case KVM_CREATE_PIT
:
1708 kvm
->arch
.vpit
= kvm_create_pit(kvm
);
1712 case KVM_IRQ_LINE
: {
1713 struct kvm_irq_level irq_event
;
1716 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1718 if (irqchip_in_kernel(kvm
)) {
1719 mutex_lock(&kvm
->lock
);
1720 if (irq_event
.irq
< 16)
1721 kvm_pic_set_irq(pic_irqchip(kvm
),
1724 kvm_ioapic_set_irq(kvm
->arch
.vioapic
,
1727 mutex_unlock(&kvm
->lock
);
1732 case KVM_GET_IRQCHIP
: {
1733 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1734 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
1740 if (copy_from_user(chip
, argp
, sizeof *chip
))
1741 goto get_irqchip_out
;
1743 if (!irqchip_in_kernel(kvm
))
1744 goto get_irqchip_out
;
1745 r
= kvm_vm_ioctl_get_irqchip(kvm
, chip
);
1747 goto get_irqchip_out
;
1749 if (copy_to_user(argp
, chip
, sizeof *chip
))
1750 goto get_irqchip_out
;
1758 case KVM_SET_IRQCHIP
: {
1759 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1760 struct kvm_irqchip
*chip
= kmalloc(sizeof(*chip
), GFP_KERNEL
);
1766 if (copy_from_user(chip
, argp
, sizeof *chip
))
1767 goto set_irqchip_out
;
1769 if (!irqchip_in_kernel(kvm
))
1770 goto set_irqchip_out
;
1771 r
= kvm_vm_ioctl_set_irqchip(kvm
, chip
);
1773 goto set_irqchip_out
;
1783 if (copy_from_user(&u
.ps
, argp
, sizeof(struct kvm_pit_state
)))
1786 if (!kvm
->arch
.vpit
)
1788 r
= kvm_vm_ioctl_get_pit(kvm
, &u
.ps
);
1792 if (copy_to_user(argp
, &u
.ps
, sizeof(struct kvm_pit_state
)))
1799 if (copy_from_user(&u
.ps
, argp
, sizeof u
.ps
))
1802 if (!kvm
->arch
.vpit
)
1804 r
= kvm_vm_ioctl_set_pit(kvm
, &u
.ps
);
1817 static void kvm_init_msr_list(void)
1822 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1823 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1826 msrs_to_save
[j
] = msrs_to_save
[i
];
1829 num_msrs_to_save
= j
;
1833 * Only apic need an MMIO device hook, so shortcut now..
1835 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
1836 gpa_t addr
, int len
,
1839 struct kvm_io_device
*dev
;
1841 if (vcpu
->arch
.apic
) {
1842 dev
= &vcpu
->arch
.apic
->dev
;
1843 if (dev
->in_range(dev
, addr
, len
, is_write
))
1850 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1851 gpa_t addr
, int len
,
1854 struct kvm_io_device
*dev
;
1856 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
, len
, is_write
);
1858 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
, len
,
1863 int emulator_read_std(unsigned long addr
,
1866 struct kvm_vcpu
*vcpu
)
1869 int r
= X86EMUL_CONTINUE
;
1872 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1873 unsigned offset
= addr
& (PAGE_SIZE
-1);
1874 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1877 if (gpa
== UNMAPPED_GVA
) {
1878 r
= X86EMUL_PROPAGATE_FAULT
;
1881 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, tocopy
);
1883 r
= X86EMUL_UNHANDLEABLE
;
1894 EXPORT_SYMBOL_GPL(emulator_read_std
);
1896 static int emulator_read_emulated(unsigned long addr
,
1899 struct kvm_vcpu
*vcpu
)
1901 struct kvm_io_device
*mmio_dev
;
1904 if (vcpu
->mmio_read_completed
) {
1905 memcpy(val
, vcpu
->mmio_data
, bytes
);
1906 vcpu
->mmio_read_completed
= 0;
1907 return X86EMUL_CONTINUE
;
1910 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1912 /* For APIC access vmexit */
1913 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1916 if (emulator_read_std(addr
, val
, bytes
, vcpu
)
1917 == X86EMUL_CONTINUE
)
1918 return X86EMUL_CONTINUE
;
1919 if (gpa
== UNMAPPED_GVA
)
1920 return X86EMUL_PROPAGATE_FAULT
;
1924 * Is this MMIO handled locally?
1926 mutex_lock(&vcpu
->kvm
->lock
);
1927 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
, bytes
, 0);
1929 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1930 mutex_unlock(&vcpu
->kvm
->lock
);
1931 return X86EMUL_CONTINUE
;
1933 mutex_unlock(&vcpu
->kvm
->lock
);
1935 vcpu
->mmio_needed
= 1;
1936 vcpu
->mmio_phys_addr
= gpa
;
1937 vcpu
->mmio_size
= bytes
;
1938 vcpu
->mmio_is_write
= 0;
1940 return X86EMUL_UNHANDLEABLE
;
1943 int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1944 const void *val
, int bytes
)
1948 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
1951 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1955 static int emulator_write_emulated_onepage(unsigned long addr
,
1958 struct kvm_vcpu
*vcpu
)
1960 struct kvm_io_device
*mmio_dev
;
1963 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1965 if (gpa
== UNMAPPED_GVA
) {
1966 kvm_inject_page_fault(vcpu
, addr
, 2);
1967 return X86EMUL_PROPAGATE_FAULT
;
1970 /* For APIC access vmexit */
1971 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1974 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1975 return X86EMUL_CONTINUE
;
1979 * Is this MMIO handled locally?
1981 mutex_lock(&vcpu
->kvm
->lock
);
1982 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
, bytes
, 1);
1984 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1985 mutex_unlock(&vcpu
->kvm
->lock
);
1986 return X86EMUL_CONTINUE
;
1988 mutex_unlock(&vcpu
->kvm
->lock
);
1990 vcpu
->mmio_needed
= 1;
1991 vcpu
->mmio_phys_addr
= gpa
;
1992 vcpu
->mmio_size
= bytes
;
1993 vcpu
->mmio_is_write
= 1;
1994 memcpy(vcpu
->mmio_data
, val
, bytes
);
1996 return X86EMUL_CONTINUE
;
1999 int emulator_write_emulated(unsigned long addr
,
2002 struct kvm_vcpu
*vcpu
)
2004 /* Crossing a page boundary? */
2005 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
2008 now
= -addr
& ~PAGE_MASK
;
2009 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
2010 if (rc
!= X86EMUL_CONTINUE
)
2016 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
2018 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
2020 static int emulator_cmpxchg_emulated(unsigned long addr
,
2024 struct kvm_vcpu
*vcpu
)
2026 static int reported
;
2030 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
2032 #ifndef CONFIG_X86_64
2033 /* guests cmpxchg8b have to be emulated atomically */
2040 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
2042 if (gpa
== UNMAPPED_GVA
||
2043 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
2046 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
2051 down_read(¤t
->mm
->mmap_sem
);
2052 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
2053 up_read(¤t
->mm
->mmap_sem
);
2055 kaddr
= kmap_atomic(page
, KM_USER0
);
2056 set_64bit((u64
*)(kaddr
+ offset_in_page(gpa
)), val
);
2057 kunmap_atomic(kaddr
, KM_USER0
);
2058 kvm_release_page_dirty(page
);
2063 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
2066 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
2068 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
2071 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
2073 return X86EMUL_CONTINUE
;
2076 int emulate_clts(struct kvm_vcpu
*vcpu
)
2078 KVMTRACE_0D(CLTS
, vcpu
, handler
);
2079 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
& ~X86_CR0_TS
);
2080 return X86EMUL_CONTINUE
;
2083 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
2085 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
2089 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
2090 return X86EMUL_CONTINUE
;
2092 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __func__
, dr
);
2093 return X86EMUL_UNHANDLEABLE
;
2097 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
2099 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
2102 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
2104 /* FIXME: better handling */
2105 return X86EMUL_UNHANDLEABLE
;
2107 return X86EMUL_CONTINUE
;
2110 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
2113 unsigned long rip
= vcpu
->arch
.rip
;
2114 unsigned long rip_linear
;
2116 if (!printk_ratelimit())
2119 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
2121 emulator_read_std(rip_linear
, (void *)opcodes
, 4, vcpu
);
2123 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2124 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
2126 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
2128 static struct x86_emulate_ops emulate_ops
= {
2129 .read_std
= emulator_read_std
,
2130 .read_emulated
= emulator_read_emulated
,
2131 .write_emulated
= emulator_write_emulated
,
2132 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
2135 int emulate_instruction(struct kvm_vcpu
*vcpu
,
2136 struct kvm_run
*run
,
2142 struct decode_cache
*c
;
2144 vcpu
->arch
.mmio_fault_cr2
= cr2
;
2145 kvm_x86_ops
->cache_regs(vcpu
);
2147 vcpu
->mmio_is_write
= 0;
2148 vcpu
->arch
.pio
.string
= 0;
2150 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
2152 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
2154 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
2155 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
2156 vcpu
->arch
.emulate_ctxt
.mode
=
2157 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
2158 ? X86EMUL_MODE_REAL
: cs_l
2159 ? X86EMUL_MODE_PROT64
: cs_db
2160 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
2162 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2164 /* Reject the instructions other than VMCALL/VMMCALL when
2165 * try to emulate invalid opcode */
2166 c
= &vcpu
->arch
.emulate_ctxt
.decode
;
2167 if ((emulation_type
& EMULTYPE_TRAP_UD
) &&
2168 (!(c
->twobyte
&& c
->b
== 0x01 &&
2169 (c
->modrm_reg
== 0 || c
->modrm_reg
== 3) &&
2170 c
->modrm_mod
== 3 && c
->modrm_rm
== 1)))
2171 return EMULATE_FAIL
;
2173 ++vcpu
->stat
.insn_emulation
;
2175 ++vcpu
->stat
.insn_emulation_fail
;
2176 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2177 return EMULATE_DONE
;
2178 return EMULATE_FAIL
;
2182 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2184 if (vcpu
->arch
.pio
.string
)
2185 return EMULATE_DO_MMIO
;
2187 if ((r
|| vcpu
->mmio_is_write
) && run
) {
2188 run
->exit_reason
= KVM_EXIT_MMIO
;
2189 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
2190 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
2191 run
->mmio
.len
= vcpu
->mmio_size
;
2192 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
2196 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2197 return EMULATE_DONE
;
2198 if (!vcpu
->mmio_needed
) {
2199 kvm_report_emulation_failure(vcpu
, "mmio");
2200 return EMULATE_FAIL
;
2202 return EMULATE_DO_MMIO
;
2205 kvm_x86_ops
->decache_regs(vcpu
);
2206 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
2208 if (vcpu
->mmio_is_write
) {
2209 vcpu
->mmio_needed
= 0;
2210 return EMULATE_DO_MMIO
;
2213 return EMULATE_DONE
;
2215 EXPORT_SYMBOL_GPL(emulate_instruction
);
2217 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
2221 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.pio
.guest_pages
); ++i
)
2222 if (vcpu
->arch
.pio
.guest_pages
[i
]) {
2223 kvm_release_page_dirty(vcpu
->arch
.pio
.guest_pages
[i
]);
2224 vcpu
->arch
.pio
.guest_pages
[i
] = NULL
;
2228 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
2230 void *p
= vcpu
->arch
.pio_data
;
2233 int nr_pages
= vcpu
->arch
.pio
.guest_pages
[1] ? 2 : 1;
2235 q
= vmap(vcpu
->arch
.pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
2238 free_pio_guest_pages(vcpu
);
2241 q
+= vcpu
->arch
.pio
.guest_page_offset
;
2242 bytes
= vcpu
->arch
.pio
.size
* vcpu
->arch
.pio
.cur_count
;
2243 if (vcpu
->arch
.pio
.in
)
2244 memcpy(q
, p
, bytes
);
2246 memcpy(p
, q
, bytes
);
2247 q
-= vcpu
->arch
.pio
.guest_page_offset
;
2249 free_pio_guest_pages(vcpu
);
2253 int complete_pio(struct kvm_vcpu
*vcpu
)
2255 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2259 kvm_x86_ops
->cache_regs(vcpu
);
2263 memcpy(&vcpu
->arch
.regs
[VCPU_REGS_RAX
], vcpu
->arch
.pio_data
,
2267 r
= pio_copy_data(vcpu
);
2269 kvm_x86_ops
->cache_regs(vcpu
);
2276 delta
*= io
->cur_count
;
2278 * The size of the register should really depend on
2279 * current address size.
2281 vcpu
->arch
.regs
[VCPU_REGS_RCX
] -= delta
;
2287 vcpu
->arch
.regs
[VCPU_REGS_RDI
] += delta
;
2289 vcpu
->arch
.regs
[VCPU_REGS_RSI
] += delta
;
2292 kvm_x86_ops
->decache_regs(vcpu
);
2294 io
->count
-= io
->cur_count
;
2300 static void kernel_pio(struct kvm_io_device
*pio_dev
,
2301 struct kvm_vcpu
*vcpu
,
2304 /* TODO: String I/O for in kernel device */
2306 mutex_lock(&vcpu
->kvm
->lock
);
2307 if (vcpu
->arch
.pio
.in
)
2308 kvm_iodevice_read(pio_dev
, vcpu
->arch
.pio
.port
,
2309 vcpu
->arch
.pio
.size
,
2312 kvm_iodevice_write(pio_dev
, vcpu
->arch
.pio
.port
,
2313 vcpu
->arch
.pio
.size
,
2315 mutex_unlock(&vcpu
->kvm
->lock
);
2318 static void pio_string_write(struct kvm_io_device
*pio_dev
,
2319 struct kvm_vcpu
*vcpu
)
2321 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2322 void *pd
= vcpu
->arch
.pio_data
;
2325 mutex_lock(&vcpu
->kvm
->lock
);
2326 for (i
= 0; i
< io
->cur_count
; i
++) {
2327 kvm_iodevice_write(pio_dev
, io
->port
,
2332 mutex_unlock(&vcpu
->kvm
->lock
);
2335 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
2336 gpa_t addr
, int len
,
2339 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
, len
, is_write
);
2342 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2343 int size
, unsigned port
)
2345 struct kvm_io_device
*pio_dev
;
2347 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2348 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2349 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2350 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2351 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= 1;
2352 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2353 vcpu
->arch
.pio
.in
= in
;
2354 vcpu
->arch
.pio
.string
= 0;
2355 vcpu
->arch
.pio
.down
= 0;
2356 vcpu
->arch
.pio
.guest_page_offset
= 0;
2357 vcpu
->arch
.pio
.rep
= 0;
2359 if (vcpu
->run
->io
.direction
== KVM_EXIT_IO_IN
)
2360 KVMTRACE_2D(IO_READ
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2363 KVMTRACE_2D(IO_WRITE
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2366 kvm_x86_ops
->cache_regs(vcpu
);
2367 memcpy(vcpu
->arch
.pio_data
, &vcpu
->arch
.regs
[VCPU_REGS_RAX
], 4);
2369 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2371 pio_dev
= vcpu_find_pio_dev(vcpu
, port
, size
, !in
);
2373 kernel_pio(pio_dev
, vcpu
, vcpu
->arch
.pio_data
);
2379 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
2381 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2382 int size
, unsigned long count
, int down
,
2383 gva_t address
, int rep
, unsigned port
)
2385 unsigned now
, in_page
;
2389 struct kvm_io_device
*pio_dev
;
2391 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2392 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2393 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2394 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2395 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= count
;
2396 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2397 vcpu
->arch
.pio
.in
= in
;
2398 vcpu
->arch
.pio
.string
= 1;
2399 vcpu
->arch
.pio
.down
= down
;
2400 vcpu
->arch
.pio
.guest_page_offset
= offset_in_page(address
);
2401 vcpu
->arch
.pio
.rep
= rep
;
2403 if (vcpu
->run
->io
.direction
== KVM_EXIT_IO_IN
)
2404 KVMTRACE_2D(IO_READ
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2407 KVMTRACE_2D(IO_WRITE
, vcpu
, vcpu
->run
->io
.port
, (u32
)size
,
2411 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2416 in_page
= PAGE_SIZE
- offset_in_page(address
);
2418 in_page
= offset_in_page(address
) + size
;
2419 now
= min(count
, (unsigned long)in_page
/ size
);
2422 * String I/O straddles page boundary. Pin two guest pages
2423 * so that we satisfy atomicity constraints. Do just one
2424 * transaction to avoid complexity.
2431 * String I/O in reverse. Yuck. Kill the guest, fix later.
2433 pr_unimpl(vcpu
, "guest string pio down\n");
2434 kvm_inject_gp(vcpu
, 0);
2437 vcpu
->run
->io
.count
= now
;
2438 vcpu
->arch
.pio
.cur_count
= now
;
2440 if (vcpu
->arch
.pio
.cur_count
== vcpu
->arch
.pio
.count
)
2441 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2443 for (i
= 0; i
< nr_pages
; ++i
) {
2444 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
2445 vcpu
->arch
.pio
.guest_pages
[i
] = page
;
2447 kvm_inject_gp(vcpu
, 0);
2448 free_pio_guest_pages(vcpu
);
2453 pio_dev
= vcpu_find_pio_dev(vcpu
, port
,
2454 vcpu
->arch
.pio
.cur_count
,
2455 !vcpu
->arch
.pio
.in
);
2456 if (!vcpu
->arch
.pio
.in
) {
2457 /* string PIO write */
2458 ret
= pio_copy_data(vcpu
);
2459 if (ret
>= 0 && pio_dev
) {
2460 pio_string_write(pio_dev
, vcpu
);
2462 if (vcpu
->arch
.pio
.count
== 0)
2466 pr_unimpl(vcpu
, "no string pio read support yet, "
2467 "port %x size %d count %ld\n",
2472 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2474 int kvm_arch_init(void *opaque
)
2477 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
2480 printk(KERN_ERR
"kvm: already loaded the other module\n");
2485 if (!ops
->cpu_has_kvm_support()) {
2486 printk(KERN_ERR
"kvm: no hardware support\n");
2490 if (ops
->disabled_by_bios()) {
2491 printk(KERN_ERR
"kvm: disabled by bios\n");
2496 r
= kvm_mmu_module_init();
2500 kvm_init_msr_list();
2503 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2504 kvm_mmu_set_base_ptes(PT_PRESENT_MASK
);
2505 kvm_mmu_set_mask_ptes(PT_USER_MASK
, PT_ACCESSED_MASK
,
2506 PT_DIRTY_MASK
, PT64_NX_MASK
, 0);
2513 void kvm_arch_exit(void)
2516 kvm_mmu_module_exit();
2519 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
2521 ++vcpu
->stat
.halt_exits
;
2522 KVMTRACE_0D(HLT
, vcpu
, handler
);
2523 if (irqchip_in_kernel(vcpu
->kvm
)) {
2524 vcpu
->arch
.mp_state
= KVM_MP_STATE_HALTED
;
2525 up_read(&vcpu
->kvm
->slots_lock
);
2526 kvm_vcpu_block(vcpu
);
2527 down_read(&vcpu
->kvm
->slots_lock
);
2528 if (vcpu
->arch
.mp_state
!= KVM_MP_STATE_RUNNABLE
)
2532 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
2536 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
2538 static inline gpa_t
hc_gpa(struct kvm_vcpu
*vcpu
, unsigned long a0
,
2541 if (is_long_mode(vcpu
))
2544 return a0
| ((gpa_t
)a1
<< 32);
2547 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
2549 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
2552 kvm_x86_ops
->cache_regs(vcpu
);
2554 nr
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2555 a0
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2556 a1
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2557 a2
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2558 a3
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2560 KVMTRACE_1D(VMMCALL
, vcpu
, (u32
)nr
, handler
);
2562 if (!is_long_mode(vcpu
)) {
2571 case KVM_HC_VAPIC_POLL_IRQ
:
2575 r
= kvm_pv_mmu_op(vcpu
, a0
, hc_gpa(vcpu
, a1
, a2
), &ret
);
2581 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = ret
;
2582 kvm_x86_ops
->decache_regs(vcpu
);
2583 ++vcpu
->stat
.hypercalls
;
2586 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
2588 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
2590 char instruction
[3];
2595 * Blow out the MMU to ensure that no other VCPU has an active mapping
2596 * to ensure that the updated hypercall appears atomically across all
2599 kvm_mmu_zap_all(vcpu
->kvm
);
2601 kvm_x86_ops
->cache_regs(vcpu
);
2602 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
2603 if (emulator_write_emulated(vcpu
->arch
.rip
, instruction
, 3, vcpu
)
2604 != X86EMUL_CONTINUE
)
2610 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
2612 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
2615 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2617 struct descriptor_table dt
= { limit
, base
};
2619 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2622 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2624 struct descriptor_table dt
= { limit
, base
};
2626 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2629 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
2630 unsigned long *rflags
)
2632 kvm_lmsw(vcpu
, msw
);
2633 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2636 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
2638 unsigned long value
;
2640 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2643 value
= vcpu
->arch
.cr0
;
2646 value
= vcpu
->arch
.cr2
;
2649 value
= vcpu
->arch
.cr3
;
2652 value
= vcpu
->arch
.cr4
;
2655 value
= kvm_get_cr8(vcpu
);
2658 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
2661 KVMTRACE_3D(CR_READ
, vcpu
, (u32
)cr
, (u32
)value
,
2662 (u32
)((u64
)value
>> 32), handler
);
2667 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
2668 unsigned long *rflags
)
2670 KVMTRACE_3D(CR_WRITE
, vcpu
, (u32
)cr
, (u32
)val
,
2671 (u32
)((u64
)val
>> 32), handler
);
2675 kvm_set_cr0(vcpu
, mk_cr_64(vcpu
->arch
.cr0
, val
));
2676 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2679 vcpu
->arch
.cr2
= val
;
2682 kvm_set_cr3(vcpu
, val
);
2685 kvm_set_cr4(vcpu
, mk_cr_64(vcpu
->arch
.cr4
, val
));
2688 kvm_set_cr8(vcpu
, val
& 0xfUL
);
2691 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
2695 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
2697 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
2698 int j
, nent
= vcpu
->arch
.cpuid_nent
;
2700 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
2701 /* when no next entry is found, the current entry[i] is reselected */
2702 for (j
= i
+ 1; j
== i
; j
= (j
+ 1) % nent
) {
2703 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
2704 if (ej
->function
== e
->function
) {
2705 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
2709 return 0; /* silence gcc, even though control never reaches here */
2712 /* find an entry with matching function, matching index (if needed), and that
2713 * should be read next (if it's stateful) */
2714 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
2715 u32 function
, u32 index
)
2717 if (e
->function
!= function
)
2719 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
2721 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
2722 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
2727 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
2730 u32 function
, index
;
2731 struct kvm_cpuid_entry2
*e
, *best
;
2733 kvm_x86_ops
->cache_regs(vcpu
);
2734 function
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2735 index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2736 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = 0;
2737 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = 0;
2738 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = 0;
2739 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = 0;
2741 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
2742 e
= &vcpu
->arch
.cpuid_entries
[i
];
2743 if (is_matching_cpuid_entry(e
, function
, index
)) {
2744 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
2745 move_to_next_stateful_cpuid_entry(vcpu
, i
);
2750 * Both basic or both extended?
2752 if (((e
->function
^ function
) & 0x80000000) == 0)
2753 if (!best
|| e
->function
> best
->function
)
2757 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = best
->eax
;
2758 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = best
->ebx
;
2759 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = best
->ecx
;
2760 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = best
->edx
;
2762 kvm_x86_ops
->decache_regs(vcpu
);
2763 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2764 KVMTRACE_5D(CPUID
, vcpu
, function
,
2765 (u32
)vcpu
->arch
.regs
[VCPU_REGS_RAX
],
2766 (u32
)vcpu
->arch
.regs
[VCPU_REGS_RBX
],
2767 (u32
)vcpu
->arch
.regs
[VCPU_REGS_RCX
],
2768 (u32
)vcpu
->arch
.regs
[VCPU_REGS_RDX
], handler
);
2770 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
2773 * Check if userspace requested an interrupt window, and that the
2774 * interrupt window is open.
2776 * No need to exit to userspace if we already have an interrupt queued.
2778 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
2779 struct kvm_run
*kvm_run
)
2781 return (!vcpu
->arch
.irq_summary
&&
2782 kvm_run
->request_interrupt_window
&&
2783 vcpu
->arch
.interrupt_window_open
&&
2784 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
2787 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
2788 struct kvm_run
*kvm_run
)
2790 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
2791 kvm_run
->cr8
= kvm_get_cr8(vcpu
);
2792 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
2793 if (irqchip_in_kernel(vcpu
->kvm
))
2794 kvm_run
->ready_for_interrupt_injection
= 1;
2796 kvm_run
->ready_for_interrupt_injection
=
2797 (vcpu
->arch
.interrupt_window_open
&&
2798 vcpu
->arch
.irq_summary
== 0);
2801 static void vapic_enter(struct kvm_vcpu
*vcpu
)
2803 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2806 if (!apic
|| !apic
->vapic_addr
)
2809 down_read(¤t
->mm
->mmap_sem
);
2810 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2811 up_read(¤t
->mm
->mmap_sem
);
2813 vcpu
->arch
.apic
->vapic_page
= page
;
2816 static void vapic_exit(struct kvm_vcpu
*vcpu
)
2818 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2820 if (!apic
|| !apic
->vapic_addr
)
2823 down_read(&vcpu
->kvm
->slots_lock
);
2824 kvm_release_page_dirty(apic
->vapic_page
);
2825 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2826 up_read(&vcpu
->kvm
->slots_lock
);
2829 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2833 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
)) {
2834 pr_debug("vcpu %d received sipi with vector # %x\n",
2835 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
2836 kvm_lapic_reset(vcpu
);
2837 r
= kvm_x86_ops
->vcpu_reset(vcpu
);
2840 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
2843 down_read(&vcpu
->kvm
->slots_lock
);
2848 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
2849 kvm_mmu_unload(vcpu
);
2851 r
= kvm_mmu_reload(vcpu
);
2855 if (vcpu
->requests
) {
2856 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
2857 __kvm_migrate_timers(vcpu
);
2858 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
2859 kvm_x86_ops
->tlb_flush(vcpu
);
2860 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
2862 kvm_run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
2866 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
)) {
2867 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2873 clear_bit(KVM_REQ_PENDING_TIMER
, &vcpu
->requests
);
2874 kvm_inject_pending_timer_irqs(vcpu
);
2878 kvm_x86_ops
->prepare_guest_switch(vcpu
);
2879 kvm_load_guest_fpu(vcpu
);
2881 local_irq_disable();
2883 if (vcpu
->requests
|| need_resched()) {
2890 if (signal_pending(current
)) {
2894 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2895 ++vcpu
->stat
.signal_exits
;
2899 if (vcpu
->guest_debug
.enabled
)
2900 kvm_x86_ops
->guest_debug_pre(vcpu
);
2902 vcpu
->guest_mode
= 1;
2904 * Make sure that guest_mode assignment won't happen after
2905 * testing the pending IRQ vector bitmap.
2909 if (vcpu
->arch
.exception
.pending
)
2910 __queue_exception(vcpu
);
2911 else if (irqchip_in_kernel(vcpu
->kvm
))
2912 kvm_x86_ops
->inject_pending_irq(vcpu
);
2914 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
2916 kvm_lapic_sync_to_vapic(vcpu
);
2918 up_read(&vcpu
->kvm
->slots_lock
);
2923 KVMTRACE_0D(VMENTRY
, vcpu
, entryexit
);
2924 kvm_x86_ops
->run(vcpu
, kvm_run
);
2926 vcpu
->guest_mode
= 0;
2932 * We must have an instruction between local_irq_enable() and
2933 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2934 * the interrupt shadow. The stat.exits increment will do nicely.
2935 * But we need to prevent reordering, hence this barrier():
2943 down_read(&vcpu
->kvm
->slots_lock
);
2946 * Profile KVM exit RIPs:
2948 if (unlikely(prof_on
== KVM_PROFILING
)) {
2949 kvm_x86_ops
->cache_regs(vcpu
);
2950 profile_hit(KVM_PROFILING
, (void *)vcpu
->arch
.rip
);
2953 if (vcpu
->arch
.exception
.pending
&& kvm_x86_ops
->exception_injected(vcpu
))
2954 vcpu
->arch
.exception
.pending
= false;
2956 kvm_lapic_sync_from_vapic(vcpu
);
2958 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
2961 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
2963 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2964 ++vcpu
->stat
.request_irq_exits
;
2967 if (!need_resched())
2972 up_read(&vcpu
->kvm
->slots_lock
);
2975 down_read(&vcpu
->kvm
->slots_lock
);
2979 post_kvm_run_save(vcpu
, kvm_run
);
2986 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2993 if (vcpu
->sigset_active
)
2994 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2996 if (unlikely(vcpu
->arch
.mp_state
== KVM_MP_STATE_UNINITIALIZED
)) {
2997 kvm_vcpu_block(vcpu
);
3002 /* re-sync apic's tpr */
3003 if (!irqchip_in_kernel(vcpu
->kvm
))
3004 kvm_set_cr8(vcpu
, kvm_run
->cr8
);
3006 if (vcpu
->arch
.pio
.cur_count
) {
3007 r
= complete_pio(vcpu
);
3011 #if CONFIG_HAS_IOMEM
3012 if (vcpu
->mmio_needed
) {
3013 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
3014 vcpu
->mmio_read_completed
= 1;
3015 vcpu
->mmio_needed
= 0;
3017 down_read(&vcpu
->kvm
->slots_lock
);
3018 r
= emulate_instruction(vcpu
, kvm_run
,
3019 vcpu
->arch
.mmio_fault_cr2
, 0,
3020 EMULTYPE_NO_DECODE
);
3021 up_read(&vcpu
->kvm
->slots_lock
);
3022 if (r
== EMULATE_DO_MMIO
) {
3024 * Read-modify-write. Back to userspace.
3031 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
3032 kvm_x86_ops
->cache_regs(vcpu
);
3033 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
3034 kvm_x86_ops
->decache_regs(vcpu
);
3037 r
= __vcpu_run(vcpu
, kvm_run
);
3040 if (vcpu
->sigset_active
)
3041 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
3047 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
3051 kvm_x86_ops
->cache_regs(vcpu
);
3053 regs
->rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
3054 regs
->rbx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
3055 regs
->rcx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
3056 regs
->rdx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
3057 regs
->rsi
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
3058 regs
->rdi
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
3059 regs
->rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
3060 regs
->rbp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
3061 #ifdef CONFIG_X86_64
3062 regs
->r8
= vcpu
->arch
.regs
[VCPU_REGS_R8
];
3063 regs
->r9
= vcpu
->arch
.regs
[VCPU_REGS_R9
];
3064 regs
->r10
= vcpu
->arch
.regs
[VCPU_REGS_R10
];
3065 regs
->r11
= vcpu
->arch
.regs
[VCPU_REGS_R11
];
3066 regs
->r12
= vcpu
->arch
.regs
[VCPU_REGS_R12
];
3067 regs
->r13
= vcpu
->arch
.regs
[VCPU_REGS_R13
];
3068 regs
->r14
= vcpu
->arch
.regs
[VCPU_REGS_R14
];
3069 regs
->r15
= vcpu
->arch
.regs
[VCPU_REGS_R15
];
3072 regs
->rip
= vcpu
->arch
.rip
;
3073 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
3076 * Don't leak debug flags in case they were set for guest debugging
3078 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
3079 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
3086 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
3090 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = regs
->rax
;
3091 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = regs
->rbx
;
3092 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = regs
->rcx
;
3093 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = regs
->rdx
;
3094 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = regs
->rsi
;
3095 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = regs
->rdi
;
3096 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = regs
->rsp
;
3097 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = regs
->rbp
;
3098 #ifdef CONFIG_X86_64
3099 vcpu
->arch
.regs
[VCPU_REGS_R8
] = regs
->r8
;
3100 vcpu
->arch
.regs
[VCPU_REGS_R9
] = regs
->r9
;
3101 vcpu
->arch
.regs
[VCPU_REGS_R10
] = regs
->r10
;
3102 vcpu
->arch
.regs
[VCPU_REGS_R11
] = regs
->r11
;
3103 vcpu
->arch
.regs
[VCPU_REGS_R12
] = regs
->r12
;
3104 vcpu
->arch
.regs
[VCPU_REGS_R13
] = regs
->r13
;
3105 vcpu
->arch
.regs
[VCPU_REGS_R14
] = regs
->r14
;
3106 vcpu
->arch
.regs
[VCPU_REGS_R15
] = regs
->r15
;
3109 vcpu
->arch
.rip
= regs
->rip
;
3110 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
3112 kvm_x86_ops
->decache_regs(vcpu
);
3114 vcpu
->arch
.exception
.pending
= false;
3121 void kvm_get_segment(struct kvm_vcpu
*vcpu
,
3122 struct kvm_segment
*var
, int seg
)
3124 kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
3127 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
3129 struct kvm_segment cs
;
3131 kvm_get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
3135 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
3137 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
3138 struct kvm_sregs
*sregs
)
3140 struct descriptor_table dt
;
3145 kvm_get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3146 kvm_get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3147 kvm_get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3148 kvm_get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3149 kvm_get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3150 kvm_get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3152 kvm_get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3153 kvm_get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3155 kvm_x86_ops
->get_idt(vcpu
, &dt
);
3156 sregs
->idt
.limit
= dt
.limit
;
3157 sregs
->idt
.base
= dt
.base
;
3158 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
3159 sregs
->gdt
.limit
= dt
.limit
;
3160 sregs
->gdt
.base
= dt
.base
;
3162 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3163 sregs
->cr0
= vcpu
->arch
.cr0
;
3164 sregs
->cr2
= vcpu
->arch
.cr2
;
3165 sregs
->cr3
= vcpu
->arch
.cr3
;
3166 sregs
->cr4
= vcpu
->arch
.cr4
;
3167 sregs
->cr8
= kvm_get_cr8(vcpu
);
3168 sregs
->efer
= vcpu
->arch
.shadow_efer
;
3169 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
3171 if (irqchip_in_kernel(vcpu
->kvm
)) {
3172 memset(sregs
->interrupt_bitmap
, 0,
3173 sizeof sregs
->interrupt_bitmap
);
3174 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
3175 if (pending_vec
>= 0)
3176 set_bit(pending_vec
,
3177 (unsigned long *)sregs
->interrupt_bitmap
);
3179 memcpy(sregs
->interrupt_bitmap
, vcpu
->arch
.irq_pending
,
3180 sizeof sregs
->interrupt_bitmap
);
3187 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
3188 struct kvm_mp_state
*mp_state
)
3191 mp_state
->mp_state
= vcpu
->arch
.mp_state
;
3196 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
3197 struct kvm_mp_state
*mp_state
)
3200 vcpu
->arch
.mp_state
= mp_state
->mp_state
;
3205 static void kvm_set_segment(struct kvm_vcpu
*vcpu
,
3206 struct kvm_segment
*var
, int seg
)
3208 kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
3211 static void seg_desct_to_kvm_desct(struct desc_struct
*seg_desc
, u16 selector
,
3212 struct kvm_segment
*kvm_desct
)
3214 kvm_desct
->base
= seg_desc
->base0
;
3215 kvm_desct
->base
|= seg_desc
->base1
<< 16;
3216 kvm_desct
->base
|= seg_desc
->base2
<< 24;
3217 kvm_desct
->limit
= seg_desc
->limit0
;
3218 kvm_desct
->limit
|= seg_desc
->limit
<< 16;
3220 kvm_desct
->limit
<<= 12;
3221 kvm_desct
->limit
|= 0xfff;
3223 kvm_desct
->selector
= selector
;
3224 kvm_desct
->type
= seg_desc
->type
;
3225 kvm_desct
->present
= seg_desc
->p
;
3226 kvm_desct
->dpl
= seg_desc
->dpl
;
3227 kvm_desct
->db
= seg_desc
->d
;
3228 kvm_desct
->s
= seg_desc
->s
;
3229 kvm_desct
->l
= seg_desc
->l
;
3230 kvm_desct
->g
= seg_desc
->g
;
3231 kvm_desct
->avl
= seg_desc
->avl
;
3233 kvm_desct
->unusable
= 1;
3235 kvm_desct
->unusable
= 0;
3236 kvm_desct
->padding
= 0;
3239 static void get_segment_descritptor_dtable(struct kvm_vcpu
*vcpu
,
3241 struct descriptor_table
*dtable
)
3243 if (selector
& 1 << 2) {
3244 struct kvm_segment kvm_seg
;
3246 kvm_get_segment(vcpu
, &kvm_seg
, VCPU_SREG_LDTR
);
3248 if (kvm_seg
.unusable
)
3251 dtable
->limit
= kvm_seg
.limit
;
3252 dtable
->base
= kvm_seg
.base
;
3255 kvm_x86_ops
->get_gdt(vcpu
, dtable
);
3258 /* allowed just for 8 bytes segments */
3259 static int load_guest_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3260 struct desc_struct
*seg_desc
)
3263 struct descriptor_table dtable
;
3264 u16 index
= selector
>> 3;
3266 get_segment_descritptor_dtable(vcpu
, selector
, &dtable
);
3268 if (dtable
.limit
< index
* 8 + 7) {
3269 kvm_queue_exception_e(vcpu
, GP_VECTOR
, selector
& 0xfffc);
3272 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, dtable
.base
);
3274 return kvm_read_guest(vcpu
->kvm
, gpa
, seg_desc
, 8);
3277 /* allowed just for 8 bytes segments */
3278 static int save_guest_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3279 struct desc_struct
*seg_desc
)
3282 struct descriptor_table dtable
;
3283 u16 index
= selector
>> 3;
3285 get_segment_descritptor_dtable(vcpu
, selector
, &dtable
);
3287 if (dtable
.limit
< index
* 8 + 7)
3289 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, dtable
.base
);
3291 return kvm_write_guest(vcpu
->kvm
, gpa
, seg_desc
, 8);
3294 static u32
get_tss_base_addr(struct kvm_vcpu
*vcpu
,
3295 struct desc_struct
*seg_desc
)
3299 base_addr
= seg_desc
->base0
;
3300 base_addr
|= (seg_desc
->base1
<< 16);
3301 base_addr
|= (seg_desc
->base2
<< 24);
3303 return vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, base_addr
);
3306 static u16
get_segment_selector(struct kvm_vcpu
*vcpu
, int seg
)
3308 struct kvm_segment kvm_seg
;
3310 kvm_get_segment(vcpu
, &kvm_seg
, seg
);
3311 return kvm_seg
.selector
;
3314 static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu
*vcpu
,
3316 struct kvm_segment
*kvm_seg
)
3318 struct desc_struct seg_desc
;
3320 if (load_guest_segment_descriptor(vcpu
, selector
, &seg_desc
))
3322 seg_desct_to_kvm_desct(&seg_desc
, selector
, kvm_seg
);
3326 int kvm_load_realmode_segment(struct kvm_vcpu
*vcpu
, u16 selector
, int seg
)
3328 struct kvm_segment segvar
= {
3329 .base
= selector
<< 4,
3331 .selector
= selector
,
3342 kvm_x86_ops
->set_segment(vcpu
, &segvar
, seg
);
3346 int kvm_load_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3347 int type_bits
, int seg
)
3349 struct kvm_segment kvm_seg
;
3351 if (!(vcpu
->arch
.cr0
& X86_CR0_PE
))
3352 return kvm_load_realmode_segment(vcpu
, selector
, seg
);
3353 if (load_segment_descriptor_to_kvm_desct(vcpu
, selector
, &kvm_seg
))
3355 kvm_seg
.type
|= type_bits
;
3357 if (seg
!= VCPU_SREG_SS
&& seg
!= VCPU_SREG_CS
&&
3358 seg
!= VCPU_SREG_LDTR
)
3360 kvm_seg
.unusable
= 1;
3362 kvm_set_segment(vcpu
, &kvm_seg
, seg
);
3366 static void save_state_to_tss32(struct kvm_vcpu
*vcpu
,
3367 struct tss_segment_32
*tss
)
3369 tss
->cr3
= vcpu
->arch
.cr3
;
3370 tss
->eip
= vcpu
->arch
.rip
;
3371 tss
->eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3372 tss
->eax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
3373 tss
->ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
3374 tss
->edx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
3375 tss
->ebx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
3376 tss
->esp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
3377 tss
->ebp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
3378 tss
->esi
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
3379 tss
->edi
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
3381 tss
->es
= get_segment_selector(vcpu
, VCPU_SREG_ES
);
3382 tss
->cs
= get_segment_selector(vcpu
, VCPU_SREG_CS
);
3383 tss
->ss
= get_segment_selector(vcpu
, VCPU_SREG_SS
);
3384 tss
->ds
= get_segment_selector(vcpu
, VCPU_SREG_DS
);
3385 tss
->fs
= get_segment_selector(vcpu
, VCPU_SREG_FS
);
3386 tss
->gs
= get_segment_selector(vcpu
, VCPU_SREG_GS
);
3387 tss
->ldt_selector
= get_segment_selector(vcpu
, VCPU_SREG_LDTR
);
3388 tss
->prev_task_link
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
3391 static int load_state_from_tss32(struct kvm_vcpu
*vcpu
,
3392 struct tss_segment_32
*tss
)
3394 kvm_set_cr3(vcpu
, tss
->cr3
);
3396 vcpu
->arch
.rip
= tss
->eip
;
3397 kvm_x86_ops
->set_rflags(vcpu
, tss
->eflags
| 2);
3399 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = tss
->eax
;
3400 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = tss
->ecx
;
3401 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = tss
->edx
;
3402 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = tss
->ebx
;
3403 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = tss
->esp
;
3404 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = tss
->ebp
;
3405 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = tss
->esi
;
3406 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = tss
->edi
;
3408 if (kvm_load_segment_descriptor(vcpu
, tss
->ldt_selector
, 0, VCPU_SREG_LDTR
))
3411 if (kvm_load_segment_descriptor(vcpu
, tss
->es
, 1, VCPU_SREG_ES
))
3414 if (kvm_load_segment_descriptor(vcpu
, tss
->cs
, 9, VCPU_SREG_CS
))
3417 if (kvm_load_segment_descriptor(vcpu
, tss
->ss
, 1, VCPU_SREG_SS
))
3420 if (kvm_load_segment_descriptor(vcpu
, tss
->ds
, 1, VCPU_SREG_DS
))
3423 if (kvm_load_segment_descriptor(vcpu
, tss
->fs
, 1, VCPU_SREG_FS
))
3426 if (kvm_load_segment_descriptor(vcpu
, tss
->gs
, 1, VCPU_SREG_GS
))
3431 static void save_state_to_tss16(struct kvm_vcpu
*vcpu
,
3432 struct tss_segment_16
*tss
)
3434 tss
->ip
= vcpu
->arch
.rip
;
3435 tss
->flag
= kvm_x86_ops
->get_rflags(vcpu
);
3436 tss
->ax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
3437 tss
->cx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
3438 tss
->dx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
3439 tss
->bx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
3440 tss
->sp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
3441 tss
->bp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
3442 tss
->si
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
3443 tss
->di
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
3445 tss
->es
= get_segment_selector(vcpu
, VCPU_SREG_ES
);
3446 tss
->cs
= get_segment_selector(vcpu
, VCPU_SREG_CS
);
3447 tss
->ss
= get_segment_selector(vcpu
, VCPU_SREG_SS
);
3448 tss
->ds
= get_segment_selector(vcpu
, VCPU_SREG_DS
);
3449 tss
->ldt
= get_segment_selector(vcpu
, VCPU_SREG_LDTR
);
3450 tss
->prev_task_link
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
3453 static int load_state_from_tss16(struct kvm_vcpu
*vcpu
,
3454 struct tss_segment_16
*tss
)
3456 vcpu
->arch
.rip
= tss
->ip
;
3457 kvm_x86_ops
->set_rflags(vcpu
, tss
->flag
| 2);
3458 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = tss
->ax
;
3459 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = tss
->cx
;
3460 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = tss
->dx
;
3461 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = tss
->bx
;
3462 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = tss
->sp
;
3463 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = tss
->bp
;
3464 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = tss
->si
;
3465 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = tss
->di
;
3467 if (kvm_load_segment_descriptor(vcpu
, tss
->ldt
, 0, VCPU_SREG_LDTR
))
3470 if (kvm_load_segment_descriptor(vcpu
, tss
->es
, 1, VCPU_SREG_ES
))
3473 if (kvm_load_segment_descriptor(vcpu
, tss
->cs
, 9, VCPU_SREG_CS
))
3476 if (kvm_load_segment_descriptor(vcpu
, tss
->ss
, 1, VCPU_SREG_SS
))
3479 if (kvm_load_segment_descriptor(vcpu
, tss
->ds
, 1, VCPU_SREG_DS
))
3484 static int kvm_task_switch_16(struct kvm_vcpu
*vcpu
, u16 tss_selector
,
3486 struct desc_struct
*nseg_desc
)
3488 struct tss_segment_16 tss_segment_16
;
3491 if (kvm_read_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_16
,
3492 sizeof tss_segment_16
))
3495 save_state_to_tss16(vcpu
, &tss_segment_16
);
3497 if (kvm_write_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_16
,
3498 sizeof tss_segment_16
))
3501 if (kvm_read_guest(vcpu
->kvm
, get_tss_base_addr(vcpu
, nseg_desc
),
3502 &tss_segment_16
, sizeof tss_segment_16
))
3505 if (load_state_from_tss16(vcpu
, &tss_segment_16
))
3513 static int kvm_task_switch_32(struct kvm_vcpu
*vcpu
, u16 tss_selector
,
3515 struct desc_struct
*nseg_desc
)
3517 struct tss_segment_32 tss_segment_32
;
3520 if (kvm_read_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_32
,
3521 sizeof tss_segment_32
))
3524 save_state_to_tss32(vcpu
, &tss_segment_32
);
3526 if (kvm_write_guest(vcpu
->kvm
, old_tss_base
, &tss_segment_32
,
3527 sizeof tss_segment_32
))
3530 if (kvm_read_guest(vcpu
->kvm
, get_tss_base_addr(vcpu
, nseg_desc
),
3531 &tss_segment_32
, sizeof tss_segment_32
))
3534 if (load_state_from_tss32(vcpu
, &tss_segment_32
))
3542 int kvm_task_switch(struct kvm_vcpu
*vcpu
, u16 tss_selector
, int reason
)
3544 struct kvm_segment tr_seg
;
3545 struct desc_struct cseg_desc
;
3546 struct desc_struct nseg_desc
;
3548 u32 old_tss_base
= get_segment_base(vcpu
, VCPU_SREG_TR
);
3549 u16 old_tss_sel
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
3551 old_tss_base
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, old_tss_base
);
3553 /* FIXME: Handle errors. Failure to read either TSS or their
3554 * descriptors should generate a pagefault.
3556 if (load_guest_segment_descriptor(vcpu
, tss_selector
, &nseg_desc
))
3559 if (load_guest_segment_descriptor(vcpu
, old_tss_sel
, &cseg_desc
))
3562 if (reason
!= TASK_SWITCH_IRET
) {
3565 cpl
= kvm_x86_ops
->get_cpl(vcpu
);
3566 if ((tss_selector
& 3) > nseg_desc
.dpl
|| cpl
> nseg_desc
.dpl
) {
3567 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
3572 if (!nseg_desc
.p
|| (nseg_desc
.limit0
| nseg_desc
.limit
<< 16) < 0x67) {
3573 kvm_queue_exception_e(vcpu
, TS_VECTOR
, tss_selector
& 0xfffc);
3577 if (reason
== TASK_SWITCH_IRET
|| reason
== TASK_SWITCH_JMP
) {
3578 cseg_desc
.type
&= ~(1 << 1); //clear the B flag
3579 save_guest_segment_descriptor(vcpu
, old_tss_sel
, &cseg_desc
);
3582 if (reason
== TASK_SWITCH_IRET
) {
3583 u32 eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3584 kvm_x86_ops
->set_rflags(vcpu
, eflags
& ~X86_EFLAGS_NT
);
3587 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
3588 kvm_x86_ops
->cache_regs(vcpu
);
3590 if (nseg_desc
.type
& 8)
3591 ret
= kvm_task_switch_32(vcpu
, tss_selector
, old_tss_base
,
3594 ret
= kvm_task_switch_16(vcpu
, tss_selector
, old_tss_base
,
3597 if (reason
== TASK_SWITCH_CALL
|| reason
== TASK_SWITCH_GATE
) {
3598 u32 eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3599 kvm_x86_ops
->set_rflags(vcpu
, eflags
| X86_EFLAGS_NT
);
3602 if (reason
!= TASK_SWITCH_IRET
) {
3603 nseg_desc
.type
|= (1 << 1);
3604 save_guest_segment_descriptor(vcpu
, tss_selector
,
3608 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
| X86_CR0_TS
);
3609 seg_desct_to_kvm_desct(&nseg_desc
, tss_selector
, &tr_seg
);
3611 kvm_set_segment(vcpu
, &tr_seg
, VCPU_SREG_TR
);
3613 kvm_x86_ops
->decache_regs(vcpu
);
3616 EXPORT_SYMBOL_GPL(kvm_task_switch
);
3618 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
3619 struct kvm_sregs
*sregs
)
3621 int mmu_reset_needed
= 0;
3622 int i
, pending_vec
, max_bits
;
3623 struct descriptor_table dt
;
3627 dt
.limit
= sregs
->idt
.limit
;
3628 dt
.base
= sregs
->idt
.base
;
3629 kvm_x86_ops
->set_idt(vcpu
, &dt
);
3630 dt
.limit
= sregs
->gdt
.limit
;
3631 dt
.base
= sregs
->gdt
.base
;
3632 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
3634 vcpu
->arch
.cr2
= sregs
->cr2
;
3635 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
3636 vcpu
->arch
.cr3
= sregs
->cr3
;
3638 kvm_set_cr8(vcpu
, sregs
->cr8
);
3640 mmu_reset_needed
|= vcpu
->arch
.shadow_efer
!= sregs
->efer
;
3641 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
3642 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
3644 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3646 mmu_reset_needed
|= vcpu
->arch
.cr0
!= sregs
->cr0
;
3647 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
3648 vcpu
->arch
.cr0
= sregs
->cr0
;
3650 mmu_reset_needed
|= vcpu
->arch
.cr4
!= sregs
->cr4
;
3651 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
3652 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
3653 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
3655 if (mmu_reset_needed
)
3656 kvm_mmu_reset_context(vcpu
);
3658 if (!irqchip_in_kernel(vcpu
->kvm
)) {
3659 memcpy(vcpu
->arch
.irq_pending
, sregs
->interrupt_bitmap
,
3660 sizeof vcpu
->arch
.irq_pending
);
3661 vcpu
->arch
.irq_summary
= 0;
3662 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.irq_pending
); ++i
)
3663 if (vcpu
->arch
.irq_pending
[i
])
3664 __set_bit(i
, &vcpu
->arch
.irq_summary
);
3666 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
3667 pending_vec
= find_first_bit(
3668 (const unsigned long *)sregs
->interrupt_bitmap
,
3670 /* Only pending external irq is handled here */
3671 if (pending_vec
< max_bits
) {
3672 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
3673 pr_debug("Set back pending irq %d\n",
3678 kvm_set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3679 kvm_set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3680 kvm_set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3681 kvm_set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3682 kvm_set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3683 kvm_set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3685 kvm_set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3686 kvm_set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3693 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
3694 struct kvm_debug_guest
*dbg
)
3700 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
3708 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3709 * we have asm/x86/processor.h
3720 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3721 #ifdef CONFIG_X86_64
3722 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3724 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3729 * Translate a guest virtual address to a guest physical address.
3731 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
3732 struct kvm_translation
*tr
)
3734 unsigned long vaddr
= tr
->linear_address
;
3738 down_read(&vcpu
->kvm
->slots_lock
);
3739 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, vaddr
);
3740 up_read(&vcpu
->kvm
->slots_lock
);
3741 tr
->physical_address
= gpa
;
3742 tr
->valid
= gpa
!= UNMAPPED_GVA
;
3750 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3752 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3756 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
3757 fpu
->fcw
= fxsave
->cwd
;
3758 fpu
->fsw
= fxsave
->swd
;
3759 fpu
->ftwx
= fxsave
->twd
;
3760 fpu
->last_opcode
= fxsave
->fop
;
3761 fpu
->last_ip
= fxsave
->rip
;
3762 fpu
->last_dp
= fxsave
->rdp
;
3763 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
3770 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3772 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3776 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
3777 fxsave
->cwd
= fpu
->fcw
;
3778 fxsave
->swd
= fpu
->fsw
;
3779 fxsave
->twd
= fpu
->ftwx
;
3780 fxsave
->fop
= fpu
->last_opcode
;
3781 fxsave
->rip
= fpu
->last_ip
;
3782 fxsave
->rdp
= fpu
->last_dp
;
3783 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
3790 void fx_init(struct kvm_vcpu
*vcpu
)
3792 unsigned after_mxcsr_mask
;
3795 * Touch the fpu the first time in non atomic context as if
3796 * this is the first fpu instruction the exception handler
3797 * will fire before the instruction returns and it'll have to
3798 * allocate ram with GFP_KERNEL.
3801 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
3803 /* Initialize guest FPU by resetting ours and saving into guest's */
3805 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
3807 kvm_fx_save(&vcpu
->arch
.guest_fx_image
);
3808 kvm_fx_restore(&vcpu
->arch
.host_fx_image
);
3811 vcpu
->arch
.cr0
|= X86_CR0_ET
;
3812 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
3813 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
3814 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
3815 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
3817 EXPORT_SYMBOL_GPL(fx_init
);
3819 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
3821 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
3824 vcpu
->guest_fpu_loaded
= 1;
3825 kvm_fx_save(&vcpu
->arch
.host_fx_image
);
3826 kvm_fx_restore(&vcpu
->arch
.guest_fx_image
);
3828 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
3830 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
3832 if (!vcpu
->guest_fpu_loaded
)
3835 vcpu
->guest_fpu_loaded
= 0;
3836 kvm_fx_save(&vcpu
->arch
.guest_fx_image
);
3837 kvm_fx_restore(&vcpu
->arch
.host_fx_image
);
3838 ++vcpu
->stat
.fpu_reload
;
3840 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
3842 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
3844 kvm_x86_ops
->vcpu_free(vcpu
);
3847 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
3850 return kvm_x86_ops
->vcpu_create(kvm
, id
);
3853 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
3857 /* We do fxsave: this must be aligned. */
3858 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
3861 r
= kvm_arch_vcpu_reset(vcpu
);
3863 r
= kvm_mmu_setup(vcpu
);
3870 kvm_x86_ops
->vcpu_free(vcpu
);
3874 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
3877 kvm_mmu_unload(vcpu
);
3880 kvm_x86_ops
->vcpu_free(vcpu
);
3883 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
3885 return kvm_x86_ops
->vcpu_reset(vcpu
);
3888 void kvm_arch_hardware_enable(void *garbage
)
3890 kvm_x86_ops
->hardware_enable(garbage
);
3893 void kvm_arch_hardware_disable(void *garbage
)
3895 kvm_x86_ops
->hardware_disable(garbage
);
3898 int kvm_arch_hardware_setup(void)
3900 return kvm_x86_ops
->hardware_setup();
3903 void kvm_arch_hardware_unsetup(void)
3905 kvm_x86_ops
->hardware_unsetup();
3908 void kvm_arch_check_processor_compat(void *rtn
)
3910 kvm_x86_ops
->check_processor_compatibility(rtn
);
3913 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
3919 BUG_ON(vcpu
->kvm
== NULL
);
3922 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
3923 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
3924 vcpu
->arch
.mp_state
= KVM_MP_STATE_RUNNABLE
;
3926 vcpu
->arch
.mp_state
= KVM_MP_STATE_UNINITIALIZED
;
3928 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
3933 vcpu
->arch
.pio_data
= page_address(page
);
3935 r
= kvm_mmu_create(vcpu
);
3937 goto fail_free_pio_data
;
3939 if (irqchip_in_kernel(kvm
)) {
3940 r
= kvm_create_lapic(vcpu
);
3942 goto fail_mmu_destroy
;
3948 kvm_mmu_destroy(vcpu
);
3950 free_page((unsigned long)vcpu
->arch
.pio_data
);
3955 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
3957 kvm_free_lapic(vcpu
);
3958 down_read(&vcpu
->kvm
->slots_lock
);
3959 kvm_mmu_destroy(vcpu
);
3960 up_read(&vcpu
->kvm
->slots_lock
);
3961 free_page((unsigned long)vcpu
->arch
.pio_data
);
3964 struct kvm
*kvm_arch_create_vm(void)
3966 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
3969 return ERR_PTR(-ENOMEM
);
3971 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
3976 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
3979 kvm_mmu_unload(vcpu
);
3983 static void kvm_free_vcpus(struct kvm
*kvm
)
3988 * Unpin any mmu pages first.
3990 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
3992 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
3993 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3994 if (kvm
->vcpus
[i
]) {
3995 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
3996 kvm
->vcpus
[i
] = NULL
;
4002 void kvm_arch_destroy_vm(struct kvm
*kvm
)
4005 kfree(kvm
->arch
.vpic
);
4006 kfree(kvm
->arch
.vioapic
);
4007 kvm_free_vcpus(kvm
);
4008 kvm_free_physmem(kvm
);
4009 if (kvm
->arch
.apic_access_page
)
4010 put_page(kvm
->arch
.apic_access_page
);
4011 if (kvm
->arch
.ept_identity_pagetable
)
4012 put_page(kvm
->arch
.ept_identity_pagetable
);
4016 int kvm_arch_set_memory_region(struct kvm
*kvm
,
4017 struct kvm_userspace_memory_region
*mem
,
4018 struct kvm_memory_slot old
,
4021 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
4022 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
4024 /*To keep backward compatibility with older userspace,
4025 *x86 needs to hanlde !user_alloc case.
4028 if (npages
&& !old
.rmap
) {
4029 unsigned long userspace_addr
;
4031 down_write(¤t
->mm
->mmap_sem
);
4032 userspace_addr
= do_mmap(NULL
, 0,
4034 PROT_READ
| PROT_WRITE
,
4035 MAP_PRIVATE
| MAP_ANONYMOUS
,
4037 up_write(¤t
->mm
->mmap_sem
);
4039 if (IS_ERR((void *)userspace_addr
))
4040 return PTR_ERR((void *)userspace_addr
);
4042 /* set userspace_addr atomically for kvm_hva_to_rmapp */
4043 spin_lock(&kvm
->mmu_lock
);
4044 memslot
->userspace_addr
= userspace_addr
;
4045 spin_unlock(&kvm
->mmu_lock
);
4047 if (!old
.user_alloc
&& old
.rmap
) {
4050 down_write(¤t
->mm
->mmap_sem
);
4051 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
4052 old
.npages
* PAGE_SIZE
);
4053 up_write(¤t
->mm
->mmap_sem
);
4056 "kvm_vm_ioctl_set_memory_region: "
4057 "failed to munmap memory\n");
4062 if (!kvm
->arch
.n_requested_mmu_pages
) {
4063 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
4064 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
4067 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
4068 kvm_flush_remote_tlbs(kvm
);
4073 void kvm_arch_flush_shadow(struct kvm
*kvm
)
4075 kvm_mmu_zap_all(kvm
);
4076 kvm_reload_remote_mmus(kvm
);
4079 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
4081 return vcpu
->arch
.mp_state
== KVM_MP_STATE_RUNNABLE
4082 || vcpu
->arch
.mp_state
== KVM_MP_STATE_SIPI_RECEIVED
;
4085 static void vcpu_kick_intr(void *info
)
4088 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)info
;
4089 printk(KERN_DEBUG
"vcpu_kick_intr %p \n", vcpu
);
4093 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
4095 int ipi_pcpu
= vcpu
->cpu
;
4096 int cpu
= get_cpu();
4098 if (waitqueue_active(&vcpu
->wq
)) {
4099 wake_up_interruptible(&vcpu
->wq
);
4100 ++vcpu
->stat
.halt_wakeup
;
4103 * We may be called synchronously with irqs disabled in guest mode,
4104 * So need not to call smp_call_function_single() in that case.
4106 if (vcpu
->guest_mode
&& vcpu
->cpu
!= cpu
)
4107 smp_call_function_single(ipi_pcpu
, vcpu_kick_intr
, vcpu
, 0);