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>
18 #include "segment_descriptor.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>
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 { "request_irq", VCPU_STAT(request_irq_exits
) },
76 { "irq_exits", VCPU_STAT(irq_exits
) },
77 { "host_state_reload", VCPU_STAT(host_state_reload
) },
78 { "efer_reload", VCPU_STAT(efer_reload
) },
79 { "fpu_reload", VCPU_STAT(fpu_reload
) },
80 { "insn_emulation", VCPU_STAT(insn_emulation
) },
81 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
82 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
83 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
84 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
85 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
86 { "mmu_flooded", VM_STAT(mmu_flooded
) },
87 { "mmu_recycled", VM_STAT(mmu_recycled
) },
88 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
89 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
94 unsigned long segment_base(u16 selector
)
96 struct descriptor_table gdt
;
97 struct segment_descriptor
*d
;
98 unsigned long table_base
;
104 asm("sgdt %0" : "=m"(gdt
));
105 table_base
= gdt
.base
;
107 if (selector
& 4) { /* from ldt */
110 asm("sldt %0" : "=g"(ldt_selector
));
111 table_base
= segment_base(ldt_selector
);
113 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
114 v
= d
->base_low
| ((unsigned long)d
->base_mid
<< 16) |
115 ((unsigned long)d
->base_high
<< 24);
117 if (d
->system
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
118 v
|= ((unsigned long) \
119 ((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
123 EXPORT_SYMBOL_GPL(segment_base
);
125 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
127 if (irqchip_in_kernel(vcpu
->kvm
))
128 return vcpu
->arch
.apic_base
;
130 return vcpu
->arch
.apic_base
;
132 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
134 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
136 /* TODO: reserve bits check */
137 if (irqchip_in_kernel(vcpu
->kvm
))
138 kvm_lapic_set_base(vcpu
, data
);
140 vcpu
->arch
.apic_base
= data
;
142 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
144 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
146 WARN_ON(vcpu
->arch
.exception
.pending
);
147 vcpu
->arch
.exception
.pending
= true;
148 vcpu
->arch
.exception
.has_error_code
= false;
149 vcpu
->arch
.exception
.nr
= nr
;
151 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
153 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
156 ++vcpu
->stat
.pf_guest
;
157 if (vcpu
->arch
.exception
.pending
&& vcpu
->arch
.exception
.nr
== PF_VECTOR
) {
158 printk(KERN_DEBUG
"kvm: inject_page_fault:"
159 " double fault 0x%lx\n", addr
);
160 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
161 vcpu
->arch
.exception
.error_code
= 0;
164 vcpu
->arch
.cr2
= addr
;
165 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
168 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
170 WARN_ON(vcpu
->arch
.exception
.pending
);
171 vcpu
->arch
.exception
.pending
= true;
172 vcpu
->arch
.exception
.has_error_code
= true;
173 vcpu
->arch
.exception
.nr
= nr
;
174 vcpu
->arch
.exception
.error_code
= error_code
;
176 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
178 static void __queue_exception(struct kvm_vcpu
*vcpu
)
180 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
181 vcpu
->arch
.exception
.has_error_code
,
182 vcpu
->arch
.exception
.error_code
);
186 * Load the pae pdptrs. Return true is they are all valid.
188 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
190 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
191 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
194 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
196 down_read(&vcpu
->kvm
->slots_lock
);
197 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
198 offset
* sizeof(u64
), sizeof(pdpte
));
203 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
204 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
211 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
213 up_read(&vcpu
->kvm
->slots_lock
);
217 EXPORT_SYMBOL_GPL(load_pdptrs
);
219 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
221 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
225 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
228 down_read(&vcpu
->kvm
->slots_lock
);
229 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
232 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
234 up_read(&vcpu
->kvm
->slots_lock
);
239 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
241 if (cr0
& CR0_RESERVED_BITS
) {
242 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
243 cr0
, vcpu
->arch
.cr0
);
244 kvm_inject_gp(vcpu
, 0);
248 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
249 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
250 kvm_inject_gp(vcpu
, 0);
254 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
255 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
256 "and a clear PE flag\n");
257 kvm_inject_gp(vcpu
, 0);
261 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
263 if ((vcpu
->arch
.shadow_efer
& EFER_LME
)) {
267 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
268 "in long mode while PAE is disabled\n");
269 kvm_inject_gp(vcpu
, 0);
272 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
274 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
275 "in long mode while CS.L == 1\n");
276 kvm_inject_gp(vcpu
, 0);
282 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
283 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
285 kvm_inject_gp(vcpu
, 0);
291 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
292 vcpu
->arch
.cr0
= cr0
;
294 kvm_mmu_reset_context(vcpu
);
297 EXPORT_SYMBOL_GPL(set_cr0
);
299 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
301 set_cr0(vcpu
, (vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f));
303 EXPORT_SYMBOL_GPL(lmsw
);
305 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
307 if (cr4
& CR4_RESERVED_BITS
) {
308 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
309 kvm_inject_gp(vcpu
, 0);
313 if (is_long_mode(vcpu
)) {
314 if (!(cr4
& X86_CR4_PAE
)) {
315 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
317 kvm_inject_gp(vcpu
, 0);
320 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
321 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
322 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
323 kvm_inject_gp(vcpu
, 0);
327 if (cr4
& X86_CR4_VMXE
) {
328 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
329 kvm_inject_gp(vcpu
, 0);
332 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
333 vcpu
->arch
.cr4
= cr4
;
334 kvm_mmu_reset_context(vcpu
);
336 EXPORT_SYMBOL_GPL(set_cr4
);
338 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
340 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
341 kvm_mmu_flush_tlb(vcpu
);
345 if (is_long_mode(vcpu
)) {
346 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
347 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
348 kvm_inject_gp(vcpu
, 0);
353 if (cr3
& CR3_PAE_RESERVED_BITS
) {
355 "set_cr3: #GP, reserved bits\n");
356 kvm_inject_gp(vcpu
, 0);
359 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
360 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
362 kvm_inject_gp(vcpu
, 0);
367 * We don't check reserved bits in nonpae mode, because
368 * this isn't enforced, and VMware depends on this.
372 down_read(&vcpu
->kvm
->slots_lock
);
374 * Does the new cr3 value map to physical memory? (Note, we
375 * catch an invalid cr3 even in real-mode, because it would
376 * cause trouble later on when we turn on paging anyway.)
378 * A real CPU would silently accept an invalid cr3 and would
379 * attempt to use it - with largely undefined (and often hard
380 * to debug) behavior on the guest side.
382 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
383 kvm_inject_gp(vcpu
, 0);
385 vcpu
->arch
.cr3
= cr3
;
386 vcpu
->arch
.mmu
.new_cr3(vcpu
);
388 up_read(&vcpu
->kvm
->slots_lock
);
390 EXPORT_SYMBOL_GPL(set_cr3
);
392 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
394 if (cr8
& CR8_RESERVED_BITS
) {
395 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
396 kvm_inject_gp(vcpu
, 0);
399 if (irqchip_in_kernel(vcpu
->kvm
))
400 kvm_lapic_set_tpr(vcpu
, cr8
);
402 vcpu
->arch
.cr8
= cr8
;
404 EXPORT_SYMBOL_GPL(set_cr8
);
406 unsigned long get_cr8(struct kvm_vcpu
*vcpu
)
408 if (irqchip_in_kernel(vcpu
->kvm
))
409 return kvm_lapic_get_cr8(vcpu
);
411 return vcpu
->arch
.cr8
;
413 EXPORT_SYMBOL_GPL(get_cr8
);
416 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
417 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
419 * This list is modified at module load time to reflect the
420 * capabilities of the host cpu.
422 static u32 msrs_to_save
[] = {
423 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
426 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
428 MSR_IA32_TIME_STAMP_COUNTER
, MSR_KVM_SYSTEM_TIME
, MSR_KVM_WALL_CLOCK
,
431 static unsigned num_msrs_to_save
;
433 static u32 emulated_msrs
[] = {
434 MSR_IA32_MISC_ENABLE
,
437 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
439 if (efer
& efer_reserved_bits
) {
440 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
442 kvm_inject_gp(vcpu
, 0);
447 && (vcpu
->arch
.shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
448 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
449 kvm_inject_gp(vcpu
, 0);
453 kvm_x86_ops
->set_efer(vcpu
, efer
);
456 efer
|= vcpu
->arch
.shadow_efer
& EFER_LMA
;
458 vcpu
->arch
.shadow_efer
= efer
;
461 void kvm_enable_efer_bits(u64 mask
)
463 efer_reserved_bits
&= ~mask
;
465 EXPORT_SYMBOL_GPL(kvm_enable_efer_bits
);
469 * Writes msr value into into the appropriate "register".
470 * Returns 0 on success, non-0 otherwise.
471 * Assumes vcpu_load() was already called.
473 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
475 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
479 * Adapt set_msr() to msr_io()'s calling convention
481 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
483 return kvm_set_msr(vcpu
, index
, *data
);
486 static void kvm_write_wall_clock(struct kvm
*kvm
, gpa_t wall_clock
)
489 struct kvm_wall_clock wc
;
490 struct timespec wc_ts
;
497 down_read(&kvm
->slots_lock
);
498 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
500 wc_ts
= current_kernel_time();
501 wc
.wc_sec
= wc_ts
.tv_sec
;
502 wc
.wc_nsec
= wc_ts
.tv_nsec
;
503 wc
.wc_version
= version
;
505 kvm_write_guest(kvm
, wall_clock
, &wc
, sizeof(wc
));
508 kvm_write_guest(kvm
, wall_clock
, &version
, sizeof(version
));
509 up_read(&kvm
->slots_lock
);
512 static void kvm_write_guest_time(struct kvm_vcpu
*v
)
516 struct kvm_vcpu_arch
*vcpu
= &v
->arch
;
519 if ((!vcpu
->time_page
))
522 /* Keep irq disabled to prevent changes to the clock */
523 local_irq_save(flags
);
524 kvm_get_msr(v
, MSR_IA32_TIME_STAMP_COUNTER
,
525 &vcpu
->hv_clock
.tsc_timestamp
);
527 local_irq_restore(flags
);
529 /* With all the info we got, fill in the values */
531 vcpu
->hv_clock
.system_time
= ts
.tv_nsec
+
532 (NSEC_PER_SEC
* (u64
)ts
.tv_sec
);
534 * The interface expects us to write an even number signaling that the
535 * update is finished. Since the guest won't see the intermediate
536 * state, we just write "2" at the end
538 vcpu
->hv_clock
.version
= 2;
540 shared_kaddr
= kmap_atomic(vcpu
->time_page
, KM_USER0
);
542 memcpy(shared_kaddr
+ vcpu
->time_offset
, &vcpu
->hv_clock
,
543 sizeof(vcpu
->hv_clock
));
545 kunmap_atomic(shared_kaddr
, KM_USER0
);
547 mark_page_dirty(v
->kvm
, vcpu
->time
>> PAGE_SHIFT
);
551 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
555 set_efer(vcpu
, data
);
557 case MSR_IA32_MC0_STATUS
:
558 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
561 case MSR_IA32_MCG_STATUS
:
562 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
565 case MSR_IA32_MCG_CTL
:
566 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
569 case MSR_IA32_UCODE_REV
:
570 case MSR_IA32_UCODE_WRITE
:
571 case 0x200 ... 0x2ff: /* MTRRs */
573 case MSR_IA32_APICBASE
:
574 kvm_set_apic_base(vcpu
, data
);
576 case MSR_IA32_MISC_ENABLE
:
577 vcpu
->arch
.ia32_misc_enable_msr
= data
;
579 case MSR_KVM_WALL_CLOCK
:
580 vcpu
->kvm
->arch
.wall_clock
= data
;
581 kvm_write_wall_clock(vcpu
->kvm
, data
);
583 case MSR_KVM_SYSTEM_TIME
: {
584 if (vcpu
->arch
.time_page
) {
585 kvm_release_page_dirty(vcpu
->arch
.time_page
);
586 vcpu
->arch
.time_page
= NULL
;
589 vcpu
->arch
.time
= data
;
591 /* we verify if the enable bit is set... */
595 /* ...but clean it before doing the actual write */
596 vcpu
->arch
.time_offset
= data
& ~(PAGE_MASK
| 1);
598 vcpu
->arch
.hv_clock
.tsc_to_system_mul
=
599 clocksource_khz2mult(tsc_khz
, 22);
600 vcpu
->arch
.hv_clock
.tsc_shift
= 22;
602 down_read(¤t
->mm
->mmap_sem
);
603 down_read(&vcpu
->kvm
->slots_lock
);
604 vcpu
->arch
.time_page
=
605 gfn_to_page(vcpu
->kvm
, data
>> PAGE_SHIFT
);
606 up_read(&vcpu
->kvm
->slots_lock
);
607 up_read(¤t
->mm
->mmap_sem
);
609 if (is_error_page(vcpu
->arch
.time_page
)) {
610 kvm_release_page_clean(vcpu
->arch
.time_page
);
611 vcpu
->arch
.time_page
= NULL
;
614 kvm_write_guest_time(vcpu
);
618 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n", msr
, data
);
623 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
627 * Reads an msr value (of 'msr_index') into 'pdata'.
628 * Returns 0 on success, non-0 otherwise.
629 * Assumes vcpu_load() was already called.
631 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
633 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
636 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
641 case 0xc0010010: /* SYSCFG */
642 case 0xc0010015: /* HWCR */
643 case MSR_IA32_PLATFORM_ID
:
644 case MSR_IA32_P5_MC_ADDR
:
645 case MSR_IA32_P5_MC_TYPE
:
646 case MSR_IA32_MC0_CTL
:
647 case MSR_IA32_MCG_STATUS
:
648 case MSR_IA32_MCG_CAP
:
649 case MSR_IA32_MCG_CTL
:
650 case MSR_IA32_MC0_MISC
:
651 case MSR_IA32_MC0_MISC
+4:
652 case MSR_IA32_MC0_MISC
+8:
653 case MSR_IA32_MC0_MISC
+12:
654 case MSR_IA32_MC0_MISC
+16:
655 case MSR_IA32_UCODE_REV
:
656 case MSR_IA32_PERF_STATUS
:
657 case MSR_IA32_EBL_CR_POWERON
:
660 case 0x200 ... 0x2ff:
663 case 0xcd: /* fsb frequency */
666 case MSR_IA32_APICBASE
:
667 data
= kvm_get_apic_base(vcpu
);
669 case MSR_IA32_MISC_ENABLE
:
670 data
= vcpu
->arch
.ia32_misc_enable_msr
;
673 data
= vcpu
->arch
.shadow_efer
;
675 case MSR_KVM_WALL_CLOCK
:
676 data
= vcpu
->kvm
->arch
.wall_clock
;
678 case MSR_KVM_SYSTEM_TIME
:
679 data
= vcpu
->arch
.time
;
682 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
688 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
691 * Read or write a bunch of msrs. All parameters are kernel addresses.
693 * @return number of msrs set successfully.
695 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
696 struct kvm_msr_entry
*entries
,
697 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
698 unsigned index
, u64
*data
))
704 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
705 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
714 * Read or write a bunch of msrs. Parameters are user addresses.
716 * @return number of msrs set successfully.
718 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
719 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
720 unsigned index
, u64
*data
),
723 struct kvm_msrs msrs
;
724 struct kvm_msr_entry
*entries
;
729 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
733 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
737 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
738 entries
= vmalloc(size
);
743 if (copy_from_user(entries
, user_msrs
->entries
, size
))
746 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
751 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
763 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
766 void decache_vcpus_on_cpu(int cpu
)
769 struct kvm_vcpu
*vcpu
;
772 spin_lock(&kvm_lock
);
773 list_for_each_entry(vm
, &vm_list
, vm_list
)
774 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
779 * If the vcpu is locked, then it is running on some
780 * other cpu and therefore it is not cached on the
783 * If it's not locked, check the last cpu it executed
786 if (mutex_trylock(&vcpu
->mutex
)) {
787 if (vcpu
->cpu
== cpu
) {
788 kvm_x86_ops
->vcpu_decache(vcpu
);
791 mutex_unlock(&vcpu
->mutex
);
794 spin_unlock(&kvm_lock
);
797 int kvm_dev_ioctl_check_extension(long ext
)
802 case KVM_CAP_IRQCHIP
:
804 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
805 case KVM_CAP_USER_MEMORY
:
806 case KVM_CAP_SET_TSS_ADDR
:
807 case KVM_CAP_EXT_CPUID
:
808 case KVM_CAP_CLOCKSOURCE
:
812 r
= !kvm_x86_ops
->cpu_has_accelerated_tpr();
814 case KVM_CAP_NR_VCPUS
:
817 case KVM_CAP_NR_MEMSLOTS
:
818 r
= KVM_MEMORY_SLOTS
;
828 long kvm_arch_dev_ioctl(struct file
*filp
,
829 unsigned int ioctl
, unsigned long arg
)
831 void __user
*argp
= (void __user
*)arg
;
835 case KVM_GET_MSR_INDEX_LIST
: {
836 struct kvm_msr_list __user
*user_msr_list
= argp
;
837 struct kvm_msr_list msr_list
;
841 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
844 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
845 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
848 if (n
< num_msrs_to_save
)
851 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
852 num_msrs_to_save
* sizeof(u32
)))
854 if (copy_to_user(user_msr_list
->indices
855 + num_msrs_to_save
* sizeof(u32
),
857 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
862 case KVM_GET_SUPPORTED_CPUID
: {
863 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
864 struct kvm_cpuid2 cpuid
;
867 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
869 r
= kvm_dev_ioctl_get_supported_cpuid(&cpuid
,
875 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
887 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
889 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
890 kvm_write_guest_time(vcpu
);
893 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
895 kvm_x86_ops
->vcpu_put(vcpu
);
896 kvm_put_guest_fpu(vcpu
);
899 static int is_efer_nx(void)
903 rdmsrl(MSR_EFER
, efer
);
904 return efer
& EFER_NX
;
907 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
910 struct kvm_cpuid_entry2
*e
, *entry
;
913 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
914 e
= &vcpu
->arch
.cpuid_entries
[i
];
915 if (e
->function
== 0x80000001) {
920 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
921 entry
->edx
&= ~(1 << 20);
922 printk(KERN_INFO
"kvm: guest NX capability removed\n");
926 /* when an old userspace process fills a new kernel module */
927 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
928 struct kvm_cpuid
*cpuid
,
929 struct kvm_cpuid_entry __user
*entries
)
932 struct kvm_cpuid_entry
*cpuid_entries
;
935 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
938 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
942 if (copy_from_user(cpuid_entries
, entries
,
943 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
945 for (i
= 0; i
< cpuid
->nent
; i
++) {
946 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
947 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
948 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
949 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
950 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
951 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
952 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
953 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
954 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
955 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
957 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
958 cpuid_fix_nx_cap(vcpu
);
962 vfree(cpuid_entries
);
967 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
968 struct kvm_cpuid2
*cpuid
,
969 struct kvm_cpuid_entry2 __user
*entries
)
974 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
977 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
978 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
980 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
987 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
988 struct kvm_cpuid2
*cpuid
,
989 struct kvm_cpuid_entry2 __user
*entries
)
994 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
997 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
998 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
1003 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
1007 static inline u32
bit(int bitno
)
1009 return 1 << (bitno
& 31);
1012 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1015 entry
->function
= function
;
1016 entry
->index
= index
;
1017 cpuid_count(entry
->function
, entry
->index
,
1018 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
1022 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
1023 u32 index
, int *nent
, int maxnent
)
1025 const u32 kvm_supported_word0_x86_features
= bit(X86_FEATURE_FPU
) |
1026 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1027 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1028 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1029 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1030 bit(X86_FEATURE_SEP
) | bit(X86_FEATURE_PGE
) |
1031 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1032 bit(X86_FEATURE_CLFLSH
) | bit(X86_FEATURE_MMX
) |
1033 bit(X86_FEATURE_FXSR
) | bit(X86_FEATURE_XMM
) |
1034 bit(X86_FEATURE_XMM2
) | bit(X86_FEATURE_SELFSNOOP
);
1035 const u32 kvm_supported_word1_x86_features
= bit(X86_FEATURE_FPU
) |
1036 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
1037 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
1038 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
1039 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
1040 bit(X86_FEATURE_PGE
) |
1041 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
1042 bit(X86_FEATURE_MMX
) | bit(X86_FEATURE_FXSR
) |
1043 bit(X86_FEATURE_SYSCALL
) |
1044 (bit(X86_FEATURE_NX
) && is_efer_nx()) |
1045 #ifdef CONFIG_X86_64
1046 bit(X86_FEATURE_LM
) |
1048 bit(X86_FEATURE_MMXEXT
) |
1049 bit(X86_FEATURE_3DNOWEXT
) |
1050 bit(X86_FEATURE_3DNOW
);
1051 const u32 kvm_supported_word3_x86_features
=
1052 bit(X86_FEATURE_XMM3
) | bit(X86_FEATURE_CX16
);
1053 const u32 kvm_supported_word6_x86_features
=
1054 bit(X86_FEATURE_LAHF_LM
) | bit(X86_FEATURE_CMP_LEGACY
);
1056 /* all func 2 cpuid_count() should be called on the same cpu */
1058 do_cpuid_1_ent(entry
, function
, index
);
1063 entry
->eax
= min(entry
->eax
, (u32
)0xb);
1066 entry
->edx
&= kvm_supported_word0_x86_features
;
1067 entry
->ecx
&= kvm_supported_word3_x86_features
;
1069 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
1070 * may return different values. This forces us to get_cpu() before
1071 * issuing the first command, and also to emulate this annoying behavior
1072 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
1074 int t
, times
= entry
->eax
& 0xff;
1076 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1077 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
1078 do_cpuid_1_ent(&entry
[t
], function
, 0);
1079 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
1084 /* function 4 and 0xb have additional index. */
1086 int index
, cache_type
;
1088 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1089 /* read more entries until cache_type is zero */
1090 for (index
= 1; *nent
< maxnent
; ++index
) {
1091 cache_type
= entry
[index
- 1].eax
& 0x1f;
1094 do_cpuid_1_ent(&entry
[index
], function
, index
);
1095 entry
[index
].flags
|=
1096 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1102 int index
, level_type
;
1104 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1105 /* read more entries until level_type is zero */
1106 for (index
= 1; *nent
< maxnent
; ++index
) {
1107 level_type
= entry
[index
- 1].ecx
& 0xff;
1110 do_cpuid_1_ent(&entry
[index
], function
, index
);
1111 entry
[index
].flags
|=
1112 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1118 entry
->eax
= min(entry
->eax
, 0x8000001a);
1121 entry
->edx
&= kvm_supported_word1_x86_features
;
1122 entry
->ecx
&= kvm_supported_word6_x86_features
;
1128 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
1129 struct kvm_cpuid_entry2 __user
*entries
)
1131 struct kvm_cpuid_entry2
*cpuid_entries
;
1132 int limit
, nent
= 0, r
= -E2BIG
;
1135 if (cpuid
->nent
< 1)
1138 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
1142 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
1143 limit
= cpuid_entries
[0].eax
;
1144 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1145 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1146 &nent
, cpuid
->nent
);
1148 if (nent
>= cpuid
->nent
)
1151 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
1152 limit
= cpuid_entries
[nent
- 1].eax
;
1153 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1154 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1155 &nent
, cpuid
->nent
);
1157 if (copy_to_user(entries
, cpuid_entries
,
1158 nent
* sizeof(struct kvm_cpuid_entry2
)))
1164 vfree(cpuid_entries
);
1169 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
1170 struct kvm_lapic_state
*s
)
1173 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
1179 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
1180 struct kvm_lapic_state
*s
)
1183 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1184 kvm_apic_post_state_restore(vcpu
);
1190 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1191 struct kvm_interrupt
*irq
)
1193 if (irq
->irq
< 0 || irq
->irq
>= 256)
1195 if (irqchip_in_kernel(vcpu
->kvm
))
1199 set_bit(irq
->irq
, vcpu
->arch
.irq_pending
);
1200 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
1207 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
1208 struct kvm_tpr_access_ctl
*tac
)
1212 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
1216 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1217 unsigned int ioctl
, unsigned long arg
)
1219 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1220 void __user
*argp
= (void __user
*)arg
;
1224 case KVM_GET_LAPIC
: {
1225 struct kvm_lapic_state lapic
;
1227 memset(&lapic
, 0, sizeof lapic
);
1228 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, &lapic
);
1232 if (copy_to_user(argp
, &lapic
, sizeof lapic
))
1237 case KVM_SET_LAPIC
: {
1238 struct kvm_lapic_state lapic
;
1241 if (copy_from_user(&lapic
, argp
, sizeof lapic
))
1243 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, &lapic
);;
1249 case KVM_INTERRUPT
: {
1250 struct kvm_interrupt irq
;
1253 if (copy_from_user(&irq
, argp
, sizeof irq
))
1255 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1261 case KVM_SET_CPUID
: {
1262 struct kvm_cpuid __user
*cpuid_arg
= argp
;
1263 struct kvm_cpuid cpuid
;
1266 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1268 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
1273 case KVM_SET_CPUID2
: {
1274 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1275 struct kvm_cpuid2 cpuid
;
1278 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1280 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
1281 cpuid_arg
->entries
);
1286 case KVM_GET_CPUID2
: {
1287 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1288 struct kvm_cpuid2 cpuid
;
1291 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1293 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
1294 cpuid_arg
->entries
);
1298 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1304 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
1307 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
1309 case KVM_TPR_ACCESS_REPORTING
: {
1310 struct kvm_tpr_access_ctl tac
;
1313 if (copy_from_user(&tac
, argp
, sizeof tac
))
1315 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
1319 if (copy_to_user(argp
, &tac
, sizeof tac
))
1324 case KVM_SET_VAPIC_ADDR
: {
1325 struct kvm_vapic_addr va
;
1328 if (!irqchip_in_kernel(vcpu
->kvm
))
1331 if (copy_from_user(&va
, argp
, sizeof va
))
1334 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
1344 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
1348 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
1350 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
1354 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
1355 u32 kvm_nr_mmu_pages
)
1357 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
1360 down_write(&kvm
->slots_lock
);
1362 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
1363 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
1365 up_write(&kvm
->slots_lock
);
1369 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
1371 return kvm
->arch
.n_alloc_mmu_pages
;
1374 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1377 struct kvm_mem_alias
*alias
;
1379 for (i
= 0; i
< kvm
->arch
.naliases
; ++i
) {
1380 alias
= &kvm
->arch
.aliases
[i
];
1381 if (gfn
>= alias
->base_gfn
1382 && gfn
< alias
->base_gfn
+ alias
->npages
)
1383 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1389 * Set a new alias region. Aliases map a portion of physical memory into
1390 * another portion. This is useful for memory windows, for example the PC
1393 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
1394 struct kvm_memory_alias
*alias
)
1397 struct kvm_mem_alias
*p
;
1400 /* General sanity checks */
1401 if (alias
->memory_size
& (PAGE_SIZE
- 1))
1403 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
1405 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
1407 if (alias
->guest_phys_addr
+ alias
->memory_size
1408 < alias
->guest_phys_addr
)
1410 if (alias
->target_phys_addr
+ alias
->memory_size
1411 < alias
->target_phys_addr
)
1414 down_write(&kvm
->slots_lock
);
1416 p
= &kvm
->arch
.aliases
[alias
->slot
];
1417 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
1418 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
1419 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
1421 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
1422 if (kvm
->arch
.aliases
[n
- 1].npages
)
1424 kvm
->arch
.naliases
= n
;
1426 kvm_mmu_zap_all(kvm
);
1428 up_write(&kvm
->slots_lock
);
1436 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1441 switch (chip
->chip_id
) {
1442 case KVM_IRQCHIP_PIC_MASTER
:
1443 memcpy(&chip
->chip
.pic
,
1444 &pic_irqchip(kvm
)->pics
[0],
1445 sizeof(struct kvm_pic_state
));
1447 case KVM_IRQCHIP_PIC_SLAVE
:
1448 memcpy(&chip
->chip
.pic
,
1449 &pic_irqchip(kvm
)->pics
[1],
1450 sizeof(struct kvm_pic_state
));
1452 case KVM_IRQCHIP_IOAPIC
:
1453 memcpy(&chip
->chip
.ioapic
,
1454 ioapic_irqchip(kvm
),
1455 sizeof(struct kvm_ioapic_state
));
1464 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1469 switch (chip
->chip_id
) {
1470 case KVM_IRQCHIP_PIC_MASTER
:
1471 memcpy(&pic_irqchip(kvm
)->pics
[0],
1473 sizeof(struct kvm_pic_state
));
1475 case KVM_IRQCHIP_PIC_SLAVE
:
1476 memcpy(&pic_irqchip(kvm
)->pics
[1],
1478 sizeof(struct kvm_pic_state
));
1480 case KVM_IRQCHIP_IOAPIC
:
1481 memcpy(ioapic_irqchip(kvm
),
1483 sizeof(struct kvm_ioapic_state
));
1489 kvm_pic_update_irq(pic_irqchip(kvm
));
1494 * Get (and clear) the dirty memory log for a memory slot.
1496 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1497 struct kvm_dirty_log
*log
)
1501 struct kvm_memory_slot
*memslot
;
1504 down_write(&kvm
->slots_lock
);
1506 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1510 /* If nothing is dirty, don't bother messing with page tables. */
1512 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
1513 kvm_flush_remote_tlbs(kvm
);
1514 memslot
= &kvm
->memslots
[log
->slot
];
1515 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1516 memset(memslot
->dirty_bitmap
, 0, n
);
1520 up_write(&kvm
->slots_lock
);
1524 long kvm_arch_vm_ioctl(struct file
*filp
,
1525 unsigned int ioctl
, unsigned long arg
)
1527 struct kvm
*kvm
= filp
->private_data
;
1528 void __user
*argp
= (void __user
*)arg
;
1532 case KVM_SET_TSS_ADDR
:
1533 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
1537 case KVM_SET_MEMORY_REGION
: {
1538 struct kvm_memory_region kvm_mem
;
1539 struct kvm_userspace_memory_region kvm_userspace_mem
;
1542 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
1544 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
1545 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
1546 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
1547 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
1548 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1553 case KVM_SET_NR_MMU_PAGES
:
1554 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1558 case KVM_GET_NR_MMU_PAGES
:
1559 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1561 case KVM_SET_MEMORY_ALIAS
: {
1562 struct kvm_memory_alias alias
;
1565 if (copy_from_user(&alias
, argp
, sizeof alias
))
1567 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
1572 case KVM_CREATE_IRQCHIP
:
1574 kvm
->arch
.vpic
= kvm_create_pic(kvm
);
1575 if (kvm
->arch
.vpic
) {
1576 r
= kvm_ioapic_init(kvm
);
1578 kfree(kvm
->arch
.vpic
);
1579 kvm
->arch
.vpic
= NULL
;
1585 case KVM_IRQ_LINE
: {
1586 struct kvm_irq_level irq_event
;
1589 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1591 if (irqchip_in_kernel(kvm
)) {
1592 mutex_lock(&kvm
->lock
);
1593 if (irq_event
.irq
< 16)
1594 kvm_pic_set_irq(pic_irqchip(kvm
),
1597 kvm_ioapic_set_irq(kvm
->arch
.vioapic
,
1600 mutex_unlock(&kvm
->lock
);
1605 case KVM_GET_IRQCHIP
: {
1606 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1607 struct kvm_irqchip chip
;
1610 if (copy_from_user(&chip
, argp
, sizeof chip
))
1613 if (!irqchip_in_kernel(kvm
))
1615 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
1619 if (copy_to_user(argp
, &chip
, sizeof chip
))
1624 case KVM_SET_IRQCHIP
: {
1625 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1626 struct kvm_irqchip chip
;
1629 if (copy_from_user(&chip
, argp
, sizeof chip
))
1632 if (!irqchip_in_kernel(kvm
))
1634 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
1647 static void kvm_init_msr_list(void)
1652 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1653 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1656 msrs_to_save
[j
] = msrs_to_save
[i
];
1659 num_msrs_to_save
= j
;
1663 * Only apic need an MMIO device hook, so shortcut now..
1665 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
1668 struct kvm_io_device
*dev
;
1670 if (vcpu
->arch
.apic
) {
1671 dev
= &vcpu
->arch
.apic
->dev
;
1672 if (dev
->in_range(dev
, addr
))
1679 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1682 struct kvm_io_device
*dev
;
1684 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
);
1686 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1690 int emulator_read_std(unsigned long addr
,
1693 struct kvm_vcpu
*vcpu
)
1696 int r
= X86EMUL_CONTINUE
;
1698 down_read(&vcpu
->kvm
->slots_lock
);
1700 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1701 unsigned offset
= addr
& (PAGE_SIZE
-1);
1702 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1705 if (gpa
== UNMAPPED_GVA
) {
1706 r
= X86EMUL_PROPAGATE_FAULT
;
1709 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, tocopy
);
1711 r
= X86EMUL_UNHANDLEABLE
;
1720 up_read(&vcpu
->kvm
->slots_lock
);
1723 EXPORT_SYMBOL_GPL(emulator_read_std
);
1725 static int emulator_read_emulated(unsigned long addr
,
1728 struct kvm_vcpu
*vcpu
)
1730 struct kvm_io_device
*mmio_dev
;
1733 if (vcpu
->mmio_read_completed
) {
1734 memcpy(val
, vcpu
->mmio_data
, bytes
);
1735 vcpu
->mmio_read_completed
= 0;
1736 return X86EMUL_CONTINUE
;
1739 down_read(&vcpu
->kvm
->slots_lock
);
1740 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1741 up_read(&vcpu
->kvm
->slots_lock
);
1743 /* For APIC access vmexit */
1744 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1747 if (emulator_read_std(addr
, val
, bytes
, vcpu
)
1748 == X86EMUL_CONTINUE
)
1749 return X86EMUL_CONTINUE
;
1750 if (gpa
== UNMAPPED_GVA
)
1751 return X86EMUL_PROPAGATE_FAULT
;
1755 * Is this MMIO handled locally?
1757 mutex_lock(&vcpu
->kvm
->lock
);
1758 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1760 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1761 mutex_unlock(&vcpu
->kvm
->lock
);
1762 return X86EMUL_CONTINUE
;
1764 mutex_unlock(&vcpu
->kvm
->lock
);
1766 vcpu
->mmio_needed
= 1;
1767 vcpu
->mmio_phys_addr
= gpa
;
1768 vcpu
->mmio_size
= bytes
;
1769 vcpu
->mmio_is_write
= 0;
1771 return X86EMUL_UNHANDLEABLE
;
1774 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1775 const void *val
, int bytes
)
1779 down_read(&vcpu
->kvm
->slots_lock
);
1780 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
1782 up_read(&vcpu
->kvm
->slots_lock
);
1785 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1786 up_read(&vcpu
->kvm
->slots_lock
);
1790 static int emulator_write_emulated_onepage(unsigned long addr
,
1793 struct kvm_vcpu
*vcpu
)
1795 struct kvm_io_device
*mmio_dev
;
1798 down_read(&vcpu
->kvm
->slots_lock
);
1799 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1800 up_read(&vcpu
->kvm
->slots_lock
);
1802 if (gpa
== UNMAPPED_GVA
) {
1803 kvm_inject_page_fault(vcpu
, addr
, 2);
1804 return X86EMUL_PROPAGATE_FAULT
;
1807 /* For APIC access vmexit */
1808 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1811 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1812 return X86EMUL_CONTINUE
;
1816 * Is this MMIO handled locally?
1818 mutex_lock(&vcpu
->kvm
->lock
);
1819 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1821 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1822 mutex_unlock(&vcpu
->kvm
->lock
);
1823 return X86EMUL_CONTINUE
;
1825 mutex_unlock(&vcpu
->kvm
->lock
);
1827 vcpu
->mmio_needed
= 1;
1828 vcpu
->mmio_phys_addr
= gpa
;
1829 vcpu
->mmio_size
= bytes
;
1830 vcpu
->mmio_is_write
= 1;
1831 memcpy(vcpu
->mmio_data
, val
, bytes
);
1833 return X86EMUL_CONTINUE
;
1836 int emulator_write_emulated(unsigned long addr
,
1839 struct kvm_vcpu
*vcpu
)
1841 /* Crossing a page boundary? */
1842 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1845 now
= -addr
& ~PAGE_MASK
;
1846 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
1847 if (rc
!= X86EMUL_CONTINUE
)
1853 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
1855 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
1857 static int emulator_cmpxchg_emulated(unsigned long addr
,
1861 struct kvm_vcpu
*vcpu
)
1863 static int reported
;
1867 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1869 #ifndef CONFIG_X86_64
1870 /* guests cmpxchg8b have to be emulated atomically */
1877 down_read(&vcpu
->kvm
->slots_lock
);
1878 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1880 if (gpa
== UNMAPPED_GVA
||
1881 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1884 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
1889 down_read(¤t
->mm
->mmap_sem
);
1890 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1891 up_read(¤t
->mm
->mmap_sem
);
1893 kaddr
= kmap_atomic(page
, KM_USER0
);
1894 set_64bit((u64
*)(kaddr
+ offset_in_page(gpa
)), val
);
1895 kunmap_atomic(kaddr
, KM_USER0
);
1896 kvm_release_page_dirty(page
);
1898 up_read(&vcpu
->kvm
->slots_lock
);
1902 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
1905 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1907 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
1910 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1912 return X86EMUL_CONTINUE
;
1915 int emulate_clts(struct kvm_vcpu
*vcpu
)
1917 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
& ~X86_CR0_TS
);
1918 return X86EMUL_CONTINUE
;
1921 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
1923 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1927 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
1928 return X86EMUL_CONTINUE
;
1930 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __FUNCTION__
, dr
);
1931 return X86EMUL_UNHANDLEABLE
;
1935 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1937 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1940 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1942 /* FIXME: better handling */
1943 return X86EMUL_UNHANDLEABLE
;
1945 return X86EMUL_CONTINUE
;
1948 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
1950 static int reported
;
1952 unsigned long rip
= vcpu
->arch
.rip
;
1953 unsigned long rip_linear
;
1955 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
1960 emulator_read_std(rip_linear
, (void *)opcodes
, 4, vcpu
);
1962 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1963 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1966 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
1968 struct x86_emulate_ops emulate_ops
= {
1969 .read_std
= emulator_read_std
,
1970 .read_emulated
= emulator_read_emulated
,
1971 .write_emulated
= emulator_write_emulated
,
1972 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1975 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1976 struct kvm_run
*run
,
1982 struct decode_cache
*c
;
1984 vcpu
->arch
.mmio_fault_cr2
= cr2
;
1985 kvm_x86_ops
->cache_regs(vcpu
);
1987 vcpu
->mmio_is_write
= 0;
1988 vcpu
->arch
.pio
.string
= 0;
1990 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
1992 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
1994 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
1995 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
1996 vcpu
->arch
.emulate_ctxt
.mode
=
1997 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
1998 ? X86EMUL_MODE_REAL
: cs_l
1999 ? X86EMUL_MODE_PROT64
: cs_db
2000 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
2002 if (vcpu
->arch
.emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
2003 vcpu
->arch
.emulate_ctxt
.cs_base
= 0;
2004 vcpu
->arch
.emulate_ctxt
.ds_base
= 0;
2005 vcpu
->arch
.emulate_ctxt
.es_base
= 0;
2006 vcpu
->arch
.emulate_ctxt
.ss_base
= 0;
2008 vcpu
->arch
.emulate_ctxt
.cs_base
=
2009 get_segment_base(vcpu
, VCPU_SREG_CS
);
2010 vcpu
->arch
.emulate_ctxt
.ds_base
=
2011 get_segment_base(vcpu
, VCPU_SREG_DS
);
2012 vcpu
->arch
.emulate_ctxt
.es_base
=
2013 get_segment_base(vcpu
, VCPU_SREG_ES
);
2014 vcpu
->arch
.emulate_ctxt
.ss_base
=
2015 get_segment_base(vcpu
, VCPU_SREG_SS
);
2018 vcpu
->arch
.emulate_ctxt
.gs_base
=
2019 get_segment_base(vcpu
, VCPU_SREG_GS
);
2020 vcpu
->arch
.emulate_ctxt
.fs_base
=
2021 get_segment_base(vcpu
, VCPU_SREG_FS
);
2023 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2025 /* Reject the instructions other than VMCALL/VMMCALL when
2026 * try to emulate invalid opcode */
2027 c
= &vcpu
->arch
.emulate_ctxt
.decode
;
2028 if ((emulation_type
& EMULTYPE_TRAP_UD
) &&
2029 (!(c
->twobyte
&& c
->b
== 0x01 &&
2030 (c
->modrm_reg
== 0 || c
->modrm_reg
== 3) &&
2031 c
->modrm_mod
== 3 && c
->modrm_rm
== 1)))
2032 return EMULATE_FAIL
;
2034 ++vcpu
->stat
.insn_emulation
;
2036 ++vcpu
->stat
.insn_emulation_fail
;
2037 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2038 return EMULATE_DONE
;
2039 return EMULATE_FAIL
;
2043 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2045 if (vcpu
->arch
.pio
.string
)
2046 return EMULATE_DO_MMIO
;
2048 if ((r
|| vcpu
->mmio_is_write
) && run
) {
2049 run
->exit_reason
= KVM_EXIT_MMIO
;
2050 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
2051 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
2052 run
->mmio
.len
= vcpu
->mmio_size
;
2053 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
2057 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2058 return EMULATE_DONE
;
2059 if (!vcpu
->mmio_needed
) {
2060 kvm_report_emulation_failure(vcpu
, "mmio");
2061 return EMULATE_FAIL
;
2063 return EMULATE_DO_MMIO
;
2066 kvm_x86_ops
->decache_regs(vcpu
);
2067 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
2069 if (vcpu
->mmio_is_write
) {
2070 vcpu
->mmio_needed
= 0;
2071 return EMULATE_DO_MMIO
;
2074 return EMULATE_DONE
;
2076 EXPORT_SYMBOL_GPL(emulate_instruction
);
2078 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
2082 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.pio
.guest_pages
); ++i
)
2083 if (vcpu
->arch
.pio
.guest_pages
[i
]) {
2084 kvm_release_page_dirty(vcpu
->arch
.pio
.guest_pages
[i
]);
2085 vcpu
->arch
.pio
.guest_pages
[i
] = NULL
;
2089 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
2091 void *p
= vcpu
->arch
.pio_data
;
2094 int nr_pages
= vcpu
->arch
.pio
.guest_pages
[1] ? 2 : 1;
2096 q
= vmap(vcpu
->arch
.pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
2099 free_pio_guest_pages(vcpu
);
2102 q
+= vcpu
->arch
.pio
.guest_page_offset
;
2103 bytes
= vcpu
->arch
.pio
.size
* vcpu
->arch
.pio
.cur_count
;
2104 if (vcpu
->arch
.pio
.in
)
2105 memcpy(q
, p
, bytes
);
2107 memcpy(p
, q
, bytes
);
2108 q
-= vcpu
->arch
.pio
.guest_page_offset
;
2110 free_pio_guest_pages(vcpu
);
2114 int complete_pio(struct kvm_vcpu
*vcpu
)
2116 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2120 kvm_x86_ops
->cache_regs(vcpu
);
2124 memcpy(&vcpu
->arch
.regs
[VCPU_REGS_RAX
], vcpu
->arch
.pio_data
,
2128 r
= pio_copy_data(vcpu
);
2130 kvm_x86_ops
->cache_regs(vcpu
);
2137 delta
*= io
->cur_count
;
2139 * The size of the register should really depend on
2140 * current address size.
2142 vcpu
->arch
.regs
[VCPU_REGS_RCX
] -= delta
;
2148 vcpu
->arch
.regs
[VCPU_REGS_RDI
] += delta
;
2150 vcpu
->arch
.regs
[VCPU_REGS_RSI
] += delta
;
2153 kvm_x86_ops
->decache_regs(vcpu
);
2155 io
->count
-= io
->cur_count
;
2161 static void kernel_pio(struct kvm_io_device
*pio_dev
,
2162 struct kvm_vcpu
*vcpu
,
2165 /* TODO: String I/O for in kernel device */
2167 mutex_lock(&vcpu
->kvm
->lock
);
2168 if (vcpu
->arch
.pio
.in
)
2169 kvm_iodevice_read(pio_dev
, vcpu
->arch
.pio
.port
,
2170 vcpu
->arch
.pio
.size
,
2173 kvm_iodevice_write(pio_dev
, vcpu
->arch
.pio
.port
,
2174 vcpu
->arch
.pio
.size
,
2176 mutex_unlock(&vcpu
->kvm
->lock
);
2179 static void pio_string_write(struct kvm_io_device
*pio_dev
,
2180 struct kvm_vcpu
*vcpu
)
2182 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2183 void *pd
= vcpu
->arch
.pio_data
;
2186 mutex_lock(&vcpu
->kvm
->lock
);
2187 for (i
= 0; i
< io
->cur_count
; i
++) {
2188 kvm_iodevice_write(pio_dev
, io
->port
,
2193 mutex_unlock(&vcpu
->kvm
->lock
);
2196 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
2199 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
2202 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2203 int size
, unsigned port
)
2205 struct kvm_io_device
*pio_dev
;
2207 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2208 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2209 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2210 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2211 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= 1;
2212 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2213 vcpu
->arch
.pio
.in
= in
;
2214 vcpu
->arch
.pio
.string
= 0;
2215 vcpu
->arch
.pio
.down
= 0;
2216 vcpu
->arch
.pio
.guest_page_offset
= 0;
2217 vcpu
->arch
.pio
.rep
= 0;
2219 kvm_x86_ops
->cache_regs(vcpu
);
2220 memcpy(vcpu
->arch
.pio_data
, &vcpu
->arch
.regs
[VCPU_REGS_RAX
], 4);
2221 kvm_x86_ops
->decache_regs(vcpu
);
2223 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2225 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2227 kernel_pio(pio_dev
, vcpu
, vcpu
->arch
.pio_data
);
2233 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
2235 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2236 int size
, unsigned long count
, int down
,
2237 gva_t address
, int rep
, unsigned port
)
2239 unsigned now
, in_page
;
2243 struct kvm_io_device
*pio_dev
;
2245 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2246 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2247 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2248 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2249 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= count
;
2250 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2251 vcpu
->arch
.pio
.in
= in
;
2252 vcpu
->arch
.pio
.string
= 1;
2253 vcpu
->arch
.pio
.down
= down
;
2254 vcpu
->arch
.pio
.guest_page_offset
= offset_in_page(address
);
2255 vcpu
->arch
.pio
.rep
= rep
;
2258 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2263 in_page
= PAGE_SIZE
- offset_in_page(address
);
2265 in_page
= offset_in_page(address
) + size
;
2266 now
= min(count
, (unsigned long)in_page
/ size
);
2269 * String I/O straddles page boundary. Pin two guest pages
2270 * so that we satisfy atomicity constraints. Do just one
2271 * transaction to avoid complexity.
2278 * String I/O in reverse. Yuck. Kill the guest, fix later.
2280 pr_unimpl(vcpu
, "guest string pio down\n");
2281 kvm_inject_gp(vcpu
, 0);
2284 vcpu
->run
->io
.count
= now
;
2285 vcpu
->arch
.pio
.cur_count
= now
;
2287 if (vcpu
->arch
.pio
.cur_count
== vcpu
->arch
.pio
.count
)
2288 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2290 for (i
= 0; i
< nr_pages
; ++i
) {
2291 down_read(&vcpu
->kvm
->slots_lock
);
2292 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
2293 vcpu
->arch
.pio
.guest_pages
[i
] = page
;
2294 up_read(&vcpu
->kvm
->slots_lock
);
2296 kvm_inject_gp(vcpu
, 0);
2297 free_pio_guest_pages(vcpu
);
2302 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2303 if (!vcpu
->arch
.pio
.in
) {
2304 /* string PIO write */
2305 ret
= pio_copy_data(vcpu
);
2306 if (ret
>= 0 && pio_dev
) {
2307 pio_string_write(pio_dev
, vcpu
);
2309 if (vcpu
->arch
.pio
.count
== 0)
2313 pr_unimpl(vcpu
, "no string pio read support yet, "
2314 "port %x size %d count %ld\n",
2319 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2321 int kvm_arch_init(void *opaque
)
2324 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
2327 printk(KERN_ERR
"kvm: already loaded the other module\n");
2332 if (!ops
->cpu_has_kvm_support()) {
2333 printk(KERN_ERR
"kvm: no hardware support\n");
2337 if (ops
->disabled_by_bios()) {
2338 printk(KERN_ERR
"kvm: disabled by bios\n");
2343 r
= kvm_mmu_module_init();
2347 kvm_init_msr_list();
2350 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2357 void kvm_arch_exit(void)
2360 kvm_mmu_module_exit();
2363 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
2365 ++vcpu
->stat
.halt_exits
;
2366 if (irqchip_in_kernel(vcpu
->kvm
)) {
2367 vcpu
->arch
.mp_state
= VCPU_MP_STATE_HALTED
;
2368 kvm_vcpu_block(vcpu
);
2369 if (vcpu
->arch
.mp_state
!= VCPU_MP_STATE_RUNNABLE
)
2373 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
2377 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
2379 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
2381 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
2383 kvm_x86_ops
->cache_regs(vcpu
);
2385 nr
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2386 a0
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2387 a1
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2388 a2
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2389 a3
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2391 if (!is_long_mode(vcpu
)) {
2400 case KVM_HC_VAPIC_POLL_IRQ
:
2407 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = ret
;
2408 kvm_x86_ops
->decache_regs(vcpu
);
2411 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
2413 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
2415 char instruction
[3];
2420 * Blow out the MMU to ensure that no other VCPU has an active mapping
2421 * to ensure that the updated hypercall appears atomically across all
2424 kvm_mmu_zap_all(vcpu
->kvm
);
2426 kvm_x86_ops
->cache_regs(vcpu
);
2427 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
2428 if (emulator_write_emulated(vcpu
->arch
.rip
, instruction
, 3, vcpu
)
2429 != X86EMUL_CONTINUE
)
2435 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
2437 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
2440 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2442 struct descriptor_table dt
= { limit
, base
};
2444 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2447 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2449 struct descriptor_table dt
= { limit
, base
};
2451 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2454 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
2455 unsigned long *rflags
)
2458 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2461 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
2463 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2466 return vcpu
->arch
.cr0
;
2468 return vcpu
->arch
.cr2
;
2470 return vcpu
->arch
.cr3
;
2472 return vcpu
->arch
.cr4
;
2474 return get_cr8(vcpu
);
2476 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
2481 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
2482 unsigned long *rflags
)
2486 set_cr0(vcpu
, mk_cr_64(vcpu
->arch
.cr0
, val
));
2487 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2490 vcpu
->arch
.cr2
= val
;
2496 set_cr4(vcpu
, mk_cr_64(vcpu
->arch
.cr4
, val
));
2499 set_cr8(vcpu
, val
& 0xfUL
);
2502 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
2506 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
2508 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
2509 int j
, nent
= vcpu
->arch
.cpuid_nent
;
2511 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
2512 /* when no next entry is found, the current entry[i] is reselected */
2513 for (j
= i
+ 1; j
== i
; j
= (j
+ 1) % nent
) {
2514 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
2515 if (ej
->function
== e
->function
) {
2516 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
2520 return 0; /* silence gcc, even though control never reaches here */
2523 /* find an entry with matching function, matching index (if needed), and that
2524 * should be read next (if it's stateful) */
2525 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
2526 u32 function
, u32 index
)
2528 if (e
->function
!= function
)
2530 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
2532 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
2533 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
2538 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
2541 u32 function
, index
;
2542 struct kvm_cpuid_entry2
*e
, *best
;
2544 kvm_x86_ops
->cache_regs(vcpu
);
2545 function
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2546 index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2547 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = 0;
2548 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = 0;
2549 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = 0;
2550 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = 0;
2552 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
2553 e
= &vcpu
->arch
.cpuid_entries
[i
];
2554 if (is_matching_cpuid_entry(e
, function
, index
)) {
2555 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
2556 move_to_next_stateful_cpuid_entry(vcpu
, i
);
2561 * Both basic or both extended?
2563 if (((e
->function
^ function
) & 0x80000000) == 0)
2564 if (!best
|| e
->function
> best
->function
)
2568 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = best
->eax
;
2569 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = best
->ebx
;
2570 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = best
->ecx
;
2571 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = best
->edx
;
2573 kvm_x86_ops
->decache_regs(vcpu
);
2574 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2576 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
2579 * Check if userspace requested an interrupt window, and that the
2580 * interrupt window is open.
2582 * No need to exit to userspace if we already have an interrupt queued.
2584 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
2585 struct kvm_run
*kvm_run
)
2587 return (!vcpu
->arch
.irq_summary
&&
2588 kvm_run
->request_interrupt_window
&&
2589 vcpu
->arch
.interrupt_window_open
&&
2590 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
2593 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
2594 struct kvm_run
*kvm_run
)
2596 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
2597 kvm_run
->cr8
= get_cr8(vcpu
);
2598 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
2599 if (irqchip_in_kernel(vcpu
->kvm
))
2600 kvm_run
->ready_for_interrupt_injection
= 1;
2602 kvm_run
->ready_for_interrupt_injection
=
2603 (vcpu
->arch
.interrupt_window_open
&&
2604 vcpu
->arch
.irq_summary
== 0);
2607 static void vapic_enter(struct kvm_vcpu
*vcpu
)
2609 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2612 if (!apic
|| !apic
->vapic_addr
)
2615 down_read(¤t
->mm
->mmap_sem
);
2616 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2617 up_read(¤t
->mm
->mmap_sem
);
2619 vcpu
->arch
.apic
->vapic_page
= page
;
2622 static void vapic_exit(struct kvm_vcpu
*vcpu
)
2624 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2626 if (!apic
|| !apic
->vapic_addr
)
2629 kvm_release_page_dirty(apic
->vapic_page
);
2630 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2633 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2637 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
)) {
2638 pr_debug("vcpu %d received sipi with vector # %x\n",
2639 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
2640 kvm_lapic_reset(vcpu
);
2641 r
= kvm_x86_ops
->vcpu_reset(vcpu
);
2644 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
2650 if (vcpu
->guest_debug
.enabled
)
2651 kvm_x86_ops
->guest_debug_pre(vcpu
);
2654 r
= kvm_mmu_reload(vcpu
);
2658 if (vcpu
->requests
) {
2659 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
2660 __kvm_migrate_apic_timer(vcpu
);
2661 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
2663 kvm_run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
2669 kvm_inject_pending_timer_irqs(vcpu
);
2673 kvm_x86_ops
->prepare_guest_switch(vcpu
);
2674 kvm_load_guest_fpu(vcpu
);
2676 local_irq_disable();
2678 if (need_resched()) {
2685 if (signal_pending(current
)) {
2689 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2690 ++vcpu
->stat
.signal_exits
;
2694 if (vcpu
->arch
.exception
.pending
)
2695 __queue_exception(vcpu
);
2696 else if (irqchip_in_kernel(vcpu
->kvm
))
2697 kvm_x86_ops
->inject_pending_irq(vcpu
);
2699 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
2701 kvm_lapic_sync_to_vapic(vcpu
);
2703 vcpu
->guest_mode
= 1;
2707 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
2708 kvm_x86_ops
->tlb_flush(vcpu
);
2710 kvm_x86_ops
->run(vcpu
, kvm_run
);
2712 vcpu
->guest_mode
= 0;
2718 * We must have an instruction between local_irq_enable() and
2719 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2720 * the interrupt shadow. The stat.exits increment will do nicely.
2721 * But we need to prevent reordering, hence this barrier():
2730 * Profile KVM exit RIPs:
2732 if (unlikely(prof_on
== KVM_PROFILING
)) {
2733 kvm_x86_ops
->cache_regs(vcpu
);
2734 profile_hit(KVM_PROFILING
, (void *)vcpu
->arch
.rip
);
2737 if (vcpu
->arch
.exception
.pending
&& kvm_x86_ops
->exception_injected(vcpu
))
2738 vcpu
->arch
.exception
.pending
= false;
2740 kvm_lapic_sync_from_vapic(vcpu
);
2742 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
2745 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
2747 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2748 ++vcpu
->stat
.request_irq_exits
;
2751 if (!need_resched())
2761 post_kvm_run_save(vcpu
, kvm_run
);
2768 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2775 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_UNINITIALIZED
)) {
2776 kvm_vcpu_block(vcpu
);
2781 if (vcpu
->sigset_active
)
2782 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2784 /* re-sync apic's tpr */
2785 if (!irqchip_in_kernel(vcpu
->kvm
))
2786 set_cr8(vcpu
, kvm_run
->cr8
);
2788 if (vcpu
->arch
.pio
.cur_count
) {
2789 r
= complete_pio(vcpu
);
2793 #if CONFIG_HAS_IOMEM
2794 if (vcpu
->mmio_needed
) {
2795 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
2796 vcpu
->mmio_read_completed
= 1;
2797 vcpu
->mmio_needed
= 0;
2798 r
= emulate_instruction(vcpu
, kvm_run
,
2799 vcpu
->arch
.mmio_fault_cr2
, 0,
2800 EMULTYPE_NO_DECODE
);
2801 if (r
== EMULATE_DO_MMIO
) {
2803 * Read-modify-write. Back to userspace.
2810 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
2811 kvm_x86_ops
->cache_regs(vcpu
);
2812 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
2813 kvm_x86_ops
->decache_regs(vcpu
);
2816 r
= __vcpu_run(vcpu
, kvm_run
);
2819 if (vcpu
->sigset_active
)
2820 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2826 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2830 kvm_x86_ops
->cache_regs(vcpu
);
2832 regs
->rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2833 regs
->rbx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2834 regs
->rcx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2835 regs
->rdx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2836 regs
->rsi
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2837 regs
->rdi
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
2838 regs
->rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
2839 regs
->rbp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
2840 #ifdef CONFIG_X86_64
2841 regs
->r8
= vcpu
->arch
.regs
[VCPU_REGS_R8
];
2842 regs
->r9
= vcpu
->arch
.regs
[VCPU_REGS_R9
];
2843 regs
->r10
= vcpu
->arch
.regs
[VCPU_REGS_R10
];
2844 regs
->r11
= vcpu
->arch
.regs
[VCPU_REGS_R11
];
2845 regs
->r12
= vcpu
->arch
.regs
[VCPU_REGS_R12
];
2846 regs
->r13
= vcpu
->arch
.regs
[VCPU_REGS_R13
];
2847 regs
->r14
= vcpu
->arch
.regs
[VCPU_REGS_R14
];
2848 regs
->r15
= vcpu
->arch
.regs
[VCPU_REGS_R15
];
2851 regs
->rip
= vcpu
->arch
.rip
;
2852 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2855 * Don't leak debug flags in case they were set for guest debugging
2857 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2858 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2865 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2869 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = regs
->rax
;
2870 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2871 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2872 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2873 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2874 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2875 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2876 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2877 #ifdef CONFIG_X86_64
2878 vcpu
->arch
.regs
[VCPU_REGS_R8
] = regs
->r8
;
2879 vcpu
->arch
.regs
[VCPU_REGS_R9
] = regs
->r9
;
2880 vcpu
->arch
.regs
[VCPU_REGS_R10
] = regs
->r10
;
2881 vcpu
->arch
.regs
[VCPU_REGS_R11
] = regs
->r11
;
2882 vcpu
->arch
.regs
[VCPU_REGS_R12
] = regs
->r12
;
2883 vcpu
->arch
.regs
[VCPU_REGS_R13
] = regs
->r13
;
2884 vcpu
->arch
.regs
[VCPU_REGS_R14
] = regs
->r14
;
2885 vcpu
->arch
.regs
[VCPU_REGS_R15
] = regs
->r15
;
2888 vcpu
->arch
.rip
= regs
->rip
;
2889 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
2891 kvm_x86_ops
->decache_regs(vcpu
);
2898 static void get_segment(struct kvm_vcpu
*vcpu
,
2899 struct kvm_segment
*var
, int seg
)
2901 return kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
2904 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
2906 struct kvm_segment cs
;
2908 get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
2912 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
2914 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2915 struct kvm_sregs
*sregs
)
2917 struct descriptor_table dt
;
2922 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2923 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2924 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2925 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2926 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2927 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2929 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2930 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2932 kvm_x86_ops
->get_idt(vcpu
, &dt
);
2933 sregs
->idt
.limit
= dt
.limit
;
2934 sregs
->idt
.base
= dt
.base
;
2935 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
2936 sregs
->gdt
.limit
= dt
.limit
;
2937 sregs
->gdt
.base
= dt
.base
;
2939 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2940 sregs
->cr0
= vcpu
->arch
.cr0
;
2941 sregs
->cr2
= vcpu
->arch
.cr2
;
2942 sregs
->cr3
= vcpu
->arch
.cr3
;
2943 sregs
->cr4
= vcpu
->arch
.cr4
;
2944 sregs
->cr8
= get_cr8(vcpu
);
2945 sregs
->efer
= vcpu
->arch
.shadow_efer
;
2946 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
2948 if (irqchip_in_kernel(vcpu
->kvm
)) {
2949 memset(sregs
->interrupt_bitmap
, 0,
2950 sizeof sregs
->interrupt_bitmap
);
2951 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
2952 if (pending_vec
>= 0)
2953 set_bit(pending_vec
,
2954 (unsigned long *)sregs
->interrupt_bitmap
);
2956 memcpy(sregs
->interrupt_bitmap
, vcpu
->arch
.irq_pending
,
2957 sizeof sregs
->interrupt_bitmap
);
2964 static void set_segment(struct kvm_vcpu
*vcpu
,
2965 struct kvm_segment
*var
, int seg
)
2967 return kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
2970 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2971 struct kvm_sregs
*sregs
)
2973 int mmu_reset_needed
= 0;
2974 int i
, pending_vec
, max_bits
;
2975 struct descriptor_table dt
;
2979 dt
.limit
= sregs
->idt
.limit
;
2980 dt
.base
= sregs
->idt
.base
;
2981 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2982 dt
.limit
= sregs
->gdt
.limit
;
2983 dt
.base
= sregs
->gdt
.base
;
2984 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2986 vcpu
->arch
.cr2
= sregs
->cr2
;
2987 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
2988 vcpu
->arch
.cr3
= sregs
->cr3
;
2990 set_cr8(vcpu
, sregs
->cr8
);
2992 mmu_reset_needed
|= vcpu
->arch
.shadow_efer
!= sregs
->efer
;
2993 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
2994 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
2996 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2998 mmu_reset_needed
|= vcpu
->arch
.cr0
!= sregs
->cr0
;
2999 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
3000 vcpu
->arch
.cr0
= sregs
->cr0
;
3002 mmu_reset_needed
|= vcpu
->arch
.cr4
!= sregs
->cr4
;
3003 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
3004 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
3005 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
3007 if (mmu_reset_needed
)
3008 kvm_mmu_reset_context(vcpu
);
3010 if (!irqchip_in_kernel(vcpu
->kvm
)) {
3011 memcpy(vcpu
->arch
.irq_pending
, sregs
->interrupt_bitmap
,
3012 sizeof vcpu
->arch
.irq_pending
);
3013 vcpu
->arch
.irq_summary
= 0;
3014 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.irq_pending
); ++i
)
3015 if (vcpu
->arch
.irq_pending
[i
])
3016 __set_bit(i
, &vcpu
->arch
.irq_summary
);
3018 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
3019 pending_vec
= find_first_bit(
3020 (const unsigned long *)sregs
->interrupt_bitmap
,
3022 /* Only pending external irq is handled here */
3023 if (pending_vec
< max_bits
) {
3024 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
3025 pr_debug("Set back pending irq %d\n",
3030 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3031 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3032 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3033 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3034 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3035 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3037 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3038 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3045 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
3046 struct kvm_debug_guest
*dbg
)
3052 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
3060 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3061 * we have asm/x86/processor.h
3072 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3073 #ifdef CONFIG_X86_64
3074 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3076 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3081 * Translate a guest virtual address to a guest physical address.
3083 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
3084 struct kvm_translation
*tr
)
3086 unsigned long vaddr
= tr
->linear_address
;
3090 down_read(&vcpu
->kvm
->slots_lock
);
3091 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, vaddr
);
3092 up_read(&vcpu
->kvm
->slots_lock
);
3093 tr
->physical_address
= gpa
;
3094 tr
->valid
= gpa
!= UNMAPPED_GVA
;
3102 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3104 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3108 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
3109 fpu
->fcw
= fxsave
->cwd
;
3110 fpu
->fsw
= fxsave
->swd
;
3111 fpu
->ftwx
= fxsave
->twd
;
3112 fpu
->last_opcode
= fxsave
->fop
;
3113 fpu
->last_ip
= fxsave
->rip
;
3114 fpu
->last_dp
= fxsave
->rdp
;
3115 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
3122 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3124 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3128 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
3129 fxsave
->cwd
= fpu
->fcw
;
3130 fxsave
->swd
= fpu
->fsw
;
3131 fxsave
->twd
= fpu
->ftwx
;
3132 fxsave
->fop
= fpu
->last_opcode
;
3133 fxsave
->rip
= fpu
->last_ip
;
3134 fxsave
->rdp
= fpu
->last_dp
;
3135 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
3142 void fx_init(struct kvm_vcpu
*vcpu
)
3144 unsigned after_mxcsr_mask
;
3146 /* Initialize guest FPU by resetting ours and saving into guest's */
3148 fx_save(&vcpu
->arch
.host_fx_image
);
3150 fx_save(&vcpu
->arch
.guest_fx_image
);
3151 fx_restore(&vcpu
->arch
.host_fx_image
);
3154 vcpu
->arch
.cr0
|= X86_CR0_ET
;
3155 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
3156 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
3157 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
3158 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
3160 EXPORT_SYMBOL_GPL(fx_init
);
3162 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
3164 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
3167 vcpu
->guest_fpu_loaded
= 1;
3168 fx_save(&vcpu
->arch
.host_fx_image
);
3169 fx_restore(&vcpu
->arch
.guest_fx_image
);
3171 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
3173 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
3175 if (!vcpu
->guest_fpu_loaded
)
3178 vcpu
->guest_fpu_loaded
= 0;
3179 fx_save(&vcpu
->arch
.guest_fx_image
);
3180 fx_restore(&vcpu
->arch
.host_fx_image
);
3181 ++vcpu
->stat
.fpu_reload
;
3183 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
3185 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
3187 kvm_x86_ops
->vcpu_free(vcpu
);
3190 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
3193 return kvm_x86_ops
->vcpu_create(kvm
, id
);
3196 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
3200 /* We do fxsave: this must be aligned. */
3201 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
3204 r
= kvm_arch_vcpu_reset(vcpu
);
3206 r
= kvm_mmu_setup(vcpu
);
3213 kvm_x86_ops
->vcpu_free(vcpu
);
3217 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
3220 kvm_mmu_unload(vcpu
);
3223 kvm_x86_ops
->vcpu_free(vcpu
);
3226 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
3228 return kvm_x86_ops
->vcpu_reset(vcpu
);
3231 void kvm_arch_hardware_enable(void *garbage
)
3233 kvm_x86_ops
->hardware_enable(garbage
);
3236 void kvm_arch_hardware_disable(void *garbage
)
3238 kvm_x86_ops
->hardware_disable(garbage
);
3241 int kvm_arch_hardware_setup(void)
3243 return kvm_x86_ops
->hardware_setup();
3246 void kvm_arch_hardware_unsetup(void)
3248 kvm_x86_ops
->hardware_unsetup();
3251 void kvm_arch_check_processor_compat(void *rtn
)
3253 kvm_x86_ops
->check_processor_compatibility(rtn
);
3256 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
3262 BUG_ON(vcpu
->kvm
== NULL
);
3265 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
3266 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
3267 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
3269 vcpu
->arch
.mp_state
= VCPU_MP_STATE_UNINITIALIZED
;
3271 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
3276 vcpu
->arch
.pio_data
= page_address(page
);
3278 r
= kvm_mmu_create(vcpu
);
3280 goto fail_free_pio_data
;
3282 if (irqchip_in_kernel(kvm
)) {
3283 r
= kvm_create_lapic(vcpu
);
3285 goto fail_mmu_destroy
;
3291 kvm_mmu_destroy(vcpu
);
3293 free_page((unsigned long)vcpu
->arch
.pio_data
);
3298 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
3300 kvm_free_lapic(vcpu
);
3301 kvm_mmu_destroy(vcpu
);
3302 free_page((unsigned long)vcpu
->arch
.pio_data
);
3305 struct kvm
*kvm_arch_create_vm(void)
3307 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
3310 return ERR_PTR(-ENOMEM
);
3312 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
3317 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
3320 kvm_mmu_unload(vcpu
);
3324 static void kvm_free_vcpus(struct kvm
*kvm
)
3329 * Unpin any mmu pages first.
3331 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
3333 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
3334 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3335 if (kvm
->vcpus
[i
]) {
3336 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
3337 kvm
->vcpus
[i
] = NULL
;
3343 void kvm_arch_destroy_vm(struct kvm
*kvm
)
3345 kfree(kvm
->arch
.vpic
);
3346 kfree(kvm
->arch
.vioapic
);
3347 kvm_free_vcpus(kvm
);
3348 kvm_free_physmem(kvm
);
3352 int kvm_arch_set_memory_region(struct kvm
*kvm
,
3353 struct kvm_userspace_memory_region
*mem
,
3354 struct kvm_memory_slot old
,
3357 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
3358 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
3360 /*To keep backward compatibility with older userspace,
3361 *x86 needs to hanlde !user_alloc case.
3364 if (npages
&& !old
.rmap
) {
3365 down_write(¤t
->mm
->mmap_sem
);
3366 memslot
->userspace_addr
= do_mmap(NULL
, 0,
3368 PROT_READ
| PROT_WRITE
,
3369 MAP_SHARED
| MAP_ANONYMOUS
,
3371 up_write(¤t
->mm
->mmap_sem
);
3373 if (IS_ERR((void *)memslot
->userspace_addr
))
3374 return PTR_ERR((void *)memslot
->userspace_addr
);
3376 if (!old
.user_alloc
&& old
.rmap
) {
3379 down_write(¤t
->mm
->mmap_sem
);
3380 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
3381 old
.npages
* PAGE_SIZE
);
3382 up_write(¤t
->mm
->mmap_sem
);
3385 "kvm_vm_ioctl_set_memory_region: "
3386 "failed to munmap memory\n");
3391 if (!kvm
->arch
.n_requested_mmu_pages
) {
3392 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
3393 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
3396 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
3397 kvm_flush_remote_tlbs(kvm
);
3402 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
3404 return vcpu
->arch
.mp_state
== VCPU_MP_STATE_RUNNABLE
3405 || vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
;
3408 static void vcpu_kick_intr(void *info
)
3411 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)info
;
3412 printk(KERN_DEBUG
"vcpu_kick_intr %p \n", vcpu
);
3416 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
3418 int ipi_pcpu
= vcpu
->cpu
;
3420 if (waitqueue_active(&vcpu
->wq
)) {
3421 wake_up_interruptible(&vcpu
->wq
);
3422 ++vcpu
->stat
.halt_wakeup
;
3424 if (vcpu
->guest_mode
)
3425 smp_call_function_single(ipi_pcpu
, vcpu_kick_intr
, vcpu
, 0, 0);