]> git.ipfire.org Git - thirdparty/linux.git/blame - arch/x86/kvm/vmx/vmx.c
KVM: VMX: Move the hardware {un}setup functions to the bottom
[thirdparty/linux.git] / arch / x86 / kvm / vmx / 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
199b118a
SC
19#include <linux/frame.h>
20#include <linux/highmem.h>
21#include <linux/hrtimer.h>
22#include <linux/kernel.h>
edf88417 23#include <linux/kvm_host.h>
6aa8b732 24#include <linux/module.h>
c7addb90 25#include <linux/moduleparam.h>
e9bda3b3 26#include <linux/mod_devicetable.h>
199b118a 27#include <linux/mm.h>
199b118a 28#include <linux/sched.h>
5a0e3ad6 29#include <linux/slab.h>
cafd6659 30#include <linux/tboot.h>
199b118a 31#include <linux/trace_events.h>
e495606d 32
199b118a 33#include <asm/apic.h>
fd8ca6da 34#include <asm/asm.h>
28b835d6 35#include <asm/cpu.h>
199b118a 36#include <asm/debugreg.h>
3b3be0d1 37#include <asm/desc.h>
952f07ec 38#include <asm/fpu/internal.h>
199b118a 39#include <asm/io.h>
efc64404 40#include <asm/irq_remapping.h>
199b118a
SC
41#include <asm/kexec.h>
42#include <asm/perf_event.h>
43#include <asm/mce.h>
d6e41f11 44#include <asm/mmu_context.h>
773e8a04 45#include <asm/mshyperv.h>
199b118a
SC
46#include <asm/spec-ctrl.h>
47#include <asm/virtext.h>
48#include <asm/vmx.h>
6aa8b732 49
3077c191 50#include "capabilities.h"
199b118a 51#include "cpuid.h"
4cebd747 52#include "evmcs.h"
199b118a
SC
53#include "hyperv.h"
54#include "irq.h"
55#include "kvm_cache_regs.h"
56#include "lapic.h"
57#include "mmu.h"
89b0c9f5 58#include "ops.h"
25462f7f 59#include "pmu.h"
199b118a 60#include "trace.h"
cb1d474b 61#include "vmcs.h"
609363cf 62#include "vmcs12.h"
89b0c9f5 63#include "vmx.h"
199b118a 64#include "x86.h"
8373d25d 65#include "vmx.h"
229456fc 66
6aa8b732
AK
67MODULE_AUTHOR("Qumranet");
68MODULE_LICENSE("GPL");
69
e9bda3b3
JT
70static const struct x86_cpu_id vmx_cpu_id[] = {
71 X86_FEATURE_MATCH(X86_FEATURE_VMX),
72 {}
73};
74MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
75
2c4fd91d 76bool __read_mostly enable_vpid = 1;
736caefe 77module_param_named(vpid, enable_vpid, bool, 0444);
2384d2b3 78
d02fcf50
PB
79static bool __read_mostly enable_vnmi = 1;
80module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
81
2c4fd91d 82bool __read_mostly flexpriority_enabled = 1;
736caefe 83module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
4c9fc8ef 84
2c4fd91d 85bool __read_mostly enable_ept = 1;
736caefe 86module_param_named(ept, enable_ept, bool, S_IRUGO);
d56f546d 87
2c4fd91d 88bool __read_mostly enable_unrestricted_guest = 1;
3a624e29
NK
89module_param_named(unrestricted_guest,
90 enable_unrestricted_guest, bool, S_IRUGO);
91
2c4fd91d 92bool __read_mostly enable_ept_ad_bits = 1;
83c3a331
XH
93module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
94
a27685c3 95static bool __read_mostly emulate_invalid_guest_state = true;
c1f8bc04 96module_param(emulate_invalid_guest_state, bool, S_IRUGO);
04fa4d32 97
476bc001 98static bool __read_mostly fasteoi = 1;
58fbbf26
KT
99module_param(fasteoi, bool, S_IRUGO);
100
5a71785d 101static bool __read_mostly enable_apicv = 1;
01e439be 102module_param(enable_apicv, bool, S_IRUGO);
83d4c286 103
abc4fc58
AG
104static bool __read_mostly enable_shadow_vmcs = 1;
105module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
801d3424
NHE
106/*
107 * If nested=1, nested virtualization is supported, i.e., guests may use
108 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
109 * use VMX instructions.
110 */
1e58e5e5 111static bool __read_mostly nested = 1;
801d3424
NHE
112module_param(nested, bool, S_IRUGO);
113
52017608
SC
114static bool __read_mostly nested_early_check = 0;
115module_param(nested_early_check, bool, S_IRUGO);
116
20300099
WL
117static u64 __read_mostly host_xss;
118
2c4fd91d 119bool __read_mostly enable_pml = 1;
843e4330
KH
120module_param_named(pml, enable_pml, bool, S_IRUGO);
121
904e14fb
PB
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
904e14fb 124
64903d61
HZ
125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
64672c95
YJ
127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
3de6347b 134#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
1706bd0c
SC
135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
4c38609a
AK
139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
fd8cb433 141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
4c38609a 142
5dc1f044 143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
cdc0e244
AK
144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
78ac8b47
AK
147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
f4124500
JK
149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
16c2aec6
JD
151/*
152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
4b8d54f9
ZE
161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
00c25bce 165 * According to test, this time is usually smaller than 128 cycles.
4b8d54f9
ZE
166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
c8e88717 172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
a87c99e6 173module_param(ple_gap, uint, 0444);
b4a2d31d 174
7fbc85a5
BM
175static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
176module_param(ple_window, uint, 0444);
4b8d54f9 177
b4a2d31d 178/* Default doubles per-vcpu window every exit. */
c8e88717 179static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
7fbc85a5 180module_param(ple_window_grow, uint, 0444);
b4a2d31d
RK
181
182/* Default resets per-vcpu window every exit to ple_window. */
c8e88717 183static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
7fbc85a5 184module_param(ple_window_shrink, uint, 0444);
b4a2d31d
RK
185
186/* Default is to compute the maximum so we can never overflow. */
7fbc85a5
BM
187static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, uint, 0444);
b4a2d31d 189
83287ea4 190extern const ulong vmx_return;
52017608 191extern const ulong vmx_early_consistency_check_return;
83287ea4 192
a399477e 193static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
427362a1 194static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
dd4bfa73 195static DEFINE_MUTEX(vmx_l1d_flush_mutex);
a399477e 196
7db92e16
TG
197/* Storage for pre module init parameter parsing */
198static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
a399477e
KRW
199
200static const struct {
201 const char *option;
0027ff2a 202 bool for_parse;
a399477e 203} vmentry_l1d_param[] = {
0027ff2a
PB
204 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
205 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
206 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
207 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
208 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
209 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
a399477e
KRW
210};
211
7db92e16
TG
212#define L1D_CACHE_ORDER 4
213static void *vmx_l1d_flush_pages;
214
215static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
a399477e 216{
7db92e16 217 struct page *page;
288d152c 218 unsigned int i;
a399477e 219
7db92e16
TG
220 if (!enable_ept) {
221 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
222 return 0;
a399477e
KRW
223 }
224
d806afa4
YW
225 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
226 u64 msr;
227
228 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
229 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
230 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
231 return 0;
232 }
233 }
8e0b2b91 234
d90a7a0e
JK
235 /* If set to auto use the default l1tf mitigation method */
236 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
237 switch (l1tf_mitigation) {
238 case L1TF_MITIGATION_OFF:
239 l1tf = VMENTER_L1D_FLUSH_NEVER;
240 break;
241 case L1TF_MITIGATION_FLUSH_NOWARN:
242 case L1TF_MITIGATION_FLUSH:
243 case L1TF_MITIGATION_FLUSH_NOSMT:
244 l1tf = VMENTER_L1D_FLUSH_COND;
245 break;
246 case L1TF_MITIGATION_FULL:
247 case L1TF_MITIGATION_FULL_FORCE:
248 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
249 break;
250 }
251 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
252 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
253 }
254
7db92e16
TG
255 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
256 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
257 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
258 if (!page)
259 return -ENOMEM;
260 vmx_l1d_flush_pages = page_address(page);
288d152c
NS
261
262 /*
263 * Initialize each page with a different pattern in
264 * order to protect against KSM in the nested
265 * virtualization case.
266 */
267 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
268 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
269 PAGE_SIZE);
270 }
7db92e16
TG
271 }
272
273 l1tf_vmx_mitigation = l1tf;
274
895ae47f
TG
275 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
276 static_branch_enable(&vmx_l1d_should_flush);
277 else
278 static_branch_disable(&vmx_l1d_should_flush);
4c6523ec 279
427362a1
NS
280 if (l1tf == VMENTER_L1D_FLUSH_COND)
281 static_branch_enable(&vmx_l1d_flush_cond);
895ae47f 282 else
427362a1 283 static_branch_disable(&vmx_l1d_flush_cond);
7db92e16
TG
284 return 0;
285}
286
287static int vmentry_l1d_flush_parse(const char *s)
288{
289 unsigned int i;
290
291 if (s) {
292 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
0027ff2a
PB
293 if (vmentry_l1d_param[i].for_parse &&
294 sysfs_streq(s, vmentry_l1d_param[i].option))
295 return i;
7db92e16
TG
296 }
297 }
a399477e
KRW
298 return -EINVAL;
299}
300
7db92e16
TG
301static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
302{
dd4bfa73 303 int l1tf, ret;
7db92e16 304
7db92e16
TG
305 l1tf = vmentry_l1d_flush_parse(s);
306 if (l1tf < 0)
307 return l1tf;
308
0027ff2a
PB
309 if (!boot_cpu_has(X86_BUG_L1TF))
310 return 0;
311
7db92e16
TG
312 /*
313 * Has vmx_init() run already? If not then this is the pre init
314 * parameter parsing. In that case just store the value and let
315 * vmx_init() do the proper setup after enable_ept has been
316 * established.
317 */
318 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
319 vmentry_l1d_flush_param = l1tf;
320 return 0;
321 }
322
dd4bfa73
TG
323 mutex_lock(&vmx_l1d_flush_mutex);
324 ret = vmx_setup_l1d_flush(l1tf);
325 mutex_unlock(&vmx_l1d_flush_mutex);
326 return ret;
7db92e16
TG
327}
328
a399477e
KRW
329static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
330{
0027ff2a
PB
331 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
332 return sprintf(s, "???\n");
333
7db92e16 334 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
a399477e
KRW
335}
336
337static const struct kernel_param_ops vmentry_l1d_flush_ops = {
338 .set = vmentry_l1d_flush_set,
339 .get = vmentry_l1d_flush_get,
340};
895ae47f 341module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
a399477e 342
44900ba6 343static u16 shadow_read_only_fields[] = {
c9e9deae 344#define SHADOW_FIELD_RO(x) x,
e0123119 345#include "vmcs_shadow_fields.h"
4607c2d7 346};
fe2b201b 347static int max_shadow_read_only_fields =
4607c2d7
AG
348 ARRAY_SIZE(shadow_read_only_fields);
349
44900ba6 350static u16 shadow_read_write_fields[] = {
c9e9deae 351#define SHADOW_FIELD_RW(x) x,
e0123119 352#include "vmcs_shadow_fields.h"
4607c2d7 353};
fe2b201b 354static int max_shadow_read_write_fields =
4607c2d7
AG
355 ARRAY_SIZE(shadow_read_write_fields);
356
a9d30f33
NHE
357static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
358{
4f2777bc 359 return to_vmx(vcpu)->nested.cached_vmcs12;
a9d30f33
NHE
360}
361
61ada748
LA
362static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
363{
364 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
365}
366
995f00a6 367static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
bfd0a56b 368static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
b246dd5d
OW
369static void vmx_set_segment(struct kvm_vcpu *vcpu,
370 struct kvm_segment *var, int seg);
371static void vmx_get_segment(struct kvm_vcpu *vcpu,
372 struct kvm_segment *var, int seg);
d99e4152
GN
373static bool guest_state_valid(struct kvm_vcpu *vcpu);
374static u32 vmx_segment_access_rights(struct kvm_segment *var);
16f5b903 375static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
b96fb439
PB
376static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
377static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
378static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
379 u16 error_code);
904e14fb 380static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
1e4329ee 381static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
15d45071 382 u32 msr, int type);
75880a01 383
6aa8b732 384static DEFINE_PER_CPU(struct vmcs *, vmxarea);
75edce8a 385DEFINE_PER_CPU(struct vmcs *, current_vmcs);
d462b819
NHE
386/*
387 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
388 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
389 */
390static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
6aa8b732 391
bf9f6ac8
FW
392/*
393 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
394 * can find which vCPU should be waken up.
395 */
396static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
397static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
398
23611332 399enum {
23611332
RK
400 VMX_VMREAD_BITMAP,
401 VMX_VMWRITE_BITMAP,
402 VMX_BITMAP_NR
403};
404
405static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
406
23611332
RK
407#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
408#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
fdef3ad1 409
2384d2b3
SY
410static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
411static DEFINE_SPINLOCK(vmx_vpid_lock);
412
3077c191
SC
413struct vmcs_config vmcs_config;
414struct vmx_capability vmx_capability;
d56f546d 415
6aa8b732
AK
416#define VMX_SEGMENT_FIELD(seg) \
417 [VCPU_SREG_##seg] = { \
418 .selector = GUEST_##seg##_SELECTOR, \
419 .base = GUEST_##seg##_BASE, \
420 .limit = GUEST_##seg##_LIMIT, \
421 .ar_bytes = GUEST_##seg##_AR_BYTES, \
422 }
423
772e0318 424static const struct kvm_vmx_segment_field {
6aa8b732
AK
425 unsigned selector;
426 unsigned base;
427 unsigned limit;
428 unsigned ar_bytes;
429} kvm_vmx_segment_fields[] = {
430 VMX_SEGMENT_FIELD(CS),
431 VMX_SEGMENT_FIELD(DS),
432 VMX_SEGMENT_FIELD(ES),
433 VMX_SEGMENT_FIELD(FS),
434 VMX_SEGMENT_FIELD(GS),
435 VMX_SEGMENT_FIELD(SS),
436 VMX_SEGMENT_FIELD(TR),
437 VMX_SEGMENT_FIELD(LDTR),
438};
439
26bb0981
AK
440static u64 host_efer;
441
6de4f3ad
AK
442static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
443
4d56c8a7 444/*
8c06585d 445 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
4d56c8a7
AK
446 * away by decrementing the array size.
447 */
6aa8b732 448static const u32 vmx_msr_index[] = {
05b3e0c2 449#ifdef CONFIG_X86_64
44ea2b17 450 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
6aa8b732 451#endif
8c06585d 452 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
6aa8b732 453};
6aa8b732 454
773e8a04
VK
455#if IS_ENABLED(CONFIG_HYPERV)
456static bool __read_mostly enlightened_vmcs = true;
457module_param(enlightened_vmcs, bool, 0444);
458
877ad952
TL
459/* check_ept_pointer() should be under protection of ept_pointer_lock. */
460static void check_ept_pointer_match(struct kvm *kvm)
461{
462 struct kvm_vcpu *vcpu;
463 u64 tmp_eptp = INVALID_PAGE;
464 int i;
465
466 kvm_for_each_vcpu(i, vcpu, kvm) {
467 if (!VALID_PAGE(tmp_eptp)) {
468 tmp_eptp = to_vmx(vcpu)->ept_pointer;
469 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
470 to_kvm_vmx(kvm)->ept_pointers_match
471 = EPT_POINTERS_MISMATCH;
472 return;
473 }
474 }
475
476 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
477}
478
479static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
480{
a5c214da
LT
481 struct kvm_vcpu *vcpu;
482 int ret = -ENOTSUPP, i;
877ad952
TL
483
484 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
485
486 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
487 check_ept_pointer_match(kvm);
488
5f8bb004
VK
489 /*
490 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
491 * base of EPT PML4 table, strip off EPT configuration information.
492 */
877ad952 493 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
a5c214da
LT
494 kvm_for_each_vcpu(i, vcpu, kvm)
495 ret |= hyperv_flush_guest_mapping(
0d1e8b8d 496 to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
a5c214da
LT
497 } else {
498 ret = hyperv_flush_guest_mapping(
0d1e8b8d 499 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
877ad952 500 }
877ad952 501
877ad952
TL
502 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
503 return ret;
504}
773e8a04
VK
505#endif /* IS_ENABLED(CONFIG_HYPERV) */
506
64672c95
YJ
507/*
508 * Comment's format: document - errata name - stepping - processor name.
509 * Refer from
510 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
511 */
512static u32 vmx_preemption_cpu_tfms[] = {
513/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
5140x000206E6,
515/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
516/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
517/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
5180x00020652,
519/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
5200x00020655,
521/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
522/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
523/*
524 * 320767.pdf - AAP86 - B1 -
525 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
526 */
5270x000106E5,
528/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
5290x000106A0,
530/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
5310x000106A1,
532/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
5330x000106A4,
534 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
535 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
536 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
5370x000106A5,
538};
539
540static inline bool cpu_has_broken_vmx_preemption_timer(void)
541{
542 u32 eax = cpuid_eax(0x00000001), i;
543
544 /* Clear the reserved bits */
545 eax &= ~(0x3U << 14 | 0xfU << 28);
03f6a22a 546 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
64672c95
YJ
547 if (eax == vmx_preemption_cpu_tfms[i])
548 return true;
549
550 return false;
551}
552
35754c98 553static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
f78e0e2e 554{
35754c98 555 return flexpriority_enabled && lapic_in_kernel(vcpu);
f78e0e2e
SY
556}
557
04547156
SY
558static inline bool report_flexpriority(void)
559{
560 return flexpriority_enabled;
561}
562
c7c2c709
JM
563static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
564{
6677f3da 565 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
c7c2c709
JM
566}
567
f4160e45
JM
568/*
569 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
570 * to modify any valid field of the VMCS, or are the VM-exit
571 * information fields read-only?
572 */
573static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
574{
575 return to_vmx(vcpu)->nested.msrs.misc_low &
576 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
577}
578
0447378a
MO
579static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
580{
581 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
582}
583
584static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
585{
586 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
587 CPU_BASED_MONITOR_TRAP_FLAG;
588}
589
fa97d7db
LA
590static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
591{
592 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
593 SECONDARY_EXEC_SHADOW_VMCS;
594}
595
fe3ef05c
NHE
596static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
597{
598 return vmcs12->cpu_based_vm_exec_control & bit;
599}
600
601static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
602{
603 return (vmcs12->cpu_based_vm_exec_control &
604 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
605 (vmcs12->secondary_vm_exec_control & bit);
606}
607
f4124500
JK
608static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
609{
610 return vmcs12->pin_based_vm_exec_control &
611 PIN_BASED_VMX_PREEMPTION_TIMER;
612}
613
0c7f650e
KS
614static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
615{
616 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
617}
618
619static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
620{
621 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
622}
623
155a97a3
NHE
624static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
625{
626 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
627}
628
81dc01f7
WL
629static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
630{
3db13480 631 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
81dc01f7
WL
632}
633
c5f983f6
BD
634static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
635{
636 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
637}
638
f2b93280
WV
639static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
640{
641 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
642}
643
5c614b35
WL
644static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
645{
646 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
647}
648
82f0dd4b
WV
649static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
650{
651 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
652}
653
608406e2
WV
654static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
655{
656 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
657}
658
705699a1
WV
659static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
660{
661 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
662}
663
27c42a1b
BD
664static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
665{
666 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
667}
668
41ab9372
BD
669static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
670{
671 return nested_cpu_has_vmfunc(vmcs12) &&
672 (vmcs12->vm_function_control &
673 VMX_VMFUNC_EPTP_SWITCHING);
674}
675
f792d274
LA
676static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
677{
678 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
679}
680
14aa61d0
KS
681static inline bool nested_cpu_has_save_preemption_timer(struct vmcs12 *vmcs12)
682{
683 return vmcs12->vm_exit_controls &
684 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
685}
686
533558bc
JK
687static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
688 u32 exit_intr_info,
689 unsigned long exit_qualification);
7c177938 690
8b9cf98c 691static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
7725f0ba
AK
692{
693 int i;
694
a2fa3e9f 695 for (i = 0; i < vmx->nmsrs; ++i)
26bb0981 696 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
a75beee6
ED
697 return i;
698 return -1;
699}
700
26bb0981 701static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
a75beee6
ED
702{
703 int i;
704
8b9cf98c 705 i = __find_msr_index(vmx, msr);
a75beee6 706 if (i >= 0)
a2fa3e9f 707 return &vmx->guest_msrs[i];
8b6d44c7 708 return NULL;
7725f0ba
AK
709}
710
89b0c9f5 711void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
d462b819
NHE
712{
713 vmcs_clear(loaded_vmcs->vmcs);
355f4fb1
JM
714 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
715 vmcs_clear(loaded_vmcs->shadow_vmcs);
d462b819
NHE
716 loaded_vmcs->cpu = -1;
717 loaded_vmcs->launched = 0;
718}
719
2965faa5 720#ifdef CONFIG_KEXEC_CORE
8f536b76
ZY
721/*
722 * This bitmap is used to indicate whether the vmclear
723 * operation is enabled on all cpus. All disabled by
724 * default.
725 */
726static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
727
728static inline void crash_enable_local_vmclear(int cpu)
729{
730 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
731}
732
733static inline void crash_disable_local_vmclear(int cpu)
734{
735 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
736}
737
738static inline int crash_local_vmclear_enabled(int cpu)
739{
740 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
741}
742
743static void crash_vmclear_local_loaded_vmcss(void)
744{
745 int cpu = raw_smp_processor_id();
746 struct loaded_vmcs *v;
747
748 if (!crash_local_vmclear_enabled(cpu))
749 return;
750
751 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
752 loaded_vmcss_on_cpu_link)
753 vmcs_clear(v->vmcs);
754}
755#else
756static inline void crash_enable_local_vmclear(int cpu) { }
757static inline void crash_disable_local_vmclear(int cpu) { }
2965faa5 758#endif /* CONFIG_KEXEC_CORE */
8f536b76 759
d462b819 760static void __loaded_vmcs_clear(void *arg)
6aa8b732 761{
d462b819 762 struct loaded_vmcs *loaded_vmcs = arg;
d3b2c338 763 int cpu = raw_smp_processor_id();
6aa8b732 764
d462b819
NHE
765 if (loaded_vmcs->cpu != cpu)
766 return; /* vcpu migration can race with cpu offline */
767 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
6aa8b732 768 per_cpu(current_vmcs, cpu) = NULL;
8f536b76 769 crash_disable_local_vmclear(cpu);
d462b819 770 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
5a560f8b
XG
771
772 /*
773 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
774 * is before setting loaded_vmcs->vcpu to -1 which is done in
775 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
776 * then adds the vmcs into percpu list before it is deleted.
777 */
778 smp_wmb();
779
d462b819 780 loaded_vmcs_init(loaded_vmcs);
8f536b76 781 crash_enable_local_vmclear(cpu);
6aa8b732
AK
782}
783
89b0c9f5 784void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
8d0be2b3 785{
e6c7d321
XG
786 int cpu = loaded_vmcs->cpu;
787
788 if (cpu != -1)
789 smp_call_function_single(cpu,
790 __loaded_vmcs_clear, loaded_vmcs, 1);
8d0be2b3
AK
791}
792
2fb92db1
AK
793static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
794 unsigned field)
795{
796 bool ret;
797 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
798
799 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
800 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
801 vmx->segment_cache.bitmask = 0;
802 }
803 ret = vmx->segment_cache.bitmask & mask;
804 vmx->segment_cache.bitmask |= mask;
805 return ret;
806}
807
808static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
809{
810 u16 *p = &vmx->segment_cache.seg[seg].selector;
811
812 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
813 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
814 return *p;
815}
816
817static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
818{
819 ulong *p = &vmx->segment_cache.seg[seg].base;
820
821 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
822 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
823 return *p;
824}
825
826static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
827{
828 u32 *p = &vmx->segment_cache.seg[seg].limit;
829
830 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
831 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
832 return *p;
833}
834
835static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
836{
837 u32 *p = &vmx->segment_cache.seg[seg].ar;
838
839 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
840 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
841 return *p;
842}
843
abd3f2d6
AK
844static void update_exception_bitmap(struct kvm_vcpu *vcpu)
845{
846 u32 eb;
847
fd7373cc 848 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
bd7e5b08 849 (1u << DB_VECTOR) | (1u << AC_VECTOR);
9e869480
LA
850 /*
851 * Guest access to VMware backdoor ports could legitimately
852 * trigger #GP because of TSS I/O permission bitmap.
853 * We intercept those #GP and allow access to them anyway
854 * as VMware does.
855 */
856 if (enable_vmware_backdoor)
857 eb |= (1u << GP_VECTOR);
fd7373cc
JK
858 if ((vcpu->guest_debug &
859 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
860 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
861 eb |= 1u << BP_VECTOR;
7ffd92c5 862 if (to_vmx(vcpu)->rmode.vm86_active)
abd3f2d6 863 eb = ~0;
089d034e 864 if (enable_ept)
1439442c 865 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
36cf24e0
NHE
866
867 /* When we are running a nested L2 guest and L1 specified for it a
868 * certain exception bitmap, we must trap the same exceptions and pass
869 * them to L1. When running L2, we will only handle the exceptions
870 * specified above if L1 did not want them.
871 */
872 if (is_guest_mode(vcpu))
873 eb |= get_vmcs12(vcpu)->exception_bitmap;
874
abd3f2d6
AK
875 vmcs_write32(EXCEPTION_BITMAP, eb);
876}
877
d28b387f
KA
878/*
879 * Check if MSR is intercepted for currently loaded MSR bitmap.
880 */
881static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
882{
883 unsigned long *msr_bitmap;
884 int f = sizeof(unsigned long);
885
886 if (!cpu_has_vmx_msr_bitmap())
887 return true;
888
889 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
890
891 if (msr <= 0x1fff) {
892 return !!test_bit(msr, msr_bitmap + 0x800 / f);
893 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
894 msr &= 0x1fff;
895 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
896 }
897
898 return true;
899}
900
15d45071
AR
901/*
902 * Check if MSR is intercepted for L01 MSR bitmap.
903 */
904static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
905{
906 unsigned long *msr_bitmap;
907 int f = sizeof(unsigned long);
908
909 if (!cpu_has_vmx_msr_bitmap())
910 return true;
911
912 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
913
914 if (msr <= 0x1fff) {
915 return !!test_bit(msr, msr_bitmap + 0x800 / f);
916 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
917 msr &= 0x1fff;
918 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
919 }
920
921 return true;
922}
923
2961e876
GN
924static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
925 unsigned long entry, unsigned long exit)
8bf00a52 926{
2961e876
GN
927 vm_entry_controls_clearbit(vmx, entry);
928 vm_exit_controls_clearbit(vmx, exit);
8bf00a52
GN
929}
930
ca83b4a7
KRW
931static int find_msr(struct vmx_msrs *m, unsigned int msr)
932{
933 unsigned int i;
934
935 for (i = 0; i < m->nr; ++i) {
936 if (m->val[i].index == msr)
937 return i;
938 }
939 return -ENOENT;
940}
941
61d2ef2c
AK
942static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
943{
ca83b4a7 944 int i;
61d2ef2c
AK
945 struct msr_autoload *m = &vmx->msr_autoload;
946
8bf00a52
GN
947 switch (msr) {
948 case MSR_EFER:
c73da3fc 949 if (cpu_has_load_ia32_efer()) {
2961e876
GN
950 clear_atomic_switch_msr_special(vmx,
951 VM_ENTRY_LOAD_IA32_EFER,
8bf00a52
GN
952 VM_EXIT_LOAD_IA32_EFER);
953 return;
954 }
955 break;
956 case MSR_CORE_PERF_GLOBAL_CTRL:
c73da3fc 957 if (cpu_has_load_perf_global_ctrl()) {
2961e876 958 clear_atomic_switch_msr_special(vmx,
8bf00a52
GN
959 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
960 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
961 return;
962 }
963 break;
110312c8 964 }
ca83b4a7
KRW
965 i = find_msr(&m->guest, msr);
966 if (i < 0)
31907093 967 goto skip_guest;
33966dd6 968 --m->guest.nr;
33966dd6 969 m->guest.val[i] = m->guest.val[m->guest.nr];
33966dd6 970 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
110312c8 971
31907093
KRW
972skip_guest:
973 i = find_msr(&m->host, msr);
974 if (i < 0)
61d2ef2c 975 return;
31907093
KRW
976
977 --m->host.nr;
978 m->host.val[i] = m->host.val[m->host.nr];
33966dd6 979 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
61d2ef2c
AK
980}
981
2961e876
GN
982static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
983 unsigned long entry, unsigned long exit,
984 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
985 u64 guest_val, u64 host_val)
8bf00a52
GN
986{
987 vmcs_write64(guest_val_vmcs, guest_val);
5a5e8a15
SC
988 if (host_val_vmcs != HOST_IA32_EFER)
989 vmcs_write64(host_val_vmcs, host_val);
2961e876
GN
990 vm_entry_controls_setbit(vmx, entry);
991 vm_exit_controls_setbit(vmx, exit);
8bf00a52
GN
992}
993
61d2ef2c 994static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
989e3992 995 u64 guest_val, u64 host_val, bool entry_only)
61d2ef2c 996{
989e3992 997 int i, j = 0;
61d2ef2c
AK
998 struct msr_autoload *m = &vmx->msr_autoload;
999
8bf00a52
GN
1000 switch (msr) {
1001 case MSR_EFER:
c73da3fc 1002 if (cpu_has_load_ia32_efer()) {
2961e876
GN
1003 add_atomic_switch_msr_special(vmx,
1004 VM_ENTRY_LOAD_IA32_EFER,
8bf00a52
GN
1005 VM_EXIT_LOAD_IA32_EFER,
1006 GUEST_IA32_EFER,
1007 HOST_IA32_EFER,
1008 guest_val, host_val);
1009 return;
1010 }
1011 break;
1012 case MSR_CORE_PERF_GLOBAL_CTRL:
c73da3fc 1013 if (cpu_has_load_perf_global_ctrl()) {
2961e876 1014 add_atomic_switch_msr_special(vmx,
8bf00a52
GN
1015 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1016 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1017 GUEST_IA32_PERF_GLOBAL_CTRL,
1018 HOST_IA32_PERF_GLOBAL_CTRL,
1019 guest_val, host_val);
1020 return;
1021 }
1022 break;
7099e2e1
RK
1023 case MSR_IA32_PEBS_ENABLE:
1024 /* PEBS needs a quiescent period after being disabled (to write
1025 * a record). Disabling PEBS through VMX MSR swapping doesn't
1026 * provide that period, so a CPU could write host's record into
1027 * guest's memory.
1028 */
1029 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
110312c8
AK
1030 }
1031
ca83b4a7 1032 i = find_msr(&m->guest, msr);
989e3992
KRW
1033 if (!entry_only)
1034 j = find_msr(&m->host, msr);
61d2ef2c 1035
31907093 1036 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
60266204 1037 printk_once(KERN_WARNING "Not enough msr switch entries. "
e7fc6f93
GN
1038 "Can't add msr %x\n", msr);
1039 return;
61d2ef2c 1040 }
31907093 1041 if (i < 0) {
ca83b4a7 1042 i = m->guest.nr++;
33966dd6 1043 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
31907093 1044 }
989e3992
KRW
1045 m->guest.val[i].index = msr;
1046 m->guest.val[i].value = guest_val;
1047
1048 if (entry_only)
1049 return;
61d2ef2c 1050
31907093
KRW
1051 if (j < 0) {
1052 j = m->host.nr++;
33966dd6 1053 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
61d2ef2c 1054 }
31907093
KRW
1055 m->host.val[j].index = msr;
1056 m->host.val[j].value = host_val;
61d2ef2c
AK
1057}
1058
92c0d900 1059static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2cc51560 1060{
844a5fe2
PB
1061 u64 guest_efer = vmx->vcpu.arch.efer;
1062 u64 ignore_bits = 0;
1063
1064 if (!enable_ept) {
1065 /*
1066 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
1067 * host CPUID is more efficient than testing guest CPUID
1068 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
1069 */
1070 if (boot_cpu_has(X86_FEATURE_SMEP))
1071 guest_efer |= EFER_NX;
1072 else if (!(guest_efer & EFER_NX))
1073 ignore_bits |= EFER_NX;
1074 }
3a34a881 1075
51c6cf66 1076 /*
844a5fe2 1077 * LMA and LME handled by hardware; SCE meaningless outside long mode.
51c6cf66 1078 */
844a5fe2 1079 ignore_bits |= EFER_SCE;
51c6cf66
AK
1080#ifdef CONFIG_X86_64
1081 ignore_bits |= EFER_LMA | EFER_LME;
1082 /* SCE is meaningful only in long mode on Intel */
1083 if (guest_efer & EFER_LMA)
1084 ignore_bits &= ~(u64)EFER_SCE;
1085#endif
84ad33ef 1086
f6577a5f
AL
1087 /*
1088 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1089 * On CPUs that support "load IA32_EFER", always switch EFER
1090 * atomically, since it's faster than switching it manually.
1091 */
c73da3fc 1092 if (cpu_has_load_ia32_efer() ||
f6577a5f 1093 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
84ad33ef
AK
1094 if (!(guest_efer & EFER_LMA))
1095 guest_efer &= ~EFER_LME;
54b98bff
AL
1096 if (guest_efer != host_efer)
1097 add_atomic_switch_msr(vmx, MSR_EFER,
989e3992 1098 guest_efer, host_efer, false);
02343cf2
SC
1099 else
1100 clear_atomic_switch_msr(vmx, MSR_EFER);
84ad33ef 1101 return false;
844a5fe2 1102 } else {
02343cf2
SC
1103 clear_atomic_switch_msr(vmx, MSR_EFER);
1104
844a5fe2
PB
1105 guest_efer &= ~ignore_bits;
1106 guest_efer |= host_efer & ignore_bits;
1107
1108 vmx->guest_msrs[efer_offset].data = guest_efer;
1109 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
84ad33ef 1110
844a5fe2
PB
1111 return true;
1112 }
51c6cf66
AK
1113}
1114
e28baead
AL
1115#ifdef CONFIG_X86_32
1116/*
1117 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1118 * VMCS rather than the segment table. KVM uses this helper to figure
1119 * out the current bases to poke them into the VMCS before entry.
1120 */
2d49ec72
GN
1121static unsigned long segment_base(u16 selector)
1122{
8c2e41f7 1123 struct desc_struct *table;
2d49ec72
GN
1124 unsigned long v;
1125
8c2e41f7 1126 if (!(selector & ~SEGMENT_RPL_MASK))
2d49ec72
GN
1127 return 0;
1128
45fc8757 1129 table = get_current_gdt_ro();
2d49ec72 1130
8c2e41f7 1131 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2d49ec72
GN
1132 u16 ldt_selector = kvm_read_ldt();
1133
8c2e41f7 1134 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2d49ec72
GN
1135 return 0;
1136
8c2e41f7 1137 table = (struct desc_struct *)segment_base(ldt_selector);
2d49ec72 1138 }
8c2e41f7 1139 v = get_desc_base(&table[selector >> 3]);
2d49ec72
GN
1140 return v;
1141}
e28baead 1142#endif
2d49ec72 1143
6d6095bd 1144static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
33ed6329 1145{
04d2cc77 1146 struct vcpu_vmx *vmx = to_vmx(vcpu);
d7ee039e 1147 struct vmcs_host_state *host_state;
51e8a8cc 1148#ifdef CONFIG_X86_64
35060ed6 1149 int cpu = raw_smp_processor_id();
51e8a8cc 1150#endif
e368b875
SC
1151 unsigned long fs_base, gs_base;
1152 u16 fs_sel, gs_sel;
26bb0981 1153 int i;
04d2cc77 1154
d264ee0c
SC
1155 vmx->req_immediate_exit = false;
1156
f48b4711
LA
1157 /*
1158 * Note that guest MSRs to be saved/restored can also be changed
1159 * when guest state is loaded. This happens when guest transitions
1160 * to/from long-mode by setting MSR_EFER.LMA.
1161 */
1162 if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
1163 vmx->guest_msrs_dirty = false;
1164 for (i = 0; i < vmx->save_nmsrs; ++i)
1165 kvm_set_shared_msr(vmx->guest_msrs[i].index,
1166 vmx->guest_msrs[i].data,
1167 vmx->guest_msrs[i].mask);
1168
1169 }
1170
bd9966de 1171 if (vmx->loaded_cpu_state)
33ed6329
AK
1172 return;
1173
bd9966de 1174 vmx->loaded_cpu_state = vmx->loaded_vmcs;
d7ee039e 1175 host_state = &vmx->loaded_cpu_state->host_state;
bd9966de 1176
33ed6329
AK
1177 /*
1178 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1179 * allow segment selectors with cpl > 0 or ti == 1.
1180 */
d7ee039e 1181 host_state->ldt_sel = kvm_read_ldt();
42b933b5
VK
1182
1183#ifdef CONFIG_X86_64
d7ee039e
SC
1184 savesegment(ds, host_state->ds_sel);
1185 savesegment(es, host_state->es_sel);
e368b875
SC
1186
1187 gs_base = cpu_kernelmode_gs_base(cpu);
b062b794
VK
1188 if (likely(is_64bit_mm(current->mm))) {
1189 save_fsgs_for_kvm();
e368b875
SC
1190 fs_sel = current->thread.fsindex;
1191 gs_sel = current->thread.gsindex;
b062b794 1192 fs_base = current->thread.fsbase;
e368b875 1193 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
b062b794 1194 } else {
e368b875
SC
1195 savesegment(fs, fs_sel);
1196 savesegment(gs, gs_sel);
b062b794 1197 fs_base = read_msr(MSR_FS_BASE);
e368b875 1198 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
33ed6329 1199 }
b2da15ac 1200
4679b61f 1201 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
4fde8d57 1202#else
e368b875
SC
1203 savesegment(fs, fs_sel);
1204 savesegment(gs, gs_sel);
1205 fs_base = segment_base(fs_sel);
1206 gs_base = segment_base(gs_sel);
707c0874 1207#endif
e368b875 1208
8f21a0bb
SC
1209 if (unlikely(fs_sel != host_state->fs_sel)) {
1210 if (!(fs_sel & 7))
1211 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1212 else
1213 vmcs_write16(HOST_FS_SELECTOR, 0);
1214 host_state->fs_sel = fs_sel;
1215 }
1216 if (unlikely(gs_sel != host_state->gs_sel)) {
1217 if (!(gs_sel & 7))
1218 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1219 else
1220 vmcs_write16(HOST_GS_SELECTOR, 0);
1221 host_state->gs_sel = gs_sel;
1222 }
5e079c7e
SC
1223 if (unlikely(fs_base != host_state->fs_base)) {
1224 vmcs_writel(HOST_FS_BASE, fs_base);
1225 host_state->fs_base = fs_base;
1226 }
1227 if (unlikely(gs_base != host_state->gs_base)) {
1228 vmcs_writel(HOST_GS_BASE, gs_base);
1229 host_state->gs_base = gs_base;
1230 }
33ed6329
AK
1231}
1232
6d6095bd 1233static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
33ed6329 1234{
d7ee039e
SC
1235 struct vmcs_host_state *host_state;
1236
bd9966de 1237 if (!vmx->loaded_cpu_state)
33ed6329
AK
1238 return;
1239
bd9966de 1240 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
d7ee039e 1241 host_state = &vmx->loaded_cpu_state->host_state;
bd9966de 1242
e1beb1d3 1243 ++vmx->vcpu.stat.host_state_reload;
bd9966de
SC
1244 vmx->loaded_cpu_state = NULL;
1245
c8770e7b 1246#ifdef CONFIG_X86_64
4679b61f 1247 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
c8770e7b 1248#endif
d7ee039e
SC
1249 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1250 kvm_load_ldt(host_state->ldt_sel);
33ed6329 1251#ifdef CONFIG_X86_64
d7ee039e 1252 load_gs_index(host_state->gs_sel);
9581d442 1253#else
d7ee039e 1254 loadsegment(gs, host_state->gs_sel);
33ed6329 1255#endif
33ed6329 1256 }
d7ee039e
SC
1257 if (host_state->fs_sel & 7)
1258 loadsegment(fs, host_state->fs_sel);
b2da15ac 1259#ifdef CONFIG_X86_64
d7ee039e
SC
1260 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1261 loadsegment(ds, host_state->ds_sel);
1262 loadsegment(es, host_state->es_sel);
b2da15ac 1263 }
b2da15ac 1264#endif
b7ffc44d 1265 invalidate_tss_limit();
44ea2b17 1266#ifdef CONFIG_X86_64
c8770e7b 1267 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
44ea2b17 1268#endif
45fc8757 1269 load_fixmap_gdt(raw_smp_processor_id());
33ed6329
AK
1270}
1271
678e315e
SC
1272#ifdef CONFIG_X86_64
1273static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
a9b21b62 1274{
4679b61f
PB
1275 preempt_disable();
1276 if (vmx->loaded_cpu_state)
1277 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1278 preempt_enable();
678e315e 1279 return vmx->msr_guest_kernel_gs_base;
a9b21b62
AK
1280}
1281
678e315e
SC
1282static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1283{
4679b61f
PB
1284 preempt_disable();
1285 if (vmx->loaded_cpu_state)
1286 wrmsrl(MSR_KERNEL_GS_BASE, data);
1287 preempt_enable();
678e315e
SC
1288 vmx->msr_guest_kernel_gs_base = data;
1289}
1290#endif
1291
28b835d6
FW
1292static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
1293{
1294 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1295 struct pi_desc old, new;
1296 unsigned int dest;
1297
31afb2ea
PB
1298 /*
1299 * In case of hot-plug or hot-unplug, we may have to undo
1300 * vmx_vcpu_pi_put even if there is no assigned device. And we
1301 * always keep PI.NDST up to date for simplicity: it makes the
1302 * code easier, and CPU migration is not a fast path.
1303 */
1304 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
28b835d6
FW
1305 return;
1306
31afb2ea
PB
1307 /*
1308 * First handle the simple case where no cmpxchg is necessary; just
1309 * allow posting non-urgent interrupts.
1310 *
1311 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
1312 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
1313 * expects the VCPU to be on the blocked_vcpu_list that matches
1314 * PI.NDST.
1315 */
1316 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
1317 vcpu->cpu == cpu) {
1318 pi_clear_sn(pi_desc);
28b835d6 1319 return;
31afb2ea 1320 }
28b835d6 1321
31afb2ea 1322 /* The full case. */
28b835d6
FW
1323 do {
1324 old.control = new.control = pi_desc->control;
1325
31afb2ea 1326 dest = cpu_physical_id(cpu);
28b835d6 1327
31afb2ea
PB
1328 if (x2apic_enabled())
1329 new.ndst = dest;
1330 else
1331 new.ndst = (dest << 8) & 0xFF00;
28b835d6 1332
28b835d6 1333 new.sn = 0;
c0a1666b
PB
1334 } while (cmpxchg64(&pi_desc->control, old.control,
1335 new.control) != old.control);
28b835d6 1336}
1be0e61c 1337
6aa8b732
AK
1338/*
1339 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1340 * vcpu mutex is already taken.
1341 */
15ad7146 1342static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
6aa8b732 1343{
a2fa3e9f 1344 struct vcpu_vmx *vmx = to_vmx(vcpu);
b80c76ec 1345 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
6aa8b732 1346
b80c76ec 1347 if (!already_loaded) {
fe0e80be 1348 loaded_vmcs_clear(vmx->loaded_vmcs);
92fe13be 1349 local_irq_disable();
8f536b76 1350 crash_disable_local_vmclear(cpu);
5a560f8b
XG
1351
1352 /*
1353 * Read loaded_vmcs->cpu should be before fetching
1354 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1355 * See the comments in __loaded_vmcs_clear().
1356 */
1357 smp_rmb();
1358
d462b819
NHE
1359 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1360 &per_cpu(loaded_vmcss_on_cpu, cpu));
8f536b76 1361 crash_enable_local_vmclear(cpu);
92fe13be 1362 local_irq_enable();
b80c76ec
JM
1363 }
1364
1365 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1366 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1367 vmcs_load(vmx->loaded_vmcs->vmcs);
15d45071 1368 indirect_branch_prediction_barrier();
b80c76ec
JM
1369 }
1370
1371 if (!already_loaded) {
59c58ceb 1372 void *gdt = get_current_gdt_ro();
b80c76ec
JM
1373 unsigned long sysenter_esp;
1374
1375 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
92fe13be 1376
6aa8b732
AK
1377 /*
1378 * Linux uses per-cpu TSS and GDT, so set these when switching
e0c23063 1379 * processors. See 22.2.4.
6aa8b732 1380 */
e0c23063 1381 vmcs_writel(HOST_TR_BASE,
72f5e08d 1382 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
59c58ceb 1383 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
6aa8b732 1384
b7ffc44d
AL
1385 /*
1386 * VM exits change the host TR limit to 0x67 after a VM
1387 * exit. This is okay, since 0x67 covers everything except
1388 * the IO bitmap and have have code to handle the IO bitmap
1389 * being lost after a VM exit.
1390 */
1391 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
1392
6aa8b732
AK
1393 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1394 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
ff2c3a18 1395
d462b819 1396 vmx->loaded_vmcs->cpu = cpu;
6aa8b732 1397 }
28b835d6 1398
2680d6da
OH
1399 /* Setup TSC multiplier */
1400 if (kvm_has_tsc_control &&
c95ba92a
PF
1401 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1402 decache_tsc_multiplier(vmx);
2680d6da 1403
28b835d6 1404 vmx_vcpu_pi_load(vcpu, cpu);
1be0e61c 1405 vmx->host_pkru = read_pkru();
74c55931 1406 vmx->host_debugctlmsr = get_debugctlmsr();
28b835d6
FW
1407}
1408
1409static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
1410{
1411 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1412
1413 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
a0052191
YZ
1414 !irq_remapping_cap(IRQ_POSTING_CAP) ||
1415 !kvm_vcpu_apicv_active(vcpu))
28b835d6
FW
1416 return;
1417
1418 /* Set SN when the vCPU is preempted */
1419 if (vcpu->preempted)
1420 pi_set_sn(pi_desc);
6aa8b732
AK
1421}
1422
1423static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1424{
28b835d6
FW
1425 vmx_vcpu_pi_put(vcpu);
1426
6d6095bd 1427 vmx_prepare_switch_to_host(to_vmx(vcpu));
6aa8b732
AK
1428}
1429
f244deed
WL
1430static bool emulation_required(struct kvm_vcpu *vcpu)
1431{
1432 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
1433}
1434
edcafe3c
AK
1435static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1436
fe3ef05c
NHE
1437/*
1438 * Return the cr0 value that a nested guest would read. This is a combination
1439 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1440 * its hypervisor (cr0_read_shadow).
1441 */
1442static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1443{
1444 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1445 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1446}
1447static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1448{
1449 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1450 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1451}
1452
6aa8b732
AK
1453static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1454{
78ac8b47 1455 unsigned long rflags, save_rflags;
345dcaa8 1456
6de12732
AK
1457 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1458 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1459 rflags = vmcs_readl(GUEST_RFLAGS);
1460 if (to_vmx(vcpu)->rmode.vm86_active) {
1461 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1462 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1463 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1464 }
1465 to_vmx(vcpu)->rflags = rflags;
78ac8b47 1466 }
6de12732 1467 return to_vmx(vcpu)->rflags;
6aa8b732
AK
1468}
1469
1470static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1471{
f244deed
WL
1472 unsigned long old_rflags = vmx_get_rflags(vcpu);
1473
6de12732
AK
1474 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1475 to_vmx(vcpu)->rflags = rflags;
78ac8b47
AK
1476 if (to_vmx(vcpu)->rmode.vm86_active) {
1477 to_vmx(vcpu)->rmode.save_rflags = rflags;
053de044 1478 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
78ac8b47 1479 }
6aa8b732 1480 vmcs_writel(GUEST_RFLAGS, rflags);
f244deed
WL
1481
1482 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
1483 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
6aa8b732
AK
1484}
1485
37ccdcbe 1486static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2809f5d2
GC
1487{
1488 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1489 int ret = 0;
1490
1491 if (interruptibility & GUEST_INTR_STATE_STI)
48005f64 1492 ret |= KVM_X86_SHADOW_INT_STI;
2809f5d2 1493 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
48005f64 1494 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2809f5d2 1495
37ccdcbe 1496 return ret;
2809f5d2
GC
1497}
1498
1499static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1500{
1501 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1502 u32 interruptibility = interruptibility_old;
1503
1504 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1505
48005f64 1506 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2809f5d2 1507 interruptibility |= GUEST_INTR_STATE_MOV_SS;
48005f64 1508 else if (mask & KVM_X86_SHADOW_INT_STI)
2809f5d2
GC
1509 interruptibility |= GUEST_INTR_STATE_STI;
1510
1511 if ((interruptibility != interruptibility_old))
1512 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1513}
1514
6aa8b732
AK
1515static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1516{
1517 unsigned long rip;
6aa8b732 1518
5fdbf976 1519 rip = kvm_rip_read(vcpu);
6aa8b732 1520 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5fdbf976 1521 kvm_rip_write(vcpu, rip);
6aa8b732 1522
2809f5d2
GC
1523 /* skipping an emulated instruction also counts */
1524 vmx_set_interrupt_shadow(vcpu, 0);
6aa8b732
AK
1525}
1526
b96fb439
PB
1527static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
1528 unsigned long exit_qual)
1529{
1530 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1531 unsigned int nr = vcpu->arch.exception.nr;
1532 u32 intr_info = nr | INTR_INFO_VALID_MASK;
1533
1534 if (vcpu->arch.exception.has_error_code) {
1535 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
1536 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1537 }
1538
1539 if (kvm_exception_is_soft(nr))
1540 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1541 else
1542 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1543
1544 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
1545 vmx_get_nmi_mask(vcpu))
1546 intr_info |= INTR_INFO_UNBLOCK_NMI;
1547
1548 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
1549}
1550
0b6ac343
NHE
1551/*
1552 * KVM wants to inject page-faults which it got to the guest. This function
1553 * checks whether in a nested guest, we need to inject them to L1 or L2.
0b6ac343 1554 */
bfcf83b1 1555static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
0b6ac343
NHE
1556{
1557 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
adfe20fb 1558 unsigned int nr = vcpu->arch.exception.nr;
da998b46
JM
1559 bool has_payload = vcpu->arch.exception.has_payload;
1560 unsigned long payload = vcpu->arch.exception.payload;
0b6ac343 1561
b96fb439
PB
1562 if (nr == PF_VECTOR) {
1563 if (vcpu->arch.exception.nested_apf) {
bfcf83b1 1564 *exit_qual = vcpu->arch.apf.nested_apf_token;
b96fb439
PB
1565 return 1;
1566 }
b96fb439
PB
1567 if (nested_vmx_is_page_fault_vmexit(vmcs12,
1568 vcpu->arch.exception.error_code)) {
da998b46 1569 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
b96fb439
PB
1570 return 1;
1571 }
f10c729f
JM
1572 } else if (vmcs12->exception_bitmap & (1u << nr)) {
1573 if (nr == DB_VECTOR) {
1574 if (!has_payload) {
1575 payload = vcpu->arch.dr6;
1576 payload &= ~(DR6_FIXED_1 | DR6_BT);
1577 payload ^= DR6_RTM;
cfb634fe 1578 }
f10c729f
JM
1579 *exit_qual = payload;
1580 } else
1581 *exit_qual = 0;
1582 return 1;
adfe20fb
WL
1583 }
1584
b96fb439 1585 return 0;
0b6ac343
NHE
1586}
1587
caa057a2
WL
1588static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1589{
1590 /*
1591 * Ensure that we clear the HLT state in the VMCS. We don't need to
1592 * explicitly skip the instruction because if the HLT state is set,
1593 * then the instruction is already executing and RIP has already been
1594 * advanced.
1595 */
1596 if (kvm_hlt_in_guest(vcpu->kvm) &&
1597 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1598 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1599}
1600
cfcd20e5 1601static void vmx_queue_exception(struct kvm_vcpu *vcpu)
298101da 1602{
77ab6db0 1603 struct vcpu_vmx *vmx = to_vmx(vcpu);
cfcd20e5
WL
1604 unsigned nr = vcpu->arch.exception.nr;
1605 bool has_error_code = vcpu->arch.exception.has_error_code;
cfcd20e5 1606 u32 error_code = vcpu->arch.exception.error_code;
8ab2d2e2 1607 u32 intr_info = nr | INTR_INFO_VALID_MASK;
77ab6db0 1608
da998b46
JM
1609 kvm_deliver_exception_payload(vcpu);
1610
8ab2d2e2 1611 if (has_error_code) {
77ab6db0 1612 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
8ab2d2e2
JK
1613 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1614 }
77ab6db0 1615
7ffd92c5 1616 if (vmx->rmode.vm86_active) {
71f9833b
SH
1617 int inc_eip = 0;
1618 if (kvm_exception_is_soft(nr))
1619 inc_eip = vcpu->arch.event_exit_inst_len;
1620 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
a92601bb 1621 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
77ab6db0
JK
1622 return;
1623 }
1624
add5ff7a
SC
1625 WARN_ON_ONCE(vmx->emulation_required);
1626
66fd3f7f
GN
1627 if (kvm_exception_is_soft(nr)) {
1628 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1629 vmx->vcpu.arch.event_exit_inst_len);
8ab2d2e2
JK
1630 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1631 } else
1632 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1633
1634 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
caa057a2
WL
1635
1636 vmx_clear_hlt(vcpu);
298101da
AK
1637}
1638
4e47c7a6
SY
1639static bool vmx_rdtscp_supported(void)
1640{
1641 return cpu_has_vmx_rdtscp();
1642}
1643
ad756a16
MJ
1644static bool vmx_invpcid_supported(void)
1645{
eb4b248e 1646 return cpu_has_vmx_invpcid();
ad756a16
MJ
1647}
1648
a75beee6
ED
1649/*
1650 * Swap MSR entry in host/guest MSR entry array.
1651 */
8b9cf98c 1652static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
a75beee6 1653{
26bb0981 1654 struct shared_msr_entry tmp;
a2fa3e9f
GH
1655
1656 tmp = vmx->guest_msrs[to];
1657 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1658 vmx->guest_msrs[from] = tmp;
a75beee6
ED
1659}
1660
e38aea3e
AK
1661/*
1662 * Set up the vmcs to automatically save and restore system
1663 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1664 * mode, as fiddling with msrs is very expensive.
1665 */
8b9cf98c 1666static void setup_msrs(struct vcpu_vmx *vmx)
e38aea3e 1667{
26bb0981 1668 int save_nmsrs, index;
e38aea3e 1669
a75beee6
ED
1670 save_nmsrs = 0;
1671#ifdef CONFIG_X86_64
8b9cf98c 1672 if (is_long_mode(&vmx->vcpu)) {
8b9cf98c 1673 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
a75beee6 1674 if (index >= 0)
8b9cf98c
RR
1675 move_msr_up(vmx, index, save_nmsrs++);
1676 index = __find_msr_index(vmx, MSR_LSTAR);
a75beee6 1677 if (index >= 0)
8b9cf98c
RR
1678 move_msr_up(vmx, index, save_nmsrs++);
1679 index = __find_msr_index(vmx, MSR_CSTAR);
a75beee6 1680 if (index >= 0)
8b9cf98c 1681 move_msr_up(vmx, index, save_nmsrs++);
4e47c7a6 1682 index = __find_msr_index(vmx, MSR_TSC_AUX);
d6321d49 1683 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
4e47c7a6 1684 move_msr_up(vmx, index, save_nmsrs++);
a75beee6 1685 /*
8c06585d 1686 * MSR_STAR is only needed on long mode guests, and only
a75beee6
ED
1687 * if efer.sce is enabled.
1688 */
8c06585d 1689 index = __find_msr_index(vmx, MSR_STAR);
f6801dff 1690 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
8b9cf98c 1691 move_msr_up(vmx, index, save_nmsrs++);
a75beee6
ED
1692 }
1693#endif
92c0d900
AK
1694 index = __find_msr_index(vmx, MSR_EFER);
1695 if (index >= 0 && update_transition_efer(vmx, index))
26bb0981 1696 move_msr_up(vmx, index, save_nmsrs++);
e38aea3e 1697
26bb0981 1698 vmx->save_nmsrs = save_nmsrs;
f48b4711 1699 vmx->guest_msrs_dirty = true;
5897297b 1700
8d14695f 1701 if (cpu_has_vmx_msr_bitmap())
904e14fb 1702 vmx_update_msr_bitmap(&vmx->vcpu);
e38aea3e
AK
1703}
1704
e79f245d 1705static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
6aa8b732 1706{
e79f245d 1707 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6aa8b732 1708
e79f245d
KA
1709 if (is_guest_mode(vcpu) &&
1710 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1711 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
1712
1713 return vcpu->arch.tsc_offset;
6aa8b732
AK
1714}
1715
326e7425 1716static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
6aa8b732 1717{
45c3af97
PB
1718 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1719 u64 g_tsc_offset = 0;
1720
1721 /*
1722 * We're here if L1 chose not to trap WRMSR to TSC. According
1723 * to the spec, this should set L1's TSC; The offset that L1
1724 * set for L2 remains unchanged, and still needs to be added
1725 * to the newly set TSC to get L2's TSC.
1726 */
1727 if (is_guest_mode(vcpu) &&
1728 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
1729 g_tsc_offset = vmcs12->tsc_offset;
326e7425 1730
45c3af97
PB
1731 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1732 vcpu->arch.tsc_offset - g_tsc_offset,
1733 offset);
1734 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1735 return offset + g_tsc_offset;
6aa8b732
AK
1736}
1737
801d3424
NHE
1738/*
1739 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1740 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1741 * all guests if the "nested" module option is off, and can also be disabled
1742 * for a single guest by disabling its VMX cpuid bit.
1743 */
1744static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
1745{
d6321d49 1746 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
801d3424
NHE
1747}
1748
b87a51ae
NHE
1749/*
1750 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
1751 * returned for the various VMX controls MSRs when nested VMX is enabled.
1752 * The same values should also be used to verify that vmcs12 control fields are
1753 * valid during nested entry from L1 to L2.
1754 * Each of these control msrs has a low and high 32-bit half: A low bit is on
1755 * if the corresponding bit in the (32-bit) control field *must* be on, and a
1756 * bit in the high half is on if the corresponding bit in the control field
1757 * may be on. See also vmx_control_verify().
b87a51ae 1758 */
7caaa711
SC
1759static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs,
1760 u32 ept_caps, bool apicv)
b87a51ae 1761{
1389309c
PB
1762 if (!nested) {
1763 memset(msrs, 0, sizeof(*msrs));
1764 return;
1765 }
1766
b87a51ae
NHE
1767 /*
1768 * Note that as a general rule, the high half of the MSRs (bits in
1769 * the control fields which may be 1) should be initialized by the
1770 * intersection of the underlying hardware's MSR (i.e., features which
1771 * can be supported) and the list of features we want to expose -
1772 * because they are known to be properly supported in our code.
1773 * Also, usually, the low half of the MSRs (bits which must be 1) can
1774 * be set to 0, meaning that L1 may turn off any of these bits. The
1775 * reason is that if one of these bits is necessary, it will appear
1776 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
1777 * fields of vmcs01 and vmcs02, will turn these bits off - and
7313c698 1778 * nested_vmx_exit_reflected() will not pass related exits to L1.
b87a51ae
NHE
1779 * These rules have exceptions below.
1780 */
1781
1782 /* pin-based controls */
eabeaacc 1783 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6677f3da
PB
1784 msrs->pinbased_ctls_low,
1785 msrs->pinbased_ctls_high);
1786 msrs->pinbased_ctls_low |=
b9c237bb 1787 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6677f3da 1788 msrs->pinbased_ctls_high &=
b9c237bb
WV
1789 PIN_BASED_EXT_INTR_MASK |
1790 PIN_BASED_NMI_EXITING |
1389309c
PB
1791 PIN_BASED_VIRTUAL_NMIS |
1792 (apicv ? PIN_BASED_POSTED_INTR : 0);
6677f3da 1793 msrs->pinbased_ctls_high |=
b9c237bb 1794 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
0238ea91 1795 PIN_BASED_VMX_PREEMPTION_TIMER;
b87a51ae 1796
3dbcd8da 1797 /* exit controls */
c0dfee58 1798 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6677f3da
PB
1799 msrs->exit_ctls_low,
1800 msrs->exit_ctls_high);
1801 msrs->exit_ctls_low =
b9c237bb 1802 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
e0ba1a6f 1803
6677f3da 1804 msrs->exit_ctls_high &=
b87a51ae 1805#ifdef CONFIG_X86_64
c0dfee58 1806 VM_EXIT_HOST_ADDR_SPACE_SIZE |
b87a51ae 1807#endif
f4124500 1808 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
6677f3da 1809 msrs->exit_ctls_high |=
b9c237bb 1810 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
f4124500 1811 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
e0ba1a6f
BD
1812 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
1813
2996fca0 1814 /* We support free control of debug control saving. */
6677f3da 1815 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2996fca0 1816
b87a51ae
NHE
1817 /* entry controls */
1818 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6677f3da
PB
1819 msrs->entry_ctls_low,
1820 msrs->entry_ctls_high);
1821 msrs->entry_ctls_low =
b9c237bb 1822 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6677f3da 1823 msrs->entry_ctls_high &=
57435349
JK
1824#ifdef CONFIG_X86_64
1825 VM_ENTRY_IA32E_MODE |
1826#endif
1827 VM_ENTRY_LOAD_IA32_PAT;
6677f3da 1828 msrs->entry_ctls_high |=
b9c237bb 1829 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
57435349 1830
2996fca0 1831 /* We support free control of debug control loading. */
6677f3da 1832 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2996fca0 1833
b87a51ae
NHE
1834 /* cpu-based controls */
1835 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6677f3da
PB
1836 msrs->procbased_ctls_low,
1837 msrs->procbased_ctls_high);
1838 msrs->procbased_ctls_low =
b9c237bb 1839 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6677f3da 1840 msrs->procbased_ctls_high &=
a294c9bb
JK
1841 CPU_BASED_VIRTUAL_INTR_PENDING |
1842 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
b87a51ae
NHE
1843 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
1844 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
1845 CPU_BASED_CR3_STORE_EXITING |
1846#ifdef CONFIG_X86_64
1847 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
1848#endif
1849 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
5f3d45e7
MD
1850 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
1851 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
1852 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
1853 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
b87a51ae
NHE
1854 /*
1855 * We can allow some features even when not supported by the
1856 * hardware. For example, L1 can specify an MSR bitmap - and we
1857 * can use it to avoid exits to L1 - even when L0 runs L2
1858 * without MSR bitmaps.
1859 */
6677f3da 1860 msrs->procbased_ctls_high |=
b9c237bb 1861 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
560b7ee1 1862 CPU_BASED_USE_MSR_BITMAPS;
b87a51ae 1863
3dcdf3ec 1864 /* We support free control of CR3 access interception. */
6677f3da 1865 msrs->procbased_ctls_low &=
3dcdf3ec
JK
1866 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
1867
80154d77
PB
1868 /*
1869 * secondary cpu-based controls. Do not include those that
1870 * depend on CPUID bits, they are added later by vmx_cpuid_update.
1871 */
b87a51ae 1872 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6677f3da
PB
1873 msrs->secondary_ctls_low,
1874 msrs->secondary_ctls_high);
1875 msrs->secondary_ctls_low = 0;
1876 msrs->secondary_ctls_high &=
1b07304c 1877 SECONDARY_EXEC_DESC |
f2b93280 1878 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
82f0dd4b 1879 SECONDARY_EXEC_APIC_REGISTER_VIRT |
608406e2 1880 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3db13480 1881 SECONDARY_EXEC_WBINVD_EXITING;
2cf7ea9f 1882
32c7acf0
LA
1883 /*
1884 * We can emulate "VMCS shadowing," even if the hardware
1885 * doesn't support it.
1886 */
1887 msrs->secondary_ctls_high |=
1888 SECONDARY_EXEC_SHADOW_VMCS;
c18911a2 1889
afa61f75
NHE
1890 if (enable_ept) {
1891 /* nested EPT: emulate EPT also to L1 */
6677f3da 1892 msrs->secondary_ctls_high |=
0790ec17 1893 SECONDARY_EXEC_ENABLE_EPT;
6677f3da 1894 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
7db74265 1895 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
02120c45 1896 if (cpu_has_vmx_ept_execute_only())
6677f3da 1897 msrs->ept_caps |=
02120c45 1898 VMX_EPT_EXECUTE_ONLY_BIT;
7caaa711 1899 msrs->ept_caps &= ept_caps;
6677f3da 1900 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
7db74265
PB
1901 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
1902 VMX_EPT_1GB_PAGE_BIT;
03efce6f 1903 if (enable_ept_ad_bits) {
6677f3da 1904 msrs->secondary_ctls_high |=
03efce6f 1905 SECONDARY_EXEC_ENABLE_PML;
6677f3da 1906 msrs->ept_caps |= VMX_EPT_AD_BIT;
03efce6f 1907 }
1c13bffd 1908 }
afa61f75 1909
27c42a1b 1910 if (cpu_has_vmx_vmfunc()) {
6677f3da 1911 msrs->secondary_ctls_high |=
27c42a1b 1912 SECONDARY_EXEC_ENABLE_VMFUNC;
41ab9372
BD
1913 /*
1914 * Advertise EPTP switching unconditionally
1915 * since we emulate it
1916 */
575b3a2c 1917 if (enable_ept)
6677f3da 1918 msrs->vmfunc_controls =
575b3a2c 1919 VMX_VMFUNC_EPTP_SWITCHING;
27c42a1b
BD
1920 }
1921
ef697a71
PB
1922 /*
1923 * Old versions of KVM use the single-context version without
1924 * checking for support, so declare that it is supported even
1925 * though it is treated as global context. The alternative is
1926 * not failing the single-context invvpid, and it is worse.
1927 */
63cb6d5f 1928 if (enable_vpid) {
6677f3da 1929 msrs->secondary_ctls_high |=
63cb6d5f 1930 SECONDARY_EXEC_ENABLE_VPID;
6677f3da 1931 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
bcdde302 1932 VMX_VPID_EXTENT_SUPPORTED_MASK;
1c13bffd 1933 }
99b83ac8 1934
0790ec17 1935 if (enable_unrestricted_guest)
6677f3da 1936 msrs->secondary_ctls_high |=
0790ec17
RK
1937 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1938
2cf7ea9f
PB
1939 if (flexpriority_enabled)
1940 msrs->secondary_ctls_high |=
1941 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1942
c18911a2 1943 /* miscellaneous data */
b9c237bb 1944 rdmsr(MSR_IA32_VMX_MISC,
6677f3da
PB
1945 msrs->misc_low,
1946 msrs->misc_high);
1947 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
1948 msrs->misc_low |=
f4160e45 1949 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
b9c237bb 1950 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
f4124500 1951 VMX_MISC_ACTIVITY_HLT;
6677f3da 1952 msrs->misc_high = 0;
62cc6b9d
DM
1953
1954 /*
1955 * This MSR reports some information about VMX support. We
1956 * should return information about the VMX we emulate for the
1957 * guest, and the VMCS structure we give it - not about the
1958 * VMX support of the underlying hardware.
1959 */
6677f3da 1960 msrs->basic =
62cc6b9d
DM
1961 VMCS12_REVISION |
1962 VMX_BASIC_TRUE_CTLS |
1963 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
1964 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
1965
1966 if (cpu_has_vmx_basic_inout())
6677f3da 1967 msrs->basic |= VMX_BASIC_INOUT;
62cc6b9d
DM
1968
1969 /*
8322ebbb 1970 * These MSRs specify bits which the guest must keep fixed on
62cc6b9d
DM
1971 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
1972 * We picked the standard core2 setting.
1973 */
1974#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
1975#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6677f3da
PB
1976 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
1977 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
8322ebbb
DM
1978
1979 /* These MSRs specify bits which the guest must keep fixed off. */
6677f3da
PB
1980 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
1981 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
62cc6b9d
DM
1982
1983 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
6677f3da 1984 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
b87a51ae
NHE
1985}
1986
3899152c
DM
1987/*
1988 * if fixed0[i] == 1: val[i] must be 1
1989 * if fixed1[i] == 0: val[i] must be 0
1990 */
1991static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
1992{
1993 return ((val & fixed1) | fixed0) == val;
b87a51ae
NHE
1994}
1995
1996static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
1997{
3899152c 1998 return fixed_bits_valid(control, low, high);
b87a51ae
NHE
1999}
2000
2001static inline u64 vmx_control_msr(u32 low, u32 high)
2002{
2003 return low | ((u64)high << 32);
2004}
2005
62cc6b9d
DM
2006static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2007{
2008 superset &= mask;
2009 subset &= mask;
2010
2011 return (superset | subset) == superset;
2012}
2013
2014static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2015{
2016 const u64 feature_and_reserved =
2017 /* feature (except bit 48; see below) */
2018 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2019 /* reserved */
2020 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
6677f3da 2021 u64 vmx_basic = vmx->nested.msrs.basic;
62cc6b9d
DM
2022
2023 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2024 return -EINVAL;
2025
2026 /*
2027 * KVM does not emulate a version of VMX that constrains physical
2028 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2029 */
2030 if (data & BIT_ULL(48))
2031 return -EINVAL;
2032
2033 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2034 vmx_basic_vmcs_revision_id(data))
2035 return -EINVAL;
2036
2037 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2038 return -EINVAL;
2039
6677f3da 2040 vmx->nested.msrs.basic = data;
62cc6b9d
DM
2041 return 0;
2042}
2043
2044static int
2045vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2046{
2047 u64 supported;
2048 u32 *lowp, *highp;
2049
2050 switch (msr_index) {
2051 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
6677f3da
PB
2052 lowp = &vmx->nested.msrs.pinbased_ctls_low;
2053 highp = &vmx->nested.msrs.pinbased_ctls_high;
62cc6b9d
DM
2054 break;
2055 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
6677f3da
PB
2056 lowp = &vmx->nested.msrs.procbased_ctls_low;
2057 highp = &vmx->nested.msrs.procbased_ctls_high;
62cc6b9d
DM
2058 break;
2059 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
6677f3da
PB
2060 lowp = &vmx->nested.msrs.exit_ctls_low;
2061 highp = &vmx->nested.msrs.exit_ctls_high;
62cc6b9d
DM
2062 break;
2063 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
6677f3da
PB
2064 lowp = &vmx->nested.msrs.entry_ctls_low;
2065 highp = &vmx->nested.msrs.entry_ctls_high;
62cc6b9d
DM
2066 break;
2067 case MSR_IA32_VMX_PROCBASED_CTLS2:
6677f3da
PB
2068 lowp = &vmx->nested.msrs.secondary_ctls_low;
2069 highp = &vmx->nested.msrs.secondary_ctls_high;
62cc6b9d
DM
2070 break;
2071 default:
2072 BUG();
2073 }
2074
2075 supported = vmx_control_msr(*lowp, *highp);
2076
2077 /* Check must-be-1 bits are still 1. */
2078 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
2079 return -EINVAL;
2080
2081 /* Check must-be-0 bits are still 0. */
2082 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
2083 return -EINVAL;
2084
2085 *lowp = data;
2086 *highp = data >> 32;
2087 return 0;
2088}
2089
2090static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
2091{
2092 const u64 feature_and_reserved_bits =
2093 /* feature */
2094 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
2095 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
2096 /* reserved */
2097 GENMASK_ULL(13, 9) | BIT_ULL(31);
2098 u64 vmx_misc;
2099
6677f3da
PB
2100 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
2101 vmx->nested.msrs.misc_high);
62cc6b9d
DM
2102
2103 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
2104 return -EINVAL;
2105
6677f3da 2106 if ((vmx->nested.msrs.pinbased_ctls_high &
62cc6b9d
DM
2107 PIN_BASED_VMX_PREEMPTION_TIMER) &&
2108 vmx_misc_preemption_timer_rate(data) !=
2109 vmx_misc_preemption_timer_rate(vmx_misc))
2110 return -EINVAL;
2111
2112 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
2113 return -EINVAL;
2114
2115 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
2116 return -EINVAL;
2117
2118 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
2119 return -EINVAL;
2120
6677f3da
PB
2121 vmx->nested.msrs.misc_low = data;
2122 vmx->nested.msrs.misc_high = data >> 32;
f4160e45
JM
2123
2124 /*
2125 * If L1 has read-only VM-exit information fields, use the
2126 * less permissive vmx_vmwrite_bitmap to specify write
2127 * permissions for the shadow VMCS.
2128 */
2129 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
2130 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
2131
62cc6b9d
DM
2132 return 0;
2133}
2134
2135static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
2136{
2137 u64 vmx_ept_vpid_cap;
2138
6677f3da
PB
2139 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
2140 vmx->nested.msrs.vpid_caps);
62cc6b9d
DM
2141
2142 /* Every bit is either reserved or a feature bit. */
2143 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
2144 return -EINVAL;
2145
6677f3da
PB
2146 vmx->nested.msrs.ept_caps = data;
2147 vmx->nested.msrs.vpid_caps = data >> 32;
62cc6b9d
DM
2148 return 0;
2149}
2150
2151static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2152{
2153 u64 *msr;
2154
2155 switch (msr_index) {
2156 case MSR_IA32_VMX_CR0_FIXED0:
6677f3da 2157 msr = &vmx->nested.msrs.cr0_fixed0;
62cc6b9d
DM
2158 break;
2159 case MSR_IA32_VMX_CR4_FIXED0:
6677f3da 2160 msr = &vmx->nested.msrs.cr4_fixed0;
62cc6b9d
DM
2161 break;
2162 default:
2163 BUG();
2164 }
2165
2166 /*
2167 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
2168 * must be 1 in the restored value.
2169 */
2170 if (!is_bitwise_subset(data, *msr, -1ULL))
2171 return -EINVAL;
2172
2173 *msr = data;
2174 return 0;
2175}
2176
2177/*
2178 * Called when userspace is restoring VMX MSRs.
2179 *
2180 * Returns 0 on success, non-0 otherwise.
2181 */
2182static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
b87a51ae 2183{
b9c237bb
WV
2184 struct vcpu_vmx *vmx = to_vmx(vcpu);
2185
a943ac50
JM
2186 /*
2187 * Don't allow changes to the VMX capability MSRs while the vCPU
2188 * is in VMX operation.
2189 */
2190 if (vmx->nested.vmxon)
2191 return -EBUSY;
2192
b87a51ae 2193 switch (msr_index) {
b87a51ae 2194 case MSR_IA32_VMX_BASIC:
62cc6b9d
DM
2195 return vmx_restore_vmx_basic(vmx, data);
2196 case MSR_IA32_VMX_PINBASED_CTLS:
2197 case MSR_IA32_VMX_PROCBASED_CTLS:
2198 case MSR_IA32_VMX_EXIT_CTLS:
2199 case MSR_IA32_VMX_ENTRY_CTLS:
b87a51ae 2200 /*
62cc6b9d
DM
2201 * The "non-true" VMX capability MSRs are generated from the
2202 * "true" MSRs, so we do not support restoring them directly.
2203 *
2204 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
2205 * should restore the "true" MSRs with the must-be-1 bits
2206 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
2207 * DEFAULT SETTINGS".
b87a51ae 2208 */
62cc6b9d
DM
2209 return -EINVAL;
2210 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2211 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2212 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2213 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2214 case MSR_IA32_VMX_PROCBASED_CTLS2:
2215 return vmx_restore_control_msr(vmx, msr_index, data);
2216 case MSR_IA32_VMX_MISC:
2217 return vmx_restore_vmx_misc(vmx, data);
2218 case MSR_IA32_VMX_CR0_FIXED0:
2219 case MSR_IA32_VMX_CR4_FIXED0:
2220 return vmx_restore_fixed0_msr(vmx, msr_index, data);
2221 case MSR_IA32_VMX_CR0_FIXED1:
2222 case MSR_IA32_VMX_CR4_FIXED1:
2223 /*
2224 * These MSRs are generated based on the vCPU's CPUID, so we
2225 * do not support restoring them directly.
2226 */
2227 return -EINVAL;
2228 case MSR_IA32_VMX_EPT_VPID_CAP:
2229 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
2230 case MSR_IA32_VMX_VMCS_ENUM:
6677f3da 2231 vmx->nested.msrs.vmcs_enum = data;
62cc6b9d
DM
2232 return 0;
2233 default:
b87a51ae 2234 /*
62cc6b9d 2235 * The rest of the VMX capability MSRs do not support restore.
b87a51ae 2236 */
62cc6b9d
DM
2237 return -EINVAL;
2238 }
2239}
2240
2241/* Returns 0 on success, non-0 otherwise. */
6677f3da 2242static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
62cc6b9d 2243{
62cc6b9d
DM
2244 switch (msr_index) {
2245 case MSR_IA32_VMX_BASIC:
6677f3da 2246 *pdata = msrs->basic;
b87a51ae
NHE
2247 break;
2248 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2249 case MSR_IA32_VMX_PINBASED_CTLS:
b9c237bb 2250 *pdata = vmx_control_msr(
6677f3da
PB
2251 msrs->pinbased_ctls_low,
2252 msrs->pinbased_ctls_high);
0115f9cb
DM
2253 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
2254 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
2255 break;
2256 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2257 case MSR_IA32_VMX_PROCBASED_CTLS:
b9c237bb 2258 *pdata = vmx_control_msr(
6677f3da
PB
2259 msrs->procbased_ctls_low,
2260 msrs->procbased_ctls_high);
0115f9cb
DM
2261 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
2262 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
2263 break;
2264 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2265 case MSR_IA32_VMX_EXIT_CTLS:
b9c237bb 2266 *pdata = vmx_control_msr(
6677f3da
PB
2267 msrs->exit_ctls_low,
2268 msrs->exit_ctls_high);
0115f9cb
DM
2269 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
2270 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
2271 break;
2272 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2273 case MSR_IA32_VMX_ENTRY_CTLS:
b9c237bb 2274 *pdata = vmx_control_msr(
6677f3da
PB
2275 msrs->entry_ctls_low,
2276 msrs->entry_ctls_high);
0115f9cb
DM
2277 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
2278 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
2279 break;
2280 case MSR_IA32_VMX_MISC:
b9c237bb 2281 *pdata = vmx_control_msr(
6677f3da
PB
2282 msrs->misc_low,
2283 msrs->misc_high);
b87a51ae 2284 break;
b87a51ae 2285 case MSR_IA32_VMX_CR0_FIXED0:
6677f3da 2286 *pdata = msrs->cr0_fixed0;
b87a51ae
NHE
2287 break;
2288 case MSR_IA32_VMX_CR0_FIXED1:
6677f3da 2289 *pdata = msrs->cr0_fixed1;
b87a51ae
NHE
2290 break;
2291 case MSR_IA32_VMX_CR4_FIXED0:
6677f3da 2292 *pdata = msrs->cr4_fixed0;
b87a51ae
NHE
2293 break;
2294 case MSR_IA32_VMX_CR4_FIXED1:
6677f3da 2295 *pdata = msrs->cr4_fixed1;
b87a51ae
NHE
2296 break;
2297 case MSR_IA32_VMX_VMCS_ENUM:
6677f3da 2298 *pdata = msrs->vmcs_enum;
b87a51ae
NHE
2299 break;
2300 case MSR_IA32_VMX_PROCBASED_CTLS2:
b9c237bb 2301 *pdata = vmx_control_msr(
6677f3da
PB
2302 msrs->secondary_ctls_low,
2303 msrs->secondary_ctls_high);
b87a51ae
NHE
2304 break;
2305 case MSR_IA32_VMX_EPT_VPID_CAP:
6677f3da
PB
2306 *pdata = msrs->ept_caps |
2307 ((u64)msrs->vpid_caps << 32);
b87a51ae 2308 break;
27c42a1b 2309 case MSR_IA32_VMX_VMFUNC:
6677f3da 2310 *pdata = msrs->vmfunc_controls;
27c42a1b 2311 break;
b87a51ae 2312 default:
b87a51ae 2313 return 1;
b3897a49
NHE
2314 }
2315
b87a51ae
NHE
2316 return 0;
2317}
2318
37e4c997
HZ
2319static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2320 uint64_t val)
2321{
2322 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2323
2324 return !(val & ~valid_bits);
2325}
2326
801e459a
TL
2327static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
2328{
1389309c
PB
2329 switch (msr->index) {
2330 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2331 if (!nested)
2332 return 1;
2333 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
2334 default:
2335 return 1;
2336 }
2337
2338 return 0;
801e459a
TL
2339}
2340
6aa8b732
AK
2341/*
2342 * Reads an msr value (of 'msr_index') into 'pdata'.
2343 * Returns 0 on success, non-0 otherwise.
2344 * Assumes vcpu_load() was already called.
2345 */
609e36d3 2346static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
6aa8b732 2347{
a6cb099a 2348 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981 2349 struct shared_msr_entry *msr;
6aa8b732 2350
609e36d3 2351 switch (msr_info->index) {
05b3e0c2 2352#ifdef CONFIG_X86_64
6aa8b732 2353 case MSR_FS_BASE:
609e36d3 2354 msr_info->data = vmcs_readl(GUEST_FS_BASE);
6aa8b732
AK
2355 break;
2356 case MSR_GS_BASE:
609e36d3 2357 msr_info->data = vmcs_readl(GUEST_GS_BASE);
6aa8b732 2358 break;
44ea2b17 2359 case MSR_KERNEL_GS_BASE:
678e315e 2360 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
44ea2b17 2361 break;
26bb0981 2362#endif
6aa8b732 2363 case MSR_EFER:
609e36d3 2364 return kvm_get_msr_common(vcpu, msr_info);
d28b387f
KA
2365 case MSR_IA32_SPEC_CTRL:
2366 if (!msr_info->host_initiated &&
d28b387f
KA
2367 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2368 return 1;
2369
2370 msr_info->data = to_vmx(vcpu)->spec_ctrl;
2371 break;
28c1c9fa
KA
2372 case MSR_IA32_ARCH_CAPABILITIES:
2373 if (!msr_info->host_initiated &&
2374 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
2375 return 1;
2376 msr_info->data = to_vmx(vcpu)->arch_capabilities;
2377 break;
6aa8b732 2378 case MSR_IA32_SYSENTER_CS:
609e36d3 2379 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
6aa8b732
AK
2380 break;
2381 case MSR_IA32_SYSENTER_EIP:
609e36d3 2382 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
6aa8b732
AK
2383 break;
2384 case MSR_IA32_SYSENTER_ESP:
609e36d3 2385 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
6aa8b732 2386 break;
0dd376e7 2387 case MSR_IA32_BNDCFGS:
691bd434 2388 if (!kvm_mpx_supported() ||
d6321d49
RK
2389 (!msr_info->host_initiated &&
2390 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
93c4adc7 2391 return 1;
609e36d3 2392 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
0dd376e7 2393 break;
c45dcc71
AR
2394 case MSR_IA32_MCG_EXT_CTL:
2395 if (!msr_info->host_initiated &&
a6cb099a 2396 !(vmx->msr_ia32_feature_control &
c45dcc71 2397 FEATURE_CONTROL_LMCE))
cae50139 2398 return 1;
c45dcc71
AR
2399 msr_info->data = vcpu->arch.mcg_ext_ctl;
2400 break;
cae50139 2401 case MSR_IA32_FEATURE_CONTROL:
a6cb099a 2402 msr_info->data = vmx->msr_ia32_feature_control;
cae50139
JK
2403 break;
2404 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2405 if (!nested_vmx_allowed(vcpu))
2406 return 1;
6677f3da
PB
2407 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
2408 &msr_info->data);
20300099
WL
2409 case MSR_IA32_XSS:
2410 if (!vmx_xsaves_supported())
2411 return 1;
609e36d3 2412 msr_info->data = vcpu->arch.ia32_xss;
20300099 2413 break;
4e47c7a6 2414 case MSR_TSC_AUX:
d6321d49
RK
2415 if (!msr_info->host_initiated &&
2416 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
4e47c7a6
SY
2417 return 1;
2418 /* Otherwise falls through */
6aa8b732 2419 default:
a6cb099a 2420 msr = find_msr_entry(vmx, msr_info->index);
3bab1f5d 2421 if (msr) {
609e36d3 2422 msr_info->data = msr->data;
3bab1f5d 2423 break;
6aa8b732 2424 }
609e36d3 2425 return kvm_get_msr_common(vcpu, msr_info);
6aa8b732
AK
2426 }
2427
6aa8b732
AK
2428 return 0;
2429}
2430
cae50139
JK
2431static void vmx_leave_nested(struct kvm_vcpu *vcpu);
2432
6aa8b732
AK
2433/*
2434 * Writes msr value into into the appropriate "register".
2435 * Returns 0 on success, non-0 otherwise.
2436 * Assumes vcpu_load() was already called.
2437 */
8fe8ab46 2438static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
6aa8b732 2439{
a2fa3e9f 2440 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981 2441 struct shared_msr_entry *msr;
2cc51560 2442 int ret = 0;
8fe8ab46
WA
2443 u32 msr_index = msr_info->index;
2444 u64 data = msr_info->data;
2cc51560 2445
6aa8b732 2446 switch (msr_index) {
3bab1f5d 2447 case MSR_EFER:
8fe8ab46 2448 ret = kvm_set_msr_common(vcpu, msr_info);
2cc51560 2449 break;
16175a79 2450#ifdef CONFIG_X86_64
6aa8b732 2451 case MSR_FS_BASE:
2fb92db1 2452 vmx_segment_cache_clear(vmx);
6aa8b732
AK
2453 vmcs_writel(GUEST_FS_BASE, data);
2454 break;
2455 case MSR_GS_BASE:
2fb92db1 2456 vmx_segment_cache_clear(vmx);
6aa8b732
AK
2457 vmcs_writel(GUEST_GS_BASE, data);
2458 break;
44ea2b17 2459 case MSR_KERNEL_GS_BASE:
678e315e 2460 vmx_write_guest_kernel_gs_base(vmx, data);
44ea2b17 2461 break;
6aa8b732
AK
2462#endif
2463 case MSR_IA32_SYSENTER_CS:
2464 vmcs_write32(GUEST_SYSENTER_CS, data);
2465 break;
2466 case MSR_IA32_SYSENTER_EIP:
f5b42c33 2467 vmcs_writel(GUEST_SYSENTER_EIP, data);
6aa8b732
AK
2468 break;
2469 case MSR_IA32_SYSENTER_ESP:
f5b42c33 2470 vmcs_writel(GUEST_SYSENTER_ESP, data);
6aa8b732 2471 break;
0dd376e7 2472 case MSR_IA32_BNDCFGS:
691bd434 2473 if (!kvm_mpx_supported() ||
d6321d49
RK
2474 (!msr_info->host_initiated &&
2475 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
93c4adc7 2476 return 1;
fd8cb433 2477 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
4531662d 2478 (data & MSR_IA32_BNDCFGS_RSVD))
93c4adc7 2479 return 1;
0dd376e7
LJ
2480 vmcs_write64(GUEST_BNDCFGS, data);
2481 break;
d28b387f
KA
2482 case MSR_IA32_SPEC_CTRL:
2483 if (!msr_info->host_initiated &&
d28b387f
KA
2484 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2485 return 1;
2486
2487 /* The STIBP bit doesn't fault even if it's not advertised */
9f65fb29 2488 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
d28b387f
KA
2489 return 1;
2490
2491 vmx->spec_ctrl = data;
2492
2493 if (!data)
2494 break;
2495
2496 /*
2497 * For non-nested:
2498 * When it's written (to non-zero) for the first time, pass
2499 * it through.
2500 *
2501 * For nested:
2502 * The handling of the MSR bitmap for L2 guests is done in
2503 * nested_vmx_merge_msr_bitmap. We should not touch the
2504 * vmcs02.msr_bitmap here since it gets completely overwritten
2505 * in the merging. We update the vmcs01 here for L1 as well
2506 * since it will end up touching the MSR anyway now.
2507 */
2508 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
2509 MSR_IA32_SPEC_CTRL,
2510 MSR_TYPE_RW);
2511 break;
15d45071
AR
2512 case MSR_IA32_PRED_CMD:
2513 if (!msr_info->host_initiated &&
15d45071
AR
2514 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2515 return 1;
2516
2517 if (data & ~PRED_CMD_IBPB)
2518 return 1;
2519
2520 if (!data)
2521 break;
2522
2523 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2524
2525 /*
2526 * For non-nested:
2527 * When it's written (to non-zero) for the first time, pass
2528 * it through.
2529 *
2530 * For nested:
2531 * The handling of the MSR bitmap for L2 guests is done in
2532 * nested_vmx_merge_msr_bitmap. We should not touch the
2533 * vmcs02.msr_bitmap here since it gets completely overwritten
2534 * in the merging.
2535 */
2536 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
2537 MSR_TYPE_W);
2538 break;
28c1c9fa
KA
2539 case MSR_IA32_ARCH_CAPABILITIES:
2540 if (!msr_info->host_initiated)
2541 return 1;
2542 vmx->arch_capabilities = data;
2543 break;
468d472f
SY
2544 case MSR_IA32_CR_PAT:
2545 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
4566654b
NA
2546 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2547 return 1;
468d472f
SY
2548 vmcs_write64(GUEST_IA32_PAT, data);
2549 vcpu->arch.pat = data;
2550 break;
2551 }
8fe8ab46 2552 ret = kvm_set_msr_common(vcpu, msr_info);
4e47c7a6 2553 break;
ba904635
WA
2554 case MSR_IA32_TSC_ADJUST:
2555 ret = kvm_set_msr_common(vcpu, msr_info);
4e47c7a6 2556 break;
c45dcc71
AR
2557 case MSR_IA32_MCG_EXT_CTL:
2558 if ((!msr_info->host_initiated &&
2559 !(to_vmx(vcpu)->msr_ia32_feature_control &
2560 FEATURE_CONTROL_LMCE)) ||
2561 (data & ~MCG_EXT_CTL_LMCE_EN))
2562 return 1;
2563 vcpu->arch.mcg_ext_ctl = data;
2564 break;
cae50139 2565 case MSR_IA32_FEATURE_CONTROL:
37e4c997 2566 if (!vmx_feature_control_msr_valid(vcpu, data) ||
3b84080b 2567 (to_vmx(vcpu)->msr_ia32_feature_control &
cae50139
JK
2568 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2569 return 1;
3b84080b 2570 vmx->msr_ia32_feature_control = data;
cae50139
JK
2571 if (msr_info->host_initiated && data == 0)
2572 vmx_leave_nested(vcpu);
2573 break;
2574 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
62cc6b9d
DM
2575 if (!msr_info->host_initiated)
2576 return 1; /* they are read-only */
2577 if (!nested_vmx_allowed(vcpu))
2578 return 1;
2579 return vmx_set_vmx_msr(vcpu, msr_index, data);
20300099
WL
2580 case MSR_IA32_XSS:
2581 if (!vmx_xsaves_supported())
2582 return 1;
2583 /*
2584 * The only supported bit as of Skylake is bit 8, but
2585 * it is not supported on KVM.
2586 */
2587 if (data != 0)
2588 return 1;
2589 vcpu->arch.ia32_xss = data;
2590 if (vcpu->arch.ia32_xss != host_xss)
2591 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
989e3992 2592 vcpu->arch.ia32_xss, host_xss, false);
20300099
WL
2593 else
2594 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
2595 break;
4e47c7a6 2596 case MSR_TSC_AUX:
d6321d49
RK
2597 if (!msr_info->host_initiated &&
2598 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
4e47c7a6
SY
2599 return 1;
2600 /* Check reserved bit, higher 32 bits should be zero */
2601 if ((data >> 32) != 0)
2602 return 1;
2603 /* Otherwise falls through */
6aa8b732 2604 default:
8b9cf98c 2605 msr = find_msr_entry(vmx, msr_index);
3bab1f5d 2606 if (msr) {
8b3c3104 2607 u64 old_msr_data = msr->data;
3bab1f5d 2608 msr->data = data;
2225fd56
AK
2609 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2610 preempt_disable();
8b3c3104
AH
2611 ret = kvm_set_shared_msr(msr->index, msr->data,
2612 msr->mask);
2225fd56 2613 preempt_enable();
8b3c3104
AH
2614 if (ret)
2615 msr->data = old_msr_data;
2225fd56 2616 }
3bab1f5d 2617 break;
6aa8b732 2618 }
8fe8ab46 2619 ret = kvm_set_msr_common(vcpu, msr_info);
6aa8b732
AK
2620 }
2621
2cc51560 2622 return ret;
6aa8b732
AK
2623}
2624
5fdbf976 2625static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
6aa8b732 2626{
5fdbf976
MT
2627 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2628 switch (reg) {
2629 case VCPU_REGS_RSP:
2630 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2631 break;
2632 case VCPU_REGS_RIP:
2633 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2634 break;
6de4f3ad
AK
2635 case VCPU_EXREG_PDPTR:
2636 if (enable_ept)
2637 ept_save_pdptrs(vcpu);
2638 break;
5fdbf976
MT
2639 default:
2640 break;
2641 }
6aa8b732
AK
2642}
2643
6aa8b732
AK
2644static __init int cpu_has_kvm_support(void)
2645{
6210e37b 2646 return cpu_has_vmx();
6aa8b732
AK
2647}
2648
2649static __init int vmx_disabled_by_bios(void)
2650{
2651 u64 msr;
2652
2653 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
cafd6659 2654 if (msr & FEATURE_CONTROL_LOCKED) {
23f3e991 2655 /* launched w/ TXT and VMX disabled */
cafd6659
SW
2656 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2657 && tboot_enabled())
2658 return 1;
23f3e991 2659 /* launched w/o TXT and VMX only enabled w/ TXT */
cafd6659 2660 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
23f3e991 2661 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
f9335afe
SW
2662 && !tboot_enabled()) {
2663 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
23f3e991 2664 "activate TXT before enabling KVM\n");
cafd6659 2665 return 1;
f9335afe 2666 }
23f3e991
JC
2667 /* launched w/o TXT and VMX disabled */
2668 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2669 && !tboot_enabled())
2670 return 1;
cafd6659
SW
2671 }
2672
2673 return 0;
6aa8b732
AK
2674}
2675
7725b894
DX
2676static void kvm_cpu_vmxon(u64 addr)
2677{
fe0e80be 2678 cr4_set_bits(X86_CR4_VMXE);
1c5ac21a
AS
2679 intel_pt_handle_vmx(1);
2680
4b1e5478 2681 asm volatile ("vmxon %0" : : "m"(addr));
7725b894
DX
2682}
2683
13a34e06 2684static int hardware_enable(void)
6aa8b732
AK
2685{
2686 int cpu = raw_smp_processor_id();
2687 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
cafd6659 2688 u64 old, test_bits;
6aa8b732 2689
1e02ce4c 2690 if (cr4_read_shadow() & X86_CR4_VMXE)
10474ae8
AG
2691 return -EBUSY;
2692
773e8a04
VK
2693 /*
2694 * This can happen if we hot-added a CPU but failed to allocate
2695 * VP assist page for it.
2696 */
2697 if (static_branch_unlikely(&enable_evmcs) &&
2698 !hv_get_vp_assist_page(cpu))
2699 return -EFAULT;
2700
d462b819 2701 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
bf9f6ac8
FW
2702 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
2703 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
8f536b76
ZY
2704
2705 /*
2706 * Now we can enable the vmclear operation in kdump
2707 * since the loaded_vmcss_on_cpu list on this cpu
2708 * has been initialized.
2709 *
2710 * Though the cpu is not in VMX operation now, there
2711 * is no problem to enable the vmclear operation
2712 * for the loaded_vmcss_on_cpu list is empty!
2713 */
2714 crash_enable_local_vmclear(cpu);
2715
6aa8b732 2716 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
cafd6659
SW
2717
2718 test_bits = FEATURE_CONTROL_LOCKED;
2719 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2720 if (tboot_enabled())
2721 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2722
2723 if ((old & test_bits) != test_bits) {
6aa8b732 2724 /* enable and lock */
cafd6659
SW
2725 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2726 }
fe0e80be 2727 kvm_cpu_vmxon(phys_addr);
fdf288bf
DH
2728 if (enable_ept)
2729 ept_sync_global();
10474ae8
AG
2730
2731 return 0;
6aa8b732
AK
2732}
2733
d462b819 2734static void vmclear_local_loaded_vmcss(void)
543e4243
AK
2735{
2736 int cpu = raw_smp_processor_id();
d462b819 2737 struct loaded_vmcs *v, *n;
543e4243 2738
d462b819
NHE
2739 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2740 loaded_vmcss_on_cpu_link)
2741 __loaded_vmcs_clear(v);
543e4243
AK
2742}
2743
710ff4a8
EH
2744
2745/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2746 * tricks.
2747 */
2748static void kvm_cpu_vmxoff(void)
6aa8b732 2749{
4b1e5478 2750 asm volatile (__ex("vmxoff"));
1c5ac21a
AS
2751
2752 intel_pt_handle_vmx(0);
fe0e80be 2753 cr4_clear_bits(X86_CR4_VMXE);
6aa8b732
AK
2754}
2755
13a34e06 2756static void hardware_disable(void)
710ff4a8 2757{
fe0e80be
DH
2758 vmclear_local_loaded_vmcss();
2759 kvm_cpu_vmxoff();
710ff4a8
EH
2760}
2761
1c3d14fe 2762static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
d77c26fc 2763 u32 msr, u32 *result)
1c3d14fe
YS
2764{
2765 u32 vmx_msr_low, vmx_msr_high;
2766 u32 ctl = ctl_min | ctl_opt;
2767
2768 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2769
2770 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2771 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2772
2773 /* Ensure minimum (required) set of control bits are supported. */
2774 if (ctl_min & ~ctl)
002c7f7c 2775 return -EIO;
1c3d14fe
YS
2776
2777 *result = ctl;
2778 return 0;
2779}
2780
7caaa711
SC
2781static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
2782 struct vmx_capability *vmx_cap)
6aa8b732
AK
2783{
2784 u32 vmx_msr_low, vmx_msr_high;
d56f546d 2785 u32 min, opt, min2, opt2;
1c3d14fe
YS
2786 u32 _pin_based_exec_control = 0;
2787 u32 _cpu_based_exec_control = 0;
f78e0e2e 2788 u32 _cpu_based_2nd_exec_control = 0;
1c3d14fe
YS
2789 u32 _vmexit_control = 0;
2790 u32 _vmentry_control = 0;
2791
1389309c 2792 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
10166744 2793 min = CPU_BASED_HLT_EXITING |
1c3d14fe
YS
2794#ifdef CONFIG_X86_64
2795 CPU_BASED_CR8_LOAD_EXITING |
2796 CPU_BASED_CR8_STORE_EXITING |
2797#endif
d56f546d
SY
2798 CPU_BASED_CR3_LOAD_EXITING |
2799 CPU_BASED_CR3_STORE_EXITING |
8eb73e2d 2800 CPU_BASED_UNCOND_IO_EXITING |
1c3d14fe 2801 CPU_BASED_MOV_DR_EXITING |
a7052897 2802 CPU_BASED_USE_TSC_OFFSETING |
4d5422ce
WL
2803 CPU_BASED_MWAIT_EXITING |
2804 CPU_BASED_MONITOR_EXITING |
fee84b07
AK
2805 CPU_BASED_INVLPG_EXITING |
2806 CPU_BASED_RDPMC_EXITING;
443381a8 2807
f78e0e2e 2808 opt = CPU_BASED_TPR_SHADOW |
25c5f225 2809 CPU_BASED_USE_MSR_BITMAPS |
f78e0e2e 2810 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1c3d14fe
YS
2811 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2812 &_cpu_based_exec_control) < 0)
002c7f7c 2813 return -EIO;
6e5d865c
YS
2814#ifdef CONFIG_X86_64
2815 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2816 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2817 ~CPU_BASED_CR8_STORE_EXITING;
2818#endif
f78e0e2e 2819 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
d56f546d
SY
2820 min2 = 0;
2821 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
8d14695f 2822 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2384d2b3 2823 SECONDARY_EXEC_WBINVD_EXITING |
d56f546d 2824 SECONDARY_EXEC_ENABLE_VPID |
3a624e29 2825 SECONDARY_EXEC_ENABLE_EPT |
4b8d54f9 2826 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4e47c7a6 2827 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
0367f205 2828 SECONDARY_EXEC_DESC |
ad756a16 2829 SECONDARY_EXEC_RDTSCP |
83d4c286 2830 SECONDARY_EXEC_ENABLE_INVPCID |
c7c9c56c 2831 SECONDARY_EXEC_APIC_REGISTER_VIRT |
abc4fc58 2832 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
20300099 2833 SECONDARY_EXEC_SHADOW_VMCS |
843e4330 2834 SECONDARY_EXEC_XSAVES |
736fdf72
DH
2835 SECONDARY_EXEC_RDSEED_EXITING |
2836 SECONDARY_EXEC_RDRAND_EXITING |
8b3e34e4 2837 SECONDARY_EXEC_ENABLE_PML |
2a499e49 2838 SECONDARY_EXEC_TSC_SCALING |
0b665d30
SC
2839 SECONDARY_EXEC_ENABLE_VMFUNC |
2840 SECONDARY_EXEC_ENCLS_EXITING;
d56f546d
SY
2841 if (adjust_vmx_controls(min2, opt2,
2842 MSR_IA32_VMX_PROCBASED_CTLS2,
f78e0e2e
SY
2843 &_cpu_based_2nd_exec_control) < 0)
2844 return -EIO;
2845 }
2846#ifndef CONFIG_X86_64
2847 if (!(_cpu_based_2nd_exec_control &
2848 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2849 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2850#endif
83d4c286
YZ
2851
2852 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2853 _cpu_based_2nd_exec_control &= ~(
8d14695f 2854 SECONDARY_EXEC_APIC_REGISTER_VIRT |
c7c9c56c
YZ
2855 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2856 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
83d4c286 2857
61f1dd90 2858 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
7caaa711 2859 &vmx_cap->ept, &vmx_cap->vpid);
61f1dd90 2860
d56f546d 2861 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
a7052897
MT
2862 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2863 enabled */
5fff7d27
GN
2864 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2865 CPU_BASED_CR3_STORE_EXITING |
2866 CPU_BASED_INVLPG_EXITING);
7caaa711
SC
2867 } else if (vmx_cap->ept) {
2868 vmx_cap->ept = 0;
61f1dd90
WL
2869 pr_warn_once("EPT CAP should not exist if not support "
2870 "1-setting enable EPT VM-execution control\n");
2871 }
2872 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
7caaa711
SC
2873 vmx_cap->vpid) {
2874 vmx_cap->vpid = 0;
61f1dd90
WL
2875 pr_warn_once("VPID CAP should not exist if not support "
2876 "1-setting enable VPID VM-execution control\n");
d56f546d 2877 }
1c3d14fe 2878
91fa0f8e 2879 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
1c3d14fe
YS
2880#ifdef CONFIG_X86_64
2881 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2882#endif
c73da3fc
SC
2883 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
2884 VM_EXIT_SAVE_IA32_PAT |
2885 VM_EXIT_LOAD_IA32_PAT |
2886 VM_EXIT_LOAD_IA32_EFER |
2887 VM_EXIT_CLEAR_BNDCFGS;
1c3d14fe
YS
2888 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2889 &_vmexit_control) < 0)
002c7f7c 2890 return -EIO;
1c3d14fe 2891
8a1b4392
PB
2892 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2893 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2894 PIN_BASED_VMX_PREEMPTION_TIMER;
01e439be
YZ
2895 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2896 &_pin_based_exec_control) < 0)
2897 return -EIO;
2898
1c17c3e6
PB
2899 if (cpu_has_broken_vmx_preemption_timer())
2900 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
01e439be 2901 if (!(_cpu_based_2nd_exec_control &
91fa0f8e 2902 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
01e439be
YZ
2903 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2904
c845f9c6 2905 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
c73da3fc
SC
2906 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2907 VM_ENTRY_LOAD_IA32_PAT |
2908 VM_ENTRY_LOAD_IA32_EFER |
2909 VM_ENTRY_LOAD_BNDCFGS;
1c3d14fe
YS
2910 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2911 &_vmentry_control) < 0)
002c7f7c 2912 return -EIO;
6aa8b732 2913
c73da3fc
SC
2914 /*
2915 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2916 * can't be used due to an errata where VM Exit may incorrectly clear
2917 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2918 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2919 */
2920 if (boot_cpu_data.x86 == 0x6) {
2921 switch (boot_cpu_data.x86_model) {
2922 case 26: /* AAK155 */
2923 case 30: /* AAP115 */
2924 case 37: /* AAT100 */
2925 case 44: /* BC86,AAY89,BD102 */
2926 case 46: /* BA97 */
2927 _vmexit_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
2928 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2929 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2930 "does not work properly. Using workaround\n");
2931 break;
2932 default:
2933 break;
2934 }
2935 }
2936
2937
c68876fd 2938 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
1c3d14fe
YS
2939
2940 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2941 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
002c7f7c 2942 return -EIO;
1c3d14fe
YS
2943
2944#ifdef CONFIG_X86_64
2945 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2946 if (vmx_msr_high & (1u<<16))
002c7f7c 2947 return -EIO;
1c3d14fe
YS
2948#endif
2949
2950 /* Require Write-Back (WB) memory type for VMCS accesses. */
2951 if (((vmx_msr_high >> 18) & 15) != 6)
002c7f7c 2952 return -EIO;
1c3d14fe 2953
002c7f7c 2954 vmcs_conf->size = vmx_msr_high & 0x1fff;
16cb0255 2955 vmcs_conf->order = get_order(vmcs_conf->size);
9ac7e3e8 2956 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
773e8a04 2957
2307af1c 2958 vmcs_conf->revision_id = vmx_msr_low;
1c3d14fe 2959
002c7f7c
YS
2960 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2961 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
f78e0e2e 2962 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
002c7f7c
YS
2963 vmcs_conf->vmexit_ctrl = _vmexit_control;
2964 vmcs_conf->vmentry_ctrl = _vmentry_control;
1c3d14fe 2965
773e8a04
VK
2966 if (static_branch_unlikely(&enable_evmcs))
2967 evmcs_sanitize_exec_ctrls(vmcs_conf);
2968
1c3d14fe 2969 return 0;
c68876fd 2970}
6aa8b732 2971
89b0c9f5 2972struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
6aa8b732
AK
2973{
2974 int node = cpu_to_node(cpu);
2975 struct page *pages;
2976 struct vmcs *vmcs;
2977
96db800f 2978 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
6aa8b732
AK
2979 if (!pages)
2980 return NULL;
2981 vmcs = page_address(pages);
1c3d14fe 2982 memset(vmcs, 0, vmcs_config.size);
2307af1c
LA
2983
2984 /* KVM supports Enlightened VMCS v1 only */
2985 if (static_branch_unlikely(&enable_evmcs))
392b2f25 2986 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
2307af1c 2987 else
392b2f25 2988 vmcs->hdr.revision_id = vmcs_config.revision_id;
2307af1c 2989
491a6038
LA
2990 if (shadow)
2991 vmcs->hdr.shadow_vmcs = 1;
6aa8b732
AK
2992 return vmcs;
2993}
2994
89b0c9f5 2995void free_vmcs(struct vmcs *vmcs)
6aa8b732 2996{
1c3d14fe 2997 free_pages((unsigned long)vmcs, vmcs_config.order);
6aa8b732
AK
2998}
2999
d462b819
NHE
3000/*
3001 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3002 */
89b0c9f5 3003void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
d462b819
NHE
3004{
3005 if (!loaded_vmcs->vmcs)
3006 return;
3007 loaded_vmcs_clear(loaded_vmcs);
3008 free_vmcs(loaded_vmcs->vmcs);
3009 loaded_vmcs->vmcs = NULL;
904e14fb
PB
3010 if (loaded_vmcs->msr_bitmap)
3011 free_page((unsigned long)loaded_vmcs->msr_bitmap);
355f4fb1 3012 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
d462b819
NHE
3013}
3014
89b0c9f5 3015int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
f21f165e 3016{
491a6038 3017 loaded_vmcs->vmcs = alloc_vmcs(false);
f21f165e
PB
3018 if (!loaded_vmcs->vmcs)
3019 return -ENOMEM;
3020
3021 loaded_vmcs->shadow_vmcs = NULL;
3022 loaded_vmcs_init(loaded_vmcs);
904e14fb
PB
3023
3024 if (cpu_has_vmx_msr_bitmap()) {
3025 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3026 if (!loaded_vmcs->msr_bitmap)
3027 goto out_vmcs;
3028 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
ceef7d10 3029
1f008e11
AB
3030 if (IS_ENABLED(CONFIG_HYPERV) &&
3031 static_branch_unlikely(&enable_evmcs) &&
ceef7d10
VK
3032 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
3033 struct hv_enlightened_vmcs *evmcs =
3034 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
3035
3036 evmcs->hv_enlightenments_control.msr_bitmap = 1;
3037 }
904e14fb 3038 }
d7ee039e
SC
3039
3040 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
3041
f21f165e 3042 return 0;
904e14fb
PB
3043
3044out_vmcs:
3045 free_loaded_vmcs(loaded_vmcs);
3046 return -ENOMEM;
f21f165e
PB
3047}
3048
39959588 3049static void free_kvm_area(void)
6aa8b732
AK
3050{
3051 int cpu;
3052
3230bb47 3053 for_each_possible_cpu(cpu) {
6aa8b732 3054 free_vmcs(per_cpu(vmxarea, cpu));
3230bb47
ZA
3055 per_cpu(vmxarea, cpu) = NULL;
3056 }
6aa8b732
AK
3057}
3058
fe2b201b
BD
3059static void init_vmcs_shadow_fields(void)
3060{
3061 int i, j;
3062
dfae3c03
SC
3063 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
3064 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
3065
44900ba6
PB
3066 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
3067 u16 field = shadow_read_only_fields[i];
d37f4267 3068 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
44900ba6
PB
3069 (i + 1 == max_shadow_read_only_fields ||
3070 shadow_read_only_fields[i + 1] != field + 1))
3071 pr_err("Missing field from shadow_read_only_field %x\n",
3072 field + 1);
3073
3074 clear_bit(field, vmx_vmread_bitmap);
3075#ifdef CONFIG_X86_64
3076 if (field & 1)
3077 continue;
3078#endif
3079 if (j < i)
3080 shadow_read_only_fields[j] = field;
3081 j++;
3082 }
3083 max_shadow_read_only_fields = j;
fe2b201b
BD
3084
3085 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
44900ba6 3086 u16 field = shadow_read_write_fields[i];
d37f4267 3087 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
44900ba6
PB
3088 (i + 1 == max_shadow_read_write_fields ||
3089 shadow_read_write_fields[i + 1] != field + 1))
3090 pr_err("Missing field from shadow_read_write_field %x\n",
3091 field + 1);
3092
c5d167b2
PB
3093 /*
3094 * PML and the preemption timer can be emulated, but the
3095 * processor cannot vmwrite to fields that don't exist
3096 * on bare metal.
3097 */
44900ba6 3098 switch (field) {
c5d167b2
PB
3099 case GUEST_PML_INDEX:
3100 if (!cpu_has_vmx_pml())
3101 continue;
3102 break;
3103 case VMX_PREEMPTION_TIMER_VALUE:
3104 if (!cpu_has_vmx_preemption_timer())
3105 continue;
3106 break;
3107 case GUEST_INTR_STATUS:
3108 if (!cpu_has_vmx_apicv())
fe2b201b
BD
3109 continue;
3110 break;
3111 default:
3112 break;
3113 }
3114
44900ba6
PB
3115 clear_bit(field, vmx_vmwrite_bitmap);
3116 clear_bit(field, vmx_vmread_bitmap);
3117#ifdef CONFIG_X86_64
3118 if (field & 1)
3119 continue;
3120#endif
fe2b201b 3121 if (j < i)
44900ba6 3122 shadow_read_write_fields[j] = field;
fe2b201b
BD
3123 j++;
3124 }
3125 max_shadow_read_write_fields = j;
fe2b201b
BD
3126}
3127
6aa8b732
AK
3128static __init int alloc_kvm_area(void)
3129{
3130 int cpu;
3131
3230bb47 3132 for_each_possible_cpu(cpu) {
6aa8b732
AK
3133 struct vmcs *vmcs;
3134
491a6038 3135 vmcs = alloc_vmcs_cpu(false, cpu);
6aa8b732
AK
3136 if (!vmcs) {
3137 free_kvm_area();
3138 return -ENOMEM;
3139 }
3140
2307af1c
LA
3141 /*
3142 * When eVMCS is enabled, alloc_vmcs_cpu() sets
3143 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
3144 * revision_id reported by MSR_IA32_VMX_BASIC.
3145 *
3146 * However, even though not explictly documented by
3147 * TLFS, VMXArea passed as VMXON argument should
3148 * still be marked with revision_id reported by
3149 * physical CPU.
3150 */
3151 if (static_branch_unlikely(&enable_evmcs))
392b2f25 3152 vmcs->hdr.revision_id = vmcs_config.revision_id;
2307af1c 3153
6aa8b732
AK
3154 per_cpu(vmxarea, cpu) = vmcs;
3155 }
3156 return 0;
3157}
3158
91b0aa2c 3159static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
d99e4152 3160 struct kvm_segment *save)
6aa8b732 3161{
d99e4152
GN
3162 if (!emulate_invalid_guest_state) {
3163 /*
3164 * CS and SS RPL should be equal during guest entry according
3165 * to VMX spec, but in reality it is not always so. Since vcpu
3166 * is in the middle of the transition from real mode to
3167 * protected mode it is safe to assume that RPL 0 is a good
3168 * default value.
3169 */
3170 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
b32a9918
NA
3171 save->selector &= ~SEGMENT_RPL_MASK;
3172 save->dpl = save->selector & SEGMENT_RPL_MASK;
d99e4152 3173 save->s = 1;
6aa8b732 3174 }
d99e4152 3175 vmx_set_segment(vcpu, save, seg);
6aa8b732
AK
3176}
3177
3178static void enter_pmode(struct kvm_vcpu *vcpu)
3179{
3180 unsigned long flags;
a89a8fb9 3181 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732 3182
d99e4152
GN
3183 /*
3184 * Update real mode segment cache. It may be not up-to-date if sement
3185 * register was written while vcpu was in a guest mode.
3186 */
3187 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3188 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3189 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3190 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3191 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3192 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3193
7ffd92c5 3194 vmx->rmode.vm86_active = 0;
6aa8b732 3195
2fb92db1
AK
3196 vmx_segment_cache_clear(vmx);
3197
f5f7b2fe 3198 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
6aa8b732
AK
3199
3200 flags = vmcs_readl(GUEST_RFLAGS);
78ac8b47
AK
3201 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3202 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
6aa8b732
AK
3203 vmcs_writel(GUEST_RFLAGS, flags);
3204
66aee91a
RR
3205 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3206 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
6aa8b732
AK
3207
3208 update_exception_bitmap(vcpu);
3209
91b0aa2c
GN
3210 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3211 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3212 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3213 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3214 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3215 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
6aa8b732
AK
3216}
3217
f5f7b2fe 3218static void fix_rmode_seg(int seg, struct kvm_segment *save)
6aa8b732 3219{
772e0318 3220 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
d99e4152
GN
3221 struct kvm_segment var = *save;
3222
3223 var.dpl = 0x3;
3224 if (seg == VCPU_SREG_CS)
3225 var.type = 0x3;
3226
3227 if (!emulate_invalid_guest_state) {
3228 var.selector = var.base >> 4;
3229 var.base = var.base & 0xffff0;
3230 var.limit = 0xffff;
3231 var.g = 0;
3232 var.db = 0;
3233 var.present = 1;
3234 var.s = 1;
3235 var.l = 0;
3236 var.unusable = 0;
3237 var.type = 0x3;
3238 var.avl = 0;
3239 if (save->base & 0xf)
3240 printk_once(KERN_WARNING "kvm: segment base is not "
3241 "paragraph aligned when entering "
3242 "protected mode (seg=%d)", seg);
3243 }
6aa8b732 3244
d99e4152 3245 vmcs_write16(sf->selector, var.selector);
96794e4e 3246 vmcs_writel(sf->base, var.base);
d99e4152
GN
3247 vmcs_write32(sf->limit, var.limit);
3248 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
6aa8b732
AK
3249}
3250
3251static void enter_rmode(struct kvm_vcpu *vcpu)
3252{
3253 unsigned long flags;
a89a8fb9 3254 struct vcpu_vmx *vmx = to_vmx(vcpu);
40bbb9d0 3255 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
6aa8b732 3256
f5f7b2fe
AK
3257 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3258 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3259 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3260 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3261 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
c6ad1153
GN
3262 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3263 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
f5f7b2fe 3264
7ffd92c5 3265 vmx->rmode.vm86_active = 1;
6aa8b732 3266
776e58ea
GN
3267 /*
3268 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4918c6ca 3269 * vcpu. Warn the user that an update is overdue.
776e58ea 3270 */
40bbb9d0 3271 if (!kvm_vmx->tss_addr)
776e58ea
GN
3272 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3273 "called before entering vcpu\n");
776e58ea 3274
2fb92db1
AK
3275 vmx_segment_cache_clear(vmx);
3276
40bbb9d0 3277 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
6aa8b732 3278 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
6aa8b732
AK
3279 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3280
3281 flags = vmcs_readl(GUEST_RFLAGS);
78ac8b47 3282 vmx->rmode.save_rflags = flags;
6aa8b732 3283
053de044 3284 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
6aa8b732
AK
3285
3286 vmcs_writel(GUEST_RFLAGS, flags);
66aee91a 3287 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
6aa8b732
AK
3288 update_exception_bitmap(vcpu);
3289
d99e4152
GN
3290 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3291 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3292 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3293 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3294 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3295 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
b246dd5d 3296
8668a3c4 3297 kvm_mmu_reset_context(vcpu);
6aa8b732
AK
3298}
3299
401d10de
AS
3300static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3301{
3302 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981
AK
3303 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3304
3305 if (!msr)
3306 return;
401d10de 3307
f6801dff 3308 vcpu->arch.efer = efer;
401d10de 3309 if (efer & EFER_LMA) {
2961e876 3310 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
401d10de
AS
3311 msr->data = efer;
3312 } else {
2961e876 3313 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
401d10de
AS
3314
3315 msr->data = efer & ~EFER_LME;
3316 }
3317 setup_msrs(vmx);
3318}
3319
05b3e0c2 3320#ifdef CONFIG_X86_64
6aa8b732
AK
3321
3322static void enter_lmode(struct kvm_vcpu *vcpu)
3323{
3324 u32 guest_tr_ar;
3325
2fb92db1
AK
3326 vmx_segment_cache_clear(to_vmx(vcpu));
3327
6aa8b732 3328 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
4d283ec9 3329 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
bd80158a
JK
3330 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3331 __func__);
6aa8b732 3332 vmcs_write32(GUEST_TR_AR_BYTES,
4d283ec9
AL
3333 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3334 | VMX_AR_TYPE_BUSY_64_TSS);
6aa8b732 3335 }
da38f438 3336 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
6aa8b732
AK
3337}
3338
3339static void exit_lmode(struct kvm_vcpu *vcpu)
3340{
2961e876 3341 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
da38f438 3342 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
6aa8b732
AK
3343}
3344
3345#endif
3346
faff8758
JS
3347static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
3348{
3349 int vpid = to_vmx(vcpu)->vpid;
3350
3351 if (!vpid_sync_vcpu_addr(vpid, addr))
3352 vpid_sync_context(vpid);
3353
3354 /*
3355 * If VPIDs are not supported or enabled, then the above is a no-op.
3356 * But we don't really need a TLB flush in that case anyway, because
3357 * each VM entry/exit includes an implicit flush when VPID is 0.
3358 */
3359}
3360
e8467fda
AK
3361static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3362{
3363 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3364
3365 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3366 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3367}
3368
aff48baa
AK
3369static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3370{
b4d18517 3371 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
aff48baa
AK
3372 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3373 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3374}
3375
25c4c276 3376static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
399badf3 3377{
fc78f519
AK
3378 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3379
3380 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3381 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
399badf3
AK
3382}
3383
1439442c
SY
3384static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3385{
d0d538b9
GN
3386 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3387
6de4f3ad
AK
3388 if (!test_bit(VCPU_EXREG_PDPTR,
3389 (unsigned long *)&vcpu->arch.regs_dirty))
3390 return;
3391
1439442c 3392 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
d0d538b9
GN
3393 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3394 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3395 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3396 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
1439442c
SY
3397 }
3398}
3399
8f5d549f
AK
3400static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3401{
d0d538b9
GN
3402 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3403
8f5d549f 3404 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
d0d538b9
GN
3405 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3406 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3407 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3408 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
8f5d549f 3409 }
6de4f3ad
AK
3410
3411 __set_bit(VCPU_EXREG_PDPTR,
3412 (unsigned long *)&vcpu->arch.regs_avail);
3413 __set_bit(VCPU_EXREG_PDPTR,
3414 (unsigned long *)&vcpu->arch.regs_dirty);
8f5d549f
AK
3415}
3416
3899152c
DM
3417static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
3418{
6677f3da
PB
3419 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
3420 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
3899152c
DM
3421 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3422
6677f3da 3423 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
3899152c
DM
3424 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
3425 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
3426 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
3427
3428 return fixed_bits_valid(val, fixed0, fixed1);
3429}
3430
3431static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
3432{
6677f3da
PB
3433 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
3434 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
3899152c
DM
3435
3436 return fixed_bits_valid(val, fixed0, fixed1);
3437}
3438
3439static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
3440{
6677f3da
PB
3441 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
3442 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
3899152c
DM
3443
3444 return fixed_bits_valid(val, fixed0, fixed1);
3445}
3446
3447/* No difference in the restrictions on guest and host CR4 in VMX operation. */
3448#define nested_guest_cr4_valid nested_cr4_valid
3449#define nested_host_cr4_valid nested_cr4_valid
3450
5e1746d6 3451static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1439442c
SY
3452
3453static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3454 unsigned long cr0,
3455 struct kvm_vcpu *vcpu)
3456{
5233dd51
MT
3457 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3458 vmx_decache_cr3(vcpu);
1439442c
SY
3459 if (!(cr0 & X86_CR0_PG)) {
3460 /* From paging/starting to nonpaging */
3461 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
65267ea1 3462 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
1439442c
SY
3463 (CPU_BASED_CR3_LOAD_EXITING |
3464 CPU_BASED_CR3_STORE_EXITING));
3465 vcpu->arch.cr0 = cr0;
fc78f519 3466 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
1439442c
SY
3467 } else if (!is_paging(vcpu)) {
3468 /* From nonpaging to paging */
3469 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
65267ea1 3470 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
1439442c
SY
3471 ~(CPU_BASED_CR3_LOAD_EXITING |
3472 CPU_BASED_CR3_STORE_EXITING));
3473 vcpu->arch.cr0 = cr0;
fc78f519 3474 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
1439442c 3475 }
95eb84a7
SY
3476
3477 if (!(cr0 & X86_CR0_WP))
3478 *hw_cr0 &= ~X86_CR0_WP;
1439442c
SY
3479}
3480
6aa8b732
AK
3481static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3482{
7ffd92c5 3483 struct vcpu_vmx *vmx = to_vmx(vcpu);
3a624e29
NK
3484 unsigned long hw_cr0;
3485
3de6347b 3486 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
3a624e29 3487 if (enable_unrestricted_guest)
5037878e 3488 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
218e763f 3489 else {
5037878e 3490 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
1439442c 3491
218e763f
GN
3492 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3493 enter_pmode(vcpu);
6aa8b732 3494
218e763f
GN
3495 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3496 enter_rmode(vcpu);
3497 }
6aa8b732 3498
05b3e0c2 3499#ifdef CONFIG_X86_64
f6801dff 3500 if (vcpu->arch.efer & EFER_LME) {
707d92fa 3501 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
6aa8b732 3502 enter_lmode(vcpu);
707d92fa 3503 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
6aa8b732
AK
3504 exit_lmode(vcpu);
3505 }
3506#endif
3507
b4d18517 3508 if (enable_ept && !enable_unrestricted_guest)
1439442c
SY
3509 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3510
6aa8b732 3511 vmcs_writel(CR0_READ_SHADOW, cr0);
1439442c 3512 vmcs_writel(GUEST_CR0, hw_cr0);
ad312c7c 3513 vcpu->arch.cr0 = cr0;
14168786
GN
3514
3515 /* depends on vcpu->arch.cr0 to be set to a new value */
3516 vmx->emulation_required = emulation_required(vcpu);
6aa8b732
AK
3517}
3518
855feb67
YZ
3519static int get_ept_level(struct kvm_vcpu *vcpu)
3520{
3521 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
3522 return 5;
3523 return 4;
3524}
3525
89b0c9f5 3526u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
1439442c 3527{
855feb67
YZ
3528 u64 eptp = VMX_EPTP_MT_WB;
3529
3530 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
1439442c 3531
995f00a6
PF
3532 if (enable_ept_ad_bits &&
3533 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
bb97a016 3534 eptp |= VMX_EPTP_AD_ENABLE_BIT;
1439442c
SY
3535 eptp |= (root_hpa & PAGE_MASK);
3536
3537 return eptp;
3538}
3539
6aa8b732
AK
3540static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3541{
877ad952 3542 struct kvm *kvm = vcpu->kvm;
1439442c
SY
3543 unsigned long guest_cr3;
3544 u64 eptp;
3545
3546 guest_cr3 = cr3;
089d034e 3547 if (enable_ept) {
995f00a6 3548 eptp = construct_eptp(vcpu, cr3);
1439442c 3549 vmcs_write64(EPT_POINTER, eptp);
877ad952
TL
3550
3551 if (kvm_x86_ops->tlb_remote_flush) {
3552 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3553 to_vmx(vcpu)->ept_pointer = eptp;
3554 to_kvm_vmx(kvm)->ept_pointers_match
3555 = EPT_POINTERS_CHECK;
3556 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3557 }
3558
e90008df
SC
3559 if (enable_unrestricted_guest || is_paging(vcpu) ||
3560 is_guest_mode(vcpu))
59ab5a8f
JK
3561 guest_cr3 = kvm_read_cr3(vcpu);
3562 else
877ad952 3563 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
7c93be44 3564 ept_load_pdptrs(vcpu);
1439442c
SY
3565 }
3566
1439442c 3567 vmcs_writel(GUEST_CR3, guest_cr3);
6aa8b732
AK
3568}
3569
5e1746d6 3570static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
6aa8b732 3571{
085e68ee
BS
3572 /*
3573 * Pass through host's Machine Check Enable value to hw_cr4, which
3574 * is in force while we are in guest mode. Do not let guests control
3575 * this bit, even if host CR4.MCE == 0.
3576 */
5dc1f044
SC
3577 unsigned long hw_cr4;
3578
3579 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
3580 if (enable_unrestricted_guest)
3581 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
3582 else if (to_vmx(vcpu)->rmode.vm86_active)
3583 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3584 else
3585 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
1439442c 3586
64f7a115
SC
3587 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3588 if (cr4 & X86_CR4_UMIP) {
3589 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
0367f205 3590 SECONDARY_EXEC_DESC);
64f7a115
SC
3591 hw_cr4 &= ~X86_CR4_UMIP;
3592 } else if (!is_guest_mode(vcpu) ||
3593 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
3594 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
3595 SECONDARY_EXEC_DESC);
3596 }
0367f205 3597
5e1746d6
NHE
3598 if (cr4 & X86_CR4_VMXE) {
3599 /*
3600 * To use VMXON (and later other VMX instructions), a guest
3601 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3602 * So basically the check on whether to allow nested VMX
5bea5123
PB
3603 * is here. We operate under the default treatment of SMM,
3604 * so VMX cannot be enabled under SMM.
5e1746d6 3605 */
5bea5123 3606 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
5e1746d6 3607 return 1;
1a0d74e6 3608 }
3899152c
DM
3609
3610 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
5e1746d6
NHE
3611 return 1;
3612
ad312c7c 3613 vcpu->arch.cr4 = cr4;
5dc1f044
SC
3614
3615 if (!enable_unrestricted_guest) {
3616 if (enable_ept) {
3617 if (!is_paging(vcpu)) {
3618 hw_cr4 &= ~X86_CR4_PAE;
3619 hw_cr4 |= X86_CR4_PSE;
3620 } else if (!(cr4 & X86_CR4_PAE)) {
3621 hw_cr4 &= ~X86_CR4_PAE;
3622 }
bc23008b 3623 }
1439442c 3624
656ec4a4 3625 /*
ddba2628
HH
3626 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3627 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3628 * to be manually disabled when guest switches to non-paging
3629 * mode.
3630 *
3631 * If !enable_unrestricted_guest, the CPU is always running
3632 * with CR0.PG=1 and CR4 needs to be modified.
3633 * If enable_unrestricted_guest, the CPU automatically
3634 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
656ec4a4 3635 */
5dc1f044
SC
3636 if (!is_paging(vcpu))
3637 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3638 }
656ec4a4 3639
1439442c
SY
3640 vmcs_writel(CR4_READ_SHADOW, cr4);
3641 vmcs_writel(GUEST_CR4, hw_cr4);
5e1746d6 3642 return 0;
6aa8b732
AK
3643}
3644
6aa8b732
AK
3645static void vmx_get_segment(struct kvm_vcpu *vcpu,
3646 struct kvm_segment *var, int seg)
3647{
a9179499 3648 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732
AK
3649 u32 ar;
3650
c6ad1153 3651 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
f5f7b2fe 3652 *var = vmx->rmode.segs[seg];
a9179499 3653 if (seg == VCPU_SREG_TR
2fb92db1 3654 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
f5f7b2fe 3655 return;
1390a28b
AK
3656 var->base = vmx_read_guest_seg_base(vmx, seg);
3657 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3658 return;
a9179499 3659 }
2fb92db1
AK
3660 var->base = vmx_read_guest_seg_base(vmx, seg);
3661 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3662 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3663 ar = vmx_read_guest_seg_ar(vmx, seg);
03617c18 3664 var->unusable = (ar >> 16) & 1;
6aa8b732
AK
3665 var->type = ar & 15;
3666 var->s = (ar >> 4) & 1;
3667 var->dpl = (ar >> 5) & 3;
03617c18
GN
3668 /*
3669 * Some userspaces do not preserve unusable property. Since usable
3670 * segment has to be present according to VMX spec we can use present
3671 * property to amend userspace bug by making unusable segment always
3672 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3673 * segment as unusable.
3674 */
3675 var->present = !var->unusable;
6aa8b732
AK
3676 var->avl = (ar >> 12) & 1;
3677 var->l = (ar >> 13) & 1;
3678 var->db = (ar >> 14) & 1;
3679 var->g = (ar >> 15) & 1;
6aa8b732
AK
3680}
3681
a9179499
AK
3682static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3683{
a9179499
AK
3684 struct kvm_segment s;
3685
3686 if (to_vmx(vcpu)->rmode.vm86_active) {
3687 vmx_get_segment(vcpu, &s, seg);
3688 return s.base;
3689 }
2fb92db1 3690 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
a9179499
AK
3691}
3692
b09408d0 3693static int vmx_get_cpl(struct kvm_vcpu *vcpu)
2e4d2653 3694{
b09408d0
MT
3695 struct vcpu_vmx *vmx = to_vmx(vcpu);
3696
ae9fedc7 3697 if (unlikely(vmx->rmode.vm86_active))
2e4d2653 3698 return 0;
ae9fedc7
PB
3699 else {
3700 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4d283ec9 3701 return VMX_AR_DPL(ar);
69c73028 3702 }
69c73028
AK
3703}
3704
653e3108 3705static u32 vmx_segment_access_rights(struct kvm_segment *var)
6aa8b732 3706{
6aa8b732
AK
3707 u32 ar;
3708
f0495f9b 3709 if (var->unusable || !var->present)
6aa8b732
AK
3710 ar = 1 << 16;
3711 else {
3712 ar = var->type & 15;
3713 ar |= (var->s & 1) << 4;
3714 ar |= (var->dpl & 3) << 5;
3715 ar |= (var->present & 1) << 7;
3716 ar |= (var->avl & 1) << 12;
3717 ar |= (var->l & 1) << 13;
3718 ar |= (var->db & 1) << 14;
3719 ar |= (var->g & 1) << 15;
3720 }
653e3108
AK
3721
3722 return ar;
3723}
3724
3725static void vmx_set_segment(struct kvm_vcpu *vcpu,
3726 struct kvm_segment *var, int seg)
3727{
7ffd92c5 3728 struct vcpu_vmx *vmx = to_vmx(vcpu);
772e0318 3729 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
653e3108 3730
2fb92db1
AK
3731 vmx_segment_cache_clear(vmx);
3732
1ecd50a9
GN
3733 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3734 vmx->rmode.segs[seg] = *var;
3735 if (seg == VCPU_SREG_TR)
3736 vmcs_write16(sf->selector, var->selector);
3737 else if (var->s)
3738 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
d99e4152 3739 goto out;
653e3108 3740 }
1ecd50a9 3741
653e3108
AK
3742 vmcs_writel(sf->base, var->base);
3743 vmcs_write32(sf->limit, var->limit);
3744 vmcs_write16(sf->selector, var->selector);
3a624e29
NK
3745
3746 /*
3747 * Fix the "Accessed" bit in AR field of segment registers for older
3748 * qemu binaries.
3749 * IA32 arch specifies that at the time of processor reset the
3750 * "Accessed" bit in the AR field of segment registers is 1. And qemu
0fa06071 3751 * is setting it to 0 in the userland code. This causes invalid guest
3a624e29
NK
3752 * state vmexit when "unrestricted guest" mode is turned on.
3753 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3754 * tree. Newer qemu binaries with that qemu fix would not need this
3755 * kvm hack.
3756 */
3757 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
f924d66d 3758 var->type |= 0x1; /* Accessed */
3a624e29 3759
f924d66d 3760 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
d99e4152
GN
3761
3762out:
98eb2f8b 3763 vmx->emulation_required = emulation_required(vcpu);
6aa8b732
AK
3764}
3765
6aa8b732
AK
3766static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3767{
2fb92db1 3768 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
6aa8b732
AK
3769
3770 *db = (ar >> 14) & 1;
3771 *l = (ar >> 13) & 1;
3772}
3773
89a27f4d 3774static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 3775{
89a27f4d
GN
3776 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3777 dt->address = vmcs_readl(GUEST_IDTR_BASE);
6aa8b732
AK
3778}
3779
89a27f4d 3780static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 3781{
89a27f4d
GN
3782 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3783 vmcs_writel(GUEST_IDTR_BASE, dt->address);
6aa8b732
AK
3784}
3785
89a27f4d 3786static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 3787{
89a27f4d
GN
3788 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3789 dt->address = vmcs_readl(GUEST_GDTR_BASE);
6aa8b732
AK
3790}
3791
89a27f4d 3792static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 3793{
89a27f4d
GN
3794 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3795 vmcs_writel(GUEST_GDTR_BASE, dt->address);
6aa8b732
AK
3796}
3797
648dfaa7
MG
3798static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3799{
3800 struct kvm_segment var;
3801 u32 ar;
3802
3803 vmx_get_segment(vcpu, &var, seg);
07f42f5f 3804 var.dpl = 0x3;
0647f4aa
GN
3805 if (seg == VCPU_SREG_CS)
3806 var.type = 0x3;
648dfaa7
MG
3807 ar = vmx_segment_access_rights(&var);
3808
3809 if (var.base != (var.selector << 4))
3810 return false;
89efbed0 3811 if (var.limit != 0xffff)
648dfaa7 3812 return false;
07f42f5f 3813 if (ar != 0xf3)
648dfaa7
MG
3814 return false;
3815
3816 return true;
3817}
3818
3819static bool code_segment_valid(struct kvm_vcpu *vcpu)
3820{
3821 struct kvm_segment cs;
3822 unsigned int cs_rpl;
3823
3824 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
b32a9918 3825 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
648dfaa7 3826
1872a3f4
AK
3827 if (cs.unusable)
3828 return false;
4d283ec9 3829 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
648dfaa7
MG
3830 return false;
3831 if (!cs.s)
3832 return false;
4d283ec9 3833 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
648dfaa7
MG
3834 if (cs.dpl > cs_rpl)
3835 return false;
1872a3f4 3836 } else {
648dfaa7
MG
3837 if (cs.dpl != cs_rpl)
3838 return false;
3839 }
3840 if (!cs.present)
3841 return false;
3842
3843 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3844 return true;
3845}
3846
3847static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3848{
3849 struct kvm_segment ss;
3850 unsigned int ss_rpl;
3851
3852 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
b32a9918 3853 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
648dfaa7 3854
1872a3f4
AK
3855 if (ss.unusable)
3856 return true;
3857 if (ss.type != 3 && ss.type != 7)
648dfaa7
MG
3858 return false;
3859 if (!ss.s)
3860 return false;
3861 if (ss.dpl != ss_rpl) /* DPL != RPL */
3862 return false;
3863 if (!ss.present)
3864 return false;
3865
3866 return true;
3867}
3868
3869static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3870{
3871 struct kvm_segment var;
3872 unsigned int rpl;
3873
3874 vmx_get_segment(vcpu, &var, seg);
b32a9918 3875 rpl = var.selector & SEGMENT_RPL_MASK;
648dfaa7 3876
1872a3f4
AK
3877 if (var.unusable)
3878 return true;
648dfaa7
MG
3879 if (!var.s)
3880 return false;
3881 if (!var.present)
3882 return false;
4d283ec9 3883 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
648dfaa7
MG
3884 if (var.dpl < rpl) /* DPL < RPL */
3885 return false;
3886 }
3887
3888 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3889 * rights flags
3890 */
3891 return true;
3892}
3893
3894static bool tr_valid(struct kvm_vcpu *vcpu)
3895{
3896 struct kvm_segment tr;
3897
3898 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3899
1872a3f4
AK
3900 if (tr.unusable)
3901 return false;
b32a9918 3902 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
648dfaa7 3903 return false;
1872a3f4 3904 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
648dfaa7
MG
3905 return false;
3906 if (!tr.present)
3907 return false;
3908
3909 return true;
3910}
3911
3912static bool ldtr_valid(struct kvm_vcpu *vcpu)
3913{
3914 struct kvm_segment ldtr;
3915
3916 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3917
1872a3f4
AK
3918 if (ldtr.unusable)
3919 return true;
b32a9918 3920 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
648dfaa7
MG
3921 return false;
3922 if (ldtr.type != 2)
3923 return false;
3924 if (!ldtr.present)
3925 return false;
3926
3927 return true;
3928}
3929
3930static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3931{
3932 struct kvm_segment cs, ss;
3933
3934 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3935 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3936
b32a9918
NA
3937 return ((cs.selector & SEGMENT_RPL_MASK) ==
3938 (ss.selector & SEGMENT_RPL_MASK));
648dfaa7
MG
3939}
3940
3941/*
3942 * Check if guest state is valid. Returns true if valid, false if
3943 * not.
3944 * We assume that registers are always usable
3945 */
3946static bool guest_state_valid(struct kvm_vcpu *vcpu)
3947{
c5e97c80
GN
3948 if (enable_unrestricted_guest)
3949 return true;
3950
648dfaa7 3951 /* real mode guest state checks */
f13882d8 3952 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
648dfaa7
MG
3953 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3954 return false;
3955 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3956 return false;
3957 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3958 return false;
3959 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3960 return false;
3961 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3962 return false;
3963 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3964 return false;
3965 } else {
3966 /* protected mode guest state checks */
3967 if (!cs_ss_rpl_check(vcpu))
3968 return false;
3969 if (!code_segment_valid(vcpu))
3970 return false;
3971 if (!stack_segment_valid(vcpu))
3972 return false;
3973 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3974 return false;
3975 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3976 return false;
3977 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3978 return false;
3979 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3980 return false;
3981 if (!tr_valid(vcpu))
3982 return false;
3983 if (!ldtr_valid(vcpu))
3984 return false;
3985 }
3986 /* TODO:
3987 * - Add checks on RIP
3988 * - Add checks on RFLAGS
3989 */
3990
3991 return true;
3992}
3993
5fa99cbe
JM
3994static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
3995{
3996 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
3997}
3998
d77c26fc 3999static int init_rmode_tss(struct kvm *kvm)
6aa8b732 4000{
40dcaa9f 4001 gfn_t fn;
195aefde 4002 u16 data = 0;
1f755a82 4003 int idx, r;
6aa8b732 4004
40dcaa9f 4005 idx = srcu_read_lock(&kvm->srcu);
40bbb9d0 4006 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
195aefde
IE
4007 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4008 if (r < 0)
10589a46 4009 goto out;
195aefde 4010 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
464d17c8
SY
4011 r = kvm_write_guest_page(kvm, fn++, &data,
4012 TSS_IOPB_BASE_OFFSET, sizeof(u16));
195aefde 4013 if (r < 0)
10589a46 4014 goto out;
195aefde
IE
4015 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4016 if (r < 0)
10589a46 4017 goto out;
195aefde
IE
4018 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4019 if (r < 0)
10589a46 4020 goto out;
195aefde 4021 data = ~0;
10589a46
MT
4022 r = kvm_write_guest_page(kvm, fn, &data,
4023 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4024 sizeof(u8));
10589a46 4025out:
40dcaa9f 4026 srcu_read_unlock(&kvm->srcu, idx);
1f755a82 4027 return r;
6aa8b732
AK
4028}
4029
b7ebfb05
SY
4030static int init_rmode_identity_map(struct kvm *kvm)
4031{
40bbb9d0 4032 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
f51770ed 4033 int i, idx, r = 0;
ba049e93 4034 kvm_pfn_t identity_map_pfn;
b7ebfb05
SY
4035 u32 tmp;
4036
40bbb9d0 4037 /* Protect kvm_vmx->ept_identity_pagetable_done. */
a255d479
TC
4038 mutex_lock(&kvm->slots_lock);
4039
40bbb9d0 4040 if (likely(kvm_vmx->ept_identity_pagetable_done))
a255d479 4041 goto out2;
a255d479 4042
40bbb9d0
SC
4043 if (!kvm_vmx->ept_identity_map_addr)
4044 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
4045 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
a255d479 4046
d8a6e365 4047 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
40bbb9d0 4048 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
f51770ed 4049 if (r < 0)
a255d479
TC
4050 goto out2;
4051
40dcaa9f 4052 idx = srcu_read_lock(&kvm->srcu);
b7ebfb05
SY
4053 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4054 if (r < 0)
4055 goto out;
4056 /* Set up identity-mapping pagetable for EPT in real mode */
4057 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4058 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4059 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4060 r = kvm_write_guest_page(kvm, identity_map_pfn,
4061 &tmp, i * sizeof(tmp), sizeof(tmp));
4062 if (r < 0)
4063 goto out;
4064 }
40bbb9d0 4065 kvm_vmx->ept_identity_pagetable_done = true;
f51770ed 4066
b7ebfb05 4067out:
40dcaa9f 4068 srcu_read_unlock(&kvm->srcu, idx);
a255d479
TC
4069
4070out2:
4071 mutex_unlock(&kvm->slots_lock);
f51770ed 4072 return r;
b7ebfb05
SY
4073}
4074
6aa8b732
AK
4075static void seg_setup(int seg)
4076{
772e0318 4077 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3a624e29 4078 unsigned int ar;
6aa8b732
AK
4079
4080 vmcs_write16(sf->selector, 0);
4081 vmcs_writel(sf->base, 0);
4082 vmcs_write32(sf->limit, 0xffff);
d54d07b2
GN
4083 ar = 0x93;
4084 if (seg == VCPU_SREG_CS)
4085 ar |= 0x08; /* code segment */
3a624e29
NK
4086
4087 vmcs_write32(sf->ar_bytes, ar);
6aa8b732
AK
4088}
4089
f78e0e2e
SY
4090static int alloc_apic_access_page(struct kvm *kvm)
4091{
4484141a 4092 struct page *page;
f78e0e2e
SY
4093 int r = 0;
4094
79fac95e 4095 mutex_lock(&kvm->slots_lock);
c24ae0dc 4096 if (kvm->arch.apic_access_page_done)
f78e0e2e 4097 goto out;
1d8007bd
PB
4098 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4099 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
f78e0e2e
SY
4100 if (r)
4101 goto out;
72dc67a6 4102
73a6d941 4103 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
4484141a
XG
4104 if (is_error_page(page)) {
4105 r = -EFAULT;
4106 goto out;
4107 }
4108
c24ae0dc
TC
4109 /*
4110 * Do not pin the page in memory, so that memory hot-unplug
4111 * is able to migrate it.
4112 */
4113 put_page(page);
4114 kvm->arch.apic_access_page_done = true;
f78e0e2e 4115out:
79fac95e 4116 mutex_unlock(&kvm->slots_lock);
f78e0e2e
SY
4117 return r;
4118}
4119
991e7a0e 4120static int allocate_vpid(void)
2384d2b3
SY
4121{
4122 int vpid;
4123
919818ab 4124 if (!enable_vpid)
991e7a0e 4125 return 0;
2384d2b3
SY
4126 spin_lock(&vmx_vpid_lock);
4127 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
991e7a0e 4128 if (vpid < VMX_NR_VPIDS)
2384d2b3 4129 __set_bit(vpid, vmx_vpid_bitmap);
991e7a0e
WL
4130 else
4131 vpid = 0;
2384d2b3 4132 spin_unlock(&vmx_vpid_lock);
991e7a0e 4133 return vpid;
2384d2b3
SY
4134}
4135
991e7a0e 4136static void free_vpid(int vpid)
cdbecfc3 4137{
991e7a0e 4138 if (!enable_vpid || vpid == 0)
cdbecfc3
LJ
4139 return;
4140 spin_lock(&vmx_vpid_lock);
991e7a0e 4141 __clear_bit(vpid, vmx_vpid_bitmap);
cdbecfc3
LJ
4142 spin_unlock(&vmx_vpid_lock);
4143}
4144
1e4329ee 4145static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
904e14fb 4146 u32 msr, int type)
25c5f225 4147{
3e7c73e9 4148 int f = sizeof(unsigned long);
25c5f225
SY
4149
4150 if (!cpu_has_vmx_msr_bitmap())
4151 return;
4152
ceef7d10
VK
4153 if (static_branch_unlikely(&enable_evmcs))
4154 evmcs_touch_msr_bitmap();
4155
25c5f225
SY
4156 /*
4157 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4158 * have the write-low and read-high bitmap offsets the wrong way round.
4159 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4160 */
25c5f225 4161 if (msr <= 0x1fff) {
8d14695f
YZ
4162 if (type & MSR_TYPE_R)
4163 /* read-low */
4164 __clear_bit(msr, msr_bitmap + 0x000 / f);
4165
4166 if (type & MSR_TYPE_W)
4167 /* write-low */
4168 __clear_bit(msr, msr_bitmap + 0x800 / f);
4169
25c5f225
SY
4170 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4171 msr &= 0x1fff;
8d14695f
YZ
4172 if (type & MSR_TYPE_R)
4173 /* read-high */
4174 __clear_bit(msr, msr_bitmap + 0x400 / f);
4175
4176 if (type & MSR_TYPE_W)
4177 /* write-high */
4178 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4179
4180 }
4181}
4182
1e4329ee 4183static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
904e14fb
PB
4184 u32 msr, int type)
4185{
4186 int f = sizeof(unsigned long);
4187
4188 if (!cpu_has_vmx_msr_bitmap())
4189 return;
4190
ceef7d10
VK
4191 if (static_branch_unlikely(&enable_evmcs))
4192 evmcs_touch_msr_bitmap();
4193
904e14fb
PB
4194 /*
4195 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4196 * have the write-low and read-high bitmap offsets the wrong way round.
4197 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4198 */
4199 if (msr <= 0x1fff) {
4200 if (type & MSR_TYPE_R)
4201 /* read-low */
4202 __set_bit(msr, msr_bitmap + 0x000 / f);
4203
4204 if (type & MSR_TYPE_W)
4205 /* write-low */
4206 __set_bit(msr, msr_bitmap + 0x800 / f);
4207
4208 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4209 msr &= 0x1fff;
4210 if (type & MSR_TYPE_R)
4211 /* read-high */
4212 __set_bit(msr, msr_bitmap + 0x400 / f);
4213
4214 if (type & MSR_TYPE_W)
4215 /* write-high */
4216 __set_bit(msr, msr_bitmap + 0xc00 / f);
4217
4218 }
4219}
4220
1e4329ee 4221static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
904e14fb
PB
4222 u32 msr, int type, bool value)
4223{
4224 if (value)
4225 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4226 else
4227 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4228}
4229
f2b93280
WV
4230/*
4231 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4232 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4233 */
4234static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4235 unsigned long *msr_bitmap_nested,
4236 u32 msr, int type)
4237{
4238 int f = sizeof(unsigned long);
4239
f2b93280
WV
4240 /*
4241 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4242 * have the write-low and read-high bitmap offsets the wrong way round.
4243 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4244 */
4245 if (msr <= 0x1fff) {
4246 if (type & MSR_TYPE_R &&
4247 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4248 /* read-low */
4249 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4250
4251 if (type & MSR_TYPE_W &&
4252 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4253 /* write-low */
4254 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4255
4256 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4257 msr &= 0x1fff;
4258 if (type & MSR_TYPE_R &&
4259 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4260 /* read-high */
4261 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4262
4263 if (type & MSR_TYPE_W &&
4264 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4265 /* write-high */
4266 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4267
4268 }
4269}
4270
904e14fb 4271static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
5897297b 4272{
904e14fb
PB
4273 u8 mode = 0;
4274
4275 if (cpu_has_secondary_exec_ctrls() &&
4276 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
4277 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4278 mode |= MSR_BITMAP_MODE_X2APIC;
4279 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4280 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4281 }
4282
904e14fb 4283 return mode;
8d14695f
YZ
4284}
4285
904e14fb
PB
4286static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4287 u8 mode)
8d14695f 4288{
904e14fb
PB
4289 int msr;
4290
4291 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4292 unsigned word = msr / BITS_PER_LONG;
4293 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4294 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
4295 }
4296
4297 if (mode & MSR_BITMAP_MODE_X2APIC) {
4298 /*
4299 * TPR reads and writes can be virtualized even if virtual interrupt
4300 * delivery is not in use.
4301 */
4302 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4303 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4304 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4305 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4306 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4307 }
f6e90f9e 4308 }
5897297b
AK
4309}
4310
904e14fb
PB
4311static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
4312{
4313 struct vcpu_vmx *vmx = to_vmx(vcpu);
4314 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4315 u8 mode = vmx_msr_bitmap_mode(vcpu);
4316 u8 changed = mode ^ vmx->msr_bitmap_mode;
4317
4318 if (!changed)
4319 return;
4320
904e14fb
PB
4321 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4322 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4323
4324 vmx->msr_bitmap_mode = mode;
4325}
4326
b2a05fef 4327static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
d50ab6c1 4328{
d62caabb 4329 return enable_apicv;
d50ab6c1
PB
4330}
4331
c9f04407
DM
4332static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
4333{
4334 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4335 gfn_t gfn;
4336
4337 /*
4338 * Don't need to mark the APIC access page dirty; it is never
4339 * written to by the CPU during APIC virtualization.
4340 */
4341
4342 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
4343 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
4344 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4345 }
4346
4347 if (nested_cpu_has_posted_intr(vmcs12)) {
4348 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
4349 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4350 }
4351}
4352
4353
6342c50a 4354static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
705699a1
WV
4355{
4356 struct vcpu_vmx *vmx = to_vmx(vcpu);
4357 int max_irr;
4358 void *vapic_page;
4359 u16 status;
4360
c9f04407
DM
4361 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
4362 return;
705699a1 4363
c9f04407
DM
4364 vmx->nested.pi_pending = false;
4365 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4366 return;
705699a1 4367
c9f04407
DM
4368 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
4369 if (max_irr != 256) {
705699a1 4370 vapic_page = kmap(vmx->nested.virtual_apic_page);
e7387b0e
LA
4371 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
4372 vapic_page, &max_irr);
705699a1
WV
4373 kunmap(vmx->nested.virtual_apic_page);
4374
4375 status = vmcs_read16(GUEST_INTR_STATUS);
4376 if ((u8)max_irr > ((u8)status & 0xff)) {
4377 status &= ~0xff;
4378 status |= (u8)max_irr;
4379 vmcs_write16(GUEST_INTR_STATUS, status);
4380 }
4381 }
c9f04407
DM
4382
4383 nested_mark_vmcs12_pages_dirty(vcpu);
705699a1
WV
4384}
4385
e6c67d8c
LA
4386static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
4387{
4388 struct vcpu_vmx *vmx = to_vmx(vcpu);
4389 void *vapic_page;
4390 u32 vppr;
4391 int rvi;
4392
4393 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
4394 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
4395 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
4396 return false;
4397
7e712684 4398 rvi = vmx_get_rvi();
e6c67d8c
LA
4399
4400 vapic_page = kmap(vmx->nested.virtual_apic_page);
4401 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
4402 kunmap(vmx->nested.virtual_apic_page);
4403
4404 return ((rvi & 0xf0) > (vppr & 0xf0));
4405}
4406
06a5524f
WV
4407static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
4408 bool nested)
21bc8dc5
RK
4409{
4410#ifdef CONFIG_SMP
06a5524f
WV
4411 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
4412
21bc8dc5 4413 if (vcpu->mode == IN_GUEST_MODE) {
28b835d6 4414 /*
5753743f
HZ
4415 * The vector of interrupt to be delivered to vcpu had
4416 * been set in PIR before this function.
4417 *
4418 * Following cases will be reached in this block, and
4419 * we always send a notification event in all cases as
4420 * explained below.
4421 *
4422 * Case 1: vcpu keeps in non-root mode. Sending a
4423 * notification event posts the interrupt to vcpu.
4424 *
4425 * Case 2: vcpu exits to root mode and is still
4426 * runnable. PIR will be synced to vIRR before the
4427 * next vcpu entry. Sending a notification event in
4428 * this case has no effect, as vcpu is not in root
4429 * mode.
28b835d6 4430 *
5753743f
HZ
4431 * Case 3: vcpu exits to root mode and is blocked.
4432 * vcpu_block() has already synced PIR to vIRR and
4433 * never blocks vcpu if vIRR is not cleared. Therefore,
4434 * a blocked vcpu here does not wait for any requested
4435 * interrupts in PIR, and sending a notification event
4436 * which has no effect is safe here.
28b835d6 4437 */
28b835d6 4438
06a5524f 4439 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
21bc8dc5
RK
4440 return true;
4441 }
4442#endif
4443 return false;
4444}
4445
705699a1
WV
4446static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4447 int vector)
4448{
4449 struct vcpu_vmx *vmx = to_vmx(vcpu);
4450
4451 if (is_guest_mode(vcpu) &&
4452 vector == vmx->nested.posted_intr_nv) {
705699a1
WV
4453 /*
4454 * If a posted intr is not recognized by hardware,
4455 * we will accomplish it in the next vmentry.
4456 */
4457 vmx->nested.pi_pending = true;
4458 kvm_make_request(KVM_REQ_EVENT, vcpu);
6b697711
LA
4459 /* the PIR and ON have been set by L1. */
4460 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
4461 kvm_vcpu_kick(vcpu);
705699a1
WV
4462 return 0;
4463 }
4464 return -1;
4465}
a20ed54d
YZ
4466/*
4467 * Send interrupt to vcpu via posted interrupt way.
4468 * 1. If target vcpu is running(non-root mode), send posted interrupt
4469 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4470 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4471 * interrupt from PIR in next vmentry.
4472 */
4473static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4474{
4475 struct vcpu_vmx *vmx = to_vmx(vcpu);
4476 int r;
4477
705699a1
WV
4478 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4479 if (!r)
4480 return;
4481
a20ed54d
YZ
4482 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4483 return;
4484
b95234c8
PB
4485 /* If a previous notification has sent the IPI, nothing to do. */
4486 if (pi_test_and_set_on(&vmx->pi_desc))
4487 return;
4488
06a5524f 4489 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
a20ed54d
YZ
4490 kvm_vcpu_kick(vcpu);
4491}
4492
a3a8ff8e
NHE
4493/*
4494 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4495 * will not change in the lifetime of the guest.
4496 * Note that host-state that does change is set elsewhere. E.g., host-state
4497 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4498 */
a547c6db 4499static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
a3a8ff8e
NHE
4500{
4501 u32 low32, high32;
4502 unsigned long tmpl;
4503 struct desc_ptr dt;
d6e41f11 4504 unsigned long cr0, cr3, cr4;
a3a8ff8e 4505
04ac88ab
AL
4506 cr0 = read_cr0();
4507 WARN_ON(cr0 & X86_CR0_TS);
4508 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
d6e41f11
AL
4509
4510 /*
4511 * Save the most likely value for this task's CR3 in the VMCS.
4512 * We can't use __get_current_cr3_fast() because we're not atomic.
4513 */
6c690ee1 4514 cr3 = __read_cr3();
d6e41f11 4515 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
d7ee039e 4516 vmx->loaded_vmcs->host_state.cr3 = cr3;
a3a8ff8e 4517
d974baa3 4518 /* Save the most likely value for this task's CR4 in the VMCS. */
1e02ce4c 4519 cr4 = cr4_read_shadow();
d974baa3 4520 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
d7ee039e 4521 vmx->loaded_vmcs->host_state.cr4 = cr4;
d974baa3 4522
a3a8ff8e 4523 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
b2da15ac
AK
4524#ifdef CONFIG_X86_64
4525 /*
4526 * Load null selectors, so we can avoid reloading them in
6d6095bd
SC
4527 * vmx_prepare_switch_to_host(), in case userspace uses
4528 * the null selectors too (the expected case).
b2da15ac
AK
4529 */
4530 vmcs_write16(HOST_DS_SELECTOR, 0);
4531 vmcs_write16(HOST_ES_SELECTOR, 0);
4532#else
a3a8ff8e
NHE
4533 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4534 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
b2da15ac 4535#endif
a3a8ff8e
NHE
4536 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4537 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4538
87930019 4539 store_idt(&dt);
a3a8ff8e 4540 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
a547c6db 4541 vmx->host_idt_base = dt.address;
a3a8ff8e 4542
83287ea4 4543 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
a3a8ff8e
NHE
4544
4545 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4546 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4547 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4548 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4549
4550 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4551 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4552 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4553 }
5a5e8a15 4554
c73da3fc 4555 if (cpu_has_load_ia32_efer())
5a5e8a15 4556 vmcs_write64(HOST_IA32_EFER, host_efer);
a3a8ff8e
NHE
4557}
4558
bf8179a0
NHE
4559static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4560{
4561 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4562 if (enable_ept)
4563 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
fe3ef05c
NHE
4564 if (is_guest_mode(&vmx->vcpu))
4565 vmx->vcpu.arch.cr4_guest_owned_bits &=
4566 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
bf8179a0
NHE
4567 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4568}
4569
01e439be
YZ
4570static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4571{
4572 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4573
d62caabb 4574 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
01e439be 4575 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
d02fcf50
PB
4576
4577 if (!enable_vnmi)
4578 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
4579
64672c95
YJ
4580 /* Enable the preemption timer dynamically */
4581 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
01e439be
YZ
4582 return pin_based_exec_ctrl;
4583}
4584
d62caabb
AS
4585static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4586{
4587 struct vcpu_vmx *vmx = to_vmx(vcpu);
4588
4589 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
3ce424e4
RK
4590 if (cpu_has_secondary_exec_ctrls()) {
4591 if (kvm_vcpu_apicv_active(vcpu))
4592 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4593 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4594 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4595 else
4596 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4597 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4598 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4599 }
4600
4601 if (cpu_has_vmx_msr_bitmap())
904e14fb 4602 vmx_update_msr_bitmap(vcpu);
d62caabb
AS
4603}
4604
89b0c9f5
SC
4605u32 vmx_exec_control(struct vcpu_vmx *vmx)
4606{
4607 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4608
4609 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4610 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4611
4612 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4613 exec_control &= ~CPU_BASED_TPR_SHADOW;
4614#ifdef CONFIG_X86_64
4615 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4616 CPU_BASED_CR8_LOAD_EXITING;
4617#endif
4618 }
4619 if (!enable_ept)
4620 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4621 CPU_BASED_CR3_LOAD_EXITING |
4622 CPU_BASED_INVLPG_EXITING;
4623 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4624 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4625 CPU_BASED_MONITOR_EXITING);
4626 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4627 exec_control &= ~CPU_BASED_HLT_EXITING;
4628 return exec_control;
4629}
4630
4631
80154d77 4632static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
bf8179a0 4633{
80154d77
PB
4634 struct kvm_vcpu *vcpu = &vmx->vcpu;
4635
bf8179a0 4636 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
0367f205 4637
80154d77 4638 if (!cpu_need_virtualize_apic_accesses(vcpu))
bf8179a0
NHE
4639 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4640 if (vmx->vpid == 0)
4641 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4642 if (!enable_ept) {
4643 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4644 enable_unrestricted_guest = 0;
4645 }
4646 if (!enable_unrestricted_guest)
4647 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
b31c114b 4648 if (kvm_pause_in_guest(vmx->vcpu.kvm))
bf8179a0 4649 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
80154d77 4650 if (!kvm_vcpu_apicv_active(vcpu))
c7c9c56c
YZ
4651 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4652 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
8d14695f 4653 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
0367f205
PB
4654
4655 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4656 * in vmx_set_cr4. */
4657 exec_control &= ~SECONDARY_EXEC_DESC;
4658
abc4fc58
AG
4659 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4660 (handle_vmptrld).
4661 We can NOT enable shadow_vmcs here because we don't have yet
4662 a current VMCS12
4663 */
4664 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
a3eaa864
KH
4665
4666 if (!enable_pml)
4667 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
843e4330 4668
3db13480
PB
4669 if (vmx_xsaves_supported()) {
4670 /* Exposing XSAVES only when XSAVE is exposed */
4671 bool xsaves_enabled =
4672 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4673 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4674
4675 if (!xsaves_enabled)
4676 exec_control &= ~SECONDARY_EXEC_XSAVES;
4677
4678 if (nested) {
4679 if (xsaves_enabled)
6677f3da 4680 vmx->nested.msrs.secondary_ctls_high |=
3db13480
PB
4681 SECONDARY_EXEC_XSAVES;
4682 else
6677f3da 4683 vmx->nested.msrs.secondary_ctls_high &=
3db13480
PB
4684 ~SECONDARY_EXEC_XSAVES;
4685 }
4686 }
4687
80154d77
PB
4688 if (vmx_rdtscp_supported()) {
4689 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
4690 if (!rdtscp_enabled)
4691 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4692
4693 if (nested) {
4694 if (rdtscp_enabled)
6677f3da 4695 vmx->nested.msrs.secondary_ctls_high |=
80154d77
PB
4696 SECONDARY_EXEC_RDTSCP;
4697 else
6677f3da 4698 vmx->nested.msrs.secondary_ctls_high &=
80154d77
PB
4699 ~SECONDARY_EXEC_RDTSCP;
4700 }
4701 }
4702
4703 if (vmx_invpcid_supported()) {
4704 /* Exposing INVPCID only when PCID is exposed */
4705 bool invpcid_enabled =
4706 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
4707 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
4708
4709 if (!invpcid_enabled) {
4710 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
4711 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
4712 }
4713
4714 if (nested) {
4715 if (invpcid_enabled)
6677f3da 4716 vmx->nested.msrs.secondary_ctls_high |=
80154d77
PB
4717 SECONDARY_EXEC_ENABLE_INVPCID;
4718 else
6677f3da 4719 vmx->nested.msrs.secondary_ctls_high &=
80154d77
PB
4720 ~SECONDARY_EXEC_ENABLE_INVPCID;
4721 }
4722 }
4723
45ec368c
JM
4724 if (vmx_rdrand_supported()) {
4725 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
4726 if (rdrand_enabled)
736fdf72 4727 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
45ec368c
JM
4728
4729 if (nested) {
4730 if (rdrand_enabled)
6677f3da 4731 vmx->nested.msrs.secondary_ctls_high |=
736fdf72 4732 SECONDARY_EXEC_RDRAND_EXITING;
45ec368c 4733 else
6677f3da 4734 vmx->nested.msrs.secondary_ctls_high &=
736fdf72 4735 ~SECONDARY_EXEC_RDRAND_EXITING;
45ec368c
JM
4736 }
4737 }
4738
75f4fc8d
JM
4739 if (vmx_rdseed_supported()) {
4740 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
4741 if (rdseed_enabled)
736fdf72 4742 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
75f4fc8d
JM
4743
4744 if (nested) {
4745 if (rdseed_enabled)
6677f3da 4746 vmx->nested.msrs.secondary_ctls_high |=
736fdf72 4747 SECONDARY_EXEC_RDSEED_EXITING;
75f4fc8d 4748 else
6677f3da 4749 vmx->nested.msrs.secondary_ctls_high &=
736fdf72 4750 ~SECONDARY_EXEC_RDSEED_EXITING;
75f4fc8d
JM
4751 }
4752 }
4753
80154d77 4754 vmx->secondary_exec_control = exec_control;
bf8179a0
NHE
4755}
4756
ce88decf
XG
4757static void ept_set_mmio_spte_mask(void)
4758{
4759 /*
4760 * EPT Misconfigurations can be generated if the value of bits 2:0
4761 * of an EPT paging-structure entry is 110b (write/execute).
ce88decf 4762 */
dcdca5fe
PF
4763 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
4764 VMX_EPT_MISCONFIG_WX_VALUE);
ce88decf
XG
4765}
4766
f53cd63c 4767#define VMX_XSS_EXIT_BITMAP 0
6aa8b732 4768
944c3464
SC
4769static void nested_vmx_vcpu_setup(void)
4770{
4607c2d7 4771 if (enable_shadow_vmcs) {
f4160e45
JM
4772 /*
4773 * At vCPU creation, "VMWRITE to any supported field
4774 * in the VMCS" is supported, so use the more
4775 * permissive vmx_vmread_bitmap to specify both read
4776 * and write permissions for the shadow VMCS.
4777 */
4607c2d7 4778 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
f4160e45 4779 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
4607c2d7 4780 }
944c3464
SC
4781}
4782
4783/*
4784 * Sets up the vmcs for emulated real mode.
4785 */
4786static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
4787{
4788 int i;
4789
4790 if (nested)
4791 nested_vmx_vcpu_setup();
4792
25c5f225 4793 if (cpu_has_vmx_msr_bitmap())
904e14fb 4794 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
25c5f225 4795
6aa8b732
AK
4796 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4797
6aa8b732 4798 /* Control */
01e439be 4799 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
64672c95 4800 vmx->hv_deadline_tsc = -1;
6e5d865c 4801
bf8179a0 4802 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
6aa8b732 4803
dfa169bb 4804 if (cpu_has_secondary_exec_ctrls()) {
80154d77 4805 vmx_compute_secondary_exec_control(vmx);
bf8179a0 4806 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
80154d77 4807 vmx->secondary_exec_control);
dfa169bb 4808 }
f78e0e2e 4809
d62caabb 4810 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
c7c9c56c
YZ
4811 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4812 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4813 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4814 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4815
4816 vmcs_write16(GUEST_INTR_STATUS, 0);
01e439be 4817
0bcf261c 4818 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
01e439be 4819 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
c7c9c56c
YZ
4820 }
4821
b31c114b 4822 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
4b8d54f9 4823 vmcs_write32(PLE_GAP, ple_gap);
a7653ecd
RK
4824 vmx->ple_window = ple_window;
4825 vmx->ple_window_dirty = true;
4b8d54f9
ZE
4826 }
4827
c3707958
XG
4828 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4829 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
6aa8b732
AK
4830 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4831
9581d442
AK
4832 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4833 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
a547c6db 4834 vmx_set_constant_host_state(vmx);
6aa8b732
AK
4835 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4836 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6aa8b732 4837
2a499e49
BD
4838 if (cpu_has_vmx_vmfunc())
4839 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4840
2cc51560
ED
4841 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4842 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
33966dd6 4843 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2cc51560 4844 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
33966dd6 4845 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
6aa8b732 4846
74545705
RK
4847 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4848 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
468d472f 4849
03916db9 4850 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6aa8b732
AK
4851 u32 index = vmx_msr_index[i];
4852 u32 data_low, data_high;
a2fa3e9f 4853 int j = vmx->nmsrs;
6aa8b732
AK
4854
4855 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4856 continue;
432bd6cb
AK
4857 if (wrmsr_safe(index, data_low, data_high) < 0)
4858 continue;
26bb0981
AK
4859 vmx->guest_msrs[j].index = i;
4860 vmx->guest_msrs[j].data = 0;
d5696725 4861 vmx->guest_msrs[j].mask = -1ull;
a2fa3e9f 4862 ++vmx->nmsrs;
6aa8b732 4863 }
6aa8b732 4864
5b76a3cf 4865 vmx->arch_capabilities = kvm_get_arch_capabilities();
2961e876 4866
c73da3fc 4867 vm_exit_controls_init(vmx, vmx_vmexit_ctrl());
6aa8b732
AK
4868
4869 /* 22.2.1, 20.8.1 */
c73da3fc 4870 vm_entry_controls_init(vmx, vmx_vmentry_ctrl());
1c3d14fe 4871
bd7e5b08
PB
4872 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
4873 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
4874
bf8179a0 4875 set_cr4_guest_host_mask(vmx);
e00c8cf2 4876
f53cd63c
WL
4877 if (vmx_xsaves_supported())
4878 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4879
4e59516a 4880 if (enable_pml) {
4e59516a
PF
4881 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4882 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4883 }
0b665d30
SC
4884
4885 if (cpu_has_vmx_encls_vmexit())
4886 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
e00c8cf2
AK
4887}
4888
d28bc9dd 4889static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
e00c8cf2
AK
4890{
4891 struct vcpu_vmx *vmx = to_vmx(vcpu);
58cb628d 4892 struct msr_data apic_base_msr;
d28bc9dd 4893 u64 cr0;
e00c8cf2 4894
7ffd92c5 4895 vmx->rmode.vm86_active = 0;
d28b387f 4896 vmx->spec_ctrl = 0;
e00c8cf2 4897
518e7b94 4898 vcpu->arch.microcode_version = 0x100000000ULL;
ad312c7c 4899 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
d28bc9dd
NA
4900 kvm_set_cr8(vcpu, 0);
4901
4902 if (!init_event) {
4903 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4904 MSR_IA32_APICBASE_ENABLE;
4905 if (kvm_vcpu_is_reset_bsp(vcpu))
4906 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4907 apic_base_msr.host_initiated = true;
4908 kvm_set_apic_base(vcpu, &apic_base_msr);
4909 }
e00c8cf2 4910
2fb92db1
AK
4911 vmx_segment_cache_clear(vmx);
4912
5706be0d 4913 seg_setup(VCPU_SREG_CS);
66450a21 4914 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
f3531054 4915 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
e00c8cf2
AK
4916
4917 seg_setup(VCPU_SREG_DS);
4918 seg_setup(VCPU_SREG_ES);
4919 seg_setup(VCPU_SREG_FS);
4920 seg_setup(VCPU_SREG_GS);
4921 seg_setup(VCPU_SREG_SS);
4922
4923 vmcs_write16(GUEST_TR_SELECTOR, 0);
4924 vmcs_writel(GUEST_TR_BASE, 0);
4925 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4926 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4927
4928 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4929 vmcs_writel(GUEST_LDTR_BASE, 0);
4930 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4931 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4932
d28bc9dd
NA
4933 if (!init_event) {
4934 vmcs_write32(GUEST_SYSENTER_CS, 0);
4935 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4936 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4937 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4938 }
e00c8cf2 4939
c37c2873 4940 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
66450a21 4941 kvm_rip_write(vcpu, 0xfff0);
e00c8cf2 4942
e00c8cf2
AK
4943 vmcs_writel(GUEST_GDTR_BASE, 0);
4944 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4945
4946 vmcs_writel(GUEST_IDTR_BASE, 0);
4947 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4948
443381a8 4949 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
e00c8cf2 4950 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
f3531054 4951 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
a554d207
WL
4952 if (kvm_mpx_supported())
4953 vmcs_write64(GUEST_BNDCFGS, 0);
e00c8cf2 4954
e00c8cf2
AK
4955 setup_msrs(vmx);
4956
6aa8b732
AK
4957 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4958
d28bc9dd 4959 if (cpu_has_vmx_tpr_shadow() && !init_event) {
f78e0e2e 4960 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
35754c98 4961 if (cpu_need_tpr_shadow(vcpu))
f78e0e2e 4962 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
d28bc9dd 4963 __pa(vcpu->arch.apic->regs));
f78e0e2e
SY
4964 vmcs_write32(TPR_THRESHOLD, 0);
4965 }
4966
a73896cb 4967 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6aa8b732 4968
2384d2b3
SY
4969 if (vmx->vpid != 0)
4970 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4971
d28bc9dd 4972 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
d28bc9dd 4973 vmx->vcpu.arch.cr0 = cr0;
f2463247 4974 vmx_set_cr0(vcpu, cr0); /* enter rmode */
d28bc9dd 4975 vmx_set_cr4(vcpu, 0);
5690891b 4976 vmx_set_efer(vcpu, 0);
bd7e5b08 4977
d28bc9dd 4978 update_exception_bitmap(vcpu);
6aa8b732 4979
dd5f5341 4980 vpid_sync_context(vmx->vpid);
caa057a2
WL
4981 if (init_event)
4982 vmx_clear_hlt(vcpu);
6aa8b732
AK
4983}
4984
b6f1250e
NHE
4985/*
4986 * In nested virtualization, check if L1 asked to exit on external interrupts.
4987 * For most existing hypervisors, this will always return true.
4988 */
4989static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4990{
4991 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4992 PIN_BASED_EXT_INTR_MASK;
4993}
4994
77b0f5d6
BD
4995/*
4996 * In nested virtualization, check if L1 has set
4997 * VM_EXIT_ACK_INTR_ON_EXIT
4998 */
4999static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5000{
5001 return get_vmcs12(vcpu)->vm_exit_controls &
5002 VM_EXIT_ACK_INTR_ON_EXIT;
5003}
5004
ea8ceb83
JK
5005static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5006{
0c7f650e 5007 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
ea8ceb83
JK
5008}
5009
c9a7953f 5010static void enable_irq_window(struct kvm_vcpu *vcpu)
3b86cd99 5011{
47c0152e
PB
5012 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5013 CPU_BASED_VIRTUAL_INTR_PENDING);
3b86cd99
JK
5014}
5015
c9a7953f 5016static void enable_nmi_window(struct kvm_vcpu *vcpu)
3b86cd99 5017{
d02fcf50 5018 if (!enable_vnmi ||
8a1b4392 5019 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
c9a7953f
JK
5020 enable_irq_window(vcpu);
5021 return;
5022 }
3b86cd99 5023
47c0152e
PB
5024 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5025 CPU_BASED_VIRTUAL_NMI_PENDING);
3b86cd99
JK
5026}
5027
66fd3f7f 5028static void vmx_inject_irq(struct kvm_vcpu *vcpu)
85f455f7 5029{
9c8cba37 5030 struct vcpu_vmx *vmx = to_vmx(vcpu);
66fd3f7f
GN
5031 uint32_t intr;
5032 int irq = vcpu->arch.interrupt.nr;
9c8cba37 5033
229456fc 5034 trace_kvm_inj_virq(irq);
2714d1d3 5035
fa89a817 5036 ++vcpu->stat.irq_injections;
7ffd92c5 5037 if (vmx->rmode.vm86_active) {
71f9833b
SH
5038 int inc_eip = 0;
5039 if (vcpu->arch.interrupt.soft)
5040 inc_eip = vcpu->arch.event_exit_inst_len;
5041 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
a92601bb 5042 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
85f455f7
ED
5043 return;
5044 }
66fd3f7f
GN
5045 intr = irq | INTR_INFO_VALID_MASK;
5046 if (vcpu->arch.interrupt.soft) {
5047 intr |= INTR_TYPE_SOFT_INTR;
5048 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5049 vmx->vcpu.arch.event_exit_inst_len);
5050 } else
5051 intr |= INTR_TYPE_EXT_INTR;
5052 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
caa057a2
WL
5053
5054 vmx_clear_hlt(vcpu);
85f455f7
ED
5055}
5056
f08864b4
SY
5057static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5058{
66a5a347
JK
5059 struct vcpu_vmx *vmx = to_vmx(vcpu);
5060
d02fcf50 5061 if (!enable_vnmi) {
8a1b4392
PB
5062 /*
5063 * Tracking the NMI-blocked state in software is built upon
5064 * finding the next open IRQ window. This, in turn, depends on
5065 * well-behaving guests: They have to keep IRQs disabled at
5066 * least as long as the NMI handler runs. Otherwise we may
5067 * cause NMI nesting, maybe breaking the guest. But as this is
5068 * highly unlikely, we can live with the residual risk.
5069 */
5070 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5071 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5072 }
5073
4c4a6f79
PB
5074 ++vcpu->stat.nmi_injections;
5075 vmx->loaded_vmcs->nmi_known_unmasked = false;
3b86cd99 5076
7ffd92c5 5077 if (vmx->rmode.vm86_active) {
71f9833b 5078 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
a92601bb 5079 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
66a5a347
JK
5080 return;
5081 }
c5a6d5f7 5082
f08864b4
SY
5083 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5084 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
caa057a2
WL
5085
5086 vmx_clear_hlt(vcpu);
f08864b4
SY
5087}
5088
3cfc3092
JK
5089static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5090{
4c4a6f79
PB
5091 struct vcpu_vmx *vmx = to_vmx(vcpu);
5092 bool masked;
5093
d02fcf50 5094 if (!enable_vnmi)
8a1b4392 5095 return vmx->loaded_vmcs->soft_vnmi_blocked;
4c4a6f79 5096 if (vmx->loaded_vmcs->nmi_known_unmasked)
9d58b931 5097 return false;
4c4a6f79
PB
5098 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5099 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5100 return masked;
3cfc3092
JK
5101}
5102
5103static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5104{
5105 struct vcpu_vmx *vmx = to_vmx(vcpu);
5106
d02fcf50 5107 if (!enable_vnmi) {
8a1b4392
PB
5108 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5109 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5110 vmx->loaded_vmcs->vnmi_blocked_time = 0;
5111 }
5112 } else {
5113 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5114 if (masked)
5115 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5116 GUEST_INTR_STATE_NMI);
5117 else
5118 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5119 GUEST_INTR_STATE_NMI);
5120 }
3cfc3092
JK
5121}
5122
2505dc9f
JK
5123static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5124{
b6b8a145
JK
5125 if (to_vmx(vcpu)->nested.nested_run_pending)
5126 return 0;
ea8ceb83 5127
d02fcf50 5128 if (!enable_vnmi &&
8a1b4392
PB
5129 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5130 return 0;
5131
2505dc9f
JK
5132 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5133 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5134 | GUEST_INTR_STATE_NMI));
5135}
5136
78646121
GN
5137static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5138{
b6b8a145
JK
5139 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5140 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
c4282df9
GN
5141 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5142 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
78646121
GN
5143}
5144
cbc94022
IE
5145static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5146{
5147 int ret;
cbc94022 5148
f7eaeb0a
SC
5149 if (enable_unrestricted_guest)
5150 return 0;
5151
1d8007bd
PB
5152 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5153 PAGE_SIZE * 3);
cbc94022
IE
5154 if (ret)
5155 return ret;
40bbb9d0 5156 to_kvm_vmx(kvm)->tss_addr = addr;
1f755a82 5157 return init_rmode_tss(kvm);
cbc94022
IE
5158}
5159
2ac52ab8
SC
5160static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5161{
40bbb9d0 5162 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
2ac52ab8
SC
5163 return 0;
5164}
5165
0ca1b4f4 5166static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
6aa8b732 5167{
77ab6db0 5168 switch (vec) {
77ab6db0 5169 case BP_VECTOR:
c573cd22
JK
5170 /*
5171 * Update instruction length as we may reinject the exception
5172 * from user space while in guest debugging mode.
5173 */
5174 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5175 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
d0bfb940 5176 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
0ca1b4f4
GN
5177 return false;
5178 /* fall through */
5179 case DB_VECTOR:
5180 if (vcpu->guest_debug &
5181 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5182 return false;
d0bfb940
JK
5183 /* fall through */
5184 case DE_VECTOR:
77ab6db0
JK
5185 case OF_VECTOR:
5186 case BR_VECTOR:
5187 case UD_VECTOR:
5188 case DF_VECTOR:
5189 case SS_VECTOR:
5190 case GP_VECTOR:
5191 case MF_VECTOR:
0ca1b4f4
GN
5192 return true;
5193 break;
77ab6db0 5194 }
0ca1b4f4
GN
5195 return false;
5196}
5197
5198static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5199 int vec, u32 err_code)
5200{
5201 /*
5202 * Instruction with address size override prefix opcode 0x67
5203 * Cause the #SS fault with 0 error code in VM86 mode.
5204 */
5205 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
0ce97a2b 5206 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
0ca1b4f4
GN
5207 if (vcpu->arch.halt_request) {
5208 vcpu->arch.halt_request = 0;
5cb56059 5209 return kvm_vcpu_halt(vcpu);
0ca1b4f4
GN
5210 }
5211 return 1;
5212 }
5213 return 0;
5214 }
5215
5216 /*
5217 * Forward all other exceptions that are valid in real mode.
5218 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5219 * the required debugging infrastructure rework.
5220 */
5221 kvm_queue_exception(vcpu, vec);
5222 return 1;
6aa8b732
AK
5223}
5224
a0861c02
AK
5225/*
5226 * Trigger machine check on the host. We assume all the MSRs are already set up
5227 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5228 * We pass a fake environment to the machine check handler because we want
5229 * the guest to be always treated like user space, no matter what context
5230 * it used internally.
5231 */
5232static void kvm_machine_check(void)
5233{
5234#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5235 struct pt_regs regs = {
5236 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5237 .flags = X86_EFLAGS_IF,
5238 };
5239
5240 do_machine_check(&regs, 0);
5241#endif
5242}
5243
851ba692 5244static int handle_machine_check(struct kvm_vcpu *vcpu)
a0861c02
AK
5245{
5246 /* already handled by vcpu_run */
5247 return 1;
5248}
5249
851ba692 5250static int handle_exception(struct kvm_vcpu *vcpu)
6aa8b732 5251{
1155f76a 5252 struct vcpu_vmx *vmx = to_vmx(vcpu);
851ba692 5253 struct kvm_run *kvm_run = vcpu->run;
d0bfb940 5254 u32 intr_info, ex_no, error_code;
42dbaa5a 5255 unsigned long cr2, rip, dr6;
6aa8b732
AK
5256 u32 vect_info;
5257 enum emulation_result er;
5258
1155f76a 5259 vect_info = vmx->idt_vectoring_info;
88786475 5260 intr_info = vmx->exit_intr_info;
6aa8b732 5261
a0861c02 5262 if (is_machine_check(intr_info))
851ba692 5263 return handle_machine_check(vcpu);
a0861c02 5264
ef85b673 5265 if (is_nmi(intr_info))
1b6269db 5266 return 1; /* already handled by vmx_vcpu_run() */
2ab455cc 5267
082d06ed
WL
5268 if (is_invalid_opcode(intr_info))
5269 return handle_ud(vcpu);
7aa81cc0 5270
6aa8b732 5271 error_code = 0;
2e11384c 5272 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
6aa8b732 5273 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
bf4ca23e 5274
9e869480
LA
5275 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
5276 WARN_ON_ONCE(!enable_vmware_backdoor);
0ce97a2b 5277 er = kvm_emulate_instruction(vcpu,
9e869480
LA
5278 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
5279 if (er == EMULATE_USER_EXIT)
5280 return 0;
5281 else if (er != EMULATE_DONE)
5282 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
5283 return 1;
5284 }
5285
bf4ca23e
XG
5286 /*
5287 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5288 * MMIO, it is better to report an internal error.
5289 * See the comments in vmx_handle_exit.
5290 */
5291 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5292 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5293 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5294 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
80f0e95d 5295 vcpu->run->internal.ndata = 3;
bf4ca23e
XG
5296 vcpu->run->internal.data[0] = vect_info;
5297 vcpu->run->internal.data[1] = intr_info;
80f0e95d 5298 vcpu->run->internal.data[2] = error_code;
bf4ca23e
XG
5299 return 0;
5300 }
5301
6aa8b732
AK
5302 if (is_page_fault(intr_info)) {
5303 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1261bfa3
WL
5304 /* EPT won't cause page fault directly */
5305 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
d0006530 5306 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
6aa8b732
AK
5307 }
5308
d0bfb940 5309 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
0ca1b4f4
GN
5310
5311 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5312 return handle_rmode_exception(vcpu, ex_no, error_code);
5313
42dbaa5a 5314 switch (ex_no) {
54a20552
EN
5315 case AC_VECTOR:
5316 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5317 return 1;
42dbaa5a
JK
5318 case DB_VECTOR:
5319 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5320 if (!(vcpu->guest_debug &
5321 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
8246bf52 5322 vcpu->arch.dr6 &= ~15;
6f43ed01 5323 vcpu->arch.dr6 |= dr6 | DR6_RTM;
32d43cd3 5324 if (is_icebp(intr_info))
fd2a445a
HD
5325 skip_emulated_instruction(vcpu);
5326
42dbaa5a
JK
5327 kvm_queue_exception(vcpu, DB_VECTOR);
5328 return 1;
5329 }
5330 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5331 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5332 /* fall through */
5333 case BP_VECTOR:
c573cd22
JK
5334 /*
5335 * Update instruction length as we may reinject #BP from
5336 * user space while in guest debugging mode. Reading it for
5337 * #DB as well causes no harm, it is not used in that case.
5338 */
5339 vmx->vcpu.arch.event_exit_inst_len =
5340 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6aa8b732 5341 kvm_run->exit_reason = KVM_EXIT_DEBUG;
0a434bb2 5342 rip = kvm_rip_read(vcpu);
d0bfb940
JK
5343 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5344 kvm_run->debug.arch.exception = ex_no;
42dbaa5a
JK
5345 break;
5346 default:
d0bfb940
JK
5347 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5348 kvm_run->ex.exception = ex_no;
5349 kvm_run->ex.error_code = error_code;
42dbaa5a 5350 break;
6aa8b732 5351 }
6aa8b732
AK
5352 return 0;
5353}
5354
851ba692 5355static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6aa8b732 5356{
1165f5fe 5357 ++vcpu->stat.irq_exits;
6aa8b732
AK
5358 return 1;
5359}
5360
851ba692 5361static int handle_triple_fault(struct kvm_vcpu *vcpu)
988ad74f 5362{
851ba692 5363 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
bbeac283 5364 vcpu->mmio_needed = 0;
988ad74f
AK
5365 return 0;
5366}
6aa8b732 5367
851ba692 5368static int handle_io(struct kvm_vcpu *vcpu)
6aa8b732 5369{
bfdaab09 5370 unsigned long exit_qualification;
dca7f128 5371 int size, in, string;
039576c0 5372 unsigned port;
6aa8b732 5373
bfdaab09 5374 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
039576c0 5375 string = (exit_qualification & 16) != 0;
e70669ab 5376
cf8f70bf 5377 ++vcpu->stat.io_exits;
e70669ab 5378
432baf60 5379 if (string)
0ce97a2b 5380 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
e70669ab 5381
cf8f70bf
GN
5382 port = exit_qualification >> 16;
5383 size = (exit_qualification & 7) + 1;
432baf60 5384 in = (exit_qualification & 8) != 0;
cf8f70bf 5385
dca7f128 5386 return kvm_fast_pio(vcpu, size, port, in);
6aa8b732
AK
5387}
5388
102d8325
IM
5389static void
5390vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5391{
5392 /*
5393 * Patch in the VMCALL instruction:
5394 */
5395 hypercall[0] = 0x0f;
5396 hypercall[1] = 0x01;
5397 hypercall[2] = 0xc1;
102d8325
IM
5398}
5399
0fa06071 5400/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
eeadf9e7
NHE
5401static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5402{
eeadf9e7 5403 if (is_guest_mode(vcpu)) {
1a0d74e6
JK
5404 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5405 unsigned long orig_val = val;
5406
eeadf9e7
NHE
5407 /*
5408 * We get here when L2 changed cr0 in a way that did not change
5409 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
1a0d74e6
JK
5410 * but did change L0 shadowed bits. So we first calculate the
5411 * effective cr0 value that L1 would like to write into the
5412 * hardware. It consists of the L2-owned bits from the new
5413 * value combined with the L1-owned bits from L1's guest_cr0.
eeadf9e7 5414 */
1a0d74e6
JK
5415 val = (val & ~vmcs12->cr0_guest_host_mask) |
5416 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5417
3899152c 5418 if (!nested_guest_cr0_valid(vcpu, val))
eeadf9e7 5419 return 1;
1a0d74e6
JK
5420
5421 if (kvm_set_cr0(vcpu, val))
5422 return 1;
5423 vmcs_writel(CR0_READ_SHADOW, orig_val);
eeadf9e7 5424 return 0;
1a0d74e6
JK
5425 } else {
5426 if (to_vmx(vcpu)->nested.vmxon &&
3899152c 5427 !nested_host_cr0_valid(vcpu, val))
1a0d74e6 5428 return 1;
3899152c 5429
eeadf9e7 5430 return kvm_set_cr0(vcpu, val);
1a0d74e6 5431 }
eeadf9e7
NHE
5432}
5433
5434static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5435{
5436 if (is_guest_mode(vcpu)) {
1a0d74e6
JK
5437 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5438 unsigned long orig_val = val;
5439
5440 /* analogously to handle_set_cr0 */
5441 val = (val & ~vmcs12->cr4_guest_host_mask) |
5442 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5443 if (kvm_set_cr4(vcpu, val))
eeadf9e7 5444 return 1;
1a0d74e6 5445 vmcs_writel(CR4_READ_SHADOW, orig_val);
eeadf9e7
NHE
5446 return 0;
5447 } else
5448 return kvm_set_cr4(vcpu, val);
5449}
5450
0367f205
PB
5451static int handle_desc(struct kvm_vcpu *vcpu)
5452{
5453 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
0ce97a2b 5454 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
0367f205
PB
5455}
5456
851ba692 5457static int handle_cr(struct kvm_vcpu *vcpu)
6aa8b732 5458{
229456fc 5459 unsigned long exit_qualification, val;
6aa8b732
AK
5460 int cr;
5461 int reg;
49a9b07e 5462 int err;
6affcbed 5463 int ret;
6aa8b732 5464
bfdaab09 5465 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6aa8b732
AK
5466 cr = exit_qualification & 15;
5467 reg = (exit_qualification >> 8) & 15;
5468 switch ((exit_qualification >> 4) & 3) {
5469 case 0: /* mov to cr */
1e32c079 5470 val = kvm_register_readl(vcpu, reg);
229456fc 5471 trace_kvm_cr_write(cr, val);
6aa8b732
AK
5472 switch (cr) {
5473 case 0:
eeadf9e7 5474 err = handle_set_cr0(vcpu, val);
6affcbed 5475 return kvm_complete_insn_gp(vcpu, err);
6aa8b732 5476 case 3:
e1de91cc 5477 WARN_ON_ONCE(enable_unrestricted_guest);
2390218b 5478 err = kvm_set_cr3(vcpu, val);
6affcbed 5479 return kvm_complete_insn_gp(vcpu, err);
6aa8b732 5480 case 4:
eeadf9e7 5481 err = handle_set_cr4(vcpu, val);
6affcbed 5482 return kvm_complete_insn_gp(vcpu, err);
0a5fff19
GN
5483 case 8: {
5484 u8 cr8_prev = kvm_get_cr8(vcpu);
1e32c079 5485 u8 cr8 = (u8)val;
eea1cff9 5486 err = kvm_set_cr8(vcpu, cr8);
6affcbed 5487 ret = kvm_complete_insn_gp(vcpu, err);
35754c98 5488 if (lapic_in_kernel(vcpu))
6affcbed 5489 return ret;
0a5fff19 5490 if (cr8_prev <= cr8)
6affcbed
KH
5491 return ret;
5492 /*
5493 * TODO: we might be squashing a
5494 * KVM_GUESTDBG_SINGLESTEP-triggered
5495 * KVM_EXIT_DEBUG here.
5496 */
851ba692 5497 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
0a5fff19
GN
5498 return 0;
5499 }
4b8073e4 5500 }
6aa8b732 5501 break;
25c4c276 5502 case 2: /* clts */
bd7e5b08
PB
5503 WARN_ONCE(1, "Guest should always own CR0.TS");
5504 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4d4ec087 5505 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
6affcbed 5506 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
5507 case 1: /*mov from cr*/
5508 switch (cr) {
5509 case 3:
e1de91cc 5510 WARN_ON_ONCE(enable_unrestricted_guest);
9f8fe504
AK
5511 val = kvm_read_cr3(vcpu);
5512 kvm_register_write(vcpu, reg, val);
5513 trace_kvm_cr_read(cr, val);
6affcbed 5514 return kvm_skip_emulated_instruction(vcpu);
6aa8b732 5515 case 8:
229456fc
MT
5516 val = kvm_get_cr8(vcpu);
5517 kvm_register_write(vcpu, reg, val);
5518 trace_kvm_cr_read(cr, val);
6affcbed 5519 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
5520 }
5521 break;
5522 case 3: /* lmsw */
a1f83a74 5523 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
4d4ec087 5524 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
a1f83a74 5525 kvm_lmsw(vcpu, val);
6aa8b732 5526
6affcbed 5527 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
5528 default:
5529 break;
5530 }
851ba692 5531 vcpu->run->exit_reason = 0;
a737f256 5532 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6aa8b732
AK
5533 (int)(exit_qualification >> 4) & 3, cr);
5534 return 0;
5535}
5536
851ba692 5537static int handle_dr(struct kvm_vcpu *vcpu)
6aa8b732 5538{
bfdaab09 5539 unsigned long exit_qualification;
16f8a6f9
NA
5540 int dr, dr7, reg;
5541
5542 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5543 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5544
5545 /* First, if DR does not exist, trigger UD */
5546 if (!kvm_require_dr(vcpu, dr))
5547 return 1;
6aa8b732 5548
f2483415 5549 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
0a79b009
AK
5550 if (!kvm_require_cpl(vcpu, 0))
5551 return 1;
16f8a6f9
NA
5552 dr7 = vmcs_readl(GUEST_DR7);
5553 if (dr7 & DR7_GD) {
42dbaa5a
JK
5554 /*
5555 * As the vm-exit takes precedence over the debug trap, we
5556 * need to emulate the latter, either for the host or the
5557 * guest debugging itself.
5558 */
5559 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
851ba692 5560 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
16f8a6f9 5561 vcpu->run->debug.arch.dr7 = dr7;
82b32774 5562 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
851ba692
AK
5563 vcpu->run->debug.arch.exception = DB_VECTOR;
5564 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
42dbaa5a
JK
5565 return 0;
5566 } else {
7305eb5d 5567 vcpu->arch.dr6 &= ~15;
6f43ed01 5568 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
42dbaa5a
JK
5569 kvm_queue_exception(vcpu, DB_VECTOR);
5570 return 1;
5571 }
5572 }
5573
81908bf4 5574 if (vcpu->guest_debug == 0) {
8f22372f
PB
5575 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5576 CPU_BASED_MOV_DR_EXITING);
81908bf4
PB
5577
5578 /*
5579 * No more DR vmexits; force a reload of the debug registers
5580 * and reenter on this instruction. The next vmexit will
5581 * retrieve the full state of the debug registers.
5582 */
5583 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5584 return 1;
5585 }
5586
42dbaa5a
JK
5587 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5588 if (exit_qualification & TYPE_MOV_FROM_DR) {
020df079 5589 unsigned long val;
4c4d563b
JK
5590
5591 if (kvm_get_dr(vcpu, dr, &val))
5592 return 1;
5593 kvm_register_write(vcpu, reg, val);
020df079 5594 } else
5777392e 5595 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
4c4d563b
JK
5596 return 1;
5597
6affcbed 5598 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
5599}
5600
73aaf249
JK
5601static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5602{
5603 return vcpu->arch.dr6;
5604}
5605
5606static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5607{
5608}
5609
81908bf4
PB
5610static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5611{
81908bf4
PB
5612 get_debugreg(vcpu->arch.db[0], 0);
5613 get_debugreg(vcpu->arch.db[1], 1);
5614 get_debugreg(vcpu->arch.db[2], 2);
5615 get_debugreg(vcpu->arch.db[3], 3);
5616 get_debugreg(vcpu->arch.dr6, 6);
5617 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5618
5619 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
8f22372f 5620 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
81908bf4
PB
5621}
5622
020df079
GN
5623static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5624{
5625 vmcs_writel(GUEST_DR7, val);
5626}
5627
851ba692 5628static int handle_cpuid(struct kvm_vcpu *vcpu)
6aa8b732 5629{
6a908b62 5630 return kvm_emulate_cpuid(vcpu);
6aa8b732
AK
5631}
5632
851ba692 5633static int handle_rdmsr(struct kvm_vcpu *vcpu)
6aa8b732 5634{
ad312c7c 5635 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
609e36d3 5636 struct msr_data msr_info;
6aa8b732 5637
609e36d3
PB
5638 msr_info.index = ecx;
5639 msr_info.host_initiated = false;
5640 if (vmx_get_msr(vcpu, &msr_info)) {
59200273 5641 trace_kvm_msr_read_ex(ecx);
c1a5d4f9 5642 kvm_inject_gp(vcpu, 0);
6aa8b732
AK
5643 return 1;
5644 }
5645
609e36d3 5646 trace_kvm_msr_read(ecx, msr_info.data);
2714d1d3 5647
6aa8b732 5648 /* FIXME: handling of bits 32:63 of rax, rdx */
609e36d3
PB
5649 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5650 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
6affcbed 5651 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
5652}
5653
851ba692 5654static int handle_wrmsr(struct kvm_vcpu *vcpu)
6aa8b732 5655{
8fe8ab46 5656 struct msr_data msr;
ad312c7c
ZX
5657 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5658 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5659 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
6aa8b732 5660
8fe8ab46
WA
5661 msr.data = data;
5662 msr.index = ecx;
5663 msr.host_initiated = false;
854e8bb1 5664 if (kvm_set_msr(vcpu, &msr) != 0) {
59200273 5665 trace_kvm_msr_write_ex(ecx, data);
c1a5d4f9 5666 kvm_inject_gp(vcpu, 0);
6aa8b732
AK
5667 return 1;
5668 }
5669
59200273 5670 trace_kvm_msr_write(ecx, data);
6affcbed 5671 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
5672}
5673
851ba692 5674static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6e5d865c 5675{
eb90f341 5676 kvm_apic_update_ppr(vcpu);
6e5d865c
YS
5677 return 1;
5678}
5679
851ba692 5680static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6aa8b732 5681{
47c0152e
PB
5682 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5683 CPU_BASED_VIRTUAL_INTR_PENDING);
2714d1d3 5684
3842d135
AK
5685 kvm_make_request(KVM_REQ_EVENT, vcpu);
5686
a26bf12a 5687 ++vcpu->stat.irq_window_exits;
6aa8b732
AK
5688 return 1;
5689}
5690
851ba692 5691static int handle_halt(struct kvm_vcpu *vcpu)
6aa8b732 5692{
d3bef15f 5693 return kvm_emulate_halt(vcpu);
6aa8b732
AK
5694}
5695
851ba692 5696static int handle_vmcall(struct kvm_vcpu *vcpu)
c21415e8 5697{
0d9c055e 5698 return kvm_emulate_hypercall(vcpu);
c21415e8
IM
5699}
5700
ec25d5e6
GN
5701static int handle_invd(struct kvm_vcpu *vcpu)
5702{
0ce97a2b 5703 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
ec25d5e6
GN
5704}
5705
851ba692 5706static int handle_invlpg(struct kvm_vcpu *vcpu)
a7052897 5707{
f9c617f6 5708 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
a7052897
MT
5709
5710 kvm_mmu_invlpg(vcpu, exit_qualification);
6affcbed 5711 return kvm_skip_emulated_instruction(vcpu);
a7052897
MT
5712}
5713
fee84b07
AK
5714static int handle_rdpmc(struct kvm_vcpu *vcpu)
5715{
5716 int err;
5717
5718 err = kvm_rdpmc(vcpu);
6affcbed 5719 return kvm_complete_insn_gp(vcpu, err);
fee84b07
AK
5720}
5721
851ba692 5722static int handle_wbinvd(struct kvm_vcpu *vcpu)
e5edaa01 5723{
6affcbed 5724 return kvm_emulate_wbinvd(vcpu);
e5edaa01
ED
5725}
5726
2acf923e
DC
5727static int handle_xsetbv(struct kvm_vcpu *vcpu)
5728{
5729 u64 new_bv = kvm_read_edx_eax(vcpu);
5730 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5731
5732 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6affcbed 5733 return kvm_skip_emulated_instruction(vcpu);
2acf923e
DC
5734 return 1;
5735}
5736
f53cd63c
WL
5737static int handle_xsaves(struct kvm_vcpu *vcpu)
5738{
6affcbed 5739 kvm_skip_emulated_instruction(vcpu);
f53cd63c
WL
5740 WARN(1, "this should never happen\n");
5741 return 1;
5742}
5743
5744static int handle_xrstors(struct kvm_vcpu *vcpu)
5745{
6affcbed 5746 kvm_skip_emulated_instruction(vcpu);
f53cd63c
WL
5747 WARN(1, "this should never happen\n");
5748 return 1;
5749}
5750
851ba692 5751static int handle_apic_access(struct kvm_vcpu *vcpu)
f78e0e2e 5752{
58fbbf26
KT
5753 if (likely(fasteoi)) {
5754 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5755 int access_type, offset;
5756
5757 access_type = exit_qualification & APIC_ACCESS_TYPE;
5758 offset = exit_qualification & APIC_ACCESS_OFFSET;
5759 /*
5760 * Sane guest uses MOV to write EOI, with written value
5761 * not cared. So make a short-circuit here by avoiding
5762 * heavy instruction emulation.
5763 */
5764 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5765 (offset == APIC_EOI)) {
5766 kvm_lapic_set_eoi(vcpu);
6affcbed 5767 return kvm_skip_emulated_instruction(vcpu);
58fbbf26
KT
5768 }
5769 }
0ce97a2b 5770 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
f78e0e2e
SY
5771}
5772
c7c9c56c
YZ
5773static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5774{
5775 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5776 int vector = exit_qualification & 0xff;
5777
5778 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5779 kvm_apic_set_eoi_accelerated(vcpu, vector);
5780 return 1;
5781}
5782
83d4c286
YZ
5783static int handle_apic_write(struct kvm_vcpu *vcpu)
5784{
5785 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5786 u32 offset = exit_qualification & 0xfff;
5787
5788 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5789 kvm_apic_write_nodecode(vcpu, offset);
5790 return 1;
5791}
5792
851ba692 5793static int handle_task_switch(struct kvm_vcpu *vcpu)
37817f29 5794{
60637aac 5795 struct vcpu_vmx *vmx = to_vmx(vcpu);
37817f29 5796 unsigned long exit_qualification;
e269fb21
JK
5797 bool has_error_code = false;
5798 u32 error_code = 0;
37817f29 5799 u16 tss_selector;
7f3d35fd 5800 int reason, type, idt_v, idt_index;
64a7ec06
GN
5801
5802 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
7f3d35fd 5803 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
64a7ec06 5804 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
37817f29
IE
5805
5806 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5807
5808 reason = (u32)exit_qualification >> 30;
64a7ec06
GN
5809 if (reason == TASK_SWITCH_GATE && idt_v) {
5810 switch (type) {
5811 case INTR_TYPE_NMI_INTR:
5812 vcpu->arch.nmi_injected = false;
654f06fc 5813 vmx_set_nmi_mask(vcpu, true);
64a7ec06
GN
5814 break;
5815 case INTR_TYPE_EXT_INTR:
66fd3f7f 5816 case INTR_TYPE_SOFT_INTR:
64a7ec06
GN
5817 kvm_clear_interrupt_queue(vcpu);
5818 break;
5819 case INTR_TYPE_HARD_EXCEPTION:
e269fb21
JK
5820 if (vmx->idt_vectoring_info &
5821 VECTORING_INFO_DELIVER_CODE_MASK) {
5822 has_error_code = true;
5823 error_code =
5824 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5825 }
5826 /* fall through */
64a7ec06
GN
5827 case INTR_TYPE_SOFT_EXCEPTION:
5828 kvm_clear_exception_queue(vcpu);
5829 break;
5830 default:
5831 break;
5832 }
60637aac 5833 }
37817f29
IE
5834 tss_selector = exit_qualification;
5835
64a7ec06
GN
5836 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5837 type != INTR_TYPE_EXT_INTR &&
5838 type != INTR_TYPE_NMI_INTR))
5839 skip_emulated_instruction(vcpu);
5840
7f3d35fd
KW
5841 if (kvm_task_switch(vcpu, tss_selector,
5842 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5843 has_error_code, error_code) == EMULATE_FAIL) {
acb54517
GN
5844 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5845 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5846 vcpu->run->internal.ndata = 0;
42dbaa5a 5847 return 0;
acb54517 5848 }
42dbaa5a 5849
42dbaa5a
JK
5850 /*
5851 * TODO: What about debug traps on tss switch?
5852 * Are we supposed to inject them and update dr6?
5853 */
5854
5855 return 1;
37817f29
IE
5856}
5857
851ba692 5858static int handle_ept_violation(struct kvm_vcpu *vcpu)
1439442c 5859{
f9c617f6 5860 unsigned long exit_qualification;
1439442c 5861 gpa_t gpa;
eebed243 5862 u64 error_code;
1439442c 5863
f9c617f6 5864 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
1439442c 5865
0be9c7a8
GN
5866 /*
5867 * EPT violation happened while executing iret from NMI,
5868 * "blocked by NMI" bit has to be set before next VM entry.
5869 * There are errata that may cause this bit to not be set:
5870 * AAK134, BY25.
5871 */
bcd1c294 5872 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
d02fcf50 5873 enable_vnmi &&
bcd1c294 5874 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
0be9c7a8
GN
5875 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5876
1439442c 5877 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
229456fc 5878 trace_kvm_page_fault(gpa, exit_qualification);
4f5982a5 5879
27959a44 5880 /* Is it a read fault? */
ab22a473 5881 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
27959a44
JS
5882 ? PFERR_USER_MASK : 0;
5883 /* Is it a write fault? */
ab22a473 5884 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
27959a44
JS
5885 ? PFERR_WRITE_MASK : 0;
5886 /* Is it a fetch fault? */
ab22a473 5887 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
27959a44
JS
5888 ? PFERR_FETCH_MASK : 0;
5889 /* ept page table entry is present? */
5890 error_code |= (exit_qualification &
5891 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5892 EPT_VIOLATION_EXECUTABLE))
5893 ? PFERR_PRESENT_MASK : 0;
4f5982a5 5894
eebed243
PB
5895 error_code |= (exit_qualification & 0x100) != 0 ?
5896 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
25d92081 5897
25d92081 5898 vcpu->arch.exit_qualification = exit_qualification;
4f5982a5 5899 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
1439442c
SY
5900}
5901
851ba692 5902static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
68f89400 5903{
68f89400
MT
5904 gpa_t gpa;
5905
9034e6e8
PB
5906 /*
5907 * A nested guest cannot optimize MMIO vmexits, because we have an
5908 * nGPA here instead of the required GPA.
5909 */
68f89400 5910 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9034e6e8
PB
5911 if (!is_guest_mode(vcpu) &&
5912 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
931c33b1 5913 trace_kvm_fast_mmio(gpa);
d391f120
VK
5914 /*
5915 * Doing kvm_skip_emulated_instruction() depends on undefined
5916 * behavior: Intel's manual doesn't mandate
5917 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
5918 * occurs and while on real hardware it was observed to be set,
5919 * other hypervisors (namely Hyper-V) don't set it, we end up
5920 * advancing IP with some random value. Disable fast mmio when
5921 * running nested and keep it for real hardware in hope that
5922 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
5923 */
5924 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
5925 return kvm_skip_emulated_instruction(vcpu);
5926 else
0ce97a2b 5927 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
c4409905 5928 EMULATE_DONE;
68c3b4d1 5929 }
68f89400 5930
c75d0edc 5931 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
68f89400
MT
5932}
5933
851ba692 5934static int handle_nmi_window(struct kvm_vcpu *vcpu)
f08864b4 5935{
d02fcf50 5936 WARN_ON_ONCE(!enable_vnmi);
47c0152e
PB
5937 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5938 CPU_BASED_VIRTUAL_NMI_PENDING);
f08864b4 5939 ++vcpu->stat.nmi_window_exits;
3842d135 5940 kvm_make_request(KVM_REQ_EVENT, vcpu);
f08864b4
SY
5941
5942 return 1;
5943}
5944
80ced186 5945static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
ea953ef0 5946{
8b3079a5
AK
5947 struct vcpu_vmx *vmx = to_vmx(vcpu);
5948 enum emulation_result err = EMULATE_DONE;
80ced186 5949 int ret = 1;
49e9d557
AK
5950 u32 cpu_exec_ctrl;
5951 bool intr_window_requested;
b8405c18 5952 unsigned count = 130;
49e9d557 5953
2bb8cafe
SC
5954 /*
5955 * We should never reach the point where we are emulating L2
5956 * due to invalid guest state as that means we incorrectly
5957 * allowed a nested VMEntry with an invalid vmcs12.
5958 */
5959 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
5960
49e9d557
AK
5961 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5962 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
ea953ef0 5963
98eb2f8b 5964 while (vmx->emulation_required && count-- != 0) {
bdea48e3 5965 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
49e9d557
AK
5966 return handle_interrupt_window(&vmx->vcpu);
5967
72875d8a 5968 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
de87dcdd
AK
5969 return 1;
5970
0ce97a2b 5971 err = kvm_emulate_instruction(vcpu, 0);
ea953ef0 5972
ac0a48c3 5973 if (err == EMULATE_USER_EXIT) {
94452b9e 5974 ++vcpu->stat.mmio_exits;
80ced186
MG
5975 ret = 0;
5976 goto out;
5977 }
1d5a4d9b 5978
add5ff7a
SC
5979 if (err != EMULATE_DONE)
5980 goto emulation_error;
5981
5982 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
5983 vcpu->arch.exception.pending)
5984 goto emulation_error;
ea953ef0 5985
8d76c49e
GN
5986 if (vcpu->arch.halt_request) {
5987 vcpu->arch.halt_request = 0;
5cb56059 5988 ret = kvm_vcpu_halt(vcpu);
8d76c49e
GN
5989 goto out;
5990 }
5991
ea953ef0 5992 if (signal_pending(current))
80ced186 5993 goto out;
ea953ef0
MG
5994 if (need_resched())
5995 schedule();
5996 }
5997
80ced186
MG
5998out:
5999 return ret;
b4a2d31d 6000
add5ff7a
SC
6001emulation_error:
6002 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6003 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6004 vcpu->run->internal.ndata = 0;
6005 return 0;
b4a2d31d
RK
6006}
6007
6008static void grow_ple_window(struct kvm_vcpu *vcpu)
6009{
6010 struct vcpu_vmx *vmx = to_vmx(vcpu);
6011 int old = vmx->ple_window;
6012
c8e88717
BM
6013 vmx->ple_window = __grow_ple_window(old, ple_window,
6014 ple_window_grow,
6015 ple_window_max);
b4a2d31d
RK
6016
6017 if (vmx->ple_window != old)
6018 vmx->ple_window_dirty = true;
7b46268d
RK
6019
6020 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
b4a2d31d
RK
6021}
6022
6023static void shrink_ple_window(struct kvm_vcpu *vcpu)
6024{
6025 struct vcpu_vmx *vmx = to_vmx(vcpu);
6026 int old = vmx->ple_window;
6027
c8e88717
BM
6028 vmx->ple_window = __shrink_ple_window(old, ple_window,
6029 ple_window_shrink,
6030 ple_window);
b4a2d31d
RK
6031
6032 if (vmx->ple_window != old)
6033 vmx->ple_window_dirty = true;
7b46268d
RK
6034
6035 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
b4a2d31d
RK
6036}
6037
bf9f6ac8
FW
6038/*
6039 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6040 */
6041static void wakeup_handler(void)
6042{
6043 struct kvm_vcpu *vcpu;
6044 int cpu = smp_processor_id();
6045
6046 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6047 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6048 blocked_vcpu_list) {
6049 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6050
6051 if (pi_test_on(pi_desc) == 1)
6052 kvm_vcpu_kick(vcpu);
6053 }
6054 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6055}
6056
e01bca2f 6057static void vmx_enable_tdp(void)
f160c7b7
JS
6058{
6059 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6060 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6061 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6062 0ull, VMX_EPT_EXECUTABLE_MASK,
6063 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
d0ec49d4 6064 VMX_EPT_RWX_MASK, 0ull);
f160c7b7
JS
6065
6066 ept_set_mmio_spte_mask();
6067 kvm_enable_tdp();
6068}
6069
4b8d54f9
ZE
6070/*
6071 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6072 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6073 */
9fb41ba8 6074static int handle_pause(struct kvm_vcpu *vcpu)
4b8d54f9 6075{
b31c114b 6076 if (!kvm_pause_in_guest(vcpu->kvm))
b4a2d31d
RK
6077 grow_ple_window(vcpu);
6078
de63ad4c
LM
6079 /*
6080 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
6081 * VM-execution control is ignored if CPL > 0. OTOH, KVM
6082 * never set PAUSE_EXITING and just set PLE if supported,
6083 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
6084 */
6085 kvm_vcpu_on_spin(vcpu, true);
6affcbed 6086 return kvm_skip_emulated_instruction(vcpu);
4b8d54f9
ZE
6087}
6088
87c00572 6089static int handle_nop(struct kvm_vcpu *vcpu)
59708670 6090{
6affcbed 6091 return kvm_skip_emulated_instruction(vcpu);
59708670
SY
6092}
6093
87c00572
GS
6094static int handle_mwait(struct kvm_vcpu *vcpu)
6095{
6096 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6097 return handle_nop(vcpu);
6098}
6099
45ec368c
JM
6100static int handle_invalid_op(struct kvm_vcpu *vcpu)
6101{
6102 kvm_queue_exception(vcpu, UD_VECTOR);
6103 return 1;
6104}
6105
5f3d45e7
MD
6106static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6107{
6108 return 1;
6109}
6110
87c00572
GS
6111static int handle_monitor(struct kvm_vcpu *vcpu)
6112{
6113 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6114 return handle_nop(vcpu);
6115}
6116
0658fbaa
ACL
6117/*
6118 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
09abb5e3
SC
6119 * set the success or error code of an emulated VMX instruction (as specified
6120 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
6121 * instruction.
0658fbaa 6122 */
09abb5e3 6123static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
0658fbaa
ACL
6124{
6125 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6126 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6127 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
09abb5e3 6128 return kvm_skip_emulated_instruction(vcpu);
0658fbaa
ACL
6129}
6130
09abb5e3 6131static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
0658fbaa
ACL
6132{
6133 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6134 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6135 X86_EFLAGS_SF | X86_EFLAGS_OF))
6136 | X86_EFLAGS_CF);
09abb5e3 6137 return kvm_skip_emulated_instruction(vcpu);
0658fbaa
ACL
6138}
6139
09abb5e3
SC
6140static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
6141 u32 vm_instruction_error)
0658fbaa 6142{
b8bbab92
VK
6143 struct vcpu_vmx *vmx = to_vmx(vcpu);
6144
09abb5e3
SC
6145 /*
6146 * failValid writes the error number to the current VMCS, which
6147 * can't be done if there isn't a current VMCS.
6148 */
b8bbab92 6149 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
09abb5e3
SC
6150 return nested_vmx_failInvalid(vcpu);
6151
0658fbaa
ACL
6152 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6153 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6154 X86_EFLAGS_SF | X86_EFLAGS_OF))
6155 | X86_EFLAGS_ZF);
6156 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6157 /*
6158 * We don't need to force a shadow sync because
6159 * VM_INSTRUCTION_ERROR is not shadowed
6160 */
09abb5e3 6161 return kvm_skip_emulated_instruction(vcpu);
0658fbaa 6162}
145c28dd 6163
ff651cb6
WV
6164static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6165{
6166 /* TODO: not to reset guest simply here. */
6167 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
bbe41b95 6168 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
ff651cb6
WV
6169}
6170
f4124500
JK
6171static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6172{
6173 struct vcpu_vmx *vmx =
6174 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6175
6176 vmx->nested.preemption_timer_expired = true;
6177 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6178 kvm_vcpu_kick(&vmx->vcpu);
6179
6180 return HRTIMER_NORESTART;
6181}
6182
19677e32
BD
6183/*
6184 * Decode the memory-address operand of a vmx instruction, as recorded on an
6185 * exit caused by such an instruction (run by a guest hypervisor).
6186 * On success, returns 0. When the operand is invalid, returns 1 and throws
6187 * #UD or #GP.
6188 */
6189static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6190 unsigned long exit_qualification,
f9eb4af6 6191 u32 vmx_instruction_info, bool wr, gva_t *ret)
19677e32 6192{
f9eb4af6
EK
6193 gva_t off;
6194 bool exn;
6195 struct kvm_segment s;
6196
19677e32
BD
6197 /*
6198 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6199 * Execution", on an exit, vmx_instruction_info holds most of the
6200 * addressing components of the operand. Only the displacement part
6201 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6202 * For how an actual address is calculated from all these components,
6203 * refer to Vol. 1, "Operand Addressing".
6204 */
6205 int scaling = vmx_instruction_info & 3;
6206 int addr_size = (vmx_instruction_info >> 7) & 7;
6207 bool is_reg = vmx_instruction_info & (1u << 10);
6208 int seg_reg = (vmx_instruction_info >> 15) & 7;
6209 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6210 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6211 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6212 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6213
6214 if (is_reg) {
6215 kvm_queue_exception(vcpu, UD_VECTOR);
6216 return 1;
6217 }
6218
6219 /* Addr = segment_base + offset */
6220 /* offset = base + [index * scale] + displacement */
f9eb4af6 6221 off = exit_qualification; /* holds the displacement */
19677e32 6222 if (base_is_valid)
f9eb4af6 6223 off += kvm_register_read(vcpu, base_reg);
19677e32 6224 if (index_is_valid)
f9eb4af6
EK
6225 off += kvm_register_read(vcpu, index_reg)<<scaling;
6226 vmx_get_segment(vcpu, &s, seg_reg);
6227 *ret = s.base + off;
19677e32
BD
6228
6229 if (addr_size == 1) /* 32 bit */
6230 *ret &= 0xffffffff;
6231
f9eb4af6
EK
6232 /* Checks for #GP/#SS exceptions. */
6233 exn = false;
ff30ef40
QC
6234 if (is_long_mode(vcpu)) {
6235 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6236 * non-canonical form. This is the only check on the memory
6237 * destination for long mode!
6238 */
fd8cb433 6239 exn = is_noncanonical_address(*ret, vcpu);
ff30ef40 6240 } else if (is_protmode(vcpu)) {
f9eb4af6
EK
6241 /* Protected mode: apply checks for segment validity in the
6242 * following order:
6243 * - segment type check (#GP(0) may be thrown)
6244 * - usability check (#GP(0)/#SS(0))
6245 * - limit check (#GP(0)/#SS(0))
6246 */
6247 if (wr)
6248 /* #GP(0) if the destination operand is located in a
6249 * read-only data segment or any code segment.
6250 */
6251 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6252 else
6253 /* #GP(0) if the source operand is located in an
6254 * execute-only code segment
6255 */
6256 exn = ((s.type & 0xa) == 8);
ff30ef40
QC
6257 if (exn) {
6258 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6259 return 1;
6260 }
f9eb4af6
EK
6261 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6262 */
6263 exn = (s.unusable != 0);
6264 /* Protected mode: #GP(0)/#SS(0) if the memory
6265 * operand is outside the segment limit.
6266 */
6267 exn = exn || (off + sizeof(u64) > s.limit);
6268 }
6269 if (exn) {
6270 kvm_queue_exception_e(vcpu,
6271 seg_reg == VCPU_SREG_SS ?
6272 SS_VECTOR : GP_VECTOR,
6273 0);
6274 return 1;
6275 }
6276
19677e32
BD
6277 return 0;
6278}
6279
cbf71279 6280static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
3573e22c
BD
6281{
6282 gva_t gva;
3573e22c 6283 struct x86_exception e;
3573e22c
BD
6284
6285 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
f9eb4af6 6286 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
3573e22c
BD
6287 return 1;
6288
ce14e868 6289 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
3573e22c
BD
6290 kvm_inject_page_fault(vcpu, &e);
6291 return 1;
6292 }
6293
3573e22c
BD
6294 return 0;
6295}
6296
abfc52c6
LA
6297/*
6298 * Allocate a shadow VMCS and associate it with the currently loaded
6299 * VMCS, unless such a shadow VMCS already exists. The newly allocated
6300 * VMCS is also VMCLEARed, so that it is ready for use.
6301 */
6302static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
6303{
6304 struct vcpu_vmx *vmx = to_vmx(vcpu);
6305 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
6306
6307 /*
6308 * We should allocate a shadow vmcs for vmcs01 only when L1
6309 * executes VMXON and free it when L1 executes VMXOFF.
6310 * As it is invalid to execute VMXON twice, we shouldn't reach
6311 * here when vmcs01 already have an allocated shadow vmcs.
6312 */
6313 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
6314
6315 if (!loaded_vmcs->shadow_vmcs) {
6316 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
6317 if (loaded_vmcs->shadow_vmcs)
6318 vmcs_clear(loaded_vmcs->shadow_vmcs);
6319 }
6320 return loaded_vmcs->shadow_vmcs;
6321}
6322
e29acc55
JM
6323static int enter_vmx_operation(struct kvm_vcpu *vcpu)
6324{
6325 struct vcpu_vmx *vmx = to_vmx(vcpu);
f21f165e 6326 int r;
e29acc55 6327
f21f165e
PB
6328 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
6329 if (r < 0)
de3a0021 6330 goto out_vmcs02;
e29acc55
JM
6331
6332 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
6333 if (!vmx->nested.cached_vmcs12)
6334 goto out_cached_vmcs12;
6335
61ada748
LA
6336 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
6337 if (!vmx->nested.cached_shadow_vmcs12)
6338 goto out_cached_shadow_vmcs12;
6339
abfc52c6
LA
6340 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
6341 goto out_shadow_vmcs;
e29acc55 6342
e29acc55
JM
6343 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
6344 HRTIMER_MODE_REL_PINNED);
6345 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
6346
63aff655
RK
6347 vmx->nested.vpid02 = allocate_vpid();
6348
9d6105b2 6349 vmx->nested.vmcs02_initialized = false;
e29acc55
JM
6350 vmx->nested.vmxon = true;
6351 return 0;
6352
6353out_shadow_vmcs:
61ada748
LA
6354 kfree(vmx->nested.cached_shadow_vmcs12);
6355
6356out_cached_shadow_vmcs12:
e29acc55
JM
6357 kfree(vmx->nested.cached_vmcs12);
6358
6359out_cached_vmcs12:
de3a0021 6360 free_loaded_vmcs(&vmx->nested.vmcs02);
e29acc55 6361
de3a0021 6362out_vmcs02:
e29acc55
JM
6363 return -ENOMEM;
6364}
6365
ec378aee
NHE
6366/*
6367 * Emulate the VMXON instruction.
6368 * Currently, we just remember that VMX is active, and do not save or even
6369 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6370 * do not currently need to store anything in that guest-allocated memory
6371 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6372 * argument is different from the VMXON pointer (which the spec says they do).
6373 */
6374static int handle_vmon(struct kvm_vcpu *vcpu)
6375{
e29acc55 6376 int ret;
cbf71279
RK
6377 gpa_t vmptr;
6378 struct page *page;
ec378aee 6379 struct vcpu_vmx *vmx = to_vmx(vcpu);
b3897a49
NHE
6380 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6381 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
ec378aee 6382
70f3aac9
JM
6383 /*
6384 * The Intel VMX Instruction Reference lists a bunch of bits that are
6385 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
6386 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
6387 * Otherwise, we should fail with #UD. But most faulting conditions
6388 * have already been checked by hardware, prior to the VM-exit for
6389 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
6390 * that bit set to 1 in non-root mode.
ec378aee 6391 */
70f3aac9 6392 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
ec378aee
NHE
6393 kvm_queue_exception(vcpu, UD_VECTOR);
6394 return 1;
6395 }
6396
727ba748
FW
6397 /* CPL=0 must be checked manually. */
6398 if (vmx_get_cpl(vcpu)) {
36090bf4 6399 kvm_inject_gp(vcpu, 0);
727ba748
FW
6400 return 1;
6401 }
6402
09abb5e3
SC
6403 if (vmx->nested.vmxon)
6404 return nested_vmx_failValid(vcpu,
6405 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
b3897a49 6406
3b84080b 6407 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
b3897a49
NHE
6408 != VMXON_NEEDED_FEATURES) {
6409 kvm_inject_gp(vcpu, 0);
6410 return 1;
6411 }
6412
cbf71279 6413 if (nested_vmx_get_vmptr(vcpu, &vmptr))
21e7fbe7 6414 return 1;
cbf71279
RK
6415
6416 /*
6417 * SDM 3: 24.11.5
6418 * The first 4 bytes of VMXON region contain the supported
6419 * VMCS revision identifier
6420 *
6421 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
6422 * which replaces physical address width with 32
6423 */
09abb5e3
SC
6424 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
6425 return nested_vmx_failInvalid(vcpu);
cbf71279 6426
5e2f30b7 6427 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
09abb5e3
SC
6428 if (is_error_page(page))
6429 return nested_vmx_failInvalid(vcpu);
6430
cbf71279
RK
6431 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
6432 kunmap(page);
53a70daf 6433 kvm_release_page_clean(page);
09abb5e3 6434 return nested_vmx_failInvalid(vcpu);
cbf71279
RK
6435 }
6436 kunmap(page);
53a70daf 6437 kvm_release_page_clean(page);
cbf71279
RK
6438
6439 vmx->nested.vmxon_ptr = vmptr;
e29acc55
JM
6440 ret = enter_vmx_operation(vcpu);
6441 if (ret)
6442 return ret;
ec378aee 6443
09abb5e3 6444 return nested_vmx_succeed(vcpu);
ec378aee
NHE
6445}
6446
6447/*
6448 * Intel's VMX Instruction Reference specifies a common set of prerequisites
6449 * for running VMX instructions (except VMXON, whose prerequisites are
6450 * slightly different). It also specifies what exception to inject otherwise.
70f3aac9
JM
6451 * Note that many of these exceptions have priority over VM exits, so they
6452 * don't have to be checked again here.
ec378aee
NHE
6453 */
6454static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
6455{
e49fcb8b 6456 if (!to_vmx(vcpu)->nested.vmxon) {
727ba748
FW
6457 kvm_queue_exception(vcpu, UD_VECTOR);
6458 return 0;
6459 }
6460
e49fcb8b
JM
6461 if (vmx_get_cpl(vcpu)) {
6462 kvm_inject_gp(vcpu, 0);
ec378aee
NHE
6463 return 0;
6464 }
e49fcb8b 6465
ec378aee
NHE
6466 return 1;
6467}
6468
8ca44e88
DM
6469static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
6470{
6471 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
6472 vmcs_write64(VMCS_LINK_POINTER, -1ull);
6473}
6474
b8bbab92
VK
6475static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
6476{
6477 struct vcpu_vmx *vmx = to_vmx(vcpu);
6478
6479 if (!vmx->nested.hv_evmcs)
6480 return;
6481
6482 kunmap(vmx->nested.hv_evmcs_page);
6483 kvm_release_page_dirty(vmx->nested.hv_evmcs_page);
6484 vmx->nested.hv_evmcs_vmptr = -1ull;
6485 vmx->nested.hv_evmcs_page = NULL;
6486 vmx->nested.hv_evmcs = NULL;
6487}
6488
14c07ad8 6489static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
e7953d7f 6490{
14c07ad8
VK
6491 struct vcpu_vmx *vmx = to_vmx(vcpu);
6492
9a2a05b9
PB
6493 if (vmx->nested.current_vmptr == -1ull)
6494 return;
6495
012f83cb 6496 if (enable_shadow_vmcs) {
9a2a05b9
PB
6497 /* copy to memory all shadowed fields in case
6498 they were modified */
6499 copy_shadow_to_vmcs12(vmx);
945679e3 6500 vmx->nested.need_vmcs12_sync = false;
8ca44e88 6501 vmx_disable_shadow_vmcs(vmx);
012f83cb 6502 }
705699a1 6503 vmx->nested.posted_intr_nv = -1;
4f2777bc
DM
6504
6505 /* Flush VMCS12 to guest memory */
14c07ad8 6506 kvm_vcpu_write_guest_page(vcpu,
9f744c59
PB
6507 vmx->nested.current_vmptr >> PAGE_SHIFT,
6508 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4f2777bc 6509
14c07ad8
VK
6510 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
6511
9a2a05b9 6512 vmx->nested.current_vmptr = -1ull;
e7953d7f
AG
6513}
6514
ec378aee
NHE
6515/*
6516 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
6517 * just stops using VMX.
6518 */
14c07ad8 6519static void free_nested(struct kvm_vcpu *vcpu)
ec378aee 6520{
14c07ad8
VK
6521 struct vcpu_vmx *vmx = to_vmx(vcpu);
6522
b7455825 6523 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
ec378aee 6524 return;
9a2a05b9 6525
ec378aee 6526 vmx->nested.vmxon = false;
b7455825 6527 vmx->nested.smm.vmxon = false;
5c614b35 6528 free_vpid(vmx->nested.vpid02);
8ca44e88
DM
6529 vmx->nested.posted_intr_nv = -1;
6530 vmx->nested.current_vmptr = -1ull;
355f4fb1 6531 if (enable_shadow_vmcs) {
8ca44e88 6532 vmx_disable_shadow_vmcs(vmx);
355f4fb1
JM
6533 vmcs_clear(vmx->vmcs01.shadow_vmcs);
6534 free_vmcs(vmx->vmcs01.shadow_vmcs);
6535 vmx->vmcs01.shadow_vmcs = NULL;
6536 }
4f2777bc 6537 kfree(vmx->nested.cached_vmcs12);
61ada748 6538 kfree(vmx->nested.cached_shadow_vmcs12);
de3a0021 6539 /* Unpin physical memory we referred to in the vmcs02 */
fe3ef05c 6540 if (vmx->nested.apic_access_page) {
53a70daf 6541 kvm_release_page_dirty(vmx->nested.apic_access_page);
48d89b92 6542 vmx->nested.apic_access_page = NULL;
fe3ef05c 6543 }
a7c0b07d 6544 if (vmx->nested.virtual_apic_page) {
53a70daf 6545 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
48d89b92 6546 vmx->nested.virtual_apic_page = NULL;
a7c0b07d 6547 }
705699a1
WV
6548 if (vmx->nested.pi_desc_page) {
6549 kunmap(vmx->nested.pi_desc_page);
53a70daf 6550 kvm_release_page_dirty(vmx->nested.pi_desc_page);
705699a1
WV
6551 vmx->nested.pi_desc_page = NULL;
6552 vmx->nested.pi_desc = NULL;
6553 }
ff2f6fe9 6554
14c07ad8
VK
6555 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
6556
b8bbab92
VK
6557 nested_release_evmcs(vcpu);
6558
de3a0021 6559 free_loaded_vmcs(&vmx->nested.vmcs02);
ec378aee
NHE
6560}
6561
6562/* Emulate the VMXOFF instruction */
6563static int handle_vmoff(struct kvm_vcpu *vcpu)
6564{
6565 if (!nested_vmx_check_permission(vcpu))
6566 return 1;
14c07ad8 6567 free_nested(vcpu);
09abb5e3 6568 return nested_vmx_succeed(vcpu);
ec378aee
NHE
6569}
6570
27d6c865
NHE
6571/* Emulate the VMCLEAR instruction */
6572static int handle_vmclear(struct kvm_vcpu *vcpu)
6573{
6574 struct vcpu_vmx *vmx = to_vmx(vcpu);
587d7e72 6575 u32 zero = 0;
27d6c865 6576 gpa_t vmptr;
27d6c865
NHE
6577
6578 if (!nested_vmx_check_permission(vcpu))
6579 return 1;
6580
cbf71279 6581 if (nested_vmx_get_vmptr(vcpu, &vmptr))
27d6c865 6582 return 1;
27d6c865 6583
09abb5e3
SC
6584 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
6585 return nested_vmx_failValid(vcpu,
6586 VMXERR_VMCLEAR_INVALID_ADDRESS);
cbf71279 6587
09abb5e3
SC
6588 if (vmptr == vmx->nested.vmxon_ptr)
6589 return nested_vmx_failValid(vcpu,
6590 VMXERR_VMCLEAR_VMXON_POINTER);
cbf71279 6591
b8bbab92
VK
6592 if (vmx->nested.hv_evmcs_page) {
6593 if (vmptr == vmx->nested.hv_evmcs_vmptr)
6594 nested_release_evmcs(vcpu);
6595 } else {
6596 if (vmptr == vmx->nested.current_vmptr)
6597 nested_release_vmcs12(vcpu);
27d6c865 6598
b8bbab92
VK
6599 kvm_vcpu_write_guest(vcpu,
6600 vmptr + offsetof(struct vmcs12,
6601 launch_state),
6602 &zero, sizeof(zero));
6603 }
27d6c865 6604
09abb5e3 6605 return nested_vmx_succeed(vcpu);
27d6c865
NHE
6606}
6607
cd232ad0
NHE
6608static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
6609
6610/* Emulate the VMLAUNCH instruction */
6611static int handle_vmlaunch(struct kvm_vcpu *vcpu)
6612{
6613 return nested_vmx_run(vcpu, true);
6614}
6615
6616/* Emulate the VMRESUME instruction */
6617static int handle_vmresume(struct kvm_vcpu *vcpu)
6618{
6619
6620 return nested_vmx_run(vcpu, false);
6621}
6622
945679e3
VK
6623static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
6624{
6625 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
6626 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
6627
6628 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
6629 vmcs12->tpr_threshold = evmcs->tpr_threshold;
6630 vmcs12->guest_rip = evmcs->guest_rip;
6631
6632 if (unlikely(!(evmcs->hv_clean_fields &
6633 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
6634 vmcs12->guest_rsp = evmcs->guest_rsp;
6635 vmcs12->guest_rflags = evmcs->guest_rflags;
6636 vmcs12->guest_interruptibility_info =
6637 evmcs->guest_interruptibility_info;
6638 }
6639
6640 if (unlikely(!(evmcs->hv_clean_fields &
6641 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
6642 vmcs12->cpu_based_vm_exec_control =
6643 evmcs->cpu_based_vm_exec_control;
6644 }
6645
6646 if (unlikely(!(evmcs->hv_clean_fields &
6647 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
6648 vmcs12->exception_bitmap = evmcs->exception_bitmap;
6649 }
6650
6651 if (unlikely(!(evmcs->hv_clean_fields &
6652 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
6653 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
6654 }
6655
6656 if (unlikely(!(evmcs->hv_clean_fields &
6657 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
6658 vmcs12->vm_entry_intr_info_field =
6659 evmcs->vm_entry_intr_info_field;
6660 vmcs12->vm_entry_exception_error_code =
6661 evmcs->vm_entry_exception_error_code;
6662 vmcs12->vm_entry_instruction_len =
6663 evmcs->vm_entry_instruction_len;
6664 }
6665
6666 if (unlikely(!(evmcs->hv_clean_fields &
6667 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
6668 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
6669 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
6670 vmcs12->host_cr0 = evmcs->host_cr0;
6671 vmcs12->host_cr3 = evmcs->host_cr3;
6672 vmcs12->host_cr4 = evmcs->host_cr4;
6673 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
6674 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
6675 vmcs12->host_rip = evmcs->host_rip;
6676 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
6677 vmcs12->host_es_selector = evmcs->host_es_selector;
6678 vmcs12->host_cs_selector = evmcs->host_cs_selector;
6679 vmcs12->host_ss_selector = evmcs->host_ss_selector;
6680 vmcs12->host_ds_selector = evmcs->host_ds_selector;
6681 vmcs12->host_fs_selector = evmcs->host_fs_selector;
6682 vmcs12->host_gs_selector = evmcs->host_gs_selector;
6683 vmcs12->host_tr_selector = evmcs->host_tr_selector;
6684 }
6685
6686 if (unlikely(!(evmcs->hv_clean_fields &
6687 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
6688 vmcs12->pin_based_vm_exec_control =
6689 evmcs->pin_based_vm_exec_control;
6690 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
6691 vmcs12->secondary_vm_exec_control =
6692 evmcs->secondary_vm_exec_control;
6693 }
6694
6695 if (unlikely(!(evmcs->hv_clean_fields &
6696 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
6697 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
6698 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
6699 }
6700
6701 if (unlikely(!(evmcs->hv_clean_fields &
6702 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
6703 vmcs12->msr_bitmap = evmcs->msr_bitmap;
6704 }
6705
6706 if (unlikely(!(evmcs->hv_clean_fields &
6707 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
6708 vmcs12->guest_es_base = evmcs->guest_es_base;
6709 vmcs12->guest_cs_base = evmcs->guest_cs_base;
6710 vmcs12->guest_ss_base = evmcs->guest_ss_base;
6711 vmcs12->guest_ds_base = evmcs->guest_ds_base;
6712 vmcs12->guest_fs_base = evmcs->guest_fs_base;
6713 vmcs12->guest_gs_base = evmcs->guest_gs_base;
6714 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
6715 vmcs12->guest_tr_base = evmcs->guest_tr_base;
6716 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
6717 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
6718 vmcs12->guest_es_limit = evmcs->guest_es_limit;
6719 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
6720 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
6721 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
6722 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
6723 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
6724 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
6725 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
6726 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
6727 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
6728 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
6729 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
6730 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
6731 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
6732 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
6733 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
6734 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
6735 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
6736 vmcs12->guest_es_selector = evmcs->guest_es_selector;
6737 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
6738 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
6739 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
6740 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
6741 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
6742 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
6743 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
6744 }
6745
6746 if (unlikely(!(evmcs->hv_clean_fields &
6747 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
6748 vmcs12->tsc_offset = evmcs->tsc_offset;
6749 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
6750 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
6751 }
6752
6753 if (unlikely(!(evmcs->hv_clean_fields &
6754 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
6755 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
6756 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
6757 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
6758 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
6759 vmcs12->guest_cr0 = evmcs->guest_cr0;
6760 vmcs12->guest_cr3 = evmcs->guest_cr3;
6761 vmcs12->guest_cr4 = evmcs->guest_cr4;
6762 vmcs12->guest_dr7 = evmcs->guest_dr7;
6763 }
6764
6765 if (unlikely(!(evmcs->hv_clean_fields &
6766 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
6767 vmcs12->host_fs_base = evmcs->host_fs_base;
6768 vmcs12->host_gs_base = evmcs->host_gs_base;
6769 vmcs12->host_tr_base = evmcs->host_tr_base;
6770 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
6771 vmcs12->host_idtr_base = evmcs->host_idtr_base;
6772 vmcs12->host_rsp = evmcs->host_rsp;
6773 }
6774
6775 if (unlikely(!(evmcs->hv_clean_fields &
6776 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
6777 vmcs12->ept_pointer = evmcs->ept_pointer;
6778 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
6779 }
6780
6781 if (unlikely(!(evmcs->hv_clean_fields &
6782 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
6783 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
6784 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
6785 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
6786 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
6787 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
6788 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
6789 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
6790 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
6791 vmcs12->guest_pending_dbg_exceptions =
6792 evmcs->guest_pending_dbg_exceptions;
6793 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
6794 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
6795 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
6796 vmcs12->guest_activity_state = evmcs->guest_activity_state;
6797 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
6798 }
6799
6800 /*
6801 * Not used?
6802 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
6803 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
6804 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
6805 * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0;
6806 * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1;
6807 * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2;
6808 * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3;
6809 * vmcs12->page_fault_error_code_mask =
6810 * evmcs->page_fault_error_code_mask;
6811 * vmcs12->page_fault_error_code_match =
6812 * evmcs->page_fault_error_code_match;
6813 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
6814 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
6815 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
6816 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
6817 */
6818
6819 /*
6820 * Read only fields:
6821 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
6822 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
6823 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
6824 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
6825 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
6826 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
6827 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
6828 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
6829 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
6830 * vmcs12->exit_qualification = evmcs->exit_qualification;
6831 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
6832 *
6833 * Not present in struct vmcs12:
6834 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
6835 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
6836 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
6837 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
6838 */
6839
6840 return 0;
6841}
6842
6843static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
6844{
6845 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
6846 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
6847
6848 /*
6849 * Should not be changed by KVM:
6850 *
6851 * evmcs->host_es_selector = vmcs12->host_es_selector;
6852 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
6853 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
6854 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
6855 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
6856 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
6857 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
6858 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
6859 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
6860 * evmcs->host_cr0 = vmcs12->host_cr0;
6861 * evmcs->host_cr3 = vmcs12->host_cr3;
6862 * evmcs->host_cr4 = vmcs12->host_cr4;
6863 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
6864 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
6865 * evmcs->host_rip = vmcs12->host_rip;
6866 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
6867 * evmcs->host_fs_base = vmcs12->host_fs_base;
6868 * evmcs->host_gs_base = vmcs12->host_gs_base;
6869 * evmcs->host_tr_base = vmcs12->host_tr_base;
6870 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
6871 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
6872 * evmcs->host_rsp = vmcs12->host_rsp;
6873 * sync_vmcs12() doesn't read these:
6874 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
6875 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
6876 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
6877 * evmcs->ept_pointer = vmcs12->ept_pointer;
6878 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
6879 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
6880 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
6881 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
6882 * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0;
6883 * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1;
6884 * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2;
6885 * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3;
6886 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
6887 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
6888 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
6889 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
6890 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
6891 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
6892 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
6893 * evmcs->page_fault_error_code_mask =
6894 * vmcs12->page_fault_error_code_mask;
6895 * evmcs->page_fault_error_code_match =
6896 * vmcs12->page_fault_error_code_match;
6897 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
6898 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
6899 * evmcs->tsc_offset = vmcs12->tsc_offset;
6900 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
6901 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
6902 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
6903 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
6904 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
6905 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
6906 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
6907 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
6908 *
6909 * Not present in struct vmcs12:
6910 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
6911 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
6912 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
6913 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
6914 */
6915
6916 evmcs->guest_es_selector = vmcs12->guest_es_selector;
6917 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
6918 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
6919 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
6920 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
6921 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
6922 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
6923 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
6924
6925 evmcs->guest_es_limit = vmcs12->guest_es_limit;
6926 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
6927 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
6928 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
6929 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
6930 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
6931 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
6932 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
6933 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
6934 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
6935
6936 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
6937 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
6938 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
6939 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
6940 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
6941 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
6942 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
6943 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
6944
6945 evmcs->guest_es_base = vmcs12->guest_es_base;
6946 evmcs->guest_cs_base = vmcs12->guest_cs_base;
6947 evmcs->guest_ss_base = vmcs12->guest_ss_base;
6948 evmcs->guest_ds_base = vmcs12->guest_ds_base;
6949 evmcs->guest_fs_base = vmcs12->guest_fs_base;
6950 evmcs->guest_gs_base = vmcs12->guest_gs_base;
6951 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
6952 evmcs->guest_tr_base = vmcs12->guest_tr_base;
6953 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
6954 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
6955
6956 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
6957 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
6958
6959 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
6960 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
6961 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
6962 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
6963
6964 evmcs->guest_pending_dbg_exceptions =
6965 vmcs12->guest_pending_dbg_exceptions;
6966 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
6967 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
6968
6969 evmcs->guest_activity_state = vmcs12->guest_activity_state;
6970 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
6971
6972 evmcs->guest_cr0 = vmcs12->guest_cr0;
6973 evmcs->guest_cr3 = vmcs12->guest_cr3;
6974 evmcs->guest_cr4 = vmcs12->guest_cr4;
6975 evmcs->guest_dr7 = vmcs12->guest_dr7;
6976
6977 evmcs->guest_physical_address = vmcs12->guest_physical_address;
6978
6979 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
6980 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
6981 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
6982 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
6983 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
6984 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
6985 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
6986 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
6987
6988 evmcs->exit_qualification = vmcs12->exit_qualification;
6989
6990 evmcs->guest_linear_address = vmcs12->guest_linear_address;
6991 evmcs->guest_rsp = vmcs12->guest_rsp;
6992 evmcs->guest_rflags = vmcs12->guest_rflags;
6993
6994 evmcs->guest_interruptibility_info =
6995 vmcs12->guest_interruptibility_info;
6996 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
6997 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
6998 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
6999 evmcs->vm_entry_exception_error_code =
7000 vmcs12->vm_entry_exception_error_code;
7001 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
7002
7003 evmcs->guest_rip = vmcs12->guest_rip;
7004
7005 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
7006
7007 return 0;
7008}
7009
f4160e45
JM
7010/*
7011 * Copy the writable VMCS shadow fields back to the VMCS12, in case
7012 * they have been modified by the L1 guest. Note that the "read-only"
7013 * VM-exit information fields are actually writable if the vCPU is
7014 * configured to support "VMWRITE to any supported field in the VMCS."
7015 */
16f5b903
AG
7016static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7017{
f4160e45
JM
7018 const u16 *fields[] = {
7019 shadow_read_write_fields,
7020 shadow_read_only_fields
7021 };
7022 const int max_fields[] = {
7023 max_shadow_read_write_fields,
7024 max_shadow_read_only_fields
7025 };
7026 int i, q;
16f5b903
AG
7027 unsigned long field;
7028 u64 field_value;
355f4fb1 7029 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
16f5b903 7030
282da870
JK
7031 preempt_disable();
7032
16f5b903
AG
7033 vmcs_load(shadow_vmcs);
7034
f4160e45
JM
7035 for (q = 0; q < ARRAY_SIZE(fields); q++) {
7036 for (i = 0; i < max_fields[q]; i++) {
7037 field = fields[q][i];
7038 field_value = __vmcs_readl(field);
e2536742 7039 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
f4160e45
JM
7040 }
7041 /*
7042 * Skip the VM-exit information fields if they are read-only.
7043 */
7044 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
7045 break;
16f5b903
AG
7046 }
7047
7048 vmcs_clear(shadow_vmcs);
7049 vmcs_load(vmx->loaded_vmcs->vmcs);
282da870
JK
7050
7051 preempt_enable();
16f5b903
AG
7052}
7053
c3114420
AG
7054static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7055{
44900ba6 7056 const u16 *fields[] = {
c2bae893
MK
7057 shadow_read_write_fields,
7058 shadow_read_only_fields
c3114420 7059 };
c2bae893 7060 const int max_fields[] = {
c3114420
AG
7061 max_shadow_read_write_fields,
7062 max_shadow_read_only_fields
7063 };
7064 int i, q;
7065 unsigned long field;
7066 u64 field_value = 0;
355f4fb1 7067 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
c3114420
AG
7068
7069 vmcs_load(shadow_vmcs);
7070
c2bae893 7071 for (q = 0; q < ARRAY_SIZE(fields); q++) {
c3114420
AG
7072 for (i = 0; i < max_fields[q]; i++) {
7073 field = fields[q][i];
e2536742 7074 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
44900ba6 7075 __vmcs_writel(field, field_value);
c3114420
AG
7076 }
7077 }
7078
7079 vmcs_clear(shadow_vmcs);
7080 vmcs_load(vmx->loaded_vmcs->vmcs);
7081}
7082
49f705c5
NHE
7083static int handle_vmread(struct kvm_vcpu *vcpu)
7084{
7085 unsigned long field;
7086 u64 field_value;
7087 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7088 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7089 gva_t gva = 0;
6d894f49 7090 struct vmcs12 *vmcs12;
49f705c5 7091
eb277562 7092 if (!nested_vmx_check_permission(vcpu))
49f705c5
NHE
7093 return 1;
7094
09abb5e3
SC
7095 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
7096 return nested_vmx_failInvalid(vcpu);
49f705c5 7097
6d894f49
LA
7098 if (!is_guest_mode(vcpu))
7099 vmcs12 = get_vmcs12(vcpu);
7100 else {
7101 /*
7102 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
7103 * to shadowed-field sets the ALU flags for VMfailInvalid.
7104 */
09abb5e3
SC
7105 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
7106 return nested_vmx_failInvalid(vcpu);
6d894f49
LA
7107 vmcs12 = get_shadow_vmcs12(vcpu);
7108 }
7109
49f705c5 7110 /* Decode instruction info and find the field to read */
27e6fb5d 7111 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
49f705c5 7112 /* Read the field, zero-extended to a u64 field_value */
09abb5e3
SC
7113 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
7114 return nested_vmx_failValid(vcpu,
7115 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7116
49f705c5
NHE
7117 /*
7118 * Now copy part of this value to register or memory, as requested.
7119 * Note that the number of bits actually copied is 32 or 64 depending
7120 * on the guest's mode (32 or 64 bit), not on the given field's length.
7121 */
7122 if (vmx_instruction_info & (1u << 10)) {
27e6fb5d 7123 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
49f705c5
NHE
7124 field_value);
7125 } else {
7126 if (get_vmx_mem_address(vcpu, exit_qualification,
f9eb4af6 7127 vmx_instruction_info, true, &gva))
49f705c5 7128 return 1;
727ba748 7129 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
ce14e868
PB
7130 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7131 (is_long_mode(vcpu) ? 8 : 4), NULL);
49f705c5
NHE
7132 }
7133
09abb5e3 7134 return nested_vmx_succeed(vcpu);
49f705c5
NHE
7135}
7136
7137
7138static int handle_vmwrite(struct kvm_vcpu *vcpu)
7139{
7140 unsigned long field;
7141 gva_t gva;
74a497fa 7142 struct vcpu_vmx *vmx = to_vmx(vcpu);
49f705c5
NHE
7143 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7144 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
74a497fa 7145
49f705c5
NHE
7146 /* The value to write might be 32 or 64 bits, depending on L1's long
7147 * mode, and eventually we need to write that into a field of several
7148 * possible lengths. The code below first zero-extends the value to 64
6a6256f9 7149 * bit (field_value), and then copies only the appropriate number of
49f705c5
NHE
7150 * bits into the vmcs12 field.
7151 */
7152 u64 field_value = 0;
7153 struct x86_exception e;
6d894f49 7154 struct vmcs12 *vmcs12;
49f705c5 7155
eb277562 7156 if (!nested_vmx_check_permission(vcpu))
49f705c5
NHE
7157 return 1;
7158
09abb5e3
SC
7159 if (vmx->nested.current_vmptr == -1ull)
7160 return nested_vmx_failInvalid(vcpu);
eb277562 7161
49f705c5 7162 if (vmx_instruction_info & (1u << 10))
27e6fb5d 7163 field_value = kvm_register_readl(vcpu,
49f705c5
NHE
7164 (((vmx_instruction_info) >> 3) & 0xf));
7165 else {
7166 if (get_vmx_mem_address(vcpu, exit_qualification,
f9eb4af6 7167 vmx_instruction_info, false, &gva))
49f705c5 7168 return 1;
ce14e868
PB
7169 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7170 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
49f705c5
NHE
7171 kvm_inject_page_fault(vcpu, &e);
7172 return 1;
7173 }
7174 }
7175
7176
27e6fb5d 7177 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
f4160e45
JM
7178 /*
7179 * If the vCPU supports "VMWRITE to any supported field in the
7180 * VMCS," then the "read-only" fields are actually read/write.
7181 */
7182 if (vmcs_field_readonly(field) &&
09abb5e3
SC
7183 !nested_cpu_has_vmwrite_any_field(vcpu))
7184 return nested_vmx_failValid(vcpu,
49f705c5 7185 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
49f705c5 7186
6d894f49
LA
7187 if (!is_guest_mode(vcpu))
7188 vmcs12 = get_vmcs12(vcpu);
7189 else {
7190 /*
7191 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
7192 * to shadowed-field sets the ALU flags for VMfailInvalid.
7193 */
09abb5e3
SC
7194 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
7195 return nested_vmx_failInvalid(vcpu);
6d894f49 7196 vmcs12 = get_shadow_vmcs12(vcpu);
6d894f49
LA
7197 }
7198
09abb5e3
SC
7199 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
7200 return nested_vmx_failValid(vcpu,
7201 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
49f705c5 7202
6d894f49
LA
7203 /*
7204 * Do not track vmcs12 dirty-state if in guest-mode
7205 * as we actually dirty shadow vmcs12 instead of vmcs12.
7206 */
7207 if (!is_guest_mode(vcpu)) {
7208 switch (field) {
74a497fa 7209#define SHADOW_FIELD_RW(x) case x:
e0123119 7210#include "vmcs_shadow_fields.h"
6d894f49
LA
7211 /*
7212 * The fields that can be updated by L1 without a vmexit are
7213 * always updated in the vmcs02, the others go down the slow
7214 * path of prepare_vmcs02.
7215 */
7216 break;
7217 default:
7218 vmx->nested.dirty_vmcs12 = true;
7219 break;
7220 }
74a497fa
PB
7221 }
7222
09abb5e3 7223 return nested_vmx_succeed(vcpu);
49f705c5
NHE
7224}
7225
a8bc284e
JM
7226static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7227{
7228 vmx->nested.current_vmptr = vmptr;
7229 if (enable_shadow_vmcs) {
7230 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7231 SECONDARY_EXEC_SHADOW_VMCS);
7232 vmcs_write64(VMCS_LINK_POINTER,
7233 __pa(vmx->vmcs01.shadow_vmcs));
945679e3 7234 vmx->nested.need_vmcs12_sync = true;
a8bc284e 7235 }
74a497fa 7236 vmx->nested.dirty_vmcs12 = true;
a8bc284e
JM
7237}
7238
63846663
NHE
7239/* Emulate the VMPTRLD instruction */
7240static int handle_vmptrld(struct kvm_vcpu *vcpu)
7241{
7242 struct vcpu_vmx *vmx = to_vmx(vcpu);
63846663 7243 gpa_t vmptr;
63846663
NHE
7244
7245 if (!nested_vmx_check_permission(vcpu))
7246 return 1;
7247
cbf71279 7248 if (nested_vmx_get_vmptr(vcpu, &vmptr))
63846663 7249 return 1;
63846663 7250
09abb5e3
SC
7251 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
7252 return nested_vmx_failValid(vcpu,
7253 VMXERR_VMPTRLD_INVALID_ADDRESS);
cbf71279 7254
09abb5e3
SC
7255 if (vmptr == vmx->nested.vmxon_ptr)
7256 return nested_vmx_failValid(vcpu,
7257 VMXERR_VMPTRLD_VMXON_POINTER);
cbf71279 7258
b8bbab92
VK
7259 /* Forbid normal VMPTRLD if Enlightened version was used */
7260 if (vmx->nested.hv_evmcs)
7261 return 1;
cbf71279 7262
63846663
NHE
7263 if (vmx->nested.current_vmptr != vmptr) {
7264 struct vmcs12 *new_vmcs12;
7265 struct page *page;
5e2f30b7 7266 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
fca91f6d
JM
7267 if (is_error_page(page)) {
7268 /*
7269 * Reads from an unbacked page return all 1s,
7270 * which means that the 32 bits located at the
7271 * given physical address won't match the required
7272 * VMCS12_REVISION identifier.
7273 */
7274 nested_vmx_failValid(vcpu,
7275 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7276 return kvm_skip_emulated_instruction(vcpu);
7277 }
63846663 7278 new_vmcs12 = kmap(page);
392b2f25 7279 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
fa97d7db
LA
7280 (new_vmcs12->hdr.shadow_vmcs &&
7281 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
63846663 7282 kunmap(page);
53a70daf 7283 kvm_release_page_clean(page);
09abb5e3 7284 return nested_vmx_failValid(vcpu,
63846663 7285 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
63846663 7286 }
63846663 7287
14c07ad8
VK
7288 nested_release_vmcs12(vcpu);
7289
4f2777bc
DM
7290 /*
7291 * Load VMCS12 from guest memory since it is not already
7292 * cached.
7293 */
9f744c59
PB
7294 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
7295 kunmap(page);
53a70daf 7296 kvm_release_page_clean(page);
9f744c59 7297
a8bc284e 7298 set_current_vmptr(vmx, vmptr);
63846663
NHE
7299 }
7300
09abb5e3 7301 return nested_vmx_succeed(vcpu);
63846663
NHE
7302}
7303
b8bbab92
VK
7304/*
7305 * This is an equivalent of the nested hypervisor executing the vmptrld
7306 * instruction.
7307 */
8cab6507
VK
7308static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu,
7309 bool from_launch)
b8bbab92
VK
7310{
7311 struct vcpu_vmx *vmx = to_vmx(vcpu);
7312 struct hv_vp_assist_page assist_page;
7313
7314 if (likely(!vmx->nested.enlightened_vmcs_enabled))
7315 return 1;
7316
7317 if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
7318 return 1;
7319
7320 if (unlikely(!assist_page.enlighten_vmentry))
7321 return 1;
7322
7323 if (unlikely(assist_page.current_nested_vmcs !=
7324 vmx->nested.hv_evmcs_vmptr)) {
7325
7326 if (!vmx->nested.hv_evmcs)
7327 vmx->nested.current_vmptr = -1ull;
7328
7329 nested_release_evmcs(vcpu);
7330
7331 vmx->nested.hv_evmcs_page = kvm_vcpu_gpa_to_page(
7332 vcpu, assist_page.current_nested_vmcs);
7333
7334 if (unlikely(is_error_page(vmx->nested.hv_evmcs_page)))
7335 return 0;
7336
7337 vmx->nested.hv_evmcs = kmap(vmx->nested.hv_evmcs_page);
7338
72aeb60c
LA
7339 /*
7340 * Currently, KVM only supports eVMCS version 1
7341 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
7342 * value to first u32 field of eVMCS which should specify eVMCS
7343 * VersionNumber.
7344 *
7345 * Guest should be aware of supported eVMCS versions by host by
7346 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
7347 * expected to set this CPUID leaf according to the value
7348 * returned in vmcs_version from nested_enable_evmcs().
7349 *
7350 * However, it turns out that Microsoft Hyper-V fails to comply
7351 * to their own invented interface: When Hyper-V use eVMCS, it
7352 * just sets first u32 field of eVMCS to revision_id specified
7353 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
7354 * which is one of the supported versions specified in
7355 * CPUID.0x4000000A.EAX[0:15].
7356 *
7357 * To overcome Hyper-V bug, we accept here either a supported
7358 * eVMCS version or VMCS12 revision_id as valid values for first
7359 * u32 field of eVMCS.
7360 */
7361 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
7362 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
b8bbab92
VK
7363 nested_release_evmcs(vcpu);
7364 return 0;
7365 }
7366
7367 vmx->nested.dirty_vmcs12 = true;
7368 /*
7369 * As we keep L2 state for one guest only 'hv_clean_fields' mask
7370 * can't be used when we switch between them. Reset it here for
7371 * simplicity.
7372 */
7373 vmx->nested.hv_evmcs->hv_clean_fields &=
7374 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
7375 vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs;
7376
7377 /*
7378 * Unlike normal vmcs12, enlightened vmcs12 is not fully
7379 * reloaded from guest's memory (read only fields, fields not
7380 * present in struct hv_enlightened_vmcs, ...). Make sure there
7381 * are no leftovers.
7382 */
52ad7eb3
LA
7383 if (from_launch) {
7384 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7385 memset(vmcs12, 0, sizeof(*vmcs12));
7386 vmcs12->hdr.revision_id = VMCS12_REVISION;
7387 }
b8bbab92
VK
7388
7389 }
7390 return 1;
63846663
NHE
7391}
7392
6a4d7550
NHE
7393/* Emulate the VMPTRST instruction */
7394static int handle_vmptrst(struct kvm_vcpu *vcpu)
7395{
0a06d425
SC
7396 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
7397 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7398 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
6a4d7550 7399 struct x86_exception e;
0a06d425 7400 gva_t gva;
6a4d7550
NHE
7401
7402 if (!nested_vmx_check_permission(vcpu))
7403 return 1;
7404
b8bbab92
VK
7405 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
7406 return 1;
7407
0a06d425 7408 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
6a4d7550 7409 return 1;
727ba748 7410 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
0a06d425
SC
7411 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
7412 sizeof(gpa_t), &e)) {
6a4d7550
NHE
7413 kvm_inject_page_fault(vcpu, &e);
7414 return 1;
7415 }
09abb5e3 7416 return nested_vmx_succeed(vcpu);
6a4d7550
NHE
7417}
7418
bfd0a56b
NHE
7419/* Emulate the INVEPT instruction */
7420static int handle_invept(struct kvm_vcpu *vcpu)
7421{
b9c237bb 7422 struct vcpu_vmx *vmx = to_vmx(vcpu);
bfd0a56b
NHE
7423 u32 vmx_instruction_info, types;
7424 unsigned long type;
7425 gva_t gva;
7426 struct x86_exception e;
7427 struct {
7428 u64 eptp, gpa;
7429 } operand;
bfd0a56b 7430
6677f3da 7431 if (!(vmx->nested.msrs.secondary_ctls_high &
b9c237bb 7432 SECONDARY_EXEC_ENABLE_EPT) ||
6677f3da 7433 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
bfd0a56b
NHE
7434 kvm_queue_exception(vcpu, UD_VECTOR);
7435 return 1;
7436 }
7437
7438 if (!nested_vmx_check_permission(vcpu))
7439 return 1;
7440
bfd0a56b 7441 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
27e6fb5d 7442 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
bfd0a56b 7443
6677f3da 7444 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
bfd0a56b 7445
09abb5e3
SC
7446 if (type >= 32 || !(types & (1 << type)))
7447 return nested_vmx_failValid(vcpu,
bfd0a56b 7448 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
bfd0a56b
NHE
7449
7450 /* According to the Intel VMX instruction reference, the memory
7451 * operand is read even if it isn't needed (e.g., for type==global)
7452 */
7453 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
f9eb4af6 7454 vmx_instruction_info, false, &gva))
bfd0a56b 7455 return 1;
ce14e868 7456 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
bfd0a56b
NHE
7457 kvm_inject_page_fault(vcpu, &e);
7458 return 1;
7459 }
7460
7461 switch (type) {
bfd0a56b 7462 case VMX_EPT_EXTENT_GLOBAL:
45e11817
BD
7463 /*
7464 * TODO: track mappings and invalidate
7465 * single context requests appropriately
7466 */
7467 case VMX_EPT_EXTENT_CONTEXT:
bfd0a56b 7468 kvm_mmu_sync_roots(vcpu);
77c3913b 7469 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
bfd0a56b
NHE
7470 break;
7471 default:
7472 BUG_ON(1);
7473 break;
7474 }
7475
09abb5e3 7476 return nested_vmx_succeed(vcpu);
bfd0a56b
NHE
7477}
7478
3d5bdae8
LA
7479static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
7480{
7481 struct vcpu_vmx *vmx = to_vmx(vcpu);
7482
7483 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
bfd0a56b
NHE
7484}
7485
a642fc30
PM
7486static int handle_invvpid(struct kvm_vcpu *vcpu)
7487{
99b83ac8
WL
7488 struct vcpu_vmx *vmx = to_vmx(vcpu);
7489 u32 vmx_instruction_info;
7490 unsigned long type, types;
7491 gva_t gva;
7492 struct x86_exception e;
40352605
JM
7493 struct {
7494 u64 vpid;
7495 u64 gla;
7496 } operand;
3d5bdae8 7497 u16 vpid02;
99b83ac8 7498
6677f3da 7499 if (!(vmx->nested.msrs.secondary_ctls_high &
99b83ac8 7500 SECONDARY_EXEC_ENABLE_VPID) ||
6677f3da 7501 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
99b83ac8
WL
7502 kvm_queue_exception(vcpu, UD_VECTOR);
7503 return 1;
7504 }
7505
7506 if (!nested_vmx_check_permission(vcpu))
7507 return 1;
7508
7509 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7510 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7511
6677f3da 7512 types = (vmx->nested.msrs.vpid_caps &
bcdde302 7513 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
99b83ac8 7514
09abb5e3
SC
7515 if (type >= 32 || !(types & (1 << type)))
7516 return nested_vmx_failValid(vcpu,
99b83ac8 7517 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
99b83ac8
WL
7518
7519 /* according to the intel vmx instruction reference, the memory
7520 * operand is read even if it isn't needed (e.g., for type==global)
7521 */
7522 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7523 vmx_instruction_info, false, &gva))
7524 return 1;
ce14e868 7525 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
99b83ac8
WL
7526 kvm_inject_page_fault(vcpu, &e);
7527 return 1;
7528 }
09abb5e3
SC
7529 if (operand.vpid >> 16)
7530 return nested_vmx_failValid(vcpu,
40352605 7531 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
99b83ac8 7532
3d5bdae8 7533 vpid02 = nested_get_vpid02(vcpu);
99b83ac8 7534 switch (type) {
bcdde302 7535 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
cd9a491f 7536 if (!operand.vpid ||
09abb5e3
SC
7537 is_noncanonical_address(operand.gla, vcpu))
7538 return nested_vmx_failValid(vcpu,
40352605 7539 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
3d5bdae8 7540 if (cpu_has_vmx_invvpid_individual_addr()) {
cd9a491f 7541 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
3d5bdae8 7542 vpid02, operand.gla);
cd9a491f 7543 } else
327c0721 7544 __vmx_flush_tlb(vcpu, vpid02, false);
cd9a491f 7545 break;
ef697a71 7546 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
bcdde302 7547 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
09abb5e3
SC
7548 if (!operand.vpid)
7549 return nested_vmx_failValid(vcpu,
bcdde302 7550 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
327c0721 7551 __vmx_flush_tlb(vcpu, vpid02, false);
bcdde302 7552 break;
99b83ac8 7553 case VMX_VPID_EXTENT_ALL_CONTEXT:
327c0721 7554 __vmx_flush_tlb(vcpu, vpid02, false);
99b83ac8
WL
7555 break;
7556 default:
bcdde302 7557 WARN_ON_ONCE(1);
6affcbed 7558 return kvm_skip_emulated_instruction(vcpu);
99b83ac8
WL
7559 }
7560
09abb5e3 7561 return nested_vmx_succeed(vcpu);
a642fc30
PM
7562}
7563
eb4b248e
JS
7564static int handle_invpcid(struct kvm_vcpu *vcpu)
7565{
7566 u32 vmx_instruction_info;
7567 unsigned long type;
7568 bool pcid_enabled;
7569 gva_t gva;
7570 struct x86_exception e;
b94742c9
JS
7571 unsigned i;
7572 unsigned long roots_to_free = 0;
eb4b248e
JS
7573 struct {
7574 u64 pcid;
7575 u64 gla;
7576 } operand;
7577
7578 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
7579 kvm_queue_exception(vcpu, UD_VECTOR);
7580 return 1;
7581 }
7582
7583 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7584 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7585
7586 if (type > 3) {
7587 kvm_inject_gp(vcpu, 0);
7588 return 1;
7589 }
7590
7591 /* According to the Intel instruction reference, the memory operand
7592 * is read even if it isn't needed (e.g., for type==all)
7593 */
7594 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7595 vmx_instruction_info, false, &gva))
7596 return 1;
7597
7598 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
7599 kvm_inject_page_fault(vcpu, &e);
7600 return 1;
7601 }
7602
7603 if (operand.pcid >> 12 != 0) {
7604 kvm_inject_gp(vcpu, 0);
7605 return 1;
7606 }
7607
7608 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
7609
7610 switch (type) {
7611 case INVPCID_TYPE_INDIV_ADDR:
7612 if ((!pcid_enabled && (operand.pcid != 0)) ||
7613 is_noncanonical_address(operand.gla, vcpu)) {
7614 kvm_inject_gp(vcpu, 0);
7615 return 1;
7616 }
7617 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
7618 return kvm_skip_emulated_instruction(vcpu);
7619
7620 case INVPCID_TYPE_SINGLE_CTXT:
7621 if (!pcid_enabled && (operand.pcid != 0)) {
7622 kvm_inject_gp(vcpu, 0);
7623 return 1;
7624 }
7625
7626 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
7627 kvm_mmu_sync_roots(vcpu);
7628 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
7629 }
7630
b94742c9 7631 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
44dd3ffa 7632 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
b94742c9
JS
7633 == operand.pcid)
7634 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
ade61e28 7635
6a82cd1c 7636 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
eb4b248e 7637 /*
b94742c9 7638 * If neither the current cr3 nor any of the prev_roots use the
ade61e28
JS
7639 * given PCID, then nothing needs to be done here because a
7640 * resync will happen anyway before switching to any other CR3.
eb4b248e
JS
7641 */
7642
7643 return kvm_skip_emulated_instruction(vcpu);
7644
7645 case INVPCID_TYPE_ALL_NON_GLOBAL:
7646 /*
7647 * Currently, KVM doesn't mark global entries in the shadow
7648 * page tables, so a non-global flush just degenerates to a
7649 * global flush. If needed, we could optimize this later by
7650 * keeping track of global entries in shadow page tables.
7651 */
7652
7653 /* fall-through */
7654 case INVPCID_TYPE_ALL_INCL_GLOBAL:
7655 kvm_mmu_unload(vcpu);
7656 return kvm_skip_emulated_instruction(vcpu);
7657
7658 default:
7659 BUG(); /* We have already checked above that type <= 3 */
7660 }
7661}
7662
843e4330
KH
7663static int handle_pml_full(struct kvm_vcpu *vcpu)
7664{
7665 unsigned long exit_qualification;
7666
7667 trace_kvm_pml_full(vcpu->vcpu_id);
7668
7669 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7670
7671 /*
7672 * PML buffer FULL happened while executing iret from NMI,
7673 * "blocked by NMI" bit has to be set before next VM entry.
7674 */
7675 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
d02fcf50 7676 enable_vnmi &&
843e4330
KH
7677 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7678 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7679 GUEST_INTR_STATE_NMI);
7680
7681 /*
7682 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7683 * here.., and there's no userspace involvement needed for PML.
7684 */
7685 return 1;
7686}
7687
64672c95
YJ
7688static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7689{
d264ee0c
SC
7690 if (!to_vmx(vcpu)->req_immediate_exit)
7691 kvm_lapic_expired_hv_timer(vcpu);
64672c95
YJ
7692 return 1;
7693}
7694
41ab9372
BD
7695static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
7696{
7697 struct vcpu_vmx *vmx = to_vmx(vcpu);
41ab9372
BD
7698 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7699
7700 /* Check for memory type validity */
bb97a016
DH
7701 switch (address & VMX_EPTP_MT_MASK) {
7702 case VMX_EPTP_MT_UC:
6677f3da 7703 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
41ab9372
BD
7704 return false;
7705 break;
bb97a016 7706 case VMX_EPTP_MT_WB:
6677f3da 7707 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
41ab9372
BD
7708 return false;
7709 break;
7710 default:
7711 return false;
7712 }
7713
bb97a016
DH
7714 /* only 4 levels page-walk length are valid */
7715 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
41ab9372
BD
7716 return false;
7717
7718 /* Reserved bits should not be set */
7719 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
7720 return false;
7721
7722 /* AD, if set, should be supported */
bb97a016 7723 if (address & VMX_EPTP_AD_ENABLE_BIT) {
6677f3da 7724 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
41ab9372
BD
7725 return false;
7726 }
7727
7728 return true;
7729}
7730
7731static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
7732 struct vmcs12 *vmcs12)
7733{
7734 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
7735 u64 address;
7736 bool accessed_dirty;
7737 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
7738
7739 if (!nested_cpu_has_eptp_switching(vmcs12) ||
7740 !nested_cpu_has_ept(vmcs12))
7741 return 1;
7742
7743 if (index >= VMFUNC_EPTP_ENTRIES)
7744 return 1;
7745
7746
7747 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
7748 &address, index * 8, 8))
7749 return 1;
7750
bb97a016 7751 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
41ab9372
BD
7752
7753 /*
7754 * If the (L2) guest does a vmfunc to the currently
7755 * active ept pointer, we don't have to do anything else
7756 */
7757 if (vmcs12->ept_pointer != address) {
7758 if (!valid_ept_address(vcpu, address))
7759 return 1;
7760
7761 kvm_mmu_unload(vcpu);
7762 mmu->ept_ad = accessed_dirty;
36d9594d 7763 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
41ab9372
BD
7764 vmcs12->ept_pointer = address;
7765 /*
7766 * TODO: Check what's the correct approach in case
7767 * mmu reload fails. Currently, we just let the next
7768 * reload potentially fail
7769 */
7770 kvm_mmu_reload(vcpu);
7771 }
7772
7773 return 0;
7774}
7775
2a499e49
BD
7776static int handle_vmfunc(struct kvm_vcpu *vcpu)
7777{
27c42a1b
BD
7778 struct vcpu_vmx *vmx = to_vmx(vcpu);
7779 struct vmcs12 *vmcs12;
7780 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
7781
7782 /*
7783 * VMFUNC is only supported for nested guests, but we always enable the
7784 * secondary control for simplicity; for non-nested mode, fake that we
7785 * didn't by injecting #UD.
7786 */
7787 if (!is_guest_mode(vcpu)) {
7788 kvm_queue_exception(vcpu, UD_VECTOR);
7789 return 1;
7790 }
7791
7792 vmcs12 = get_vmcs12(vcpu);
7793 if ((vmcs12->vm_function_control & (1 << function)) == 0)
7794 goto fail;
41ab9372
BD
7795
7796 switch (function) {
7797 case 0:
7798 if (nested_vmx_eptp_switching(vcpu, vmcs12))
7799 goto fail;
7800 break;
7801 default:
7802 goto fail;
7803 }
7804 return kvm_skip_emulated_instruction(vcpu);
27c42a1b
BD
7805
7806fail:
7807 nested_vmx_vmexit(vcpu, vmx->exit_reason,
7808 vmcs_read32(VM_EXIT_INTR_INFO),
7809 vmcs_readl(EXIT_QUALIFICATION));
2a499e49
BD
7810 return 1;
7811}
7812
0b665d30
SC
7813static int handle_encls(struct kvm_vcpu *vcpu)
7814{
7815 /*
7816 * SGX virtualization is not yet supported. There is no software
7817 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
7818 * to prevent the guest from executing ENCLS.
7819 */
7820 kvm_queue_exception(vcpu, UD_VECTOR);
7821 return 1;
7822}
7823
6aa8b732
AK
7824/*
7825 * The exit handlers return 1 if the exit was handled fully and guest execution
7826 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7827 * to be done to userspace and return 0.
7828 */
772e0318 7829static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
6aa8b732
AK
7830 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7831 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
988ad74f 7832 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
f08864b4 7833 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
6aa8b732 7834 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
6aa8b732
AK
7835 [EXIT_REASON_CR_ACCESS] = handle_cr,
7836 [EXIT_REASON_DR_ACCESS] = handle_dr,
7837 [EXIT_REASON_CPUID] = handle_cpuid,
7838 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7839 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7840 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7841 [EXIT_REASON_HLT] = handle_halt,
ec25d5e6 7842 [EXIT_REASON_INVD] = handle_invd,
a7052897 7843 [EXIT_REASON_INVLPG] = handle_invlpg,
fee84b07 7844 [EXIT_REASON_RDPMC] = handle_rdpmc,
c21415e8 7845 [EXIT_REASON_VMCALL] = handle_vmcall,
27d6c865 7846 [EXIT_REASON_VMCLEAR] = handle_vmclear,
cd232ad0 7847 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
63846663 7848 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
6a4d7550 7849 [EXIT_REASON_VMPTRST] = handle_vmptrst,
49f705c5 7850 [EXIT_REASON_VMREAD] = handle_vmread,
cd232ad0 7851 [EXIT_REASON_VMRESUME] = handle_vmresume,
49f705c5 7852 [EXIT_REASON_VMWRITE] = handle_vmwrite,
ec378aee
NHE
7853 [EXIT_REASON_VMOFF] = handle_vmoff,
7854 [EXIT_REASON_VMON] = handle_vmon,
f78e0e2e
SY
7855 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7856 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
83d4c286 7857 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
c7c9c56c 7858 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
e5edaa01 7859 [EXIT_REASON_WBINVD] = handle_wbinvd,
2acf923e 7860 [EXIT_REASON_XSETBV] = handle_xsetbv,
37817f29 7861 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
a0861c02 7862 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
0367f205
PB
7863 [EXIT_REASON_GDTR_IDTR] = handle_desc,
7864 [EXIT_REASON_LDTR_TR] = handle_desc,
68f89400
MT
7865 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7866 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
4b8d54f9 7867 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
87c00572 7868 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
5f3d45e7 7869 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
87c00572 7870 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
bfd0a56b 7871 [EXIT_REASON_INVEPT] = handle_invept,
a642fc30 7872 [EXIT_REASON_INVVPID] = handle_invvpid,
45ec368c 7873 [EXIT_REASON_RDRAND] = handle_invalid_op,
75f4fc8d 7874 [EXIT_REASON_RDSEED] = handle_invalid_op,
f53cd63c
WL
7875 [EXIT_REASON_XSAVES] = handle_xsaves,
7876 [EXIT_REASON_XRSTORS] = handle_xrstors,
843e4330 7877 [EXIT_REASON_PML_FULL] = handle_pml_full,
eb4b248e 7878 [EXIT_REASON_INVPCID] = handle_invpcid,
2a499e49 7879 [EXIT_REASON_VMFUNC] = handle_vmfunc,
64672c95 7880 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
0b665d30 7881 [EXIT_REASON_ENCLS] = handle_encls,
6aa8b732
AK
7882};
7883
7884static const int kvm_vmx_max_exit_handlers =
50a3485c 7885 ARRAY_SIZE(kvm_vmx_exit_handlers);
6aa8b732 7886
908a7bdd
JK
7887static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7888 struct vmcs12 *vmcs12)
7889{
7890 unsigned long exit_qualification;
7891 gpa_t bitmap, last_bitmap;
7892 unsigned int port;
7893 int size;
7894 u8 b;
7895
908a7bdd 7896 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
2f0a6397 7897 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
908a7bdd
JK
7898
7899 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7900
7901 port = exit_qualification >> 16;
7902 size = (exit_qualification & 7) + 1;
7903
7904 last_bitmap = (gpa_t)-1;
7905 b = -1;
7906
7907 while (size > 0) {
7908 if (port < 0x8000)
7909 bitmap = vmcs12->io_bitmap_a;
7910 else if (port < 0x10000)
7911 bitmap = vmcs12->io_bitmap_b;
7912 else
1d804d07 7913 return true;
908a7bdd
JK
7914 bitmap += (port & 0x7fff) / 8;
7915
7916 if (last_bitmap != bitmap)
54bf36aa 7917 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
1d804d07 7918 return true;
908a7bdd 7919 if (b & (1 << (port & 7)))
1d804d07 7920 return true;
908a7bdd
JK
7921
7922 port++;
7923 size--;
7924 last_bitmap = bitmap;
7925 }
7926
1d804d07 7927 return false;
908a7bdd
JK
7928}
7929
644d711a
NHE
7930/*
7931 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7932 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7933 * disinterest in the current event (read or write a specific MSR) by using an
7934 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7935 */
7936static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7937 struct vmcs12 *vmcs12, u32 exit_reason)
7938{
7939 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7940 gpa_t bitmap;
7941
cbd29cb6 7942 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
1d804d07 7943 return true;
644d711a
NHE
7944
7945 /*
7946 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7947 * for the four combinations of read/write and low/high MSR numbers.
7948 * First we need to figure out which of the four to use:
7949 */
7950 bitmap = vmcs12->msr_bitmap;
7951 if (exit_reason == EXIT_REASON_MSR_WRITE)
7952 bitmap += 2048;
7953 if (msr_index >= 0xc0000000) {
7954 msr_index -= 0xc0000000;
7955 bitmap += 1024;
7956 }
7957
7958 /* Then read the msr_index'th bit from this bitmap: */
7959 if (msr_index < 1024*8) {
7960 unsigned char b;
54bf36aa 7961 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
1d804d07 7962 return true;
644d711a
NHE
7963 return 1 & (b >> (msr_index & 7));
7964 } else
1d804d07 7965 return true; /* let L1 handle the wrong parameter */
644d711a
NHE
7966}
7967
7968/*
7969 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7970 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7971 * intercept (via guest_host_mask etc.) the current event.
7972 */
7973static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7974 struct vmcs12 *vmcs12)
7975{
7976 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7977 int cr = exit_qualification & 15;
e1d39b17
JS
7978 int reg;
7979 unsigned long val;
644d711a
NHE
7980
7981 switch ((exit_qualification >> 4) & 3) {
7982 case 0: /* mov to cr */
e1d39b17
JS
7983 reg = (exit_qualification >> 8) & 15;
7984 val = kvm_register_readl(vcpu, reg);
644d711a
NHE
7985 switch (cr) {
7986 case 0:
7987 if (vmcs12->cr0_guest_host_mask &
7988 (val ^ vmcs12->cr0_read_shadow))
1d804d07 7989 return true;
644d711a
NHE
7990 break;
7991 case 3:
7992 if ((vmcs12->cr3_target_count >= 1 &&
7993 vmcs12->cr3_target_value0 == val) ||
7994 (vmcs12->cr3_target_count >= 2 &&
7995 vmcs12->cr3_target_value1 == val) ||
7996 (vmcs12->cr3_target_count >= 3 &&
7997 vmcs12->cr3_target_value2 == val) ||
7998 (vmcs12->cr3_target_count >= 4 &&
7999 vmcs12->cr3_target_value3 == val))
1d804d07 8000 return false;
644d711a 8001 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
1d804d07 8002 return true;
644d711a
NHE
8003 break;
8004 case 4:
8005 if (vmcs12->cr4_guest_host_mask &
8006 (vmcs12->cr4_read_shadow ^ val))
1d804d07 8007 return true;
644d711a
NHE
8008 break;
8009 case 8:
8010 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
1d804d07 8011 return true;
644d711a
NHE
8012 break;
8013 }
8014 break;
8015 case 2: /* clts */
8016 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8017 (vmcs12->cr0_read_shadow & X86_CR0_TS))
1d804d07 8018 return true;
644d711a
NHE
8019 break;
8020 case 1: /* mov from cr */
8021 switch (cr) {
8022 case 3:
8023 if (vmcs12->cpu_based_vm_exec_control &
8024 CPU_BASED_CR3_STORE_EXITING)
1d804d07 8025 return true;
644d711a
NHE
8026 break;
8027 case 8:
8028 if (vmcs12->cpu_based_vm_exec_control &
8029 CPU_BASED_CR8_STORE_EXITING)
1d804d07 8030 return true;
644d711a
NHE
8031 break;
8032 }
8033 break;
8034 case 3: /* lmsw */
8035 /*
8036 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8037 * cr0. Other attempted changes are ignored, with no exit.
8038 */
e1d39b17 8039 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
644d711a
NHE
8040 if (vmcs12->cr0_guest_host_mask & 0xe &
8041 (val ^ vmcs12->cr0_read_shadow))
1d804d07 8042 return true;
644d711a
NHE
8043 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8044 !(vmcs12->cr0_read_shadow & 0x1) &&
8045 (val & 0x1))
1d804d07 8046 return true;
644d711a
NHE
8047 break;
8048 }
1d804d07 8049 return false;
644d711a
NHE
8050}
8051
a7cde481
LA
8052static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
8053 struct vmcs12 *vmcs12, gpa_t bitmap)
8054{
8055 u32 vmx_instruction_info;
8056 unsigned long field;
8057 u8 b;
8058
8059 if (!nested_cpu_has_shadow_vmcs(vmcs12))
8060 return true;
8061
8062 /* Decode instruction info and find the field to access */
8063 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8064 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
8065
8066 /* Out-of-range fields always cause a VM exit from L2 to L1 */
8067 if (field >> 15)
8068 return true;
8069
8070 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
8071 return true;
8072
8073 return 1 & (b >> (field & 7));
8074}
8075
644d711a
NHE
8076/*
8077 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8078 * should handle it ourselves in L0 (and then continue L2). Only call this
8079 * when in is_guest_mode (L2).
8080 */
7313c698 8081static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
644d711a 8082{
644d711a
NHE
8083 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8084 struct vcpu_vmx *vmx = to_vmx(vcpu);
8085 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8086
4f350c6d
JM
8087 if (vmx->nested.nested_run_pending)
8088 return false;
8089
8090 if (unlikely(vmx->fail)) {
8091 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8092 vmcs_read32(VM_INSTRUCTION_ERROR));
8093 return true;
8094 }
542060ea 8095
c9f04407
DM
8096 /*
8097 * The host physical addresses of some pages of guest memory
de3a0021
JM
8098 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8099 * Page). The CPU may write to these pages via their host
8100 * physical address while L2 is running, bypassing any
8101 * address-translation-based dirty tracking (e.g. EPT write
8102 * protection).
c9f04407
DM
8103 *
8104 * Mark them dirty on every exit from L2 to prevent them from
8105 * getting out of sync with dirty tracking.
8106 */
8107 nested_mark_vmcs12_pages_dirty(vcpu);
8108
4f350c6d
JM
8109 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8110 vmcs_readl(EXIT_QUALIFICATION),
8111 vmx->idt_vectoring_info,
8112 intr_info,
8113 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8114 KVM_ISA_VMX);
644d711a
NHE
8115
8116 switch (exit_reason) {
8117 case EXIT_REASON_EXCEPTION_NMI:
ef85b673 8118 if (is_nmi(intr_info))
1d804d07 8119 return false;
644d711a 8120 else if (is_page_fault(intr_info))
52a5c155 8121 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
6f05485d
JK
8122 else if (is_debug(intr_info) &&
8123 vcpu->guest_debug &
8124 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8125 return false;
8126 else if (is_breakpoint(intr_info) &&
8127 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8128 return false;
644d711a
NHE
8129 return vmcs12->exception_bitmap &
8130 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8131 case EXIT_REASON_EXTERNAL_INTERRUPT:
1d804d07 8132 return false;
644d711a 8133 case EXIT_REASON_TRIPLE_FAULT:
1d804d07 8134 return true;
644d711a 8135 case EXIT_REASON_PENDING_INTERRUPT:
3b656cf7 8136 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
644d711a 8137 case EXIT_REASON_NMI_WINDOW:
3b656cf7 8138 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
644d711a 8139 case EXIT_REASON_TASK_SWITCH:
1d804d07 8140 return true;
644d711a 8141 case EXIT_REASON_CPUID:
1d804d07 8142 return true;
644d711a
NHE
8143 case EXIT_REASON_HLT:
8144 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8145 case EXIT_REASON_INVD:
1d804d07 8146 return true;
644d711a
NHE
8147 case EXIT_REASON_INVLPG:
8148 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8149 case EXIT_REASON_RDPMC:
8150 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
a5f46457 8151 case EXIT_REASON_RDRAND:
736fdf72 8152 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
a5f46457 8153 case EXIT_REASON_RDSEED:
736fdf72 8154 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
b3a2a907 8155 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
644d711a 8156 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
a7cde481
LA
8157 case EXIT_REASON_VMREAD:
8158 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
8159 vmcs12->vmread_bitmap);
8160 case EXIT_REASON_VMWRITE:
8161 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
8162 vmcs12->vmwrite_bitmap);
644d711a
NHE
8163 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8164 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
a7cde481 8165 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
644d711a 8166 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
a642fc30 8167 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
644d711a
NHE
8168 /*
8169 * VMX instructions trap unconditionally. This allows L1 to
8170 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8171 */
1d804d07 8172 return true;
644d711a
NHE
8173 case EXIT_REASON_CR_ACCESS:
8174 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8175 case EXIT_REASON_DR_ACCESS:
8176 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8177 case EXIT_REASON_IO_INSTRUCTION:
908a7bdd 8178 return nested_vmx_exit_handled_io(vcpu, vmcs12);
1b07304c
PB
8179 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8180 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
644d711a
NHE
8181 case EXIT_REASON_MSR_READ:
8182 case EXIT_REASON_MSR_WRITE:
8183 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8184 case EXIT_REASON_INVALID_STATE:
1d804d07 8185 return true;
644d711a
NHE
8186 case EXIT_REASON_MWAIT_INSTRUCTION:
8187 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5f3d45e7
MD
8188 case EXIT_REASON_MONITOR_TRAP_FLAG:
8189 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
644d711a
NHE
8190 case EXIT_REASON_MONITOR_INSTRUCTION:
8191 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8192 case EXIT_REASON_PAUSE_INSTRUCTION:
8193 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8194 nested_cpu_has2(vmcs12,
8195 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8196 case EXIT_REASON_MCE_DURING_VMENTRY:
1d804d07 8197 return false;
644d711a 8198 case EXIT_REASON_TPR_BELOW_THRESHOLD:
a7c0b07d 8199 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
644d711a 8200 case EXIT_REASON_APIC_ACCESS:
82f0dd4b 8201 case EXIT_REASON_APIC_WRITE:
608406e2 8202 case EXIT_REASON_EOI_INDUCED:
ab5df31c
JM
8203 /*
8204 * The controls for "virtualize APIC accesses," "APIC-
8205 * register virtualization," and "virtual-interrupt
8206 * delivery" only come from vmcs12.
8207 */
1d804d07 8208 return true;
644d711a 8209 case EXIT_REASON_EPT_VIOLATION:
2b1be677
NHE
8210 /*
8211 * L0 always deals with the EPT violation. If nested EPT is
8212 * used, and the nested mmu code discovers that the address is
8213 * missing in the guest EPT table (EPT12), the EPT violation
8214 * will be injected with nested_ept_inject_page_fault()
8215 */
1d804d07 8216 return false;
644d711a 8217 case EXIT_REASON_EPT_MISCONFIG:
2b1be677
NHE
8218 /*
8219 * L2 never uses directly L1's EPT, but rather L0's own EPT
8220 * table (shadow on EPT) or a merged EPT table that L0 built
8221 * (EPT on EPT). So any problems with the structure of the
8222 * table is L0's fault.
8223 */
1d804d07 8224 return false;
90a2db6d
PB
8225 case EXIT_REASON_INVPCID:
8226 return
8227 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8228 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
644d711a
NHE
8229 case EXIT_REASON_WBINVD:
8230 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8231 case EXIT_REASON_XSETBV:
1d804d07 8232 return true;
81dc01f7
WL
8233 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8234 /*
8235 * This should never happen, since it is not possible to
8236 * set XSS to a non-zero value---neither in L1 nor in L2.
8237 * If if it were, XSS would have to be checked against
8238 * the XSS exit bitmap in vmcs12.
8239 */
8240 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
55123e3c
WL
8241 case EXIT_REASON_PREEMPTION_TIMER:
8242 return false;
ab007cc9 8243 case EXIT_REASON_PML_FULL:
03efce6f 8244 /* We emulate PML support to L1. */
ab007cc9 8245 return false;
2a499e49
BD
8246 case EXIT_REASON_VMFUNC:
8247 /* VM functions are emulated through L2->L0 vmexits. */
8248 return false;
0b665d30
SC
8249 case EXIT_REASON_ENCLS:
8250 /* SGX is never exposed to L1 */
8251 return false;
644d711a 8252 default:
1d804d07 8253 return true;
644d711a
NHE
8254 }
8255}
8256
7313c698
PB
8257static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8258{
8259 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8260
8261 /*
8262 * At this point, the exit interruption info in exit_intr_info
8263 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8264 * we need to query the in-kernel LAPIC.
8265 */
8266 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8267 if ((exit_intr_info &
8268 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8269 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8270 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8271 vmcs12->vm_exit_intr_error_code =
8272 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8273 }
8274
8275 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8276 vmcs_readl(EXIT_QUALIFICATION));
8277 return 1;
8278}
8279
586f9607
AK
8280static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8281{
8282 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8283 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8284}
8285
a3eaa864 8286static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
843e4330 8287{
a3eaa864
KH
8288 if (vmx->pml_pg) {
8289 __free_page(vmx->pml_pg);
8290 vmx->pml_pg = NULL;
8291 }
843e4330
KH
8292}
8293
54bf36aa 8294static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
843e4330 8295{
54bf36aa 8296 struct vcpu_vmx *vmx = to_vmx(vcpu);
843e4330
KH
8297 u64 *pml_buf;
8298 u16 pml_idx;
8299
8300 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8301
8302 /* Do nothing if PML buffer is empty */
8303 if (pml_idx == (PML_ENTITY_NUM - 1))
8304 return;
8305
8306 /* PML index always points to next available PML buffer entity */
8307 if (pml_idx >= PML_ENTITY_NUM)
8308 pml_idx = 0;
8309 else
8310 pml_idx++;
8311
8312 pml_buf = page_address(vmx->pml_pg);
8313 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8314 u64 gpa;
8315
8316 gpa = pml_buf[pml_idx];
8317 WARN_ON(gpa & (PAGE_SIZE - 1));
54bf36aa 8318 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
843e4330
KH
8319 }
8320
8321 /* reset PML index */
8322 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8323}
8324
8325/*
8326 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8327 * Called before reporting dirty_bitmap to userspace.
8328 */
8329static void kvm_flush_pml_buffers(struct kvm *kvm)
8330{
8331 int i;
8332 struct kvm_vcpu *vcpu;
8333 /*
8334 * We only need to kick vcpu out of guest mode here, as PML buffer
8335 * is flushed at beginning of all VMEXITs, and it's obvious that only
8336 * vcpus running in guest are possible to have unflushed GPAs in PML
8337 * buffer.
8338 */
8339 kvm_for_each_vcpu(i, vcpu, kvm)
8340 kvm_vcpu_kick(vcpu);
8341}
8342
4eb64dce
PB
8343static void vmx_dump_sel(char *name, uint32_t sel)
8344{
8345 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
96794e4e 8346 name, vmcs_read16(sel),
4eb64dce
PB
8347 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8348 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8349 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8350}
8351
8352static void vmx_dump_dtsel(char *name, uint32_t limit)
8353{
8354 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8355 name, vmcs_read32(limit),
8356 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8357}
8358
8359static void dump_vmcs(void)
8360{
8361 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8362 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8363 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8364 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8365 u32 secondary_exec_control = 0;
8366 unsigned long cr4 = vmcs_readl(GUEST_CR4);
f3531054 8367 u64 efer = vmcs_read64(GUEST_IA32_EFER);
4eb64dce
PB
8368 int i, n;
8369
8370 if (cpu_has_secondary_exec_ctrls())
8371 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8372
8373 pr_err("*** Guest State ***\n");
8374 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8375 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8376 vmcs_readl(CR0_GUEST_HOST_MASK));
8377 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8378 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8379 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8380 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8381 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8382 {
845c5b40
PB
8383 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8384 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8385 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8386 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
4eb64dce
PB
8387 }
8388 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8389 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8390 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8391 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8392 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8393 vmcs_readl(GUEST_SYSENTER_ESP),
8394 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8395 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8396 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8397 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8398 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8399 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8400 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8401 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8402 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8403 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8404 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8405 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8406 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
845c5b40
PB
8407 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8408 efer, vmcs_read64(GUEST_IA32_PAT));
8409 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8410 vmcs_read64(GUEST_IA32_DEBUGCTL),
4eb64dce 8411 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
c73da3fc 8412 if (cpu_has_load_perf_global_ctrl() &&
773e8a04 8413 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
845c5b40
PB
8414 pr_err("PerfGlobCtl = 0x%016llx\n",
8415 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
4eb64dce 8416 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
845c5b40 8417 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
4eb64dce
PB
8418 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8419 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8420 vmcs_read32(GUEST_ACTIVITY_STATE));
8421 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8422 pr_err("InterruptStatus = %04x\n",
8423 vmcs_read16(GUEST_INTR_STATUS));
8424
8425 pr_err("*** Host State ***\n");
8426 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8427 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8428 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8429 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8430 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8431 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8432 vmcs_read16(HOST_TR_SELECTOR));
8433 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8434 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8435 vmcs_readl(HOST_TR_BASE));
8436 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8437 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8438 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8439 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8440 vmcs_readl(HOST_CR4));
8441 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8442 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8443 vmcs_read32(HOST_IA32_SYSENTER_CS),
8444 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8445 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
845c5b40
PB
8446 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8447 vmcs_read64(HOST_IA32_EFER),
8448 vmcs_read64(HOST_IA32_PAT));
c73da3fc 8449 if (cpu_has_load_perf_global_ctrl() &&
773e8a04 8450 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
845c5b40
PB
8451 pr_err("PerfGlobCtl = 0x%016llx\n",
8452 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
4eb64dce
PB
8453
8454 pr_err("*** Control State ***\n");
8455 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8456 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8457 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8458 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8459 vmcs_read32(EXCEPTION_BITMAP),
8460 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8461 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8462 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8463 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8464 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8465 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8466 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8467 vmcs_read32(VM_EXIT_INTR_INFO),
8468 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8469 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8470 pr_err(" reason=%08x qualification=%016lx\n",
8471 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8472 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8473 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8474 vmcs_read32(IDT_VECTORING_ERROR_CODE));
845c5b40 8475 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
8cfe9866 8476 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
845c5b40
PB
8477 pr_err("TSC Multiplier = 0x%016llx\n",
8478 vmcs_read64(TSC_MULTIPLIER));
4eb64dce
PB
8479 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8480 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8481 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8482 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8483 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
845c5b40 8484 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
4eb64dce
PB
8485 n = vmcs_read32(CR3_TARGET_COUNT);
8486 for (i = 0; i + 1 < n; i += 4)
8487 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8488 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8489 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8490 if (i < n)
8491 pr_err("CR3 target%u=%016lx\n",
8492 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8493 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8494 pr_err("PLE Gap=%08x Window=%08x\n",
8495 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8496 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8497 pr_err("Virtual processor ID = 0x%04x\n",
8498 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8499}
8500
6aa8b732
AK
8501/*
8502 * The guest has exited. See if we can fix it or if we need userspace
8503 * assistance.
8504 */
851ba692 8505static int vmx_handle_exit(struct kvm_vcpu *vcpu)
6aa8b732 8506{
29bd8a78 8507 struct vcpu_vmx *vmx = to_vmx(vcpu);
a0861c02 8508 u32 exit_reason = vmx->exit_reason;
1155f76a 8509 u32 vectoring_info = vmx->idt_vectoring_info;
29bd8a78 8510
8b89fe1f
PB
8511 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8512
843e4330
KH
8513 /*
8514 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8515 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8516 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8517 * mode as if vcpus is in root mode, the PML buffer must has been
8518 * flushed already.
8519 */
8520 if (enable_pml)
54bf36aa 8521 vmx_flush_pml_buffer(vcpu);
843e4330 8522
80ced186 8523 /* If guest state is invalid, start emulating */
14168786 8524 if (vmx->emulation_required)
80ced186 8525 return handle_invalid_guest_state(vcpu);
1d5a4d9b 8526
7313c698
PB
8527 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
8528 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
644d711a 8529
5120702e 8530 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
4eb64dce 8531 dump_vmcs();
5120702e
MG
8532 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8533 vcpu->run->fail_entry.hardware_entry_failure_reason
8534 = exit_reason;
8535 return 0;
8536 }
8537
29bd8a78 8538 if (unlikely(vmx->fail)) {
851ba692
AK
8539 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8540 vcpu->run->fail_entry.hardware_entry_failure_reason
29bd8a78
AK
8541 = vmcs_read32(VM_INSTRUCTION_ERROR);
8542 return 0;
8543 }
6aa8b732 8544
b9bf6882
XG
8545 /*
8546 * Note:
8547 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8548 * delivery event since it indicates guest is accessing MMIO.
8549 * The vm-exit can be triggered again after return to guest that
8550 * will cause infinite loop.
8551 */
d77c26fc 8552 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
1439442c 8553 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
60637aac 8554 exit_reason != EXIT_REASON_EPT_VIOLATION &&
b244c9fc 8555 exit_reason != EXIT_REASON_PML_FULL &&
b9bf6882
XG
8556 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8557 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8558 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
70bcd708 8559 vcpu->run->internal.ndata = 3;
b9bf6882
XG
8560 vcpu->run->internal.data[0] = vectoring_info;
8561 vcpu->run->internal.data[1] = exit_reason;
70bcd708
PB
8562 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
8563 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
8564 vcpu->run->internal.ndata++;
8565 vcpu->run->internal.data[3] =
8566 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
8567 }
b9bf6882
XG
8568 return 0;
8569 }
3b86cd99 8570
d02fcf50 8571 if (unlikely(!enable_vnmi &&
8a1b4392
PB
8572 vmx->loaded_vmcs->soft_vnmi_blocked)) {
8573 if (vmx_interrupt_allowed(vcpu)) {
8574 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8575 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
8576 vcpu->arch.nmi_pending) {
8577 /*
8578 * This CPU don't support us in finding the end of an
8579 * NMI-blocked window if the guest runs with IRQs
8580 * disabled. So we pull the trigger after 1 s of
8581 * futile waiting, but inform the user about this.
8582 */
8583 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8584 "state on VCPU %d after 1 s timeout\n",
8585 __func__, vcpu->vcpu_id);
8586 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
8587 }
8588 }
8589
6aa8b732
AK
8590 if (exit_reason < kvm_vmx_max_exit_handlers
8591 && kvm_vmx_exit_handlers[exit_reason])
851ba692 8592 return kvm_vmx_exit_handlers[exit_reason](vcpu);
6aa8b732 8593 else {
6c6c5e03
RK
8594 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8595 exit_reason);
2bc19dc3
MT
8596 kvm_queue_exception(vcpu, UD_VECTOR);
8597 return 1;
6aa8b732 8598 }
6aa8b732
AK
8599}
8600
a47dd5f0
PB
8601/*
8602 * Software based L1D cache flush which is used when microcode providing
8603 * the cache control MSR is not loaded.
8604 *
8605 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8606 * flush it is required to read in 64 KiB because the replacement algorithm
8607 * is not exactly LRU. This could be sized at runtime via topology
8608 * information but as all relevant affected CPUs have 32KiB L1D cache size
8609 * there is no point in doing so.
8610 */
c595ceee 8611static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
a47dd5f0
PB
8612{
8613 int size = PAGE_SIZE << L1D_CACHE_ORDER;
c595ceee
PB
8614
8615 /*
2f055947
TG
8616 * This code is only executed when the the flush mode is 'cond' or
8617 * 'always'
c595ceee 8618 */
427362a1 8619 if (static_branch_likely(&vmx_l1d_flush_cond)) {
45b575c0 8620 bool flush_l1d;
5b6ccc6c 8621
379fd0c7 8622 /*
45b575c0
NS
8623 * Clear the per-vcpu flush bit, it gets set again
8624 * either from vcpu_run() or from one of the unsafe
8625 * VMEXIT handlers.
379fd0c7 8626 */
45b575c0 8627 flush_l1d = vcpu->arch.l1tf_flush_l1d;
4c6523ec 8628 vcpu->arch.l1tf_flush_l1d = false;
45b575c0
NS
8629
8630 /*
8631 * Clear the per-cpu flush bit, it gets set again from
8632 * the interrupt handlers.
8633 */
8634 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
8635 kvm_clear_cpu_l1tf_flush_l1d();
8636
5b6ccc6c
NS
8637 if (!flush_l1d)
8638 return;
379fd0c7 8639 }
c595ceee
PB
8640
8641 vcpu->stat.l1d_flush++;
a47dd5f0 8642
3fa045be
PB
8643 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8644 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8645 return;
8646 }
8647
a47dd5f0
PB
8648 asm volatile(
8649 /* First ensure the pages are in the TLB */
8650 "xorl %%eax, %%eax\n"
8651 ".Lpopulate_tlb:\n\t"
288d152c 8652 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
a47dd5f0
PB
8653 "addl $4096, %%eax\n\t"
8654 "cmpl %%eax, %[size]\n\t"
8655 "jne .Lpopulate_tlb\n\t"
8656 "xorl %%eax, %%eax\n\t"
8657 "cpuid\n\t"
8658 /* Now fill the cache */
8659 "xorl %%eax, %%eax\n"
8660 ".Lfill_cache:\n"
288d152c 8661 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
a47dd5f0
PB
8662 "addl $64, %%eax\n\t"
8663 "cmpl %%eax, %[size]\n\t"
8664 "jne .Lfill_cache\n\t"
8665 "lfence\n"
288d152c 8666 :: [flush_pages] "r" (vmx_l1d_flush_pages),
a47dd5f0
PB
8667 [size] "r" (size)
8668 : "eax", "ebx", "ecx", "edx");
8669}
8670
95ba8273 8671static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
6e5d865c 8672{
a7c0b07d
WL
8673 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8674
8675 if (is_guest_mode(vcpu) &&
8676 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8677 return;
8678
95ba8273 8679 if (irr == -1 || tpr < irr) {
6e5d865c
YS
8680 vmcs_write32(TPR_THRESHOLD, 0);
8681 return;
8682 }
8683
95ba8273 8684 vmcs_write32(TPR_THRESHOLD, irr);
6e5d865c
YS
8685}
8686
8d860bbe 8687static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
8d14695f
YZ
8688{
8689 u32 sec_exec_control;
8690
8d860bbe
JM
8691 if (!lapic_in_kernel(vcpu))
8692 return;
8693
fd6b6d9b
SC
8694 if (!flexpriority_enabled &&
8695 !cpu_has_vmx_virtualize_x2apic_mode())
8696 return;
8697
dccbfcf5
RK
8698 /* Postpone execution until vmcs01 is the current VMCS. */
8699 if (is_guest_mode(vcpu)) {
8d860bbe 8700 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
dccbfcf5
RK
8701 return;
8702 }
8703
8d14695f 8704 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8d860bbe
JM
8705 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
8706 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
8d14695f 8707
8d860bbe
JM
8708 switch (kvm_get_apic_mode(vcpu)) {
8709 case LAPIC_MODE_INVALID:
8710 WARN_ONCE(true, "Invalid local APIC state");
8711 case LAPIC_MODE_DISABLED:
8712 break;
8713 case LAPIC_MODE_XAPIC:
8714 if (flexpriority_enabled) {
8715 sec_exec_control |=
8716 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8717 vmx_flush_tlb(vcpu, true);
8718 }
8719 break;
8720 case LAPIC_MODE_X2APIC:
8721 if (cpu_has_vmx_virtualize_x2apic_mode())
8722 sec_exec_control |=
8723 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8724 break;
8d14695f
YZ
8725 }
8726 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8727
904e14fb 8728 vmx_update_msr_bitmap(vcpu);
8d14695f
YZ
8729}
8730
38b99173
TC
8731static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8732{
ab5df31c 8733 if (!is_guest_mode(vcpu)) {
38b99173 8734 vmcs_write64(APIC_ACCESS_ADDR, hpa);
a468f2db 8735 vmx_flush_tlb(vcpu, true);
fb6c8198 8736 }
38b99173
TC
8737}
8738
67c9dddc 8739static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
c7c9c56c
YZ
8740{
8741 u16 status;
8742 u8 old;
8743
67c9dddc
PB
8744 if (max_isr == -1)
8745 max_isr = 0;
c7c9c56c
YZ
8746
8747 status = vmcs_read16(GUEST_INTR_STATUS);
8748 old = status >> 8;
67c9dddc 8749 if (max_isr != old) {
c7c9c56c 8750 status &= 0xff;
67c9dddc 8751 status |= max_isr << 8;
c7c9c56c
YZ
8752 vmcs_write16(GUEST_INTR_STATUS, status);
8753 }
8754}
8755
8756static void vmx_set_rvi(int vector)
8757{
8758 u16 status;
8759 u8 old;
8760
4114c27d
WW
8761 if (vector == -1)
8762 vector = 0;
8763
c7c9c56c
YZ
8764 status = vmcs_read16(GUEST_INTR_STATUS);
8765 old = (u8)status & 0xff;
8766 if ((u8)vector != old) {
8767 status &= ~0xff;
8768 status |= (u8)vector;
8769 vmcs_write16(GUEST_INTR_STATUS, status);
8770 }
8771}
8772
8773static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8774{
963fee16 8775 /*
851c1a18
LA
8776 * When running L2, updating RVI is only relevant when
8777 * vmcs12 virtual-interrupt-delivery enabled.
8778 * However, it can be enabled only when L1 also
8779 * intercepts external-interrupts and in that case
8780 * we should not update vmcs02 RVI but instead intercept
8781 * interrupt. Therefore, do nothing when running L2.
963fee16 8782 */
851c1a18
LA
8783 if (!is_guest_mode(vcpu))
8784 vmx_set_rvi(max_irr);
c7c9c56c
YZ
8785}
8786
76dfafd5 8787static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
810e6def
PB
8788{
8789 struct vcpu_vmx *vmx = to_vmx(vcpu);
76dfafd5 8790 int max_irr;
f27a85c4 8791 bool max_irr_updated;
810e6def 8792
76dfafd5
PB
8793 WARN_ON(!vcpu->arch.apicv_active);
8794 if (pi_test_on(&vmx->pi_desc)) {
8795 pi_clear_on(&vmx->pi_desc);
8796 /*
8797 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8798 * But on x86 this is just a compiler barrier anyway.
8799 */
8800 smp_mb__after_atomic();
f27a85c4
LA
8801 max_irr_updated =
8802 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
8803
8804 /*
8805 * If we are running L2 and L1 has a new pending interrupt
8806 * which can be injected, we should re-evaluate
8807 * what should be done with this new L1 interrupt.
851c1a18
LA
8808 * If L1 intercepts external-interrupts, we should
8809 * exit from L2 to L1. Otherwise, interrupt should be
8810 * delivered directly to L2.
f27a85c4 8811 */
851c1a18
LA
8812 if (is_guest_mode(vcpu) && max_irr_updated) {
8813 if (nested_exit_on_intr(vcpu))
8814 kvm_vcpu_exiting_guest_mode(vcpu);
8815 else
8816 kvm_make_request(KVM_REQ_EVENT, vcpu);
8817 }
76dfafd5
PB
8818 } else {
8819 max_irr = kvm_lapic_find_highest_irr(vcpu);
8820 }
8821 vmx_hwapic_irr_update(vcpu, max_irr);
8822 return max_irr;
810e6def
PB
8823}
8824
7e712684
PB
8825static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
8826{
8827 u8 rvi = vmx_get_rvi();
8828 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
8829
8830 return ((rvi & 0xf0) > (vppr & 0xf0));
8831}
8832
6308630b 8833static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
c7c9c56c 8834{
d62caabb 8835 if (!kvm_vcpu_apicv_active(vcpu))
3d81bc7e
YZ
8836 return;
8837
c7c9c56c
YZ
8838 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8839 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8840 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8841 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8842}
8843
967235d3
PB
8844static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
8845{
8846 struct vcpu_vmx *vmx = to_vmx(vcpu);
8847
8848 pi_clear_on(&vmx->pi_desc);
8849 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
8850}
8851
51aa01d1 8852static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
cf393f75 8853{
48ae0fb4
JM
8854 u32 exit_intr_info = 0;
8855 u16 basic_exit_reason = (u16)vmx->exit_reason;
00eba012 8856
48ae0fb4
JM
8857 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8858 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
00eba012
AK
8859 return;
8860
48ae0fb4
JM
8861 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
8862 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8863 vmx->exit_intr_info = exit_intr_info;
a0861c02 8864
1261bfa3
WL
8865 /* if exit due to PF check for async PF */
8866 if (is_page_fault(exit_intr_info))
8867 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
8868
a0861c02 8869 /* Handle machine checks before interrupts are enabled */
48ae0fb4
JM
8870 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
8871 is_machine_check(exit_intr_info))
a0861c02
AK
8872 kvm_machine_check();
8873
20f65983 8874 /* We need to handle NMIs before interrupts are enabled */
ef85b673 8875 if (is_nmi(exit_intr_info)) {
dd60d217 8876 kvm_before_interrupt(&vmx->vcpu);
20f65983 8877 asm("int $2");
dd60d217 8878 kvm_after_interrupt(&vmx->vcpu);
ff9d07a0 8879 }
51aa01d1 8880}
20f65983 8881
a547c6db
YZ
8882static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8883{
8884 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8885
a547c6db
YZ
8886 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8887 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8888 unsigned int vector;
8889 unsigned long entry;
8890 gate_desc *desc;
8891 struct vcpu_vmx *vmx = to_vmx(vcpu);
8892#ifdef CONFIG_X86_64
8893 unsigned long tmp;
8894#endif
8895
8896 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8897 desc = (gate_desc *)vmx->host_idt_base + vector;
64b163fa 8898 entry = gate_offset(desc);
a547c6db
YZ
8899 asm volatile(
8900#ifdef CONFIG_X86_64
8901 "mov %%" _ASM_SP ", %[sp]\n\t"
8902 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8903 "push $%c[ss]\n\t"
8904 "push %[sp]\n\t"
8905#endif
8906 "pushf\n\t"
a547c6db 8907 __ASM_SIZE(push) " $%c[cs]\n\t"
c940a3fb 8908 CALL_NOSPEC
a547c6db
YZ
8909 :
8910#ifdef CONFIG_X86_64
3f62de5f 8911 [sp]"=&r"(tmp),
a547c6db 8912#endif
f5caf621 8913 ASM_CALL_CONSTRAINT
a547c6db 8914 :
c940a3fb 8915 THUNK_TARGET(entry),
a547c6db
YZ
8916 [ss]"i"(__KERNEL_DS),
8917 [cs]"i"(__KERNEL_CS)
8918 );
f2485b3e 8919 }
a547c6db 8920}
c207aee4 8921STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
a547c6db 8922
bc226f07 8923static bool vmx_has_emulated_msr(int index)
6d396b55 8924{
bc226f07
TL
8925 switch (index) {
8926 case MSR_IA32_SMBASE:
8927 /*
8928 * We cannot do SMM unless we can run the guest in big
8929 * real mode.
8930 */
8931 return enable_unrestricted_guest || emulate_invalid_guest_state;
8932 case MSR_AMD64_VIRT_SPEC_CTRL:
8933 /* This is AMD only. */
8934 return false;
8935 default:
8936 return true;
8937 }
6d396b55
PB
8938}
8939
51aa01d1
AK
8940static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8941{
c5ca8e57 8942 u32 exit_intr_info;
51aa01d1
AK
8943 bool unblock_nmi;
8944 u8 vector;
8945 bool idtv_info_valid;
8946
8947 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
20f65983 8948
d02fcf50 8949 if (enable_vnmi) {
8a1b4392
PB
8950 if (vmx->loaded_vmcs->nmi_known_unmasked)
8951 return;
8952 /*
8953 * Can't use vmx->exit_intr_info since we're not sure what
8954 * the exit reason is.
8955 */
8956 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8957 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8958 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8959 /*
8960 * SDM 3: 27.7.1.2 (September 2008)
8961 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8962 * a guest IRET fault.
8963 * SDM 3: 23.2.2 (September 2008)
8964 * Bit 12 is undefined in any of the following cases:
8965 * If the VM exit sets the valid bit in the IDT-vectoring
8966 * information field.
8967 * If the VM exit is due to a double fault.
8968 */
8969 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8970 vector != DF_VECTOR && !idtv_info_valid)
8971 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8972 GUEST_INTR_STATE_NMI);
8973 else
8974 vmx->loaded_vmcs->nmi_known_unmasked =
8975 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8976 & GUEST_INTR_STATE_NMI);
8977 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
8978 vmx->loaded_vmcs->vnmi_blocked_time +=
8979 ktime_to_ns(ktime_sub(ktime_get(),
8980 vmx->loaded_vmcs->entry_time));
51aa01d1
AK
8981}
8982
3ab66e8a 8983static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
83422e17
AK
8984 u32 idt_vectoring_info,
8985 int instr_len_field,
8986 int error_code_field)
51aa01d1 8987{
51aa01d1
AK
8988 u8 vector;
8989 int type;
8990 bool idtv_info_valid;
8991
8992 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
668f612f 8993
3ab66e8a
JK
8994 vcpu->arch.nmi_injected = false;
8995 kvm_clear_exception_queue(vcpu);
8996 kvm_clear_interrupt_queue(vcpu);
37b96e98
GN
8997
8998 if (!idtv_info_valid)
8999 return;
9000
3ab66e8a 9001 kvm_make_request(KVM_REQ_EVENT, vcpu);
3842d135 9002
668f612f
AK
9003 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9004 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
37b96e98 9005
64a7ec06 9006 switch (type) {
37b96e98 9007 case INTR_TYPE_NMI_INTR:
3ab66e8a 9008 vcpu->arch.nmi_injected = true;
668f612f 9009 /*
7b4a25cb 9010 * SDM 3: 27.7.1.2 (September 2008)
37b96e98
GN
9011 * Clear bit "block by NMI" before VM entry if a NMI
9012 * delivery faulted.
668f612f 9013 */
3ab66e8a 9014 vmx_set_nmi_mask(vcpu, false);
37b96e98 9015 break;
37b96e98 9016 case INTR_TYPE_SOFT_EXCEPTION:
3ab66e8a 9017 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
66fd3f7f
GN
9018 /* fall through */
9019 case INTR_TYPE_HARD_EXCEPTION:
35920a35 9020 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
83422e17 9021 u32 err = vmcs_read32(error_code_field);
851eb667 9022 kvm_requeue_exception_e(vcpu, vector, err);
35920a35 9023 } else
851eb667 9024 kvm_requeue_exception(vcpu, vector);
37b96e98 9025 break;
66fd3f7f 9026 case INTR_TYPE_SOFT_INTR:
3ab66e8a 9027 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
66fd3f7f 9028 /* fall through */
37b96e98 9029 case INTR_TYPE_EXT_INTR:
3ab66e8a 9030 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
37b96e98
GN
9031 break;
9032 default:
9033 break;
f7d9238f 9034 }
cf393f75
AK
9035}
9036
83422e17
AK
9037static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9038{
3ab66e8a 9039 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
83422e17
AK
9040 VM_EXIT_INSTRUCTION_LEN,
9041 IDT_VECTORING_ERROR_CODE);
9042}
9043
b463a6f7
AK
9044static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9045{
3ab66e8a 9046 __vmx_complete_interrupts(vcpu,
b463a6f7
AK
9047 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9048 VM_ENTRY_INSTRUCTION_LEN,
9049 VM_ENTRY_EXCEPTION_ERROR_CODE);
9050
9051 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9052}
9053
d7cd9796
GN
9054static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9055{
9056 int i, nr_msrs;
9057 struct perf_guest_switch_msr *msrs;
9058
9059 msrs = perf_guest_get_msrs(&nr_msrs);
9060
9061 if (!msrs)
9062 return;
9063
9064 for (i = 0; i < nr_msrs; i++)
9065 if (msrs[i].host == msrs[i].guest)
9066 clear_atomic_switch_msr(vmx, msrs[i].msr);
9067 else
9068 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
989e3992 9069 msrs[i].host, false);
d7cd9796
GN
9070}
9071
f459a707
SC
9072static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
9073{
9074 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
9075 if (!vmx->loaded_vmcs->hv_timer_armed)
9076 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
9077 PIN_BASED_VMX_PREEMPTION_TIMER);
9078 vmx->loaded_vmcs->hv_timer_armed = true;
9079}
9080
9081static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
64672c95
YJ
9082{
9083 struct vcpu_vmx *vmx = to_vmx(vcpu);
9084 u64 tscl;
9085 u32 delta_tsc;
9086
d264ee0c
SC
9087 if (vmx->req_immediate_exit) {
9088 vmx_arm_hv_timer(vmx, 0);
9089 return;
9090 }
9091
f459a707
SC
9092 if (vmx->hv_deadline_tsc != -1) {
9093 tscl = rdtsc();
9094 if (vmx->hv_deadline_tsc > tscl)
9095 /* set_hv_timer ensures the delta fits in 32-bits */
9096 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9097 cpu_preemption_timer_multi);
9098 else
9099 delta_tsc = 0;
64672c95 9100
f459a707
SC
9101 vmx_arm_hv_timer(vmx, delta_tsc);
9102 return;
9103 }
64672c95 9104
f459a707
SC
9105 if (vmx->loaded_vmcs->hv_timer_armed)
9106 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
9107 PIN_BASED_VMX_PREEMPTION_TIMER);
9108 vmx->loaded_vmcs->hv_timer_armed = false;
64672c95
YJ
9109}
9110
a3b5ba49 9111static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
6aa8b732 9112{
a2fa3e9f 9113 struct vcpu_vmx *vmx = to_vmx(vcpu);
773e8a04 9114 unsigned long cr3, cr4, evmcs_rsp;
104f226b 9115
8a1b4392 9116 /* Record the guest's net vcpu time for enforced NMI injections. */
d02fcf50 9117 if (unlikely(!enable_vnmi &&
8a1b4392
PB
9118 vmx->loaded_vmcs->soft_vnmi_blocked))
9119 vmx->loaded_vmcs->entry_time = ktime_get();
9120
104f226b
AK
9121 /* Don't enter VMX if guest state is invalid, let the exit handler
9122 start emulation until we arrive back to a valid state */
14168786 9123 if (vmx->emulation_required)
104f226b
AK
9124 return;
9125
a7653ecd
RK
9126 if (vmx->ple_window_dirty) {
9127 vmx->ple_window_dirty = false;
9128 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9129 }
9130
945679e3 9131 if (vmx->nested.need_vmcs12_sync) {
8cab6507
VK
9132 /*
9133 * hv_evmcs may end up being not mapped after migration (when
9134 * L2 was running), map it here to make sure vmcs12 changes are
9135 * properly reflected.
9136 */
9137 if (vmx->nested.enlightened_vmcs_enabled &&
9138 !vmx->nested.hv_evmcs)
9139 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
9140
945679e3
VK
9141 if (vmx->nested.hv_evmcs) {
9142 copy_vmcs12_to_enlightened(vmx);
9143 /* All fields are clean */
9144 vmx->nested.hv_evmcs->hv_clean_fields |=
9145 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9146 } else {
9147 copy_vmcs12_to_shadow(vmx);
9148 }
9149 vmx->nested.need_vmcs12_sync = false;
012f83cb
AG
9150 }
9151
104f226b
AK
9152 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9153 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9154 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9155 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9156
d6e41f11 9157 cr3 = __get_current_cr3_fast();
d7ee039e 9158 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
d6e41f11 9159 vmcs_writel(HOST_CR3, cr3);
d7ee039e 9160 vmx->loaded_vmcs->host_state.cr3 = cr3;
d6e41f11
AL
9161 }
9162
1e02ce4c 9163 cr4 = cr4_read_shadow();
d7ee039e 9164 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
d974baa3 9165 vmcs_writel(HOST_CR4, cr4);
d7ee039e 9166 vmx->loaded_vmcs->host_state.cr4 = cr4;
d974baa3
AL
9167 }
9168
104f226b
AK
9169 /* When single-stepping over STI and MOV SS, we must clear the
9170 * corresponding interruptibility bits in the guest state. Otherwise
9171 * vmentry fails as it then expects bit 14 (BS) in pending debug
9172 * exceptions being set, but that's not correct for the guest debugging
9173 * case. */
9174 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9175 vmx_set_interrupt_shadow(vcpu, 0);
9176
b9dd21e1
PB
9177 if (static_cpu_has(X86_FEATURE_PKU) &&
9178 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9179 vcpu->arch.pkru != vmx->host_pkru)
9180 __write_pkru(vcpu->arch.pkru);
1be0e61c 9181
d7cd9796
GN
9182 atomic_switch_perf_msrs(vmx);
9183
f459a707 9184 vmx_update_hv_timer(vcpu);
64672c95 9185
d28b387f
KA
9186 /*
9187 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9188 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9189 * is no need to worry about the conditional branch over the wrmsr
9190 * being speculatively taken.
9191 */
ccbcd267 9192 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
d28b387f 9193
d462b819 9194 vmx->__launched = vmx->loaded_vmcs->launched;
773e8a04
VK
9195
9196 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9197 (unsigned long)&current_evmcs->host_rsp : 0;
9198
5b6ccc6c
NS
9199 if (static_branch_unlikely(&vmx_l1d_should_flush))
9200 vmx_l1d_flush(vcpu);
c595ceee 9201
104f226b 9202 asm(
6aa8b732 9203 /* Store host registers */
b188c81f
AK
9204 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9205 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9206 "push %%" _ASM_CX " \n\t"
9207 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
313dbd49 9208 "je 1f \n\t"
b188c81f 9209 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
773e8a04
VK
9210 /* Avoid VMWRITE when Enlightened VMCS is in use */
9211 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9212 "jz 2f \n\t"
9213 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9214 "jmp 1f \n\t"
9215 "2: \n\t"
4b1e5478 9216 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
313dbd49 9217 "1: \n\t"
d3edefc0 9218 /* Reload cr2 if changed */
b188c81f
AK
9219 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9220 "mov %%cr2, %%" _ASM_DX " \n\t"
9221 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
773e8a04 9222 "je 3f \n\t"
b188c81f 9223 "mov %%" _ASM_AX", %%cr2 \n\t"
773e8a04 9224 "3: \n\t"
00df9181 9225 /* Check if vmlaunch or vmresume is needed */
e08aa78a 9226 "cmpl $0, %c[launched](%0) \n\t"
6aa8b732 9227 /* Load guest registers. Don't clobber flags. */
b188c81f
AK
9228 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9229 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9230 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9231 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9232 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9233 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
05b3e0c2 9234#ifdef CONFIG_X86_64
e08aa78a
AK
9235 "mov %c[r8](%0), %%r8 \n\t"
9236 "mov %c[r9](%0), %%r9 \n\t"
9237 "mov %c[r10](%0), %%r10 \n\t"
9238 "mov %c[r11](%0), %%r11 \n\t"
9239 "mov %c[r12](%0), %%r12 \n\t"
9240 "mov %c[r13](%0), %%r13 \n\t"
9241 "mov %c[r14](%0), %%r14 \n\t"
9242 "mov %c[r15](%0), %%r15 \n\t"
6aa8b732 9243#endif
b188c81f 9244 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
c801949d 9245
6aa8b732 9246 /* Enter guest mode */
83287ea4 9247 "jne 1f \n\t"
4b1e5478 9248 __ex("vmlaunch") "\n\t"
83287ea4 9249 "jmp 2f \n\t"
4b1e5478 9250 "1: " __ex("vmresume") "\n\t"
83287ea4 9251 "2: "
6aa8b732 9252 /* Save guest registers, load host registers, keep flags */
b188c81f 9253 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
40712fae 9254 "pop %0 \n\t"
0cb5b306 9255 "setbe %c[fail](%0)\n\t"
b188c81f
AK
9256 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9257 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9258 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9259 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9260 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9261 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9262 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
05b3e0c2 9263#ifdef CONFIG_X86_64
e08aa78a
AK
9264 "mov %%r8, %c[r8](%0) \n\t"
9265 "mov %%r9, %c[r9](%0) \n\t"
9266 "mov %%r10, %c[r10](%0) \n\t"
9267 "mov %%r11, %c[r11](%0) \n\t"
9268 "mov %%r12, %c[r12](%0) \n\t"
9269 "mov %%r13, %c[r13](%0) \n\t"
9270 "mov %%r14, %c[r14](%0) \n\t"
9271 "mov %%r15, %c[r15](%0) \n\t"
43ce76ce
UB
9272 /*
9273 * Clear host registers marked as clobbered to prevent
9274 * speculative use.
9275 */
0cb5b306
JM
9276 "xor %%r8d, %%r8d \n\t"
9277 "xor %%r9d, %%r9d \n\t"
9278 "xor %%r10d, %%r10d \n\t"
9279 "xor %%r11d, %%r11d \n\t"
9280 "xor %%r12d, %%r12d \n\t"
9281 "xor %%r13d, %%r13d \n\t"
9282 "xor %%r14d, %%r14d \n\t"
9283 "xor %%r15d, %%r15d \n\t"
6aa8b732 9284#endif
b188c81f
AK
9285 "mov %%cr2, %%" _ASM_AX " \n\t"
9286 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
c801949d 9287
0cb5b306
JM
9288 "xor %%eax, %%eax \n\t"
9289 "xor %%ebx, %%ebx \n\t"
9290 "xor %%esi, %%esi \n\t"
9291 "xor %%edi, %%edi \n\t"
b188c81f 9292 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
83287ea4
AK
9293 ".pushsection .rodata \n\t"
9294 ".global vmx_return \n\t"
9295 "vmx_return: " _ASM_PTR " 2b \n\t"
9296 ".popsection"
773e8a04 9297 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
d462b819 9298 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
e08aa78a 9299 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
313dbd49 9300 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
ad312c7c
ZX
9301 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9302 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9303 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9304 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9305 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9306 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9307 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
05b3e0c2 9308#ifdef CONFIG_X86_64
ad312c7c
ZX
9309 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9310 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9311 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9312 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9313 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9314 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9315 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9316 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
6aa8b732 9317#endif
40712fae
AK
9318 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9319 [wordsize]"i"(sizeof(ulong))
c2036300
LV
9320 : "cc", "memory"
9321#ifdef CONFIG_X86_64
773e8a04 9322 , "rax", "rbx", "rdi"
c2036300 9323 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
b188c81f 9324#else
773e8a04 9325 , "eax", "ebx", "edi"
c2036300
LV
9326#endif
9327 );
6aa8b732 9328
d28b387f
KA
9329 /*
9330 * We do not use IBRS in the kernel. If this vCPU has used the
9331 * SPEC_CTRL MSR it may have left it on; save the value and
9332 * turn it off. This is much more efficient than blindly adding
9333 * it to the atomic save/restore list. Especially as the former
9334 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9335 *
9336 * For non-nested case:
9337 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9338 * save it.
9339 *
9340 * For nested case:
9341 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9342 * save it.
9343 */
946fbbc1 9344 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
ecb586bd 9345 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
d28b387f 9346
ccbcd267 9347 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
d28b387f 9348
117cc7a9
DW
9349 /* Eliminate branch target predictions from guest mode */
9350 vmexit_fill_RSB();
9351
773e8a04
VK
9352 /* All fields are clean at this point */
9353 if (static_branch_unlikely(&enable_evmcs))
9354 current_evmcs->hv_clean_fields |=
9355 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9356
2a7921b7 9357 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
74c55931
WL
9358 if (vmx->host_debugctlmsr)
9359 update_debugctlmsr(vmx->host_debugctlmsr);
2a7921b7 9360
aa67f609
AK
9361#ifndef CONFIG_X86_64
9362 /*
9363 * The sysexit path does not restore ds/es, so we must set them to
9364 * a reasonable value ourselves.
9365 *
6d6095bd
SC
9366 * We can't defer this to vmx_prepare_switch_to_host() since that
9367 * function may be executed in interrupt context, which saves and
9368 * restore segments around it, nullifying its effect.
aa67f609
AK
9369 */
9370 loadsegment(ds, __USER_DS);
9371 loadsegment(es, __USER_DS);
9372#endif
9373
6de4f3ad 9374 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
6de12732 9375 | (1 << VCPU_EXREG_RFLAGS)
aff48baa 9376 | (1 << VCPU_EXREG_PDPTR)
2fb92db1 9377 | (1 << VCPU_EXREG_SEGMENTS)
aff48baa 9378 | (1 << VCPU_EXREG_CR3));
5fdbf976
MT
9379 vcpu->arch.regs_dirty = 0;
9380
1be0e61c
XG
9381 /*
9382 * eager fpu is enabled if PKEY is supported and CR4 is switched
9383 * back on host, so it is safe to read guest PKRU from current
9384 * XSAVE.
9385 */
b9dd21e1
PB
9386 if (static_cpu_has(X86_FEATURE_PKU) &&
9387 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9388 vcpu->arch.pkru = __read_pkru();
9389 if (vcpu->arch.pkru != vmx->host_pkru)
1be0e61c 9390 __write_pkru(vmx->host_pkru);
1be0e61c
XG
9391 }
9392
e0b890d3 9393 vmx->nested.nested_run_pending = 0;
b060ca3b
JM
9394 vmx->idt_vectoring_info = 0;
9395
9396 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9397 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9398 return;
9399
9400 vmx->loaded_vmcs->launched = 1;
9401 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
e0b890d3 9402
51aa01d1
AK
9403 vmx_complete_atomic_exit(vmx);
9404 vmx_recover_nmi_blocking(vmx);
cf393f75 9405 vmx_complete_interrupts(vmx);
6aa8b732 9406}
c207aee4 9407STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
6aa8b732 9408
434a1e94
SC
9409static struct kvm *vmx_vm_alloc(void)
9410{
d1e5b0e9 9411 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
40bbb9d0 9412 return &kvm_vmx->kvm;
434a1e94
SC
9413}
9414
9415static void vmx_vm_free(struct kvm *kvm)
9416{
d1e5b0e9 9417 vfree(to_kvm_vmx(kvm));
434a1e94
SC
9418}
9419
1279a6b1 9420static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
4fa7734c
PB
9421{
9422 struct vcpu_vmx *vmx = to_vmx(vcpu);
9423 int cpu;
9424
1279a6b1 9425 if (vmx->loaded_vmcs == vmcs)
4fa7734c
PB
9426 return;
9427
9428 cpu = get_cpu();
4fa7734c 9429 vmx_vcpu_put(vcpu);
bd9966de 9430 vmx->loaded_vmcs = vmcs;
4fa7734c 9431 vmx_vcpu_load(vcpu, cpu);
4fa7734c 9432 put_cpu();
b7031fd4
SC
9433
9434 vm_entry_controls_reset_shadow(vmx);
9435 vm_exit_controls_reset_shadow(vmx);
9436 vmx_segment_cache_clear(vmx);
4fa7734c
PB
9437}
9438
2f1fe811
JM
9439/*
9440 * Ensure that the current vmcs of the logical processor is the
9441 * vmcs01 of the vcpu before calling free_nested().
9442 */
9443static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9444{
14c07ad8
VK
9445 vcpu_load(vcpu);
9446 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
9447 free_nested(vcpu);
9448 vcpu_put(vcpu);
2f1fe811
JM
9449}
9450
6aa8b732
AK
9451static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9452{
fb3f0f51
RR
9453 struct vcpu_vmx *vmx = to_vmx(vcpu);
9454
843e4330 9455 if (enable_pml)
a3eaa864 9456 vmx_destroy_pml_buffer(vmx);
991e7a0e 9457 free_vpid(vmx->vpid);
4fa7734c 9458 leave_guest_mode(vcpu);
2f1fe811 9459 vmx_free_vcpu_nested(vcpu);
4fa7734c 9460 free_loaded_vmcs(vmx->loaded_vmcs);
fb3f0f51
RR
9461 kfree(vmx->guest_msrs);
9462 kvm_vcpu_uninit(vcpu);
a4770347 9463 kmem_cache_free(kvm_vcpu_cache, vmx);
6aa8b732
AK
9464}
9465
fb3f0f51 9466static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
6aa8b732 9467{
fb3f0f51 9468 int err;
c16f862d 9469 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
904e14fb 9470 unsigned long *msr_bitmap;
15ad7146 9471 int cpu;
6aa8b732 9472
a2fa3e9f 9473 if (!vmx)
fb3f0f51
RR
9474 return ERR_PTR(-ENOMEM);
9475
991e7a0e 9476 vmx->vpid = allocate_vpid();
2384d2b3 9477
fb3f0f51
RR
9478 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9479 if (err)
9480 goto free_vcpu;
965b58a5 9481
4e59516a
PF
9482 err = -ENOMEM;
9483
9484 /*
9485 * If PML is turned on, failure on enabling PML just results in failure
9486 * of creating the vcpu, therefore we can simplify PML logic (by
9487 * avoiding dealing with cases, such as enabling PML partially on vcpus
9488 * for the guest, etc.
9489 */
9490 if (enable_pml) {
9491 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9492 if (!vmx->pml_pg)
9493 goto uninit_vcpu;
9494 }
9495
a2fa3e9f 9496 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
03916db9
PB
9497 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9498 > PAGE_SIZE);
0123be42 9499
4e59516a
PF
9500 if (!vmx->guest_msrs)
9501 goto free_pml;
965b58a5 9502
f21f165e
PB
9503 err = alloc_loaded_vmcs(&vmx->vmcs01);
9504 if (err < 0)
fb3f0f51 9505 goto free_msrs;
a2fa3e9f 9506
904e14fb
PB
9507 msr_bitmap = vmx->vmcs01.msr_bitmap;
9508 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9509 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9510 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9511 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9512 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9513 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9514 vmx->msr_bitmap_mode = 0;
9515
f21f165e 9516 vmx->loaded_vmcs = &vmx->vmcs01;
15ad7146
AK
9517 cpu = get_cpu();
9518 vmx_vcpu_load(&vmx->vcpu, cpu);
e48672fa 9519 vmx->vcpu.cpu = cpu;
12d79917 9520 vmx_vcpu_setup(vmx);
fb3f0f51 9521 vmx_vcpu_put(&vmx->vcpu);
15ad7146 9522 put_cpu();
35754c98 9523 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
be6d05cf
JK
9524 err = alloc_apic_access_page(kvm);
9525 if (err)
5e4a0b3c 9526 goto free_vmcs;
a63cb560 9527 }
fb3f0f51 9528
e90008df 9529 if (enable_ept && !enable_unrestricted_guest) {
f51770ed
TC
9530 err = init_rmode_identity_map(kvm);
9531 if (err)
93ea5388 9532 goto free_vmcs;
b927a3ce 9533 }
b7ebfb05 9534
63aff655 9535 if (nested)
6677f3da 9536 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
7caaa711 9537 vmx_capability.ept,
6677f3da 9538 kvm_vcpu_apicv_active(&vmx->vcpu));
b9c237bb 9539
705699a1 9540 vmx->nested.posted_intr_nv = -1;
a9d30f33 9541 vmx->nested.current_vmptr = -1ull;
a9d30f33 9542
37e4c997
HZ
9543 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9544
31afb2ea
PB
9545 /*
9546 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9547 * or POSTED_INTR_WAKEUP_VECTOR.
9548 */
9549 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9550 vmx->pi_desc.sn = 1;
9551
fb3f0f51
RR
9552 return &vmx->vcpu;
9553
9554free_vmcs:
5f3fbc34 9555 free_loaded_vmcs(vmx->loaded_vmcs);
fb3f0f51 9556free_msrs:
fb3f0f51 9557 kfree(vmx->guest_msrs);
4e59516a
PF
9558free_pml:
9559 vmx_destroy_pml_buffer(vmx);
fb3f0f51
RR
9560uninit_vcpu:
9561 kvm_vcpu_uninit(&vmx->vcpu);
9562free_vcpu:
991e7a0e 9563 free_vpid(vmx->vpid);
a4770347 9564 kmem_cache_free(kvm_vcpu_cache, vmx);
fb3f0f51 9565 return ERR_PTR(err);
6aa8b732
AK
9566}
9567
d90a7a0e
JK
9568#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
9569#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
26acfb66 9570
b31c114b
WL
9571static int vmx_vm_init(struct kvm *kvm)
9572{
877ad952
TL
9573 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
9574
b31c114b
WL
9575 if (!ple_gap)
9576 kvm->arch.pause_in_guest = true;
26acfb66 9577
d90a7a0e
JK
9578 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
9579 switch (l1tf_mitigation) {
9580 case L1TF_MITIGATION_OFF:
9581 case L1TF_MITIGATION_FLUSH_NOWARN:
9582 /* 'I explicitly don't care' is set */
9583 break;
9584 case L1TF_MITIGATION_FLUSH:
9585 case L1TF_MITIGATION_FLUSH_NOSMT:
9586 case L1TF_MITIGATION_FULL:
9587 /*
9588 * Warn upon starting the first VM in a potentially
9589 * insecure environment.
9590 */
9591 if (cpu_smt_control == CPU_SMT_ENABLED)
9592 pr_warn_once(L1TF_MSG_SMT);
9593 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
9594 pr_warn_once(L1TF_MSG_L1D);
9595 break;
9596 case L1TF_MITIGATION_FULL_FORCE:
9597 /* Flush is enforced */
9598 break;
26acfb66 9599 }
26acfb66 9600 }
b31c114b
WL
9601 return 0;
9602}
9603
002c7f7c
YS
9604static void __init vmx_check_processor_compat(void *rtn)
9605{
9606 struct vmcs_config vmcs_conf;
7caaa711 9607 struct vmx_capability vmx_cap;
002c7f7c
YS
9608
9609 *(int *)rtn = 0;
7caaa711 9610 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
002c7f7c 9611 *(int *)rtn = -EIO;
7caaa711 9612 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept, enable_apicv);
002c7f7c
YS
9613 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9614 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9615 smp_processor_id());
9616 *(int *)rtn = -EIO;
9617 }
9618}
9619
4b12f0de 9620static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
64d4d521 9621{
b18d5431
XG
9622 u8 cache;
9623 u64 ipat = 0;
4b12f0de 9624
522c68c4 9625 /* For VT-d and EPT combination
606decd6 9626 * 1. MMIO: always map as UC
522c68c4
SY
9627 * 2. EPT with VT-d:
9628 * a. VT-d without snooping control feature: can't guarantee the
606decd6 9629 * result, try to trust guest.
522c68c4
SY
9630 * b. VT-d with snooping control feature: snooping control feature of
9631 * VT-d engine can guarantee the cache correctness. Just set it
9632 * to WB to keep consistent with host. So the same as item 3.
a19a6d11 9633 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
522c68c4
SY
9634 * consistent with host MTRR
9635 */
606decd6
PB
9636 if (is_mmio) {
9637 cache = MTRR_TYPE_UNCACHABLE;
9638 goto exit;
9639 }
9640
9641 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
b18d5431
XG
9642 ipat = VMX_EPT_IPAT_BIT;
9643 cache = MTRR_TYPE_WRBACK;
9644 goto exit;
9645 }
9646
9647 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9648 ipat = VMX_EPT_IPAT_BIT;
0da029ed 9649 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
fb279950
XG
9650 cache = MTRR_TYPE_WRBACK;
9651 else
9652 cache = MTRR_TYPE_UNCACHABLE;
b18d5431
XG
9653 goto exit;
9654 }
9655
ff53604b 9656 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
b18d5431
XG
9657
9658exit:
9659 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
64d4d521
SY
9660}
9661
17cc3935 9662static int vmx_get_lpage_level(void)
344f414f 9663{
878403b7
SY
9664 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9665 return PT_DIRECTORY_LEVEL;
9666 else
9667 /* For shadow and EPT supported 1GB page */
9668 return PT_PDPE_LEVEL;
344f414f
JR
9669}
9670
feda805f
XG
9671static void vmcs_set_secondary_exec_control(u32 new_ctl)
9672{
9673 /*
9674 * These bits in the secondary execution controls field
9675 * are dynamic, the others are mostly based on the hypervisor
9676 * architecture and the guest's CPUID. Do not touch the
9677 * dynamic bits.
9678 */
9679 u32 mask =
9680 SECONDARY_EXEC_SHADOW_VMCS |
9681 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
0367f205
PB
9682 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9683 SECONDARY_EXEC_DESC;
feda805f
XG
9684
9685 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9686
9687 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9688 (new_ctl & ~mask) | (cur_ctl & mask));
9689}
9690
8322ebbb
DM
9691/*
9692 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9693 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9694 */
9695static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9696{
9697 struct vcpu_vmx *vmx = to_vmx(vcpu);
9698 struct kvm_cpuid_entry2 *entry;
9699
6677f3da
PB
9700 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
9701 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
8322ebbb
DM
9702
9703#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9704 if (entry && (entry->_reg & (_cpuid_mask))) \
6677f3da 9705 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
8322ebbb
DM
9706} while (0)
9707
9708 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9709 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9710 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9711 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9712 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9713 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9714 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9715 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9716 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9717 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9718 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9719 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9720 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9721 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9722 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9723
9724 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9725 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9726 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9727 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9728 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
c4ad77e0 9729 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
8322ebbb
DM
9730
9731#undef cr4_fixed1_update
9732}
9733
5f76f6f5
LA
9734static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
9735{
9736 struct vcpu_vmx *vmx = to_vmx(vcpu);
9737
9738 if (kvm_mpx_supported()) {
9739 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
9740
9741 if (mpx_enabled) {
9742 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
9743 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
9744 } else {
9745 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
9746 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
9747 }
9748 }
9749}
9750
0e851880
SY
9751static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9752{
4e47c7a6 9753 struct vcpu_vmx *vmx = to_vmx(vcpu);
4e47c7a6 9754
80154d77
PB
9755 if (cpu_has_secondary_exec_ctrls()) {
9756 vmx_compute_secondary_exec_control(vmx);
9757 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
ad756a16 9758 }
8b3e34e4 9759
37e4c997
HZ
9760 if (nested_vmx_allowed(vcpu))
9761 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9762 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9763 else
9764 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9765 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
8322ebbb 9766
5f76f6f5 9767 if (nested_vmx_allowed(vcpu)) {
8322ebbb 9768 nested_vmx_cr_fixed1_bits_update(vcpu);
5f76f6f5
LA
9769 nested_vmx_entry_exit_ctls_update(vcpu);
9770 }
0e851880
SY
9771}
9772
d4330ef2
JR
9773static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9774{
7b8050f5
NHE
9775 if (func == 1 && nested)
9776 entry->ecx |= bit(X86_FEATURE_VMX);
d4330ef2
JR
9777}
9778
25d92081
YZ
9779static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9780 struct x86_exception *fault)
9781{
533558bc 9782 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
c5f983f6 9783 struct vcpu_vmx *vmx = to_vmx(vcpu);
533558bc 9784 u32 exit_reason;
c5f983f6 9785 unsigned long exit_qualification = vcpu->arch.exit_qualification;
25d92081 9786
c5f983f6
BD
9787 if (vmx->nested.pml_full) {
9788 exit_reason = EXIT_REASON_PML_FULL;
9789 vmx->nested.pml_full = false;
9790 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
9791 } else if (fault->error_code & PFERR_RSVD_MASK)
533558bc 9792 exit_reason = EXIT_REASON_EPT_MISCONFIG;
25d92081 9793 else
533558bc 9794 exit_reason = EXIT_REASON_EPT_VIOLATION;
c5f983f6
BD
9795
9796 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
25d92081
YZ
9797 vmcs12->guest_physical_address = fault->address;
9798}
9799
995f00a6
PF
9800static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
9801{
bb97a016 9802 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
995f00a6
PF
9803}
9804
155a97a3
NHE
9805/* Callbacks for nested_ept_init_mmu_context: */
9806
9807static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9808{
9809 /* return the page table to be shadowed - in our case, EPT12 */
9810 return get_vmcs12(vcpu)->ept_pointer;
9811}
9812
5b8ba41d 9813static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
155a97a3 9814{
ad896af0 9815 WARN_ON(mmu_is_nested(vcpu));
ae1e2d10 9816
14c07ad8 9817 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
ad896af0 9818 kvm_init_shadow_ept_mmu(vcpu,
6677f3da 9819 to_vmx(vcpu)->nested.msrs.ept_caps &
ae1e2d10 9820 VMX_EPT_EXECUTE_ONLY_BIT,
50c28f21
JS
9821 nested_ept_ad_enabled(vcpu),
9822 nested_ept_get_cr3(vcpu));
44dd3ffa
VK
9823 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
9824 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
9825 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
3dc773e7 9826 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
155a97a3
NHE
9827
9828 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
155a97a3
NHE
9829}
9830
9831static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9832{
14c07ad8 9833 vcpu->arch.mmu = &vcpu->arch.root_mmu;
44dd3ffa 9834 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
155a97a3
NHE
9835}
9836
19d5f10b
EK
9837static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9838 u16 error_code)
9839{
9840 bool inequality, bit;
9841
9842 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9843 inequality =
9844 (error_code & vmcs12->page_fault_error_code_mask) !=
9845 vmcs12->page_fault_error_code_match;
9846 return inequality ^ bit;
9847}
9848
feaf0c7d
GN
9849static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9850 struct x86_exception *fault)
9851{
9852 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9853
9854 WARN_ON(!is_guest_mode(vcpu));
9855
305d0ab4
WL
9856 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
9857 !to_vmx(vcpu)->nested.nested_run_pending) {
b96fb439
PB
9858 vmcs12->vm_exit_intr_error_code = fault->error_code;
9859 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9860 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
9861 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
9862 fault->address);
7313c698 9863 } else {
feaf0c7d 9864 kvm_inject_page_fault(vcpu, fault);
7313c698 9865 }
feaf0c7d
GN
9866}
9867
c992384b
PB
9868static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
9869 struct vmcs12 *vmcs12);
6beb7bd5 9870
7f7f1ba3 9871static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
a2bcba50 9872{
7f7f1ba3 9873 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
a2bcba50 9874 struct vcpu_vmx *vmx = to_vmx(vcpu);
5e2f30b7 9875 struct page *page;
6beb7bd5 9876 u64 hpa;
a2bcba50
WL
9877
9878 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
a2bcba50
WL
9879 /*
9880 * Translate L1 physical address to host physical
9881 * address for vmcs02. Keep the page pinned, so this
9882 * physical address remains valid. We keep a reference
9883 * to it so we can release it later.
9884 */
5e2f30b7 9885 if (vmx->nested.apic_access_page) { /* shouldn't happen */
53a70daf 9886 kvm_release_page_dirty(vmx->nested.apic_access_page);
5e2f30b7
DH
9887 vmx->nested.apic_access_page = NULL;
9888 }
9889 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
6beb7bd5
JM
9890 /*
9891 * If translation failed, no matter: This feature asks
9892 * to exit when accessing the given address, and if it
9893 * can never be accessed, this feature won't do
9894 * anything anyway.
9895 */
5e2f30b7
DH
9896 if (!is_error_page(page)) {
9897 vmx->nested.apic_access_page = page;
6beb7bd5
JM
9898 hpa = page_to_phys(vmx->nested.apic_access_page);
9899 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9900 } else {
9901 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9902 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9903 }
a2bcba50 9904 }
a7c0b07d
WL
9905
9906 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5e2f30b7 9907 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
53a70daf 9908 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
5e2f30b7
DH
9909 vmx->nested.virtual_apic_page = NULL;
9910 }
9911 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
a7c0b07d
WL
9912
9913 /*
6beb7bd5
JM
9914 * If translation failed, VM entry will fail because
9915 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9916 * Failing the vm entry is _not_ what the processor
9917 * does but it's basically the only possibility we
9918 * have. We could still enter the guest if CR8 load
9919 * exits are enabled, CR8 store exits are enabled, and
9920 * virtualize APIC access is disabled; in this case
9921 * the processor would never use the TPR shadow and we
9922 * could simply clear the bit from the execution
9923 * control. But such a configuration is useless, so
9924 * let's keep the code simple.
a7c0b07d 9925 */
5e2f30b7
DH
9926 if (!is_error_page(page)) {
9927 vmx->nested.virtual_apic_page = page;
6beb7bd5
JM
9928 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9929 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9930 }
a7c0b07d
WL
9931 }
9932
705699a1 9933 if (nested_cpu_has_posted_intr(vmcs12)) {
705699a1
WV
9934 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9935 kunmap(vmx->nested.pi_desc_page);
53a70daf 9936 kvm_release_page_dirty(vmx->nested.pi_desc_page);
5e2f30b7 9937 vmx->nested.pi_desc_page = NULL;
705699a1 9938 }
5e2f30b7
DH
9939 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
9940 if (is_error_page(page))
6beb7bd5 9941 return;
5e2f30b7
DH
9942 vmx->nested.pi_desc_page = page;
9943 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
705699a1
WV
9944 vmx->nested.pi_desc =
9945 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9946 (unsigned long)(vmcs12->posted_intr_desc_addr &
9947 (PAGE_SIZE - 1)));
6beb7bd5
JM
9948 vmcs_write64(POSTED_INTR_DESC_ADDR,
9949 page_to_phys(vmx->nested.pi_desc_page) +
9950 (unsigned long)(vmcs12->posted_intr_desc_addr &
9951 (PAGE_SIZE - 1)));
705699a1 9952 }
d4667ca1 9953 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
3712caeb
KA
9954 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
9955 CPU_BASED_USE_MSR_BITMAPS);
6beb7bd5
JM
9956 else
9957 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
9958 CPU_BASED_USE_MSR_BITMAPS);
a2bcba50
WL
9959}
9960
f4124500
JK
9961static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9962{
9963 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9964 struct vcpu_vmx *vmx = to_vmx(vcpu);
9965
4c008127
SC
9966 /*
9967 * A timer value of zero is architecturally guaranteed to cause
9968 * a VMExit prior to executing any instructions in the guest.
9969 */
9970 if (preemption_timeout == 0) {
f4124500
JK
9971 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9972 return;
9973 }
9974
4c008127
SC
9975 if (vcpu->arch.virtual_tsc_khz == 0)
9976 return;
9977
f4124500
JK
9978 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9979 preemption_timeout *= 1000000;
9980 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9981 hrtimer_start(&vmx->nested.preemption_timer,
9982 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9983}
9984
56a20510
JM
9985static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
9986 struct vmcs12 *vmcs12)
9987{
9988 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
9989 return 0;
9990
9991 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
9992 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
9993 return -EINVAL;
9994
9995 return 0;
9996}
9997
3af18d9c
WV
9998static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9999 struct vmcs12 *vmcs12)
10000{
3af18d9c
WV
10001 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10002 return 0;
10003
5fa99cbe 10004 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
3af18d9c
WV
10005 return -EINVAL;
10006
10007 return 0;
10008}
10009
712b12d7
JM
10010static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10011 struct vmcs12 *vmcs12)
10012{
10013 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10014 return 0;
10015
10016 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10017 return -EINVAL;
10018
10019 return 0;
10020}
10021
3af18d9c
WV
10022/*
10023 * Merge L0's and L1's MSR bitmap, return false to indicate that
10024 * we do not use the hardware.
10025 */
c992384b
PB
10026static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10027 struct vmcs12 *vmcs12)
3af18d9c 10028{
82f0dd4b 10029 int msr;
f2b93280 10030 struct page *page;
d048c098 10031 unsigned long *msr_bitmap_l1;
904e14fb 10032 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
15d45071 10033 /*
d28b387f 10034 * pred_cmd & spec_ctrl are trying to verify two things:
15d45071
AR
10035 *
10036 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10037 * ensures that we do not accidentally generate an L02 MSR bitmap
10038 * from the L12 MSR bitmap that is too permissive.
10039 * 2. That L1 or L2s have actually used the MSR. This avoids
10040 * unnecessarily merging of the bitmap if the MSR is unused. This
10041 * works properly because we only update the L01 MSR bitmap lazily.
10042 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10043 * updated to reflect this when L1 (or its L2s) actually write to
10044 * the MSR.
10045 */
206587a9
KA
10046 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10047 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
f2b93280 10048
c992384b
PB
10049 /* Nothing to do if the MSR bitmap is not in use. */
10050 if (!cpu_has_vmx_msr_bitmap() ||
10051 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10052 return false;
10053
15d45071 10054 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
d28b387f 10055 !pred_cmd && !spec_ctrl)
f2b93280
WV
10056 return false;
10057
5e2f30b7
DH
10058 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10059 if (is_error_page(page))
f2b93280 10060 return false;
f2b93280 10061
c992384b
PB
10062 msr_bitmap_l1 = (unsigned long *)kmap(page);
10063 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10064 /*
10065 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10066 * just lets the processor take the value from the virtual-APIC page;
10067 * take those 256 bits directly from the L1 bitmap.
10068 */
10069 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10070 unsigned word = msr / BITS_PER_LONG;
10071 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10072 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10073 }
10074 } else {
10075 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10076 unsigned word = msr / BITS_PER_LONG;
10077 msr_bitmap_l0[word] = ~0;
10078 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10079 }
10080 }
d048c098 10081
c992384b
PB
10082 nested_vmx_disable_intercept_for_msr(
10083 msr_bitmap_l1, msr_bitmap_l0,
d7231e75 10084 X2APIC_MSR(APIC_TASKPRI),
c992384b 10085 MSR_TYPE_W);
d048c098 10086
c992384b 10087 if (nested_cpu_has_vid(vmcs12)) {
d048c098 10088 nested_vmx_disable_intercept_for_msr(
c992384b 10089 msr_bitmap_l1, msr_bitmap_l0,
d7231e75 10090 X2APIC_MSR(APIC_EOI),
c992384b
PB
10091 MSR_TYPE_W);
10092 nested_vmx_disable_intercept_for_msr(
10093 msr_bitmap_l1, msr_bitmap_l0,
d7231e75 10094 X2APIC_MSR(APIC_SELF_IPI),
c992384b 10095 MSR_TYPE_W);
82f0dd4b 10096 }
15d45071 10097
d28b387f
KA
10098 if (spec_ctrl)
10099 nested_vmx_disable_intercept_for_msr(
10100 msr_bitmap_l1, msr_bitmap_l0,
10101 MSR_IA32_SPEC_CTRL,
10102 MSR_TYPE_R | MSR_TYPE_W);
10103
15d45071
AR
10104 if (pred_cmd)
10105 nested_vmx_disable_intercept_for_msr(
10106 msr_bitmap_l1, msr_bitmap_l0,
10107 MSR_IA32_PRED_CMD,
10108 MSR_TYPE_W);
10109
f2b93280 10110 kunmap(page);
53a70daf 10111 kvm_release_page_clean(page);
f2b93280
WV
10112
10113 return true;
10114}
10115
61ada748
LA
10116static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
10117 struct vmcs12 *vmcs12)
10118{
10119 struct vmcs12 *shadow;
10120 struct page *page;
10121
10122 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
10123 vmcs12->vmcs_link_pointer == -1ull)
10124 return;
10125
10126 shadow = get_shadow_vmcs12(vcpu);
10127 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
10128
10129 memcpy(shadow, kmap(page), VMCS12_SIZE);
10130
10131 kunmap(page);
10132 kvm_release_page_clean(page);
10133}
10134
10135static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
10136 struct vmcs12 *vmcs12)
10137{
10138 struct vcpu_vmx *vmx = to_vmx(vcpu);
10139
10140 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
10141 vmcs12->vmcs_link_pointer == -1ull)
10142 return;
10143
10144 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
10145 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
10146}
10147
f0f4cf5b
KS
10148static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10149 struct vmcs12 *vmcs12)
10150{
10151 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10152 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10153 return -EINVAL;
10154 else
10155 return 0;
10156}
10157
f2b93280
WV
10158static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10159 struct vmcs12 *vmcs12)
10160{
82f0dd4b 10161 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
608406e2 10162 !nested_cpu_has_apic_reg_virt(vmcs12) &&
705699a1
WV
10163 !nested_cpu_has_vid(vmcs12) &&
10164 !nested_cpu_has_posted_intr(vmcs12))
f2b93280
WV
10165 return 0;
10166
10167 /*
10168 * If virtualize x2apic mode is enabled,
10169 * virtualize apic access must be disabled.
10170 */
82f0dd4b
WV
10171 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10172 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
f2b93280
WV
10173 return -EINVAL;
10174
608406e2
WV
10175 /*
10176 * If virtual interrupt delivery is enabled,
10177 * we must exit on external interrupts.
10178 */
10179 if (nested_cpu_has_vid(vmcs12) &&
10180 !nested_exit_on_intr(vcpu))
10181 return -EINVAL;
10182
705699a1
WV
10183 /*
10184 * bits 15:8 should be zero in posted_intr_nv,
10185 * the descriptor address has been already checked
10186 * in nested_get_vmcs12_pages.
6de84e58
KS
10187 *
10188 * bits 5:0 of posted_intr_desc_addr should be zero.
705699a1
WV
10189 */
10190 if (nested_cpu_has_posted_intr(vmcs12) &&
10191 (!nested_cpu_has_vid(vmcs12) ||
10192 !nested_exit_intr_ack_set(vcpu) ||
6de84e58
KS
10193 (vmcs12->posted_intr_nv & 0xff00) ||
10194 (vmcs12->posted_intr_desc_addr & 0x3f) ||
22a7cdca 10195 (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
705699a1
WV
10196 return -EINVAL;
10197
f2b93280
WV
10198 /* tpr shadow is needed by all apicv features. */
10199 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10200 return -EINVAL;
10201
10202 return 0;
3af18d9c
WV
10203}
10204
e9ac033e
EK
10205static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10206 unsigned long count_field,
92d71bc6 10207 unsigned long addr_field)
ff651cb6 10208{
e2536742 10209 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
92d71bc6 10210 int maxphyaddr;
e9ac033e
EK
10211 u64 count, addr;
10212
e2536742
LA
10213 if (vmcs12_read_any(vmcs12, count_field, &count) ||
10214 vmcs12_read_any(vmcs12, addr_field, &addr)) {
e9ac033e
EK
10215 WARN_ON(1);
10216 return -EINVAL;
10217 }
10218 if (count == 0)
10219 return 0;
92d71bc6 10220 maxphyaddr = cpuid_maxphyaddr(vcpu);
e9ac033e
EK
10221 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10222 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
bbe41b95 10223 pr_debug_ratelimited(
e9ac033e
EK
10224 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10225 addr_field, maxphyaddr, count, addr);
10226 return -EINVAL;
10227 }
10228 return 0;
10229}
10230
10231static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10232 struct vmcs12 *vmcs12)
10233{
e9ac033e
EK
10234 if (vmcs12->vm_exit_msr_load_count == 0 &&
10235 vmcs12->vm_exit_msr_store_count == 0 &&
10236 vmcs12->vm_entry_msr_load_count == 0)
10237 return 0; /* Fast path */
e9ac033e 10238 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
92d71bc6 10239 VM_EXIT_MSR_LOAD_ADDR) ||
e9ac033e 10240 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
92d71bc6 10241 VM_EXIT_MSR_STORE_ADDR) ||
e9ac033e 10242 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
92d71bc6 10243 VM_ENTRY_MSR_LOAD_ADDR))
e9ac033e
EK
10244 return -EINVAL;
10245 return 0;
10246}
10247
c5f983f6
BD
10248static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10249 struct vmcs12 *vmcs12)
10250{
55c1dcd8
KS
10251 if (!nested_cpu_has_pml(vmcs12))
10252 return 0;
c5f983f6 10253
55c1dcd8
KS
10254 if (!nested_cpu_has_ept(vmcs12) ||
10255 !page_address_valid(vcpu, vmcs12->pml_address))
10256 return -EINVAL;
c5f983f6
BD
10257
10258 return 0;
10259}
10260
88656040
JM
10261static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
10262 struct vmcs12 *vmcs12)
10263{
10264 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
10265 !nested_cpu_has_ept(vmcs12))
10266 return -EINVAL;
10267 return 0;
10268}
10269
10270static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
10271 struct vmcs12 *vmcs12)
10272{
10273 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
10274 !nested_cpu_has_ept(vmcs12))
10275 return -EINVAL;
10276 return 0;
10277}
10278
a8a7c02b
LA
10279static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
10280 struct vmcs12 *vmcs12)
10281{
10282 if (!nested_cpu_has_shadow_vmcs(vmcs12))
10283 return 0;
10284
10285 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
10286 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
10287 return -EINVAL;
10288
10289 return 0;
10290}
10291
e9ac033e
EK
10292static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10293 struct vmx_msr_entry *e)
10294{
10295 /* x2APIC MSR accesses are not allowed */
8a9781f7 10296 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
e9ac033e
EK
10297 return -EINVAL;
10298 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10299 e->index == MSR_IA32_UCODE_REV)
10300 return -EINVAL;
10301 if (e->reserved != 0)
ff651cb6
WV
10302 return -EINVAL;
10303 return 0;
10304}
10305
e9ac033e
EK
10306static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10307 struct vmx_msr_entry *e)
ff651cb6
WV
10308{
10309 if (e->index == MSR_FS_BASE ||
10310 e->index == MSR_GS_BASE ||
e9ac033e
EK
10311 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10312 nested_vmx_msr_check_common(vcpu, e))
10313 return -EINVAL;
10314 return 0;
10315}
10316
10317static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10318 struct vmx_msr_entry *e)
10319{
10320 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10321 nested_vmx_msr_check_common(vcpu, e))
ff651cb6
WV
10322 return -EINVAL;
10323 return 0;
10324}
10325
10326/*
10327 * Load guest's/host's msr at nested entry/exit.
10328 * return 0 for success, entry index for failure.
10329 */
10330static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10331{
10332 u32 i;
10333 struct vmx_msr_entry e;
10334 struct msr_data msr;
10335
10336 msr.host_initiated = false;
10337 for (i = 0; i < count; i++) {
54bf36aa
PB
10338 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10339 &e, sizeof(e))) {
bbe41b95 10340 pr_debug_ratelimited(
e9ac033e
EK
10341 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10342 __func__, i, gpa + i * sizeof(e));
ff651cb6 10343 goto fail;
e9ac033e
EK
10344 }
10345 if (nested_vmx_load_msr_check(vcpu, &e)) {
bbe41b95 10346 pr_debug_ratelimited(
e9ac033e
EK
10347 "%s check failed (%u, 0x%x, 0x%x)\n",
10348 __func__, i, e.index, e.reserved);
10349 goto fail;
10350 }
ff651cb6
WV
10351 msr.index = e.index;
10352 msr.data = e.value;
e9ac033e 10353 if (kvm_set_msr(vcpu, &msr)) {
bbe41b95 10354 pr_debug_ratelimited(
e9ac033e
EK
10355 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10356 __func__, i, e.index, e.value);
ff651cb6 10357 goto fail;
e9ac033e 10358 }
ff651cb6
WV
10359 }
10360 return 0;
10361fail:
10362 return i + 1;
10363}
10364
10365static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10366{
10367 u32 i;
10368 struct vmx_msr_entry e;
10369
10370 for (i = 0; i < count; i++) {
609e36d3 10371 struct msr_data msr_info;
54bf36aa
PB
10372 if (kvm_vcpu_read_guest(vcpu,
10373 gpa + i * sizeof(e),
10374 &e, 2 * sizeof(u32))) {
bbe41b95 10375 pr_debug_ratelimited(
e9ac033e
EK
10376 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10377 __func__, i, gpa + i * sizeof(e));
ff651cb6 10378 return -EINVAL;
e9ac033e
EK
10379 }
10380 if (nested_vmx_store_msr_check(vcpu, &e)) {
bbe41b95 10381 pr_debug_ratelimited(
e9ac033e
EK
10382 "%s check failed (%u, 0x%x, 0x%x)\n",
10383 __func__, i, e.index, e.reserved);
ff651cb6 10384 return -EINVAL;
e9ac033e 10385 }
609e36d3
PB
10386 msr_info.host_initiated = false;
10387 msr_info.index = e.index;
10388 if (kvm_get_msr(vcpu, &msr_info)) {
bbe41b95 10389 pr_debug_ratelimited(
e9ac033e
EK
10390 "%s cannot read MSR (%u, 0x%x)\n",
10391 __func__, i, e.index);
10392 return -EINVAL;
10393 }
54bf36aa
PB
10394 if (kvm_vcpu_write_guest(vcpu,
10395 gpa + i * sizeof(e) +
10396 offsetof(struct vmx_msr_entry, value),
10397 &msr_info.data, sizeof(msr_info.data))) {
bbe41b95 10398 pr_debug_ratelimited(
e9ac033e 10399 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
609e36d3 10400 __func__, i, e.index, msr_info.data);
e9ac033e
EK
10401 return -EINVAL;
10402 }
ff651cb6
WV
10403 }
10404 return 0;
10405}
10406
1dc35dac
LP
10407static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10408{
10409 unsigned long invalid_mask;
10410
10411 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10412 return (val & invalid_mask) == 0;
10413}
10414
9ed38ffa
LP
10415/*
10416 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10417 * emulating VM entry into a guest with EPT enabled.
10418 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10419 * is assigned to entry_failure_code on failure.
10420 */
10421static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
ca0bde28 10422 u32 *entry_failure_code)
9ed38ffa 10423{
9ed38ffa 10424 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
1dc35dac 10425 if (!nested_cr3_valid(vcpu, cr3)) {
9ed38ffa
LP
10426 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10427 return 1;
10428 }
10429
10430 /*
10431 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10432 * must not be dereferenced.
10433 */
10434 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10435 !nested_ept) {
10436 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10437 *entry_failure_code = ENTRY_FAIL_PDPTE;
10438 return 1;
10439 }
10440 }
9ed38ffa
LP
10441 }
10442
50c28f21 10443 if (!nested_ept)
ade61e28 10444 kvm_mmu_new_cr3(vcpu, cr3, false);
50c28f21
JS
10445
10446 vcpu->arch.cr3 = cr3;
10447 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10448
10449 kvm_init_mmu(vcpu, false);
10450
9ed38ffa
LP
10451 return 0;
10452}
10453
efebf0aa
LA
10454/*
10455 * Returns if KVM is able to config CPU to tag TLB entries
10456 * populated by L2 differently than TLB entries populated
10457 * by L1.
10458 *
10459 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
10460 *
10461 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
10462 * with different VPID (L1 entries are tagged with vmx->vpid
10463 * while L2 entries are tagged with vmx->nested.vpid02).
10464 */
10465static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
10466{
10467 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
fe3ef05c 10468
efebf0aa
LA
10469 return nested_cpu_has_ept(vmcs12) ||
10470 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
10471}
25a2e4fe 10472
3df5c37e
SC
10473static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
10474{
10475 if (vmx->nested.nested_run_pending &&
10476 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
10477 return vmcs12->guest_ia32_efer;
10478 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10479 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
10480 else
10481 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
10482}
25a2e4fe 10483
09abe320 10484static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
fe3ef05c 10485{
09abe320 10486 /*
9d6105b2 10487 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
09abe320
SC
10488 * according to L0's settings (vmcs12 is irrelevant here). Host
10489 * fields that come from L0 and are not constant, e.g. HOST_CR3,
10490 * will be set as needed prior to VMLAUNCH/VMRESUME.
10491 */
9d6105b2 10492 if (vmx->nested.vmcs02_initialized)
09abe320 10493 return;
9d6105b2 10494 vmx->nested.vmcs02_initialized = true;
25a2e4fe
PB
10495
10496 /*
52017608
SC
10497 * We don't care what the EPTP value is we just need to guarantee
10498 * it's valid so we don't get a false positive when doing early
10499 * consistency checks.
25a2e4fe 10500 */
52017608
SC
10501 if (enable_ept && nested_early_check)
10502 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
25a2e4fe
PB
10503
10504 /* All VMFUNCs are currently emulated through L0 vmexits. */
10505 if (cpu_has_vmx_vmfunc())
10506 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10507
09abe320
SC
10508 if (cpu_has_vmx_posted_intr())
10509 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
25a2e4fe 10510
09abe320
SC
10511 if (cpu_has_vmx_msr_bitmap())
10512 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10513
10514 if (enable_pml)
10515 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
25a2e4fe
PB
10516
10517 /*
09abe320
SC
10518 * Set the MSR load/store lists to match L0's settings. Only the
10519 * addresses are constant (for vmcs02), the counts can change based
10520 * on L2's behavior, e.g. switching to/from long mode.
25a2e4fe
PB
10521 */
10522 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
33966dd6 10523 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
33966dd6 10524 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
25a2e4fe 10525
09abe320
SC
10526 vmx_set_constant_host_state(vmx);
10527}
25a2e4fe 10528
09abe320
SC
10529static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
10530 struct vmcs12 *vmcs12)
10531{
10532 prepare_vmcs02_constant_state(vmx);
10533
10534 vmcs_write64(VMCS_LINK_POINTER, -1ull);
25a2e4fe
PB
10535
10536 if (enable_vpid) {
10537 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10538 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10539 else
10540 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10541 }
8665c3f9
PB
10542}
10543
09abe320 10544static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
8665c3f9 10545{
8665c3f9 10546 u32 exec_control, vmcs12_exec_ctrl;
09abe320 10547 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
8665c3f9 10548
945679e3 10549 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
09abe320 10550 prepare_vmcs02_early_full(vmx, vmcs12);
9d1887ef 10551
8665c3f9 10552 /*
09abe320
SC
10553 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10554 * entry, but only if the current (host) sp changed from the value
10555 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10556 * if we switch vmcs, and rather than hold a separate cache per vmcs,
52017608
SC
10557 * here we just force the write to happen on entry. host_rsp will
10558 * also be written unconditionally by nested_vmx_check_vmentry_hw()
10559 * if we are doing early consistency checks via hardware.
8665c3f9 10560 */
09abe320 10561 vmx->host_rsp = 0;
8665c3f9 10562
09abe320
SC
10563 /*
10564 * PIN CONTROLS
10565 */
f4124500 10566 exec_control = vmcs12->pin_based_vm_exec_control;
9314006d 10567
f459a707 10568 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
9314006d 10569 exec_control |= vmcs_config.pin_based_exec_ctrl;
f459a707
SC
10570 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10571 vmx->loaded_vmcs->hv_timer_armed = false;
705699a1 10572
9314006d 10573 /* Posted interrupts setting is only taken from vmcs12. */
705699a1 10574 if (nested_cpu_has_posted_intr(vmcs12)) {
705699a1
WV
10575 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10576 vmx->nested.pi_pending = false;
6beb7bd5 10577 } else {
705699a1 10578 exec_control &= ~PIN_BASED_POSTED_INTR;
6beb7bd5 10579 }
f4124500 10580 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
fe3ef05c 10581
09abe320
SC
10582 /*
10583 * EXEC CONTROLS
10584 */
10585 exec_control = vmx_exec_control(vmx); /* L0's desires */
10586 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10587 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10588 exec_control &= ~CPU_BASED_TPR_SHADOW;
10589 exec_control |= vmcs12->cpu_based_vm_exec_control;
10590
10591 /*
10592 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10593 * nested_get_vmcs12_pages can't fix it up, the illegal value
10594 * will result in a VM entry failure.
10595 */
10596 if (exec_control & CPU_BASED_TPR_SHADOW) {
10597 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
10598 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
10599 } else {
10600#ifdef CONFIG_X86_64
10601 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10602 CPU_BASED_CR8_STORE_EXITING;
10603#endif
10604 }
10605
10606 /*
10607 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
10608 * for I/O port accesses.
10609 */
10610 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10611 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10612 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
0238ea91 10613
09abe320
SC
10614 /*
10615 * SECONDARY EXEC CONTROLS
10616 */
fe3ef05c 10617 if (cpu_has_secondary_exec_ctrls()) {
80154d77 10618 exec_control = vmx->secondary_exec_control;
e2821620 10619
fe3ef05c 10620 /* Take the following fields only from vmcs12 */
696dfd95 10621 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
90a2db6d 10622 SECONDARY_EXEC_ENABLE_INVPCID |
b3a2a907 10623 SECONDARY_EXEC_RDTSCP |
3db13480 10624 SECONDARY_EXEC_XSAVES |
696dfd95 10625 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
27c42a1b
BD
10626 SECONDARY_EXEC_APIC_REGISTER_VIRT |
10627 SECONDARY_EXEC_ENABLE_VMFUNC);
fe3ef05c 10628 if (nested_cpu_has(vmcs12,
03efce6f
BD
10629 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
10630 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
10631 ~SECONDARY_EXEC_ENABLE_PML;
10632 exec_control |= vmcs12_exec_ctrl;
10633 }
fe3ef05c 10634
32c7acf0
LA
10635 /* VMCS shadowing for L2 is emulated for now */
10636 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
10637
25a2e4fe 10638 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
608406e2
WV
10639 vmcs_write16(GUEST_INTR_STATUS,
10640 vmcs12->guest_intr_status);
608406e2 10641
6beb7bd5
JM
10642 /*
10643 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10644 * nested_get_vmcs12_pages will either fix it up or
10645 * remove the VM execution control.
10646 */
10647 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10648 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10649
0b665d30
SC
10650 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
10651 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
10652
fe3ef05c
NHE
10653 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10654 }
10655
fe3ef05c 10656 /*
09abe320
SC
10657 * ENTRY CONTROLS
10658 *
10659 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
10660 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
10661 * on the related bits (if supported by the CPU) in the hope that
10662 * we can avoid VMWrites during vmx_set_efer().
10663 */
c73da3fc 10664 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
09abe320 10665 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
c73da3fc 10666 if (cpu_has_load_ia32_efer()) {
09abe320
SC
10667 if (guest_efer & EFER_LMA)
10668 exec_control |= VM_ENTRY_IA32E_MODE;
10669 if (guest_efer != host_efer)
10670 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
10671 }
10672 vm_entry_controls_init(vmx, exec_control);
10673
10674 /*
10675 * EXIT CONTROLS
10676 *
10677 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
10678 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10679 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
fe3ef05c 10680 */
c73da3fc
SC
10681 exec_control = vmx_vmexit_ctrl();
10682 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
09abe320
SC
10683 exec_control |= VM_EXIT_LOAD_IA32_EFER;
10684 vm_exit_controls_init(vmx, exec_control);
fe3ef05c 10685
09abe320
SC
10686 /*
10687 * Conceptually we want to copy the PML address and index from
10688 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10689 * since we always flush the log on each vmexit and never change
10690 * the PML address (once set), this happens to be equivalent to
10691 * simply resetting the index in vmcs02.
10692 */
10693 if (enable_pml)
10694 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
a7c0b07d 10695
6beb7bd5 10696 /*
09abe320 10697 * Interrupt/Exception Fields
6beb7bd5 10698 */
09abe320
SC
10699 if (vmx->nested.nested_run_pending) {
10700 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10701 vmcs12->vm_entry_intr_info_field);
10702 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10703 vmcs12->vm_entry_exception_error_code);
10704 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10705 vmcs12->vm_entry_instruction_len);
10706 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10707 vmcs12->guest_interruptibility_info);
10708 vmx->loaded_vmcs->nmi_known_unmasked =
10709 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
51aa68e7 10710 } else {
09abe320
SC
10711 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10712 }
10713}
fe3ef05c 10714
09abe320
SC
10715static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
10716{
c4ebd629
VK
10717 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
10718
10719 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
10720 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
10721 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
cbe3f898 10722 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
c4ebd629
VK
10723 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10724 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10725 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10726 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10727 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10728 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10729 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
cbe3f898 10730 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
c4ebd629
VK
10731 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10732 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10733 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10734 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10735 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10736 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10737 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10738 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10739 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
c4ebd629
VK
10740 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10741 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10742 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10743 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10744 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
cbe3f898
VK
10745 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10746 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
c4ebd629
VK
10747 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10748 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10749 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10750 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10751 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10752 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10753 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10754 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10755 }
10756
10757 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
10758 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
10759 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10760 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10761 vmcs12->guest_pending_dbg_exceptions);
10762 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10763 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10764
10765 /*
10766 * L1 may access the L2's PDPTR, so save them to construct
10767 * vmcs12
10768 */
10769 if (enable_ept) {
10770 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10771 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10772 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10773 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10774 }
a7c0b07d
WL
10775 }
10776
25a2e4fe
PB
10777 if (nested_cpu_has_xsaves(vmcs12))
10778 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
25a2e4fe 10779
fe3ef05c 10780 /*
25a2e4fe
PB
10781 * Whether page-faults are trapped is determined by a combination of
10782 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10783 * If enable_ept, L0 doesn't care about page faults and we should
10784 * set all of these to L1's desires. However, if !enable_ept, L0 does
10785 * care about (at least some) page faults, and because it is not easy
10786 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10787 * to exit on each and every L2 page fault. This is done by setting
10788 * MASK=MATCH=0 and (see below) EB.PF=1.
10789 * Note that below we don't need special code to set EB.PF beyond the
10790 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10791 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10792 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
fe3ef05c 10793 */
25a2e4fe
PB
10794 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10795 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10796 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10797 enable_ept ? vmcs12->page_fault_error_code_match : 0);
fe3ef05c 10798
25a2e4fe
PB
10799 if (cpu_has_vmx_apicv()) {
10800 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10801 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10802 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10803 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10804 }
10805
33966dd6 10806 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
33966dd6 10807 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
25a2e4fe
PB
10808
10809 set_cr4_guest_host_mask(vmx);
10810
62cf9bd8
LA
10811 if (kvm_mpx_supported()) {
10812 if (vmx->nested.nested_run_pending &&
10813 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
10814 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10815 else
10816 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
10817 }
8665c3f9
PB
10818}
10819
10820/*
10821 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10822 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
10823 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
10824 * guest in a way that will both be appropriate to L1's requests, and our
10825 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10826 * function also has additional necessary side-effects, like setting various
10827 * vcpu->arch fields.
10828 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10829 * is assigned to entry_failure_code on failure.
10830 */
10831static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
6514dc38 10832 u32 *entry_failure_code)
8665c3f9
PB
10833{
10834 struct vcpu_vmx *vmx = to_vmx(vcpu);
c4ebd629 10835 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
8665c3f9 10836
945679e3 10837 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) {
09abe320 10838 prepare_vmcs02_full(vmx, vmcs12);
9d1887ef
SC
10839 vmx->nested.dirty_vmcs12 = false;
10840 }
10841
8665c3f9
PB
10842 /*
10843 * First, the fields that are shadowed. This must be kept in sync
e0123119 10844 * with vmcs_shadow_fields.h.
8665c3f9 10845 */
c4ebd629
VK
10846 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
10847 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
c4ebd629 10848 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
cbe3f898 10849 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
c4ebd629 10850 }
8665c3f9 10851
6514dc38 10852 if (vmx->nested.nested_run_pending &&
cf8b84f4 10853 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2996fca0
JK
10854 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10855 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10856 } else {
10857 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10858 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10859 }
63fbf59f 10860 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
fe3ef05c 10861
f4124500
JK
10862 vmx->nested.preemption_timer_expired = false;
10863 if (nested_cpu_has_preemption_timer(vmcs12))
10864 vmx_start_preemption_timer(vcpu);
fe3ef05c
NHE
10865
10866 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10867 * bitwise-or of what L1 wants to trap for L2, and what we want to
10868 * trap. Note that CR0.TS also needs updating - we do this later.
10869 */
10870 update_exception_bitmap(vcpu);
10871 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10872 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10873
6514dc38 10874 if (vmx->nested.nested_run_pending &&
cf8b84f4 10875 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
fe3ef05c 10876 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
44811c02 10877 vcpu->arch.pat = vmcs12->guest_ia32_pat;
cf8b84f4 10878 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
fe3ef05c 10879 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
cf8b84f4 10880 }
fe3ef05c 10881
e79f245d
KA
10882 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
10883
c95ba92a
PF
10884 if (kvm_has_tsc_control)
10885 decache_tsc_multiplier(vmx);
fe3ef05c
NHE
10886
10887 if (enable_vpid) {
10888 /*
5c614b35
WL
10889 * There is no direct mapping between vpid02 and vpid12, the
10890 * vpid02 is per-vCPU for L0 and reused while the value of
10891 * vpid12 is changed w/ one invvpid during nested vmentry.
10892 * The vpid12 is allocated by L1 for L2, so it will not
10893 * influence global bitmap(for vpid01 and vpid02 allocation)
10894 * even if spawn a lot of nested vCPUs.
fe3ef05c 10895 */
efebf0aa 10896 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
5c614b35
WL
10897 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10898 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
efebf0aa 10899 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
5c614b35
WL
10900 }
10901 } else {
1438921c
LA
10902 /*
10903 * If L1 use EPT, then L0 needs to execute INVEPT on
10904 * EPTP02 instead of EPTP01. Therefore, delay TLB
10905 * flush until vmcs02->eptp is fully updated by
10906 * KVM_REQ_LOAD_CR3. Note that this assumes
10907 * KVM_REQ_TLB_FLUSH is evaluated after
10908 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
10909 */
10910 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
5c614b35 10911 }
fe3ef05c
NHE
10912 }
10913
5b8ba41d
SC
10914 if (nested_cpu_has_ept(vmcs12))
10915 nested_ept_init_mmu_context(vcpu);
10916 else if (nested_cpu_has2(vmcs12,
10917 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
a468f2db 10918 vmx_flush_tlb(vcpu, true);
155a97a3 10919
fe3ef05c 10920 /*
bd7e5b08
PB
10921 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10922 * bits which we consider mandatory enabled.
fe3ef05c
NHE
10923 * The CR0_READ_SHADOW is what L2 should have expected to read given
10924 * the specifications by L1; It's not enough to take
10925 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10926 * have more bits than L1 expected.
10927 */
10928 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10929 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10930
10931 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10932 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10933
09abe320 10934 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
3df5c37e 10935 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
5a6a9748
DM
10936 vmx_set_efer(vcpu, vcpu->arch.efer);
10937
2bb8cafe
SC
10938 /*
10939 * Guest state is invalid and unrestricted guest is disabled,
10940 * which means L1 attempted VMEntry to L2 with invalid state.
10941 * Fail the VMEntry.
10942 */
3184a995
PB
10943 if (vmx->emulation_required) {
10944 *entry_failure_code = ENTRY_FAIL_DEFAULT;
2bb8cafe 10945 return 1;
3184a995 10946 }
2bb8cafe 10947
9ed38ffa 10948 /* Shadow page tables on either EPT or shadow page tables. */
7ad658b6 10949 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
9ed38ffa
LP
10950 entry_failure_code))
10951 return 1;
7ca29de2 10952
feaf0c7d
GN
10953 if (!enable_ept)
10954 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10955
fe3ef05c
NHE
10956 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10957 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
ee146c1c 10958 return 0;
fe3ef05c
NHE
10959}
10960
0c7f650e
KS
10961static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
10962{
10963 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
10964 nested_cpu_has_virtual_nmis(vmcs12))
10965 return -EINVAL;
10966
10967 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
10968 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
10969 return -EINVAL;
10970
10971 return 0;
10972}
10973
ca0bde28 10974static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
cd232ad0 10975{
cd232ad0 10976 struct vcpu_vmx *vmx = to_vmx(vcpu);
64a919f7 10977 bool ia32e;
7c177938 10978
6dfacadd 10979 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
ca0bde28
JM
10980 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10981 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
26539bd0 10982
ba8e23db
KS
10983 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
10984 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10985
56a20510
JM
10986 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
10987 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10988
ca0bde28
JM
10989 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10990 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
7c177938 10991
f0f4cf5b
KS
10992 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
10993 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10994
712b12d7
JM
10995 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
10996 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10997
ca0bde28
JM
10998 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10999 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
f2b93280 11000
ca0bde28
JM
11001 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11002 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
e9ac033e 11003
14aa61d0
KS
11004 if (!nested_cpu_has_preemption_timer(vmcs12) &&
11005 nested_cpu_has_save_preemption_timer(vmcs12))
11006 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11007
c5f983f6
BD
11008 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11009 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11010
88656040
JM
11011 if (nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12))
11012 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11013
11014 if (nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12))
11015 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11016
a8a7c02b
LA
11017 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
11018 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11019
7c177938 11020 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
6677f3da
PB
11021 vmx->nested.msrs.procbased_ctls_low,
11022 vmx->nested.msrs.procbased_ctls_high) ||
2e5b0bd9
JM
11023 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11024 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
6677f3da
PB
11025 vmx->nested.msrs.secondary_ctls_low,
11026 vmx->nested.msrs.secondary_ctls_high)) ||
7c177938 11027 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
6677f3da
PB
11028 vmx->nested.msrs.pinbased_ctls_low,
11029 vmx->nested.msrs.pinbased_ctls_high) ||
7c177938 11030 !vmx_control_verify(vmcs12->vm_exit_controls,
6677f3da
PB
11031 vmx->nested.msrs.exit_ctls_low,
11032 vmx->nested.msrs.exit_ctls_high) ||
7c177938 11033 !vmx_control_verify(vmcs12->vm_entry_controls,
6677f3da
PB
11034 vmx->nested.msrs.entry_ctls_low,
11035 vmx->nested.msrs.entry_ctls_high))
ca0bde28 11036 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
7c177938 11037
0c7f650e 11038 if (nested_vmx_check_nmi_controls(vmcs12))
ca0bde28 11039 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
7c177938 11040
41ab9372
BD
11041 if (nested_cpu_has_vmfunc(vmcs12)) {
11042 if (vmcs12->vm_function_control &
6677f3da 11043 ~vmx->nested.msrs.vmfunc_controls)
41ab9372
BD
11044 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11045
11046 if (nested_cpu_has_eptp_switching(vmcs12)) {
11047 if (!nested_cpu_has_ept(vmcs12) ||
11048 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11049 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11050 }
11051 }
27c42a1b 11052
c7c2c709
JM
11053 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11054 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11055
3899152c 11056 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
1dc35dac 11057 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
ca0bde28
JM
11058 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11059 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11060
64a919f7
SC
11061 /*
11062 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11063 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11064 * the values of the LMA and LME bits in the field must each be that of
11065 * the host address-space size VM-exit control.
11066 */
11067 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11068 ia32e = (vmcs12->vm_exit_controls &
11069 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11070 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11071 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11072 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11073 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11074 }
11075
0447378a
MO
11076 /*
11077 * From the Intel SDM, volume 3:
11078 * Fields relevant to VM-entry event injection must be set properly.
11079 * These fields are the VM-entry interruption-information field, the
11080 * VM-entry exception error code, and the VM-entry instruction length.
11081 */
11082 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
11083 u32 intr_info = vmcs12->vm_entry_intr_info_field;
11084 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
11085 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
11086 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
11087 bool should_have_error_code;
11088 bool urg = nested_cpu_has2(vmcs12,
11089 SECONDARY_EXEC_UNRESTRICTED_GUEST);
11090 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
11091
11092 /* VM-entry interruption-info field: interruption type */
11093 if (intr_type == INTR_TYPE_RESERVED ||
11094 (intr_type == INTR_TYPE_OTHER_EVENT &&
11095 !nested_cpu_supports_monitor_trap_flag(vcpu)))
11096 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11097
11098 /* VM-entry interruption-info field: vector */
11099 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
11100 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
11101 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
11102 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11103
11104 /* VM-entry interruption-info field: deliver error code */
11105 should_have_error_code =
11106 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
11107 x86_exception_has_error_code(vector);
11108 if (has_error_code != should_have_error_code)
11109 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11110
11111 /* VM-entry exception error code */
11112 if (has_error_code &&
11113 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
11114 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11115
11116 /* VM-entry interruption-info field: reserved bits */
11117 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
11118 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11119
11120 /* VM-entry instruction length */
11121 switch (intr_type) {
11122 case INTR_TYPE_SOFT_EXCEPTION:
11123 case INTR_TYPE_SOFT_INTR:
11124 case INTR_TYPE_PRIV_SW_EXCEPTION:
11125 if ((vmcs12->vm_entry_instruction_len > 15) ||
11126 (vmcs12->vm_entry_instruction_len == 0 &&
11127 !nested_cpu_has_zero_length_injection(vcpu)))
11128 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11129 }
11130 }
11131
5b8ba41d
SC
11132 if (nested_cpu_has_ept(vmcs12) &&
11133 !valid_ept_address(vcpu, vmcs12->ept_pointer))
11134 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11135
ca0bde28
JM
11136 return 0;
11137}
11138
f145d90d
LA
11139static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
11140 struct vmcs12 *vmcs12)
11141{
11142 int r;
11143 struct page *page;
11144 struct vmcs12 *shadow;
11145
11146 if (vmcs12->vmcs_link_pointer == -1ull)
11147 return 0;
11148
11149 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
11150 return -EINVAL;
11151
11152 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11153 if (is_error_page(page))
11154 return -EINVAL;
11155
11156 r = 0;
11157 shadow = kmap(page);
11158 if (shadow->hdr.revision_id != VMCS12_REVISION ||
11159 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
11160 r = -EINVAL;
11161 kunmap(page);
11162 kvm_release_page_clean(page);
11163 return r;
11164}
11165
ca0bde28
JM
11166static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11167 u32 *exit_qual)
11168{
11169 bool ia32e;
11170
11171 *exit_qual = ENTRY_FAIL_DEFAULT;
7c177938 11172
3899152c 11173 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
ca0bde28 11174 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
b428018a 11175 return 1;
ca0bde28 11176
f145d90d 11177 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
ca0bde28 11178 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
b428018a 11179 return 1;
7c177938
NHE
11180 }
11181
384bb783 11182 /*
cb0c8cda 11183 * If the load IA32_EFER VM-entry control is 1, the following checks
384bb783
JK
11184 * are performed on the field for the IA32_EFER MSR:
11185 * - Bits reserved in the IA32_EFER MSR must be 0.
11186 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11187 * the IA-32e mode guest VM-exit control. It must also be identical
11188 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11189 * CR0.PG) is 1.
11190 */
ca0bde28
JM
11191 if (to_vmx(vcpu)->nested.nested_run_pending &&
11192 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
384bb783
JK
11193 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11194 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11195 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11196 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
ca0bde28 11197 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
b428018a 11198 return 1;
384bb783
JK
11199 }
11200
f1b026a3
WL
11201 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11202 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11203 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11204 return 1;
11205
ca0bde28
JM
11206 return 0;
11207}
11208
52017608
SC
11209static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
11210{
11211 struct vcpu_vmx *vmx = to_vmx(vcpu);
11212 unsigned long cr3, cr4;
11213
11214 if (!nested_early_check)
11215 return 0;
11216
11217 if (vmx->msr_autoload.host.nr)
11218 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
11219 if (vmx->msr_autoload.guest.nr)
11220 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
11221
11222 preempt_disable();
11223
11224 vmx_prepare_switch_to_guest(vcpu);
11225
11226 /*
11227 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
11228 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
11229 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
11230 * there is no need to preserve other bits or save/restore the field.
11231 */
11232 vmcs_writel(GUEST_RFLAGS, 0);
11233
11234 vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
11235
11236 cr3 = __get_current_cr3_fast();
11237 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
11238 vmcs_writel(HOST_CR3, cr3);
11239 vmx->loaded_vmcs->host_state.cr3 = cr3;
11240 }
11241
11242 cr4 = cr4_read_shadow();
11243 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
11244 vmcs_writel(HOST_CR4, cr4);
11245 vmx->loaded_vmcs->host_state.cr4 = cr4;
11246 }
11247
11248 vmx->__launched = vmx->loaded_vmcs->launched;
11249
11250 asm(
11251 /* Set HOST_RSP */
4b1e5478 11252 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
52017608
SC
11253 "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
11254
00df9181 11255 /* Check if vmlaunch or vmresume is needed */
52017608 11256 "cmpl $0, %c[launched](%0)\n\t"
00df9181
UB
11257 "jne 1f\n\t"
11258 __ex("vmlaunch") "\n\t"
52017608 11259 "jmp 2f\n\t"
00df9181 11260 "1: " __ex("vmresume") "\n\t"
52017608 11261 "2: "
52017608
SC
11262 /* Set vmx->fail accordingly */
11263 "setbe %c[fail](%0)\n\t"
f145d90d 11264
52017608
SC
11265 ".pushsection .rodata\n\t"
11266 ".global vmx_early_consistency_check_return\n\t"
11267 "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
11268 ".popsection"
11269 :
11270 : "c"(vmx), "d"((unsigned long)HOST_RSP),
11271 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
11272 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
11273 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
11274 : "rax", "cc", "memory"
11275 );
ca0bde28 11276
52017608 11277 vmcs_writel(HOST_RIP, vmx_return);
7c177938 11278
52017608 11279 preempt_enable();
ca0bde28 11280
52017608
SC
11281 if (vmx->msr_autoload.host.nr)
11282 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11283 if (vmx->msr_autoload.guest.nr)
11284 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11285
11286 if (vmx->fail) {
11287 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
11288 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
11289 vmx->fail = 0;
b428018a 11290 return 1;
7c177938
NHE
11291 }
11292
384bb783 11293 /*
52017608 11294 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
384bb783 11295 */
52017608
SC
11296 local_irq_enable();
11297 if (hw_breakpoint_active())
11298 set_debugreg(__this_cpu_read(cpu_dr7), 7);
384bb783
JK
11299
11300 /*
52017608
SC
11301 * A non-failing VMEntry means we somehow entered guest mode with
11302 * an illegal RIP, and that's just the tip of the iceberg. There
11303 * is no telling what memory has been modified or what state has
11304 * been exposed to unknown code. Hitting this all but guarantees
11305 * a (very critical) hardware issue.
384bb783 11306 */
52017608
SC
11307 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
11308 VMX_EXIT_REASONS_FAILED_VMENTRY));
f1b026a3 11309
ca0bde28
JM
11310 return 0;
11311}
52017608
SC
11312STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
11313
a633e41e
SC
11314static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11315 struct vmcs12 *vmcs12);
ca0bde28 11316
7f7f1ba3 11317/*
a633e41e 11318 * If from_vmentry is false, this is being called from state restore (either RSM
8fcc4b59 11319 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
52017608
SC
11320+ *
11321+ * Returns:
11322+ * 0 - success, i.e. proceed with actual VMEnter
11323+ * 1 - consistency check VMExit
11324+ * -1 - consistency check VMFail
7f7f1ba3 11325 */
a633e41e
SC
11326static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
11327 bool from_vmentry)
858e25c0
JM
11328{
11329 struct vcpu_vmx *vmx = to_vmx(vcpu);
11330 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7e712684 11331 bool evaluate_pending_interrupts;
a633e41e
SC
11332 u32 exit_reason = EXIT_REASON_INVALID_STATE;
11333 u32 exit_qual;
858e25c0 11334
7e712684
PB
11335 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
11336 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
11337 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
11338 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
b5861e5c 11339
858e25c0
JM
11340 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11341 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
62cf9bd8
LA
11342 if (kvm_mpx_supported() &&
11343 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
11344 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
858e25c0 11345
de3a0021 11346 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
858e25c0 11347
16fb9a46
SC
11348 prepare_vmcs02_early(vmx, vmcs12);
11349
11350 if (from_vmentry) {
11351 nested_get_vmcs12_pages(vcpu);
11352
52017608
SC
11353 if (nested_vmx_check_vmentry_hw(vcpu)) {
11354 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11355 return -1;
11356 }
11357
16fb9a46
SC
11358 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
11359 goto vmentry_fail_vmexit;
11360 }
11361
11362 enter_guest_mode(vcpu);
e79f245d
KA
11363 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11364 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11365
a633e41e 11366 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
39f9c388 11367 goto vmentry_fail_vmexit_guest_mode;
858e25c0 11368
7f7f1ba3 11369 if (from_vmentry) {
a633e41e
SC
11370 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
11371 exit_qual = nested_vmx_load_msr(vcpu,
11372 vmcs12->vm_entry_msr_load_addr,
11373 vmcs12->vm_entry_msr_load_count);
11374 if (exit_qual)
39f9c388 11375 goto vmentry_fail_vmexit_guest_mode;
7f7f1ba3
PB
11376 } else {
11377 /*
11378 * The MMU is not initialized to point at the right entities yet and
11379 * "get pages" would need to read data from the guest (i.e. we will
11380 * need to perform gpa to hpa translation). Request a call
11381 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
11382 * have already been set at vmentry time and should not be reset.
11383 */
11384 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
11385 }
858e25c0 11386
b5861e5c
LA
11387 /*
11388 * If L1 had a pending IRQ/NMI until it executed
11389 * VMLAUNCH/VMRESUME which wasn't delivered because it was
11390 * disallowed (e.g. interrupts disabled), L0 needs to
11391 * evaluate if this pending event should cause an exit from L2
11392 * to L1 or delivered directly to L2 (e.g. In case L1 don't
11393 * intercept EXTERNAL_INTERRUPT).
11394 *
7e712684
PB
11395 * Usually this would be handled by the processor noticing an
11396 * IRQ/NMI window request, or checking RVI during evaluation of
11397 * pending virtual interrupts. However, this setting was done
11398 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
11399 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
b5861e5c 11400 */
7e712684 11401 if (unlikely(evaluate_pending_interrupts))
b5861e5c 11402 kvm_make_request(KVM_REQ_EVENT, vcpu);
b5861e5c 11403
858e25c0
JM
11404 /*
11405 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11406 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11407 * returned as far as L1 is concerned. It will only return (and set
11408 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11409 */
11410 return 0;
e79f245d 11411
a633e41e
SC
11412 /*
11413 * A failed consistency check that leads to a VMExit during L1's
11414 * VMEnter to L2 is a variation of a normal VMexit, as explained in
11415 * 26.7 "VM-entry failures during or after loading guest state".
11416 */
39f9c388 11417vmentry_fail_vmexit_guest_mode:
e79f245d
KA
11418 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11419 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11420 leave_guest_mode(vcpu);
16fb9a46
SC
11421
11422vmentry_fail_vmexit:
e79f245d 11423 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
a633e41e
SC
11424
11425 if (!from_vmentry)
11426 return 1;
11427
a633e41e
SC
11428 load_vmcs12_host_state(vcpu, vmcs12);
11429 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11430 vmcs12->exit_qualification = exit_qual;
945679e3
VK
11431 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
11432 vmx->nested.need_vmcs12_sync = true;
a633e41e 11433 return 1;
858e25c0
JM
11434}
11435
ca0bde28
JM
11436/*
11437 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11438 * for running an L2 nested guest.
11439 */
11440static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11441{
11442 struct vmcs12 *vmcs12;
11443 struct vcpu_vmx *vmx = to_vmx(vcpu);
b3f1dfb6 11444 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
ca0bde28
JM
11445 int ret;
11446
11447 if (!nested_vmx_check_permission(vcpu))
11448 return 1;
11449
8cab6507 11450 if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true))
b8bbab92
VK
11451 return 1;
11452
11453 if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
09abb5e3 11454 return nested_vmx_failInvalid(vcpu);
ca0bde28
JM
11455
11456 vmcs12 = get_vmcs12(vcpu);
11457
a6192d40
LA
11458 /*
11459 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
11460 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
11461 * rather than RFLAGS.ZF, and no error number is stored to the
11462 * VM-instruction error field.
11463 */
09abb5e3
SC
11464 if (vmcs12->hdr.shadow_vmcs)
11465 return nested_vmx_failInvalid(vcpu);
a6192d40 11466
945679e3
VK
11467 if (vmx->nested.hv_evmcs) {
11468 copy_enlightened_to_vmcs12(vmx);
11469 /* Enlightened VMCS doesn't have launch state */
11470 vmcs12->launch_state = !launch;
11471 } else if (enable_shadow_vmcs) {
ca0bde28 11472 copy_shadow_to_vmcs12(vmx);
945679e3 11473 }
ca0bde28
JM
11474
11475 /*
11476 * The nested entry process starts with enforcing various prerequisites
11477 * on vmcs12 as required by the Intel SDM, and act appropriately when
11478 * they fail: As the SDM explains, some conditions should cause the
11479 * instruction to fail, while others will cause the instruction to seem
11480 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11481 * To speed up the normal (success) code path, we should avoid checking
11482 * for misconfigurations which will anyway be caught by the processor
11483 * when using the merged vmcs02.
11484 */
09abb5e3
SC
11485 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
11486 return nested_vmx_failValid(vcpu,
11487 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
b3f1dfb6 11488
09abb5e3
SC
11489 if (vmcs12->launch_state == launch)
11490 return nested_vmx_failValid(vcpu,
ca0bde28
JM
11491 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11492 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
ca0bde28
JM
11493
11494 ret = check_vmentry_prereqs(vcpu, vmcs12);
09abb5e3
SC
11495 if (ret)
11496 return nested_vmx_failValid(vcpu, ret);
384bb783 11497
7c177938
NHE
11498 /*
11499 * We're finally done with prerequisite checking, and can start with
11500 * the nested entry.
11501 */
6514dc38 11502 vmx->nested.nested_run_pending = 1;
a633e41e 11503 ret = nested_vmx_enter_non_root_mode(vcpu, true);
52017608
SC
11504 vmx->nested.nested_run_pending = !ret;
11505 if (ret > 0)
7f7f1ba3 11506 return 1;
52017608
SC
11507 else if (ret)
11508 return nested_vmx_failValid(vcpu,
11509 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
ff651cb6 11510
c595ceee
PB
11511 /* Hide L1D cache contents from the nested guest. */
11512 vmx->vcpu.arch.l1tf_flush_l1d = true;
11513
61ada748 11514 /*
d63907dc 11515 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
61ada748
LA
11516 * also be used as part of restoring nVMX state for
11517 * snapshot restore (migration).
11518 *
11519 * In this flow, it is assumed that vmcs12 cache was
11520 * trasferred as part of captured nVMX state and should
11521 * therefore not be read from guest memory (which may not
11522 * exist on destination host yet).
11523 */
11524 nested_cache_shadow_vmcs12(vcpu, vmcs12);
11525
135a06c3
CG
11526 /*
11527 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11528 * by event injection, halt vcpu.
11529 */
11530 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
6514dc38
JM
11531 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11532 vmx->nested.nested_run_pending = 0;
5cb56059 11533 return kvm_vcpu_halt(vcpu);
6514dc38 11534 }
cd232ad0
NHE
11535 return 1;
11536}
11537
4704d0be
NHE
11538/*
11539 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11540 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11541 * This function returns the new value we should put in vmcs12.guest_cr0.
11542 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11543 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11544 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11545 * didn't trap the bit, because if L1 did, so would L0).
11546 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11547 * been modified by L2, and L1 knows it. So just leave the old value of
11548 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11549 * isn't relevant, because if L0 traps this bit it can set it to anything.
11550 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11551 * changed these bits, and therefore they need to be updated, but L0
11552 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11553 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11554 */
11555static inline unsigned long
11556vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11557{
11558 return
11559 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11560 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11561 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11562 vcpu->arch.cr0_guest_owned_bits));
11563}
11564
11565static inline unsigned long
11566vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11567{
11568 return
11569 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11570 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11571 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11572 vcpu->arch.cr4_guest_owned_bits));
11573}
11574
5f3d5799
JK
11575static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11576 struct vmcs12 *vmcs12)
11577{
11578 u32 idt_vectoring;
11579 unsigned int nr;
11580
664f8e26 11581 if (vcpu->arch.exception.injected) {
5f3d5799
JK
11582 nr = vcpu->arch.exception.nr;
11583 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11584
11585 if (kvm_exception_is_soft(nr)) {
11586 vmcs12->vm_exit_instruction_len =
11587 vcpu->arch.event_exit_inst_len;
11588 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11589 } else
11590 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11591
11592 if (vcpu->arch.exception.has_error_code) {
11593 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11594 vmcs12->idt_vectoring_error_code =
11595 vcpu->arch.exception.error_code;
11596 }
11597
11598 vmcs12->idt_vectoring_info_field = idt_vectoring;
cd2633c5 11599 } else if (vcpu->arch.nmi_injected) {
5f3d5799
JK
11600 vmcs12->idt_vectoring_info_field =
11601 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
04140b41 11602 } else if (vcpu->arch.interrupt.injected) {
5f3d5799
JK
11603 nr = vcpu->arch.interrupt.nr;
11604 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11605
11606 if (vcpu->arch.interrupt.soft) {
11607 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11608 vmcs12->vm_entry_instruction_len =
11609 vcpu->arch.event_exit_inst_len;
11610 } else
11611 idt_vectoring |= INTR_TYPE_EXT_INTR;
11612
11613 vmcs12->idt_vectoring_info_field = idt_vectoring;
11614 }
11615}
11616
b6b8a145
JK
11617static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11618{
11619 struct vcpu_vmx *vmx = to_vmx(vcpu);
bfcf83b1 11620 unsigned long exit_qual;
917dc606
LA
11621 bool block_nested_events =
11622 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
acc9ab60 11623
bfcf83b1
WL
11624 if (vcpu->arch.exception.pending &&
11625 nested_vmx_check_exception(vcpu, &exit_qual)) {
917dc606 11626 if (block_nested_events)
bfcf83b1
WL
11627 return -EBUSY;
11628 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
bfcf83b1
WL
11629 return 0;
11630 }
11631
f4124500
JK
11632 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11633 vmx->nested.preemption_timer_expired) {
917dc606 11634 if (block_nested_events)
f4124500
JK
11635 return -EBUSY;
11636 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11637 return 0;
11638 }
11639
b6b8a145 11640 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
917dc606 11641 if (block_nested_events)
b6b8a145
JK
11642 return -EBUSY;
11643 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11644 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11645 INTR_INFO_VALID_MASK, 0);
11646 /*
11647 * The NMI-triggered VM exit counts as injection:
11648 * clear this one and block further NMIs.
11649 */
11650 vcpu->arch.nmi_pending = 0;
11651 vmx_set_nmi_mask(vcpu, true);
11652 return 0;
11653 }
11654
11655 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11656 nested_exit_on_intr(vcpu)) {
917dc606 11657 if (block_nested_events)
b6b8a145
JK
11658 return -EBUSY;
11659 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
705699a1 11660 return 0;
b6b8a145
JK
11661 }
11662
6342c50a
DH
11663 vmx_complete_nested_posted_interrupt(vcpu);
11664 return 0;
b6b8a145
JK
11665}
11666
d264ee0c
SC
11667static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
11668{
11669 to_vmx(vcpu)->req_immediate_exit = true;
11670}
11671
f4124500
JK
11672static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11673{
11674 ktime_t remaining =
11675 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11676 u64 value;
11677
11678 if (ktime_to_ns(remaining) <= 0)
11679 return 0;
11680
11681 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11682 do_div(value, 1000000);
11683 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11684}
11685
4704d0be 11686/*
cf8b84f4
JM
11687 * Update the guest state fields of vmcs12 to reflect changes that
11688 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11689 * VM-entry controls is also updated, since this is really a guest
11690 * state bit.)
4704d0be 11691 */
cf8b84f4 11692static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4704d0be 11693{
4704d0be
NHE
11694 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11695 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11696
4704d0be
NHE
11697 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11698 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11699 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11700
11701 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11702 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11703 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11704 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11705 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11706 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11707 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11708 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11709 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11710 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11711 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11712 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11713 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11714 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11715 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11716 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11717 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11718 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11719 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11720 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11721 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11722 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11723 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11724 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11725 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11726 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11727 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11728 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11729 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11730 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11731 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11732 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11733 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11734 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11735 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11736 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11737
4704d0be
NHE
11738 vmcs12->guest_interruptibility_info =
11739 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11740 vmcs12->guest_pending_dbg_exceptions =
11741 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
3edf1e69
JK
11742 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11743 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11744 else
11745 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4704d0be 11746
f4124500
JK
11747 if (nested_cpu_has_preemption_timer(vmcs12)) {
11748 if (vmcs12->vm_exit_controls &
11749 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11750 vmcs12->vmx_preemption_timer_value =
11751 vmx_get_preemption_timer_value(vcpu);
11752 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11753 }
7854cbca 11754
3633cfc3
NHE
11755 /*
11756 * In some cases (usually, nested EPT), L2 is allowed to change its
11757 * own CR3 without exiting. If it has changed it, we must keep it.
11758 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11759 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11760 *
11761 * Additionally, restore L2's PDPTR to vmcs12.
11762 */
11763 if (enable_ept) {
f3531054 11764 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
3633cfc3
NHE
11765 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11766 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11767 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11768 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11769 }
11770
d281e13b 11771 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
119a9c01 11772
608406e2
WV
11773 if (nested_cpu_has_vid(vmcs12))
11774 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11775
c18911a2
JK
11776 vmcs12->vm_entry_controls =
11777 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
2961e876 11778 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
c18911a2 11779
2996fca0
JK
11780 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11781 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11782 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11783 }
11784
4704d0be
NHE
11785 /* TODO: These cannot have changed unless we have MSR bitmaps and
11786 * the relevant bit asks not to trap the change */
b8c07d55 11787 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
4704d0be 11788 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
10ba54a5
JK
11789 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11790 vmcs12->guest_ia32_efer = vcpu->arch.efer;
4704d0be
NHE
11791 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11792 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11793 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
a87036ad 11794 if (kvm_mpx_supported())
36be0b9d 11795 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
cf8b84f4
JM
11796}
11797
11798/*
11799 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11800 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11801 * and this function updates it to reflect the changes to the guest state while
11802 * L2 was running (and perhaps made some exits which were handled directly by L0
11803 * without going back to L1), and to reflect the exit reason.
11804 * Note that we do not have to copy here all VMCS fields, just those that
11805 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11806 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11807 * which already writes to vmcs12 directly.
11808 */
11809static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11810 u32 exit_reason, u32 exit_intr_info,
11811 unsigned long exit_qualification)
11812{
11813 /* update guest state fields: */
11814 sync_vmcs12(vcpu, vmcs12);
4704d0be
NHE
11815
11816 /* update exit information fields: */
11817
533558bc
JK
11818 vmcs12->vm_exit_reason = exit_reason;
11819 vmcs12->exit_qualification = exit_qualification;
533558bc 11820 vmcs12->vm_exit_intr_info = exit_intr_info;
7313c698 11821
5f3d5799 11822 vmcs12->idt_vectoring_info_field = 0;
4704d0be
NHE
11823 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11824 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11825
5f3d5799 11826 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
7cdc2d62
JM
11827 vmcs12->launch_state = 1;
11828
5f3d5799
JK
11829 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11830 * instead of reading the real value. */
4704d0be 11831 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
5f3d5799
JK
11832
11833 /*
11834 * Transfer the event that L0 or L1 may wanted to inject into
11835 * L2 to IDT_VECTORING_INFO_FIELD.
11836 */
11837 vmcs12_save_pending_event(vcpu, vmcs12);
11838 }
11839
11840 /*
11841 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11842 * preserved above and would only end up incorrectly in L1.
11843 */
11844 vcpu->arch.nmi_injected = false;
11845 kvm_clear_exception_queue(vcpu);
11846 kvm_clear_interrupt_queue(vcpu);
4704d0be
NHE
11847}
11848
11849/*
11850 * A part of what we need to when the nested L2 guest exits and we want to
11851 * run its L1 parent, is to reset L1's guest state to the host state specified
11852 * in vmcs12.
11853 * This function is to be called not only on normal nested exit, but also on
11854 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11855 * Failures During or After Loading Guest State").
11856 * This function should be called when the active VMCS is L1's (vmcs01).
11857 */
733568f9
JK
11858static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11859 struct vmcs12 *vmcs12)
4704d0be 11860{
21feb4eb 11861 struct kvm_segment seg;
bd18bffc 11862 u32 entry_failure_code;
21feb4eb 11863
4704d0be
NHE
11864 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11865 vcpu->arch.efer = vmcs12->host_ia32_efer;
d1fa0352 11866 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4704d0be
NHE
11867 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11868 else
11869 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11870 vmx_set_efer(vcpu, vcpu->arch.efer);
11871
11872 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11873 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
1adfa76a 11874 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
cb61de2f
SC
11875 vmx_set_interrupt_shadow(vcpu, 0);
11876
4704d0be
NHE
11877 /*
11878 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
bd7e5b08
PB
11879 * actually changed, because vmx_set_cr0 refers to efer set above.
11880 *
11881 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11882 * (KVM doesn't change it);
4704d0be 11883 */
bd7e5b08 11884 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
9e3e4dbf 11885 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4704d0be 11886
bd7e5b08 11887 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4704d0be 11888 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
8eb3f87d 11889 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4704d0be 11890
bd18bffc
SC
11891 nested_ept_uninit_mmu_context(vcpu);
11892
11893 /*
11894 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11895 * couldn't have changed.
11896 */
11897 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11898 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11899
11900 if (!enable_ept)
11901 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
feaf0c7d 11902
6f1e03bc 11903 /*
efebf0aa 11904 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
6f1e03bc
LA
11905 * VMEntry/VMExit. Thus, no need to flush TLB.
11906 *
efebf0aa
LA
11907 * If vmcs12 doesn't use VPID, L1 expects TLB to be
11908 * flushed on every VMEntry/VMExit.
6f1e03bc 11909 *
efebf0aa
LA
11910 * Otherwise, we can preserve TLB entries as long as we are
11911 * able to tag L1 TLB entries differently than L2 TLB entries.
1438921c
LA
11912 *
11913 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
11914 * and therefore we request the TLB flush to happen only after VMCS EPTP
11915 * has been set by KVM_REQ_LOAD_CR3.
6f1e03bc
LA
11916 */
11917 if (enable_vpid &&
efebf0aa 11918 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
1438921c 11919 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4704d0be 11920 }
4704d0be
NHE
11921
11922 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11923 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11924 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11925 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11926 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
21f2d551
LP
11927 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11928 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4704d0be 11929
36be0b9d
PB
11930 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11931 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11932 vmcs_write64(GUEST_BNDCFGS, 0);
11933
44811c02 11934 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4704d0be 11935 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
44811c02
JK
11936 vcpu->arch.pat = vmcs12->host_ia32_pat;
11937 }
4704d0be
NHE
11938 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11939 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11940 vmcs12->host_ia32_perf_global_ctrl);
503cd0c5 11941
21feb4eb
ACL
11942 /* Set L1 segment info according to Intel SDM
11943 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11944 seg = (struct kvm_segment) {
11945 .base = 0,
11946 .limit = 0xFFFFFFFF,
11947 .selector = vmcs12->host_cs_selector,
11948 .type = 11,
11949 .present = 1,
11950 .s = 1,
11951 .g = 1
11952 };
11953 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11954 seg.l = 1;
11955 else
11956 seg.db = 1;
11957 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11958 seg = (struct kvm_segment) {
11959 .base = 0,
11960 .limit = 0xFFFFFFFF,
11961 .type = 3,
11962 .present = 1,
11963 .s = 1,
11964 .db = 1,
11965 .g = 1
11966 };
11967 seg.selector = vmcs12->host_ds_selector;
11968 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11969 seg.selector = vmcs12->host_es_selector;
11970 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11971 seg.selector = vmcs12->host_ss_selector;
11972 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11973 seg.selector = vmcs12->host_fs_selector;
11974 seg.base = vmcs12->host_fs_base;
11975 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11976 seg.selector = vmcs12->host_gs_selector;
11977 seg.base = vmcs12->host_gs_base;
11978 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11979 seg = (struct kvm_segment) {
205befd9 11980 .base = vmcs12->host_tr_base,
21feb4eb
ACL
11981 .limit = 0x67,
11982 .selector = vmcs12->host_tr_selector,
11983 .type = 11,
11984 .present = 1
11985 };
11986 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11987
503cd0c5
JK
11988 kvm_set_dr(vcpu, 7, 0x400);
11989 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
ff651cb6 11990
3af18d9c 11991 if (cpu_has_vmx_msr_bitmap())
904e14fb 11992 vmx_update_msr_bitmap(vcpu);
3af18d9c 11993
ff651cb6
WV
11994 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11995 vmcs12->vm_exit_msr_load_count))
11996 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4704d0be
NHE
11997}
11998
bd18bffc
SC
11999static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
12000{
12001 struct shared_msr_entry *efer_msr;
12002 unsigned int i;
12003
12004 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
12005 return vmcs_read64(GUEST_IA32_EFER);
12006
c73da3fc 12007 if (cpu_has_load_ia32_efer())
bd18bffc
SC
12008 return host_efer;
12009
12010 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
12011 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
12012 return vmx->msr_autoload.guest.val[i].value;
12013 }
12014
12015 efer_msr = find_msr_entry(vmx, MSR_EFER);
12016 if (efer_msr)
12017 return efer_msr->data;
12018
12019 return host_efer;
12020}
12021
12022static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
12023{
12024 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12025 struct vcpu_vmx *vmx = to_vmx(vcpu);
12026 struct vmx_msr_entry g, h;
12027 struct msr_data msr;
12028 gpa_t gpa;
12029 u32 i, j;
12030
12031 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
12032
12033 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
12034 /*
12035 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
12036 * as vmcs01.GUEST_DR7 contains a userspace defined value
12037 * and vcpu->arch.dr7 is not squirreled away before the
12038 * nested VMENTER (not worth adding a variable in nested_vmx).
12039 */
12040 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
12041 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
12042 else
12043 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
12044 }
12045
12046 /*
12047 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
12048 * handle a variety of side effects to KVM's software model.
12049 */
12050 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
12051
12052 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
12053 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
12054
12055 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
12056 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
12057
12058 nested_ept_uninit_mmu_context(vcpu);
12059 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
12060 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
12061
12062 /*
12063 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
12064 * from vmcs01 (if necessary). The PDPTRs are not loaded on
12065 * VMFail, like everything else we just need to ensure our
12066 * software model is up-to-date.
12067 */
12068 ept_save_pdptrs(vcpu);
12069
12070 kvm_mmu_reset_context(vcpu);
12071
12072 if (cpu_has_vmx_msr_bitmap())
12073 vmx_update_msr_bitmap(vcpu);
12074
12075 /*
12076 * This nasty bit of open coding is a compromise between blindly
12077 * loading L1's MSRs using the exit load lists (incorrect emulation
12078 * of VMFail), leaving the nested VM's MSRs in the software model
12079 * (incorrect behavior) and snapshotting the modified MSRs (too
12080 * expensive since the lists are unbound by hardware). For each
12081 * MSR that was (prematurely) loaded from the nested VMEntry load
12082 * list, reload it from the exit load list if it exists and differs
12083 * from the guest value. The intent is to stuff host state as
12084 * silently as possible, not to fully process the exit load list.
12085 */
12086 msr.host_initiated = false;
12087 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
12088 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
12089 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
12090 pr_debug_ratelimited(
12091 "%s read MSR index failed (%u, 0x%08llx)\n",
12092 __func__, i, gpa);
12093 goto vmabort;
12094 }
12095
12096 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
12097 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
12098 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
12099 pr_debug_ratelimited(
12100 "%s read MSR failed (%u, 0x%08llx)\n",
12101 __func__, j, gpa);
12102 goto vmabort;
12103 }
12104 if (h.index != g.index)
12105 continue;
12106 if (h.value == g.value)
12107 break;
12108
12109 if (nested_vmx_load_msr_check(vcpu, &h)) {
12110 pr_debug_ratelimited(
12111 "%s check failed (%u, 0x%x, 0x%x)\n",
12112 __func__, j, h.index, h.reserved);
12113 goto vmabort;
12114 }
12115
12116 msr.index = h.index;
12117 msr.data = h.value;
12118 if (kvm_set_msr(vcpu, &msr)) {
12119 pr_debug_ratelimited(
12120 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
12121 __func__, j, h.index, h.value);
12122 goto vmabort;
12123 }
12124 }
12125 }
12126
12127 return;
12128
12129vmabort:
12130 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
12131}
12132
4704d0be
NHE
12133/*
12134 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12135 * and modify vmcs12 to make it see what it would expect to see there if
12136 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12137 */
533558bc
JK
12138static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12139 u32 exit_intr_info,
12140 unsigned long exit_qualification)
4704d0be
NHE
12141{
12142 struct vcpu_vmx *vmx = to_vmx(vcpu);
4704d0be
NHE
12143 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12144
5f3d5799
JK
12145 /* trying to cancel vmlaunch/vmresume is a bug */
12146 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12147
4704d0be 12148 leave_guest_mode(vcpu);
4704d0be 12149
e79f245d
KA
12150 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12151 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12152
4f350c6d 12153 if (likely(!vmx->fail)) {
72e9cbdb
LP
12154 if (exit_reason == -1)
12155 sync_vmcs12(vcpu, vmcs12);
12156 else
12157 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12158 exit_qualification);
ff651cb6 12159
61ada748
LA
12160 /*
12161 * Must happen outside of sync_vmcs12() as it will
12162 * also be used to capture vmcs12 cache as part of
12163 * capturing nVMX state for snapshot (migration).
12164 *
12165 * Otherwise, this flush will dirty guest memory at a
12166 * point it is already assumed by user-space to be
12167 * immutable.
12168 */
12169 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12170
4f350c6d
JM
12171 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12172 vmcs12->vm_exit_msr_store_count))
12173 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
2768c0cc
SC
12174 } else {
12175 /*
12176 * The only expected VM-instruction error is "VM entry with
12177 * invalid control field(s)." Anything else indicates a
12178 * problem with L0. And we should never get here with a
12179 * VMFail of any type if early consistency checks are enabled.
12180 */
12181 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
12182 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12183 WARN_ON_ONCE(nested_early_check);
4f350c6d 12184 }
cf3215d9 12185
1279a6b1 12186 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
36c3cc42 12187
9314006d 12188 /* Update any VMCS fields that might have changed while L2 ran */
33966dd6
KRW
12189 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12190 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
ea26e4ec 12191 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
f459a707 12192
c95ba92a
PF
12193 if (kvm_has_tsc_control)
12194 decache_tsc_multiplier(vmx);
4704d0be 12195
8d860bbe
JM
12196 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12197 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12198 vmx_set_virtual_apic_mode(vcpu);
fb6c8198
JM
12199 } else if (!nested_cpu_has_ept(vmcs12) &&
12200 nested_cpu_has2(vmcs12,
12201 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
a468f2db 12202 vmx_flush_tlb(vcpu, true);
dccbfcf5 12203 }
4704d0be
NHE
12204
12205 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12206 vmx->host_rsp = 0;
12207
12208 /* Unpin physical memory we referred to in vmcs02 */
12209 if (vmx->nested.apic_access_page) {
53a70daf 12210 kvm_release_page_dirty(vmx->nested.apic_access_page);
48d89b92 12211 vmx->nested.apic_access_page = NULL;
4704d0be 12212 }
a7c0b07d 12213 if (vmx->nested.virtual_apic_page) {
53a70daf 12214 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
48d89b92 12215 vmx->nested.virtual_apic_page = NULL;
a7c0b07d 12216 }
705699a1
WV
12217 if (vmx->nested.pi_desc_page) {
12218 kunmap(vmx->nested.pi_desc_page);
53a70daf 12219 kvm_release_page_dirty(vmx->nested.pi_desc_page);
705699a1
WV
12220 vmx->nested.pi_desc_page = NULL;
12221 vmx->nested.pi_desc = NULL;
12222 }
4704d0be 12223
38b99173
TC
12224 /*
12225 * We are now running in L2, mmu_notifier will force to reload the
12226 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12227 */
c83b6d15 12228 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
38b99173 12229
945679e3
VK
12230 if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs))
12231 vmx->nested.need_vmcs12_sync = true;
b6b8a145
JK
12232
12233 /* in case we halted in L2 */
12234 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4f350c6d
JM
12235
12236 if (likely(!vmx->fail)) {
12237 /*
12238 * TODO: SDM says that with acknowledge interrupt on
12239 * exit, bit 31 of the VM-exit interrupt information
12240 * (valid interrupt) is always set to 1 on
12241 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12242 * need kvm_cpu_has_interrupt(). See the commit
12243 * message for details.
12244 */
12245 if (nested_exit_intr_ack_set(vcpu) &&
12246 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12247 kvm_cpu_has_interrupt(vcpu)) {
12248 int irq = kvm_cpu_get_interrupt(vcpu);
12249 WARN_ON(irq < 0);
12250 vmcs12->vm_exit_intr_info = irq |
12251 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12252 }
12253
72e9cbdb
LP
12254 if (exit_reason != -1)
12255 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12256 vmcs12->exit_qualification,
12257 vmcs12->idt_vectoring_info_field,
12258 vmcs12->vm_exit_intr_info,
12259 vmcs12->vm_exit_intr_error_code,
12260 KVM_ISA_VMX);
4f350c6d
JM
12261
12262 load_vmcs12_host_state(vcpu, vmcs12);
12263
12264 return;
12265 }
09abb5e3 12266
4f350c6d
JM
12267 /*
12268 * After an early L2 VM-entry failure, we're now back
12269 * in L1 which thinks it just finished a VMLAUNCH or
12270 * VMRESUME instruction, so we need to set the failure
12271 * flag and the VM-instruction error field of the VMCS
cb61de2f 12272 * accordingly, and skip the emulated instruction.
4f350c6d 12273 */
09abb5e3 12274 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
5af41573 12275
4f350c6d 12276 /*
bd18bffc
SC
12277 * Restore L1's host state to KVM's software model. We're here
12278 * because a consistency check was caught by hardware, which
12279 * means some amount of guest state has been propagated to KVM's
12280 * model and needs to be unwound to the host's state.
4f350c6d 12281 */
bd18bffc 12282 nested_vmx_restore_host_state(vcpu);
5af41573 12283
4f350c6d 12284 vmx->fail = 0;
4704d0be
NHE
12285}
12286
42124925
JK
12287/*
12288 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12289 */
12290static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12291{
2f707d97
WL
12292 if (is_guest_mode(vcpu)) {
12293 to_vmx(vcpu)->nested.nested_run_pending = 0;
533558bc 12294 nested_vmx_vmexit(vcpu, -1, 0, 0);
2f707d97 12295 }
14c07ad8 12296 free_nested(vcpu);
7c177938
NHE
12297}
12298
8a76d7f2
JR
12299static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12300 struct x86_instruction_info *info,
12301 enum x86_intercept_stage stage)
12302{
fb6d4d34
PB
12303 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12304 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12305
12306 /*
12307 * RDPID causes #UD if disabled through secondary execution controls.
12308 * Because it is marked as EmulateOnUD, we need to intercept it here.
12309 */
12310 if (info->intercept == x86_intercept_rdtscp &&
12311 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12312 ctxt->exception.vector = UD_VECTOR;
12313 ctxt->exception.error_code_valid = false;
12314 return X86EMUL_PROPAGATE_FAULT;
12315 }
12316
12317 /* TODO: check more intercepts... */
8a76d7f2
JR
12318 return X86EMUL_CONTINUE;
12319}
12320
64672c95
YJ
12321#ifdef CONFIG_X86_64
12322/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12323static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12324 u64 divisor, u64 *result)
12325{
12326 u64 low = a << shift, high = a >> (64 - shift);
12327
12328 /* To avoid the overflow on divq */
12329 if (high >= divisor)
12330 return 1;
12331
12332 /* Low hold the result, high hold rem which is discarded */
12333 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12334 "rm" (divisor), "0" (low), "1" (high));
12335 *result = low;
12336
12337 return 0;
12338}
12339
12340static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12341{
386c6ddb 12342 struct vcpu_vmx *vmx;
c5ce8235 12343 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
386c6ddb
KA
12344
12345 if (kvm_mwait_in_guest(vcpu->kvm))
12346 return -EOPNOTSUPP;
12347
12348 vmx = to_vmx(vcpu);
12349 tscl = rdtsc();
12350 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12351 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
c5ce8235
WL
12352 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12353
12354 if (delta_tsc > lapic_timer_advance_cycles)
12355 delta_tsc -= lapic_timer_advance_cycles;
12356 else
12357 delta_tsc = 0;
64672c95
YJ
12358
12359 /* Convert to host delta tsc if tsc scaling is enabled */
12360 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12361 u64_shl_div_u64(delta_tsc,
12362 kvm_tsc_scaling_ratio_frac_bits,
12363 vcpu->arch.tsc_scaling_ratio,
12364 &delta_tsc))
12365 return -ERANGE;
12366
12367 /*
12368 * If the delta tsc can't fit in the 32 bit after the multi shift,
12369 * we can't use the preemption timer.
12370 * It's possible that it fits on later vmentries, but checking
12371 * on every vmentry is costly so we just use an hrtimer.
12372 */
12373 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12374 return -ERANGE;
12375
12376 vmx->hv_deadline_tsc = tscl + delta_tsc;
c8533544 12377 return delta_tsc == 0;
64672c95
YJ
12378}
12379
12380static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12381{
f459a707 12382 to_vmx(vcpu)->hv_deadline_tsc = -1;
64672c95
YJ
12383}
12384#endif
12385
48d89b92 12386static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
ae97a3b8 12387{
b31c114b 12388 if (!kvm_pause_in_guest(vcpu->kvm))
b4a2d31d 12389 shrink_ple_window(vcpu);
ae97a3b8
RK
12390}
12391
843e4330
KH
12392static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12393 struct kvm_memory_slot *slot)
12394{
12395 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12396 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12397}
12398
12399static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12400 struct kvm_memory_slot *slot)
12401{
12402 kvm_mmu_slot_set_dirty(kvm, slot);
12403}
12404
12405static void vmx_flush_log_dirty(struct kvm *kvm)
12406{
12407 kvm_flush_pml_buffers(kvm);
12408}
12409
c5f983f6
BD
12410static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12411{
12412 struct vmcs12 *vmcs12;
12413 struct vcpu_vmx *vmx = to_vmx(vcpu);
12414 gpa_t gpa;
12415 struct page *page = NULL;
12416 u64 *pml_address;
12417
12418 if (is_guest_mode(vcpu)) {
12419 WARN_ON_ONCE(vmx->nested.pml_full);
12420
12421 /*
12422 * Check if PML is enabled for the nested guest.
12423 * Whether eptp bit 6 is set is already checked
12424 * as part of A/D emulation.
12425 */
12426 vmcs12 = get_vmcs12(vcpu);
12427 if (!nested_cpu_has_pml(vmcs12))
12428 return 0;
12429
4769886b 12430 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
c5f983f6
BD
12431 vmx->nested.pml_full = true;
12432 return 1;
12433 }
12434
12435 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12436
5e2f30b7
DH
12437 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12438 if (is_error_page(page))
c5f983f6
BD
12439 return 0;
12440
12441 pml_address = kmap(page);
12442 pml_address[vmcs12->guest_pml_index--] = gpa;
12443 kunmap(page);
53a70daf 12444 kvm_release_page_clean(page);
c5f983f6
BD
12445 }
12446
12447 return 0;
12448}
12449
843e4330
KH
12450static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12451 struct kvm_memory_slot *memslot,
12452 gfn_t offset, unsigned long mask)
12453{
12454 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12455}
12456
cd39e117
PB
12457static void __pi_post_block(struct kvm_vcpu *vcpu)
12458{
12459 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12460 struct pi_desc old, new;
12461 unsigned int dest;
cd39e117
PB
12462
12463 do {
12464 old.control = new.control = pi_desc->control;
8b306e2f
PB
12465 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12466 "Wakeup handler not enabled while the VCPU is blocked\n");
cd39e117
PB
12467
12468 dest = cpu_physical_id(vcpu->cpu);
12469
12470 if (x2apic_enabled())
12471 new.ndst = dest;
12472 else
12473 new.ndst = (dest << 8) & 0xFF00;
12474
cd39e117
PB
12475 /* set 'NV' to 'notification vector' */
12476 new.nv = POSTED_INTR_VECTOR;
c0a1666b
PB
12477 } while (cmpxchg64(&pi_desc->control, old.control,
12478 new.control) != old.control);
cd39e117 12479
8b306e2f
PB
12480 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12481 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
cd39e117 12482 list_del(&vcpu->blocked_vcpu_list);
8b306e2f 12483 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
cd39e117
PB
12484 vcpu->pre_pcpu = -1;
12485 }
12486}
12487
bf9f6ac8
FW
12488/*
12489 * This routine does the following things for vCPU which is going
12490 * to be blocked if VT-d PI is enabled.
12491 * - Store the vCPU to the wakeup list, so when interrupts happen
12492 * we can find the right vCPU to wake up.
12493 * - Change the Posted-interrupt descriptor as below:
12494 * 'NDST' <-- vcpu->pre_pcpu
12495 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12496 * - If 'ON' is set during this process, which means at least one
12497 * interrupt is posted for this vCPU, we cannot block it, in
12498 * this case, return 1, otherwise, return 0.
12499 *
12500 */
bc22512b 12501static int pi_pre_block(struct kvm_vcpu *vcpu)
bf9f6ac8 12502{
bf9f6ac8
FW
12503 unsigned int dest;
12504 struct pi_desc old, new;
12505 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12506
12507 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
a0052191
YZ
12508 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12509 !kvm_vcpu_apicv_active(vcpu))
bf9f6ac8
FW
12510 return 0;
12511
8b306e2f
PB
12512 WARN_ON(irqs_disabled());
12513 local_irq_disable();
12514 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12515 vcpu->pre_pcpu = vcpu->cpu;
12516 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12517 list_add_tail(&vcpu->blocked_vcpu_list,
12518 &per_cpu(blocked_vcpu_on_cpu,
12519 vcpu->pre_pcpu));
12520 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12521 }
bf9f6ac8
FW
12522
12523 do {
12524 old.control = new.control = pi_desc->control;
12525
bf9f6ac8
FW
12526 WARN((pi_desc->sn == 1),
12527 "Warning: SN field of posted-interrupts "
12528 "is set before blocking\n");
12529
12530 /*
12531 * Since vCPU can be preempted during this process,
12532 * vcpu->cpu could be different with pre_pcpu, we
12533 * need to set pre_pcpu as the destination of wakeup
12534 * notification event, then we can find the right vCPU
12535 * to wakeup in wakeup handler if interrupts happen
12536 * when the vCPU is in blocked state.
12537 */
12538 dest = cpu_physical_id(vcpu->pre_pcpu);
12539
12540 if (x2apic_enabled())
12541 new.ndst = dest;
12542 else
12543 new.ndst = (dest << 8) & 0xFF00;
12544
12545 /* set 'NV' to 'wakeup vector' */
12546 new.nv = POSTED_INTR_WAKEUP_VECTOR;
c0a1666b
PB
12547 } while (cmpxchg64(&pi_desc->control, old.control,
12548 new.control) != old.control);
bf9f6ac8 12549
8b306e2f
PB
12550 /* We should not block the vCPU if an interrupt is posted for it. */
12551 if (pi_test_on(pi_desc) == 1)
12552 __pi_post_block(vcpu);
12553
12554 local_irq_enable();
12555 return (vcpu->pre_pcpu == -1);
bf9f6ac8
FW
12556}
12557
bc22512b
YJ
12558static int vmx_pre_block(struct kvm_vcpu *vcpu)
12559{
12560 if (pi_pre_block(vcpu))
12561 return 1;
12562
64672c95
YJ
12563 if (kvm_lapic_hv_timer_in_use(vcpu))
12564 kvm_lapic_switch_to_sw_timer(vcpu);
12565
bc22512b
YJ
12566 return 0;
12567}
12568
12569static void pi_post_block(struct kvm_vcpu *vcpu)
bf9f6ac8 12570{
8b306e2f 12571 if (vcpu->pre_pcpu == -1)
bf9f6ac8
FW
12572 return;
12573
8b306e2f
PB
12574 WARN_ON(irqs_disabled());
12575 local_irq_disable();
cd39e117 12576 __pi_post_block(vcpu);
8b306e2f 12577 local_irq_enable();
bf9f6ac8
FW
12578}
12579
bc22512b
YJ
12580static void vmx_post_block(struct kvm_vcpu *vcpu)
12581{
64672c95
YJ
12582 if (kvm_x86_ops->set_hv_timer)
12583 kvm_lapic_switch_to_hv_timer(vcpu);
12584
bc22512b
YJ
12585 pi_post_block(vcpu);
12586}
12587
efc64404
FW
12588/*
12589 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12590 *
12591 * @kvm: kvm
12592 * @host_irq: host irq of the interrupt
12593 * @guest_irq: gsi of the interrupt
12594 * @set: set or unset PI
12595 * returns 0 on success, < 0 on failure
12596 */
12597static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12598 uint32_t guest_irq, bool set)
12599{
12600 struct kvm_kernel_irq_routing_entry *e;
12601 struct kvm_irq_routing_table *irq_rt;
12602 struct kvm_lapic_irq irq;
12603 struct kvm_vcpu *vcpu;
12604 struct vcpu_data vcpu_info;
3a8b0677 12605 int idx, ret = 0;
efc64404
FW
12606
12607 if (!kvm_arch_has_assigned_device(kvm) ||
a0052191
YZ
12608 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12609 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
efc64404
FW
12610 return 0;
12611
12612 idx = srcu_read_lock(&kvm->irq_srcu);
12613 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
3a8b0677
JS
12614 if (guest_irq >= irq_rt->nr_rt_entries ||
12615 hlist_empty(&irq_rt->map[guest_irq])) {
12616 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12617 guest_irq, irq_rt->nr_rt_entries);
12618 goto out;
12619 }
efc64404
FW
12620
12621 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12622 if (e->type != KVM_IRQ_ROUTING_MSI)
12623 continue;
12624 /*
12625 * VT-d PI cannot support posting multicast/broadcast
12626 * interrupts to a vCPU, we still use interrupt remapping
12627 * for these kind of interrupts.
12628 *
12629 * For lowest-priority interrupts, we only support
12630 * those with single CPU as the destination, e.g. user
12631 * configures the interrupts via /proc/irq or uses
12632 * irqbalance to make the interrupts single-CPU.
12633 *
12634 * We will support full lowest-priority interrupt later.
12635 */
12636
37131313 12637 kvm_set_msi_irq(kvm, e, &irq);
23a1c257
FW
12638 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12639 /*
12640 * Make sure the IRTE is in remapped mode if
12641 * we don't handle it in posted mode.
12642 */
12643 ret = irq_set_vcpu_affinity(host_irq, NULL);
12644 if (ret < 0) {
12645 printk(KERN_INFO
12646 "failed to back to remapped mode, irq: %u\n",
12647 host_irq);
12648 goto out;
12649 }
12650
efc64404 12651 continue;
23a1c257 12652 }
efc64404
FW
12653
12654 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12655 vcpu_info.vector = irq.vector;
12656
2698d82e 12657 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
efc64404
FW
12658 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12659
12660 if (set)
12661 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
dc91f2eb 12662 else
efc64404 12663 ret = irq_set_vcpu_affinity(host_irq, NULL);
efc64404
FW
12664
12665 if (ret < 0) {
12666 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12667 __func__);
12668 goto out;
12669 }
12670 }
12671
12672 ret = 0;
12673out:
12674 srcu_read_unlock(&kvm->irq_srcu, idx);
12675 return ret;
12676}
12677
c45dcc71
AR
12678static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12679{
12680 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12681 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12682 FEATURE_CONTROL_LMCE;
12683 else
12684 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12685 ~FEATURE_CONTROL_LMCE;
12686}
12687
72d7b374
LP
12688static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12689{
72e9cbdb
LP
12690 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12691 if (to_vmx(vcpu)->nested.nested_run_pending)
12692 return 0;
72d7b374
LP
12693 return 1;
12694}
12695
0234bf88
LP
12696static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12697{
72e9cbdb
LP
12698 struct vcpu_vmx *vmx = to_vmx(vcpu);
12699
12700 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12701 if (vmx->nested.smm.guest_mode)
12702 nested_vmx_vmexit(vcpu, -1, 0, 0);
12703
12704 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12705 vmx->nested.vmxon = false;
caa057a2 12706 vmx_clear_hlt(vcpu);
0234bf88
LP
12707 return 0;
12708}
12709
12710static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12711{
72e9cbdb
LP
12712 struct vcpu_vmx *vmx = to_vmx(vcpu);
12713 int ret;
12714
12715 if (vmx->nested.smm.vmxon) {
12716 vmx->nested.vmxon = true;
12717 vmx->nested.smm.vmxon = false;
12718 }
12719
12720 if (vmx->nested.smm.guest_mode) {
12721 vcpu->arch.hflags &= ~HF_SMM_MASK;
a633e41e 12722 ret = nested_vmx_enter_non_root_mode(vcpu, false);
72e9cbdb
LP
12723 vcpu->arch.hflags |= HF_SMM_MASK;
12724 if (ret)
12725 return ret;
12726
12727 vmx->nested.smm.guest_mode = false;
12728 }
0234bf88
LP
12729 return 0;
12730}
12731
cc3d967f
LP
12732static int enable_smi_window(struct kvm_vcpu *vcpu)
12733{
12734 return 0;
12735}
12736
8cab6507
VK
12737static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu)
12738{
12739 struct vcpu_vmx *vmx = to_vmx(vcpu);
12740
12741 /*
12742 * In case we do two consecutive get/set_nested_state()s while L2 was
12743 * running hv_evmcs may end up not being mapped (we map it from
12744 * nested_vmx_run()/vmx_vcpu_run()). Check is_guest_mode() as we always
12745 * have vmcs12 if it is true.
12746 */
12747 return is_guest_mode(vcpu) || vmx->nested.current_vmptr != -1ull ||
12748 vmx->nested.hv_evmcs;
12749}
12750
8fcc4b59
JM
12751static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
12752 struct kvm_nested_state __user *user_kvm_nested_state,
12753 u32 user_data_size)
12754{
12755 struct vcpu_vmx *vmx;
12756 struct vmcs12 *vmcs12;
12757 struct kvm_nested_state kvm_state = {
12758 .flags = 0,
12759 .format = 0,
12760 .size = sizeof(kvm_state),
12761 .vmx.vmxon_pa = -1ull,
12762 .vmx.vmcs_pa = -1ull,
12763 };
12764
12765 if (!vcpu)
12766 return kvm_state.size + 2 * VMCS12_SIZE;
12767
12768 vmx = to_vmx(vcpu);
12769 vmcs12 = get_vmcs12(vcpu);
945679e3 12770
8cab6507
VK
12771 if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
12772 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
945679e3 12773
8fcc4b59
JM
12774 if (nested_vmx_allowed(vcpu) &&
12775 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
12776 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
12777 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
12778
8cab6507 12779 if (vmx_has_valid_vmcs12(vcpu)) {
8fcc4b59
JM
12780 kvm_state.size += VMCS12_SIZE;
12781
fa58a9fa
PB
12782 if (is_guest_mode(vcpu) &&
12783 nested_cpu_has_shadow_vmcs(vmcs12) &&
12784 vmcs12->vmcs_link_pointer != -1ull)
12785 kvm_state.size += VMCS12_SIZE;
12786 }
12787
8fcc4b59
JM
12788 if (vmx->nested.smm.vmxon)
12789 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
12790
12791 if (vmx->nested.smm.guest_mode)
12792 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
12793
12794 if (is_guest_mode(vcpu)) {
12795 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
12796
12797 if (vmx->nested.nested_run_pending)
12798 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
12799 }
12800 }
12801
12802 if (user_data_size < kvm_state.size)
12803 goto out;
12804
12805 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
12806 return -EFAULT;
12807
8cab6507 12808 if (!vmx_has_valid_vmcs12(vcpu))
8fcc4b59
JM
12809 goto out;
12810
12811 /*
12812 * When running L2, the authoritative vmcs12 state is in the
12813 * vmcs02. When running L1, the authoritative vmcs12 state is
8cab6507 12814 * in the shadow or enlightened vmcs linked to vmcs01, unless
945679e3 12815 * need_vmcs12_sync is set, in which case, the authoritative
8fcc4b59
JM
12816 * vmcs12 state is in the vmcs12 already.
12817 */
8cab6507 12818 if (is_guest_mode(vcpu)) {
8fcc4b59 12819 sync_vmcs12(vcpu, vmcs12);
8cab6507
VK
12820 } else if (!vmx->nested.need_vmcs12_sync) {
12821 if (vmx->nested.hv_evmcs)
12822 copy_enlightened_to_vmcs12(vmx);
12823 else if (enable_shadow_vmcs)
12824 copy_shadow_to_vmcs12(vmx);
12825 }
8fcc4b59
JM
12826
12827 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
12828 return -EFAULT;
12829
fa58a9fa
PB
12830 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
12831 vmcs12->vmcs_link_pointer != -1ull) {
12832 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
12833 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
12834 return -EFAULT;
12835 }
12836
8fcc4b59
JM
12837out:
12838 return kvm_state.size;
12839}
12840
12841static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
12842 struct kvm_nested_state __user *user_kvm_nested_state,
12843 struct kvm_nested_state *kvm_state)
12844{
12845 struct vcpu_vmx *vmx = to_vmx(vcpu);
12846 struct vmcs12 *vmcs12;
12847 u32 exit_qual;
12848 int ret;
12849
12850 if (kvm_state->format != 0)
12851 return -EINVAL;
12852
8cab6507
VK
12853 if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
12854 nested_enable_evmcs(vcpu, NULL);
12855
8fcc4b59
JM
12856 if (!nested_vmx_allowed(vcpu))
12857 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
12858
12859 if (kvm_state->vmx.vmxon_pa == -1ull) {
12860 if (kvm_state->vmx.smm.flags)
12861 return -EINVAL;
12862
12863 if (kvm_state->vmx.vmcs_pa != -1ull)
12864 return -EINVAL;
12865
12866 vmx_leave_nested(vcpu);
12867 return 0;
12868 }
12869
12870 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
12871 return -EINVAL;
12872
8fcc4b59
JM
12873 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
12874 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
12875 return -EINVAL;
12876
12877 if (kvm_state->vmx.smm.flags &
12878 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
12879 return -EINVAL;
12880
5bea5123
PB
12881 /*
12882 * SMM temporarily disables VMX, so we cannot be in guest mode,
12883 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
12884 * must be zero.
12885 */
12886 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
12887 return -EINVAL;
12888
8fcc4b59
JM
12889 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
12890 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
12891 return -EINVAL;
12892
12893 vmx_leave_nested(vcpu);
12894 if (kvm_state->vmx.vmxon_pa == -1ull)
12895 return 0;
12896
12897 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
12898 ret = enter_vmx_operation(vcpu);
12899 if (ret)
12900 return ret;
12901
a1b0c1c6
VK
12902 /* Empty 'VMXON' state is permitted */
12903 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
12904 return 0;
12905
8cab6507
VK
12906 if (kvm_state->vmx.vmcs_pa != -1ull) {
12907 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
12908 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
12909 return -EINVAL;
a1b0c1c6 12910
8cab6507
VK
12911 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
12912 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
12913 /*
12914 * Sync eVMCS upon entry as we may not have
12915 * HV_X64_MSR_VP_ASSIST_PAGE set up yet.
12916 */
12917 vmx->nested.need_vmcs12_sync = true;
12918 } else {
12919 return -EINVAL;
12920 }
8fcc4b59
JM
12921
12922 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
12923 vmx->nested.smm.vmxon = true;
12924 vmx->nested.vmxon = false;
12925
12926 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
12927 vmx->nested.smm.guest_mode = true;
12928 }
12929
12930 vmcs12 = get_vmcs12(vcpu);
12931 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
12932 return -EFAULT;
12933
392b2f25 12934 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
8fcc4b59
JM
12935 return -EINVAL;
12936
12937 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
12938 return 0;
12939
12940 vmx->nested.nested_run_pending =
12941 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
12942
fa58a9fa
PB
12943 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
12944 vmcs12->vmcs_link_pointer != -1ull) {
12945 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
12946 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
12947 return -EINVAL;
12948
12949 if (copy_from_user(shadow_vmcs12,
12950 user_kvm_nested_state->data + VMCS12_SIZE,
12951 sizeof(*vmcs12)))
12952 return -EFAULT;
12953
12954 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
12955 !shadow_vmcs12->hdr.shadow_vmcs)
12956 return -EINVAL;
12957 }
12958
8fcc4b59
JM
12959 if (check_vmentry_prereqs(vcpu, vmcs12) ||
12960 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
12961 return -EINVAL;
12962
8fcc4b59 12963 vmx->nested.dirty_vmcs12 = true;
a633e41e 12964 ret = nested_vmx_enter_non_root_mode(vcpu, false);
8fcc4b59
JM
12965 if (ret)
12966 return -EINVAL;
12967
12968 return 0;
12969}
12970
a3203381
SC
12971static __exit void nested_vmx_hardware_unsetup(void)
12972{
12973 int i;
12974
12975 if (enable_shadow_vmcs) {
12976 for (i = 0; i < VMX_BITMAP_NR; i++)
12977 free_page((unsigned long)vmx_bitmap[i]);
12978 }
12979}
12980
12981static __init int nested_vmx_hardware_setup(void)
12982{
12983 int i;
12984
12985 if (enable_shadow_vmcs) {
12986 for (i = 0; i < VMX_BITMAP_NR; i++) {
12987 vmx_bitmap[i] = (unsigned long *)
12988 __get_free_page(GFP_KERNEL);
12989 if (!vmx_bitmap[i]) {
12990 nested_vmx_hardware_unsetup();
12991 return -ENOMEM;
12992 }
12993 }
12994
12995 init_vmcs_shadow_fields();
12996 }
12997
12998 return 0;
12999}
13000
13001static __init int hardware_setup(void)
13002{
13003 unsigned long host_bndcfgs;
13004 int r, i;
13005
13006 rdmsrl_safe(MSR_EFER, &host_efer);
13007
13008 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
13009 kvm_define_shared_msr(i, vmx_msr_index[i]);
13010
13011 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
13012 return -EIO;
13013
13014 if (boot_cpu_has(X86_FEATURE_NX))
13015 kvm_enable_efer_bits(EFER_NX);
13016
13017 if (boot_cpu_has(X86_FEATURE_MPX)) {
13018 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
13019 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
13020 }
13021
13022 if (boot_cpu_has(X86_FEATURE_XSAVES))
13023 rdmsrl(MSR_IA32_XSS, host_xss);
13024
13025 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
13026 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
13027 enable_vpid = 0;
13028
13029 if (!cpu_has_vmx_ept() ||
13030 !cpu_has_vmx_ept_4levels() ||
13031 !cpu_has_vmx_ept_mt_wb() ||
13032 !cpu_has_vmx_invept_global())
13033 enable_ept = 0;
13034
13035 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
13036 enable_ept_ad_bits = 0;
13037
13038 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
13039 enable_unrestricted_guest = 0;
13040
13041 if (!cpu_has_vmx_flexpriority())
13042 flexpriority_enabled = 0;
13043
13044 if (!cpu_has_virtual_nmis())
13045 enable_vnmi = 0;
13046
13047 /*
13048 * set_apic_access_page_addr() is used to reload apic access
13049 * page upon invalidation. No need to do anything if not
13050 * using the APIC_ACCESS_ADDR VMCS field.
13051 */
13052 if (!flexpriority_enabled)
13053 kvm_x86_ops->set_apic_access_page_addr = NULL;
13054
13055 if (!cpu_has_vmx_tpr_shadow())
13056 kvm_x86_ops->update_cr8_intercept = NULL;
13057
13058 if (enable_ept && !cpu_has_vmx_ept_2m_page())
13059 kvm_disable_largepages();
13060
13061#if IS_ENABLED(CONFIG_HYPERV)
13062 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
13063 && enable_ept)
13064 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
13065#endif
13066
13067 if (!cpu_has_vmx_ple()) {
13068 ple_gap = 0;
13069 ple_window = 0;
13070 ple_window_grow = 0;
13071 ple_window_max = 0;
13072 ple_window_shrink = 0;
13073 }
13074
13075 if (!cpu_has_vmx_apicv()) {
13076 enable_apicv = 0;
13077 kvm_x86_ops->sync_pir_to_irr = NULL;
13078 }
13079
13080 if (cpu_has_vmx_tsc_scaling()) {
13081 kvm_has_tsc_control = true;
13082 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
13083 kvm_tsc_scaling_ratio_frac_bits = 48;
13084 }
13085
13086 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
13087
13088 if (enable_ept)
13089 vmx_enable_tdp();
13090 else
13091 kvm_disable_tdp();
13092
13093 if (!nested) {
13094 kvm_x86_ops->get_nested_state = NULL;
13095 kvm_x86_ops->set_nested_state = NULL;
13096 }
13097
13098 /*
13099 * Only enable PML when hardware supports PML feature, and both EPT
13100 * and EPT A/D bit features are enabled -- PML depends on them to work.
13101 */
13102 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
13103 enable_pml = 0;
13104
13105 if (!enable_pml) {
13106 kvm_x86_ops->slot_enable_log_dirty = NULL;
13107 kvm_x86_ops->slot_disable_log_dirty = NULL;
13108 kvm_x86_ops->flush_log_dirty = NULL;
13109 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
13110 }
13111
13112 if (!cpu_has_vmx_preemption_timer())
13113 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
13114
13115 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
13116 u64 vmx_msr;
13117
13118 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
13119 cpu_preemption_timer_multi =
13120 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
13121 } else {
13122 kvm_x86_ops->set_hv_timer = NULL;
13123 kvm_x86_ops->cancel_hv_timer = NULL;
13124 }
13125
13126 if (!cpu_has_vmx_shadow_vmcs() || !nested)
13127 enable_shadow_vmcs = 0;
13128
13129 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
13130 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, vmx_capability.ept,
13131 enable_apicv);
13132
13133 kvm_mce_cap_supported |= MCG_LMCE_P;
13134
13135 if (nested) {
13136 r = nested_vmx_hardware_setup();
13137 if (r)
13138 return r;
13139 }
13140
13141 r = alloc_kvm_area();
13142 if (r)
13143 nested_vmx_hardware_unsetup();
13144 return r;
13145}
13146
13147static __exit void hardware_unsetup(void)
13148{
13149 if (nested)
13150 nested_vmx_hardware_unsetup();
13151
13152 free_kvm_area();
13153}
13154
404f6aac 13155static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
6aa8b732
AK
13156 .cpu_has_kvm_support = cpu_has_kvm_support,
13157 .disabled_by_bios = vmx_disabled_by_bios,
13158 .hardware_setup = hardware_setup,
13159 .hardware_unsetup = hardware_unsetup,
002c7f7c 13160 .check_processor_compatibility = vmx_check_processor_compat,
6aa8b732
AK
13161 .hardware_enable = hardware_enable,
13162 .hardware_disable = hardware_disable,
04547156 13163 .cpu_has_accelerated_tpr = report_flexpriority,
bc226f07 13164 .has_emulated_msr = vmx_has_emulated_msr,
6aa8b732 13165
b31c114b 13166 .vm_init = vmx_vm_init,
434a1e94
SC
13167 .vm_alloc = vmx_vm_alloc,
13168 .vm_free = vmx_vm_free,
b31c114b 13169
6aa8b732
AK
13170 .vcpu_create = vmx_create_vcpu,
13171 .vcpu_free = vmx_free_vcpu,
04d2cc77 13172 .vcpu_reset = vmx_vcpu_reset,
6aa8b732 13173
6d6095bd 13174 .prepare_guest_switch = vmx_prepare_switch_to_guest,
6aa8b732
AK
13175 .vcpu_load = vmx_vcpu_load,
13176 .vcpu_put = vmx_vcpu_put,
13177
a96036b8 13178 .update_bp_intercept = update_exception_bitmap,
801e459a 13179 .get_msr_feature = vmx_get_msr_feature,
6aa8b732
AK
13180 .get_msr = vmx_get_msr,
13181 .set_msr = vmx_set_msr,
13182 .get_segment_base = vmx_get_segment_base,
13183 .get_segment = vmx_get_segment,
13184 .set_segment = vmx_set_segment,
2e4d2653 13185 .get_cpl = vmx_get_cpl,
6aa8b732 13186 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
e8467fda 13187 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
aff48baa 13188 .decache_cr3 = vmx_decache_cr3,
25c4c276 13189 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
6aa8b732 13190 .set_cr0 = vmx_set_cr0,
6aa8b732
AK
13191 .set_cr3 = vmx_set_cr3,
13192 .set_cr4 = vmx_set_cr4,
6aa8b732 13193 .set_efer = vmx_set_efer,
6aa8b732
AK
13194 .get_idt = vmx_get_idt,
13195 .set_idt = vmx_set_idt,
13196 .get_gdt = vmx_get_gdt,
13197 .set_gdt = vmx_set_gdt,
73aaf249
JK
13198 .get_dr6 = vmx_get_dr6,
13199 .set_dr6 = vmx_set_dr6,
020df079 13200 .set_dr7 = vmx_set_dr7,
81908bf4 13201 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
5fdbf976 13202 .cache_reg = vmx_cache_reg,
6aa8b732
AK
13203 .get_rflags = vmx_get_rflags,
13204 .set_rflags = vmx_set_rflags,
be94f6b7 13205
6aa8b732 13206 .tlb_flush = vmx_flush_tlb,
faff8758 13207 .tlb_flush_gva = vmx_flush_tlb_gva,
6aa8b732 13208
6aa8b732 13209 .run = vmx_vcpu_run,
6062d012 13210 .handle_exit = vmx_handle_exit,
6aa8b732 13211 .skip_emulated_instruction = skip_emulated_instruction,
2809f5d2
GC
13212 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13213 .get_interrupt_shadow = vmx_get_interrupt_shadow,
102d8325 13214 .patch_hypercall = vmx_patch_hypercall,
2a8067f1 13215 .set_irq = vmx_inject_irq,
95ba8273 13216 .set_nmi = vmx_inject_nmi,
298101da 13217 .queue_exception = vmx_queue_exception,
b463a6f7 13218 .cancel_injection = vmx_cancel_injection,
78646121 13219 .interrupt_allowed = vmx_interrupt_allowed,
95ba8273 13220 .nmi_allowed = vmx_nmi_allowed,
3cfc3092
JK
13221 .get_nmi_mask = vmx_get_nmi_mask,
13222 .set_nmi_mask = vmx_set_nmi_mask,
95ba8273
GN
13223 .enable_nmi_window = enable_nmi_window,
13224 .enable_irq_window = enable_irq_window,
13225 .update_cr8_intercept = update_cr8_intercept,
8d860bbe 13226 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
38b99173 13227 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
d62caabb
AS
13228 .get_enable_apicv = vmx_get_enable_apicv,
13229 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
c7c9c56c 13230 .load_eoi_exitmap = vmx_load_eoi_exitmap,
967235d3 13231 .apicv_post_state_restore = vmx_apicv_post_state_restore,
c7c9c56c
YZ
13232 .hwapic_irr_update = vmx_hwapic_irr_update,
13233 .hwapic_isr_update = vmx_hwapic_isr_update,
e6c67d8c 13234 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
a20ed54d
YZ
13235 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13236 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
95ba8273 13237
cbc94022 13238 .set_tss_addr = vmx_set_tss_addr,
2ac52ab8 13239 .set_identity_map_addr = vmx_set_identity_map_addr,
67253af5 13240 .get_tdp_level = get_ept_level,
4b12f0de 13241 .get_mt_mask = vmx_get_mt_mask,
229456fc 13242
586f9607 13243 .get_exit_info = vmx_get_exit_info,
586f9607 13244
17cc3935 13245 .get_lpage_level = vmx_get_lpage_level,
0e851880
SY
13246
13247 .cpuid_update = vmx_cpuid_update,
4e47c7a6
SY
13248
13249 .rdtscp_supported = vmx_rdtscp_supported,
ad756a16 13250 .invpcid_supported = vmx_invpcid_supported,
d4330ef2
JR
13251
13252 .set_supported_cpuid = vmx_set_supported_cpuid,
f5f48ee1
SY
13253
13254 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
99e3e30a 13255
e79f245d 13256 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
326e7425 13257 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
1c97f0a0
JR
13258
13259 .set_tdp_cr3 = vmx_set_cr3,
8a76d7f2
JR
13260
13261 .check_intercept = vmx_check_intercept,
a547c6db 13262 .handle_external_intr = vmx_handle_external_intr,
da8999d3 13263 .mpx_supported = vmx_mpx_supported,
55412b2e 13264 .xsaves_supported = vmx_xsaves_supported,
66336cab 13265 .umip_emulated = vmx_umip_emulated,
b6b8a145
JK
13266
13267 .check_nested_events = vmx_check_nested_events,
d264ee0c 13268 .request_immediate_exit = vmx_request_immediate_exit,
ae97a3b8
RK
13269
13270 .sched_in = vmx_sched_in,
843e4330
KH
13271
13272 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13273 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13274 .flush_log_dirty = vmx_flush_log_dirty,
13275 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
c5f983f6 13276 .write_log_dirty = vmx_write_pml_buffer,
25462f7f 13277
bf9f6ac8
FW
13278 .pre_block = vmx_pre_block,
13279 .post_block = vmx_post_block,
13280
25462f7f 13281 .pmu_ops = &intel_pmu_ops,
efc64404
FW
13282
13283 .update_pi_irte = vmx_update_pi_irte,
64672c95
YJ
13284
13285#ifdef CONFIG_X86_64
13286 .set_hv_timer = vmx_set_hv_timer,
13287 .cancel_hv_timer = vmx_cancel_hv_timer,
13288#endif
c45dcc71
AR
13289
13290 .setup_mce = vmx_setup_mce,
0234bf88 13291
8fcc4b59
JM
13292 .get_nested_state = vmx_get_nested_state,
13293 .set_nested_state = vmx_set_nested_state,
7f7f1ba3
PB
13294 .get_vmcs12_pages = nested_get_vmcs12_pages,
13295
72d7b374 13296 .smi_allowed = vmx_smi_allowed,
0234bf88
LP
13297 .pre_enter_smm = vmx_pre_enter_smm,
13298 .pre_leave_smm = vmx_pre_leave_smm,
cc3d967f 13299 .enable_smi_window = enable_smi_window,
57b119da
VK
13300
13301 .nested_enable_evmcs = nested_enable_evmcs,
6aa8b732
AK
13302};
13303
72c6d2db 13304static void vmx_cleanup_l1d_flush(void)
a47dd5f0
PB
13305{
13306 if (vmx_l1d_flush_pages) {
13307 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13308 vmx_l1d_flush_pages = NULL;
13309 }
72c6d2db
TG
13310 /* Restore state so sysfs ignores VMX */
13311 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
a399477e
KRW
13312}
13313
a7b9020b
TG
13314static void vmx_exit(void)
13315{
13316#ifdef CONFIG_KEXEC_CORE
13317 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13318 synchronize_rcu();
13319#endif
13320
13321 kvm_exit();
13322
13323#if IS_ENABLED(CONFIG_HYPERV)
13324 if (static_branch_unlikely(&enable_evmcs)) {
13325 int cpu;
13326 struct hv_vp_assist_page *vp_ap;
13327 /*
13328 * Reset everything to support using non-enlightened VMCS
13329 * access later (e.g. when we reload the module with
13330 * enlightened_vmcs=0)
13331 */
13332 for_each_online_cpu(cpu) {
13333 vp_ap = hv_get_vp_assist_page(cpu);
13334
13335 if (!vp_ap)
13336 continue;
13337
13338 vp_ap->current_nested_vmcs = 0;
13339 vp_ap->enlighten_vmentry = 0;
13340 }
13341
13342 static_branch_disable(&enable_evmcs);
13343 }
13344#endif
13345 vmx_cleanup_l1d_flush();
13346}
13347module_exit(vmx_exit);
13348
6aa8b732
AK
13349static int __init vmx_init(void)
13350{
773e8a04
VK
13351 int r;
13352
13353#if IS_ENABLED(CONFIG_HYPERV)
13354 /*
13355 * Enlightened VMCS usage should be recommended and the host needs
13356 * to support eVMCS v1 or above. We can also disable eVMCS support
13357 * with module parameter.
13358 */
13359 if (enlightened_vmcs &&
13360 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13361 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13362 KVM_EVMCS_VERSION) {
13363 int cpu;
13364
13365 /* Check that we have assist pages on all online CPUs */
13366 for_each_online_cpu(cpu) {
13367 if (!hv_get_vp_assist_page(cpu)) {
13368 enlightened_vmcs = false;
13369 break;
13370 }
13371 }
13372
13373 if (enlightened_vmcs) {
13374 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13375 static_branch_enable(&enable_evmcs);
13376 }
13377 } else {
13378 enlightened_vmcs = false;
13379 }
13380#endif
13381
13382 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
a7b9020b 13383 __alignof__(struct vcpu_vmx), THIS_MODULE);
fdef3ad1 13384 if (r)
34a1cd60 13385 return r;
25c5f225 13386
a7b9020b 13387 /*
7db92e16
TG
13388 * Must be called after kvm_init() so enable_ept is properly set
13389 * up. Hand the parameter mitigation value in which was stored in
13390 * the pre module init parser. If no parameter was given, it will
13391 * contain 'auto' which will be turned into the default 'cond'
13392 * mitigation mode.
13393 */
13394 if (boot_cpu_has(X86_BUG_L1TF)) {
13395 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
13396 if (r) {
13397 vmx_exit();
13398 return r;
13399 }
a47dd5f0 13400 }
25c5f225 13401
2965faa5 13402#ifdef CONFIG_KEXEC_CORE
8f536b76
ZY
13403 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13404 crash_vmclear_local_loaded_vmcss);
13405#endif
21ebf53b 13406 vmx_check_vmcs12_offsets();
8f536b76 13407
fdef3ad1 13408 return 0;
6aa8b732 13409}
a7b9020b 13410module_init(vmx_init);