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 { "halt_exits", VCPU_STAT(halt_exits
) },
76 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
77 { "hypercalls", VCPU_STAT(hypercalls
) },
78 { "request_irq", VCPU_STAT(request_irq_exits
) },
79 { "irq_exits", VCPU_STAT(irq_exits
) },
80 { "host_state_reload", VCPU_STAT(host_state_reload
) },
81 { "efer_reload", VCPU_STAT(efer_reload
) },
82 { "fpu_reload", VCPU_STAT(fpu_reload
) },
83 { "insn_emulation", VCPU_STAT(insn_emulation
) },
84 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
85 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
86 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
87 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
88 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
89 { "mmu_flooded", VM_STAT(mmu_flooded
) },
90 { "mmu_recycled", VM_STAT(mmu_recycled
) },
91 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
92 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
93 { "largepages", VM_STAT(lpages
) },
98 unsigned long segment_base(u16 selector
)
100 struct descriptor_table gdt
;
101 struct desc_struct
*d
;
102 unsigned long table_base
;
108 asm("sgdt %0" : "=m"(gdt
));
109 table_base
= gdt
.base
;
111 if (selector
& 4) { /* from ldt */
114 asm("sldt %0" : "=g"(ldt_selector
));
115 table_base
= segment_base(ldt_selector
);
117 d
= (struct desc_struct
*)(table_base
+ (selector
& ~7));
118 v
= d
->base0
| ((unsigned long)d
->base1
<< 16) |
119 ((unsigned long)d
->base2
<< 24);
121 if (d
->s
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
122 v
|= ((unsigned long)((struct ldttss_desc64
*)d
)->base3
) << 32;
126 EXPORT_SYMBOL_GPL(segment_base
);
128 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
130 if (irqchip_in_kernel(vcpu
->kvm
))
131 return vcpu
->arch
.apic_base
;
133 return vcpu
->arch
.apic_base
;
135 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
137 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
139 /* TODO: reserve bits check */
140 if (irqchip_in_kernel(vcpu
->kvm
))
141 kvm_lapic_set_base(vcpu
, data
);
143 vcpu
->arch
.apic_base
= data
;
145 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
147 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
149 WARN_ON(vcpu
->arch
.exception
.pending
);
150 vcpu
->arch
.exception
.pending
= true;
151 vcpu
->arch
.exception
.has_error_code
= false;
152 vcpu
->arch
.exception
.nr
= nr
;
154 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
156 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
159 ++vcpu
->stat
.pf_guest
;
160 if (vcpu
->arch
.exception
.pending
) {
161 if (vcpu
->arch
.exception
.nr
== PF_VECTOR
) {
162 printk(KERN_DEBUG
"kvm: inject_page_fault:"
163 " double fault 0x%lx\n", addr
);
164 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
165 vcpu
->arch
.exception
.error_code
= 0;
166 } else if (vcpu
->arch
.exception
.nr
== DF_VECTOR
) {
167 /* triple fault -> shutdown */
168 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
172 vcpu
->arch
.cr2
= addr
;
173 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
176 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
178 WARN_ON(vcpu
->arch
.exception
.pending
);
179 vcpu
->arch
.exception
.pending
= true;
180 vcpu
->arch
.exception
.has_error_code
= true;
181 vcpu
->arch
.exception
.nr
= nr
;
182 vcpu
->arch
.exception
.error_code
= error_code
;
184 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
186 static void __queue_exception(struct kvm_vcpu
*vcpu
)
188 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
189 vcpu
->arch
.exception
.has_error_code
,
190 vcpu
->arch
.exception
.error_code
);
194 * Load the pae pdptrs. Return true is they are all valid.
196 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
198 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
199 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
202 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
204 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
205 offset
* sizeof(u64
), sizeof(pdpte
));
210 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
211 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
218 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
223 EXPORT_SYMBOL_GPL(load_pdptrs
);
225 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
227 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
231 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
234 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
237 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
243 void kvm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
245 if (cr0
& CR0_RESERVED_BITS
) {
246 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
247 cr0
, vcpu
->arch
.cr0
);
248 kvm_inject_gp(vcpu
, 0);
252 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
253 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
254 kvm_inject_gp(vcpu
, 0);
258 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
259 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
260 "and a clear PE flag\n");
261 kvm_inject_gp(vcpu
, 0);
265 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
267 if ((vcpu
->arch
.shadow_efer
& EFER_LME
)) {
271 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
272 "in long mode while PAE is disabled\n");
273 kvm_inject_gp(vcpu
, 0);
276 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
278 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
279 "in long mode while CS.L == 1\n");
280 kvm_inject_gp(vcpu
, 0);
286 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
287 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
289 kvm_inject_gp(vcpu
, 0);
295 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
296 vcpu
->arch
.cr0
= cr0
;
298 kvm_mmu_reset_context(vcpu
);
301 EXPORT_SYMBOL_GPL(kvm_set_cr0
);
303 void kvm_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
305 kvm_set_cr0(vcpu
, (vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f));
307 EXPORT_SYMBOL_GPL(kvm_lmsw
);
309 void kvm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
311 if (cr4
& CR4_RESERVED_BITS
) {
312 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
313 kvm_inject_gp(vcpu
, 0);
317 if (is_long_mode(vcpu
)) {
318 if (!(cr4
& X86_CR4_PAE
)) {
319 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
321 kvm_inject_gp(vcpu
, 0);
324 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
325 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
326 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
327 kvm_inject_gp(vcpu
, 0);
331 if (cr4
& X86_CR4_VMXE
) {
332 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
333 kvm_inject_gp(vcpu
, 0);
336 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
337 vcpu
->arch
.cr4
= cr4
;
338 kvm_mmu_reset_context(vcpu
);
340 EXPORT_SYMBOL_GPL(kvm_set_cr4
);
342 void kvm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
344 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
345 kvm_mmu_flush_tlb(vcpu
);
349 if (is_long_mode(vcpu
)) {
350 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
351 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
352 kvm_inject_gp(vcpu
, 0);
357 if (cr3
& CR3_PAE_RESERVED_BITS
) {
359 "set_cr3: #GP, reserved bits\n");
360 kvm_inject_gp(vcpu
, 0);
363 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
364 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
366 kvm_inject_gp(vcpu
, 0);
371 * We don't check reserved bits in nonpae mode, because
372 * this isn't enforced, and VMware depends on this.
377 * Does the new cr3 value map to physical memory? (Note, we
378 * catch an invalid cr3 even in real-mode, because it would
379 * cause trouble later on when we turn on paging anyway.)
381 * A real CPU would silently accept an invalid cr3 and would
382 * attempt to use it - with largely undefined (and often hard
383 * to debug) behavior on the guest side.
385 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
386 kvm_inject_gp(vcpu
, 0);
388 vcpu
->arch
.cr3
= cr3
;
389 vcpu
->arch
.mmu
.new_cr3(vcpu
);
392 EXPORT_SYMBOL_GPL(kvm_set_cr3
);
394 void kvm_set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
396 if (cr8
& CR8_RESERVED_BITS
) {
397 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
398 kvm_inject_gp(vcpu
, 0);
401 if (irqchip_in_kernel(vcpu
->kvm
))
402 kvm_lapic_set_tpr(vcpu
, cr8
);
404 vcpu
->arch
.cr8
= cr8
;
406 EXPORT_SYMBOL_GPL(kvm_set_cr8
);
408 unsigned long kvm_get_cr8(struct kvm_vcpu
*vcpu
)
410 if (irqchip_in_kernel(vcpu
->kvm
))
411 return kvm_lapic_get_cr8(vcpu
);
413 return vcpu
->arch
.cr8
;
415 EXPORT_SYMBOL_GPL(kvm_get_cr8
);
418 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
419 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
421 * This list is modified at module load time to reflect the
422 * capabilities of the host cpu.
424 static u32 msrs_to_save
[] = {
425 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
428 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
430 MSR_IA32_TIME_STAMP_COUNTER
, MSR_KVM_SYSTEM_TIME
, MSR_KVM_WALL_CLOCK
,
431 MSR_IA32_PERF_STATUS
,
434 static unsigned num_msrs_to_save
;
436 static u32 emulated_msrs
[] = {
437 MSR_IA32_MISC_ENABLE
,
440 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
442 if (efer
& efer_reserved_bits
) {
443 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
445 kvm_inject_gp(vcpu
, 0);
450 && (vcpu
->arch
.shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
451 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
452 kvm_inject_gp(vcpu
, 0);
456 kvm_x86_ops
->set_efer(vcpu
, efer
);
459 efer
|= vcpu
->arch
.shadow_efer
& EFER_LMA
;
461 vcpu
->arch
.shadow_efer
= efer
;
464 void kvm_enable_efer_bits(u64 mask
)
466 efer_reserved_bits
&= ~mask
;
468 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits
);
472 * Writes msr value into into the appropriate "register".
473 * Returns 0 on success, non-0 otherwise.
474 * Assumes vcpu_load() was already called.
476 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
478 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
482 * Adapt set_msr() to msr_io()'s calling convention
484 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
486 return kvm_set_msr(vcpu
, index
, *data
);
489 static void kvm_write_wall_clock(struct kvm
*kvm
, gpa_t wall_clock
)
492 struct kvm_wall_clock wc
;
493 struct timespec wc_ts
;
500 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
502 wc_ts
= current_kernel_time();
503 wc
.wc_sec
= wc_ts
.tv_sec
;
504 wc
.wc_nsec
= wc_ts
.tv_nsec
;
505 wc
.wc_version
= version
;
507 kvm_write_guest(kvm
, wall_clock
, &wc
, sizeof(wc
));
510 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
513 static void kvm_write_guest_time(struct kvm_vcpu
*v
)
517 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
520 if ((!vcpu
->time_page
))
523 /* Keep irq disabled to prevent changes to the clock */
524 local_irq_save(flags
);
525 kvm_get_msr(v
, MSR_IA32_TIME_STAMP_COUNTER
,
526 &vcpu
->hv_clock
.tsc_timestamp
);
528 local_irq_restore(flags
);
530 /* With all the info we got, fill in the values */
532 vcpu
->hv_clock
.system_time
= ts
.tv_nsec
+
533 (NSEC_PER_SEC
* (u64
)ts
.tv_sec
);
535 * The interface expects us to write an even number signaling that the
536 * update is finished. Since the guest won't see the intermediate
537 * state, we just write "2" at the end
539 vcpu
->hv_clock
.version
= 2;
541 shared_kaddr
= kmap_atomic(vcpu
->time_page
, KM_USER0
);
543 memcpy(shared_kaddr
+ vcpu
->time_offset
, &vcpu
->hv_clock
,
544 sizeof(vcpu
->hv_clock
));
546 kunmap_atomic(shared_kaddr
, KM_USER0
);
548 mark_page_dirty(v
->kvm
, vcpu
->time
>> PAGE_SHIFT
);
552 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
556 set_efer(vcpu
, data
);
558 case MSR_IA32_MC0_STATUS
:
559 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
562 case MSR_IA32_MCG_STATUS
:
563 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
566 case MSR_IA32_MCG_CTL
:
567 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
570 case MSR_IA32_UCODE_REV
:
571 case MSR_IA32_UCODE_WRITE
:
572 case 0x200 ... 0x2ff: /* MTRRs */
574 case MSR_IA32_APICBASE
:
575 kvm_set_apic_base(vcpu
, data
);
577 case MSR_IA32_MISC_ENABLE
:
578 vcpu
->arch
.ia32_misc_enable_msr
= data
;
580 case MSR_KVM_WALL_CLOCK
:
581 vcpu
->kvm
->arch
.wall_clock
= data
;
582 kvm_write_wall_clock(vcpu
->kvm
, data
);
584 case MSR_KVM_SYSTEM_TIME
: {
585 if (vcpu
->arch
.time_page
) {
586 kvm_release_page_dirty(vcpu
->arch
.time_page
);
587 vcpu
->arch
.time_page
= NULL
;
590 vcpu
->arch
.time
= data
;
592 /* we verify if the enable bit is set... */
596 /* ...but clean it before doing the actual write */
597 vcpu
->arch
.time_offset
= data
& ~(PAGE_MASK
| 1);
599 vcpu
->arch
.hv_clock
.tsc_to_system_mul
=
600 clocksource_khz2mult(tsc_khz
, 22);
601 vcpu
->arch
.hv_clock
.tsc_shift
= 22;
603 down_read(¤t
->mm
->mmap_sem
);
604 vcpu
->arch
.time_page
=
605 gfn_to_page(vcpu
->kvm
, data
>> PAGE_SHIFT
);
606 up_read(¤t
->mm
->mmap_sem
);
608 if (is_error_page(vcpu
->arch
.time_page
)) {
609 kvm_release_page_clean(vcpu
->arch
.time_page
);
610 vcpu
->arch
.time_page
= NULL
;
613 kvm_write_guest_time(vcpu
);
617 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n", msr
, data
);
622 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
626 * Reads an msr value (of 'msr_index') into 'pdata'.
627 * Returns 0 on success, non-0 otherwise.
628 * Assumes vcpu_load() was already called.
630 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
632 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
635 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
640 case 0xc0010010: /* SYSCFG */
641 case 0xc0010015: /* HWCR */
642 case MSR_IA32_PLATFORM_ID
:
643 case MSR_IA32_P5_MC_ADDR
:
644 case MSR_IA32_P5_MC_TYPE
:
645 case MSR_IA32_MC0_CTL
:
646 case MSR_IA32_MCG_STATUS
:
647 case MSR_IA32_MCG_CAP
:
648 case MSR_IA32_MCG_CTL
:
649 case MSR_IA32_MC0_MISC
:
650 case MSR_IA32_MC0_MISC
+4:
651 case MSR_IA32_MC0_MISC
+8:
652 case MSR_IA32_MC0_MISC
+12:
653 case MSR_IA32_MC0_MISC
+16:
654 case MSR_IA32_UCODE_REV
:
655 case MSR_IA32_EBL_CR_POWERON
:
658 case 0x200 ... 0x2ff:
661 case 0xcd: /* fsb frequency */
664 case MSR_IA32_APICBASE
:
665 data
= kvm_get_apic_base(vcpu
);
667 case MSR_IA32_MISC_ENABLE
:
668 data
= vcpu
->arch
.ia32_misc_enable_msr
;
670 case MSR_IA32_PERF_STATUS
:
671 /* TSC increment by tick */
674 data
|= (((uint64_t)4ULL) << 40);
677 data
= vcpu
->arch
.shadow_efer
;
679 case MSR_KVM_WALL_CLOCK
:
680 data
= vcpu
->kvm
->arch
.wall_clock
;
682 case MSR_KVM_SYSTEM_TIME
:
683 data
= vcpu
->arch
.time
;
686 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
692 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
695 * Read or write a bunch of msrs. All parameters are kernel addresses.
697 * @return number of msrs set successfully.
699 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
700 struct kvm_msr_entry
*entries
,
701 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
702 unsigned index
, u64
*data
))
708 down_read(&vcpu
->kvm
->slots_lock
);
709 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
710 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
712 up_read(&vcpu
->kvm
->slots_lock
);
720 * Read or write a bunch of msrs. Parameters are user addresses.
722 * @return number of msrs set successfully.
724 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
725 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
726 unsigned index
, u64
*data
),
729 struct kvm_msrs msrs
;
730 struct kvm_msr_entry
*entries
;
735 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
739 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
743 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
744 entries
= vmalloc(size
);
749 if (copy_from_user(entries
, user_msrs
->entries
, size
))
752 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
757 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
769 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
772 void decache_vcpus_on_cpu(int cpu
)
775 struct kvm_vcpu
*vcpu
;
778 spin_lock(&kvm_lock
);
779 list_for_each_entry(vm
, &vm_list
, vm_list
)
780 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
785 * If the vcpu is locked, then it is running on some
786 * other cpu and therefore it is not cached on the
789 * If it's not locked, check the last cpu it executed
792 if (mutex_trylock(&vcpu
->mutex
)) {
793 if (vcpu
->cpu
== cpu
) {
794 kvm_x86_ops
->vcpu_decache(vcpu
);
797 mutex_unlock(&vcpu
->mutex
);
800 spin_unlock(&kvm_lock
);
803 int kvm_dev_ioctl_check_extension(long ext
)
808 case KVM_CAP_IRQCHIP
:
810 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
811 case KVM_CAP_USER_MEMORY
:
812 case KVM_CAP_SET_TSS_ADDR
:
813 case KVM_CAP_EXT_CPUID
:
814 case KVM_CAP_CLOCKSOURCE
:
816 case KVM_CAP_NOP_IO_DELAY
:
820 r
= !kvm_x86_ops
->cpu_has_accelerated_tpr();
822 case KVM_CAP_NR_VCPUS
:
825 case KVM_CAP_NR_MEMSLOTS
:
826 r
= KVM_MEMORY_SLOTS
;
839 long kvm_arch_dev_ioctl(struct file
*filp
,
840 unsigned int ioctl
, unsigned long arg
)
842 void __user
*argp
= (void __user
*)arg
;
846 case KVM_GET_MSR_INDEX_LIST
: {
847 struct kvm_msr_list __user
*user_msr_list
= argp
;
848 struct kvm_msr_list msr_list
;
852 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
855 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
856 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
859 if (n
< num_msrs_to_save
)
862 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
863 num_msrs_to_save
* sizeof(u32
)))
865 if (copy_to_user(user_msr_list
->indices
866 + num_msrs_to_save
* sizeof(u32
),
868 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
873 case KVM_GET_SUPPORTED_CPUID
: {
874 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
875 struct kvm_cpuid2 cpuid
;
878 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
880 r
= kvm_dev_ioctl_get_supported_cpuid(&cpuid
,
886 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
898 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
900 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
901 kvm_write_guest_time(vcpu
);
904 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
906 kvm_x86_ops
->vcpu_put(vcpu
);
907 kvm_put_guest_fpu(vcpu
);
910 static int is_efer_nx(void)
914 rdmsrl(MSR_EFER
, efer
);
915 return efer
& EFER_NX
;
918 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
921 struct kvm_cpuid_entry2
*e
, *entry
;
924 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
925 e
= &vcpu
->arch
.cpuid_entries
[i
];
926 if (e
->function
== 0x80000001) {
931 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
932 entry
->edx
&= ~(1 << 20);
933 printk(KERN_INFO
"kvm: guest NX capability removed\n");
937 /* when an old userspace process fills a new kernel module */
938 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
939 struct kvm_cpuid
*cpuid
,
940 struct kvm_cpuid_entry __user
*entries
)
943 struct kvm_cpuid_entry
*cpuid_entries
;
946 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
949 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
953 if (copy_from_user(cpuid_entries
, entries
,
954 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
956 for (i
= 0; i
< cpuid
->nent
; i
++) {
957 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
958 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
959 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
960 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
961 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
962 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
963 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
964 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
965 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
966 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
968 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
969 cpuid_fix_nx_cap(vcpu
);
973 vfree(cpuid_entries
);
978 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
979 struct kvm_cpuid2
*cpuid
,
980 struct kvm_cpuid_entry2 __user
*entries
)
985 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
988 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
989 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
991 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
998 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
999 struct kvm_cpuid2
*cpuid
,
1000 struct kvm_cpuid_entry2 __user
*entries
)
1005 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
1008 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
1009 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
1014 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
1018 static inline u32
bit(int bitno
)
1020 return 1 << (bitno
& 31);
1023 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1026 entry
->function
= function
;
1027 entry
->index
= index
;
1028 cpuid_count(entry
->function
, entry
->index
,
1029 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
1033 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1034 u32 index
, int *nent
, int maxnent
)
1036 const u32 kvm_supported_word0_x86_features
= bit(X86_FEATURE_FPU
) |
1037 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1038 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1039 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1040 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1041 bit(X86_FEATURE_SEP
) | bit(X86_FEATURE_PGE
) |
1042 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1043 bit(X86_FEATURE_CLFLSH
) | bit(X86_FEATURE_MMX
) |
1044 bit(X86_FEATURE_FXSR
) | bit(X86_FEATURE_XMM
) |
1045 bit(X86_FEATURE_XMM2
) | bit(X86_FEATURE_SELFSNOOP
);
1046 const u32 kvm_supported_word1_x86_features
= bit(X86_FEATURE_FPU
) |
1047 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1048 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1049 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1050 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1051 bit(X86_FEATURE_PGE
) |
1052 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1053 bit(X86_FEATURE_MMX
) | bit(X86_FEATURE_FXSR
) |
1054 bit(X86_FEATURE_SYSCALL
) |
1055 (bit(X86_FEATURE_NX
) && is_efer_nx()) |
1056 #ifdef CONFIG_X86_64
1057 bit(X86_FEATURE_LM
) |
1059 bit(X86_FEATURE_MMXEXT
) |
1060 bit(X86_FEATURE_3DNOWEXT
) |
1061 bit(X86_FEATURE_3DNOW
);
1062 const u32 kvm_supported_word3_x86_features
=
1063 bit(X86_FEATURE_XMM3
) | bit(X86_FEATURE_CX16
);
1064 const u32 kvm_supported_word6_x86_features
=
1065 bit(X86_FEATURE_LAHF_LM
) | bit(X86_FEATURE_CMP_LEGACY
);
1067 /* all func 2 cpuid_count() should be called on the same cpu */
1069 do_cpuid_1_ent(entry
, function
, index
);
1074 entry
->eax
= min(entry
->eax
, (u32
)0xb);
1077 entry
->edx
&= kvm_supported_word0_x86_features
;
1078 entry
->ecx
&= kvm_supported_word3_x86_features
;
1080 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1081 * may return different values. This forces us to get_cpu() before
1082 * issuing the first command, and also to emulate this annoying behavior
1083 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1085 int t
, times
= entry
->eax
& 0xff;
1087 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1088 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
1089 do_cpuid_1_ent(&entry
[t
], function
, 0);
1090 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1095 /* function 4 and 0xb have additional index. */
1099 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1100 /* read more entries until cache_type is zero */
1101 for (i
= 1; *nent
< maxnent
; ++i
) {
1102 cache_type
= entry
[i
- 1].eax
& 0x1f;
1105 do_cpuid_1_ent(&entry
[i
], function
, i
);
1107 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1115 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1116 /* read more entries until level_type is zero */
1117 for (i
= 1; *nent
< maxnent
; ++i
) {
1118 level_type
= entry
[i
- 1].ecx
& 0xff;
1121 do_cpuid_1_ent(&entry
[i
], function
, i
);
1123 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1129 entry
->eax
= min(entry
->eax
, 0x8000001a);
1132 entry
->edx
&= kvm_supported_word1_x86_features
;
1133 entry
->ecx
&= kvm_supported_word6_x86_features
;
1139 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
1140 struct kvm_cpuid_entry2 __user
*entries
)
1142 struct kvm_cpuid_entry2
*cpuid_entries
;
1143 int limit
, nent
= 0, r
= -E2BIG
;
1146 if (cpuid
->nent
< 1)
1149 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
1153 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
1154 limit
= cpuid_entries
[0].eax
;
1155 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1156 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1157 &nent
, cpuid
->nent
);
1159 if (nent
>= cpuid
->nent
)
1162 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
1163 limit
= cpuid_entries
[nent
- 1].eax
;
1164 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1165 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1166 &nent
, cpuid
->nent
);
1168 if (copy_to_user(entries
, cpuid_entries
,
1169 nent
* sizeof(struct kvm_cpuid_entry2
)))
1175 vfree(cpuid_entries
);
1180 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
1181 struct kvm_lapic_state
*s
)
1184 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
1190 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
1191 struct kvm_lapic_state
*s
)
1194 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1195 kvm_apic_post_state_restore(vcpu
);
1201 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1202 struct kvm_interrupt
*irq
)
1204 if (irq
->irq
< 0 || irq
->irq
>= 256)
1206 if (irqchip_in_kernel(vcpu
->kvm
))
1210 set_bit(irq
->irq
, vcpu
->arch
.irq_pending
);
1211 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
1218 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
1219 struct kvm_tpr_access_ctl
*tac
)
1223 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
1227 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1228 unsigned int ioctl
, unsigned long arg
)
1230 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1231 void __user
*argp
= (void __user
*)arg
;
1235 case KVM_GET_LAPIC
: {
1236 struct kvm_lapic_state lapic
;
1238 memset(&lapic
, 0, sizeof lapic
);
1239 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, &lapic
);
1243 if (copy_to_user(argp
, &lapic
, sizeof lapic
))
1248 case KVM_SET_LAPIC
: {
1249 struct kvm_lapic_state lapic
;
1252 if (copy_from_user(&lapic
, argp
, sizeof lapic
))
1254 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, &lapic
);;
1260 case KVM_INTERRUPT
: {
1261 struct kvm_interrupt irq
;
1264 if (copy_from_user(&irq
, argp
, sizeof irq
))
1266 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1272 case KVM_SET_CPUID
: {
1273 struct kvm_cpuid __user
*cpuid_arg
= argp
;
1274 struct kvm_cpuid cpuid
;
1277 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1279 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
1284 case KVM_SET_CPUID2
: {
1285 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1286 struct kvm_cpuid2 cpuid
;
1289 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1291 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
1292 cpuid_arg
->entries
);
1297 case KVM_GET_CPUID2
: {
1298 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1299 struct kvm_cpuid2 cpuid
;
1302 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1304 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
1305 cpuid_arg
->entries
);
1309 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1315 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
1318 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
1320 case KVM_TPR_ACCESS_REPORTING
: {
1321 struct kvm_tpr_access_ctl tac
;
1324 if (copy_from_user(&tac
, argp
, sizeof tac
))
1326 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
1330 if (copy_to_user(argp
, &tac
, sizeof tac
))
1335 case KVM_SET_VAPIC_ADDR
: {
1336 struct kvm_vapic_addr va
;
1339 if (!irqchip_in_kernel(vcpu
->kvm
))
1342 if (copy_from_user(&va
, argp
, sizeof va
))
1345 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
1355 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
1359 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
1361 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
1365 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
1366 u32 kvm_nr_mmu_pages
)
1368 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
1371 down_write(&kvm
->slots_lock
);
1373 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
1374 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
1376 up_write(&kvm
->slots_lock
);
1380 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
1382 return kvm
->arch
.n_alloc_mmu_pages
;
1385 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1388 struct kvm_mem_alias
*alias
;
1390 for (i
= 0; i
< kvm
->arch
.naliases
; ++i
) {
1391 alias
= &kvm
->arch
.aliases
[i
];
1392 if (gfn
>= alias
->base_gfn
1393 && gfn
< alias
->base_gfn
+ alias
->npages
)
1394 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1400 * Set a new alias region. Aliases map a portion of physical memory into
1401 * another portion. This is useful for memory windows, for example the PC
1404 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
1405 struct kvm_memory_alias
*alias
)
1408 struct kvm_mem_alias
*p
;
1411 /* General sanity checks */
1412 if (alias
->memory_size
& (PAGE_SIZE
- 1))
1414 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
1416 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
1418 if (alias
->guest_phys_addr
+ alias
->memory_size
1419 < alias
->guest_phys_addr
)
1421 if (alias
->target_phys_addr
+ alias
->memory_size
1422 < alias
->target_phys_addr
)
1425 down_write(&kvm
->slots_lock
);
1427 p
= &kvm
->arch
.aliases
[alias
->slot
];
1428 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
1429 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
1430 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
1432 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
1433 if (kvm
->arch
.aliases
[n
- 1].npages
)
1435 kvm
->arch
.naliases
= n
;
1437 kvm_mmu_zap_all(kvm
);
1439 up_write(&kvm
->slots_lock
);
1447 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1452 switch (chip
->chip_id
) {
1453 case KVM_IRQCHIP_PIC_MASTER
:
1454 memcpy(&chip
->chip
.pic
,
1455 &pic_irqchip(kvm
)->pics
[0],
1456 sizeof(struct kvm_pic_state
));
1458 case KVM_IRQCHIP_PIC_SLAVE
:
1459 memcpy(&chip
->chip
.pic
,
1460 &pic_irqchip(kvm
)->pics
[1],
1461 sizeof(struct kvm_pic_state
));
1463 case KVM_IRQCHIP_IOAPIC
:
1464 memcpy(&chip
->chip
.ioapic
,
1465 ioapic_irqchip(kvm
),
1466 sizeof(struct kvm_ioapic_state
));
1475 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1480 switch (chip
->chip_id
) {
1481 case KVM_IRQCHIP_PIC_MASTER
:
1482 memcpy(&pic_irqchip(kvm
)->pics
[0],
1484 sizeof(struct kvm_pic_state
));
1486 case KVM_IRQCHIP_PIC_SLAVE
:
1487 memcpy(&pic_irqchip(kvm
)->pics
[1],
1489 sizeof(struct kvm_pic_state
));
1491 case KVM_IRQCHIP_IOAPIC
:
1492 memcpy(ioapic_irqchip(kvm
),
1494 sizeof(struct kvm_ioapic_state
));
1500 kvm_pic_update_irq(pic_irqchip(kvm
));
1504 static int kvm_vm_ioctl_get_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1508 memcpy(ps
, &kvm
->arch
.vpit
->pit_state
, sizeof(struct kvm_pit_state
));
1512 static int kvm_vm_ioctl_set_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1516 memcpy(&kvm
->arch
.vpit
->pit_state
, ps
, sizeof(struct kvm_pit_state
));
1517 kvm_pit_load_count(kvm
, 0, ps
->channels
[0].count
);
1522 * Get (and clear) the dirty memory log for a memory slot.
1524 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1525 struct kvm_dirty_log
*log
)
1529 struct kvm_memory_slot
*memslot
;
1532 down_write(&kvm
->slots_lock
);
1534 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1538 /* If nothing is dirty, don't bother messing with page tables. */
1540 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
1541 kvm_flush_remote_tlbs(kvm
);
1542 memslot
= &kvm
->memslots
[log
->slot
];
1543 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1544 memset(memslot
->dirty_bitmap
, 0, n
);
1548 up_write(&kvm
->slots_lock
);
1552 long kvm_arch_vm_ioctl(struct file
*filp
,
1553 unsigned int ioctl
, unsigned long arg
)
1555 struct kvm
*kvm
= filp
->private_data
;
1556 void __user
*argp
= (void __user
*)arg
;
1560 case KVM_SET_TSS_ADDR
:
1561 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
1565 case KVM_SET_MEMORY_REGION
: {
1566 struct kvm_memory_region kvm_mem
;
1567 struct kvm_userspace_memory_region kvm_userspace_mem
;
1570 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
1572 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
1573 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
1574 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
1575 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
1576 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1581 case KVM_SET_NR_MMU_PAGES
:
1582 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1586 case KVM_GET_NR_MMU_PAGES
:
1587 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1589 case KVM_SET_MEMORY_ALIAS
: {
1590 struct kvm_memory_alias alias
;
1593 if (copy_from_user(&alias
, argp
, sizeof alias
))
1595 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
1600 case KVM_CREATE_IRQCHIP
:
1602 kvm
->arch
.vpic
= kvm_create_pic(kvm
);
1603 if (kvm
->arch
.vpic
) {
1604 r
= kvm_ioapic_init(kvm
);
1606 kfree(kvm
->arch
.vpic
);
1607 kvm
->arch
.vpic
= NULL
;
1613 case KVM_CREATE_PIT
:
1615 kvm
->arch
.vpit
= kvm_create_pit(kvm
);
1619 case KVM_IRQ_LINE
: {
1620 struct kvm_irq_level irq_event
;
1623 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1625 if (irqchip_in_kernel(kvm
)) {
1626 mutex_lock(&kvm
->lock
);
1627 if (irq_event
.irq
< 16)
1628 kvm_pic_set_irq(pic_irqchip(kvm
),
1631 kvm_ioapic_set_irq(kvm
->arch
.vioapic
,
1634 mutex_unlock(&kvm
->lock
);
1639 case KVM_GET_IRQCHIP
: {
1640 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1641 struct kvm_irqchip chip
;
1644 if (copy_from_user(&chip
, argp
, sizeof chip
))
1647 if (!irqchip_in_kernel(kvm
))
1649 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
1653 if (copy_to_user(argp
, &chip
, sizeof chip
))
1658 case KVM_SET_IRQCHIP
: {
1659 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1660 struct kvm_irqchip chip
;
1663 if (copy_from_user(&chip
, argp
, sizeof chip
))
1666 if (!irqchip_in_kernel(kvm
))
1668 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
1675 struct kvm_pit_state ps
;
1677 if (copy_from_user(&ps
, argp
, sizeof ps
))
1680 if (!kvm
->arch
.vpit
)
1682 r
= kvm_vm_ioctl_get_pit(kvm
, &ps
);
1686 if (copy_to_user(argp
, &ps
, sizeof ps
))
1692 struct kvm_pit_state ps
;
1694 if (copy_from_user(&ps
, argp
, sizeof ps
))
1697 if (!kvm
->arch
.vpit
)
1699 r
= kvm_vm_ioctl_set_pit(kvm
, &ps
);
1712 static void kvm_init_msr_list(void)
1717 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1718 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1721 msrs_to_save
[j
] = msrs_to_save
[i
];
1724 num_msrs_to_save
= j
;
1728 * Only apic need an MMIO device hook, so shortcut now..
1730 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
1733 struct kvm_io_device
*dev
;
1735 if (vcpu
->arch
.apic
) {
1736 dev
= &vcpu
->arch
.apic
->dev
;
1737 if (dev
->in_range(dev
, addr
))
1744 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1747 struct kvm_io_device
*dev
;
1749 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
);
1751 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1755 int emulator_read_std(unsigned long addr
,
1758 struct kvm_vcpu
*vcpu
)
1761 int r
= X86EMUL_CONTINUE
;
1764 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1765 unsigned offset
= addr
& (PAGE_SIZE
-1);
1766 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1769 if (gpa
== UNMAPPED_GVA
) {
1770 r
= X86EMUL_PROPAGATE_FAULT
;
1773 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, tocopy
);
1775 r
= X86EMUL_UNHANDLEABLE
;
1786 EXPORT_SYMBOL_GPL(emulator_read_std
);
1788 static int emulator_read_emulated(unsigned long addr
,
1791 struct kvm_vcpu
*vcpu
)
1793 struct kvm_io_device
*mmio_dev
;
1796 if (vcpu
->mmio_read_completed
) {
1797 memcpy(val
, vcpu
->mmio_data
, bytes
);
1798 vcpu
->mmio_read_completed
= 0;
1799 return X86EMUL_CONTINUE
;
1802 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1804 /* For APIC access vmexit */
1805 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1808 if (emulator_read_std(addr
, val
, bytes
, vcpu
)
1809 == X86EMUL_CONTINUE
)
1810 return X86EMUL_CONTINUE
;
1811 if (gpa
== UNMAPPED_GVA
)
1812 return X86EMUL_PROPAGATE_FAULT
;
1816 * Is this MMIO handled locally?
1818 mutex_lock(&vcpu
->kvm
->lock
);
1819 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1821 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1822 mutex_unlock(&vcpu
->kvm
->lock
);
1823 return X86EMUL_CONTINUE
;
1825 mutex_unlock(&vcpu
->kvm
->lock
);
1827 vcpu
->mmio_needed
= 1;
1828 vcpu
->mmio_phys_addr
= gpa
;
1829 vcpu
->mmio_size
= bytes
;
1830 vcpu
->mmio_is_write
= 0;
1832 return X86EMUL_UNHANDLEABLE
;
1835 int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1836 const void *val
, int bytes
)
1840 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
1843 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1847 static int emulator_write_emulated_onepage(unsigned long addr
,
1850 struct kvm_vcpu
*vcpu
)
1852 struct kvm_io_device
*mmio_dev
;
1855 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1857 if (gpa
== UNMAPPED_GVA
) {
1858 kvm_inject_page_fault(vcpu
, addr
, 2);
1859 return X86EMUL_PROPAGATE_FAULT
;
1862 /* For APIC access vmexit */
1863 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1866 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1867 return X86EMUL_CONTINUE
;
1871 * Is this MMIO handled locally?
1873 mutex_lock(&vcpu
->kvm
->lock
);
1874 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1876 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1877 mutex_unlock(&vcpu
->kvm
->lock
);
1878 return X86EMUL_CONTINUE
;
1880 mutex_unlock(&vcpu
->kvm
->lock
);
1882 vcpu
->mmio_needed
= 1;
1883 vcpu
->mmio_phys_addr
= gpa
;
1884 vcpu
->mmio_size
= bytes
;
1885 vcpu
->mmio_is_write
= 1;
1886 memcpy(vcpu
->mmio_data
, val
, bytes
);
1888 return X86EMUL_CONTINUE
;
1891 int emulator_write_emulated(unsigned long addr
,
1894 struct kvm_vcpu
*vcpu
)
1896 /* Crossing a page boundary? */
1897 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1900 now
= -addr
& ~PAGE_MASK
;
1901 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
1902 if (rc
!= X86EMUL_CONTINUE
)
1908 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
1910 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
1912 static int emulator_cmpxchg_emulated(unsigned long addr
,
1916 struct kvm_vcpu
*vcpu
)
1918 static int reported
;
1922 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1924 #ifndef CONFIG_X86_64
1925 /* guests cmpxchg8b have to be emulated atomically */
1932 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1934 if (gpa
== UNMAPPED_GVA
||
1935 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1938 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
1943 down_read(¤t
->mm
->mmap_sem
);
1944 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1945 up_read(¤t
->mm
->mmap_sem
);
1947 kaddr
= kmap_atomic(page
, KM_USER0
);
1948 set_64bit((u64
*)(kaddr
+ offset_in_page(gpa
)), val
);
1949 kunmap_atomic(kaddr
, KM_USER0
);
1950 kvm_release_page_dirty(page
);
1955 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
1958 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1960 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
1963 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1965 return X86EMUL_CONTINUE
;
1968 int emulate_clts(struct kvm_vcpu
*vcpu
)
1970 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
& ~X86_CR0_TS
);
1971 return X86EMUL_CONTINUE
;
1974 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
1976 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1980 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
1981 return X86EMUL_CONTINUE
;
1983 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __func__
, dr
);
1984 return X86EMUL_UNHANDLEABLE
;
1988 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1990 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1993 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1995 /* FIXME: better handling */
1996 return X86EMUL_UNHANDLEABLE
;
1998 return X86EMUL_CONTINUE
;
2001 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
2003 static int reported
;
2005 unsigned long rip
= vcpu
->arch
.rip
;
2006 unsigned long rip_linear
;
2008 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
2013 emulator_read_std(rip_linear
, (void *)opcodes
, 4, vcpu
);
2015 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2016 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
2019 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
2021 static struct x86_emulate_ops emulate_ops
= {
2022 .read_std
= emulator_read_std
,
2023 .read_emulated
= emulator_read_emulated
,
2024 .write_emulated
= emulator_write_emulated
,
2025 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
2028 int emulate_instruction(struct kvm_vcpu
*vcpu
,
2029 struct kvm_run
*run
,
2035 struct decode_cache
*c
;
2037 vcpu
->arch
.mmio_fault_cr2
= cr2
;
2038 kvm_x86_ops
->cache_regs(vcpu
);
2040 vcpu
->mmio_is_write
= 0;
2041 vcpu
->arch
.pio
.string
= 0;
2043 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
2045 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
2047 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
2048 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
2049 vcpu
->arch
.emulate_ctxt
.mode
=
2050 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
2051 ? X86EMUL_MODE_REAL
: cs_l
2052 ? X86EMUL_MODE_PROT64
: cs_db
2053 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
2055 if (vcpu
->arch
.emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
2056 vcpu
->arch
.emulate_ctxt
.cs_base
= 0;
2057 vcpu
->arch
.emulate_ctxt
.ds_base
= 0;
2058 vcpu
->arch
.emulate_ctxt
.es_base
= 0;
2059 vcpu
->arch
.emulate_ctxt
.ss_base
= 0;
2061 vcpu
->arch
.emulate_ctxt
.cs_base
=
2062 get_segment_base(vcpu
, VCPU_SREG_CS
);
2063 vcpu
->arch
.emulate_ctxt
.ds_base
=
2064 get_segment_base(vcpu
, VCPU_SREG_DS
);
2065 vcpu
->arch
.emulate_ctxt
.es_base
=
2066 get_segment_base(vcpu
, VCPU_SREG_ES
);
2067 vcpu
->arch
.emulate_ctxt
.ss_base
=
2068 get_segment_base(vcpu
, VCPU_SREG_SS
);
2071 vcpu
->arch
.emulate_ctxt
.gs_base
=
2072 get_segment_base(vcpu
, VCPU_SREG_GS
);
2073 vcpu
->arch
.emulate_ctxt
.fs_base
=
2074 get_segment_base(vcpu
, VCPU_SREG_FS
);
2076 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2078 /* Reject the instructions other than VMCALL/VMMCALL when
2079 * try to emulate invalid opcode */
2080 c
= &vcpu
->arch
.emulate_ctxt
.decode
;
2081 if ((emulation_type
& EMULTYPE_TRAP_UD
) &&
2082 (!(c
->twobyte
&& c
->b
== 0x01 &&
2083 (c
->modrm_reg
== 0 || c
->modrm_reg
== 3) &&
2084 c
->modrm_mod
== 3 && c
->modrm_rm
== 1)))
2085 return EMULATE_FAIL
;
2087 ++vcpu
->stat
.insn_emulation
;
2089 ++vcpu
->stat
.insn_emulation_fail
;
2090 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2091 return EMULATE_DONE
;
2092 return EMULATE_FAIL
;
2096 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2098 if (vcpu
->arch
.pio
.string
)
2099 return EMULATE_DO_MMIO
;
2101 if ((r
|| vcpu
->mmio_is_write
) && run
) {
2102 run
->exit_reason
= KVM_EXIT_MMIO
;
2103 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
2104 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
2105 run
->mmio
.len
= vcpu
->mmio_size
;
2106 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
2110 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2111 return EMULATE_DONE
;
2112 if (!vcpu
->mmio_needed
) {
2113 kvm_report_emulation_failure(vcpu
, "mmio");
2114 return EMULATE_FAIL
;
2116 return EMULATE_DO_MMIO
;
2119 kvm_x86_ops
->decache_regs(vcpu
);
2120 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
2122 if (vcpu
->mmio_is_write
) {
2123 vcpu
->mmio_needed
= 0;
2124 return EMULATE_DO_MMIO
;
2127 return EMULATE_DONE
;
2129 EXPORT_SYMBOL_GPL(emulate_instruction
);
2131 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
2135 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.pio
.guest_pages
); ++i
)
2136 if (vcpu
->arch
.pio
.guest_pages
[i
]) {
2137 kvm_release_page_dirty(vcpu
->arch
.pio
.guest_pages
[i
]);
2138 vcpu
->arch
.pio
.guest_pages
[i
] = NULL
;
2142 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
2144 void *p
= vcpu
->arch
.pio_data
;
2147 int nr_pages
= vcpu
->arch
.pio
.guest_pages
[1] ? 2 : 1;
2149 q
= vmap(vcpu
->arch
.pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
2152 free_pio_guest_pages(vcpu
);
2155 q
+= vcpu
->arch
.pio
.guest_page_offset
;
2156 bytes
= vcpu
->arch
.pio
.size
* vcpu
->arch
.pio
.cur_count
;
2157 if (vcpu
->arch
.pio
.in
)
2158 memcpy(q
, p
, bytes
);
2160 memcpy(p
, q
, bytes
);
2161 q
-= vcpu
->arch
.pio
.guest_page_offset
;
2163 free_pio_guest_pages(vcpu
);
2167 int complete_pio(struct kvm_vcpu
*vcpu
)
2169 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2173 kvm_x86_ops
->cache_regs(vcpu
);
2177 memcpy(&vcpu
->arch
.regs
[VCPU_REGS_RAX
], vcpu
->arch
.pio_data
,
2181 r
= pio_copy_data(vcpu
);
2183 kvm_x86_ops
->cache_regs(vcpu
);
2190 delta
*= io
->cur_count
;
2192 * The size of the register should really depend on
2193 * current address size.
2195 vcpu
->arch
.regs
[VCPU_REGS_RCX
] -= delta
;
2201 vcpu
->arch
.regs
[VCPU_REGS_RDI
] += delta
;
2203 vcpu
->arch
.regs
[VCPU_REGS_RSI
] += delta
;
2206 kvm_x86_ops
->decache_regs(vcpu
);
2208 io
->count
-= io
->cur_count
;
2214 static void kernel_pio(struct kvm_io_device
*pio_dev
,
2215 struct kvm_vcpu
*vcpu
,
2218 /* TODO: String I/O for in kernel device */
2220 mutex_lock(&vcpu
->kvm
->lock
);
2221 if (vcpu
->arch
.pio
.in
)
2222 kvm_iodevice_read(pio_dev
, vcpu
->arch
.pio
.port
,
2223 vcpu
->arch
.pio
.size
,
2226 kvm_iodevice_write(pio_dev
, vcpu
->arch
.pio
.port
,
2227 vcpu
->arch
.pio
.size
,
2229 mutex_unlock(&vcpu
->kvm
->lock
);
2232 static void pio_string_write(struct kvm_io_device
*pio_dev
,
2233 struct kvm_vcpu
*vcpu
)
2235 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2236 void *pd
= vcpu
->arch
.pio_data
;
2239 mutex_lock(&vcpu
->kvm
->lock
);
2240 for (i
= 0; i
< io
->cur_count
; i
++) {
2241 kvm_iodevice_write(pio_dev
, io
->port
,
2246 mutex_unlock(&vcpu
->kvm
->lock
);
2249 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
2252 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
2255 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2256 int size
, unsigned port
)
2258 struct kvm_io_device
*pio_dev
;
2260 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2261 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2262 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2263 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2264 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= 1;
2265 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2266 vcpu
->arch
.pio
.in
= in
;
2267 vcpu
->arch
.pio
.string
= 0;
2268 vcpu
->arch
.pio
.down
= 0;
2269 vcpu
->arch
.pio
.guest_page_offset
= 0;
2270 vcpu
->arch
.pio
.rep
= 0;
2272 kvm_x86_ops
->cache_regs(vcpu
);
2273 memcpy(vcpu
->arch
.pio_data
, &vcpu
->arch
.regs
[VCPU_REGS_RAX
], 4);
2274 kvm_x86_ops
->decache_regs(vcpu
);
2276 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2278 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2280 kernel_pio(pio_dev
, vcpu
, vcpu
->arch
.pio_data
);
2286 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
2288 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2289 int size
, unsigned long count
, int down
,
2290 gva_t address
, int rep
, unsigned port
)
2292 unsigned now
, in_page
;
2296 struct kvm_io_device
*pio_dev
;
2298 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2299 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2300 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2301 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2302 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= count
;
2303 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2304 vcpu
->arch
.pio
.in
= in
;
2305 vcpu
->arch
.pio
.string
= 1;
2306 vcpu
->arch
.pio
.down
= down
;
2307 vcpu
->arch
.pio
.guest_page_offset
= offset_in_page(address
);
2308 vcpu
->arch
.pio
.rep
= rep
;
2311 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2316 in_page
= PAGE_SIZE
- offset_in_page(address
);
2318 in_page
= offset_in_page(address
) + size
;
2319 now
= min(count
, (unsigned long)in_page
/ size
);
2322 * String I/O straddles page boundary. Pin two guest pages
2323 * so that we satisfy atomicity constraints. Do just one
2324 * transaction to avoid complexity.
2331 * String I/O in reverse. Yuck. Kill the guest, fix later.
2333 pr_unimpl(vcpu
, "guest string pio down\n");
2334 kvm_inject_gp(vcpu
, 0);
2337 vcpu
->run
->io
.count
= now
;
2338 vcpu
->arch
.pio
.cur_count
= now
;
2340 if (vcpu
->arch
.pio
.cur_count
== vcpu
->arch
.pio
.count
)
2341 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2343 for (i
= 0; i
< nr_pages
; ++i
) {
2344 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
2345 vcpu
->arch
.pio
.guest_pages
[i
] = page
;
2347 kvm_inject_gp(vcpu
, 0);
2348 free_pio_guest_pages(vcpu
);
2353 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2354 if (!vcpu
->arch
.pio
.in
) {
2355 /* string PIO write */
2356 ret
= pio_copy_data(vcpu
);
2357 if (ret
>= 0 && pio_dev
) {
2358 pio_string_write(pio_dev
, vcpu
);
2360 if (vcpu
->arch
.pio
.count
== 0)
2364 pr_unimpl(vcpu
, "no string pio read support yet, "
2365 "port %x size %d count %ld\n",
2370 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2372 int kvm_arch_init(void *opaque
)
2375 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
2378 printk(KERN_ERR
"kvm: already loaded the other module\n");
2383 if (!ops
->cpu_has_kvm_support()) {
2384 printk(KERN_ERR
"kvm: no hardware support\n");
2388 if (ops
->disabled_by_bios()) {
2389 printk(KERN_ERR
"kvm: disabled by bios\n");
2394 r
= kvm_mmu_module_init();
2398 kvm_init_msr_list();
2401 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2408 void kvm_arch_exit(void)
2411 kvm_mmu_module_exit();
2414 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
2416 ++vcpu
->stat
.halt_exits
;
2417 if (irqchip_in_kernel(vcpu
->kvm
)) {
2418 vcpu
->arch
.mp_state
= VCPU_MP_STATE_HALTED
;
2419 up_read(&vcpu
->kvm
->slots_lock
);
2420 kvm_vcpu_block(vcpu
);
2421 down_read(&vcpu
->kvm
->slots_lock
);
2422 if (vcpu
->arch
.mp_state
!= VCPU_MP_STATE_RUNNABLE
)
2426 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
2430 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
2432 static inline gpa_t
hc_gpa(struct kvm_vcpu
*vcpu
, unsigned long a0
,
2435 if (is_long_mode(vcpu
))
2438 return a0
| ((gpa_t
)a1
<< 32);
2441 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
2443 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
2446 kvm_x86_ops
->cache_regs(vcpu
);
2448 nr
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2449 a0
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2450 a1
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2451 a2
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2452 a3
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2454 if (!is_long_mode(vcpu
)) {
2463 case KVM_HC_VAPIC_POLL_IRQ
:
2467 r
= kvm_pv_mmu_op(vcpu
, a0
, hc_gpa(vcpu
, a1
, a2
), &ret
);
2473 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = ret
;
2474 kvm_x86_ops
->decache_regs(vcpu
);
2475 ++vcpu
->stat
.hypercalls
;
2478 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
2480 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
2482 char instruction
[3];
2487 * Blow out the MMU to ensure that no other VCPU has an active mapping
2488 * to ensure that the updated hypercall appears atomically across all
2491 kvm_mmu_zap_all(vcpu
->kvm
);
2493 kvm_x86_ops
->cache_regs(vcpu
);
2494 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
2495 if (emulator_write_emulated(vcpu
->arch
.rip
, instruction
, 3, vcpu
)
2496 != X86EMUL_CONTINUE
)
2502 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
2504 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
2507 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2509 struct descriptor_table dt
= { limit
, base
};
2511 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2514 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2516 struct descriptor_table dt
= { limit
, base
};
2518 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2521 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
2522 unsigned long *rflags
)
2524 kvm_lmsw(vcpu
, msw
);
2525 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2528 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
2530 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2533 return vcpu
->arch
.cr0
;
2535 return vcpu
->arch
.cr2
;
2537 return vcpu
->arch
.cr3
;
2539 return vcpu
->arch
.cr4
;
2541 return kvm_get_cr8(vcpu
);
2543 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
2548 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
2549 unsigned long *rflags
)
2553 kvm_set_cr0(vcpu
, mk_cr_64(vcpu
->arch
.cr0
, val
));
2554 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2557 vcpu
->arch
.cr2
= val
;
2560 kvm_set_cr3(vcpu
, val
);
2563 kvm_set_cr4(vcpu
, mk_cr_64(vcpu
->arch
.cr4
, val
));
2566 kvm_set_cr8(vcpu
, val
& 0xfUL
);
2569 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
2573 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
2575 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
2576 int j
, nent
= vcpu
->arch
.cpuid_nent
;
2578 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
2579 /* when no next entry is found, the current entry[i] is reselected */
2580 for (j
= i
+ 1; j
== i
; j
= (j
+ 1) % nent
) {
2581 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
2582 if (ej
->function
== e
->function
) {
2583 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
2587 return 0; /* silence gcc, even though control never reaches here */
2590 /* find an entry with matching function, matching index (if needed), and that
2591 * should be read next (if it's stateful) */
2592 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
2593 u32 function
, u32 index
)
2595 if (e
->function
!= function
)
2597 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
2599 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
2600 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
2605 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
2608 u32 function
, index
;
2609 struct kvm_cpuid_entry2
*e
, *best
;
2611 kvm_x86_ops
->cache_regs(vcpu
);
2612 function
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2613 index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2614 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = 0;
2615 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = 0;
2616 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = 0;
2617 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = 0;
2619 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
2620 e
= &vcpu
->arch
.cpuid_entries
[i
];
2621 if (is_matching_cpuid_entry(e
, function
, index
)) {
2622 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
2623 move_to_next_stateful_cpuid_entry(vcpu
, i
);
2628 * Both basic or both extended?
2630 if (((e
->function
^ function
) & 0x80000000) == 0)
2631 if (!best
|| e
->function
> best
->function
)
2635 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = best
->eax
;
2636 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = best
->ebx
;
2637 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = best
->ecx
;
2638 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = best
->edx
;
2640 kvm_x86_ops
->decache_regs(vcpu
);
2641 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2643 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
2646 * Check if userspace requested an interrupt window, and that the
2647 * interrupt window is open.
2649 * No need to exit to userspace if we already have an interrupt queued.
2651 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
2652 struct kvm_run
*kvm_run
)
2654 return (!vcpu
->arch
.irq_summary
&&
2655 kvm_run
->request_interrupt_window
&&
2656 vcpu
->arch
.interrupt_window_open
&&
2657 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
2660 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
2661 struct kvm_run
*kvm_run
)
2663 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
2664 kvm_run
->cr8
= kvm_get_cr8(vcpu
);
2665 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
2666 if (irqchip_in_kernel(vcpu
->kvm
))
2667 kvm_run
->ready_for_interrupt_injection
= 1;
2669 kvm_run
->ready_for_interrupt_injection
=
2670 (vcpu
->arch
.interrupt_window_open
&&
2671 vcpu
->arch
.irq_summary
== 0);
2674 static void vapic_enter(struct kvm_vcpu
*vcpu
)
2676 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2679 if (!apic
|| !apic
->vapic_addr
)
2682 down_read(¤t
->mm
->mmap_sem
);
2683 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2684 up_read(¤t
->mm
->mmap_sem
);
2686 vcpu
->arch
.apic
->vapic_page
= page
;
2689 static void vapic_exit(struct kvm_vcpu
*vcpu
)
2691 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2693 if (!apic
|| !apic
->vapic_addr
)
2696 kvm_release_page_dirty(apic
->vapic_page
);
2697 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2700 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2704 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
)) {
2705 pr_debug("vcpu %d received sipi with vector # %x\n",
2706 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
2707 kvm_lapic_reset(vcpu
);
2708 r
= kvm_x86_ops
->vcpu_reset(vcpu
);
2711 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
2714 down_read(&vcpu
->kvm
->slots_lock
);
2718 if (vcpu
->guest_debug
.enabled
)
2719 kvm_x86_ops
->guest_debug_pre(vcpu
);
2723 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
2724 kvm_mmu_unload(vcpu
);
2726 r
= kvm_mmu_reload(vcpu
);
2730 if (vcpu
->requests
) {
2731 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
2732 __kvm_migrate_apic_timer(vcpu
);
2733 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
2735 kvm_run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
2739 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
)) {
2740 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2746 kvm_inject_pending_timer_irqs(vcpu
);
2750 kvm_x86_ops
->prepare_guest_switch(vcpu
);
2751 kvm_load_guest_fpu(vcpu
);
2753 local_irq_disable();
2755 if (need_resched()) {
2763 if (test_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
)) {
2770 if (signal_pending(current
)) {
2774 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2775 ++vcpu
->stat
.signal_exits
;
2779 if (vcpu
->arch
.exception
.pending
)
2780 __queue_exception(vcpu
);
2781 else if (irqchip_in_kernel(vcpu
->kvm
))
2782 kvm_x86_ops
->inject_pending_irq(vcpu
);
2784 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
2786 kvm_lapic_sync_to_vapic(vcpu
);
2788 up_read(&vcpu
->kvm
->slots_lock
);
2790 vcpu
->guest_mode
= 1;
2794 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
2795 kvm_x86_ops
->tlb_flush(vcpu
);
2797 kvm_x86_ops
->run(vcpu
, kvm_run
);
2799 vcpu
->guest_mode
= 0;
2805 * We must have an instruction between local_irq_enable() and
2806 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2807 * the interrupt shadow. The stat.exits increment will do nicely.
2808 * But we need to prevent reordering, hence this barrier():
2816 down_read(&vcpu
->kvm
->slots_lock
);
2819 * Profile KVM exit RIPs:
2821 if (unlikely(prof_on
== KVM_PROFILING
)) {
2822 kvm_x86_ops
->cache_regs(vcpu
);
2823 profile_hit(KVM_PROFILING
, (void *)vcpu
->arch
.rip
);
2826 if (vcpu
->arch
.exception
.pending
&& kvm_x86_ops
->exception_injected(vcpu
))
2827 vcpu
->arch
.exception
.pending
= false;
2829 kvm_lapic_sync_from_vapic(vcpu
);
2831 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
2834 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
2836 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2837 ++vcpu
->stat
.request_irq_exits
;
2840 if (!need_resched())
2845 up_read(&vcpu
->kvm
->slots_lock
);
2848 down_read(&vcpu
->kvm
->slots_lock
);
2852 post_kvm_run_save(vcpu
, kvm_run
);
2854 down_read(&vcpu
->kvm
->slots_lock
);
2856 up_read(&vcpu
->kvm
->slots_lock
);
2861 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2868 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_UNINITIALIZED
)) {
2869 kvm_vcpu_block(vcpu
);
2874 if (vcpu
->sigset_active
)
2875 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2877 /* re-sync apic's tpr */
2878 if (!irqchip_in_kernel(vcpu
->kvm
))
2879 kvm_set_cr8(vcpu
, kvm_run
->cr8
);
2881 if (vcpu
->arch
.pio
.cur_count
) {
2882 r
= complete_pio(vcpu
);
2886 #if CONFIG_HAS_IOMEM
2887 if (vcpu
->mmio_needed
) {
2888 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
2889 vcpu
->mmio_read_completed
= 1;
2890 vcpu
->mmio_needed
= 0;
2892 down_read(&vcpu
->kvm
->slots_lock
);
2893 r
= emulate_instruction(vcpu
, kvm_run
,
2894 vcpu
->arch
.mmio_fault_cr2
, 0,
2895 EMULTYPE_NO_DECODE
);
2896 up_read(&vcpu
->kvm
->slots_lock
);
2897 if (r
== EMULATE_DO_MMIO
) {
2899 * Read-modify-write. Back to userspace.
2906 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
2907 kvm_x86_ops
->cache_regs(vcpu
);
2908 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
2909 kvm_x86_ops
->decache_regs(vcpu
);
2912 r
= __vcpu_run(vcpu
, kvm_run
);
2915 if (vcpu
->sigset_active
)
2916 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2922 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2926 kvm_x86_ops
->cache_regs(vcpu
);
2928 regs
->rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2929 regs
->rbx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2930 regs
->rcx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2931 regs
->rdx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2932 regs
->rsi
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2933 regs
->rdi
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
2934 regs
->rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
2935 regs
->rbp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
2936 #ifdef CONFIG_X86_64
2937 regs
->r8
= vcpu
->arch
.regs
[VCPU_REGS_R8
];
2938 regs
->r9
= vcpu
->arch
.regs
[VCPU_REGS_R9
];
2939 regs
->r10
= vcpu
->arch
.regs
[VCPU_REGS_R10
];
2940 regs
->r11
= vcpu
->arch
.regs
[VCPU_REGS_R11
];
2941 regs
->r12
= vcpu
->arch
.regs
[VCPU_REGS_R12
];
2942 regs
->r13
= vcpu
->arch
.regs
[VCPU_REGS_R13
];
2943 regs
->r14
= vcpu
->arch
.regs
[VCPU_REGS_R14
];
2944 regs
->r15
= vcpu
->arch
.regs
[VCPU_REGS_R15
];
2947 regs
->rip
= vcpu
->arch
.rip
;
2948 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2951 * Don't leak debug flags in case they were set for guest debugging
2953 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2954 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2961 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2965 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = regs
->rax
;
2966 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2967 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2968 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2969 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2970 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2971 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2972 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2973 #ifdef CONFIG_X86_64
2974 vcpu
->arch
.regs
[VCPU_REGS_R8
] = regs
->r8
;
2975 vcpu
->arch
.regs
[VCPU_REGS_R9
] = regs
->r9
;
2976 vcpu
->arch
.regs
[VCPU_REGS_R10
] = regs
->r10
;
2977 vcpu
->arch
.regs
[VCPU_REGS_R11
] = regs
->r11
;
2978 vcpu
->arch
.regs
[VCPU_REGS_R12
] = regs
->r12
;
2979 vcpu
->arch
.regs
[VCPU_REGS_R13
] = regs
->r13
;
2980 vcpu
->arch
.regs
[VCPU_REGS_R14
] = regs
->r14
;
2981 vcpu
->arch
.regs
[VCPU_REGS_R15
] = regs
->r15
;
2984 vcpu
->arch
.rip
= regs
->rip
;
2985 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
2987 kvm_x86_ops
->decache_regs(vcpu
);
2994 static void get_segment(struct kvm_vcpu
*vcpu
,
2995 struct kvm_segment
*var
, int seg
)
2997 kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
3000 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
3002 struct kvm_segment cs
;
3004 get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
3008 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
3010 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
3011 struct kvm_sregs
*sregs
)
3013 struct descriptor_table dt
;
3018 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3019 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3020 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3021 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3022 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3023 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3025 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3026 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3028 kvm_x86_ops
->get_idt(vcpu
, &dt
);
3029 sregs
->idt
.limit
= dt
.limit
;
3030 sregs
->idt
.base
= dt
.base
;
3031 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
3032 sregs
->gdt
.limit
= dt
.limit
;
3033 sregs
->gdt
.base
= dt
.base
;
3035 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3036 sregs
->cr0
= vcpu
->arch
.cr0
;
3037 sregs
->cr2
= vcpu
->arch
.cr2
;
3038 sregs
->cr3
= vcpu
->arch
.cr3
;
3039 sregs
->cr4
= vcpu
->arch
.cr4
;
3040 sregs
->cr8
= kvm_get_cr8(vcpu
);
3041 sregs
->efer
= vcpu
->arch
.shadow_efer
;
3042 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
3044 if (irqchip_in_kernel(vcpu
->kvm
)) {
3045 memset(sregs
->interrupt_bitmap
, 0,
3046 sizeof sregs
->interrupt_bitmap
);
3047 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
3048 if (pending_vec
>= 0)
3049 set_bit(pending_vec
,
3050 (unsigned long *)sregs
->interrupt_bitmap
);
3052 memcpy(sregs
->interrupt_bitmap
, vcpu
->arch
.irq_pending
,
3053 sizeof sregs
->interrupt_bitmap
);
3060 static void set_segment(struct kvm_vcpu
*vcpu
,
3061 struct kvm_segment
*var
, int seg
)
3063 kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
3066 static void seg_desct_to_kvm_desct(struct desc_struct
*seg_desc
, u16 selector
,
3067 struct kvm_segment
*kvm_desct
)
3069 kvm_desct
->base
= seg_desc
->base0
;
3070 kvm_desct
->base
|= seg_desc
->base1
<< 16;
3071 kvm_desct
->base
|= seg_desc
->base2
<< 24;
3072 kvm_desct
->limit
= seg_desc
->limit0
;
3073 kvm_desct
->limit
|= seg_desc
->limit
<< 16;
3074 kvm_desct
->selector
= selector
;
3075 kvm_desct
->type
= seg_desc
->type
;
3076 kvm_desct
->present
= seg_desc
->p
;
3077 kvm_desct
->dpl
= seg_desc
->dpl
;
3078 kvm_desct
->db
= seg_desc
->d
;
3079 kvm_desct
->s
= seg_desc
->s
;
3080 kvm_desct
->l
= seg_desc
->l
;
3081 kvm_desct
->g
= seg_desc
->g
;
3082 kvm_desct
->avl
= seg_desc
->avl
;
3084 kvm_desct
->unusable
= 1;
3086 kvm_desct
->unusable
= 0;
3087 kvm_desct
->padding
= 0;
3090 static void get_segment_descritptor_dtable(struct kvm_vcpu
*vcpu
,
3092 struct descriptor_table
*dtable
)
3094 if (selector
& 1 << 2) {
3095 struct kvm_segment kvm_seg
;
3097 get_segment(vcpu
, &kvm_seg
, VCPU_SREG_LDTR
);
3099 if (kvm_seg
.unusable
)
3102 dtable
->limit
= kvm_seg
.limit
;
3103 dtable
->base
= kvm_seg
.base
;
3106 kvm_x86_ops
->get_gdt(vcpu
, dtable
);
3109 /* allowed just for 8 bytes segments */
3110 static int load_guest_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3111 struct desc_struct
*seg_desc
)
3113 struct descriptor_table dtable
;
3114 u16 index
= selector
>> 3;
3116 get_segment_descritptor_dtable(vcpu
, selector
, &dtable
);
3118 if (dtable
.limit
< index
* 8 + 7) {
3119 kvm_queue_exception_e(vcpu
, GP_VECTOR
, selector
& 0xfffc);
3122 return kvm_read_guest(vcpu
->kvm
, dtable
.base
+ index
* 8, seg_desc
, 8);
3125 /* allowed just for 8 bytes segments */
3126 static int save_guest_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3127 struct desc_struct
*seg_desc
)
3129 struct descriptor_table dtable
;
3130 u16 index
= selector
>> 3;
3132 get_segment_descritptor_dtable(vcpu
, selector
, &dtable
);
3134 if (dtable
.limit
< index
* 8 + 7)
3136 return kvm_write_guest(vcpu
->kvm
, dtable
.base
+ index
* 8, seg_desc
, 8);
3139 static u32
get_tss_base_addr(struct kvm_vcpu
*vcpu
,
3140 struct desc_struct
*seg_desc
)
3144 base_addr
= seg_desc
->base0
;
3145 base_addr
|= (seg_desc
->base1
<< 16);
3146 base_addr
|= (seg_desc
->base2
<< 24);
3151 static int load_tss_segment32(struct kvm_vcpu
*vcpu
,
3152 struct desc_struct
*seg_desc
,
3153 struct tss_segment_32
*tss
)
3157 base_addr
= get_tss_base_addr(vcpu
, seg_desc
);
3159 return kvm_read_guest(vcpu
->kvm
, base_addr
, tss
,
3160 sizeof(struct tss_segment_32
));
3163 static int save_tss_segment32(struct kvm_vcpu
*vcpu
,
3164 struct desc_struct
*seg_desc
,
3165 struct tss_segment_32
*tss
)
3169 base_addr
= get_tss_base_addr(vcpu
, seg_desc
);
3171 return kvm_write_guest(vcpu
->kvm
, base_addr
, tss
,
3172 sizeof(struct tss_segment_32
));
3175 static int load_tss_segment16(struct kvm_vcpu
*vcpu
,
3176 struct desc_struct
*seg_desc
,
3177 struct tss_segment_16
*tss
)
3181 base_addr
= get_tss_base_addr(vcpu
, seg_desc
);
3183 return kvm_read_guest(vcpu
->kvm
, base_addr
, tss
,
3184 sizeof(struct tss_segment_16
));
3187 static int save_tss_segment16(struct kvm_vcpu
*vcpu
,
3188 struct desc_struct
*seg_desc
,
3189 struct tss_segment_16
*tss
)
3193 base_addr
= get_tss_base_addr(vcpu
, seg_desc
);
3195 return kvm_write_guest(vcpu
->kvm
, base_addr
, tss
,
3196 sizeof(struct tss_segment_16
));
3199 static u16
get_segment_selector(struct kvm_vcpu
*vcpu
, int seg
)
3201 struct kvm_segment kvm_seg
;
3203 get_segment(vcpu
, &kvm_seg
, seg
);
3204 return kvm_seg
.selector
;
3207 static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu
*vcpu
,
3209 struct kvm_segment
*kvm_seg
)
3211 struct desc_struct seg_desc
;
3213 if (load_guest_segment_descriptor(vcpu
, selector
, &seg_desc
))
3215 seg_desct_to_kvm_desct(&seg_desc
, selector
, kvm_seg
);
3219 static int load_segment_descriptor(struct kvm_vcpu
*vcpu
, u16 selector
,
3220 int type_bits
, int seg
)
3222 struct kvm_segment kvm_seg
;
3224 if (load_segment_descriptor_to_kvm_desct(vcpu
, selector
, &kvm_seg
))
3226 kvm_seg
.type
|= type_bits
;
3228 if (seg
!= VCPU_SREG_SS
&& seg
!= VCPU_SREG_CS
&&
3229 seg
!= VCPU_SREG_LDTR
)
3231 kvm_seg
.unusable
= 1;
3233 set_segment(vcpu
, &kvm_seg
, seg
);
3237 static void save_state_to_tss32(struct kvm_vcpu
*vcpu
,
3238 struct tss_segment_32
*tss
)
3240 tss
->cr3
= vcpu
->arch
.cr3
;
3241 tss
->eip
= vcpu
->arch
.rip
;
3242 tss
->eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3243 tss
->eax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
3244 tss
->ecx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
3245 tss
->edx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
3246 tss
->ebx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
3247 tss
->esp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
3248 tss
->ebp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
3249 tss
->esi
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
3250 tss
->edi
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
3252 tss
->es
= get_segment_selector(vcpu
, VCPU_SREG_ES
);
3253 tss
->cs
= get_segment_selector(vcpu
, VCPU_SREG_CS
);
3254 tss
->ss
= get_segment_selector(vcpu
, VCPU_SREG_SS
);
3255 tss
->ds
= get_segment_selector(vcpu
, VCPU_SREG_DS
);
3256 tss
->fs
= get_segment_selector(vcpu
, VCPU_SREG_FS
);
3257 tss
->gs
= get_segment_selector(vcpu
, VCPU_SREG_GS
);
3258 tss
->ldt_selector
= get_segment_selector(vcpu
, VCPU_SREG_LDTR
);
3259 tss
->prev_task_link
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
3262 static int load_state_from_tss32(struct kvm_vcpu
*vcpu
,
3263 struct tss_segment_32
*tss
)
3265 kvm_set_cr3(vcpu
, tss
->cr3
);
3267 vcpu
->arch
.rip
= tss
->eip
;
3268 kvm_x86_ops
->set_rflags(vcpu
, tss
->eflags
| 2);
3270 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = tss
->eax
;
3271 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = tss
->ecx
;
3272 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = tss
->edx
;
3273 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = tss
->ebx
;
3274 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = tss
->esp
;
3275 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = tss
->ebp
;
3276 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = tss
->esi
;
3277 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = tss
->edi
;
3279 if (load_segment_descriptor(vcpu
, tss
->ldt_selector
, 0, VCPU_SREG_LDTR
))
3282 if (load_segment_descriptor(vcpu
, tss
->es
, 1, VCPU_SREG_ES
))
3285 if (load_segment_descriptor(vcpu
, tss
->cs
, 9, VCPU_SREG_CS
))
3288 if (load_segment_descriptor(vcpu
, tss
->ss
, 1, VCPU_SREG_SS
))
3291 if (load_segment_descriptor(vcpu
, tss
->ds
, 1, VCPU_SREG_DS
))
3294 if (load_segment_descriptor(vcpu
, tss
->fs
, 1, VCPU_SREG_FS
))
3297 if (load_segment_descriptor(vcpu
, tss
->gs
, 1, VCPU_SREG_GS
))
3302 static void save_state_to_tss16(struct kvm_vcpu
*vcpu
,
3303 struct tss_segment_16
*tss
)
3305 tss
->ip
= vcpu
->arch
.rip
;
3306 tss
->flag
= kvm_x86_ops
->get_rflags(vcpu
);
3307 tss
->ax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
3308 tss
->cx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
3309 tss
->dx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
3310 tss
->bx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
3311 tss
->sp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
3312 tss
->bp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
3313 tss
->si
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
3314 tss
->di
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
3316 tss
->es
= get_segment_selector(vcpu
, VCPU_SREG_ES
);
3317 tss
->cs
= get_segment_selector(vcpu
, VCPU_SREG_CS
);
3318 tss
->ss
= get_segment_selector(vcpu
, VCPU_SREG_SS
);
3319 tss
->ds
= get_segment_selector(vcpu
, VCPU_SREG_DS
);
3320 tss
->ldt
= get_segment_selector(vcpu
, VCPU_SREG_LDTR
);
3321 tss
->prev_task_link
= get_segment_selector(vcpu
, VCPU_SREG_TR
);
3324 static int load_state_from_tss16(struct kvm_vcpu
*vcpu
,
3325 struct tss_segment_16
*tss
)
3327 vcpu
->arch
.rip
= tss
->ip
;
3328 kvm_x86_ops
->set_rflags(vcpu
, tss
->flag
| 2);
3329 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = tss
->ax
;
3330 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = tss
->cx
;
3331 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = tss
->dx
;
3332 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = tss
->bx
;
3333 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = tss
->sp
;
3334 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = tss
->bp
;
3335 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = tss
->si
;
3336 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = tss
->di
;
3338 if (load_segment_descriptor(vcpu
, tss
->ldt
, 0, VCPU_SREG_LDTR
))
3341 if (load_segment_descriptor(vcpu
, tss
->es
, 1, VCPU_SREG_ES
))
3344 if (load_segment_descriptor(vcpu
, tss
->cs
, 9, VCPU_SREG_CS
))
3347 if (load_segment_descriptor(vcpu
, tss
->ss
, 1, VCPU_SREG_SS
))
3350 if (load_segment_descriptor(vcpu
, tss
->ds
, 1, VCPU_SREG_DS
))
3355 int kvm_task_switch_16(struct kvm_vcpu
*vcpu
, u16 tss_selector
,
3356 struct desc_struct
*cseg_desc
,
3357 struct desc_struct
*nseg_desc
)
3359 struct tss_segment_16 tss_segment_16
;
3362 if (load_tss_segment16(vcpu
, cseg_desc
, &tss_segment_16
))
3365 save_state_to_tss16(vcpu
, &tss_segment_16
);
3366 save_tss_segment16(vcpu
, cseg_desc
, &tss_segment_16
);
3368 if (load_tss_segment16(vcpu
, nseg_desc
, &tss_segment_16
))
3370 if (load_state_from_tss16(vcpu
, &tss_segment_16
))
3378 int kvm_task_switch_32(struct kvm_vcpu
*vcpu
, u16 tss_selector
,
3379 struct desc_struct
*cseg_desc
,
3380 struct desc_struct
*nseg_desc
)
3382 struct tss_segment_32 tss_segment_32
;
3385 if (load_tss_segment32(vcpu
, cseg_desc
, &tss_segment_32
))
3388 save_state_to_tss32(vcpu
, &tss_segment_32
);
3389 save_tss_segment32(vcpu
, cseg_desc
, &tss_segment_32
);
3391 if (load_tss_segment32(vcpu
, nseg_desc
, &tss_segment_32
))
3393 if (load_state_from_tss32(vcpu
, &tss_segment_32
))
3401 int kvm_task_switch(struct kvm_vcpu
*vcpu
, u16 tss_selector
, int reason
)
3403 struct kvm_segment tr_seg
;
3404 struct desc_struct cseg_desc
;
3405 struct desc_struct nseg_desc
;
3408 get_segment(vcpu
, &tr_seg
, VCPU_SREG_TR
);
3410 if (load_guest_segment_descriptor(vcpu
, tss_selector
, &nseg_desc
))
3413 if (load_guest_segment_descriptor(vcpu
, tr_seg
.selector
, &cseg_desc
))
3417 if (reason
!= TASK_SWITCH_IRET
) {
3420 cpl
= kvm_x86_ops
->get_cpl(vcpu
);
3421 if ((tss_selector
& 3) > nseg_desc
.dpl
|| cpl
> nseg_desc
.dpl
) {
3422 kvm_queue_exception_e(vcpu
, GP_VECTOR
, 0);
3427 if (!nseg_desc
.p
|| (nseg_desc
.limit0
| nseg_desc
.limit
<< 16) < 0x67) {
3428 kvm_queue_exception_e(vcpu
, TS_VECTOR
, tss_selector
& 0xfffc);
3432 if (reason
== TASK_SWITCH_IRET
|| reason
== TASK_SWITCH_JMP
) {
3433 cseg_desc
.type
&= ~(1 << 8); //clear the B flag
3434 save_guest_segment_descriptor(vcpu
, tr_seg
.selector
,
3438 if (reason
== TASK_SWITCH_IRET
) {
3439 u32 eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3440 kvm_x86_ops
->set_rflags(vcpu
, eflags
& ~X86_EFLAGS_NT
);
3443 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
3444 kvm_x86_ops
->cache_regs(vcpu
);
3446 if (nseg_desc
.type
& 8)
3447 ret
= kvm_task_switch_32(vcpu
, tss_selector
, &cseg_desc
,
3450 ret
= kvm_task_switch_16(vcpu
, tss_selector
, &cseg_desc
,
3453 if (reason
== TASK_SWITCH_CALL
|| reason
== TASK_SWITCH_GATE
) {
3454 u32 eflags
= kvm_x86_ops
->get_rflags(vcpu
);
3455 kvm_x86_ops
->set_rflags(vcpu
, eflags
| X86_EFLAGS_NT
);
3458 if (reason
!= TASK_SWITCH_IRET
) {
3459 nseg_desc
.type
|= (1 << 8);
3460 save_guest_segment_descriptor(vcpu
, tss_selector
,
3464 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
| X86_CR0_TS
);
3465 seg_desct_to_kvm_desct(&nseg_desc
, tss_selector
, &tr_seg
);
3467 set_segment(vcpu
, &tr_seg
, VCPU_SREG_TR
);
3469 kvm_x86_ops
->decache_regs(vcpu
);
3472 EXPORT_SYMBOL_GPL(kvm_task_switch
);
3474 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
3475 struct kvm_sregs
*sregs
)
3477 int mmu_reset_needed
= 0;
3478 int i
, pending_vec
, max_bits
;
3479 struct descriptor_table dt
;
3483 dt
.limit
= sregs
->idt
.limit
;
3484 dt
.base
= sregs
->idt
.base
;
3485 kvm_x86_ops
->set_idt(vcpu
, &dt
);
3486 dt
.limit
= sregs
->gdt
.limit
;
3487 dt
.base
= sregs
->gdt
.base
;
3488 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
3490 vcpu
->arch
.cr2
= sregs
->cr2
;
3491 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
3492 vcpu
->arch
.cr3
= sregs
->cr3
;
3494 kvm_set_cr8(vcpu
, sregs
->cr8
);
3496 mmu_reset_needed
|= vcpu
->arch
.shadow_efer
!= sregs
->efer
;
3497 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
3498 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
3500 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3502 mmu_reset_needed
|= vcpu
->arch
.cr0
!= sregs
->cr0
;
3503 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
3504 vcpu
->arch
.cr0
= sregs
->cr0
;
3506 mmu_reset_needed
|= vcpu
->arch
.cr4
!= sregs
->cr4
;
3507 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
3508 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
3509 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
3511 if (mmu_reset_needed
)
3512 kvm_mmu_reset_context(vcpu
);
3514 if (!irqchip_in_kernel(vcpu
->kvm
)) {
3515 memcpy(vcpu
->arch
.irq_pending
, sregs
->interrupt_bitmap
,
3516 sizeof vcpu
->arch
.irq_pending
);
3517 vcpu
->arch
.irq_summary
= 0;
3518 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.irq_pending
); ++i
)
3519 if (vcpu
->arch
.irq_pending
[i
])
3520 __set_bit(i
, &vcpu
->arch
.irq_summary
);
3522 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
3523 pending_vec
= find_first_bit(
3524 (const unsigned long *)sregs
->interrupt_bitmap
,
3526 /* Only pending external irq is handled here */
3527 if (pending_vec
< max_bits
) {
3528 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
3529 pr_debug("Set back pending irq %d\n",
3534 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3535 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3536 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3537 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3538 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3539 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3541 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3542 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3549 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
3550 struct kvm_debug_guest
*dbg
)
3556 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
3564 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3565 * we have asm/x86/processor.h
3576 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3577 #ifdef CONFIG_X86_64
3578 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3580 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3585 * Translate a guest virtual address to a guest physical address.
3587 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
3588 struct kvm_translation
*tr
)
3590 unsigned long vaddr
= tr
->linear_address
;
3594 down_read(&vcpu
->kvm
->slots_lock
);
3595 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, vaddr
);
3596 up_read(&vcpu
->kvm
->slots_lock
);
3597 tr
->physical_address
= gpa
;
3598 tr
->valid
= gpa
!= UNMAPPED_GVA
;
3606 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3608 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3612 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
3613 fpu
->fcw
= fxsave
->cwd
;
3614 fpu
->fsw
= fxsave
->swd
;
3615 fpu
->ftwx
= fxsave
->twd
;
3616 fpu
->last_opcode
= fxsave
->fop
;
3617 fpu
->last_ip
= fxsave
->rip
;
3618 fpu
->last_dp
= fxsave
->rdp
;
3619 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
3626 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3628 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3632 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
3633 fxsave
->cwd
= fpu
->fcw
;
3634 fxsave
->swd
= fpu
->fsw
;
3635 fxsave
->twd
= fpu
->ftwx
;
3636 fxsave
->fop
= fpu
->last_opcode
;
3637 fxsave
->rip
= fpu
->last_ip
;
3638 fxsave
->rdp
= fpu
->last_dp
;
3639 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
3646 void fx_init(struct kvm_vcpu
*vcpu
)
3648 unsigned after_mxcsr_mask
;
3650 /* Initialize guest FPU by resetting ours and saving into guest's */
3652 fx_save(&vcpu
->arch
.host_fx_image
);
3654 fx_save(&vcpu
->arch
.guest_fx_image
);
3655 fx_restore(&vcpu
->arch
.host_fx_image
);
3658 vcpu
->arch
.cr0
|= X86_CR0_ET
;
3659 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
3660 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
3661 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
3662 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
3664 EXPORT_SYMBOL_GPL(fx_init
);
3666 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
3668 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
3671 vcpu
->guest_fpu_loaded
= 1;
3672 fx_save(&vcpu
->arch
.host_fx_image
);
3673 fx_restore(&vcpu
->arch
.guest_fx_image
);
3675 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
3677 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
3679 if (!vcpu
->guest_fpu_loaded
)
3682 vcpu
->guest_fpu_loaded
= 0;
3683 fx_save(&vcpu
->arch
.guest_fx_image
);
3684 fx_restore(&vcpu
->arch
.host_fx_image
);
3685 ++vcpu
->stat
.fpu_reload
;
3687 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
3689 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
3691 kvm_x86_ops
->vcpu_free(vcpu
);
3694 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
3697 return kvm_x86_ops
->vcpu_create(kvm
, id
);
3700 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
3704 /* We do fxsave: this must be aligned. */
3705 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
3708 r
= kvm_arch_vcpu_reset(vcpu
);
3710 r
= kvm_mmu_setup(vcpu
);
3717 kvm_x86_ops
->vcpu_free(vcpu
);
3721 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
3724 kvm_mmu_unload(vcpu
);
3727 kvm_x86_ops
->vcpu_free(vcpu
);
3730 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
3732 return kvm_x86_ops
->vcpu_reset(vcpu
);
3735 void kvm_arch_hardware_enable(void *garbage
)
3737 kvm_x86_ops
->hardware_enable(garbage
);
3740 void kvm_arch_hardware_disable(void *garbage
)
3742 kvm_x86_ops
->hardware_disable(garbage
);
3745 int kvm_arch_hardware_setup(void)
3747 return kvm_x86_ops
->hardware_setup();
3750 void kvm_arch_hardware_unsetup(void)
3752 kvm_x86_ops
->hardware_unsetup();
3755 void kvm_arch_check_processor_compat(void *rtn
)
3757 kvm_x86_ops
->check_processor_compatibility(rtn
);
3760 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
3766 BUG_ON(vcpu
->kvm
== NULL
);
3769 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
3770 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
3771 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
3773 vcpu
->arch
.mp_state
= VCPU_MP_STATE_UNINITIALIZED
;
3775 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
3780 vcpu
->arch
.pio_data
= page_address(page
);
3782 r
= kvm_mmu_create(vcpu
);
3784 goto fail_free_pio_data
;
3786 if (irqchip_in_kernel(kvm
)) {
3787 r
= kvm_create_lapic(vcpu
);
3789 goto fail_mmu_destroy
;
3795 kvm_mmu_destroy(vcpu
);
3797 free_page((unsigned long)vcpu
->arch
.pio_data
);
3802 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
3804 kvm_free_lapic(vcpu
);
3805 down_read(&vcpu
->kvm
->slots_lock
);
3806 kvm_mmu_destroy(vcpu
);
3807 up_read(&vcpu
->kvm
->slots_lock
);
3808 free_page((unsigned long)vcpu
->arch
.pio_data
);
3811 struct kvm
*kvm_arch_create_vm(void)
3813 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
3816 return ERR_PTR(-ENOMEM
);
3818 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
3823 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
3826 kvm_mmu_unload(vcpu
);
3830 static void kvm_free_vcpus(struct kvm
*kvm
)
3835 * Unpin any mmu pages first.
3837 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
3839 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
3840 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3841 if (kvm
->vcpus
[i
]) {
3842 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
3843 kvm
->vcpus
[i
] = NULL
;
3849 void kvm_arch_destroy_vm(struct kvm
*kvm
)
3852 kfree(kvm
->arch
.vpic
);
3853 kfree(kvm
->arch
.vioapic
);
3854 kvm_free_vcpus(kvm
);
3855 kvm_free_physmem(kvm
);
3856 if (kvm
->arch
.apic_access_page
)
3857 put_page(kvm
->arch
.apic_access_page
);
3861 int kvm_arch_set_memory_region(struct kvm
*kvm
,
3862 struct kvm_userspace_memory_region
*mem
,
3863 struct kvm_memory_slot old
,
3866 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
3867 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
3869 /*To keep backward compatibility with older userspace,
3870 *x86 needs to hanlde !user_alloc case.
3873 if (npages
&& !old
.rmap
) {
3874 down_write(¤t
->mm
->mmap_sem
);
3875 memslot
->userspace_addr
= do_mmap(NULL
, 0,
3877 PROT_READ
| PROT_WRITE
,
3878 MAP_SHARED
| MAP_ANONYMOUS
,
3880 up_write(¤t
->mm
->mmap_sem
);
3882 if (IS_ERR((void *)memslot
->userspace_addr
))
3883 return PTR_ERR((void *)memslot
->userspace_addr
);
3885 if (!old
.user_alloc
&& old
.rmap
) {
3888 down_write(¤t
->mm
->mmap_sem
);
3889 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
3890 old
.npages
* PAGE_SIZE
);
3891 up_write(¤t
->mm
->mmap_sem
);
3894 "kvm_vm_ioctl_set_memory_region: "
3895 "failed to munmap memory\n");
3900 if (!kvm
->arch
.n_requested_mmu_pages
) {
3901 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
3902 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
3905 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
3906 kvm_flush_remote_tlbs(kvm
);
3911 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
3913 return vcpu
->arch
.mp_state
== VCPU_MP_STATE_RUNNABLE
3914 || vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
;
3917 static void vcpu_kick_intr(void *info
)
3920 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)info
;
3921 printk(KERN_DEBUG
"vcpu_kick_intr %p \n", vcpu
);
3925 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
3927 int ipi_pcpu
= vcpu
->cpu
;
3929 if (waitqueue_active(&vcpu
->wq
)) {
3930 wake_up_interruptible(&vcpu
->wq
);
3931 ++vcpu
->stat
.halt_wakeup
;
3933 if (vcpu
->guest_mode
)
3934 smp_call_function_single(ipi_pcpu
, vcpu_kick_intr
, vcpu
, 0, 0);