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>
21 #include <linux/clocksource.h>
22 #include <linux/kvm.h>
24 #include <linux/vmalloc.h>
25 #include <linux/module.h>
26 #include <linux/mman.h>
27 #include <linux/highmem.h>
29 #include <asm/uaccess.h>
33 #define MAX_IO_MSRS 256
34 #define CR0_RESERVED_BITS \
35 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
36 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
37 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
38 #define CR4_RESERVED_BITS \
39 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
40 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
41 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
42 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
44 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
46 * - enable syscall per default because its emulated by KVM
47 * - enable LME and LMA per default on 64 bit KVM
50 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffafeULL
;
52 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffffeULL
;
55 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
56 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
58 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
59 struct kvm_cpuid_entry2 __user
*entries
);
61 struct kvm_x86_ops
*kvm_x86_ops
;
63 struct kvm_stats_debugfs_item debugfs_entries
[] = {
64 { "pf_fixed", VCPU_STAT(pf_fixed
) },
65 { "pf_guest", VCPU_STAT(pf_guest
) },
66 { "tlb_flush", VCPU_STAT(tlb_flush
) },
67 { "invlpg", VCPU_STAT(invlpg
) },
68 { "exits", VCPU_STAT(exits
) },
69 { "io_exits", VCPU_STAT(io_exits
) },
70 { "mmio_exits", VCPU_STAT(mmio_exits
) },
71 { "signal_exits", VCPU_STAT(signal_exits
) },
72 { "irq_window", VCPU_STAT(irq_window_exits
) },
73 { "halt_exits", VCPU_STAT(halt_exits
) },
74 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
75 { "hypercalls", VCPU_STAT(hypercalls
) },
76 { "request_irq", VCPU_STAT(request_irq_exits
) },
77 { "irq_exits", VCPU_STAT(irq_exits
) },
78 { "host_state_reload", VCPU_STAT(host_state_reload
) },
79 { "efer_reload", VCPU_STAT(efer_reload
) },
80 { "fpu_reload", VCPU_STAT(fpu_reload
) },
81 { "insn_emulation", VCPU_STAT(insn_emulation
) },
82 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
83 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
84 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
85 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
86 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
87 { "mmu_flooded", VM_STAT(mmu_flooded
) },
88 { "mmu_recycled", VM_STAT(mmu_recycled
) },
89 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
90 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
91 { "largepages", VM_STAT(lpages
) },
96 unsigned long segment_base(u16 selector
)
98 struct descriptor_table gdt
;
99 struct desc_struct
*d
;
100 unsigned long table_base
;
106 asm("sgdt %0" : "=m"(gdt
));
107 table_base
= gdt
.base
;
109 if (selector
& 4) { /* from ldt */
112 asm("sldt %0" : "=g"(ldt_selector
));
113 table_base
= segment_base(ldt_selector
);
115 d
= (struct desc_struct
*)(table_base
+ (selector
& ~7));
116 v
= d
->base0
| ((unsigned long)d
->base1
<< 16) |
117 ((unsigned long)d
->base2
<< 24);
119 if (d
->s
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
120 v
|= ((unsigned long)((struct ldttss_desc64
*)d
)->base3
) << 32;
124 EXPORT_SYMBOL_GPL(segment_base
);
126 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
128 if (irqchip_in_kernel(vcpu
->kvm
))
129 return vcpu
->arch
.apic_base
;
131 return vcpu
->arch
.apic_base
;
133 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
135 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
137 /* TODO: reserve bits check */
138 if (irqchip_in_kernel(vcpu
->kvm
))
139 kvm_lapic_set_base(vcpu
, data
);
141 vcpu
->arch
.apic_base
= data
;
143 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
145 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
147 WARN_ON(vcpu
->arch
.exception
.pending
);
148 vcpu
->arch
.exception
.pending
= true;
149 vcpu
->arch
.exception
.has_error_code
= false;
150 vcpu
->arch
.exception
.nr
= nr
;
152 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
154 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
157 ++vcpu
->stat
.pf_guest
;
158 if (vcpu
->arch
.exception
.pending
&& vcpu
->arch
.exception
.nr
== PF_VECTOR
) {
159 printk(KERN_DEBUG
"kvm: inject_page_fault:"
160 " double fault 0x%lx\n", addr
);
161 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
162 vcpu
->arch
.exception
.error_code
= 0;
165 vcpu
->arch
.cr2
= addr
;
166 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
169 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
171 WARN_ON(vcpu
->arch
.exception
.pending
);
172 vcpu
->arch
.exception
.pending
= true;
173 vcpu
->arch
.exception
.has_error_code
= true;
174 vcpu
->arch
.exception
.nr
= nr
;
175 vcpu
->arch
.exception
.error_code
= error_code
;
177 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
179 static void __queue_exception(struct kvm_vcpu
*vcpu
)
181 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
182 vcpu
->arch
.exception
.has_error_code
,
183 vcpu
->arch
.exception
.error_code
);
187 * Load the pae pdptrs. Return true is they are all valid.
189 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
191 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
192 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
195 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
197 down_read(&vcpu
->kvm
->slots_lock
);
198 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
199 offset
* sizeof(u64
), sizeof(pdpte
));
204 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
205 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
212 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
214 up_read(&vcpu
->kvm
->slots_lock
);
218 EXPORT_SYMBOL_GPL(load_pdptrs
);
220 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
222 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
226 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
229 down_read(&vcpu
->kvm
->slots_lock
);
230 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
233 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
235 up_read(&vcpu
->kvm
->slots_lock
);
240 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
242 if (cr0
& CR0_RESERVED_BITS
) {
243 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
244 cr0
, vcpu
->arch
.cr0
);
245 kvm_inject_gp(vcpu
, 0);
249 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
250 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
251 kvm_inject_gp(vcpu
, 0);
255 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
256 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
257 "and a clear PE flag\n");
258 kvm_inject_gp(vcpu
, 0);
262 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
264 if ((vcpu
->arch
.shadow_efer
& EFER_LME
)) {
268 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
269 "in long mode while PAE is disabled\n");
270 kvm_inject_gp(vcpu
, 0);
273 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
275 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
276 "in long mode while CS.L == 1\n");
277 kvm_inject_gp(vcpu
, 0);
283 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
284 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
286 kvm_inject_gp(vcpu
, 0);
292 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
293 vcpu
->arch
.cr0
= cr0
;
295 kvm_mmu_reset_context(vcpu
);
298 EXPORT_SYMBOL_GPL(set_cr0
);
300 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
302 set_cr0(vcpu
, (vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f));
304 EXPORT_SYMBOL_GPL(lmsw
);
306 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
308 if (cr4
& CR4_RESERVED_BITS
) {
309 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
310 kvm_inject_gp(vcpu
, 0);
314 if (is_long_mode(vcpu
)) {
315 if (!(cr4
& X86_CR4_PAE
)) {
316 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
318 kvm_inject_gp(vcpu
, 0);
321 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
322 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
323 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
324 kvm_inject_gp(vcpu
, 0);
328 if (cr4
& X86_CR4_VMXE
) {
329 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
330 kvm_inject_gp(vcpu
, 0);
333 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
334 vcpu
->arch
.cr4
= cr4
;
335 kvm_mmu_reset_context(vcpu
);
337 EXPORT_SYMBOL_GPL(set_cr4
);
339 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
341 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
342 kvm_mmu_flush_tlb(vcpu
);
346 if (is_long_mode(vcpu
)) {
347 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
348 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
349 kvm_inject_gp(vcpu
, 0);
354 if (cr3
& CR3_PAE_RESERVED_BITS
) {
356 "set_cr3: #GP, reserved bits\n");
357 kvm_inject_gp(vcpu
, 0);
360 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
361 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
363 kvm_inject_gp(vcpu
, 0);
368 * We don't check reserved bits in nonpae mode, because
369 * this isn't enforced, and VMware depends on this.
373 down_read(&vcpu
->kvm
->slots_lock
);
375 * Does the new cr3 value map to physical memory? (Note, we
376 * catch an invalid cr3 even in real-mode, because it would
377 * cause trouble later on when we turn on paging anyway.)
379 * A real CPU would silently accept an invalid cr3 and would
380 * attempt to use it - with largely undefined (and often hard
381 * to debug) behavior on the guest side.
383 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
384 kvm_inject_gp(vcpu
, 0);
386 vcpu
->arch
.cr3
= cr3
;
387 vcpu
->arch
.mmu
.new_cr3(vcpu
);
389 up_read(&vcpu
->kvm
->slots_lock
);
391 EXPORT_SYMBOL_GPL(set_cr3
);
393 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
395 if (cr8
& CR8_RESERVED_BITS
) {
396 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
397 kvm_inject_gp(vcpu
, 0);
400 if (irqchip_in_kernel(vcpu
->kvm
))
401 kvm_lapic_set_tpr(vcpu
, cr8
);
403 vcpu
->arch
.cr8
= cr8
;
405 EXPORT_SYMBOL_GPL(set_cr8
);
407 unsigned long get_cr8(struct kvm_vcpu
*vcpu
)
409 if (irqchip_in_kernel(vcpu
->kvm
))
410 return kvm_lapic_get_cr8(vcpu
);
412 return vcpu
->arch
.cr8
;
414 EXPORT_SYMBOL_GPL(get_cr8
);
417 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
418 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
420 * This list is modified at module load time to reflect the
421 * capabilities of the host cpu.
423 static u32 msrs_to_save
[] = {
424 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
427 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
429 MSR_IA32_TIME_STAMP_COUNTER
, MSR_KVM_SYSTEM_TIME
, MSR_KVM_WALL_CLOCK
,
430 MSR_IA32_PERF_STATUS
,
433 static unsigned num_msrs_to_save
;
435 static u32 emulated_msrs
[] = {
436 MSR_IA32_MISC_ENABLE
,
439 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
441 if (efer
& efer_reserved_bits
) {
442 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
444 kvm_inject_gp(vcpu
, 0);
449 && (vcpu
->arch
.shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
450 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
451 kvm_inject_gp(vcpu
, 0);
455 kvm_x86_ops
->set_efer(vcpu
, efer
);
458 efer
|= vcpu
->arch
.shadow_efer
& EFER_LMA
;
460 vcpu
->arch
.shadow_efer
= efer
;
463 void kvm_enable_efer_bits(u64 mask
)
465 efer_reserved_bits
&= ~mask
;
467 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits
);
471 * Writes msr value into into the appropriate "register".
472 * Returns 0 on success, non-0 otherwise.
473 * Assumes vcpu_load() was already called.
475 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
477 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
481 * Adapt set_msr() to msr_io()'s calling convention
483 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
485 return kvm_set_msr(vcpu
, index
, *data
);
488 static void kvm_write_wall_clock(struct kvm
*kvm
, gpa_t wall_clock
)
491 struct kvm_wall_clock wc
;
492 struct timespec wc_ts
;
499 down_read(&kvm
->slots_lock
);
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
));
511 up_read(&kvm
->slots_lock
);
514 static void kvm_write_guest_time(struct kvm_vcpu
*v
)
518 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
521 if ((!vcpu
->time_page
))
524 /* Keep irq disabled to prevent changes to the clock */
525 local_irq_save(flags
);
526 kvm_get_msr(v
, MSR_IA32_TIME_STAMP_COUNTER
,
527 &vcpu
->hv_clock
.tsc_timestamp
);
529 local_irq_restore(flags
);
531 /* With all the info we got, fill in the values */
533 vcpu
->hv_clock
.system_time
= ts
.tv_nsec
+
534 (NSEC_PER_SEC
* (u64
)ts
.tv_sec
);
536 * The interface expects us to write an even number signaling that the
537 * update is finished. Since the guest won't see the intermediate
538 * state, we just write "2" at the end
540 vcpu
->hv_clock
.version
= 2;
542 shared_kaddr
= kmap_atomic(vcpu
->time_page
, KM_USER0
);
544 memcpy(shared_kaddr
+ vcpu
->time_offset
, &vcpu
->hv_clock
,
545 sizeof(vcpu
->hv_clock
));
547 kunmap_atomic(shared_kaddr
, KM_USER0
);
549 mark_page_dirty(v
->kvm
, vcpu
->time
>> PAGE_SHIFT
);
553 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
557 set_efer(vcpu
, data
);
559 case MSR_IA32_MC0_STATUS
:
560 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
563 case MSR_IA32_MCG_STATUS
:
564 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
567 case MSR_IA32_MCG_CTL
:
568 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
571 case MSR_IA32_UCODE_REV
:
572 case MSR_IA32_UCODE_WRITE
:
573 case 0x200 ... 0x2ff: /* MTRRs */
575 case MSR_IA32_APICBASE
:
576 kvm_set_apic_base(vcpu
, data
);
578 case MSR_IA32_MISC_ENABLE
:
579 vcpu
->arch
.ia32_misc_enable_msr
= data
;
581 case MSR_KVM_WALL_CLOCK
:
582 vcpu
->kvm
->arch
.wall_clock
= data
;
583 kvm_write_wall_clock(vcpu
->kvm
, data
);
585 case MSR_KVM_SYSTEM_TIME
: {
586 if (vcpu
->arch
.time_page
) {
587 kvm_release_page_dirty(vcpu
->arch
.time_page
);
588 vcpu
->arch
.time_page
= NULL
;
591 vcpu
->arch
.time
= data
;
593 /* we verify if the enable bit is set... */
597 /* ...but clean it before doing the actual write */
598 vcpu
->arch
.time_offset
= data
& ~(PAGE_MASK
| 1);
600 vcpu
->arch
.hv_clock
.tsc_to_system_mul
=
601 clocksource_khz2mult(tsc_khz
, 22);
602 vcpu
->arch
.hv_clock
.tsc_shift
= 22;
604 down_read(¤t
->mm
->mmap_sem
);
605 down_read(&vcpu
->kvm
->slots_lock
);
606 vcpu
->arch
.time_page
=
607 gfn_to_page(vcpu
->kvm
, data
>> PAGE_SHIFT
);
608 up_read(&vcpu
->kvm
->slots_lock
);
609 up_read(¤t
->mm
->mmap_sem
);
611 if (is_error_page(vcpu
->arch
.time_page
)) {
612 kvm_release_page_clean(vcpu
->arch
.time_page
);
613 vcpu
->arch
.time_page
= NULL
;
616 kvm_write_guest_time(vcpu
);
620 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n", msr
, data
);
625 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
629 * Reads an msr value (of 'msr_index') into 'pdata'.
630 * Returns 0 on success, non-0 otherwise.
631 * Assumes vcpu_load() was already called.
633 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
635 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
638 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
643 case 0xc0010010: /* SYSCFG */
644 case 0xc0010015: /* HWCR */
645 case MSR_IA32_PLATFORM_ID
:
646 case MSR_IA32_P5_MC_ADDR
:
647 case MSR_IA32_P5_MC_TYPE
:
648 case MSR_IA32_MC0_CTL
:
649 case MSR_IA32_MCG_STATUS
:
650 case MSR_IA32_MCG_CAP
:
651 case MSR_IA32_MCG_CTL
:
652 case MSR_IA32_MC0_MISC
:
653 case MSR_IA32_MC0_MISC
+4:
654 case MSR_IA32_MC0_MISC
+8:
655 case MSR_IA32_MC0_MISC
+12:
656 case MSR_IA32_MC0_MISC
+16:
657 case MSR_IA32_UCODE_REV
:
658 case MSR_IA32_EBL_CR_POWERON
:
661 case 0x200 ... 0x2ff:
664 case 0xcd: /* fsb frequency */
667 case MSR_IA32_APICBASE
:
668 data
= kvm_get_apic_base(vcpu
);
670 case MSR_IA32_MISC_ENABLE
:
671 data
= vcpu
->arch
.ia32_misc_enable_msr
;
673 case MSR_IA32_PERF_STATUS
:
674 /* TSC increment by tick */
677 data
|= (((uint64_t)4ULL) << 40);
680 data
= vcpu
->arch
.shadow_efer
;
682 case MSR_KVM_WALL_CLOCK
:
683 data
= vcpu
->kvm
->arch
.wall_clock
;
685 case MSR_KVM_SYSTEM_TIME
:
686 data
= vcpu
->arch
.time
;
689 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
695 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
698 * Read or write a bunch of msrs. All parameters are kernel addresses.
700 * @return number of msrs set successfully.
702 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
703 struct kvm_msr_entry
*entries
,
704 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
705 unsigned index
, u64
*data
))
711 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
712 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
721 * Read or write a bunch of msrs. Parameters are user addresses.
723 * @return number of msrs set successfully.
725 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
726 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
727 unsigned index
, u64
*data
),
730 struct kvm_msrs msrs
;
731 struct kvm_msr_entry
*entries
;
736 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
740 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
744 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
745 entries
= vmalloc(size
);
750 if (copy_from_user(entries
, user_msrs
->entries
, size
))
753 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
758 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
770 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
773 void decache_vcpus_on_cpu(int cpu
)
776 struct kvm_vcpu
*vcpu
;
779 spin_lock(&kvm_lock
);
780 list_for_each_entry(vm
, &vm_list
, vm_list
)
781 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
786 * If the vcpu is locked, then it is running on some
787 * other cpu and therefore it is not cached on the
790 * If it's not locked, check the last cpu it executed
793 if (mutex_trylock(&vcpu
->mutex
)) {
794 if (vcpu
->cpu
== cpu
) {
795 kvm_x86_ops
->vcpu_decache(vcpu
);
798 mutex_unlock(&vcpu
->mutex
);
801 spin_unlock(&kvm_lock
);
804 int kvm_dev_ioctl_check_extension(long ext
)
809 case KVM_CAP_IRQCHIP
:
811 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
812 case KVM_CAP_USER_MEMORY
:
813 case KVM_CAP_SET_TSS_ADDR
:
814 case KVM_CAP_EXT_CPUID
:
815 case KVM_CAP_CLOCKSOURCE
:
819 r
= !kvm_x86_ops
->cpu_has_accelerated_tpr();
821 case KVM_CAP_NR_VCPUS
:
824 case KVM_CAP_NR_MEMSLOTS
:
825 r
= KVM_MEMORY_SLOTS
;
835 long kvm_arch_dev_ioctl(struct file
*filp
,
836 unsigned int ioctl
, unsigned long arg
)
838 void __user
*argp
= (void __user
*)arg
;
842 case KVM_GET_MSR_INDEX_LIST
: {
843 struct kvm_msr_list __user
*user_msr_list
= argp
;
844 struct kvm_msr_list msr_list
;
848 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
851 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
852 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
855 if (n
< num_msrs_to_save
)
858 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
859 num_msrs_to_save
* sizeof(u32
)))
861 if (copy_to_user(user_msr_list
->indices
862 + num_msrs_to_save
* sizeof(u32
),
864 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
869 case KVM_GET_SUPPORTED_CPUID
: {
870 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
871 struct kvm_cpuid2 cpuid
;
874 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
876 r
= kvm_dev_ioctl_get_supported_cpuid(&cpuid
,
882 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
894 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
896 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
897 kvm_write_guest_time(vcpu
);
900 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
902 kvm_x86_ops
->vcpu_put(vcpu
);
903 kvm_put_guest_fpu(vcpu
);
906 static int is_efer_nx(void)
910 rdmsrl(MSR_EFER
, efer
);
911 return efer
& EFER_NX
;
914 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
917 struct kvm_cpuid_entry2
*e
, *entry
;
920 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
921 e
= &vcpu
->arch
.cpuid_entries
[i
];
922 if (e
->function
== 0x80000001) {
927 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
928 entry
->edx
&= ~(1 << 20);
929 printk(KERN_INFO
"kvm: guest NX capability removed\n");
933 /* when an old userspace process fills a new kernel module */
934 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
935 struct kvm_cpuid
*cpuid
,
936 struct kvm_cpuid_entry __user
*entries
)
939 struct kvm_cpuid_entry
*cpuid_entries
;
942 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
945 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
949 if (copy_from_user(cpuid_entries
, entries
,
950 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
952 for (i
= 0; i
< cpuid
->nent
; i
++) {
953 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
954 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
955 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
956 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
957 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
958 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
959 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
960 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
961 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
962 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
964 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
965 cpuid_fix_nx_cap(vcpu
);
969 vfree(cpuid_entries
);
974 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
975 struct kvm_cpuid2
*cpuid
,
976 struct kvm_cpuid_entry2 __user
*entries
)
981 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
984 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
985 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
987 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
994 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
995 struct kvm_cpuid2
*cpuid
,
996 struct kvm_cpuid_entry2 __user
*entries
)
1001 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
1004 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
1005 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
1010 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
1014 static inline u32
bit(int bitno
)
1016 return 1 << (bitno
& 31);
1019 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1022 entry
->function
= function
;
1023 entry
->index
= index
;
1024 cpuid_count(entry
->function
, entry
->index
,
1025 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
1029 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1030 u32 index
, int *nent
, int maxnent
)
1032 const u32 kvm_supported_word0_x86_features
= bit(X86_FEATURE_FPU
) |
1033 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1034 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1035 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1036 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1037 bit(X86_FEATURE_SEP
) | bit(X86_FEATURE_PGE
) |
1038 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1039 bit(X86_FEATURE_CLFLSH
) | bit(X86_FEATURE_MMX
) |
1040 bit(X86_FEATURE_FXSR
) | bit(X86_FEATURE_XMM
) |
1041 bit(X86_FEATURE_XMM2
) | bit(X86_FEATURE_SELFSNOOP
);
1042 const u32 kvm_supported_word1_x86_features
= bit(X86_FEATURE_FPU
) |
1043 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1044 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1045 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1046 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1047 bit(X86_FEATURE_PGE
) |
1048 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1049 bit(X86_FEATURE_MMX
) | bit(X86_FEATURE_FXSR
) |
1050 bit(X86_FEATURE_SYSCALL
) |
1051 (bit(X86_FEATURE_NX
) && is_efer_nx()) |
1052 #ifdef CONFIG_X86_64
1053 bit(X86_FEATURE_LM
) |
1055 bit(X86_FEATURE_MMXEXT
) |
1056 bit(X86_FEATURE_3DNOWEXT
) |
1057 bit(X86_FEATURE_3DNOW
);
1058 const u32 kvm_supported_word3_x86_features
=
1059 bit(X86_FEATURE_XMM3
) | bit(X86_FEATURE_CX16
);
1060 const u32 kvm_supported_word6_x86_features
=
1061 bit(X86_FEATURE_LAHF_LM
) | bit(X86_FEATURE_CMP_LEGACY
);
1063 /* all func 2 cpuid_count() should be called on the same cpu */
1065 do_cpuid_1_ent(entry
, function
, index
);
1070 entry
->eax
= min(entry
->eax
, (u32
)0xb);
1073 entry
->edx
&= kvm_supported_word0_x86_features
;
1074 entry
->ecx
&= kvm_supported_word3_x86_features
;
1076 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1077 * may return different values. This forces us to get_cpu() before
1078 * issuing the first command, and also to emulate this annoying behavior
1079 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1081 int t
, times
= entry
->eax
& 0xff;
1083 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1084 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
1085 do_cpuid_1_ent(&entry
[t
], function
, 0);
1086 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1091 /* function 4 and 0xb have additional index. */
1095 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1096 /* read more entries until cache_type is zero */
1097 for (i
= 1; *nent
< maxnent
; ++i
) {
1098 cache_type
= entry
[i
- 1].eax
& 0x1f;
1101 do_cpuid_1_ent(&entry
[i
], function
, i
);
1103 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1111 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1112 /* read more entries until level_type is zero */
1113 for (i
= 1; *nent
< maxnent
; ++i
) {
1114 level_type
= entry
[i
- 1].ecx
& 0xff;
1117 do_cpuid_1_ent(&entry
[i
], function
, i
);
1119 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1125 entry
->eax
= min(entry
->eax
, 0x8000001a);
1128 entry
->edx
&= kvm_supported_word1_x86_features
;
1129 entry
->ecx
&= kvm_supported_word6_x86_features
;
1135 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
1136 struct kvm_cpuid_entry2 __user
*entries
)
1138 struct kvm_cpuid_entry2
*cpuid_entries
;
1139 int limit
, nent
= 0, r
= -E2BIG
;
1142 if (cpuid
->nent
< 1)
1145 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
1149 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
1150 limit
= cpuid_entries
[0].eax
;
1151 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1152 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1153 &nent
, cpuid
->nent
);
1155 if (nent
>= cpuid
->nent
)
1158 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
1159 limit
= cpuid_entries
[nent
- 1].eax
;
1160 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1161 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1162 &nent
, cpuid
->nent
);
1164 if (copy_to_user(entries
, cpuid_entries
,
1165 nent
* sizeof(struct kvm_cpuid_entry2
)))
1171 vfree(cpuid_entries
);
1176 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
1177 struct kvm_lapic_state
*s
)
1180 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
1186 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
1187 struct kvm_lapic_state
*s
)
1190 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1191 kvm_apic_post_state_restore(vcpu
);
1197 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1198 struct kvm_interrupt
*irq
)
1200 if (irq
->irq
< 0 || irq
->irq
>= 256)
1202 if (irqchip_in_kernel(vcpu
->kvm
))
1206 set_bit(irq
->irq
, vcpu
->arch
.irq_pending
);
1207 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
1214 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
1215 struct kvm_tpr_access_ctl
*tac
)
1219 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
1223 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1224 unsigned int ioctl
, unsigned long arg
)
1226 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1227 void __user
*argp
= (void __user
*)arg
;
1231 case KVM_GET_LAPIC
: {
1232 struct kvm_lapic_state lapic
;
1234 memset(&lapic
, 0, sizeof lapic
);
1235 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, &lapic
);
1239 if (copy_to_user(argp
, &lapic
, sizeof lapic
))
1244 case KVM_SET_LAPIC
: {
1245 struct kvm_lapic_state lapic
;
1248 if (copy_from_user(&lapic
, argp
, sizeof lapic
))
1250 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, &lapic
);;
1256 case KVM_INTERRUPT
: {
1257 struct kvm_interrupt irq
;
1260 if (copy_from_user(&irq
, argp
, sizeof irq
))
1262 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1268 case KVM_SET_CPUID
: {
1269 struct kvm_cpuid __user
*cpuid_arg
= argp
;
1270 struct kvm_cpuid cpuid
;
1273 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1275 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
1280 case KVM_SET_CPUID2
: {
1281 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1282 struct kvm_cpuid2 cpuid
;
1285 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1287 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
1288 cpuid_arg
->entries
);
1293 case KVM_GET_CPUID2
: {
1294 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1295 struct kvm_cpuid2 cpuid
;
1298 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1300 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
1301 cpuid_arg
->entries
);
1305 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1311 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
1314 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
1316 case KVM_TPR_ACCESS_REPORTING
: {
1317 struct kvm_tpr_access_ctl tac
;
1320 if (copy_from_user(&tac
, argp
, sizeof tac
))
1322 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
1326 if (copy_to_user(argp
, &tac
, sizeof tac
))
1331 case KVM_SET_VAPIC_ADDR
: {
1332 struct kvm_vapic_addr va
;
1335 if (!irqchip_in_kernel(vcpu
->kvm
))
1338 if (copy_from_user(&va
, argp
, sizeof va
))
1341 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
1351 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
1355 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
1357 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
1361 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
1362 u32 kvm_nr_mmu_pages
)
1364 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
1367 down_write(&kvm
->slots_lock
);
1369 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
1370 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
1372 up_write(&kvm
->slots_lock
);
1376 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
1378 return kvm
->arch
.n_alloc_mmu_pages
;
1381 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1384 struct kvm_mem_alias
*alias
;
1386 for (i
= 0; i
< kvm
->arch
.naliases
; ++i
) {
1387 alias
= &kvm
->arch
.aliases
[i
];
1388 if (gfn
>= alias
->base_gfn
1389 && gfn
< alias
->base_gfn
+ alias
->npages
)
1390 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1396 * Set a new alias region. Aliases map a portion of physical memory into
1397 * another portion. This is useful for memory windows, for example the PC
1400 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
1401 struct kvm_memory_alias
*alias
)
1404 struct kvm_mem_alias
*p
;
1407 /* General sanity checks */
1408 if (alias
->memory_size
& (PAGE_SIZE
- 1))
1410 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
1412 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
1414 if (alias
->guest_phys_addr
+ alias
->memory_size
1415 < alias
->guest_phys_addr
)
1417 if (alias
->target_phys_addr
+ alias
->memory_size
1418 < alias
->target_phys_addr
)
1421 down_write(&kvm
->slots_lock
);
1423 p
= &kvm
->arch
.aliases
[alias
->slot
];
1424 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
1425 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
1426 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
1428 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
1429 if (kvm
->arch
.aliases
[n
- 1].npages
)
1431 kvm
->arch
.naliases
= n
;
1433 kvm_mmu_zap_all(kvm
);
1435 up_write(&kvm
->slots_lock
);
1443 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1448 switch (chip
->chip_id
) {
1449 case KVM_IRQCHIP_PIC_MASTER
:
1450 memcpy(&chip
->chip
.pic
,
1451 &pic_irqchip(kvm
)->pics
[0],
1452 sizeof(struct kvm_pic_state
));
1454 case KVM_IRQCHIP_PIC_SLAVE
:
1455 memcpy(&chip
->chip
.pic
,
1456 &pic_irqchip(kvm
)->pics
[1],
1457 sizeof(struct kvm_pic_state
));
1459 case KVM_IRQCHIP_IOAPIC
:
1460 memcpy(&chip
->chip
.ioapic
,
1461 ioapic_irqchip(kvm
),
1462 sizeof(struct kvm_ioapic_state
));
1471 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1476 switch (chip
->chip_id
) {
1477 case KVM_IRQCHIP_PIC_MASTER
:
1478 memcpy(&pic_irqchip(kvm
)->pics
[0],
1480 sizeof(struct kvm_pic_state
));
1482 case KVM_IRQCHIP_PIC_SLAVE
:
1483 memcpy(&pic_irqchip(kvm
)->pics
[1],
1485 sizeof(struct kvm_pic_state
));
1487 case KVM_IRQCHIP_IOAPIC
:
1488 memcpy(ioapic_irqchip(kvm
),
1490 sizeof(struct kvm_ioapic_state
));
1496 kvm_pic_update_irq(pic_irqchip(kvm
));
1501 * Get (and clear) the dirty memory log for a memory slot.
1503 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1504 struct kvm_dirty_log
*log
)
1508 struct kvm_memory_slot
*memslot
;
1511 down_write(&kvm
->slots_lock
);
1513 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1517 /* If nothing is dirty, don't bother messing with page tables. */
1519 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
1520 kvm_flush_remote_tlbs(kvm
);
1521 memslot
= &kvm
->memslots
[log
->slot
];
1522 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1523 memset(memslot
->dirty_bitmap
, 0, n
);
1527 up_write(&kvm
->slots_lock
);
1531 long kvm_arch_vm_ioctl(struct file
*filp
,
1532 unsigned int ioctl
, unsigned long arg
)
1534 struct kvm
*kvm
= filp
->private_data
;
1535 void __user
*argp
= (void __user
*)arg
;
1539 case KVM_SET_TSS_ADDR
:
1540 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
1544 case KVM_SET_MEMORY_REGION
: {
1545 struct kvm_memory_region kvm_mem
;
1546 struct kvm_userspace_memory_region kvm_userspace_mem
;
1549 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
1551 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
1552 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
1553 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
1554 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
1555 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1560 case KVM_SET_NR_MMU_PAGES
:
1561 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1565 case KVM_GET_NR_MMU_PAGES
:
1566 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1568 case KVM_SET_MEMORY_ALIAS
: {
1569 struct kvm_memory_alias alias
;
1572 if (copy_from_user(&alias
, argp
, sizeof alias
))
1574 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
1579 case KVM_CREATE_IRQCHIP
:
1581 kvm
->arch
.vpic
= kvm_create_pic(kvm
);
1582 if (kvm
->arch
.vpic
) {
1583 r
= kvm_ioapic_init(kvm
);
1585 kfree(kvm
->arch
.vpic
);
1586 kvm
->arch
.vpic
= NULL
;
1592 case KVM_IRQ_LINE
: {
1593 struct kvm_irq_level irq_event
;
1596 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1598 if (irqchip_in_kernel(kvm
)) {
1599 mutex_lock(&kvm
->lock
);
1600 if (irq_event
.irq
< 16)
1601 kvm_pic_set_irq(pic_irqchip(kvm
),
1604 kvm_ioapic_set_irq(kvm
->arch
.vioapic
,
1607 mutex_unlock(&kvm
->lock
);
1612 case KVM_GET_IRQCHIP
: {
1613 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1614 struct kvm_irqchip chip
;
1617 if (copy_from_user(&chip
, argp
, sizeof chip
))
1620 if (!irqchip_in_kernel(kvm
))
1622 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
1626 if (copy_to_user(argp
, &chip
, sizeof chip
))
1631 case KVM_SET_IRQCHIP
: {
1632 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1633 struct kvm_irqchip chip
;
1636 if (copy_from_user(&chip
, argp
, sizeof chip
))
1639 if (!irqchip_in_kernel(kvm
))
1641 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
1654 static void kvm_init_msr_list(void)
1659 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1660 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1663 msrs_to_save
[j
] = msrs_to_save
[i
];
1666 num_msrs_to_save
= j
;
1670 * Only apic need an MMIO device hook, so shortcut now..
1672 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
1675 struct kvm_io_device
*dev
;
1677 if (vcpu
->arch
.apic
) {
1678 dev
= &vcpu
->arch
.apic
->dev
;
1679 if (dev
->in_range(dev
, addr
))
1686 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1689 struct kvm_io_device
*dev
;
1691 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
);
1693 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1697 int emulator_read_std(unsigned long addr
,
1700 struct kvm_vcpu
*vcpu
)
1703 int r
= X86EMUL_CONTINUE
;
1705 down_read(&vcpu
->kvm
->slots_lock
);
1707 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1708 unsigned offset
= addr
& (PAGE_SIZE
-1);
1709 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1712 if (gpa
== UNMAPPED_GVA
) {
1713 r
= X86EMUL_PROPAGATE_FAULT
;
1716 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, tocopy
);
1718 r
= X86EMUL_UNHANDLEABLE
;
1727 up_read(&vcpu
->kvm
->slots_lock
);
1730 EXPORT_SYMBOL_GPL(emulator_read_std
);
1732 static int emulator_read_emulated(unsigned long addr
,
1735 struct kvm_vcpu
*vcpu
)
1737 struct kvm_io_device
*mmio_dev
;
1740 if (vcpu
->mmio_read_completed
) {
1741 memcpy(val
, vcpu
->mmio_data
, bytes
);
1742 vcpu
->mmio_read_completed
= 0;
1743 return X86EMUL_CONTINUE
;
1746 down_read(&vcpu
->kvm
->slots_lock
);
1747 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1748 up_read(&vcpu
->kvm
->slots_lock
);
1750 /* For APIC access vmexit */
1751 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1754 if (emulator_read_std(addr
, val
, bytes
, vcpu
)
1755 == X86EMUL_CONTINUE
)
1756 return X86EMUL_CONTINUE
;
1757 if (gpa
== UNMAPPED_GVA
)
1758 return X86EMUL_PROPAGATE_FAULT
;
1762 * Is this MMIO handled locally?
1764 mutex_lock(&vcpu
->kvm
->lock
);
1765 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1767 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1768 mutex_unlock(&vcpu
->kvm
->lock
);
1769 return X86EMUL_CONTINUE
;
1771 mutex_unlock(&vcpu
->kvm
->lock
);
1773 vcpu
->mmio_needed
= 1;
1774 vcpu
->mmio_phys_addr
= gpa
;
1775 vcpu
->mmio_size
= bytes
;
1776 vcpu
->mmio_is_write
= 0;
1778 return X86EMUL_UNHANDLEABLE
;
1781 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1782 const void *val
, int bytes
)
1786 down_read(&vcpu
->kvm
->slots_lock
);
1787 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
1789 up_read(&vcpu
->kvm
->slots_lock
);
1792 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1793 up_read(&vcpu
->kvm
->slots_lock
);
1797 static int emulator_write_emulated_onepage(unsigned long addr
,
1800 struct kvm_vcpu
*vcpu
)
1802 struct kvm_io_device
*mmio_dev
;
1805 down_read(&vcpu
->kvm
->slots_lock
);
1806 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1807 up_read(&vcpu
->kvm
->slots_lock
);
1809 if (gpa
== UNMAPPED_GVA
) {
1810 kvm_inject_page_fault(vcpu
, addr
, 2);
1811 return X86EMUL_PROPAGATE_FAULT
;
1814 /* For APIC access vmexit */
1815 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1818 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1819 return X86EMUL_CONTINUE
;
1823 * Is this MMIO handled locally?
1825 mutex_lock(&vcpu
->kvm
->lock
);
1826 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1828 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1829 mutex_unlock(&vcpu
->kvm
->lock
);
1830 return X86EMUL_CONTINUE
;
1832 mutex_unlock(&vcpu
->kvm
->lock
);
1834 vcpu
->mmio_needed
= 1;
1835 vcpu
->mmio_phys_addr
= gpa
;
1836 vcpu
->mmio_size
= bytes
;
1837 vcpu
->mmio_is_write
= 1;
1838 memcpy(vcpu
->mmio_data
, val
, bytes
);
1840 return X86EMUL_CONTINUE
;
1843 int emulator_write_emulated(unsigned long addr
,
1846 struct kvm_vcpu
*vcpu
)
1848 /* Crossing a page boundary? */
1849 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1852 now
= -addr
& ~PAGE_MASK
;
1853 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
1854 if (rc
!= X86EMUL_CONTINUE
)
1860 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
1862 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
1864 static int emulator_cmpxchg_emulated(unsigned long addr
,
1868 struct kvm_vcpu
*vcpu
)
1870 static int reported
;
1874 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1876 #ifndef CONFIG_X86_64
1877 /* guests cmpxchg8b have to be emulated atomically */
1884 down_read(&vcpu
->kvm
->slots_lock
);
1885 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1887 if (gpa
== UNMAPPED_GVA
||
1888 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1891 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
1896 down_read(¤t
->mm
->mmap_sem
);
1897 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1898 up_read(¤t
->mm
->mmap_sem
);
1900 kaddr
= kmap_atomic(page
, KM_USER0
);
1901 set_64bit((u64
*)(kaddr
+ offset_in_page(gpa
)), val
);
1902 kunmap_atomic(kaddr
, KM_USER0
);
1903 kvm_release_page_dirty(page
);
1905 up_read(&vcpu
->kvm
->slots_lock
);
1909 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
1912 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1914 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
1917 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1919 return X86EMUL_CONTINUE
;
1922 int emulate_clts(struct kvm_vcpu
*vcpu
)
1924 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
& ~X86_CR0_TS
);
1925 return X86EMUL_CONTINUE
;
1928 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
1930 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1934 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
1935 return X86EMUL_CONTINUE
;
1937 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __FUNCTION__
, dr
);
1938 return X86EMUL_UNHANDLEABLE
;
1942 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1944 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1947 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1949 /* FIXME: better handling */
1950 return X86EMUL_UNHANDLEABLE
;
1952 return X86EMUL_CONTINUE
;
1955 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
1957 static int reported
;
1959 unsigned long rip
= vcpu
->arch
.rip
;
1960 unsigned long rip_linear
;
1962 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
1967 emulator_read_std(rip_linear
, (void *)opcodes
, 4, vcpu
);
1969 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1970 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1973 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
1975 static struct x86_emulate_ops emulate_ops
= {
1976 .read_std
= emulator_read_std
,
1977 .read_emulated
= emulator_read_emulated
,
1978 .write_emulated
= emulator_write_emulated
,
1979 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1982 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1983 struct kvm_run
*run
,
1989 struct decode_cache
*c
;
1991 vcpu
->arch
.mmio_fault_cr2
= cr2
;
1992 kvm_x86_ops
->cache_regs(vcpu
);
1994 vcpu
->mmio_is_write
= 0;
1995 vcpu
->arch
.pio
.string
= 0;
1997 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
1999 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
2001 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
2002 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
2003 vcpu
->arch
.emulate_ctxt
.mode
=
2004 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
2005 ? X86EMUL_MODE_REAL
: cs_l
2006 ? X86EMUL_MODE_PROT64
: cs_db
2007 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
2009 if (vcpu
->arch
.emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
2010 vcpu
->arch
.emulate_ctxt
.cs_base
= 0;
2011 vcpu
->arch
.emulate_ctxt
.ds_base
= 0;
2012 vcpu
->arch
.emulate_ctxt
.es_base
= 0;
2013 vcpu
->arch
.emulate_ctxt
.ss_base
= 0;
2015 vcpu
->arch
.emulate_ctxt
.cs_base
=
2016 get_segment_base(vcpu
, VCPU_SREG_CS
);
2017 vcpu
->arch
.emulate_ctxt
.ds_base
=
2018 get_segment_base(vcpu
, VCPU_SREG_DS
);
2019 vcpu
->arch
.emulate_ctxt
.es_base
=
2020 get_segment_base(vcpu
, VCPU_SREG_ES
);
2021 vcpu
->arch
.emulate_ctxt
.ss_base
=
2022 get_segment_base(vcpu
, VCPU_SREG_SS
);
2025 vcpu
->arch
.emulate_ctxt
.gs_base
=
2026 get_segment_base(vcpu
, VCPU_SREG_GS
);
2027 vcpu
->arch
.emulate_ctxt
.fs_base
=
2028 get_segment_base(vcpu
, VCPU_SREG_FS
);
2030 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2032 /* Reject the instructions other than VMCALL/VMMCALL when
2033 * try to emulate invalid opcode */
2034 c
= &vcpu
->arch
.emulate_ctxt
.decode
;
2035 if ((emulation_type
& EMULTYPE_TRAP_UD
) &&
2036 (!(c
->twobyte
&& c
->b
== 0x01 &&
2037 (c
->modrm_reg
== 0 || c
->modrm_reg
== 3) &&
2038 c
->modrm_mod
== 3 && c
->modrm_rm
== 1)))
2039 return EMULATE_FAIL
;
2041 ++vcpu
->stat
.insn_emulation
;
2043 ++vcpu
->stat
.insn_emulation_fail
;
2044 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2045 return EMULATE_DONE
;
2046 return EMULATE_FAIL
;
2050 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2052 if (vcpu
->arch
.pio
.string
)
2053 return EMULATE_DO_MMIO
;
2055 if ((r
|| vcpu
->mmio_is_write
) && run
) {
2056 run
->exit_reason
= KVM_EXIT_MMIO
;
2057 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
2058 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
2059 run
->mmio
.len
= vcpu
->mmio_size
;
2060 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
2064 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2065 return EMULATE_DONE
;
2066 if (!vcpu
->mmio_needed
) {
2067 kvm_report_emulation_failure(vcpu
, "mmio");
2068 return EMULATE_FAIL
;
2070 return EMULATE_DO_MMIO
;
2073 kvm_x86_ops
->decache_regs(vcpu
);
2074 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
2076 if (vcpu
->mmio_is_write
) {
2077 vcpu
->mmio_needed
= 0;
2078 return EMULATE_DO_MMIO
;
2081 return EMULATE_DONE
;
2083 EXPORT_SYMBOL_GPL(emulate_instruction
);
2085 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
2089 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.pio
.guest_pages
); ++i
)
2090 if (vcpu
->arch
.pio
.guest_pages
[i
]) {
2091 kvm_release_page_dirty(vcpu
->arch
.pio
.guest_pages
[i
]);
2092 vcpu
->arch
.pio
.guest_pages
[i
] = NULL
;
2096 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
2098 void *p
= vcpu
->arch
.pio_data
;
2101 int nr_pages
= vcpu
->arch
.pio
.guest_pages
[1] ? 2 : 1;
2103 q
= vmap(vcpu
->arch
.pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
2106 free_pio_guest_pages(vcpu
);
2109 q
+= vcpu
->arch
.pio
.guest_page_offset
;
2110 bytes
= vcpu
->arch
.pio
.size
* vcpu
->arch
.pio
.cur_count
;
2111 if (vcpu
->arch
.pio
.in
)
2112 memcpy(q
, p
, bytes
);
2114 memcpy(p
, q
, bytes
);
2115 q
-= vcpu
->arch
.pio
.guest_page_offset
;
2117 free_pio_guest_pages(vcpu
);
2121 int complete_pio(struct kvm_vcpu
*vcpu
)
2123 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2127 kvm_x86_ops
->cache_regs(vcpu
);
2131 memcpy(&vcpu
->arch
.regs
[VCPU_REGS_RAX
], vcpu
->arch
.pio_data
,
2135 r
= pio_copy_data(vcpu
);
2137 kvm_x86_ops
->cache_regs(vcpu
);
2144 delta
*= io
->cur_count
;
2146 * The size of the register should really depend on
2147 * current address size.
2149 vcpu
->arch
.regs
[VCPU_REGS_RCX
] -= delta
;
2155 vcpu
->arch
.regs
[VCPU_REGS_RDI
] += delta
;
2157 vcpu
->arch
.regs
[VCPU_REGS_RSI
] += delta
;
2160 kvm_x86_ops
->decache_regs(vcpu
);
2162 io
->count
-= io
->cur_count
;
2168 static void kernel_pio(struct kvm_io_device
*pio_dev
,
2169 struct kvm_vcpu
*vcpu
,
2172 /* TODO: String I/O for in kernel device */
2174 mutex_lock(&vcpu
->kvm
->lock
);
2175 if (vcpu
->arch
.pio
.in
)
2176 kvm_iodevice_read(pio_dev
, vcpu
->arch
.pio
.port
,
2177 vcpu
->arch
.pio
.size
,
2180 kvm_iodevice_write(pio_dev
, vcpu
->arch
.pio
.port
,
2181 vcpu
->arch
.pio
.size
,
2183 mutex_unlock(&vcpu
->kvm
->lock
);
2186 static void pio_string_write(struct kvm_io_device
*pio_dev
,
2187 struct kvm_vcpu
*vcpu
)
2189 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2190 void *pd
= vcpu
->arch
.pio_data
;
2193 mutex_lock(&vcpu
->kvm
->lock
);
2194 for (i
= 0; i
< io
->cur_count
; i
++) {
2195 kvm_iodevice_write(pio_dev
, io
->port
,
2200 mutex_unlock(&vcpu
->kvm
->lock
);
2203 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
2206 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
2209 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2210 int size
, unsigned port
)
2212 struct kvm_io_device
*pio_dev
;
2214 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2215 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2216 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2217 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2218 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= 1;
2219 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2220 vcpu
->arch
.pio
.in
= in
;
2221 vcpu
->arch
.pio
.string
= 0;
2222 vcpu
->arch
.pio
.down
= 0;
2223 vcpu
->arch
.pio
.guest_page_offset
= 0;
2224 vcpu
->arch
.pio
.rep
= 0;
2226 kvm_x86_ops
->cache_regs(vcpu
);
2227 memcpy(vcpu
->arch
.pio_data
, &vcpu
->arch
.regs
[VCPU_REGS_RAX
], 4);
2228 kvm_x86_ops
->decache_regs(vcpu
);
2230 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2232 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2234 kernel_pio(pio_dev
, vcpu
, vcpu
->arch
.pio_data
);
2240 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
2242 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2243 int size
, unsigned long count
, int down
,
2244 gva_t address
, int rep
, unsigned port
)
2246 unsigned now
, in_page
;
2250 struct kvm_io_device
*pio_dev
;
2252 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2253 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2254 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2255 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2256 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= count
;
2257 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2258 vcpu
->arch
.pio
.in
= in
;
2259 vcpu
->arch
.pio
.string
= 1;
2260 vcpu
->arch
.pio
.down
= down
;
2261 vcpu
->arch
.pio
.guest_page_offset
= offset_in_page(address
);
2262 vcpu
->arch
.pio
.rep
= rep
;
2265 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2270 in_page
= PAGE_SIZE
- offset_in_page(address
);
2272 in_page
= offset_in_page(address
) + size
;
2273 now
= min(count
, (unsigned long)in_page
/ size
);
2276 * String I/O straddles page boundary. Pin two guest pages
2277 * so that we satisfy atomicity constraints. Do just one
2278 * transaction to avoid complexity.
2285 * String I/O in reverse. Yuck. Kill the guest, fix later.
2287 pr_unimpl(vcpu
, "guest string pio down\n");
2288 kvm_inject_gp(vcpu
, 0);
2291 vcpu
->run
->io
.count
= now
;
2292 vcpu
->arch
.pio
.cur_count
= now
;
2294 if (vcpu
->arch
.pio
.cur_count
== vcpu
->arch
.pio
.count
)
2295 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2297 for (i
= 0; i
< nr_pages
; ++i
) {
2298 down_read(&vcpu
->kvm
->slots_lock
);
2299 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
2300 vcpu
->arch
.pio
.guest_pages
[i
] = page
;
2301 up_read(&vcpu
->kvm
->slots_lock
);
2303 kvm_inject_gp(vcpu
, 0);
2304 free_pio_guest_pages(vcpu
);
2309 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2310 if (!vcpu
->arch
.pio
.in
) {
2311 /* string PIO write */
2312 ret
= pio_copy_data(vcpu
);
2313 if (ret
>= 0 && pio_dev
) {
2314 pio_string_write(pio_dev
, vcpu
);
2316 if (vcpu
->arch
.pio
.count
== 0)
2320 pr_unimpl(vcpu
, "no string pio read support yet, "
2321 "port %x size %d count %ld\n",
2326 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2328 int kvm_arch_init(void *opaque
)
2331 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
2334 printk(KERN_ERR
"kvm: already loaded the other module\n");
2339 if (!ops
->cpu_has_kvm_support()) {
2340 printk(KERN_ERR
"kvm: no hardware support\n");
2344 if (ops
->disabled_by_bios()) {
2345 printk(KERN_ERR
"kvm: disabled by bios\n");
2350 r
= kvm_mmu_module_init();
2354 kvm_init_msr_list();
2357 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2364 void kvm_arch_exit(void)
2367 kvm_mmu_module_exit();
2370 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
2372 ++vcpu
->stat
.halt_exits
;
2373 if (irqchip_in_kernel(vcpu
->kvm
)) {
2374 vcpu
->arch
.mp_state
= VCPU_MP_STATE_HALTED
;
2375 kvm_vcpu_block(vcpu
);
2376 if (vcpu
->arch
.mp_state
!= VCPU_MP_STATE_RUNNABLE
)
2380 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
2384 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
2386 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
2388 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
2390 kvm_x86_ops
->cache_regs(vcpu
);
2392 nr
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2393 a0
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2394 a1
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2395 a2
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2396 a3
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2398 if (!is_long_mode(vcpu
)) {
2407 case KVM_HC_VAPIC_POLL_IRQ
:
2414 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = ret
;
2415 kvm_x86_ops
->decache_regs(vcpu
);
2416 ++vcpu
->stat
.hypercalls
;
2419 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
2421 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
2423 char instruction
[3];
2428 * Blow out the MMU to ensure that no other VCPU has an active mapping
2429 * to ensure that the updated hypercall appears atomically across all
2432 kvm_mmu_zap_all(vcpu
->kvm
);
2434 kvm_x86_ops
->cache_regs(vcpu
);
2435 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
2436 if (emulator_write_emulated(vcpu
->arch
.rip
, instruction
, 3, vcpu
)
2437 != X86EMUL_CONTINUE
)
2443 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
2445 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
2448 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2450 struct descriptor_table dt
= { limit
, base
};
2452 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2455 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2457 struct descriptor_table dt
= { limit
, base
};
2459 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2462 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
2463 unsigned long *rflags
)
2466 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2469 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
2471 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2474 return vcpu
->arch
.cr0
;
2476 return vcpu
->arch
.cr2
;
2478 return vcpu
->arch
.cr3
;
2480 return vcpu
->arch
.cr4
;
2482 return get_cr8(vcpu
);
2484 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
2489 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
2490 unsigned long *rflags
)
2494 set_cr0(vcpu
, mk_cr_64(vcpu
->arch
.cr0
, val
));
2495 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2498 vcpu
->arch
.cr2
= val
;
2504 set_cr4(vcpu
, mk_cr_64(vcpu
->arch
.cr4
, val
));
2507 set_cr8(vcpu
, val
& 0xfUL
);
2510 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
2514 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
2516 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
2517 int j
, nent
= vcpu
->arch
.cpuid_nent
;
2519 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
2520 /* when no next entry is found, the current entry[i] is reselected */
2521 for (j
= i
+ 1; j
== i
; j
= (j
+ 1) % nent
) {
2522 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
2523 if (ej
->function
== e
->function
) {
2524 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
2528 return 0; /* silence gcc, even though control never reaches here */
2531 /* find an entry with matching function, matching index (if needed), and that
2532 * should be read next (if it's stateful) */
2533 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
2534 u32 function
, u32 index
)
2536 if (e
->function
!= function
)
2538 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
2540 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
2541 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
2546 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
2549 u32 function
, index
;
2550 struct kvm_cpuid_entry2
*e
, *best
;
2552 kvm_x86_ops
->cache_regs(vcpu
);
2553 function
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2554 index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2555 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = 0;
2556 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = 0;
2557 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = 0;
2558 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = 0;
2560 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
2561 e
= &vcpu
->arch
.cpuid_entries
[i
];
2562 if (is_matching_cpuid_entry(e
, function
, index
)) {
2563 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
2564 move_to_next_stateful_cpuid_entry(vcpu
, i
);
2569 * Both basic or both extended?
2571 if (((e
->function
^ function
) & 0x80000000) == 0)
2572 if (!best
|| e
->function
> best
->function
)
2576 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = best
->eax
;
2577 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = best
->ebx
;
2578 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = best
->ecx
;
2579 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = best
->edx
;
2581 kvm_x86_ops
->decache_regs(vcpu
);
2582 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2584 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
2587 * Check if userspace requested an interrupt window, and that the
2588 * interrupt window is open.
2590 * No need to exit to userspace if we already have an interrupt queued.
2592 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
2593 struct kvm_run
*kvm_run
)
2595 return (!vcpu
->arch
.irq_summary
&&
2596 kvm_run
->request_interrupt_window
&&
2597 vcpu
->arch
.interrupt_window_open
&&
2598 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
2601 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
2602 struct kvm_run
*kvm_run
)
2604 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
2605 kvm_run
->cr8
= get_cr8(vcpu
);
2606 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
2607 if (irqchip_in_kernel(vcpu
->kvm
))
2608 kvm_run
->ready_for_interrupt_injection
= 1;
2610 kvm_run
->ready_for_interrupt_injection
=
2611 (vcpu
->arch
.interrupt_window_open
&&
2612 vcpu
->arch
.irq_summary
== 0);
2615 static void vapic_enter(struct kvm_vcpu
*vcpu
)
2617 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2620 if (!apic
|| !apic
->vapic_addr
)
2623 down_read(¤t
->mm
->mmap_sem
);
2624 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2625 up_read(¤t
->mm
->mmap_sem
);
2627 vcpu
->arch
.apic
->vapic_page
= page
;
2630 static void vapic_exit(struct kvm_vcpu
*vcpu
)
2632 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2634 if (!apic
|| !apic
->vapic_addr
)
2637 kvm_release_page_dirty(apic
->vapic_page
);
2638 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2641 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2645 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
)) {
2646 pr_debug("vcpu %d received sipi with vector # %x\n",
2647 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
2648 kvm_lapic_reset(vcpu
);
2649 r
= kvm_x86_ops
->vcpu_reset(vcpu
);
2652 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
2658 if (vcpu
->guest_debug
.enabled
)
2659 kvm_x86_ops
->guest_debug_pre(vcpu
);
2663 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
2664 kvm_mmu_unload(vcpu
);
2666 r
= kvm_mmu_reload(vcpu
);
2670 if (vcpu
->requests
) {
2671 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
2672 __kvm_migrate_apic_timer(vcpu
);
2673 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
2675 kvm_run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
2681 kvm_inject_pending_timer_irqs(vcpu
);
2685 kvm_x86_ops
->prepare_guest_switch(vcpu
);
2686 kvm_load_guest_fpu(vcpu
);
2688 local_irq_disable();
2690 if (need_resched()) {
2698 if (test_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
)) {
2705 if (signal_pending(current
)) {
2709 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2710 ++vcpu
->stat
.signal_exits
;
2714 if (vcpu
->arch
.exception
.pending
)
2715 __queue_exception(vcpu
);
2716 else if (irqchip_in_kernel(vcpu
->kvm
))
2717 kvm_x86_ops
->inject_pending_irq(vcpu
);
2719 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
2721 kvm_lapic_sync_to_vapic(vcpu
);
2723 vcpu
->guest_mode
= 1;
2727 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
2728 kvm_x86_ops
->tlb_flush(vcpu
);
2730 kvm_x86_ops
->run(vcpu
, kvm_run
);
2732 vcpu
->guest_mode
= 0;
2738 * We must have an instruction between local_irq_enable() and
2739 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2740 * the interrupt shadow. The stat.exits increment will do nicely.
2741 * But we need to prevent reordering, hence this barrier():
2750 * Profile KVM exit RIPs:
2752 if (unlikely(prof_on
== KVM_PROFILING
)) {
2753 kvm_x86_ops
->cache_regs(vcpu
);
2754 profile_hit(KVM_PROFILING
, (void *)vcpu
->arch
.rip
);
2757 if (vcpu
->arch
.exception
.pending
&& kvm_x86_ops
->exception_injected(vcpu
))
2758 vcpu
->arch
.exception
.pending
= false;
2760 kvm_lapic_sync_from_vapic(vcpu
);
2762 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
2765 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
2767 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2768 ++vcpu
->stat
.request_irq_exits
;
2771 if (!need_resched())
2781 post_kvm_run_save(vcpu
, kvm_run
);
2788 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2795 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_UNINITIALIZED
)) {
2796 kvm_vcpu_block(vcpu
);
2801 if (vcpu
->sigset_active
)
2802 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2804 /* re-sync apic's tpr */
2805 if (!irqchip_in_kernel(vcpu
->kvm
))
2806 set_cr8(vcpu
, kvm_run
->cr8
);
2808 if (vcpu
->arch
.pio
.cur_count
) {
2809 r
= complete_pio(vcpu
);
2813 #if CONFIG_HAS_IOMEM
2814 if (vcpu
->mmio_needed
) {
2815 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
2816 vcpu
->mmio_read_completed
= 1;
2817 vcpu
->mmio_needed
= 0;
2818 r
= emulate_instruction(vcpu
, kvm_run
,
2819 vcpu
->arch
.mmio_fault_cr2
, 0,
2820 EMULTYPE_NO_DECODE
);
2821 if (r
== EMULATE_DO_MMIO
) {
2823 * Read-modify-write. Back to userspace.
2830 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
2831 kvm_x86_ops
->cache_regs(vcpu
);
2832 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
2833 kvm_x86_ops
->decache_regs(vcpu
);
2836 r
= __vcpu_run(vcpu
, kvm_run
);
2839 if (vcpu
->sigset_active
)
2840 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2846 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2850 kvm_x86_ops
->cache_regs(vcpu
);
2852 regs
->rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2853 regs
->rbx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2854 regs
->rcx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2855 regs
->rdx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2856 regs
->rsi
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2857 regs
->rdi
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
2858 regs
->rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
2859 regs
->rbp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
2860 #ifdef CONFIG_X86_64
2861 regs
->r8
= vcpu
->arch
.regs
[VCPU_REGS_R8
];
2862 regs
->r9
= vcpu
->arch
.regs
[VCPU_REGS_R9
];
2863 regs
->r10
= vcpu
->arch
.regs
[VCPU_REGS_R10
];
2864 regs
->r11
= vcpu
->arch
.regs
[VCPU_REGS_R11
];
2865 regs
->r12
= vcpu
->arch
.regs
[VCPU_REGS_R12
];
2866 regs
->r13
= vcpu
->arch
.regs
[VCPU_REGS_R13
];
2867 regs
->r14
= vcpu
->arch
.regs
[VCPU_REGS_R14
];
2868 regs
->r15
= vcpu
->arch
.regs
[VCPU_REGS_R15
];
2871 regs
->rip
= vcpu
->arch
.rip
;
2872 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2875 * Don't leak debug flags in case they were set for guest debugging
2877 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2878 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2885 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2889 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = regs
->rax
;
2890 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2891 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2892 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2893 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2894 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2895 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2896 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2897 #ifdef CONFIG_X86_64
2898 vcpu
->arch
.regs
[VCPU_REGS_R8
] = regs
->r8
;
2899 vcpu
->arch
.regs
[VCPU_REGS_R9
] = regs
->r9
;
2900 vcpu
->arch
.regs
[VCPU_REGS_R10
] = regs
->r10
;
2901 vcpu
->arch
.regs
[VCPU_REGS_R11
] = regs
->r11
;
2902 vcpu
->arch
.regs
[VCPU_REGS_R12
] = regs
->r12
;
2903 vcpu
->arch
.regs
[VCPU_REGS_R13
] = regs
->r13
;
2904 vcpu
->arch
.regs
[VCPU_REGS_R14
] = regs
->r14
;
2905 vcpu
->arch
.regs
[VCPU_REGS_R15
] = regs
->r15
;
2908 vcpu
->arch
.rip
= regs
->rip
;
2909 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
2911 kvm_x86_ops
->decache_regs(vcpu
);
2918 static void get_segment(struct kvm_vcpu
*vcpu
,
2919 struct kvm_segment
*var
, int seg
)
2921 kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
2924 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
2926 struct kvm_segment cs
;
2928 get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
2932 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
2934 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2935 struct kvm_sregs
*sregs
)
2937 struct descriptor_table dt
;
2942 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2943 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2944 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2945 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2946 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2947 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2949 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2950 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2952 kvm_x86_ops
->get_idt(vcpu
, &dt
);
2953 sregs
->idt
.limit
= dt
.limit
;
2954 sregs
->idt
.base
= dt
.base
;
2955 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
2956 sregs
->gdt
.limit
= dt
.limit
;
2957 sregs
->gdt
.base
= dt
.base
;
2959 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2960 sregs
->cr0
= vcpu
->arch
.cr0
;
2961 sregs
->cr2
= vcpu
->arch
.cr2
;
2962 sregs
->cr3
= vcpu
->arch
.cr3
;
2963 sregs
->cr4
= vcpu
->arch
.cr4
;
2964 sregs
->cr8
= get_cr8(vcpu
);
2965 sregs
->efer
= vcpu
->arch
.shadow_efer
;
2966 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
2968 if (irqchip_in_kernel(vcpu
->kvm
)) {
2969 memset(sregs
->interrupt_bitmap
, 0,
2970 sizeof sregs
->interrupt_bitmap
);
2971 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
2972 if (pending_vec
>= 0)
2973 set_bit(pending_vec
,
2974 (unsigned long *)sregs
->interrupt_bitmap
);
2976 memcpy(sregs
->interrupt_bitmap
, vcpu
->arch
.irq_pending
,
2977 sizeof sregs
->interrupt_bitmap
);
2984 static void set_segment(struct kvm_vcpu
*vcpu
,
2985 struct kvm_segment
*var
, int seg
)
2987 kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
2990 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2991 struct kvm_sregs
*sregs
)
2993 int mmu_reset_needed
= 0;
2994 int i
, pending_vec
, max_bits
;
2995 struct descriptor_table dt
;
2999 dt
.limit
= sregs
->idt
.limit
;
3000 dt
.base
= sregs
->idt
.base
;
3001 kvm_x86_ops
->set_idt(vcpu
, &dt
);
3002 dt
.limit
= sregs
->gdt
.limit
;
3003 dt
.base
= sregs
->gdt
.base
;
3004 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
3006 vcpu
->arch
.cr2
= sregs
->cr2
;
3007 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
3008 vcpu
->arch
.cr3
= sregs
->cr3
;
3010 set_cr8(vcpu
, sregs
->cr8
);
3012 mmu_reset_needed
|= vcpu
->arch
.shadow_efer
!= sregs
->efer
;
3013 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
3014 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
3016 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3018 mmu_reset_needed
|= vcpu
->arch
.cr0
!= sregs
->cr0
;
3019 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
3020 vcpu
->arch
.cr0
= sregs
->cr0
;
3022 mmu_reset_needed
|= vcpu
->arch
.cr4
!= sregs
->cr4
;
3023 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
3024 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
3025 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
3027 if (mmu_reset_needed
)
3028 kvm_mmu_reset_context(vcpu
);
3030 if (!irqchip_in_kernel(vcpu
->kvm
)) {
3031 memcpy(vcpu
->arch
.irq_pending
, sregs
->interrupt_bitmap
,
3032 sizeof vcpu
->arch
.irq_pending
);
3033 vcpu
->arch
.irq_summary
= 0;
3034 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.irq_pending
); ++i
)
3035 if (vcpu
->arch
.irq_pending
[i
])
3036 __set_bit(i
, &vcpu
->arch
.irq_summary
);
3038 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
3039 pending_vec
= find_first_bit(
3040 (const unsigned long *)sregs
->interrupt_bitmap
,
3042 /* Only pending external irq is handled here */
3043 if (pending_vec
< max_bits
) {
3044 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
3045 pr_debug("Set back pending irq %d\n",
3050 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3051 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3052 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3053 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3054 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3055 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3057 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3058 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3065 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
3066 struct kvm_debug_guest
*dbg
)
3072 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
3080 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3081 * we have asm/x86/processor.h
3092 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3093 #ifdef CONFIG_X86_64
3094 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3096 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3101 * Translate a guest virtual address to a guest physical address.
3103 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
3104 struct kvm_translation
*tr
)
3106 unsigned long vaddr
= tr
->linear_address
;
3110 down_read(&vcpu
->kvm
->slots_lock
);
3111 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, vaddr
);
3112 up_read(&vcpu
->kvm
->slots_lock
);
3113 tr
->physical_address
= gpa
;
3114 tr
->valid
= gpa
!= UNMAPPED_GVA
;
3122 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3124 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3128 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
3129 fpu
->fcw
= fxsave
->cwd
;
3130 fpu
->fsw
= fxsave
->swd
;
3131 fpu
->ftwx
= fxsave
->twd
;
3132 fpu
->last_opcode
= fxsave
->fop
;
3133 fpu
->last_ip
= fxsave
->rip
;
3134 fpu
->last_dp
= fxsave
->rdp
;
3135 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
3142 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3144 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3148 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
3149 fxsave
->cwd
= fpu
->fcw
;
3150 fxsave
->swd
= fpu
->fsw
;
3151 fxsave
->twd
= fpu
->ftwx
;
3152 fxsave
->fop
= fpu
->last_opcode
;
3153 fxsave
->rip
= fpu
->last_ip
;
3154 fxsave
->rdp
= fpu
->last_dp
;
3155 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
3162 void fx_init(struct kvm_vcpu
*vcpu
)
3164 unsigned after_mxcsr_mask
;
3166 /* Initialize guest FPU by resetting ours and saving into guest's */
3168 fx_save(&vcpu
->arch
.host_fx_image
);
3170 fx_save(&vcpu
->arch
.guest_fx_image
);
3171 fx_restore(&vcpu
->arch
.host_fx_image
);
3174 vcpu
->arch
.cr0
|= X86_CR0_ET
;
3175 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
3176 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
3177 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
3178 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
3180 EXPORT_SYMBOL_GPL(fx_init
);
3182 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
3184 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
3187 vcpu
->guest_fpu_loaded
= 1;
3188 fx_save(&vcpu
->arch
.host_fx_image
);
3189 fx_restore(&vcpu
->arch
.guest_fx_image
);
3191 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
3193 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
3195 if (!vcpu
->guest_fpu_loaded
)
3198 vcpu
->guest_fpu_loaded
= 0;
3199 fx_save(&vcpu
->arch
.guest_fx_image
);
3200 fx_restore(&vcpu
->arch
.host_fx_image
);
3201 ++vcpu
->stat
.fpu_reload
;
3203 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
3205 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
3207 kvm_x86_ops
->vcpu_free(vcpu
);
3210 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
3213 return kvm_x86_ops
->vcpu_create(kvm
, id
);
3216 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
3220 /* We do fxsave: this must be aligned. */
3221 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
3224 r
= kvm_arch_vcpu_reset(vcpu
);
3226 r
= kvm_mmu_setup(vcpu
);
3233 kvm_x86_ops
->vcpu_free(vcpu
);
3237 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
3240 kvm_mmu_unload(vcpu
);
3243 kvm_x86_ops
->vcpu_free(vcpu
);
3246 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
3248 return kvm_x86_ops
->vcpu_reset(vcpu
);
3251 void kvm_arch_hardware_enable(void *garbage
)
3253 kvm_x86_ops
->hardware_enable(garbage
);
3256 void kvm_arch_hardware_disable(void *garbage
)
3258 kvm_x86_ops
->hardware_disable(garbage
);
3261 int kvm_arch_hardware_setup(void)
3263 return kvm_x86_ops
->hardware_setup();
3266 void kvm_arch_hardware_unsetup(void)
3268 kvm_x86_ops
->hardware_unsetup();
3271 void kvm_arch_check_processor_compat(void *rtn
)
3273 kvm_x86_ops
->check_processor_compatibility(rtn
);
3276 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
3282 BUG_ON(vcpu
->kvm
== NULL
);
3285 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
3286 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
3287 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
3289 vcpu
->arch
.mp_state
= VCPU_MP_STATE_UNINITIALIZED
;
3291 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
3296 vcpu
->arch
.pio_data
= page_address(page
);
3298 r
= kvm_mmu_create(vcpu
);
3300 goto fail_free_pio_data
;
3302 if (irqchip_in_kernel(kvm
)) {
3303 r
= kvm_create_lapic(vcpu
);
3305 goto fail_mmu_destroy
;
3311 kvm_mmu_destroy(vcpu
);
3313 free_page((unsigned long)vcpu
->arch
.pio_data
);
3318 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
3320 kvm_free_lapic(vcpu
);
3321 kvm_mmu_destroy(vcpu
);
3322 free_page((unsigned long)vcpu
->arch
.pio_data
);
3325 struct kvm
*kvm_arch_create_vm(void)
3327 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
3330 return ERR_PTR(-ENOMEM
);
3332 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
3337 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
3340 kvm_mmu_unload(vcpu
);
3344 static void kvm_free_vcpus(struct kvm
*kvm
)
3349 * Unpin any mmu pages first.
3351 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
3353 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
3354 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3355 if (kvm
->vcpus
[i
]) {
3356 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
3357 kvm
->vcpus
[i
] = NULL
;
3363 void kvm_arch_destroy_vm(struct kvm
*kvm
)
3365 kfree(kvm
->arch
.vpic
);
3366 kfree(kvm
->arch
.vioapic
);
3367 kvm_free_vcpus(kvm
);
3368 kvm_free_physmem(kvm
);
3372 int kvm_arch_set_memory_region(struct kvm
*kvm
,
3373 struct kvm_userspace_memory_region
*mem
,
3374 struct kvm_memory_slot old
,
3377 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
3378 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
3380 /*To keep backward compatibility with older userspace,
3381 *x86 needs to hanlde !user_alloc case.
3384 if (npages
&& !old
.rmap
) {
3385 down_write(¤t
->mm
->mmap_sem
);
3386 memslot
->userspace_addr
= do_mmap(NULL
, 0,
3388 PROT_READ
| PROT_WRITE
,
3389 MAP_SHARED
| MAP_ANONYMOUS
,
3391 up_write(¤t
->mm
->mmap_sem
);
3393 if (IS_ERR((void *)memslot
->userspace_addr
))
3394 return PTR_ERR((void *)memslot
->userspace_addr
);
3396 if (!old
.user_alloc
&& old
.rmap
) {
3399 down_write(¤t
->mm
->mmap_sem
);
3400 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
3401 old
.npages
* PAGE_SIZE
);
3402 up_write(¤t
->mm
->mmap_sem
);
3405 "kvm_vm_ioctl_set_memory_region: "
3406 "failed to munmap memory\n");
3411 if (!kvm
->arch
.n_requested_mmu_pages
) {
3412 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
3413 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
3416 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
3417 kvm_flush_remote_tlbs(kvm
);
3422 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
3424 return vcpu
->arch
.mp_state
== VCPU_MP_STATE_RUNNABLE
3425 || vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
;
3428 static void vcpu_kick_intr(void *info
)
3431 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)info
;
3432 printk(KERN_DEBUG
"vcpu_kick_intr %p \n", vcpu
);
3436 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
3438 int ipi_pcpu
= vcpu
->cpu
;
3440 if (waitqueue_active(&vcpu
->wq
)) {
3441 wake_up_interruptible(&vcpu
->wq
);
3442 ++vcpu
->stat
.halt_wakeup
;
3444 if (vcpu
->guest_mode
)
3445 smp_call_function_single(ipi_pcpu
, vcpu_kick_intr
, vcpu
, 0, 0);