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>
22 #include <linux/clocksource.h>
23 #include <linux/kvm.h>
25 #include <linux/vmalloc.h>
26 #include <linux/module.h>
27 #include <linux/mman.h>
28 #include <linux/highmem.h>
30 #include <asm/uaccess.h>
34 #define MAX_IO_MSRS 256
35 #define CR0_RESERVED_BITS \
36 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
37 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
38 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
39 #define CR4_RESERVED_BITS \
40 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
41 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
42 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
43 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
45 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
47 * - enable syscall per default because its emulated by KVM
48 * - enable LME and LMA per default on 64 bit KVM
51 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffafeULL
;
53 static u64 __read_mostly efer_reserved_bits
= 0xfffffffffffffffeULL
;
56 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
57 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
59 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
60 struct kvm_cpuid_entry2 __user
*entries
);
62 struct kvm_x86_ops
*kvm_x86_ops
;
64 struct kvm_stats_debugfs_item debugfs_entries
[] = {
65 { "pf_fixed", VCPU_STAT(pf_fixed
) },
66 { "pf_guest", VCPU_STAT(pf_guest
) },
67 { "tlb_flush", VCPU_STAT(tlb_flush
) },
68 { "invlpg", VCPU_STAT(invlpg
) },
69 { "exits", VCPU_STAT(exits
) },
70 { "io_exits", VCPU_STAT(io_exits
) },
71 { "mmio_exits", VCPU_STAT(mmio_exits
) },
72 { "signal_exits", VCPU_STAT(signal_exits
) },
73 { "irq_window", VCPU_STAT(irq_window_exits
) },
74 { "halt_exits", VCPU_STAT(halt_exits
) },
75 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
76 { "hypercalls", VCPU_STAT(hypercalls
) },
77 { "request_irq", VCPU_STAT(request_irq_exits
) },
78 { "irq_exits", VCPU_STAT(irq_exits
) },
79 { "host_state_reload", VCPU_STAT(host_state_reload
) },
80 { "efer_reload", VCPU_STAT(efer_reload
) },
81 { "fpu_reload", VCPU_STAT(fpu_reload
) },
82 { "insn_emulation", VCPU_STAT(insn_emulation
) },
83 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
84 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
85 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
86 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
87 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
88 { "mmu_flooded", VM_STAT(mmu_flooded
) },
89 { "mmu_recycled", VM_STAT(mmu_recycled
) },
90 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
91 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
92 { "largepages", VM_STAT(lpages
) },
97 unsigned long segment_base(u16 selector
)
99 struct descriptor_table gdt
;
100 struct desc_struct
*d
;
101 unsigned long table_base
;
107 asm("sgdt %0" : "=m"(gdt
));
108 table_base
= gdt
.base
;
110 if (selector
& 4) { /* from ldt */
113 asm("sldt %0" : "=g"(ldt_selector
));
114 table_base
= segment_base(ldt_selector
);
116 d
= (struct desc_struct
*)(table_base
+ (selector
& ~7));
117 v
= d
->base0
| ((unsigned long)d
->base1
<< 16) |
118 ((unsigned long)d
->base2
<< 24);
120 if (d
->s
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
121 v
|= ((unsigned long)((struct ldttss_desc64
*)d
)->base3
) << 32;
125 EXPORT_SYMBOL_GPL(segment_base
);
127 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
129 if (irqchip_in_kernel(vcpu
->kvm
))
130 return vcpu
->arch
.apic_base
;
132 return vcpu
->arch
.apic_base
;
134 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
136 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
138 /* TODO: reserve bits check */
139 if (irqchip_in_kernel(vcpu
->kvm
))
140 kvm_lapic_set_base(vcpu
, data
);
142 vcpu
->arch
.apic_base
= data
;
144 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
146 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
148 WARN_ON(vcpu
->arch
.exception
.pending
);
149 vcpu
->arch
.exception
.pending
= true;
150 vcpu
->arch
.exception
.has_error_code
= false;
151 vcpu
->arch
.exception
.nr
= nr
;
153 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
155 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
158 ++vcpu
->stat
.pf_guest
;
159 if (vcpu
->arch
.exception
.pending
) {
160 if (vcpu
->arch
.exception
.nr
== PF_VECTOR
) {
161 printk(KERN_DEBUG
"kvm: inject_page_fault:"
162 " double fault 0x%lx\n", addr
);
163 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
164 vcpu
->arch
.exception
.error_code
= 0;
165 } else if (vcpu
->arch
.exception
.nr
== DF_VECTOR
) {
166 /* triple fault -> shutdown */
167 set_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
);
171 vcpu
->arch
.cr2
= addr
;
172 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
175 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
177 WARN_ON(vcpu
->arch
.exception
.pending
);
178 vcpu
->arch
.exception
.pending
= true;
179 vcpu
->arch
.exception
.has_error_code
= true;
180 vcpu
->arch
.exception
.nr
= nr
;
181 vcpu
->arch
.exception
.error_code
= error_code
;
183 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
185 static void __queue_exception(struct kvm_vcpu
*vcpu
)
187 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
188 vcpu
->arch
.exception
.has_error_code
,
189 vcpu
->arch
.exception
.error_code
);
193 * Load the pae pdptrs. Return true is they are all valid.
195 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
197 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
198 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
201 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
203 down_read(&vcpu
->kvm
->slots_lock
);
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
));
220 up_read(&vcpu
->kvm
->slots_lock
);
224 EXPORT_SYMBOL_GPL(load_pdptrs
);
226 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
228 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
232 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
235 down_read(&vcpu
->kvm
->slots_lock
);
236 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
239 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
241 up_read(&vcpu
->kvm
->slots_lock
);
246 void kvm_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
248 if (cr0
& CR0_RESERVED_BITS
) {
249 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
250 cr0
, vcpu
->arch
.cr0
);
251 kvm_inject_gp(vcpu
, 0);
255 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
256 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
257 kvm_inject_gp(vcpu
, 0);
261 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
262 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
263 "and a clear PE flag\n");
264 kvm_inject_gp(vcpu
, 0);
268 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
270 if ((vcpu
->arch
.shadow_efer
& EFER_LME
)) {
274 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
275 "in long mode while PAE is disabled\n");
276 kvm_inject_gp(vcpu
, 0);
279 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
281 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
282 "in long mode while CS.L == 1\n");
283 kvm_inject_gp(vcpu
, 0);
289 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
290 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
292 kvm_inject_gp(vcpu
, 0);
298 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
299 vcpu
->arch
.cr0
= cr0
;
301 kvm_mmu_reset_context(vcpu
);
304 EXPORT_SYMBOL_GPL(kvm_set_cr0
);
306 void kvm_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
308 kvm_set_cr0(vcpu
, (vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f));
310 EXPORT_SYMBOL_GPL(kvm_lmsw
);
312 void kvm_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
314 if (cr4
& CR4_RESERVED_BITS
) {
315 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
316 kvm_inject_gp(vcpu
, 0);
320 if (is_long_mode(vcpu
)) {
321 if (!(cr4
& X86_CR4_PAE
)) {
322 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
324 kvm_inject_gp(vcpu
, 0);
327 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
328 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
329 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
330 kvm_inject_gp(vcpu
, 0);
334 if (cr4
& X86_CR4_VMXE
) {
335 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
336 kvm_inject_gp(vcpu
, 0);
339 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
340 vcpu
->arch
.cr4
= cr4
;
341 kvm_mmu_reset_context(vcpu
);
343 EXPORT_SYMBOL_GPL(kvm_set_cr4
);
345 void kvm_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
347 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
348 kvm_mmu_flush_tlb(vcpu
);
352 if (is_long_mode(vcpu
)) {
353 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
354 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
355 kvm_inject_gp(vcpu
, 0);
360 if (cr3
& CR3_PAE_RESERVED_BITS
) {
362 "set_cr3: #GP, reserved bits\n");
363 kvm_inject_gp(vcpu
, 0);
366 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
367 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
369 kvm_inject_gp(vcpu
, 0);
374 * We don't check reserved bits in nonpae mode, because
375 * this isn't enforced, and VMware depends on this.
379 down_read(&vcpu
->kvm
->slots_lock
);
381 * Does the new cr3 value map to physical memory? (Note, we
382 * catch an invalid cr3 even in real-mode, because it would
383 * cause trouble later on when we turn on paging anyway.)
385 * A real CPU would silently accept an invalid cr3 and would
386 * attempt to use it - with largely undefined (and often hard
387 * to debug) behavior on the guest side.
389 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
390 kvm_inject_gp(vcpu
, 0);
392 vcpu
->arch
.cr3
= cr3
;
393 vcpu
->arch
.mmu
.new_cr3(vcpu
);
395 up_read(&vcpu
->kvm
->slots_lock
);
397 EXPORT_SYMBOL_GPL(kvm_set_cr3
);
399 void kvm_set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
401 if (cr8
& CR8_RESERVED_BITS
) {
402 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
403 kvm_inject_gp(vcpu
, 0);
406 if (irqchip_in_kernel(vcpu
->kvm
))
407 kvm_lapic_set_tpr(vcpu
, cr8
);
409 vcpu
->arch
.cr8
= cr8
;
411 EXPORT_SYMBOL_GPL(kvm_set_cr8
);
413 unsigned long kvm_get_cr8(struct kvm_vcpu
*vcpu
)
415 if (irqchip_in_kernel(vcpu
->kvm
))
416 return kvm_lapic_get_cr8(vcpu
);
418 return vcpu
->arch
.cr8
;
420 EXPORT_SYMBOL_GPL(kvm_get_cr8
);
423 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
424 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
426 * This list is modified at module load time to reflect the
427 * capabilities of the host cpu.
429 static u32 msrs_to_save
[] = {
430 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
433 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
435 MSR_IA32_TIME_STAMP_COUNTER
, MSR_KVM_SYSTEM_TIME
, MSR_KVM_WALL_CLOCK
,
436 MSR_IA32_PERF_STATUS
,
439 static unsigned num_msrs_to_save
;
441 static u32 emulated_msrs
[] = {
442 MSR_IA32_MISC_ENABLE
,
445 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
447 if (efer
& efer_reserved_bits
) {
448 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
450 kvm_inject_gp(vcpu
, 0);
455 && (vcpu
->arch
.shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
456 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
457 kvm_inject_gp(vcpu
, 0);
461 kvm_x86_ops
->set_efer(vcpu
, efer
);
464 efer
|= vcpu
->arch
.shadow_efer
& EFER_LMA
;
466 vcpu
->arch
.shadow_efer
= efer
;
469 void kvm_enable_efer_bits(u64 mask
)
471 efer_reserved_bits
&= ~mask
;
473 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits
);
477 * Writes msr value into into the appropriate "register".
478 * Returns 0 on success, non-0 otherwise.
479 * Assumes vcpu_load() was already called.
481 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
483 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
487 * Adapt set_msr() to msr_io()'s calling convention
489 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
491 return kvm_set_msr(vcpu
, index
, *data
);
494 static void kvm_write_wall_clock(struct kvm
*kvm
, gpa_t wall_clock
)
497 struct kvm_wall_clock wc
;
498 struct timespec wc_ts
;
505 down_read(&kvm
->slots_lock
);
506 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
508 wc_ts
= current_kernel_time();
509 wc
.wc_sec
= wc_ts
.tv_sec
;
510 wc
.wc_nsec
= wc_ts
.tv_nsec
;
511 wc
.wc_version
= version
;
513 kvm_write_guest(kvm
, wall_clock
, &wc
, sizeof(wc
));
516 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
517 up_read(&kvm
->slots_lock
);
520 static void kvm_write_guest_time(struct kvm_vcpu
*v
)
524 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
527 if ((!vcpu
->time_page
))
530 /* Keep irq disabled to prevent changes to the clock */
531 local_irq_save(flags
);
532 kvm_get_msr(v
, MSR_IA32_TIME_STAMP_COUNTER
,
533 &vcpu
->hv_clock
.tsc_timestamp
);
535 local_irq_restore(flags
);
537 /* With all the info we got, fill in the values */
539 vcpu
->hv_clock
.system_time
= ts
.tv_nsec
+
540 (NSEC_PER_SEC
* (u64
)ts
.tv_sec
);
542 * The interface expects us to write an even number signaling that the
543 * update is finished. Since the guest won't see the intermediate
544 * state, we just write "2" at the end
546 vcpu
->hv_clock
.version
= 2;
548 shared_kaddr
= kmap_atomic(vcpu
->time_page
, KM_USER0
);
550 memcpy(shared_kaddr
+ vcpu
->time_offset
, &vcpu
->hv_clock
,
551 sizeof(vcpu
->hv_clock
));
553 kunmap_atomic(shared_kaddr
, KM_USER0
);
555 mark_page_dirty(v
->kvm
, vcpu
->time
>> PAGE_SHIFT
);
559 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
563 set_efer(vcpu
, data
);
565 case MSR_IA32_MC0_STATUS
:
566 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
569 case MSR_IA32_MCG_STATUS
:
570 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
573 case MSR_IA32_MCG_CTL
:
574 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
577 case MSR_IA32_UCODE_REV
:
578 case MSR_IA32_UCODE_WRITE
:
579 case 0x200 ... 0x2ff: /* MTRRs */
581 case MSR_IA32_APICBASE
:
582 kvm_set_apic_base(vcpu
, data
);
584 case MSR_IA32_MISC_ENABLE
:
585 vcpu
->arch
.ia32_misc_enable_msr
= data
;
587 case MSR_KVM_WALL_CLOCK
:
588 vcpu
->kvm
->arch
.wall_clock
= data
;
589 kvm_write_wall_clock(vcpu
->kvm
, data
);
591 case MSR_KVM_SYSTEM_TIME
: {
592 if (vcpu
->arch
.time_page
) {
593 kvm_release_page_dirty(vcpu
->arch
.time_page
);
594 vcpu
->arch
.time_page
= NULL
;
597 vcpu
->arch
.time
= data
;
599 /* we verify if the enable bit is set... */
603 /* ...but clean it before doing the actual write */
604 vcpu
->arch
.time_offset
= data
& ~(PAGE_MASK
| 1);
606 vcpu
->arch
.hv_clock
.tsc_to_system_mul
=
607 clocksource_khz2mult(tsc_khz
, 22);
608 vcpu
->arch
.hv_clock
.tsc_shift
= 22;
610 down_read(¤t
->mm
->mmap_sem
);
611 down_read(&vcpu
->kvm
->slots_lock
);
612 vcpu
->arch
.time_page
=
613 gfn_to_page(vcpu
->kvm
, data
>> PAGE_SHIFT
);
614 up_read(&vcpu
->kvm
->slots_lock
);
615 up_read(¤t
->mm
->mmap_sem
);
617 if (is_error_page(vcpu
->arch
.time_page
)) {
618 kvm_release_page_clean(vcpu
->arch
.time_page
);
619 vcpu
->arch
.time_page
= NULL
;
622 kvm_write_guest_time(vcpu
);
626 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n", msr
, data
);
631 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
635 * Reads an msr value (of 'msr_index') into 'pdata'.
636 * Returns 0 on success, non-0 otherwise.
637 * Assumes vcpu_load() was already called.
639 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
641 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
644 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
649 case 0xc0010010: /* SYSCFG */
650 case 0xc0010015: /* HWCR */
651 case MSR_IA32_PLATFORM_ID
:
652 case MSR_IA32_P5_MC_ADDR
:
653 case MSR_IA32_P5_MC_TYPE
:
654 case MSR_IA32_MC0_CTL
:
655 case MSR_IA32_MCG_STATUS
:
656 case MSR_IA32_MCG_CAP
:
657 case MSR_IA32_MCG_CTL
:
658 case MSR_IA32_MC0_MISC
:
659 case MSR_IA32_MC0_MISC
+4:
660 case MSR_IA32_MC0_MISC
+8:
661 case MSR_IA32_MC0_MISC
+12:
662 case MSR_IA32_MC0_MISC
+16:
663 case MSR_IA32_UCODE_REV
:
664 case MSR_IA32_EBL_CR_POWERON
:
667 case 0x200 ... 0x2ff:
670 case 0xcd: /* fsb frequency */
673 case MSR_IA32_APICBASE
:
674 data
= kvm_get_apic_base(vcpu
);
676 case MSR_IA32_MISC_ENABLE
:
677 data
= vcpu
->arch
.ia32_misc_enable_msr
;
679 case MSR_IA32_PERF_STATUS
:
680 /* TSC increment by tick */
683 data
|= (((uint64_t)4ULL) << 40);
686 data
= vcpu
->arch
.shadow_efer
;
688 case MSR_KVM_WALL_CLOCK
:
689 data
= vcpu
->kvm
->arch
.wall_clock
;
691 case MSR_KVM_SYSTEM_TIME
:
692 data
= vcpu
->arch
.time
;
695 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
701 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
704 * Read or write a bunch of msrs. All parameters are kernel addresses.
706 * @return number of msrs set successfully.
708 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
709 struct kvm_msr_entry
*entries
,
710 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
711 unsigned index
, u64
*data
))
717 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
718 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
727 * Read or write a bunch of msrs. Parameters are user addresses.
729 * @return number of msrs set successfully.
731 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
732 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
733 unsigned index
, u64
*data
),
736 struct kvm_msrs msrs
;
737 struct kvm_msr_entry
*entries
;
742 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
746 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
750 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
751 entries
= vmalloc(size
);
756 if (copy_from_user(entries
, user_msrs
->entries
, size
))
759 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
764 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
776 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
779 void decache_vcpus_on_cpu(int cpu
)
782 struct kvm_vcpu
*vcpu
;
785 spin_lock(&kvm_lock
);
786 list_for_each_entry(vm
, &vm_list
, vm_list
)
787 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
792 * If the vcpu is locked, then it is running on some
793 * other cpu and therefore it is not cached on the
796 * If it's not locked, check the last cpu it executed
799 if (mutex_trylock(&vcpu
->mutex
)) {
800 if (vcpu
->cpu
== cpu
) {
801 kvm_x86_ops
->vcpu_decache(vcpu
);
804 mutex_unlock(&vcpu
->mutex
);
807 spin_unlock(&kvm_lock
);
810 int kvm_dev_ioctl_check_extension(long ext
)
815 case KVM_CAP_IRQCHIP
:
817 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
818 case KVM_CAP_USER_MEMORY
:
819 case KVM_CAP_SET_TSS_ADDR
:
820 case KVM_CAP_EXT_CPUID
:
821 case KVM_CAP_CLOCKSOURCE
:
823 case KVM_CAP_NOP_IO_DELAY
:
827 r
= !kvm_x86_ops
->cpu_has_accelerated_tpr();
829 case KVM_CAP_NR_VCPUS
:
832 case KVM_CAP_NR_MEMSLOTS
:
833 r
= KVM_MEMORY_SLOTS
;
843 long kvm_arch_dev_ioctl(struct file
*filp
,
844 unsigned int ioctl
, unsigned long arg
)
846 void __user
*argp
= (void __user
*)arg
;
850 case KVM_GET_MSR_INDEX_LIST
: {
851 struct kvm_msr_list __user
*user_msr_list
= argp
;
852 struct kvm_msr_list msr_list
;
856 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
859 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
860 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
863 if (n
< num_msrs_to_save
)
866 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
867 num_msrs_to_save
* sizeof(u32
)))
869 if (copy_to_user(user_msr_list
->indices
870 + num_msrs_to_save
* sizeof(u32
),
872 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
877 case KVM_GET_SUPPORTED_CPUID
: {
878 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
879 struct kvm_cpuid2 cpuid
;
882 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
884 r
= kvm_dev_ioctl_get_supported_cpuid(&cpuid
,
890 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
902 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
904 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
905 kvm_write_guest_time(vcpu
);
908 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
910 kvm_x86_ops
->vcpu_put(vcpu
);
911 kvm_put_guest_fpu(vcpu
);
914 static int is_efer_nx(void)
918 rdmsrl(MSR_EFER
, efer
);
919 return efer
& EFER_NX
;
922 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
925 struct kvm_cpuid_entry2
*e
, *entry
;
928 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
929 e
= &vcpu
->arch
.cpuid_entries
[i
];
930 if (e
->function
== 0x80000001) {
935 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
936 entry
->edx
&= ~(1 << 20);
937 printk(KERN_INFO
"kvm: guest NX capability removed\n");
941 /* when an old userspace process fills a new kernel module */
942 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
943 struct kvm_cpuid
*cpuid
,
944 struct kvm_cpuid_entry __user
*entries
)
947 struct kvm_cpuid_entry
*cpuid_entries
;
950 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
953 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
957 if (copy_from_user(cpuid_entries
, entries
,
958 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
960 for (i
= 0; i
< cpuid
->nent
; i
++) {
961 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
962 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
963 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
964 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
965 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
966 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
967 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
968 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
969 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
970 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
972 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
973 cpuid_fix_nx_cap(vcpu
);
977 vfree(cpuid_entries
);
982 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
983 struct kvm_cpuid2
*cpuid
,
984 struct kvm_cpuid_entry2 __user
*entries
)
989 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
992 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
993 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
995 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
1002 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
1003 struct kvm_cpuid2
*cpuid
,
1004 struct kvm_cpuid_entry2 __user
*entries
)
1009 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
1012 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
1013 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
1018 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
1022 static inline u32
bit(int bitno
)
1024 return 1 << (bitno
& 31);
1027 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1030 entry
->function
= function
;
1031 entry
->index
= index
;
1032 cpuid_count(entry
->function
, entry
->index
,
1033 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
1037 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1038 u32 index
, int *nent
, int maxnent
)
1040 const u32 kvm_supported_word0_x86_features
= bit(X86_FEATURE_FPU
) |
1041 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1042 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1043 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1044 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1045 bit(X86_FEATURE_SEP
) | bit(X86_FEATURE_PGE
) |
1046 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1047 bit(X86_FEATURE_CLFLSH
) | bit(X86_FEATURE_MMX
) |
1048 bit(X86_FEATURE_FXSR
) | bit(X86_FEATURE_XMM
) |
1049 bit(X86_FEATURE_XMM2
) | bit(X86_FEATURE_SELFSNOOP
);
1050 const u32 kvm_supported_word1_x86_features
= bit(X86_FEATURE_FPU
) |
1051 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1052 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1053 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1054 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1055 bit(X86_FEATURE_PGE
) |
1056 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1057 bit(X86_FEATURE_MMX
) | bit(X86_FEATURE_FXSR
) |
1058 bit(X86_FEATURE_SYSCALL
) |
1059 (bit(X86_FEATURE_NX
) && is_efer_nx()) |
1060 #ifdef CONFIG_X86_64
1061 bit(X86_FEATURE_LM
) |
1063 bit(X86_FEATURE_MMXEXT
) |
1064 bit(X86_FEATURE_3DNOWEXT
) |
1065 bit(X86_FEATURE_3DNOW
);
1066 const u32 kvm_supported_word3_x86_features
=
1067 bit(X86_FEATURE_XMM3
) | bit(X86_FEATURE_CX16
);
1068 const u32 kvm_supported_word6_x86_features
=
1069 bit(X86_FEATURE_LAHF_LM
) | bit(X86_FEATURE_CMP_LEGACY
);
1071 /* all func 2 cpuid_count() should be called on the same cpu */
1073 do_cpuid_1_ent(entry
, function
, index
);
1078 entry
->eax
= min(entry
->eax
, (u32
)0xb);
1081 entry
->edx
&= kvm_supported_word0_x86_features
;
1082 entry
->ecx
&= kvm_supported_word3_x86_features
;
1084 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1085 * may return different values. This forces us to get_cpu() before
1086 * issuing the first command, and also to emulate this annoying behavior
1087 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1089 int t
, times
= entry
->eax
& 0xff;
1091 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1092 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
1093 do_cpuid_1_ent(&entry
[t
], function
, 0);
1094 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1099 /* function 4 and 0xb have additional index. */
1103 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1104 /* read more entries until cache_type is zero */
1105 for (i
= 1; *nent
< maxnent
; ++i
) {
1106 cache_type
= entry
[i
- 1].eax
& 0x1f;
1109 do_cpuid_1_ent(&entry
[i
], function
, i
);
1111 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1119 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1120 /* read more entries until level_type is zero */
1121 for (i
= 1; *nent
< maxnent
; ++i
) {
1122 level_type
= entry
[i
- 1].ecx
& 0xff;
1125 do_cpuid_1_ent(&entry
[i
], function
, i
);
1127 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1133 entry
->eax
= min(entry
->eax
, 0x8000001a);
1136 entry
->edx
&= kvm_supported_word1_x86_features
;
1137 entry
->ecx
&= kvm_supported_word6_x86_features
;
1143 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
1144 struct kvm_cpuid_entry2 __user
*entries
)
1146 struct kvm_cpuid_entry2
*cpuid_entries
;
1147 int limit
, nent
= 0, r
= -E2BIG
;
1150 if (cpuid
->nent
< 1)
1153 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
1157 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
1158 limit
= cpuid_entries
[0].eax
;
1159 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1160 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1161 &nent
, cpuid
->nent
);
1163 if (nent
>= cpuid
->nent
)
1166 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
1167 limit
= cpuid_entries
[nent
- 1].eax
;
1168 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1169 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1170 &nent
, cpuid
->nent
);
1172 if (copy_to_user(entries
, cpuid_entries
,
1173 nent
* sizeof(struct kvm_cpuid_entry2
)))
1179 vfree(cpuid_entries
);
1184 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
1185 struct kvm_lapic_state
*s
)
1188 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
1194 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
1195 struct kvm_lapic_state
*s
)
1198 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1199 kvm_apic_post_state_restore(vcpu
);
1205 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1206 struct kvm_interrupt
*irq
)
1208 if (irq
->irq
< 0 || irq
->irq
>= 256)
1210 if (irqchip_in_kernel(vcpu
->kvm
))
1214 set_bit(irq
->irq
, vcpu
->arch
.irq_pending
);
1215 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
1222 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
1223 struct kvm_tpr_access_ctl
*tac
)
1227 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
1231 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1232 unsigned int ioctl
, unsigned long arg
)
1234 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1235 void __user
*argp
= (void __user
*)arg
;
1239 case KVM_GET_LAPIC
: {
1240 struct kvm_lapic_state lapic
;
1242 memset(&lapic
, 0, sizeof lapic
);
1243 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, &lapic
);
1247 if (copy_to_user(argp
, &lapic
, sizeof lapic
))
1252 case KVM_SET_LAPIC
: {
1253 struct kvm_lapic_state lapic
;
1256 if (copy_from_user(&lapic
, argp
, sizeof lapic
))
1258 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, &lapic
);;
1264 case KVM_INTERRUPT
: {
1265 struct kvm_interrupt irq
;
1268 if (copy_from_user(&irq
, argp
, sizeof irq
))
1270 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1276 case KVM_SET_CPUID
: {
1277 struct kvm_cpuid __user
*cpuid_arg
= argp
;
1278 struct kvm_cpuid cpuid
;
1281 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1283 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
1288 case KVM_SET_CPUID2
: {
1289 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1290 struct kvm_cpuid2 cpuid
;
1293 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1295 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
1296 cpuid_arg
->entries
);
1301 case KVM_GET_CPUID2
: {
1302 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1303 struct kvm_cpuid2 cpuid
;
1306 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1308 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
1309 cpuid_arg
->entries
);
1313 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1319 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
1322 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
1324 case KVM_TPR_ACCESS_REPORTING
: {
1325 struct kvm_tpr_access_ctl tac
;
1328 if (copy_from_user(&tac
, argp
, sizeof tac
))
1330 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
1334 if (copy_to_user(argp
, &tac
, sizeof tac
))
1339 case KVM_SET_VAPIC_ADDR
: {
1340 struct kvm_vapic_addr va
;
1343 if (!irqchip_in_kernel(vcpu
->kvm
))
1346 if (copy_from_user(&va
, argp
, sizeof va
))
1349 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
1359 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
1363 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
1365 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
1369 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
1370 u32 kvm_nr_mmu_pages
)
1372 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
1375 down_write(&kvm
->slots_lock
);
1377 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
1378 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
1380 up_write(&kvm
->slots_lock
);
1384 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
1386 return kvm
->arch
.n_alloc_mmu_pages
;
1389 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1392 struct kvm_mem_alias
*alias
;
1394 for (i
= 0; i
< kvm
->arch
.naliases
; ++i
) {
1395 alias
= &kvm
->arch
.aliases
[i
];
1396 if (gfn
>= alias
->base_gfn
1397 && gfn
< alias
->base_gfn
+ alias
->npages
)
1398 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1404 * Set a new alias region. Aliases map a portion of physical memory into
1405 * another portion. This is useful for memory windows, for example the PC
1408 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
1409 struct kvm_memory_alias
*alias
)
1412 struct kvm_mem_alias
*p
;
1415 /* General sanity checks */
1416 if (alias
->memory_size
& (PAGE_SIZE
- 1))
1418 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
1420 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
1422 if (alias
->guest_phys_addr
+ alias
->memory_size
1423 < alias
->guest_phys_addr
)
1425 if (alias
->target_phys_addr
+ alias
->memory_size
1426 < alias
->target_phys_addr
)
1429 down_write(&kvm
->slots_lock
);
1431 p
= &kvm
->arch
.aliases
[alias
->slot
];
1432 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
1433 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
1434 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
1436 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
1437 if (kvm
->arch
.aliases
[n
- 1].npages
)
1439 kvm
->arch
.naliases
= n
;
1441 kvm_mmu_zap_all(kvm
);
1443 up_write(&kvm
->slots_lock
);
1451 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1456 switch (chip
->chip_id
) {
1457 case KVM_IRQCHIP_PIC_MASTER
:
1458 memcpy(&chip
->chip
.pic
,
1459 &pic_irqchip(kvm
)->pics
[0],
1460 sizeof(struct kvm_pic_state
));
1462 case KVM_IRQCHIP_PIC_SLAVE
:
1463 memcpy(&chip
->chip
.pic
,
1464 &pic_irqchip(kvm
)->pics
[1],
1465 sizeof(struct kvm_pic_state
));
1467 case KVM_IRQCHIP_IOAPIC
:
1468 memcpy(&chip
->chip
.ioapic
,
1469 ioapic_irqchip(kvm
),
1470 sizeof(struct kvm_ioapic_state
));
1479 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1484 switch (chip
->chip_id
) {
1485 case KVM_IRQCHIP_PIC_MASTER
:
1486 memcpy(&pic_irqchip(kvm
)->pics
[0],
1488 sizeof(struct kvm_pic_state
));
1490 case KVM_IRQCHIP_PIC_SLAVE
:
1491 memcpy(&pic_irqchip(kvm
)->pics
[1],
1493 sizeof(struct kvm_pic_state
));
1495 case KVM_IRQCHIP_IOAPIC
:
1496 memcpy(ioapic_irqchip(kvm
),
1498 sizeof(struct kvm_ioapic_state
));
1504 kvm_pic_update_irq(pic_irqchip(kvm
));
1508 static int kvm_vm_ioctl_get_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1512 memcpy(ps
, &kvm
->arch
.vpit
->pit_state
, sizeof(struct kvm_pit_state
));
1516 static int kvm_vm_ioctl_set_pit(struct kvm
*kvm
, struct kvm_pit_state
*ps
)
1520 memcpy(&kvm
->arch
.vpit
->pit_state
, ps
, sizeof(struct kvm_pit_state
));
1521 kvm_pit_load_count(kvm
, 0, ps
->channels
[0].count
);
1526 * Get (and clear) the dirty memory log for a memory slot.
1528 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1529 struct kvm_dirty_log
*log
)
1533 struct kvm_memory_slot
*memslot
;
1536 down_write(&kvm
->slots_lock
);
1538 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1542 /* If nothing is dirty, don't bother messing with page tables. */
1544 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
1545 kvm_flush_remote_tlbs(kvm
);
1546 memslot
= &kvm
->memslots
[log
->slot
];
1547 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1548 memset(memslot
->dirty_bitmap
, 0, n
);
1552 up_write(&kvm
->slots_lock
);
1556 long kvm_arch_vm_ioctl(struct file
*filp
,
1557 unsigned int ioctl
, unsigned long arg
)
1559 struct kvm
*kvm
= filp
->private_data
;
1560 void __user
*argp
= (void __user
*)arg
;
1564 case KVM_SET_TSS_ADDR
:
1565 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
1569 case KVM_SET_MEMORY_REGION
: {
1570 struct kvm_memory_region kvm_mem
;
1571 struct kvm_userspace_memory_region kvm_userspace_mem
;
1574 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
1576 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
1577 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
1578 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
1579 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
1580 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1585 case KVM_SET_NR_MMU_PAGES
:
1586 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1590 case KVM_GET_NR_MMU_PAGES
:
1591 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1593 case KVM_SET_MEMORY_ALIAS
: {
1594 struct kvm_memory_alias alias
;
1597 if (copy_from_user(&alias
, argp
, sizeof alias
))
1599 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
1604 case KVM_CREATE_IRQCHIP
:
1606 kvm
->arch
.vpic
= kvm_create_pic(kvm
);
1607 if (kvm
->arch
.vpic
) {
1608 r
= kvm_ioapic_init(kvm
);
1610 kfree(kvm
->arch
.vpic
);
1611 kvm
->arch
.vpic
= NULL
;
1617 case KVM_CREATE_PIT
:
1619 kvm
->arch
.vpit
= kvm_create_pit(kvm
);
1623 case KVM_IRQ_LINE
: {
1624 struct kvm_irq_level irq_event
;
1627 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1629 if (irqchip_in_kernel(kvm
)) {
1630 mutex_lock(&kvm
->lock
);
1631 if (irq_event
.irq
< 16)
1632 kvm_pic_set_irq(pic_irqchip(kvm
),
1635 kvm_ioapic_set_irq(kvm
->arch
.vioapic
,
1638 mutex_unlock(&kvm
->lock
);
1643 case KVM_GET_IRQCHIP
: {
1644 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1645 struct kvm_irqchip chip
;
1648 if (copy_from_user(&chip
, argp
, sizeof chip
))
1651 if (!irqchip_in_kernel(kvm
))
1653 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
1657 if (copy_to_user(argp
, &chip
, sizeof chip
))
1662 case KVM_SET_IRQCHIP
: {
1663 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1664 struct kvm_irqchip chip
;
1667 if (copy_from_user(&chip
, argp
, sizeof chip
))
1670 if (!irqchip_in_kernel(kvm
))
1672 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
1679 struct kvm_pit_state ps
;
1681 if (copy_from_user(&ps
, argp
, sizeof ps
))
1684 if (!kvm
->arch
.vpit
)
1686 r
= kvm_vm_ioctl_get_pit(kvm
, &ps
);
1690 if (copy_to_user(argp
, &ps
, sizeof ps
))
1696 struct kvm_pit_state ps
;
1698 if (copy_from_user(&ps
, argp
, sizeof ps
))
1701 if (!kvm
->arch
.vpit
)
1703 r
= kvm_vm_ioctl_set_pit(kvm
, &ps
);
1716 static void kvm_init_msr_list(void)
1721 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1722 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1725 msrs_to_save
[j
] = msrs_to_save
[i
];
1728 num_msrs_to_save
= j
;
1732 * Only apic need an MMIO device hook, so shortcut now..
1734 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
1737 struct kvm_io_device
*dev
;
1739 if (vcpu
->arch
.apic
) {
1740 dev
= &vcpu
->arch
.apic
->dev
;
1741 if (dev
->in_range(dev
, addr
))
1748 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1751 struct kvm_io_device
*dev
;
1753 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
);
1755 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1759 int emulator_read_std(unsigned long addr
,
1762 struct kvm_vcpu
*vcpu
)
1765 int r
= X86EMUL_CONTINUE
;
1767 down_read(&vcpu
->kvm
->slots_lock
);
1769 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1770 unsigned offset
= addr
& (PAGE_SIZE
-1);
1771 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1774 if (gpa
== UNMAPPED_GVA
) {
1775 r
= X86EMUL_PROPAGATE_FAULT
;
1778 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, tocopy
);
1780 r
= X86EMUL_UNHANDLEABLE
;
1789 up_read(&vcpu
->kvm
->slots_lock
);
1792 EXPORT_SYMBOL_GPL(emulator_read_std
);
1794 static int emulator_read_emulated(unsigned long addr
,
1797 struct kvm_vcpu
*vcpu
)
1799 struct kvm_io_device
*mmio_dev
;
1802 if (vcpu
->mmio_read_completed
) {
1803 memcpy(val
, vcpu
->mmio_data
, bytes
);
1804 vcpu
->mmio_read_completed
= 0;
1805 return X86EMUL_CONTINUE
;
1808 down_read(&vcpu
->kvm
->slots_lock
);
1809 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1810 up_read(&vcpu
->kvm
->slots_lock
);
1812 /* For APIC access vmexit */
1813 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1816 if (emulator_read_std(addr
, val
, bytes
, vcpu
)
1817 == X86EMUL_CONTINUE
)
1818 return X86EMUL_CONTINUE
;
1819 if (gpa
== UNMAPPED_GVA
)
1820 return X86EMUL_PROPAGATE_FAULT
;
1824 * Is this MMIO handled locally?
1826 mutex_lock(&vcpu
->kvm
->lock
);
1827 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1829 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1830 mutex_unlock(&vcpu
->kvm
->lock
);
1831 return X86EMUL_CONTINUE
;
1833 mutex_unlock(&vcpu
->kvm
->lock
);
1835 vcpu
->mmio_needed
= 1;
1836 vcpu
->mmio_phys_addr
= gpa
;
1837 vcpu
->mmio_size
= bytes
;
1838 vcpu
->mmio_is_write
= 0;
1840 return X86EMUL_UNHANDLEABLE
;
1843 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1844 const void *val
, int bytes
)
1848 down_read(&vcpu
->kvm
->slots_lock
);
1849 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
1851 up_read(&vcpu
->kvm
->slots_lock
);
1854 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1855 up_read(&vcpu
->kvm
->slots_lock
);
1859 static int emulator_write_emulated_onepage(unsigned long addr
,
1862 struct kvm_vcpu
*vcpu
)
1864 struct kvm_io_device
*mmio_dev
;
1867 down_read(&vcpu
->kvm
->slots_lock
);
1868 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1869 up_read(&vcpu
->kvm
->slots_lock
);
1871 if (gpa
== UNMAPPED_GVA
) {
1872 kvm_inject_page_fault(vcpu
, addr
, 2);
1873 return X86EMUL_PROPAGATE_FAULT
;
1876 /* For APIC access vmexit */
1877 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1880 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1881 return X86EMUL_CONTINUE
;
1885 * Is this MMIO handled locally?
1887 mutex_lock(&vcpu
->kvm
->lock
);
1888 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1890 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1891 mutex_unlock(&vcpu
->kvm
->lock
);
1892 return X86EMUL_CONTINUE
;
1894 mutex_unlock(&vcpu
->kvm
->lock
);
1896 vcpu
->mmio_needed
= 1;
1897 vcpu
->mmio_phys_addr
= gpa
;
1898 vcpu
->mmio_size
= bytes
;
1899 vcpu
->mmio_is_write
= 1;
1900 memcpy(vcpu
->mmio_data
, val
, bytes
);
1902 return X86EMUL_CONTINUE
;
1905 int emulator_write_emulated(unsigned long addr
,
1908 struct kvm_vcpu
*vcpu
)
1910 /* Crossing a page boundary? */
1911 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1914 now
= -addr
& ~PAGE_MASK
;
1915 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
1916 if (rc
!= X86EMUL_CONTINUE
)
1922 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
1924 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
1926 static int emulator_cmpxchg_emulated(unsigned long addr
,
1930 struct kvm_vcpu
*vcpu
)
1932 static int reported
;
1936 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1938 #ifndef CONFIG_X86_64
1939 /* guests cmpxchg8b have to be emulated atomically */
1946 down_read(&vcpu
->kvm
->slots_lock
);
1947 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1949 if (gpa
== UNMAPPED_GVA
||
1950 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1953 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
1958 down_read(¤t
->mm
->mmap_sem
);
1959 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1960 up_read(¤t
->mm
->mmap_sem
);
1962 kaddr
= kmap_atomic(page
, KM_USER0
);
1963 set_64bit((u64
*)(kaddr
+ offset_in_page(gpa
)), val
);
1964 kunmap_atomic(kaddr
, KM_USER0
);
1965 kvm_release_page_dirty(page
);
1967 up_read(&vcpu
->kvm
->slots_lock
);
1971 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
1974 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1976 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
1979 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1981 return X86EMUL_CONTINUE
;
1984 int emulate_clts(struct kvm_vcpu
*vcpu
)
1986 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
& ~X86_CR0_TS
);
1987 return X86EMUL_CONTINUE
;
1990 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
1992 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1996 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
1997 return X86EMUL_CONTINUE
;
1999 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __func__
, dr
);
2000 return X86EMUL_UNHANDLEABLE
;
2004 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
2006 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
2009 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
2011 /* FIXME: better handling */
2012 return X86EMUL_UNHANDLEABLE
;
2014 return X86EMUL_CONTINUE
;
2017 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
2019 static int reported
;
2021 unsigned long rip
= vcpu
->arch
.rip
;
2022 unsigned long rip_linear
;
2024 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
2029 emulator_read_std(rip_linear
, (void *)opcodes
, 4, vcpu
);
2031 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
2032 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
2035 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
2037 static struct x86_emulate_ops emulate_ops
= {
2038 .read_std
= emulator_read_std
,
2039 .read_emulated
= emulator_read_emulated
,
2040 .write_emulated
= emulator_write_emulated
,
2041 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
2044 int emulate_instruction(struct kvm_vcpu
*vcpu
,
2045 struct kvm_run
*run
,
2051 struct decode_cache
*c
;
2053 vcpu
->arch
.mmio_fault_cr2
= cr2
;
2054 kvm_x86_ops
->cache_regs(vcpu
);
2056 vcpu
->mmio_is_write
= 0;
2057 vcpu
->arch
.pio
.string
= 0;
2059 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
2061 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
2063 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
2064 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
2065 vcpu
->arch
.emulate_ctxt
.mode
=
2066 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
2067 ? X86EMUL_MODE_REAL
: cs_l
2068 ? X86EMUL_MODE_PROT64
: cs_db
2069 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
2071 if (vcpu
->arch
.emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
2072 vcpu
->arch
.emulate_ctxt
.cs_base
= 0;
2073 vcpu
->arch
.emulate_ctxt
.ds_base
= 0;
2074 vcpu
->arch
.emulate_ctxt
.es_base
= 0;
2075 vcpu
->arch
.emulate_ctxt
.ss_base
= 0;
2077 vcpu
->arch
.emulate_ctxt
.cs_base
=
2078 get_segment_base(vcpu
, VCPU_SREG_CS
);
2079 vcpu
->arch
.emulate_ctxt
.ds_base
=
2080 get_segment_base(vcpu
, VCPU_SREG_DS
);
2081 vcpu
->arch
.emulate_ctxt
.es_base
=
2082 get_segment_base(vcpu
, VCPU_SREG_ES
);
2083 vcpu
->arch
.emulate_ctxt
.ss_base
=
2084 get_segment_base(vcpu
, VCPU_SREG_SS
);
2087 vcpu
->arch
.emulate_ctxt
.gs_base
=
2088 get_segment_base(vcpu
, VCPU_SREG_GS
);
2089 vcpu
->arch
.emulate_ctxt
.fs_base
=
2090 get_segment_base(vcpu
, VCPU_SREG_FS
);
2092 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2094 /* Reject the instructions other than VMCALL/VMMCALL when
2095 * try to emulate invalid opcode */
2096 c
= &vcpu
->arch
.emulate_ctxt
.decode
;
2097 if ((emulation_type
& EMULTYPE_TRAP_UD
) &&
2098 (!(c
->twobyte
&& c
->b
== 0x01 &&
2099 (c
->modrm_reg
== 0 || c
->modrm_reg
== 3) &&
2100 c
->modrm_mod
== 3 && c
->modrm_rm
== 1)))
2101 return EMULATE_FAIL
;
2103 ++vcpu
->stat
.insn_emulation
;
2105 ++vcpu
->stat
.insn_emulation_fail
;
2106 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2107 return EMULATE_DONE
;
2108 return EMULATE_FAIL
;
2112 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2114 if (vcpu
->arch
.pio
.string
)
2115 return EMULATE_DO_MMIO
;
2117 if ((r
|| vcpu
->mmio_is_write
) && run
) {
2118 run
->exit_reason
= KVM_EXIT_MMIO
;
2119 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
2120 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
2121 run
->mmio
.len
= vcpu
->mmio_size
;
2122 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
2126 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2127 return EMULATE_DONE
;
2128 if (!vcpu
->mmio_needed
) {
2129 kvm_report_emulation_failure(vcpu
, "mmio");
2130 return EMULATE_FAIL
;
2132 return EMULATE_DO_MMIO
;
2135 kvm_x86_ops
->decache_regs(vcpu
);
2136 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
2138 if (vcpu
->mmio_is_write
) {
2139 vcpu
->mmio_needed
= 0;
2140 return EMULATE_DO_MMIO
;
2143 return EMULATE_DONE
;
2145 EXPORT_SYMBOL_GPL(emulate_instruction
);
2147 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
2151 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.pio
.guest_pages
); ++i
)
2152 if (vcpu
->arch
.pio
.guest_pages
[i
]) {
2153 kvm_release_page_dirty(vcpu
->arch
.pio
.guest_pages
[i
]);
2154 vcpu
->arch
.pio
.guest_pages
[i
] = NULL
;
2158 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
2160 void *p
= vcpu
->arch
.pio_data
;
2163 int nr_pages
= vcpu
->arch
.pio
.guest_pages
[1] ? 2 : 1;
2165 q
= vmap(vcpu
->arch
.pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
2168 free_pio_guest_pages(vcpu
);
2171 q
+= vcpu
->arch
.pio
.guest_page_offset
;
2172 bytes
= vcpu
->arch
.pio
.size
* vcpu
->arch
.pio
.cur_count
;
2173 if (vcpu
->arch
.pio
.in
)
2174 memcpy(q
, p
, bytes
);
2176 memcpy(p
, q
, bytes
);
2177 q
-= vcpu
->arch
.pio
.guest_page_offset
;
2179 free_pio_guest_pages(vcpu
);
2183 int complete_pio(struct kvm_vcpu
*vcpu
)
2185 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2189 kvm_x86_ops
->cache_regs(vcpu
);
2193 memcpy(&vcpu
->arch
.regs
[VCPU_REGS_RAX
], vcpu
->arch
.pio_data
,
2197 r
= pio_copy_data(vcpu
);
2199 kvm_x86_ops
->cache_regs(vcpu
);
2206 delta
*= io
->cur_count
;
2208 * The size of the register should really depend on
2209 * current address size.
2211 vcpu
->arch
.regs
[VCPU_REGS_RCX
] -= delta
;
2217 vcpu
->arch
.regs
[VCPU_REGS_RDI
] += delta
;
2219 vcpu
->arch
.regs
[VCPU_REGS_RSI
] += delta
;
2222 kvm_x86_ops
->decache_regs(vcpu
);
2224 io
->count
-= io
->cur_count
;
2230 static void kernel_pio(struct kvm_io_device
*pio_dev
,
2231 struct kvm_vcpu
*vcpu
,
2234 /* TODO: String I/O for in kernel device */
2236 mutex_lock(&vcpu
->kvm
->lock
);
2237 if (vcpu
->arch
.pio
.in
)
2238 kvm_iodevice_read(pio_dev
, vcpu
->arch
.pio
.port
,
2239 vcpu
->arch
.pio
.size
,
2242 kvm_iodevice_write(pio_dev
, vcpu
->arch
.pio
.port
,
2243 vcpu
->arch
.pio
.size
,
2245 mutex_unlock(&vcpu
->kvm
->lock
);
2248 static void pio_string_write(struct kvm_io_device
*pio_dev
,
2249 struct kvm_vcpu
*vcpu
)
2251 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2252 void *pd
= vcpu
->arch
.pio_data
;
2255 mutex_lock(&vcpu
->kvm
->lock
);
2256 for (i
= 0; i
< io
->cur_count
; i
++) {
2257 kvm_iodevice_write(pio_dev
, io
->port
,
2262 mutex_unlock(&vcpu
->kvm
->lock
);
2265 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
2268 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
2271 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2272 int size
, unsigned port
)
2274 struct kvm_io_device
*pio_dev
;
2276 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2277 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2278 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2279 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2280 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= 1;
2281 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2282 vcpu
->arch
.pio
.in
= in
;
2283 vcpu
->arch
.pio
.string
= 0;
2284 vcpu
->arch
.pio
.down
= 0;
2285 vcpu
->arch
.pio
.guest_page_offset
= 0;
2286 vcpu
->arch
.pio
.rep
= 0;
2288 kvm_x86_ops
->cache_regs(vcpu
);
2289 memcpy(vcpu
->arch
.pio_data
, &vcpu
->arch
.regs
[VCPU_REGS_RAX
], 4);
2290 kvm_x86_ops
->decache_regs(vcpu
);
2292 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2294 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2296 kernel_pio(pio_dev
, vcpu
, vcpu
->arch
.pio_data
);
2302 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
2304 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2305 int size
, unsigned long count
, int down
,
2306 gva_t address
, int rep
, unsigned port
)
2308 unsigned now
, in_page
;
2312 struct kvm_io_device
*pio_dev
;
2314 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2315 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2316 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2317 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2318 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= count
;
2319 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2320 vcpu
->arch
.pio
.in
= in
;
2321 vcpu
->arch
.pio
.string
= 1;
2322 vcpu
->arch
.pio
.down
= down
;
2323 vcpu
->arch
.pio
.guest_page_offset
= offset_in_page(address
);
2324 vcpu
->arch
.pio
.rep
= rep
;
2327 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2332 in_page
= PAGE_SIZE
- offset_in_page(address
);
2334 in_page
= offset_in_page(address
) + size
;
2335 now
= min(count
, (unsigned long)in_page
/ size
);
2338 * String I/O straddles page boundary. Pin two guest pages
2339 * so that we satisfy atomicity constraints. Do just one
2340 * transaction to avoid complexity.
2347 * String I/O in reverse. Yuck. Kill the guest, fix later.
2349 pr_unimpl(vcpu
, "guest string pio down\n");
2350 kvm_inject_gp(vcpu
, 0);
2353 vcpu
->run
->io
.count
= now
;
2354 vcpu
->arch
.pio
.cur_count
= now
;
2356 if (vcpu
->arch
.pio
.cur_count
== vcpu
->arch
.pio
.count
)
2357 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2359 for (i
= 0; i
< nr_pages
; ++i
) {
2360 down_read(&vcpu
->kvm
->slots_lock
);
2361 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
2362 vcpu
->arch
.pio
.guest_pages
[i
] = page
;
2363 up_read(&vcpu
->kvm
->slots_lock
);
2365 kvm_inject_gp(vcpu
, 0);
2366 free_pio_guest_pages(vcpu
);
2371 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2372 if (!vcpu
->arch
.pio
.in
) {
2373 /* string PIO write */
2374 ret
= pio_copy_data(vcpu
);
2375 if (ret
>= 0 && pio_dev
) {
2376 pio_string_write(pio_dev
, vcpu
);
2378 if (vcpu
->arch
.pio
.count
== 0)
2382 pr_unimpl(vcpu
, "no string pio read support yet, "
2383 "port %x size %d count %ld\n",
2388 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2390 int kvm_arch_init(void *opaque
)
2393 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
2396 printk(KERN_ERR
"kvm: already loaded the other module\n");
2401 if (!ops
->cpu_has_kvm_support()) {
2402 printk(KERN_ERR
"kvm: no hardware support\n");
2406 if (ops
->disabled_by_bios()) {
2407 printk(KERN_ERR
"kvm: disabled by bios\n");
2412 r
= kvm_mmu_module_init();
2416 kvm_init_msr_list();
2419 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2426 void kvm_arch_exit(void)
2429 kvm_mmu_module_exit();
2432 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
2434 ++vcpu
->stat
.halt_exits
;
2435 if (irqchip_in_kernel(vcpu
->kvm
)) {
2436 vcpu
->arch
.mp_state
= VCPU_MP_STATE_HALTED
;
2437 kvm_vcpu_block(vcpu
);
2438 if (vcpu
->arch
.mp_state
!= VCPU_MP_STATE_RUNNABLE
)
2442 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
2446 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
2448 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
2450 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
2452 kvm_x86_ops
->cache_regs(vcpu
);
2454 nr
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2455 a0
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2456 a1
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2457 a2
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2458 a3
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2460 if (!is_long_mode(vcpu
)) {
2469 case KVM_HC_VAPIC_POLL_IRQ
:
2476 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = ret
;
2477 kvm_x86_ops
->decache_regs(vcpu
);
2478 ++vcpu
->stat
.hypercalls
;
2481 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
2483 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
2485 char instruction
[3];
2490 * Blow out the MMU to ensure that no other VCPU has an active mapping
2491 * to ensure that the updated hypercall appears atomically across all
2494 kvm_mmu_zap_all(vcpu
->kvm
);
2496 kvm_x86_ops
->cache_regs(vcpu
);
2497 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
2498 if (emulator_write_emulated(vcpu
->arch
.rip
, instruction
, 3, vcpu
)
2499 != X86EMUL_CONTINUE
)
2505 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
2507 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
2510 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2512 struct descriptor_table dt
= { limit
, base
};
2514 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2517 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2519 struct descriptor_table dt
= { limit
, base
};
2521 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2524 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
2525 unsigned long *rflags
)
2527 kvm_lmsw(vcpu
, msw
);
2528 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2531 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
2533 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2536 return vcpu
->arch
.cr0
;
2538 return vcpu
->arch
.cr2
;
2540 return vcpu
->arch
.cr3
;
2542 return vcpu
->arch
.cr4
;
2544 return kvm_get_cr8(vcpu
);
2546 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
2551 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
2552 unsigned long *rflags
)
2556 kvm_set_cr0(vcpu
, mk_cr_64(vcpu
->arch
.cr0
, val
));
2557 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2560 vcpu
->arch
.cr2
= val
;
2563 kvm_set_cr3(vcpu
, val
);
2566 kvm_set_cr4(vcpu
, mk_cr_64(vcpu
->arch
.cr4
, val
));
2569 kvm_set_cr8(vcpu
, val
& 0xfUL
);
2572 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __func__
, cr
);
2576 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
2578 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
2579 int j
, nent
= vcpu
->arch
.cpuid_nent
;
2581 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
2582 /* when no next entry is found, the current entry[i] is reselected */
2583 for (j
= i
+ 1; j
== i
; j
= (j
+ 1) % nent
) {
2584 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
2585 if (ej
->function
== e
->function
) {
2586 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
2590 return 0; /* silence gcc, even though control never reaches here */
2593 /* find an entry with matching function, matching index (if needed), and that
2594 * should be read next (if it's stateful) */
2595 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
2596 u32 function
, u32 index
)
2598 if (e
->function
!= function
)
2600 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
2602 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
2603 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
2608 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
2611 u32 function
, index
;
2612 struct kvm_cpuid_entry2
*e
, *best
;
2614 kvm_x86_ops
->cache_regs(vcpu
);
2615 function
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2616 index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2617 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = 0;
2618 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = 0;
2619 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = 0;
2620 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = 0;
2622 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
2623 e
= &vcpu
->arch
.cpuid_entries
[i
];
2624 if (is_matching_cpuid_entry(e
, function
, index
)) {
2625 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
2626 move_to_next_stateful_cpuid_entry(vcpu
, i
);
2631 * Both basic or both extended?
2633 if (((e
->function
^ function
) & 0x80000000) == 0)
2634 if (!best
|| e
->function
> best
->function
)
2638 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = best
->eax
;
2639 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = best
->ebx
;
2640 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = best
->ecx
;
2641 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = best
->edx
;
2643 kvm_x86_ops
->decache_regs(vcpu
);
2644 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2646 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
2649 * Check if userspace requested an interrupt window, and that the
2650 * interrupt window is open.
2652 * No need to exit to userspace if we already have an interrupt queued.
2654 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
2655 struct kvm_run
*kvm_run
)
2657 return (!vcpu
->arch
.irq_summary
&&
2658 kvm_run
->request_interrupt_window
&&
2659 vcpu
->arch
.interrupt_window_open
&&
2660 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
2663 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
2664 struct kvm_run
*kvm_run
)
2666 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
2667 kvm_run
->cr8
= kvm_get_cr8(vcpu
);
2668 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
2669 if (irqchip_in_kernel(vcpu
->kvm
))
2670 kvm_run
->ready_for_interrupt_injection
= 1;
2672 kvm_run
->ready_for_interrupt_injection
=
2673 (vcpu
->arch
.interrupt_window_open
&&
2674 vcpu
->arch
.irq_summary
== 0);
2677 static void vapic_enter(struct kvm_vcpu
*vcpu
)
2679 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2682 if (!apic
|| !apic
->vapic_addr
)
2685 down_read(¤t
->mm
->mmap_sem
);
2686 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2687 up_read(¤t
->mm
->mmap_sem
);
2689 vcpu
->arch
.apic
->vapic_page
= page
;
2692 static void vapic_exit(struct kvm_vcpu
*vcpu
)
2694 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2696 if (!apic
|| !apic
->vapic_addr
)
2699 kvm_release_page_dirty(apic
->vapic_page
);
2700 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2703 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2707 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
)) {
2708 pr_debug("vcpu %d received sipi with vector # %x\n",
2709 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
2710 kvm_lapic_reset(vcpu
);
2711 r
= kvm_x86_ops
->vcpu_reset(vcpu
);
2714 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
2720 if (vcpu
->guest_debug
.enabled
)
2721 kvm_x86_ops
->guest_debug_pre(vcpu
);
2725 if (test_and_clear_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
))
2726 kvm_mmu_unload(vcpu
);
2728 r
= kvm_mmu_reload(vcpu
);
2732 if (vcpu
->requests
) {
2733 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
2734 __kvm_migrate_apic_timer(vcpu
);
2735 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
2737 kvm_run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
2741 if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT
, &vcpu
->requests
)) {
2742 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
2748 kvm_inject_pending_timer_irqs(vcpu
);
2752 kvm_x86_ops
->prepare_guest_switch(vcpu
);
2753 kvm_load_guest_fpu(vcpu
);
2755 local_irq_disable();
2757 if (need_resched()) {
2765 if (test_bit(KVM_REQ_MMU_RELOAD
, &vcpu
->requests
)) {
2772 if (signal_pending(current
)) {
2776 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2777 ++vcpu
->stat
.signal_exits
;
2781 if (vcpu
->arch
.exception
.pending
)
2782 __queue_exception(vcpu
);
2783 else if (irqchip_in_kernel(vcpu
->kvm
))
2784 kvm_x86_ops
->inject_pending_irq(vcpu
);
2786 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
2788 kvm_lapic_sync_to_vapic(vcpu
);
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():
2817 * Profile KVM exit RIPs:
2819 if (unlikely(prof_on
== KVM_PROFILING
)) {
2820 kvm_x86_ops
->cache_regs(vcpu
);
2821 profile_hit(KVM_PROFILING
, (void *)vcpu
->arch
.rip
);
2824 if (vcpu
->arch
.exception
.pending
&& kvm_x86_ops
->exception_injected(vcpu
))
2825 vcpu
->arch
.exception
.pending
= false;
2827 kvm_lapic_sync_from_vapic(vcpu
);
2829 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
2832 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
2834 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2835 ++vcpu
->stat
.request_irq_exits
;
2838 if (!need_resched())
2848 post_kvm_run_save(vcpu
, kvm_run
);
2855 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2862 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_UNINITIALIZED
)) {
2863 kvm_vcpu_block(vcpu
);
2868 if (vcpu
->sigset_active
)
2869 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2871 /* re-sync apic's tpr */
2872 if (!irqchip_in_kernel(vcpu
->kvm
))
2873 kvm_set_cr8(vcpu
, kvm_run
->cr8
);
2875 if (vcpu
->arch
.pio
.cur_count
) {
2876 r
= complete_pio(vcpu
);
2880 #if CONFIG_HAS_IOMEM
2881 if (vcpu
->mmio_needed
) {
2882 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
2883 vcpu
->mmio_read_completed
= 1;
2884 vcpu
->mmio_needed
= 0;
2885 r
= emulate_instruction(vcpu
, kvm_run
,
2886 vcpu
->arch
.mmio_fault_cr2
, 0,
2887 EMULTYPE_NO_DECODE
);
2888 if (r
== EMULATE_DO_MMIO
) {
2890 * Read-modify-write. Back to userspace.
2897 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
2898 kvm_x86_ops
->cache_regs(vcpu
);
2899 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
2900 kvm_x86_ops
->decache_regs(vcpu
);
2903 r
= __vcpu_run(vcpu
, kvm_run
);
2906 if (vcpu
->sigset_active
)
2907 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2913 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2917 kvm_x86_ops
->cache_regs(vcpu
);
2919 regs
->rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2920 regs
->rbx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2921 regs
->rcx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2922 regs
->rdx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2923 regs
->rsi
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2924 regs
->rdi
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
2925 regs
->rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
2926 regs
->rbp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
2927 #ifdef CONFIG_X86_64
2928 regs
->r8
= vcpu
->arch
.regs
[VCPU_REGS_R8
];
2929 regs
->r9
= vcpu
->arch
.regs
[VCPU_REGS_R9
];
2930 regs
->r10
= vcpu
->arch
.regs
[VCPU_REGS_R10
];
2931 regs
->r11
= vcpu
->arch
.regs
[VCPU_REGS_R11
];
2932 regs
->r12
= vcpu
->arch
.regs
[VCPU_REGS_R12
];
2933 regs
->r13
= vcpu
->arch
.regs
[VCPU_REGS_R13
];
2934 regs
->r14
= vcpu
->arch
.regs
[VCPU_REGS_R14
];
2935 regs
->r15
= vcpu
->arch
.regs
[VCPU_REGS_R15
];
2938 regs
->rip
= vcpu
->arch
.rip
;
2939 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2942 * Don't leak debug flags in case they were set for guest debugging
2944 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2945 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2952 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2956 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = regs
->rax
;
2957 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2958 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2959 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2960 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2961 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2962 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2963 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2964 #ifdef CONFIG_X86_64
2965 vcpu
->arch
.regs
[VCPU_REGS_R8
] = regs
->r8
;
2966 vcpu
->arch
.regs
[VCPU_REGS_R9
] = regs
->r9
;
2967 vcpu
->arch
.regs
[VCPU_REGS_R10
] = regs
->r10
;
2968 vcpu
->arch
.regs
[VCPU_REGS_R11
] = regs
->r11
;
2969 vcpu
->arch
.regs
[VCPU_REGS_R12
] = regs
->r12
;
2970 vcpu
->arch
.regs
[VCPU_REGS_R13
] = regs
->r13
;
2971 vcpu
->arch
.regs
[VCPU_REGS_R14
] = regs
->r14
;
2972 vcpu
->arch
.regs
[VCPU_REGS_R15
] = regs
->r15
;
2975 vcpu
->arch
.rip
= regs
->rip
;
2976 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
2978 kvm_x86_ops
->decache_regs(vcpu
);
2985 static void get_segment(struct kvm_vcpu
*vcpu
,
2986 struct kvm_segment
*var
, int seg
)
2988 kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
2991 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
2993 struct kvm_segment cs
;
2995 get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
2999 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
3001 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
3002 struct kvm_sregs
*sregs
)
3004 struct descriptor_table dt
;
3009 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3010 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3011 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3012 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3013 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3014 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3016 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3017 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3019 kvm_x86_ops
->get_idt(vcpu
, &dt
);
3020 sregs
->idt
.limit
= dt
.limit
;
3021 sregs
->idt
.base
= dt
.base
;
3022 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
3023 sregs
->gdt
.limit
= dt
.limit
;
3024 sregs
->gdt
.base
= dt
.base
;
3026 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3027 sregs
->cr0
= vcpu
->arch
.cr0
;
3028 sregs
->cr2
= vcpu
->arch
.cr2
;
3029 sregs
->cr3
= vcpu
->arch
.cr3
;
3030 sregs
->cr4
= vcpu
->arch
.cr4
;
3031 sregs
->cr8
= kvm_get_cr8(vcpu
);
3032 sregs
->efer
= vcpu
->arch
.shadow_efer
;
3033 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
3035 if (irqchip_in_kernel(vcpu
->kvm
)) {
3036 memset(sregs
->interrupt_bitmap
, 0,
3037 sizeof sregs
->interrupt_bitmap
);
3038 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
3039 if (pending_vec
>= 0)
3040 set_bit(pending_vec
,
3041 (unsigned long *)sregs
->interrupt_bitmap
);
3043 memcpy(sregs
->interrupt_bitmap
, vcpu
->arch
.irq_pending
,
3044 sizeof sregs
->interrupt_bitmap
);
3051 static void set_segment(struct kvm_vcpu
*vcpu
,
3052 struct kvm_segment
*var
, int seg
)
3054 kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
3057 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
3058 struct kvm_sregs
*sregs
)
3060 int mmu_reset_needed
= 0;
3061 int i
, pending_vec
, max_bits
;
3062 struct descriptor_table dt
;
3066 dt
.limit
= sregs
->idt
.limit
;
3067 dt
.base
= sregs
->idt
.base
;
3068 kvm_x86_ops
->set_idt(vcpu
, &dt
);
3069 dt
.limit
= sregs
->gdt
.limit
;
3070 dt
.base
= sregs
->gdt
.base
;
3071 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
3073 vcpu
->arch
.cr2
= sregs
->cr2
;
3074 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
3075 vcpu
->arch
.cr3
= sregs
->cr3
;
3077 kvm_set_cr8(vcpu
, sregs
->cr8
);
3079 mmu_reset_needed
|= vcpu
->arch
.shadow_efer
!= sregs
->efer
;
3080 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
3081 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
3083 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3085 mmu_reset_needed
|= vcpu
->arch
.cr0
!= sregs
->cr0
;
3086 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
3087 vcpu
->arch
.cr0
= sregs
->cr0
;
3089 mmu_reset_needed
|= vcpu
->arch
.cr4
!= sregs
->cr4
;
3090 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
3091 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
3092 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
3094 if (mmu_reset_needed
)
3095 kvm_mmu_reset_context(vcpu
);
3097 if (!irqchip_in_kernel(vcpu
->kvm
)) {
3098 memcpy(vcpu
->arch
.irq_pending
, sregs
->interrupt_bitmap
,
3099 sizeof vcpu
->arch
.irq_pending
);
3100 vcpu
->arch
.irq_summary
= 0;
3101 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.irq_pending
); ++i
)
3102 if (vcpu
->arch
.irq_pending
[i
])
3103 __set_bit(i
, &vcpu
->arch
.irq_summary
);
3105 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
3106 pending_vec
= find_first_bit(
3107 (const unsigned long *)sregs
->interrupt_bitmap
,
3109 /* Only pending external irq is handled here */
3110 if (pending_vec
< max_bits
) {
3111 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
3112 pr_debug("Set back pending irq %d\n",
3117 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3118 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3119 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3120 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3121 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3122 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3124 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3125 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3132 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
3133 struct kvm_debug_guest
*dbg
)
3139 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
3147 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3148 * we have asm/x86/processor.h
3159 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3160 #ifdef CONFIG_X86_64
3161 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3163 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3168 * Translate a guest virtual address to a guest physical address.
3170 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
3171 struct kvm_translation
*tr
)
3173 unsigned long vaddr
= tr
->linear_address
;
3177 down_read(&vcpu
->kvm
->slots_lock
);
3178 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, vaddr
);
3179 up_read(&vcpu
->kvm
->slots_lock
);
3180 tr
->physical_address
= gpa
;
3181 tr
->valid
= gpa
!= UNMAPPED_GVA
;
3189 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3191 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3195 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
3196 fpu
->fcw
= fxsave
->cwd
;
3197 fpu
->fsw
= fxsave
->swd
;
3198 fpu
->ftwx
= fxsave
->twd
;
3199 fpu
->last_opcode
= fxsave
->fop
;
3200 fpu
->last_ip
= fxsave
->rip
;
3201 fpu
->last_dp
= fxsave
->rdp
;
3202 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
3209 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3211 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3215 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
3216 fxsave
->cwd
= fpu
->fcw
;
3217 fxsave
->swd
= fpu
->fsw
;
3218 fxsave
->twd
= fpu
->ftwx
;
3219 fxsave
->fop
= fpu
->last_opcode
;
3220 fxsave
->rip
= fpu
->last_ip
;
3221 fxsave
->rdp
= fpu
->last_dp
;
3222 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
3229 void fx_init(struct kvm_vcpu
*vcpu
)
3231 unsigned after_mxcsr_mask
;
3233 /* Initialize guest FPU by resetting ours and saving into guest's */
3235 fx_save(&vcpu
->arch
.host_fx_image
);
3237 fx_save(&vcpu
->arch
.guest_fx_image
);
3238 fx_restore(&vcpu
->arch
.host_fx_image
);
3241 vcpu
->arch
.cr0
|= X86_CR0_ET
;
3242 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
3243 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
3244 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
3245 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
3247 EXPORT_SYMBOL_GPL(fx_init
);
3249 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
3251 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
3254 vcpu
->guest_fpu_loaded
= 1;
3255 fx_save(&vcpu
->arch
.host_fx_image
);
3256 fx_restore(&vcpu
->arch
.guest_fx_image
);
3258 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
3260 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
3262 if (!vcpu
->guest_fpu_loaded
)
3265 vcpu
->guest_fpu_loaded
= 0;
3266 fx_save(&vcpu
->arch
.guest_fx_image
);
3267 fx_restore(&vcpu
->arch
.host_fx_image
);
3268 ++vcpu
->stat
.fpu_reload
;
3270 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
3272 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
3274 kvm_x86_ops
->vcpu_free(vcpu
);
3277 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
3280 return kvm_x86_ops
->vcpu_create(kvm
, id
);
3283 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
3287 /* We do fxsave: this must be aligned. */
3288 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
3291 r
= kvm_arch_vcpu_reset(vcpu
);
3293 r
= kvm_mmu_setup(vcpu
);
3300 kvm_x86_ops
->vcpu_free(vcpu
);
3304 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
3307 kvm_mmu_unload(vcpu
);
3310 kvm_x86_ops
->vcpu_free(vcpu
);
3313 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
3315 return kvm_x86_ops
->vcpu_reset(vcpu
);
3318 void kvm_arch_hardware_enable(void *garbage
)
3320 kvm_x86_ops
->hardware_enable(garbage
);
3323 void kvm_arch_hardware_disable(void *garbage
)
3325 kvm_x86_ops
->hardware_disable(garbage
);
3328 int kvm_arch_hardware_setup(void)
3330 return kvm_x86_ops
->hardware_setup();
3333 void kvm_arch_hardware_unsetup(void)
3335 kvm_x86_ops
->hardware_unsetup();
3338 void kvm_arch_check_processor_compat(void *rtn
)
3340 kvm_x86_ops
->check_processor_compatibility(rtn
);
3343 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
3349 BUG_ON(vcpu
->kvm
== NULL
);
3352 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
3353 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
3354 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
3356 vcpu
->arch
.mp_state
= VCPU_MP_STATE_UNINITIALIZED
;
3358 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
3363 vcpu
->arch
.pio_data
= page_address(page
);
3365 r
= kvm_mmu_create(vcpu
);
3367 goto fail_free_pio_data
;
3369 if (irqchip_in_kernel(kvm
)) {
3370 r
= kvm_create_lapic(vcpu
);
3372 goto fail_mmu_destroy
;
3378 kvm_mmu_destroy(vcpu
);
3380 free_page((unsigned long)vcpu
->arch
.pio_data
);
3385 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
3387 kvm_free_lapic(vcpu
);
3388 kvm_mmu_destroy(vcpu
);
3389 free_page((unsigned long)vcpu
->arch
.pio_data
);
3392 struct kvm
*kvm_arch_create_vm(void)
3394 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
3397 return ERR_PTR(-ENOMEM
);
3399 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
3404 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
3407 kvm_mmu_unload(vcpu
);
3411 static void kvm_free_vcpus(struct kvm
*kvm
)
3416 * Unpin any mmu pages first.
3418 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
3420 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
3421 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3422 if (kvm
->vcpus
[i
]) {
3423 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
3424 kvm
->vcpus
[i
] = NULL
;
3430 void kvm_arch_destroy_vm(struct kvm
*kvm
)
3433 kfree(kvm
->arch
.vpic
);
3434 kfree(kvm
->arch
.vioapic
);
3435 kvm_free_vcpus(kvm
);
3436 kvm_free_physmem(kvm
);
3440 int kvm_arch_set_memory_region(struct kvm
*kvm
,
3441 struct kvm_userspace_memory_region
*mem
,
3442 struct kvm_memory_slot old
,
3445 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
3446 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
3448 /*To keep backward compatibility with older userspace,
3449 *x86 needs to hanlde !user_alloc case.
3452 if (npages
&& !old
.rmap
) {
3453 down_write(¤t
->mm
->mmap_sem
);
3454 memslot
->userspace_addr
= do_mmap(NULL
, 0,
3456 PROT_READ
| PROT_WRITE
,
3457 MAP_SHARED
| MAP_ANONYMOUS
,
3459 up_write(¤t
->mm
->mmap_sem
);
3461 if (IS_ERR((void *)memslot
->userspace_addr
))
3462 return PTR_ERR((void *)memslot
->userspace_addr
);
3464 if (!old
.user_alloc
&& old
.rmap
) {
3467 down_write(¤t
->mm
->mmap_sem
);
3468 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
3469 old
.npages
* PAGE_SIZE
);
3470 up_write(¤t
->mm
->mmap_sem
);
3473 "kvm_vm_ioctl_set_memory_region: "
3474 "failed to munmap memory\n");
3479 if (!kvm
->arch
.n_requested_mmu_pages
) {
3480 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
3481 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
3484 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
3485 kvm_flush_remote_tlbs(kvm
);
3490 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
3492 return vcpu
->arch
.mp_state
== VCPU_MP_STATE_RUNNABLE
3493 || vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
;
3496 static void vcpu_kick_intr(void *info
)
3499 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)info
;
3500 printk(KERN_DEBUG
"vcpu_kick_intr %p \n", vcpu
);
3504 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
3506 int ipi_pcpu
= vcpu
->cpu
;
3508 if (waitqueue_active(&vcpu
->wq
)) {
3509 wake_up_interruptible(&vcpu
->wq
);
3510 ++vcpu
->stat
.halt_wakeup
;
3512 if (vcpu
->guest_mode
)
3513 smp_call_function_single(ipi_pcpu
, vcpu_kick_intr
, vcpu
, 0, 0);