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/kvm.h>
24 #include <linux/vmalloc.h>
25 #include <linux/module.h>
26 #include <linux/mman.h>
27 #include <linux/highmem.h>
29 #include <asm/uaccess.h>
32 #define MAX_IO_MSRS 256
33 #define CR0_RESERVED_BITS \
34 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
35 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
36 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
37 #define CR4_RESERVED_BITS \
38 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
39 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
40 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
41 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
43 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
44 #define EFER_RESERVED_BITS 0xfffffffffffff2fe
46 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
47 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
49 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
51 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
52 struct kvm_cpuid_entry2 __user
*entries
);
54 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
55 struct kvm_x86_ops
*kvm_x86_ops
;
57 struct kvm_stats_debugfs_item debugfs_entries
[] = {
58 { "pf_fixed", VCPU_STAT(pf_fixed
) },
59 { "pf_guest", VCPU_STAT(pf_guest
) },
60 { "tlb_flush", VCPU_STAT(tlb_flush
) },
61 { "invlpg", VCPU_STAT(invlpg
) },
62 { "exits", VCPU_STAT(exits
) },
63 { "io_exits", VCPU_STAT(io_exits
) },
64 { "mmio_exits", VCPU_STAT(mmio_exits
) },
65 { "signal_exits", VCPU_STAT(signal_exits
) },
66 { "irq_window", VCPU_STAT(irq_window_exits
) },
67 { "halt_exits", VCPU_STAT(halt_exits
) },
68 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
69 { "request_irq", VCPU_STAT(request_irq_exits
) },
70 { "irq_exits", VCPU_STAT(irq_exits
) },
71 { "host_state_reload", VCPU_STAT(host_state_reload
) },
72 { "efer_reload", VCPU_STAT(efer_reload
) },
73 { "fpu_reload", VCPU_STAT(fpu_reload
) },
74 { "insn_emulation", VCPU_STAT(insn_emulation
) },
75 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail
) },
76 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped
) },
77 { "mmu_pte_write", VM_STAT(mmu_pte_write
) },
78 { "mmu_pte_updated", VM_STAT(mmu_pte_updated
) },
79 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped
) },
80 { "mmu_flooded", VM_STAT(mmu_flooded
) },
81 { "mmu_recycled", VM_STAT(mmu_recycled
) },
82 { "mmu_cache_miss", VM_STAT(mmu_cache_miss
) },
83 { "remote_tlb_flush", VM_STAT(remote_tlb_flush
) },
88 unsigned long segment_base(u16 selector
)
90 struct descriptor_table gdt
;
91 struct segment_descriptor
*d
;
92 unsigned long table_base
;
98 asm("sgdt %0" : "=m"(gdt
));
99 table_base
= gdt
.base
;
101 if (selector
& 4) { /* from ldt */
104 asm("sldt %0" : "=g"(ldt_selector
));
105 table_base
= segment_base(ldt_selector
);
107 d
= (struct segment_descriptor
*)(table_base
+ (selector
& ~7));
108 v
= d
->base_low
| ((unsigned long)d
->base_mid
<< 16) |
109 ((unsigned long)d
->base_high
<< 24);
111 if (d
->system
== 0 && (d
->type
== 2 || d
->type
== 9 || d
->type
== 11))
112 v
|= ((unsigned long) \
113 ((struct segment_descriptor_64
*)d
)->base_higher
) << 32;
117 EXPORT_SYMBOL_GPL(segment_base
);
119 u64
kvm_get_apic_base(struct kvm_vcpu
*vcpu
)
121 if (irqchip_in_kernel(vcpu
->kvm
))
122 return vcpu
->arch
.apic_base
;
124 return vcpu
->arch
.apic_base
;
126 EXPORT_SYMBOL_GPL(kvm_get_apic_base
);
128 void kvm_set_apic_base(struct kvm_vcpu
*vcpu
, u64 data
)
130 /* TODO: reserve bits check */
131 if (irqchip_in_kernel(vcpu
->kvm
))
132 kvm_lapic_set_base(vcpu
, data
);
134 vcpu
->arch
.apic_base
= data
;
136 EXPORT_SYMBOL_GPL(kvm_set_apic_base
);
138 void kvm_queue_exception(struct kvm_vcpu
*vcpu
, unsigned nr
)
140 WARN_ON(vcpu
->arch
.exception
.pending
);
141 vcpu
->arch
.exception
.pending
= true;
142 vcpu
->arch
.exception
.has_error_code
= false;
143 vcpu
->arch
.exception
.nr
= nr
;
145 EXPORT_SYMBOL_GPL(kvm_queue_exception
);
147 void kvm_inject_page_fault(struct kvm_vcpu
*vcpu
, unsigned long addr
,
150 ++vcpu
->stat
.pf_guest
;
151 if (vcpu
->arch
.exception
.pending
&& vcpu
->arch
.exception
.nr
== PF_VECTOR
) {
152 printk(KERN_DEBUG
"kvm: inject_page_fault:"
153 " double fault 0x%lx\n", addr
);
154 vcpu
->arch
.exception
.nr
= DF_VECTOR
;
155 vcpu
->arch
.exception
.error_code
= 0;
158 vcpu
->arch
.cr2
= addr
;
159 kvm_queue_exception_e(vcpu
, PF_VECTOR
, error_code
);
162 void kvm_queue_exception_e(struct kvm_vcpu
*vcpu
, unsigned nr
, u32 error_code
)
164 WARN_ON(vcpu
->arch
.exception
.pending
);
165 vcpu
->arch
.exception
.pending
= true;
166 vcpu
->arch
.exception
.has_error_code
= true;
167 vcpu
->arch
.exception
.nr
= nr
;
168 vcpu
->arch
.exception
.error_code
= error_code
;
170 EXPORT_SYMBOL_GPL(kvm_queue_exception_e
);
172 static void __queue_exception(struct kvm_vcpu
*vcpu
)
174 kvm_x86_ops
->queue_exception(vcpu
, vcpu
->arch
.exception
.nr
,
175 vcpu
->arch
.exception
.has_error_code
,
176 vcpu
->arch
.exception
.error_code
);
180 * Load the pae pdptrs. Return true is they are all valid.
182 int load_pdptrs(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
184 gfn_t pdpt_gfn
= cr3
>> PAGE_SHIFT
;
185 unsigned offset
= ((cr3
& (PAGE_SIZE
-1)) >> 5) << 2;
188 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
190 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
191 down_read(¤t
->mm
->mmap_sem
);
193 down_read(&vcpu
->kvm
->slots_lock
);
194 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
195 ret
= kvm_read_guest_page(vcpu
->kvm
, pdpt_gfn
, pdpte
,
196 offset
* sizeof(u64
), sizeof(pdpte
));
201 for (i
= 0; i
< ARRAY_SIZE(pdpte
); ++i
) {
202 if ((pdpte
[i
] & 1) && (pdpte
[i
] & 0xfffffff0000001e6ull
)) {
209 memcpy(vcpu
->arch
.pdptrs
, pdpte
, sizeof(vcpu
->arch
.pdptrs
));
211 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
212 up_read(¤t
->mm
->mmap_sem
);
214 up_read(&vcpu
->kvm
->slots_lock
);
215 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
220 static bool pdptrs_changed(struct kvm_vcpu
*vcpu
)
222 u64 pdpte
[ARRAY_SIZE(vcpu
->arch
.pdptrs
)];
226 if (is_long_mode(vcpu
) || !is_pae(vcpu
))
229 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
230 down_read(¤t
->mm
->mmap_sem
);
232 down_read(&vcpu
->kvm
->slots_lock
);
233 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
234 r
= kvm_read_guest(vcpu
->kvm
, vcpu
->arch
.cr3
& ~31u, pdpte
, sizeof(pdpte
));
237 changed
= memcmp(pdpte
, vcpu
->arch
.pdptrs
, sizeof(pdpte
)) != 0;
239 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
240 up_read(¤t
->mm
->mmap_sem
);
242 up_read(&vcpu
->kvm
->slots_lock
);
243 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
248 void set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
250 if (cr0
& CR0_RESERVED_BITS
) {
251 printk(KERN_DEBUG
"set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
252 cr0
, vcpu
->arch
.cr0
);
253 kvm_inject_gp(vcpu
, 0);
257 if ((cr0
& X86_CR0_NW
) && !(cr0
& X86_CR0_CD
)) {
258 printk(KERN_DEBUG
"set_cr0: #GP, CD == 0 && NW == 1\n");
259 kvm_inject_gp(vcpu
, 0);
263 if ((cr0
& X86_CR0_PG
) && !(cr0
& X86_CR0_PE
)) {
264 printk(KERN_DEBUG
"set_cr0: #GP, set PG flag "
265 "and a clear PE flag\n");
266 kvm_inject_gp(vcpu
, 0);
270 if (!is_paging(vcpu
) && (cr0
& X86_CR0_PG
)) {
272 if ((vcpu
->arch
.shadow_efer
& EFER_LME
)) {
276 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
277 "in long mode while PAE is disabled\n");
278 kvm_inject_gp(vcpu
, 0);
281 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
283 printk(KERN_DEBUG
"set_cr0: #GP, start paging "
284 "in long mode while CS.L == 1\n");
285 kvm_inject_gp(vcpu
, 0);
291 if (is_pae(vcpu
) && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
292 printk(KERN_DEBUG
"set_cr0: #GP, pdptrs "
294 kvm_inject_gp(vcpu
, 0);
300 kvm_x86_ops
->set_cr0(vcpu
, cr0
);
301 vcpu
->arch
.cr0
= cr0
;
303 kvm_mmu_reset_context(vcpu
);
306 EXPORT_SYMBOL_GPL(set_cr0
);
308 void lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
)
310 set_cr0(vcpu
, (vcpu
->arch
.cr0
& ~0x0ful
) | (msw
& 0x0f));
312 EXPORT_SYMBOL_GPL(lmsw
);
314 void set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
316 if (cr4
& CR4_RESERVED_BITS
) {
317 printk(KERN_DEBUG
"set_cr4: #GP, reserved bits\n");
318 kvm_inject_gp(vcpu
, 0);
322 if (is_long_mode(vcpu
)) {
323 if (!(cr4
& X86_CR4_PAE
)) {
324 printk(KERN_DEBUG
"set_cr4: #GP, clearing PAE while "
326 kvm_inject_gp(vcpu
, 0);
329 } else if (is_paging(vcpu
) && !is_pae(vcpu
) && (cr4
& X86_CR4_PAE
)
330 && !load_pdptrs(vcpu
, vcpu
->arch
.cr3
)) {
331 printk(KERN_DEBUG
"set_cr4: #GP, pdptrs reserved bits\n");
332 kvm_inject_gp(vcpu
, 0);
336 if (cr4
& X86_CR4_VMXE
) {
337 printk(KERN_DEBUG
"set_cr4: #GP, setting VMXE\n");
338 kvm_inject_gp(vcpu
, 0);
341 kvm_x86_ops
->set_cr4(vcpu
, cr4
);
342 vcpu
->arch
.cr4
= cr4
;
343 kvm_mmu_reset_context(vcpu
);
345 EXPORT_SYMBOL_GPL(set_cr4
);
347 void set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
349 if (cr3
== vcpu
->arch
.cr3
&& !pdptrs_changed(vcpu
)) {
350 kvm_mmu_flush_tlb(vcpu
);
354 if (is_long_mode(vcpu
)) {
355 if (cr3
& CR3_L_MODE_RESERVED_BITS
) {
356 printk(KERN_DEBUG
"set_cr3: #GP, reserved bits\n");
357 kvm_inject_gp(vcpu
, 0);
362 if (cr3
& CR3_PAE_RESERVED_BITS
) {
364 "set_cr3: #GP, reserved bits\n");
365 kvm_inject_gp(vcpu
, 0);
368 if (is_paging(vcpu
) && !load_pdptrs(vcpu
, cr3
)) {
369 printk(KERN_DEBUG
"set_cr3: #GP, pdptrs "
371 kvm_inject_gp(vcpu
, 0);
376 * We don't check reserved bits in nonpae mode, because
377 * this isn't enforced, and VMware depends on this.
381 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
382 down_read(¤t
->mm
->mmap_sem
);
384 down_read(&vcpu
->kvm
->slots_lock
);
385 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
387 * Does the new cr3 value map to physical memory? (Note, we
388 * catch an invalid cr3 even in real-mode, because it would
389 * cause trouble later on when we turn on paging anyway.)
391 * A real CPU would silently accept an invalid cr3 and would
392 * attempt to use it - with largely undefined (and often hard
393 * to debug) behavior on the guest side.
395 if (unlikely(!gfn_to_memslot(vcpu
->kvm
, cr3
>> PAGE_SHIFT
)))
396 kvm_inject_gp(vcpu
, 0);
398 vcpu
->arch
.cr3
= cr3
;
399 vcpu
->arch
.mmu
.new_cr3(vcpu
);
401 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
402 up_read(¤t
->mm
->mmap_sem
);
404 up_read(&vcpu
->kvm
->slots_lock
);
405 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
407 EXPORT_SYMBOL_GPL(set_cr3
);
409 void set_cr8(struct kvm_vcpu
*vcpu
, unsigned long cr8
)
411 if (cr8
& CR8_RESERVED_BITS
) {
412 printk(KERN_DEBUG
"set_cr8: #GP, reserved bits 0x%lx\n", cr8
);
413 kvm_inject_gp(vcpu
, 0);
416 if (irqchip_in_kernel(vcpu
->kvm
))
417 kvm_lapic_set_tpr(vcpu
, cr8
);
419 vcpu
->arch
.cr8
= cr8
;
421 EXPORT_SYMBOL_GPL(set_cr8
);
423 unsigned long get_cr8(struct kvm_vcpu
*vcpu
)
425 if (irqchip_in_kernel(vcpu
->kvm
))
426 return kvm_lapic_get_cr8(vcpu
);
428 return vcpu
->arch
.cr8
;
430 EXPORT_SYMBOL_GPL(get_cr8
);
433 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
434 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
436 * This list is modified at module load time to reflect the
437 * capabilities of the host cpu.
439 static u32 msrs_to_save
[] = {
440 MSR_IA32_SYSENTER_CS
, MSR_IA32_SYSENTER_ESP
, MSR_IA32_SYSENTER_EIP
,
443 MSR_CSTAR
, MSR_KERNEL_GS_BASE
, MSR_SYSCALL_MASK
, MSR_LSTAR
,
445 MSR_IA32_TIME_STAMP_COUNTER
,
448 static unsigned num_msrs_to_save
;
450 static u32 emulated_msrs
[] = {
451 MSR_IA32_MISC_ENABLE
,
456 static void set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
458 if (efer
& EFER_RESERVED_BITS
) {
459 printk(KERN_DEBUG
"set_efer: 0x%llx #GP, reserved bits\n",
461 kvm_inject_gp(vcpu
, 0);
466 && (vcpu
->arch
.shadow_efer
& EFER_LME
) != (efer
& EFER_LME
)) {
467 printk(KERN_DEBUG
"set_efer: #GP, change LME while paging\n");
468 kvm_inject_gp(vcpu
, 0);
472 kvm_x86_ops
->set_efer(vcpu
, efer
);
475 efer
|= vcpu
->arch
.shadow_efer
& EFER_LMA
;
477 vcpu
->arch
.shadow_efer
= efer
;
483 * Writes msr value into into the appropriate "register".
484 * Returns 0 on success, non-0 otherwise.
485 * Assumes vcpu_load() was already called.
487 int kvm_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
489 return kvm_x86_ops
->set_msr(vcpu
, msr_index
, data
);
493 * Adapt set_msr() to msr_io()'s calling convention
495 static int do_set_msr(struct kvm_vcpu
*vcpu
, unsigned index
, u64
*data
)
497 return kvm_set_msr(vcpu
, index
, *data
);
501 int kvm_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
)
506 set_efer(vcpu
, data
);
509 case MSR_IA32_MC0_STATUS
:
510 pr_unimpl(vcpu
, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
513 case MSR_IA32_MCG_STATUS
:
514 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
517 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
519 case MSR_IA32_MCG_CTL
:
520 pr_unimpl(vcpu
, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
523 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
524 case MSR_IA32_UCODE_REV
:
525 case MSR_IA32_UCODE_WRITE
:
526 case 0x200 ... 0x2ff: /* MTRRs */
528 case MSR_IA32_APICBASE
:
529 kvm_set_apic_base(vcpu
, data
);
531 case MSR_IA32_MISC_ENABLE
:
532 vcpu
->arch
.ia32_misc_enable_msr
= data
;
535 pr_unimpl(vcpu
, "unhandled wrmsr: 0x%x data %llx\n", msr
, data
);
540 EXPORT_SYMBOL_GPL(kvm_set_msr_common
);
544 * Reads an msr value (of 'msr_index') into 'pdata'.
545 * Returns 0 on success, non-0 otherwise.
546 * Assumes vcpu_load() was already called.
548 int kvm_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
550 return kvm_x86_ops
->get_msr(vcpu
, msr_index
, pdata
);
553 int kvm_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
558 case 0xc0010010: /* SYSCFG */
559 case 0xc0010015: /* HWCR */
560 case MSR_IA32_PLATFORM_ID
:
561 case MSR_IA32_P5_MC_ADDR
:
562 case MSR_IA32_P5_MC_TYPE
:
563 case MSR_IA32_MC0_CTL
:
564 case MSR_IA32_MCG_STATUS
:
565 case MSR_IA32_MCG_CAP
:
566 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
568 case MSR_IA32_MCG_CTL
:
569 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
570 case MSR_IA32_MC0_MISC
:
571 case MSR_IA32_MC0_MISC
+4:
572 case MSR_IA32_MC0_MISC
+8:
573 case MSR_IA32_MC0_MISC
+12:
574 case MSR_IA32_MC0_MISC
+16:
575 case MSR_IA32_UCODE_REV
:
576 case MSR_IA32_PERF_STATUS
:
577 case MSR_IA32_EBL_CR_POWERON
:
580 case 0x200 ... 0x2ff:
583 case 0xcd: /* fsb frequency */
586 case MSR_IA32_APICBASE
:
587 data
= kvm_get_apic_base(vcpu
);
589 case MSR_IA32_MISC_ENABLE
:
590 data
= vcpu
->arch
.ia32_misc_enable_msr
;
594 data
= vcpu
->arch
.shadow_efer
;
598 pr_unimpl(vcpu
, "unhandled rdmsr: 0x%x\n", msr
);
604 EXPORT_SYMBOL_GPL(kvm_get_msr_common
);
607 * Read or write a bunch of msrs. All parameters are kernel addresses.
609 * @return number of msrs set successfully.
611 static int __msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs
*msrs
,
612 struct kvm_msr_entry
*entries
,
613 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
614 unsigned index
, u64
*data
))
620 for (i
= 0; i
< msrs
->nmsrs
; ++i
)
621 if (do_msr(vcpu
, entries
[i
].index
, &entries
[i
].data
))
630 * Read or write a bunch of msrs. Parameters are user addresses.
632 * @return number of msrs set successfully.
634 static int msr_io(struct kvm_vcpu
*vcpu
, struct kvm_msrs __user
*user_msrs
,
635 int (*do_msr
)(struct kvm_vcpu
*vcpu
,
636 unsigned index
, u64
*data
),
639 struct kvm_msrs msrs
;
640 struct kvm_msr_entry
*entries
;
645 if (copy_from_user(&msrs
, user_msrs
, sizeof msrs
))
649 if (msrs
.nmsrs
>= MAX_IO_MSRS
)
653 size
= sizeof(struct kvm_msr_entry
) * msrs
.nmsrs
;
654 entries
= vmalloc(size
);
659 if (copy_from_user(entries
, user_msrs
->entries
, size
))
662 r
= n
= __msr_io(vcpu
, &msrs
, entries
, do_msr
);
667 if (writeback
&& copy_to_user(user_msrs
->entries
, entries
, size
))
679 * Make sure that a cpu that is being hot-unplugged does not have any vcpus
682 void decache_vcpus_on_cpu(int cpu
)
685 struct kvm_vcpu
*vcpu
;
688 spin_lock(&kvm_lock
);
689 list_for_each_entry(vm
, &vm_list
, vm_list
)
690 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
695 * If the vcpu is locked, then it is running on some
696 * other cpu and therefore it is not cached on the
699 * If it's not locked, check the last cpu it executed
702 if (mutex_trylock(&vcpu
->mutex
)) {
703 if (vcpu
->cpu
== cpu
) {
704 kvm_x86_ops
->vcpu_decache(vcpu
);
707 mutex_unlock(&vcpu
->mutex
);
710 spin_unlock(&kvm_lock
);
713 int kvm_dev_ioctl_check_extension(long ext
)
718 case KVM_CAP_IRQCHIP
:
720 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL
:
721 case KVM_CAP_USER_MEMORY
:
722 case KVM_CAP_SET_TSS_ADDR
:
723 case KVM_CAP_EXT_CPUID
:
727 r
= !kvm_x86_ops
->cpu_has_accelerated_tpr();
737 long kvm_arch_dev_ioctl(struct file
*filp
,
738 unsigned int ioctl
, unsigned long arg
)
740 void __user
*argp
= (void __user
*)arg
;
744 case KVM_GET_MSR_INDEX_LIST
: {
745 struct kvm_msr_list __user
*user_msr_list
= argp
;
746 struct kvm_msr_list msr_list
;
750 if (copy_from_user(&msr_list
, user_msr_list
, sizeof msr_list
))
753 msr_list
.nmsrs
= num_msrs_to_save
+ ARRAY_SIZE(emulated_msrs
);
754 if (copy_to_user(user_msr_list
, &msr_list
, sizeof msr_list
))
757 if (n
< num_msrs_to_save
)
760 if (copy_to_user(user_msr_list
->indices
, &msrs_to_save
,
761 num_msrs_to_save
* sizeof(u32
)))
763 if (copy_to_user(user_msr_list
->indices
764 + num_msrs_to_save
* sizeof(u32
),
766 ARRAY_SIZE(emulated_msrs
) * sizeof(u32
)))
771 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
773 case KVM_GET_SUPPORTED_CPUID
: {
774 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
775 struct kvm_cpuid2 cpuid
;
778 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
780 r
= kvm_dev_ioctl_get_supported_cpuid(&cpuid
,
786 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
791 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
799 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
801 kvm_x86_ops
->vcpu_load(vcpu
, cpu
);
804 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
806 kvm_x86_ops
->vcpu_put(vcpu
);
807 kvm_put_guest_fpu(vcpu
);
810 static int is_efer_nx(void)
814 rdmsrl(MSR_EFER
, efer
);
815 return efer
& EFER_NX
;
818 static void cpuid_fix_nx_cap(struct kvm_vcpu
*vcpu
)
821 struct kvm_cpuid_entry2
*e
, *entry
;
824 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
825 e
= &vcpu
->arch
.cpuid_entries
[i
];
826 if (e
->function
== 0x80000001) {
831 if (entry
&& (entry
->edx
& (1 << 20)) && !is_efer_nx()) {
832 entry
->edx
&= ~(1 << 20);
833 printk(KERN_INFO
"kvm: guest NX capability removed\n");
837 /* when an old userspace process fills a new kernel module */
838 static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu
*vcpu
,
839 struct kvm_cpuid
*cpuid
,
840 struct kvm_cpuid_entry __user
*entries
)
843 struct kvm_cpuid_entry
*cpuid_entries
;
846 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
849 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry
) * cpuid
->nent
);
853 if (copy_from_user(cpuid_entries
, entries
,
854 cpuid
->nent
* sizeof(struct kvm_cpuid_entry
)))
856 for (i
= 0; i
< cpuid
->nent
; i
++) {
857 vcpu
->arch
.cpuid_entries
[i
].function
= cpuid_entries
[i
].function
;
858 vcpu
->arch
.cpuid_entries
[i
].eax
= cpuid_entries
[i
].eax
;
859 vcpu
->arch
.cpuid_entries
[i
].ebx
= cpuid_entries
[i
].ebx
;
860 vcpu
->arch
.cpuid_entries
[i
].ecx
= cpuid_entries
[i
].ecx
;
861 vcpu
->arch
.cpuid_entries
[i
].edx
= cpuid_entries
[i
].edx
;
862 vcpu
->arch
.cpuid_entries
[i
].index
= 0;
863 vcpu
->arch
.cpuid_entries
[i
].flags
= 0;
864 vcpu
->arch
.cpuid_entries
[i
].padding
[0] = 0;
865 vcpu
->arch
.cpuid_entries
[i
].padding
[1] = 0;
866 vcpu
->arch
.cpuid_entries
[i
].padding
[2] = 0;
868 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
869 cpuid_fix_nx_cap(vcpu
);
873 vfree(cpuid_entries
);
878 static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu
*vcpu
,
879 struct kvm_cpuid2
*cpuid
,
880 struct kvm_cpuid_entry2 __user
*entries
)
885 if (cpuid
->nent
> KVM_MAX_CPUID_ENTRIES
)
888 if (copy_from_user(&vcpu
->arch
.cpuid_entries
, entries
,
889 cpuid
->nent
* sizeof(struct kvm_cpuid_entry2
)))
891 vcpu
->arch
.cpuid_nent
= cpuid
->nent
;
898 static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu
*vcpu
,
899 struct kvm_cpuid2
*cpuid
,
900 struct kvm_cpuid_entry2 __user
*entries
)
905 if (cpuid
->nent
< vcpu
->arch
.cpuid_nent
)
908 if (copy_to_user(entries
, &vcpu
->arch
.cpuid_entries
,
909 vcpu
->arch
.cpuid_nent
* sizeof(struct kvm_cpuid_entry2
)))
914 cpuid
->nent
= vcpu
->arch
.cpuid_nent
;
918 static inline u32
bit(int bitno
)
920 return 1 << (bitno
& 31);
923 static void do_cpuid_1_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
926 entry
->function
= function
;
927 entry
->index
= index
;
928 cpuid_count(entry
->function
, entry
->index
,
929 &entry
->eax
, &entry
->ebx
, &entry
->ecx
, &entry
->edx
);
933 static void do_cpuid_ent(struct kvm_cpuid_entry2
*entry
, u32 function
,
934 u32 index
, int *nent
, int maxnent
)
936 const u32 kvm_supported_word0_x86_features
= bit(X86_FEATURE_FPU
) |
937 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
938 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
939 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
940 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
941 bit(X86_FEATURE_SEP
) | bit(X86_FEATURE_PGE
) |
942 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
943 bit(X86_FEATURE_CLFLSH
) | bit(X86_FEATURE_MMX
) |
944 bit(X86_FEATURE_FXSR
) | bit(X86_FEATURE_XMM
) |
945 bit(X86_FEATURE_XMM2
) | bit(X86_FEATURE_SELFSNOOP
);
946 const u32 kvm_supported_word1_x86_features
= bit(X86_FEATURE_FPU
) |
947 bit(X86_FEATURE_VME
) | bit(X86_FEATURE_DE
) |
948 bit(X86_FEATURE_PSE
) | bit(X86_FEATURE_TSC
) |
949 bit(X86_FEATURE_MSR
) | bit(X86_FEATURE_PAE
) |
950 bit(X86_FEATURE_CX8
) | bit(X86_FEATURE_APIC
) |
951 bit(X86_FEATURE_PGE
) |
952 bit(X86_FEATURE_CMOV
) | bit(X86_FEATURE_PSE36
) |
953 bit(X86_FEATURE_MMX
) | bit(X86_FEATURE_FXSR
) |
954 bit(X86_FEATURE_SYSCALL
) |
955 (bit(X86_FEATURE_NX
) && is_efer_nx()) |
957 bit(X86_FEATURE_LM
) |
959 bit(X86_FEATURE_MMXEXT
) |
960 bit(X86_FEATURE_3DNOWEXT
) |
961 bit(X86_FEATURE_3DNOW
);
962 const u32 kvm_supported_word3_x86_features
=
963 bit(X86_FEATURE_XMM3
) | bit(X86_FEATURE_CX16
);
964 const u32 kvm_supported_word6_x86_features
=
965 bit(X86_FEATURE_LAHF_LM
) | bit(X86_FEATURE_CMP_LEGACY
);
967 /* all func 2 cpuid_count() should be called on the same cpu */
969 do_cpuid_1_ent(entry
, function
, index
);
974 entry
->eax
= min(entry
->eax
, (u32
)0xb);
977 entry
->edx
&= kvm_supported_word0_x86_features
;
978 entry
->ecx
&= kvm_supported_word3_x86_features
;
980 /* function 2 entries are STATEFUL. That is, repeated cpuid commands
981 * may return different values. This forces us to get_cpu() before
982 * issuing the first command, and also to emulate this annoying behavior
983 * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
985 int t
, times
= entry
->eax
& 0xff;
987 entry
->flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
988 for (t
= 1; t
< times
&& *nent
< maxnent
; ++t
) {
989 do_cpuid_1_ent(&entry
[t
], function
, 0);
990 entry
[t
].flags
|= KVM_CPUID_FLAG_STATEFUL_FUNC
;
995 /* function 4 and 0xb have additional index. */
997 int index
, cache_type
;
999 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1000 /* read more entries until cache_type is zero */
1001 for (index
= 1; *nent
< maxnent
; ++index
) {
1002 cache_type
= entry
[index
- 1].eax
& 0x1f;
1005 do_cpuid_1_ent(&entry
[index
], function
, index
);
1006 entry
[index
].flags
|=
1007 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1013 int index
, level_type
;
1015 entry
->flags
|= KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1016 /* read more entries until level_type is zero */
1017 for (index
= 1; *nent
< maxnent
; ++index
) {
1018 level_type
= entry
[index
- 1].ecx
& 0xff;
1021 do_cpuid_1_ent(&entry
[index
], function
, index
);
1022 entry
[index
].flags
|=
1023 KVM_CPUID_FLAG_SIGNIFCANT_INDEX
;
1029 entry
->eax
= min(entry
->eax
, 0x8000001a);
1032 entry
->edx
&= kvm_supported_word1_x86_features
;
1033 entry
->ecx
&= kvm_supported_word6_x86_features
;
1039 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1040 static int kvm_vm_ioctl_get_supported_cpuid(struct kvm
*kvm
,
1041 struct kvm_cpuid2
*cpuid
,
1043 static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2
*cpuid
,
1044 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1045 struct kvm_cpuid_entry2 __user
*entries
)
1047 struct kvm_cpuid_entry2
*cpuid_entries
;
1048 int limit
, nent
= 0, r
= -E2BIG
;
1051 if (cpuid
->nent
< 1)
1054 cpuid_entries
= vmalloc(sizeof(struct kvm_cpuid_entry2
) * cpuid
->nent
);
1058 do_cpuid_ent(&cpuid_entries
[0], 0, 0, &nent
, cpuid
->nent
);
1059 limit
= cpuid_entries
[0].eax
;
1060 for (func
= 1; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1061 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1062 &nent
, cpuid
->nent
);
1064 if (nent
>= cpuid
->nent
)
1067 do_cpuid_ent(&cpuid_entries
[nent
], 0x80000000, 0, &nent
, cpuid
->nent
);
1068 limit
= cpuid_entries
[nent
- 1].eax
;
1069 for (func
= 0x80000001; func
<= limit
&& nent
< cpuid
->nent
; ++func
)
1070 do_cpuid_ent(&cpuid_entries
[nent
], func
, 0,
1071 &nent
, cpuid
->nent
);
1073 if (copy_to_user(entries
, cpuid_entries
,
1074 nent
* sizeof(struct kvm_cpuid_entry2
)))
1080 vfree(cpuid_entries
);
1085 static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu
*vcpu
,
1086 struct kvm_lapic_state
*s
)
1089 memcpy(s
->regs
, vcpu
->arch
.apic
->regs
, sizeof *s
);
1095 static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu
*vcpu
,
1096 struct kvm_lapic_state
*s
)
1099 memcpy(vcpu
->arch
.apic
->regs
, s
->regs
, sizeof *s
);
1100 kvm_apic_post_state_restore(vcpu
);
1106 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
,
1107 struct kvm_interrupt
*irq
)
1109 if (irq
->irq
< 0 || irq
->irq
>= 256)
1111 if (irqchip_in_kernel(vcpu
->kvm
))
1115 set_bit(irq
->irq
, vcpu
->arch
.irq_pending
);
1116 set_bit(irq
->irq
/ BITS_PER_LONG
, &vcpu
->arch
.irq_summary
);
1123 static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu
*vcpu
,
1124 struct kvm_tpr_access_ctl
*tac
)
1128 vcpu
->arch
.tpr_access_reporting
= !!tac
->enabled
;
1132 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1133 unsigned int ioctl
, unsigned long arg
)
1135 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1136 void __user
*argp
= (void __user
*)arg
;
1140 case KVM_GET_LAPIC
: {
1141 struct kvm_lapic_state lapic
;
1143 memset(&lapic
, 0, sizeof lapic
);
1144 r
= kvm_vcpu_ioctl_get_lapic(vcpu
, &lapic
);
1148 if (copy_to_user(argp
, &lapic
, sizeof lapic
))
1153 case KVM_SET_LAPIC
: {
1154 struct kvm_lapic_state lapic
;
1157 if (copy_from_user(&lapic
, argp
, sizeof lapic
))
1159 r
= kvm_vcpu_ioctl_set_lapic(vcpu
, &lapic
);;
1165 case KVM_INTERRUPT
: {
1166 struct kvm_interrupt irq
;
1169 if (copy_from_user(&irq
, argp
, sizeof irq
))
1171 r
= kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1177 case KVM_SET_CPUID
: {
1178 struct kvm_cpuid __user
*cpuid_arg
= argp
;
1179 struct kvm_cpuid cpuid
;
1182 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1184 r
= kvm_vcpu_ioctl_set_cpuid(vcpu
, &cpuid
, cpuid_arg
->entries
);
1189 case KVM_SET_CPUID2
: {
1190 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1191 struct kvm_cpuid2 cpuid
;
1194 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1196 r
= kvm_vcpu_ioctl_set_cpuid2(vcpu
, &cpuid
,
1197 cpuid_arg
->entries
);
1202 case KVM_GET_CPUID2
: {
1203 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1204 struct kvm_cpuid2 cpuid
;
1207 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1209 r
= kvm_vcpu_ioctl_get_cpuid2(vcpu
, &cpuid
,
1210 cpuid_arg
->entries
);
1214 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1220 r
= msr_io(vcpu
, argp
, kvm_get_msr
, 1);
1223 r
= msr_io(vcpu
, argp
, do_set_msr
, 0);
1225 case KVM_TPR_ACCESS_REPORTING
: {
1226 struct kvm_tpr_access_ctl tac
;
1229 if (copy_from_user(&tac
, argp
, sizeof tac
))
1231 r
= vcpu_ioctl_tpr_access_reporting(vcpu
, &tac
);
1235 if (copy_to_user(argp
, &tac
, sizeof tac
))
1240 case KVM_SET_VAPIC_ADDR
: {
1241 struct kvm_vapic_addr va
;
1244 if (!irqchip_in_kernel(vcpu
->kvm
))
1247 if (copy_from_user(&va
, argp
, sizeof va
))
1250 kvm_lapic_set_vapic_addr(vcpu
, va
.vapic_addr
);
1260 static int kvm_vm_ioctl_set_tss_addr(struct kvm
*kvm
, unsigned long addr
)
1264 if (addr
> (unsigned int)(-3 * PAGE_SIZE
))
1266 ret
= kvm_x86_ops
->set_tss_addr(kvm
, addr
);
1270 static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm
*kvm
,
1271 u32 kvm_nr_mmu_pages
)
1273 if (kvm_nr_mmu_pages
< KVM_MIN_ALLOC_MMU_PAGES
)
1276 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1277 down_write(¤t
->mm
->mmap_sem
);
1279 down_write(&kvm
->slots_lock
);
1280 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1282 kvm_mmu_change_mmu_pages(kvm
, kvm_nr_mmu_pages
);
1283 kvm
->arch
.n_requested_mmu_pages
= kvm_nr_mmu_pages
;
1285 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1286 up_write(¤t
->mm
->mmap_sem
);
1288 up_write(&kvm
->slots_lock
);
1289 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1293 static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm
*kvm
)
1295 return kvm
->arch
.n_alloc_mmu_pages
;
1298 gfn_t
unalias_gfn(struct kvm
*kvm
, gfn_t gfn
)
1301 struct kvm_mem_alias
*alias
;
1303 for (i
= 0; i
< kvm
->arch
.naliases
; ++i
) {
1304 alias
= &kvm
->arch
.aliases
[i
];
1305 if (gfn
>= alias
->base_gfn
1306 && gfn
< alias
->base_gfn
+ alias
->npages
)
1307 return alias
->target_gfn
+ gfn
- alias
->base_gfn
;
1313 * Set a new alias region. Aliases map a portion of physical memory into
1314 * another portion. This is useful for memory windows, for example the PC
1317 static int kvm_vm_ioctl_set_memory_alias(struct kvm
*kvm
,
1318 struct kvm_memory_alias
*alias
)
1321 struct kvm_mem_alias
*p
;
1324 /* General sanity checks */
1325 if (alias
->memory_size
& (PAGE_SIZE
- 1))
1327 if (alias
->guest_phys_addr
& (PAGE_SIZE
- 1))
1329 if (alias
->slot
>= KVM_ALIAS_SLOTS
)
1331 if (alias
->guest_phys_addr
+ alias
->memory_size
1332 < alias
->guest_phys_addr
)
1334 if (alias
->target_phys_addr
+ alias
->memory_size
1335 < alias
->target_phys_addr
)
1338 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1339 down_write(¤t
->mm
->mmap_sem
);
1341 down_write(&kvm
->slots_lock
);
1342 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1344 p
= &kvm
->arch
.aliases
[alias
->slot
];
1345 p
->base_gfn
= alias
->guest_phys_addr
>> PAGE_SHIFT
;
1346 p
->npages
= alias
->memory_size
>> PAGE_SHIFT
;
1347 p
->target_gfn
= alias
->target_phys_addr
>> PAGE_SHIFT
;
1349 for (n
= KVM_ALIAS_SLOTS
; n
> 0; --n
)
1350 if (kvm
->arch
.aliases
[n
- 1].npages
)
1352 kvm
->arch
.naliases
= n
;
1354 kvm_mmu_zap_all(kvm
);
1356 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1357 up_write(¤t
->mm
->mmap_sem
);
1359 up_write(&kvm
->slots_lock
);
1360 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1368 static int kvm_vm_ioctl_get_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1373 switch (chip
->chip_id
) {
1374 case KVM_IRQCHIP_PIC_MASTER
:
1375 memcpy(&chip
->chip
.pic
,
1376 &pic_irqchip(kvm
)->pics
[0],
1377 sizeof(struct kvm_pic_state
));
1379 case KVM_IRQCHIP_PIC_SLAVE
:
1380 memcpy(&chip
->chip
.pic
,
1381 &pic_irqchip(kvm
)->pics
[1],
1382 sizeof(struct kvm_pic_state
));
1384 case KVM_IRQCHIP_IOAPIC
:
1385 memcpy(&chip
->chip
.ioapic
,
1386 ioapic_irqchip(kvm
),
1387 sizeof(struct kvm_ioapic_state
));
1396 static int kvm_vm_ioctl_set_irqchip(struct kvm
*kvm
, struct kvm_irqchip
*chip
)
1401 switch (chip
->chip_id
) {
1402 case KVM_IRQCHIP_PIC_MASTER
:
1403 memcpy(&pic_irqchip(kvm
)->pics
[0],
1405 sizeof(struct kvm_pic_state
));
1407 case KVM_IRQCHIP_PIC_SLAVE
:
1408 memcpy(&pic_irqchip(kvm
)->pics
[1],
1410 sizeof(struct kvm_pic_state
));
1412 case KVM_IRQCHIP_IOAPIC
:
1413 memcpy(ioapic_irqchip(kvm
),
1415 sizeof(struct kvm_ioapic_state
));
1421 kvm_pic_update_irq(pic_irqchip(kvm
));
1426 * Get (and clear) the dirty memory log for a memory slot.
1428 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
1429 struct kvm_dirty_log
*log
)
1433 struct kvm_memory_slot
*memslot
;
1436 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1437 down_write(¤t
->mm
->mmap_sem
);
1439 down_write(&kvm
->slots_lock
);
1440 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1442 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
1446 /* If nothing is dirty, don't bother messing with page tables. */
1448 kvm_mmu_slot_remove_write_access(kvm
, log
->slot
);
1449 kvm_flush_remote_tlbs(kvm
);
1450 memslot
= &kvm
->memslots
[log
->slot
];
1451 n
= ALIGN(memslot
->npages
, BITS_PER_LONG
) / 8;
1452 memset(memslot
->dirty_bitmap
, 0, n
);
1456 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1457 up_write(¤t
->mm
->mmap_sem
);
1459 up_write(&kvm
->slots_lock
);
1460 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1464 long kvm_arch_vm_ioctl(struct file
*filp
,
1465 unsigned int ioctl
, unsigned long arg
)
1467 struct kvm
*kvm
= filp
->private_data
;
1468 void __user
*argp
= (void __user
*)arg
;
1472 case KVM_SET_TSS_ADDR
:
1473 r
= kvm_vm_ioctl_set_tss_addr(kvm
, arg
);
1477 case KVM_SET_MEMORY_REGION
: {
1478 struct kvm_memory_region kvm_mem
;
1479 struct kvm_userspace_memory_region kvm_userspace_mem
;
1482 if (copy_from_user(&kvm_mem
, argp
, sizeof kvm_mem
))
1484 kvm_userspace_mem
.slot
= kvm_mem
.slot
;
1485 kvm_userspace_mem
.flags
= kvm_mem
.flags
;
1486 kvm_userspace_mem
.guest_phys_addr
= kvm_mem
.guest_phys_addr
;
1487 kvm_userspace_mem
.memory_size
= kvm_mem
.memory_size
;
1488 r
= kvm_vm_ioctl_set_memory_region(kvm
, &kvm_userspace_mem
, 0);
1493 case KVM_SET_NR_MMU_PAGES
:
1494 r
= kvm_vm_ioctl_set_nr_mmu_pages(kvm
, arg
);
1498 case KVM_GET_NR_MMU_PAGES
:
1499 r
= kvm_vm_ioctl_get_nr_mmu_pages(kvm
);
1501 case KVM_SET_MEMORY_ALIAS
: {
1502 struct kvm_memory_alias alias
;
1505 if (copy_from_user(&alias
, argp
, sizeof alias
))
1507 r
= kvm_vm_ioctl_set_memory_alias(kvm
, &alias
);
1512 case KVM_CREATE_IRQCHIP
:
1514 kvm
->arch
.vpic
= kvm_create_pic(kvm
);
1515 if (kvm
->arch
.vpic
) {
1516 r
= kvm_ioapic_init(kvm
);
1518 kfree(kvm
->arch
.vpic
);
1519 kvm
->arch
.vpic
= NULL
;
1525 case KVM_IRQ_LINE
: {
1526 struct kvm_irq_level irq_event
;
1529 if (copy_from_user(&irq_event
, argp
, sizeof irq_event
))
1531 if (irqchip_in_kernel(kvm
)) {
1532 mutex_lock(&kvm
->lock
);
1533 if (irq_event
.irq
< 16)
1534 kvm_pic_set_irq(pic_irqchip(kvm
),
1537 kvm_ioapic_set_irq(kvm
->arch
.vioapic
,
1540 mutex_unlock(&kvm
->lock
);
1545 case KVM_GET_IRQCHIP
: {
1546 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1547 struct kvm_irqchip chip
;
1550 if (copy_from_user(&chip
, argp
, sizeof chip
))
1553 if (!irqchip_in_kernel(kvm
))
1555 r
= kvm_vm_ioctl_get_irqchip(kvm
, &chip
);
1559 if (copy_to_user(argp
, &chip
, sizeof chip
))
1564 case KVM_SET_IRQCHIP
: {
1565 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1566 struct kvm_irqchip chip
;
1569 if (copy_from_user(&chip
, argp
, sizeof chip
))
1572 if (!irqchip_in_kernel(kvm
))
1574 r
= kvm_vm_ioctl_set_irqchip(kvm
, &chip
);
1580 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1581 case KVM_GET_SUPPORTED_CPUID
: {
1582 struct kvm_cpuid2 __user
*cpuid_arg
= argp
;
1583 struct kvm_cpuid2 cpuid
;
1586 if (copy_from_user(&cpuid
, cpuid_arg
, sizeof cpuid
))
1588 r
= kvm_vm_ioctl_get_supported_cpuid(kvm
, &cpuid
,
1589 cpuid_arg
->entries
);
1594 if (copy_to_user(cpuid_arg
, &cpuid
, sizeof cpuid
))
1600 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1608 static void kvm_init_msr_list(void)
1613 for (i
= j
= 0; i
< ARRAY_SIZE(msrs_to_save
); i
++) {
1614 if (rdmsr_safe(msrs_to_save
[i
], &dummy
[0], &dummy
[1]) < 0)
1617 msrs_to_save
[j
] = msrs_to_save
[i
];
1620 num_msrs_to_save
= j
;
1624 * Only apic need an MMIO device hook, so shortcut now..
1626 static struct kvm_io_device
*vcpu_find_pervcpu_dev(struct kvm_vcpu
*vcpu
,
1629 struct kvm_io_device
*dev
;
1631 if (vcpu
->arch
.apic
) {
1632 dev
= &vcpu
->arch
.apic
->dev
;
1633 if (dev
->in_range(dev
, addr
))
1640 static struct kvm_io_device
*vcpu_find_mmio_dev(struct kvm_vcpu
*vcpu
,
1643 struct kvm_io_device
*dev
;
1645 dev
= vcpu_find_pervcpu_dev(vcpu
, addr
);
1647 dev
= kvm_io_bus_find_dev(&vcpu
->kvm
->mmio_bus
, addr
);
1651 int emulator_read_std(unsigned long addr
,
1654 struct kvm_vcpu
*vcpu
)
1657 int r
= X86EMUL_CONTINUE
;
1659 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1660 down_read(¤t
->mm
->mmap_sem
);
1662 down_read(&vcpu
->kvm
->slots_lock
);
1663 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1665 gpa_t gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1666 unsigned offset
= addr
& (PAGE_SIZE
-1);
1667 unsigned tocopy
= min(bytes
, (unsigned)PAGE_SIZE
- offset
);
1670 if (gpa
== UNMAPPED_GVA
) {
1671 r
= X86EMUL_PROPAGATE_FAULT
;
1674 ret
= kvm_read_guest(vcpu
->kvm
, gpa
, data
, tocopy
);
1676 r
= X86EMUL_UNHANDLEABLE
;
1685 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1686 up_read(¤t
->mm
->mmap_sem
);
1688 up_read(&vcpu
->kvm
->slots_lock
);
1689 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1692 EXPORT_SYMBOL_GPL(emulator_read_std
);
1694 static int emulator_read_emulated(unsigned long addr
,
1697 struct kvm_vcpu
*vcpu
)
1699 struct kvm_io_device
*mmio_dev
;
1702 if (vcpu
->mmio_read_completed
) {
1703 memcpy(val
, vcpu
->mmio_data
, bytes
);
1704 vcpu
->mmio_read_completed
= 0;
1705 return X86EMUL_CONTINUE
;
1708 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1709 down_read(¤t
->mm
->mmap_sem
);
1711 down_read(&vcpu
->kvm
->slots_lock
);
1712 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1713 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1714 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1715 up_read(¤t
->mm
->mmap_sem
);
1717 up_read(&vcpu
->kvm
->slots_lock
);
1718 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1720 /* For APIC access vmexit */
1721 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1724 if (emulator_read_std(addr
, val
, bytes
, vcpu
)
1725 == X86EMUL_CONTINUE
)
1726 return X86EMUL_CONTINUE
;
1727 if (gpa
== UNMAPPED_GVA
)
1728 return X86EMUL_PROPAGATE_FAULT
;
1732 * Is this MMIO handled locally?
1734 mutex_lock(&vcpu
->kvm
->lock
);
1735 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1737 kvm_iodevice_read(mmio_dev
, gpa
, bytes
, val
);
1738 mutex_unlock(&vcpu
->kvm
->lock
);
1739 return X86EMUL_CONTINUE
;
1741 mutex_unlock(&vcpu
->kvm
->lock
);
1743 vcpu
->mmio_needed
= 1;
1744 vcpu
->mmio_phys_addr
= gpa
;
1745 vcpu
->mmio_size
= bytes
;
1746 vcpu
->mmio_is_write
= 0;
1748 return X86EMUL_UNHANDLEABLE
;
1751 static int emulator_write_phys(struct kvm_vcpu
*vcpu
, gpa_t gpa
,
1752 const void *val
, int bytes
)
1756 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1757 down_read(¤t
->mm
->mmap_sem
);
1759 down_read(&vcpu
->kvm
->slots_lock
);
1760 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1761 ret
= kvm_write_guest(vcpu
->kvm
, gpa
, val
, bytes
);
1763 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1764 up_read(¤t
->mm
->mmap_sem
);
1766 up_read(&vcpu
->kvm
->slots_lock
);
1767 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1770 kvm_mmu_pte_write(vcpu
, gpa
, val
, bytes
);
1771 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1772 up_read(¤t
->mm
->mmap_sem
);
1774 up_read(&vcpu
->kvm
->slots_lock
);
1775 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1779 static int emulator_write_emulated_onepage(unsigned long addr
,
1782 struct kvm_vcpu
*vcpu
)
1784 struct kvm_io_device
*mmio_dev
;
1787 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1788 down_read(¤t
->mm
->mmap_sem
);
1790 down_read(&vcpu
->kvm
->slots_lock
);
1791 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1792 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1793 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1794 up_read(¤t
->mm
->mmap_sem
);
1796 up_read(&vcpu
->kvm
->slots_lock
);
1797 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1799 if (gpa
== UNMAPPED_GVA
) {
1800 kvm_inject_page_fault(vcpu
, addr
, 2);
1801 return X86EMUL_PROPAGATE_FAULT
;
1804 /* For APIC access vmexit */
1805 if ((gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1808 if (emulator_write_phys(vcpu
, gpa
, val
, bytes
))
1809 return X86EMUL_CONTINUE
;
1813 * Is this MMIO handled locally?
1815 mutex_lock(&vcpu
->kvm
->lock
);
1816 mmio_dev
= vcpu_find_mmio_dev(vcpu
, gpa
);
1818 kvm_iodevice_write(mmio_dev
, gpa
, bytes
, val
);
1819 mutex_unlock(&vcpu
->kvm
->lock
);
1820 return X86EMUL_CONTINUE
;
1822 mutex_unlock(&vcpu
->kvm
->lock
);
1824 vcpu
->mmio_needed
= 1;
1825 vcpu
->mmio_phys_addr
= gpa
;
1826 vcpu
->mmio_size
= bytes
;
1827 vcpu
->mmio_is_write
= 1;
1828 memcpy(vcpu
->mmio_data
, val
, bytes
);
1830 return X86EMUL_CONTINUE
;
1833 int emulator_write_emulated(unsigned long addr
,
1836 struct kvm_vcpu
*vcpu
)
1838 /* Crossing a page boundary? */
1839 if (((addr
+ bytes
- 1) ^ addr
) & PAGE_MASK
) {
1842 now
= -addr
& ~PAGE_MASK
;
1843 rc
= emulator_write_emulated_onepage(addr
, val
, now
, vcpu
);
1844 if (rc
!= X86EMUL_CONTINUE
)
1850 return emulator_write_emulated_onepage(addr
, val
, bytes
, vcpu
);
1852 EXPORT_SYMBOL_GPL(emulator_write_emulated
);
1854 static int emulator_cmpxchg_emulated(unsigned long addr
,
1858 struct kvm_vcpu
*vcpu
)
1860 static int reported
;
1864 printk(KERN_WARNING
"kvm: emulating exchange as write\n");
1866 #ifndef CONFIG_X86_64
1867 /* guests cmpxchg8b have to be emulated atomically */
1874 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1875 down_read(¤t
->mm
->mmap_sem
);
1877 down_read(&vcpu
->kvm
->slots_lock
);
1878 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1879 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, addr
);
1881 if (gpa
== UNMAPPED_GVA
||
1882 (gpa
& PAGE_MASK
) == APIC_DEFAULT_PHYS_BASE
)
1885 if (((gpa
+ bytes
- 1) & PAGE_MASK
) != (gpa
& PAGE_MASK
))
1889 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1892 down_read(¤t
->mm
->mmap_sem
);
1893 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1894 page
= gfn_to_page(vcpu
->kvm
, gpa
>> PAGE_SHIFT
);
1895 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1897 up_read(¤t
->mm
->mmap_sem
);
1899 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1900 kaddr
= kmap_atomic(page
, KM_USER0
);
1901 set_64bit((u64
*)(kaddr
+ offset_in_page(gpa
)), val
);
1902 kunmap_atomic(kaddr
, KM_USER0
);
1903 kvm_release_page_dirty(page
);
1905 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
1906 up_read(¤t
->mm
->mmap_sem
);
1908 up_read(&vcpu
->kvm
->slots_lock
);
1909 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
1913 return emulator_write_emulated(addr
, new, bytes
, vcpu
);
1916 static unsigned long get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
1918 return kvm_x86_ops
->get_segment_base(vcpu
, seg
);
1921 int emulate_invlpg(struct kvm_vcpu
*vcpu
, gva_t address
)
1923 return X86EMUL_CONTINUE
;
1926 int emulate_clts(struct kvm_vcpu
*vcpu
)
1928 kvm_x86_ops
->set_cr0(vcpu
, vcpu
->arch
.cr0
& ~X86_CR0_TS
);
1929 return X86EMUL_CONTINUE
;
1932 int emulator_get_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long *dest
)
1934 struct kvm_vcpu
*vcpu
= ctxt
->vcpu
;
1938 *dest
= kvm_x86_ops
->get_dr(vcpu
, dr
);
1939 return X86EMUL_CONTINUE
;
1941 pr_unimpl(vcpu
, "%s: unexpected dr %u\n", __FUNCTION__
, dr
);
1942 return X86EMUL_UNHANDLEABLE
;
1946 int emulator_set_dr(struct x86_emulate_ctxt
*ctxt
, int dr
, unsigned long value
)
1948 unsigned long mask
= (ctxt
->mode
== X86EMUL_MODE_PROT64
) ? ~0ULL : ~0U;
1951 kvm_x86_ops
->set_dr(ctxt
->vcpu
, dr
, value
& mask
, &exception
);
1953 /* FIXME: better handling */
1954 return X86EMUL_UNHANDLEABLE
;
1956 return X86EMUL_CONTINUE
;
1959 void kvm_report_emulation_failure(struct kvm_vcpu
*vcpu
, const char *context
)
1961 static int reported
;
1963 unsigned long rip
= vcpu
->arch
.rip
;
1964 unsigned long rip_linear
;
1966 rip_linear
= rip
+ get_segment_base(vcpu
, VCPU_SREG_CS
);
1971 emulator_read_std(rip_linear
, (void *)opcodes
, 4, vcpu
);
1973 printk(KERN_ERR
"emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
1974 context
, rip
, opcodes
[0], opcodes
[1], opcodes
[2], opcodes
[3]);
1977 EXPORT_SYMBOL_GPL(kvm_report_emulation_failure
);
1979 struct x86_emulate_ops emulate_ops
= {
1980 .read_std
= emulator_read_std
,
1981 .read_emulated
= emulator_read_emulated
,
1982 .write_emulated
= emulator_write_emulated
,
1983 .cmpxchg_emulated
= emulator_cmpxchg_emulated
,
1986 int emulate_instruction(struct kvm_vcpu
*vcpu
,
1987 struct kvm_run
*run
,
1993 struct decode_cache
*c
;
1995 vcpu
->arch
.mmio_fault_cr2
= cr2
;
1996 kvm_x86_ops
->cache_regs(vcpu
);
1998 vcpu
->mmio_is_write
= 0;
1999 vcpu
->arch
.pio
.string
= 0;
2001 if (!(emulation_type
& EMULTYPE_NO_DECODE
)) {
2003 kvm_x86_ops
->get_cs_db_l_bits(vcpu
, &cs_db
, &cs_l
);
2005 vcpu
->arch
.emulate_ctxt
.vcpu
= vcpu
;
2006 vcpu
->arch
.emulate_ctxt
.eflags
= kvm_x86_ops
->get_rflags(vcpu
);
2007 vcpu
->arch
.emulate_ctxt
.mode
=
2008 (vcpu
->arch
.emulate_ctxt
.eflags
& X86_EFLAGS_VM
)
2009 ? X86EMUL_MODE_REAL
: cs_l
2010 ? X86EMUL_MODE_PROT64
: cs_db
2011 ? X86EMUL_MODE_PROT32
: X86EMUL_MODE_PROT16
;
2013 if (vcpu
->arch
.emulate_ctxt
.mode
== X86EMUL_MODE_PROT64
) {
2014 vcpu
->arch
.emulate_ctxt
.cs_base
= 0;
2015 vcpu
->arch
.emulate_ctxt
.ds_base
= 0;
2016 vcpu
->arch
.emulate_ctxt
.es_base
= 0;
2017 vcpu
->arch
.emulate_ctxt
.ss_base
= 0;
2019 vcpu
->arch
.emulate_ctxt
.cs_base
=
2020 get_segment_base(vcpu
, VCPU_SREG_CS
);
2021 vcpu
->arch
.emulate_ctxt
.ds_base
=
2022 get_segment_base(vcpu
, VCPU_SREG_DS
);
2023 vcpu
->arch
.emulate_ctxt
.es_base
=
2024 get_segment_base(vcpu
, VCPU_SREG_ES
);
2025 vcpu
->arch
.emulate_ctxt
.ss_base
=
2026 get_segment_base(vcpu
, VCPU_SREG_SS
);
2029 vcpu
->arch
.emulate_ctxt
.gs_base
=
2030 get_segment_base(vcpu
, VCPU_SREG_GS
);
2031 vcpu
->arch
.emulate_ctxt
.fs_base
=
2032 get_segment_base(vcpu
, VCPU_SREG_FS
);
2034 r
= x86_decode_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2036 /* Reject the instructions other than VMCALL/VMMCALL when
2037 * try to emulate invalid opcode */
2038 c
= &vcpu
->arch
.emulate_ctxt
.decode
;
2039 if ((emulation_type
& EMULTYPE_TRAP_UD
) &&
2040 (!(c
->twobyte
&& c
->b
== 0x01 &&
2041 (c
->modrm_reg
== 0 || c
->modrm_reg
== 3) &&
2042 c
->modrm_mod
== 3 && c
->modrm_rm
== 1)))
2043 return EMULATE_FAIL
;
2045 ++vcpu
->stat
.insn_emulation
;
2047 ++vcpu
->stat
.insn_emulation_fail
;
2048 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2049 return EMULATE_DONE
;
2050 return EMULATE_FAIL
;
2054 r
= x86_emulate_insn(&vcpu
->arch
.emulate_ctxt
, &emulate_ops
);
2056 if (vcpu
->arch
.pio
.string
)
2057 return EMULATE_DO_MMIO
;
2059 if ((r
|| vcpu
->mmio_is_write
) && run
) {
2060 run
->exit_reason
= KVM_EXIT_MMIO
;
2061 run
->mmio
.phys_addr
= vcpu
->mmio_phys_addr
;
2062 memcpy(run
->mmio
.data
, vcpu
->mmio_data
, 8);
2063 run
->mmio
.len
= vcpu
->mmio_size
;
2064 run
->mmio
.is_write
= vcpu
->mmio_is_write
;
2068 if (kvm_mmu_unprotect_page_virt(vcpu
, cr2
))
2069 return EMULATE_DONE
;
2070 if (!vcpu
->mmio_needed
) {
2071 kvm_report_emulation_failure(vcpu
, "mmio");
2072 return EMULATE_FAIL
;
2074 return EMULATE_DO_MMIO
;
2077 kvm_x86_ops
->decache_regs(vcpu
);
2078 kvm_x86_ops
->set_rflags(vcpu
, vcpu
->arch
.emulate_ctxt
.eflags
);
2080 if (vcpu
->mmio_is_write
) {
2081 vcpu
->mmio_needed
= 0;
2082 return EMULATE_DO_MMIO
;
2085 return EMULATE_DONE
;
2087 EXPORT_SYMBOL_GPL(emulate_instruction
);
2089 static void free_pio_guest_pages(struct kvm_vcpu
*vcpu
)
2093 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.pio
.guest_pages
); ++i
)
2094 if (vcpu
->arch
.pio
.guest_pages
[i
]) {
2095 kvm_release_page_dirty(vcpu
->arch
.pio
.guest_pages
[i
]);
2096 vcpu
->arch
.pio
.guest_pages
[i
] = NULL
;
2100 static int pio_copy_data(struct kvm_vcpu
*vcpu
)
2102 void *p
= vcpu
->arch
.pio_data
;
2105 int nr_pages
= vcpu
->arch
.pio
.guest_pages
[1] ? 2 : 1;
2107 q
= vmap(vcpu
->arch
.pio
.guest_pages
, nr_pages
, VM_READ
|VM_WRITE
,
2110 free_pio_guest_pages(vcpu
);
2113 q
+= vcpu
->arch
.pio
.guest_page_offset
;
2114 bytes
= vcpu
->arch
.pio
.size
* vcpu
->arch
.pio
.cur_count
;
2115 if (vcpu
->arch
.pio
.in
)
2116 memcpy(q
, p
, bytes
);
2118 memcpy(p
, q
, bytes
);
2119 q
-= vcpu
->arch
.pio
.guest_page_offset
;
2121 free_pio_guest_pages(vcpu
);
2125 int complete_pio(struct kvm_vcpu
*vcpu
)
2127 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2131 kvm_x86_ops
->cache_regs(vcpu
);
2135 memcpy(&vcpu
->arch
.regs
[VCPU_REGS_RAX
], vcpu
->arch
.pio_data
,
2139 r
= pio_copy_data(vcpu
);
2141 kvm_x86_ops
->cache_regs(vcpu
);
2148 delta
*= io
->cur_count
;
2150 * The size of the register should really depend on
2151 * current address size.
2153 vcpu
->arch
.regs
[VCPU_REGS_RCX
] -= delta
;
2159 vcpu
->arch
.regs
[VCPU_REGS_RDI
] += delta
;
2161 vcpu
->arch
.regs
[VCPU_REGS_RSI
] += delta
;
2164 kvm_x86_ops
->decache_regs(vcpu
);
2166 io
->count
-= io
->cur_count
;
2172 static void kernel_pio(struct kvm_io_device
*pio_dev
,
2173 struct kvm_vcpu
*vcpu
,
2176 /* TODO: String I/O for in kernel device */
2178 mutex_lock(&vcpu
->kvm
->lock
);
2179 if (vcpu
->arch
.pio
.in
)
2180 kvm_iodevice_read(pio_dev
, vcpu
->arch
.pio
.port
,
2181 vcpu
->arch
.pio
.size
,
2184 kvm_iodevice_write(pio_dev
, vcpu
->arch
.pio
.port
,
2185 vcpu
->arch
.pio
.size
,
2187 mutex_unlock(&vcpu
->kvm
->lock
);
2190 static void pio_string_write(struct kvm_io_device
*pio_dev
,
2191 struct kvm_vcpu
*vcpu
)
2193 struct kvm_pio_request
*io
= &vcpu
->arch
.pio
;
2194 void *pd
= vcpu
->arch
.pio_data
;
2197 mutex_lock(&vcpu
->kvm
->lock
);
2198 for (i
= 0; i
< io
->cur_count
; i
++) {
2199 kvm_iodevice_write(pio_dev
, io
->port
,
2204 mutex_unlock(&vcpu
->kvm
->lock
);
2207 static struct kvm_io_device
*vcpu_find_pio_dev(struct kvm_vcpu
*vcpu
,
2210 return kvm_io_bus_find_dev(&vcpu
->kvm
->pio_bus
, addr
);
2213 int kvm_emulate_pio(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2214 int size
, unsigned port
)
2216 struct kvm_io_device
*pio_dev
;
2218 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2219 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2220 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2221 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2222 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= 1;
2223 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2224 vcpu
->arch
.pio
.in
= in
;
2225 vcpu
->arch
.pio
.string
= 0;
2226 vcpu
->arch
.pio
.down
= 0;
2227 vcpu
->arch
.pio
.guest_page_offset
= 0;
2228 vcpu
->arch
.pio
.rep
= 0;
2230 kvm_x86_ops
->cache_regs(vcpu
);
2231 memcpy(vcpu
->arch
.pio_data
, &vcpu
->arch
.regs
[VCPU_REGS_RAX
], 4);
2232 kvm_x86_ops
->decache_regs(vcpu
);
2234 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2236 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2238 kernel_pio(pio_dev
, vcpu
, vcpu
->arch
.pio_data
);
2244 EXPORT_SYMBOL_GPL(kvm_emulate_pio
);
2246 int kvm_emulate_pio_string(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
, int in
,
2247 int size
, unsigned long count
, int down
,
2248 gva_t address
, int rep
, unsigned port
)
2250 unsigned now
, in_page
;
2254 struct kvm_io_device
*pio_dev
;
2256 vcpu
->run
->exit_reason
= KVM_EXIT_IO
;
2257 vcpu
->run
->io
.direction
= in
? KVM_EXIT_IO_IN
: KVM_EXIT_IO_OUT
;
2258 vcpu
->run
->io
.size
= vcpu
->arch
.pio
.size
= size
;
2259 vcpu
->run
->io
.data_offset
= KVM_PIO_PAGE_OFFSET
* PAGE_SIZE
;
2260 vcpu
->run
->io
.count
= vcpu
->arch
.pio
.count
= vcpu
->arch
.pio
.cur_count
= count
;
2261 vcpu
->run
->io
.port
= vcpu
->arch
.pio
.port
= port
;
2262 vcpu
->arch
.pio
.in
= in
;
2263 vcpu
->arch
.pio
.string
= 1;
2264 vcpu
->arch
.pio
.down
= down
;
2265 vcpu
->arch
.pio
.guest_page_offset
= offset_in_page(address
);
2266 vcpu
->arch
.pio
.rep
= rep
;
2269 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2274 in_page
= PAGE_SIZE
- offset_in_page(address
);
2276 in_page
= offset_in_page(address
) + size
;
2277 now
= min(count
, (unsigned long)in_page
/ size
);
2280 * String I/O straddles page boundary. Pin two guest pages
2281 * so that we satisfy atomicity constraints. Do just one
2282 * transaction to avoid complexity.
2289 * String I/O in reverse. Yuck. Kill the guest, fix later.
2291 pr_unimpl(vcpu
, "guest string pio down\n");
2292 kvm_inject_gp(vcpu
, 0);
2295 vcpu
->run
->io
.count
= now
;
2296 vcpu
->arch
.pio
.cur_count
= now
;
2298 if (vcpu
->arch
.pio
.cur_count
== vcpu
->arch
.pio
.count
)
2299 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2301 for (i
= 0; i
< nr_pages
; ++i
) {
2302 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
2303 down_read(¤t
->mm
->mmap_sem
);
2305 down_read(&vcpu
->kvm
->slots_lock
);
2306 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
2307 page
= gva_to_page(vcpu
, address
+ i
* PAGE_SIZE
);
2308 vcpu
->arch
.pio
.guest_pages
[i
] = page
;
2309 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
2310 up_read(¤t
->mm
->mmap_sem
);
2312 up_read(&vcpu
->kvm
->slots_lock
);
2313 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
2315 kvm_inject_gp(vcpu
, 0);
2316 free_pio_guest_pages(vcpu
);
2321 pio_dev
= vcpu_find_pio_dev(vcpu
, port
);
2322 if (!vcpu
->arch
.pio
.in
) {
2323 /* string PIO write */
2324 ret
= pio_copy_data(vcpu
);
2325 if (ret
>= 0 && pio_dev
) {
2326 pio_string_write(pio_dev
, vcpu
);
2328 if (vcpu
->arch
.pio
.count
== 0)
2332 pr_unimpl(vcpu
, "no string pio read support yet, "
2333 "port %x size %d count %ld\n",
2338 EXPORT_SYMBOL_GPL(kvm_emulate_pio_string
);
2340 int kvm_arch_init(void *opaque
)
2343 struct kvm_x86_ops
*ops
= (struct kvm_x86_ops
*)opaque
;
2346 printk(KERN_ERR
"kvm: already loaded the other module\n");
2351 if (!ops
->cpu_has_kvm_support()) {
2352 printk(KERN_ERR
"kvm: no hardware support\n");
2356 if (ops
->disabled_by_bios()) {
2357 printk(KERN_ERR
"kvm: disabled by bios\n");
2362 r
= kvm_mmu_module_init();
2366 kvm_init_msr_list();
2369 kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
2376 void kvm_arch_exit(void)
2379 kvm_mmu_module_exit();
2382 int kvm_emulate_halt(struct kvm_vcpu
*vcpu
)
2384 ++vcpu
->stat
.halt_exits
;
2385 if (irqchip_in_kernel(vcpu
->kvm
)) {
2386 vcpu
->arch
.mp_state
= VCPU_MP_STATE_HALTED
;
2387 kvm_vcpu_block(vcpu
);
2388 if (vcpu
->arch
.mp_state
!= VCPU_MP_STATE_RUNNABLE
)
2392 vcpu
->run
->exit_reason
= KVM_EXIT_HLT
;
2396 EXPORT_SYMBOL_GPL(kvm_emulate_halt
);
2398 int kvm_emulate_hypercall(struct kvm_vcpu
*vcpu
)
2400 unsigned long nr
, a0
, a1
, a2
, a3
, ret
;
2402 kvm_x86_ops
->cache_regs(vcpu
);
2404 nr
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2405 a0
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2406 a1
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2407 a2
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2408 a3
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2410 if (!is_long_mode(vcpu
)) {
2419 case KVM_HC_VAPIC_POLL_IRQ
:
2426 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = ret
;
2427 kvm_x86_ops
->decache_regs(vcpu
);
2430 EXPORT_SYMBOL_GPL(kvm_emulate_hypercall
);
2432 int kvm_fix_hypercall(struct kvm_vcpu
*vcpu
)
2434 char instruction
[3];
2439 * Blow out the MMU to ensure that no other VCPU has an active mapping
2440 * to ensure that the updated hypercall appears atomically across all
2443 kvm_mmu_zap_all(vcpu
->kvm
);
2445 kvm_x86_ops
->cache_regs(vcpu
);
2446 kvm_x86_ops
->patch_hypercall(vcpu
, instruction
);
2447 if (emulator_write_emulated(vcpu
->arch
.rip
, instruction
, 3, vcpu
)
2448 != X86EMUL_CONTINUE
)
2454 static u64
mk_cr_64(u64 curr_cr
, u32 new_val
)
2456 return (curr_cr
& ~((1ULL << 32) - 1)) | new_val
;
2459 void realmode_lgdt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2461 struct descriptor_table dt
= { limit
, base
};
2463 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
2466 void realmode_lidt(struct kvm_vcpu
*vcpu
, u16 limit
, unsigned long base
)
2468 struct descriptor_table dt
= { limit
, base
};
2470 kvm_x86_ops
->set_idt(vcpu
, &dt
);
2473 void realmode_lmsw(struct kvm_vcpu
*vcpu
, unsigned long msw
,
2474 unsigned long *rflags
)
2477 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2480 unsigned long realmode_get_cr(struct kvm_vcpu
*vcpu
, int cr
)
2482 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2485 return vcpu
->arch
.cr0
;
2487 return vcpu
->arch
.cr2
;
2489 return vcpu
->arch
.cr3
;
2491 return vcpu
->arch
.cr4
;
2493 return get_cr8(vcpu
);
2495 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
2500 void realmode_set_cr(struct kvm_vcpu
*vcpu
, int cr
, unsigned long val
,
2501 unsigned long *rflags
)
2505 set_cr0(vcpu
, mk_cr_64(vcpu
->arch
.cr0
, val
));
2506 *rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2509 vcpu
->arch
.cr2
= val
;
2515 set_cr4(vcpu
, mk_cr_64(vcpu
->arch
.cr4
, val
));
2518 set_cr8(vcpu
, val
& 0xfUL
);
2521 vcpu_printf(vcpu
, "%s: unexpected cr %u\n", __FUNCTION__
, cr
);
2525 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu
*vcpu
, int i
)
2527 struct kvm_cpuid_entry2
*e
= &vcpu
->arch
.cpuid_entries
[i
];
2528 int j
, nent
= vcpu
->arch
.cpuid_nent
;
2530 e
->flags
&= ~KVM_CPUID_FLAG_STATE_READ_NEXT
;
2531 /* when no next entry is found, the current entry[i] is reselected */
2532 for (j
= i
+ 1; j
== i
; j
= (j
+ 1) % nent
) {
2533 struct kvm_cpuid_entry2
*ej
= &vcpu
->arch
.cpuid_entries
[j
];
2534 if (ej
->function
== e
->function
) {
2535 ej
->flags
|= KVM_CPUID_FLAG_STATE_READ_NEXT
;
2539 return 0; /* silence gcc, even though control never reaches here */
2542 /* find an entry with matching function, matching index (if needed), and that
2543 * should be read next (if it's stateful) */
2544 static int is_matching_cpuid_entry(struct kvm_cpuid_entry2
*e
,
2545 u32 function
, u32 index
)
2547 if (e
->function
!= function
)
2549 if ((e
->flags
& KVM_CPUID_FLAG_SIGNIFCANT_INDEX
) && e
->index
!= index
)
2551 if ((e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
) &&
2552 !(e
->flags
& KVM_CPUID_FLAG_STATE_READ_NEXT
))
2557 void kvm_emulate_cpuid(struct kvm_vcpu
*vcpu
)
2560 u32 function
, index
;
2561 struct kvm_cpuid_entry2
*e
, *best
;
2563 kvm_x86_ops
->cache_regs(vcpu
);
2564 function
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2565 index
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2566 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = 0;
2567 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = 0;
2568 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = 0;
2569 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = 0;
2571 for (i
= 0; i
< vcpu
->arch
.cpuid_nent
; ++i
) {
2572 e
= &vcpu
->arch
.cpuid_entries
[i
];
2573 if (is_matching_cpuid_entry(e
, function
, index
)) {
2574 if (e
->flags
& KVM_CPUID_FLAG_STATEFUL_FUNC
)
2575 move_to_next_stateful_cpuid_entry(vcpu
, i
);
2580 * Both basic or both extended?
2582 if (((e
->function
^ function
) & 0x80000000) == 0)
2583 if (!best
|| e
->function
> best
->function
)
2587 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = best
->eax
;
2588 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = best
->ebx
;
2589 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = best
->ecx
;
2590 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = best
->edx
;
2592 kvm_x86_ops
->decache_regs(vcpu
);
2593 kvm_x86_ops
->skip_emulated_instruction(vcpu
);
2595 EXPORT_SYMBOL_GPL(kvm_emulate_cpuid
);
2598 * Check if userspace requested an interrupt window, and that the
2599 * interrupt window is open.
2601 * No need to exit to userspace if we already have an interrupt queued.
2603 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
2604 struct kvm_run
*kvm_run
)
2606 return (!vcpu
->arch
.irq_summary
&&
2607 kvm_run
->request_interrupt_window
&&
2608 vcpu
->arch
.interrupt_window_open
&&
2609 (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
));
2612 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
2613 struct kvm_run
*kvm_run
)
2615 kvm_run
->if_flag
= (kvm_x86_ops
->get_rflags(vcpu
) & X86_EFLAGS_IF
) != 0;
2616 kvm_run
->cr8
= get_cr8(vcpu
);
2617 kvm_run
->apic_base
= kvm_get_apic_base(vcpu
);
2618 if (irqchip_in_kernel(vcpu
->kvm
))
2619 kvm_run
->ready_for_interrupt_injection
= 1;
2621 kvm_run
->ready_for_interrupt_injection
=
2622 (vcpu
->arch
.interrupt_window_open
&&
2623 vcpu
->arch
.irq_summary
== 0);
2626 static void vapic_enter(struct kvm_vcpu
*vcpu
)
2628 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2631 if (!apic
|| !apic
->vapic_addr
)
2634 down_read(¤t
->mm
->mmap_sem
);
2635 page
= gfn_to_page(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2636 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
2637 vcpu
->arch
.apic
->vapic_page
= page
;
2639 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
2640 up_read(¤t
->mm
->mmap_sem
);
2641 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
2644 vcpu
->arch
.apic
->vapic_page
= page
;
2645 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
2648 static void vapic_exit(struct kvm_vcpu
*vcpu
)
2650 struct kvm_lapic
*apic
= vcpu
->arch
.apic
;
2652 if (!apic
|| !apic
->vapic_addr
)
2655 kvm_release_page_dirty(apic
->vapic_page
);
2656 mark_page_dirty(vcpu
->kvm
, apic
->vapic_addr
>> PAGE_SHIFT
);
2659 static int __vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2663 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
)) {
2664 pr_debug("vcpu %d received sipi with vector # %x\n",
2665 vcpu
->vcpu_id
, vcpu
->arch
.sipi_vector
);
2666 kvm_lapic_reset(vcpu
);
2667 r
= kvm_x86_ops
->vcpu_reset(vcpu
);
2670 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
2676 if (vcpu
->guest_debug
.enabled
)
2677 kvm_x86_ops
->guest_debug_pre(vcpu
);
2680 r
= kvm_mmu_reload(vcpu
);
2684 if (vcpu
->requests
) {
2685 if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER
, &vcpu
->requests
))
2686 __kvm_migrate_apic_timer(vcpu
);
2687 if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS
,
2689 kvm_run
->exit_reason
= KVM_EXIT_TPR_ACCESS
;
2695 kvm_inject_pending_timer_irqs(vcpu
);
2699 kvm_x86_ops
->prepare_guest_switch(vcpu
);
2700 kvm_load_guest_fpu(vcpu
);
2702 local_irq_disable();
2704 if (need_resched()) {
2711 if (signal_pending(current
)) {
2715 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2716 ++vcpu
->stat
.signal_exits
;
2720 if (vcpu
->arch
.exception
.pending
)
2721 __queue_exception(vcpu
);
2722 else if (irqchip_in_kernel(vcpu
->kvm
))
2723 kvm_x86_ops
->inject_pending_irq(vcpu
);
2725 kvm_x86_ops
->inject_pending_vectors(vcpu
, kvm_run
);
2727 kvm_lapic_sync_to_vapic(vcpu
);
2729 vcpu
->guest_mode
= 1;
2733 if (test_and_clear_bit(KVM_REQ_TLB_FLUSH
, &vcpu
->requests
))
2734 kvm_x86_ops
->tlb_flush(vcpu
);
2736 kvm_x86_ops
->run(vcpu
, kvm_run
);
2738 vcpu
->guest_mode
= 0;
2744 * We must have an instruction between local_irq_enable() and
2745 * kvm_guest_exit(), so the timer interrupt isn't delayed by
2746 * the interrupt shadow. The stat.exits increment will do nicely.
2747 * But we need to prevent reordering, hence this barrier():
2756 * Profile KVM exit RIPs:
2758 if (unlikely(prof_on
== KVM_PROFILING
)) {
2759 kvm_x86_ops
->cache_regs(vcpu
);
2760 profile_hit(KVM_PROFILING
, (void *)vcpu
->arch
.rip
);
2763 if (vcpu
->arch
.exception
.pending
&& kvm_x86_ops
->exception_injected(vcpu
))
2764 vcpu
->arch
.exception
.pending
= false;
2766 kvm_lapic_sync_from_vapic(vcpu
);
2768 r
= kvm_x86_ops
->handle_exit(kvm_run
, vcpu
);
2771 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
2773 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2774 ++vcpu
->stat
.request_irq_exits
;
2777 if (!need_resched())
2787 post_kvm_run_save(vcpu
, kvm_run
);
2794 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2801 if (unlikely(vcpu
->arch
.mp_state
== VCPU_MP_STATE_UNINITIALIZED
)) {
2802 kvm_vcpu_block(vcpu
);
2807 if (vcpu
->sigset_active
)
2808 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2810 /* re-sync apic's tpr */
2811 if (!irqchip_in_kernel(vcpu
->kvm
))
2812 set_cr8(vcpu
, kvm_run
->cr8
);
2814 if (vcpu
->arch
.pio
.cur_count
) {
2815 r
= complete_pio(vcpu
);
2819 #if CONFIG_HAS_IOMEM
2820 if (vcpu
->mmio_needed
) {
2821 memcpy(vcpu
->mmio_data
, kvm_run
->mmio
.data
, 8);
2822 vcpu
->mmio_read_completed
= 1;
2823 vcpu
->mmio_needed
= 0;
2824 r
= emulate_instruction(vcpu
, kvm_run
,
2825 vcpu
->arch
.mmio_fault_cr2
, 0,
2826 EMULTYPE_NO_DECODE
);
2827 if (r
== EMULATE_DO_MMIO
) {
2829 * Read-modify-write. Back to userspace.
2836 if (kvm_run
->exit_reason
== KVM_EXIT_HYPERCALL
) {
2837 kvm_x86_ops
->cache_regs(vcpu
);
2838 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = kvm_run
->hypercall
.ret
;
2839 kvm_x86_ops
->decache_regs(vcpu
);
2842 r
= __vcpu_run(vcpu
, kvm_run
);
2845 if (vcpu
->sigset_active
)
2846 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2852 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2856 kvm_x86_ops
->cache_regs(vcpu
);
2858 regs
->rax
= vcpu
->arch
.regs
[VCPU_REGS_RAX
];
2859 regs
->rbx
= vcpu
->arch
.regs
[VCPU_REGS_RBX
];
2860 regs
->rcx
= vcpu
->arch
.regs
[VCPU_REGS_RCX
];
2861 regs
->rdx
= vcpu
->arch
.regs
[VCPU_REGS_RDX
];
2862 regs
->rsi
= vcpu
->arch
.regs
[VCPU_REGS_RSI
];
2863 regs
->rdi
= vcpu
->arch
.regs
[VCPU_REGS_RDI
];
2864 regs
->rsp
= vcpu
->arch
.regs
[VCPU_REGS_RSP
];
2865 regs
->rbp
= vcpu
->arch
.regs
[VCPU_REGS_RBP
];
2866 #ifdef CONFIG_X86_64
2867 regs
->r8
= vcpu
->arch
.regs
[VCPU_REGS_R8
];
2868 regs
->r9
= vcpu
->arch
.regs
[VCPU_REGS_R9
];
2869 regs
->r10
= vcpu
->arch
.regs
[VCPU_REGS_R10
];
2870 regs
->r11
= vcpu
->arch
.regs
[VCPU_REGS_R11
];
2871 regs
->r12
= vcpu
->arch
.regs
[VCPU_REGS_R12
];
2872 regs
->r13
= vcpu
->arch
.regs
[VCPU_REGS_R13
];
2873 regs
->r14
= vcpu
->arch
.regs
[VCPU_REGS_R14
];
2874 regs
->r15
= vcpu
->arch
.regs
[VCPU_REGS_R15
];
2877 regs
->rip
= vcpu
->arch
.rip
;
2878 regs
->rflags
= kvm_x86_ops
->get_rflags(vcpu
);
2881 * Don't leak debug flags in case they were set for guest debugging
2883 if (vcpu
->guest_debug
.enabled
&& vcpu
->guest_debug
.singlestep
)
2884 regs
->rflags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
2891 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2895 vcpu
->arch
.regs
[VCPU_REGS_RAX
] = regs
->rax
;
2896 vcpu
->arch
.regs
[VCPU_REGS_RBX
] = regs
->rbx
;
2897 vcpu
->arch
.regs
[VCPU_REGS_RCX
] = regs
->rcx
;
2898 vcpu
->arch
.regs
[VCPU_REGS_RDX
] = regs
->rdx
;
2899 vcpu
->arch
.regs
[VCPU_REGS_RSI
] = regs
->rsi
;
2900 vcpu
->arch
.regs
[VCPU_REGS_RDI
] = regs
->rdi
;
2901 vcpu
->arch
.regs
[VCPU_REGS_RSP
] = regs
->rsp
;
2902 vcpu
->arch
.regs
[VCPU_REGS_RBP
] = regs
->rbp
;
2903 #ifdef CONFIG_X86_64
2904 vcpu
->arch
.regs
[VCPU_REGS_R8
] = regs
->r8
;
2905 vcpu
->arch
.regs
[VCPU_REGS_R9
] = regs
->r9
;
2906 vcpu
->arch
.regs
[VCPU_REGS_R10
] = regs
->r10
;
2907 vcpu
->arch
.regs
[VCPU_REGS_R11
] = regs
->r11
;
2908 vcpu
->arch
.regs
[VCPU_REGS_R12
] = regs
->r12
;
2909 vcpu
->arch
.regs
[VCPU_REGS_R13
] = regs
->r13
;
2910 vcpu
->arch
.regs
[VCPU_REGS_R14
] = regs
->r14
;
2911 vcpu
->arch
.regs
[VCPU_REGS_R15
] = regs
->r15
;
2914 vcpu
->arch
.rip
= regs
->rip
;
2915 kvm_x86_ops
->set_rflags(vcpu
, regs
->rflags
);
2917 kvm_x86_ops
->decache_regs(vcpu
);
2924 static void get_segment(struct kvm_vcpu
*vcpu
,
2925 struct kvm_segment
*var
, int seg
)
2927 return kvm_x86_ops
->get_segment(vcpu
, var
, seg
);
2930 void kvm_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
2932 struct kvm_segment cs
;
2934 get_segment(vcpu
, &cs
, VCPU_SREG_CS
);
2938 EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits
);
2940 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2941 struct kvm_sregs
*sregs
)
2943 struct descriptor_table dt
;
2948 get_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
2949 get_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
2950 get_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
2951 get_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
2952 get_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
2953 get_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
2955 get_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
2956 get_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
2958 kvm_x86_ops
->get_idt(vcpu
, &dt
);
2959 sregs
->idt
.limit
= dt
.limit
;
2960 sregs
->idt
.base
= dt
.base
;
2961 kvm_x86_ops
->get_gdt(vcpu
, &dt
);
2962 sregs
->gdt
.limit
= dt
.limit
;
2963 sregs
->gdt
.base
= dt
.base
;
2965 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
2966 sregs
->cr0
= vcpu
->arch
.cr0
;
2967 sregs
->cr2
= vcpu
->arch
.cr2
;
2968 sregs
->cr3
= vcpu
->arch
.cr3
;
2969 sregs
->cr4
= vcpu
->arch
.cr4
;
2970 sregs
->cr8
= get_cr8(vcpu
);
2971 sregs
->efer
= vcpu
->arch
.shadow_efer
;
2972 sregs
->apic_base
= kvm_get_apic_base(vcpu
);
2974 if (irqchip_in_kernel(vcpu
->kvm
)) {
2975 memset(sregs
->interrupt_bitmap
, 0,
2976 sizeof sregs
->interrupt_bitmap
);
2977 pending_vec
= kvm_x86_ops
->get_irq(vcpu
);
2978 if (pending_vec
>= 0)
2979 set_bit(pending_vec
,
2980 (unsigned long *)sregs
->interrupt_bitmap
);
2982 memcpy(sregs
->interrupt_bitmap
, vcpu
->arch
.irq_pending
,
2983 sizeof sregs
->interrupt_bitmap
);
2990 static void set_segment(struct kvm_vcpu
*vcpu
,
2991 struct kvm_segment
*var
, int seg
)
2993 return kvm_x86_ops
->set_segment(vcpu
, var
, seg
);
2996 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2997 struct kvm_sregs
*sregs
)
2999 int mmu_reset_needed
= 0;
3000 int i
, pending_vec
, max_bits
;
3001 struct descriptor_table dt
;
3005 dt
.limit
= sregs
->idt
.limit
;
3006 dt
.base
= sregs
->idt
.base
;
3007 kvm_x86_ops
->set_idt(vcpu
, &dt
);
3008 dt
.limit
= sregs
->gdt
.limit
;
3009 dt
.base
= sregs
->gdt
.base
;
3010 kvm_x86_ops
->set_gdt(vcpu
, &dt
);
3012 vcpu
->arch
.cr2
= sregs
->cr2
;
3013 mmu_reset_needed
|= vcpu
->arch
.cr3
!= sregs
->cr3
;
3014 vcpu
->arch
.cr3
= sregs
->cr3
;
3016 set_cr8(vcpu
, sregs
->cr8
);
3018 mmu_reset_needed
|= vcpu
->arch
.shadow_efer
!= sregs
->efer
;
3019 #ifdef CONFIG_X86_64
3020 kvm_x86_ops
->set_efer(vcpu
, sregs
->efer
);
3022 kvm_set_apic_base(vcpu
, sregs
->apic_base
);
3024 kvm_x86_ops
->decache_cr4_guest_bits(vcpu
);
3026 mmu_reset_needed
|= vcpu
->arch
.cr0
!= sregs
->cr0
;
3027 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
3028 vcpu
->arch
.cr0
= sregs
->cr0
;
3030 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
3031 kvm_x86_ops
->set_cr0(vcpu
, sregs
->cr0
);
3032 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
3034 vcpu
->arch
.cr0
= sregs
->cr0
;
3035 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
3037 mmu_reset_needed
|= vcpu
->arch
.cr4
!= sregs
->cr4
;
3038 kvm_x86_ops
->set_cr4(vcpu
, sregs
->cr4
);
3039 if (!is_long_mode(vcpu
) && is_pae(vcpu
))
3040 load_pdptrs(vcpu
, vcpu
->arch
.cr3
);
3042 if (mmu_reset_needed
)
3043 kvm_mmu_reset_context(vcpu
);
3045 if (!irqchip_in_kernel(vcpu
->kvm
)) {
3046 memcpy(vcpu
->arch
.irq_pending
, sregs
->interrupt_bitmap
,
3047 sizeof vcpu
->arch
.irq_pending
);
3048 vcpu
->arch
.irq_summary
= 0;
3049 for (i
= 0; i
< ARRAY_SIZE(vcpu
->arch
.irq_pending
); ++i
)
3050 if (vcpu
->arch
.irq_pending
[i
])
3051 __set_bit(i
, &vcpu
->arch
.irq_summary
);
3053 max_bits
= (sizeof sregs
->interrupt_bitmap
) << 3;
3054 pending_vec
= find_first_bit(
3055 (const unsigned long *)sregs
->interrupt_bitmap
,
3057 /* Only pending external irq is handled here */
3058 if (pending_vec
< max_bits
) {
3059 kvm_x86_ops
->set_irq(vcpu
, pending_vec
);
3060 pr_debug("Set back pending irq %d\n",
3065 set_segment(vcpu
, &sregs
->cs
, VCPU_SREG_CS
);
3066 set_segment(vcpu
, &sregs
->ds
, VCPU_SREG_DS
);
3067 set_segment(vcpu
, &sregs
->es
, VCPU_SREG_ES
);
3068 set_segment(vcpu
, &sregs
->fs
, VCPU_SREG_FS
);
3069 set_segment(vcpu
, &sregs
->gs
, VCPU_SREG_GS
);
3070 set_segment(vcpu
, &sregs
->ss
, VCPU_SREG_SS
);
3072 set_segment(vcpu
, &sregs
->tr
, VCPU_SREG_TR
);
3073 set_segment(vcpu
, &sregs
->ldt
, VCPU_SREG_LDTR
);
3080 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu
*vcpu
,
3081 struct kvm_debug_guest
*dbg
)
3087 r
= kvm_x86_ops
->set_guest_debug(vcpu
, dbg
);
3095 * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
3096 * we have asm/x86/processor.h
3107 u32 st_space
[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
3108 #ifdef CONFIG_X86_64
3109 u32 xmm_space
[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
3111 u32 xmm_space
[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
3116 * Translate a guest virtual address to a guest physical address.
3118 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
3119 struct kvm_translation
*tr
)
3121 unsigned long vaddr
= tr
->linear_address
;
3125 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
3126 down_read(¤t
->mm
->mmap_sem
);
3128 down_read(&vcpu
->kvm
->slots_lock
);
3129 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
3130 gpa
= vcpu
->arch
.mmu
.gva_to_gpa(vcpu
, vaddr
);
3131 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
3132 up_read(¤t
->mm
->mmap_sem
);
3134 up_read(&vcpu
->kvm
->slots_lock
);
3135 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
3136 tr
->physical_address
= gpa
;
3137 tr
->valid
= gpa
!= UNMAPPED_GVA
;
3145 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3147 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3151 memcpy(fpu
->fpr
, fxsave
->st_space
, 128);
3152 fpu
->fcw
= fxsave
->cwd
;
3153 fpu
->fsw
= fxsave
->swd
;
3154 fpu
->ftwx
= fxsave
->twd
;
3155 fpu
->last_opcode
= fxsave
->fop
;
3156 fpu
->last_ip
= fxsave
->rip
;
3157 fpu
->last_dp
= fxsave
->rdp
;
3158 memcpy(fpu
->xmm
, fxsave
->xmm_space
, sizeof fxsave
->xmm_space
);
3165 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3167 struct fxsave
*fxsave
= (struct fxsave
*)&vcpu
->arch
.guest_fx_image
;
3171 memcpy(fxsave
->st_space
, fpu
->fpr
, 128);
3172 fxsave
->cwd
= fpu
->fcw
;
3173 fxsave
->swd
= fpu
->fsw
;
3174 fxsave
->twd
= fpu
->ftwx
;
3175 fxsave
->fop
= fpu
->last_opcode
;
3176 fxsave
->rip
= fpu
->last_ip
;
3177 fxsave
->rdp
= fpu
->last_dp
;
3178 memcpy(fxsave
->xmm_space
, fpu
->xmm
, sizeof fxsave
->xmm_space
);
3185 void fx_init(struct kvm_vcpu
*vcpu
)
3187 unsigned after_mxcsr_mask
;
3189 /* Initialize guest FPU by resetting ours and saving into guest's */
3191 fx_save(&vcpu
->arch
.host_fx_image
);
3193 fx_save(&vcpu
->arch
.guest_fx_image
);
3194 fx_restore(&vcpu
->arch
.host_fx_image
);
3197 vcpu
->arch
.cr0
|= X86_CR0_ET
;
3198 after_mxcsr_mask
= offsetof(struct i387_fxsave_struct
, st_space
);
3199 vcpu
->arch
.guest_fx_image
.mxcsr
= 0x1f80;
3200 memset((void *)&vcpu
->arch
.guest_fx_image
+ after_mxcsr_mask
,
3201 0, sizeof(struct i387_fxsave_struct
) - after_mxcsr_mask
);
3203 EXPORT_SYMBOL_GPL(fx_init
);
3205 void kvm_load_guest_fpu(struct kvm_vcpu
*vcpu
)
3207 if (!vcpu
->fpu_active
|| vcpu
->guest_fpu_loaded
)
3210 vcpu
->guest_fpu_loaded
= 1;
3211 fx_save(&vcpu
->arch
.host_fx_image
);
3212 fx_restore(&vcpu
->arch
.guest_fx_image
);
3214 EXPORT_SYMBOL_GPL(kvm_load_guest_fpu
);
3216 void kvm_put_guest_fpu(struct kvm_vcpu
*vcpu
)
3218 if (!vcpu
->guest_fpu_loaded
)
3221 vcpu
->guest_fpu_loaded
= 0;
3222 fx_save(&vcpu
->arch
.guest_fx_image
);
3223 fx_restore(&vcpu
->arch
.host_fx_image
);
3224 ++vcpu
->stat
.fpu_reload
;
3226 EXPORT_SYMBOL_GPL(kvm_put_guest_fpu
);
3228 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
3230 kvm_x86_ops
->vcpu_free(vcpu
);
3233 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
3236 return kvm_x86_ops
->vcpu_create(kvm
, id
);
3239 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
3243 /* We do fxsave: this must be aligned. */
3244 BUG_ON((unsigned long)&vcpu
->arch
.host_fx_image
& 0xF);
3247 r
= kvm_arch_vcpu_reset(vcpu
);
3249 r
= kvm_mmu_setup(vcpu
);
3256 kvm_x86_ops
->vcpu_free(vcpu
);
3260 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
3263 kvm_mmu_unload(vcpu
);
3266 kvm_x86_ops
->vcpu_free(vcpu
);
3269 int kvm_arch_vcpu_reset(struct kvm_vcpu
*vcpu
)
3271 return kvm_x86_ops
->vcpu_reset(vcpu
);
3274 void kvm_arch_hardware_enable(void *garbage
)
3276 kvm_x86_ops
->hardware_enable(garbage
);
3279 void kvm_arch_hardware_disable(void *garbage
)
3281 kvm_x86_ops
->hardware_disable(garbage
);
3284 int kvm_arch_hardware_setup(void)
3286 return kvm_x86_ops
->hardware_setup();
3289 void kvm_arch_hardware_unsetup(void)
3291 kvm_x86_ops
->hardware_unsetup();
3294 void kvm_arch_check_processor_compat(void *rtn
)
3296 kvm_x86_ops
->check_processor_compatibility(rtn
);
3299 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
3305 BUG_ON(vcpu
->kvm
== NULL
);
3308 vcpu
->arch
.mmu
.root_hpa
= INVALID_PAGE
;
3309 if (!irqchip_in_kernel(kvm
) || vcpu
->vcpu_id
== 0)
3310 vcpu
->arch
.mp_state
= VCPU_MP_STATE_RUNNABLE
;
3312 vcpu
->arch
.mp_state
= VCPU_MP_STATE_UNINITIALIZED
;
3314 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
3319 vcpu
->arch
.pio_data
= page_address(page
);
3321 r
= kvm_mmu_create(vcpu
);
3323 goto fail_free_pio_data
;
3325 if (irqchip_in_kernel(kvm
)) {
3326 r
= kvm_create_lapic(vcpu
);
3328 goto fail_mmu_destroy
;
3334 kvm_mmu_destroy(vcpu
);
3336 free_page((unsigned long)vcpu
->arch
.pio_data
);
3341 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
3343 kvm_free_lapic(vcpu
);
3344 kvm_mmu_destroy(vcpu
);
3345 free_page((unsigned long)vcpu
->arch
.pio_data
);
3348 struct kvm
*kvm_arch_create_vm(void)
3350 struct kvm
*kvm
= kzalloc(sizeof(struct kvm
), GFP_KERNEL
);
3353 return ERR_PTR(-ENOMEM
);
3355 INIT_LIST_HEAD(&kvm
->arch
.active_mmu_pages
);
3360 static void kvm_unload_vcpu_mmu(struct kvm_vcpu
*vcpu
)
3363 kvm_mmu_unload(vcpu
);
3367 static void kvm_free_vcpus(struct kvm
*kvm
)
3372 * Unpin any mmu pages first.
3374 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
)
3376 kvm_unload_vcpu_mmu(kvm
->vcpus
[i
]);
3377 for (i
= 0; i
< KVM_MAX_VCPUS
; ++i
) {
3378 if (kvm
->vcpus
[i
]) {
3379 kvm_arch_vcpu_free(kvm
->vcpus
[i
]);
3380 kvm
->vcpus
[i
] = NULL
;
3386 void kvm_arch_destroy_vm(struct kvm
*kvm
)
3388 kfree(kvm
->arch
.vpic
);
3389 kfree(kvm
->arch
.vioapic
);
3390 kvm_free_vcpus(kvm
);
3391 kvm_free_physmem(kvm
);
3395 int kvm_arch_set_memory_region(struct kvm
*kvm
,
3396 struct kvm_userspace_memory_region
*mem
,
3397 struct kvm_memory_slot old
,
3400 int npages
= mem
->memory_size
>> PAGE_SHIFT
;
3401 struct kvm_memory_slot
*memslot
= &kvm
->memslots
[mem
->slot
];
3403 /*To keep backward compatibility with older userspace,
3404 *x86 needs to hanlde !user_alloc case.
3407 if (npages
&& !old
.rmap
) {
3408 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
3410 down_write(¤t
->mm
->mmap_sem
);
3411 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
3412 memslot
->userspace_addr
= do_mmap(NULL
, 0,
3414 PROT_READ
| PROT_WRITE
,
3415 MAP_SHARED
| MAP_ANONYMOUS
,
3417 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
3419 up_write(¤t
->mm
->mmap_sem
);
3420 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
3422 if (IS_ERR((void *)memslot
->userspace_addr
))
3423 return PTR_ERR((void *)memslot
->userspace_addr
);
3425 if (!old
.user_alloc
&& old
.rmap
) {
3428 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
3430 down_write(¤t
->mm
->mmap_sem
);
3431 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
3432 ret
= do_munmap(current
->mm
, old
.userspace_addr
,
3433 old
.npages
* PAGE_SIZE
);
3434 <<<<<<< HEAD
:arch
/x86
/kvm
/x86
.c
3436 up_write(¤t
->mm
->mmap_sem
);
3437 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kvm
/x86
.c
3440 "kvm_vm_ioctl_set_memory_region: "
3441 "failed to munmap memory\n");
3446 if (!kvm
->arch
.n_requested_mmu_pages
) {
3447 unsigned int nr_mmu_pages
= kvm_mmu_calculate_mmu_pages(kvm
);
3448 kvm_mmu_change_mmu_pages(kvm
, nr_mmu_pages
);
3451 kvm_mmu_slot_remove_write_access(kvm
, mem
->slot
);
3452 kvm_flush_remote_tlbs(kvm
);
3457 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
3459 return vcpu
->arch
.mp_state
== VCPU_MP_STATE_RUNNABLE
3460 || vcpu
->arch
.mp_state
== VCPU_MP_STATE_SIPI_RECEIVED
;
3463 static void vcpu_kick_intr(void *info
)
3466 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*)info
;
3467 printk(KERN_DEBUG
"vcpu_kick_intr %p \n", vcpu
);
3471 void kvm_vcpu_kick(struct kvm_vcpu
*vcpu
)
3473 int ipi_pcpu
= vcpu
->cpu
;
3475 if (waitqueue_active(&vcpu
->wq
)) {
3476 wake_up_interruptible(&vcpu
->wq
);
3477 ++vcpu
->stat
.halt_wakeup
;
3479 if (vcpu
->guest_mode
)
3480 smp_call_function_single(ipi_pcpu
, vcpu_kick_intr
, vcpu
, 0, 0);