2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
7 * Copyright (C) 2006 Qumranet, Inc.
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
21 #include <linux/module.h>
22 #include <linux/kernel.h>
24 #include <linux/highmem.h>
25 #include <linux/profile.h>
29 #include "segment_descriptor.h"
31 MODULE_AUTHOR("Qumranet");
32 MODULE_LICENSE("GPL");
34 static DEFINE_PER_CPU(struct vmcs
*, vmxarea
);
35 static DEFINE_PER_CPU(struct vmcs
*, current_vmcs
);
43 static struct vmcs_descriptor
{
49 #define VMX_SEGMENT_FIELD(seg) \
50 [VCPU_SREG_##seg] = { \
51 .selector = GUEST_##seg##_SELECTOR, \
52 .base = GUEST_##seg##_BASE, \
53 .limit = GUEST_##seg##_LIMIT, \
54 .ar_bytes = GUEST_##seg##_AR_BYTES, \
57 static struct kvm_vmx_segment_field
{
62 } kvm_vmx_segment_fields
[] = {
63 VMX_SEGMENT_FIELD(CS
),
64 VMX_SEGMENT_FIELD(DS
),
65 VMX_SEGMENT_FIELD(ES
),
66 VMX_SEGMENT_FIELD(FS
),
67 VMX_SEGMENT_FIELD(GS
),
68 VMX_SEGMENT_FIELD(SS
),
69 VMX_SEGMENT_FIELD(TR
),
70 VMX_SEGMENT_FIELD(LDTR
),
73 static const u32 vmx_msr_index
[] = {
75 MSR_SYSCALL_MASK
, MSR_LSTAR
, MSR_CSTAR
, MSR_KERNEL_GS_BASE
,
77 MSR_EFER
, MSR_K6_STAR
,
79 #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
82 static unsigned msr_offset_kernel_gs_base
;
83 #define NR_64BIT_MSRS 4
85 #define NR_64BIT_MSRS 0
88 static inline int is_page_fault(u32 intr_info
)
90 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
|
91 INTR_INFO_VALID_MASK
)) ==
92 (INTR_TYPE_EXCEPTION
| PF_VECTOR
| INTR_INFO_VALID_MASK
);
95 static inline int is_external_interrupt(u32 intr_info
)
97 return (intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VALID_MASK
))
98 == (INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
101 static struct vmx_msr_entry
*find_msr_entry(struct kvm_vcpu
*vcpu
, u32 msr
)
105 for (i
= 0; i
< vcpu
->nmsrs
; ++i
)
106 if (vcpu
->guest_msrs
[i
].index
== msr
)
107 return &vcpu
->guest_msrs
[i
];
111 static void vmcs_clear(struct vmcs
*vmcs
)
113 u64 phys_addr
= __pa(vmcs
);
116 asm volatile (ASM_VMX_VMCLEAR_RAX
"; setna %0"
117 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
120 printk(KERN_ERR
"kvm: vmclear fail: %p/%llx\n",
124 static void __vcpu_clear(void *arg
)
126 struct kvm_vcpu
*vcpu
= arg
;
127 int cpu
= raw_smp_processor_id();
129 if (vcpu
->cpu
== cpu
)
130 vmcs_clear(vcpu
->vmcs
);
131 if (per_cpu(current_vmcs
, cpu
) == vcpu
->vmcs
)
132 per_cpu(current_vmcs
, cpu
) = NULL
;
135 static void vcpu_clear(struct kvm_vcpu
*vcpu
)
137 if (vcpu
->cpu
!= raw_smp_processor_id() && vcpu
->cpu
!= -1)
138 smp_call_function_single(vcpu
->cpu
, __vcpu_clear
, vcpu
, 0, 1);
144 static unsigned long vmcs_readl(unsigned long field
)
148 asm volatile (ASM_VMX_VMREAD_RDX_RAX
149 : "=a"(value
) : "d"(field
) : "cc");
153 static u16
vmcs_read16(unsigned long field
)
155 return vmcs_readl(field
);
158 static u32
vmcs_read32(unsigned long field
)
160 return vmcs_readl(field
);
163 static u64
vmcs_read64(unsigned long field
)
166 return vmcs_readl(field
);
168 return vmcs_readl(field
) | ((u64
)vmcs_readl(field
+1) << 32);
172 static noinline
void vmwrite_error(unsigned long field
, unsigned long value
)
174 printk(KERN_ERR
"vmwrite error: reg %lx value %lx (err %d)\n",
175 field
, value
, vmcs_read32(VM_INSTRUCTION_ERROR
));
179 static void vmcs_writel(unsigned long field
, unsigned long value
)
183 asm volatile (ASM_VMX_VMWRITE_RAX_RDX
"; setna %0"
184 : "=q"(error
) : "a"(value
), "d"(field
) : "cc" );
186 vmwrite_error(field
, value
);
189 static void vmcs_write16(unsigned long field
, u16 value
)
191 vmcs_writel(field
, value
);
194 static void vmcs_write32(unsigned long field
, u32 value
)
196 vmcs_writel(field
, value
);
199 static void vmcs_write64(unsigned long field
, u64 value
)
202 vmcs_writel(field
, value
);
204 vmcs_writel(field
, value
);
206 vmcs_writel(field
+1, value
>> 32);
211 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
212 * vcpu mutex is already taken.
214 static void vmx_vcpu_load(struct kvm_vcpu
*vcpu
)
216 u64 phys_addr
= __pa(vcpu
->vmcs
);
221 if (vcpu
->cpu
!= cpu
)
224 if (per_cpu(current_vmcs
, cpu
) != vcpu
->vmcs
) {
227 per_cpu(current_vmcs
, cpu
) = vcpu
->vmcs
;
228 asm volatile (ASM_VMX_VMPTRLD_RAX
"; setna %0"
229 : "=g"(error
) : "a"(&phys_addr
), "m"(phys_addr
)
232 printk(KERN_ERR
"kvm: vmptrld %p/%llx fail\n",
233 vcpu
->vmcs
, phys_addr
);
236 if (vcpu
->cpu
!= cpu
) {
237 struct descriptor_table dt
;
238 unsigned long sysenter_esp
;
242 * Linux uses per-cpu TSS and GDT, so set these when switching
245 vmcs_writel(HOST_TR_BASE
, read_tr_base()); /* 22.2.4 */
247 vmcs_writel(HOST_GDTR_BASE
, dt
.base
); /* 22.2.4 */
249 rdmsrl(MSR_IA32_SYSENTER_ESP
, sysenter_esp
);
250 vmcs_writel(HOST_IA32_SYSENTER_ESP
, sysenter_esp
); /* 22.2.3 */
254 static void vmx_vcpu_put(struct kvm_vcpu
*vcpu
)
259 static void vmx_vcpu_decache(struct kvm_vcpu
*vcpu
)
264 static unsigned long vmx_get_rflags(struct kvm_vcpu
*vcpu
)
266 return vmcs_readl(GUEST_RFLAGS
);
269 static void vmx_set_rflags(struct kvm_vcpu
*vcpu
, unsigned long rflags
)
271 vmcs_writel(GUEST_RFLAGS
, rflags
);
274 static void skip_emulated_instruction(struct kvm_vcpu
*vcpu
)
277 u32 interruptibility
;
279 rip
= vmcs_readl(GUEST_RIP
);
280 rip
+= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
281 vmcs_writel(GUEST_RIP
, rip
);
284 * We emulated an instruction, so temporary interrupt blocking
285 * should be removed, if set.
287 interruptibility
= vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
);
288 if (interruptibility
& 3)
289 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
,
290 interruptibility
& ~3);
291 vcpu
->interrupt_window_open
= 1;
294 static void vmx_inject_gp(struct kvm_vcpu
*vcpu
, unsigned error_code
)
296 printk(KERN_DEBUG
"inject_general_protection: rip 0x%lx\n",
297 vmcs_readl(GUEST_RIP
));
298 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, error_code
);
299 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
301 INTR_TYPE_EXCEPTION
|
302 INTR_INFO_DELIEVER_CODE_MASK
|
303 INTR_INFO_VALID_MASK
);
307 * Set up the vmcs to automatically save and restore system
308 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
309 * mode, as fiddling with msrs is very expensive.
311 static void setup_msrs(struct kvm_vcpu
*vcpu
)
313 int nr_skip
, nr_good_msrs
;
315 if (is_long_mode(vcpu
))
316 nr_skip
= NR_BAD_MSRS
;
318 nr_skip
= NR_64BIT_MSRS
;
319 nr_good_msrs
= vcpu
->nmsrs
- nr_skip
;
321 vmcs_writel(VM_ENTRY_MSR_LOAD_ADDR
,
322 virt_to_phys(vcpu
->guest_msrs
+ nr_skip
));
323 vmcs_writel(VM_EXIT_MSR_STORE_ADDR
,
324 virt_to_phys(vcpu
->guest_msrs
+ nr_skip
));
325 vmcs_writel(VM_EXIT_MSR_LOAD_ADDR
,
326 virt_to_phys(vcpu
->host_msrs
+ nr_skip
));
327 vmcs_write32(VM_EXIT_MSR_STORE_COUNT
, nr_good_msrs
); /* 22.2.2 */
328 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT
, nr_good_msrs
); /* 22.2.2 */
329 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT
, nr_good_msrs
); /* 22.2.2 */
333 * reads and returns guest's timestamp counter "register"
334 * guest_tsc = host_tsc + tsc_offset -- 21.3
336 static u64
guest_read_tsc(void)
338 u64 host_tsc
, tsc_offset
;
341 tsc_offset
= vmcs_read64(TSC_OFFSET
);
342 return host_tsc
+ tsc_offset
;
346 * writes 'guest_tsc' into guest's timestamp counter "register"
347 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
349 static void guest_write_tsc(u64 guest_tsc
)
354 vmcs_write64(TSC_OFFSET
, guest_tsc
- host_tsc
);
357 static void reload_tss(void)
359 #ifndef CONFIG_X86_64
362 * VT restores TR but not its size. Useless.
364 struct descriptor_table gdt
;
365 struct segment_descriptor
*descs
;
368 descs
= (void *)gdt
.base
;
369 descs
[GDT_ENTRY_TSS
].type
= 9; /* available TSS */
375 * Reads an msr value (of 'msr_index') into 'pdata'.
376 * Returns 0 on success, non-0 otherwise.
377 * Assumes vcpu_load() was already called.
379 static int vmx_get_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64
*pdata
)
382 struct vmx_msr_entry
*msr
;
385 printk(KERN_ERR
"BUG: get_msr called with NULL pdata\n");
392 data
= vmcs_readl(GUEST_FS_BASE
);
395 data
= vmcs_readl(GUEST_GS_BASE
);
398 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
400 case MSR_IA32_TIME_STAMP_COUNTER
:
401 data
= guest_read_tsc();
403 case MSR_IA32_SYSENTER_CS
:
404 data
= vmcs_read32(GUEST_SYSENTER_CS
);
406 case MSR_IA32_SYSENTER_EIP
:
407 data
= vmcs_readl(GUEST_SYSENTER_EIP
);
409 case MSR_IA32_SYSENTER_ESP
:
410 data
= vmcs_readl(GUEST_SYSENTER_ESP
);
413 msr
= find_msr_entry(vcpu
, msr_index
);
418 return kvm_get_msr_common(vcpu
, msr_index
, pdata
);
426 * Writes msr value into into the appropriate "register".
427 * Returns 0 on success, non-0 otherwise.
428 * Assumes vcpu_load() was already called.
430 static int vmx_set_msr(struct kvm_vcpu
*vcpu
, u32 msr_index
, u64 data
)
432 struct vmx_msr_entry
*msr
;
436 return kvm_set_msr_common(vcpu
, msr_index
, data
);
438 vmcs_writel(GUEST_FS_BASE
, data
);
441 vmcs_writel(GUEST_GS_BASE
, data
);
444 case MSR_IA32_SYSENTER_CS
:
445 vmcs_write32(GUEST_SYSENTER_CS
, data
);
447 case MSR_IA32_SYSENTER_EIP
:
448 vmcs_writel(GUEST_SYSENTER_EIP
, data
);
450 case MSR_IA32_SYSENTER_ESP
:
451 vmcs_writel(GUEST_SYSENTER_ESP
, data
);
453 case MSR_IA32_TIME_STAMP_COUNTER
:
454 guest_write_tsc(data
);
457 msr
= find_msr_entry(vcpu
, msr_index
);
462 return kvm_set_msr_common(vcpu
, msr_index
, data
);
471 * Sync the rsp and rip registers into the vcpu structure. This allows
472 * registers to be accessed by indexing vcpu->regs.
474 static void vcpu_load_rsp_rip(struct kvm_vcpu
*vcpu
)
476 vcpu
->regs
[VCPU_REGS_RSP
] = vmcs_readl(GUEST_RSP
);
477 vcpu
->rip
= vmcs_readl(GUEST_RIP
);
481 * Syncs rsp and rip back into the vmcs. Should be called after possible
484 static void vcpu_put_rsp_rip(struct kvm_vcpu
*vcpu
)
486 vmcs_writel(GUEST_RSP
, vcpu
->regs
[VCPU_REGS_RSP
]);
487 vmcs_writel(GUEST_RIP
, vcpu
->rip
);
490 static int set_guest_debug(struct kvm_vcpu
*vcpu
, struct kvm_debug_guest
*dbg
)
492 unsigned long dr7
= 0x400;
493 u32 exception_bitmap
;
496 exception_bitmap
= vmcs_read32(EXCEPTION_BITMAP
);
497 old_singlestep
= vcpu
->guest_debug
.singlestep
;
499 vcpu
->guest_debug
.enabled
= dbg
->enabled
;
500 if (vcpu
->guest_debug
.enabled
) {
503 dr7
|= 0x200; /* exact */
504 for (i
= 0; i
< 4; ++i
) {
505 if (!dbg
->breakpoints
[i
].enabled
)
507 vcpu
->guest_debug
.bp
[i
] = dbg
->breakpoints
[i
].address
;
508 dr7
|= 2 << (i
*2); /* global enable */
509 dr7
|= 0 << (i
*4+16); /* execution breakpoint */
512 exception_bitmap
|= (1u << 1); /* Trap debug exceptions */
514 vcpu
->guest_debug
.singlestep
= dbg
->singlestep
;
516 exception_bitmap
&= ~(1u << 1); /* Ignore debug exceptions */
517 vcpu
->guest_debug
.singlestep
= 0;
520 if (old_singlestep
&& !vcpu
->guest_debug
.singlestep
) {
523 flags
= vmcs_readl(GUEST_RFLAGS
);
524 flags
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_RF
);
525 vmcs_writel(GUEST_RFLAGS
, flags
);
528 vmcs_write32(EXCEPTION_BITMAP
, exception_bitmap
);
529 vmcs_writel(GUEST_DR7
, dr7
);
534 static __init
int cpu_has_kvm_support(void)
536 unsigned long ecx
= cpuid_ecx(1);
537 return test_bit(5, &ecx
); /* CPUID.1:ECX.VMX[bit 5] -> VT */
540 static __init
int vmx_disabled_by_bios(void)
544 rdmsrl(MSR_IA32_FEATURE_CONTROL
, msr
);
545 return (msr
& 5) == 1; /* locked but not enabled */
548 static void hardware_enable(void *garbage
)
550 int cpu
= raw_smp_processor_id();
551 u64 phys_addr
= __pa(per_cpu(vmxarea
, cpu
));
554 rdmsrl(MSR_IA32_FEATURE_CONTROL
, old
);
556 /* enable and lock */
557 wrmsrl(MSR_IA32_FEATURE_CONTROL
, old
| 5);
558 write_cr4(read_cr4() | CR4_VMXE
); /* FIXME: not cpu hotplug safe */
559 asm volatile (ASM_VMX_VMXON_RAX
: : "a"(&phys_addr
), "m"(phys_addr
)
563 static void hardware_disable(void *garbage
)
565 asm volatile (ASM_VMX_VMXOFF
: : : "cc");
568 static __init
void setup_vmcs_descriptor(void)
570 u32 vmx_msr_low
, vmx_msr_high
;
572 rdmsr(MSR_IA32_VMX_BASIC
, vmx_msr_low
, vmx_msr_high
);
573 vmcs_descriptor
.size
= vmx_msr_high
& 0x1fff;
574 vmcs_descriptor
.order
= get_order(vmcs_descriptor
.size
);
575 vmcs_descriptor
.revision_id
= vmx_msr_low
;
578 static struct vmcs
*alloc_vmcs_cpu(int cpu
)
580 int node
= cpu_to_node(cpu
);
584 pages
= alloc_pages_node(node
, GFP_KERNEL
, vmcs_descriptor
.order
);
587 vmcs
= page_address(pages
);
588 memset(vmcs
, 0, vmcs_descriptor
.size
);
589 vmcs
->revision_id
= vmcs_descriptor
.revision_id
; /* vmcs revision id */
593 static struct vmcs
*alloc_vmcs(void)
595 return alloc_vmcs_cpu(raw_smp_processor_id());
598 static void free_vmcs(struct vmcs
*vmcs
)
600 free_pages((unsigned long)vmcs
, vmcs_descriptor
.order
);
603 static __exit
void free_kvm_area(void)
607 for_each_online_cpu(cpu
)
608 free_vmcs(per_cpu(vmxarea
, cpu
));
611 extern struct vmcs
*alloc_vmcs_cpu(int cpu
);
613 static __init
int alloc_kvm_area(void)
617 for_each_online_cpu(cpu
) {
620 vmcs
= alloc_vmcs_cpu(cpu
);
626 per_cpu(vmxarea
, cpu
) = vmcs
;
631 static __init
int hardware_setup(void)
633 setup_vmcs_descriptor();
634 return alloc_kvm_area();
637 static __exit
void hardware_unsetup(void)
642 static void update_exception_bitmap(struct kvm_vcpu
*vcpu
)
644 if (vcpu
->rmode
.active
)
645 vmcs_write32(EXCEPTION_BITMAP
, ~0);
647 vmcs_write32(EXCEPTION_BITMAP
, 1 << PF_VECTOR
);
650 static void fix_pmode_dataseg(int seg
, struct kvm_save_segment
*save
)
652 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
654 if (vmcs_readl(sf
->base
) == save
->base
&& (save
->base
& AR_S_MASK
)) {
655 vmcs_write16(sf
->selector
, save
->selector
);
656 vmcs_writel(sf
->base
, save
->base
);
657 vmcs_write32(sf
->limit
, save
->limit
);
658 vmcs_write32(sf
->ar_bytes
, save
->ar
);
660 u32 dpl
= (vmcs_read16(sf
->selector
) & SELECTOR_RPL_MASK
)
662 vmcs_write32(sf
->ar_bytes
, 0x93 | dpl
);
666 static void enter_pmode(struct kvm_vcpu
*vcpu
)
670 vcpu
->rmode
.active
= 0;
672 vmcs_writel(GUEST_TR_BASE
, vcpu
->rmode
.tr
.base
);
673 vmcs_write32(GUEST_TR_LIMIT
, vcpu
->rmode
.tr
.limit
);
674 vmcs_write32(GUEST_TR_AR_BYTES
, vcpu
->rmode
.tr
.ar
);
676 flags
= vmcs_readl(GUEST_RFLAGS
);
677 flags
&= ~(IOPL_MASK
| X86_EFLAGS_VM
);
678 flags
|= (vcpu
->rmode
.save_iopl
<< IOPL_SHIFT
);
679 vmcs_writel(GUEST_RFLAGS
, flags
);
681 vmcs_writel(GUEST_CR4
, (vmcs_readl(GUEST_CR4
) & ~CR4_VME_MASK
) |
682 (vmcs_readl(CR4_READ_SHADOW
) & CR4_VME_MASK
));
684 update_exception_bitmap(vcpu
);
686 fix_pmode_dataseg(VCPU_SREG_ES
, &vcpu
->rmode
.es
);
687 fix_pmode_dataseg(VCPU_SREG_DS
, &vcpu
->rmode
.ds
);
688 fix_pmode_dataseg(VCPU_SREG_GS
, &vcpu
->rmode
.gs
);
689 fix_pmode_dataseg(VCPU_SREG_FS
, &vcpu
->rmode
.fs
);
691 vmcs_write16(GUEST_SS_SELECTOR
, 0);
692 vmcs_write32(GUEST_SS_AR_BYTES
, 0x93);
694 vmcs_write16(GUEST_CS_SELECTOR
,
695 vmcs_read16(GUEST_CS_SELECTOR
) & ~SELECTOR_RPL_MASK
);
696 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
699 static int rmode_tss_base(struct kvm
* kvm
)
701 gfn_t base_gfn
= kvm
->memslots
[0].base_gfn
+ kvm
->memslots
[0].npages
- 3;
702 return base_gfn
<< PAGE_SHIFT
;
705 static void fix_rmode_seg(int seg
, struct kvm_save_segment
*save
)
707 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
709 save
->selector
= vmcs_read16(sf
->selector
);
710 save
->base
= vmcs_readl(sf
->base
);
711 save
->limit
= vmcs_read32(sf
->limit
);
712 save
->ar
= vmcs_read32(sf
->ar_bytes
);
713 vmcs_write16(sf
->selector
, vmcs_readl(sf
->base
) >> 4);
714 vmcs_write32(sf
->limit
, 0xffff);
715 vmcs_write32(sf
->ar_bytes
, 0xf3);
718 static void enter_rmode(struct kvm_vcpu
*vcpu
)
722 vcpu
->rmode
.active
= 1;
724 vcpu
->rmode
.tr
.base
= vmcs_readl(GUEST_TR_BASE
);
725 vmcs_writel(GUEST_TR_BASE
, rmode_tss_base(vcpu
->kvm
));
727 vcpu
->rmode
.tr
.limit
= vmcs_read32(GUEST_TR_LIMIT
);
728 vmcs_write32(GUEST_TR_LIMIT
, RMODE_TSS_SIZE
- 1);
730 vcpu
->rmode
.tr
.ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
731 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
733 flags
= vmcs_readl(GUEST_RFLAGS
);
734 vcpu
->rmode
.save_iopl
= (flags
& IOPL_MASK
) >> IOPL_SHIFT
;
736 flags
|= IOPL_MASK
| X86_EFLAGS_VM
;
738 vmcs_writel(GUEST_RFLAGS
, flags
);
739 vmcs_writel(GUEST_CR4
, vmcs_readl(GUEST_CR4
) | CR4_VME_MASK
);
740 update_exception_bitmap(vcpu
);
742 vmcs_write16(GUEST_SS_SELECTOR
, vmcs_readl(GUEST_SS_BASE
) >> 4);
743 vmcs_write32(GUEST_SS_LIMIT
, 0xffff);
744 vmcs_write32(GUEST_SS_AR_BYTES
, 0xf3);
746 vmcs_write32(GUEST_CS_AR_BYTES
, 0xf3);
747 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
748 if (vmcs_readl(GUEST_CS_BASE
) == 0xffff0000)
749 vmcs_writel(GUEST_CS_BASE
, 0xf0000);
750 vmcs_write16(GUEST_CS_SELECTOR
, vmcs_readl(GUEST_CS_BASE
) >> 4);
752 fix_rmode_seg(VCPU_SREG_ES
, &vcpu
->rmode
.es
);
753 fix_rmode_seg(VCPU_SREG_DS
, &vcpu
->rmode
.ds
);
754 fix_rmode_seg(VCPU_SREG_GS
, &vcpu
->rmode
.gs
);
755 fix_rmode_seg(VCPU_SREG_FS
, &vcpu
->rmode
.fs
);
760 static void enter_lmode(struct kvm_vcpu
*vcpu
)
764 guest_tr_ar
= vmcs_read32(GUEST_TR_AR_BYTES
);
765 if ((guest_tr_ar
& AR_TYPE_MASK
) != AR_TYPE_BUSY_64_TSS
) {
766 printk(KERN_DEBUG
"%s: tss fixup for long mode. \n",
768 vmcs_write32(GUEST_TR_AR_BYTES
,
769 (guest_tr_ar
& ~AR_TYPE_MASK
)
770 | AR_TYPE_BUSY_64_TSS
);
773 vcpu
->shadow_efer
|= EFER_LMA
;
775 find_msr_entry(vcpu
, MSR_EFER
)->data
|= EFER_LMA
| EFER_LME
;
776 vmcs_write32(VM_ENTRY_CONTROLS
,
777 vmcs_read32(VM_ENTRY_CONTROLS
)
778 | VM_ENTRY_CONTROLS_IA32E_MASK
);
781 static void exit_lmode(struct kvm_vcpu
*vcpu
)
783 vcpu
->shadow_efer
&= ~EFER_LMA
;
785 vmcs_write32(VM_ENTRY_CONTROLS
,
786 vmcs_read32(VM_ENTRY_CONTROLS
)
787 & ~VM_ENTRY_CONTROLS_IA32E_MASK
);
792 static void vmx_decache_cr0_cr4_guest_bits(struct kvm_vcpu
*vcpu
)
794 vcpu
->cr0
&= KVM_GUEST_CR0_MASK
;
795 vcpu
->cr0
|= vmcs_readl(GUEST_CR0
) & ~KVM_GUEST_CR0_MASK
;
797 vcpu
->cr4
&= KVM_GUEST_CR4_MASK
;
798 vcpu
->cr4
|= vmcs_readl(GUEST_CR4
) & ~KVM_GUEST_CR4_MASK
;
801 static void vmx_set_cr0(struct kvm_vcpu
*vcpu
, unsigned long cr0
)
803 if (vcpu
->rmode
.active
&& (cr0
& CR0_PE_MASK
))
806 if (!vcpu
->rmode
.active
&& !(cr0
& CR0_PE_MASK
))
810 if (vcpu
->shadow_efer
& EFER_LME
) {
811 if (!is_paging(vcpu
) && (cr0
& CR0_PG_MASK
))
813 if (is_paging(vcpu
) && !(cr0
& CR0_PG_MASK
))
818 vmcs_writel(CR0_READ_SHADOW
, cr0
);
819 vmcs_writel(GUEST_CR0
,
820 (cr0
& ~KVM_GUEST_CR0_MASK
) | KVM_VM_CR0_ALWAYS_ON
);
824 static void vmx_set_cr3(struct kvm_vcpu
*vcpu
, unsigned long cr3
)
826 vmcs_writel(GUEST_CR3
, cr3
);
829 static void vmx_set_cr4(struct kvm_vcpu
*vcpu
, unsigned long cr4
)
831 vmcs_writel(CR4_READ_SHADOW
, cr4
);
832 vmcs_writel(GUEST_CR4
, cr4
| (vcpu
->rmode
.active
?
833 KVM_RMODE_VM_CR4_ALWAYS_ON
: KVM_PMODE_VM_CR4_ALWAYS_ON
));
839 static void vmx_set_efer(struct kvm_vcpu
*vcpu
, u64 efer
)
841 struct vmx_msr_entry
*msr
= find_msr_entry(vcpu
, MSR_EFER
);
843 vcpu
->shadow_efer
= efer
;
844 if (efer
& EFER_LMA
) {
845 vmcs_write32(VM_ENTRY_CONTROLS
,
846 vmcs_read32(VM_ENTRY_CONTROLS
) |
847 VM_ENTRY_CONTROLS_IA32E_MASK
);
851 vmcs_write32(VM_ENTRY_CONTROLS
,
852 vmcs_read32(VM_ENTRY_CONTROLS
) &
853 ~VM_ENTRY_CONTROLS_IA32E_MASK
);
855 msr
->data
= efer
& ~EFER_LME
;
862 static u64
vmx_get_segment_base(struct kvm_vcpu
*vcpu
, int seg
)
864 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
866 return vmcs_readl(sf
->base
);
869 static void vmx_get_segment(struct kvm_vcpu
*vcpu
,
870 struct kvm_segment
*var
, int seg
)
872 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
875 var
->base
= vmcs_readl(sf
->base
);
876 var
->limit
= vmcs_read32(sf
->limit
);
877 var
->selector
= vmcs_read16(sf
->selector
);
878 ar
= vmcs_read32(sf
->ar_bytes
);
879 if (ar
& AR_UNUSABLE_MASK
)
882 var
->s
= (ar
>> 4) & 1;
883 var
->dpl
= (ar
>> 5) & 3;
884 var
->present
= (ar
>> 7) & 1;
885 var
->avl
= (ar
>> 12) & 1;
886 var
->l
= (ar
>> 13) & 1;
887 var
->db
= (ar
>> 14) & 1;
888 var
->g
= (ar
>> 15) & 1;
889 var
->unusable
= (ar
>> 16) & 1;
892 static void vmx_set_segment(struct kvm_vcpu
*vcpu
,
893 struct kvm_segment
*var
, int seg
)
895 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
898 vmcs_writel(sf
->base
, var
->base
);
899 vmcs_write32(sf
->limit
, var
->limit
);
900 vmcs_write16(sf
->selector
, var
->selector
);
901 if (vcpu
->rmode
.active
&& var
->s
) {
903 * Hack real-mode segments into vm86 compatibility.
905 if (var
->base
== 0xffff0000 && var
->selector
== 0xf000)
906 vmcs_writel(sf
->base
, 0xf0000);
908 } else if (var
->unusable
)
912 ar
|= (var
->s
& 1) << 4;
913 ar
|= (var
->dpl
& 3) << 5;
914 ar
|= (var
->present
& 1) << 7;
915 ar
|= (var
->avl
& 1) << 12;
916 ar
|= (var
->l
& 1) << 13;
917 ar
|= (var
->db
& 1) << 14;
918 ar
|= (var
->g
& 1) << 15;
920 if (ar
== 0) /* a 0 value means unusable */
921 ar
= AR_UNUSABLE_MASK
;
922 vmcs_write32(sf
->ar_bytes
, ar
);
925 static void vmx_get_cs_db_l_bits(struct kvm_vcpu
*vcpu
, int *db
, int *l
)
927 u32 ar
= vmcs_read32(GUEST_CS_AR_BYTES
);
929 *db
= (ar
>> 14) & 1;
933 static void vmx_get_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
935 dt
->limit
= vmcs_read32(GUEST_IDTR_LIMIT
);
936 dt
->base
= vmcs_readl(GUEST_IDTR_BASE
);
939 static void vmx_set_idt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
941 vmcs_write32(GUEST_IDTR_LIMIT
, dt
->limit
);
942 vmcs_writel(GUEST_IDTR_BASE
, dt
->base
);
945 static void vmx_get_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
947 dt
->limit
= vmcs_read32(GUEST_GDTR_LIMIT
);
948 dt
->base
= vmcs_readl(GUEST_GDTR_BASE
);
951 static void vmx_set_gdt(struct kvm_vcpu
*vcpu
, struct descriptor_table
*dt
)
953 vmcs_write32(GUEST_GDTR_LIMIT
, dt
->limit
);
954 vmcs_writel(GUEST_GDTR_BASE
, dt
->base
);
957 static int init_rmode_tss(struct kvm
* kvm
)
959 struct page
*p1
, *p2
, *p3
;
960 gfn_t fn
= rmode_tss_base(kvm
) >> PAGE_SHIFT
;
963 p1
= gfn_to_page(kvm
, fn
++);
964 p2
= gfn_to_page(kvm
, fn
++);
965 p3
= gfn_to_page(kvm
, fn
);
967 if (!p1
|| !p2
|| !p3
) {
968 kvm_printf(kvm
,"%s: gfn_to_page failed\n", __FUNCTION__
);
972 page
= kmap_atomic(p1
, KM_USER0
);
973 memset(page
, 0, PAGE_SIZE
);
974 *(u16
*)(page
+ 0x66) = TSS_BASE_SIZE
+ TSS_REDIRECTION_SIZE
;
975 kunmap_atomic(page
, KM_USER0
);
977 page
= kmap_atomic(p2
, KM_USER0
);
978 memset(page
, 0, PAGE_SIZE
);
979 kunmap_atomic(page
, KM_USER0
);
981 page
= kmap_atomic(p3
, KM_USER0
);
982 memset(page
, 0, PAGE_SIZE
);
983 *(page
+ RMODE_TSS_SIZE
- 2 * PAGE_SIZE
- 1) = ~0;
984 kunmap_atomic(page
, KM_USER0
);
989 static void vmcs_write32_fixedbits(u32 msr
, u32 vmcs_field
, u32 val
)
991 u32 msr_high
, msr_low
;
993 rdmsr(msr
, msr_low
, msr_high
);
997 vmcs_write32(vmcs_field
, val
);
1000 static void seg_setup(int seg
)
1002 struct kvm_vmx_segment_field
*sf
= &kvm_vmx_segment_fields
[seg
];
1004 vmcs_write16(sf
->selector
, 0);
1005 vmcs_writel(sf
->base
, 0);
1006 vmcs_write32(sf
->limit
, 0xffff);
1007 vmcs_write32(sf
->ar_bytes
, 0x93);
1011 * Sets up the vmcs for emulated real mode.
1013 static int vmx_vcpu_setup(struct kvm_vcpu
*vcpu
)
1015 u32 host_sysenter_cs
;
1018 struct descriptor_table dt
;
1021 extern asmlinkage
void kvm_vmx_return(void);
1023 if (!init_rmode_tss(vcpu
->kvm
)) {
1028 memset(vcpu
->regs
, 0, sizeof(vcpu
->regs
));
1029 vcpu
->regs
[VCPU_REGS_RDX
] = get_rdx_init_val();
1031 vcpu
->apic_base
= 0xfee00000 |
1032 /*for vcpu 0*/ MSR_IA32_APICBASE_BSP
|
1033 MSR_IA32_APICBASE_ENABLE
;
1038 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1039 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1041 vmcs_write16(GUEST_CS_SELECTOR
, 0xf000);
1042 vmcs_writel(GUEST_CS_BASE
, 0x000f0000);
1043 vmcs_write32(GUEST_CS_LIMIT
, 0xffff);
1044 vmcs_write32(GUEST_CS_AR_BYTES
, 0x9b);
1046 seg_setup(VCPU_SREG_DS
);
1047 seg_setup(VCPU_SREG_ES
);
1048 seg_setup(VCPU_SREG_FS
);
1049 seg_setup(VCPU_SREG_GS
);
1050 seg_setup(VCPU_SREG_SS
);
1052 vmcs_write16(GUEST_TR_SELECTOR
, 0);
1053 vmcs_writel(GUEST_TR_BASE
, 0);
1054 vmcs_write32(GUEST_TR_LIMIT
, 0xffff);
1055 vmcs_write32(GUEST_TR_AR_BYTES
, 0x008b);
1057 vmcs_write16(GUEST_LDTR_SELECTOR
, 0);
1058 vmcs_writel(GUEST_LDTR_BASE
, 0);
1059 vmcs_write32(GUEST_LDTR_LIMIT
, 0xffff);
1060 vmcs_write32(GUEST_LDTR_AR_BYTES
, 0x00082);
1062 vmcs_write32(GUEST_SYSENTER_CS
, 0);
1063 vmcs_writel(GUEST_SYSENTER_ESP
, 0);
1064 vmcs_writel(GUEST_SYSENTER_EIP
, 0);
1066 vmcs_writel(GUEST_RFLAGS
, 0x02);
1067 vmcs_writel(GUEST_RIP
, 0xfff0);
1068 vmcs_writel(GUEST_RSP
, 0);
1070 //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0
1071 vmcs_writel(GUEST_DR7
, 0x400);
1073 vmcs_writel(GUEST_GDTR_BASE
, 0);
1074 vmcs_write32(GUEST_GDTR_LIMIT
, 0xffff);
1076 vmcs_writel(GUEST_IDTR_BASE
, 0);
1077 vmcs_write32(GUEST_IDTR_LIMIT
, 0xffff);
1079 vmcs_write32(GUEST_ACTIVITY_STATE
, 0);
1080 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO
, 0);
1081 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS
, 0);
1084 vmcs_write64(IO_BITMAP_A
, 0);
1085 vmcs_write64(IO_BITMAP_B
, 0);
1089 vmcs_write64(VMCS_LINK_POINTER
, -1ull); /* 22.3.1.5 */
1091 /* Special registers */
1092 vmcs_write64(GUEST_IA32_DEBUGCTL
, 0);
1095 vmcs_write32_fixedbits(MSR_IA32_VMX_PINBASED_CTLS
,
1096 PIN_BASED_VM_EXEC_CONTROL
,
1097 PIN_BASED_EXT_INTR_MASK
/* 20.6.1 */
1098 | PIN_BASED_NMI_EXITING
/* 20.6.1 */
1100 vmcs_write32_fixedbits(MSR_IA32_VMX_PROCBASED_CTLS
,
1101 CPU_BASED_VM_EXEC_CONTROL
,
1102 CPU_BASED_HLT_EXITING
/* 20.6.2 */
1103 | CPU_BASED_CR8_LOAD_EXITING
/* 20.6.2 */
1104 | CPU_BASED_CR8_STORE_EXITING
/* 20.6.2 */
1105 | CPU_BASED_UNCOND_IO_EXITING
/* 20.6.2 */
1106 | CPU_BASED_MOV_DR_EXITING
1107 | CPU_BASED_USE_TSC_OFFSETING
/* 21.3 */
1110 vmcs_write32(EXCEPTION_BITMAP
, 1 << PF_VECTOR
);
1111 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK
, 0);
1112 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH
, 0);
1113 vmcs_write32(CR3_TARGET_COUNT
, 0); /* 22.2.1 */
1115 vmcs_writel(HOST_CR0
, read_cr0()); /* 22.2.3 */
1116 vmcs_writel(HOST_CR4
, read_cr4()); /* 22.2.3, 22.2.5 */
1117 vmcs_writel(HOST_CR3
, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1119 vmcs_write16(HOST_CS_SELECTOR
, __KERNEL_CS
); /* 22.2.4 */
1120 vmcs_write16(HOST_DS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1121 vmcs_write16(HOST_ES_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1122 vmcs_write16(HOST_FS_SELECTOR
, read_fs()); /* 22.2.4 */
1123 vmcs_write16(HOST_GS_SELECTOR
, read_gs()); /* 22.2.4 */
1124 vmcs_write16(HOST_SS_SELECTOR
, __KERNEL_DS
); /* 22.2.4 */
1125 #ifdef CONFIG_X86_64
1126 rdmsrl(MSR_FS_BASE
, a
);
1127 vmcs_writel(HOST_FS_BASE
, a
); /* 22.2.4 */
1128 rdmsrl(MSR_GS_BASE
, a
);
1129 vmcs_writel(HOST_GS_BASE
, a
); /* 22.2.4 */
1131 vmcs_writel(HOST_FS_BASE
, 0); /* 22.2.4 */
1132 vmcs_writel(HOST_GS_BASE
, 0); /* 22.2.4 */
1135 vmcs_write16(HOST_TR_SELECTOR
, GDT_ENTRY_TSS
*8); /* 22.2.4 */
1138 vmcs_writel(HOST_IDTR_BASE
, dt
.base
); /* 22.2.4 */
1141 vmcs_writel(HOST_RIP
, (unsigned long)kvm_vmx_return
); /* 22.2.5 */
1143 rdmsr(MSR_IA32_SYSENTER_CS
, host_sysenter_cs
, junk
);
1144 vmcs_write32(HOST_IA32_SYSENTER_CS
, host_sysenter_cs
);
1145 rdmsrl(MSR_IA32_SYSENTER_ESP
, a
);
1146 vmcs_writel(HOST_IA32_SYSENTER_ESP
, a
); /* 22.2.3 */
1147 rdmsrl(MSR_IA32_SYSENTER_EIP
, a
);
1148 vmcs_writel(HOST_IA32_SYSENTER_EIP
, a
); /* 22.2.3 */
1150 for (i
= 0; i
< NR_VMX_MSR
; ++i
) {
1151 u32 index
= vmx_msr_index
[i
];
1152 u32 data_low
, data_high
;
1154 int j
= vcpu
->nmsrs
;
1156 if (rdmsr_safe(index
, &data_low
, &data_high
) < 0)
1158 if (wrmsr_safe(index
, data_low
, data_high
) < 0)
1160 data
= data_low
| ((u64
)data_high
<< 32);
1161 vcpu
->host_msrs
[j
].index
= index
;
1162 vcpu
->host_msrs
[j
].reserved
= 0;
1163 vcpu
->host_msrs
[j
].data
= data
;
1164 vcpu
->guest_msrs
[j
] = vcpu
->host_msrs
[j
];
1165 #ifdef CONFIG_X86_64
1166 if (index
== MSR_KERNEL_GS_BASE
)
1167 msr_offset_kernel_gs_base
= j
;
1174 vmcs_write32_fixedbits(MSR_IA32_VMX_EXIT_CTLS
, VM_EXIT_CONTROLS
,
1175 (HOST_IS_64
<< 9)); /* 22.2,1, 20.7.1 */
1177 /* 22.2.1, 20.8.1 */
1178 vmcs_write32_fixedbits(MSR_IA32_VMX_ENTRY_CTLS
,
1179 VM_ENTRY_CONTROLS
, 0);
1180 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
, 0); /* 22.2.1 */
1182 #ifdef CONFIG_X86_64
1183 vmcs_writel(VIRTUAL_APIC_PAGE_ADDR
, 0);
1184 vmcs_writel(TPR_THRESHOLD
, 0);
1187 vmcs_writel(CR0_GUEST_HOST_MASK
, KVM_GUEST_CR0_MASK
);
1188 vmcs_writel(CR4_GUEST_HOST_MASK
, KVM_GUEST_CR4_MASK
);
1190 vcpu
->cr0
= 0x60000010;
1191 vmx_set_cr0(vcpu
, vcpu
->cr0
); // enter rmode
1192 vmx_set_cr4(vcpu
, 0);
1193 #ifdef CONFIG_X86_64
1194 vmx_set_efer(vcpu
, 0);
1203 static void inject_rmode_irq(struct kvm_vcpu
*vcpu
, int irq
)
1208 unsigned long flags
;
1209 unsigned long ss_base
= vmcs_readl(GUEST_SS_BASE
);
1210 u16 sp
= vmcs_readl(GUEST_RSP
);
1211 u32 ss_limit
= vmcs_read32(GUEST_SS_LIMIT
);
1213 if (sp
> ss_limit
|| sp
< 6 ) {
1214 vcpu_printf(vcpu
, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1216 vmcs_readl(GUEST_RSP
),
1217 vmcs_readl(GUEST_SS_BASE
),
1218 vmcs_read32(GUEST_SS_LIMIT
));
1222 if (kvm_read_guest(vcpu
, irq
* sizeof(ent
), sizeof(ent
), &ent
) !=
1224 vcpu_printf(vcpu
, "%s: read guest err\n", __FUNCTION__
);
1228 flags
= vmcs_readl(GUEST_RFLAGS
);
1229 cs
= vmcs_readl(GUEST_CS_BASE
) >> 4;
1230 ip
= vmcs_readl(GUEST_RIP
);
1233 if (kvm_write_guest(vcpu
, ss_base
+ sp
- 2, 2, &flags
) != 2 ||
1234 kvm_write_guest(vcpu
, ss_base
+ sp
- 4, 2, &cs
) != 2 ||
1235 kvm_write_guest(vcpu
, ss_base
+ sp
- 6, 2, &ip
) != 2) {
1236 vcpu_printf(vcpu
, "%s: write guest err\n", __FUNCTION__
);
1240 vmcs_writel(GUEST_RFLAGS
, flags
&
1241 ~( X86_EFLAGS_IF
| X86_EFLAGS_AC
| X86_EFLAGS_TF
));
1242 vmcs_write16(GUEST_CS_SELECTOR
, ent
[1]) ;
1243 vmcs_writel(GUEST_CS_BASE
, ent
[1] << 4);
1244 vmcs_writel(GUEST_RIP
, ent
[0]);
1245 vmcs_writel(GUEST_RSP
, (vmcs_readl(GUEST_RSP
) & ~0xffff) | (sp
- 6));
1248 static void kvm_do_inject_irq(struct kvm_vcpu
*vcpu
)
1250 int word_index
= __ffs(vcpu
->irq_summary
);
1251 int bit_index
= __ffs(vcpu
->irq_pending
[word_index
]);
1252 int irq
= word_index
* BITS_PER_LONG
+ bit_index
;
1254 clear_bit(bit_index
, &vcpu
->irq_pending
[word_index
]);
1255 if (!vcpu
->irq_pending
[word_index
])
1256 clear_bit(word_index
, &vcpu
->irq_summary
);
1258 if (vcpu
->rmode
.active
) {
1259 inject_rmode_irq(vcpu
, irq
);
1262 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
1263 irq
| INTR_TYPE_EXT_INTR
| INTR_INFO_VALID_MASK
);
1267 static void do_interrupt_requests(struct kvm_vcpu
*vcpu
,
1268 struct kvm_run
*kvm_run
)
1270 u32 cpu_based_vm_exec_control
;
1272 vcpu
->interrupt_window_open
=
1273 ((vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) &&
1274 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & 3) == 0);
1276 if (vcpu
->interrupt_window_open
&&
1277 vcpu
->irq_summary
&&
1278 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD
) & INTR_INFO_VALID_MASK
))
1280 * If interrupts enabled, and not blocked by sti or mov ss. Good.
1282 kvm_do_inject_irq(vcpu
);
1284 cpu_based_vm_exec_control
= vmcs_read32(CPU_BASED_VM_EXEC_CONTROL
);
1285 if (!vcpu
->interrupt_window_open
&&
1286 (vcpu
->irq_summary
|| kvm_run
->request_interrupt_window
))
1288 * Interrupts blocked. Wait for unblock.
1290 cpu_based_vm_exec_control
|= CPU_BASED_VIRTUAL_INTR_PENDING
;
1292 cpu_based_vm_exec_control
&= ~CPU_BASED_VIRTUAL_INTR_PENDING
;
1293 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL
, cpu_based_vm_exec_control
);
1296 static void kvm_guest_debug_pre(struct kvm_vcpu
*vcpu
)
1298 struct kvm_guest_debug
*dbg
= &vcpu
->guest_debug
;
1300 set_debugreg(dbg
->bp
[0], 0);
1301 set_debugreg(dbg
->bp
[1], 1);
1302 set_debugreg(dbg
->bp
[2], 2);
1303 set_debugreg(dbg
->bp
[3], 3);
1305 if (dbg
->singlestep
) {
1306 unsigned long flags
;
1308 flags
= vmcs_readl(GUEST_RFLAGS
);
1309 flags
|= X86_EFLAGS_TF
| X86_EFLAGS_RF
;
1310 vmcs_writel(GUEST_RFLAGS
, flags
);
1314 static int handle_rmode_exception(struct kvm_vcpu
*vcpu
,
1315 int vec
, u32 err_code
)
1317 if (!vcpu
->rmode
.active
)
1320 if (vec
== GP_VECTOR
&& err_code
== 0)
1321 if (emulate_instruction(vcpu
, NULL
, 0, 0) == EMULATE_DONE
)
1326 static int handle_exception(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1328 u32 intr_info
, error_code
;
1329 unsigned long cr2
, rip
;
1331 enum emulation_result er
;
1334 vect_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
1335 intr_info
= vmcs_read32(VM_EXIT_INTR_INFO
);
1337 if ((vect_info
& VECTORING_INFO_VALID_MASK
) &&
1338 !is_page_fault(intr_info
)) {
1339 printk(KERN_ERR
"%s: unexpected, vectoring info 0x%x "
1340 "intr info 0x%x\n", __FUNCTION__
, vect_info
, intr_info
);
1343 if (is_external_interrupt(vect_info
)) {
1344 int irq
= vect_info
& VECTORING_INFO_VECTOR_MASK
;
1345 set_bit(irq
, vcpu
->irq_pending
);
1346 set_bit(irq
/ BITS_PER_LONG
, &vcpu
->irq_summary
);
1349 if ((intr_info
& INTR_INFO_INTR_TYPE_MASK
) == 0x200) { /* nmi */
1354 rip
= vmcs_readl(GUEST_RIP
);
1355 if (intr_info
& INTR_INFO_DELIEVER_CODE_MASK
)
1356 error_code
= vmcs_read32(VM_EXIT_INTR_ERROR_CODE
);
1357 if (is_page_fault(intr_info
)) {
1358 cr2
= vmcs_readl(EXIT_QUALIFICATION
);
1360 spin_lock(&vcpu
->kvm
->lock
);
1361 r
= kvm_mmu_page_fault(vcpu
, cr2
, error_code
);
1363 spin_unlock(&vcpu
->kvm
->lock
);
1367 spin_unlock(&vcpu
->kvm
->lock
);
1371 er
= emulate_instruction(vcpu
, kvm_run
, cr2
, error_code
);
1372 spin_unlock(&vcpu
->kvm
->lock
);
1377 case EMULATE_DO_MMIO
:
1378 ++kvm_stat
.mmio_exits
;
1379 kvm_run
->exit_reason
= KVM_EXIT_MMIO
;
1382 vcpu_printf(vcpu
, "%s: emulate fail\n", __FUNCTION__
);
1389 if (vcpu
->rmode
.active
&&
1390 handle_rmode_exception(vcpu
, intr_info
& INTR_INFO_VECTOR_MASK
,
1394 if ((intr_info
& (INTR_INFO_INTR_TYPE_MASK
| INTR_INFO_VECTOR_MASK
)) == (INTR_TYPE_EXCEPTION
| 1)) {
1395 kvm_run
->exit_reason
= KVM_EXIT_DEBUG
;
1398 kvm_run
->exit_reason
= KVM_EXIT_EXCEPTION
;
1399 kvm_run
->ex
.exception
= intr_info
& INTR_INFO_VECTOR_MASK
;
1400 kvm_run
->ex
.error_code
= error_code
;
1404 static int handle_external_interrupt(struct kvm_vcpu
*vcpu
,
1405 struct kvm_run
*kvm_run
)
1407 ++kvm_stat
.irq_exits
;
1411 static int handle_triple_fault(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1413 kvm_run
->exit_reason
= KVM_EXIT_SHUTDOWN
;
1417 static int get_io_count(struct kvm_vcpu
*vcpu
, unsigned long *count
)
1424 if ((vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_VM
)) {
1427 u32 cs_ar
= vmcs_read32(GUEST_CS_AR_BYTES
);
1429 countr_size
= (cs_ar
& AR_L_MASK
) ? 8:
1430 (cs_ar
& AR_DB_MASK
) ? 4: 2;
1433 rip
= vmcs_readl(GUEST_RIP
);
1434 if (countr_size
!= 8)
1435 rip
+= vmcs_readl(GUEST_CS_BASE
);
1437 n
= kvm_read_guest(vcpu
, rip
, sizeof(inst
), &inst
);
1439 for (i
= 0; i
< n
; i
++) {
1440 switch (((u8
*)&inst
)[i
]) {
1453 countr_size
= (countr_size
== 2) ? 4: (countr_size
>> 1);
1461 *count
= vcpu
->regs
[VCPU_REGS_RCX
] & (~0ULL >> (64 - countr_size
));
1462 //printk("cx: %lx\n", vcpu->regs[VCPU_REGS_RCX]);
1466 static int handle_io(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1468 u64 exit_qualification
;
1469 int size
, down
, in
, string
, rep
;
1471 unsigned long count
;
1474 ++kvm_stat
.io_exits
;
1475 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
1476 in
= (exit_qualification
& 8) != 0;
1477 size
= (exit_qualification
& 7) + 1;
1478 string
= (exit_qualification
& 16) != 0;
1479 down
= (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_DF
) != 0;
1481 rep
= (exit_qualification
& 32) != 0;
1482 port
= exit_qualification
>> 16;
1485 if (rep
&& !get_io_count(vcpu
, &count
))
1487 address
= vmcs_readl(GUEST_LINEAR_ADDRESS
);
1489 return kvm_setup_pio(vcpu
, kvm_run
, in
, size
, count
, string
, down
,
1490 address
, rep
, port
);
1494 vmx_patch_hypercall(struct kvm_vcpu
*vcpu
, unsigned char *hypercall
)
1497 * Patch in the VMCALL instruction:
1499 hypercall
[0] = 0x0f;
1500 hypercall
[1] = 0x01;
1501 hypercall
[2] = 0xc1;
1502 hypercall
[3] = 0xc3;
1505 static int handle_cr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1507 u64 exit_qualification
;
1511 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
1512 cr
= exit_qualification
& 15;
1513 reg
= (exit_qualification
>> 8) & 15;
1514 switch ((exit_qualification
>> 4) & 3) {
1515 case 0: /* mov to cr */
1518 vcpu_load_rsp_rip(vcpu
);
1519 set_cr0(vcpu
, vcpu
->regs
[reg
]);
1520 skip_emulated_instruction(vcpu
);
1523 vcpu_load_rsp_rip(vcpu
);
1524 set_cr3(vcpu
, vcpu
->regs
[reg
]);
1525 skip_emulated_instruction(vcpu
);
1528 vcpu_load_rsp_rip(vcpu
);
1529 set_cr4(vcpu
, vcpu
->regs
[reg
]);
1530 skip_emulated_instruction(vcpu
);
1533 vcpu_load_rsp_rip(vcpu
);
1534 set_cr8(vcpu
, vcpu
->regs
[reg
]);
1535 skip_emulated_instruction(vcpu
);
1539 case 1: /*mov from cr*/
1542 vcpu_load_rsp_rip(vcpu
);
1543 vcpu
->regs
[reg
] = vcpu
->cr3
;
1544 vcpu_put_rsp_rip(vcpu
);
1545 skip_emulated_instruction(vcpu
);
1548 printk(KERN_DEBUG
"handle_cr: read CR8 "
1549 "cpu erratum AA15\n");
1550 vcpu_load_rsp_rip(vcpu
);
1551 vcpu
->regs
[reg
] = vcpu
->cr8
;
1552 vcpu_put_rsp_rip(vcpu
);
1553 skip_emulated_instruction(vcpu
);
1558 lmsw(vcpu
, (exit_qualification
>> LMSW_SOURCE_DATA_SHIFT
) & 0x0f);
1560 skip_emulated_instruction(vcpu
);
1565 kvm_run
->exit_reason
= 0;
1566 printk(KERN_ERR
"kvm: unhandled control register: op %d cr %d\n",
1567 (int)(exit_qualification
>> 4) & 3, cr
);
1571 static int handle_dr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1573 u64 exit_qualification
;
1578 * FIXME: this code assumes the host is debugging the guest.
1579 * need to deal with guest debugging itself too.
1581 exit_qualification
= vmcs_read64(EXIT_QUALIFICATION
);
1582 dr
= exit_qualification
& 7;
1583 reg
= (exit_qualification
>> 8) & 15;
1584 vcpu_load_rsp_rip(vcpu
);
1585 if (exit_qualification
& 16) {
1597 vcpu
->regs
[reg
] = val
;
1601 vcpu_put_rsp_rip(vcpu
);
1602 skip_emulated_instruction(vcpu
);
1606 static int handle_cpuid(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1608 kvm_emulate_cpuid(vcpu
);
1612 static int handle_rdmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1614 u32 ecx
= vcpu
->regs
[VCPU_REGS_RCX
];
1617 if (vmx_get_msr(vcpu
, ecx
, &data
)) {
1618 vmx_inject_gp(vcpu
, 0);
1622 /* FIXME: handling of bits 32:63 of rax, rdx */
1623 vcpu
->regs
[VCPU_REGS_RAX
] = data
& -1u;
1624 vcpu
->regs
[VCPU_REGS_RDX
] = (data
>> 32) & -1u;
1625 skip_emulated_instruction(vcpu
);
1629 static int handle_wrmsr(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1631 u32 ecx
= vcpu
->regs
[VCPU_REGS_RCX
];
1632 u64 data
= (vcpu
->regs
[VCPU_REGS_RAX
] & -1u)
1633 | ((u64
)(vcpu
->regs
[VCPU_REGS_RDX
] & -1u) << 32);
1635 if (vmx_set_msr(vcpu
, ecx
, data
) != 0) {
1636 vmx_inject_gp(vcpu
, 0);
1640 skip_emulated_instruction(vcpu
);
1644 static void post_kvm_run_save(struct kvm_vcpu
*vcpu
,
1645 struct kvm_run
*kvm_run
)
1647 kvm_run
->if_flag
= (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
) != 0;
1648 kvm_run
->cr8
= vcpu
->cr8
;
1649 kvm_run
->apic_base
= vcpu
->apic_base
;
1650 kvm_run
->ready_for_interrupt_injection
= (vcpu
->interrupt_window_open
&&
1651 vcpu
->irq_summary
== 0);
1654 static int handle_interrupt_window(struct kvm_vcpu
*vcpu
,
1655 struct kvm_run
*kvm_run
)
1658 * If the user space waits to inject interrupts, exit as soon as
1661 if (kvm_run
->request_interrupt_window
&&
1662 !vcpu
->irq_summary
) {
1663 kvm_run
->exit_reason
= KVM_EXIT_IRQ_WINDOW_OPEN
;
1664 ++kvm_stat
.irq_window_exits
;
1670 static int handle_halt(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1672 skip_emulated_instruction(vcpu
);
1673 if (vcpu
->irq_summary
)
1676 kvm_run
->exit_reason
= KVM_EXIT_HLT
;
1677 ++kvm_stat
.halt_exits
;
1681 static int handle_vmcall(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1683 skip_emulated_instruction(vcpu
);
1684 return kvm_hypercall(vcpu
, kvm_run
);
1688 * The exit handlers return 1 if the exit was handled fully and guest execution
1689 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
1690 * to be done to userspace and return 0.
1692 static int (*kvm_vmx_exit_handlers
[])(struct kvm_vcpu
*vcpu
,
1693 struct kvm_run
*kvm_run
) = {
1694 [EXIT_REASON_EXCEPTION_NMI
] = handle_exception
,
1695 [EXIT_REASON_EXTERNAL_INTERRUPT
] = handle_external_interrupt
,
1696 [EXIT_REASON_TRIPLE_FAULT
] = handle_triple_fault
,
1697 [EXIT_REASON_IO_INSTRUCTION
] = handle_io
,
1698 [EXIT_REASON_CR_ACCESS
] = handle_cr
,
1699 [EXIT_REASON_DR_ACCESS
] = handle_dr
,
1700 [EXIT_REASON_CPUID
] = handle_cpuid
,
1701 [EXIT_REASON_MSR_READ
] = handle_rdmsr
,
1702 [EXIT_REASON_MSR_WRITE
] = handle_wrmsr
,
1703 [EXIT_REASON_PENDING_INTERRUPT
] = handle_interrupt_window
,
1704 [EXIT_REASON_HLT
] = handle_halt
,
1705 [EXIT_REASON_VMCALL
] = handle_vmcall
,
1708 static const int kvm_vmx_max_exit_handlers
=
1709 sizeof(kvm_vmx_exit_handlers
) / sizeof(*kvm_vmx_exit_handlers
);
1712 * The guest has exited. See if we can fix it or if we need userspace
1715 static int kvm_handle_exit(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
1717 u32 vectoring_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
1718 u32 exit_reason
= vmcs_read32(VM_EXIT_REASON
);
1720 if ( (vectoring_info
& VECTORING_INFO_VALID_MASK
) &&
1721 exit_reason
!= EXIT_REASON_EXCEPTION_NMI
)
1722 printk(KERN_WARNING
"%s: unexpected, valid vectoring info and "
1723 "exit reason is 0x%x\n", __FUNCTION__
, exit_reason
);
1724 kvm_run
->instruction_length
= vmcs_read32(VM_EXIT_INSTRUCTION_LEN
);
1725 if (exit_reason
< kvm_vmx_max_exit_handlers
1726 && kvm_vmx_exit_handlers
[exit_reason
])
1727 return kvm_vmx_exit_handlers
[exit_reason
](vcpu
, kvm_run
);
1729 kvm_run
->exit_reason
= KVM_EXIT_UNKNOWN
;
1730 kvm_run
->hw
.hardware_exit_reason
= exit_reason
;
1736 * Check if userspace requested an interrupt window, and that the
1737 * interrupt window is open.
1739 * No need to exit to userspace if we already have an interrupt queued.
1741 static int dm_request_for_irq_injection(struct kvm_vcpu
*vcpu
,
1742 struct kvm_run
*kvm_run
)
1744 return (!vcpu
->irq_summary
&&
1745 kvm_run
->request_interrupt_window
&&
1746 vcpu
->interrupt_window_open
&&
1747 (vmcs_readl(GUEST_RFLAGS
) & X86_EFLAGS_IF
));
1750 static int vmx_vcpu_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
1753 u16 fs_sel
, gs_sel
, ldt_sel
;
1754 int fs_gs_ldt_reload_needed
;
1759 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1760 * allow segment selectors with cpl > 0 or ti == 1.
1764 ldt_sel
= read_ldt();
1765 fs_gs_ldt_reload_needed
= (fs_sel
& 7) | (gs_sel
& 7) | ldt_sel
;
1766 if (!fs_gs_ldt_reload_needed
) {
1767 vmcs_write16(HOST_FS_SELECTOR
, fs_sel
);
1768 vmcs_write16(HOST_GS_SELECTOR
, gs_sel
);
1770 vmcs_write16(HOST_FS_SELECTOR
, 0);
1771 vmcs_write16(HOST_GS_SELECTOR
, 0);
1774 #ifdef CONFIG_X86_64
1775 vmcs_writel(HOST_FS_BASE
, read_msr(MSR_FS_BASE
));
1776 vmcs_writel(HOST_GS_BASE
, read_msr(MSR_GS_BASE
));
1778 vmcs_writel(HOST_FS_BASE
, segment_base(fs_sel
));
1779 vmcs_writel(HOST_GS_BASE
, segment_base(gs_sel
));
1782 if (!vcpu
->mmio_read_completed
)
1783 do_interrupt_requests(vcpu
, kvm_run
);
1785 if (vcpu
->guest_debug
.enabled
)
1786 kvm_guest_debug_pre(vcpu
);
1788 fx_save(vcpu
->host_fx_image
);
1789 fx_restore(vcpu
->guest_fx_image
);
1791 #ifdef CONFIG_X86_64
1792 if (is_long_mode(vcpu
)) {
1793 save_msrs(vcpu
->host_msrs
+ msr_offset_kernel_gs_base
, 1);
1794 load_msrs(vcpu
->guest_msrs
, NR_BAD_MSRS
);
1799 /* Store host registers */
1801 #ifdef CONFIG_X86_64
1802 "push %%rax; push %%rbx; push %%rdx;"
1803 "push %%rsi; push %%rdi; push %%rbp;"
1804 "push %%r8; push %%r9; push %%r10; push %%r11;"
1805 "push %%r12; push %%r13; push %%r14; push %%r15;"
1807 ASM_VMX_VMWRITE_RSP_RDX
"\n\t"
1809 "pusha; push %%ecx \n\t"
1810 ASM_VMX_VMWRITE_RSP_RDX
"\n\t"
1812 /* Check if vmlaunch of vmresume is needed */
1814 /* Load guest registers. Don't clobber flags. */
1815 #ifdef CONFIG_X86_64
1816 "mov %c[cr2](%3), %%rax \n\t"
1817 "mov %%rax, %%cr2 \n\t"
1818 "mov %c[rax](%3), %%rax \n\t"
1819 "mov %c[rbx](%3), %%rbx \n\t"
1820 "mov %c[rdx](%3), %%rdx \n\t"
1821 "mov %c[rsi](%3), %%rsi \n\t"
1822 "mov %c[rdi](%3), %%rdi \n\t"
1823 "mov %c[rbp](%3), %%rbp \n\t"
1824 "mov %c[r8](%3), %%r8 \n\t"
1825 "mov %c[r9](%3), %%r9 \n\t"
1826 "mov %c[r10](%3), %%r10 \n\t"
1827 "mov %c[r11](%3), %%r11 \n\t"
1828 "mov %c[r12](%3), %%r12 \n\t"
1829 "mov %c[r13](%3), %%r13 \n\t"
1830 "mov %c[r14](%3), %%r14 \n\t"
1831 "mov %c[r15](%3), %%r15 \n\t"
1832 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
1834 "mov %c[cr2](%3), %%eax \n\t"
1835 "mov %%eax, %%cr2 \n\t"
1836 "mov %c[rax](%3), %%eax \n\t"
1837 "mov %c[rbx](%3), %%ebx \n\t"
1838 "mov %c[rdx](%3), %%edx \n\t"
1839 "mov %c[rsi](%3), %%esi \n\t"
1840 "mov %c[rdi](%3), %%edi \n\t"
1841 "mov %c[rbp](%3), %%ebp \n\t"
1842 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
1844 /* Enter guest mode */
1846 ASM_VMX_VMLAUNCH
"\n\t"
1847 "jmp kvm_vmx_return \n\t"
1848 "launched: " ASM_VMX_VMRESUME
"\n\t"
1849 ".globl kvm_vmx_return \n\t"
1851 /* Save guest registers, load host registers, keep flags */
1852 #ifdef CONFIG_X86_64
1853 "xchg %3, (%%rsp) \n\t"
1854 "mov %%rax, %c[rax](%3) \n\t"
1855 "mov %%rbx, %c[rbx](%3) \n\t"
1856 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
1857 "mov %%rdx, %c[rdx](%3) \n\t"
1858 "mov %%rsi, %c[rsi](%3) \n\t"
1859 "mov %%rdi, %c[rdi](%3) \n\t"
1860 "mov %%rbp, %c[rbp](%3) \n\t"
1861 "mov %%r8, %c[r8](%3) \n\t"
1862 "mov %%r9, %c[r9](%3) \n\t"
1863 "mov %%r10, %c[r10](%3) \n\t"
1864 "mov %%r11, %c[r11](%3) \n\t"
1865 "mov %%r12, %c[r12](%3) \n\t"
1866 "mov %%r13, %c[r13](%3) \n\t"
1867 "mov %%r14, %c[r14](%3) \n\t"
1868 "mov %%r15, %c[r15](%3) \n\t"
1869 "mov %%cr2, %%rax \n\t"
1870 "mov %%rax, %c[cr2](%3) \n\t"
1871 "mov (%%rsp), %3 \n\t"
1873 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
1874 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
1875 "pop %%rbp; pop %%rdi; pop %%rsi;"
1876 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
1878 "xchg %3, (%%esp) \n\t"
1879 "mov %%eax, %c[rax](%3) \n\t"
1880 "mov %%ebx, %c[rbx](%3) \n\t"
1881 "pushl (%%esp); popl %c[rcx](%3) \n\t"
1882 "mov %%edx, %c[rdx](%3) \n\t"
1883 "mov %%esi, %c[rsi](%3) \n\t"
1884 "mov %%edi, %c[rdi](%3) \n\t"
1885 "mov %%ebp, %c[rbp](%3) \n\t"
1886 "mov %%cr2, %%eax \n\t"
1887 "mov %%eax, %c[cr2](%3) \n\t"
1888 "mov (%%esp), %3 \n\t"
1890 "pop %%ecx; popa \n\t"
1895 : "r"(vcpu
->launched
), "d"((unsigned long)HOST_RSP
),
1897 [rax
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RAX
])),
1898 [rbx
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RBX
])),
1899 [rcx
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RCX
])),
1900 [rdx
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RDX
])),
1901 [rsi
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RSI
])),
1902 [rdi
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RDI
])),
1903 [rbp
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_RBP
])),
1904 #ifdef CONFIG_X86_64
1905 [r8
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R8
])),
1906 [r9
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R9
])),
1907 [r10
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R10
])),
1908 [r11
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R11
])),
1909 [r12
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R12
])),
1910 [r13
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R13
])),
1911 [r14
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R14
])),
1912 [r15
]"i"(offsetof(struct kvm_vcpu
, regs
[VCPU_REGS_R15
])),
1914 [cr2
]"i"(offsetof(struct kvm_vcpu
, cr2
))
1918 * Reload segment selectors ASAP. (it's needed for a functional
1919 * kernel: x86 relies on having __KERNEL_PDA in %fs and x86_64
1920 * relies on having 0 in %gs for the CPU PDA to work.)
1922 if (fs_gs_ldt_reload_needed
) {
1926 * If we have to reload gs, we must take care to
1927 * preserve our gs base.
1929 local_irq_disable();
1931 #ifdef CONFIG_X86_64
1932 wrmsrl(MSR_GS_BASE
, vmcs_readl(HOST_GS_BASE
));
1940 #ifdef CONFIG_X86_64
1941 if (is_long_mode(vcpu
)) {
1942 save_msrs(vcpu
->guest_msrs
, NR_BAD_MSRS
);
1943 load_msrs(vcpu
->host_msrs
, NR_BAD_MSRS
);
1947 fx_save(vcpu
->guest_fx_image
);
1948 fx_restore(vcpu
->host_fx_image
);
1949 vcpu
->interrupt_window_open
= (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO
) & 3) == 0;
1951 asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS
));
1954 kvm_run
->exit_reason
= KVM_EXIT_FAIL_ENTRY
;
1955 kvm_run
->fail_entry
.hardware_entry_failure_reason
1956 = vmcs_read32(VM_INSTRUCTION_ERROR
);
1960 * Profile KVM exit RIPs:
1962 if (unlikely(prof_on
== KVM_PROFILING
))
1963 profile_hit(KVM_PROFILING
, (void *)vmcs_readl(GUEST_RIP
));
1966 r
= kvm_handle_exit(kvm_run
, vcpu
);
1968 /* Give scheduler a change to reschedule. */
1969 if (signal_pending(current
)) {
1970 ++kvm_stat
.signal_exits
;
1971 post_kvm_run_save(vcpu
, kvm_run
);
1972 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1976 if (dm_request_for_irq_injection(vcpu
, kvm_run
)) {
1977 ++kvm_stat
.request_irq_exits
;
1978 post_kvm_run_save(vcpu
, kvm_run
);
1979 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1988 post_kvm_run_save(vcpu
, kvm_run
);
1992 static void vmx_flush_tlb(struct kvm_vcpu
*vcpu
)
1994 vmcs_writel(GUEST_CR3
, vmcs_readl(GUEST_CR3
));
1997 static void vmx_inject_page_fault(struct kvm_vcpu
*vcpu
,
2001 u32 vect_info
= vmcs_read32(IDT_VECTORING_INFO_FIELD
);
2003 ++kvm_stat
.pf_guest
;
2005 if (is_page_fault(vect_info
)) {
2006 printk(KERN_DEBUG
"inject_page_fault: "
2007 "double fault 0x%lx @ 0x%lx\n",
2008 addr
, vmcs_readl(GUEST_RIP
));
2009 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, 0);
2010 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2012 INTR_TYPE_EXCEPTION
|
2013 INTR_INFO_DELIEVER_CODE_MASK
|
2014 INTR_INFO_VALID_MASK
);
2018 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE
, err_code
);
2019 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD
,
2021 INTR_TYPE_EXCEPTION
|
2022 INTR_INFO_DELIEVER_CODE_MASK
|
2023 INTR_INFO_VALID_MASK
);
2027 static void vmx_free_vmcs(struct kvm_vcpu
*vcpu
)
2030 on_each_cpu(__vcpu_clear
, vcpu
, 0, 1);
2031 free_vmcs(vcpu
->vmcs
);
2036 static void vmx_free_vcpu(struct kvm_vcpu
*vcpu
)
2038 vmx_free_vmcs(vcpu
);
2041 static int vmx_create_vcpu(struct kvm_vcpu
*vcpu
)
2045 vcpu
->guest_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2046 if (!vcpu
->guest_msrs
)
2049 vcpu
->host_msrs
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2050 if (!vcpu
->host_msrs
)
2051 goto out_free_guest_msrs
;
2053 vmcs
= alloc_vmcs();
2064 kfree(vcpu
->host_msrs
);
2065 vcpu
->host_msrs
= NULL
;
2067 out_free_guest_msrs
:
2068 kfree(vcpu
->guest_msrs
);
2069 vcpu
->guest_msrs
= NULL
;
2074 static struct kvm_arch_ops vmx_arch_ops
= {
2075 .cpu_has_kvm_support
= cpu_has_kvm_support
,
2076 .disabled_by_bios
= vmx_disabled_by_bios
,
2077 .hardware_setup
= hardware_setup
,
2078 .hardware_unsetup
= hardware_unsetup
,
2079 .hardware_enable
= hardware_enable
,
2080 .hardware_disable
= hardware_disable
,
2082 .vcpu_create
= vmx_create_vcpu
,
2083 .vcpu_free
= vmx_free_vcpu
,
2085 .vcpu_load
= vmx_vcpu_load
,
2086 .vcpu_put
= vmx_vcpu_put
,
2087 .vcpu_decache
= vmx_vcpu_decache
,
2089 .set_guest_debug
= set_guest_debug
,
2090 .get_msr
= vmx_get_msr
,
2091 .set_msr
= vmx_set_msr
,
2092 .get_segment_base
= vmx_get_segment_base
,
2093 .get_segment
= vmx_get_segment
,
2094 .set_segment
= vmx_set_segment
,
2095 .get_cs_db_l_bits
= vmx_get_cs_db_l_bits
,
2096 .decache_cr0_cr4_guest_bits
= vmx_decache_cr0_cr4_guest_bits
,
2097 .set_cr0
= vmx_set_cr0
,
2098 .set_cr3
= vmx_set_cr3
,
2099 .set_cr4
= vmx_set_cr4
,
2100 #ifdef CONFIG_X86_64
2101 .set_efer
= vmx_set_efer
,
2103 .get_idt
= vmx_get_idt
,
2104 .set_idt
= vmx_set_idt
,
2105 .get_gdt
= vmx_get_gdt
,
2106 .set_gdt
= vmx_set_gdt
,
2107 .cache_regs
= vcpu_load_rsp_rip
,
2108 .decache_regs
= vcpu_put_rsp_rip
,
2109 .get_rflags
= vmx_get_rflags
,
2110 .set_rflags
= vmx_set_rflags
,
2112 .tlb_flush
= vmx_flush_tlb
,
2113 .inject_page_fault
= vmx_inject_page_fault
,
2115 .inject_gp
= vmx_inject_gp
,
2117 .run
= vmx_vcpu_run
,
2118 .skip_emulated_instruction
= skip_emulated_instruction
,
2119 .vcpu_setup
= vmx_vcpu_setup
,
2120 .patch_hypercall
= vmx_patch_hypercall
,
2123 static int __init
vmx_init(void)
2125 return kvm_init_arch(&vmx_arch_ops
, THIS_MODULE
);
2128 static void __exit
vmx_exit(void)
2133 module_init(vmx_init
)
2134 module_exit(vmx_exit
)