]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kvm/vmx.c
KVM: nVMX: Introduce vmcs02: VMCS used to run L2
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kvm / vmx.c
CommitLineData
6aa8b732
AK
1/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
9611c187 8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
6aa8b732
AK
9 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
85f455f7 19#include "irq.h"
1d737c8a 20#include "mmu.h"
e495606d 21
edf88417 22#include <linux/kvm_host.h>
6aa8b732 23#include <linux/module.h>
9d8f549d 24#include <linux/kernel.h>
6aa8b732
AK
25#include <linux/mm.h>
26#include <linux/highmem.h>
e8edc6e0 27#include <linux/sched.h>
c7addb90 28#include <linux/moduleparam.h>
229456fc 29#include <linux/ftrace_event.h>
5a0e3ad6 30#include <linux/slab.h>
cafd6659 31#include <linux/tboot.h>
5fdbf976 32#include "kvm_cache_regs.h"
35920a35 33#include "x86.h"
e495606d 34
6aa8b732 35#include <asm/io.h>
3b3be0d1 36#include <asm/desc.h>
13673a90 37#include <asm/vmx.h>
6210e37b 38#include <asm/virtext.h>
a0861c02 39#include <asm/mce.h>
2acf923e
DC
40#include <asm/i387.h>
41#include <asm/xcr.h>
6aa8b732 42
229456fc
MT
43#include "trace.h"
44
4ecac3fd 45#define __ex(x) __kvm_handle_fault_on_reboot(x)
5e520e62
AK
46#define __ex_clear(x, reg) \
47 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
4ecac3fd 48
6aa8b732
AK
49MODULE_AUTHOR("Qumranet");
50MODULE_LICENSE("GPL");
51
4462d21a 52static int __read_mostly bypass_guest_pf = 1;
c1f8bc04 53module_param(bypass_guest_pf, bool, S_IRUGO);
c7addb90 54
4462d21a 55static int __read_mostly enable_vpid = 1;
736caefe 56module_param_named(vpid, enable_vpid, bool, 0444);
2384d2b3 57
4462d21a 58static int __read_mostly flexpriority_enabled = 1;
736caefe 59module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
4c9fc8ef 60
4462d21a 61static int __read_mostly enable_ept = 1;
736caefe 62module_param_named(ept, enable_ept, bool, S_IRUGO);
d56f546d 63
3a624e29
NK
64static int __read_mostly enable_unrestricted_guest = 1;
65module_param_named(unrestricted_guest,
66 enable_unrestricted_guest, bool, S_IRUGO);
67
4462d21a 68static int __read_mostly emulate_invalid_guest_state = 0;
c1f8bc04 69module_param(emulate_invalid_guest_state, bool, S_IRUGO);
04fa4d32 70
b923e62e
DX
71static int __read_mostly vmm_exclusive = 1;
72module_param(vmm_exclusive, bool, S_IRUGO);
73
443381a8
AL
74static int __read_mostly yield_on_hlt = 1;
75module_param(yield_on_hlt, bool, S_IRUGO);
76
801d3424
NHE
77/*
78 * If nested=1, nested virtualization is supported, i.e., guests may use
79 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
80 * use VMX instructions.
81 */
82static int __read_mostly nested = 0;
83module_param(nested, bool, S_IRUGO);
84
cdc0e244
AK
85#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
86 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
87#define KVM_GUEST_CR0_MASK \
88 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
89#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
81231c69 90 (X86_CR0_WP | X86_CR0_NE)
cdc0e244
AK
91#define KVM_VM_CR0_ALWAYS_ON \
92 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
4c38609a
AK
93#define KVM_CR4_GUEST_OWNED_BITS \
94 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
95 | X86_CR4_OSXMMEXCPT)
96
cdc0e244
AK
97#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
98#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
99
78ac8b47
AK
100#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
101
4b8d54f9
ZE
102/*
103 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
104 * ple_gap: upper bound on the amount of time between two successive
105 * executions of PAUSE in a loop. Also indicate if ple enabled.
00c25bce 106 * According to test, this time is usually smaller than 128 cycles.
4b8d54f9
ZE
107 * ple_window: upper bound on the amount of time a guest is allowed to execute
108 * in a PAUSE loop. Tests indicate that most spinlocks are held for
109 * less than 2^12 cycles
110 * Time is measured based on a counter that runs at the same rate as the TSC,
111 * refer SDM volume 3b section 21.6.13 & 22.1.3.
112 */
00c25bce 113#define KVM_VMX_DEFAULT_PLE_GAP 128
4b8d54f9
ZE
114#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
115static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
116module_param(ple_gap, int, S_IRUGO);
117
118static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
119module_param(ple_window, int, S_IRUGO);
120
61d2ef2c 121#define NR_AUTOLOAD_MSRS 1
ff2f6fe9 122#define VMCS02_POOL_SIZE 1
61d2ef2c 123
a2fa3e9f
GH
124struct vmcs {
125 u32 revision_id;
126 u32 abort;
127 char data[0];
128};
129
d462b819
NHE
130/*
131 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
132 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
133 * loaded on this CPU (so we can clear them if the CPU goes down).
134 */
135struct loaded_vmcs {
136 struct vmcs *vmcs;
137 int cpu;
138 int launched;
139 struct list_head loaded_vmcss_on_cpu_link;
140};
141
26bb0981
AK
142struct shared_msr_entry {
143 unsigned index;
144 u64 data;
d5696725 145 u64 mask;
26bb0981
AK
146};
147
a9d30f33
NHE
148/*
149 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
150 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
151 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
152 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
153 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
154 * More than one of these structures may exist, if L1 runs multiple L2 guests.
155 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
156 * underlying hardware which will be used to run L2.
157 * This structure is packed to ensure that its layout is identical across
158 * machines (necessary for live migration).
159 * If there are changes in this struct, VMCS12_REVISION must be changed.
160 */
161struct __packed vmcs12 {
162 /* According to the Intel spec, a VMCS region must start with the
163 * following two fields. Then follow implementation-specific data.
164 */
165 u32 revision_id;
166 u32 abort;
167};
168
169/*
170 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
171 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
172 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
173 */
174#define VMCS12_REVISION 0x11e57ed0
175
176/*
177 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
178 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
179 * current implementation, 4K are reserved to avoid future complications.
180 */
181#define VMCS12_SIZE 0x1000
182
ff2f6fe9
NHE
183/* Used to remember the last vmcs02 used for some recently used vmcs12s */
184struct vmcs02_list {
185 struct list_head list;
186 gpa_t vmptr;
187 struct loaded_vmcs vmcs02;
188};
189
ec378aee
NHE
190/*
191 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
192 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
193 */
194struct nested_vmx {
195 /* Has the level1 guest done vmxon? */
196 bool vmxon;
a9d30f33
NHE
197
198 /* The guest-physical address of the current VMCS L1 keeps for L2 */
199 gpa_t current_vmptr;
200 /* The host-usable pointer to the above */
201 struct page *current_vmcs12_page;
202 struct vmcs12 *current_vmcs12;
ff2f6fe9
NHE
203
204 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
205 struct list_head vmcs02_pool;
206 int vmcs02_num;
ec378aee
NHE
207};
208
a2fa3e9f 209struct vcpu_vmx {
fb3f0f51 210 struct kvm_vcpu vcpu;
313dbd49 211 unsigned long host_rsp;
29bd8a78 212 u8 fail;
69c73028 213 u8 cpl;
9d58b931 214 bool nmi_known_unmasked;
51aa01d1 215 u32 exit_intr_info;
1155f76a 216 u32 idt_vectoring_info;
6de12732 217 ulong rflags;
26bb0981 218 struct shared_msr_entry *guest_msrs;
a2fa3e9f
GH
219 int nmsrs;
220 int save_nmsrs;
a2fa3e9f 221#ifdef CONFIG_X86_64
44ea2b17
AK
222 u64 msr_host_kernel_gs_base;
223 u64 msr_guest_kernel_gs_base;
a2fa3e9f 224#endif
d462b819
NHE
225 /*
226 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
227 * non-nested (L1) guest, it always points to vmcs01. For a nested
228 * guest (L2), it points to a different VMCS.
229 */
230 struct loaded_vmcs vmcs01;
231 struct loaded_vmcs *loaded_vmcs;
232 bool __launched; /* temporary, used in vmx_vcpu_run */
61d2ef2c
AK
233 struct msr_autoload {
234 unsigned nr;
235 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
236 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
237 } msr_autoload;
a2fa3e9f
GH
238 struct {
239 int loaded;
240 u16 fs_sel, gs_sel, ldt_sel;
152d3f2f
LV
241 int gs_ldt_reload_needed;
242 int fs_reload_needed;
d77c26fc 243 } host_state;
9c8cba37 244 struct {
7ffd92c5 245 int vm86_active;
78ac8b47 246 ulong save_rflags;
7ffd92c5
AK
247 struct kvm_save_segment {
248 u16 selector;
249 unsigned long base;
250 u32 limit;
251 u32 ar;
252 } tr, es, ds, fs, gs;
9c8cba37 253 } rmode;
2fb92db1
AK
254 struct {
255 u32 bitmask; /* 4 bits per segment (1 bit per field) */
256 struct kvm_save_segment seg[8];
257 } segment_cache;
2384d2b3 258 int vpid;
04fa4d32 259 bool emulation_required;
3b86cd99
JK
260
261 /* Support for vnmi-less CPUs */
262 int soft_vnmi_blocked;
263 ktime_t entry_time;
264 s64 vnmi_blocked_time;
a0861c02 265 u32 exit_reason;
4e47c7a6
SY
266
267 bool rdtscp_enabled;
ec378aee
NHE
268
269 /* Support for a guest hypervisor (nested VMX) */
270 struct nested_vmx nested;
a2fa3e9f
GH
271};
272
2fb92db1
AK
273enum segment_cache_field {
274 SEG_FIELD_SEL = 0,
275 SEG_FIELD_BASE = 1,
276 SEG_FIELD_LIMIT = 2,
277 SEG_FIELD_AR = 3,
278
279 SEG_FIELD_NR = 4
280};
281
a2fa3e9f
GH
282static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
283{
fb3f0f51 284 return container_of(vcpu, struct vcpu_vmx, vcpu);
a2fa3e9f
GH
285}
286
a9d30f33
NHE
287static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
288{
289 return to_vmx(vcpu)->nested.current_vmcs12;
290}
291
292static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
293{
294 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
295 if (is_error_page(page)) {
296 kvm_release_page_clean(page);
297 return NULL;
298 }
299 return page;
300}
301
302static void nested_release_page(struct page *page)
303{
304 kvm_release_page_dirty(page);
305}
306
307static void nested_release_page_clean(struct page *page)
308{
309 kvm_release_page_clean(page);
310}
311
4e1096d2 312static u64 construct_eptp(unsigned long root_hpa);
4610c9cc
DX
313static void kvm_cpu_vmxon(u64 addr);
314static void kvm_cpu_vmxoff(void);
aff48baa 315static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
776e58ea 316static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
75880a01 317
6aa8b732
AK
318static DEFINE_PER_CPU(struct vmcs *, vmxarea);
319static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
d462b819
NHE
320/*
321 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
322 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
323 */
324static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
3444d7da 325static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
6aa8b732 326
3e7c73e9
AK
327static unsigned long *vmx_io_bitmap_a;
328static unsigned long *vmx_io_bitmap_b;
5897297b
AK
329static unsigned long *vmx_msr_bitmap_legacy;
330static unsigned long *vmx_msr_bitmap_longmode;
fdef3ad1 331
110312c8
AK
332static bool cpu_has_load_ia32_efer;
333
2384d2b3
SY
334static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
335static DEFINE_SPINLOCK(vmx_vpid_lock);
336
1c3d14fe 337static struct vmcs_config {
6aa8b732
AK
338 int size;
339 int order;
340 u32 revision_id;
1c3d14fe
YS
341 u32 pin_based_exec_ctrl;
342 u32 cpu_based_exec_ctrl;
f78e0e2e 343 u32 cpu_based_2nd_exec_ctrl;
1c3d14fe
YS
344 u32 vmexit_ctrl;
345 u32 vmentry_ctrl;
346} vmcs_config;
6aa8b732 347
efff9e53 348static struct vmx_capability {
d56f546d
SY
349 u32 ept;
350 u32 vpid;
351} vmx_capability;
352
6aa8b732
AK
353#define VMX_SEGMENT_FIELD(seg) \
354 [VCPU_SREG_##seg] = { \
355 .selector = GUEST_##seg##_SELECTOR, \
356 .base = GUEST_##seg##_BASE, \
357 .limit = GUEST_##seg##_LIMIT, \
358 .ar_bytes = GUEST_##seg##_AR_BYTES, \
359 }
360
361static struct kvm_vmx_segment_field {
362 unsigned selector;
363 unsigned base;
364 unsigned limit;
365 unsigned ar_bytes;
366} kvm_vmx_segment_fields[] = {
367 VMX_SEGMENT_FIELD(CS),
368 VMX_SEGMENT_FIELD(DS),
369 VMX_SEGMENT_FIELD(ES),
370 VMX_SEGMENT_FIELD(FS),
371 VMX_SEGMENT_FIELD(GS),
372 VMX_SEGMENT_FIELD(SS),
373 VMX_SEGMENT_FIELD(TR),
374 VMX_SEGMENT_FIELD(LDTR),
375};
376
26bb0981
AK
377static u64 host_efer;
378
6de4f3ad
AK
379static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
380
4d56c8a7 381/*
8c06585d 382 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
4d56c8a7
AK
383 * away by decrementing the array size.
384 */
6aa8b732 385static const u32 vmx_msr_index[] = {
05b3e0c2 386#ifdef CONFIG_X86_64
44ea2b17 387 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
6aa8b732 388#endif
8c06585d 389 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
6aa8b732 390};
9d8f549d 391#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
6aa8b732 392
31299944 393static inline bool is_page_fault(u32 intr_info)
6aa8b732
AK
394{
395 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
396 INTR_INFO_VALID_MASK)) ==
8ab2d2e2 397 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
6aa8b732
AK
398}
399
31299944 400static inline bool is_no_device(u32 intr_info)
2ab455cc
AL
401{
402 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
403 INTR_INFO_VALID_MASK)) ==
8ab2d2e2 404 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
2ab455cc
AL
405}
406
31299944 407static inline bool is_invalid_opcode(u32 intr_info)
7aa81cc0
AL
408{
409 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
410 INTR_INFO_VALID_MASK)) ==
8ab2d2e2 411 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
7aa81cc0
AL
412}
413
31299944 414static inline bool is_external_interrupt(u32 intr_info)
6aa8b732
AK
415{
416 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
417 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
418}
419
31299944 420static inline bool is_machine_check(u32 intr_info)
a0861c02
AK
421{
422 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
423 INTR_INFO_VALID_MASK)) ==
424 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
425}
426
31299944 427static inline bool cpu_has_vmx_msr_bitmap(void)
25c5f225 428{
04547156 429 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
25c5f225
SY
430}
431
31299944 432static inline bool cpu_has_vmx_tpr_shadow(void)
6e5d865c 433{
04547156 434 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
6e5d865c
YS
435}
436
31299944 437static inline bool vm_need_tpr_shadow(struct kvm *kvm)
6e5d865c 438{
04547156 439 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
6e5d865c
YS
440}
441
31299944 442static inline bool cpu_has_secondary_exec_ctrls(void)
f78e0e2e 443{
04547156
SY
444 return vmcs_config.cpu_based_exec_ctrl &
445 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
f78e0e2e
SY
446}
447
774ead3a 448static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
f78e0e2e 449{
04547156
SY
450 return vmcs_config.cpu_based_2nd_exec_ctrl &
451 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
452}
453
454static inline bool cpu_has_vmx_flexpriority(void)
455{
456 return cpu_has_vmx_tpr_shadow() &&
457 cpu_has_vmx_virtualize_apic_accesses();
f78e0e2e
SY
458}
459
e799794e
MT
460static inline bool cpu_has_vmx_ept_execute_only(void)
461{
31299944 462 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
e799794e
MT
463}
464
465static inline bool cpu_has_vmx_eptp_uncacheable(void)
466{
31299944 467 return vmx_capability.ept & VMX_EPTP_UC_BIT;
e799794e
MT
468}
469
470static inline bool cpu_has_vmx_eptp_writeback(void)
471{
31299944 472 return vmx_capability.ept & VMX_EPTP_WB_BIT;
e799794e
MT
473}
474
475static inline bool cpu_has_vmx_ept_2m_page(void)
476{
31299944 477 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
e799794e
MT
478}
479
878403b7
SY
480static inline bool cpu_has_vmx_ept_1g_page(void)
481{
31299944 482 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
878403b7
SY
483}
484
4bc9b982
SY
485static inline bool cpu_has_vmx_ept_4levels(void)
486{
487 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
488}
489
31299944 490static inline bool cpu_has_vmx_invept_individual_addr(void)
d56f546d 491{
31299944 492 return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
d56f546d
SY
493}
494
31299944 495static inline bool cpu_has_vmx_invept_context(void)
d56f546d 496{
31299944 497 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
d56f546d
SY
498}
499
31299944 500static inline bool cpu_has_vmx_invept_global(void)
d56f546d 501{
31299944 502 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
d56f546d
SY
503}
504
518c8aee
GJ
505static inline bool cpu_has_vmx_invvpid_single(void)
506{
507 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
508}
509
b9d762fa
GJ
510static inline bool cpu_has_vmx_invvpid_global(void)
511{
512 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
513}
514
31299944 515static inline bool cpu_has_vmx_ept(void)
d56f546d 516{
04547156
SY
517 return vmcs_config.cpu_based_2nd_exec_ctrl &
518 SECONDARY_EXEC_ENABLE_EPT;
d56f546d
SY
519}
520
31299944 521static inline bool cpu_has_vmx_unrestricted_guest(void)
3a624e29
NK
522{
523 return vmcs_config.cpu_based_2nd_exec_ctrl &
524 SECONDARY_EXEC_UNRESTRICTED_GUEST;
525}
526
31299944 527static inline bool cpu_has_vmx_ple(void)
4b8d54f9
ZE
528{
529 return vmcs_config.cpu_based_2nd_exec_ctrl &
530 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
531}
532
31299944 533static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
f78e0e2e 534{
6d3e435e 535 return flexpriority_enabled && irqchip_in_kernel(kvm);
f78e0e2e
SY
536}
537
31299944 538static inline bool cpu_has_vmx_vpid(void)
2384d2b3 539{
04547156
SY
540 return vmcs_config.cpu_based_2nd_exec_ctrl &
541 SECONDARY_EXEC_ENABLE_VPID;
2384d2b3
SY
542}
543
31299944 544static inline bool cpu_has_vmx_rdtscp(void)
4e47c7a6
SY
545{
546 return vmcs_config.cpu_based_2nd_exec_ctrl &
547 SECONDARY_EXEC_RDTSCP;
548}
549
31299944 550static inline bool cpu_has_virtual_nmis(void)
f08864b4
SY
551{
552 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
553}
554
f5f48ee1
SY
555static inline bool cpu_has_vmx_wbinvd_exit(void)
556{
557 return vmcs_config.cpu_based_2nd_exec_ctrl &
558 SECONDARY_EXEC_WBINVD_EXITING;
559}
560
04547156
SY
561static inline bool report_flexpriority(void)
562{
563 return flexpriority_enabled;
564}
565
8b9cf98c 566static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
7725f0ba
AK
567{
568 int i;
569
a2fa3e9f 570 for (i = 0; i < vmx->nmsrs; ++i)
26bb0981 571 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
a75beee6
ED
572 return i;
573 return -1;
574}
575
2384d2b3
SY
576static inline void __invvpid(int ext, u16 vpid, gva_t gva)
577{
578 struct {
579 u64 vpid : 16;
580 u64 rsvd : 48;
581 u64 gva;
582 } operand = { vpid, 0, gva };
583
4ecac3fd 584 asm volatile (__ex(ASM_VMX_INVVPID)
2384d2b3
SY
585 /* CF==1 or ZF==1 --> rc = -1 */
586 "; ja 1f ; ud2 ; 1:"
587 : : "a"(&operand), "c"(ext) : "cc", "memory");
588}
589
1439442c
SY
590static inline void __invept(int ext, u64 eptp, gpa_t gpa)
591{
592 struct {
593 u64 eptp, gpa;
594 } operand = {eptp, gpa};
595
4ecac3fd 596 asm volatile (__ex(ASM_VMX_INVEPT)
1439442c
SY
597 /* CF==1 or ZF==1 --> rc = -1 */
598 "; ja 1f ; ud2 ; 1:\n"
599 : : "a" (&operand), "c" (ext) : "cc", "memory");
600}
601
26bb0981 602static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
a75beee6
ED
603{
604 int i;
605
8b9cf98c 606 i = __find_msr_index(vmx, msr);
a75beee6 607 if (i >= 0)
a2fa3e9f 608 return &vmx->guest_msrs[i];
8b6d44c7 609 return NULL;
7725f0ba
AK
610}
611
6aa8b732
AK
612static void vmcs_clear(struct vmcs *vmcs)
613{
614 u64 phys_addr = __pa(vmcs);
615 u8 error;
616
4ecac3fd 617 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
16d8f72f 618 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
6aa8b732
AK
619 : "cc", "memory");
620 if (error)
621 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
622 vmcs, phys_addr);
623}
624
d462b819
NHE
625static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
626{
627 vmcs_clear(loaded_vmcs->vmcs);
628 loaded_vmcs->cpu = -1;
629 loaded_vmcs->launched = 0;
630}
631
7725b894
DX
632static void vmcs_load(struct vmcs *vmcs)
633{
634 u64 phys_addr = __pa(vmcs);
635 u8 error;
636
637 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
16d8f72f 638 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
7725b894
DX
639 : "cc", "memory");
640 if (error)
641 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
642 vmcs, phys_addr);
643}
644
d462b819 645static void __loaded_vmcs_clear(void *arg)
6aa8b732 646{
d462b819 647 struct loaded_vmcs *loaded_vmcs = arg;
d3b2c338 648 int cpu = raw_smp_processor_id();
6aa8b732 649
d462b819
NHE
650 if (loaded_vmcs->cpu != cpu)
651 return; /* vcpu migration can race with cpu offline */
652 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
6aa8b732 653 per_cpu(current_vmcs, cpu) = NULL;
d462b819
NHE
654 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
655 loaded_vmcs_init(loaded_vmcs);
6aa8b732
AK
656}
657
d462b819 658static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
8d0be2b3 659{
d462b819
NHE
660 if (loaded_vmcs->cpu != -1)
661 smp_call_function_single(
662 loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1);
8d0be2b3
AK
663}
664
1760dd49 665static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
2384d2b3
SY
666{
667 if (vmx->vpid == 0)
668 return;
669
518c8aee
GJ
670 if (cpu_has_vmx_invvpid_single())
671 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
2384d2b3
SY
672}
673
b9d762fa
GJ
674static inline void vpid_sync_vcpu_global(void)
675{
676 if (cpu_has_vmx_invvpid_global())
677 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
678}
679
680static inline void vpid_sync_context(struct vcpu_vmx *vmx)
681{
682 if (cpu_has_vmx_invvpid_single())
1760dd49 683 vpid_sync_vcpu_single(vmx);
b9d762fa
GJ
684 else
685 vpid_sync_vcpu_global();
686}
687
1439442c
SY
688static inline void ept_sync_global(void)
689{
690 if (cpu_has_vmx_invept_global())
691 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
692}
693
694static inline void ept_sync_context(u64 eptp)
695{
089d034e 696 if (enable_ept) {
1439442c
SY
697 if (cpu_has_vmx_invept_context())
698 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
699 else
700 ept_sync_global();
701 }
702}
703
704static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
705{
089d034e 706 if (enable_ept) {
1439442c
SY
707 if (cpu_has_vmx_invept_individual_addr())
708 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
709 eptp, gpa);
710 else
711 ept_sync_context(eptp);
712 }
713}
714
96304217 715static __always_inline unsigned long vmcs_readl(unsigned long field)
6aa8b732 716{
5e520e62 717 unsigned long value;
6aa8b732 718
5e520e62
AK
719 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
720 : "=a"(value) : "d"(field) : "cc");
6aa8b732
AK
721 return value;
722}
723
96304217 724static __always_inline u16 vmcs_read16(unsigned long field)
6aa8b732
AK
725{
726 return vmcs_readl(field);
727}
728
96304217 729static __always_inline u32 vmcs_read32(unsigned long field)
6aa8b732
AK
730{
731 return vmcs_readl(field);
732}
733
96304217 734static __always_inline u64 vmcs_read64(unsigned long field)
6aa8b732 735{
05b3e0c2 736#ifdef CONFIG_X86_64
6aa8b732
AK
737 return vmcs_readl(field);
738#else
739 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
740#endif
741}
742
e52de1b8
AK
743static noinline void vmwrite_error(unsigned long field, unsigned long value)
744{
745 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
746 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
747 dump_stack();
748}
749
6aa8b732
AK
750static void vmcs_writel(unsigned long field, unsigned long value)
751{
752 u8 error;
753
4ecac3fd 754 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
d77c26fc 755 : "=q"(error) : "a"(value), "d"(field) : "cc");
e52de1b8
AK
756 if (unlikely(error))
757 vmwrite_error(field, value);
6aa8b732
AK
758}
759
760static void vmcs_write16(unsigned long field, u16 value)
761{
762 vmcs_writel(field, value);
763}
764
765static void vmcs_write32(unsigned long field, u32 value)
766{
767 vmcs_writel(field, value);
768}
769
770static void vmcs_write64(unsigned long field, u64 value)
771{
6aa8b732 772 vmcs_writel(field, value);
7682f2d0 773#ifndef CONFIG_X86_64
6aa8b732
AK
774 asm volatile ("");
775 vmcs_writel(field+1, value >> 32);
776#endif
777}
778
2ab455cc
AL
779static void vmcs_clear_bits(unsigned long field, u32 mask)
780{
781 vmcs_writel(field, vmcs_readl(field) & ~mask);
782}
783
784static void vmcs_set_bits(unsigned long field, u32 mask)
785{
786 vmcs_writel(field, vmcs_readl(field) | mask);
787}
788
2fb92db1
AK
789static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
790{
791 vmx->segment_cache.bitmask = 0;
792}
793
794static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
795 unsigned field)
796{
797 bool ret;
798 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
799
800 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
801 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
802 vmx->segment_cache.bitmask = 0;
803 }
804 ret = vmx->segment_cache.bitmask & mask;
805 vmx->segment_cache.bitmask |= mask;
806 return ret;
807}
808
809static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
810{
811 u16 *p = &vmx->segment_cache.seg[seg].selector;
812
813 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
814 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
815 return *p;
816}
817
818static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
819{
820 ulong *p = &vmx->segment_cache.seg[seg].base;
821
822 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
823 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
824 return *p;
825}
826
827static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
828{
829 u32 *p = &vmx->segment_cache.seg[seg].limit;
830
831 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
832 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
833 return *p;
834}
835
836static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
837{
838 u32 *p = &vmx->segment_cache.seg[seg].ar;
839
840 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
841 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
842 return *p;
843}
844
abd3f2d6
AK
845static void update_exception_bitmap(struct kvm_vcpu *vcpu)
846{
847 u32 eb;
848
fd7373cc
JK
849 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
850 (1u << NM_VECTOR) | (1u << DB_VECTOR);
851 if ((vcpu->guest_debug &
852 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
853 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
854 eb |= 1u << BP_VECTOR;
7ffd92c5 855 if (to_vmx(vcpu)->rmode.vm86_active)
abd3f2d6 856 eb = ~0;
089d034e 857 if (enable_ept)
1439442c 858 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
02daab21
AK
859 if (vcpu->fpu_active)
860 eb &= ~(1u << NM_VECTOR);
abd3f2d6
AK
861 vmcs_write32(EXCEPTION_BITMAP, eb);
862}
863
61d2ef2c
AK
864static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
865{
866 unsigned i;
867 struct msr_autoload *m = &vmx->msr_autoload;
868
110312c8
AK
869 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
870 vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
871 vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
872 return;
873 }
874
61d2ef2c
AK
875 for (i = 0; i < m->nr; ++i)
876 if (m->guest[i].index == msr)
877 break;
878
879 if (i == m->nr)
880 return;
881 --m->nr;
882 m->guest[i] = m->guest[m->nr];
883 m->host[i] = m->host[m->nr];
884 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
885 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
886}
887
888static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
889 u64 guest_val, u64 host_val)
890{
891 unsigned i;
892 struct msr_autoload *m = &vmx->msr_autoload;
893
110312c8
AK
894 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
895 vmcs_write64(GUEST_IA32_EFER, guest_val);
896 vmcs_write64(HOST_IA32_EFER, host_val);
897 vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
898 vmcs_set_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
899 return;
900 }
901
61d2ef2c
AK
902 for (i = 0; i < m->nr; ++i)
903 if (m->guest[i].index == msr)
904 break;
905
906 if (i == m->nr) {
907 ++m->nr;
908 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
909 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
910 }
911
912 m->guest[i].index = msr;
913 m->guest[i].value = guest_val;
914 m->host[i].index = msr;
915 m->host[i].value = host_val;
916}
917
33ed6329
AK
918static void reload_tss(void)
919{
33ed6329
AK
920 /*
921 * VT restores TR but not its size. Useless.
922 */
d359192f 923 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
a5f61300 924 struct desc_struct *descs;
33ed6329 925
d359192f 926 descs = (void *)gdt->address;
33ed6329
AK
927 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
928 load_TR_desc();
33ed6329
AK
929}
930
92c0d900 931static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2cc51560 932{
3a34a881 933 u64 guest_efer;
51c6cf66
AK
934 u64 ignore_bits;
935
f6801dff 936 guest_efer = vmx->vcpu.arch.efer;
3a34a881 937
51c6cf66
AK
938 /*
939 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
940 * outside long mode
941 */
942 ignore_bits = EFER_NX | EFER_SCE;
943#ifdef CONFIG_X86_64
944 ignore_bits |= EFER_LMA | EFER_LME;
945 /* SCE is meaningful only in long mode on Intel */
946 if (guest_efer & EFER_LMA)
947 ignore_bits &= ~(u64)EFER_SCE;
948#endif
51c6cf66
AK
949 guest_efer &= ~ignore_bits;
950 guest_efer |= host_efer & ignore_bits;
26bb0981 951 vmx->guest_msrs[efer_offset].data = guest_efer;
d5696725 952 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
84ad33ef
AK
953
954 clear_atomic_switch_msr(vmx, MSR_EFER);
955 /* On ept, can't emulate nx, and must switch nx atomically */
956 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
957 guest_efer = vmx->vcpu.arch.efer;
958 if (!(guest_efer & EFER_LMA))
959 guest_efer &= ~EFER_LME;
960 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
961 return false;
962 }
963
26bb0981 964 return true;
51c6cf66
AK
965}
966
2d49ec72
GN
967static unsigned long segment_base(u16 selector)
968{
d359192f 969 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
2d49ec72
GN
970 struct desc_struct *d;
971 unsigned long table_base;
972 unsigned long v;
973
974 if (!(selector & ~3))
975 return 0;
976
d359192f 977 table_base = gdt->address;
2d49ec72
GN
978
979 if (selector & 4) { /* from ldt */
980 u16 ldt_selector = kvm_read_ldt();
981
982 if (!(ldt_selector & ~3))
983 return 0;
984
985 table_base = segment_base(ldt_selector);
986 }
987 d = (struct desc_struct *)(table_base + (selector & ~7));
988 v = get_desc_base(d);
989#ifdef CONFIG_X86_64
990 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
991 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
992#endif
993 return v;
994}
995
996static inline unsigned long kvm_read_tr_base(void)
997{
998 u16 tr;
999 asm("str %0" : "=g"(tr));
1000 return segment_base(tr);
1001}
1002
04d2cc77 1003static void vmx_save_host_state(struct kvm_vcpu *vcpu)
33ed6329 1004{
04d2cc77 1005 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981 1006 int i;
04d2cc77 1007
a2fa3e9f 1008 if (vmx->host_state.loaded)
33ed6329
AK
1009 return;
1010
a2fa3e9f 1011 vmx->host_state.loaded = 1;
33ed6329
AK
1012 /*
1013 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1014 * allow segment selectors with cpl > 0 or ti == 1.
1015 */
d6e88aec 1016 vmx->host_state.ldt_sel = kvm_read_ldt();
152d3f2f 1017 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
9581d442 1018 savesegment(fs, vmx->host_state.fs_sel);
152d3f2f 1019 if (!(vmx->host_state.fs_sel & 7)) {
a2fa3e9f 1020 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
152d3f2f
LV
1021 vmx->host_state.fs_reload_needed = 0;
1022 } else {
33ed6329 1023 vmcs_write16(HOST_FS_SELECTOR, 0);
152d3f2f 1024 vmx->host_state.fs_reload_needed = 1;
33ed6329 1025 }
9581d442 1026 savesegment(gs, vmx->host_state.gs_sel);
a2fa3e9f
GH
1027 if (!(vmx->host_state.gs_sel & 7))
1028 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
33ed6329
AK
1029 else {
1030 vmcs_write16(HOST_GS_SELECTOR, 0);
152d3f2f 1031 vmx->host_state.gs_ldt_reload_needed = 1;
33ed6329
AK
1032 }
1033
1034#ifdef CONFIG_X86_64
1035 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1036 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1037#else
a2fa3e9f
GH
1038 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1039 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
33ed6329 1040#endif
707c0874
AK
1041
1042#ifdef CONFIG_X86_64
c8770e7b
AK
1043 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1044 if (is_long_mode(&vmx->vcpu))
44ea2b17 1045 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
707c0874 1046#endif
26bb0981
AK
1047 for (i = 0; i < vmx->save_nmsrs; ++i)
1048 kvm_set_shared_msr(vmx->guest_msrs[i].index,
d5696725
AK
1049 vmx->guest_msrs[i].data,
1050 vmx->guest_msrs[i].mask);
33ed6329
AK
1051}
1052
a9b21b62 1053static void __vmx_load_host_state(struct vcpu_vmx *vmx)
33ed6329 1054{
a2fa3e9f 1055 if (!vmx->host_state.loaded)
33ed6329
AK
1056 return;
1057
e1beb1d3 1058 ++vmx->vcpu.stat.host_state_reload;
a2fa3e9f 1059 vmx->host_state.loaded = 0;
c8770e7b
AK
1060#ifdef CONFIG_X86_64
1061 if (is_long_mode(&vmx->vcpu))
1062 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1063#endif
152d3f2f 1064 if (vmx->host_state.gs_ldt_reload_needed) {
d6e88aec 1065 kvm_load_ldt(vmx->host_state.ldt_sel);
33ed6329 1066#ifdef CONFIG_X86_64
9581d442 1067 load_gs_index(vmx->host_state.gs_sel);
9581d442
AK
1068#else
1069 loadsegment(gs, vmx->host_state.gs_sel);
33ed6329 1070#endif
33ed6329 1071 }
0a77fe4c
AK
1072 if (vmx->host_state.fs_reload_needed)
1073 loadsegment(fs, vmx->host_state.fs_sel);
152d3f2f 1074 reload_tss();
44ea2b17 1075#ifdef CONFIG_X86_64
c8770e7b 1076 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
44ea2b17 1077#endif
1c11e713
AK
1078 if (current_thread_info()->status & TS_USEDFPU)
1079 clts();
3444d7da 1080 load_gdt(&__get_cpu_var(host_gdt));
33ed6329
AK
1081}
1082
a9b21b62
AK
1083static void vmx_load_host_state(struct vcpu_vmx *vmx)
1084{
1085 preempt_disable();
1086 __vmx_load_host_state(vmx);
1087 preempt_enable();
1088}
1089
6aa8b732
AK
1090/*
1091 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1092 * vcpu mutex is already taken.
1093 */
15ad7146 1094static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
6aa8b732 1095{
a2fa3e9f 1096 struct vcpu_vmx *vmx = to_vmx(vcpu);
4610c9cc 1097 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
6aa8b732 1098
4610c9cc
DX
1099 if (!vmm_exclusive)
1100 kvm_cpu_vmxon(phys_addr);
d462b819
NHE
1101 else if (vmx->loaded_vmcs->cpu != cpu)
1102 loaded_vmcs_clear(vmx->loaded_vmcs);
6aa8b732 1103
d462b819
NHE
1104 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1105 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1106 vmcs_load(vmx->loaded_vmcs->vmcs);
6aa8b732
AK
1107 }
1108
d462b819 1109 if (vmx->loaded_vmcs->cpu != cpu) {
d359192f 1110 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
6aa8b732
AK
1111 unsigned long sysenter_esp;
1112
a8eeb04a 1113 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
92fe13be 1114 local_irq_disable();
d462b819
NHE
1115 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1116 &per_cpu(loaded_vmcss_on_cpu, cpu));
92fe13be
DX
1117 local_irq_enable();
1118
6aa8b732
AK
1119 /*
1120 * Linux uses per-cpu TSS and GDT, so set these when switching
1121 * processors.
1122 */
d6e88aec 1123 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
d359192f 1124 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
6aa8b732
AK
1125
1126 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1127 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
d462b819 1128 vmx->loaded_vmcs->cpu = cpu;
6aa8b732 1129 }
6aa8b732
AK
1130}
1131
1132static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1133{
a9b21b62 1134 __vmx_load_host_state(to_vmx(vcpu));
4610c9cc 1135 if (!vmm_exclusive) {
d462b819
NHE
1136 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1137 vcpu->cpu = -1;
4610c9cc
DX
1138 kvm_cpu_vmxoff();
1139 }
6aa8b732
AK
1140}
1141
5fd86fcf
AK
1142static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1143{
81231c69
AK
1144 ulong cr0;
1145
5fd86fcf
AK
1146 if (vcpu->fpu_active)
1147 return;
1148 vcpu->fpu_active = 1;
81231c69
AK
1149 cr0 = vmcs_readl(GUEST_CR0);
1150 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1151 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1152 vmcs_writel(GUEST_CR0, cr0);
5fd86fcf 1153 update_exception_bitmap(vcpu);
edcafe3c
AK
1154 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
1155 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
5fd86fcf
AK
1156}
1157
edcafe3c
AK
1158static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1159
5fd86fcf
AK
1160static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1161{
edcafe3c 1162 vmx_decache_cr0_guest_bits(vcpu);
81231c69 1163 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
5fd86fcf 1164 update_exception_bitmap(vcpu);
edcafe3c
AK
1165 vcpu->arch.cr0_guest_owned_bits = 0;
1166 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
1167 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
5fd86fcf
AK
1168}
1169
6aa8b732
AK
1170static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1171{
78ac8b47 1172 unsigned long rflags, save_rflags;
345dcaa8 1173
6de12732
AK
1174 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1175 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1176 rflags = vmcs_readl(GUEST_RFLAGS);
1177 if (to_vmx(vcpu)->rmode.vm86_active) {
1178 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1179 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1180 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1181 }
1182 to_vmx(vcpu)->rflags = rflags;
78ac8b47 1183 }
6de12732 1184 return to_vmx(vcpu)->rflags;
6aa8b732
AK
1185}
1186
1187static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1188{
6de12732 1189 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
69c73028 1190 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
6de12732 1191 to_vmx(vcpu)->rflags = rflags;
78ac8b47
AK
1192 if (to_vmx(vcpu)->rmode.vm86_active) {
1193 to_vmx(vcpu)->rmode.save_rflags = rflags;
053de044 1194 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
78ac8b47 1195 }
6aa8b732
AK
1196 vmcs_writel(GUEST_RFLAGS, rflags);
1197}
1198
2809f5d2
GC
1199static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1200{
1201 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1202 int ret = 0;
1203
1204 if (interruptibility & GUEST_INTR_STATE_STI)
48005f64 1205 ret |= KVM_X86_SHADOW_INT_STI;
2809f5d2 1206 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
48005f64 1207 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2809f5d2
GC
1208
1209 return ret & mask;
1210}
1211
1212static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1213{
1214 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1215 u32 interruptibility = interruptibility_old;
1216
1217 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1218
48005f64 1219 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2809f5d2 1220 interruptibility |= GUEST_INTR_STATE_MOV_SS;
48005f64 1221 else if (mask & KVM_X86_SHADOW_INT_STI)
2809f5d2
GC
1222 interruptibility |= GUEST_INTR_STATE_STI;
1223
1224 if ((interruptibility != interruptibility_old))
1225 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1226}
1227
6aa8b732
AK
1228static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1229{
1230 unsigned long rip;
6aa8b732 1231
5fdbf976 1232 rip = kvm_rip_read(vcpu);
6aa8b732 1233 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5fdbf976 1234 kvm_rip_write(vcpu, rip);
6aa8b732 1235
2809f5d2
GC
1236 /* skipping an emulated instruction also counts */
1237 vmx_set_interrupt_shadow(vcpu, 0);
6aa8b732
AK
1238}
1239
443381a8
AL
1240static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1241{
1242 /* Ensure that we clear the HLT state in the VMCS. We don't need to
1243 * explicitly skip the instruction because if the HLT state is set, then
1244 * the instruction is already executing and RIP has already been
1245 * advanced. */
1246 if (!yield_on_hlt &&
1247 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1248 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1249}
1250
298101da 1251static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
ce7ddec4
JR
1252 bool has_error_code, u32 error_code,
1253 bool reinject)
298101da 1254{
77ab6db0 1255 struct vcpu_vmx *vmx = to_vmx(vcpu);
8ab2d2e2 1256 u32 intr_info = nr | INTR_INFO_VALID_MASK;
77ab6db0 1257
8ab2d2e2 1258 if (has_error_code) {
77ab6db0 1259 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
8ab2d2e2
JK
1260 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1261 }
77ab6db0 1262
7ffd92c5 1263 if (vmx->rmode.vm86_active) {
71f9833b
SH
1264 int inc_eip = 0;
1265 if (kvm_exception_is_soft(nr))
1266 inc_eip = vcpu->arch.event_exit_inst_len;
1267 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
a92601bb 1268 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
77ab6db0
JK
1269 return;
1270 }
1271
66fd3f7f
GN
1272 if (kvm_exception_is_soft(nr)) {
1273 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1274 vmx->vcpu.arch.event_exit_inst_len);
8ab2d2e2
JK
1275 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1276 } else
1277 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1278
1279 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
443381a8 1280 vmx_clear_hlt(vcpu);
298101da
AK
1281}
1282
4e47c7a6
SY
1283static bool vmx_rdtscp_supported(void)
1284{
1285 return cpu_has_vmx_rdtscp();
1286}
1287
a75beee6
ED
1288/*
1289 * Swap MSR entry in host/guest MSR entry array.
1290 */
8b9cf98c 1291static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
a75beee6 1292{
26bb0981 1293 struct shared_msr_entry tmp;
a2fa3e9f
GH
1294
1295 tmp = vmx->guest_msrs[to];
1296 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1297 vmx->guest_msrs[from] = tmp;
a75beee6
ED
1298}
1299
e38aea3e
AK
1300/*
1301 * Set up the vmcs to automatically save and restore system
1302 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1303 * mode, as fiddling with msrs is very expensive.
1304 */
8b9cf98c 1305static void setup_msrs(struct vcpu_vmx *vmx)
e38aea3e 1306{
26bb0981 1307 int save_nmsrs, index;
5897297b 1308 unsigned long *msr_bitmap;
e38aea3e 1309
33f9c505 1310 vmx_load_host_state(vmx);
a75beee6
ED
1311 save_nmsrs = 0;
1312#ifdef CONFIG_X86_64
8b9cf98c 1313 if (is_long_mode(&vmx->vcpu)) {
8b9cf98c 1314 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
a75beee6 1315 if (index >= 0)
8b9cf98c
RR
1316 move_msr_up(vmx, index, save_nmsrs++);
1317 index = __find_msr_index(vmx, MSR_LSTAR);
a75beee6 1318 if (index >= 0)
8b9cf98c
RR
1319 move_msr_up(vmx, index, save_nmsrs++);
1320 index = __find_msr_index(vmx, MSR_CSTAR);
a75beee6 1321 if (index >= 0)
8b9cf98c 1322 move_msr_up(vmx, index, save_nmsrs++);
4e47c7a6
SY
1323 index = __find_msr_index(vmx, MSR_TSC_AUX);
1324 if (index >= 0 && vmx->rdtscp_enabled)
1325 move_msr_up(vmx, index, save_nmsrs++);
a75beee6 1326 /*
8c06585d 1327 * MSR_STAR is only needed on long mode guests, and only
a75beee6
ED
1328 * if efer.sce is enabled.
1329 */
8c06585d 1330 index = __find_msr_index(vmx, MSR_STAR);
f6801dff 1331 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
8b9cf98c 1332 move_msr_up(vmx, index, save_nmsrs++);
a75beee6
ED
1333 }
1334#endif
92c0d900
AK
1335 index = __find_msr_index(vmx, MSR_EFER);
1336 if (index >= 0 && update_transition_efer(vmx, index))
26bb0981 1337 move_msr_up(vmx, index, save_nmsrs++);
e38aea3e 1338
26bb0981 1339 vmx->save_nmsrs = save_nmsrs;
5897297b
AK
1340
1341 if (cpu_has_vmx_msr_bitmap()) {
1342 if (is_long_mode(&vmx->vcpu))
1343 msr_bitmap = vmx_msr_bitmap_longmode;
1344 else
1345 msr_bitmap = vmx_msr_bitmap_legacy;
1346
1347 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1348 }
e38aea3e
AK
1349}
1350
6aa8b732
AK
1351/*
1352 * reads and returns guest's timestamp counter "register"
1353 * guest_tsc = host_tsc + tsc_offset -- 21.3
1354 */
1355static u64 guest_read_tsc(void)
1356{
1357 u64 host_tsc, tsc_offset;
1358
1359 rdtscll(host_tsc);
1360 tsc_offset = vmcs_read64(TSC_OFFSET);
1361 return host_tsc + tsc_offset;
1362}
1363
4051b188
JR
1364/*
1365 * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ
1366 * ioctl. In this case the call-back should update internal vmx state to make
1367 * the changes effective.
1368 */
1369static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
1370{
1371 /* Nothing to do here */
1372}
1373
6aa8b732 1374/*
99e3e30a 1375 * writes 'offset' into guest's timestamp counter offset register
6aa8b732 1376 */
99e3e30a 1377static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
6aa8b732 1378{
f4e1b3c8 1379 vmcs_write64(TSC_OFFSET, offset);
6aa8b732
AK
1380}
1381
e48672fa
ZA
1382static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
1383{
1384 u64 offset = vmcs_read64(TSC_OFFSET);
1385 vmcs_write64(TSC_OFFSET, offset + adjustment);
1386}
1387
857e4099
JR
1388static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1389{
1390 return target_tsc - native_read_tsc();
1391}
1392
801d3424
NHE
1393static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
1394{
1395 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
1396 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
1397}
1398
1399/*
1400 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1401 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1402 * all guests if the "nested" module option is off, and can also be disabled
1403 * for a single guest by disabling its VMX cpuid bit.
1404 */
1405static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
1406{
1407 return nested && guest_cpuid_has_vmx(vcpu);
1408}
1409
b87a51ae
NHE
1410/*
1411 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
1412 * returned for the various VMX controls MSRs when nested VMX is enabled.
1413 * The same values should also be used to verify that vmcs12 control fields are
1414 * valid during nested entry from L1 to L2.
1415 * Each of these control msrs has a low and high 32-bit half: A low bit is on
1416 * if the corresponding bit in the (32-bit) control field *must* be on, and a
1417 * bit in the high half is on if the corresponding bit in the control field
1418 * may be on. See also vmx_control_verify().
1419 * TODO: allow these variables to be modified (downgraded) by module options
1420 * or other means.
1421 */
1422static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
1423static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
1424static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
1425static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
1426static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
1427static __init void nested_vmx_setup_ctls_msrs(void)
1428{
1429 /*
1430 * Note that as a general rule, the high half of the MSRs (bits in
1431 * the control fields which may be 1) should be initialized by the
1432 * intersection of the underlying hardware's MSR (i.e., features which
1433 * can be supported) and the list of features we want to expose -
1434 * because they are known to be properly supported in our code.
1435 * Also, usually, the low half of the MSRs (bits which must be 1) can
1436 * be set to 0, meaning that L1 may turn off any of these bits. The
1437 * reason is that if one of these bits is necessary, it will appear
1438 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
1439 * fields of vmcs01 and vmcs02, will turn these bits off - and
1440 * nested_vmx_exit_handled() will not pass related exits to L1.
1441 * These rules have exceptions below.
1442 */
1443
1444 /* pin-based controls */
1445 /*
1446 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
1447 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
1448 */
1449 nested_vmx_pinbased_ctls_low = 0x16 ;
1450 nested_vmx_pinbased_ctls_high = 0x16 |
1451 PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
1452 PIN_BASED_VIRTUAL_NMIS;
1453
1454 /* exit controls */
1455 nested_vmx_exit_ctls_low = 0;
1456#ifdef CONFIG_X86_64
1457 nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
1458#else
1459 nested_vmx_exit_ctls_high = 0;
1460#endif
1461
1462 /* entry controls */
1463 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
1464 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
1465 nested_vmx_entry_ctls_low = 0;
1466 nested_vmx_entry_ctls_high &=
1467 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
1468
1469 /* cpu-based controls */
1470 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
1471 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
1472 nested_vmx_procbased_ctls_low = 0;
1473 nested_vmx_procbased_ctls_high &=
1474 CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
1475 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
1476 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
1477 CPU_BASED_CR3_STORE_EXITING |
1478#ifdef CONFIG_X86_64
1479 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
1480#endif
1481 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
1482 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
1483 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1484 /*
1485 * We can allow some features even when not supported by the
1486 * hardware. For example, L1 can specify an MSR bitmap - and we
1487 * can use it to avoid exits to L1 - even when L0 runs L2
1488 * without MSR bitmaps.
1489 */
1490 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
1491
1492 /* secondary cpu-based controls */
1493 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
1494 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
1495 nested_vmx_secondary_ctls_low = 0;
1496 nested_vmx_secondary_ctls_high &=
1497 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1498}
1499
1500static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
1501{
1502 /*
1503 * Bits 0 in high must be 0, and bits 1 in low must be 1.
1504 */
1505 return ((control & high) | low) == control;
1506}
1507
1508static inline u64 vmx_control_msr(u32 low, u32 high)
1509{
1510 return low | ((u64)high << 32);
1511}
1512
1513/*
1514 * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
1515 * also let it use VMX-specific MSRs.
1516 * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
1517 * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
1518 * like all other MSRs).
1519 */
1520static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1521{
1522 if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
1523 msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
1524 /*
1525 * According to the spec, processors which do not support VMX
1526 * should throw a #GP(0) when VMX capability MSRs are read.
1527 */
1528 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
1529 return 1;
1530 }
1531
1532 switch (msr_index) {
1533 case MSR_IA32_FEATURE_CONTROL:
1534 *pdata = 0;
1535 break;
1536 case MSR_IA32_VMX_BASIC:
1537 /*
1538 * This MSR reports some information about VMX support. We
1539 * should return information about the VMX we emulate for the
1540 * guest, and the VMCS structure we give it - not about the
1541 * VMX support of the underlying hardware.
1542 */
1543 *pdata = VMCS12_REVISION |
1544 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
1545 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
1546 break;
1547 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1548 case MSR_IA32_VMX_PINBASED_CTLS:
1549 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
1550 nested_vmx_pinbased_ctls_high);
1551 break;
1552 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1553 case MSR_IA32_VMX_PROCBASED_CTLS:
1554 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
1555 nested_vmx_procbased_ctls_high);
1556 break;
1557 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1558 case MSR_IA32_VMX_EXIT_CTLS:
1559 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
1560 nested_vmx_exit_ctls_high);
1561 break;
1562 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1563 case MSR_IA32_VMX_ENTRY_CTLS:
1564 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
1565 nested_vmx_entry_ctls_high);
1566 break;
1567 case MSR_IA32_VMX_MISC:
1568 *pdata = 0;
1569 break;
1570 /*
1571 * These MSRs specify bits which the guest must keep fixed (on or off)
1572 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
1573 * We picked the standard core2 setting.
1574 */
1575#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
1576#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
1577 case MSR_IA32_VMX_CR0_FIXED0:
1578 *pdata = VMXON_CR0_ALWAYSON;
1579 break;
1580 case MSR_IA32_VMX_CR0_FIXED1:
1581 *pdata = -1ULL;
1582 break;
1583 case MSR_IA32_VMX_CR4_FIXED0:
1584 *pdata = VMXON_CR4_ALWAYSON;
1585 break;
1586 case MSR_IA32_VMX_CR4_FIXED1:
1587 *pdata = -1ULL;
1588 break;
1589 case MSR_IA32_VMX_VMCS_ENUM:
1590 *pdata = 0x1f;
1591 break;
1592 case MSR_IA32_VMX_PROCBASED_CTLS2:
1593 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
1594 nested_vmx_secondary_ctls_high);
1595 break;
1596 case MSR_IA32_VMX_EPT_VPID_CAP:
1597 /* Currently, no nested ept or nested vpid */
1598 *pdata = 0;
1599 break;
1600 default:
1601 return 0;
1602 }
1603
1604 return 1;
1605}
1606
1607static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1608{
1609 if (!nested_vmx_allowed(vcpu))
1610 return 0;
1611
1612 if (msr_index == MSR_IA32_FEATURE_CONTROL)
1613 /* TODO: the right thing. */
1614 return 1;
1615 /*
1616 * No need to treat VMX capability MSRs specially: If we don't handle
1617 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
1618 */
1619 return 0;
1620}
1621
6aa8b732
AK
1622/*
1623 * Reads an msr value (of 'msr_index') into 'pdata'.
1624 * Returns 0 on success, non-0 otherwise.
1625 * Assumes vcpu_load() was already called.
1626 */
1627static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1628{
1629 u64 data;
26bb0981 1630 struct shared_msr_entry *msr;
6aa8b732
AK
1631
1632 if (!pdata) {
1633 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1634 return -EINVAL;
1635 }
1636
1637 switch (msr_index) {
05b3e0c2 1638#ifdef CONFIG_X86_64
6aa8b732
AK
1639 case MSR_FS_BASE:
1640 data = vmcs_readl(GUEST_FS_BASE);
1641 break;
1642 case MSR_GS_BASE:
1643 data = vmcs_readl(GUEST_GS_BASE);
1644 break;
44ea2b17
AK
1645 case MSR_KERNEL_GS_BASE:
1646 vmx_load_host_state(to_vmx(vcpu));
1647 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1648 break;
26bb0981 1649#endif
6aa8b732 1650 case MSR_EFER:
3bab1f5d 1651 return kvm_get_msr_common(vcpu, msr_index, pdata);
af24a4e4 1652 case MSR_IA32_TSC:
6aa8b732
AK
1653 data = guest_read_tsc();
1654 break;
1655 case MSR_IA32_SYSENTER_CS:
1656 data = vmcs_read32(GUEST_SYSENTER_CS);
1657 break;
1658 case MSR_IA32_SYSENTER_EIP:
f5b42c33 1659 data = vmcs_readl(GUEST_SYSENTER_EIP);
6aa8b732
AK
1660 break;
1661 case MSR_IA32_SYSENTER_ESP:
f5b42c33 1662 data = vmcs_readl(GUEST_SYSENTER_ESP);
6aa8b732 1663 break;
4e47c7a6
SY
1664 case MSR_TSC_AUX:
1665 if (!to_vmx(vcpu)->rdtscp_enabled)
1666 return 1;
1667 /* Otherwise falls through */
6aa8b732 1668 default:
26bb0981 1669 vmx_load_host_state(to_vmx(vcpu));
b87a51ae
NHE
1670 if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
1671 return 0;
8b9cf98c 1672 msr = find_msr_entry(to_vmx(vcpu), msr_index);
3bab1f5d 1673 if (msr) {
542423b0 1674 vmx_load_host_state(to_vmx(vcpu));
3bab1f5d
AK
1675 data = msr->data;
1676 break;
6aa8b732 1677 }
3bab1f5d 1678 return kvm_get_msr_common(vcpu, msr_index, pdata);
6aa8b732
AK
1679 }
1680
1681 *pdata = data;
1682 return 0;
1683}
1684
1685/*
1686 * Writes msr value into into the appropriate "register".
1687 * Returns 0 on success, non-0 otherwise.
1688 * Assumes vcpu_load() was already called.
1689 */
1690static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1691{
a2fa3e9f 1692 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981 1693 struct shared_msr_entry *msr;
2cc51560
ED
1694 int ret = 0;
1695
6aa8b732 1696 switch (msr_index) {
3bab1f5d 1697 case MSR_EFER:
a9b21b62 1698 vmx_load_host_state(vmx);
2cc51560 1699 ret = kvm_set_msr_common(vcpu, msr_index, data);
2cc51560 1700 break;
16175a79 1701#ifdef CONFIG_X86_64
6aa8b732 1702 case MSR_FS_BASE:
2fb92db1 1703 vmx_segment_cache_clear(vmx);
6aa8b732
AK
1704 vmcs_writel(GUEST_FS_BASE, data);
1705 break;
1706 case MSR_GS_BASE:
2fb92db1 1707 vmx_segment_cache_clear(vmx);
6aa8b732
AK
1708 vmcs_writel(GUEST_GS_BASE, data);
1709 break;
44ea2b17
AK
1710 case MSR_KERNEL_GS_BASE:
1711 vmx_load_host_state(vmx);
1712 vmx->msr_guest_kernel_gs_base = data;
1713 break;
6aa8b732
AK
1714#endif
1715 case MSR_IA32_SYSENTER_CS:
1716 vmcs_write32(GUEST_SYSENTER_CS, data);
1717 break;
1718 case MSR_IA32_SYSENTER_EIP:
f5b42c33 1719 vmcs_writel(GUEST_SYSENTER_EIP, data);
6aa8b732
AK
1720 break;
1721 case MSR_IA32_SYSENTER_ESP:
f5b42c33 1722 vmcs_writel(GUEST_SYSENTER_ESP, data);
6aa8b732 1723 break;
af24a4e4 1724 case MSR_IA32_TSC:
99e3e30a 1725 kvm_write_tsc(vcpu, data);
6aa8b732 1726 break;
468d472f
SY
1727 case MSR_IA32_CR_PAT:
1728 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1729 vmcs_write64(GUEST_IA32_PAT, data);
1730 vcpu->arch.pat = data;
1731 break;
1732 }
4e47c7a6
SY
1733 ret = kvm_set_msr_common(vcpu, msr_index, data);
1734 break;
1735 case MSR_TSC_AUX:
1736 if (!vmx->rdtscp_enabled)
1737 return 1;
1738 /* Check reserved bit, higher 32 bits should be zero */
1739 if ((data >> 32) != 0)
1740 return 1;
1741 /* Otherwise falls through */
6aa8b732 1742 default:
b87a51ae
NHE
1743 if (vmx_set_vmx_msr(vcpu, msr_index, data))
1744 break;
8b9cf98c 1745 msr = find_msr_entry(vmx, msr_index);
3bab1f5d 1746 if (msr) {
542423b0 1747 vmx_load_host_state(vmx);
3bab1f5d
AK
1748 msr->data = data;
1749 break;
6aa8b732 1750 }
2cc51560 1751 ret = kvm_set_msr_common(vcpu, msr_index, data);
6aa8b732
AK
1752 }
1753
2cc51560 1754 return ret;
6aa8b732
AK
1755}
1756
5fdbf976 1757static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
6aa8b732 1758{
5fdbf976
MT
1759 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1760 switch (reg) {
1761 case VCPU_REGS_RSP:
1762 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1763 break;
1764 case VCPU_REGS_RIP:
1765 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1766 break;
6de4f3ad
AK
1767 case VCPU_EXREG_PDPTR:
1768 if (enable_ept)
1769 ept_save_pdptrs(vcpu);
1770 break;
5fdbf976
MT
1771 default:
1772 break;
1773 }
6aa8b732
AK
1774}
1775
355be0b9 1776static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
6aa8b732 1777{
ae675ef0
JK
1778 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1779 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1780 else
1781 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1782
abd3f2d6 1783 update_exception_bitmap(vcpu);
6aa8b732
AK
1784}
1785
1786static __init int cpu_has_kvm_support(void)
1787{
6210e37b 1788 return cpu_has_vmx();
6aa8b732
AK
1789}
1790
1791static __init int vmx_disabled_by_bios(void)
1792{
1793 u64 msr;
1794
1795 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
cafd6659 1796 if (msr & FEATURE_CONTROL_LOCKED) {
23f3e991 1797 /* launched w/ TXT and VMX disabled */
cafd6659
SW
1798 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1799 && tboot_enabled())
1800 return 1;
23f3e991 1801 /* launched w/o TXT and VMX only enabled w/ TXT */
cafd6659 1802 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
23f3e991 1803 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
f9335afe
SW
1804 && !tboot_enabled()) {
1805 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
23f3e991 1806 "activate TXT before enabling KVM\n");
cafd6659 1807 return 1;
f9335afe 1808 }
23f3e991
JC
1809 /* launched w/o TXT and VMX disabled */
1810 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1811 && !tboot_enabled())
1812 return 1;
cafd6659
SW
1813 }
1814
1815 return 0;
6aa8b732
AK
1816}
1817
7725b894
DX
1818static void kvm_cpu_vmxon(u64 addr)
1819{
1820 asm volatile (ASM_VMX_VMXON_RAX
1821 : : "a"(&addr), "m"(addr)
1822 : "memory", "cc");
1823}
1824
10474ae8 1825static int hardware_enable(void *garbage)
6aa8b732
AK
1826{
1827 int cpu = raw_smp_processor_id();
1828 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
cafd6659 1829 u64 old, test_bits;
6aa8b732 1830
10474ae8
AG
1831 if (read_cr4() & X86_CR4_VMXE)
1832 return -EBUSY;
1833
d462b819 1834 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
6aa8b732 1835 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
cafd6659
SW
1836
1837 test_bits = FEATURE_CONTROL_LOCKED;
1838 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1839 if (tboot_enabled())
1840 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1841
1842 if ((old & test_bits) != test_bits) {
6aa8b732 1843 /* enable and lock */
cafd6659
SW
1844 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1845 }
66aee91a 1846 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
10474ae8 1847
4610c9cc
DX
1848 if (vmm_exclusive) {
1849 kvm_cpu_vmxon(phys_addr);
1850 ept_sync_global();
1851 }
10474ae8 1852
3444d7da
AK
1853 store_gdt(&__get_cpu_var(host_gdt));
1854
10474ae8 1855 return 0;
6aa8b732
AK
1856}
1857
d462b819 1858static void vmclear_local_loaded_vmcss(void)
543e4243
AK
1859{
1860 int cpu = raw_smp_processor_id();
d462b819 1861 struct loaded_vmcs *v, *n;
543e4243 1862
d462b819
NHE
1863 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
1864 loaded_vmcss_on_cpu_link)
1865 __loaded_vmcs_clear(v);
543e4243
AK
1866}
1867
710ff4a8
EH
1868
1869/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1870 * tricks.
1871 */
1872static void kvm_cpu_vmxoff(void)
6aa8b732 1873{
4ecac3fd 1874 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
6aa8b732
AK
1875}
1876
710ff4a8
EH
1877static void hardware_disable(void *garbage)
1878{
4610c9cc 1879 if (vmm_exclusive) {
d462b819 1880 vmclear_local_loaded_vmcss();
4610c9cc
DX
1881 kvm_cpu_vmxoff();
1882 }
7725b894 1883 write_cr4(read_cr4() & ~X86_CR4_VMXE);
710ff4a8
EH
1884}
1885
1c3d14fe 1886static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
d77c26fc 1887 u32 msr, u32 *result)
1c3d14fe
YS
1888{
1889 u32 vmx_msr_low, vmx_msr_high;
1890 u32 ctl = ctl_min | ctl_opt;
1891
1892 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1893
1894 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1895 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1896
1897 /* Ensure minimum (required) set of control bits are supported. */
1898 if (ctl_min & ~ctl)
002c7f7c 1899 return -EIO;
1c3d14fe
YS
1900
1901 *result = ctl;
1902 return 0;
1903}
1904
110312c8
AK
1905static __init bool allow_1_setting(u32 msr, u32 ctl)
1906{
1907 u32 vmx_msr_low, vmx_msr_high;
1908
1909 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1910 return vmx_msr_high & ctl;
1911}
1912
002c7f7c 1913static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
6aa8b732
AK
1914{
1915 u32 vmx_msr_low, vmx_msr_high;
d56f546d 1916 u32 min, opt, min2, opt2;
1c3d14fe
YS
1917 u32 _pin_based_exec_control = 0;
1918 u32 _cpu_based_exec_control = 0;
f78e0e2e 1919 u32 _cpu_based_2nd_exec_control = 0;
1c3d14fe
YS
1920 u32 _vmexit_control = 0;
1921 u32 _vmentry_control = 0;
1922
1923 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
f08864b4 1924 opt = PIN_BASED_VIRTUAL_NMIS;
1c3d14fe
YS
1925 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1926 &_pin_based_exec_control) < 0)
002c7f7c 1927 return -EIO;
1c3d14fe 1928
443381a8 1929 min =
1c3d14fe
YS
1930#ifdef CONFIG_X86_64
1931 CPU_BASED_CR8_LOAD_EXITING |
1932 CPU_BASED_CR8_STORE_EXITING |
1933#endif
d56f546d
SY
1934 CPU_BASED_CR3_LOAD_EXITING |
1935 CPU_BASED_CR3_STORE_EXITING |
1c3d14fe
YS
1936 CPU_BASED_USE_IO_BITMAPS |
1937 CPU_BASED_MOV_DR_EXITING |
a7052897 1938 CPU_BASED_USE_TSC_OFFSETING |
59708670
SY
1939 CPU_BASED_MWAIT_EXITING |
1940 CPU_BASED_MONITOR_EXITING |
a7052897 1941 CPU_BASED_INVLPG_EXITING;
443381a8
AL
1942
1943 if (yield_on_hlt)
1944 min |= CPU_BASED_HLT_EXITING;
1945
f78e0e2e 1946 opt = CPU_BASED_TPR_SHADOW |
25c5f225 1947 CPU_BASED_USE_MSR_BITMAPS |
f78e0e2e 1948 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1c3d14fe
YS
1949 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1950 &_cpu_based_exec_control) < 0)
002c7f7c 1951 return -EIO;
6e5d865c
YS
1952#ifdef CONFIG_X86_64
1953 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1954 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1955 ~CPU_BASED_CR8_STORE_EXITING;
1956#endif
f78e0e2e 1957 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
d56f546d
SY
1958 min2 = 0;
1959 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2384d2b3 1960 SECONDARY_EXEC_WBINVD_EXITING |
d56f546d 1961 SECONDARY_EXEC_ENABLE_VPID |
3a624e29 1962 SECONDARY_EXEC_ENABLE_EPT |
4b8d54f9 1963 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4e47c7a6
SY
1964 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1965 SECONDARY_EXEC_RDTSCP;
d56f546d
SY
1966 if (adjust_vmx_controls(min2, opt2,
1967 MSR_IA32_VMX_PROCBASED_CTLS2,
f78e0e2e
SY
1968 &_cpu_based_2nd_exec_control) < 0)
1969 return -EIO;
1970 }
1971#ifndef CONFIG_X86_64
1972 if (!(_cpu_based_2nd_exec_control &
1973 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1974 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1975#endif
d56f546d 1976 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
a7052897
MT
1977 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1978 enabled */
5fff7d27
GN
1979 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1980 CPU_BASED_CR3_STORE_EXITING |
1981 CPU_BASED_INVLPG_EXITING);
d56f546d
SY
1982 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1983 vmx_capability.ept, vmx_capability.vpid);
1984 }
1c3d14fe
YS
1985
1986 min = 0;
1987#ifdef CONFIG_X86_64
1988 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1989#endif
468d472f 1990 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
1c3d14fe
YS
1991 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1992 &_vmexit_control) < 0)
002c7f7c 1993 return -EIO;
1c3d14fe 1994
468d472f
SY
1995 min = 0;
1996 opt = VM_ENTRY_LOAD_IA32_PAT;
1c3d14fe
YS
1997 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1998 &_vmentry_control) < 0)
002c7f7c 1999 return -EIO;
6aa8b732 2000
c68876fd 2001 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
1c3d14fe
YS
2002
2003 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2004 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
002c7f7c 2005 return -EIO;
1c3d14fe
YS
2006
2007#ifdef CONFIG_X86_64
2008 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2009 if (vmx_msr_high & (1u<<16))
002c7f7c 2010 return -EIO;
1c3d14fe
YS
2011#endif
2012
2013 /* Require Write-Back (WB) memory type for VMCS accesses. */
2014 if (((vmx_msr_high >> 18) & 15) != 6)
002c7f7c 2015 return -EIO;
1c3d14fe 2016
002c7f7c
YS
2017 vmcs_conf->size = vmx_msr_high & 0x1fff;
2018 vmcs_conf->order = get_order(vmcs_config.size);
2019 vmcs_conf->revision_id = vmx_msr_low;
1c3d14fe 2020
002c7f7c
YS
2021 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2022 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
f78e0e2e 2023 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
002c7f7c
YS
2024 vmcs_conf->vmexit_ctrl = _vmexit_control;
2025 vmcs_conf->vmentry_ctrl = _vmentry_control;
1c3d14fe 2026
110312c8
AK
2027 cpu_has_load_ia32_efer =
2028 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2029 VM_ENTRY_LOAD_IA32_EFER)
2030 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2031 VM_EXIT_LOAD_IA32_EFER);
2032
1c3d14fe 2033 return 0;
c68876fd 2034}
6aa8b732
AK
2035
2036static struct vmcs *alloc_vmcs_cpu(int cpu)
2037{
2038 int node = cpu_to_node(cpu);
2039 struct page *pages;
2040 struct vmcs *vmcs;
2041
6484eb3e 2042 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
6aa8b732
AK
2043 if (!pages)
2044 return NULL;
2045 vmcs = page_address(pages);
1c3d14fe
YS
2046 memset(vmcs, 0, vmcs_config.size);
2047 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
6aa8b732
AK
2048 return vmcs;
2049}
2050
2051static struct vmcs *alloc_vmcs(void)
2052{
d3b2c338 2053 return alloc_vmcs_cpu(raw_smp_processor_id());
6aa8b732
AK
2054}
2055
2056static void free_vmcs(struct vmcs *vmcs)
2057{
1c3d14fe 2058 free_pages((unsigned long)vmcs, vmcs_config.order);
6aa8b732
AK
2059}
2060
d462b819
NHE
2061/*
2062 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2063 */
2064static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2065{
2066 if (!loaded_vmcs->vmcs)
2067 return;
2068 loaded_vmcs_clear(loaded_vmcs);
2069 free_vmcs(loaded_vmcs->vmcs);
2070 loaded_vmcs->vmcs = NULL;
2071}
2072
39959588 2073static void free_kvm_area(void)
6aa8b732
AK
2074{
2075 int cpu;
2076
3230bb47 2077 for_each_possible_cpu(cpu) {
6aa8b732 2078 free_vmcs(per_cpu(vmxarea, cpu));
3230bb47
ZA
2079 per_cpu(vmxarea, cpu) = NULL;
2080 }
6aa8b732
AK
2081}
2082
6aa8b732
AK
2083static __init int alloc_kvm_area(void)
2084{
2085 int cpu;
2086
3230bb47 2087 for_each_possible_cpu(cpu) {
6aa8b732
AK
2088 struct vmcs *vmcs;
2089
2090 vmcs = alloc_vmcs_cpu(cpu);
2091 if (!vmcs) {
2092 free_kvm_area();
2093 return -ENOMEM;
2094 }
2095
2096 per_cpu(vmxarea, cpu) = vmcs;
2097 }
2098 return 0;
2099}
2100
2101static __init int hardware_setup(void)
2102{
002c7f7c
YS
2103 if (setup_vmcs_config(&vmcs_config) < 0)
2104 return -EIO;
50a37eb4
JR
2105
2106 if (boot_cpu_has(X86_FEATURE_NX))
2107 kvm_enable_efer_bits(EFER_NX);
2108
93ba03c2
SY
2109 if (!cpu_has_vmx_vpid())
2110 enable_vpid = 0;
2111
4bc9b982
SY
2112 if (!cpu_has_vmx_ept() ||
2113 !cpu_has_vmx_ept_4levels()) {
93ba03c2 2114 enable_ept = 0;
3a624e29
NK
2115 enable_unrestricted_guest = 0;
2116 }
2117
2118 if (!cpu_has_vmx_unrestricted_guest())
2119 enable_unrestricted_guest = 0;
93ba03c2
SY
2120
2121 if (!cpu_has_vmx_flexpriority())
2122 flexpriority_enabled = 0;
2123
95ba8273
GN
2124 if (!cpu_has_vmx_tpr_shadow())
2125 kvm_x86_ops->update_cr8_intercept = NULL;
2126
54dee993
MT
2127 if (enable_ept && !cpu_has_vmx_ept_2m_page())
2128 kvm_disable_largepages();
2129
4b8d54f9
ZE
2130 if (!cpu_has_vmx_ple())
2131 ple_gap = 0;
2132
b87a51ae
NHE
2133 if (nested)
2134 nested_vmx_setup_ctls_msrs();
2135
6aa8b732
AK
2136 return alloc_kvm_area();
2137}
2138
2139static __exit void hardware_unsetup(void)
2140{
2141 free_kvm_area();
2142}
2143
6aa8b732
AK
2144static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
2145{
2146 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2147
6af11b9e 2148 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
6aa8b732
AK
2149 vmcs_write16(sf->selector, save->selector);
2150 vmcs_writel(sf->base, save->base);
2151 vmcs_write32(sf->limit, save->limit);
2152 vmcs_write32(sf->ar_bytes, save->ar);
2153 } else {
2154 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
2155 << AR_DPL_SHIFT;
2156 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
2157 }
2158}
2159
2160static void enter_pmode(struct kvm_vcpu *vcpu)
2161{
2162 unsigned long flags;
a89a8fb9 2163 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732 2164
a89a8fb9 2165 vmx->emulation_required = 1;
7ffd92c5 2166 vmx->rmode.vm86_active = 0;
6aa8b732 2167
2fb92db1
AK
2168 vmx_segment_cache_clear(vmx);
2169
d0ba64f9 2170 vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
7ffd92c5
AK
2171 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
2172 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
2173 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
6aa8b732
AK
2174
2175 flags = vmcs_readl(GUEST_RFLAGS);
78ac8b47
AK
2176 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2177 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
6aa8b732
AK
2178 vmcs_writel(GUEST_RFLAGS, flags);
2179
66aee91a
RR
2180 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2181 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
6aa8b732
AK
2182
2183 update_exception_bitmap(vcpu);
2184
a89a8fb9
MG
2185 if (emulate_invalid_guest_state)
2186 return;
2187
7ffd92c5
AK
2188 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
2189 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
2190 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
2191 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
6aa8b732 2192
2fb92db1
AK
2193 vmx_segment_cache_clear(vmx);
2194
6aa8b732
AK
2195 vmcs_write16(GUEST_SS_SELECTOR, 0);
2196 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
2197
2198 vmcs_write16(GUEST_CS_SELECTOR,
2199 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
2200 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
2201}
2202
d77c26fc 2203static gva_t rmode_tss_base(struct kvm *kvm)
6aa8b732 2204{
bfc6d222 2205 if (!kvm->arch.tss_addr) {
bc6678a3
MT
2206 struct kvm_memslots *slots;
2207 gfn_t base_gfn;
2208
90d83dc3 2209 slots = kvm_memslots(kvm);
f495c6e5 2210 base_gfn = slots->memslots[0].base_gfn +
46a26bf5 2211 kvm->memslots->memslots[0].npages - 3;
cbc94022
IE
2212 return base_gfn << PAGE_SHIFT;
2213 }
bfc6d222 2214 return kvm->arch.tss_addr;
6aa8b732
AK
2215}
2216
2217static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
2218{
2219 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2220
2221 save->selector = vmcs_read16(sf->selector);
2222 save->base = vmcs_readl(sf->base);
2223 save->limit = vmcs_read32(sf->limit);
2224 save->ar = vmcs_read32(sf->ar_bytes);
15b00f32 2225 vmcs_write16(sf->selector, save->base >> 4);
444e863d 2226 vmcs_write32(sf->base, save->base & 0xffff0);
6aa8b732
AK
2227 vmcs_write32(sf->limit, 0xffff);
2228 vmcs_write32(sf->ar_bytes, 0xf3);
444e863d
GN
2229 if (save->base & 0xf)
2230 printk_once(KERN_WARNING "kvm: segment base is not paragraph"
2231 " aligned when entering protected mode (seg=%d)",
2232 seg);
6aa8b732
AK
2233}
2234
2235static void enter_rmode(struct kvm_vcpu *vcpu)
2236{
2237 unsigned long flags;
a89a8fb9 2238 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732 2239
3a624e29
NK
2240 if (enable_unrestricted_guest)
2241 return;
2242
a89a8fb9 2243 vmx->emulation_required = 1;
7ffd92c5 2244 vmx->rmode.vm86_active = 1;
6aa8b732 2245
776e58ea
GN
2246 /*
2247 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
2248 * vcpu. Call it here with phys address pointing 16M below 4G.
2249 */
2250 if (!vcpu->kvm->arch.tss_addr) {
2251 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2252 "called before entering vcpu\n");
2253 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2254 vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
2255 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2256 }
2257
2fb92db1
AK
2258 vmx_segment_cache_clear(vmx);
2259
d0ba64f9 2260 vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
7ffd92c5 2261 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
6aa8b732
AK
2262 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
2263
7ffd92c5 2264 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
6aa8b732
AK
2265 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
2266
7ffd92c5 2267 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
6aa8b732
AK
2268 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2269
2270 flags = vmcs_readl(GUEST_RFLAGS);
78ac8b47 2271 vmx->rmode.save_rflags = flags;
6aa8b732 2272
053de044 2273 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
6aa8b732
AK
2274
2275 vmcs_writel(GUEST_RFLAGS, flags);
66aee91a 2276 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
6aa8b732
AK
2277 update_exception_bitmap(vcpu);
2278
a89a8fb9
MG
2279 if (emulate_invalid_guest_state)
2280 goto continue_rmode;
2281
6aa8b732
AK
2282 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
2283 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
2284 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
2285
2286 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
abacf8df 2287 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
8cb5b033
AK
2288 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
2289 vmcs_writel(GUEST_CS_BASE, 0xf0000);
6aa8b732
AK
2290 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
2291
7ffd92c5
AK
2292 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
2293 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
2294 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
2295 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
75880a01 2296
a89a8fb9 2297continue_rmode:
8668a3c4 2298 kvm_mmu_reset_context(vcpu);
6aa8b732
AK
2299}
2300
401d10de
AS
2301static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
2302{
2303 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981
AK
2304 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2305
2306 if (!msr)
2307 return;
401d10de 2308
44ea2b17
AK
2309 /*
2310 * Force kernel_gs_base reloading before EFER changes, as control
2311 * of this msr depends on is_long_mode().
2312 */
2313 vmx_load_host_state(to_vmx(vcpu));
f6801dff 2314 vcpu->arch.efer = efer;
401d10de
AS
2315 if (efer & EFER_LMA) {
2316 vmcs_write32(VM_ENTRY_CONTROLS,
2317 vmcs_read32(VM_ENTRY_CONTROLS) |
2318 VM_ENTRY_IA32E_MODE);
2319 msr->data = efer;
2320 } else {
2321 vmcs_write32(VM_ENTRY_CONTROLS,
2322 vmcs_read32(VM_ENTRY_CONTROLS) &
2323 ~VM_ENTRY_IA32E_MODE);
2324
2325 msr->data = efer & ~EFER_LME;
2326 }
2327 setup_msrs(vmx);
2328}
2329
05b3e0c2 2330#ifdef CONFIG_X86_64
6aa8b732
AK
2331
2332static void enter_lmode(struct kvm_vcpu *vcpu)
2333{
2334 u32 guest_tr_ar;
2335
2fb92db1
AK
2336 vmx_segment_cache_clear(to_vmx(vcpu));
2337
6aa8b732
AK
2338 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
2339 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
2340 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
b8688d51 2341 __func__);
6aa8b732
AK
2342 vmcs_write32(GUEST_TR_AR_BYTES,
2343 (guest_tr_ar & ~AR_TYPE_MASK)
2344 | AR_TYPE_BUSY_64_TSS);
2345 }
da38f438 2346 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
6aa8b732
AK
2347}
2348
2349static void exit_lmode(struct kvm_vcpu *vcpu)
2350{
6aa8b732
AK
2351 vmcs_write32(VM_ENTRY_CONTROLS,
2352 vmcs_read32(VM_ENTRY_CONTROLS)
1e4e6e00 2353 & ~VM_ENTRY_IA32E_MODE);
da38f438 2354 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
6aa8b732
AK
2355}
2356
2357#endif
2358
2384d2b3
SY
2359static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2360{
b9d762fa 2361 vpid_sync_context(to_vmx(vcpu));
dd180b3e
XG
2362 if (enable_ept) {
2363 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2364 return;
4e1096d2 2365 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
dd180b3e 2366 }
2384d2b3
SY
2367}
2368
e8467fda
AK
2369static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2370{
2371 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2372
2373 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
2374 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
2375}
2376
aff48baa
AK
2377static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2378{
2379 if (enable_ept && is_paging(vcpu))
2380 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2381 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2382}
2383
25c4c276 2384static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
399badf3 2385{
fc78f519
AK
2386 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2387
2388 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2389 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
399badf3
AK
2390}
2391
1439442c
SY
2392static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2393{
6de4f3ad
AK
2394 if (!test_bit(VCPU_EXREG_PDPTR,
2395 (unsigned long *)&vcpu->arch.regs_dirty))
2396 return;
2397
1439442c 2398 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
ff03a073
JR
2399 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
2400 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
2401 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
2402 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
1439442c
SY
2403 }
2404}
2405
8f5d549f
AK
2406static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
2407{
2408 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
ff03a073
JR
2409 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2410 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2411 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2412 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
8f5d549f 2413 }
6de4f3ad
AK
2414
2415 __set_bit(VCPU_EXREG_PDPTR,
2416 (unsigned long *)&vcpu->arch.regs_avail);
2417 __set_bit(VCPU_EXREG_PDPTR,
2418 (unsigned long *)&vcpu->arch.regs_dirty);
8f5d549f
AK
2419}
2420
5e1746d6 2421static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1439442c
SY
2422
2423static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2424 unsigned long cr0,
2425 struct kvm_vcpu *vcpu)
2426{
5233dd51
MT
2427 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
2428 vmx_decache_cr3(vcpu);
1439442c
SY
2429 if (!(cr0 & X86_CR0_PG)) {
2430 /* From paging/starting to nonpaging */
2431 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
65267ea1 2432 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
1439442c
SY
2433 (CPU_BASED_CR3_LOAD_EXITING |
2434 CPU_BASED_CR3_STORE_EXITING));
2435 vcpu->arch.cr0 = cr0;
fc78f519 2436 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
1439442c
SY
2437 } else if (!is_paging(vcpu)) {
2438 /* From nonpaging to paging */
2439 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
65267ea1 2440 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
1439442c
SY
2441 ~(CPU_BASED_CR3_LOAD_EXITING |
2442 CPU_BASED_CR3_STORE_EXITING));
2443 vcpu->arch.cr0 = cr0;
fc78f519 2444 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
1439442c 2445 }
95eb84a7
SY
2446
2447 if (!(cr0 & X86_CR0_WP))
2448 *hw_cr0 &= ~X86_CR0_WP;
1439442c
SY
2449}
2450
6aa8b732
AK
2451static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2452{
7ffd92c5 2453 struct vcpu_vmx *vmx = to_vmx(vcpu);
3a624e29
NK
2454 unsigned long hw_cr0;
2455
2456 if (enable_unrestricted_guest)
2457 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
2458 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
2459 else
2460 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
1439442c 2461
7ffd92c5 2462 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
6aa8b732
AK
2463 enter_pmode(vcpu);
2464
7ffd92c5 2465 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
6aa8b732
AK
2466 enter_rmode(vcpu);
2467
05b3e0c2 2468#ifdef CONFIG_X86_64
f6801dff 2469 if (vcpu->arch.efer & EFER_LME) {
707d92fa 2470 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
6aa8b732 2471 enter_lmode(vcpu);
707d92fa 2472 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
6aa8b732
AK
2473 exit_lmode(vcpu);
2474 }
2475#endif
2476
089d034e 2477 if (enable_ept)
1439442c
SY
2478 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2479
02daab21 2480 if (!vcpu->fpu_active)
81231c69 2481 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
02daab21 2482
6aa8b732 2483 vmcs_writel(CR0_READ_SHADOW, cr0);
1439442c 2484 vmcs_writel(GUEST_CR0, hw_cr0);
ad312c7c 2485 vcpu->arch.cr0 = cr0;
69c73028 2486 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
6aa8b732
AK
2487}
2488
1439442c
SY
2489static u64 construct_eptp(unsigned long root_hpa)
2490{
2491 u64 eptp;
2492
2493 /* TODO write the value reading from MSR */
2494 eptp = VMX_EPT_DEFAULT_MT |
2495 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
2496 eptp |= (root_hpa & PAGE_MASK);
2497
2498 return eptp;
2499}
2500
6aa8b732
AK
2501static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
2502{
1439442c
SY
2503 unsigned long guest_cr3;
2504 u64 eptp;
2505
2506 guest_cr3 = cr3;
089d034e 2507 if (enable_ept) {
1439442c
SY
2508 eptp = construct_eptp(cr3);
2509 vmcs_write64(EPT_POINTER, eptp);
9f8fe504 2510 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
b927a3ce 2511 vcpu->kvm->arch.ept_identity_map_addr;
7c93be44 2512 ept_load_pdptrs(vcpu);
1439442c
SY
2513 }
2514
2384d2b3 2515 vmx_flush_tlb(vcpu);
1439442c 2516 vmcs_writel(GUEST_CR3, guest_cr3);
6aa8b732
AK
2517}
2518
5e1746d6 2519static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
6aa8b732 2520{
7ffd92c5 2521 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
1439442c
SY
2522 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
2523
5e1746d6
NHE
2524 if (cr4 & X86_CR4_VMXE) {
2525 /*
2526 * To use VMXON (and later other VMX instructions), a guest
2527 * must first be able to turn on cr4.VMXE (see handle_vmon()).
2528 * So basically the check on whether to allow nested VMX
2529 * is here.
2530 */
2531 if (!nested_vmx_allowed(vcpu))
2532 return 1;
2533 } else if (to_vmx(vcpu)->nested.vmxon)
2534 return 1;
2535
ad312c7c 2536 vcpu->arch.cr4 = cr4;
bc23008b
AK
2537 if (enable_ept) {
2538 if (!is_paging(vcpu)) {
2539 hw_cr4 &= ~X86_CR4_PAE;
2540 hw_cr4 |= X86_CR4_PSE;
2541 } else if (!(cr4 & X86_CR4_PAE)) {
2542 hw_cr4 &= ~X86_CR4_PAE;
2543 }
2544 }
1439442c
SY
2545
2546 vmcs_writel(CR4_READ_SHADOW, cr4);
2547 vmcs_writel(GUEST_CR4, hw_cr4);
5e1746d6 2548 return 0;
6aa8b732
AK
2549}
2550
6aa8b732
AK
2551static void vmx_get_segment(struct kvm_vcpu *vcpu,
2552 struct kvm_segment *var, int seg)
2553{
a9179499 2554 struct vcpu_vmx *vmx = to_vmx(vcpu);
a9179499 2555 struct kvm_save_segment *save;
6aa8b732
AK
2556 u32 ar;
2557
a9179499
AK
2558 if (vmx->rmode.vm86_active
2559 && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
2560 || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
2561 || seg == VCPU_SREG_GS)
2562 && !emulate_invalid_guest_state) {
2563 switch (seg) {
2564 case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
2565 case VCPU_SREG_ES: save = &vmx->rmode.es; break;
2566 case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
2567 case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
2568 case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
2569 default: BUG();
2570 }
2571 var->selector = save->selector;
2572 var->base = save->base;
2573 var->limit = save->limit;
2574 ar = save->ar;
2575 if (seg == VCPU_SREG_TR
2fb92db1 2576 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
a9179499
AK
2577 goto use_saved_rmode_seg;
2578 }
2fb92db1
AK
2579 var->base = vmx_read_guest_seg_base(vmx, seg);
2580 var->limit = vmx_read_guest_seg_limit(vmx, seg);
2581 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2582 ar = vmx_read_guest_seg_ar(vmx, seg);
a9179499 2583use_saved_rmode_seg:
9fd4a3b7 2584 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
6aa8b732
AK
2585 ar = 0;
2586 var->type = ar & 15;
2587 var->s = (ar >> 4) & 1;
2588 var->dpl = (ar >> 5) & 3;
2589 var->present = (ar >> 7) & 1;
2590 var->avl = (ar >> 12) & 1;
2591 var->l = (ar >> 13) & 1;
2592 var->db = (ar >> 14) & 1;
2593 var->g = (ar >> 15) & 1;
2594 var->unusable = (ar >> 16) & 1;
2595}
2596
a9179499
AK
2597static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2598{
a9179499
AK
2599 struct kvm_segment s;
2600
2601 if (to_vmx(vcpu)->rmode.vm86_active) {
2602 vmx_get_segment(vcpu, &s, seg);
2603 return s.base;
2604 }
2fb92db1 2605 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
a9179499
AK
2606}
2607
69c73028 2608static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
2e4d2653 2609{
3eeb3288 2610 if (!is_protmode(vcpu))
2e4d2653
IE
2611 return 0;
2612
f4c63e5d
AK
2613 if (!is_long_mode(vcpu)
2614 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
2e4d2653
IE
2615 return 3;
2616
2fb92db1 2617 return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
2e4d2653
IE
2618}
2619
69c73028
AK
2620static int vmx_get_cpl(struct kvm_vcpu *vcpu)
2621{
2622 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
2623 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2624 to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
2625 }
2626 return to_vmx(vcpu)->cpl;
2627}
2628
2629
653e3108 2630static u32 vmx_segment_access_rights(struct kvm_segment *var)
6aa8b732 2631{
6aa8b732
AK
2632 u32 ar;
2633
653e3108 2634 if (var->unusable)
6aa8b732
AK
2635 ar = 1 << 16;
2636 else {
2637 ar = var->type & 15;
2638 ar |= (var->s & 1) << 4;
2639 ar |= (var->dpl & 3) << 5;
2640 ar |= (var->present & 1) << 7;
2641 ar |= (var->avl & 1) << 12;
2642 ar |= (var->l & 1) << 13;
2643 ar |= (var->db & 1) << 14;
2644 ar |= (var->g & 1) << 15;
2645 }
f7fbf1fd
UL
2646 if (ar == 0) /* a 0 value means unusable */
2647 ar = AR_UNUSABLE_MASK;
653e3108
AK
2648
2649 return ar;
2650}
2651
2652static void vmx_set_segment(struct kvm_vcpu *vcpu,
2653 struct kvm_segment *var, int seg)
2654{
7ffd92c5 2655 struct vcpu_vmx *vmx = to_vmx(vcpu);
653e3108
AK
2656 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2657 u32 ar;
2658
2fb92db1
AK
2659 vmx_segment_cache_clear(vmx);
2660
7ffd92c5 2661 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
a8ba6c26 2662 vmcs_write16(sf->selector, var->selector);
7ffd92c5
AK
2663 vmx->rmode.tr.selector = var->selector;
2664 vmx->rmode.tr.base = var->base;
2665 vmx->rmode.tr.limit = var->limit;
2666 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
653e3108
AK
2667 return;
2668 }
2669 vmcs_writel(sf->base, var->base);
2670 vmcs_write32(sf->limit, var->limit);
2671 vmcs_write16(sf->selector, var->selector);
7ffd92c5 2672 if (vmx->rmode.vm86_active && var->s) {
653e3108
AK
2673 /*
2674 * Hack real-mode segments into vm86 compatibility.
2675 */
2676 if (var->base == 0xffff0000 && var->selector == 0xf000)
2677 vmcs_writel(sf->base, 0xf0000);
2678 ar = 0xf3;
2679 } else
2680 ar = vmx_segment_access_rights(var);
3a624e29
NK
2681
2682 /*
2683 * Fix the "Accessed" bit in AR field of segment registers for older
2684 * qemu binaries.
2685 * IA32 arch specifies that at the time of processor reset the
2686 * "Accessed" bit in the AR field of segment registers is 1. And qemu
2687 * is setting it to 0 in the usedland code. This causes invalid guest
2688 * state vmexit when "unrestricted guest" mode is turned on.
2689 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2690 * tree. Newer qemu binaries with that qemu fix would not need this
2691 * kvm hack.
2692 */
2693 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
2694 ar |= 0x1; /* Accessed */
2695
6aa8b732 2696 vmcs_write32(sf->ar_bytes, ar);
69c73028 2697 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
6aa8b732
AK
2698}
2699
6aa8b732
AK
2700static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2701{
2fb92db1 2702 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
6aa8b732
AK
2703
2704 *db = (ar >> 14) & 1;
2705 *l = (ar >> 13) & 1;
2706}
2707
89a27f4d 2708static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 2709{
89a27f4d
GN
2710 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2711 dt->address = vmcs_readl(GUEST_IDTR_BASE);
6aa8b732
AK
2712}
2713
89a27f4d 2714static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 2715{
89a27f4d
GN
2716 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2717 vmcs_writel(GUEST_IDTR_BASE, dt->address);
6aa8b732
AK
2718}
2719
89a27f4d 2720static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 2721{
89a27f4d
GN
2722 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2723 dt->address = vmcs_readl(GUEST_GDTR_BASE);
6aa8b732
AK
2724}
2725
89a27f4d 2726static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 2727{
89a27f4d
GN
2728 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2729 vmcs_writel(GUEST_GDTR_BASE, dt->address);
6aa8b732
AK
2730}
2731
648dfaa7
MG
2732static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2733{
2734 struct kvm_segment var;
2735 u32 ar;
2736
2737 vmx_get_segment(vcpu, &var, seg);
2738 ar = vmx_segment_access_rights(&var);
2739
2740 if (var.base != (var.selector << 4))
2741 return false;
2742 if (var.limit != 0xffff)
2743 return false;
2744 if (ar != 0xf3)
2745 return false;
2746
2747 return true;
2748}
2749
2750static bool code_segment_valid(struct kvm_vcpu *vcpu)
2751{
2752 struct kvm_segment cs;
2753 unsigned int cs_rpl;
2754
2755 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2756 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
2757
1872a3f4
AK
2758 if (cs.unusable)
2759 return false;
648dfaa7
MG
2760 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
2761 return false;
2762 if (!cs.s)
2763 return false;
1872a3f4 2764 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
648dfaa7
MG
2765 if (cs.dpl > cs_rpl)
2766 return false;
1872a3f4 2767 } else {
648dfaa7
MG
2768 if (cs.dpl != cs_rpl)
2769 return false;
2770 }
2771 if (!cs.present)
2772 return false;
2773
2774 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2775 return true;
2776}
2777
2778static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2779{
2780 struct kvm_segment ss;
2781 unsigned int ss_rpl;
2782
2783 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2784 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
2785
1872a3f4
AK
2786 if (ss.unusable)
2787 return true;
2788 if (ss.type != 3 && ss.type != 7)
648dfaa7
MG
2789 return false;
2790 if (!ss.s)
2791 return false;
2792 if (ss.dpl != ss_rpl) /* DPL != RPL */
2793 return false;
2794 if (!ss.present)
2795 return false;
2796
2797 return true;
2798}
2799
2800static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2801{
2802 struct kvm_segment var;
2803 unsigned int rpl;
2804
2805 vmx_get_segment(vcpu, &var, seg);
2806 rpl = var.selector & SELECTOR_RPL_MASK;
2807
1872a3f4
AK
2808 if (var.unusable)
2809 return true;
648dfaa7
MG
2810 if (!var.s)
2811 return false;
2812 if (!var.present)
2813 return false;
2814 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2815 if (var.dpl < rpl) /* DPL < RPL */
2816 return false;
2817 }
2818
2819 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2820 * rights flags
2821 */
2822 return true;
2823}
2824
2825static bool tr_valid(struct kvm_vcpu *vcpu)
2826{
2827 struct kvm_segment tr;
2828
2829 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2830
1872a3f4
AK
2831 if (tr.unusable)
2832 return false;
648dfaa7
MG
2833 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2834 return false;
1872a3f4 2835 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
648dfaa7
MG
2836 return false;
2837 if (!tr.present)
2838 return false;
2839
2840 return true;
2841}
2842
2843static bool ldtr_valid(struct kvm_vcpu *vcpu)
2844{
2845 struct kvm_segment ldtr;
2846
2847 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2848
1872a3f4
AK
2849 if (ldtr.unusable)
2850 return true;
648dfaa7
MG
2851 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2852 return false;
2853 if (ldtr.type != 2)
2854 return false;
2855 if (!ldtr.present)
2856 return false;
2857
2858 return true;
2859}
2860
2861static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2862{
2863 struct kvm_segment cs, ss;
2864
2865 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2866 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2867
2868 return ((cs.selector & SELECTOR_RPL_MASK) ==
2869 (ss.selector & SELECTOR_RPL_MASK));
2870}
2871
2872/*
2873 * Check if guest state is valid. Returns true if valid, false if
2874 * not.
2875 * We assume that registers are always usable
2876 */
2877static bool guest_state_valid(struct kvm_vcpu *vcpu)
2878{
2879 /* real mode guest state checks */
3eeb3288 2880 if (!is_protmode(vcpu)) {
648dfaa7
MG
2881 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2882 return false;
2883 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2884 return false;
2885 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2886 return false;
2887 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2888 return false;
2889 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2890 return false;
2891 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2892 return false;
2893 } else {
2894 /* protected mode guest state checks */
2895 if (!cs_ss_rpl_check(vcpu))
2896 return false;
2897 if (!code_segment_valid(vcpu))
2898 return false;
2899 if (!stack_segment_valid(vcpu))
2900 return false;
2901 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2902 return false;
2903 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2904 return false;
2905 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2906 return false;
2907 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2908 return false;
2909 if (!tr_valid(vcpu))
2910 return false;
2911 if (!ldtr_valid(vcpu))
2912 return false;
2913 }
2914 /* TODO:
2915 * - Add checks on RIP
2916 * - Add checks on RFLAGS
2917 */
2918
2919 return true;
2920}
2921
d77c26fc 2922static int init_rmode_tss(struct kvm *kvm)
6aa8b732 2923{
40dcaa9f 2924 gfn_t fn;
195aefde 2925 u16 data = 0;
40dcaa9f 2926 int r, idx, ret = 0;
6aa8b732 2927
40dcaa9f
XG
2928 idx = srcu_read_lock(&kvm->srcu);
2929 fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
195aefde
IE
2930 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2931 if (r < 0)
10589a46 2932 goto out;
195aefde 2933 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
464d17c8
SY
2934 r = kvm_write_guest_page(kvm, fn++, &data,
2935 TSS_IOPB_BASE_OFFSET, sizeof(u16));
195aefde 2936 if (r < 0)
10589a46 2937 goto out;
195aefde
IE
2938 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2939 if (r < 0)
10589a46 2940 goto out;
195aefde
IE
2941 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2942 if (r < 0)
10589a46 2943 goto out;
195aefde 2944 data = ~0;
10589a46
MT
2945 r = kvm_write_guest_page(kvm, fn, &data,
2946 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2947 sizeof(u8));
195aefde 2948 if (r < 0)
10589a46
MT
2949 goto out;
2950
2951 ret = 1;
2952out:
40dcaa9f 2953 srcu_read_unlock(&kvm->srcu, idx);
10589a46 2954 return ret;
6aa8b732
AK
2955}
2956
b7ebfb05
SY
2957static int init_rmode_identity_map(struct kvm *kvm)
2958{
40dcaa9f 2959 int i, idx, r, ret;
b7ebfb05
SY
2960 pfn_t identity_map_pfn;
2961 u32 tmp;
2962
089d034e 2963 if (!enable_ept)
b7ebfb05
SY
2964 return 1;
2965 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2966 printk(KERN_ERR "EPT: identity-mapping pagetable "
2967 "haven't been allocated!\n");
2968 return 0;
2969 }
2970 if (likely(kvm->arch.ept_identity_pagetable_done))
2971 return 1;
2972 ret = 0;
b927a3ce 2973 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
40dcaa9f 2974 idx = srcu_read_lock(&kvm->srcu);
b7ebfb05
SY
2975 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2976 if (r < 0)
2977 goto out;
2978 /* Set up identity-mapping pagetable for EPT in real mode */
2979 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2980 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2981 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2982 r = kvm_write_guest_page(kvm, identity_map_pfn,
2983 &tmp, i * sizeof(tmp), sizeof(tmp));
2984 if (r < 0)
2985 goto out;
2986 }
2987 kvm->arch.ept_identity_pagetable_done = true;
2988 ret = 1;
2989out:
40dcaa9f 2990 srcu_read_unlock(&kvm->srcu, idx);
b7ebfb05
SY
2991 return ret;
2992}
2993
6aa8b732
AK
2994static void seg_setup(int seg)
2995{
2996 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3a624e29 2997 unsigned int ar;
6aa8b732
AK
2998
2999 vmcs_write16(sf->selector, 0);
3000 vmcs_writel(sf->base, 0);
3001 vmcs_write32(sf->limit, 0xffff);
3a624e29
NK
3002 if (enable_unrestricted_guest) {
3003 ar = 0x93;
3004 if (seg == VCPU_SREG_CS)
3005 ar |= 0x08; /* code segment */
3006 } else
3007 ar = 0xf3;
3008
3009 vmcs_write32(sf->ar_bytes, ar);
6aa8b732
AK
3010}
3011
f78e0e2e
SY
3012static int alloc_apic_access_page(struct kvm *kvm)
3013{
3014 struct kvm_userspace_memory_region kvm_userspace_mem;
3015 int r = 0;
3016
79fac95e 3017 mutex_lock(&kvm->slots_lock);
bfc6d222 3018 if (kvm->arch.apic_access_page)
f78e0e2e
SY
3019 goto out;
3020 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3021 kvm_userspace_mem.flags = 0;
3022 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3023 kvm_userspace_mem.memory_size = PAGE_SIZE;
3024 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
3025 if (r)
3026 goto out;
72dc67a6 3027
bfc6d222 3028 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
f78e0e2e 3029out:
79fac95e 3030 mutex_unlock(&kvm->slots_lock);
f78e0e2e
SY
3031 return r;
3032}
3033
b7ebfb05
SY
3034static int alloc_identity_pagetable(struct kvm *kvm)
3035{
3036 struct kvm_userspace_memory_region kvm_userspace_mem;
3037 int r = 0;
3038
79fac95e 3039 mutex_lock(&kvm->slots_lock);
b7ebfb05
SY
3040 if (kvm->arch.ept_identity_pagetable)
3041 goto out;
3042 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3043 kvm_userspace_mem.flags = 0;
b927a3ce
SY
3044 kvm_userspace_mem.guest_phys_addr =
3045 kvm->arch.ept_identity_map_addr;
b7ebfb05
SY
3046 kvm_userspace_mem.memory_size = PAGE_SIZE;
3047 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
3048 if (r)
3049 goto out;
3050
b7ebfb05 3051 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
b927a3ce 3052 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
b7ebfb05 3053out:
79fac95e 3054 mutex_unlock(&kvm->slots_lock);
b7ebfb05
SY
3055 return r;
3056}
3057
2384d2b3
SY
3058static void allocate_vpid(struct vcpu_vmx *vmx)
3059{
3060 int vpid;
3061
3062 vmx->vpid = 0;
919818ab 3063 if (!enable_vpid)
2384d2b3
SY
3064 return;
3065 spin_lock(&vmx_vpid_lock);
3066 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
3067 if (vpid < VMX_NR_VPIDS) {
3068 vmx->vpid = vpid;
3069 __set_bit(vpid, vmx_vpid_bitmap);
3070 }
3071 spin_unlock(&vmx_vpid_lock);
3072}
3073
cdbecfc3
LJ
3074static void free_vpid(struct vcpu_vmx *vmx)
3075{
3076 if (!enable_vpid)
3077 return;
3078 spin_lock(&vmx_vpid_lock);
3079 if (vmx->vpid != 0)
3080 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3081 spin_unlock(&vmx_vpid_lock);
3082}
3083
5897297b 3084static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
25c5f225 3085{
3e7c73e9 3086 int f = sizeof(unsigned long);
25c5f225
SY
3087
3088 if (!cpu_has_vmx_msr_bitmap())
3089 return;
3090
3091 /*
3092 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3093 * have the write-low and read-high bitmap offsets the wrong way round.
3094 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3095 */
25c5f225 3096 if (msr <= 0x1fff) {
3e7c73e9
AK
3097 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
3098 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
25c5f225
SY
3099 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3100 msr &= 0x1fff;
3e7c73e9
AK
3101 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
3102 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
25c5f225 3103 }
25c5f225
SY
3104}
3105
5897297b
AK
3106static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
3107{
3108 if (!longmode_only)
3109 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
3110 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
3111}
3112
6aa8b732
AK
3113/*
3114 * Sets up the vmcs for emulated real mode.
3115 */
8b9cf98c 3116static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
6aa8b732 3117{
468d472f 3118 u32 host_sysenter_cs, msr_low, msr_high;
6aa8b732 3119 u32 junk;
f4e1b3c8 3120 u64 host_pat;
6aa8b732 3121 unsigned long a;
89a27f4d 3122 struct desc_ptr dt;
6aa8b732 3123 int i;
cd2276a7 3124 unsigned long kvm_vmx_return;
6e5d865c 3125 u32 exec_control;
6aa8b732 3126
6aa8b732 3127 /* I/O */
3e7c73e9
AK
3128 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
3129 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
6aa8b732 3130
25c5f225 3131 if (cpu_has_vmx_msr_bitmap())
5897297b 3132 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
25c5f225 3133
6aa8b732
AK
3134 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
3135
6aa8b732 3136 /* Control */
1c3d14fe
YS
3137 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
3138 vmcs_config.pin_based_exec_ctrl);
6e5d865c
YS
3139
3140 exec_control = vmcs_config.cpu_based_exec_ctrl;
3141 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
3142 exec_control &= ~CPU_BASED_TPR_SHADOW;
3143#ifdef CONFIG_X86_64
3144 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3145 CPU_BASED_CR8_LOAD_EXITING;
3146#endif
3147 }
089d034e 3148 if (!enable_ept)
d56f546d 3149 exec_control |= CPU_BASED_CR3_STORE_EXITING |
83dbc83a
MT
3150 CPU_BASED_CR3_LOAD_EXITING |
3151 CPU_BASED_INVLPG_EXITING;
6e5d865c 3152 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
6aa8b732 3153
83ff3b9d
SY
3154 if (cpu_has_secondary_exec_ctrls()) {
3155 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
3156 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3157 exec_control &=
3158 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
2384d2b3
SY
3159 if (vmx->vpid == 0)
3160 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
046d8710 3161 if (!enable_ept) {
d56f546d 3162 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
046d8710
SY
3163 enable_unrestricted_guest = 0;
3164 }
3a624e29
NK
3165 if (!enable_unrestricted_guest)
3166 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4b8d54f9
ZE
3167 if (!ple_gap)
3168 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
83ff3b9d
SY
3169 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
3170 }
f78e0e2e 3171
4b8d54f9
ZE
3172 if (ple_gap) {
3173 vmcs_write32(PLE_GAP, ple_gap);
3174 vmcs_write32(PLE_WINDOW, ple_window);
3175 }
3176
c7addb90
AK
3177 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
3178 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
6aa8b732
AK
3179 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
3180
1c11e713 3181 vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
6aa8b732
AK
3182 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
3183 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
3184
3185 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
3186 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3187 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
9581d442
AK
3188 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
3189 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
6aa8b732 3190 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
05b3e0c2 3191#ifdef CONFIG_X86_64
6aa8b732
AK
3192 rdmsrl(MSR_FS_BASE, a);
3193 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
3194 rdmsrl(MSR_GS_BASE, a);
3195 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
3196#else
3197 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
3198 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
3199#endif
3200
3201 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3202
ec68798c 3203 native_store_idt(&dt);
89a27f4d 3204 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
6aa8b732 3205
d77c26fc 3206 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
cd2276a7 3207 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
2cc51560
ED
3208 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
3209 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
61d2ef2c 3210 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
2cc51560 3211 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
61d2ef2c 3212 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
6aa8b732
AK
3213
3214 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
3215 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
3216 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
3217 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
3218 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
3219 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
3220
468d472f
SY
3221 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3222 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
3223 host_pat = msr_low | ((u64) msr_high << 32);
3224 vmcs_write64(HOST_IA32_PAT, host_pat);
3225 }
3226 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
3227 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
3228 host_pat = msr_low | ((u64) msr_high << 32);
3229 /* Write the default value follow host pat */
3230 vmcs_write64(GUEST_IA32_PAT, host_pat);
3231 /* Keep arch.pat sync with GUEST_IA32_PAT */
3232 vmx->vcpu.arch.pat = host_pat;
3233 }
3234
6aa8b732
AK
3235 for (i = 0; i < NR_VMX_MSR; ++i) {
3236 u32 index = vmx_msr_index[i];
3237 u32 data_low, data_high;
a2fa3e9f 3238 int j = vmx->nmsrs;
6aa8b732
AK
3239
3240 if (rdmsr_safe(index, &data_low, &data_high) < 0)
3241 continue;
432bd6cb
AK
3242 if (wrmsr_safe(index, data_low, data_high) < 0)
3243 continue;
26bb0981
AK
3244 vmx->guest_msrs[j].index = i;
3245 vmx->guest_msrs[j].data = 0;
d5696725 3246 vmx->guest_msrs[j].mask = -1ull;
a2fa3e9f 3247 ++vmx->nmsrs;
6aa8b732 3248 }
6aa8b732 3249
1c3d14fe 3250 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
6aa8b732
AK
3251
3252 /* 22.2.1, 20.8.1 */
1c3d14fe
YS
3253 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
3254
e00c8cf2 3255 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
4c38609a 3256 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
ce03e4f2
AK
3257 if (enable_ept)
3258 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
4c38609a 3259 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
e00c8cf2 3260
99e3e30a 3261 kvm_write_tsc(&vmx->vcpu, 0);
f78e0e2e 3262
e00c8cf2
AK
3263 return 0;
3264}
3265
3266static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
3267{
3268 struct vcpu_vmx *vmx = to_vmx(vcpu);
3269 u64 msr;
4b9d3a04 3270 int ret;
e00c8cf2 3271
5fdbf976 3272 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
e00c8cf2 3273
7ffd92c5 3274 vmx->rmode.vm86_active = 0;
e00c8cf2 3275
3b86cd99
JK
3276 vmx->soft_vnmi_blocked = 0;
3277
ad312c7c 3278 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
2d3ad1f4 3279 kvm_set_cr8(&vmx->vcpu, 0);
e00c8cf2 3280 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
c5af89b6 3281 if (kvm_vcpu_is_bsp(&vmx->vcpu))
e00c8cf2
AK
3282 msr |= MSR_IA32_APICBASE_BSP;
3283 kvm_set_apic_base(&vmx->vcpu, msr);
3284
10ab25cd
JK
3285 ret = fx_init(&vmx->vcpu);
3286 if (ret != 0)
3287 goto out;
e00c8cf2 3288
2fb92db1
AK
3289 vmx_segment_cache_clear(vmx);
3290
5706be0d 3291 seg_setup(VCPU_SREG_CS);
e00c8cf2
AK
3292 /*
3293 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
3294 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
3295 */
c5af89b6 3296 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
e00c8cf2
AK
3297 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
3298 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
3299 } else {
ad312c7c
ZX
3300 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
3301 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
e00c8cf2 3302 }
e00c8cf2
AK
3303
3304 seg_setup(VCPU_SREG_DS);
3305 seg_setup(VCPU_SREG_ES);
3306 seg_setup(VCPU_SREG_FS);
3307 seg_setup(VCPU_SREG_GS);
3308 seg_setup(VCPU_SREG_SS);
3309
3310 vmcs_write16(GUEST_TR_SELECTOR, 0);
3311 vmcs_writel(GUEST_TR_BASE, 0);
3312 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
3313 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3314
3315 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
3316 vmcs_writel(GUEST_LDTR_BASE, 0);
3317 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
3318 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
3319
3320 vmcs_write32(GUEST_SYSENTER_CS, 0);
3321 vmcs_writel(GUEST_SYSENTER_ESP, 0);
3322 vmcs_writel(GUEST_SYSENTER_EIP, 0);
3323
3324 vmcs_writel(GUEST_RFLAGS, 0x02);
c5af89b6 3325 if (kvm_vcpu_is_bsp(&vmx->vcpu))
5fdbf976 3326 kvm_rip_write(vcpu, 0xfff0);
e00c8cf2 3327 else
5fdbf976
MT
3328 kvm_rip_write(vcpu, 0);
3329 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
e00c8cf2 3330
e00c8cf2
AK
3331 vmcs_writel(GUEST_DR7, 0x400);
3332
3333 vmcs_writel(GUEST_GDTR_BASE, 0);
3334 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
3335
3336 vmcs_writel(GUEST_IDTR_BASE, 0);
3337 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
3338
443381a8 3339 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
e00c8cf2
AK
3340 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
3341 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
3342
e00c8cf2
AK
3343 /* Special registers */
3344 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
3345
3346 setup_msrs(vmx);
3347
6aa8b732
AK
3348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
3349
f78e0e2e
SY
3350 if (cpu_has_vmx_tpr_shadow()) {
3351 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
3352 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
3353 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
afc20184 3354 __pa(vmx->vcpu.arch.apic->regs));
f78e0e2e
SY
3355 vmcs_write32(TPR_THRESHOLD, 0);
3356 }
3357
3358 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3359 vmcs_write64(APIC_ACCESS_ADDR,
bfc6d222 3360 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
6aa8b732 3361
2384d2b3
SY
3362 if (vmx->vpid != 0)
3363 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
3364
fa40052c 3365 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
4d4ec087 3366 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
8b9cf98c 3367 vmx_set_cr4(&vmx->vcpu, 0);
8b9cf98c 3368 vmx_set_efer(&vmx->vcpu, 0);
8b9cf98c
RR
3369 vmx_fpu_activate(&vmx->vcpu);
3370 update_exception_bitmap(&vmx->vcpu);
6aa8b732 3371
b9d762fa 3372 vpid_sync_context(vmx);
2384d2b3 3373
3200f405 3374 ret = 0;
6aa8b732 3375
a89a8fb9
MG
3376 /* HACK: Don't enable emulation on guest boot/reset */
3377 vmx->emulation_required = 0;
3378
6aa8b732
AK
3379out:
3380 return ret;
3381}
3382
3b86cd99
JK
3383static void enable_irq_window(struct kvm_vcpu *vcpu)
3384{
3385 u32 cpu_based_vm_exec_control;
3386
3387 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3388 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
3389 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3390}
3391
3392static void enable_nmi_window(struct kvm_vcpu *vcpu)
3393{
3394 u32 cpu_based_vm_exec_control;
3395
3396 if (!cpu_has_virtual_nmis()) {
3397 enable_irq_window(vcpu);
3398 return;
3399 }
3400
30bd0c4c
AK
3401 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
3402 enable_irq_window(vcpu);
3403 return;
3404 }
3b86cd99
JK
3405 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3406 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
3407 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3408}
3409
66fd3f7f 3410static void vmx_inject_irq(struct kvm_vcpu *vcpu)
85f455f7 3411{
9c8cba37 3412 struct vcpu_vmx *vmx = to_vmx(vcpu);
66fd3f7f
GN
3413 uint32_t intr;
3414 int irq = vcpu->arch.interrupt.nr;
9c8cba37 3415
229456fc 3416 trace_kvm_inj_virq(irq);
2714d1d3 3417
fa89a817 3418 ++vcpu->stat.irq_injections;
7ffd92c5 3419 if (vmx->rmode.vm86_active) {
71f9833b
SH
3420 int inc_eip = 0;
3421 if (vcpu->arch.interrupt.soft)
3422 inc_eip = vcpu->arch.event_exit_inst_len;
3423 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
a92601bb 3424 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
85f455f7
ED
3425 return;
3426 }
66fd3f7f
GN
3427 intr = irq | INTR_INFO_VALID_MASK;
3428 if (vcpu->arch.interrupt.soft) {
3429 intr |= INTR_TYPE_SOFT_INTR;
3430 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3431 vmx->vcpu.arch.event_exit_inst_len);
3432 } else
3433 intr |= INTR_TYPE_EXT_INTR;
3434 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
443381a8 3435 vmx_clear_hlt(vcpu);
85f455f7
ED
3436}
3437
f08864b4
SY
3438static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
3439{
66a5a347
JK
3440 struct vcpu_vmx *vmx = to_vmx(vcpu);
3441
3b86cd99
JK
3442 if (!cpu_has_virtual_nmis()) {
3443 /*
3444 * Tracking the NMI-blocked state in software is built upon
3445 * finding the next open IRQ window. This, in turn, depends on
3446 * well-behaving guests: They have to keep IRQs disabled at
3447 * least as long as the NMI handler runs. Otherwise we may
3448 * cause NMI nesting, maybe breaking the guest. But as this is
3449 * highly unlikely, we can live with the residual risk.
3450 */
3451 vmx->soft_vnmi_blocked = 1;
3452 vmx->vnmi_blocked_time = 0;
3453 }
3454
487b391d 3455 ++vcpu->stat.nmi_injections;
9d58b931 3456 vmx->nmi_known_unmasked = false;
7ffd92c5 3457 if (vmx->rmode.vm86_active) {
71f9833b 3458 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
a92601bb 3459 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
66a5a347
JK
3460 return;
3461 }
f08864b4
SY
3462 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
3463 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
443381a8 3464 vmx_clear_hlt(vcpu);
f08864b4
SY
3465}
3466
c4282df9 3467static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
33f089ca 3468{
3b86cd99 3469 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
c4282df9 3470 return 0;
33f089ca 3471
c4282df9 3472 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
30bd0c4c
AK
3473 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
3474 | GUEST_INTR_STATE_NMI));
33f089ca
JK
3475}
3476
3cfc3092
JK
3477static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
3478{
3479 if (!cpu_has_virtual_nmis())
3480 return to_vmx(vcpu)->soft_vnmi_blocked;
9d58b931
AK
3481 if (to_vmx(vcpu)->nmi_known_unmasked)
3482 return false;
c332c83a 3483 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
3cfc3092
JK
3484}
3485
3486static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3487{
3488 struct vcpu_vmx *vmx = to_vmx(vcpu);
3489
3490 if (!cpu_has_virtual_nmis()) {
3491 if (vmx->soft_vnmi_blocked != masked) {
3492 vmx->soft_vnmi_blocked = masked;
3493 vmx->vnmi_blocked_time = 0;
3494 }
3495 } else {
9d58b931 3496 vmx->nmi_known_unmasked = !masked;
3cfc3092
JK
3497 if (masked)
3498 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3499 GUEST_INTR_STATE_NMI);
3500 else
3501 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3502 GUEST_INTR_STATE_NMI);
3503 }
3504}
3505
78646121
GN
3506static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
3507{
c4282df9
GN
3508 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
3509 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3510 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
78646121
GN
3511}
3512
cbc94022
IE
3513static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
3514{
3515 int ret;
3516 struct kvm_userspace_memory_region tss_mem = {
6fe63979 3517 .slot = TSS_PRIVATE_MEMSLOT,
cbc94022
IE
3518 .guest_phys_addr = addr,
3519 .memory_size = PAGE_SIZE * 3,
3520 .flags = 0,
3521 };
3522
3523 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
3524 if (ret)
3525 return ret;
bfc6d222 3526 kvm->arch.tss_addr = addr;
93ea5388
GN
3527 if (!init_rmode_tss(kvm))
3528 return -ENOMEM;
3529
cbc94022
IE
3530 return 0;
3531}
3532
6aa8b732
AK
3533static int handle_rmode_exception(struct kvm_vcpu *vcpu,
3534 int vec, u32 err_code)
3535{
b3f37707
NK
3536 /*
3537 * Instruction with address size override prefix opcode 0x67
3538 * Cause the #SS fault with 0 error code in VM86 mode.
3539 */
3540 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
51d8b661 3541 if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
6aa8b732 3542 return 1;
77ab6db0
JK
3543 /*
3544 * Forward all other exceptions that are valid in real mode.
3545 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
3546 * the required debugging infrastructure rework.
3547 */
3548 switch (vec) {
77ab6db0 3549 case DB_VECTOR:
d0bfb940
JK
3550 if (vcpu->guest_debug &
3551 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
3552 return 0;
3553 kvm_queue_exception(vcpu, vec);
3554 return 1;
77ab6db0 3555 case BP_VECTOR:
c573cd22
JK
3556 /*
3557 * Update instruction length as we may reinject the exception
3558 * from user space while in guest debugging mode.
3559 */
3560 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
3561 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
d0bfb940
JK
3562 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
3563 return 0;
3564 /* fall through */
3565 case DE_VECTOR:
77ab6db0
JK
3566 case OF_VECTOR:
3567 case BR_VECTOR:
3568 case UD_VECTOR:
3569 case DF_VECTOR:
3570 case SS_VECTOR:
3571 case GP_VECTOR:
3572 case MF_VECTOR:
3573 kvm_queue_exception(vcpu, vec);
3574 return 1;
3575 }
6aa8b732
AK
3576 return 0;
3577}
3578
a0861c02
AK
3579/*
3580 * Trigger machine check on the host. We assume all the MSRs are already set up
3581 * by the CPU and that we still run on the same CPU as the MCE occurred on.
3582 * We pass a fake environment to the machine check handler because we want
3583 * the guest to be always treated like user space, no matter what context
3584 * it used internally.
3585 */
3586static void kvm_machine_check(void)
3587{
3588#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
3589 struct pt_regs regs = {
3590 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
3591 .flags = X86_EFLAGS_IF,
3592 };
3593
3594 do_machine_check(&regs, 0);
3595#endif
3596}
3597
851ba692 3598static int handle_machine_check(struct kvm_vcpu *vcpu)
a0861c02
AK
3599{
3600 /* already handled by vcpu_run */
3601 return 1;
3602}
3603
851ba692 3604static int handle_exception(struct kvm_vcpu *vcpu)
6aa8b732 3605{
1155f76a 3606 struct vcpu_vmx *vmx = to_vmx(vcpu);
851ba692 3607 struct kvm_run *kvm_run = vcpu->run;
d0bfb940 3608 u32 intr_info, ex_no, error_code;
42dbaa5a 3609 unsigned long cr2, rip, dr6;
6aa8b732
AK
3610 u32 vect_info;
3611 enum emulation_result er;
3612
1155f76a 3613 vect_info = vmx->idt_vectoring_info;
88786475 3614 intr_info = vmx->exit_intr_info;
6aa8b732 3615
a0861c02 3616 if (is_machine_check(intr_info))
851ba692 3617 return handle_machine_check(vcpu);
a0861c02 3618
6aa8b732 3619 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
65ac7264
AK
3620 !is_page_fault(intr_info)) {
3621 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3622 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
3623 vcpu->run->internal.ndata = 2;
3624 vcpu->run->internal.data[0] = vect_info;
3625 vcpu->run->internal.data[1] = intr_info;
3626 return 0;
3627 }
6aa8b732 3628
e4a41889 3629 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
1b6269db 3630 return 1; /* already handled by vmx_vcpu_run() */
2ab455cc
AL
3631
3632 if (is_no_device(intr_info)) {
5fd86fcf 3633 vmx_fpu_activate(vcpu);
2ab455cc
AL
3634 return 1;
3635 }
3636
7aa81cc0 3637 if (is_invalid_opcode(intr_info)) {
51d8b661 3638 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
7aa81cc0 3639 if (er != EMULATE_DONE)
7ee5d940 3640 kvm_queue_exception(vcpu, UD_VECTOR);
7aa81cc0
AL
3641 return 1;
3642 }
3643
6aa8b732 3644 error_code = 0;
2e11384c 3645 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
6aa8b732
AK
3646 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
3647 if (is_page_fault(intr_info)) {
1439442c 3648 /* EPT won't cause page fault directly */
089d034e 3649 if (enable_ept)
1439442c 3650 BUG();
6aa8b732 3651 cr2 = vmcs_readl(EXIT_QUALIFICATION);
229456fc
MT
3652 trace_kvm_page_fault(cr2, error_code);
3653
3298b75c 3654 if (kvm_event_needs_reinjection(vcpu))
577bdc49 3655 kvm_mmu_unprotect_page_virt(vcpu, cr2);
dc25e89e 3656 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
6aa8b732
AK
3657 }
3658
7ffd92c5 3659 if (vmx->rmode.vm86_active &&
6aa8b732 3660 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
72d6e5a0 3661 error_code)) {
ad312c7c
ZX
3662 if (vcpu->arch.halt_request) {
3663 vcpu->arch.halt_request = 0;
72d6e5a0
AK
3664 return kvm_emulate_halt(vcpu);
3665 }
6aa8b732 3666 return 1;
72d6e5a0 3667 }
6aa8b732 3668
d0bfb940 3669 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
42dbaa5a
JK
3670 switch (ex_no) {
3671 case DB_VECTOR:
3672 dr6 = vmcs_readl(EXIT_QUALIFICATION);
3673 if (!(vcpu->guest_debug &
3674 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
3675 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
3676 kvm_queue_exception(vcpu, DB_VECTOR);
3677 return 1;
3678 }
3679 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
3680 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
3681 /* fall through */
3682 case BP_VECTOR:
c573cd22
JK
3683 /*
3684 * Update instruction length as we may reinject #BP from
3685 * user space while in guest debugging mode. Reading it for
3686 * #DB as well causes no harm, it is not used in that case.
3687 */
3688 vmx->vcpu.arch.event_exit_inst_len =
3689 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6aa8b732 3690 kvm_run->exit_reason = KVM_EXIT_DEBUG;
0a434bb2 3691 rip = kvm_rip_read(vcpu);
d0bfb940
JK
3692 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
3693 kvm_run->debug.arch.exception = ex_no;
42dbaa5a
JK
3694 break;
3695 default:
d0bfb940
JK
3696 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
3697 kvm_run->ex.exception = ex_no;
3698 kvm_run->ex.error_code = error_code;
42dbaa5a 3699 break;
6aa8b732 3700 }
6aa8b732
AK
3701 return 0;
3702}
3703
851ba692 3704static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6aa8b732 3705{
1165f5fe 3706 ++vcpu->stat.irq_exits;
6aa8b732
AK
3707 return 1;
3708}
3709
851ba692 3710static int handle_triple_fault(struct kvm_vcpu *vcpu)
988ad74f 3711{
851ba692 3712 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
988ad74f
AK
3713 return 0;
3714}
6aa8b732 3715
851ba692 3716static int handle_io(struct kvm_vcpu *vcpu)
6aa8b732 3717{
bfdaab09 3718 unsigned long exit_qualification;
34c33d16 3719 int size, in, string;
039576c0 3720 unsigned port;
6aa8b732 3721
bfdaab09 3722 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
039576c0 3723 string = (exit_qualification & 16) != 0;
cf8f70bf 3724 in = (exit_qualification & 8) != 0;
e70669ab 3725
cf8f70bf 3726 ++vcpu->stat.io_exits;
e70669ab 3727
cf8f70bf 3728 if (string || in)
51d8b661 3729 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
e70669ab 3730
cf8f70bf
GN
3731 port = exit_qualification >> 16;
3732 size = (exit_qualification & 7) + 1;
e93f36bc 3733 skip_emulated_instruction(vcpu);
cf8f70bf
GN
3734
3735 return kvm_fast_pio_out(vcpu, size, port);
6aa8b732
AK
3736}
3737
102d8325
IM
3738static void
3739vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3740{
3741 /*
3742 * Patch in the VMCALL instruction:
3743 */
3744 hypercall[0] = 0x0f;
3745 hypercall[1] = 0x01;
3746 hypercall[2] = 0xc1;
102d8325
IM
3747}
3748
851ba692 3749static int handle_cr(struct kvm_vcpu *vcpu)
6aa8b732 3750{
229456fc 3751 unsigned long exit_qualification, val;
6aa8b732
AK
3752 int cr;
3753 int reg;
49a9b07e 3754 int err;
6aa8b732 3755
bfdaab09 3756 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6aa8b732
AK
3757 cr = exit_qualification & 15;
3758 reg = (exit_qualification >> 8) & 15;
3759 switch ((exit_qualification >> 4) & 3) {
3760 case 0: /* mov to cr */
229456fc
MT
3761 val = kvm_register_read(vcpu, reg);
3762 trace_kvm_cr_write(cr, val);
6aa8b732
AK
3763 switch (cr) {
3764 case 0:
49a9b07e 3765 err = kvm_set_cr0(vcpu, val);
db8fcefa 3766 kvm_complete_insn_gp(vcpu, err);
6aa8b732
AK
3767 return 1;
3768 case 3:
2390218b 3769 err = kvm_set_cr3(vcpu, val);
db8fcefa 3770 kvm_complete_insn_gp(vcpu, err);
6aa8b732
AK
3771 return 1;
3772 case 4:
a83b29c6 3773 err = kvm_set_cr4(vcpu, val);
db8fcefa 3774 kvm_complete_insn_gp(vcpu, err);
6aa8b732 3775 return 1;
0a5fff19
GN
3776 case 8: {
3777 u8 cr8_prev = kvm_get_cr8(vcpu);
3778 u8 cr8 = kvm_register_read(vcpu, reg);
eea1cff9 3779 err = kvm_set_cr8(vcpu, cr8);
db8fcefa 3780 kvm_complete_insn_gp(vcpu, err);
0a5fff19
GN
3781 if (irqchip_in_kernel(vcpu->kvm))
3782 return 1;
3783 if (cr8_prev <= cr8)
3784 return 1;
851ba692 3785 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
0a5fff19
GN
3786 return 0;
3787 }
6aa8b732
AK
3788 };
3789 break;
25c4c276 3790 case 2: /* clts */
edcafe3c 3791 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4d4ec087 3792 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
25c4c276 3793 skip_emulated_instruction(vcpu);
6b52d186 3794 vmx_fpu_activate(vcpu);
25c4c276 3795 return 1;
6aa8b732
AK
3796 case 1: /*mov from cr*/
3797 switch (cr) {
3798 case 3:
9f8fe504
AK
3799 val = kvm_read_cr3(vcpu);
3800 kvm_register_write(vcpu, reg, val);
3801 trace_kvm_cr_read(cr, val);
6aa8b732
AK
3802 skip_emulated_instruction(vcpu);
3803 return 1;
3804 case 8:
229456fc
MT
3805 val = kvm_get_cr8(vcpu);
3806 kvm_register_write(vcpu, reg, val);
3807 trace_kvm_cr_read(cr, val);
6aa8b732
AK
3808 skip_emulated_instruction(vcpu);
3809 return 1;
3810 }
3811 break;
3812 case 3: /* lmsw */
a1f83a74 3813 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
4d4ec087 3814 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
a1f83a74 3815 kvm_lmsw(vcpu, val);
6aa8b732
AK
3816
3817 skip_emulated_instruction(vcpu);
3818 return 1;
3819 default:
3820 break;
3821 }
851ba692 3822 vcpu->run->exit_reason = 0;
f0242478 3823 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6aa8b732
AK
3824 (int)(exit_qualification >> 4) & 3, cr);
3825 return 0;
3826}
3827
851ba692 3828static int handle_dr(struct kvm_vcpu *vcpu)
6aa8b732 3829{
bfdaab09 3830 unsigned long exit_qualification;
6aa8b732
AK
3831 int dr, reg;
3832
f2483415 3833 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
0a79b009
AK
3834 if (!kvm_require_cpl(vcpu, 0))
3835 return 1;
42dbaa5a
JK
3836 dr = vmcs_readl(GUEST_DR7);
3837 if (dr & DR7_GD) {
3838 /*
3839 * As the vm-exit takes precedence over the debug trap, we
3840 * need to emulate the latter, either for the host or the
3841 * guest debugging itself.
3842 */
3843 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
851ba692
AK
3844 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3845 vcpu->run->debug.arch.dr7 = dr;
3846 vcpu->run->debug.arch.pc =
42dbaa5a
JK
3847 vmcs_readl(GUEST_CS_BASE) +
3848 vmcs_readl(GUEST_RIP);
851ba692
AK
3849 vcpu->run->debug.arch.exception = DB_VECTOR;
3850 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
42dbaa5a
JK
3851 return 0;
3852 } else {
3853 vcpu->arch.dr7 &= ~DR7_GD;
3854 vcpu->arch.dr6 |= DR6_BD;
3855 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3856 kvm_queue_exception(vcpu, DB_VECTOR);
3857 return 1;
3858 }
3859 }
3860
bfdaab09 3861 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
42dbaa5a
JK
3862 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3863 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3864 if (exit_qualification & TYPE_MOV_FROM_DR) {
020df079
GN
3865 unsigned long val;
3866 if (!kvm_get_dr(vcpu, dr, &val))
3867 kvm_register_write(vcpu, reg, val);
3868 } else
3869 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
6aa8b732
AK
3870 skip_emulated_instruction(vcpu);
3871 return 1;
3872}
3873
020df079
GN
3874static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
3875{
3876 vmcs_writel(GUEST_DR7, val);
3877}
3878
851ba692 3879static int handle_cpuid(struct kvm_vcpu *vcpu)
6aa8b732 3880{
06465c5a
AK
3881 kvm_emulate_cpuid(vcpu);
3882 return 1;
6aa8b732
AK
3883}
3884
851ba692 3885static int handle_rdmsr(struct kvm_vcpu *vcpu)
6aa8b732 3886{
ad312c7c 3887 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6aa8b732
AK
3888 u64 data;
3889
3890 if (vmx_get_msr(vcpu, ecx, &data)) {
59200273 3891 trace_kvm_msr_read_ex(ecx);
c1a5d4f9 3892 kvm_inject_gp(vcpu, 0);
6aa8b732
AK
3893 return 1;
3894 }
3895
229456fc 3896 trace_kvm_msr_read(ecx, data);
2714d1d3 3897
6aa8b732 3898 /* FIXME: handling of bits 32:63 of rax, rdx */
ad312c7c
ZX
3899 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3900 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
6aa8b732
AK
3901 skip_emulated_instruction(vcpu);
3902 return 1;
3903}
3904
851ba692 3905static int handle_wrmsr(struct kvm_vcpu *vcpu)
6aa8b732 3906{
ad312c7c
ZX
3907 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3908 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3909 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
6aa8b732
AK
3910
3911 if (vmx_set_msr(vcpu, ecx, data) != 0) {
59200273 3912 trace_kvm_msr_write_ex(ecx, data);
c1a5d4f9 3913 kvm_inject_gp(vcpu, 0);
6aa8b732
AK
3914 return 1;
3915 }
3916
59200273 3917 trace_kvm_msr_write(ecx, data);
6aa8b732
AK
3918 skip_emulated_instruction(vcpu);
3919 return 1;
3920}
3921
851ba692 3922static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6e5d865c 3923{
3842d135 3924 kvm_make_request(KVM_REQ_EVENT, vcpu);
6e5d865c
YS
3925 return 1;
3926}
3927
851ba692 3928static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6aa8b732 3929{
85f455f7
ED
3930 u32 cpu_based_vm_exec_control;
3931
3932 /* clear pending irq */
3933 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3934 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3935 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2714d1d3 3936
3842d135
AK
3937 kvm_make_request(KVM_REQ_EVENT, vcpu);
3938
a26bf12a 3939 ++vcpu->stat.irq_window_exits;
2714d1d3 3940
c1150d8c
DL
3941 /*
3942 * If the user space waits to inject interrupts, exit as soon as
3943 * possible
3944 */
8061823a 3945 if (!irqchip_in_kernel(vcpu->kvm) &&
851ba692 3946 vcpu->run->request_interrupt_window &&
8061823a 3947 !kvm_cpu_has_interrupt(vcpu)) {
851ba692 3948 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
c1150d8c
DL
3949 return 0;
3950 }
6aa8b732
AK
3951 return 1;
3952}
3953
851ba692 3954static int handle_halt(struct kvm_vcpu *vcpu)
6aa8b732
AK
3955{
3956 skip_emulated_instruction(vcpu);
d3bef15f 3957 return kvm_emulate_halt(vcpu);
6aa8b732
AK
3958}
3959
851ba692 3960static int handle_vmcall(struct kvm_vcpu *vcpu)
c21415e8 3961{
510043da 3962 skip_emulated_instruction(vcpu);
7aa81cc0
AL
3963 kvm_emulate_hypercall(vcpu);
3964 return 1;
c21415e8
IM
3965}
3966
851ba692 3967static int handle_vmx_insn(struct kvm_vcpu *vcpu)
e3c7cb6a
AK
3968{
3969 kvm_queue_exception(vcpu, UD_VECTOR);
3970 return 1;
3971}
3972
ec25d5e6
GN
3973static int handle_invd(struct kvm_vcpu *vcpu)
3974{
51d8b661 3975 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
ec25d5e6
GN
3976}
3977
851ba692 3978static int handle_invlpg(struct kvm_vcpu *vcpu)
a7052897 3979{
f9c617f6 3980 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
a7052897
MT
3981
3982 kvm_mmu_invlpg(vcpu, exit_qualification);
3983 skip_emulated_instruction(vcpu);
3984 return 1;
3985}
3986
851ba692 3987static int handle_wbinvd(struct kvm_vcpu *vcpu)
e5edaa01
ED
3988{
3989 skip_emulated_instruction(vcpu);
f5f48ee1 3990 kvm_emulate_wbinvd(vcpu);
e5edaa01
ED
3991 return 1;
3992}
3993
2acf923e
DC
3994static int handle_xsetbv(struct kvm_vcpu *vcpu)
3995{
3996 u64 new_bv = kvm_read_edx_eax(vcpu);
3997 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3998
3999 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
4000 skip_emulated_instruction(vcpu);
4001 return 1;
4002}
4003
851ba692 4004static int handle_apic_access(struct kvm_vcpu *vcpu)
f78e0e2e 4005{
51d8b661 4006 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
f78e0e2e
SY
4007}
4008
851ba692 4009static int handle_task_switch(struct kvm_vcpu *vcpu)
37817f29 4010{
60637aac 4011 struct vcpu_vmx *vmx = to_vmx(vcpu);
37817f29 4012 unsigned long exit_qualification;
e269fb21
JK
4013 bool has_error_code = false;
4014 u32 error_code = 0;
37817f29 4015 u16 tss_selector;
64a7ec06
GN
4016 int reason, type, idt_v;
4017
4018 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
4019 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
37817f29
IE
4020
4021 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4022
4023 reason = (u32)exit_qualification >> 30;
64a7ec06
GN
4024 if (reason == TASK_SWITCH_GATE && idt_v) {
4025 switch (type) {
4026 case INTR_TYPE_NMI_INTR:
4027 vcpu->arch.nmi_injected = false;
654f06fc 4028 vmx_set_nmi_mask(vcpu, true);
64a7ec06
GN
4029 break;
4030 case INTR_TYPE_EXT_INTR:
66fd3f7f 4031 case INTR_TYPE_SOFT_INTR:
64a7ec06
GN
4032 kvm_clear_interrupt_queue(vcpu);
4033 break;
4034 case INTR_TYPE_HARD_EXCEPTION:
e269fb21
JK
4035 if (vmx->idt_vectoring_info &
4036 VECTORING_INFO_DELIVER_CODE_MASK) {
4037 has_error_code = true;
4038 error_code =
4039 vmcs_read32(IDT_VECTORING_ERROR_CODE);
4040 }
4041 /* fall through */
64a7ec06
GN
4042 case INTR_TYPE_SOFT_EXCEPTION:
4043 kvm_clear_exception_queue(vcpu);
4044 break;
4045 default:
4046 break;
4047 }
60637aac 4048 }
37817f29
IE
4049 tss_selector = exit_qualification;
4050
64a7ec06
GN
4051 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
4052 type != INTR_TYPE_EXT_INTR &&
4053 type != INTR_TYPE_NMI_INTR))
4054 skip_emulated_instruction(vcpu);
4055
acb54517
GN
4056 if (kvm_task_switch(vcpu, tss_selector, reason,
4057 has_error_code, error_code) == EMULATE_FAIL) {
4058 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4059 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4060 vcpu->run->internal.ndata = 0;
42dbaa5a 4061 return 0;
acb54517 4062 }
42dbaa5a
JK
4063
4064 /* clear all local breakpoint enable flags */
4065 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
4066
4067 /*
4068 * TODO: What about debug traps on tss switch?
4069 * Are we supposed to inject them and update dr6?
4070 */
4071
4072 return 1;
37817f29
IE
4073}
4074
851ba692 4075static int handle_ept_violation(struct kvm_vcpu *vcpu)
1439442c 4076{
f9c617f6 4077 unsigned long exit_qualification;
1439442c 4078 gpa_t gpa;
1439442c 4079 int gla_validity;
1439442c 4080
f9c617f6 4081 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
1439442c
SY
4082
4083 if (exit_qualification & (1 << 6)) {
4084 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
7f582ab6 4085 return -EINVAL;
1439442c
SY
4086 }
4087
4088 gla_validity = (exit_qualification >> 7) & 0x3;
4089 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
4090 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
4091 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
4092 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
f9c617f6 4093 vmcs_readl(GUEST_LINEAR_ADDRESS));
1439442c
SY
4094 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
4095 (long unsigned int)exit_qualification);
851ba692
AK
4096 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4097 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
596ae895 4098 return 0;
1439442c
SY
4099 }
4100
4101 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
229456fc 4102 trace_kvm_page_fault(gpa, exit_qualification);
dc25e89e 4103 return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
1439442c
SY
4104}
4105
68f89400
MT
4106static u64 ept_rsvd_mask(u64 spte, int level)
4107{
4108 int i;
4109 u64 mask = 0;
4110
4111 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
4112 mask |= (1ULL << i);
4113
4114 if (level > 2)
4115 /* bits 7:3 reserved */
4116 mask |= 0xf8;
4117 else if (level == 2) {
4118 if (spte & (1ULL << 7))
4119 /* 2MB ref, bits 20:12 reserved */
4120 mask |= 0x1ff000;
4121 else
4122 /* bits 6:3 reserved */
4123 mask |= 0x78;
4124 }
4125
4126 return mask;
4127}
4128
4129static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
4130 int level)
4131{
4132 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
4133
4134 /* 010b (write-only) */
4135 WARN_ON((spte & 0x7) == 0x2);
4136
4137 /* 110b (write/execute) */
4138 WARN_ON((spte & 0x7) == 0x6);
4139
4140 /* 100b (execute-only) and value not supported by logical processor */
4141 if (!cpu_has_vmx_ept_execute_only())
4142 WARN_ON((spte & 0x7) == 0x4);
4143
4144 /* not 000b */
4145 if ((spte & 0x7)) {
4146 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
4147
4148 if (rsvd_bits != 0) {
4149 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
4150 __func__, rsvd_bits);
4151 WARN_ON(1);
4152 }
4153
4154 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
4155 u64 ept_mem_type = (spte & 0x38) >> 3;
4156
4157 if (ept_mem_type == 2 || ept_mem_type == 3 ||
4158 ept_mem_type == 7) {
4159 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
4160 __func__, ept_mem_type);
4161 WARN_ON(1);
4162 }
4163 }
4164 }
4165}
4166
851ba692 4167static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
68f89400
MT
4168{
4169 u64 sptes[4];
4170 int nr_sptes, i;
4171 gpa_t gpa;
4172
4173 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
4174
4175 printk(KERN_ERR "EPT: Misconfiguration.\n");
4176 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
4177
4178 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
4179
4180 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
4181 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
4182
851ba692
AK
4183 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4184 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
68f89400
MT
4185
4186 return 0;
4187}
4188
851ba692 4189static int handle_nmi_window(struct kvm_vcpu *vcpu)
f08864b4
SY
4190{
4191 u32 cpu_based_vm_exec_control;
4192
4193 /* clear pending NMI */
4194 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4195 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
4196 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4197 ++vcpu->stat.nmi_window_exits;
3842d135 4198 kvm_make_request(KVM_REQ_EVENT, vcpu);
f08864b4
SY
4199
4200 return 1;
4201}
4202
80ced186 4203static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
ea953ef0 4204{
8b3079a5
AK
4205 struct vcpu_vmx *vmx = to_vmx(vcpu);
4206 enum emulation_result err = EMULATE_DONE;
80ced186 4207 int ret = 1;
49e9d557
AK
4208 u32 cpu_exec_ctrl;
4209 bool intr_window_requested;
4210
4211 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4212 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
ea953ef0
MG
4213
4214 while (!guest_state_valid(vcpu)) {
49e9d557
AK
4215 if (intr_window_requested
4216 && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
4217 return handle_interrupt_window(&vmx->vcpu);
4218
51d8b661 4219 err = emulate_instruction(vcpu, 0);
ea953ef0 4220
80ced186
MG
4221 if (err == EMULATE_DO_MMIO) {
4222 ret = 0;
4223 goto out;
4224 }
1d5a4d9b 4225
6d77dbfc
GN
4226 if (err != EMULATE_DONE)
4227 return 0;
ea953ef0
MG
4228
4229 if (signal_pending(current))
80ced186 4230 goto out;
ea953ef0
MG
4231 if (need_resched())
4232 schedule();
4233 }
4234
80ced186
MG
4235 vmx->emulation_required = 0;
4236out:
4237 return ret;
ea953ef0
MG
4238}
4239
4b8d54f9
ZE
4240/*
4241 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
4242 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
4243 */
9fb41ba8 4244static int handle_pause(struct kvm_vcpu *vcpu)
4b8d54f9
ZE
4245{
4246 skip_emulated_instruction(vcpu);
4247 kvm_vcpu_on_spin(vcpu);
4248
4249 return 1;
4250}
4251
59708670
SY
4252static int handle_invalid_op(struct kvm_vcpu *vcpu)
4253{
4254 kvm_queue_exception(vcpu, UD_VECTOR);
4255 return 1;
4256}
4257
ff2f6fe9
NHE
4258/*
4259 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
4260 * We could reuse a single VMCS for all the L2 guests, but we also want the
4261 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
4262 * allows keeping them loaded on the processor, and in the future will allow
4263 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
4264 * every entry if they never change.
4265 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
4266 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
4267 *
4268 * The following functions allocate and free a vmcs02 in this pool.
4269 */
4270
4271/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
4272static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
4273{
4274 struct vmcs02_list *item;
4275 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
4276 if (item->vmptr == vmx->nested.current_vmptr) {
4277 list_move(&item->list, &vmx->nested.vmcs02_pool);
4278 return &item->vmcs02;
4279 }
4280
4281 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
4282 /* Recycle the least recently used VMCS. */
4283 item = list_entry(vmx->nested.vmcs02_pool.prev,
4284 struct vmcs02_list, list);
4285 item->vmptr = vmx->nested.current_vmptr;
4286 list_move(&item->list, &vmx->nested.vmcs02_pool);
4287 return &item->vmcs02;
4288 }
4289
4290 /* Create a new VMCS */
4291 item = (struct vmcs02_list *)
4292 kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
4293 if (!item)
4294 return NULL;
4295 item->vmcs02.vmcs = alloc_vmcs();
4296 if (!item->vmcs02.vmcs) {
4297 kfree(item);
4298 return NULL;
4299 }
4300 loaded_vmcs_init(&item->vmcs02);
4301 item->vmptr = vmx->nested.current_vmptr;
4302 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
4303 vmx->nested.vmcs02_num++;
4304 return &item->vmcs02;
4305}
4306
4307/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
4308static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
4309{
4310 struct vmcs02_list *item;
4311 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
4312 if (item->vmptr == vmptr) {
4313 free_loaded_vmcs(&item->vmcs02);
4314 list_del(&item->list);
4315 kfree(item);
4316 vmx->nested.vmcs02_num--;
4317 return;
4318 }
4319}
4320
4321/*
4322 * Free all VMCSs saved for this vcpu, except the one pointed by
4323 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
4324 * currently used, if running L2), and vmcs01 when running L2.
4325 */
4326static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
4327{
4328 struct vmcs02_list *item, *n;
4329 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
4330 if (vmx->loaded_vmcs != &item->vmcs02)
4331 free_loaded_vmcs(&item->vmcs02);
4332 list_del(&item->list);
4333 kfree(item);
4334 }
4335 vmx->nested.vmcs02_num = 0;
4336
4337 if (vmx->loaded_vmcs != &vmx->vmcs01)
4338 free_loaded_vmcs(&vmx->vmcs01);
4339}
4340
ec378aee
NHE
4341/*
4342 * Emulate the VMXON instruction.
4343 * Currently, we just remember that VMX is active, and do not save or even
4344 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4345 * do not currently need to store anything in that guest-allocated memory
4346 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4347 * argument is different from the VMXON pointer (which the spec says they do).
4348 */
4349static int handle_vmon(struct kvm_vcpu *vcpu)
4350{
4351 struct kvm_segment cs;
4352 struct vcpu_vmx *vmx = to_vmx(vcpu);
4353
4354 /* The Intel VMX Instruction Reference lists a bunch of bits that
4355 * are prerequisite to running VMXON, most notably cr4.VMXE must be
4356 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
4357 * Otherwise, we should fail with #UD. We test these now:
4358 */
4359 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
4360 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
4361 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4362 kvm_queue_exception(vcpu, UD_VECTOR);
4363 return 1;
4364 }
4365
4366 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4367 if (is_long_mode(vcpu) && !cs.l) {
4368 kvm_queue_exception(vcpu, UD_VECTOR);
4369 return 1;
4370 }
4371
4372 if (vmx_get_cpl(vcpu)) {
4373 kvm_inject_gp(vcpu, 0);
4374 return 1;
4375 }
4376
ff2f6fe9
NHE
4377 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
4378 vmx->nested.vmcs02_num = 0;
4379
ec378aee
NHE
4380 vmx->nested.vmxon = true;
4381
4382 skip_emulated_instruction(vcpu);
4383 return 1;
4384}
4385
4386/*
4387 * Intel's VMX Instruction Reference specifies a common set of prerequisites
4388 * for running VMX instructions (except VMXON, whose prerequisites are
4389 * slightly different). It also specifies what exception to inject otherwise.
4390 */
4391static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
4392{
4393 struct kvm_segment cs;
4394 struct vcpu_vmx *vmx = to_vmx(vcpu);
4395
4396 if (!vmx->nested.vmxon) {
4397 kvm_queue_exception(vcpu, UD_VECTOR);
4398 return 0;
4399 }
4400
4401 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4402 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
4403 (is_long_mode(vcpu) && !cs.l)) {
4404 kvm_queue_exception(vcpu, UD_VECTOR);
4405 return 0;
4406 }
4407
4408 if (vmx_get_cpl(vcpu)) {
4409 kvm_inject_gp(vcpu, 0);
4410 return 0;
4411 }
4412
4413 return 1;
4414}
4415
4416/*
4417 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
4418 * just stops using VMX.
4419 */
4420static void free_nested(struct vcpu_vmx *vmx)
4421{
4422 if (!vmx->nested.vmxon)
4423 return;
4424 vmx->nested.vmxon = false;
a9d30f33
NHE
4425 if (vmx->nested.current_vmptr != -1ull) {
4426 kunmap(vmx->nested.current_vmcs12_page);
4427 nested_release_page(vmx->nested.current_vmcs12_page);
4428 vmx->nested.current_vmptr = -1ull;
4429 vmx->nested.current_vmcs12 = NULL;
4430 }
ff2f6fe9
NHE
4431
4432 nested_free_all_saved_vmcss(vmx);
ec378aee
NHE
4433}
4434
4435/* Emulate the VMXOFF instruction */
4436static int handle_vmoff(struct kvm_vcpu *vcpu)
4437{
4438 if (!nested_vmx_check_permission(vcpu))
4439 return 1;
4440 free_nested(to_vmx(vcpu));
4441 skip_emulated_instruction(vcpu);
4442 return 1;
4443}
4444
064aea77
NHE
4445/*
4446 * Decode the memory-address operand of a vmx instruction, as recorded on an
4447 * exit caused by such an instruction (run by a guest hypervisor).
4448 * On success, returns 0. When the operand is invalid, returns 1 and throws
4449 * #UD or #GP.
4450 */
4451static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
4452 unsigned long exit_qualification,
4453 u32 vmx_instruction_info, gva_t *ret)
4454{
4455 /*
4456 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4457 * Execution", on an exit, vmx_instruction_info holds most of the
4458 * addressing components of the operand. Only the displacement part
4459 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4460 * For how an actual address is calculated from all these components,
4461 * refer to Vol. 1, "Operand Addressing".
4462 */
4463 int scaling = vmx_instruction_info & 3;
4464 int addr_size = (vmx_instruction_info >> 7) & 7;
4465 bool is_reg = vmx_instruction_info & (1u << 10);
4466 int seg_reg = (vmx_instruction_info >> 15) & 7;
4467 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4468 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4469 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4470 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4471
4472 if (is_reg) {
4473 kvm_queue_exception(vcpu, UD_VECTOR);
4474 return 1;
4475 }
4476
4477 /* Addr = segment_base + offset */
4478 /* offset = base + [index * scale] + displacement */
4479 *ret = vmx_get_segment_base(vcpu, seg_reg);
4480 if (base_is_valid)
4481 *ret += kvm_register_read(vcpu, base_reg);
4482 if (index_is_valid)
4483 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
4484 *ret += exit_qualification; /* holds the displacement */
4485
4486 if (addr_size == 1) /* 32 bit */
4487 *ret &= 0xffffffff;
4488
4489 /*
4490 * TODO: throw #GP (and return 1) in various cases that the VM*
4491 * instructions require it - e.g., offset beyond segment limit,
4492 * unusable or unreadable/unwritable segment, non-canonical 64-bit
4493 * address, and so on. Currently these are not checked.
4494 */
4495 return 0;
4496}
4497
6aa8b732
AK
4498/*
4499 * The exit handlers return 1 if the exit was handled fully and guest execution
4500 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
4501 * to be done to userspace and return 0.
4502 */
851ba692 4503static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
6aa8b732
AK
4504 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
4505 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
988ad74f 4506 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
f08864b4 4507 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
6aa8b732 4508 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
6aa8b732
AK
4509 [EXIT_REASON_CR_ACCESS] = handle_cr,
4510 [EXIT_REASON_DR_ACCESS] = handle_dr,
4511 [EXIT_REASON_CPUID] = handle_cpuid,
4512 [EXIT_REASON_MSR_READ] = handle_rdmsr,
4513 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
4514 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
4515 [EXIT_REASON_HLT] = handle_halt,
ec25d5e6 4516 [EXIT_REASON_INVD] = handle_invd,
a7052897 4517 [EXIT_REASON_INVLPG] = handle_invlpg,
c21415e8 4518 [EXIT_REASON_VMCALL] = handle_vmcall,
e3c7cb6a
AK
4519 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
4520 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
4521 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
4522 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
4523 [EXIT_REASON_VMREAD] = handle_vmx_insn,
4524 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
4525 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
ec378aee
NHE
4526 [EXIT_REASON_VMOFF] = handle_vmoff,
4527 [EXIT_REASON_VMON] = handle_vmon,
f78e0e2e
SY
4528 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
4529 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
e5edaa01 4530 [EXIT_REASON_WBINVD] = handle_wbinvd,
2acf923e 4531 [EXIT_REASON_XSETBV] = handle_xsetbv,
37817f29 4532 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
a0861c02 4533 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
68f89400
MT
4534 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
4535 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
4b8d54f9 4536 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
59708670
SY
4537 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
4538 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
6aa8b732
AK
4539};
4540
4541static const int kvm_vmx_max_exit_handlers =
50a3485c 4542 ARRAY_SIZE(kvm_vmx_exit_handlers);
6aa8b732 4543
586f9607
AK
4544static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
4545{
4546 *info1 = vmcs_readl(EXIT_QUALIFICATION);
4547 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
4548}
4549
6aa8b732
AK
4550/*
4551 * The guest has exited. See if we can fix it or if we need userspace
4552 * assistance.
4553 */
851ba692 4554static int vmx_handle_exit(struct kvm_vcpu *vcpu)
6aa8b732 4555{
29bd8a78 4556 struct vcpu_vmx *vmx = to_vmx(vcpu);
a0861c02 4557 u32 exit_reason = vmx->exit_reason;
1155f76a 4558 u32 vectoring_info = vmx->idt_vectoring_info;
29bd8a78 4559
aa17911e 4560 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
2714d1d3 4561
80ced186
MG
4562 /* If guest state is invalid, start emulating */
4563 if (vmx->emulation_required && emulate_invalid_guest_state)
4564 return handle_invalid_guest_state(vcpu);
1d5a4d9b 4565
5120702e
MG
4566 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
4567 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4568 vcpu->run->fail_entry.hardware_entry_failure_reason
4569 = exit_reason;
4570 return 0;
4571 }
4572
29bd8a78 4573 if (unlikely(vmx->fail)) {
851ba692
AK
4574 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4575 vcpu->run->fail_entry.hardware_entry_failure_reason
29bd8a78
AK
4576 = vmcs_read32(VM_INSTRUCTION_ERROR);
4577 return 0;
4578 }
6aa8b732 4579
d77c26fc 4580 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
1439442c 4581 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
60637aac
JK
4582 exit_reason != EXIT_REASON_EPT_VIOLATION &&
4583 exit_reason != EXIT_REASON_TASK_SWITCH))
4584 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
4585 "(0x%x) and exit reason is 0x%x\n",
4586 __func__, vectoring_info, exit_reason);
3b86cd99
JK
4587
4588 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
c4282df9 4589 if (vmx_interrupt_allowed(vcpu)) {
3b86cd99 4590 vmx->soft_vnmi_blocked = 0;
3b86cd99 4591 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
4531220b 4592 vcpu->arch.nmi_pending) {
3b86cd99
JK
4593 /*
4594 * This CPU don't support us in finding the end of an
4595 * NMI-blocked window if the guest runs with IRQs
4596 * disabled. So we pull the trigger after 1 s of
4597 * futile waiting, but inform the user about this.
4598 */
4599 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
4600 "state on VCPU %d after 1 s timeout\n",
4601 __func__, vcpu->vcpu_id);
4602 vmx->soft_vnmi_blocked = 0;
3b86cd99 4603 }
3b86cd99
JK
4604 }
4605
6aa8b732
AK
4606 if (exit_reason < kvm_vmx_max_exit_handlers
4607 && kvm_vmx_exit_handlers[exit_reason])
851ba692 4608 return kvm_vmx_exit_handlers[exit_reason](vcpu);
6aa8b732 4609 else {
851ba692
AK
4610 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4611 vcpu->run->hw.hardware_exit_reason = exit_reason;
6aa8b732
AK
4612 }
4613 return 0;
4614}
4615
95ba8273 4616static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
6e5d865c 4617{
95ba8273 4618 if (irr == -1 || tpr < irr) {
6e5d865c
YS
4619 vmcs_write32(TPR_THRESHOLD, 0);
4620 return;
4621 }
4622
95ba8273 4623 vmcs_write32(TPR_THRESHOLD, irr);
6e5d865c
YS
4624}
4625
51aa01d1 4626static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
cf393f75 4627{
00eba012
AK
4628 u32 exit_intr_info;
4629
4630 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
4631 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
4632 return;
4633
c5ca8e57 4634 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
00eba012 4635 exit_intr_info = vmx->exit_intr_info;
a0861c02
AK
4636
4637 /* Handle machine checks before interrupts are enabled */
00eba012 4638 if (is_machine_check(exit_intr_info))
a0861c02
AK
4639 kvm_machine_check();
4640
20f65983 4641 /* We need to handle NMIs before interrupts are enabled */
00eba012 4642 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
ff9d07a0
ZY
4643 (exit_intr_info & INTR_INFO_VALID_MASK)) {
4644 kvm_before_handle_nmi(&vmx->vcpu);
20f65983 4645 asm("int $2");
ff9d07a0
ZY
4646 kvm_after_handle_nmi(&vmx->vcpu);
4647 }
51aa01d1 4648}
20f65983 4649
51aa01d1
AK
4650static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
4651{
c5ca8e57 4652 u32 exit_intr_info;
51aa01d1
AK
4653 bool unblock_nmi;
4654 u8 vector;
4655 bool idtv_info_valid;
4656
4657 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
20f65983 4658
cf393f75 4659 if (cpu_has_virtual_nmis()) {
9d58b931
AK
4660 if (vmx->nmi_known_unmasked)
4661 return;
c5ca8e57
AK
4662 /*
4663 * Can't use vmx->exit_intr_info since we're not sure what
4664 * the exit reason is.
4665 */
4666 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
cf393f75
AK
4667 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
4668 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
4669 /*
7b4a25cb 4670 * SDM 3: 27.7.1.2 (September 2008)
cf393f75
AK
4671 * Re-set bit "block by NMI" before VM entry if vmexit caused by
4672 * a guest IRET fault.
7b4a25cb
GN
4673 * SDM 3: 23.2.2 (September 2008)
4674 * Bit 12 is undefined in any of the following cases:
4675 * If the VM exit sets the valid bit in the IDT-vectoring
4676 * information field.
4677 * If the VM exit is due to a double fault.
cf393f75 4678 */
7b4a25cb
GN
4679 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
4680 vector != DF_VECTOR && !idtv_info_valid)
cf393f75
AK
4681 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4682 GUEST_INTR_STATE_NMI);
9d58b931
AK
4683 else
4684 vmx->nmi_known_unmasked =
4685 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
4686 & GUEST_INTR_STATE_NMI);
3b86cd99
JK
4687 } else if (unlikely(vmx->soft_vnmi_blocked))
4688 vmx->vnmi_blocked_time +=
4689 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
51aa01d1
AK
4690}
4691
83422e17
AK
4692static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
4693 u32 idt_vectoring_info,
4694 int instr_len_field,
4695 int error_code_field)
51aa01d1 4696{
51aa01d1
AK
4697 u8 vector;
4698 int type;
4699 bool idtv_info_valid;
4700
4701 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
668f612f 4702
37b96e98
GN
4703 vmx->vcpu.arch.nmi_injected = false;
4704 kvm_clear_exception_queue(&vmx->vcpu);
4705 kvm_clear_interrupt_queue(&vmx->vcpu);
4706
4707 if (!idtv_info_valid)
4708 return;
4709
3842d135
AK
4710 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
4711
668f612f
AK
4712 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
4713 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
37b96e98 4714
64a7ec06 4715 switch (type) {
37b96e98
GN
4716 case INTR_TYPE_NMI_INTR:
4717 vmx->vcpu.arch.nmi_injected = true;
668f612f 4718 /*
7b4a25cb 4719 * SDM 3: 27.7.1.2 (September 2008)
37b96e98
GN
4720 * Clear bit "block by NMI" before VM entry if a NMI
4721 * delivery faulted.
668f612f 4722 */
654f06fc 4723 vmx_set_nmi_mask(&vmx->vcpu, false);
37b96e98 4724 break;
37b96e98 4725 case INTR_TYPE_SOFT_EXCEPTION:
66fd3f7f 4726 vmx->vcpu.arch.event_exit_inst_len =
83422e17 4727 vmcs_read32(instr_len_field);
66fd3f7f
GN
4728 /* fall through */
4729 case INTR_TYPE_HARD_EXCEPTION:
35920a35 4730 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
83422e17 4731 u32 err = vmcs_read32(error_code_field);
37b96e98 4732 kvm_queue_exception_e(&vmx->vcpu, vector, err);
35920a35
AK
4733 } else
4734 kvm_queue_exception(&vmx->vcpu, vector);
37b96e98 4735 break;
66fd3f7f
GN
4736 case INTR_TYPE_SOFT_INTR:
4737 vmx->vcpu.arch.event_exit_inst_len =
83422e17 4738 vmcs_read32(instr_len_field);
66fd3f7f 4739 /* fall through */
37b96e98 4740 case INTR_TYPE_EXT_INTR:
66fd3f7f
GN
4741 kvm_queue_interrupt(&vmx->vcpu, vector,
4742 type == INTR_TYPE_SOFT_INTR);
37b96e98
GN
4743 break;
4744 default:
4745 break;
f7d9238f 4746 }
cf393f75
AK
4747}
4748
83422e17
AK
4749static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
4750{
4751 __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
4752 VM_EXIT_INSTRUCTION_LEN,
4753 IDT_VECTORING_ERROR_CODE);
4754}
4755
b463a6f7
AK
4756static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
4757{
4758 __vmx_complete_interrupts(to_vmx(vcpu),
4759 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
4760 VM_ENTRY_INSTRUCTION_LEN,
4761 VM_ENTRY_EXCEPTION_ERROR_CODE);
4762
4763 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
4764}
4765
c801949d
AK
4766#ifdef CONFIG_X86_64
4767#define R "r"
4768#define Q "q"
4769#else
4770#define R "e"
4771#define Q "l"
4772#endif
4773
a3b5ba49 4774static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
6aa8b732 4775{
a2fa3e9f 4776 struct vcpu_vmx *vmx = to_vmx(vcpu);
104f226b
AK
4777
4778 /* Record the guest's net vcpu time for enforced NMI injections. */
4779 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
4780 vmx->entry_time = ktime_get();
4781
4782 /* Don't enter VMX if guest state is invalid, let the exit handler
4783 start emulation until we arrive back to a valid state */
4784 if (vmx->emulation_required && emulate_invalid_guest_state)
4785 return;
4786
4787 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
4788 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
4789 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
4790 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
4791
4792 /* When single-stepping over STI and MOV SS, we must clear the
4793 * corresponding interruptibility bits in the guest state. Otherwise
4794 * vmentry fails as it then expects bit 14 (BS) in pending debug
4795 * exceptions being set, but that's not correct for the guest debugging
4796 * case. */
4797 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
4798 vmx_set_interrupt_shadow(vcpu, 0);
4799
d462b819 4800 vmx->__launched = vmx->loaded_vmcs->launched;
104f226b 4801 asm(
6aa8b732 4802 /* Store host registers */
c801949d 4803 "push %%"R"dx; push %%"R"bp;"
40712fae 4804 "push %%"R"cx \n\t" /* placeholder for guest rcx */
c801949d 4805 "push %%"R"cx \n\t"
313dbd49
AK
4806 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
4807 "je 1f \n\t"
4808 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
4ecac3fd 4809 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
313dbd49 4810 "1: \n\t"
d3edefc0
AK
4811 /* Reload cr2 if changed */
4812 "mov %c[cr2](%0), %%"R"ax \n\t"
4813 "mov %%cr2, %%"R"dx \n\t"
4814 "cmp %%"R"ax, %%"R"dx \n\t"
4815 "je 2f \n\t"
4816 "mov %%"R"ax, %%cr2 \n\t"
4817 "2: \n\t"
6aa8b732 4818 /* Check if vmlaunch of vmresume is needed */
e08aa78a 4819 "cmpl $0, %c[launched](%0) \n\t"
6aa8b732 4820 /* Load guest registers. Don't clobber flags. */
c801949d
AK
4821 "mov %c[rax](%0), %%"R"ax \n\t"
4822 "mov %c[rbx](%0), %%"R"bx \n\t"
4823 "mov %c[rdx](%0), %%"R"dx \n\t"
4824 "mov %c[rsi](%0), %%"R"si \n\t"
4825 "mov %c[rdi](%0), %%"R"di \n\t"
4826 "mov %c[rbp](%0), %%"R"bp \n\t"
05b3e0c2 4827#ifdef CONFIG_X86_64
e08aa78a
AK
4828 "mov %c[r8](%0), %%r8 \n\t"
4829 "mov %c[r9](%0), %%r9 \n\t"
4830 "mov %c[r10](%0), %%r10 \n\t"
4831 "mov %c[r11](%0), %%r11 \n\t"
4832 "mov %c[r12](%0), %%r12 \n\t"
4833 "mov %c[r13](%0), %%r13 \n\t"
4834 "mov %c[r14](%0), %%r14 \n\t"
4835 "mov %c[r15](%0), %%r15 \n\t"
6aa8b732 4836#endif
c801949d
AK
4837 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
4838
6aa8b732 4839 /* Enter guest mode */
cd2276a7 4840 "jne .Llaunched \n\t"
4ecac3fd 4841 __ex(ASM_VMX_VMLAUNCH) "\n\t"
cd2276a7 4842 "jmp .Lkvm_vmx_return \n\t"
4ecac3fd 4843 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
cd2276a7 4844 ".Lkvm_vmx_return: "
6aa8b732 4845 /* Save guest registers, load host registers, keep flags */
40712fae
AK
4846 "mov %0, %c[wordsize](%%"R"sp) \n\t"
4847 "pop %0 \n\t"
c801949d
AK
4848 "mov %%"R"ax, %c[rax](%0) \n\t"
4849 "mov %%"R"bx, %c[rbx](%0) \n\t"
1c696d0e 4850 "pop"Q" %c[rcx](%0) \n\t"
c801949d
AK
4851 "mov %%"R"dx, %c[rdx](%0) \n\t"
4852 "mov %%"R"si, %c[rsi](%0) \n\t"
4853 "mov %%"R"di, %c[rdi](%0) \n\t"
4854 "mov %%"R"bp, %c[rbp](%0) \n\t"
05b3e0c2 4855#ifdef CONFIG_X86_64
e08aa78a
AK
4856 "mov %%r8, %c[r8](%0) \n\t"
4857 "mov %%r9, %c[r9](%0) \n\t"
4858 "mov %%r10, %c[r10](%0) \n\t"
4859 "mov %%r11, %c[r11](%0) \n\t"
4860 "mov %%r12, %c[r12](%0) \n\t"
4861 "mov %%r13, %c[r13](%0) \n\t"
4862 "mov %%r14, %c[r14](%0) \n\t"
4863 "mov %%r15, %c[r15](%0) \n\t"
6aa8b732 4864#endif
c801949d
AK
4865 "mov %%cr2, %%"R"ax \n\t"
4866 "mov %%"R"ax, %c[cr2](%0) \n\t"
4867
1c696d0e 4868 "pop %%"R"bp; pop %%"R"dx \n\t"
e08aa78a
AK
4869 "setbe %c[fail](%0) \n\t"
4870 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
d462b819 4871 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
e08aa78a 4872 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
313dbd49 4873 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
ad312c7c
ZX
4874 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
4875 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
4876 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
4877 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
4878 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
4879 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
4880 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
05b3e0c2 4881#ifdef CONFIG_X86_64
ad312c7c
ZX
4882 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
4883 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
4884 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
4885 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
4886 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
4887 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
4888 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
4889 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
6aa8b732 4890#endif
40712fae
AK
4891 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
4892 [wordsize]"i"(sizeof(ulong))
c2036300 4893 : "cc", "memory"
07d6f555 4894 , R"ax", R"bx", R"di", R"si"
c2036300 4895#ifdef CONFIG_X86_64
c2036300
LV
4896 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
4897#endif
4898 );
6aa8b732 4899
6de4f3ad 4900 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
6de12732 4901 | (1 << VCPU_EXREG_RFLAGS)
69c73028 4902 | (1 << VCPU_EXREG_CPL)
aff48baa 4903 | (1 << VCPU_EXREG_PDPTR)
2fb92db1 4904 | (1 << VCPU_EXREG_SEGMENTS)
aff48baa 4905 | (1 << VCPU_EXREG_CR3));
5fdbf976
MT
4906 vcpu->arch.regs_dirty = 0;
4907
1155f76a
AK
4908 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
4909
d77c26fc 4910 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
d462b819 4911 vmx->loaded_vmcs->launched = 1;
1b6269db 4912
51aa01d1 4913 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
51aa01d1
AK
4914
4915 vmx_complete_atomic_exit(vmx);
4916 vmx_recover_nmi_blocking(vmx);
cf393f75 4917 vmx_complete_interrupts(vmx);
6aa8b732
AK
4918}
4919
c801949d
AK
4920#undef R
4921#undef Q
4922
6aa8b732
AK
4923static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
4924{
fb3f0f51
RR
4925 struct vcpu_vmx *vmx = to_vmx(vcpu);
4926
cdbecfc3 4927 free_vpid(vmx);
ec378aee 4928 free_nested(vmx);
d462b819 4929 free_loaded_vmcs(vmx->loaded_vmcs);
fb3f0f51
RR
4930 kfree(vmx->guest_msrs);
4931 kvm_vcpu_uninit(vcpu);
a4770347 4932 kmem_cache_free(kvm_vcpu_cache, vmx);
6aa8b732
AK
4933}
4934
fb3f0f51 4935static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
6aa8b732 4936{
fb3f0f51 4937 int err;
c16f862d 4938 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
15ad7146 4939 int cpu;
6aa8b732 4940
a2fa3e9f 4941 if (!vmx)
fb3f0f51
RR
4942 return ERR_PTR(-ENOMEM);
4943
2384d2b3
SY
4944 allocate_vpid(vmx);
4945
fb3f0f51
RR
4946 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
4947 if (err)
4948 goto free_vcpu;
965b58a5 4949
a2fa3e9f 4950 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
be6d05cf 4951 err = -ENOMEM;
fb3f0f51 4952 if (!vmx->guest_msrs) {
fb3f0f51
RR
4953 goto uninit_vcpu;
4954 }
965b58a5 4955
d462b819
NHE
4956 vmx->loaded_vmcs = &vmx->vmcs01;
4957 vmx->loaded_vmcs->vmcs = alloc_vmcs();
4958 if (!vmx->loaded_vmcs->vmcs)
fb3f0f51 4959 goto free_msrs;
d462b819
NHE
4960 if (!vmm_exclusive)
4961 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
4962 loaded_vmcs_init(vmx->loaded_vmcs);
4963 if (!vmm_exclusive)
4964 kvm_cpu_vmxoff();
a2fa3e9f 4965
15ad7146
AK
4966 cpu = get_cpu();
4967 vmx_vcpu_load(&vmx->vcpu, cpu);
e48672fa 4968 vmx->vcpu.cpu = cpu;
8b9cf98c 4969 err = vmx_vcpu_setup(vmx);
fb3f0f51 4970 vmx_vcpu_put(&vmx->vcpu);
15ad7146 4971 put_cpu();
fb3f0f51
RR
4972 if (err)
4973 goto free_vmcs;
5e4a0b3c 4974 if (vm_need_virtualize_apic_accesses(kvm))
be6d05cf
JK
4975 err = alloc_apic_access_page(kvm);
4976 if (err)
5e4a0b3c 4977 goto free_vmcs;
fb3f0f51 4978
b927a3ce
SY
4979 if (enable_ept) {
4980 if (!kvm->arch.ept_identity_map_addr)
4981 kvm->arch.ept_identity_map_addr =
4982 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
93ea5388 4983 err = -ENOMEM;
b7ebfb05
SY
4984 if (alloc_identity_pagetable(kvm) != 0)
4985 goto free_vmcs;
93ea5388
GN
4986 if (!init_rmode_identity_map(kvm))
4987 goto free_vmcs;
b927a3ce 4988 }
b7ebfb05 4989
a9d30f33
NHE
4990 vmx->nested.current_vmptr = -1ull;
4991 vmx->nested.current_vmcs12 = NULL;
4992
fb3f0f51
RR
4993 return &vmx->vcpu;
4994
4995free_vmcs:
d462b819 4996 free_vmcs(vmx->loaded_vmcs->vmcs);
fb3f0f51 4997free_msrs:
fb3f0f51
RR
4998 kfree(vmx->guest_msrs);
4999uninit_vcpu:
5000 kvm_vcpu_uninit(&vmx->vcpu);
5001free_vcpu:
cdbecfc3 5002 free_vpid(vmx);
a4770347 5003 kmem_cache_free(kvm_vcpu_cache, vmx);
fb3f0f51 5004 return ERR_PTR(err);
6aa8b732
AK
5005}
5006
002c7f7c
YS
5007static void __init vmx_check_processor_compat(void *rtn)
5008{
5009 struct vmcs_config vmcs_conf;
5010
5011 *(int *)rtn = 0;
5012 if (setup_vmcs_config(&vmcs_conf) < 0)
5013 *(int *)rtn = -EIO;
5014 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
5015 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
5016 smp_processor_id());
5017 *(int *)rtn = -EIO;
5018 }
5019}
5020
67253af5
SY
5021static int get_ept_level(void)
5022{
5023 return VMX_EPT_DEFAULT_GAW + 1;
5024}
5025
4b12f0de 5026static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
64d4d521 5027{
4b12f0de
SY
5028 u64 ret;
5029
522c68c4
SY
5030 /* For VT-d and EPT combination
5031 * 1. MMIO: always map as UC
5032 * 2. EPT with VT-d:
5033 * a. VT-d without snooping control feature: can't guarantee the
5034 * result, try to trust guest.
5035 * b. VT-d with snooping control feature: snooping control feature of
5036 * VT-d engine can guarantee the cache correctness. Just set it
5037 * to WB to keep consistent with host. So the same as item 3.
a19a6d11 5038 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
522c68c4
SY
5039 * consistent with host MTRR
5040 */
4b12f0de
SY
5041 if (is_mmio)
5042 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
522c68c4
SY
5043 else if (vcpu->kvm->arch.iommu_domain &&
5044 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
5045 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
5046 VMX_EPT_MT_EPTE_SHIFT;
4b12f0de 5047 else
522c68c4 5048 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
a19a6d11 5049 | VMX_EPT_IPAT_BIT;
4b12f0de
SY
5050
5051 return ret;
64d4d521
SY
5052}
5053
f4c9e87c
AK
5054#define _ER(x) { EXIT_REASON_##x, #x }
5055
229456fc 5056static const struct trace_print_flags vmx_exit_reasons_str[] = {
f4c9e87c
AK
5057 _ER(EXCEPTION_NMI),
5058 _ER(EXTERNAL_INTERRUPT),
5059 _ER(TRIPLE_FAULT),
5060 _ER(PENDING_INTERRUPT),
5061 _ER(NMI_WINDOW),
5062 _ER(TASK_SWITCH),
5063 _ER(CPUID),
5064 _ER(HLT),
5065 _ER(INVLPG),
5066 _ER(RDPMC),
5067 _ER(RDTSC),
5068 _ER(VMCALL),
5069 _ER(VMCLEAR),
5070 _ER(VMLAUNCH),
5071 _ER(VMPTRLD),
5072 _ER(VMPTRST),
5073 _ER(VMREAD),
5074 _ER(VMRESUME),
5075 _ER(VMWRITE),
5076 _ER(VMOFF),
5077 _ER(VMON),
5078 _ER(CR_ACCESS),
5079 _ER(DR_ACCESS),
5080 _ER(IO_INSTRUCTION),
5081 _ER(MSR_READ),
5082 _ER(MSR_WRITE),
5083 _ER(MWAIT_INSTRUCTION),
5084 _ER(MONITOR_INSTRUCTION),
5085 _ER(PAUSE_INSTRUCTION),
5086 _ER(MCE_DURING_VMENTRY),
5087 _ER(TPR_BELOW_THRESHOLD),
5088 _ER(APIC_ACCESS),
5089 _ER(EPT_VIOLATION),
5090 _ER(EPT_MISCONFIG),
5091 _ER(WBINVD),
229456fc
MT
5092 { -1, NULL }
5093};
5094
f4c9e87c
AK
5095#undef _ER
5096
17cc3935 5097static int vmx_get_lpage_level(void)
344f414f 5098{
878403b7
SY
5099 if (enable_ept && !cpu_has_vmx_ept_1g_page())
5100 return PT_DIRECTORY_LEVEL;
5101 else
5102 /* For shadow and EPT supported 1GB page */
5103 return PT_PDPE_LEVEL;
344f414f
JR
5104}
5105
0e851880
SY
5106static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
5107{
4e47c7a6
SY
5108 struct kvm_cpuid_entry2 *best;
5109 struct vcpu_vmx *vmx = to_vmx(vcpu);
5110 u32 exec_control;
5111
5112 vmx->rdtscp_enabled = false;
5113 if (vmx_rdtscp_supported()) {
5114 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5115 if (exec_control & SECONDARY_EXEC_RDTSCP) {
5116 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
5117 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
5118 vmx->rdtscp_enabled = true;
5119 else {
5120 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5121 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5122 exec_control);
5123 }
5124 }
5125 }
0e851880
SY
5126}
5127
d4330ef2
JR
5128static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
5129{
5130}
5131
8a76d7f2
JR
5132static int vmx_check_intercept(struct kvm_vcpu *vcpu,
5133 struct x86_instruction_info *info,
5134 enum x86_intercept_stage stage)
5135{
5136 return X86EMUL_CONTINUE;
5137}
5138
cbdd1bea 5139static struct kvm_x86_ops vmx_x86_ops = {
6aa8b732
AK
5140 .cpu_has_kvm_support = cpu_has_kvm_support,
5141 .disabled_by_bios = vmx_disabled_by_bios,
5142 .hardware_setup = hardware_setup,
5143 .hardware_unsetup = hardware_unsetup,
002c7f7c 5144 .check_processor_compatibility = vmx_check_processor_compat,
6aa8b732
AK
5145 .hardware_enable = hardware_enable,
5146 .hardware_disable = hardware_disable,
04547156 5147 .cpu_has_accelerated_tpr = report_flexpriority,
6aa8b732
AK
5148
5149 .vcpu_create = vmx_create_vcpu,
5150 .vcpu_free = vmx_free_vcpu,
04d2cc77 5151 .vcpu_reset = vmx_vcpu_reset,
6aa8b732 5152
04d2cc77 5153 .prepare_guest_switch = vmx_save_host_state,
6aa8b732
AK
5154 .vcpu_load = vmx_vcpu_load,
5155 .vcpu_put = vmx_vcpu_put,
5156
5157 .set_guest_debug = set_guest_debug,
5158 .get_msr = vmx_get_msr,
5159 .set_msr = vmx_set_msr,
5160 .get_segment_base = vmx_get_segment_base,
5161 .get_segment = vmx_get_segment,
5162 .set_segment = vmx_set_segment,
2e4d2653 5163 .get_cpl = vmx_get_cpl,
6aa8b732 5164 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
e8467fda 5165 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
aff48baa 5166 .decache_cr3 = vmx_decache_cr3,
25c4c276 5167 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
6aa8b732 5168 .set_cr0 = vmx_set_cr0,
6aa8b732
AK
5169 .set_cr3 = vmx_set_cr3,
5170 .set_cr4 = vmx_set_cr4,
6aa8b732 5171 .set_efer = vmx_set_efer,
6aa8b732
AK
5172 .get_idt = vmx_get_idt,
5173 .set_idt = vmx_set_idt,
5174 .get_gdt = vmx_get_gdt,
5175 .set_gdt = vmx_set_gdt,
020df079 5176 .set_dr7 = vmx_set_dr7,
5fdbf976 5177 .cache_reg = vmx_cache_reg,
6aa8b732
AK
5178 .get_rflags = vmx_get_rflags,
5179 .set_rflags = vmx_set_rflags,
ebcbab4c 5180 .fpu_activate = vmx_fpu_activate,
02daab21 5181 .fpu_deactivate = vmx_fpu_deactivate,
6aa8b732
AK
5182
5183 .tlb_flush = vmx_flush_tlb,
6aa8b732 5184
6aa8b732 5185 .run = vmx_vcpu_run,
6062d012 5186 .handle_exit = vmx_handle_exit,
6aa8b732 5187 .skip_emulated_instruction = skip_emulated_instruction,
2809f5d2
GC
5188 .set_interrupt_shadow = vmx_set_interrupt_shadow,
5189 .get_interrupt_shadow = vmx_get_interrupt_shadow,
102d8325 5190 .patch_hypercall = vmx_patch_hypercall,
2a8067f1 5191 .set_irq = vmx_inject_irq,
95ba8273 5192 .set_nmi = vmx_inject_nmi,
298101da 5193 .queue_exception = vmx_queue_exception,
b463a6f7 5194 .cancel_injection = vmx_cancel_injection,
78646121 5195 .interrupt_allowed = vmx_interrupt_allowed,
95ba8273 5196 .nmi_allowed = vmx_nmi_allowed,
3cfc3092
JK
5197 .get_nmi_mask = vmx_get_nmi_mask,
5198 .set_nmi_mask = vmx_set_nmi_mask,
95ba8273
GN
5199 .enable_nmi_window = enable_nmi_window,
5200 .enable_irq_window = enable_irq_window,
5201 .update_cr8_intercept = update_cr8_intercept,
95ba8273 5202
cbc94022 5203 .set_tss_addr = vmx_set_tss_addr,
67253af5 5204 .get_tdp_level = get_ept_level,
4b12f0de 5205 .get_mt_mask = vmx_get_mt_mask,
229456fc 5206
586f9607 5207 .get_exit_info = vmx_get_exit_info,
229456fc 5208 .exit_reasons_str = vmx_exit_reasons_str,
586f9607 5209
17cc3935 5210 .get_lpage_level = vmx_get_lpage_level,
0e851880
SY
5211
5212 .cpuid_update = vmx_cpuid_update,
4e47c7a6
SY
5213
5214 .rdtscp_supported = vmx_rdtscp_supported,
d4330ef2
JR
5215
5216 .set_supported_cpuid = vmx_set_supported_cpuid,
f5f48ee1
SY
5217
5218 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
99e3e30a 5219
4051b188 5220 .set_tsc_khz = vmx_set_tsc_khz,
99e3e30a 5221 .write_tsc_offset = vmx_write_tsc_offset,
e48672fa 5222 .adjust_tsc_offset = vmx_adjust_tsc_offset,
857e4099 5223 .compute_tsc_offset = vmx_compute_tsc_offset,
1c97f0a0
JR
5224
5225 .set_tdp_cr3 = vmx_set_cr3,
8a76d7f2
JR
5226
5227 .check_intercept = vmx_check_intercept,
6aa8b732
AK
5228};
5229
5230static int __init vmx_init(void)
5231{
26bb0981
AK
5232 int r, i;
5233
5234 rdmsrl_safe(MSR_EFER, &host_efer);
5235
5236 for (i = 0; i < NR_VMX_MSR; ++i)
5237 kvm_define_shared_msr(i, vmx_msr_index[i]);
fdef3ad1 5238
3e7c73e9 5239 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
fdef3ad1
HQ
5240 if (!vmx_io_bitmap_a)
5241 return -ENOMEM;
5242
3e7c73e9 5243 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
fdef3ad1
HQ
5244 if (!vmx_io_bitmap_b) {
5245 r = -ENOMEM;
5246 goto out;
5247 }
5248
5897297b
AK
5249 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
5250 if (!vmx_msr_bitmap_legacy) {
25c5f225
SY
5251 r = -ENOMEM;
5252 goto out1;
5253 }
5254
5897297b
AK
5255 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
5256 if (!vmx_msr_bitmap_longmode) {
5257 r = -ENOMEM;
5258 goto out2;
5259 }
5260
fdef3ad1
HQ
5261 /*
5262 * Allow direct access to the PC debug port (it is often used for I/O
5263 * delays, but the vmexits simply slow things down).
5264 */
3e7c73e9
AK
5265 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
5266 clear_bit(0x80, vmx_io_bitmap_a);
fdef3ad1 5267
3e7c73e9 5268 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
fdef3ad1 5269
5897297b
AK
5270 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
5271 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
25c5f225 5272
2384d2b3
SY
5273 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
5274
0ee75bea
AK
5275 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
5276 __alignof__(struct vcpu_vmx), THIS_MODULE);
fdef3ad1 5277 if (r)
5897297b 5278 goto out3;
25c5f225 5279
5897297b
AK
5280 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
5281 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
5282 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
5283 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
5284 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
5285 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
fdef3ad1 5286
089d034e 5287 if (enable_ept) {
1439442c 5288 bypass_guest_pf = 0;
534e38b4 5289 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
4b12f0de 5290 VMX_EPT_EXECUTABLE_MASK);
5fdbcb9d
SY
5291 kvm_enable_tdp();
5292 } else
5293 kvm_disable_tdp();
1439442c 5294
c7addb90
AK
5295 if (bypass_guest_pf)
5296 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
5297
fdef3ad1
HQ
5298 return 0;
5299
5897297b
AK
5300out3:
5301 free_page((unsigned long)vmx_msr_bitmap_longmode);
25c5f225 5302out2:
5897297b 5303 free_page((unsigned long)vmx_msr_bitmap_legacy);
fdef3ad1 5304out1:
3e7c73e9 5305 free_page((unsigned long)vmx_io_bitmap_b);
fdef3ad1 5306out:
3e7c73e9 5307 free_page((unsigned long)vmx_io_bitmap_a);
fdef3ad1 5308 return r;
6aa8b732
AK
5309}
5310
5311static void __exit vmx_exit(void)
5312{
5897297b
AK
5313 free_page((unsigned long)vmx_msr_bitmap_legacy);
5314 free_page((unsigned long)vmx_msr_bitmap_longmode);
3e7c73e9
AK
5315 free_page((unsigned long)vmx_io_bitmap_b);
5316 free_page((unsigned long)vmx_io_bitmap_a);
fdef3ad1 5317
cb498ea2 5318 kvm_exit();
6aa8b732
AK
5319}
5320
5321module_init(vmx_init)
5322module_exit(vmx_exit)