]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kvm/vmx.c
UBUNTU: SAUCE: Synchronize MDS mitigations with upstream
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kvm / vmx.c
CommitLineData
6aa8b732
AK
1/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
9611c187 8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
6aa8b732
AK
9 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
85f455f7 19#include "irq.h"
1d737c8a 20#include "mmu.h"
00b27a3e 21#include "cpuid.h"
d62caabb 22#include "lapic.h"
e495606d 23
edf88417 24#include <linux/kvm_host.h>
6aa8b732 25#include <linux/module.h>
9d8f549d 26#include <linux/kernel.h>
6aa8b732
AK
27#include <linux/mm.h>
28#include <linux/highmem.h>
e8edc6e0 29#include <linux/sched.h>
c7addb90 30#include <linux/moduleparam.h>
e9bda3b3 31#include <linux/mod_devicetable.h>
af658dca 32#include <linux/trace_events.h>
5a0e3ad6 33#include <linux/slab.h>
cafd6659 34#include <linux/tboot.h>
f4124500 35#include <linux/hrtimer.h>
c207aee4 36#include <linux/frame.h>
bcaf287c 37#include <linux/nospec.h>
5fdbf976 38#include "kvm_cache_regs.h"
35920a35 39#include "x86.h"
e495606d 40
28b835d6 41#include <asm/cpu.h>
6aa8b732 42#include <asm/io.h>
3b3be0d1 43#include <asm/desc.h>
13673a90 44#include <asm/vmx.h>
6210e37b 45#include <asm/virtext.h>
a0861c02 46#include <asm/mce.h>
952f07ec 47#include <asm/fpu/internal.h>
d7cd9796 48#include <asm/perf_event.h>
81908bf4 49#include <asm/debugreg.h>
8f536b76 50#include <asm/kexec.h>
dab2087d 51#include <asm/apic.h>
efc64404 52#include <asm/irq_remapping.h>
d6e41f11 53#include <asm/mmu_context.h>
f471d71b 54#include <asm/microcode.h>
d7a6a163 55#include <asm/spec-ctrl.h>
6aa8b732 56
229456fc 57#include "trace.h"
25462f7f 58#include "pmu.h"
229456fc 59
4ecac3fd 60#define __ex(x) __kvm_handle_fault_on_reboot(x)
5e520e62
AK
61#define __ex_clear(x, reg) \
62 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
4ecac3fd 63
6aa8b732
AK
64MODULE_AUTHOR("Qumranet");
65MODULE_LICENSE("GPL");
66
e9bda3b3
JT
67static const struct x86_cpu_id vmx_cpu_id[] = {
68 X86_FEATURE_MATCH(X86_FEATURE_VMX),
69 {}
70};
71MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
72
476bc001 73static bool __read_mostly enable_vpid = 1;
736caefe 74module_param_named(vpid, enable_vpid, bool, 0444);
2384d2b3 75
d02fcf50
PB
76static bool __read_mostly enable_vnmi = 1;
77module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
78
476bc001 79static bool __read_mostly flexpriority_enabled = 1;
736caefe 80module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
4c9fc8ef 81
476bc001 82static bool __read_mostly enable_ept = 1;
736caefe 83module_param_named(ept, enable_ept, bool, S_IRUGO);
d56f546d 84
476bc001 85static bool __read_mostly enable_unrestricted_guest = 1;
3a624e29
NK
86module_param_named(unrestricted_guest,
87 enable_unrestricted_guest, bool, S_IRUGO);
88
83c3a331
XH
89static bool __read_mostly enable_ept_ad_bits = 1;
90module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
91
a27685c3 92static bool __read_mostly emulate_invalid_guest_state = true;
c1f8bc04 93module_param(emulate_invalid_guest_state, bool, S_IRUGO);
04fa4d32 94
476bc001 95static bool __read_mostly fasteoi = 1;
58fbbf26
KT
96module_param(fasteoi, bool, S_IRUGO);
97
5a71785d 98static bool __read_mostly enable_apicv = 1;
01e439be 99module_param(enable_apicv, bool, S_IRUGO);
83d4c286 100
abc4fc58
AG
101static bool __read_mostly enable_shadow_vmcs = 1;
102module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
801d3424
NHE
103/*
104 * If nested=1, nested virtualization is supported, i.e., guests may use
105 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
106 * use VMX instructions.
107 */
476bc001 108static bool __read_mostly nested = 0;
801d3424
NHE
109module_param(nested, bool, S_IRUGO);
110
20300099
WL
111static u64 __read_mostly host_xss;
112
843e4330
KH
113static bool __read_mostly enable_pml = 1;
114module_param_named(pml, enable_pml, bool, S_IRUGO);
115
4b0be90f
PB
116#define MSR_TYPE_R 1
117#define MSR_TYPE_W 2
118#define MSR_TYPE_RW 3
119
120#define MSR_BITMAP_MODE_X2APIC 1
121#define MSR_BITMAP_MODE_X2APIC_APICV 2
122#define MSR_BITMAP_MODE_LM 4
123
64903d61
HZ
124#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
125
64672c95
YJ
126/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
127static int __read_mostly cpu_preemption_timer_multi;
128static bool __read_mostly enable_preemption_timer = 1;
129#ifdef CONFIG_X86_64
130module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
131#endif
132
5037878e
GN
133#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
134#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
cdc0e244
AK
135#define KVM_VM_CR0_ALWAYS_ON \
136 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
4c38609a
AK
137#define KVM_CR4_GUEST_OWNED_BITS \
138 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
fd8cb433 139 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
4c38609a 140
cdc0e244
AK
141#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
142#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
143
78ac8b47
AK
144#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
145
f4124500
JK
146#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
147
16c2aec6
JD
148/*
149 * Hyper-V requires all of these, so mark them as supported even though
150 * they are just treated the same as all-context.
151 */
152#define VMX_VPID_EXTENT_SUPPORTED_MASK \
153 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
154 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
155 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
156 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
157
4b8d54f9
ZE
158/*
159 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
160 * ple_gap: upper bound on the amount of time between two successive
161 * executions of PAUSE in a loop. Also indicate if ple enabled.
00c25bce 162 * According to test, this time is usually smaller than 128 cycles.
4b8d54f9
ZE
163 * ple_window: upper bound on the amount of time a guest is allowed to execute
164 * in a PAUSE loop. Tests indicate that most spinlocks are held for
165 * less than 2^12 cycles
166 * Time is measured based on a counter that runs at the same rate as the TSC,
167 * refer SDM volume 3b section 21.6.13 & 22.1.3.
168 */
b4a2d31d
RK
169#define KVM_VMX_DEFAULT_PLE_GAP 128
170#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
171#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
172#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
173#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
174 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
175
4b8d54f9
ZE
176static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
177module_param(ple_gap, int, S_IRUGO);
178
179static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
180module_param(ple_window, int, S_IRUGO);
181
b4a2d31d
RK
182/* Default doubles per-vcpu window every exit. */
183static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
184module_param(ple_window_grow, int, S_IRUGO);
185
186/* Default resets per-vcpu window every exit to ple_window. */
187static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
188module_param(ple_window_shrink, int, S_IRUGO);
189
190/* Default is to compute the maximum so we can never overflow. */
191static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
192static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
193module_param(ple_window_max, int, S_IRUGO);
194
83287ea4
AK
195extern const ulong vmx_return;
196
1749555e 197static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
12960b11 198static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
eeec2ec1 199static DEFINE_MUTEX(vmx_l1d_flush_mutex);
1749555e 200
2bcd5b01
TG
201/* Storage for pre module init parameter parsing */
202static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
1749555e
KRW
203
204static const struct {
205 const char *option;
1dd6f98d 206 bool for_parse;
1749555e 207} vmentry_l1d_param[] = {
1dd6f98d
PB
208 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
209 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
210 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
211 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
212 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
213 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
1749555e
KRW
214};
215
2bcd5b01
TG
216#define L1D_CACHE_ORDER 4
217static void *vmx_l1d_flush_pages;
218
219static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
1749555e 220{
2bcd5b01 221 struct page *page;
55bd6950 222 unsigned int i;
1749555e 223
2bcd5b01
TG
224 if (!enable_ept) {
225 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
226 return 0;
227 }
228
917d427f
JH
229 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
230 u64 msr;
231
232 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
233 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
234 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
235 return 0;
236 }
237 }
364a4311 238
24fcb53c
JK
239 /* If set to auto use the default l1tf mitigation method */
240 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
241 switch (l1tf_mitigation) {
242 case L1TF_MITIGATION_OFF:
243 l1tf = VMENTER_L1D_FLUSH_NEVER;
244 break;
245 case L1TF_MITIGATION_FLUSH_NOWARN:
246 case L1TF_MITIGATION_FLUSH:
247 case L1TF_MITIGATION_FLUSH_NOSMT:
248 l1tf = VMENTER_L1D_FLUSH_COND;
249 break;
250 case L1TF_MITIGATION_FULL:
251 case L1TF_MITIGATION_FULL_FORCE:
252 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
253 break;
254 }
255 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
256 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
257 }
258
2bcd5b01
TG
259 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
260 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
261 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
262 if (!page)
263 return -ENOMEM;
264 vmx_l1d_flush_pages = page_address(page);
55bd6950
NS
265
266 /*
267 * Initialize each page with a different pattern in
268 * order to protect against KSM in the nested
269 * virtualization case.
270 */
271 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
272 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
273 PAGE_SIZE);
274 }
1749555e
KRW
275 }
276
2bcd5b01
TG
277 l1tf_vmx_mitigation = l1tf;
278
522c7bed
TG
279 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
280 static_branch_enable(&vmx_l1d_should_flush);
281 else
282 static_branch_disable(&vmx_l1d_should_flush);
ae021965 283
12960b11
NS
284 if (l1tf == VMENTER_L1D_FLUSH_COND)
285 static_branch_enable(&vmx_l1d_flush_cond);
522c7bed 286 else
12960b11 287 static_branch_disable(&vmx_l1d_flush_cond);
2bcd5b01
TG
288 return 0;
289}
290
291static int vmentry_l1d_flush_parse(const char *s)
292{
293 unsigned int i;
294
295 if (s) {
296 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
1dd6f98d
PB
297 if (vmentry_l1d_param[i].for_parse &&
298 sysfs_streq(s, vmentry_l1d_param[i].option))
299 return i;
2bcd5b01
TG
300 }
301 }
1749555e
KRW
302 return -EINVAL;
303}
304
2bcd5b01
TG
305static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
306{
eeec2ec1 307 int l1tf, ret;
2bcd5b01 308
2bcd5b01
TG
309 l1tf = vmentry_l1d_flush_parse(s);
310 if (l1tf < 0)
311 return l1tf;
312
1dd6f98d
PB
313 if (!boot_cpu_has(X86_BUG_L1TF))
314 return 0;
315
2bcd5b01
TG
316 /*
317 * Has vmx_init() run already? If not then this is the pre init
318 * parameter parsing. In that case just store the value and let
319 * vmx_init() do the proper setup after enable_ept has been
320 * established.
321 */
322 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
323 vmentry_l1d_flush_param = l1tf;
324 return 0;
325 }
326
eeec2ec1
TG
327 mutex_lock(&vmx_l1d_flush_mutex);
328 ret = vmx_setup_l1d_flush(l1tf);
329 mutex_unlock(&vmx_l1d_flush_mutex);
330 return ret;
2bcd5b01
TG
331}
332
1749555e
KRW
333static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
334{
1dd6f98d
PB
335 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
336 return sprintf(s, "???\n");
337
2bcd5b01 338 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
1749555e
KRW
339}
340
341static const struct kernel_param_ops vmentry_l1d_flush_ops = {
342 .set = vmentry_l1d_flush_set,
343 .get = vmentry_l1d_flush_get,
344};
522c7bed 345module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
1749555e 346
8bf00a52 347#define NR_AUTOLOAD_MSRS 8
61d2ef2c 348
a2fa3e9f
GH
349struct vmcs {
350 u32 revision_id;
351 u32 abort;
352 char data[0];
353};
354
d462b819
NHE
355/*
356 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
357 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
358 * loaded on this CPU (so we can clear them if the CPU goes down).
359 */
360struct loaded_vmcs {
361 struct vmcs *vmcs;
355f4fb1 362 struct vmcs *shadow_vmcs;
d462b819 363 int cpu;
4c4a6f79
PB
364 bool launched;
365 bool nmi_known_unmasked;
44889942
LP
366 unsigned long vmcs_host_cr3; /* May not match real cr3 */
367 unsigned long vmcs_host_cr4; /* May not match real cr4 */
8a1b4392
PB
368 /* Support for vnmi-less CPUs */
369 int soft_vnmi_blocked;
370 ktime_t entry_time;
371 s64 vnmi_blocked_time;
4b0be90f 372 unsigned long *msr_bitmap;
d462b819
NHE
373 struct list_head loaded_vmcss_on_cpu_link;
374};
375
26bb0981
AK
376struct shared_msr_entry {
377 unsigned index;
378 u64 data;
d5696725 379 u64 mask;
26bb0981
AK
380};
381
a9d30f33
NHE
382/*
383 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
384 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
385 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
386 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
387 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
388 * More than one of these structures may exist, if L1 runs multiple L2 guests.
8819227c 389 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
a9d30f33
NHE
390 * underlying hardware which will be used to run L2.
391 * This structure is packed to ensure that its layout is identical across
392 * machines (necessary for live migration).
393 * If there are changes in this struct, VMCS12_REVISION must be changed.
394 */
22bd0358 395typedef u64 natural_width;
a9d30f33
NHE
396struct __packed vmcs12 {
397 /* According to the Intel spec, a VMCS region must start with the
398 * following two fields. Then follow implementation-specific data.
399 */
400 u32 revision_id;
401 u32 abort;
22bd0358 402
27d6c865
NHE
403 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
404 u32 padding[7]; /* room for future expansion */
405
22bd0358
NHE
406 u64 io_bitmap_a;
407 u64 io_bitmap_b;
408 u64 msr_bitmap;
409 u64 vm_exit_msr_store_addr;
410 u64 vm_exit_msr_load_addr;
411 u64 vm_entry_msr_load_addr;
412 u64 tsc_offset;
413 u64 virtual_apic_page_addr;
414 u64 apic_access_addr;
705699a1 415 u64 posted_intr_desc_addr;
27c42a1b 416 u64 vm_function_control;
22bd0358 417 u64 ept_pointer;
608406e2
WV
418 u64 eoi_exit_bitmap0;
419 u64 eoi_exit_bitmap1;
420 u64 eoi_exit_bitmap2;
421 u64 eoi_exit_bitmap3;
41ab9372 422 u64 eptp_list_address;
81dc01f7 423 u64 xss_exit_bitmap;
22bd0358
NHE
424 u64 guest_physical_address;
425 u64 vmcs_link_pointer;
c5f983f6 426 u64 pml_address;
22bd0358
NHE
427 u64 guest_ia32_debugctl;
428 u64 guest_ia32_pat;
429 u64 guest_ia32_efer;
430 u64 guest_ia32_perf_global_ctrl;
431 u64 guest_pdptr0;
432 u64 guest_pdptr1;
433 u64 guest_pdptr2;
434 u64 guest_pdptr3;
36be0b9d 435 u64 guest_bndcfgs;
22bd0358
NHE
436 u64 host_ia32_pat;
437 u64 host_ia32_efer;
438 u64 host_ia32_perf_global_ctrl;
439 u64 padding64[8]; /* room for future expansion */
440 /*
441 * To allow migration of L1 (complete with its L2 guests) between
442 * machines of different natural widths (32 or 64 bit), we cannot have
443 * unsigned long fields with no explict size. We use u64 (aliased
444 * natural_width) instead. Luckily, x86 is little-endian.
445 */
446 natural_width cr0_guest_host_mask;
447 natural_width cr4_guest_host_mask;
448 natural_width cr0_read_shadow;
449 natural_width cr4_read_shadow;
450 natural_width cr3_target_value0;
451 natural_width cr3_target_value1;
452 natural_width cr3_target_value2;
453 natural_width cr3_target_value3;
454 natural_width exit_qualification;
455 natural_width guest_linear_address;
456 natural_width guest_cr0;
457 natural_width guest_cr3;
458 natural_width guest_cr4;
459 natural_width guest_es_base;
460 natural_width guest_cs_base;
461 natural_width guest_ss_base;
462 natural_width guest_ds_base;
463 natural_width guest_fs_base;
464 natural_width guest_gs_base;
465 natural_width guest_ldtr_base;
466 natural_width guest_tr_base;
467 natural_width guest_gdtr_base;
468 natural_width guest_idtr_base;
469 natural_width guest_dr7;
470 natural_width guest_rsp;
471 natural_width guest_rip;
472 natural_width guest_rflags;
473 natural_width guest_pending_dbg_exceptions;
474 natural_width guest_sysenter_esp;
475 natural_width guest_sysenter_eip;
476 natural_width host_cr0;
477 natural_width host_cr3;
478 natural_width host_cr4;
479 natural_width host_fs_base;
480 natural_width host_gs_base;
481 natural_width host_tr_base;
482 natural_width host_gdtr_base;
483 natural_width host_idtr_base;
484 natural_width host_ia32_sysenter_esp;
485 natural_width host_ia32_sysenter_eip;
486 natural_width host_rsp;
487 natural_width host_rip;
488 natural_width paddingl[8]; /* room for future expansion */
489 u32 pin_based_vm_exec_control;
490 u32 cpu_based_vm_exec_control;
491 u32 exception_bitmap;
492 u32 page_fault_error_code_mask;
493 u32 page_fault_error_code_match;
494 u32 cr3_target_count;
495 u32 vm_exit_controls;
496 u32 vm_exit_msr_store_count;
497 u32 vm_exit_msr_load_count;
498 u32 vm_entry_controls;
499 u32 vm_entry_msr_load_count;
500 u32 vm_entry_intr_info_field;
501 u32 vm_entry_exception_error_code;
502 u32 vm_entry_instruction_len;
503 u32 tpr_threshold;
504 u32 secondary_vm_exec_control;
505 u32 vm_instruction_error;
506 u32 vm_exit_reason;
507 u32 vm_exit_intr_info;
508 u32 vm_exit_intr_error_code;
509 u32 idt_vectoring_info_field;
510 u32 idt_vectoring_error_code;
511 u32 vm_exit_instruction_len;
512 u32 vmx_instruction_info;
513 u32 guest_es_limit;
514 u32 guest_cs_limit;
515 u32 guest_ss_limit;
516 u32 guest_ds_limit;
517 u32 guest_fs_limit;
518 u32 guest_gs_limit;
519 u32 guest_ldtr_limit;
520 u32 guest_tr_limit;
521 u32 guest_gdtr_limit;
522 u32 guest_idtr_limit;
523 u32 guest_es_ar_bytes;
524 u32 guest_cs_ar_bytes;
525 u32 guest_ss_ar_bytes;
526 u32 guest_ds_ar_bytes;
527 u32 guest_fs_ar_bytes;
528 u32 guest_gs_ar_bytes;
529 u32 guest_ldtr_ar_bytes;
530 u32 guest_tr_ar_bytes;
531 u32 guest_interruptibility_info;
532 u32 guest_activity_state;
533 u32 guest_sysenter_cs;
534 u32 host_ia32_sysenter_cs;
0238ea91
JK
535 u32 vmx_preemption_timer_value;
536 u32 padding32[7]; /* room for future expansion */
22bd0358 537 u16 virtual_processor_id;
705699a1 538 u16 posted_intr_nv;
22bd0358
NHE
539 u16 guest_es_selector;
540 u16 guest_cs_selector;
541 u16 guest_ss_selector;
542 u16 guest_ds_selector;
543 u16 guest_fs_selector;
544 u16 guest_gs_selector;
545 u16 guest_ldtr_selector;
546 u16 guest_tr_selector;
608406e2 547 u16 guest_intr_status;
c5f983f6 548 u16 guest_pml_index;
22bd0358
NHE
549 u16 host_es_selector;
550 u16 host_cs_selector;
551 u16 host_ss_selector;
552 u16 host_ds_selector;
553 u16 host_fs_selector;
554 u16 host_gs_selector;
555 u16 host_tr_selector;
a9d30f33
NHE
556};
557
558/*
559 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
560 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
561 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
562 */
563#define VMCS12_REVISION 0x11e57ed0
564
565/*
566 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
567 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
568 * current implementation, 4K are reserved to avoid future complications.
569 */
570#define VMCS12_SIZE 0x1000
571
ec378aee
NHE
572/*
573 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
574 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
575 */
576struct nested_vmx {
577 /* Has the level1 guest done vmxon? */
578 bool vmxon;
3573e22c 579 gpa_t vmxon_ptr;
c5f983f6 580 bool pml_full;
a9d30f33
NHE
581
582 /* The guest-physical address of the current VMCS L1 keeps for L2 */
583 gpa_t current_vmptr;
4f2777bc
DM
584 /*
585 * Cache of the guest's VMCS, existing outside of guest memory.
586 * Loaded from guest memory during VMPTRLD. Flushed to guest
8ca44e88 587 * memory during VMCLEAR and VMPTRLD.
4f2777bc
DM
588 */
589 struct vmcs12 *cached_vmcs12;
012f83cb
AG
590 /*
591 * Indicates if the shadow vmcs must be updated with the
592 * data hold by vmcs12
593 */
594 bool sync_shadow_vmcs;
ff2f6fe9 595
dccbfcf5 596 bool change_vmcs01_virtual_x2apic_mode;
644d711a
NHE
597 /* L2 must run next, and mustn't decide to exit to L1. */
598 bool nested_run_pending;
8819227c
JM
599
600 struct loaded_vmcs vmcs02;
601
fe3ef05c 602 /*
8819227c
JM
603 * Guest pages referred to in the vmcs02 with host-physical
604 * pointers, so we must keep them pinned while L2 runs.
fe3ef05c
NHE
605 */
606 struct page *apic_access_page;
a7c0b07d 607 struct page *virtual_apic_page;
705699a1
WV
608 struct page *pi_desc_page;
609 struct pi_desc *pi_desc;
610 bool pi_pending;
611 u16 posted_intr_nv;
f4124500
JK
612
613 struct hrtimer preemption_timer;
614 bool preemption_timer_expired;
2996fca0
JK
615
616 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
617 u64 vmcs01_debugctl;
b9c237bb 618
5c614b35
WL
619 u16 vpid02;
620 u16 last_vpid;
621
0115f9cb
DM
622 /*
623 * We only store the "true" versions of the VMX capability MSRs. We
624 * generate the "non-true" versions by setting the must-be-1 bits
625 * according to the SDM.
626 */
b9c237bb
WV
627 u32 nested_vmx_procbased_ctls_low;
628 u32 nested_vmx_procbased_ctls_high;
b9c237bb
WV
629 u32 nested_vmx_secondary_ctls_low;
630 u32 nested_vmx_secondary_ctls_high;
631 u32 nested_vmx_pinbased_ctls_low;
632 u32 nested_vmx_pinbased_ctls_high;
633 u32 nested_vmx_exit_ctls_low;
634 u32 nested_vmx_exit_ctls_high;
b9c237bb
WV
635 u32 nested_vmx_entry_ctls_low;
636 u32 nested_vmx_entry_ctls_high;
b9c237bb
WV
637 u32 nested_vmx_misc_low;
638 u32 nested_vmx_misc_high;
639 u32 nested_vmx_ept_caps;
99b83ac8 640 u32 nested_vmx_vpid_caps;
62cc6b9d
DM
641 u64 nested_vmx_basic;
642 u64 nested_vmx_cr0_fixed0;
643 u64 nested_vmx_cr0_fixed1;
644 u64 nested_vmx_cr4_fixed0;
645 u64 nested_vmx_cr4_fixed1;
646 u64 nested_vmx_vmcs_enum;
27c42a1b 647 u64 nested_vmx_vmfunc_controls;
72e9cbdb
LP
648
649 /* SMM related state */
650 struct {
651 /* in VMX operation on SMM entry? */
652 bool vmxon;
653 /* in guest mode on SMM entry? */
654 bool guest_mode;
655 } smm;
ec378aee
NHE
656};
657
01e439be 658#define POSTED_INTR_ON 0
ebbfc765
FW
659#define POSTED_INTR_SN 1
660
01e439be
YZ
661/* Posted-Interrupt Descriptor */
662struct pi_desc {
663 u32 pir[8]; /* Posted interrupt requested */
6ef1522f
FW
664 union {
665 struct {
666 /* bit 256 - Outstanding Notification */
667 u16 on : 1,
668 /* bit 257 - Suppress Notification */
669 sn : 1,
670 /* bit 271:258 - Reserved */
671 rsvd_1 : 14;
672 /* bit 279:272 - Notification Vector */
673 u8 nv;
674 /* bit 287:280 - Reserved */
675 u8 rsvd_2;
676 /* bit 319:288 - Notification Destination */
677 u32 ndst;
678 };
679 u64 control;
680 };
681 u32 rsvd[6];
01e439be
YZ
682} __aligned(64);
683
a20ed54d
YZ
684static bool pi_test_and_set_on(struct pi_desc *pi_desc)
685{
686 return test_and_set_bit(POSTED_INTR_ON,
687 (unsigned long *)&pi_desc->control);
688}
689
690static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
691{
692 return test_and_clear_bit(POSTED_INTR_ON,
693 (unsigned long *)&pi_desc->control);
694}
695
696static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
697{
698 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
699}
700
ebbfc765
FW
701static inline void pi_clear_sn(struct pi_desc *pi_desc)
702{
703 return clear_bit(POSTED_INTR_SN,
704 (unsigned long *)&pi_desc->control);
705}
706
707static inline void pi_set_sn(struct pi_desc *pi_desc)
708{
709 return set_bit(POSTED_INTR_SN,
710 (unsigned long *)&pi_desc->control);
711}
712
ad361091
PB
713static inline void pi_clear_on(struct pi_desc *pi_desc)
714{
715 clear_bit(POSTED_INTR_ON,
716 (unsigned long *)&pi_desc->control);
717}
718
ebbfc765
FW
719static inline int pi_test_on(struct pi_desc *pi_desc)
720{
721 return test_bit(POSTED_INTR_ON,
722 (unsigned long *)&pi_desc->control);
723}
724
725static inline int pi_test_sn(struct pi_desc *pi_desc)
726{
727 return test_bit(POSTED_INTR_SN,
728 (unsigned long *)&pi_desc->control);
729}
730
6e3dedb6
KRW
731struct vmx_msrs {
732 unsigned int nr;
733 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
734};
735
a2fa3e9f 736struct vcpu_vmx {
fb3f0f51 737 struct kvm_vcpu vcpu;
313dbd49 738 unsigned long host_rsp;
29bd8a78 739 u8 fail;
4b0be90f 740 u8 msr_bitmap_mode;
51aa01d1 741 u32 exit_intr_info;
1155f76a 742 u32 idt_vectoring_info;
6de12732 743 ulong rflags;
26bb0981 744 struct shared_msr_entry *guest_msrs;
a2fa3e9f
GH
745 int nmsrs;
746 int save_nmsrs;
a547c6db 747 unsigned long host_idt_base;
a2fa3e9f 748#ifdef CONFIG_X86_64
44ea2b17
AK
749 u64 msr_host_kernel_gs_base;
750 u64 msr_guest_kernel_gs_base;
a2fa3e9f 751#endif
33241bfe 752
a6005a79 753 u64 arch_capabilities;
74469996 754 u64 spec_ctrl;
a6005a79 755
2961e876
GN
756 u32 vm_entry_controls_shadow;
757 u32 vm_exit_controls_shadow;
80154d77
PB
758 u32 secondary_exec_control;
759
d462b819
NHE
760 /*
761 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
762 * non-nested (L1) guest, it always points to vmcs01. For a nested
763 * guest (L2), it points to a different VMCS.
764 */
765 struct loaded_vmcs vmcs01;
766 struct loaded_vmcs *loaded_vmcs;
767 bool __launched; /* temporary, used in vmx_vcpu_run */
61d2ef2c 768 struct msr_autoload {
6e3dedb6
KRW
769 struct vmx_msrs guest;
770 struct vmx_msrs host;
61d2ef2c 771 } msr_autoload;
a2fa3e9f
GH
772 struct {
773 int loaded;
774 u16 fs_sel, gs_sel, ldt_sel;
b2da15ac
AK
775#ifdef CONFIG_X86_64
776 u16 ds_sel, es_sel;
777#endif
152d3f2f
LV
778 int gs_ldt_reload_needed;
779 int fs_reload_needed;
da8999d3 780 u64 msr_host_bndcfgs;
d77c26fc 781 } host_state;
9c8cba37 782 struct {
7ffd92c5 783 int vm86_active;
78ac8b47 784 ulong save_rflags;
f5f7b2fe
AK
785 struct kvm_segment segs[8];
786 } rmode;
787 struct {
788 u32 bitmask; /* 4 bits per segment (1 bit per field) */
7ffd92c5
AK
789 struct kvm_save_segment {
790 u16 selector;
791 unsigned long base;
792 u32 limit;
793 u32 ar;
f5f7b2fe 794 } seg[8];
2fb92db1 795 } segment_cache;
2384d2b3 796 int vpid;
04fa4d32 797 bool emulation_required;
3b86cd99 798
a0861c02 799 u32 exit_reason;
4e47c7a6 800
01e439be
YZ
801 /* Posted interrupt descriptor */
802 struct pi_desc pi_desc;
803
ec378aee
NHE
804 /* Support for a guest hypervisor (nested VMX) */
805 struct nested_vmx nested;
a7653ecd
RK
806
807 /* Dynamic PLE window. */
808 int ple_window;
809 bool ple_window_dirty;
843e4330
KH
810
811 /* Support for PML */
812#define PML_ENTITY_NUM 512
813 struct page *pml_pg;
2680d6da 814
64672c95
YJ
815 /* apic deadline value in host tsc */
816 u64 hv_deadline_tsc;
817
2680d6da 818 u64 current_tsc_ratio;
1be0e61c 819
1be0e61c 820 u32 host_pkru;
3b84080b 821
37e4c997
HZ
822 /*
823 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
824 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
825 * in msr_ia32_feature_control_valid_bits.
826 */
3b84080b 827 u64 msr_ia32_feature_control;
37e4c997 828 u64 msr_ia32_feature_control_valid_bits;
a2fa3e9f
GH
829};
830
2fb92db1
AK
831enum segment_cache_field {
832 SEG_FIELD_SEL = 0,
833 SEG_FIELD_BASE = 1,
834 SEG_FIELD_LIMIT = 2,
835 SEG_FIELD_AR = 3,
836
837 SEG_FIELD_NR = 4
838};
839
a2fa3e9f
GH
840static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
841{
fb3f0f51 842 return container_of(vcpu, struct vcpu_vmx, vcpu);
a2fa3e9f
GH
843}
844
efc64404
FW
845static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
846{
847 return &(to_vmx(vcpu)->pi_desc);
848}
849
22bd0358
NHE
850#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
851#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
852#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
853 [number##_HIGH] = VMCS12_OFFSET(name)+4
854
4607c2d7 855
fe2b201b 856static unsigned long shadow_read_only_fields[] = {
4607c2d7
AG
857 /*
858 * We do NOT shadow fields that are modified when L0
859 * traps and emulates any vmx instruction (e.g. VMPTRLD,
860 * VMXON...) executed by L1.
861 * For example, VM_INSTRUCTION_ERROR is read
862 * by L1 if a vmx instruction fails (part of the error path).
863 * Note the code assumes this logic. If for some reason
864 * we start shadowing these fields then we need to
865 * force a shadow sync when L0 emulates vmx instructions
866 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
867 * by nested_vmx_failValid)
868 */
869 VM_EXIT_REASON,
870 VM_EXIT_INTR_INFO,
871 VM_EXIT_INSTRUCTION_LEN,
872 IDT_VECTORING_INFO_FIELD,
873 IDT_VECTORING_ERROR_CODE,
874 VM_EXIT_INTR_ERROR_CODE,
875 EXIT_QUALIFICATION,
876 GUEST_LINEAR_ADDRESS,
877 GUEST_PHYSICAL_ADDRESS
878};
fe2b201b 879static int max_shadow_read_only_fields =
4607c2d7
AG
880 ARRAY_SIZE(shadow_read_only_fields);
881
fe2b201b 882static unsigned long shadow_read_write_fields[] = {
a7c0b07d 883 TPR_THRESHOLD,
4607c2d7
AG
884 GUEST_RIP,
885 GUEST_RSP,
886 GUEST_CR0,
887 GUEST_CR3,
888 GUEST_CR4,
889 GUEST_INTERRUPTIBILITY_INFO,
890 GUEST_RFLAGS,
891 GUEST_CS_SELECTOR,
892 GUEST_CS_AR_BYTES,
893 GUEST_CS_LIMIT,
894 GUEST_CS_BASE,
895 GUEST_ES_BASE,
36be0b9d 896 GUEST_BNDCFGS,
4607c2d7
AG
897 CR0_GUEST_HOST_MASK,
898 CR0_READ_SHADOW,
899 CR4_READ_SHADOW,
900 TSC_OFFSET,
901 EXCEPTION_BITMAP,
902 CPU_BASED_VM_EXEC_CONTROL,
903 VM_ENTRY_EXCEPTION_ERROR_CODE,
904 VM_ENTRY_INTR_INFO_FIELD,
905 VM_ENTRY_INSTRUCTION_LEN,
906 VM_ENTRY_EXCEPTION_ERROR_CODE,
907 HOST_FS_BASE,
908 HOST_GS_BASE,
909 HOST_FS_SELECTOR,
910 HOST_GS_SELECTOR
911};
fe2b201b 912static int max_shadow_read_write_fields =
4607c2d7
AG
913 ARRAY_SIZE(shadow_read_write_fields);
914
772e0318 915static const unsigned short vmcs_field_to_offset_table[] = {
22bd0358 916 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
705699a1 917 FIELD(POSTED_INTR_NV, posted_intr_nv),
22bd0358
NHE
918 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
919 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
920 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
921 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
922 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
923 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
924 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
925 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
608406e2 926 FIELD(GUEST_INTR_STATUS, guest_intr_status),
c5f983f6 927 FIELD(GUEST_PML_INDEX, guest_pml_index),
22bd0358
NHE
928 FIELD(HOST_ES_SELECTOR, host_es_selector),
929 FIELD(HOST_CS_SELECTOR, host_cs_selector),
930 FIELD(HOST_SS_SELECTOR, host_ss_selector),
931 FIELD(HOST_DS_SELECTOR, host_ds_selector),
932 FIELD(HOST_FS_SELECTOR, host_fs_selector),
933 FIELD(HOST_GS_SELECTOR, host_gs_selector),
934 FIELD(HOST_TR_SELECTOR, host_tr_selector),
935 FIELD64(IO_BITMAP_A, io_bitmap_a),
936 FIELD64(IO_BITMAP_B, io_bitmap_b),
937 FIELD64(MSR_BITMAP, msr_bitmap),
938 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
939 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
940 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
941 FIELD64(TSC_OFFSET, tsc_offset),
942 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
943 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
705699a1 944 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
27c42a1b 945 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
22bd0358 946 FIELD64(EPT_POINTER, ept_pointer),
608406e2
WV
947 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
948 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
949 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
950 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
41ab9372 951 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
81dc01f7 952 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
22bd0358
NHE
953 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
954 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
c5f983f6 955 FIELD64(PML_ADDRESS, pml_address),
22bd0358
NHE
956 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
957 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
958 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
959 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
960 FIELD64(GUEST_PDPTR0, guest_pdptr0),
961 FIELD64(GUEST_PDPTR1, guest_pdptr1),
962 FIELD64(GUEST_PDPTR2, guest_pdptr2),
963 FIELD64(GUEST_PDPTR3, guest_pdptr3),
36be0b9d 964 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
22bd0358
NHE
965 FIELD64(HOST_IA32_PAT, host_ia32_pat),
966 FIELD64(HOST_IA32_EFER, host_ia32_efer),
967 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
968 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
969 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
970 FIELD(EXCEPTION_BITMAP, exception_bitmap),
971 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
972 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
973 FIELD(CR3_TARGET_COUNT, cr3_target_count),
974 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
975 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
976 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
977 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
978 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
979 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
980 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
981 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
982 FIELD(TPR_THRESHOLD, tpr_threshold),
983 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
984 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
985 FIELD(VM_EXIT_REASON, vm_exit_reason),
986 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
987 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
988 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
989 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
990 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
991 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
992 FIELD(GUEST_ES_LIMIT, guest_es_limit),
993 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
994 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
995 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
996 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
997 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
998 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
999 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1000 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1001 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1002 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1003 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1004 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1005 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1006 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1007 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1008 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1009 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1010 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1011 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1012 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1013 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
0238ea91 1014 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
22bd0358
NHE
1015 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1016 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1017 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1018 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1019 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1020 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1021 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1022 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1023 FIELD(EXIT_QUALIFICATION, exit_qualification),
1024 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1025 FIELD(GUEST_CR0, guest_cr0),
1026 FIELD(GUEST_CR3, guest_cr3),
1027 FIELD(GUEST_CR4, guest_cr4),
1028 FIELD(GUEST_ES_BASE, guest_es_base),
1029 FIELD(GUEST_CS_BASE, guest_cs_base),
1030 FIELD(GUEST_SS_BASE, guest_ss_base),
1031 FIELD(GUEST_DS_BASE, guest_ds_base),
1032 FIELD(GUEST_FS_BASE, guest_fs_base),
1033 FIELD(GUEST_GS_BASE, guest_gs_base),
1034 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1035 FIELD(GUEST_TR_BASE, guest_tr_base),
1036 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1037 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1038 FIELD(GUEST_DR7, guest_dr7),
1039 FIELD(GUEST_RSP, guest_rsp),
1040 FIELD(GUEST_RIP, guest_rip),
1041 FIELD(GUEST_RFLAGS, guest_rflags),
1042 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1043 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1044 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1045 FIELD(HOST_CR0, host_cr0),
1046 FIELD(HOST_CR3, host_cr3),
1047 FIELD(HOST_CR4, host_cr4),
1048 FIELD(HOST_FS_BASE, host_fs_base),
1049 FIELD(HOST_GS_BASE, host_gs_base),
1050 FIELD(HOST_TR_BASE, host_tr_base),
1051 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1052 FIELD(HOST_IDTR_BASE, host_idtr_base),
1053 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1054 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1055 FIELD(HOST_RSP, host_rsp),
1056 FIELD(HOST_RIP, host_rip),
1057};
22bd0358
NHE
1058
1059static inline short vmcs_field_to_offset(unsigned long field)
1060{
bcaf287c
DW
1061 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1062 unsigned short offset;
a2ae9df7 1063
bcaf287c
DW
1064 BUILD_BUG_ON(size > SHRT_MAX);
1065 if (field >= size)
75f139aa
AH
1066 return -ENOENT;
1067
bcaf287c
DW
1068 field = array_index_nospec(field, size);
1069 offset = vmcs_field_to_offset_table[field];
1070 if (offset == 0)
a2ae9df7 1071 return -ENOENT;
bcaf287c 1072 return offset;
22bd0358
NHE
1073}
1074
a9d30f33
NHE
1075static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1076{
4f2777bc 1077 return to_vmx(vcpu)->nested.cached_vmcs12;
a9d30f33
NHE
1078}
1079
995f00a6 1080static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
bfd0a56b 1081static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
995f00a6 1082static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
f53cd63c 1083static bool vmx_xsaves_supported(void);
b246dd5d
OW
1084static void vmx_set_segment(struct kvm_vcpu *vcpu,
1085 struct kvm_segment *var, int seg);
1086static void vmx_get_segment(struct kvm_vcpu *vcpu,
1087 struct kvm_segment *var, int seg);
d99e4152
GN
1088static bool guest_state_valid(struct kvm_vcpu *vcpu);
1089static u32 vmx_segment_access_rights(struct kvm_segment *var);
16f5b903 1090static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
b96fb439
PB
1091static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1092static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1093static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1094 u16 error_code);
4b0be90f 1095static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
33241bfe
AR
1096static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1097 u32 msr, int type);
75880a01 1098
6aa8b732
AK
1099static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1100static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
d462b819
NHE
1101/*
1102 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1103 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1104 */
1105static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
6aa8b732 1106
bf9f6ac8
FW
1107/*
1108 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1109 * can find which vCPU should be waken up.
1110 */
1111static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1112static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1113
23611332
RK
1114enum {
1115 VMX_IO_BITMAP_A,
1116 VMX_IO_BITMAP_B,
23611332
RK
1117 VMX_VMREAD_BITMAP,
1118 VMX_VMWRITE_BITMAP,
1119 VMX_BITMAP_NR
1120};
1121
1122static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1123
1124#define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A])
1125#define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B])
23611332
RK
1126#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1127#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
fdef3ad1 1128
110312c8 1129static bool cpu_has_load_ia32_efer;
8bf00a52 1130static bool cpu_has_load_perf_global_ctrl;
110312c8 1131
2384d2b3
SY
1132static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1133static DEFINE_SPINLOCK(vmx_vpid_lock);
1134
1c3d14fe 1135static struct vmcs_config {
6aa8b732
AK
1136 int size;
1137 int order;
9ac7e3e8 1138 u32 basic_cap;
6aa8b732 1139 u32 revision_id;
1c3d14fe
YS
1140 u32 pin_based_exec_ctrl;
1141 u32 cpu_based_exec_ctrl;
f78e0e2e 1142 u32 cpu_based_2nd_exec_ctrl;
1c3d14fe
YS
1143 u32 vmexit_ctrl;
1144 u32 vmentry_ctrl;
1145} vmcs_config;
6aa8b732 1146
efff9e53 1147static struct vmx_capability {
d56f546d
SY
1148 u32 ept;
1149 u32 vpid;
1150} vmx_capability;
1151
6aa8b732
AK
1152#define VMX_SEGMENT_FIELD(seg) \
1153 [VCPU_SREG_##seg] = { \
1154 .selector = GUEST_##seg##_SELECTOR, \
1155 .base = GUEST_##seg##_BASE, \
1156 .limit = GUEST_##seg##_LIMIT, \
1157 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1158 }
1159
772e0318 1160static const struct kvm_vmx_segment_field {
6aa8b732
AK
1161 unsigned selector;
1162 unsigned base;
1163 unsigned limit;
1164 unsigned ar_bytes;
1165} kvm_vmx_segment_fields[] = {
1166 VMX_SEGMENT_FIELD(CS),
1167 VMX_SEGMENT_FIELD(DS),
1168 VMX_SEGMENT_FIELD(ES),
1169 VMX_SEGMENT_FIELD(FS),
1170 VMX_SEGMENT_FIELD(GS),
1171 VMX_SEGMENT_FIELD(SS),
1172 VMX_SEGMENT_FIELD(TR),
1173 VMX_SEGMENT_FIELD(LDTR),
1174};
1175
26bb0981
AK
1176static u64 host_efer;
1177
6de4f3ad
AK
1178static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1179
4d56c8a7 1180/*
8c06585d 1181 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
4d56c8a7
AK
1182 * away by decrementing the array size.
1183 */
6aa8b732 1184static const u32 vmx_msr_index[] = {
05b3e0c2 1185#ifdef CONFIG_X86_64
44ea2b17 1186 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
6aa8b732 1187#endif
8c06585d 1188 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
6aa8b732 1189};
6aa8b732 1190
5bb16016 1191static inline bool is_exception_n(u32 intr_info, u8 vector)
6aa8b732
AK
1192{
1193 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1194 INTR_INFO_VALID_MASK)) ==
5bb16016
JK
1195 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1196}
1197
6f05485d
JK
1198static inline bool is_debug(u32 intr_info)
1199{
1200 return is_exception_n(intr_info, DB_VECTOR);
1201}
1202
1203static inline bool is_breakpoint(u32 intr_info)
1204{
1205 return is_exception_n(intr_info, BP_VECTOR);
1206}
1207
5bb16016
JK
1208static inline bool is_page_fault(u32 intr_info)
1209{
1210 return is_exception_n(intr_info, PF_VECTOR);
6aa8b732
AK
1211}
1212
31299944 1213static inline bool is_no_device(u32 intr_info)
2ab455cc 1214{
5bb16016 1215 return is_exception_n(intr_info, NM_VECTOR);
2ab455cc
AL
1216}
1217
31299944 1218static inline bool is_invalid_opcode(u32 intr_info)
7aa81cc0 1219{
5bb16016 1220 return is_exception_n(intr_info, UD_VECTOR);
7aa81cc0
AL
1221}
1222
31299944 1223static inline bool is_external_interrupt(u32 intr_info)
6aa8b732
AK
1224{
1225 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1226 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1227}
1228
31299944 1229static inline bool is_machine_check(u32 intr_info)
a0861c02
AK
1230{
1231 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1232 INTR_INFO_VALID_MASK)) ==
1233 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1234}
1235
3252850d
LT
1236/* Undocumented: icebp/int1 */
1237static inline bool is_icebp(u32 intr_info)
1238{
1239 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1240 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1241}
1242
31299944 1243static inline bool cpu_has_vmx_msr_bitmap(void)
25c5f225 1244{
04547156 1245 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
25c5f225
SY
1246}
1247
31299944 1248static inline bool cpu_has_vmx_tpr_shadow(void)
6e5d865c 1249{
04547156 1250 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
6e5d865c
YS
1251}
1252
35754c98 1253static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
6e5d865c 1254{
35754c98 1255 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
6e5d865c
YS
1256}
1257
31299944 1258static inline bool cpu_has_secondary_exec_ctrls(void)
f78e0e2e 1259{
04547156
SY
1260 return vmcs_config.cpu_based_exec_ctrl &
1261 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
f78e0e2e
SY
1262}
1263
774ead3a 1264static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
f78e0e2e 1265{
04547156
SY
1266 return vmcs_config.cpu_based_2nd_exec_ctrl &
1267 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1268}
1269
8d14695f
YZ
1270static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1271{
1272 return vmcs_config.cpu_based_2nd_exec_ctrl &
1273 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1274}
1275
83d4c286
YZ
1276static inline bool cpu_has_vmx_apic_register_virt(void)
1277{
1278 return vmcs_config.cpu_based_2nd_exec_ctrl &
1279 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1280}
1281
c7c9c56c
YZ
1282static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1283{
1284 return vmcs_config.cpu_based_2nd_exec_ctrl &
1285 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1286}
1287
64672c95
YJ
1288/*
1289 * Comment's format: document - errata name - stepping - processor name.
1290 * Refer from
1291 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1292 */
1293static u32 vmx_preemption_cpu_tfms[] = {
1294/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12950x000206E6,
1296/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1297/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1298/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
12990x00020652,
1300/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
13010x00020655,
1302/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1303/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1304/*
1305 * 320767.pdf - AAP86 - B1 -
1306 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1307 */
13080x000106E5,
1309/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
13100x000106A0,
1311/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
13120x000106A1,
1313/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
13140x000106A4,
1315 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1316 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1317 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
13180x000106A5,
1319};
1320
1321static inline bool cpu_has_broken_vmx_preemption_timer(void)
1322{
1323 u32 eax = cpuid_eax(0x00000001), i;
1324
1325 /* Clear the reserved bits */
1326 eax &= ~(0x3U << 14 | 0xfU << 28);
03f6a22a 1327 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
64672c95
YJ
1328 if (eax == vmx_preemption_cpu_tfms[i])
1329 return true;
1330
1331 return false;
1332}
1333
1334static inline bool cpu_has_vmx_preemption_timer(void)
1335{
64672c95
YJ
1336 return vmcs_config.pin_based_exec_ctrl &
1337 PIN_BASED_VMX_PREEMPTION_TIMER;
1338}
1339
01e439be
YZ
1340static inline bool cpu_has_vmx_posted_intr(void)
1341{
d6a858d1
PB
1342 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1343 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
01e439be
YZ
1344}
1345
1346static inline bool cpu_has_vmx_apicv(void)
1347{
1348 return cpu_has_vmx_apic_register_virt() &&
1349 cpu_has_vmx_virtual_intr_delivery() &&
1350 cpu_has_vmx_posted_intr();
1351}
1352
04547156
SY
1353static inline bool cpu_has_vmx_flexpriority(void)
1354{
1355 return cpu_has_vmx_tpr_shadow() &&
1356 cpu_has_vmx_virtualize_apic_accesses();
f78e0e2e
SY
1357}
1358
e799794e
MT
1359static inline bool cpu_has_vmx_ept_execute_only(void)
1360{
31299944 1361 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
e799794e
MT
1362}
1363
e799794e
MT
1364static inline bool cpu_has_vmx_ept_2m_page(void)
1365{
31299944 1366 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
e799794e
MT
1367}
1368
878403b7
SY
1369static inline bool cpu_has_vmx_ept_1g_page(void)
1370{
31299944 1371 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
878403b7
SY
1372}
1373
4bc9b982
SY
1374static inline bool cpu_has_vmx_ept_4levels(void)
1375{
1376 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1377}
1378
42aa53b4
DH
1379static inline bool cpu_has_vmx_ept_mt_wb(void)
1380{
1381 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1382}
1383
855feb67
YZ
1384static inline bool cpu_has_vmx_ept_5levels(void)
1385{
1386 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1387}
1388
83c3a331
XH
1389static inline bool cpu_has_vmx_ept_ad_bits(void)
1390{
1391 return vmx_capability.ept & VMX_EPT_AD_BIT;
1392}
1393
31299944 1394static inline bool cpu_has_vmx_invept_context(void)
d56f546d 1395{
31299944 1396 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
d56f546d
SY
1397}
1398
31299944 1399static inline bool cpu_has_vmx_invept_global(void)
d56f546d 1400{
31299944 1401 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
d56f546d
SY
1402}
1403
518c8aee
GJ
1404static inline bool cpu_has_vmx_invvpid_single(void)
1405{
1406 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1407}
1408
b9d762fa
GJ
1409static inline bool cpu_has_vmx_invvpid_global(void)
1410{
1411 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1412}
1413
08d839c4
WL
1414static inline bool cpu_has_vmx_invvpid(void)
1415{
1416 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1417}
1418
31299944 1419static inline bool cpu_has_vmx_ept(void)
d56f546d 1420{
04547156
SY
1421 return vmcs_config.cpu_based_2nd_exec_ctrl &
1422 SECONDARY_EXEC_ENABLE_EPT;
d56f546d
SY
1423}
1424
31299944 1425static inline bool cpu_has_vmx_unrestricted_guest(void)
3a624e29
NK
1426{
1427 return vmcs_config.cpu_based_2nd_exec_ctrl &
1428 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1429}
1430
31299944 1431static inline bool cpu_has_vmx_ple(void)
4b8d54f9
ZE
1432{
1433 return vmcs_config.cpu_based_2nd_exec_ctrl &
1434 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1435}
1436
9ac7e3e8
JD
1437static inline bool cpu_has_vmx_basic_inout(void)
1438{
1439 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1440}
1441
35754c98 1442static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
f78e0e2e 1443{
35754c98 1444 return flexpriority_enabled && lapic_in_kernel(vcpu);
f78e0e2e
SY
1445}
1446
31299944 1447static inline bool cpu_has_vmx_vpid(void)
2384d2b3 1448{
04547156
SY
1449 return vmcs_config.cpu_based_2nd_exec_ctrl &
1450 SECONDARY_EXEC_ENABLE_VPID;
2384d2b3
SY
1451}
1452
31299944 1453static inline bool cpu_has_vmx_rdtscp(void)
4e47c7a6
SY
1454{
1455 return vmcs_config.cpu_based_2nd_exec_ctrl &
1456 SECONDARY_EXEC_RDTSCP;
1457}
1458
ad756a16
MJ
1459static inline bool cpu_has_vmx_invpcid(void)
1460{
1461 return vmcs_config.cpu_based_2nd_exec_ctrl &
1462 SECONDARY_EXEC_ENABLE_INVPCID;
1463}
1464
8a1b4392
PB
1465static inline bool cpu_has_virtual_nmis(void)
1466{
1467 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1468}
1469
f5f48ee1
SY
1470static inline bool cpu_has_vmx_wbinvd_exit(void)
1471{
1472 return vmcs_config.cpu_based_2nd_exec_ctrl &
1473 SECONDARY_EXEC_WBINVD_EXITING;
1474}
1475
abc4fc58
AG
1476static inline bool cpu_has_vmx_shadow_vmcs(void)
1477{
1478 u64 vmx_msr;
1479 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1480 /* check if the cpu supports writing r/o exit information fields */
1481 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1482 return false;
1483
1484 return vmcs_config.cpu_based_2nd_exec_ctrl &
1485 SECONDARY_EXEC_SHADOW_VMCS;
1486}
1487
843e4330
KH
1488static inline bool cpu_has_vmx_pml(void)
1489{
1490 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1491}
1492
64903d61
HZ
1493static inline bool cpu_has_vmx_tsc_scaling(void)
1494{
1495 return vmcs_config.cpu_based_2nd_exec_ctrl &
1496 SECONDARY_EXEC_TSC_SCALING;
1497}
1498
2a499e49
BD
1499static inline bool cpu_has_vmx_vmfunc(void)
1500{
1501 return vmcs_config.cpu_based_2nd_exec_ctrl &
1502 SECONDARY_EXEC_ENABLE_VMFUNC;
1503}
1504
04547156
SY
1505static inline bool report_flexpriority(void)
1506{
1507 return flexpriority_enabled;
1508}
1509
c7c2c709
JM
1510static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1511{
1512 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low);
1513}
1514
fe3ef05c
NHE
1515static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1516{
1517 return vmcs12->cpu_based_vm_exec_control & bit;
1518}
1519
1520static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1521{
1522 return (vmcs12->cpu_based_vm_exec_control &
1523 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1524 (vmcs12->secondary_vm_exec_control & bit);
1525}
1526
f4124500
JK
1527static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1528{
1529 return vmcs12->pin_based_vm_exec_control &
1530 PIN_BASED_VMX_PREEMPTION_TIMER;
1531}
1532
155a97a3
NHE
1533static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1534{
1535 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1536}
1537
81dc01f7
WL
1538static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1539{
3db13480 1540 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
81dc01f7
WL
1541}
1542
c5f983f6
BD
1543static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1544{
1545 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1546}
1547
f2b93280
WV
1548static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1549{
1550 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1551}
1552
5c614b35
WL
1553static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1554{
1555 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1556}
1557
82f0dd4b
WV
1558static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1559{
1560 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1561}
1562
608406e2
WV
1563static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1564{
1565 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1566}
1567
705699a1
WV
1568static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1569{
1570 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1571}
1572
27c42a1b
BD
1573static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1574{
1575 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1576}
1577
41ab9372
BD
1578static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1579{
1580 return nested_cpu_has_vmfunc(vmcs12) &&
1581 (vmcs12->vm_function_control &
1582 VMX_VMFUNC_EPTP_SWITCHING);
1583}
1584
ef85b673 1585static inline bool is_nmi(u32 intr_info)
644d711a
NHE
1586{
1587 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
ef85b673 1588 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
644d711a
NHE
1589}
1590
533558bc
JK
1591static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1592 u32 exit_intr_info,
1593 unsigned long exit_qualification);
7c177938
NHE
1594static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1595 struct vmcs12 *vmcs12,
1596 u32 reason, unsigned long qualification);
1597
8b9cf98c 1598static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
7725f0ba
AK
1599{
1600 int i;
1601
a2fa3e9f 1602 for (i = 0; i < vmx->nmsrs; ++i)
26bb0981 1603 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
a75beee6
ED
1604 return i;
1605 return -1;
1606}
1607
2384d2b3
SY
1608static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1609{
1610 struct {
1611 u64 vpid : 16;
1612 u64 rsvd : 48;
1613 u64 gva;
1614 } operand = { vpid, 0, gva };
1615
4ecac3fd 1616 asm volatile (__ex(ASM_VMX_INVVPID)
2384d2b3
SY
1617 /* CF==1 or ZF==1 --> rc = -1 */
1618 "; ja 1f ; ud2 ; 1:"
1619 : : "a"(&operand), "c"(ext) : "cc", "memory");
1620}
1621
1439442c
SY
1622static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1623{
1624 struct {
1625 u64 eptp, gpa;
1626 } operand = {eptp, gpa};
1627
4ecac3fd 1628 asm volatile (__ex(ASM_VMX_INVEPT)
1439442c
SY
1629 /* CF==1 or ZF==1 --> rc = -1 */
1630 "; ja 1f ; ud2 ; 1:\n"
1631 : : "a" (&operand), "c" (ext) : "cc", "memory");
1632}
1633
26bb0981 1634static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
a75beee6
ED
1635{
1636 int i;
1637
8b9cf98c 1638 i = __find_msr_index(vmx, msr);
a75beee6 1639 if (i >= 0)
a2fa3e9f 1640 return &vmx->guest_msrs[i];
8b6d44c7 1641 return NULL;
7725f0ba
AK
1642}
1643
6aa8b732
AK
1644static void vmcs_clear(struct vmcs *vmcs)
1645{
1646 u64 phys_addr = __pa(vmcs);
1647 u8 error;
1648
4ecac3fd 1649 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
16d8f72f 1650 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
6aa8b732
AK
1651 : "cc", "memory");
1652 if (error)
1653 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1654 vmcs, phys_addr);
1655}
1656
d462b819
NHE
1657static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1658{
1659 vmcs_clear(loaded_vmcs->vmcs);
355f4fb1
JM
1660 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1661 vmcs_clear(loaded_vmcs->shadow_vmcs);
d462b819
NHE
1662 loaded_vmcs->cpu = -1;
1663 loaded_vmcs->launched = 0;
1664}
1665
7725b894
DX
1666static void vmcs_load(struct vmcs *vmcs)
1667{
1668 u64 phys_addr = __pa(vmcs);
1669 u8 error;
1670
1671 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
16d8f72f 1672 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
7725b894
DX
1673 : "cc", "memory");
1674 if (error)
2844d849 1675 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
7725b894
DX
1676 vmcs, phys_addr);
1677}
1678
2965faa5 1679#ifdef CONFIG_KEXEC_CORE
8f536b76
ZY
1680/*
1681 * This bitmap is used to indicate whether the vmclear
1682 * operation is enabled on all cpus. All disabled by
1683 * default.
1684 */
1685static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1686
1687static inline void crash_enable_local_vmclear(int cpu)
1688{
1689 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1690}
1691
1692static inline void crash_disable_local_vmclear(int cpu)
1693{
1694 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1695}
1696
1697static inline int crash_local_vmclear_enabled(int cpu)
1698{
1699 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1700}
1701
1702static void crash_vmclear_local_loaded_vmcss(void)
1703{
1704 int cpu = raw_smp_processor_id();
1705 struct loaded_vmcs *v;
1706
1707 if (!crash_local_vmclear_enabled(cpu))
1708 return;
1709
1710 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1711 loaded_vmcss_on_cpu_link)
1712 vmcs_clear(v->vmcs);
1713}
1714#else
1715static inline void crash_enable_local_vmclear(int cpu) { }
1716static inline void crash_disable_local_vmclear(int cpu) { }
2965faa5 1717#endif /* CONFIG_KEXEC_CORE */
8f536b76 1718
d462b819 1719static void __loaded_vmcs_clear(void *arg)
6aa8b732 1720{
d462b819 1721 struct loaded_vmcs *loaded_vmcs = arg;
d3b2c338 1722 int cpu = raw_smp_processor_id();
6aa8b732 1723
d462b819
NHE
1724 if (loaded_vmcs->cpu != cpu)
1725 return; /* vcpu migration can race with cpu offline */
1726 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
6aa8b732 1727 per_cpu(current_vmcs, cpu) = NULL;
8f536b76 1728 crash_disable_local_vmclear(cpu);
d462b819 1729 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
5a560f8b
XG
1730
1731 /*
1732 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1733 * is before setting loaded_vmcs->vcpu to -1 which is done in
1734 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1735 * then adds the vmcs into percpu list before it is deleted.
1736 */
1737 smp_wmb();
1738
d462b819 1739 loaded_vmcs_init(loaded_vmcs);
8f536b76 1740 crash_enable_local_vmclear(cpu);
6aa8b732
AK
1741}
1742
d462b819 1743static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
8d0be2b3 1744{
e6c7d321
XG
1745 int cpu = loaded_vmcs->cpu;
1746
1747 if (cpu != -1)
1748 smp_call_function_single(cpu,
1749 __loaded_vmcs_clear, loaded_vmcs, 1);
8d0be2b3
AK
1750}
1751
dd5f5341 1752static inline void vpid_sync_vcpu_single(int vpid)
2384d2b3 1753{
dd5f5341 1754 if (vpid == 0)
2384d2b3
SY
1755 return;
1756
518c8aee 1757 if (cpu_has_vmx_invvpid_single())
dd5f5341 1758 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
2384d2b3
SY
1759}
1760
b9d762fa
GJ
1761static inline void vpid_sync_vcpu_global(void)
1762{
1763 if (cpu_has_vmx_invvpid_global())
1764 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1765}
1766
dd5f5341 1767static inline void vpid_sync_context(int vpid)
b9d762fa
GJ
1768{
1769 if (cpu_has_vmx_invvpid_single())
dd5f5341 1770 vpid_sync_vcpu_single(vpid);
b9d762fa
GJ
1771 else
1772 vpid_sync_vcpu_global();
1773}
1774
1439442c
SY
1775static inline void ept_sync_global(void)
1776{
f5f51586 1777 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1439442c
SY
1778}
1779
1780static inline void ept_sync_context(u64 eptp)
1781{
0e1252dc
DH
1782 if (cpu_has_vmx_invept_context())
1783 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1784 else
1785 ept_sync_global();
1439442c
SY
1786}
1787
8a86aea9
PB
1788static __always_inline void vmcs_check16(unsigned long field)
1789{
1790 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1791 "16-bit accessor invalid for 64-bit field");
1792 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1793 "16-bit accessor invalid for 64-bit high field");
1794 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1795 "16-bit accessor invalid for 32-bit high field");
1796 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1797 "16-bit accessor invalid for natural width field");
1798}
1799
1800static __always_inline void vmcs_check32(unsigned long field)
1801{
1802 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1803 "32-bit accessor invalid for 16-bit field");
1804 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1805 "32-bit accessor invalid for natural width field");
1806}
1807
1808static __always_inline void vmcs_check64(unsigned long field)
1809{
1810 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1811 "64-bit accessor invalid for 16-bit field");
1812 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1813 "64-bit accessor invalid for 64-bit high field");
1814 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1815 "64-bit accessor invalid for 32-bit field");
1816 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1817 "64-bit accessor invalid for natural width field");
1818}
1819
1820static __always_inline void vmcs_checkl(unsigned long field)
1821{
1822 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1823 "Natural width accessor invalid for 16-bit field");
1824 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1825 "Natural width accessor invalid for 64-bit field");
1826 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1827 "Natural width accessor invalid for 64-bit high field");
1828 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1829 "Natural width accessor invalid for 32-bit field");
1830}
1831
1832static __always_inline unsigned long __vmcs_readl(unsigned long field)
6aa8b732 1833{
5e520e62 1834 unsigned long value;
6aa8b732 1835
5e520e62
AK
1836 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1837 : "=a"(value) : "d"(field) : "cc");
6aa8b732
AK
1838 return value;
1839}
1840
96304217 1841static __always_inline u16 vmcs_read16(unsigned long field)
6aa8b732 1842{
8a86aea9
PB
1843 vmcs_check16(field);
1844 return __vmcs_readl(field);
6aa8b732
AK
1845}
1846
96304217 1847static __always_inline u32 vmcs_read32(unsigned long field)
6aa8b732 1848{
8a86aea9
PB
1849 vmcs_check32(field);
1850 return __vmcs_readl(field);
6aa8b732
AK
1851}
1852
96304217 1853static __always_inline u64 vmcs_read64(unsigned long field)
6aa8b732 1854{
8a86aea9 1855 vmcs_check64(field);
05b3e0c2 1856#ifdef CONFIG_X86_64
8a86aea9 1857 return __vmcs_readl(field);
6aa8b732 1858#else
8a86aea9 1859 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
6aa8b732
AK
1860#endif
1861}
1862
8a86aea9
PB
1863static __always_inline unsigned long vmcs_readl(unsigned long field)
1864{
1865 vmcs_checkl(field);
1866 return __vmcs_readl(field);
1867}
1868
e52de1b8
AK
1869static noinline void vmwrite_error(unsigned long field, unsigned long value)
1870{
1871 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1872 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1873 dump_stack();
1874}
1875
8a86aea9 1876static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
6aa8b732
AK
1877{
1878 u8 error;
1879
4ecac3fd 1880 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
d77c26fc 1881 : "=q"(error) : "a"(value), "d"(field) : "cc");
e52de1b8
AK
1882 if (unlikely(error))
1883 vmwrite_error(field, value);
6aa8b732
AK
1884}
1885
8a86aea9 1886static __always_inline void vmcs_write16(unsigned long field, u16 value)
6aa8b732 1887{
8a86aea9
PB
1888 vmcs_check16(field);
1889 __vmcs_writel(field, value);
6aa8b732
AK
1890}
1891
8a86aea9 1892static __always_inline void vmcs_write32(unsigned long field, u32 value)
6aa8b732 1893{
8a86aea9
PB
1894 vmcs_check32(field);
1895 __vmcs_writel(field, value);
6aa8b732
AK
1896}
1897
8a86aea9 1898static __always_inline void vmcs_write64(unsigned long field, u64 value)
6aa8b732 1899{
8a86aea9
PB
1900 vmcs_check64(field);
1901 __vmcs_writel(field, value);
7682f2d0 1902#ifndef CONFIG_X86_64
6aa8b732 1903 asm volatile ("");
8a86aea9 1904 __vmcs_writel(field+1, value >> 32);
6aa8b732
AK
1905#endif
1906}
1907
8a86aea9 1908static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
2ab455cc 1909{
8a86aea9
PB
1910 vmcs_checkl(field);
1911 __vmcs_writel(field, value);
2ab455cc
AL
1912}
1913
8a86aea9 1914static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
2ab455cc 1915{
8a86aea9
PB
1916 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1917 "vmcs_clear_bits does not support 64-bit fields");
1918 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2ab455cc
AL
1919}
1920
8a86aea9 1921static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2ab455cc 1922{
8a86aea9
PB
1923 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1924 "vmcs_set_bits does not support 64-bit fields");
1925 __vmcs_writel(field, __vmcs_readl(field) | mask);
2ab455cc
AL
1926}
1927
8391ce44
PB
1928static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1929{
1930 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1931}
1932
2961e876
GN
1933static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1934{
1935 vmcs_write32(VM_ENTRY_CONTROLS, val);
1936 vmx->vm_entry_controls_shadow = val;
1937}
1938
1939static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1940{
1941 if (vmx->vm_entry_controls_shadow != val)
1942 vm_entry_controls_init(vmx, val);
1943}
1944
1945static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1946{
1947 return vmx->vm_entry_controls_shadow;
1948}
1949
1950
1951static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1952{
1953 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1954}
1955
1956static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1957{
1958 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1959}
1960
8391ce44
PB
1961static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1962{
1963 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1964}
1965
2961e876
GN
1966static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1967{
1968 vmcs_write32(VM_EXIT_CONTROLS, val);
1969 vmx->vm_exit_controls_shadow = val;
1970}
1971
1972static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1973{
1974 if (vmx->vm_exit_controls_shadow != val)
1975 vm_exit_controls_init(vmx, val);
1976}
1977
1978static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1979{
1980 return vmx->vm_exit_controls_shadow;
1981}
1982
1983
1984static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1985{
1986 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1987}
1988
1989static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1990{
1991 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1992}
1993
2fb92db1
AK
1994static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1995{
1996 vmx->segment_cache.bitmask = 0;
1997}
1998
1999static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2000 unsigned field)
2001{
2002 bool ret;
2003 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2004
2005 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2006 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2007 vmx->segment_cache.bitmask = 0;
2008 }
2009 ret = vmx->segment_cache.bitmask & mask;
2010 vmx->segment_cache.bitmask |= mask;
2011 return ret;
2012}
2013
2014static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2015{
2016 u16 *p = &vmx->segment_cache.seg[seg].selector;
2017
2018 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2019 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2020 return *p;
2021}
2022
2023static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2024{
2025 ulong *p = &vmx->segment_cache.seg[seg].base;
2026
2027 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2028 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2029 return *p;
2030}
2031
2032static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2033{
2034 u32 *p = &vmx->segment_cache.seg[seg].limit;
2035
2036 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2037 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2038 return *p;
2039}
2040
2041static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2042{
2043 u32 *p = &vmx->segment_cache.seg[seg].ar;
2044
2045 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2046 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2047 return *p;
2048}
2049
abd3f2d6
AK
2050static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2051{
2052 u32 eb;
2053
bd89525a 2054 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
bd7e5b08 2055 (1u << DB_VECTOR) | (1u << AC_VECTOR);
fd7373cc
JK
2056 if ((vcpu->guest_debug &
2057 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2058 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2059 eb |= 1u << BP_VECTOR;
7ffd92c5 2060 if (to_vmx(vcpu)->rmode.vm86_active)
abd3f2d6 2061 eb = ~0;
089d034e 2062 if (enable_ept)
1439442c 2063 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
36cf24e0
NHE
2064
2065 /* When we are running a nested L2 guest and L1 specified for it a
2066 * certain exception bitmap, we must trap the same exceptions and pass
2067 * them to L1. When running L2, we will only handle the exceptions
2068 * specified above if L1 did not want them.
2069 */
2070 if (is_guest_mode(vcpu))
2071 eb |= get_vmcs12(vcpu)->exception_bitmap;
2072
abd3f2d6
AK
2073 vmcs_write32(EXCEPTION_BITMAP, eb);
2074}
2075
74469996
KA
2076/*
2077 * Check if MSR is intercepted for currently loaded MSR bitmap.
2078 */
2079static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2080{
2081 unsigned long *msr_bitmap;
2082 int f = sizeof(unsigned long);
2083
2084 if (!cpu_has_vmx_msr_bitmap())
2085 return true;
2086
2087 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2088
2089 if (msr <= 0x1fff) {
2090 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2091 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2092 msr &= 0x1fff;
2093 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2094 }
2095
2096 return true;
2097}
2098
33241bfe
AR
2099/*
2100 * Check if MSR is intercepted for L01 MSR bitmap.
2101 */
2102static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2103{
2104 unsigned long *msr_bitmap;
2105 int f = sizeof(unsigned long);
2106
2107 if (!cpu_has_vmx_msr_bitmap())
2108 return true;
2109
2110 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2111
2112 if (msr <= 0x1fff) {
2113 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2114 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2115 msr &= 0x1fff;
2116 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2117 }
2118
2119 return true;
2120}
2121
2961e876
GN
2122static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2123 unsigned long entry, unsigned long exit)
8bf00a52 2124{
2961e876
GN
2125 vm_entry_controls_clearbit(vmx, entry);
2126 vm_exit_controls_clearbit(vmx, exit);
8bf00a52
GN
2127}
2128
d8066b74
KRW
2129static int find_msr(struct vmx_msrs *m, unsigned int msr)
2130{
2131 unsigned int i;
2132
2133 for (i = 0; i < m->nr; ++i) {
2134 if (m->val[i].index == msr)
2135 return i;
2136 }
2137 return -ENOENT;
2138}
2139
61d2ef2c
AK
2140static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2141{
d8066b74 2142 int i;
61d2ef2c
AK
2143 struct msr_autoload *m = &vmx->msr_autoload;
2144
8bf00a52
GN
2145 switch (msr) {
2146 case MSR_EFER:
2147 if (cpu_has_load_ia32_efer) {
2961e876
GN
2148 clear_atomic_switch_msr_special(vmx,
2149 VM_ENTRY_LOAD_IA32_EFER,
8bf00a52
GN
2150 VM_EXIT_LOAD_IA32_EFER);
2151 return;
2152 }
2153 break;
2154 case MSR_CORE_PERF_GLOBAL_CTRL:
2155 if (cpu_has_load_perf_global_ctrl) {
2961e876 2156 clear_atomic_switch_msr_special(vmx,
8bf00a52
GN
2157 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2158 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2159 return;
2160 }
2161 break;
110312c8 2162 }
d8066b74
KRW
2163 i = find_msr(&m->guest, msr);
2164 if (i < 0)
0666648b 2165 goto skip_guest;
6e3dedb6 2166 --m->guest.nr;
6e3dedb6 2167 m->guest.val[i] = m->guest.val[m->guest.nr];
6e3dedb6 2168 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
0666648b
KRW
2169
2170skip_guest:
2171 i = find_msr(&m->host, msr);
2172 if (i < 0)
2173 return;
2174
2175 --m->host.nr;
2176 m->host.val[i] = m->host.val[m->host.nr];
6e3dedb6 2177 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
61d2ef2c
AK
2178}
2179
2961e876
GN
2180static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2181 unsigned long entry, unsigned long exit,
2182 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2183 u64 guest_val, u64 host_val)
8bf00a52
GN
2184{
2185 vmcs_write64(guest_val_vmcs, guest_val);
2186 vmcs_write64(host_val_vmcs, host_val);
2961e876
GN
2187 vm_entry_controls_setbit(vmx, entry);
2188 vm_exit_controls_setbit(vmx, exit);
8bf00a52
GN
2189}
2190
61d2ef2c 2191static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
129ce7ac 2192 u64 guest_val, u64 host_val, bool entry_only)
61d2ef2c 2193{
129ce7ac 2194 int i, j = 0;
61d2ef2c
AK
2195 struct msr_autoload *m = &vmx->msr_autoload;
2196
8bf00a52
GN
2197 switch (msr) {
2198 case MSR_EFER:
2199 if (cpu_has_load_ia32_efer) {
2961e876
GN
2200 add_atomic_switch_msr_special(vmx,
2201 VM_ENTRY_LOAD_IA32_EFER,
8bf00a52
GN
2202 VM_EXIT_LOAD_IA32_EFER,
2203 GUEST_IA32_EFER,
2204 HOST_IA32_EFER,
2205 guest_val, host_val);
2206 return;
2207 }
2208 break;
2209 case MSR_CORE_PERF_GLOBAL_CTRL:
2210 if (cpu_has_load_perf_global_ctrl) {
2961e876 2211 add_atomic_switch_msr_special(vmx,
8bf00a52
GN
2212 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2213 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2214 GUEST_IA32_PERF_GLOBAL_CTRL,
2215 HOST_IA32_PERF_GLOBAL_CTRL,
2216 guest_val, host_val);
2217 return;
2218 }
2219 break;
7099e2e1
RK
2220 case MSR_IA32_PEBS_ENABLE:
2221 /* PEBS needs a quiescent period after being disabled (to write
2222 * a record). Disabling PEBS through VMX MSR swapping doesn't
2223 * provide that period, so a CPU could write host's record into
2224 * guest's memory.
2225 */
2226 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
110312c8
AK
2227 }
2228
d8066b74 2229 i = find_msr(&m->guest, msr);
129ce7ac
KRW
2230 if (!entry_only)
2231 j = find_msr(&m->host, msr);
2232
0666648b 2233 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
60266204 2234 printk_once(KERN_WARNING "Not enough msr switch entries. "
e7fc6f93
GN
2235 "Can't add msr %x\n", msr);
2236 return;
0666648b
KRW
2237 }
2238 if (i < 0) {
d8066b74 2239 i = m->guest.nr++;
6e3dedb6 2240 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
0666648b 2241 }
129ce7ac
KRW
2242 m->guest.val[i].index = msr;
2243 m->guest.val[i].value = guest_val;
2244
2245 if (entry_only)
2246 return;
2247
0666648b
KRW
2248 if (j < 0) {
2249 j = m->host.nr++;
6e3dedb6 2250 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
61d2ef2c 2251 }
0666648b
KRW
2252 m->host.val[j].index = msr;
2253 m->host.val[j].value = host_val;
61d2ef2c
AK
2254}
2255
92c0d900 2256static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2cc51560 2257{
844a5fe2
PB
2258 u64 guest_efer = vmx->vcpu.arch.efer;
2259 u64 ignore_bits = 0;
2260
2261 if (!enable_ept) {
2262 /*
2263 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2264 * host CPUID is more efficient than testing guest CPUID
2265 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2266 */
2267 if (boot_cpu_has(X86_FEATURE_SMEP))
2268 guest_efer |= EFER_NX;
2269 else if (!(guest_efer & EFER_NX))
2270 ignore_bits |= EFER_NX;
2271 }
3a34a881 2272
51c6cf66 2273 /*
844a5fe2 2274 * LMA and LME handled by hardware; SCE meaningless outside long mode.
51c6cf66 2275 */
844a5fe2 2276 ignore_bits |= EFER_SCE;
51c6cf66
AK
2277#ifdef CONFIG_X86_64
2278 ignore_bits |= EFER_LMA | EFER_LME;
2279 /* SCE is meaningful only in long mode on Intel */
2280 if (guest_efer & EFER_LMA)
2281 ignore_bits &= ~(u64)EFER_SCE;
2282#endif
84ad33ef
AK
2283
2284 clear_atomic_switch_msr(vmx, MSR_EFER);
f6577a5f
AL
2285
2286 /*
2287 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2288 * On CPUs that support "load IA32_EFER", always switch EFER
2289 * atomically, since it's faster than switching it manually.
2290 */
2291 if (cpu_has_load_ia32_efer ||
2292 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
84ad33ef
AK
2293 if (!(guest_efer & EFER_LMA))
2294 guest_efer &= ~EFER_LME;
54b98bff
AL
2295 if (guest_efer != host_efer)
2296 add_atomic_switch_msr(vmx, MSR_EFER,
129ce7ac 2297 guest_efer, host_efer, false);
84ad33ef 2298 return false;
844a5fe2
PB
2299 } else {
2300 guest_efer &= ~ignore_bits;
2301 guest_efer |= host_efer & ignore_bits;
2302
2303 vmx->guest_msrs[efer_offset].data = guest_efer;
2304 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
84ad33ef 2305
844a5fe2
PB
2306 return true;
2307 }
51c6cf66
AK
2308}
2309
e28baead
AL
2310#ifdef CONFIG_X86_32
2311/*
2312 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2313 * VMCS rather than the segment table. KVM uses this helper to figure
2314 * out the current bases to poke them into the VMCS before entry.
2315 */
2d49ec72
GN
2316static unsigned long segment_base(u16 selector)
2317{
8c2e41f7 2318 struct desc_struct *table;
2d49ec72
GN
2319 unsigned long v;
2320
8c2e41f7 2321 if (!(selector & ~SEGMENT_RPL_MASK))
2d49ec72
GN
2322 return 0;
2323
45fc8757 2324 table = get_current_gdt_ro();
2d49ec72 2325
8c2e41f7 2326 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
2d49ec72
GN
2327 u16 ldt_selector = kvm_read_ldt();
2328
8c2e41f7 2329 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
2d49ec72
GN
2330 return 0;
2331
8c2e41f7 2332 table = (struct desc_struct *)segment_base(ldt_selector);
2d49ec72 2333 }
8c2e41f7 2334 v = get_desc_base(&table[selector >> 3]);
2d49ec72
GN
2335 return v;
2336}
e28baead 2337#endif
2d49ec72 2338
04d2cc77 2339static void vmx_save_host_state(struct kvm_vcpu *vcpu)
33ed6329 2340{
04d2cc77 2341 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981 2342 int i;
04d2cc77 2343
a2fa3e9f 2344 if (vmx->host_state.loaded)
33ed6329
AK
2345 return;
2346
a2fa3e9f 2347 vmx->host_state.loaded = 1;
33ed6329
AK
2348 /*
2349 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2350 * allow segment selectors with cpl > 0 or ti == 1.
2351 */
d6e88aec 2352 vmx->host_state.ldt_sel = kvm_read_ldt();
152d3f2f 2353 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
9581d442 2354 savesegment(fs, vmx->host_state.fs_sel);
152d3f2f 2355 if (!(vmx->host_state.fs_sel & 7)) {
a2fa3e9f 2356 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
152d3f2f
LV
2357 vmx->host_state.fs_reload_needed = 0;
2358 } else {
33ed6329 2359 vmcs_write16(HOST_FS_SELECTOR, 0);
152d3f2f 2360 vmx->host_state.fs_reload_needed = 1;
33ed6329 2361 }
9581d442 2362 savesegment(gs, vmx->host_state.gs_sel);
a2fa3e9f
GH
2363 if (!(vmx->host_state.gs_sel & 7))
2364 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
33ed6329
AK
2365 else {
2366 vmcs_write16(HOST_GS_SELECTOR, 0);
152d3f2f 2367 vmx->host_state.gs_ldt_reload_needed = 1;
33ed6329
AK
2368 }
2369
b2da15ac
AK
2370#ifdef CONFIG_X86_64
2371 savesegment(ds, vmx->host_state.ds_sel);
2372 savesegment(es, vmx->host_state.es_sel);
2373#endif
2374
33ed6329
AK
2375#ifdef CONFIG_X86_64
2376 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2377 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2378#else
a2fa3e9f
GH
2379 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2380 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
33ed6329 2381#endif
707c0874
AK
2382
2383#ifdef CONFIG_X86_64
c8770e7b
AK
2384 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2385 if (is_long_mode(&vmx->vcpu))
44ea2b17 2386 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
707c0874 2387#endif
da8999d3
LJ
2388 if (boot_cpu_has(X86_FEATURE_MPX))
2389 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
26bb0981
AK
2390 for (i = 0; i < vmx->save_nmsrs; ++i)
2391 kvm_set_shared_msr(vmx->guest_msrs[i].index,
d5696725
AK
2392 vmx->guest_msrs[i].data,
2393 vmx->guest_msrs[i].mask);
33ed6329
AK
2394}
2395
a9b21b62 2396static void __vmx_load_host_state(struct vcpu_vmx *vmx)
33ed6329 2397{
a2fa3e9f 2398 if (!vmx->host_state.loaded)
33ed6329
AK
2399 return;
2400
e1beb1d3 2401 ++vmx->vcpu.stat.host_state_reload;
a2fa3e9f 2402 vmx->host_state.loaded = 0;
c8770e7b
AK
2403#ifdef CONFIG_X86_64
2404 if (is_long_mode(&vmx->vcpu))
2405 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2406#endif
152d3f2f 2407 if (vmx->host_state.gs_ldt_reload_needed) {
d6e88aec 2408 kvm_load_ldt(vmx->host_state.ldt_sel);
33ed6329 2409#ifdef CONFIG_X86_64
9581d442 2410 load_gs_index(vmx->host_state.gs_sel);
9581d442
AK
2411#else
2412 loadsegment(gs, vmx->host_state.gs_sel);
33ed6329 2413#endif
33ed6329 2414 }
0a77fe4c
AK
2415 if (vmx->host_state.fs_reload_needed)
2416 loadsegment(fs, vmx->host_state.fs_sel);
b2da15ac
AK
2417#ifdef CONFIG_X86_64
2418 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2419 loadsegment(ds, vmx->host_state.ds_sel);
2420 loadsegment(es, vmx->host_state.es_sel);
2421 }
b2da15ac 2422#endif
b7ffc44d 2423 invalidate_tss_limit();
44ea2b17 2424#ifdef CONFIG_X86_64
c8770e7b 2425 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
44ea2b17 2426#endif
da8999d3
LJ
2427 if (vmx->host_state.msr_host_bndcfgs)
2428 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
45fc8757 2429 load_fixmap_gdt(raw_smp_processor_id());
33ed6329
AK
2430}
2431
a9b21b62
AK
2432static void vmx_load_host_state(struct vcpu_vmx *vmx)
2433{
2434 preempt_disable();
2435 __vmx_load_host_state(vmx);
2436 preempt_enable();
2437}
2438
28b835d6
FW
2439static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2440{
2441 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2442 struct pi_desc old, new;
2443 unsigned int dest;
2444
31afb2ea
PB
2445 /*
2446 * In case of hot-plug or hot-unplug, we may have to undo
2447 * vmx_vcpu_pi_put even if there is no assigned device. And we
2448 * always keep PI.NDST up to date for simplicity: it makes the
2449 * code easier, and CPU migration is not a fast path.
2450 */
2451 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
28b835d6
FW
2452 return;
2453
31afb2ea
PB
2454 /*
2455 * First handle the simple case where no cmpxchg is necessary; just
2456 * allow posting non-urgent interrupts.
2457 *
2458 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2459 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2460 * expects the VCPU to be on the blocked_vcpu_list that matches
2461 * PI.NDST.
2462 */
2463 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2464 vcpu->cpu == cpu) {
2465 pi_clear_sn(pi_desc);
28b835d6 2466 return;
31afb2ea 2467 }
28b835d6 2468
31afb2ea 2469 /* The full case. */
28b835d6
FW
2470 do {
2471 old.control = new.control = pi_desc->control;
2472
31afb2ea 2473 dest = cpu_physical_id(cpu);
28b835d6 2474
31afb2ea
PB
2475 if (x2apic_enabled())
2476 new.ndst = dest;
2477 else
2478 new.ndst = (dest << 8) & 0xFF00;
28b835d6 2479
28b835d6 2480 new.sn = 0;
c0a1666b
PB
2481 } while (cmpxchg64(&pi_desc->control, old.control,
2482 new.control) != old.control);
28b835d6 2483}
1be0e61c 2484
c95ba92a
PF
2485static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2486{
2487 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2488 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2489}
2490
6aa8b732
AK
2491/*
2492 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2493 * vcpu mutex is already taken.
2494 */
15ad7146 2495static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
6aa8b732 2496{
a2fa3e9f 2497 struct vcpu_vmx *vmx = to_vmx(vcpu);
b80c76ec 2498 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
6aa8b732 2499
b80c76ec 2500 if (!already_loaded) {
fe0e80be 2501 loaded_vmcs_clear(vmx->loaded_vmcs);
92fe13be 2502 local_irq_disable();
8f536b76 2503 crash_disable_local_vmclear(cpu);
5a560f8b
XG
2504
2505 /*
2506 * Read loaded_vmcs->cpu should be before fetching
2507 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2508 * See the comments in __loaded_vmcs_clear().
2509 */
2510 smp_rmb();
2511
d462b819
NHE
2512 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2513 &per_cpu(loaded_vmcss_on_cpu, cpu));
8f536b76 2514 crash_enable_local_vmclear(cpu);
92fe13be 2515 local_irq_enable();
b80c76ec
JM
2516 }
2517
2518 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2519 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2520 vmcs_load(vmx->loaded_vmcs->vmcs);
33241bfe 2521 indirect_branch_prediction_barrier();
b80c76ec
JM
2522 }
2523
2524 if (!already_loaded) {
59c58ceb 2525 void *gdt = get_current_gdt_ro();
b80c76ec
JM
2526 unsigned long sysenter_esp;
2527
2528 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
92fe13be 2529
6aa8b732
AK
2530 /*
2531 * Linux uses per-cpu TSS and GDT, so set these when switching
e0c23063 2532 * processors. See 22.2.4.
6aa8b732 2533 */
e0c23063 2534 vmcs_writel(HOST_TR_BASE,
72f5e08d 2535 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
59c58ceb 2536 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
6aa8b732 2537
b7ffc44d
AL
2538 /*
2539 * VM exits change the host TR limit to 0x67 after a VM
2540 * exit. This is okay, since 0x67 covers everything except
2541 * the IO bitmap and have have code to handle the IO bitmap
2542 * being lost after a VM exit.
2543 */
2544 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2545
6aa8b732
AK
2546 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2547 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
ff2c3a18 2548
d462b819 2549 vmx->loaded_vmcs->cpu = cpu;
6aa8b732 2550 }
28b835d6 2551
2680d6da
OH
2552 /* Setup TSC multiplier */
2553 if (kvm_has_tsc_control &&
c95ba92a
PF
2554 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2555 decache_tsc_multiplier(vmx);
2680d6da 2556
28b835d6 2557 vmx_vcpu_pi_load(vcpu, cpu);
1be0e61c 2558 vmx->host_pkru = read_pkru();
28b835d6
FW
2559}
2560
2561static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2562{
2563 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2564
2565 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
a0052191
YZ
2566 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2567 !kvm_vcpu_apicv_active(vcpu))
28b835d6
FW
2568 return;
2569
2570 /* Set SN when the vCPU is preempted */
2571 if (vcpu->preempted)
2572 pi_set_sn(pi_desc);
6aa8b732
AK
2573}
2574
2575static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2576{
28b835d6
FW
2577 vmx_vcpu_pi_put(vcpu);
2578
a9b21b62 2579 __vmx_load_host_state(to_vmx(vcpu));
6aa8b732
AK
2580}
2581
f244deed
WL
2582static bool emulation_required(struct kvm_vcpu *vcpu)
2583{
2584 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2585}
2586
edcafe3c
AK
2587static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2588
fe3ef05c
NHE
2589/*
2590 * Return the cr0 value that a nested guest would read. This is a combination
2591 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2592 * its hypervisor (cr0_read_shadow).
2593 */
2594static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2595{
2596 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2597 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2598}
2599static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2600{
2601 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2602 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2603}
2604
6aa8b732
AK
2605static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2606{
78ac8b47 2607 unsigned long rflags, save_rflags;
345dcaa8 2608
6de12732
AK
2609 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2610 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2611 rflags = vmcs_readl(GUEST_RFLAGS);
2612 if (to_vmx(vcpu)->rmode.vm86_active) {
2613 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2614 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2615 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2616 }
2617 to_vmx(vcpu)->rflags = rflags;
78ac8b47 2618 }
6de12732 2619 return to_vmx(vcpu)->rflags;
6aa8b732
AK
2620}
2621
2622static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2623{
f244deed
WL
2624 unsigned long old_rflags = vmx_get_rflags(vcpu);
2625
6de12732
AK
2626 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2627 to_vmx(vcpu)->rflags = rflags;
78ac8b47
AK
2628 if (to_vmx(vcpu)->rmode.vm86_active) {
2629 to_vmx(vcpu)->rmode.save_rflags = rflags;
053de044 2630 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
78ac8b47 2631 }
6aa8b732 2632 vmcs_writel(GUEST_RFLAGS, rflags);
f244deed
WL
2633
2634 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2635 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
6aa8b732
AK
2636}
2637
37ccdcbe 2638static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2809f5d2
GC
2639{
2640 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2641 int ret = 0;
2642
2643 if (interruptibility & GUEST_INTR_STATE_STI)
48005f64 2644 ret |= KVM_X86_SHADOW_INT_STI;
2809f5d2 2645 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
48005f64 2646 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2809f5d2 2647
37ccdcbe 2648 return ret;
2809f5d2
GC
2649}
2650
2651static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2652{
2653 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2654 u32 interruptibility = interruptibility_old;
2655
2656 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2657
48005f64 2658 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2809f5d2 2659 interruptibility |= GUEST_INTR_STATE_MOV_SS;
48005f64 2660 else if (mask & KVM_X86_SHADOW_INT_STI)
2809f5d2
GC
2661 interruptibility |= GUEST_INTR_STATE_STI;
2662
2663 if ((interruptibility != interruptibility_old))
2664 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2665}
2666
6aa8b732
AK
2667static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2668{
2669 unsigned long rip;
6aa8b732 2670
5fdbf976 2671 rip = kvm_rip_read(vcpu);
6aa8b732 2672 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5fdbf976 2673 kvm_rip_write(vcpu, rip);
6aa8b732 2674
2809f5d2
GC
2675 /* skipping an emulated instruction also counts */
2676 vmx_set_interrupt_shadow(vcpu, 0);
6aa8b732
AK
2677}
2678
b96fb439
PB
2679static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2680 unsigned long exit_qual)
2681{
2682 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2683 unsigned int nr = vcpu->arch.exception.nr;
2684 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2685
2686 if (vcpu->arch.exception.has_error_code) {
2687 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2688 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2689 }
2690
2691 if (kvm_exception_is_soft(nr))
2692 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2693 else
2694 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2695
2696 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2697 vmx_get_nmi_mask(vcpu))
2698 intr_info |= INTR_INFO_UNBLOCK_NMI;
2699
2700 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2701}
2702
0b6ac343
NHE
2703/*
2704 * KVM wants to inject page-faults which it got to the guest. This function
2705 * checks whether in a nested guest, we need to inject them to L1 or L2.
0b6ac343 2706 */
bfcf83b1 2707static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
0b6ac343
NHE
2708{
2709 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
adfe20fb 2710 unsigned int nr = vcpu->arch.exception.nr;
0b6ac343 2711
b96fb439
PB
2712 if (nr == PF_VECTOR) {
2713 if (vcpu->arch.exception.nested_apf) {
bfcf83b1 2714 *exit_qual = vcpu->arch.apf.nested_apf_token;
b96fb439
PB
2715 return 1;
2716 }
2717 /*
2718 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2719 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2720 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2721 * can be written only when inject_pending_event runs. This should be
2722 * conditional on a new capability---if the capability is disabled,
2723 * kvm_multiple_exception would write the ancillary information to
2724 * CR2 or DR6, for backwards ABI-compatibility.
2725 */
2726 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2727 vcpu->arch.exception.error_code)) {
bfcf83b1 2728 *exit_qual = vcpu->arch.cr2;
b96fb439
PB
2729 return 1;
2730 }
2731 } else {
b96fb439 2732 if (vmcs12->exception_bitmap & (1u << nr)) {
bfcf83b1
WL
2733 if (nr == DB_VECTOR)
2734 *exit_qual = vcpu->arch.dr6;
2735 else
2736 *exit_qual = 0;
b96fb439
PB
2737 return 1;
2738 }
adfe20fb
WL
2739 }
2740
b96fb439 2741 return 0;
0b6ac343
NHE
2742}
2743
cfcd20e5 2744static void vmx_queue_exception(struct kvm_vcpu *vcpu)
298101da 2745{
77ab6db0 2746 struct vcpu_vmx *vmx = to_vmx(vcpu);
cfcd20e5
WL
2747 unsigned nr = vcpu->arch.exception.nr;
2748 bool has_error_code = vcpu->arch.exception.has_error_code;
cfcd20e5 2749 u32 error_code = vcpu->arch.exception.error_code;
8ab2d2e2 2750 u32 intr_info = nr | INTR_INFO_VALID_MASK;
77ab6db0 2751
8ab2d2e2 2752 if (has_error_code) {
77ab6db0 2753 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
8ab2d2e2
JK
2754 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2755 }
77ab6db0 2756
7ffd92c5 2757 if (vmx->rmode.vm86_active) {
71f9833b
SH
2758 int inc_eip = 0;
2759 if (kvm_exception_is_soft(nr))
2760 inc_eip = vcpu->arch.event_exit_inst_len;
2761 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
a92601bb 2762 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
77ab6db0
JK
2763 return;
2764 }
2765
299f0328
SC
2766 WARN_ON_ONCE(vmx->emulation_required);
2767
66fd3f7f
GN
2768 if (kvm_exception_is_soft(nr)) {
2769 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2770 vmx->vcpu.arch.event_exit_inst_len);
8ab2d2e2
JK
2771 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2772 } else
2773 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2774
2775 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
298101da
AK
2776}
2777
4e47c7a6
SY
2778static bool vmx_rdtscp_supported(void)
2779{
2780 return cpu_has_vmx_rdtscp();
2781}
2782
ad756a16
MJ
2783static bool vmx_invpcid_supported(void)
2784{
2785 return cpu_has_vmx_invpcid() && enable_ept;
2786}
2787
a75beee6
ED
2788/*
2789 * Swap MSR entry in host/guest MSR entry array.
2790 */
8b9cf98c 2791static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
a75beee6 2792{
26bb0981 2793 struct shared_msr_entry tmp;
a2fa3e9f
GH
2794
2795 tmp = vmx->guest_msrs[to];
2796 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2797 vmx->guest_msrs[from] = tmp;
a75beee6
ED
2798}
2799
e38aea3e
AK
2800/*
2801 * Set up the vmcs to automatically save and restore system
2802 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2803 * mode, as fiddling with msrs is very expensive.
2804 */
8b9cf98c 2805static void setup_msrs(struct vcpu_vmx *vmx)
e38aea3e 2806{
26bb0981 2807 int save_nmsrs, index;
e38aea3e 2808
a75beee6
ED
2809 save_nmsrs = 0;
2810#ifdef CONFIG_X86_64
8b9cf98c 2811 if (is_long_mode(&vmx->vcpu)) {
8b9cf98c 2812 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
a75beee6 2813 if (index >= 0)
8b9cf98c
RR
2814 move_msr_up(vmx, index, save_nmsrs++);
2815 index = __find_msr_index(vmx, MSR_LSTAR);
a75beee6 2816 if (index >= 0)
8b9cf98c
RR
2817 move_msr_up(vmx, index, save_nmsrs++);
2818 index = __find_msr_index(vmx, MSR_CSTAR);
a75beee6 2819 if (index >= 0)
8b9cf98c 2820 move_msr_up(vmx, index, save_nmsrs++);
4e47c7a6 2821 index = __find_msr_index(vmx, MSR_TSC_AUX);
d6321d49 2822 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
4e47c7a6 2823 move_msr_up(vmx, index, save_nmsrs++);
a75beee6 2824 /*
8c06585d 2825 * MSR_STAR is only needed on long mode guests, and only
a75beee6
ED
2826 * if efer.sce is enabled.
2827 */
8c06585d 2828 index = __find_msr_index(vmx, MSR_STAR);
f6801dff 2829 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
8b9cf98c 2830 move_msr_up(vmx, index, save_nmsrs++);
a75beee6
ED
2831 }
2832#endif
92c0d900
AK
2833 index = __find_msr_index(vmx, MSR_EFER);
2834 if (index >= 0 && update_transition_efer(vmx, index))
26bb0981 2835 move_msr_up(vmx, index, save_nmsrs++);
e38aea3e 2836
26bb0981 2837 vmx->save_nmsrs = save_nmsrs;
5897297b 2838
8d14695f 2839 if (cpu_has_vmx_msr_bitmap())
4b0be90f 2840 vmx_update_msr_bitmap(&vmx->vcpu);
e38aea3e
AK
2841}
2842
f7f5542f
KA
2843static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
2844{
2845 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2846
2847 if (is_guest_mode(vcpu) &&
2848 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
2849 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2850
2851 return vcpu->arch.tsc_offset;
2852}
2853
6aa8b732 2854/*
99e3e30a 2855 * writes 'offset' into guest's timestamp counter offset register
6aa8b732 2856 */
99e3e30a 2857static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
6aa8b732 2858{
27fc51b2 2859 if (is_guest_mode(vcpu)) {
7991825b 2860 /*
27fc51b2
NHE
2861 * We're here if L1 chose not to trap WRMSR to TSC. According
2862 * to the spec, this should set L1's TSC; The offset that L1
2863 * set for L2 remains unchanged, and still needs to be added
2864 * to the newly set TSC to get L2's TSC.
7991825b 2865 */
27fc51b2 2866 struct vmcs12 *vmcs12;
27fc51b2
NHE
2867 /* recalculate vmcs02.TSC_OFFSET: */
2868 vmcs12 = get_vmcs12(vcpu);
2869 vmcs_write64(TSC_OFFSET, offset +
2870 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2871 vmcs12->tsc_offset : 0));
2872 } else {
489223ed
YY
2873 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2874 vmcs_read64(TSC_OFFSET), offset);
27fc51b2
NHE
2875 vmcs_write64(TSC_OFFSET, offset);
2876 }
6aa8b732
AK
2877}
2878
801d3424
NHE
2879/*
2880 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2881 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2882 * all guests if the "nested" module option is off, and can also be disabled
2883 * for a single guest by disabling its VMX cpuid bit.
2884 */
2885static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2886{
d6321d49 2887 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
801d3424
NHE
2888}
2889
b87a51ae
NHE
2890/*
2891 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2892 * returned for the various VMX controls MSRs when nested VMX is enabled.
2893 * The same values should also be used to verify that vmcs12 control fields are
2894 * valid during nested entry from L1 to L2.
2895 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2896 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2897 * bit in the high half is on if the corresponding bit in the control field
2898 * may be on. See also vmx_control_verify().
b87a51ae 2899 */
b9c237bb 2900static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
b87a51ae
NHE
2901{
2902 /*
2903 * Note that as a general rule, the high half of the MSRs (bits in
2904 * the control fields which may be 1) should be initialized by the
2905 * intersection of the underlying hardware's MSR (i.e., features which
2906 * can be supported) and the list of features we want to expose -
2907 * because they are known to be properly supported in our code.
2908 * Also, usually, the low half of the MSRs (bits which must be 1) can
2909 * be set to 0, meaning that L1 may turn off any of these bits. The
2910 * reason is that if one of these bits is necessary, it will appear
2911 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2912 * fields of vmcs01 and vmcs02, will turn these bits off - and
7313c698 2913 * nested_vmx_exit_reflected() will not pass related exits to L1.
b87a51ae
NHE
2914 * These rules have exceptions below.
2915 */
2916
2917 /* pin-based controls */
eabeaacc 2918 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
b9c237bb
WV
2919 vmx->nested.nested_vmx_pinbased_ctls_low,
2920 vmx->nested.nested_vmx_pinbased_ctls_high);
2921 vmx->nested.nested_vmx_pinbased_ctls_low |=
2922 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2923 vmx->nested.nested_vmx_pinbased_ctls_high &=
2924 PIN_BASED_EXT_INTR_MASK |
2925 PIN_BASED_NMI_EXITING |
2926 PIN_BASED_VIRTUAL_NMIS;
2927 vmx->nested.nested_vmx_pinbased_ctls_high |=
2928 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
0238ea91 2929 PIN_BASED_VMX_PREEMPTION_TIMER;
d62caabb 2930 if (kvm_vcpu_apicv_active(&vmx->vcpu))
705699a1
WV
2931 vmx->nested.nested_vmx_pinbased_ctls_high |=
2932 PIN_BASED_POSTED_INTR;
b87a51ae 2933
3dbcd8da 2934 /* exit controls */
c0dfee58 2935 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
b9c237bb
WV
2936 vmx->nested.nested_vmx_exit_ctls_low,
2937 vmx->nested.nested_vmx_exit_ctls_high);
2938 vmx->nested.nested_vmx_exit_ctls_low =
2939 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
e0ba1a6f 2940
b9c237bb 2941 vmx->nested.nested_vmx_exit_ctls_high &=
b87a51ae 2942#ifdef CONFIG_X86_64
c0dfee58 2943 VM_EXIT_HOST_ADDR_SPACE_SIZE |
b87a51ae 2944#endif
f4124500 2945 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
b9c237bb
WV
2946 vmx->nested.nested_vmx_exit_ctls_high |=
2947 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
f4124500 2948 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
e0ba1a6f
BD
2949 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2950
a87036ad 2951 if (kvm_mpx_supported())
b9c237bb 2952 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
b87a51ae 2953
2996fca0 2954 /* We support free control of debug control saving. */
0115f9cb 2955 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2996fca0 2956
b87a51ae
NHE
2957 /* entry controls */
2958 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
b9c237bb
WV
2959 vmx->nested.nested_vmx_entry_ctls_low,
2960 vmx->nested.nested_vmx_entry_ctls_high);
2961 vmx->nested.nested_vmx_entry_ctls_low =
2962 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2963 vmx->nested.nested_vmx_entry_ctls_high &=
57435349
JK
2964#ifdef CONFIG_X86_64
2965 VM_ENTRY_IA32E_MODE |
2966#endif
2967 VM_ENTRY_LOAD_IA32_PAT;
b9c237bb
WV
2968 vmx->nested.nested_vmx_entry_ctls_high |=
2969 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
a87036ad 2970 if (kvm_mpx_supported())
b9c237bb 2971 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
57435349 2972
2996fca0 2973 /* We support free control of debug control loading. */
0115f9cb 2974 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2996fca0 2975
b87a51ae
NHE
2976 /* cpu-based controls */
2977 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
b9c237bb
WV
2978 vmx->nested.nested_vmx_procbased_ctls_low,
2979 vmx->nested.nested_vmx_procbased_ctls_high);
2980 vmx->nested.nested_vmx_procbased_ctls_low =
2981 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2982 vmx->nested.nested_vmx_procbased_ctls_high &=
a294c9bb
JK
2983 CPU_BASED_VIRTUAL_INTR_PENDING |
2984 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
b87a51ae
NHE
2985 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2986 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2987 CPU_BASED_CR3_STORE_EXITING |
2988#ifdef CONFIG_X86_64
2989 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2990#endif
2991 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
5f3d45e7
MD
2992 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2993 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2994 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2995 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
b87a51ae
NHE
2996 /*
2997 * We can allow some features even when not supported by the
2998 * hardware. For example, L1 can specify an MSR bitmap - and we
2999 * can use it to avoid exits to L1 - even when L0 runs L2
3000 * without MSR bitmaps.
3001 */
b9c237bb
WV
3002 vmx->nested.nested_vmx_procbased_ctls_high |=
3003 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
560b7ee1 3004 CPU_BASED_USE_MSR_BITMAPS;
b87a51ae 3005
3dcdf3ec 3006 /* We support free control of CR3 access interception. */
0115f9cb 3007 vmx->nested.nested_vmx_procbased_ctls_low &=
3dcdf3ec
JK
3008 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3009
80154d77
PB
3010 /*
3011 * secondary cpu-based controls. Do not include those that
3012 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3013 */
b87a51ae 3014 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
b9c237bb
WV
3015 vmx->nested.nested_vmx_secondary_ctls_low,
3016 vmx->nested.nested_vmx_secondary_ctls_high);
3017 vmx->nested.nested_vmx_secondary_ctls_low = 0;
3018 vmx->nested.nested_vmx_secondary_ctls_high &=
d6851fbe 3019 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
1b07304c 3020 SECONDARY_EXEC_DESC |
f2b93280 3021 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
82f0dd4b 3022 SECONDARY_EXEC_APIC_REGISTER_VIRT |
608406e2 3023 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3db13480 3024 SECONDARY_EXEC_WBINVD_EXITING;
c18911a2 3025
afa61f75
NHE
3026 if (enable_ept) {
3027 /* nested EPT: emulate EPT also to L1 */
b9c237bb 3028 vmx->nested.nested_vmx_secondary_ctls_high |=
0790ec17 3029 SECONDARY_EXEC_ENABLE_EPT;
b9c237bb 3030 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
7db74265 3031 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
02120c45
BD
3032 if (cpu_has_vmx_ept_execute_only())
3033 vmx->nested.nested_vmx_ept_caps |=
3034 VMX_EPT_EXECUTE_ONLY_BIT;
b9c237bb 3035 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
45e11817 3036 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
7db74265
PB
3037 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3038 VMX_EPT_1GB_PAGE_BIT;
03efce6f
BD
3039 if (enable_ept_ad_bits) {
3040 vmx->nested.nested_vmx_secondary_ctls_high |=
3041 SECONDARY_EXEC_ENABLE_PML;
7461fbc4 3042 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
03efce6f 3043 }
1c13bffd 3044 }
afa61f75 3045
27c42a1b
BD
3046 if (cpu_has_vmx_vmfunc()) {
3047 vmx->nested.nested_vmx_secondary_ctls_high |=
3048 SECONDARY_EXEC_ENABLE_VMFUNC;
41ab9372
BD
3049 /*
3050 * Advertise EPTP switching unconditionally
3051 * since we emulate it
3052 */
575b3a2c
WL
3053 if (enable_ept)
3054 vmx->nested.nested_vmx_vmfunc_controls =
3055 VMX_VMFUNC_EPTP_SWITCHING;
27c42a1b
BD
3056 }
3057
ef697a71
PB
3058 /*
3059 * Old versions of KVM use the single-context version without
3060 * checking for support, so declare that it is supported even
3061 * though it is treated as global context. The alternative is
3062 * not failing the single-context invvpid, and it is worse.
3063 */
63cb6d5f
WL
3064 if (enable_vpid) {
3065 vmx->nested.nested_vmx_secondary_ctls_high |=
3066 SECONDARY_EXEC_ENABLE_VPID;
089d7b6e 3067 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
bcdde302 3068 VMX_VPID_EXTENT_SUPPORTED_MASK;
1c13bffd 3069 }
99b83ac8 3070
0790ec17
RK
3071 if (enable_unrestricted_guest)
3072 vmx->nested.nested_vmx_secondary_ctls_high |=
3073 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3074
c18911a2 3075 /* miscellaneous data */
b9c237bb
WV
3076 rdmsr(MSR_IA32_VMX_MISC,
3077 vmx->nested.nested_vmx_misc_low,
3078 vmx->nested.nested_vmx_misc_high);
3079 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
3080 vmx->nested.nested_vmx_misc_low |=
3081 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
f4124500 3082 VMX_MISC_ACTIVITY_HLT;
b9c237bb 3083 vmx->nested.nested_vmx_misc_high = 0;
62cc6b9d
DM
3084
3085 /*
3086 * This MSR reports some information about VMX support. We
3087 * should return information about the VMX we emulate for the
3088 * guest, and the VMCS structure we give it - not about the
3089 * VMX support of the underlying hardware.
3090 */
3091 vmx->nested.nested_vmx_basic =
3092 VMCS12_REVISION |
3093 VMX_BASIC_TRUE_CTLS |
3094 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3095 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3096
3097 if (cpu_has_vmx_basic_inout())
3098 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
3099
3100 /*
8322ebbb 3101 * These MSRs specify bits which the guest must keep fixed on
62cc6b9d
DM
3102 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3103 * We picked the standard core2 setting.
3104 */
3105#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3106#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3107 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
62cc6b9d 3108 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
8322ebbb
DM
3109
3110 /* These MSRs specify bits which the guest must keep fixed off. */
3111 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
3112 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
62cc6b9d
DM
3113
3114 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
3115 vmx->nested.nested_vmx_vmcs_enum = 0x2e;
b87a51ae
NHE
3116}
3117
3899152c
DM
3118/*
3119 * if fixed0[i] == 1: val[i] must be 1
3120 * if fixed1[i] == 0: val[i] must be 0
3121 */
3122static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3123{
3124 return ((val & fixed1) | fixed0) == val;
b87a51ae
NHE
3125}
3126
3127static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3128{
3899152c 3129 return fixed_bits_valid(control, low, high);
b87a51ae
NHE
3130}
3131
3132static inline u64 vmx_control_msr(u32 low, u32 high)
3133{
3134 return low | ((u64)high << 32);
3135}
3136
62cc6b9d
DM
3137static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3138{
3139 superset &= mask;
3140 subset &= mask;
3141
3142 return (superset | subset) == superset;
3143}
3144
3145static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3146{
3147 const u64 feature_and_reserved =
3148 /* feature (except bit 48; see below) */
3149 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3150 /* reserved */
3151 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
3152 u64 vmx_basic = vmx->nested.nested_vmx_basic;
3153
3154 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3155 return -EINVAL;
3156
3157 /*
3158 * KVM does not emulate a version of VMX that constrains physical
3159 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3160 */
3161 if (data & BIT_ULL(48))
3162 return -EINVAL;
3163
3164 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3165 vmx_basic_vmcs_revision_id(data))
3166 return -EINVAL;
3167
3168 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3169 return -EINVAL;
3170
3171 vmx->nested.nested_vmx_basic = data;
3172 return 0;
3173}
3174
3175static int
3176vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3177{
3178 u64 supported;
3179 u32 *lowp, *highp;
3180
3181 switch (msr_index) {
3182 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3183 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
3184 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
3185 break;
3186 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3187 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
3188 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
3189 break;
3190 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3191 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
3192 highp = &vmx->nested.nested_vmx_exit_ctls_high;
3193 break;
3194 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3195 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
3196 highp = &vmx->nested.nested_vmx_entry_ctls_high;
3197 break;
3198 case MSR_IA32_VMX_PROCBASED_CTLS2:
3199 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
3200 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
3201 break;
3202 default:
3203 BUG();
3204 }
3205
3206 supported = vmx_control_msr(*lowp, *highp);
3207
3208 /* Check must-be-1 bits are still 1. */
3209 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3210 return -EINVAL;
3211
3212 /* Check must-be-0 bits are still 0. */
3213 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3214 return -EINVAL;
3215
3216 *lowp = data;
3217 *highp = data >> 32;
3218 return 0;
3219}
3220
3221static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3222{
3223 const u64 feature_and_reserved_bits =
3224 /* feature */
3225 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3226 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3227 /* reserved */
3228 GENMASK_ULL(13, 9) | BIT_ULL(31);
3229 u64 vmx_misc;
3230
3231 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
3232 vmx->nested.nested_vmx_misc_high);
3233
3234 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3235 return -EINVAL;
3236
3237 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
3238 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3239 vmx_misc_preemption_timer_rate(data) !=
3240 vmx_misc_preemption_timer_rate(vmx_misc))
3241 return -EINVAL;
3242
3243 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3244 return -EINVAL;
3245
3246 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3247 return -EINVAL;
3248
3249 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3250 return -EINVAL;
3251
3252 vmx->nested.nested_vmx_misc_low = data;
3253 vmx->nested.nested_vmx_misc_high = data >> 32;
3254 return 0;
3255}
3256
3257static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3258{
3259 u64 vmx_ept_vpid_cap;
3260
3261 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
3262 vmx->nested.nested_vmx_vpid_caps);
3263
3264 /* Every bit is either reserved or a feature bit. */
3265 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3266 return -EINVAL;
3267
3268 vmx->nested.nested_vmx_ept_caps = data;
3269 vmx->nested.nested_vmx_vpid_caps = data >> 32;
3270 return 0;
3271}
3272
3273static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3274{
3275 u64 *msr;
3276
3277 switch (msr_index) {
3278 case MSR_IA32_VMX_CR0_FIXED0:
3279 msr = &vmx->nested.nested_vmx_cr0_fixed0;
3280 break;
3281 case MSR_IA32_VMX_CR4_FIXED0:
3282 msr = &vmx->nested.nested_vmx_cr4_fixed0;
3283 break;
3284 default:
3285 BUG();
3286 }
3287
3288 /*
3289 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3290 * must be 1 in the restored value.
3291 */
3292 if (!is_bitwise_subset(data, *msr, -1ULL))
3293 return -EINVAL;
3294
3295 *msr = data;
3296 return 0;
3297}
3298
3299/*
3300 * Called when userspace is restoring VMX MSRs.
3301 *
3302 * Returns 0 on success, non-0 otherwise.
3303 */
3304static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
b87a51ae 3305{
b9c237bb
WV
3306 struct vcpu_vmx *vmx = to_vmx(vcpu);
3307
b87a51ae 3308 switch (msr_index) {
b87a51ae 3309 case MSR_IA32_VMX_BASIC:
62cc6b9d
DM
3310 return vmx_restore_vmx_basic(vmx, data);
3311 case MSR_IA32_VMX_PINBASED_CTLS:
3312 case MSR_IA32_VMX_PROCBASED_CTLS:
3313 case MSR_IA32_VMX_EXIT_CTLS:
3314 case MSR_IA32_VMX_ENTRY_CTLS:
b87a51ae 3315 /*
62cc6b9d
DM
3316 * The "non-true" VMX capability MSRs are generated from the
3317 * "true" MSRs, so we do not support restoring them directly.
3318 *
3319 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3320 * should restore the "true" MSRs with the must-be-1 bits
3321 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3322 * DEFAULT SETTINGS".
b87a51ae 3323 */
62cc6b9d
DM
3324 return -EINVAL;
3325 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3326 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3327 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3328 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3329 case MSR_IA32_VMX_PROCBASED_CTLS2:
3330 return vmx_restore_control_msr(vmx, msr_index, data);
3331 case MSR_IA32_VMX_MISC:
3332 return vmx_restore_vmx_misc(vmx, data);
3333 case MSR_IA32_VMX_CR0_FIXED0:
3334 case MSR_IA32_VMX_CR4_FIXED0:
3335 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3336 case MSR_IA32_VMX_CR0_FIXED1:
3337 case MSR_IA32_VMX_CR4_FIXED1:
3338 /*
3339 * These MSRs are generated based on the vCPU's CPUID, so we
3340 * do not support restoring them directly.
3341 */
3342 return -EINVAL;
3343 case MSR_IA32_VMX_EPT_VPID_CAP:
3344 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3345 case MSR_IA32_VMX_VMCS_ENUM:
3346 vmx->nested.nested_vmx_vmcs_enum = data;
3347 return 0;
3348 default:
b87a51ae 3349 /*
62cc6b9d 3350 * The rest of the VMX capability MSRs do not support restore.
b87a51ae 3351 */
62cc6b9d
DM
3352 return -EINVAL;
3353 }
3354}
3355
3356/* Returns 0 on success, non-0 otherwise. */
3357static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3358{
3359 struct vcpu_vmx *vmx = to_vmx(vcpu);
3360
3361 switch (msr_index) {
3362 case MSR_IA32_VMX_BASIC:
3363 *pdata = vmx->nested.nested_vmx_basic;
b87a51ae
NHE
3364 break;
3365 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3366 case MSR_IA32_VMX_PINBASED_CTLS:
b9c237bb
WV
3367 *pdata = vmx_control_msr(
3368 vmx->nested.nested_vmx_pinbased_ctls_low,
3369 vmx->nested.nested_vmx_pinbased_ctls_high);
0115f9cb
DM
3370 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3371 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
3372 break;
3373 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3374 case MSR_IA32_VMX_PROCBASED_CTLS:
b9c237bb
WV
3375 *pdata = vmx_control_msr(
3376 vmx->nested.nested_vmx_procbased_ctls_low,
3377 vmx->nested.nested_vmx_procbased_ctls_high);
0115f9cb
DM
3378 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3379 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
3380 break;
3381 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3382 case MSR_IA32_VMX_EXIT_CTLS:
b9c237bb
WV
3383 *pdata = vmx_control_msr(
3384 vmx->nested.nested_vmx_exit_ctls_low,
3385 vmx->nested.nested_vmx_exit_ctls_high);
0115f9cb
DM
3386 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3387 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
3388 break;
3389 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3390 case MSR_IA32_VMX_ENTRY_CTLS:
b9c237bb
WV
3391 *pdata = vmx_control_msr(
3392 vmx->nested.nested_vmx_entry_ctls_low,
3393 vmx->nested.nested_vmx_entry_ctls_high);
0115f9cb
DM
3394 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3395 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
3396 break;
3397 case MSR_IA32_VMX_MISC:
b9c237bb
WV
3398 *pdata = vmx_control_msr(
3399 vmx->nested.nested_vmx_misc_low,
3400 vmx->nested.nested_vmx_misc_high);
b87a51ae 3401 break;
b87a51ae 3402 case MSR_IA32_VMX_CR0_FIXED0:
62cc6b9d 3403 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
b87a51ae
NHE
3404 break;
3405 case MSR_IA32_VMX_CR0_FIXED1:
62cc6b9d 3406 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
b87a51ae
NHE
3407 break;
3408 case MSR_IA32_VMX_CR4_FIXED0:
62cc6b9d 3409 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
b87a51ae
NHE
3410 break;
3411 case MSR_IA32_VMX_CR4_FIXED1:
62cc6b9d 3412 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
b87a51ae
NHE
3413 break;
3414 case MSR_IA32_VMX_VMCS_ENUM:
62cc6b9d 3415 *pdata = vmx->nested.nested_vmx_vmcs_enum;
b87a51ae
NHE
3416 break;
3417 case MSR_IA32_VMX_PROCBASED_CTLS2:
b9c237bb
WV
3418 *pdata = vmx_control_msr(
3419 vmx->nested.nested_vmx_secondary_ctls_low,
3420 vmx->nested.nested_vmx_secondary_ctls_high);
b87a51ae
NHE
3421 break;
3422 case MSR_IA32_VMX_EPT_VPID_CAP:
089d7b6e
WL
3423 *pdata = vmx->nested.nested_vmx_ept_caps |
3424 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
b87a51ae 3425 break;
27c42a1b
BD
3426 case MSR_IA32_VMX_VMFUNC:
3427 *pdata = vmx->nested.nested_vmx_vmfunc_controls;
3428 break;
b87a51ae 3429 default:
b87a51ae 3430 return 1;
b3897a49
NHE
3431 }
3432
b87a51ae
NHE
3433 return 0;
3434}
3435
37e4c997
HZ
3436static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3437 uint64_t val)
3438{
3439 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3440
3441 return !(val & ~valid_bits);
3442}
3443
ab1bebf8
TL
3444static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3445{
3446 return 1;
3447}
3448
6aa8b732
AK
3449/*
3450 * Reads an msr value (of 'msr_index') into 'pdata'.
3451 * Returns 0 on success, non-0 otherwise.
3452 * Assumes vcpu_load() was already called.
3453 */
609e36d3 3454static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
6aa8b732 3455{
26bb0981 3456 struct shared_msr_entry *msr;
6aa8b732 3457
609e36d3 3458 switch (msr_info->index) {
05b3e0c2 3459#ifdef CONFIG_X86_64
6aa8b732 3460 case MSR_FS_BASE:
609e36d3 3461 msr_info->data = vmcs_readl(GUEST_FS_BASE);
6aa8b732
AK
3462 break;
3463 case MSR_GS_BASE:
609e36d3 3464 msr_info->data = vmcs_readl(GUEST_GS_BASE);
6aa8b732 3465 break;
44ea2b17
AK
3466 case MSR_KERNEL_GS_BASE:
3467 vmx_load_host_state(to_vmx(vcpu));
609e36d3 3468 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
44ea2b17 3469 break;
26bb0981 3470#endif
6aa8b732 3471 case MSR_EFER:
609e36d3 3472 return kvm_get_msr_common(vcpu, msr_info);
74469996
KA
3473 case MSR_IA32_SPEC_CTRL:
3474 if (!msr_info->host_initiated &&
5856293c 3475 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
74469996
KA
3476 return 1;
3477
3478 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3479 break;
a6005a79
KA
3480 case MSR_IA32_ARCH_CAPABILITIES:
3481 if (!msr_info->host_initiated &&
3482 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3483 return 1;
3484 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3485 break;
6aa8b732 3486 case MSR_IA32_SYSENTER_CS:
609e36d3 3487 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
6aa8b732
AK
3488 break;
3489 case MSR_IA32_SYSENTER_EIP:
609e36d3 3490 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
6aa8b732
AK
3491 break;
3492 case MSR_IA32_SYSENTER_ESP:
609e36d3 3493 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
6aa8b732 3494 break;
0dd376e7 3495 case MSR_IA32_BNDCFGS:
691bd434 3496 if (!kvm_mpx_supported() ||
d6321d49
RK
3497 (!msr_info->host_initiated &&
3498 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
93c4adc7 3499 return 1;
609e36d3 3500 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
0dd376e7 3501 break;
c45dcc71
AR
3502 case MSR_IA32_MCG_EXT_CTL:
3503 if (!msr_info->host_initiated &&
3504 !(to_vmx(vcpu)->msr_ia32_feature_control &
3505 FEATURE_CONTROL_LMCE))
cae50139 3506 return 1;
c45dcc71
AR
3507 msr_info->data = vcpu->arch.mcg_ext_ctl;
3508 break;
cae50139 3509 case MSR_IA32_FEATURE_CONTROL:
3b84080b 3510 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
cae50139
JK
3511 break;
3512 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3513 if (!nested_vmx_allowed(vcpu))
3514 return 1;
609e36d3 3515 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
20300099
WL
3516 case MSR_IA32_XSS:
3517 if (!vmx_xsaves_supported())
3518 return 1;
609e36d3 3519 msr_info->data = vcpu->arch.ia32_xss;
20300099 3520 break;
4e47c7a6 3521 case MSR_TSC_AUX:
d6321d49
RK
3522 if (!msr_info->host_initiated &&
3523 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
4e47c7a6
SY
3524 return 1;
3525 /* Otherwise falls through */
6aa8b732 3526 default:
609e36d3 3527 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
3bab1f5d 3528 if (msr) {
609e36d3 3529 msr_info->data = msr->data;
3bab1f5d 3530 break;
6aa8b732 3531 }
609e36d3 3532 return kvm_get_msr_common(vcpu, msr_info);
6aa8b732
AK
3533 }
3534
6aa8b732
AK
3535 return 0;
3536}
3537
cae50139
JK
3538static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3539
6aa8b732
AK
3540/*
3541 * Writes msr value into into the appropriate "register".
3542 * Returns 0 on success, non-0 otherwise.
3543 * Assumes vcpu_load() was already called.
3544 */
8fe8ab46 3545static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
6aa8b732 3546{
a2fa3e9f 3547 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981 3548 struct shared_msr_entry *msr;
2cc51560 3549 int ret = 0;
8fe8ab46
WA
3550 u32 msr_index = msr_info->index;
3551 u64 data = msr_info->data;
2cc51560 3552
6aa8b732 3553 switch (msr_index) {
3bab1f5d 3554 case MSR_EFER:
8fe8ab46 3555 ret = kvm_set_msr_common(vcpu, msr_info);
2cc51560 3556 break;
16175a79 3557#ifdef CONFIG_X86_64
6aa8b732 3558 case MSR_FS_BASE:
2fb92db1 3559 vmx_segment_cache_clear(vmx);
6aa8b732
AK
3560 vmcs_writel(GUEST_FS_BASE, data);
3561 break;
3562 case MSR_GS_BASE:
2fb92db1 3563 vmx_segment_cache_clear(vmx);
6aa8b732
AK
3564 vmcs_writel(GUEST_GS_BASE, data);
3565 break;
44ea2b17
AK
3566 case MSR_KERNEL_GS_BASE:
3567 vmx_load_host_state(vmx);
3568 vmx->msr_guest_kernel_gs_base = data;
3569 break;
6aa8b732
AK
3570#endif
3571 case MSR_IA32_SYSENTER_CS:
3572 vmcs_write32(GUEST_SYSENTER_CS, data);
3573 break;
3574 case MSR_IA32_SYSENTER_EIP:
f5b42c33 3575 vmcs_writel(GUEST_SYSENTER_EIP, data);
6aa8b732
AK
3576 break;
3577 case MSR_IA32_SYSENTER_ESP:
f5b42c33 3578 vmcs_writel(GUEST_SYSENTER_ESP, data);
6aa8b732 3579 break;
0dd376e7 3580 case MSR_IA32_BNDCFGS:
691bd434 3581 if (!kvm_mpx_supported() ||
d6321d49
RK
3582 (!msr_info->host_initiated &&
3583 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
93c4adc7 3584 return 1;
fd8cb433 3585 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
4531662d 3586 (data & MSR_IA32_BNDCFGS_RSVD))
93c4adc7 3587 return 1;
0dd376e7
LJ
3588 vmcs_write64(GUEST_BNDCFGS, data);
3589 break;
74469996
KA
3590 case MSR_IA32_SPEC_CTRL:
3591 if (!msr_info->host_initiated &&
5856293c 3592 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
74469996
KA
3593 return 1;
3594
3595 /* The STIBP bit doesn't fault even if it's not advertised */
8fe36c9d 3596 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
74469996
KA
3597 return 1;
3598
3599 vmx->spec_ctrl = data;
3600
3601 if (!data)
3602 break;
3603
3604 /*
3605 * For non-nested:
3606 * When it's written (to non-zero) for the first time, pass
3607 * it through.
3608 *
3609 * For nested:
3610 * The handling of the MSR bitmap for L2 guests is done in
3611 * nested_vmx_merge_msr_bitmap. We should not touch the
3612 * vmcs02.msr_bitmap here since it gets completely overwritten
3613 * in the merging. We update the vmcs01 here for L1 as well
3614 * since it will end up touching the MSR anyway now.
3615 */
3616 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3617 MSR_IA32_SPEC_CTRL,
3618 MSR_TYPE_RW);
3619 break;
33241bfe
AR
3620 case MSR_IA32_PRED_CMD:
3621 if (!msr_info->host_initiated &&
33241bfe
AR
3622 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3623 return 1;
3624
3625 if (data & ~PRED_CMD_IBPB)
3626 return 1;
3627
3628 if (!data)
3629 break;
3630
3631 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3632
3633 /*
3634 * For non-nested:
3635 * When it's written (to non-zero) for the first time, pass
3636 * it through.
3637 *
3638 * For nested:
3639 * The handling of the MSR bitmap for L2 guests is done in
3640 * nested_vmx_merge_msr_bitmap. We should not touch the
3641 * vmcs02.msr_bitmap here since it gets completely overwritten
3642 * in the merging.
3643 */
3644 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3645 MSR_TYPE_W);
3646 break;
a6005a79
KA
3647 case MSR_IA32_ARCH_CAPABILITIES:
3648 if (!msr_info->host_initiated)
3649 return 1;
3650 vmx->arch_capabilities = data;
3651 break;
468d472f
SY
3652 case MSR_IA32_CR_PAT:
3653 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
4566654b
NA
3654 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3655 return 1;
468d472f
SY
3656 vmcs_write64(GUEST_IA32_PAT, data);
3657 vcpu->arch.pat = data;
3658 break;
3659 }
8fe8ab46 3660 ret = kvm_set_msr_common(vcpu, msr_info);
4e47c7a6 3661 break;
ba904635
WA
3662 case MSR_IA32_TSC_ADJUST:
3663 ret = kvm_set_msr_common(vcpu, msr_info);
4e47c7a6 3664 break;
c45dcc71
AR
3665 case MSR_IA32_MCG_EXT_CTL:
3666 if ((!msr_info->host_initiated &&
3667 !(to_vmx(vcpu)->msr_ia32_feature_control &
3668 FEATURE_CONTROL_LMCE)) ||
3669 (data & ~MCG_EXT_CTL_LMCE_EN))
3670 return 1;
3671 vcpu->arch.mcg_ext_ctl = data;
3672 break;
cae50139 3673 case MSR_IA32_FEATURE_CONTROL:
37e4c997 3674 if (!vmx_feature_control_msr_valid(vcpu, data) ||
3b84080b 3675 (to_vmx(vcpu)->msr_ia32_feature_control &
cae50139
JK
3676 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3677 return 1;
3b84080b 3678 vmx->msr_ia32_feature_control = data;
cae50139
JK
3679 if (msr_info->host_initiated && data == 0)
3680 vmx_leave_nested(vcpu);
3681 break;
3682 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
62cc6b9d
DM
3683 if (!msr_info->host_initiated)
3684 return 1; /* they are read-only */
3685 if (!nested_vmx_allowed(vcpu))
3686 return 1;
3687 return vmx_set_vmx_msr(vcpu, msr_index, data);
20300099
WL
3688 case MSR_IA32_XSS:
3689 if (!vmx_xsaves_supported())
3690 return 1;
3691 /*
3692 * The only supported bit as of Skylake is bit 8, but
3693 * it is not supported on KVM.
3694 */
3695 if (data != 0)
3696 return 1;
3697 vcpu->arch.ia32_xss = data;
3698 if (vcpu->arch.ia32_xss != host_xss)
3699 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
129ce7ac 3700 vcpu->arch.ia32_xss, host_xss, false);
20300099
WL
3701 else
3702 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3703 break;
4e47c7a6 3704 case MSR_TSC_AUX:
d6321d49
RK
3705 if (!msr_info->host_initiated &&
3706 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
4e47c7a6
SY
3707 return 1;
3708 /* Check reserved bit, higher 32 bits should be zero */
3709 if ((data >> 32) != 0)
3710 return 1;
3711 /* Otherwise falls through */
6aa8b732 3712 default:
8b9cf98c 3713 msr = find_msr_entry(vmx, msr_index);
3bab1f5d 3714 if (msr) {
8b3c3104 3715 u64 old_msr_data = msr->data;
3bab1f5d 3716 msr->data = data;
2225fd56
AK
3717 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3718 preempt_disable();
8b3c3104
AH
3719 ret = kvm_set_shared_msr(msr->index, msr->data,
3720 msr->mask);
2225fd56 3721 preempt_enable();
8b3c3104
AH
3722 if (ret)
3723 msr->data = old_msr_data;
2225fd56 3724 }
3bab1f5d 3725 break;
6aa8b732 3726 }
8fe8ab46 3727 ret = kvm_set_msr_common(vcpu, msr_info);
6aa8b732
AK
3728 }
3729
2cc51560 3730 return ret;
6aa8b732
AK
3731}
3732
5fdbf976 3733static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
6aa8b732 3734{
5fdbf976
MT
3735 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3736 switch (reg) {
3737 case VCPU_REGS_RSP:
3738 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3739 break;
3740 case VCPU_REGS_RIP:
3741 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3742 break;
6de4f3ad
AK
3743 case VCPU_EXREG_PDPTR:
3744 if (enable_ept)
3745 ept_save_pdptrs(vcpu);
3746 break;
5fdbf976
MT
3747 default:
3748 break;
3749 }
6aa8b732
AK
3750}
3751
6aa8b732
AK
3752static __init int cpu_has_kvm_support(void)
3753{
6210e37b 3754 return cpu_has_vmx();
6aa8b732
AK
3755}
3756
3757static __init int vmx_disabled_by_bios(void)
3758{
3759 u64 msr;
3760
3761 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
cafd6659 3762 if (msr & FEATURE_CONTROL_LOCKED) {
23f3e991 3763 /* launched w/ TXT and VMX disabled */
cafd6659
SW
3764 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3765 && tboot_enabled())
3766 return 1;
23f3e991 3767 /* launched w/o TXT and VMX only enabled w/ TXT */
cafd6659 3768 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
23f3e991 3769 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
f9335afe
SW
3770 && !tboot_enabled()) {
3771 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
23f3e991 3772 "activate TXT before enabling KVM\n");
cafd6659 3773 return 1;
f9335afe 3774 }
23f3e991
JC
3775 /* launched w/o TXT and VMX disabled */
3776 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3777 && !tboot_enabled())
3778 return 1;
cafd6659
SW
3779 }
3780
3781 return 0;
6aa8b732
AK
3782}
3783
7725b894
DX
3784static void kvm_cpu_vmxon(u64 addr)
3785{
fe0e80be 3786 cr4_set_bits(X86_CR4_VMXE);
1c5ac21a
AS
3787 intel_pt_handle_vmx(1);
3788
7725b894
DX
3789 asm volatile (ASM_VMX_VMXON_RAX
3790 : : "a"(&addr), "m"(addr)
3791 : "memory", "cc");
3792}
3793
13a34e06 3794static int hardware_enable(void)
6aa8b732
AK
3795{
3796 int cpu = raw_smp_processor_id();
3797 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
cafd6659 3798 u64 old, test_bits;
6aa8b732 3799
1e02ce4c 3800 if (cr4_read_shadow() & X86_CR4_VMXE)
10474ae8
AG
3801 return -EBUSY;
3802
d462b819 3803 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
bf9f6ac8
FW
3804 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3805 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
8f536b76
ZY
3806
3807 /*
3808 * Now we can enable the vmclear operation in kdump
3809 * since the loaded_vmcss_on_cpu list on this cpu
3810 * has been initialized.
3811 *
3812 * Though the cpu is not in VMX operation now, there
3813 * is no problem to enable the vmclear operation
3814 * for the loaded_vmcss_on_cpu list is empty!
3815 */
3816 crash_enable_local_vmclear(cpu);
3817
6aa8b732 3818 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
cafd6659
SW
3819
3820 test_bits = FEATURE_CONTROL_LOCKED;
3821 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3822 if (tboot_enabled())
3823 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3824
3825 if ((old & test_bits) != test_bits) {
6aa8b732 3826 /* enable and lock */
cafd6659
SW
3827 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3828 }
fe0e80be 3829 kvm_cpu_vmxon(phys_addr);
fdf288bf
DH
3830 if (enable_ept)
3831 ept_sync_global();
10474ae8
AG
3832
3833 return 0;
6aa8b732
AK
3834}
3835
d462b819 3836static void vmclear_local_loaded_vmcss(void)
543e4243
AK
3837{
3838 int cpu = raw_smp_processor_id();
d462b819 3839 struct loaded_vmcs *v, *n;
543e4243 3840
d462b819
NHE
3841 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3842 loaded_vmcss_on_cpu_link)
3843 __loaded_vmcs_clear(v);
543e4243
AK
3844}
3845
710ff4a8
EH
3846
3847/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3848 * tricks.
3849 */
3850static void kvm_cpu_vmxoff(void)
6aa8b732 3851{
4ecac3fd 3852 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1c5ac21a
AS
3853
3854 intel_pt_handle_vmx(0);
fe0e80be 3855 cr4_clear_bits(X86_CR4_VMXE);
6aa8b732
AK
3856}
3857
13a34e06 3858static void hardware_disable(void)
710ff4a8 3859{
fe0e80be
DH
3860 vmclear_local_loaded_vmcss();
3861 kvm_cpu_vmxoff();
710ff4a8
EH
3862}
3863
1c3d14fe 3864static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
d77c26fc 3865 u32 msr, u32 *result)
1c3d14fe
YS
3866{
3867 u32 vmx_msr_low, vmx_msr_high;
3868 u32 ctl = ctl_min | ctl_opt;
3869
3870 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3871
3872 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3873 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3874
3875 /* Ensure minimum (required) set of control bits are supported. */
3876 if (ctl_min & ~ctl)
002c7f7c 3877 return -EIO;
1c3d14fe
YS
3878
3879 *result = ctl;
3880 return 0;
3881}
3882
110312c8
AK
3883static __init bool allow_1_setting(u32 msr, u32 ctl)
3884{
3885 u32 vmx_msr_low, vmx_msr_high;
3886
3887 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3888 return vmx_msr_high & ctl;
3889}
3890
002c7f7c 3891static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
6aa8b732
AK
3892{
3893 u32 vmx_msr_low, vmx_msr_high;
d56f546d 3894 u32 min, opt, min2, opt2;
1c3d14fe
YS
3895 u32 _pin_based_exec_control = 0;
3896 u32 _cpu_based_exec_control = 0;
f78e0e2e 3897 u32 _cpu_based_2nd_exec_control = 0;
1c3d14fe
YS
3898 u32 _vmexit_control = 0;
3899 u32 _vmentry_control = 0;
3900
10166744 3901 min = CPU_BASED_HLT_EXITING |
1c3d14fe
YS
3902#ifdef CONFIG_X86_64
3903 CPU_BASED_CR8_LOAD_EXITING |
3904 CPU_BASED_CR8_STORE_EXITING |
3905#endif
d56f546d
SY
3906 CPU_BASED_CR3_LOAD_EXITING |
3907 CPU_BASED_CR3_STORE_EXITING |
1c3d14fe
YS
3908 CPU_BASED_USE_IO_BITMAPS |
3909 CPU_BASED_MOV_DR_EXITING |
a7052897 3910 CPU_BASED_USE_TSC_OFFSETING |
fee84b07
AK
3911 CPU_BASED_INVLPG_EXITING |
3912 CPU_BASED_RDPMC_EXITING;
443381a8 3913
668fffa3
MT
3914 if (!kvm_mwait_in_guest())
3915 min |= CPU_BASED_MWAIT_EXITING |
3916 CPU_BASED_MONITOR_EXITING;
3917
f78e0e2e 3918 opt = CPU_BASED_TPR_SHADOW |
25c5f225 3919 CPU_BASED_USE_MSR_BITMAPS |
f78e0e2e 3920 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1c3d14fe
YS
3921 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3922 &_cpu_based_exec_control) < 0)
002c7f7c 3923 return -EIO;
6e5d865c
YS
3924#ifdef CONFIG_X86_64
3925 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3926 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3927 ~CPU_BASED_CR8_STORE_EXITING;
3928#endif
f78e0e2e 3929 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
d56f546d
SY
3930 min2 = 0;
3931 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
8d14695f 3932 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2384d2b3 3933 SECONDARY_EXEC_WBINVD_EXITING |
d56f546d 3934 SECONDARY_EXEC_ENABLE_VPID |
3a624e29 3935 SECONDARY_EXEC_ENABLE_EPT |
4b8d54f9 3936 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4e47c7a6 3937 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
ad756a16 3938 SECONDARY_EXEC_RDTSCP |
83d4c286 3939 SECONDARY_EXEC_ENABLE_INVPCID |
c7c9c56c 3940 SECONDARY_EXEC_APIC_REGISTER_VIRT |
abc4fc58 3941 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
20300099 3942 SECONDARY_EXEC_SHADOW_VMCS |
843e4330 3943 SECONDARY_EXEC_XSAVES |
736fdf72
DH
3944 SECONDARY_EXEC_RDSEED_EXITING |
3945 SECONDARY_EXEC_RDRAND_EXITING |
8b3e34e4 3946 SECONDARY_EXEC_ENABLE_PML |
2a499e49
BD
3947 SECONDARY_EXEC_TSC_SCALING |
3948 SECONDARY_EXEC_ENABLE_VMFUNC;
d56f546d
SY
3949 if (adjust_vmx_controls(min2, opt2,
3950 MSR_IA32_VMX_PROCBASED_CTLS2,
f78e0e2e
SY
3951 &_cpu_based_2nd_exec_control) < 0)
3952 return -EIO;
3953 }
3954#ifndef CONFIG_X86_64
3955 if (!(_cpu_based_2nd_exec_control &
3956 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3957 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3958#endif
83d4c286
YZ
3959
3960 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3961 _cpu_based_2nd_exec_control &= ~(
8d14695f 3962 SECONDARY_EXEC_APIC_REGISTER_VIRT |
c7c9c56c
YZ
3963 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3964 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
83d4c286 3965
61f1dd90
WL
3966 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3967 &vmx_capability.ept, &vmx_capability.vpid);
3968
d56f546d 3969 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
a7052897
MT
3970 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3971 enabled */
5fff7d27
GN
3972 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3973 CPU_BASED_CR3_STORE_EXITING |
3974 CPU_BASED_INVLPG_EXITING);
61f1dd90
WL
3975 } else if (vmx_capability.ept) {
3976 vmx_capability.ept = 0;
3977 pr_warn_once("EPT CAP should not exist if not support "
3978 "1-setting enable EPT VM-execution control\n");
3979 }
3980 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3981 vmx_capability.vpid) {
3982 vmx_capability.vpid = 0;
3983 pr_warn_once("VPID CAP should not exist if not support "
3984 "1-setting enable VPID VM-execution control\n");
d56f546d 3985 }
1c3d14fe 3986
91fa0f8e 3987 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
1c3d14fe
YS
3988#ifdef CONFIG_X86_64
3989 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3990#endif
a547c6db 3991 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
91fa0f8e 3992 VM_EXIT_CLEAR_BNDCFGS;
1c3d14fe
YS
3993 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3994 &_vmexit_control) < 0)
002c7f7c 3995 return -EIO;
1c3d14fe 3996
8a1b4392
PB
3997 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3998 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3999 PIN_BASED_VMX_PREEMPTION_TIMER;
01e439be
YZ
4000 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4001 &_pin_based_exec_control) < 0)
4002 return -EIO;
4003
1c17c3e6
PB
4004 if (cpu_has_broken_vmx_preemption_timer())
4005 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
01e439be 4006 if (!(_cpu_based_2nd_exec_control &
91fa0f8e 4007 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
01e439be
YZ
4008 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4009
c845f9c6 4010 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
da8999d3 4011 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
1c3d14fe
YS
4012 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4013 &_vmentry_control) < 0)
002c7f7c 4014 return -EIO;
6aa8b732 4015
c68876fd 4016 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
1c3d14fe
YS
4017
4018 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4019 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
002c7f7c 4020 return -EIO;
1c3d14fe
YS
4021
4022#ifdef CONFIG_X86_64
4023 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4024 if (vmx_msr_high & (1u<<16))
002c7f7c 4025 return -EIO;
1c3d14fe
YS
4026#endif
4027
4028 /* Require Write-Back (WB) memory type for VMCS accesses. */
4029 if (((vmx_msr_high >> 18) & 15) != 6)
002c7f7c 4030 return -EIO;
1c3d14fe 4031
002c7f7c 4032 vmcs_conf->size = vmx_msr_high & 0x1fff;
16cb0255 4033 vmcs_conf->order = get_order(vmcs_conf->size);
9ac7e3e8 4034 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
002c7f7c 4035 vmcs_conf->revision_id = vmx_msr_low;
1c3d14fe 4036
002c7f7c
YS
4037 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4038 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
f78e0e2e 4039 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
002c7f7c
YS
4040 vmcs_conf->vmexit_ctrl = _vmexit_control;
4041 vmcs_conf->vmentry_ctrl = _vmentry_control;
1c3d14fe 4042
110312c8
AK
4043 cpu_has_load_ia32_efer =
4044 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4045 VM_ENTRY_LOAD_IA32_EFER)
4046 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4047 VM_EXIT_LOAD_IA32_EFER);
4048
8bf00a52
GN
4049 cpu_has_load_perf_global_ctrl =
4050 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4051 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4052 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4053 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4054
4055 /*
4056 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
bb3541f1 4057 * but due to errata below it can't be used. Workaround is to use
8bf00a52
GN
4058 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4059 *
4060 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4061 *
4062 * AAK155 (model 26)
4063 * AAP115 (model 30)
4064 * AAT100 (model 37)
4065 * BC86,AAY89,BD102 (model 44)
4066 * BA97 (model 46)
4067 *
4068 */
4069 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4070 switch (boot_cpu_data.x86_model) {
4071 case 26:
4072 case 30:
4073 case 37:
4074 case 44:
4075 case 46:
4076 cpu_has_load_perf_global_ctrl = false;
4077 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4078 "does not work properly. Using workaround\n");
4079 break;
4080 default:
4081 break;
4082 }
4083 }
4084
782511b0 4085 if (boot_cpu_has(X86_FEATURE_XSAVES))
20300099
WL
4086 rdmsrl(MSR_IA32_XSS, host_xss);
4087
1c3d14fe 4088 return 0;
c68876fd 4089}
6aa8b732
AK
4090
4091static struct vmcs *alloc_vmcs_cpu(int cpu)
4092{
4093 int node = cpu_to_node(cpu);
4094 struct page *pages;
4095 struct vmcs *vmcs;
4096
96db800f 4097 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
6aa8b732
AK
4098 if (!pages)
4099 return NULL;
4100 vmcs = page_address(pages);
1c3d14fe
YS
4101 memset(vmcs, 0, vmcs_config.size);
4102 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
6aa8b732
AK
4103 return vmcs;
4104}
4105
6aa8b732
AK
4106static void free_vmcs(struct vmcs *vmcs)
4107{
1c3d14fe 4108 free_pages((unsigned long)vmcs, vmcs_config.order);
6aa8b732
AK
4109}
4110
d462b819
NHE
4111/*
4112 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4113 */
4114static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4115{
4116 if (!loaded_vmcs->vmcs)
4117 return;
4118 loaded_vmcs_clear(loaded_vmcs);
4119 free_vmcs(loaded_vmcs->vmcs);
4120 loaded_vmcs->vmcs = NULL;
4b0be90f
PB
4121 if (loaded_vmcs->msr_bitmap)
4122 free_page((unsigned long)loaded_vmcs->msr_bitmap);
355f4fb1 4123 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
d462b819
NHE
4124}
4125
b6d7026d
PB
4126static struct vmcs *alloc_vmcs(void)
4127{
4128 return alloc_vmcs_cpu(raw_smp_processor_id());
4129}
4130
4131static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4132{
4133 loaded_vmcs->vmcs = alloc_vmcs();
4134 if (!loaded_vmcs->vmcs)
4135 return -ENOMEM;
4136
4137 loaded_vmcs->shadow_vmcs = NULL;
4138 loaded_vmcs_init(loaded_vmcs);
4b0be90f
PB
4139
4140 if (cpu_has_vmx_msr_bitmap()) {
4141 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4142 if (!loaded_vmcs->msr_bitmap)
4143 goto out_vmcs;
4144 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4145 }
b6d7026d 4146 return 0;
4b0be90f
PB
4147
4148out_vmcs:
4149 free_loaded_vmcs(loaded_vmcs);
4150 return -ENOMEM;
b6d7026d
PB
4151}
4152
39959588 4153static void free_kvm_area(void)
6aa8b732
AK
4154{
4155 int cpu;
4156
3230bb47 4157 for_each_possible_cpu(cpu) {
6aa8b732 4158 free_vmcs(per_cpu(vmxarea, cpu));
3230bb47
ZA
4159 per_cpu(vmxarea, cpu) = NULL;
4160 }
6aa8b732
AK
4161}
4162
85fd514e
JM
4163enum vmcs_field_type {
4164 VMCS_FIELD_TYPE_U16 = 0,
4165 VMCS_FIELD_TYPE_U64 = 1,
4166 VMCS_FIELD_TYPE_U32 = 2,
4167 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
4168};
4169
4170static inline int vmcs_field_type(unsigned long field)
4171{
4172 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
4173 return VMCS_FIELD_TYPE_U32;
4174 return (field >> 13) & 0x3 ;
4175}
4176
4177static inline int vmcs_field_readonly(unsigned long field)
4178{
4179 return (((field >> 10) & 0x3) == 1);
4180}
4181
fe2b201b
BD
4182static void init_vmcs_shadow_fields(void)
4183{
4184 int i, j;
4185
4186 /* No checks for read only fields yet */
4187
4188 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
4189 switch (shadow_read_write_fields[i]) {
4190 case GUEST_BNDCFGS:
a87036ad 4191 if (!kvm_mpx_supported())
fe2b201b
BD
4192 continue;
4193 break;
4194 default:
4195 break;
4196 }
4197
4198 if (j < i)
4199 shadow_read_write_fields[j] =
4200 shadow_read_write_fields[i];
4201 j++;
4202 }
4203 max_shadow_read_write_fields = j;
4204
4205 /* shadowed fields guest access without vmexit */
4206 for (i = 0; i < max_shadow_read_write_fields; i++) {
85fd514e
JM
4207 unsigned long field = shadow_read_write_fields[i];
4208
4209 clear_bit(field, vmx_vmwrite_bitmap);
4210 clear_bit(field, vmx_vmread_bitmap);
4211 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) {
4212 clear_bit(field + 1, vmx_vmwrite_bitmap);
4213 clear_bit(field + 1, vmx_vmread_bitmap);
4214 }
4215 }
4216 for (i = 0; i < max_shadow_read_only_fields; i++) {
4217 unsigned long field = shadow_read_only_fields[i];
4218
4219 clear_bit(field, vmx_vmread_bitmap);
4220 if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64)
4221 clear_bit(field + 1, vmx_vmread_bitmap);
fe2b201b 4222 }
fe2b201b
BD
4223}
4224
6aa8b732
AK
4225static __init int alloc_kvm_area(void)
4226{
4227 int cpu;
4228
3230bb47 4229 for_each_possible_cpu(cpu) {
6aa8b732
AK
4230 struct vmcs *vmcs;
4231
4232 vmcs = alloc_vmcs_cpu(cpu);
4233 if (!vmcs) {
4234 free_kvm_area();
4235 return -ENOMEM;
4236 }
4237
4238 per_cpu(vmxarea, cpu) = vmcs;
4239 }
4240 return 0;
4241}
4242
91b0aa2c 4243static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
d99e4152 4244 struct kvm_segment *save)
6aa8b732 4245{
d99e4152
GN
4246 if (!emulate_invalid_guest_state) {
4247 /*
4248 * CS and SS RPL should be equal during guest entry according
4249 * to VMX spec, but in reality it is not always so. Since vcpu
4250 * is in the middle of the transition from real mode to
4251 * protected mode it is safe to assume that RPL 0 is a good
4252 * default value.
4253 */
4254 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
b32a9918
NA
4255 save->selector &= ~SEGMENT_RPL_MASK;
4256 save->dpl = save->selector & SEGMENT_RPL_MASK;
d99e4152 4257 save->s = 1;
6aa8b732 4258 }
d99e4152 4259 vmx_set_segment(vcpu, save, seg);
6aa8b732
AK
4260}
4261
4262static void enter_pmode(struct kvm_vcpu *vcpu)
4263{
4264 unsigned long flags;
a89a8fb9 4265 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732 4266
d99e4152
GN
4267 /*
4268 * Update real mode segment cache. It may be not up-to-date if sement
4269 * register was written while vcpu was in a guest mode.
4270 */
4271 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4272 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4273 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4274 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4275 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4276 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4277
7ffd92c5 4278 vmx->rmode.vm86_active = 0;
6aa8b732 4279
2fb92db1
AK
4280 vmx_segment_cache_clear(vmx);
4281
f5f7b2fe 4282 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
6aa8b732
AK
4283
4284 flags = vmcs_readl(GUEST_RFLAGS);
78ac8b47
AK
4285 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4286 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
6aa8b732
AK
4287 vmcs_writel(GUEST_RFLAGS, flags);
4288
66aee91a
RR
4289 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4290 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
6aa8b732
AK
4291
4292 update_exception_bitmap(vcpu);
4293
91b0aa2c
GN
4294 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4295 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4296 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4297 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4298 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4299 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
6aa8b732
AK
4300}
4301
f5f7b2fe 4302static void fix_rmode_seg(int seg, struct kvm_segment *save)
6aa8b732 4303{
772e0318 4304 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
d99e4152
GN
4305 struct kvm_segment var = *save;
4306
4307 var.dpl = 0x3;
4308 if (seg == VCPU_SREG_CS)
4309 var.type = 0x3;
4310
4311 if (!emulate_invalid_guest_state) {
4312 var.selector = var.base >> 4;
4313 var.base = var.base & 0xffff0;
4314 var.limit = 0xffff;
4315 var.g = 0;
4316 var.db = 0;
4317 var.present = 1;
4318 var.s = 1;
4319 var.l = 0;
4320 var.unusable = 0;
4321 var.type = 0x3;
4322 var.avl = 0;
4323 if (save->base & 0xf)
4324 printk_once(KERN_WARNING "kvm: segment base is not "
4325 "paragraph aligned when entering "
4326 "protected mode (seg=%d)", seg);
4327 }
6aa8b732 4328
d99e4152 4329 vmcs_write16(sf->selector, var.selector);
96794e4e 4330 vmcs_writel(sf->base, var.base);
d99e4152
GN
4331 vmcs_write32(sf->limit, var.limit);
4332 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
6aa8b732
AK
4333}
4334
4335static void enter_rmode(struct kvm_vcpu *vcpu)
4336{
4337 unsigned long flags;
a89a8fb9 4338 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732 4339
f5f7b2fe
AK
4340 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4341 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4342 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4343 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4344 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
c6ad1153
GN
4345 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4346 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
f5f7b2fe 4347
7ffd92c5 4348 vmx->rmode.vm86_active = 1;
6aa8b732 4349
776e58ea
GN
4350 /*
4351 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4918c6ca 4352 * vcpu. Warn the user that an update is overdue.
776e58ea 4353 */
4918c6ca 4354 if (!vcpu->kvm->arch.tss_addr)
776e58ea
GN
4355 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4356 "called before entering vcpu\n");
776e58ea 4357
2fb92db1
AK
4358 vmx_segment_cache_clear(vmx);
4359
4918c6ca 4360 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
6aa8b732 4361 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
6aa8b732
AK
4362 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4363
4364 flags = vmcs_readl(GUEST_RFLAGS);
78ac8b47 4365 vmx->rmode.save_rflags = flags;
6aa8b732 4366
053de044 4367 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
6aa8b732
AK
4368
4369 vmcs_writel(GUEST_RFLAGS, flags);
66aee91a 4370 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
6aa8b732
AK
4371 update_exception_bitmap(vcpu);
4372
d99e4152
GN
4373 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4374 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4375 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4376 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4377 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4378 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
b246dd5d 4379
8668a3c4 4380 kvm_mmu_reset_context(vcpu);
6aa8b732
AK
4381}
4382
401d10de
AS
4383static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4384{
4385 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981
AK
4386 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4387
4388 if (!msr)
4389 return;
401d10de 4390
44ea2b17
AK
4391 /*
4392 * Force kernel_gs_base reloading before EFER changes, as control
4393 * of this msr depends on is_long_mode().
4394 */
4395 vmx_load_host_state(to_vmx(vcpu));
f6801dff 4396 vcpu->arch.efer = efer;
401d10de 4397 if (efer & EFER_LMA) {
2961e876 4398 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
401d10de
AS
4399 msr->data = efer;
4400 } else {
2961e876 4401 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
401d10de
AS
4402
4403 msr->data = efer & ~EFER_LME;
4404 }
4405 setup_msrs(vmx);
4406}
4407
05b3e0c2 4408#ifdef CONFIG_X86_64
6aa8b732
AK
4409
4410static void enter_lmode(struct kvm_vcpu *vcpu)
4411{
4412 u32 guest_tr_ar;
4413
2fb92db1
AK
4414 vmx_segment_cache_clear(to_vmx(vcpu));
4415
6aa8b732 4416 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
4d283ec9 4417 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
bd80158a
JK
4418 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4419 __func__);
6aa8b732 4420 vmcs_write32(GUEST_TR_AR_BYTES,
4d283ec9
AL
4421 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4422 | VMX_AR_TYPE_BUSY_64_TSS);
6aa8b732 4423 }
da38f438 4424 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
6aa8b732
AK
4425}
4426
4427static void exit_lmode(struct kvm_vcpu *vcpu)
4428{
2961e876 4429 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
da38f438 4430 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
6aa8b732
AK
4431}
4432
4433#endif
4434
dd5f5341 4435static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
2384d2b3 4436{
dd180b3e
XG
4437 if (enable_ept) {
4438 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4439 return;
995f00a6 4440 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
f0b98c02
JM
4441 } else {
4442 vpid_sync_context(vpid);
dd180b3e 4443 }
2384d2b3
SY
4444}
4445
dd5f5341
WL
4446static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4447{
4448 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4449}
4450
e8467fda
AK
4451static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4452{
4453 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4454
4455 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4456 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4457}
4458
aff48baa
AK
4459static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4460{
4461 if (enable_ept && is_paging(vcpu))
4462 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4463 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4464}
4465
25c4c276 4466static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
399badf3 4467{
fc78f519
AK
4468 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4469
4470 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4471 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
399badf3
AK
4472}
4473
1439442c
SY
4474static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4475{
d0d538b9
GN
4476 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4477
6de4f3ad
AK
4478 if (!test_bit(VCPU_EXREG_PDPTR,
4479 (unsigned long *)&vcpu->arch.regs_dirty))
4480 return;
4481
1439442c 4482 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
d0d538b9
GN
4483 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4484 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4485 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4486 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
1439442c
SY
4487 }
4488}
4489
8f5d549f
AK
4490static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4491{
d0d538b9
GN
4492 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4493
8f5d549f 4494 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
d0d538b9
GN
4495 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4496 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4497 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4498 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
8f5d549f 4499 }
6de4f3ad
AK
4500
4501 __set_bit(VCPU_EXREG_PDPTR,
4502 (unsigned long *)&vcpu->arch.regs_avail);
4503 __set_bit(VCPU_EXREG_PDPTR,
4504 (unsigned long *)&vcpu->arch.regs_dirty);
8f5d549f
AK
4505}
4506
3899152c
DM
4507static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4508{
4509 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4510 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4511 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4512
4513 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4514 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4515 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4516 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4517
4518 return fixed_bits_valid(val, fixed0, fixed1);
4519}
4520
4521static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4522{
4523 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4524 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4525
4526 return fixed_bits_valid(val, fixed0, fixed1);
4527}
4528
4529static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4530{
4531 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4532 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4533
4534 return fixed_bits_valid(val, fixed0, fixed1);
4535}
4536
4537/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4538#define nested_guest_cr4_valid nested_cr4_valid
4539#define nested_host_cr4_valid nested_cr4_valid
4540
5e1746d6 4541static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1439442c
SY
4542
4543static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4544 unsigned long cr0,
4545 struct kvm_vcpu *vcpu)
4546{
5233dd51
MT
4547 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4548 vmx_decache_cr3(vcpu);
1439442c
SY
4549 if (!(cr0 & X86_CR0_PG)) {
4550 /* From paging/starting to nonpaging */
4551 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
65267ea1 4552 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
1439442c
SY
4553 (CPU_BASED_CR3_LOAD_EXITING |
4554 CPU_BASED_CR3_STORE_EXITING));
4555 vcpu->arch.cr0 = cr0;
fc78f519 4556 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
1439442c
SY
4557 } else if (!is_paging(vcpu)) {
4558 /* From nonpaging to paging */
4559 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
65267ea1 4560 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
1439442c
SY
4561 ~(CPU_BASED_CR3_LOAD_EXITING |
4562 CPU_BASED_CR3_STORE_EXITING));
4563 vcpu->arch.cr0 = cr0;
fc78f519 4564 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
1439442c 4565 }
95eb84a7
SY
4566
4567 if (!(cr0 & X86_CR0_WP))
4568 *hw_cr0 &= ~X86_CR0_WP;
1439442c
SY
4569}
4570
6aa8b732
AK
4571static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4572{
7ffd92c5 4573 struct vcpu_vmx *vmx = to_vmx(vcpu);
3a624e29
NK
4574 unsigned long hw_cr0;
4575
5037878e 4576 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
3a624e29 4577 if (enable_unrestricted_guest)
5037878e 4578 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
218e763f 4579 else {
5037878e 4580 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
1439442c 4581
218e763f
GN
4582 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4583 enter_pmode(vcpu);
6aa8b732 4584
218e763f
GN
4585 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4586 enter_rmode(vcpu);
4587 }
6aa8b732 4588
05b3e0c2 4589#ifdef CONFIG_X86_64
f6801dff 4590 if (vcpu->arch.efer & EFER_LME) {
707d92fa 4591 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
6aa8b732 4592 enter_lmode(vcpu);
707d92fa 4593 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
6aa8b732
AK
4594 exit_lmode(vcpu);
4595 }
4596#endif
4597
089d034e 4598 if (enable_ept)
1439442c
SY
4599 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4600
6aa8b732 4601 vmcs_writel(CR0_READ_SHADOW, cr0);
1439442c 4602 vmcs_writel(GUEST_CR0, hw_cr0);
ad312c7c 4603 vcpu->arch.cr0 = cr0;
14168786
GN
4604
4605 /* depends on vcpu->arch.cr0 to be set to a new value */
4606 vmx->emulation_required = emulation_required(vcpu);
6aa8b732
AK
4607}
4608
855feb67
YZ
4609static int get_ept_level(struct kvm_vcpu *vcpu)
4610{
4611 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4612 return 5;
4613 return 4;
4614}
4615
995f00a6 4616static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
1439442c 4617{
855feb67
YZ
4618 u64 eptp = VMX_EPTP_MT_WB;
4619
4620 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
1439442c 4621
995f00a6
PF
4622 if (enable_ept_ad_bits &&
4623 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
bb97a016 4624 eptp |= VMX_EPTP_AD_ENABLE_BIT;
1439442c
SY
4625 eptp |= (root_hpa & PAGE_MASK);
4626
4627 return eptp;
4628}
4629
6aa8b732
AK
4630static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4631{
1439442c
SY
4632 unsigned long guest_cr3;
4633 u64 eptp;
4634
4635 guest_cr3 = cr3;
089d034e 4636 if (enable_ept) {
995f00a6 4637 eptp = construct_eptp(vcpu, cr3);
1439442c 4638 vmcs_write64(EPT_POINTER, eptp);
59ab5a8f
JK
4639 if (is_paging(vcpu) || is_guest_mode(vcpu))
4640 guest_cr3 = kvm_read_cr3(vcpu);
4641 else
4642 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
7c93be44 4643 ept_load_pdptrs(vcpu);
1439442c
SY
4644 }
4645
2384d2b3 4646 vmx_flush_tlb(vcpu);
1439442c 4647 vmcs_writel(GUEST_CR3, guest_cr3);
6aa8b732
AK
4648}
4649
5e1746d6 4650static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
6aa8b732 4651{
085e68ee
BS
4652 /*
4653 * Pass through host's Machine Check Enable value to hw_cr4, which
4654 * is in force while we are in guest mode. Do not let guests control
4655 * this bit, even if host CR4.MCE == 0.
4656 */
4657 unsigned long hw_cr4 =
4658 (cr4_read_shadow() & X86_CR4_MCE) |
4659 (cr4 & ~X86_CR4_MCE) |
4660 (to_vmx(vcpu)->rmode.vm86_active ?
4661 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1439442c 4662
5e1746d6
NHE
4663 if (cr4 & X86_CR4_VMXE) {
4664 /*
4665 * To use VMXON (and later other VMX instructions), a guest
4666 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4667 * So basically the check on whether to allow nested VMX
4668 * is here.
4669 */
4670 if (!nested_vmx_allowed(vcpu))
4671 return 1;
1a0d74e6 4672 }
3899152c
DM
4673
4674 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
5e1746d6
NHE
4675 return 1;
4676
ad312c7c 4677 vcpu->arch.cr4 = cr4;
bc23008b
AK
4678 if (enable_ept) {
4679 if (!is_paging(vcpu)) {
4680 hw_cr4 &= ~X86_CR4_PAE;
4681 hw_cr4 |= X86_CR4_PSE;
4682 } else if (!(cr4 & X86_CR4_PAE)) {
4683 hw_cr4 &= ~X86_CR4_PAE;
4684 }
4685 }
1439442c 4686
656ec4a4
RK
4687 if (!enable_unrestricted_guest && !is_paging(vcpu))
4688 /*
ddba2628
HH
4689 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4690 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4691 * to be manually disabled when guest switches to non-paging
4692 * mode.
4693 *
4694 * If !enable_unrestricted_guest, the CPU is always running
4695 * with CR0.PG=1 and CR4 needs to be modified.
4696 * If enable_unrestricted_guest, the CPU automatically
4697 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
656ec4a4 4698 */
ddba2628 4699 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
656ec4a4 4700
1439442c
SY
4701 vmcs_writel(CR4_READ_SHADOW, cr4);
4702 vmcs_writel(GUEST_CR4, hw_cr4);
5e1746d6 4703 return 0;
6aa8b732
AK
4704}
4705
6aa8b732
AK
4706static void vmx_get_segment(struct kvm_vcpu *vcpu,
4707 struct kvm_segment *var, int seg)
4708{
a9179499 4709 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732
AK
4710 u32 ar;
4711
c6ad1153 4712 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
f5f7b2fe 4713 *var = vmx->rmode.segs[seg];
a9179499 4714 if (seg == VCPU_SREG_TR
2fb92db1 4715 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
f5f7b2fe 4716 return;
1390a28b
AK
4717 var->base = vmx_read_guest_seg_base(vmx, seg);
4718 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4719 return;
a9179499 4720 }
2fb92db1
AK
4721 var->base = vmx_read_guest_seg_base(vmx, seg);
4722 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4723 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4724 ar = vmx_read_guest_seg_ar(vmx, seg);
03617c18 4725 var->unusable = (ar >> 16) & 1;
6aa8b732
AK
4726 var->type = ar & 15;
4727 var->s = (ar >> 4) & 1;
4728 var->dpl = (ar >> 5) & 3;
03617c18
GN
4729 /*
4730 * Some userspaces do not preserve unusable property. Since usable
4731 * segment has to be present according to VMX spec we can use present
4732 * property to amend userspace bug by making unusable segment always
4733 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4734 * segment as unusable.
4735 */
4736 var->present = !var->unusable;
6aa8b732
AK
4737 var->avl = (ar >> 12) & 1;
4738 var->l = (ar >> 13) & 1;
4739 var->db = (ar >> 14) & 1;
4740 var->g = (ar >> 15) & 1;
6aa8b732
AK
4741}
4742
a9179499
AK
4743static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4744{
a9179499
AK
4745 struct kvm_segment s;
4746
4747 if (to_vmx(vcpu)->rmode.vm86_active) {
4748 vmx_get_segment(vcpu, &s, seg);
4749 return s.base;
4750 }
2fb92db1 4751 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
a9179499
AK
4752}
4753
b09408d0 4754static int vmx_get_cpl(struct kvm_vcpu *vcpu)
2e4d2653 4755{
b09408d0
MT
4756 struct vcpu_vmx *vmx = to_vmx(vcpu);
4757
ae9fedc7 4758 if (unlikely(vmx->rmode.vm86_active))
2e4d2653 4759 return 0;
ae9fedc7
PB
4760 else {
4761 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4d283ec9 4762 return VMX_AR_DPL(ar);
69c73028 4763 }
69c73028
AK
4764}
4765
653e3108 4766static u32 vmx_segment_access_rights(struct kvm_segment *var)
6aa8b732 4767{
6aa8b732
AK
4768 u32 ar;
4769
f0495f9b 4770 if (var->unusable || !var->present)
6aa8b732
AK
4771 ar = 1 << 16;
4772 else {
4773 ar = var->type & 15;
4774 ar |= (var->s & 1) << 4;
4775 ar |= (var->dpl & 3) << 5;
4776 ar |= (var->present & 1) << 7;
4777 ar |= (var->avl & 1) << 12;
4778 ar |= (var->l & 1) << 13;
4779 ar |= (var->db & 1) << 14;
4780 ar |= (var->g & 1) << 15;
4781 }
653e3108
AK
4782
4783 return ar;
4784}
4785
4786static void vmx_set_segment(struct kvm_vcpu *vcpu,
4787 struct kvm_segment *var, int seg)
4788{
7ffd92c5 4789 struct vcpu_vmx *vmx = to_vmx(vcpu);
772e0318 4790 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
653e3108 4791
2fb92db1
AK
4792 vmx_segment_cache_clear(vmx);
4793
1ecd50a9
GN
4794 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4795 vmx->rmode.segs[seg] = *var;
4796 if (seg == VCPU_SREG_TR)
4797 vmcs_write16(sf->selector, var->selector);
4798 else if (var->s)
4799 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
d99e4152 4800 goto out;
653e3108 4801 }
1ecd50a9 4802
653e3108
AK
4803 vmcs_writel(sf->base, var->base);
4804 vmcs_write32(sf->limit, var->limit);
4805 vmcs_write16(sf->selector, var->selector);
3a624e29
NK
4806
4807 /*
4808 * Fix the "Accessed" bit in AR field of segment registers for older
4809 * qemu binaries.
4810 * IA32 arch specifies that at the time of processor reset the
4811 * "Accessed" bit in the AR field of segment registers is 1. And qemu
0fa06071 4812 * is setting it to 0 in the userland code. This causes invalid guest
3a624e29
NK
4813 * state vmexit when "unrestricted guest" mode is turned on.
4814 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4815 * tree. Newer qemu binaries with that qemu fix would not need this
4816 * kvm hack.
4817 */
4818 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
f924d66d 4819 var->type |= 0x1; /* Accessed */
3a624e29 4820
f924d66d 4821 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
d99e4152
GN
4822
4823out:
98eb2f8b 4824 vmx->emulation_required = emulation_required(vcpu);
6aa8b732
AK
4825}
4826
6aa8b732
AK
4827static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4828{
2fb92db1 4829 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
6aa8b732
AK
4830
4831 *db = (ar >> 14) & 1;
4832 *l = (ar >> 13) & 1;
4833}
4834
89a27f4d 4835static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 4836{
89a27f4d
GN
4837 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4838 dt->address = vmcs_readl(GUEST_IDTR_BASE);
6aa8b732
AK
4839}
4840
89a27f4d 4841static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 4842{
89a27f4d
GN
4843 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4844 vmcs_writel(GUEST_IDTR_BASE, dt->address);
6aa8b732
AK
4845}
4846
89a27f4d 4847static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 4848{
89a27f4d
GN
4849 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4850 dt->address = vmcs_readl(GUEST_GDTR_BASE);
6aa8b732
AK
4851}
4852
89a27f4d 4853static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 4854{
89a27f4d
GN
4855 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4856 vmcs_writel(GUEST_GDTR_BASE, dt->address);
6aa8b732
AK
4857}
4858
648dfaa7
MG
4859static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4860{
4861 struct kvm_segment var;
4862 u32 ar;
4863
4864 vmx_get_segment(vcpu, &var, seg);
07f42f5f 4865 var.dpl = 0x3;
0647f4aa
GN
4866 if (seg == VCPU_SREG_CS)
4867 var.type = 0x3;
648dfaa7
MG
4868 ar = vmx_segment_access_rights(&var);
4869
4870 if (var.base != (var.selector << 4))
4871 return false;
89efbed0 4872 if (var.limit != 0xffff)
648dfaa7 4873 return false;
07f42f5f 4874 if (ar != 0xf3)
648dfaa7
MG
4875 return false;
4876
4877 return true;
4878}
4879
4880static bool code_segment_valid(struct kvm_vcpu *vcpu)
4881{
4882 struct kvm_segment cs;
4883 unsigned int cs_rpl;
4884
4885 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
b32a9918 4886 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
648dfaa7 4887
1872a3f4
AK
4888 if (cs.unusable)
4889 return false;
4d283ec9 4890 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
648dfaa7
MG
4891 return false;
4892 if (!cs.s)
4893 return false;
4d283ec9 4894 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
648dfaa7
MG
4895 if (cs.dpl > cs_rpl)
4896 return false;
1872a3f4 4897 } else {
648dfaa7
MG
4898 if (cs.dpl != cs_rpl)
4899 return false;
4900 }
4901 if (!cs.present)
4902 return false;
4903
4904 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4905 return true;
4906}
4907
4908static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4909{
4910 struct kvm_segment ss;
4911 unsigned int ss_rpl;
4912
4913 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
b32a9918 4914 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
648dfaa7 4915
1872a3f4
AK
4916 if (ss.unusable)
4917 return true;
4918 if (ss.type != 3 && ss.type != 7)
648dfaa7
MG
4919 return false;
4920 if (!ss.s)
4921 return false;
4922 if (ss.dpl != ss_rpl) /* DPL != RPL */
4923 return false;
4924 if (!ss.present)
4925 return false;
4926
4927 return true;
4928}
4929
4930static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4931{
4932 struct kvm_segment var;
4933 unsigned int rpl;
4934
4935 vmx_get_segment(vcpu, &var, seg);
b32a9918 4936 rpl = var.selector & SEGMENT_RPL_MASK;
648dfaa7 4937
1872a3f4
AK
4938 if (var.unusable)
4939 return true;
648dfaa7
MG
4940 if (!var.s)
4941 return false;
4942 if (!var.present)
4943 return false;
4d283ec9 4944 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
648dfaa7
MG
4945 if (var.dpl < rpl) /* DPL < RPL */
4946 return false;
4947 }
4948
4949 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4950 * rights flags
4951 */
4952 return true;
4953}
4954
4955static bool tr_valid(struct kvm_vcpu *vcpu)
4956{
4957 struct kvm_segment tr;
4958
4959 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4960
1872a3f4
AK
4961 if (tr.unusable)
4962 return false;
b32a9918 4963 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
648dfaa7 4964 return false;
1872a3f4 4965 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
648dfaa7
MG
4966 return false;
4967 if (!tr.present)
4968 return false;
4969
4970 return true;
4971}
4972
4973static bool ldtr_valid(struct kvm_vcpu *vcpu)
4974{
4975 struct kvm_segment ldtr;
4976
4977 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4978
1872a3f4
AK
4979 if (ldtr.unusable)
4980 return true;
b32a9918 4981 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
648dfaa7
MG
4982 return false;
4983 if (ldtr.type != 2)
4984 return false;
4985 if (!ldtr.present)
4986 return false;
4987
4988 return true;
4989}
4990
4991static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4992{
4993 struct kvm_segment cs, ss;
4994
4995 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4996 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4997
b32a9918
NA
4998 return ((cs.selector & SEGMENT_RPL_MASK) ==
4999 (ss.selector & SEGMENT_RPL_MASK));
648dfaa7
MG
5000}
5001
5002/*
5003 * Check if guest state is valid. Returns true if valid, false if
5004 * not.
5005 * We assume that registers are always usable
5006 */
5007static bool guest_state_valid(struct kvm_vcpu *vcpu)
5008{
c5e97c80
GN
5009 if (enable_unrestricted_guest)
5010 return true;
5011
648dfaa7 5012 /* real mode guest state checks */
f13882d8 5013 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
648dfaa7
MG
5014 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5015 return false;
5016 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5017 return false;
5018 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5019 return false;
5020 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5021 return false;
5022 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5023 return false;
5024 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5025 return false;
5026 } else {
5027 /* protected mode guest state checks */
5028 if (!cs_ss_rpl_check(vcpu))
5029 return false;
5030 if (!code_segment_valid(vcpu))
5031 return false;
5032 if (!stack_segment_valid(vcpu))
5033 return false;
5034 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5035 return false;
5036 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5037 return false;
5038 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5039 return false;
5040 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5041 return false;
5042 if (!tr_valid(vcpu))
5043 return false;
5044 if (!ldtr_valid(vcpu))
5045 return false;
5046 }
5047 /* TODO:
5048 * - Add checks on RIP
5049 * - Add checks on RFLAGS
5050 */
5051
5052 return true;
5053}
5054
5fa99cbe
JM
5055static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5056{
5057 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5058}
5059
d77c26fc 5060static int init_rmode_tss(struct kvm *kvm)
6aa8b732 5061{
40dcaa9f 5062 gfn_t fn;
195aefde 5063 u16 data = 0;
1f755a82 5064 int idx, r;
6aa8b732 5065
40dcaa9f 5066 idx = srcu_read_lock(&kvm->srcu);
4918c6ca 5067 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
195aefde
IE
5068 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5069 if (r < 0)
10589a46 5070 goto out;
195aefde 5071 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
464d17c8
SY
5072 r = kvm_write_guest_page(kvm, fn++, &data,
5073 TSS_IOPB_BASE_OFFSET, sizeof(u16));
195aefde 5074 if (r < 0)
10589a46 5075 goto out;
195aefde
IE
5076 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5077 if (r < 0)
10589a46 5078 goto out;
195aefde
IE
5079 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5080 if (r < 0)
10589a46 5081 goto out;
195aefde 5082 data = ~0;
10589a46
MT
5083 r = kvm_write_guest_page(kvm, fn, &data,
5084 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5085 sizeof(u8));
10589a46 5086out:
40dcaa9f 5087 srcu_read_unlock(&kvm->srcu, idx);
1f755a82 5088 return r;
6aa8b732
AK
5089}
5090
b7ebfb05
SY
5091static int init_rmode_identity_map(struct kvm *kvm)
5092{
f51770ed 5093 int i, idx, r = 0;
ba049e93 5094 kvm_pfn_t identity_map_pfn;
b7ebfb05
SY
5095 u32 tmp;
5096
a255d479
TC
5097 /* Protect kvm->arch.ept_identity_pagetable_done. */
5098 mutex_lock(&kvm->slots_lock);
5099
f51770ed 5100 if (likely(kvm->arch.ept_identity_pagetable_done))
a255d479 5101 goto out2;
a255d479 5102
d8a6e365
DH
5103 if (!kvm->arch.ept_identity_map_addr)
5104 kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
b927a3ce 5105 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
a255d479 5106
d8a6e365
DH
5107 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
5108 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
f51770ed 5109 if (r < 0)
a255d479
TC
5110 goto out2;
5111
40dcaa9f 5112 idx = srcu_read_lock(&kvm->srcu);
b7ebfb05
SY
5113 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5114 if (r < 0)
5115 goto out;
5116 /* Set up identity-mapping pagetable for EPT in real mode */
5117 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5118 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5119 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5120 r = kvm_write_guest_page(kvm, identity_map_pfn,
5121 &tmp, i * sizeof(tmp), sizeof(tmp));
5122 if (r < 0)
5123 goto out;
5124 }
5125 kvm->arch.ept_identity_pagetable_done = true;
f51770ed 5126
b7ebfb05 5127out:
40dcaa9f 5128 srcu_read_unlock(&kvm->srcu, idx);
a255d479
TC
5129
5130out2:
5131 mutex_unlock(&kvm->slots_lock);
f51770ed 5132 return r;
b7ebfb05
SY
5133}
5134
6aa8b732
AK
5135static void seg_setup(int seg)
5136{
772e0318 5137 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3a624e29 5138 unsigned int ar;
6aa8b732
AK
5139
5140 vmcs_write16(sf->selector, 0);
5141 vmcs_writel(sf->base, 0);
5142 vmcs_write32(sf->limit, 0xffff);
d54d07b2
GN
5143 ar = 0x93;
5144 if (seg == VCPU_SREG_CS)
5145 ar |= 0x08; /* code segment */
3a624e29
NK
5146
5147 vmcs_write32(sf->ar_bytes, ar);
6aa8b732
AK
5148}
5149
f78e0e2e
SY
5150static int alloc_apic_access_page(struct kvm *kvm)
5151{
4484141a 5152 struct page *page;
f78e0e2e
SY
5153 int r = 0;
5154
79fac95e 5155 mutex_lock(&kvm->slots_lock);
c24ae0dc 5156 if (kvm->arch.apic_access_page_done)
f78e0e2e 5157 goto out;
1d8007bd
PB
5158 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5159 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
f78e0e2e
SY
5160 if (r)
5161 goto out;
72dc67a6 5162
73a6d941 5163 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
4484141a
XG
5164 if (is_error_page(page)) {
5165 r = -EFAULT;
5166 goto out;
5167 }
5168
c24ae0dc
TC
5169 /*
5170 * Do not pin the page in memory, so that memory hot-unplug
5171 * is able to migrate it.
5172 */
5173 put_page(page);
5174 kvm->arch.apic_access_page_done = true;
f78e0e2e 5175out:
79fac95e 5176 mutex_unlock(&kvm->slots_lock);
f78e0e2e
SY
5177 return r;
5178}
5179
991e7a0e 5180static int allocate_vpid(void)
2384d2b3
SY
5181{
5182 int vpid;
5183
919818ab 5184 if (!enable_vpid)
991e7a0e 5185 return 0;
2384d2b3
SY
5186 spin_lock(&vmx_vpid_lock);
5187 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
991e7a0e 5188 if (vpid < VMX_NR_VPIDS)
2384d2b3 5189 __set_bit(vpid, vmx_vpid_bitmap);
991e7a0e
WL
5190 else
5191 vpid = 0;
2384d2b3 5192 spin_unlock(&vmx_vpid_lock);
991e7a0e 5193 return vpid;
2384d2b3
SY
5194}
5195
991e7a0e 5196static void free_vpid(int vpid)
cdbecfc3 5197{
991e7a0e 5198 if (!enable_vpid || vpid == 0)
cdbecfc3
LJ
5199 return;
5200 spin_lock(&vmx_vpid_lock);
991e7a0e 5201 __clear_bit(vpid, vmx_vpid_bitmap);
cdbecfc3
LJ
5202 spin_unlock(&vmx_vpid_lock);
5203}
5204
4b0be90f
PB
5205static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5206 u32 msr, int type)
25c5f225 5207{
3e7c73e9 5208 int f = sizeof(unsigned long);
25c5f225
SY
5209
5210 if (!cpu_has_vmx_msr_bitmap())
5211 return;
5212
5213 /*
5214 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5215 * have the write-low and read-high bitmap offsets the wrong way round.
5216 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5217 */
25c5f225 5218 if (msr <= 0x1fff) {
8d14695f
YZ
5219 if (type & MSR_TYPE_R)
5220 /* read-low */
5221 __clear_bit(msr, msr_bitmap + 0x000 / f);
5222
5223 if (type & MSR_TYPE_W)
5224 /* write-low */
5225 __clear_bit(msr, msr_bitmap + 0x800 / f);
5226
25c5f225
SY
5227 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5228 msr &= 0x1fff;
8d14695f
YZ
5229 if (type & MSR_TYPE_R)
5230 /* read-high */
5231 __clear_bit(msr, msr_bitmap + 0x400 / f);
5232
5233 if (type & MSR_TYPE_W)
5234 /* write-high */
5235 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5236
5237 }
5238}
5239
4b0be90f
PB
5240static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5241 u32 msr, int type)
5242{
5243 int f = sizeof(unsigned long);
5244
5245 if (!cpu_has_vmx_msr_bitmap())
5246 return;
5247
5248 /*
5249 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5250 * have the write-low and read-high bitmap offsets the wrong way round.
5251 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5252 */
5253 if (msr <= 0x1fff) {
5254 if (type & MSR_TYPE_R)
5255 /* read-low */
5256 __set_bit(msr, msr_bitmap + 0x000 / f);
5257
5258 if (type & MSR_TYPE_W)
5259 /* write-low */
5260 __set_bit(msr, msr_bitmap + 0x800 / f);
5261
5262 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5263 msr &= 0x1fff;
5264 if (type & MSR_TYPE_R)
5265 /* read-high */
5266 __set_bit(msr, msr_bitmap + 0x400 / f);
5267
5268 if (type & MSR_TYPE_W)
5269 /* write-high */
5270 __set_bit(msr, msr_bitmap + 0xc00 / f);
5271
5272 }
5273}
5274
5275static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5276 u32 msr, int type, bool value)
5277{
5278 if (value)
5279 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5280 else
5281 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5282}
5283
f2b93280
WV
5284/*
5285 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5286 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5287 */
5288static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5289 unsigned long *msr_bitmap_nested,
5290 u32 msr, int type)
5291{
5292 int f = sizeof(unsigned long);
5293
5294 if (!cpu_has_vmx_msr_bitmap()) {
5295 WARN_ON(1);
5296 return;
5297 }
5298
5299 /*
5300 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5301 * have the write-low and read-high bitmap offsets the wrong way round.
5302 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5303 */
5304 if (msr <= 0x1fff) {
5305 if (type & MSR_TYPE_R &&
5306 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5307 /* read-low */
5308 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5309
5310 if (type & MSR_TYPE_W &&
5311 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5312 /* write-low */
5313 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5314
5315 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5316 msr &= 0x1fff;
5317 if (type & MSR_TYPE_R &&
5318 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5319 /* read-high */
5320 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5321
5322 if (type & MSR_TYPE_W &&
5323 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5324 /* write-high */
5325 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5326
5327 }
5328}
5329
4b0be90f 5330static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
5897297b 5331{
4b0be90f
PB
5332 u8 mode = 0;
5333
5334 if (cpu_has_secondary_exec_ctrls() &&
5335 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5336 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5337 mode |= MSR_BITMAP_MODE_X2APIC;
5338 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5339 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5340 }
5341
5342 if (is_long_mode(vcpu))
5343 mode |= MSR_BITMAP_MODE_LM;
5344
5345 return mode;
8d14695f
YZ
5346}
5347
4b0be90f
PB
5348#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5349
5350static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5351 u8 mode)
8d14695f 5352{
4b0be90f
PB
5353 int msr;
5354
5355 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5356 unsigned word = msr / BITS_PER_LONG;
5357 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5358 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
5359 }
5360
5361 if (mode & MSR_BITMAP_MODE_X2APIC) {
5362 /*
5363 * TPR reads and writes can be virtualized even if virtual interrupt
5364 * delivery is not in use.
5365 */
5366 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5367 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5368 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5369 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5370 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5371 }
f6e90f9e 5372 }
5897297b
AK
5373}
5374
4b0be90f
PB
5375static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5376{
5377 struct vcpu_vmx *vmx = to_vmx(vcpu);
5378 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5379 u8 mode = vmx_msr_bitmap_mode(vcpu);
5380 u8 changed = mode ^ vmx->msr_bitmap_mode;
5381
5382 if (!changed)
5383 return;
5384
5385 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5386 !(mode & MSR_BITMAP_MODE_LM));
5387
5388 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5389 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5390
5391 vmx->msr_bitmap_mode = mode;
5392}
5393
b2a05fef 5394static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
d50ab6c1 5395{
d62caabb 5396 return enable_apicv;
d50ab6c1
PB
5397}
5398
c9f04407
DM
5399static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5400{
5401 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5402 gfn_t gfn;
5403
5404 /*
5405 * Don't need to mark the APIC access page dirty; it is never
5406 * written to by the CPU during APIC virtualization.
5407 */
5408
5409 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5410 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5411 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5412 }
5413
5414 if (nested_cpu_has_posted_intr(vmcs12)) {
5415 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5416 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5417 }
5418}
5419
5420
6342c50a 5421static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
705699a1
WV
5422{
5423 struct vcpu_vmx *vmx = to_vmx(vcpu);
5424 int max_irr;
5425 void *vapic_page;
5426 u16 status;
5427
c9f04407
DM
5428 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5429 return;
705699a1 5430
c9f04407
DM
5431 vmx->nested.pi_pending = false;
5432 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5433 return;
705699a1 5434
c9f04407
DM
5435 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5436 if (max_irr != 256) {
705699a1 5437 vapic_page = kmap(vmx->nested.virtual_apic_page);
705699a1
WV
5438 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5439 kunmap(vmx->nested.virtual_apic_page);
5440
5441 status = vmcs_read16(GUEST_INTR_STATUS);
5442 if ((u8)max_irr > ((u8)status & 0xff)) {
5443 status &= ~0xff;
5444 status |= (u8)max_irr;
5445 vmcs_write16(GUEST_INTR_STATUS, status);
5446 }
5447 }
c9f04407
DM
5448
5449 nested_mark_vmcs12_pages_dirty(vcpu);
705699a1
WV
5450}
5451
06a5524f
WV
5452static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5453 bool nested)
21bc8dc5
RK
5454{
5455#ifdef CONFIG_SMP
06a5524f
WV
5456 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5457
21bc8dc5 5458 if (vcpu->mode == IN_GUEST_MODE) {
28b835d6 5459 /*
5753743f
HZ
5460 * The vector of interrupt to be delivered to vcpu had
5461 * been set in PIR before this function.
5462 *
5463 * Following cases will be reached in this block, and
5464 * we always send a notification event in all cases as
5465 * explained below.
5466 *
5467 * Case 1: vcpu keeps in non-root mode. Sending a
5468 * notification event posts the interrupt to vcpu.
5469 *
5470 * Case 2: vcpu exits to root mode and is still
5471 * runnable. PIR will be synced to vIRR before the
5472 * next vcpu entry. Sending a notification event in
5473 * this case has no effect, as vcpu is not in root
5474 * mode.
28b835d6 5475 *
5753743f
HZ
5476 * Case 3: vcpu exits to root mode and is blocked.
5477 * vcpu_block() has already synced PIR to vIRR and
5478 * never blocks vcpu if vIRR is not cleared. Therefore,
5479 * a blocked vcpu here does not wait for any requested
5480 * interrupts in PIR, and sending a notification event
5481 * which has no effect is safe here.
28b835d6 5482 */
28b835d6 5483
06a5524f 5484 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
21bc8dc5
RK
5485 return true;
5486 }
5487#endif
5488 return false;
5489}
5490
705699a1
WV
5491static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5492 int vector)
5493{
5494 struct vcpu_vmx *vmx = to_vmx(vcpu);
5495
5496 if (is_guest_mode(vcpu) &&
5497 vector == vmx->nested.posted_intr_nv) {
705699a1
WV
5498 /*
5499 * If a posted intr is not recognized by hardware,
5500 * we will accomplish it in the next vmentry.
5501 */
5502 vmx->nested.pi_pending = true;
5503 kvm_make_request(KVM_REQ_EVENT, vcpu);
7b616667
LA
5504 /* the PIR and ON have been set by L1. */
5505 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5506 kvm_vcpu_kick(vcpu);
705699a1
WV
5507 return 0;
5508 }
5509 return -1;
5510}
a20ed54d
YZ
5511/*
5512 * Send interrupt to vcpu via posted interrupt way.
5513 * 1. If target vcpu is running(non-root mode), send posted interrupt
5514 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5515 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5516 * interrupt from PIR in next vmentry.
5517 */
5518static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5519{
5520 struct vcpu_vmx *vmx = to_vmx(vcpu);
5521 int r;
5522
705699a1
WV
5523 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5524 if (!r)
5525 return;
5526
a20ed54d
YZ
5527 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5528 return;
5529
b95234c8
PB
5530 /* If a previous notification has sent the IPI, nothing to do. */
5531 if (pi_test_and_set_on(&vmx->pi_desc))
5532 return;
5533
06a5524f 5534 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
a20ed54d
YZ
5535 kvm_vcpu_kick(vcpu);
5536}
5537
a3a8ff8e
NHE
5538/*
5539 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5540 * will not change in the lifetime of the guest.
5541 * Note that host-state that does change is set elsewhere. E.g., host-state
5542 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5543 */
a547c6db 5544static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
a3a8ff8e
NHE
5545{
5546 u32 low32, high32;
5547 unsigned long tmpl;
5548 struct desc_ptr dt;
d6e41f11 5549 unsigned long cr0, cr3, cr4;
a3a8ff8e 5550
04ac88ab
AL
5551 cr0 = read_cr0();
5552 WARN_ON(cr0 & X86_CR0_TS);
5553 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
d6e41f11
AL
5554
5555 /*
5556 * Save the most likely value for this task's CR3 in the VMCS.
5557 * We can't use __get_current_cr3_fast() because we're not atomic.
5558 */
6c690ee1 5559 cr3 = __read_cr3();
d6e41f11 5560 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
44889942 5561 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
a3a8ff8e 5562
d974baa3 5563 /* Save the most likely value for this task's CR4 in the VMCS. */
1e02ce4c 5564 cr4 = cr4_read_shadow();
d974baa3 5565 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
44889942 5566 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
d974baa3 5567
a3a8ff8e 5568 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
b2da15ac
AK
5569#ifdef CONFIG_X86_64
5570 /*
5571 * Load null selectors, so we can avoid reloading them in
5572 * __vmx_load_host_state(), in case userspace uses the null selectors
5573 * too (the expected case).
5574 */
5575 vmcs_write16(HOST_DS_SELECTOR, 0);
5576 vmcs_write16(HOST_ES_SELECTOR, 0);
5577#else
a3a8ff8e
NHE
5578 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5579 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
b2da15ac 5580#endif
a3a8ff8e
NHE
5581 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5582 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5583
87930019 5584 store_idt(&dt);
a3a8ff8e 5585 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
a547c6db 5586 vmx->host_idt_base = dt.address;
a3a8ff8e 5587
83287ea4 5588 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
a3a8ff8e
NHE
5589
5590 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5591 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5592 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5593 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5594
5595 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5596 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5597 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5598 }
5599}
5600
bf8179a0
NHE
5601static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5602{
5603 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5604 if (enable_ept)
5605 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
fe3ef05c
NHE
5606 if (is_guest_mode(&vmx->vcpu))
5607 vmx->vcpu.arch.cr4_guest_owned_bits &=
5608 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
bf8179a0
NHE
5609 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5610}
5611
01e439be
YZ
5612static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5613{
5614 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5615
d62caabb 5616 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
01e439be 5617 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
d02fcf50
PB
5618
5619 if (!enable_vnmi)
5620 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5621
64672c95
YJ
5622 /* Enable the preemption timer dynamically */
5623 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
01e439be
YZ
5624 return pin_based_exec_ctrl;
5625}
5626
d62caabb
AS
5627static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5628{
5629 struct vcpu_vmx *vmx = to_vmx(vcpu);
5630
5631 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
3ce424e4
RK
5632 if (cpu_has_secondary_exec_ctrls()) {
5633 if (kvm_vcpu_apicv_active(vcpu))
5634 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5635 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5636 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5637 else
5638 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5639 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5640 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5641 }
5642
5643 if (cpu_has_vmx_msr_bitmap())
4b0be90f 5644 vmx_update_msr_bitmap(vcpu);
d62caabb
AS
5645}
5646
bf8179a0
NHE
5647static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5648{
5649 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
d16c293e
PB
5650
5651 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5652 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5653
35754c98 5654 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
bf8179a0
NHE
5655 exec_control &= ~CPU_BASED_TPR_SHADOW;
5656#ifdef CONFIG_X86_64
5657 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5658 CPU_BASED_CR8_LOAD_EXITING;
5659#endif
5660 }
5661 if (!enable_ept)
5662 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5663 CPU_BASED_CR3_LOAD_EXITING |
5664 CPU_BASED_INVLPG_EXITING;
5665 return exec_control;
5666}
5667
45ec368c 5668static bool vmx_rdrand_supported(void)
bf8179a0 5669{
45ec368c 5670 return vmcs_config.cpu_based_2nd_exec_ctrl &
736fdf72 5671 SECONDARY_EXEC_RDRAND_EXITING;
45ec368c
JM
5672}
5673
75f4fc8d
JM
5674static bool vmx_rdseed_supported(void)
5675{
5676 return vmcs_config.cpu_based_2nd_exec_ctrl &
736fdf72 5677 SECONDARY_EXEC_RDSEED_EXITING;
75f4fc8d
JM
5678}
5679
80154d77 5680static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
bf8179a0 5681{
80154d77
PB
5682 struct kvm_vcpu *vcpu = &vmx->vcpu;
5683
bf8179a0 5684 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
80154d77 5685 if (!cpu_need_virtualize_apic_accesses(vcpu))
bf8179a0
NHE
5686 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5687 if (vmx->vpid == 0)
5688 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5689 if (!enable_ept) {
5690 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5691 enable_unrestricted_guest = 0;
ad756a16
MJ
5692 /* Enable INVPCID for non-ept guests may cause performance regression. */
5693 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
bf8179a0
NHE
5694 }
5695 if (!enable_unrestricted_guest)
5696 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5697 if (!ple_gap)
5698 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
80154d77 5699 if (!kvm_vcpu_apicv_active(vcpu))
c7c9c56c
YZ
5700 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5701 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
8d14695f 5702 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
abc4fc58
AG
5703 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5704 (handle_vmptrld).
5705 We can NOT enable shadow_vmcs here because we don't have yet
5706 a current VMCS12
5707 */
5708 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
a3eaa864
KH
5709
5710 if (!enable_pml)
5711 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
843e4330 5712
3db13480
PB
5713 if (vmx_xsaves_supported()) {
5714 /* Exposing XSAVES only when XSAVE is exposed */
5715 bool xsaves_enabled =
5716 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5717 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5718
5719 if (!xsaves_enabled)
5720 exec_control &= ~SECONDARY_EXEC_XSAVES;
5721
5722 if (nested) {
5723 if (xsaves_enabled)
5724 vmx->nested.nested_vmx_secondary_ctls_high |=
5725 SECONDARY_EXEC_XSAVES;
5726 else
5727 vmx->nested.nested_vmx_secondary_ctls_high &=
5728 ~SECONDARY_EXEC_XSAVES;
5729 }
5730 }
5731
80154d77
PB
5732 if (vmx_rdtscp_supported()) {
5733 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5734 if (!rdtscp_enabled)
5735 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5736
5737 if (nested) {
5738 if (rdtscp_enabled)
5739 vmx->nested.nested_vmx_secondary_ctls_high |=
5740 SECONDARY_EXEC_RDTSCP;
5741 else
5742 vmx->nested.nested_vmx_secondary_ctls_high &=
5743 ~SECONDARY_EXEC_RDTSCP;
5744 }
5745 }
5746
5747 if (vmx_invpcid_supported()) {
5748 /* Exposing INVPCID only when PCID is exposed */
5749 bool invpcid_enabled =
5750 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5751 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5752
5753 if (!invpcid_enabled) {
5754 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5755 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5756 }
5757
5758 if (nested) {
5759 if (invpcid_enabled)
5760 vmx->nested.nested_vmx_secondary_ctls_high |=
5761 SECONDARY_EXEC_ENABLE_INVPCID;
5762 else
5763 vmx->nested.nested_vmx_secondary_ctls_high &=
5764 ~SECONDARY_EXEC_ENABLE_INVPCID;
5765 }
5766 }
5767
45ec368c
JM
5768 if (vmx_rdrand_supported()) {
5769 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5770 if (rdrand_enabled)
736fdf72 5771 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
45ec368c
JM
5772
5773 if (nested) {
5774 if (rdrand_enabled)
5775 vmx->nested.nested_vmx_secondary_ctls_high |=
736fdf72 5776 SECONDARY_EXEC_RDRAND_EXITING;
45ec368c
JM
5777 else
5778 vmx->nested.nested_vmx_secondary_ctls_high &=
736fdf72 5779 ~SECONDARY_EXEC_RDRAND_EXITING;
45ec368c
JM
5780 }
5781 }
5782
75f4fc8d
JM
5783 if (vmx_rdseed_supported()) {
5784 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5785 if (rdseed_enabled)
736fdf72 5786 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
75f4fc8d
JM
5787
5788 if (nested) {
5789 if (rdseed_enabled)
5790 vmx->nested.nested_vmx_secondary_ctls_high |=
736fdf72 5791 SECONDARY_EXEC_RDSEED_EXITING;
75f4fc8d
JM
5792 else
5793 vmx->nested.nested_vmx_secondary_ctls_high &=
736fdf72 5794 ~SECONDARY_EXEC_RDSEED_EXITING;
75f4fc8d
JM
5795 }
5796 }
5797
80154d77 5798 vmx->secondary_exec_control = exec_control;
bf8179a0
NHE
5799}
5800
ce88decf
XG
5801static void ept_set_mmio_spte_mask(void)
5802{
5803 /*
5804 * EPT Misconfigurations can be generated if the value of bits 2:0
5805 * of an EPT paging-structure entry is 110b (write/execute).
ce88decf 5806 */
dcdca5fe
PF
5807 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5808 VMX_EPT_MISCONFIG_WX_VALUE);
ce88decf
XG
5809}
5810
f53cd63c 5811#define VMX_XSS_EXIT_BITMAP 0
6aa8b732
AK
5812/*
5813 * Sets up the vmcs for emulated real mode.
5814 */
12d79917 5815static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
6aa8b732 5816{
2e4ce7f5 5817#ifdef CONFIG_X86_64
6aa8b732 5818 unsigned long a;
2e4ce7f5 5819#endif
6aa8b732 5820 int i;
6aa8b732 5821
6aa8b732 5822 /* I/O */
3e7c73e9
AK
5823 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5824 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
6aa8b732 5825
4607c2d7
AG
5826 if (enable_shadow_vmcs) {
5827 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5828 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5829 }
25c5f225 5830 if (cpu_has_vmx_msr_bitmap())
4b0be90f 5831 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
25c5f225 5832
6aa8b732
AK
5833 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5834
6aa8b732 5835 /* Control */
01e439be 5836 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
64672c95 5837 vmx->hv_deadline_tsc = -1;
6e5d865c 5838
bf8179a0 5839 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
6aa8b732 5840
dfa169bb 5841 if (cpu_has_secondary_exec_ctrls()) {
80154d77 5842 vmx_compute_secondary_exec_control(vmx);
bf8179a0 5843 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
80154d77 5844 vmx->secondary_exec_control);
dfa169bb 5845 }
f78e0e2e 5846
d62caabb 5847 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
c7c9c56c
YZ
5848 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5849 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5850 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5851 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5852
5853 vmcs_write16(GUEST_INTR_STATUS, 0);
01e439be 5854
0bcf261c 5855 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
01e439be 5856 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
c7c9c56c
YZ
5857 }
5858
4b8d54f9
ZE
5859 if (ple_gap) {
5860 vmcs_write32(PLE_GAP, ple_gap);
a7653ecd
RK
5861 vmx->ple_window = ple_window;
5862 vmx->ple_window_dirty = true;
4b8d54f9
ZE
5863 }
5864
c3707958
XG
5865 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5866 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
6aa8b732
AK
5867 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5868
9581d442
AK
5869 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5870 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
a547c6db 5871 vmx_set_constant_host_state(vmx);
05b3e0c2 5872#ifdef CONFIG_X86_64
6aa8b732
AK
5873 rdmsrl(MSR_FS_BASE, a);
5874 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5875 rdmsrl(MSR_GS_BASE, a);
5876 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5877#else
5878 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5879 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5880#endif
5881
2a499e49
BD
5882 if (cpu_has_vmx_vmfunc())
5883 vmcs_write64(VM_FUNCTION_CONTROL, 0);
5884
2cc51560
ED
5885 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5886 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
6e3dedb6 5887 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2cc51560 5888 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
6e3dedb6 5889 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
6aa8b732 5890
74545705
RK
5891 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5892 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
468d472f 5893
03916db9 5894 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6aa8b732
AK
5895 u32 index = vmx_msr_index[i];
5896 u32 data_low, data_high;
a2fa3e9f 5897 int j = vmx->nmsrs;
6aa8b732
AK
5898
5899 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5900 continue;
432bd6cb
AK
5901 if (wrmsr_safe(index, data_low, data_high) < 0)
5902 continue;
26bb0981
AK
5903 vmx->guest_msrs[j].index = i;
5904 vmx->guest_msrs[j].data = 0;
d5696725 5905 vmx->guest_msrs[j].mask = -1ull;
a2fa3e9f 5906 ++vmx->nmsrs;
6aa8b732 5907 }
6aa8b732 5908
1ccd9994 5909 vmx->arch_capabilities = kvm_get_arch_capabilities();
2961e876
GN
5910
5911 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
6aa8b732
AK
5912
5913 /* 22.2.1, 20.8.1 */
2961e876 5914 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
1c3d14fe 5915
bd7e5b08
PB
5916 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5917 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5918
bf8179a0 5919 set_cr4_guest_host_mask(vmx);
e00c8cf2 5920
f53cd63c
WL
5921 if (vmx_xsaves_supported())
5922 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5923
4e59516a
PF
5924 if (enable_pml) {
5925 ASSERT(vmx->pml_pg);
5926 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5927 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5928 }
e00c8cf2
AK
5929}
5930
d28bc9dd 5931static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
e00c8cf2
AK
5932{
5933 struct vcpu_vmx *vmx = to_vmx(vcpu);
58cb628d 5934 struct msr_data apic_base_msr;
d28bc9dd 5935 u64 cr0;
e00c8cf2 5936
7ffd92c5 5937 vmx->rmode.vm86_active = 0;
74469996 5938 vmx->spec_ctrl = 0;
e00c8cf2 5939
2033c674 5940 vcpu->arch.microcode_version = 0x100000000ULL;
ad312c7c 5941 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
d28bc9dd
NA
5942 kvm_set_cr8(vcpu, 0);
5943
5944 if (!init_event) {
5945 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5946 MSR_IA32_APICBASE_ENABLE;
5947 if (kvm_vcpu_is_reset_bsp(vcpu))
5948 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5949 apic_base_msr.host_initiated = true;
5950 kvm_set_apic_base(vcpu, &apic_base_msr);
5951 }
e00c8cf2 5952
2fb92db1
AK
5953 vmx_segment_cache_clear(vmx);
5954
5706be0d 5955 seg_setup(VCPU_SREG_CS);
66450a21 5956 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
f3531054 5957 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
e00c8cf2
AK
5958
5959 seg_setup(VCPU_SREG_DS);
5960 seg_setup(VCPU_SREG_ES);
5961 seg_setup(VCPU_SREG_FS);
5962 seg_setup(VCPU_SREG_GS);
5963 seg_setup(VCPU_SREG_SS);
5964
5965 vmcs_write16(GUEST_TR_SELECTOR, 0);
5966 vmcs_writel(GUEST_TR_BASE, 0);
5967 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5968 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5969
5970 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5971 vmcs_writel(GUEST_LDTR_BASE, 0);
5972 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5973 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5974
d28bc9dd
NA
5975 if (!init_event) {
5976 vmcs_write32(GUEST_SYSENTER_CS, 0);
5977 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5978 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5979 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5980 }
e00c8cf2 5981
c37c2873 5982 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
66450a21 5983 kvm_rip_write(vcpu, 0xfff0);
e00c8cf2 5984
e00c8cf2
AK
5985 vmcs_writel(GUEST_GDTR_BASE, 0);
5986 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5987
5988 vmcs_writel(GUEST_IDTR_BASE, 0);
5989 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5990
443381a8 5991 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
e00c8cf2 5992 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
f3531054 5993 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
a554d207
WL
5994 if (kvm_mpx_supported())
5995 vmcs_write64(GUEST_BNDCFGS, 0);
e00c8cf2 5996
e00c8cf2
AK
5997 setup_msrs(vmx);
5998
6aa8b732
AK
5999 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6000
d28bc9dd 6001 if (cpu_has_vmx_tpr_shadow() && !init_event) {
f78e0e2e 6002 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
35754c98 6003 if (cpu_need_tpr_shadow(vcpu))
f78e0e2e 6004 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
d28bc9dd 6005 __pa(vcpu->arch.apic->regs));
f78e0e2e
SY
6006 vmcs_write32(TPR_THRESHOLD, 0);
6007 }
6008
a73896cb 6009 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6aa8b732 6010
2384d2b3
SY
6011 if (vmx->vpid != 0)
6012 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6013
d28bc9dd 6014 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
d28bc9dd 6015 vmx->vcpu.arch.cr0 = cr0;
f2463247 6016 vmx_set_cr0(vcpu, cr0); /* enter rmode */
d28bc9dd 6017 vmx_set_cr4(vcpu, 0);
5690891b 6018 vmx_set_efer(vcpu, 0);
bd7e5b08 6019
d28bc9dd 6020 update_exception_bitmap(vcpu);
6aa8b732 6021
dd5f5341 6022 vpid_sync_context(vmx->vpid);
6aa8b732
AK
6023}
6024
b6f1250e
NHE
6025/*
6026 * In nested virtualization, check if L1 asked to exit on external interrupts.
6027 * For most existing hypervisors, this will always return true.
6028 */
6029static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6030{
6031 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6032 PIN_BASED_EXT_INTR_MASK;
6033}
6034
77b0f5d6
BD
6035/*
6036 * In nested virtualization, check if L1 has set
6037 * VM_EXIT_ACK_INTR_ON_EXIT
6038 */
6039static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6040{
6041 return get_vmcs12(vcpu)->vm_exit_controls &
6042 VM_EXIT_ACK_INTR_ON_EXIT;
6043}
6044
ea8ceb83
JK
6045static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6046{
6047 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6048 PIN_BASED_NMI_EXITING;
6049}
6050
c9a7953f 6051static void enable_irq_window(struct kvm_vcpu *vcpu)
3b86cd99 6052{
47c0152e
PB
6053 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6054 CPU_BASED_VIRTUAL_INTR_PENDING);
3b86cd99
JK
6055}
6056
c9a7953f 6057static void enable_nmi_window(struct kvm_vcpu *vcpu)
3b86cd99 6058{
d02fcf50 6059 if (!enable_vnmi ||
8a1b4392 6060 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
c9a7953f
JK
6061 enable_irq_window(vcpu);
6062 return;
6063 }
3b86cd99 6064
47c0152e
PB
6065 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6066 CPU_BASED_VIRTUAL_NMI_PENDING);
3b86cd99
JK
6067}
6068
66fd3f7f 6069static void vmx_inject_irq(struct kvm_vcpu *vcpu)
85f455f7 6070{
9c8cba37 6071 struct vcpu_vmx *vmx = to_vmx(vcpu);
66fd3f7f
GN
6072 uint32_t intr;
6073 int irq = vcpu->arch.interrupt.nr;
9c8cba37 6074
229456fc 6075 trace_kvm_inj_virq(irq);
2714d1d3 6076
fa89a817 6077 ++vcpu->stat.irq_injections;
7ffd92c5 6078 if (vmx->rmode.vm86_active) {
71f9833b
SH
6079 int inc_eip = 0;
6080 if (vcpu->arch.interrupt.soft)
6081 inc_eip = vcpu->arch.event_exit_inst_len;
6082 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
a92601bb 6083 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
85f455f7
ED
6084 return;
6085 }
66fd3f7f
GN
6086 intr = irq | INTR_INFO_VALID_MASK;
6087 if (vcpu->arch.interrupt.soft) {
6088 intr |= INTR_TYPE_SOFT_INTR;
6089 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6090 vmx->vcpu.arch.event_exit_inst_len);
6091 } else
6092 intr |= INTR_TYPE_EXT_INTR;
6093 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
85f455f7
ED
6094}
6095
f08864b4
SY
6096static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6097{
66a5a347
JK
6098 struct vcpu_vmx *vmx = to_vmx(vcpu);
6099
d02fcf50 6100 if (!enable_vnmi) {
8a1b4392
PB
6101 /*
6102 * Tracking the NMI-blocked state in software is built upon
6103 * finding the next open IRQ window. This, in turn, depends on
6104 * well-behaving guests: They have to keep IRQs disabled at
6105 * least as long as the NMI handler runs. Otherwise we may
6106 * cause NMI nesting, maybe breaking the guest. But as this is
6107 * highly unlikely, we can live with the residual risk.
6108 */
6109 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6110 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6111 }
6112
4c4a6f79
PB
6113 ++vcpu->stat.nmi_injections;
6114 vmx->loaded_vmcs->nmi_known_unmasked = false;
3b86cd99 6115
7ffd92c5 6116 if (vmx->rmode.vm86_active) {
71f9833b 6117 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
a92601bb 6118 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
66a5a347
JK
6119 return;
6120 }
c5a6d5f7 6121
f08864b4
SY
6122 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6123 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
f08864b4
SY
6124}
6125
3cfc3092
JK
6126static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6127{
4c4a6f79
PB
6128 struct vcpu_vmx *vmx = to_vmx(vcpu);
6129 bool masked;
6130
d02fcf50 6131 if (!enable_vnmi)
8a1b4392 6132 return vmx->loaded_vmcs->soft_vnmi_blocked;
4c4a6f79 6133 if (vmx->loaded_vmcs->nmi_known_unmasked)
9d58b931 6134 return false;
4c4a6f79
PB
6135 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6136 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6137 return masked;
3cfc3092
JK
6138}
6139
6140static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6141{
6142 struct vcpu_vmx *vmx = to_vmx(vcpu);
6143
d02fcf50 6144 if (!enable_vnmi) {
8a1b4392
PB
6145 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6146 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6147 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6148 }
6149 } else {
6150 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6151 if (masked)
6152 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6153 GUEST_INTR_STATE_NMI);
6154 else
6155 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6156 GUEST_INTR_STATE_NMI);
6157 }
3cfc3092
JK
6158}
6159
2505dc9f
JK
6160static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6161{
b6b8a145
JK
6162 if (to_vmx(vcpu)->nested.nested_run_pending)
6163 return 0;
ea8ceb83 6164
d02fcf50 6165 if (!enable_vnmi &&
8a1b4392
PB
6166 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6167 return 0;
6168
2505dc9f
JK
6169 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6170 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6171 | GUEST_INTR_STATE_NMI));
6172}
6173
78646121
GN
6174static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6175{
b6b8a145
JK
6176 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6177 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
c4282df9
GN
6178 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6179 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
78646121
GN
6180}
6181
cbc94022
IE
6182static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6183{
6184 int ret;
cbc94022 6185
1d8007bd
PB
6186 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6187 PAGE_SIZE * 3);
cbc94022
IE
6188 if (ret)
6189 return ret;
bfc6d222 6190 kvm->arch.tss_addr = addr;
1f755a82 6191 return init_rmode_tss(kvm);
cbc94022
IE
6192}
6193
0ca1b4f4 6194static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
6aa8b732 6195{
77ab6db0 6196 switch (vec) {
77ab6db0 6197 case BP_VECTOR:
c573cd22
JK
6198 /*
6199 * Update instruction length as we may reinject the exception
6200 * from user space while in guest debugging mode.
6201 */
6202 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6203 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
d0bfb940 6204 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
0ca1b4f4
GN
6205 return false;
6206 /* fall through */
6207 case DB_VECTOR:
6208 if (vcpu->guest_debug &
6209 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6210 return false;
d0bfb940
JK
6211 /* fall through */
6212 case DE_VECTOR:
77ab6db0
JK
6213 case OF_VECTOR:
6214 case BR_VECTOR:
6215 case UD_VECTOR:
6216 case DF_VECTOR:
6217 case SS_VECTOR:
6218 case GP_VECTOR:
6219 case MF_VECTOR:
0ca1b4f4
GN
6220 return true;
6221 break;
77ab6db0 6222 }
0ca1b4f4
GN
6223 return false;
6224}
6225
6226static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6227 int vec, u32 err_code)
6228{
6229 /*
6230 * Instruction with address size override prefix opcode 0x67
6231 * Cause the #SS fault with 0 error code in VM86 mode.
6232 */
6233 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6234 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6235 if (vcpu->arch.halt_request) {
6236 vcpu->arch.halt_request = 0;
5cb56059 6237 return kvm_vcpu_halt(vcpu);
0ca1b4f4
GN
6238 }
6239 return 1;
6240 }
6241 return 0;
6242 }
6243
6244 /*
6245 * Forward all other exceptions that are valid in real mode.
6246 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6247 * the required debugging infrastructure rework.
6248 */
6249 kvm_queue_exception(vcpu, vec);
6250 return 1;
6aa8b732
AK
6251}
6252
a0861c02
AK
6253/*
6254 * Trigger machine check on the host. We assume all the MSRs are already set up
6255 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6256 * We pass a fake environment to the machine check handler because we want
6257 * the guest to be always treated like user space, no matter what context
6258 * it used internally.
6259 */
6260static void kvm_machine_check(void)
6261{
6262#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6263 struct pt_regs regs = {
6264 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6265 .flags = X86_EFLAGS_IF,
6266 };
6267
6268 do_machine_check(&regs, 0);
6269#endif
6270}
6271
851ba692 6272static int handle_machine_check(struct kvm_vcpu *vcpu)
a0861c02
AK
6273{
6274 /* already handled by vcpu_run */
6275 return 1;
6276}
6277
851ba692 6278static int handle_exception(struct kvm_vcpu *vcpu)
6aa8b732 6279{
1155f76a 6280 struct vcpu_vmx *vmx = to_vmx(vcpu);
851ba692 6281 struct kvm_run *kvm_run = vcpu->run;
d0bfb940 6282 u32 intr_info, ex_no, error_code;
42dbaa5a 6283 unsigned long cr2, rip, dr6;
6aa8b732
AK
6284 u32 vect_info;
6285 enum emulation_result er;
6286
1155f76a 6287 vect_info = vmx->idt_vectoring_info;
88786475 6288 intr_info = vmx->exit_intr_info;
6aa8b732 6289
a0861c02 6290 if (is_machine_check(intr_info))
851ba692 6291 return handle_machine_check(vcpu);
a0861c02 6292
ef85b673 6293 if (is_nmi(intr_info))
1b6269db 6294 return 1; /* already handled by vmx_vcpu_run() */
2ab455cc 6295
7aa81cc0 6296 if (is_invalid_opcode(intr_info)) {
51d8b661 6297 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
61cb57c9
LA
6298 if (er == EMULATE_USER_EXIT)
6299 return 0;
7aa81cc0 6300 if (er != EMULATE_DONE)
7ee5d940 6301 kvm_queue_exception(vcpu, UD_VECTOR);
7aa81cc0
AL
6302 return 1;
6303 }
6304
6aa8b732 6305 error_code = 0;
2e11384c 6306 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
6aa8b732 6307 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
bf4ca23e
XG
6308
6309 /*
6310 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6311 * MMIO, it is better to report an internal error.
6312 * See the comments in vmx_handle_exit.
6313 */
6314 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6315 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6316 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6317 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
80f0e95d 6318 vcpu->run->internal.ndata = 3;
bf4ca23e
XG
6319 vcpu->run->internal.data[0] = vect_info;
6320 vcpu->run->internal.data[1] = intr_info;
80f0e95d 6321 vcpu->run->internal.data[2] = error_code;
bf4ca23e
XG
6322 return 0;
6323 }
6324
6aa8b732
AK
6325 if (is_page_fault(intr_info)) {
6326 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1261bfa3
WL
6327 /* EPT won't cause page fault directly */
6328 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
d0006530 6329 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
6aa8b732
AK
6330 }
6331
d0bfb940 6332 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
0ca1b4f4
GN
6333
6334 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6335 return handle_rmode_exception(vcpu, ex_no, error_code);
6336
42dbaa5a 6337 switch (ex_no) {
54a20552
EN
6338 case AC_VECTOR:
6339 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6340 return 1;
42dbaa5a
JK
6341 case DB_VECTOR:
6342 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6343 if (!(vcpu->guest_debug &
6344 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
8246bf52 6345 vcpu->arch.dr6 &= ~15;
6f43ed01 6346 vcpu->arch.dr6 |= dr6 | DR6_RTM;
3252850d 6347 if (is_icebp(intr_info))
fd2a445a
HD
6348 skip_emulated_instruction(vcpu);
6349
42dbaa5a
JK
6350 kvm_queue_exception(vcpu, DB_VECTOR);
6351 return 1;
6352 }
6353 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6354 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6355 /* fall through */
6356 case BP_VECTOR:
c573cd22
JK
6357 /*
6358 * Update instruction length as we may reinject #BP from
6359 * user space while in guest debugging mode. Reading it for
6360 * #DB as well causes no harm, it is not used in that case.
6361 */
6362 vmx->vcpu.arch.event_exit_inst_len =
6363 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6aa8b732 6364 kvm_run->exit_reason = KVM_EXIT_DEBUG;
0a434bb2 6365 rip = kvm_rip_read(vcpu);
d0bfb940
JK
6366 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6367 kvm_run->debug.arch.exception = ex_no;
42dbaa5a
JK
6368 break;
6369 default:
d0bfb940
JK
6370 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6371 kvm_run->ex.exception = ex_no;
6372 kvm_run->ex.error_code = error_code;
42dbaa5a 6373 break;
6aa8b732 6374 }
6aa8b732
AK
6375 return 0;
6376}
6377
851ba692 6378static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6aa8b732 6379{
1165f5fe 6380 ++vcpu->stat.irq_exits;
6aa8b732
AK
6381 return 1;
6382}
6383
851ba692 6384static int handle_triple_fault(struct kvm_vcpu *vcpu)
988ad74f 6385{
851ba692 6386 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
bbeac283 6387 vcpu->mmio_needed = 0;
988ad74f
AK
6388 return 0;
6389}
6aa8b732 6390
851ba692 6391static int handle_io(struct kvm_vcpu *vcpu)
6aa8b732 6392{
bfdaab09 6393 unsigned long exit_qualification;
6affcbed 6394 int size, in, string, ret;
039576c0 6395 unsigned port;
6aa8b732 6396
bfdaab09 6397 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
039576c0 6398 string = (exit_qualification & 16) != 0;
cf8f70bf 6399 in = (exit_qualification & 8) != 0;
e70669ab 6400
cf8f70bf 6401 ++vcpu->stat.io_exits;
e70669ab 6402
cf8f70bf 6403 if (string || in)
51d8b661 6404 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
e70669ab 6405
cf8f70bf
GN
6406 port = exit_qualification >> 16;
6407 size = (exit_qualification & 7) + 1;
cf8f70bf 6408
6affcbed
KH
6409 ret = kvm_skip_emulated_instruction(vcpu);
6410
6411 /*
6412 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
6413 * KVM_EXIT_DEBUG here.
6414 */
6415 return kvm_fast_pio_out(vcpu, size, port) && ret;
6aa8b732
AK
6416}
6417
102d8325
IM
6418static void
6419vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6420{
6421 /*
6422 * Patch in the VMCALL instruction:
6423 */
6424 hypercall[0] = 0x0f;
6425 hypercall[1] = 0x01;
6426 hypercall[2] = 0xc1;
102d8325
IM
6427}
6428
0fa06071 6429/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
eeadf9e7
NHE
6430static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6431{
eeadf9e7 6432 if (is_guest_mode(vcpu)) {
1a0d74e6
JK
6433 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6434 unsigned long orig_val = val;
6435
eeadf9e7
NHE
6436 /*
6437 * We get here when L2 changed cr0 in a way that did not change
6438 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
1a0d74e6
JK
6439 * but did change L0 shadowed bits. So we first calculate the
6440 * effective cr0 value that L1 would like to write into the
6441 * hardware. It consists of the L2-owned bits from the new
6442 * value combined with the L1-owned bits from L1's guest_cr0.
eeadf9e7 6443 */
1a0d74e6
JK
6444 val = (val & ~vmcs12->cr0_guest_host_mask) |
6445 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6446
3899152c 6447 if (!nested_guest_cr0_valid(vcpu, val))
eeadf9e7 6448 return 1;
1a0d74e6
JK
6449
6450 if (kvm_set_cr0(vcpu, val))
6451 return 1;
6452 vmcs_writel(CR0_READ_SHADOW, orig_val);
eeadf9e7 6453 return 0;
1a0d74e6
JK
6454 } else {
6455 if (to_vmx(vcpu)->nested.vmxon &&
3899152c 6456 !nested_host_cr0_valid(vcpu, val))
1a0d74e6 6457 return 1;
3899152c 6458
eeadf9e7 6459 return kvm_set_cr0(vcpu, val);
1a0d74e6 6460 }
eeadf9e7
NHE
6461}
6462
6463static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6464{
6465 if (is_guest_mode(vcpu)) {
1a0d74e6
JK
6466 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6467 unsigned long orig_val = val;
6468
6469 /* analogously to handle_set_cr0 */
6470 val = (val & ~vmcs12->cr4_guest_host_mask) |
6471 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6472 if (kvm_set_cr4(vcpu, val))
eeadf9e7 6473 return 1;
1a0d74e6 6474 vmcs_writel(CR4_READ_SHADOW, orig_val);
eeadf9e7
NHE
6475 return 0;
6476 } else
6477 return kvm_set_cr4(vcpu, val);
6478}
6479
851ba692 6480static int handle_cr(struct kvm_vcpu *vcpu)
6aa8b732 6481{
229456fc 6482 unsigned long exit_qualification, val;
6aa8b732
AK
6483 int cr;
6484 int reg;
49a9b07e 6485 int err;
6affcbed 6486 int ret;
6aa8b732 6487
bfdaab09 6488 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6aa8b732
AK
6489 cr = exit_qualification & 15;
6490 reg = (exit_qualification >> 8) & 15;
6491 switch ((exit_qualification >> 4) & 3) {
6492 case 0: /* mov to cr */
1e32c079 6493 val = kvm_register_readl(vcpu, reg);
229456fc 6494 trace_kvm_cr_write(cr, val);
6aa8b732
AK
6495 switch (cr) {
6496 case 0:
eeadf9e7 6497 err = handle_set_cr0(vcpu, val);
6affcbed 6498 return kvm_complete_insn_gp(vcpu, err);
6aa8b732 6499 case 3:
2390218b 6500 err = kvm_set_cr3(vcpu, val);
6affcbed 6501 return kvm_complete_insn_gp(vcpu, err);
6aa8b732 6502 case 4:
eeadf9e7 6503 err = handle_set_cr4(vcpu, val);
6affcbed 6504 return kvm_complete_insn_gp(vcpu, err);
0a5fff19
GN
6505 case 8: {
6506 u8 cr8_prev = kvm_get_cr8(vcpu);
1e32c079 6507 u8 cr8 = (u8)val;
eea1cff9 6508 err = kvm_set_cr8(vcpu, cr8);
6affcbed 6509 ret = kvm_complete_insn_gp(vcpu, err);
35754c98 6510 if (lapic_in_kernel(vcpu))
6affcbed 6511 return ret;
0a5fff19 6512 if (cr8_prev <= cr8)
6affcbed
KH
6513 return ret;
6514 /*
6515 * TODO: we might be squashing a
6516 * KVM_GUESTDBG_SINGLESTEP-triggered
6517 * KVM_EXIT_DEBUG here.
6518 */
851ba692 6519 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
0a5fff19
GN
6520 return 0;
6521 }
4b8073e4 6522 }
6aa8b732 6523 break;
25c4c276 6524 case 2: /* clts */
bd7e5b08
PB
6525 WARN_ONCE(1, "Guest should always own CR0.TS");
6526 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4d4ec087 6527 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
6affcbed 6528 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
6529 case 1: /*mov from cr*/
6530 switch (cr) {
6531 case 3:
9f8fe504
AK
6532 val = kvm_read_cr3(vcpu);
6533 kvm_register_write(vcpu, reg, val);
6534 trace_kvm_cr_read(cr, val);
6affcbed 6535 return kvm_skip_emulated_instruction(vcpu);
6aa8b732 6536 case 8:
229456fc
MT
6537 val = kvm_get_cr8(vcpu);
6538 kvm_register_write(vcpu, reg, val);
6539 trace_kvm_cr_read(cr, val);
6affcbed 6540 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
6541 }
6542 break;
6543 case 3: /* lmsw */
a1f83a74 6544 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
4d4ec087 6545 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
a1f83a74 6546 kvm_lmsw(vcpu, val);
6aa8b732 6547
6affcbed 6548 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
6549 default:
6550 break;
6551 }
851ba692 6552 vcpu->run->exit_reason = 0;
a737f256 6553 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6aa8b732
AK
6554 (int)(exit_qualification >> 4) & 3, cr);
6555 return 0;
6556}
6557
851ba692 6558static int handle_dr(struct kvm_vcpu *vcpu)
6aa8b732 6559{
bfdaab09 6560 unsigned long exit_qualification;
16f8a6f9
NA
6561 int dr, dr7, reg;
6562
6563 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6564 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6565
6566 /* First, if DR does not exist, trigger UD */
6567 if (!kvm_require_dr(vcpu, dr))
6568 return 1;
6aa8b732 6569
f2483415 6570 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
0a79b009
AK
6571 if (!kvm_require_cpl(vcpu, 0))
6572 return 1;
16f8a6f9
NA
6573 dr7 = vmcs_readl(GUEST_DR7);
6574 if (dr7 & DR7_GD) {
42dbaa5a
JK
6575 /*
6576 * As the vm-exit takes precedence over the debug trap, we
6577 * need to emulate the latter, either for the host or the
6578 * guest debugging itself.
6579 */
6580 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
851ba692 6581 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
16f8a6f9 6582 vcpu->run->debug.arch.dr7 = dr7;
82b32774 6583 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
851ba692
AK
6584 vcpu->run->debug.arch.exception = DB_VECTOR;
6585 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
42dbaa5a
JK
6586 return 0;
6587 } else {
7305eb5d 6588 vcpu->arch.dr6 &= ~15;
6f43ed01 6589 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
42dbaa5a
JK
6590 kvm_queue_exception(vcpu, DB_VECTOR);
6591 return 1;
6592 }
6593 }
6594
81908bf4 6595 if (vcpu->guest_debug == 0) {
8f22372f
PB
6596 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6597 CPU_BASED_MOV_DR_EXITING);
81908bf4
PB
6598
6599 /*
6600 * No more DR vmexits; force a reload of the debug registers
6601 * and reenter on this instruction. The next vmexit will
6602 * retrieve the full state of the debug registers.
6603 */
6604 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6605 return 1;
6606 }
6607
42dbaa5a
JK
6608 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6609 if (exit_qualification & TYPE_MOV_FROM_DR) {
020df079 6610 unsigned long val;
4c4d563b
JK
6611
6612 if (kvm_get_dr(vcpu, dr, &val))
6613 return 1;
6614 kvm_register_write(vcpu, reg, val);
020df079 6615 } else
5777392e 6616 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
4c4d563b
JK
6617 return 1;
6618
6affcbed 6619 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
6620}
6621
73aaf249
JK
6622static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6623{
6624 return vcpu->arch.dr6;
6625}
6626
6627static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6628{
6629}
6630
81908bf4
PB
6631static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6632{
81908bf4
PB
6633 get_debugreg(vcpu->arch.db[0], 0);
6634 get_debugreg(vcpu->arch.db[1], 1);
6635 get_debugreg(vcpu->arch.db[2], 2);
6636 get_debugreg(vcpu->arch.db[3], 3);
6637 get_debugreg(vcpu->arch.dr6, 6);
6638 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6639
6640 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
8f22372f 6641 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
81908bf4
PB
6642}
6643
020df079
GN
6644static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6645{
6646 vmcs_writel(GUEST_DR7, val);
6647}
6648
851ba692 6649static int handle_cpuid(struct kvm_vcpu *vcpu)
6aa8b732 6650{
6a908b62 6651 return kvm_emulate_cpuid(vcpu);
6aa8b732
AK
6652}
6653
851ba692 6654static int handle_rdmsr(struct kvm_vcpu *vcpu)
6aa8b732 6655{
ad312c7c 6656 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
609e36d3 6657 struct msr_data msr_info;
6aa8b732 6658
609e36d3
PB
6659 msr_info.index = ecx;
6660 msr_info.host_initiated = false;
6661 if (vmx_get_msr(vcpu, &msr_info)) {
59200273 6662 trace_kvm_msr_read_ex(ecx);
c1a5d4f9 6663 kvm_inject_gp(vcpu, 0);
6aa8b732
AK
6664 return 1;
6665 }
6666
609e36d3 6667 trace_kvm_msr_read(ecx, msr_info.data);
2714d1d3 6668
6aa8b732 6669 /* FIXME: handling of bits 32:63 of rax, rdx */
609e36d3
PB
6670 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6671 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
6affcbed 6672 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
6673}
6674
851ba692 6675static int handle_wrmsr(struct kvm_vcpu *vcpu)
6aa8b732 6676{
8fe8ab46 6677 struct msr_data msr;
ad312c7c
ZX
6678 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6679 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6680 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
6aa8b732 6681
8fe8ab46
WA
6682 msr.data = data;
6683 msr.index = ecx;
6684 msr.host_initiated = false;
854e8bb1 6685 if (kvm_set_msr(vcpu, &msr) != 0) {
59200273 6686 trace_kvm_msr_write_ex(ecx, data);
c1a5d4f9 6687 kvm_inject_gp(vcpu, 0);
6aa8b732
AK
6688 return 1;
6689 }
6690
59200273 6691 trace_kvm_msr_write(ecx, data);
6affcbed 6692 return kvm_skip_emulated_instruction(vcpu);
6aa8b732
AK
6693}
6694
851ba692 6695static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6e5d865c 6696{
eb90f341 6697 kvm_apic_update_ppr(vcpu);
6e5d865c
YS
6698 return 1;
6699}
6700
851ba692 6701static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6aa8b732 6702{
47c0152e
PB
6703 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6704 CPU_BASED_VIRTUAL_INTR_PENDING);
2714d1d3 6705
3842d135
AK
6706 kvm_make_request(KVM_REQ_EVENT, vcpu);
6707
a26bf12a 6708 ++vcpu->stat.irq_window_exits;
6aa8b732
AK
6709 return 1;
6710}
6711
851ba692 6712static int handle_halt(struct kvm_vcpu *vcpu)
6aa8b732 6713{
d3bef15f 6714 return kvm_emulate_halt(vcpu);
6aa8b732
AK
6715}
6716
851ba692 6717static int handle_vmcall(struct kvm_vcpu *vcpu)
c21415e8 6718{
0d9c055e 6719 return kvm_emulate_hypercall(vcpu);
c21415e8
IM
6720}
6721
ec25d5e6
GN
6722static int handle_invd(struct kvm_vcpu *vcpu)
6723{
51d8b661 6724 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
ec25d5e6
GN
6725}
6726
851ba692 6727static int handle_invlpg(struct kvm_vcpu *vcpu)
a7052897 6728{
f9c617f6 6729 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
a7052897
MT
6730
6731 kvm_mmu_invlpg(vcpu, exit_qualification);
6affcbed 6732 return kvm_skip_emulated_instruction(vcpu);
a7052897
MT
6733}
6734
fee84b07
AK
6735static int handle_rdpmc(struct kvm_vcpu *vcpu)
6736{
6737 int err;
6738
6739 err = kvm_rdpmc(vcpu);
6affcbed 6740 return kvm_complete_insn_gp(vcpu, err);
fee84b07
AK
6741}
6742
851ba692 6743static int handle_wbinvd(struct kvm_vcpu *vcpu)
e5edaa01 6744{
6affcbed 6745 return kvm_emulate_wbinvd(vcpu);
e5edaa01
ED
6746}
6747
2acf923e
DC
6748static int handle_xsetbv(struct kvm_vcpu *vcpu)
6749{
6750 u64 new_bv = kvm_read_edx_eax(vcpu);
6751 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6752
6753 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6affcbed 6754 return kvm_skip_emulated_instruction(vcpu);
2acf923e
DC
6755 return 1;
6756}
6757
f53cd63c
WL
6758static int handle_xsaves(struct kvm_vcpu *vcpu)
6759{
6affcbed 6760 kvm_skip_emulated_instruction(vcpu);
f53cd63c
WL
6761 WARN(1, "this should never happen\n");
6762 return 1;
6763}
6764
6765static int handle_xrstors(struct kvm_vcpu *vcpu)
6766{
6affcbed 6767 kvm_skip_emulated_instruction(vcpu);
f53cd63c
WL
6768 WARN(1, "this should never happen\n");
6769 return 1;
6770}
6771
851ba692 6772static int handle_apic_access(struct kvm_vcpu *vcpu)
f78e0e2e 6773{
58fbbf26
KT
6774 if (likely(fasteoi)) {
6775 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6776 int access_type, offset;
6777
6778 access_type = exit_qualification & APIC_ACCESS_TYPE;
6779 offset = exit_qualification & APIC_ACCESS_OFFSET;
6780 /*
6781 * Sane guest uses MOV to write EOI, with written value
6782 * not cared. So make a short-circuit here by avoiding
6783 * heavy instruction emulation.
6784 */
6785 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6786 (offset == APIC_EOI)) {
6787 kvm_lapic_set_eoi(vcpu);
6affcbed 6788 return kvm_skip_emulated_instruction(vcpu);
58fbbf26
KT
6789 }
6790 }
51d8b661 6791 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
f78e0e2e
SY
6792}
6793
c7c9c56c
YZ
6794static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6795{
6796 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6797 int vector = exit_qualification & 0xff;
6798
6799 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6800 kvm_apic_set_eoi_accelerated(vcpu, vector);
6801 return 1;
6802}
6803
83d4c286
YZ
6804static int handle_apic_write(struct kvm_vcpu *vcpu)
6805{
6806 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6807 u32 offset = exit_qualification & 0xfff;
6808
6809 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6810 kvm_apic_write_nodecode(vcpu, offset);
6811 return 1;
6812}
6813
851ba692 6814static int handle_task_switch(struct kvm_vcpu *vcpu)
37817f29 6815{
60637aac 6816 struct vcpu_vmx *vmx = to_vmx(vcpu);
37817f29 6817 unsigned long exit_qualification;
e269fb21
JK
6818 bool has_error_code = false;
6819 u32 error_code = 0;
37817f29 6820 u16 tss_selector;
7f3d35fd 6821 int reason, type, idt_v, idt_index;
64a7ec06
GN
6822
6823 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
7f3d35fd 6824 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
64a7ec06 6825 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
37817f29
IE
6826
6827 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6828
6829 reason = (u32)exit_qualification >> 30;
64a7ec06
GN
6830 if (reason == TASK_SWITCH_GATE && idt_v) {
6831 switch (type) {
6832 case INTR_TYPE_NMI_INTR:
6833 vcpu->arch.nmi_injected = false;
654f06fc 6834 vmx_set_nmi_mask(vcpu, true);
64a7ec06
GN
6835 break;
6836 case INTR_TYPE_EXT_INTR:
66fd3f7f 6837 case INTR_TYPE_SOFT_INTR:
64a7ec06
GN
6838 kvm_clear_interrupt_queue(vcpu);
6839 break;
6840 case INTR_TYPE_HARD_EXCEPTION:
e269fb21
JK
6841 if (vmx->idt_vectoring_info &
6842 VECTORING_INFO_DELIVER_CODE_MASK) {
6843 has_error_code = true;
6844 error_code =
6845 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6846 }
6847 /* fall through */
64a7ec06
GN
6848 case INTR_TYPE_SOFT_EXCEPTION:
6849 kvm_clear_exception_queue(vcpu);
6850 break;
6851 default:
6852 break;
6853 }
60637aac 6854 }
37817f29
IE
6855 tss_selector = exit_qualification;
6856
64a7ec06
GN
6857 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6858 type != INTR_TYPE_EXT_INTR &&
6859 type != INTR_TYPE_NMI_INTR))
6860 skip_emulated_instruction(vcpu);
6861
7f3d35fd
KW
6862 if (kvm_task_switch(vcpu, tss_selector,
6863 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6864 has_error_code, error_code) == EMULATE_FAIL) {
acb54517
GN
6865 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6866 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6867 vcpu->run->internal.ndata = 0;
42dbaa5a 6868 return 0;
acb54517 6869 }
42dbaa5a 6870
42dbaa5a
JK
6871 /*
6872 * TODO: What about debug traps on tss switch?
6873 * Are we supposed to inject them and update dr6?
6874 */
6875
6876 return 1;
37817f29
IE
6877}
6878
851ba692 6879static int handle_ept_violation(struct kvm_vcpu *vcpu)
1439442c 6880{
f9c617f6 6881 unsigned long exit_qualification;
1439442c 6882 gpa_t gpa;
eebed243 6883 u64 error_code;
1439442c 6884
f9c617f6 6885 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
1439442c 6886
0be9c7a8
GN
6887 /*
6888 * EPT violation happened while executing iret from NMI,
6889 * "blocked by NMI" bit has to be set before next VM entry.
6890 * There are errata that may cause this bit to not be set:
6891 * AAK134, BY25.
6892 */
bcd1c294 6893 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
d02fcf50 6894 enable_vnmi &&
bcd1c294 6895 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
0be9c7a8
GN
6896 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6897
1439442c 6898 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
229456fc 6899 trace_kvm_page_fault(gpa, exit_qualification);
4f5982a5 6900
27959a44 6901 /* Is it a read fault? */
ab22a473 6902 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
27959a44
JS
6903 ? PFERR_USER_MASK : 0;
6904 /* Is it a write fault? */
ab22a473 6905 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
27959a44
JS
6906 ? PFERR_WRITE_MASK : 0;
6907 /* Is it a fetch fault? */
ab22a473 6908 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
27959a44
JS
6909 ? PFERR_FETCH_MASK : 0;
6910 /* ept page table entry is present? */
6911 error_code |= (exit_qualification &
6912 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6913 EPT_VIOLATION_EXECUTABLE))
6914 ? PFERR_PRESENT_MASK : 0;
4f5982a5 6915
eebed243
PB
6916 error_code |= (exit_qualification & 0x100) != 0 ?
6917 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
25d92081 6918
25d92081 6919 vcpu->arch.exit_qualification = exit_qualification;
4f5982a5 6920 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
1439442c
SY
6921}
6922
851ba692 6923static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
68f89400 6924{
f735d4af 6925 int ret;
68f89400
MT
6926 gpa_t gpa;
6927
9034e6e8
PB
6928 /*
6929 * A nested guest cannot optimize MMIO vmexits, because we have an
6930 * nGPA here instead of the required GPA.
6931 */
68f89400 6932 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9034e6e8
PB
6933 if (!is_guest_mode(vcpu) &&
6934 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
931c33b1 6935 trace_kvm_fast_mmio(gpa);
60165b0a
VK
6936 /*
6937 * Doing kvm_skip_emulated_instruction() depends on undefined
6938 * behavior: Intel's manual doesn't mandate
6939 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
6940 * occurs and while on real hardware it was observed to be set,
6941 * other hypervisors (namely Hyper-V) don't set it, we end up
6942 * advancing IP with some random value. Disable fast mmio when
6943 * running nested and keep it for real hardware in hope that
6944 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
6945 */
6946 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
6947 return kvm_skip_emulated_instruction(vcpu);
6948 else
6949 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
6950 NULL, 0) == EMULATE_DONE;
68c3b4d1 6951 }
68f89400 6952
e08d26f0
PB
6953 ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6954 if (ret >= 0)
6955 return ret;
ce88decf
XG
6956
6957 /* It is the real ept misconfig */
f735d4af 6958 WARN_ON(1);
68f89400 6959
851ba692
AK
6960 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6961 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
68f89400
MT
6962
6963 return 0;
6964}
6965
851ba692 6966static int handle_nmi_window(struct kvm_vcpu *vcpu)
f08864b4 6967{
d02fcf50 6968 WARN_ON_ONCE(!enable_vnmi);
47c0152e
PB
6969 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6970 CPU_BASED_VIRTUAL_NMI_PENDING);
f08864b4 6971 ++vcpu->stat.nmi_window_exits;
3842d135 6972 kvm_make_request(KVM_REQ_EVENT, vcpu);
f08864b4
SY
6973
6974 return 1;
6975}
6976
80ced186 6977static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
ea953ef0 6978{
8b3079a5
AK
6979 struct vcpu_vmx *vmx = to_vmx(vcpu);
6980 enum emulation_result err = EMULATE_DONE;
80ced186 6981 int ret = 1;
49e9d557
AK
6982 u32 cpu_exec_ctrl;
6983 bool intr_window_requested;
b8405c18 6984 unsigned count = 130;
49e9d557
AK
6985
6986 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6987 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
ea953ef0 6988
98eb2f8b 6989 while (vmx->emulation_required && count-- != 0) {
bdea48e3 6990 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
49e9d557
AK
6991 return handle_interrupt_window(&vmx->vcpu);
6992
72875d8a 6993 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
de87dcdd
AK
6994 return 1;
6995
9b8ae637 6996 err = emulate_instruction(vcpu, 0);
ea953ef0 6997
ac0a48c3 6998 if (err == EMULATE_USER_EXIT) {
94452b9e 6999 ++vcpu->stat.mmio_exits;
80ced186
MG
7000 ret = 0;
7001 goto out;
7002 }
1d5a4d9b 7003
299f0328
SC
7004 if (err != EMULATE_DONE)
7005 goto emulation_error;
7006
7007 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7008 vcpu->arch.exception.pending)
7009 goto emulation_error;
ea953ef0 7010
8d76c49e
GN
7011 if (vcpu->arch.halt_request) {
7012 vcpu->arch.halt_request = 0;
5cb56059 7013 ret = kvm_vcpu_halt(vcpu);
8d76c49e
GN
7014 goto out;
7015 }
7016
ea953ef0 7017 if (signal_pending(current))
80ced186 7018 goto out;
ea953ef0
MG
7019 if (need_resched())
7020 schedule();
7021 }
7022
80ced186
MG
7023out:
7024 return ret;
299f0328
SC
7025
7026emulation_error:
7027 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7028 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7029 vcpu->run->internal.ndata = 0;
7030 return 0;
ea953ef0
MG
7031}
7032
b4a2d31d
RK
7033static int __grow_ple_window(int val)
7034{
7035 if (ple_window_grow < 1)
7036 return ple_window;
7037
7038 val = min(val, ple_window_actual_max);
7039
7040 if (ple_window_grow < ple_window)
7041 val *= ple_window_grow;
7042 else
7043 val += ple_window_grow;
7044
7045 return val;
7046}
7047
7048static int __shrink_ple_window(int val, int modifier, int minimum)
7049{
7050 if (modifier < 1)
7051 return ple_window;
7052
7053 if (modifier < ple_window)
7054 val /= modifier;
7055 else
7056 val -= modifier;
7057
7058 return max(val, minimum);
7059}
7060
7061static void grow_ple_window(struct kvm_vcpu *vcpu)
7062{
7063 struct vcpu_vmx *vmx = to_vmx(vcpu);
7064 int old = vmx->ple_window;
7065
7066 vmx->ple_window = __grow_ple_window(old);
7067
7068 if (vmx->ple_window != old)
7069 vmx->ple_window_dirty = true;
7b46268d
RK
7070
7071 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
b4a2d31d
RK
7072}
7073
7074static void shrink_ple_window(struct kvm_vcpu *vcpu)
7075{
7076 struct vcpu_vmx *vmx = to_vmx(vcpu);
7077 int old = vmx->ple_window;
7078
7079 vmx->ple_window = __shrink_ple_window(old,
7080 ple_window_shrink, ple_window);
7081
7082 if (vmx->ple_window != old)
7083 vmx->ple_window_dirty = true;
7b46268d
RK
7084
7085 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
b4a2d31d
RK
7086}
7087
7088/*
7089 * ple_window_actual_max is computed to be one grow_ple_window() below
7090 * ple_window_max. (See __grow_ple_window for the reason.)
7091 * This prevents overflows, because ple_window_max is int.
7092 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
7093 * this process.
7094 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
7095 */
7096static void update_ple_window_actual_max(void)
7097{
7098 ple_window_actual_max =
7099 __shrink_ple_window(max(ple_window_max, ple_window),
7100 ple_window_grow, INT_MIN);
7101}
7102
bf9f6ac8
FW
7103/*
7104 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7105 */
7106static void wakeup_handler(void)
7107{
7108 struct kvm_vcpu *vcpu;
7109 int cpu = smp_processor_id();
7110
7111 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7112 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7113 blocked_vcpu_list) {
7114 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7115
7116 if (pi_test_on(pi_desc) == 1)
7117 kvm_vcpu_kick(vcpu);
7118 }
7119 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7120}
7121
f160c7b7
JS
7122void vmx_enable_tdp(void)
7123{
7124 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7125 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7126 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7127 0ull, VMX_EPT_EXECUTABLE_MASK,
7128 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
d0ec49d4 7129 VMX_EPT_RWX_MASK, 0ull);
f160c7b7
JS
7130
7131 ept_set_mmio_spte_mask();
7132 kvm_enable_tdp();
7133}
7134
f2c7648d
TC
7135static __init int hardware_setup(void)
7136{
4b0be90f 7137 int r = -ENOMEM, i;
34a1cd60
TC
7138
7139 rdmsrl_safe(MSR_EFER, &host_efer);
7140
7141 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7142 kvm_define_shared_msr(i, vmx_msr_index[i]);
7143
23611332
RK
7144 for (i = 0; i < VMX_BITMAP_NR; i++) {
7145 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7146 if (!vmx_bitmap[i])
7147 goto out;
7148 }
34a1cd60 7149
34a1cd60
TC
7150 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7151 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7152
34a1cd60 7153 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
34a1cd60
TC
7154
7155 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
7156
34a1cd60
TC
7157 if (setup_vmcs_config(&vmcs_config) < 0) {
7158 r = -EIO;
23611332 7159 goto out;
baa03522 7160 }
f2c7648d
TC
7161
7162 if (boot_cpu_has(X86_FEATURE_NX))
7163 kvm_enable_efer_bits(EFER_NX);
7164
08d839c4
WL
7165 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7166 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
f2c7648d 7167 enable_vpid = 0;
08d839c4 7168
f2c7648d
TC
7169 if (!cpu_has_vmx_shadow_vmcs())
7170 enable_shadow_vmcs = 0;
7171 if (enable_shadow_vmcs)
7172 init_vmcs_shadow_fields();
7173
7174 if (!cpu_has_vmx_ept() ||
42aa53b4 7175 !cpu_has_vmx_ept_4levels() ||
f5f51586 7176 !cpu_has_vmx_ept_mt_wb() ||
8ad8182e 7177 !cpu_has_vmx_invept_global())
f2c7648d 7178 enable_ept = 0;
f2c7648d 7179
fce6ac4c 7180 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
f2c7648d
TC
7181 enable_ept_ad_bits = 0;
7182
8ad8182e 7183 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
f2c7648d
TC
7184 enable_unrestricted_guest = 0;
7185
ad15a296 7186 if (!cpu_has_vmx_flexpriority())
f2c7648d
TC
7187 flexpriority_enabled = 0;
7188
d02fcf50
PB
7189 if (!cpu_has_virtual_nmis())
7190 enable_vnmi = 0;
7191
ad15a296
PB
7192 /*
7193 * set_apic_access_page_addr() is used to reload apic access
7194 * page upon invalidation. No need to do anything if not
7195 * using the APIC_ACCESS_ADDR VMCS field.
7196 */
7197 if (!flexpriority_enabled)
f2c7648d 7198 kvm_x86_ops->set_apic_access_page_addr = NULL;
f2c7648d
TC
7199
7200 if (!cpu_has_vmx_tpr_shadow())
7201 kvm_x86_ops->update_cr8_intercept = NULL;
7202
7203 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7204 kvm_disable_largepages();
7205
0f107682 7206 if (!cpu_has_vmx_ple()) {
f2c7648d 7207 ple_gap = 0;
0f107682
WL
7208 ple_window = 0;
7209 ple_window_grow = 0;
7210 ple_window_max = 0;
7211 ple_window_shrink = 0;
7212 }
f2c7648d 7213
76dfafd5 7214 if (!cpu_has_vmx_apicv()) {
f2c7648d 7215 enable_apicv = 0;
76dfafd5
PB
7216 kvm_x86_ops->sync_pir_to_irr = NULL;
7217 }
f2c7648d 7218
64903d61
HZ
7219 if (cpu_has_vmx_tsc_scaling()) {
7220 kvm_has_tsc_control = true;
7221 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7222 kvm_tsc_scaling_ratio_frac_bits = 48;
7223 }
7224
04bb92e4
WL
7225 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7226
f160c7b7
JS
7227 if (enable_ept)
7228 vmx_enable_tdp();
7229 else
baa03522
TC
7230 kvm_disable_tdp();
7231
7232 update_ple_window_actual_max();
7233
843e4330
KH
7234 /*
7235 * Only enable PML when hardware supports PML feature, and both EPT
7236 * and EPT A/D bit features are enabled -- PML depends on them to work.
7237 */
7238 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7239 enable_pml = 0;
7240
7241 if (!enable_pml) {
7242 kvm_x86_ops->slot_enable_log_dirty = NULL;
7243 kvm_x86_ops->slot_disable_log_dirty = NULL;
7244 kvm_x86_ops->flush_log_dirty = NULL;
7245 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7246 }
7247
64672c95
YJ
7248 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7249 u64 vmx_msr;
7250
7251 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7252 cpu_preemption_timer_multi =
7253 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7254 } else {
7255 kvm_x86_ops->set_hv_timer = NULL;
7256 kvm_x86_ops->cancel_hv_timer = NULL;
7257 }
7258
bf9f6ac8
FW
7259 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
7260
c45dcc71
AR
7261 kvm_mce_cap_supported |= MCG_LMCE_P;
7262
f2c7648d 7263 return alloc_kvm_area();
34a1cd60 7264
34a1cd60 7265out:
23611332
RK
7266 for (i = 0; i < VMX_BITMAP_NR; i++)
7267 free_page((unsigned long)vmx_bitmap[i]);
34a1cd60
TC
7268
7269 return r;
f2c7648d
TC
7270}
7271
7272static __exit void hardware_unsetup(void)
7273{
23611332
RK
7274 int i;
7275
7276 for (i = 0; i < VMX_BITMAP_NR; i++)
7277 free_page((unsigned long)vmx_bitmap[i]);
34a1cd60 7278
f2c7648d
TC
7279 free_kvm_area();
7280}
7281
4b8d54f9
ZE
7282/*
7283 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7284 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7285 */
9fb41ba8 7286static int handle_pause(struct kvm_vcpu *vcpu)
4b8d54f9 7287{
b4a2d31d
RK
7288 if (ple_gap)
7289 grow_ple_window(vcpu);
7290
de63ad4c
LM
7291 /*
7292 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7293 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7294 * never set PAUSE_EXITING and just set PLE if supported,
7295 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7296 */
7297 kvm_vcpu_on_spin(vcpu, true);
6affcbed 7298 return kvm_skip_emulated_instruction(vcpu);
4b8d54f9
ZE
7299}
7300
87c00572 7301static int handle_nop(struct kvm_vcpu *vcpu)
59708670 7302{
6affcbed 7303 return kvm_skip_emulated_instruction(vcpu);
59708670
SY
7304}
7305
87c00572
GS
7306static int handle_mwait(struct kvm_vcpu *vcpu)
7307{
7308 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7309 return handle_nop(vcpu);
7310}
7311
45ec368c
JM
7312static int handle_invalid_op(struct kvm_vcpu *vcpu)
7313{
7314 kvm_queue_exception(vcpu, UD_VECTOR);
7315 return 1;
7316}
7317
5f3d45e7
MD
7318static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7319{
7320 return 1;
7321}
7322
87c00572
GS
7323static int handle_monitor(struct kvm_vcpu *vcpu)
7324{
7325 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7326 return handle_nop(vcpu);
7327}
7328
0658fbaa
ACL
7329/*
7330 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7331 * set the success or error code of an emulated VMX instruction, as specified
7332 * by Vol 2B, VMX Instruction Reference, "Conventions".
7333 */
7334static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7335{
7336 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7337 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7338 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7339}
7340
7341static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7342{
7343 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7344 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7345 X86_EFLAGS_SF | X86_EFLAGS_OF))
7346 | X86_EFLAGS_CF);
7347}
7348
145c28dd 7349static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
0658fbaa
ACL
7350 u32 vm_instruction_error)
7351{
7352 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7353 /*
7354 * failValid writes the error number to the current VMCS, which
7355 * can't be done there isn't a current VMCS.
7356 */
7357 nested_vmx_failInvalid(vcpu);
7358 return;
7359 }
7360 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7361 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7362 X86_EFLAGS_SF | X86_EFLAGS_OF))
7363 | X86_EFLAGS_ZF);
7364 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7365 /*
7366 * We don't need to force a shadow sync because
7367 * VM_INSTRUCTION_ERROR is not shadowed
7368 */
7369}
145c28dd 7370
ff651cb6
WV
7371static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7372{
7373 /* TODO: not to reset guest simply here. */
7374 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
bbe41b95 7375 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
ff651cb6
WV
7376}
7377
f4124500
JK
7378static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7379{
7380 struct vcpu_vmx *vmx =
7381 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7382
7383 vmx->nested.preemption_timer_expired = true;
7384 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7385 kvm_vcpu_kick(&vmx->vcpu);
7386
7387 return HRTIMER_NORESTART;
7388}
7389
19677e32
BD
7390/*
7391 * Decode the memory-address operand of a vmx instruction, as recorded on an
7392 * exit caused by such an instruction (run by a guest hypervisor).
7393 * On success, returns 0. When the operand is invalid, returns 1 and throws
7394 * #UD or #GP.
7395 */
7396static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7397 unsigned long exit_qualification,
f9eb4af6 7398 u32 vmx_instruction_info, bool wr, gva_t *ret)
19677e32 7399{
f9eb4af6
EK
7400 gva_t off;
7401 bool exn;
7402 struct kvm_segment s;
7403
19677e32
BD
7404 /*
7405 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7406 * Execution", on an exit, vmx_instruction_info holds most of the
7407 * addressing components of the operand. Only the displacement part
7408 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7409 * For how an actual address is calculated from all these components,
7410 * refer to Vol. 1, "Operand Addressing".
7411 */
7412 int scaling = vmx_instruction_info & 3;
7413 int addr_size = (vmx_instruction_info >> 7) & 7;
7414 bool is_reg = vmx_instruction_info & (1u << 10);
7415 int seg_reg = (vmx_instruction_info >> 15) & 7;
7416 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7417 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7418 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7419 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7420
7421 if (is_reg) {
7422 kvm_queue_exception(vcpu, UD_VECTOR);
7423 return 1;
7424 }
7425
7426 /* Addr = segment_base + offset */
7427 /* offset = base + [index * scale] + displacement */
f9eb4af6 7428 off = exit_qualification; /* holds the displacement */
19677e32 7429 if (base_is_valid)
f9eb4af6 7430 off += kvm_register_read(vcpu, base_reg);
19677e32 7431 if (index_is_valid)
f9eb4af6
EK
7432 off += kvm_register_read(vcpu, index_reg)<<scaling;
7433 vmx_get_segment(vcpu, &s, seg_reg);
7434 *ret = s.base + off;
19677e32
BD
7435
7436 if (addr_size == 1) /* 32 bit */
7437 *ret &= 0xffffffff;
7438
f9eb4af6
EK
7439 /* Checks for #GP/#SS exceptions. */
7440 exn = false;
ff30ef40
QC
7441 if (is_long_mode(vcpu)) {
7442 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7443 * non-canonical form. This is the only check on the memory
7444 * destination for long mode!
7445 */
fd8cb433 7446 exn = is_noncanonical_address(*ret, vcpu);
ff30ef40 7447 } else if (is_protmode(vcpu)) {
f9eb4af6
EK
7448 /* Protected mode: apply checks for segment validity in the
7449 * following order:
7450 * - segment type check (#GP(0) may be thrown)
7451 * - usability check (#GP(0)/#SS(0))
7452 * - limit check (#GP(0)/#SS(0))
7453 */
7454 if (wr)
7455 /* #GP(0) if the destination operand is located in a
7456 * read-only data segment or any code segment.
7457 */
7458 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7459 else
7460 /* #GP(0) if the source operand is located in an
7461 * execute-only code segment
7462 */
7463 exn = ((s.type & 0xa) == 8);
ff30ef40
QC
7464 if (exn) {
7465 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7466 return 1;
7467 }
f9eb4af6
EK
7468 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7469 */
7470 exn = (s.unusable != 0);
7471 /* Protected mode: #GP(0)/#SS(0) if the memory
7472 * operand is outside the segment limit.
7473 */
7474 exn = exn || (off + sizeof(u64) > s.limit);
7475 }
7476 if (exn) {
7477 kvm_queue_exception_e(vcpu,
7478 seg_reg == VCPU_SREG_SS ?
7479 SS_VECTOR : GP_VECTOR,
7480 0);
7481 return 1;
7482 }
7483
19677e32
BD
7484 return 0;
7485}
7486
cbf71279 7487static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
3573e22c
BD
7488{
7489 gva_t gva;
3573e22c 7490 struct x86_exception e;
3573e22c
BD
7491
7492 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
f9eb4af6 7493 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
3573e22c
BD
7494 return 1;
7495
40d2dba3 7496 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
3573e22c
BD
7497 kvm_inject_page_fault(vcpu, &e);
7498 return 1;
7499 }
7500
3573e22c
BD
7501 return 0;
7502}
7503
e29acc55
JM
7504static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7505{
7506 struct vcpu_vmx *vmx = to_vmx(vcpu);
7507 struct vmcs *shadow_vmcs;
b6d7026d 7508 int r;
e29acc55 7509
b6d7026d
PB
7510 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7511 if (r < 0)
8819227c 7512 goto out_vmcs02;
8819227c 7513
e29acc55
JM
7514 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7515 if (!vmx->nested.cached_vmcs12)
7516 goto out_cached_vmcs12;
7517
7518 if (enable_shadow_vmcs) {
7519 shadow_vmcs = alloc_vmcs();
7520 if (!shadow_vmcs)
7521 goto out_shadow_vmcs;
7522 /* mark vmcs as shadow */
7523 shadow_vmcs->revision_id |= (1u << 31);
7524 /* init shadow vmcs */
7525 vmcs_clear(shadow_vmcs);
7526 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7527 }
7528
e29acc55
JM
7529 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7530 HRTIMER_MODE_REL_PINNED);
7531 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7532
7533 vmx->nested.vmxon = true;
7534 return 0;
7535
7536out_shadow_vmcs:
7537 kfree(vmx->nested.cached_vmcs12);
7538
7539out_cached_vmcs12:
8819227c
JM
7540 free_loaded_vmcs(&vmx->nested.vmcs02);
7541
7542out_vmcs02:
e29acc55
JM
7543 return -ENOMEM;
7544}
7545
ec378aee
NHE
7546/*
7547 * Emulate the VMXON instruction.
7548 * Currently, we just remember that VMX is active, and do not save or even
7549 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7550 * do not currently need to store anything in that guest-allocated memory
7551 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7552 * argument is different from the VMXON pointer (which the spec says they do).
7553 */
7554static int handle_vmon(struct kvm_vcpu *vcpu)
7555{
e29acc55 7556 int ret;
cbf71279
RK
7557 gpa_t vmptr;
7558 struct page *page;
ec378aee 7559 struct vcpu_vmx *vmx = to_vmx(vcpu);
b3897a49
NHE
7560 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7561 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
ec378aee 7562
70f3aac9
JM
7563 /*
7564 * The Intel VMX Instruction Reference lists a bunch of bits that are
7565 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7566 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7567 * Otherwise, we should fail with #UD. But most faulting conditions
7568 * have already been checked by hardware, prior to the VM-exit for
7569 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7570 * that bit set to 1 in non-root mode.
ec378aee 7571 */
70f3aac9 7572 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
ec378aee
NHE
7573 kvm_queue_exception(vcpu, UD_VECTOR);
7574 return 1;
7575 }
7576
a325262f
FW
7577 /* CPL=0 must be checked manually. */
7578 if (vmx_get_cpl(vcpu)) {
7579 kvm_queue_exception(vcpu, UD_VECTOR);
7580 return 1;
7581 }
7582
145c28dd
AG
7583 if (vmx->nested.vmxon) {
7584 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
6affcbed 7585 return kvm_skip_emulated_instruction(vcpu);
145c28dd 7586 }
b3897a49 7587
3b84080b 7588 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
b3897a49
NHE
7589 != VMXON_NEEDED_FEATURES) {
7590 kvm_inject_gp(vcpu, 0);
7591 return 1;
7592 }
7593
cbf71279 7594 if (nested_vmx_get_vmptr(vcpu, &vmptr))
21e7fbe7 7595 return 1;
cbf71279
RK
7596
7597 /*
7598 * SDM 3: 24.11.5
7599 * The first 4 bytes of VMXON region contain the supported
7600 * VMCS revision identifier
7601 *
7602 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7603 * which replaces physical address width with 32
7604 */
7605 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7606 nested_vmx_failInvalid(vcpu);
7607 return kvm_skip_emulated_instruction(vcpu);
7608 }
7609
5e2f30b7
DH
7610 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7611 if (is_error_page(page)) {
cbf71279
RK
7612 nested_vmx_failInvalid(vcpu);
7613 return kvm_skip_emulated_instruction(vcpu);
7614 }
7615 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7616 kunmap(page);
53a70daf 7617 kvm_release_page_clean(page);
cbf71279
RK
7618 nested_vmx_failInvalid(vcpu);
7619 return kvm_skip_emulated_instruction(vcpu);
7620 }
7621 kunmap(page);
53a70daf 7622 kvm_release_page_clean(page);
cbf71279
RK
7623
7624 vmx->nested.vmxon_ptr = vmptr;
e29acc55
JM
7625 ret = enter_vmx_operation(vcpu);
7626 if (ret)
7627 return ret;
ec378aee 7628
a25eb114 7629 nested_vmx_succeed(vcpu);
6affcbed 7630 return kvm_skip_emulated_instruction(vcpu);
ec378aee
NHE
7631}
7632
7633/*
7634 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7635 * for running VMX instructions (except VMXON, whose prerequisites are
7636 * slightly different). It also specifies what exception to inject otherwise.
70f3aac9
JM
7637 * Note that many of these exceptions have priority over VM exits, so they
7638 * don't have to be checked again here.
ec378aee
NHE
7639 */
7640static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7641{
a325262f
FW
7642 if (vmx_get_cpl(vcpu)) {
7643 kvm_queue_exception(vcpu, UD_VECTOR);
7644 return 0;
7645 }
7646
70f3aac9 7647 if (!to_vmx(vcpu)->nested.vmxon) {
ec378aee
NHE
7648 kvm_queue_exception(vcpu, UD_VECTOR);
7649 return 0;
7650 }
ec378aee
NHE
7651 return 1;
7652}
7653
8ca44e88
DM
7654static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7655{
7656 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7657 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7658}
7659
e7953d7f
AG
7660static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7661{
9a2a05b9
PB
7662 if (vmx->nested.current_vmptr == -1ull)
7663 return;
7664
012f83cb 7665 if (enable_shadow_vmcs) {
9a2a05b9
PB
7666 /* copy to memory all shadowed fields in case
7667 they were modified */
7668 copy_shadow_to_vmcs12(vmx);
7669 vmx->nested.sync_shadow_vmcs = false;
8ca44e88 7670 vmx_disable_shadow_vmcs(vmx);
012f83cb 7671 }
705699a1 7672 vmx->nested.posted_intr_nv = -1;
4f2777bc
DM
7673
7674 /* Flush VMCS12 to guest memory */
9f744c59
PB
7675 kvm_vcpu_write_guest_page(&vmx->vcpu,
7676 vmx->nested.current_vmptr >> PAGE_SHIFT,
7677 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4f2777bc 7678
9a2a05b9 7679 vmx->nested.current_vmptr = -1ull;
e7953d7f
AG
7680}
7681
ec378aee
NHE
7682/*
7683 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7684 * just stops using VMX.
7685 */
7686static void free_nested(struct vcpu_vmx *vmx)
7687{
b7455825 7688 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
ec378aee 7689 return;
9a2a05b9 7690
fe192085 7691 hrtimer_cancel(&vmx->nested.preemption_timer);
ec378aee 7692 vmx->nested.vmxon = false;
b7455825 7693 vmx->nested.smm.vmxon = false;
5c614b35 7694 free_vpid(vmx->nested.vpid02);
8ca44e88
DM
7695 vmx->nested.posted_intr_nv = -1;
7696 vmx->nested.current_vmptr = -1ull;
355f4fb1 7697 if (enable_shadow_vmcs) {
8ca44e88 7698 vmx_disable_shadow_vmcs(vmx);
355f4fb1
JM
7699 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7700 free_vmcs(vmx->vmcs01.shadow_vmcs);
7701 vmx->vmcs01.shadow_vmcs = NULL;
7702 }
4f2777bc 7703 kfree(vmx->nested.cached_vmcs12);
8819227c 7704 /* Unpin physical memory we referred to in the vmcs02 */
fe3ef05c 7705 if (vmx->nested.apic_access_page) {
53a70daf 7706 kvm_release_page_dirty(vmx->nested.apic_access_page);
48d89b92 7707 vmx->nested.apic_access_page = NULL;
fe3ef05c 7708 }
a7c0b07d 7709 if (vmx->nested.virtual_apic_page) {
53a70daf 7710 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
48d89b92 7711 vmx->nested.virtual_apic_page = NULL;
a7c0b07d 7712 }
705699a1
WV
7713 if (vmx->nested.pi_desc_page) {
7714 kunmap(vmx->nested.pi_desc_page);
53a70daf 7715 kvm_release_page_dirty(vmx->nested.pi_desc_page);
705699a1
WV
7716 vmx->nested.pi_desc_page = NULL;
7717 vmx->nested.pi_desc = NULL;
7718 }
ff2f6fe9 7719
8819227c 7720 free_loaded_vmcs(&vmx->nested.vmcs02);
ec378aee
NHE
7721}
7722
7723/* Emulate the VMXOFF instruction */
7724static int handle_vmoff(struct kvm_vcpu *vcpu)
7725{
7726 if (!nested_vmx_check_permission(vcpu))
7727 return 1;
7728 free_nested(to_vmx(vcpu));
a25eb114 7729 nested_vmx_succeed(vcpu);
6affcbed 7730 return kvm_skip_emulated_instruction(vcpu);
ec378aee
NHE
7731}
7732
27d6c865
NHE
7733/* Emulate the VMCLEAR instruction */
7734static int handle_vmclear(struct kvm_vcpu *vcpu)
7735{
7736 struct vcpu_vmx *vmx = to_vmx(vcpu);
587d7e72 7737 u32 zero = 0;
27d6c865 7738 gpa_t vmptr;
27d6c865
NHE
7739
7740 if (!nested_vmx_check_permission(vcpu))
7741 return 1;
7742
cbf71279 7743 if (nested_vmx_get_vmptr(vcpu, &vmptr))
27d6c865 7744 return 1;
27d6c865 7745
cbf71279
RK
7746 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7747 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7748 return kvm_skip_emulated_instruction(vcpu);
7749 }
7750
7751 if (vmptr == vmx->nested.vmxon_ptr) {
7752 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7753 return kvm_skip_emulated_instruction(vcpu);
7754 }
7755
9a2a05b9 7756 if (vmptr == vmx->nested.current_vmptr)
e7953d7f 7757 nested_release_vmcs12(vmx);
27d6c865 7758
587d7e72
JM
7759 kvm_vcpu_write_guest(vcpu,
7760 vmptr + offsetof(struct vmcs12, launch_state),
7761 &zero, sizeof(zero));
27d6c865 7762
27d6c865 7763 nested_vmx_succeed(vcpu);
6affcbed 7764 return kvm_skip_emulated_instruction(vcpu);
27d6c865
NHE
7765}
7766
cd232ad0
NHE
7767static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7768
7769/* Emulate the VMLAUNCH instruction */
7770static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7771{
7772 return nested_vmx_run(vcpu, true);
7773}
7774
7775/* Emulate the VMRESUME instruction */
7776static int handle_vmresume(struct kvm_vcpu *vcpu)
7777{
7778
7779 return nested_vmx_run(vcpu, false);
7780}
7781
49f705c5
NHE
7782/*
7783 * Read a vmcs12 field. Since these can have varying lengths and we return
7784 * one type, we chose the biggest type (u64) and zero-extend the return value
7785 * to that size. Note that the caller, handle_vmread, might need to use only
7786 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7787 * 64-bit fields are to be returned).
7788 */
a2ae9df7
PB
7789static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7790 unsigned long field, u64 *ret)
49f705c5
NHE
7791{
7792 short offset = vmcs_field_to_offset(field);
7793 char *p;
7794
7795 if (offset < 0)
a2ae9df7 7796 return offset;
49f705c5
NHE
7797
7798 p = ((char *)(get_vmcs12(vcpu))) + offset;
7799
7800 switch (vmcs_field_type(field)) {
7801 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7802 *ret = *((natural_width *)p);
a2ae9df7 7803 return 0;
49f705c5
NHE
7804 case VMCS_FIELD_TYPE_U16:
7805 *ret = *((u16 *)p);
a2ae9df7 7806 return 0;
49f705c5
NHE
7807 case VMCS_FIELD_TYPE_U32:
7808 *ret = *((u32 *)p);
a2ae9df7 7809 return 0;
49f705c5
NHE
7810 case VMCS_FIELD_TYPE_U64:
7811 *ret = *((u64 *)p);
a2ae9df7 7812 return 0;
49f705c5 7813 default:
a2ae9df7
PB
7814 WARN_ON(1);
7815 return -ENOENT;
49f705c5
NHE
7816 }
7817}
7818
20b97fea 7819
a2ae9df7
PB
7820static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7821 unsigned long field, u64 field_value){
20b97fea
AG
7822 short offset = vmcs_field_to_offset(field);
7823 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7824 if (offset < 0)
a2ae9df7 7825 return offset;
20b97fea
AG
7826
7827 switch (vmcs_field_type(field)) {
7828 case VMCS_FIELD_TYPE_U16:
7829 *(u16 *)p = field_value;
a2ae9df7 7830 return 0;
20b97fea
AG
7831 case VMCS_FIELD_TYPE_U32:
7832 *(u32 *)p = field_value;
a2ae9df7 7833 return 0;
20b97fea
AG
7834 case VMCS_FIELD_TYPE_U64:
7835 *(u64 *)p = field_value;
a2ae9df7 7836 return 0;
20b97fea
AG
7837 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7838 *(natural_width *)p = field_value;
a2ae9df7 7839 return 0;
20b97fea 7840 default:
a2ae9df7
PB
7841 WARN_ON(1);
7842 return -ENOENT;
20b97fea
AG
7843 }
7844
7845}
7846
16f5b903
AG
7847static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7848{
7849 int i;
7850 unsigned long field;
7851 u64 field_value;
355f4fb1 7852 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
c2bae893
MK
7853 const unsigned long *fields = shadow_read_write_fields;
7854 const int num_fields = max_shadow_read_write_fields;
16f5b903 7855
282da870
JK
7856 preempt_disable();
7857
16f5b903
AG
7858 vmcs_load(shadow_vmcs);
7859
7860 for (i = 0; i < num_fields; i++) {
7861 field = fields[i];
7862 switch (vmcs_field_type(field)) {
7863 case VMCS_FIELD_TYPE_U16:
7864 field_value = vmcs_read16(field);
7865 break;
7866 case VMCS_FIELD_TYPE_U32:
7867 field_value = vmcs_read32(field);
7868 break;
7869 case VMCS_FIELD_TYPE_U64:
7870 field_value = vmcs_read64(field);
7871 break;
7872 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7873 field_value = vmcs_readl(field);
7874 break;
a2ae9df7
PB
7875 default:
7876 WARN_ON(1);
7877 continue;
16f5b903
AG
7878 }
7879 vmcs12_write_any(&vmx->vcpu, field, field_value);
7880 }
7881
7882 vmcs_clear(shadow_vmcs);
7883 vmcs_load(vmx->loaded_vmcs->vmcs);
282da870
JK
7884
7885 preempt_enable();
16f5b903
AG
7886}
7887
c3114420
AG
7888static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7889{
c2bae893
MK
7890 const unsigned long *fields[] = {
7891 shadow_read_write_fields,
7892 shadow_read_only_fields
c3114420 7893 };
c2bae893 7894 const int max_fields[] = {
c3114420
AG
7895 max_shadow_read_write_fields,
7896 max_shadow_read_only_fields
7897 };
7898 int i, q;
7899 unsigned long field;
7900 u64 field_value = 0;
355f4fb1 7901 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
c3114420
AG
7902
7903 vmcs_load(shadow_vmcs);
7904
c2bae893 7905 for (q = 0; q < ARRAY_SIZE(fields); q++) {
c3114420
AG
7906 for (i = 0; i < max_fields[q]; i++) {
7907 field = fields[q][i];
7908 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7909
7910 switch (vmcs_field_type(field)) {
7911 case VMCS_FIELD_TYPE_U16:
7912 vmcs_write16(field, (u16)field_value);
7913 break;
7914 case VMCS_FIELD_TYPE_U32:
7915 vmcs_write32(field, (u32)field_value);
7916 break;
7917 case VMCS_FIELD_TYPE_U64:
7918 vmcs_write64(field, (u64)field_value);
7919 break;
7920 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7921 vmcs_writel(field, (long)field_value);
7922 break;
a2ae9df7
PB
7923 default:
7924 WARN_ON(1);
7925 break;
c3114420
AG
7926 }
7927 }
7928 }
7929
7930 vmcs_clear(shadow_vmcs);
7931 vmcs_load(vmx->loaded_vmcs->vmcs);
7932}
7933
49f705c5
NHE
7934/*
7935 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7936 * used before) all generate the same failure when it is missing.
7937 */
7938static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7939{
7940 struct vcpu_vmx *vmx = to_vmx(vcpu);
7941 if (vmx->nested.current_vmptr == -1ull) {
7942 nested_vmx_failInvalid(vcpu);
49f705c5
NHE
7943 return 0;
7944 }
7945 return 1;
7946}
7947
7948static int handle_vmread(struct kvm_vcpu *vcpu)
7949{
7950 unsigned long field;
7951 u64 field_value;
7952 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7953 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7954 gva_t gva = 0;
7955
eb277562 7956 if (!nested_vmx_check_permission(vcpu))
49f705c5
NHE
7957 return 1;
7958
6affcbed
KH
7959 if (!nested_vmx_check_vmcs12(vcpu))
7960 return kvm_skip_emulated_instruction(vcpu);
49f705c5
NHE
7961
7962 /* Decode instruction info and find the field to read */
27e6fb5d 7963 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
49f705c5 7964 /* Read the field, zero-extended to a u64 field_value */
a2ae9df7 7965 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
49f705c5 7966 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6affcbed 7967 return kvm_skip_emulated_instruction(vcpu);
49f705c5
NHE
7968 }
7969 /*
7970 * Now copy part of this value to register or memory, as requested.
7971 * Note that the number of bits actually copied is 32 or 64 depending
7972 * on the guest's mode (32 or 64 bit), not on the given field's length.
7973 */
7974 if (vmx_instruction_info & (1u << 10)) {
27e6fb5d 7975 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
49f705c5
NHE
7976 field_value);
7977 } else {
7978 if (get_vmx_mem_address(vcpu, exit_qualification,
f9eb4af6 7979 vmx_instruction_info, true, &gva))
49f705c5 7980 return 1;
a325262f 7981 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
40d2dba3
PB
7982 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7983 (is_long_mode(vcpu) ? 8 : 4), NULL);
49f705c5
NHE
7984 }
7985
7986 nested_vmx_succeed(vcpu);
6affcbed 7987 return kvm_skip_emulated_instruction(vcpu);
49f705c5
NHE
7988}
7989
7990
7991static int handle_vmwrite(struct kvm_vcpu *vcpu)
7992{
7993 unsigned long field;
7994 gva_t gva;
7995 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7996 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
49f705c5
NHE
7997 /* The value to write might be 32 or 64 bits, depending on L1's long
7998 * mode, and eventually we need to write that into a field of several
7999 * possible lengths. The code below first zero-extends the value to 64
6a6256f9 8000 * bit (field_value), and then copies only the appropriate number of
49f705c5
NHE
8001 * bits into the vmcs12 field.
8002 */
8003 u64 field_value = 0;
8004 struct x86_exception e;
8005
eb277562 8006 if (!nested_vmx_check_permission(vcpu))
49f705c5
NHE
8007 return 1;
8008
6affcbed
KH
8009 if (!nested_vmx_check_vmcs12(vcpu))
8010 return kvm_skip_emulated_instruction(vcpu);
eb277562 8011
49f705c5 8012 if (vmx_instruction_info & (1u << 10))
27e6fb5d 8013 field_value = kvm_register_readl(vcpu,
49f705c5
NHE
8014 (((vmx_instruction_info) >> 3) & 0xf));
8015 else {
8016 if (get_vmx_mem_address(vcpu, exit_qualification,
f9eb4af6 8017 vmx_instruction_info, false, &gva))
49f705c5 8018 return 1;
40d2dba3
PB
8019 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8020 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
49f705c5
NHE
8021 kvm_inject_page_fault(vcpu, &e);
8022 return 1;
8023 }
8024 }
8025
8026
27e6fb5d 8027 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
49f705c5
NHE
8028 if (vmcs_field_readonly(field)) {
8029 nested_vmx_failValid(vcpu,
8030 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
6affcbed 8031 return kvm_skip_emulated_instruction(vcpu);
49f705c5
NHE
8032 }
8033
a2ae9df7 8034 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
49f705c5 8035 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6affcbed 8036 return kvm_skip_emulated_instruction(vcpu);
49f705c5
NHE
8037 }
8038
8039 nested_vmx_succeed(vcpu);
6affcbed 8040 return kvm_skip_emulated_instruction(vcpu);
49f705c5
NHE
8041}
8042
a8bc284e
JM
8043static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8044{
8045 vmx->nested.current_vmptr = vmptr;
8046 if (enable_shadow_vmcs) {
8047 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8048 SECONDARY_EXEC_SHADOW_VMCS);
8049 vmcs_write64(VMCS_LINK_POINTER,
8050 __pa(vmx->vmcs01.shadow_vmcs));
8051 vmx->nested.sync_shadow_vmcs = true;
8052 }
8053}
8054
63846663
NHE
8055/* Emulate the VMPTRLD instruction */
8056static int handle_vmptrld(struct kvm_vcpu *vcpu)
8057{
8058 struct vcpu_vmx *vmx = to_vmx(vcpu);
63846663 8059 gpa_t vmptr;
63846663
NHE
8060
8061 if (!nested_vmx_check_permission(vcpu))
8062 return 1;
8063
cbf71279 8064 if (nested_vmx_get_vmptr(vcpu, &vmptr))
63846663 8065 return 1;
63846663 8066
cbf71279
RK
8067 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8068 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8069 return kvm_skip_emulated_instruction(vcpu);
8070 }
8071
8072 if (vmptr == vmx->nested.vmxon_ptr) {
8073 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8074 return kvm_skip_emulated_instruction(vcpu);
8075 }
8076
63846663
NHE
8077 if (vmx->nested.current_vmptr != vmptr) {
8078 struct vmcs12 *new_vmcs12;
8079 struct page *page;
5e2f30b7
DH
8080 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8081 if (is_error_page(page)) {
63846663 8082 nested_vmx_failInvalid(vcpu);
6affcbed 8083 return kvm_skip_emulated_instruction(vcpu);
63846663
NHE
8084 }
8085 new_vmcs12 = kmap(page);
8086 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8087 kunmap(page);
53a70daf 8088 kvm_release_page_clean(page);
63846663
NHE
8089 nested_vmx_failValid(vcpu,
8090 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
6affcbed 8091 return kvm_skip_emulated_instruction(vcpu);
63846663 8092 }
63846663 8093
9a2a05b9 8094 nested_release_vmcs12(vmx);
4f2777bc
DM
8095 /*
8096 * Load VMCS12 from guest memory since it is not already
8097 * cached.
8098 */
9f744c59
PB
8099 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8100 kunmap(page);
53a70daf 8101 kvm_release_page_clean(page);
9f744c59 8102
a8bc284e 8103 set_current_vmptr(vmx, vmptr);
63846663
NHE
8104 }
8105
8106 nested_vmx_succeed(vcpu);
6affcbed 8107 return kvm_skip_emulated_instruction(vcpu);
63846663
NHE
8108}
8109
6a4d7550
NHE
8110/* Emulate the VMPTRST instruction */
8111static int handle_vmptrst(struct kvm_vcpu *vcpu)
8112{
8113 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8114 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8115 gva_t vmcs_gva;
8116 struct x86_exception e;
8117
8118 if (!nested_vmx_check_permission(vcpu))
8119 return 1;
8120
8121 if (get_vmx_mem_address(vcpu, exit_qualification,
f9eb4af6 8122 vmx_instruction_info, true, &vmcs_gva))
6a4d7550 8123 return 1;
a325262f 8124 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
40d2dba3
PB
8125 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8126 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8127 sizeof(u64), &e)) {
6a4d7550
NHE
8128 kvm_inject_page_fault(vcpu, &e);
8129 return 1;
8130 }
8131 nested_vmx_succeed(vcpu);
6affcbed 8132 return kvm_skip_emulated_instruction(vcpu);
6a4d7550
NHE
8133}
8134
bfd0a56b
NHE
8135/* Emulate the INVEPT instruction */
8136static int handle_invept(struct kvm_vcpu *vcpu)
8137{
b9c237bb 8138 struct vcpu_vmx *vmx = to_vmx(vcpu);
bfd0a56b
NHE
8139 u32 vmx_instruction_info, types;
8140 unsigned long type;
8141 gva_t gva;
8142 struct x86_exception e;
8143 struct {
8144 u64 eptp, gpa;
8145 } operand;
bfd0a56b 8146
b9c237bb
WV
8147 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
8148 SECONDARY_EXEC_ENABLE_EPT) ||
8149 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
bfd0a56b
NHE
8150 kvm_queue_exception(vcpu, UD_VECTOR);
8151 return 1;
8152 }
8153
8154 if (!nested_vmx_check_permission(vcpu))
8155 return 1;
8156
bfd0a56b 8157 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
27e6fb5d 8158 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
bfd0a56b 8159
b9c237bb 8160 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
bfd0a56b 8161
85c856b3 8162 if (type >= 32 || !(types & (1 << type))) {
bfd0a56b
NHE
8163 nested_vmx_failValid(vcpu,
8164 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
6affcbed 8165 return kvm_skip_emulated_instruction(vcpu);
bfd0a56b
NHE
8166 }
8167
8168 /* According to the Intel VMX instruction reference, the memory
8169 * operand is read even if it isn't needed (e.g., for type==global)
8170 */
8171 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
f9eb4af6 8172 vmx_instruction_info, false, &gva))
bfd0a56b 8173 return 1;
40d2dba3 8174 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
bfd0a56b
NHE
8175 kvm_inject_page_fault(vcpu, &e);
8176 return 1;
8177 }
8178
8179 switch (type) {
bfd0a56b 8180 case VMX_EPT_EXTENT_GLOBAL:
45e11817
BD
8181 /*
8182 * TODO: track mappings and invalidate
8183 * single context requests appropriately
8184 */
8185 case VMX_EPT_EXTENT_CONTEXT:
bfd0a56b 8186 kvm_mmu_sync_roots(vcpu);
77c3913b 8187 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
bfd0a56b
NHE
8188 nested_vmx_succeed(vcpu);
8189 break;
8190 default:
8191 BUG_ON(1);
8192 break;
8193 }
8194
6affcbed 8195 return kvm_skip_emulated_instruction(vcpu);
bfd0a56b
NHE
8196}
8197
a642fc30
PM
8198static int handle_invvpid(struct kvm_vcpu *vcpu)
8199{
99b83ac8
WL
8200 struct vcpu_vmx *vmx = to_vmx(vcpu);
8201 u32 vmx_instruction_info;
8202 unsigned long type, types;
8203 gva_t gva;
8204 struct x86_exception e;
40352605
JM
8205 struct {
8206 u64 vpid;
8207 u64 gla;
8208 } operand;
99b83ac8
WL
8209
8210 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
8211 SECONDARY_EXEC_ENABLE_VPID) ||
8212 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
8213 kvm_queue_exception(vcpu, UD_VECTOR);
8214 return 1;
8215 }
8216
8217 if (!nested_vmx_check_permission(vcpu))
8218 return 1;
8219
8220 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8221 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8222
bcdde302
JD
8223 types = (vmx->nested.nested_vmx_vpid_caps &
8224 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
99b83ac8 8225
85c856b3 8226 if (type >= 32 || !(types & (1 << type))) {
99b83ac8
WL
8227 nested_vmx_failValid(vcpu,
8228 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
6affcbed 8229 return kvm_skip_emulated_instruction(vcpu);
99b83ac8
WL
8230 }
8231
8232 /* according to the intel vmx instruction reference, the memory
8233 * operand is read even if it isn't needed (e.g., for type==global)
8234 */
8235 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8236 vmx_instruction_info, false, &gva))
8237 return 1;
40d2dba3 8238 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
99b83ac8
WL
8239 kvm_inject_page_fault(vcpu, &e);
8240 return 1;
8241 }
40352605
JM
8242 if (operand.vpid >> 16) {
8243 nested_vmx_failValid(vcpu,
8244 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8245 return kvm_skip_emulated_instruction(vcpu);
8246 }
99b83ac8
WL
8247
8248 switch (type) {
bcdde302 8249 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
fd8cb433 8250 if (is_noncanonical_address(operand.gla, vcpu)) {
40352605
JM
8251 nested_vmx_failValid(vcpu,
8252 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8253 return kvm_skip_emulated_instruction(vcpu);
8254 }
8255 /* fall through */
ef697a71 8256 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
bcdde302 8257 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
40352605 8258 if (!operand.vpid) {
bcdde302
JD
8259 nested_vmx_failValid(vcpu,
8260 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
6affcbed 8261 return kvm_skip_emulated_instruction(vcpu);
bcdde302
JD
8262 }
8263 break;
99b83ac8 8264 case VMX_VPID_EXTENT_ALL_CONTEXT:
99b83ac8
WL
8265 break;
8266 default:
bcdde302 8267 WARN_ON_ONCE(1);
6affcbed 8268 return kvm_skip_emulated_instruction(vcpu);
99b83ac8
WL
8269 }
8270
bcdde302
JD
8271 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
8272 nested_vmx_succeed(vcpu);
8273
6affcbed 8274 return kvm_skip_emulated_instruction(vcpu);
a642fc30
PM
8275}
8276
843e4330
KH
8277static int handle_pml_full(struct kvm_vcpu *vcpu)
8278{
8279 unsigned long exit_qualification;
8280
8281 trace_kvm_pml_full(vcpu->vcpu_id);
8282
8283 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8284
8285 /*
8286 * PML buffer FULL happened while executing iret from NMI,
8287 * "blocked by NMI" bit has to be set before next VM entry.
8288 */
8289 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
d02fcf50 8290 enable_vnmi &&
843e4330
KH
8291 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8292 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8293 GUEST_INTR_STATE_NMI);
8294
8295 /*
8296 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8297 * here.., and there's no userspace involvement needed for PML.
8298 */
8299 return 1;
8300}
8301
64672c95
YJ
8302static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8303{
8304 kvm_lapic_expired_hv_timer(vcpu);
8305 return 1;
8306}
8307
41ab9372
BD
8308static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8309{
8310 struct vcpu_vmx *vmx = to_vmx(vcpu);
41ab9372
BD
8311 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8312
8313 /* Check for memory type validity */
bb97a016
DH
8314 switch (address & VMX_EPTP_MT_MASK) {
8315 case VMX_EPTP_MT_UC:
41ab9372
BD
8316 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT))
8317 return false;
8318 break;
bb97a016 8319 case VMX_EPTP_MT_WB:
41ab9372
BD
8320 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT))
8321 return false;
8322 break;
8323 default:
8324 return false;
8325 }
8326
bb97a016
DH
8327 /* only 4 levels page-walk length are valid */
8328 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
41ab9372
BD
8329 return false;
8330
8331 /* Reserved bits should not be set */
8332 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8333 return false;
8334
8335 /* AD, if set, should be supported */
bb97a016 8336 if (address & VMX_EPTP_AD_ENABLE_BIT) {
41ab9372
BD
8337 if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT))
8338 return false;
8339 }
8340
8341 return true;
8342}
8343
8344static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8345 struct vmcs12 *vmcs12)
8346{
8347 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8348 u64 address;
8349 bool accessed_dirty;
8350 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8351
8352 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8353 !nested_cpu_has_ept(vmcs12))
8354 return 1;
8355
8356 if (index >= VMFUNC_EPTP_ENTRIES)
8357 return 1;
8358
8359
8360 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8361 &address, index * 8, 8))
8362 return 1;
8363
bb97a016 8364 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
41ab9372
BD
8365
8366 /*
8367 * If the (L2) guest does a vmfunc to the currently
8368 * active ept pointer, we don't have to do anything else
8369 */
8370 if (vmcs12->ept_pointer != address) {
8371 if (!valid_ept_address(vcpu, address))
8372 return 1;
8373
8374 kvm_mmu_unload(vcpu);
8375 mmu->ept_ad = accessed_dirty;
8376 mmu->base_role.ad_disabled = !accessed_dirty;
8377 vmcs12->ept_pointer = address;
8378 /*
8379 * TODO: Check what's the correct approach in case
8380 * mmu reload fails. Currently, we just let the next
8381 * reload potentially fail
8382 */
8383 kvm_mmu_reload(vcpu);
8384 }
8385
8386 return 0;
8387}
8388
2a499e49
BD
8389static int handle_vmfunc(struct kvm_vcpu *vcpu)
8390{
27c42a1b
BD
8391 struct vcpu_vmx *vmx = to_vmx(vcpu);
8392 struct vmcs12 *vmcs12;
8393 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8394
8395 /*
8396 * VMFUNC is only supported for nested guests, but we always enable the
8397 * secondary control for simplicity; for non-nested mode, fake that we
8398 * didn't by injecting #UD.
8399 */
8400 if (!is_guest_mode(vcpu)) {
8401 kvm_queue_exception(vcpu, UD_VECTOR);
8402 return 1;
8403 }
8404
8405 vmcs12 = get_vmcs12(vcpu);
8406 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8407 goto fail;
41ab9372
BD
8408
8409 switch (function) {
8410 case 0:
8411 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8412 goto fail;
8413 break;
8414 default:
8415 goto fail;
8416 }
8417 return kvm_skip_emulated_instruction(vcpu);
27c42a1b
BD
8418
8419fail:
8420 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8421 vmcs_read32(VM_EXIT_INTR_INFO),
8422 vmcs_readl(EXIT_QUALIFICATION));
2a499e49
BD
8423 return 1;
8424}
8425
6aa8b732
AK
8426/*
8427 * The exit handlers return 1 if the exit was handled fully and guest execution
8428 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8429 * to be done to userspace and return 0.
8430 */
772e0318 8431static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
6aa8b732
AK
8432 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8433 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
988ad74f 8434 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
f08864b4 8435 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
6aa8b732 8436 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
6aa8b732
AK
8437 [EXIT_REASON_CR_ACCESS] = handle_cr,
8438 [EXIT_REASON_DR_ACCESS] = handle_dr,
8439 [EXIT_REASON_CPUID] = handle_cpuid,
8440 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8441 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8442 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8443 [EXIT_REASON_HLT] = handle_halt,
ec25d5e6 8444 [EXIT_REASON_INVD] = handle_invd,
a7052897 8445 [EXIT_REASON_INVLPG] = handle_invlpg,
fee84b07 8446 [EXIT_REASON_RDPMC] = handle_rdpmc,
c21415e8 8447 [EXIT_REASON_VMCALL] = handle_vmcall,
27d6c865 8448 [EXIT_REASON_VMCLEAR] = handle_vmclear,
cd232ad0 8449 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
63846663 8450 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
6a4d7550 8451 [EXIT_REASON_VMPTRST] = handle_vmptrst,
49f705c5 8452 [EXIT_REASON_VMREAD] = handle_vmread,
cd232ad0 8453 [EXIT_REASON_VMRESUME] = handle_vmresume,
49f705c5 8454 [EXIT_REASON_VMWRITE] = handle_vmwrite,
ec378aee
NHE
8455 [EXIT_REASON_VMOFF] = handle_vmoff,
8456 [EXIT_REASON_VMON] = handle_vmon,
f78e0e2e
SY
8457 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8458 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
83d4c286 8459 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
c7c9c56c 8460 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
e5edaa01 8461 [EXIT_REASON_WBINVD] = handle_wbinvd,
2acf923e 8462 [EXIT_REASON_XSETBV] = handle_xsetbv,
37817f29 8463 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
a0861c02 8464 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
68f89400
MT
8465 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8466 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
4b8d54f9 8467 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
87c00572 8468 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
5f3d45e7 8469 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
87c00572 8470 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
bfd0a56b 8471 [EXIT_REASON_INVEPT] = handle_invept,
a642fc30 8472 [EXIT_REASON_INVVPID] = handle_invvpid,
45ec368c 8473 [EXIT_REASON_RDRAND] = handle_invalid_op,
75f4fc8d 8474 [EXIT_REASON_RDSEED] = handle_invalid_op,
f53cd63c
WL
8475 [EXIT_REASON_XSAVES] = handle_xsaves,
8476 [EXIT_REASON_XRSTORS] = handle_xrstors,
843e4330 8477 [EXIT_REASON_PML_FULL] = handle_pml_full,
2a499e49 8478 [EXIT_REASON_VMFUNC] = handle_vmfunc,
64672c95 8479 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
6aa8b732
AK
8480};
8481
8482static const int kvm_vmx_max_exit_handlers =
50a3485c 8483 ARRAY_SIZE(kvm_vmx_exit_handlers);
6aa8b732 8484
908a7bdd
JK
8485static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8486 struct vmcs12 *vmcs12)
8487{
8488 unsigned long exit_qualification;
8489 gpa_t bitmap, last_bitmap;
8490 unsigned int port;
8491 int size;
8492 u8 b;
8493
908a7bdd 8494 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
2f0a6397 8495 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
908a7bdd
JK
8496
8497 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8498
8499 port = exit_qualification >> 16;
8500 size = (exit_qualification & 7) + 1;
8501
8502 last_bitmap = (gpa_t)-1;
8503 b = -1;
8504
8505 while (size > 0) {
8506 if (port < 0x8000)
8507 bitmap = vmcs12->io_bitmap_a;
8508 else if (port < 0x10000)
8509 bitmap = vmcs12->io_bitmap_b;
8510 else
1d804d07 8511 return true;
908a7bdd
JK
8512 bitmap += (port & 0x7fff) / 8;
8513
8514 if (last_bitmap != bitmap)
54bf36aa 8515 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
1d804d07 8516 return true;
908a7bdd 8517 if (b & (1 << (port & 7)))
1d804d07 8518 return true;
908a7bdd
JK
8519
8520 port++;
8521 size--;
8522 last_bitmap = bitmap;
8523 }
8524
1d804d07 8525 return false;
908a7bdd
JK
8526}
8527
644d711a
NHE
8528/*
8529 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8530 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8531 * disinterest in the current event (read or write a specific MSR) by using an
8532 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8533 */
8534static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8535 struct vmcs12 *vmcs12, u32 exit_reason)
8536{
8537 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8538 gpa_t bitmap;
8539
cbd29cb6 8540 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
1d804d07 8541 return true;
644d711a
NHE
8542
8543 /*
8544 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8545 * for the four combinations of read/write and low/high MSR numbers.
8546 * First we need to figure out which of the four to use:
8547 */
8548 bitmap = vmcs12->msr_bitmap;
8549 if (exit_reason == EXIT_REASON_MSR_WRITE)
8550 bitmap += 2048;
8551 if (msr_index >= 0xc0000000) {
8552 msr_index -= 0xc0000000;
8553 bitmap += 1024;
8554 }
8555
8556 /* Then read the msr_index'th bit from this bitmap: */
8557 if (msr_index < 1024*8) {
8558 unsigned char b;
54bf36aa 8559 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
1d804d07 8560 return true;
644d711a
NHE
8561 return 1 & (b >> (msr_index & 7));
8562 } else
1d804d07 8563 return true; /* let L1 handle the wrong parameter */
644d711a
NHE
8564}
8565
8566/*
8567 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8568 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8569 * intercept (via guest_host_mask etc.) the current event.
8570 */
8571static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8572 struct vmcs12 *vmcs12)
8573{
8574 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8575 int cr = exit_qualification & 15;
e1d39b17
JS
8576 int reg;
8577 unsigned long val;
644d711a
NHE
8578
8579 switch ((exit_qualification >> 4) & 3) {
8580 case 0: /* mov to cr */
e1d39b17
JS
8581 reg = (exit_qualification >> 8) & 15;
8582 val = kvm_register_readl(vcpu, reg);
644d711a
NHE
8583 switch (cr) {
8584 case 0:
8585 if (vmcs12->cr0_guest_host_mask &
8586 (val ^ vmcs12->cr0_read_shadow))
1d804d07 8587 return true;
644d711a
NHE
8588 break;
8589 case 3:
8590 if ((vmcs12->cr3_target_count >= 1 &&
8591 vmcs12->cr3_target_value0 == val) ||
8592 (vmcs12->cr3_target_count >= 2 &&
8593 vmcs12->cr3_target_value1 == val) ||
8594 (vmcs12->cr3_target_count >= 3 &&
8595 vmcs12->cr3_target_value2 == val) ||
8596 (vmcs12->cr3_target_count >= 4 &&
8597 vmcs12->cr3_target_value3 == val))
1d804d07 8598 return false;
644d711a 8599 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
1d804d07 8600 return true;
644d711a
NHE
8601 break;
8602 case 4:
8603 if (vmcs12->cr4_guest_host_mask &
8604 (vmcs12->cr4_read_shadow ^ val))
1d804d07 8605 return true;
644d711a
NHE
8606 break;
8607 case 8:
8608 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
1d804d07 8609 return true;
644d711a
NHE
8610 break;
8611 }
8612 break;
8613 case 2: /* clts */
8614 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8615 (vmcs12->cr0_read_shadow & X86_CR0_TS))
1d804d07 8616 return true;
644d711a
NHE
8617 break;
8618 case 1: /* mov from cr */
8619 switch (cr) {
8620 case 3:
8621 if (vmcs12->cpu_based_vm_exec_control &
8622 CPU_BASED_CR3_STORE_EXITING)
1d804d07 8623 return true;
644d711a
NHE
8624 break;
8625 case 8:
8626 if (vmcs12->cpu_based_vm_exec_control &
8627 CPU_BASED_CR8_STORE_EXITING)
1d804d07 8628 return true;
644d711a
NHE
8629 break;
8630 }
8631 break;
8632 case 3: /* lmsw */
8633 /*
8634 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8635 * cr0. Other attempted changes are ignored, with no exit.
8636 */
e1d39b17 8637 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
644d711a
NHE
8638 if (vmcs12->cr0_guest_host_mask & 0xe &
8639 (val ^ vmcs12->cr0_read_shadow))
1d804d07 8640 return true;
644d711a
NHE
8641 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8642 !(vmcs12->cr0_read_shadow & 0x1) &&
8643 (val & 0x1))
1d804d07 8644 return true;
644d711a
NHE
8645 break;
8646 }
1d804d07 8647 return false;
644d711a
NHE
8648}
8649
8650/*
8651 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8652 * should handle it ourselves in L0 (and then continue L2). Only call this
8653 * when in is_guest_mode (L2).
8654 */
7313c698 8655static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
644d711a 8656{
644d711a
NHE
8657 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8658 struct vcpu_vmx *vmx = to_vmx(vcpu);
8659 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8660
4f350c6d
JM
8661 if (vmx->nested.nested_run_pending)
8662 return false;
8663
8664 if (unlikely(vmx->fail)) {
8665 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8666 vmcs_read32(VM_INSTRUCTION_ERROR));
8667 return true;
8668 }
542060ea 8669
c9f04407
DM
8670 /*
8671 * The host physical addresses of some pages of guest memory
8819227c
JM
8672 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8673 * Page). The CPU may write to these pages via their host
8674 * physical address while L2 is running, bypassing any
8675 * address-translation-based dirty tracking (e.g. EPT write
8676 * protection).
c9f04407
DM
8677 *
8678 * Mark them dirty on every exit from L2 to prevent them from
8679 * getting out of sync with dirty tracking.
8680 */
8681 nested_mark_vmcs12_pages_dirty(vcpu);
8682
4f350c6d
JM
8683 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8684 vmcs_readl(EXIT_QUALIFICATION),
8685 vmx->idt_vectoring_info,
8686 intr_info,
8687 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8688 KVM_ISA_VMX);
644d711a
NHE
8689
8690 switch (exit_reason) {
8691 case EXIT_REASON_EXCEPTION_NMI:
ef85b673 8692 if (is_nmi(intr_info))
1d804d07 8693 return false;
644d711a 8694 else if (is_page_fault(intr_info))
52a5c155 8695 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
e504c909 8696 else if (is_no_device(intr_info) &&
ccf9844e 8697 !(vmcs12->guest_cr0 & X86_CR0_TS))
1d804d07 8698 return false;
6f05485d
JK
8699 else if (is_debug(intr_info) &&
8700 vcpu->guest_debug &
8701 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8702 return false;
8703 else if (is_breakpoint(intr_info) &&
8704 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8705 return false;
644d711a
NHE
8706 return vmcs12->exception_bitmap &
8707 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8708 case EXIT_REASON_EXTERNAL_INTERRUPT:
1d804d07 8709 return false;
644d711a 8710 case EXIT_REASON_TRIPLE_FAULT:
1d804d07 8711 return true;
644d711a 8712 case EXIT_REASON_PENDING_INTERRUPT:
3b656cf7 8713 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
644d711a 8714 case EXIT_REASON_NMI_WINDOW:
3b656cf7 8715 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
644d711a 8716 case EXIT_REASON_TASK_SWITCH:
1d804d07 8717 return true;
644d711a 8718 case EXIT_REASON_CPUID:
1d804d07 8719 return true;
644d711a
NHE
8720 case EXIT_REASON_HLT:
8721 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8722 case EXIT_REASON_INVD:
1d804d07 8723 return true;
644d711a
NHE
8724 case EXIT_REASON_INVLPG:
8725 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8726 case EXIT_REASON_RDPMC:
8727 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
a5f46457 8728 case EXIT_REASON_RDRAND:
736fdf72 8729 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
a5f46457 8730 case EXIT_REASON_RDSEED:
736fdf72 8731 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
b3a2a907 8732 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
644d711a
NHE
8733 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8734 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8735 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8736 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8737 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8738 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
a642fc30 8739 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
644d711a
NHE
8740 /*
8741 * VMX instructions trap unconditionally. This allows L1 to
8742 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8743 */
1d804d07 8744 return true;
644d711a
NHE
8745 case EXIT_REASON_CR_ACCESS:
8746 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8747 case EXIT_REASON_DR_ACCESS:
8748 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8749 case EXIT_REASON_IO_INSTRUCTION:
908a7bdd 8750 return nested_vmx_exit_handled_io(vcpu, vmcs12);
1b07304c
PB
8751 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8752 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
644d711a
NHE
8753 case EXIT_REASON_MSR_READ:
8754 case EXIT_REASON_MSR_WRITE:
8755 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8756 case EXIT_REASON_INVALID_STATE:
1d804d07 8757 return true;
644d711a
NHE
8758 case EXIT_REASON_MWAIT_INSTRUCTION:
8759 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5f3d45e7
MD
8760 case EXIT_REASON_MONITOR_TRAP_FLAG:
8761 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
644d711a
NHE
8762 case EXIT_REASON_MONITOR_INSTRUCTION:
8763 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8764 case EXIT_REASON_PAUSE_INSTRUCTION:
8765 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8766 nested_cpu_has2(vmcs12,
8767 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8768 case EXIT_REASON_MCE_DURING_VMENTRY:
1d804d07 8769 return false;
644d711a 8770 case EXIT_REASON_TPR_BELOW_THRESHOLD:
a7c0b07d 8771 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
644d711a
NHE
8772 case EXIT_REASON_APIC_ACCESS:
8773 return nested_cpu_has2(vmcs12,
8774 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
82f0dd4b 8775 case EXIT_REASON_APIC_WRITE:
608406e2
WV
8776 case EXIT_REASON_EOI_INDUCED:
8777 /* apic_write and eoi_induced should exit unconditionally. */
1d804d07 8778 return true;
644d711a 8779 case EXIT_REASON_EPT_VIOLATION:
2b1be677
NHE
8780 /*
8781 * L0 always deals with the EPT violation. If nested EPT is
8782 * used, and the nested mmu code discovers that the address is
8783 * missing in the guest EPT table (EPT12), the EPT violation
8784 * will be injected with nested_ept_inject_page_fault()
8785 */
1d804d07 8786 return false;
644d711a 8787 case EXIT_REASON_EPT_MISCONFIG:
2b1be677
NHE
8788 /*
8789 * L2 never uses directly L1's EPT, but rather L0's own EPT
8790 * table (shadow on EPT) or a merged EPT table that L0 built
8791 * (EPT on EPT). So any problems with the structure of the
8792 * table is L0's fault.
8793 */
1d804d07 8794 return false;
90a2db6d
PB
8795 case EXIT_REASON_INVPCID:
8796 return
8797 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8798 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
644d711a
NHE
8799 case EXIT_REASON_WBINVD:
8800 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8801 case EXIT_REASON_XSETBV:
1d804d07 8802 return true;
81dc01f7
WL
8803 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8804 /*
8805 * This should never happen, since it is not possible to
8806 * set XSS to a non-zero value---neither in L1 nor in L2.
8807 * If if it were, XSS would have to be checked against
8808 * the XSS exit bitmap in vmcs12.
8809 */
8810 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
55123e3c
WL
8811 case EXIT_REASON_PREEMPTION_TIMER:
8812 return false;
ab007cc9 8813 case EXIT_REASON_PML_FULL:
03efce6f 8814 /* We emulate PML support to L1. */
ab007cc9 8815 return false;
2a499e49
BD
8816 case EXIT_REASON_VMFUNC:
8817 /* VM functions are emulated through L2->L0 vmexits. */
8818 return false;
644d711a 8819 default:
1d804d07 8820 return true;
644d711a
NHE
8821 }
8822}
8823
7313c698
PB
8824static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8825{
8826 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8827
8828 /*
8829 * At this point, the exit interruption info in exit_intr_info
8830 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8831 * we need to query the in-kernel LAPIC.
8832 */
8833 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8834 if ((exit_intr_info &
8835 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8836 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8837 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8838 vmcs12->vm_exit_intr_error_code =
8839 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8840 }
8841
8842 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8843 vmcs_readl(EXIT_QUALIFICATION));
8844 return 1;
8845}
8846
586f9607
AK
8847static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8848{
8849 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8850 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8851}
8852
a3eaa864 8853static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
843e4330 8854{
a3eaa864
KH
8855 if (vmx->pml_pg) {
8856 __free_page(vmx->pml_pg);
8857 vmx->pml_pg = NULL;
8858 }
843e4330
KH
8859}
8860
54bf36aa 8861static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
843e4330 8862{
54bf36aa 8863 struct vcpu_vmx *vmx = to_vmx(vcpu);
843e4330
KH
8864 u64 *pml_buf;
8865 u16 pml_idx;
8866
8867 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8868
8869 /* Do nothing if PML buffer is empty */
8870 if (pml_idx == (PML_ENTITY_NUM - 1))
8871 return;
8872
8873 /* PML index always points to next available PML buffer entity */
8874 if (pml_idx >= PML_ENTITY_NUM)
8875 pml_idx = 0;
8876 else
8877 pml_idx++;
8878
8879 pml_buf = page_address(vmx->pml_pg);
8880 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8881 u64 gpa;
8882
8883 gpa = pml_buf[pml_idx];
8884 WARN_ON(gpa & (PAGE_SIZE - 1));
54bf36aa 8885 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
843e4330
KH
8886 }
8887
8888 /* reset PML index */
8889 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8890}
8891
8892/*
8893 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8894 * Called before reporting dirty_bitmap to userspace.
8895 */
8896static void kvm_flush_pml_buffers(struct kvm *kvm)
8897{
8898 int i;
8899 struct kvm_vcpu *vcpu;
8900 /*
8901 * We only need to kick vcpu out of guest mode here, as PML buffer
8902 * is flushed at beginning of all VMEXITs, and it's obvious that only
8903 * vcpus running in guest are possible to have unflushed GPAs in PML
8904 * buffer.
8905 */
8906 kvm_for_each_vcpu(i, vcpu, kvm)
8907 kvm_vcpu_kick(vcpu);
8908}
8909
4eb64dce
PB
8910static void vmx_dump_sel(char *name, uint32_t sel)
8911{
8912 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
96794e4e 8913 name, vmcs_read16(sel),
4eb64dce
PB
8914 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8915 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8916 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8917}
8918
8919static void vmx_dump_dtsel(char *name, uint32_t limit)
8920{
8921 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8922 name, vmcs_read32(limit),
8923 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8924}
8925
8926static void dump_vmcs(void)
8927{
8928 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8929 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8930 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8931 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8932 u32 secondary_exec_control = 0;
8933 unsigned long cr4 = vmcs_readl(GUEST_CR4);
f3531054 8934 u64 efer = vmcs_read64(GUEST_IA32_EFER);
4eb64dce
PB
8935 int i, n;
8936
8937 if (cpu_has_secondary_exec_ctrls())
8938 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8939
8940 pr_err("*** Guest State ***\n");
8941 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8942 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8943 vmcs_readl(CR0_GUEST_HOST_MASK));
8944 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8945 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8946 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8947 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8948 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8949 {
845c5b40
PB
8950 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8951 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8952 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8953 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
4eb64dce
PB
8954 }
8955 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8956 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8957 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8958 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8959 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8960 vmcs_readl(GUEST_SYSENTER_ESP),
8961 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8962 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8963 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8964 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8965 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8966 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8967 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8968 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8969 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8970 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8971 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8972 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8973 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
845c5b40
PB
8974 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8975 efer, vmcs_read64(GUEST_IA32_PAT));
8976 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8977 vmcs_read64(GUEST_IA32_DEBUGCTL),
4eb64dce
PB
8978 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8979 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
845c5b40
PB
8980 pr_err("PerfGlobCtl = 0x%016llx\n",
8981 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
4eb64dce 8982 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
845c5b40 8983 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
4eb64dce
PB
8984 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8985 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8986 vmcs_read32(GUEST_ACTIVITY_STATE));
8987 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8988 pr_err("InterruptStatus = %04x\n",
8989 vmcs_read16(GUEST_INTR_STATUS));
8990
8991 pr_err("*** Host State ***\n");
8992 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8993 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8994 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8995 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8996 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8997 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8998 vmcs_read16(HOST_TR_SELECTOR));
8999 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9000 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9001 vmcs_readl(HOST_TR_BASE));
9002 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9003 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9004 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9005 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9006 vmcs_readl(HOST_CR4));
9007 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9008 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9009 vmcs_read32(HOST_IA32_SYSENTER_CS),
9010 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9011 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
845c5b40
PB
9012 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9013 vmcs_read64(HOST_IA32_EFER),
9014 vmcs_read64(HOST_IA32_PAT));
4eb64dce 9015 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
845c5b40
PB
9016 pr_err("PerfGlobCtl = 0x%016llx\n",
9017 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
4eb64dce
PB
9018
9019 pr_err("*** Control State ***\n");
9020 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9021 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9022 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9023 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9024 vmcs_read32(EXCEPTION_BITMAP),
9025 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9026 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9027 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9028 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9029 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9030 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9031 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9032 vmcs_read32(VM_EXIT_INTR_INFO),
9033 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9034 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9035 pr_err(" reason=%08x qualification=%016lx\n",
9036 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9037 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9038 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9039 vmcs_read32(IDT_VECTORING_ERROR_CODE));
845c5b40 9040 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
8cfe9866 9041 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
845c5b40
PB
9042 pr_err("TSC Multiplier = 0x%016llx\n",
9043 vmcs_read64(TSC_MULTIPLIER));
4eb64dce
PB
9044 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9045 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9046 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9047 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9048 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
845c5b40 9049 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
4eb64dce
PB
9050 n = vmcs_read32(CR3_TARGET_COUNT);
9051 for (i = 0; i + 1 < n; i += 4)
9052 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9053 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9054 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9055 if (i < n)
9056 pr_err("CR3 target%u=%016lx\n",
9057 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9058 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9059 pr_err("PLE Gap=%08x Window=%08x\n",
9060 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9061 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9062 pr_err("Virtual processor ID = 0x%04x\n",
9063 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9064}
9065
6aa8b732
AK
9066/*
9067 * The guest has exited. See if we can fix it or if we need userspace
9068 * assistance.
9069 */
851ba692 9070static int vmx_handle_exit(struct kvm_vcpu *vcpu)
6aa8b732 9071{
29bd8a78 9072 struct vcpu_vmx *vmx = to_vmx(vcpu);
a0861c02 9073 u32 exit_reason = vmx->exit_reason;
1155f76a 9074 u32 vectoring_info = vmx->idt_vectoring_info;
29bd8a78 9075
8b89fe1f
PB
9076 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9077
843e4330
KH
9078 /*
9079 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9080 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9081 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9082 * mode as if vcpus is in root mode, the PML buffer must has been
9083 * flushed already.
9084 */
9085 if (enable_pml)
54bf36aa 9086 vmx_flush_pml_buffer(vcpu);
843e4330 9087
80ced186 9088 /* If guest state is invalid, start emulating */
14168786 9089 if (vmx->emulation_required)
80ced186 9090 return handle_invalid_guest_state(vcpu);
1d5a4d9b 9091
7313c698
PB
9092 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9093 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
644d711a 9094
5120702e 9095 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
4eb64dce 9096 dump_vmcs();
5120702e
MG
9097 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9098 vcpu->run->fail_entry.hardware_entry_failure_reason
9099 = exit_reason;
9100 return 0;
9101 }
9102
29bd8a78 9103 if (unlikely(vmx->fail)) {
851ba692
AK
9104 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9105 vcpu->run->fail_entry.hardware_entry_failure_reason
29bd8a78
AK
9106 = vmcs_read32(VM_INSTRUCTION_ERROR);
9107 return 0;
9108 }
6aa8b732 9109
b9bf6882
XG
9110 /*
9111 * Note:
9112 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9113 * delivery event since it indicates guest is accessing MMIO.
9114 * The vm-exit can be triggered again after return to guest that
9115 * will cause infinite loop.
9116 */
d77c26fc 9117 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
1439442c 9118 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
60637aac 9119 exit_reason != EXIT_REASON_EPT_VIOLATION &&
b244c9fc 9120 exit_reason != EXIT_REASON_PML_FULL &&
b9bf6882
XG
9121 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9122 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9123 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
70bcd708 9124 vcpu->run->internal.ndata = 3;
b9bf6882
XG
9125 vcpu->run->internal.data[0] = vectoring_info;
9126 vcpu->run->internal.data[1] = exit_reason;
70bcd708
PB
9127 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9128 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9129 vcpu->run->internal.ndata++;
9130 vcpu->run->internal.data[3] =
9131 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9132 }
b9bf6882
XG
9133 return 0;
9134 }
3b86cd99 9135
d02fcf50 9136 if (unlikely(!enable_vnmi &&
8a1b4392
PB
9137 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9138 if (vmx_interrupt_allowed(vcpu)) {
9139 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9140 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9141 vcpu->arch.nmi_pending) {
9142 /*
9143 * This CPU don't support us in finding the end of an
9144 * NMI-blocked window if the guest runs with IRQs
9145 * disabled. So we pull the trigger after 1 s of
9146 * futile waiting, but inform the user about this.
9147 */
9148 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9149 "state on VCPU %d after 1 s timeout\n",
9150 __func__, vcpu->vcpu_id);
9151 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9152 }
9153 }
9154
6aa8b732
AK
9155 if (exit_reason < kvm_vmx_max_exit_handlers
9156 && kvm_vmx_exit_handlers[exit_reason])
851ba692 9157 return kvm_vmx_exit_handlers[exit_reason](vcpu);
6aa8b732 9158 else {
6c6c5e03
RK
9159 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9160 exit_reason);
2bc19dc3
MT
9161 kvm_queue_exception(vcpu, UD_VECTOR);
9162 return 1;
6aa8b732 9163 }
6aa8b732
AK
9164}
9165
d665f9fc
PB
9166/*
9167 * Software based L1D cache flush which is used when microcode providing
9168 * the cache control MSR is not loaded.
9169 *
9170 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9171 * flush it is required to read in 64 KiB because the replacement algorithm
9172 * is not exactly LRU. This could be sized at runtime via topology
9173 * information but as all relevant affected CPUs have 32KiB L1D cache size
9174 * there is no point in doing so.
9175 */
9176#define L1D_CACHE_ORDER 4
9177static void *vmx_l1d_flush_pages;
9178
f0ace387 9179static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
d665f9fc
PB
9180{
9181 int size = PAGE_SIZE << L1D_CACHE_ORDER;
f0ace387
PB
9182
9183 /*
1384247a
TG
9184 * This code is only executed when the the flush mode is 'cond' or
9185 * 'always'
f0ace387 9186 */
12960b11 9187 if (static_branch_likely(&vmx_l1d_flush_cond)) {
64947f95 9188 bool flush_l1d;
bcbe4077 9189
07edf60a 9190 /*
64947f95
NS
9191 * Clear the per-vcpu flush bit, it gets set again
9192 * either from vcpu_run() or from one of the unsafe
9193 * VMEXIT handlers.
07edf60a 9194 */
64947f95 9195 flush_l1d = vcpu->arch.l1tf_flush_l1d;
ae021965 9196 vcpu->arch.l1tf_flush_l1d = false;
64947f95
NS
9197
9198 /*
9199 * Clear the per-cpu flush bit, it gets set again from
9200 * the interrupt handlers.
9201 */
9202 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
9203 kvm_clear_cpu_l1tf_flush_l1d();
9204
bcbe4077
NS
9205 if (!flush_l1d)
9206 return;
07edf60a 9207 }
f0ace387
PB
9208
9209 vcpu->stat.l1d_flush++;
d665f9fc 9210
8e494dea
PB
9211 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9212 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9213 return;
9214 }
9215
d665f9fc
PB
9216 asm volatile(
9217 /* First ensure the pages are in the TLB */
9218 "xorl %%eax, %%eax\n"
9219 ".Lpopulate_tlb:\n\t"
55bd6950 9220 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
d665f9fc
PB
9221 "addl $4096, %%eax\n\t"
9222 "cmpl %%eax, %[size]\n\t"
9223 "jne .Lpopulate_tlb\n\t"
9224 "xorl %%eax, %%eax\n\t"
9225 "cpuid\n\t"
9226 /* Now fill the cache */
9227 "xorl %%eax, %%eax\n"
9228 ".Lfill_cache:\n"
55bd6950 9229 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
d665f9fc
PB
9230 "addl $64, %%eax\n\t"
9231 "cmpl %%eax, %[size]\n\t"
9232 "jne .Lfill_cache\n\t"
9233 "lfence\n"
55bd6950 9234 :: [flush_pages] "r" (vmx_l1d_flush_pages),
d665f9fc
PB
9235 [size] "r" (size)
9236 : "eax", "ebx", "ecx", "edx");
9237}
9238
95ba8273 9239static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
6e5d865c 9240{
a7c0b07d
WL
9241 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9242
9243 if (is_guest_mode(vcpu) &&
9244 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9245 return;
9246
95ba8273 9247 if (irr == -1 || tpr < irr) {
6e5d865c
YS
9248 vmcs_write32(TPR_THRESHOLD, 0);
9249 return;
9250 }
9251
95ba8273 9252 vmcs_write32(TPR_THRESHOLD, irr);
6e5d865c
YS
9253}
9254
8d14695f
YZ
9255static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9256{
9257 u32 sec_exec_control;
9258
dccbfcf5
RK
9259 /* Postpone execution until vmcs01 is the current VMCS. */
9260 if (is_guest_mode(vcpu)) {
9261 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9262 return;
9263 }
9264
f6e90f9e 9265 if (!cpu_has_vmx_virtualize_x2apic_mode())
8d14695f
YZ
9266 return;
9267
35754c98 9268 if (!cpu_need_tpr_shadow(vcpu))
8d14695f
YZ
9269 return;
9270
9271 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9272
9273 if (set) {
9274 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9275 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9276 } else {
9277 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9278 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5ad2874a 9279 vmx_flush_tlb(vcpu);
8d14695f
YZ
9280 }
9281 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9282
4b0be90f 9283 vmx_update_msr_bitmap(vcpu);
8d14695f
YZ
9284}
9285
38b99173
TC
9286static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9287{
9288 struct vcpu_vmx *vmx = to_vmx(vcpu);
9289
9290 /*
9291 * Currently we do not handle the nested case where L2 has an
9292 * APIC access page of its own; that page is still pinned.
9293 * Hence, we skip the case where the VCPU is in guest mode _and_
9294 * L1 prepared an APIC access page for L2.
9295 *
9296 * For the case where L1 and L2 share the same APIC access page
9297 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9298 * in the vmcs12), this function will only update either the vmcs01
9299 * or the vmcs02. If the former, the vmcs02 will be updated by
9300 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9301 * the next L2->L1 exit.
9302 */
9303 if (!is_guest_mode(vcpu) ||
4f2777bc 9304 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
fb6c8198 9305 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
38b99173 9306 vmcs_write64(APIC_ACCESS_ADDR, hpa);
5ad2874a 9307 vmx_flush_tlb(vcpu);
fb6c8198 9308 }
38b99173
TC
9309}
9310
67c9dddc 9311static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
c7c9c56c
YZ
9312{
9313 u16 status;
9314 u8 old;
9315
67c9dddc
PB
9316 if (max_isr == -1)
9317 max_isr = 0;
c7c9c56c
YZ
9318
9319 status = vmcs_read16(GUEST_INTR_STATUS);
9320 old = status >> 8;
67c9dddc 9321 if (max_isr != old) {
c7c9c56c 9322 status &= 0xff;
67c9dddc 9323 status |= max_isr << 8;
c7c9c56c
YZ
9324 vmcs_write16(GUEST_INTR_STATUS, status);
9325 }
9326}
9327
9328static void vmx_set_rvi(int vector)
9329{
9330 u16 status;
9331 u8 old;
9332
4114c27d
WW
9333 if (vector == -1)
9334 vector = 0;
9335
c7c9c56c
YZ
9336 status = vmcs_read16(GUEST_INTR_STATUS);
9337 old = (u8)status & 0xff;
9338 if ((u8)vector != old) {
9339 status &= ~0xff;
9340 status |= (u8)vector;
9341 vmcs_write16(GUEST_INTR_STATUS, status);
9342 }
9343}
9344
9345static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9346{
4114c27d
WW
9347 if (!is_guest_mode(vcpu)) {
9348 vmx_set_rvi(max_irr);
9349 return;
9350 }
9351
c7c9c56c
YZ
9352 if (max_irr == -1)
9353 return;
9354
963fee16 9355 /*
4114c27d
WW
9356 * In guest mode. If a vmexit is needed, vmx_check_nested_events
9357 * handles it.
963fee16 9358 */
4114c27d 9359 if (nested_exit_on_intr(vcpu))
963fee16
WL
9360 return;
9361
963fee16 9362 /*
4114c27d 9363 * Else, fall back to pre-APICv interrupt injection since L2
963fee16
WL
9364 * is run without virtual interrupt delivery.
9365 */
9366 if (!kvm_event_needs_reinjection(vcpu) &&
9367 vmx_interrupt_allowed(vcpu)) {
9368 kvm_queue_interrupt(vcpu, max_irr, false);
9369 vmx_inject_irq(vcpu);
9370 }
c7c9c56c
YZ
9371}
9372
76dfafd5 9373static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
810e6def
PB
9374{
9375 struct vcpu_vmx *vmx = to_vmx(vcpu);
76dfafd5 9376 int max_irr;
810e6def 9377
76dfafd5
PB
9378 WARN_ON(!vcpu->arch.apicv_active);
9379 if (pi_test_on(&vmx->pi_desc)) {
9380 pi_clear_on(&vmx->pi_desc);
9381 /*
9382 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9383 * But on x86 this is just a compiler barrier anyway.
9384 */
9385 smp_mb__after_atomic();
9386 max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
9387 } else {
9388 max_irr = kvm_lapic_find_highest_irr(vcpu);
9389 }
9390 vmx_hwapic_irr_update(vcpu, max_irr);
9391 return max_irr;
810e6def
PB
9392}
9393
6308630b 9394static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
c7c9c56c 9395{
d62caabb 9396 if (!kvm_vcpu_apicv_active(vcpu))
3d81bc7e
YZ
9397 return;
9398
c7c9c56c
YZ
9399 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9400 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9401 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9402 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9403}
9404
967235d3
PB
9405static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9406{
9407 struct vcpu_vmx *vmx = to_vmx(vcpu);
9408
9409 pi_clear_on(&vmx->pi_desc);
9410 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9411}
9412
51aa01d1 9413static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
cf393f75 9414{
48ae0fb4
JM
9415 u32 exit_intr_info = 0;
9416 u16 basic_exit_reason = (u16)vmx->exit_reason;
00eba012 9417
48ae0fb4
JM
9418 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9419 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
00eba012
AK
9420 return;
9421
48ae0fb4
JM
9422 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9423 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9424 vmx->exit_intr_info = exit_intr_info;
a0861c02 9425
1261bfa3
WL
9426 /* if exit due to PF check for async PF */
9427 if (is_page_fault(exit_intr_info))
9428 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9429
a0861c02 9430 /* Handle machine checks before interrupts are enabled */
48ae0fb4
JM
9431 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9432 is_machine_check(exit_intr_info))
a0861c02
AK
9433 kvm_machine_check();
9434
20f65983 9435 /* We need to handle NMIs before interrupts are enabled */
ef85b673 9436 if (is_nmi(exit_intr_info)) {
ff9d07a0 9437 kvm_before_handle_nmi(&vmx->vcpu);
20f65983 9438 asm("int $2");
ff9d07a0
ZY
9439 kvm_after_handle_nmi(&vmx->vcpu);
9440 }
51aa01d1 9441}
20f65983 9442
a547c6db
YZ
9443static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9444{
9445 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9446
a547c6db
YZ
9447 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9448 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9449 unsigned int vector;
9450 unsigned long entry;
9451 gate_desc *desc;
9452 struct vcpu_vmx *vmx = to_vmx(vcpu);
9453#ifdef CONFIG_X86_64
9454 unsigned long tmp;
9455#endif
9456
9457 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9458 desc = (gate_desc *)vmx->host_idt_base + vector;
64b163fa 9459 entry = gate_offset(desc);
a547c6db
YZ
9460 asm volatile(
9461#ifdef CONFIG_X86_64
9462 "mov %%" _ASM_SP ", %[sp]\n\t"
9463 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9464 "push $%c[ss]\n\t"
9465 "push %[sp]\n\t"
9466#endif
9467 "pushf\n\t"
a547c6db 9468 __ASM_SIZE(push) " $%c[cs]\n\t"
405b60d2 9469 CALL_NOSPEC
a547c6db
YZ
9470 :
9471#ifdef CONFIG_X86_64
3f62de5f 9472 [sp]"=&r"(tmp),
a547c6db 9473#endif
f5caf621 9474 ASM_CALL_CONSTRAINT
a547c6db 9475 :
405b60d2 9476 THUNK_TARGET(entry),
a547c6db
YZ
9477 [ss]"i"(__KERNEL_DS),
9478 [cs]"i"(__KERNEL_CS)
9479 );
f2485b3e 9480 }
a547c6db 9481}
c207aee4 9482STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
a547c6db 9483
4d5c8a07 9484static bool vmx_has_emulated_msr(int index)
6d396b55 9485{
4d5c8a07
TL
9486 switch (index) {
9487 case MSR_IA32_SMBASE:
9488 /*
9489 * We cannot do SMM unless we can run the guest in big
9490 * real mode.
9491 */
9492 return enable_unrestricted_guest || emulate_invalid_guest_state;
9493 case MSR_AMD64_VIRT_SPEC_CTRL:
9494 /* This is AMD only. */
9495 return false;
9496 default:
9497 return true;
9498 }
6d396b55
PB
9499}
9500
da8999d3
LJ
9501static bool vmx_mpx_supported(void)
9502{
9503 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9504 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9505}
9506
55412b2e
WL
9507static bool vmx_xsaves_supported(void)
9508{
9509 return vmcs_config.cpu_based_2nd_exec_ctrl &
9510 SECONDARY_EXEC_XSAVES;
9511}
9512
51aa01d1
AK
9513static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9514{
c5ca8e57 9515 u32 exit_intr_info;
51aa01d1
AK
9516 bool unblock_nmi;
9517 u8 vector;
9518 bool idtv_info_valid;
9519
9520 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
20f65983 9521
d02fcf50 9522 if (enable_vnmi) {
8a1b4392
PB
9523 if (vmx->loaded_vmcs->nmi_known_unmasked)
9524 return;
9525 /*
9526 * Can't use vmx->exit_intr_info since we're not sure what
9527 * the exit reason is.
9528 */
9529 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9530 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9531 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9532 /*
9533 * SDM 3: 27.7.1.2 (September 2008)
9534 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9535 * a guest IRET fault.
9536 * SDM 3: 23.2.2 (September 2008)
9537 * Bit 12 is undefined in any of the following cases:
9538 * If the VM exit sets the valid bit in the IDT-vectoring
9539 * information field.
9540 * If the VM exit is due to a double fault.
9541 */
9542 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9543 vector != DF_VECTOR && !idtv_info_valid)
9544 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9545 GUEST_INTR_STATE_NMI);
9546 else
9547 vmx->loaded_vmcs->nmi_known_unmasked =
9548 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9549 & GUEST_INTR_STATE_NMI);
9550 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9551 vmx->loaded_vmcs->vnmi_blocked_time +=
9552 ktime_to_ns(ktime_sub(ktime_get(),
9553 vmx->loaded_vmcs->entry_time));
51aa01d1
AK
9554}
9555
3ab66e8a 9556static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
83422e17
AK
9557 u32 idt_vectoring_info,
9558 int instr_len_field,
9559 int error_code_field)
51aa01d1 9560{
51aa01d1
AK
9561 u8 vector;
9562 int type;
9563 bool idtv_info_valid;
9564
9565 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
668f612f 9566
3ab66e8a
JK
9567 vcpu->arch.nmi_injected = false;
9568 kvm_clear_exception_queue(vcpu);
9569 kvm_clear_interrupt_queue(vcpu);
37b96e98
GN
9570
9571 if (!idtv_info_valid)
9572 return;
9573
3ab66e8a 9574 kvm_make_request(KVM_REQ_EVENT, vcpu);
3842d135 9575
668f612f
AK
9576 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9577 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
37b96e98 9578
64a7ec06 9579 switch (type) {
37b96e98 9580 case INTR_TYPE_NMI_INTR:
3ab66e8a 9581 vcpu->arch.nmi_injected = true;
668f612f 9582 /*
7b4a25cb 9583 * SDM 3: 27.7.1.2 (September 2008)
37b96e98
GN
9584 * Clear bit "block by NMI" before VM entry if a NMI
9585 * delivery faulted.
668f612f 9586 */
3ab66e8a 9587 vmx_set_nmi_mask(vcpu, false);
37b96e98 9588 break;
37b96e98 9589 case INTR_TYPE_SOFT_EXCEPTION:
3ab66e8a 9590 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
66fd3f7f
GN
9591 /* fall through */
9592 case INTR_TYPE_HARD_EXCEPTION:
35920a35 9593 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
83422e17 9594 u32 err = vmcs_read32(error_code_field);
851eb667 9595 kvm_requeue_exception_e(vcpu, vector, err);
35920a35 9596 } else
851eb667 9597 kvm_requeue_exception(vcpu, vector);
37b96e98 9598 break;
66fd3f7f 9599 case INTR_TYPE_SOFT_INTR:
3ab66e8a 9600 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
66fd3f7f 9601 /* fall through */
37b96e98 9602 case INTR_TYPE_EXT_INTR:
3ab66e8a 9603 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
37b96e98
GN
9604 break;
9605 default:
9606 break;
f7d9238f 9607 }
cf393f75
AK
9608}
9609
83422e17
AK
9610static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9611{
3ab66e8a 9612 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
83422e17
AK
9613 VM_EXIT_INSTRUCTION_LEN,
9614 IDT_VECTORING_ERROR_CODE);
9615}
9616
b463a6f7
AK
9617static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9618{
3ab66e8a 9619 __vmx_complete_interrupts(vcpu,
b463a6f7
AK
9620 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9621 VM_ENTRY_INSTRUCTION_LEN,
9622 VM_ENTRY_EXCEPTION_ERROR_CODE);
9623
9624 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9625}
9626
d7cd9796
GN
9627static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9628{
9629 int i, nr_msrs;
9630 struct perf_guest_switch_msr *msrs;
9631
9632 msrs = perf_guest_get_msrs(&nr_msrs);
9633
9634 if (!msrs)
9635 return;
9636
9637 for (i = 0; i < nr_msrs; i++)
9638 if (msrs[i].host == msrs[i].guest)
9639 clear_atomic_switch_msr(vmx, msrs[i].msr);
9640 else
9641 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
129ce7ac 9642 msrs[i].host, false);
d7cd9796
GN
9643}
9644
33365e7a 9645static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
64672c95
YJ
9646{
9647 struct vcpu_vmx *vmx = to_vmx(vcpu);
9648 u64 tscl;
9649 u32 delta_tsc;
9650
9651 if (vmx->hv_deadline_tsc == -1)
9652 return;
9653
9654 tscl = rdtsc();
9655 if (vmx->hv_deadline_tsc > tscl)
9656 /* sure to be 32 bit only because checked on set_hv_timer */
9657 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9658 cpu_preemption_timer_multi);
9659 else
9660 delta_tsc = 0;
9661
9662 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9663}
9664
a3b5ba49 9665static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
6aa8b732 9666{
a2fa3e9f 9667 struct vcpu_vmx *vmx = to_vmx(vcpu);
d6e41f11 9668 unsigned long debugctlmsr, cr3, cr4;
104f226b 9669
8a1b4392 9670 /* Record the guest's net vcpu time for enforced NMI injections. */
d02fcf50 9671 if (unlikely(!enable_vnmi &&
8a1b4392
PB
9672 vmx->loaded_vmcs->soft_vnmi_blocked))
9673 vmx->loaded_vmcs->entry_time = ktime_get();
9674
104f226b
AK
9675 /* Don't enter VMX if guest state is invalid, let the exit handler
9676 start emulation until we arrive back to a valid state */
14168786 9677 if (vmx->emulation_required)
104f226b
AK
9678 return;
9679
a7653ecd
RK
9680 if (vmx->ple_window_dirty) {
9681 vmx->ple_window_dirty = false;
9682 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9683 }
9684
012f83cb
AG
9685 if (vmx->nested.sync_shadow_vmcs) {
9686 copy_vmcs12_to_shadow(vmx);
9687 vmx->nested.sync_shadow_vmcs = false;
9688 }
9689
104f226b
AK
9690 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9691 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9692 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9693 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9694
d6e41f11 9695 cr3 = __get_current_cr3_fast();
44889942 9696 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
d6e41f11 9697 vmcs_writel(HOST_CR3, cr3);
44889942 9698 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
d6e41f11
AL
9699 }
9700
1e02ce4c 9701 cr4 = cr4_read_shadow();
44889942 9702 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
d974baa3 9703 vmcs_writel(HOST_CR4, cr4);
44889942 9704 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
d974baa3
AL
9705 }
9706
104f226b
AK
9707 /* When single-stepping over STI and MOV SS, we must clear the
9708 * corresponding interruptibility bits in the guest state. Otherwise
9709 * vmentry fails as it then expects bit 14 (BS) in pending debug
9710 * exceptions being set, but that's not correct for the guest debugging
9711 * case. */
9712 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9713 vmx_set_interrupt_shadow(vcpu, 0);
9714
b9dd21e1
PB
9715 if (static_cpu_has(X86_FEATURE_PKU) &&
9716 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9717 vcpu->arch.pkru != vmx->host_pkru)
9718 __write_pkru(vcpu->arch.pkru);
1be0e61c 9719
d7cd9796 9720 atomic_switch_perf_msrs(vmx);
2a7921b7 9721 debugctlmsr = get_debugctlmsr();
d7cd9796 9722
64672c95
YJ
9723 vmx_arm_hv_timer(vcpu);
9724
74469996
KA
9725 /*
9726 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9727 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9728 * is no need to worry about the conditional branch over the wrmsr
9729 * being speculatively taken.
9730 */
692b5d07 9731 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
74469996 9732
d462b819 9733 vmx->__launched = vmx->loaded_vmcs->launched;
f0ace387 9734
c7ed1048 9735 /* L1D Flush includes CPU buffer clear to mitigate MDS */
bcbe4077
NS
9736 if (static_branch_unlikely(&vmx_l1d_should_flush))
9737 vmx_l1d_flush(vcpu);
abf9852b
TG
9738 else if (static_branch_unlikely(&mds_user_clear))
9739 mds_clear_cpu_buffers();
f0ace387 9740
104f226b 9741 asm(
6aa8b732 9742 /* Store host registers */
b188c81f
AK
9743 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9744 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9745 "push %%" _ASM_CX " \n\t"
9746 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
313dbd49 9747 "je 1f \n\t"
b188c81f 9748 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
4ecac3fd 9749 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
313dbd49 9750 "1: \n\t"
d3edefc0 9751 /* Reload cr2 if changed */
b188c81f
AK
9752 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9753 "mov %%cr2, %%" _ASM_DX " \n\t"
9754 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
d3edefc0 9755 "je 2f \n\t"
b188c81f 9756 "mov %%" _ASM_AX", %%cr2 \n\t"
d3edefc0 9757 "2: \n\t"
6aa8b732 9758 /* Check if vmlaunch of vmresume is needed */
e08aa78a 9759 "cmpl $0, %c[launched](%0) \n\t"
6aa8b732 9760 /* Load guest registers. Don't clobber flags. */
b188c81f
AK
9761 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9762 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9763 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9764 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9765 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9766 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
05b3e0c2 9767#ifdef CONFIG_X86_64
e08aa78a
AK
9768 "mov %c[r8](%0), %%r8 \n\t"
9769 "mov %c[r9](%0), %%r9 \n\t"
9770 "mov %c[r10](%0), %%r10 \n\t"
9771 "mov %c[r11](%0), %%r11 \n\t"
9772 "mov %c[r12](%0), %%r12 \n\t"
9773 "mov %c[r13](%0), %%r13 \n\t"
9774 "mov %c[r14](%0), %%r14 \n\t"
9775 "mov %c[r15](%0), %%r15 \n\t"
6aa8b732 9776#endif
b188c81f 9777 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
c801949d 9778
6aa8b732 9779 /* Enter guest mode */
83287ea4 9780 "jne 1f \n\t"
4ecac3fd 9781 __ex(ASM_VMX_VMLAUNCH) "\n\t"
83287ea4
AK
9782 "jmp 2f \n\t"
9783 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9784 "2: "
6aa8b732 9785 /* Save guest registers, load host registers, keep flags */
b188c81f 9786 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
40712fae 9787 "pop %0 \n\t"
0cb5b306 9788 "setbe %c[fail](%0)\n\t"
b188c81f
AK
9789 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9790 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9791 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9792 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9793 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9794 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9795 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
05b3e0c2 9796#ifdef CONFIG_X86_64
e08aa78a
AK
9797 "mov %%r8, %c[r8](%0) \n\t"
9798 "mov %%r9, %c[r9](%0) \n\t"
9799 "mov %%r10, %c[r10](%0) \n\t"
9800 "mov %%r11, %c[r11](%0) \n\t"
9801 "mov %%r12, %c[r12](%0) \n\t"
9802 "mov %%r13, %c[r13](%0) \n\t"
9803 "mov %%r14, %c[r14](%0) \n\t"
9804 "mov %%r15, %c[r15](%0) \n\t"
0cb5b306
JM
9805 "xor %%r8d, %%r8d \n\t"
9806 "xor %%r9d, %%r9d \n\t"
9807 "xor %%r10d, %%r10d \n\t"
9808 "xor %%r11d, %%r11d \n\t"
9809 "xor %%r12d, %%r12d \n\t"
9810 "xor %%r13d, %%r13d \n\t"
9811 "xor %%r14d, %%r14d \n\t"
9812 "xor %%r15d, %%r15d \n\t"
6aa8b732 9813#endif
b188c81f
AK
9814 "mov %%cr2, %%" _ASM_AX " \n\t"
9815 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
c801949d 9816
0cb5b306
JM
9817 "xor %%eax, %%eax \n\t"
9818 "xor %%ebx, %%ebx \n\t"
9819 "xor %%esi, %%esi \n\t"
9820 "xor %%edi, %%edi \n\t"
b188c81f 9821 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
83287ea4
AK
9822 ".pushsection .rodata \n\t"
9823 ".global vmx_return \n\t"
9824 "vmx_return: " _ASM_PTR " 2b \n\t"
9825 ".popsection"
e08aa78a 9826 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
d462b819 9827 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
e08aa78a 9828 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
313dbd49 9829 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
ad312c7c
ZX
9830 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9831 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9832 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9833 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9834 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9835 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9836 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
05b3e0c2 9837#ifdef CONFIG_X86_64
ad312c7c
ZX
9838 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9839 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9840 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9841 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9842 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9843 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9844 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9845 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
6aa8b732 9846#endif
40712fae
AK
9847 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9848 [wordsize]"i"(sizeof(ulong))
c2036300
LV
9849 : "cc", "memory"
9850#ifdef CONFIG_X86_64
b188c81f 9851 , "rax", "rbx", "rdi", "rsi"
c2036300 9852 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
b188c81f
AK
9853#else
9854 , "eax", "ebx", "edi", "esi"
c2036300
LV
9855#endif
9856 );
6aa8b732 9857
74469996
KA
9858 /*
9859 * We do not use IBRS in the kernel. If this vCPU has used the
9860 * SPEC_CTRL MSR it may have left it on; save the value and
9861 * turn it off. This is much more efficient than blindly adding
9862 * it to the atomic save/restore list. Especially as the former
9863 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9864 *
9865 * For non-nested case:
9866 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9867 * save it.
9868 *
9869 * For nested case:
9870 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9871 * save it.
9872 */
481ab71e 9873 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
f471d71b 9874 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
74469996 9875
692b5d07 9876 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
74469996 9877
117cc7a9
DW
9878 /* Eliminate branch target predictions from guest mode */
9879 vmexit_fill_RSB();
9880
2a7921b7
GN
9881 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9882 if (debugctlmsr)
9883 update_debugctlmsr(debugctlmsr);
9884
aa67f609
AK
9885#ifndef CONFIG_X86_64
9886 /*
9887 * The sysexit path does not restore ds/es, so we must set them to
9888 * a reasonable value ourselves.
9889 *
9890 * We can't defer this to vmx_load_host_state() since that function
9891 * may be executed in interrupt context, which saves and restore segments
9892 * around it, nullifying its effect.
9893 */
9894 loadsegment(ds, __USER_DS);
9895 loadsegment(es, __USER_DS);
9896#endif
9897
6de4f3ad 9898 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
6de12732 9899 | (1 << VCPU_EXREG_RFLAGS)
aff48baa 9900 | (1 << VCPU_EXREG_PDPTR)
2fb92db1 9901 | (1 << VCPU_EXREG_SEGMENTS)
aff48baa 9902 | (1 << VCPU_EXREG_CR3));
5fdbf976
MT
9903 vcpu->arch.regs_dirty = 0;
9904
1be0e61c
XG
9905 /*
9906 * eager fpu is enabled if PKEY is supported and CR4 is switched
9907 * back on host, so it is safe to read guest PKRU from current
9908 * XSAVE.
9909 */
b9dd21e1
PB
9910 if (static_cpu_has(X86_FEATURE_PKU) &&
9911 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9912 vcpu->arch.pkru = __read_pkru();
9913 if (vcpu->arch.pkru != vmx->host_pkru)
1be0e61c 9914 __write_pkru(vmx->host_pkru);
1be0e61c
XG
9915 }
9916
e0b890d3
GN
9917 /*
9918 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9919 * we did not inject a still-pending event to L1 now because of
9920 * nested_run_pending, we need to re-enable this bit.
9921 */
9922 if (vmx->nested.nested_run_pending)
9923 kvm_make_request(KVM_REQ_EVENT, vcpu);
9924
9925 vmx->nested.nested_run_pending = 0;
b060ca3b
JM
9926 vmx->idt_vectoring_info = 0;
9927
9928 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9929 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9930 return;
9931
9932 vmx->loaded_vmcs->launched = 1;
9933 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
e0b890d3 9934
51aa01d1
AK
9935 vmx_complete_atomic_exit(vmx);
9936 vmx_recover_nmi_blocking(vmx);
cf393f75 9937 vmx_complete_interrupts(vmx);
6aa8b732 9938}
c207aee4 9939STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
6aa8b732 9940
1279a6b1 9941static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
4fa7734c
PB
9942{
9943 struct vcpu_vmx *vmx = to_vmx(vcpu);
9944 int cpu;
9945
1279a6b1 9946 if (vmx->loaded_vmcs == vmcs)
4fa7734c
PB
9947 return;
9948
9949 cpu = get_cpu();
1279a6b1 9950 vmx->loaded_vmcs = vmcs;
4fa7734c
PB
9951 vmx_vcpu_put(vcpu);
9952 vmx_vcpu_load(vcpu, cpu);
4fa7734c
PB
9953 put_cpu();
9954}
9955
2f1fe811
JM
9956/*
9957 * Ensure that the current vmcs of the logical processor is the
9958 * vmcs01 of the vcpu before calling free_nested().
9959 */
9960static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9961{
9962 struct vcpu_vmx *vmx = to_vmx(vcpu);
9963 int r;
9964
9965 r = vcpu_load(vcpu);
9966 BUG_ON(r);
1279a6b1 9967 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
2f1fe811
JM
9968 free_nested(vmx);
9969 vcpu_put(vcpu);
9970}
9971
6aa8b732
AK
9972static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9973{
fb3f0f51
RR
9974 struct vcpu_vmx *vmx = to_vmx(vcpu);
9975
843e4330 9976 if (enable_pml)
a3eaa864 9977 vmx_destroy_pml_buffer(vmx);
991e7a0e 9978 free_vpid(vmx->vpid);
4fa7734c 9979 leave_guest_mode(vcpu);
2f1fe811 9980 vmx_free_vcpu_nested(vcpu);
4fa7734c 9981 free_loaded_vmcs(vmx->loaded_vmcs);
fb3f0f51
RR
9982 kfree(vmx->guest_msrs);
9983 kvm_vcpu_uninit(vcpu);
a4770347 9984 kmem_cache_free(kvm_vcpu_cache, vmx);
6aa8b732
AK
9985}
9986
fb3f0f51 9987static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
6aa8b732 9988{
fb3f0f51 9989 int err;
c16f862d 9990 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
4b0be90f 9991 unsigned long *msr_bitmap;
15ad7146 9992 int cpu;
6aa8b732 9993
a2fa3e9f 9994 if (!vmx)
fb3f0f51
RR
9995 return ERR_PTR(-ENOMEM);
9996
991e7a0e 9997 vmx->vpid = allocate_vpid();
2384d2b3 9998
fb3f0f51
RR
9999 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10000 if (err)
10001 goto free_vcpu;
965b58a5 10002
4e59516a
PF
10003 err = -ENOMEM;
10004
10005 /*
10006 * If PML is turned on, failure on enabling PML just results in failure
10007 * of creating the vcpu, therefore we can simplify PML logic (by
10008 * avoiding dealing with cases, such as enabling PML partially on vcpus
10009 * for the guest, etc.
10010 */
10011 if (enable_pml) {
10012 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10013 if (!vmx->pml_pg)
10014 goto uninit_vcpu;
10015 }
10016
a2fa3e9f 10017 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
03916db9
PB
10018 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10019 > PAGE_SIZE);
0123be42 10020
4e59516a
PF
10021 if (!vmx->guest_msrs)
10022 goto free_pml;
965b58a5 10023
b6d7026d
PB
10024 err = alloc_loaded_vmcs(&vmx->vmcs01);
10025 if (err < 0)
fb3f0f51 10026 goto free_msrs;
a2fa3e9f 10027
4b0be90f
PB
10028 msr_bitmap = vmx->vmcs01.msr_bitmap;
10029 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10030 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10031 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10032 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10033 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10034 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10035 vmx->msr_bitmap_mode = 0;
10036
b6d7026d 10037 vmx->loaded_vmcs = &vmx->vmcs01;
15ad7146
AK
10038 cpu = get_cpu();
10039 vmx_vcpu_load(&vmx->vcpu, cpu);
e48672fa 10040 vmx->vcpu.cpu = cpu;
12d79917 10041 vmx_vcpu_setup(vmx);
fb3f0f51 10042 vmx_vcpu_put(&vmx->vcpu);
15ad7146 10043 put_cpu();
35754c98 10044 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
be6d05cf
JK
10045 err = alloc_apic_access_page(kvm);
10046 if (err)
5e4a0b3c 10047 goto free_vmcs;
a63cb560 10048 }
fb3f0f51 10049
b927a3ce 10050 if (enable_ept) {
f51770ed
TC
10051 err = init_rmode_identity_map(kvm);
10052 if (err)
93ea5388 10053 goto free_vmcs;
b927a3ce 10054 }
b7ebfb05 10055
5c614b35 10056 if (nested) {
b9c237bb 10057 nested_vmx_setup_ctls_msrs(vmx);
5c614b35
WL
10058 vmx->nested.vpid02 = allocate_vpid();
10059 }
b9c237bb 10060
705699a1 10061 vmx->nested.posted_intr_nv = -1;
a9d30f33 10062 vmx->nested.current_vmptr = -1ull;
a9d30f33 10063
37e4c997
HZ
10064 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10065
31afb2ea
PB
10066 /*
10067 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10068 * or POSTED_INTR_WAKEUP_VECTOR.
10069 */
10070 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10071 vmx->pi_desc.sn = 1;
10072
fb3f0f51
RR
10073 return &vmx->vcpu;
10074
10075free_vmcs:
5c614b35 10076 free_vpid(vmx->nested.vpid02);
5f3fbc34 10077 free_loaded_vmcs(vmx->loaded_vmcs);
fb3f0f51 10078free_msrs:
fb3f0f51 10079 kfree(vmx->guest_msrs);
4e59516a
PF
10080free_pml:
10081 vmx_destroy_pml_buffer(vmx);
fb3f0f51
RR
10082uninit_vcpu:
10083 kvm_vcpu_uninit(&vmx->vcpu);
10084free_vcpu:
991e7a0e 10085 free_vpid(vmx->vpid);
a4770347 10086 kmem_cache_free(kvm_vcpu_cache, vmx);
fb3f0f51 10087 return ERR_PTR(err);
6aa8b732
AK
10088}
10089
c7ed1048
TH
10090#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/hw-vuln/l1tf.html for details.\n"
10091#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/hw-vuln/l1tf.html for details.\n"
275b72a9
KRW
10092
10093static int vmx_vm_init(struct kvm *kvm)
10094{
24fcb53c
JK
10095 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
10096 switch (l1tf_mitigation) {
10097 case L1TF_MITIGATION_OFF:
10098 case L1TF_MITIGATION_FLUSH_NOWARN:
10099 /* 'I explicitly don't care' is set */
10100 break;
10101 case L1TF_MITIGATION_FLUSH:
10102 case L1TF_MITIGATION_FLUSH_NOSMT:
10103 case L1TF_MITIGATION_FULL:
10104 /*
10105 * Warn upon starting the first VM in a potentially
10106 * insecure environment.
10107 */
10108 if (cpu_smt_control == CPU_SMT_ENABLED)
10109 pr_warn_once(L1TF_MSG_SMT);
10110 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
10111 pr_warn_once(L1TF_MSG_L1D);
10112 break;
10113 case L1TF_MITIGATION_FULL_FORCE:
10114 /* Flush is enforced */
10115 break;
275b72a9 10116 }
275b72a9
KRW
10117 }
10118 return 0;
10119}
10120
002c7f7c
YS
10121static void __init vmx_check_processor_compat(void *rtn)
10122{
10123 struct vmcs_config vmcs_conf;
10124
10125 *(int *)rtn = 0;
10126 if (setup_vmcs_config(&vmcs_conf) < 0)
10127 *(int *)rtn = -EIO;
10128 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10129 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10130 smp_processor_id());
10131 *(int *)rtn = -EIO;
10132 }
10133}
10134
4b12f0de 10135static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
64d4d521 10136{
b18d5431
XG
10137 u8 cache;
10138 u64 ipat = 0;
4b12f0de 10139
522c68c4 10140 /* For VT-d and EPT combination
606decd6 10141 * 1. MMIO: always map as UC
522c68c4
SY
10142 * 2. EPT with VT-d:
10143 * a. VT-d without snooping control feature: can't guarantee the
606decd6 10144 * result, try to trust guest.
522c68c4
SY
10145 * b. VT-d with snooping control feature: snooping control feature of
10146 * VT-d engine can guarantee the cache correctness. Just set it
10147 * to WB to keep consistent with host. So the same as item 3.
a19a6d11 10148 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
522c68c4
SY
10149 * consistent with host MTRR
10150 */
606decd6
PB
10151 if (is_mmio) {
10152 cache = MTRR_TYPE_UNCACHABLE;
10153 goto exit;
10154 }
10155
10156 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
b18d5431
XG
10157 ipat = VMX_EPT_IPAT_BIT;
10158 cache = MTRR_TYPE_WRBACK;
10159 goto exit;
10160 }
10161
10162 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10163 ipat = VMX_EPT_IPAT_BIT;
0da029ed 10164 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
fb279950
XG
10165 cache = MTRR_TYPE_WRBACK;
10166 else
10167 cache = MTRR_TYPE_UNCACHABLE;
b18d5431
XG
10168 goto exit;
10169 }
10170
ff53604b 10171 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
b18d5431
XG
10172
10173exit:
10174 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
64d4d521
SY
10175}
10176
17cc3935 10177static int vmx_get_lpage_level(void)
344f414f 10178{
878403b7
SY
10179 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10180 return PT_DIRECTORY_LEVEL;
10181 else
10182 /* For shadow and EPT supported 1GB page */
10183 return PT_PDPE_LEVEL;
344f414f
JR
10184}
10185
feda805f
XG
10186static void vmcs_set_secondary_exec_control(u32 new_ctl)
10187{
10188 /*
10189 * These bits in the secondary execution controls field
10190 * are dynamic, the others are mostly based on the hypervisor
10191 * architecture and the guest's CPUID. Do not touch the
10192 * dynamic bits.
10193 */
10194 u32 mask =
10195 SECONDARY_EXEC_SHADOW_VMCS |
10196 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
10197 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10198
10199 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10200
10201 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10202 (new_ctl & ~mask) | (cur_ctl & mask));
10203}
10204
8322ebbb
DM
10205/*
10206 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10207 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10208 */
10209static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10210{
10211 struct vcpu_vmx *vmx = to_vmx(vcpu);
10212 struct kvm_cpuid_entry2 *entry;
10213
10214 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
10215 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
10216
10217#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10218 if (entry && (entry->_reg & (_cpuid_mask))) \
10219 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
10220} while (0)
10221
10222 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10223 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10224 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10225 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10226 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10227 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10228 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10229 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10230 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10231 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10232 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10233 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10234 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10235 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10236 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10237
10238 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10239 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10240 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10241 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10242 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
c4ad77e0 10243 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
8322ebbb
DM
10244
10245#undef cr4_fixed1_update
10246}
10247
0e851880
SY
10248static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10249{
4e47c7a6 10250 struct vcpu_vmx *vmx = to_vmx(vcpu);
4e47c7a6 10251
80154d77
PB
10252 if (cpu_has_secondary_exec_ctrls()) {
10253 vmx_compute_secondary_exec_control(vmx);
10254 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
ad756a16 10255 }
8b3e34e4 10256
37e4c997
HZ
10257 if (nested_vmx_allowed(vcpu))
10258 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10259 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10260 else
10261 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10262 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
8322ebbb
DM
10263
10264 if (nested_vmx_allowed(vcpu))
10265 nested_vmx_cr_fixed1_bits_update(vcpu);
0e851880
SY
10266}
10267
d4330ef2
JR
10268static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10269{
7b8050f5
NHE
10270 if (func == 1 && nested)
10271 entry->ecx |= bit(X86_FEATURE_VMX);
d4330ef2
JR
10272}
10273
25d92081
YZ
10274static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10275 struct x86_exception *fault)
10276{
533558bc 10277 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
c5f983f6 10278 struct vcpu_vmx *vmx = to_vmx(vcpu);
533558bc 10279 u32 exit_reason;
c5f983f6 10280 unsigned long exit_qualification = vcpu->arch.exit_qualification;
25d92081 10281
c5f983f6
BD
10282 if (vmx->nested.pml_full) {
10283 exit_reason = EXIT_REASON_PML_FULL;
10284 vmx->nested.pml_full = false;
10285 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10286 } else if (fault->error_code & PFERR_RSVD_MASK)
533558bc 10287 exit_reason = EXIT_REASON_EPT_MISCONFIG;
25d92081 10288 else
533558bc 10289 exit_reason = EXIT_REASON_EPT_VIOLATION;
c5f983f6
BD
10290
10291 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
25d92081
YZ
10292 vmcs12->guest_physical_address = fault->address;
10293}
10294
995f00a6
PF
10295static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10296{
bb97a016 10297 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
995f00a6
PF
10298}
10299
155a97a3
NHE
10300/* Callbacks for nested_ept_init_mmu_context: */
10301
10302static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10303{
10304 /* return the page table to be shadowed - in our case, EPT12 */
10305 return get_vmcs12(vcpu)->ept_pointer;
10306}
10307
ae1e2d10 10308static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
155a97a3 10309{
ad896af0 10310 WARN_ON(mmu_is_nested(vcpu));
a057e0e2 10311 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
ae1e2d10
PB
10312 return 1;
10313
10314 kvm_mmu_unload(vcpu);
ad896af0 10315 kvm_init_shadow_ept_mmu(vcpu,
b9c237bb 10316 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
ae1e2d10 10317 VMX_EPT_EXECUTE_ONLY_BIT,
a057e0e2 10318 nested_ept_ad_enabled(vcpu));
155a97a3
NHE
10319 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10320 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10321 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10322
10323 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
ae1e2d10 10324 return 0;
155a97a3
NHE
10325}
10326
10327static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10328{
10329 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10330}
10331
19d5f10b
EK
10332static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10333 u16 error_code)
10334{
10335 bool inequality, bit;
10336
10337 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10338 inequality =
10339 (error_code & vmcs12->page_fault_error_code_mask) !=
10340 vmcs12->page_fault_error_code_match;
10341 return inequality ^ bit;
10342}
10343
feaf0c7d
GN
10344static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10345 struct x86_exception *fault)
10346{
10347 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10348
10349 WARN_ON(!is_guest_mode(vcpu));
10350
305d0ab4
WL
10351 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10352 !to_vmx(vcpu)->nested.nested_run_pending) {
b96fb439
PB
10353 vmcs12->vm_exit_intr_error_code = fault->error_code;
10354 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10355 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10356 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10357 fault->address);
7313c698 10358 } else {
feaf0c7d 10359 kvm_inject_page_fault(vcpu, fault);
7313c698 10360 }
feaf0c7d
GN
10361}
10362
6beb7bd5
JM
10363static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
10364 struct vmcs12 *vmcs12);
10365
10366static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
a2bcba50
WL
10367 struct vmcs12 *vmcs12)
10368{
10369 struct vcpu_vmx *vmx = to_vmx(vcpu);
5e2f30b7 10370 struct page *page;
6beb7bd5 10371 u64 hpa;
a2bcba50
WL
10372
10373 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
a2bcba50
WL
10374 /*
10375 * Translate L1 physical address to host physical
10376 * address for vmcs02. Keep the page pinned, so this
10377 * physical address remains valid. We keep a reference
10378 * to it so we can release it later.
10379 */
5e2f30b7 10380 if (vmx->nested.apic_access_page) { /* shouldn't happen */
53a70daf 10381 kvm_release_page_dirty(vmx->nested.apic_access_page);
5e2f30b7
DH
10382 vmx->nested.apic_access_page = NULL;
10383 }
10384 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
6beb7bd5
JM
10385 /*
10386 * If translation failed, no matter: This feature asks
10387 * to exit when accessing the given address, and if it
10388 * can never be accessed, this feature won't do
10389 * anything anyway.
10390 */
5e2f30b7
DH
10391 if (!is_error_page(page)) {
10392 vmx->nested.apic_access_page = page;
6beb7bd5
JM
10393 hpa = page_to_phys(vmx->nested.apic_access_page);
10394 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10395 } else {
10396 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10397 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10398 }
10399 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10400 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10401 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10402 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10403 kvm_vcpu_reload_apic_access_page(vcpu);
a2bcba50 10404 }
a7c0b07d
WL
10405
10406 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5e2f30b7 10407 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
53a70daf 10408 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
5e2f30b7
DH
10409 vmx->nested.virtual_apic_page = NULL;
10410 }
10411 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
a7c0b07d
WL
10412
10413 /*
6beb7bd5
JM
10414 * If translation failed, VM entry will fail because
10415 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10416 * Failing the vm entry is _not_ what the processor
10417 * does but it's basically the only possibility we
10418 * have. We could still enter the guest if CR8 load
10419 * exits are enabled, CR8 store exits are enabled, and
10420 * virtualize APIC access is disabled; in this case
10421 * the processor would never use the TPR shadow and we
10422 * could simply clear the bit from the execution
10423 * control. But such a configuration is useless, so
10424 * let's keep the code simple.
a7c0b07d 10425 */
5e2f30b7
DH
10426 if (!is_error_page(page)) {
10427 vmx->nested.virtual_apic_page = page;
6beb7bd5
JM
10428 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10429 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10430 }
a7c0b07d
WL
10431 }
10432
705699a1 10433 if (nested_cpu_has_posted_intr(vmcs12)) {
705699a1
WV
10434 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10435 kunmap(vmx->nested.pi_desc_page);
53a70daf 10436 kvm_release_page_dirty(vmx->nested.pi_desc_page);
5e2f30b7 10437 vmx->nested.pi_desc_page = NULL;
3cf01066
CC
10438 vmx->nested.pi_desc = NULL;
10439 vmcs_write64(POSTED_INTR_DESC_ADDR, -1ull);
705699a1 10440 }
5e2f30b7
DH
10441 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10442 if (is_error_page(page))
6beb7bd5 10443 return;
5e2f30b7
DH
10444 vmx->nested.pi_desc_page = page;
10445 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
705699a1
WV
10446 vmx->nested.pi_desc =
10447 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10448 (unsigned long)(vmcs12->posted_intr_desc_addr &
10449 (PAGE_SIZE - 1)));
6beb7bd5
JM
10450 vmcs_write64(POSTED_INTR_DESC_ADDR,
10451 page_to_phys(vmx->nested.pi_desc_page) +
10452 (unsigned long)(vmcs12->posted_intr_desc_addr &
10453 (PAGE_SIZE - 1)));
705699a1 10454 }
6beb7bd5
JM
10455 if (cpu_has_vmx_msr_bitmap() &&
10456 nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) &&
10457 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
cf40088f
KA
10458 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10459 CPU_BASED_USE_MSR_BITMAPS);
6beb7bd5
JM
10460 else
10461 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10462 CPU_BASED_USE_MSR_BITMAPS);
a2bcba50
WL
10463}
10464
f4124500
JK
10465static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10466{
10467 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10468 struct vcpu_vmx *vmx = to_vmx(vcpu);
10469
10470 if (vcpu->arch.virtual_tsc_khz == 0)
10471 return;
10472
10473 /* Make sure short timeouts reliably trigger an immediate vmexit.
10474 * hrtimer_start does not guarantee this. */
10475 if (preemption_timeout <= 1) {
10476 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10477 return;
10478 }
10479
10480 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10481 preemption_timeout *= 1000000;
10482 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10483 hrtimer_start(&vmx->nested.preemption_timer,
10484 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10485}
10486
56a20510
JM
10487static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10488 struct vmcs12 *vmcs12)
10489{
10490 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10491 return 0;
10492
10493 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10494 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10495 return -EINVAL;
10496
10497 return 0;
10498}
10499
3af18d9c
WV
10500static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10501 struct vmcs12 *vmcs12)
10502{
3af18d9c
WV
10503 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10504 return 0;
10505
5fa99cbe 10506 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
3af18d9c
WV
10507 return -EINVAL;
10508
10509 return 0;
10510}
10511
712b12d7
JM
10512static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10513 struct vmcs12 *vmcs12)
10514{
10515 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10516 return 0;
10517
10518 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10519 return -EINVAL;
10520
10521 return 0;
10522}
10523
3af18d9c
WV
10524/*
10525 * Merge L0's and L1's MSR bitmap, return false to indicate that
10526 * we do not use the hardware.
10527 */
10528static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
10529 struct vmcs12 *vmcs12)
10530{
82f0dd4b 10531 int msr;
f2b93280 10532 struct page *page;
d048c098 10533 unsigned long *msr_bitmap_l1;
4b0be90f 10534 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
33241bfe 10535 /*
74469996 10536 * pred_cmd & spec_ctrl are trying to verify two things:
33241bfe
AR
10537 *
10538 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10539 * ensures that we do not accidentally generate an L02 MSR bitmap
10540 * from the L12 MSR bitmap that is too permissive.
10541 * 2. That L1 or L2s have actually used the MSR. This avoids
10542 * unnecessarily merging of the bitmap if the MSR is unused. This
10543 * works properly because we only update the L01 MSR bitmap lazily.
10544 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10545 * updated to reflect this when L1 (or its L2s) actually write to
10546 * the MSR.
10547 */
cb9138cc
KA
10548 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10549 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
f2b93280 10550
33241bfe 10551 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
74469996 10552 !pred_cmd && !spec_ctrl)
f2b93280
WV
10553 return false;
10554
5e2f30b7
DH
10555 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10556 if (is_error_page(page))
f2b93280 10557 return false;
d048c098 10558 msr_bitmap_l1 = (unsigned long *)kmap(page);
f2b93280 10559
d048c098
RK
10560 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
10561
f2b93280 10562 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
82f0dd4b
WV
10563 if (nested_cpu_has_apic_reg_virt(vmcs12))
10564 for (msr = 0x800; msr <= 0x8ff; msr++)
10565 nested_vmx_disable_intercept_for_msr(
d048c098 10566 msr_bitmap_l1, msr_bitmap_l0,
82f0dd4b 10567 msr, MSR_TYPE_R);
d048c098
RK
10568
10569 nested_vmx_disable_intercept_for_msr(
10570 msr_bitmap_l1, msr_bitmap_l0,
f2b93280
WV
10571 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
10572 MSR_TYPE_R | MSR_TYPE_W);
d048c098 10573
608406e2 10574 if (nested_cpu_has_vid(vmcs12)) {
608406e2 10575 nested_vmx_disable_intercept_for_msr(
d048c098 10576 msr_bitmap_l1, msr_bitmap_l0,
608406e2
WV
10577 APIC_BASE_MSR + (APIC_EOI >> 4),
10578 MSR_TYPE_W);
10579 nested_vmx_disable_intercept_for_msr(
d048c098 10580 msr_bitmap_l1, msr_bitmap_l0,
608406e2
WV
10581 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
10582 MSR_TYPE_W);
10583 }
82f0dd4b 10584 }
33241bfe 10585
74469996
KA
10586 if (spec_ctrl)
10587 nested_vmx_disable_intercept_for_msr(
10588 msr_bitmap_l1, msr_bitmap_l0,
10589 MSR_IA32_SPEC_CTRL,
10590 MSR_TYPE_R | MSR_TYPE_W);
10591
33241bfe
AR
10592 if (pred_cmd)
10593 nested_vmx_disable_intercept_for_msr(
10594 msr_bitmap_l1, msr_bitmap_l0,
10595 MSR_IA32_PRED_CMD,
10596 MSR_TYPE_W);
10597
f2b93280 10598 kunmap(page);
53a70daf 10599 kvm_release_page_clean(page);
f2b93280
WV
10600
10601 return true;
10602}
10603
76a19ac5
KS
10604static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
10605 struct vmcs12 *vmcs12)
10606{
10607 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
10608 !page_address_valid(vcpu, vmcs12->apic_access_addr))
10609 return -EINVAL;
10610 else
10611 return 0;
10612}
10613
f2b93280
WV
10614static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10615 struct vmcs12 *vmcs12)
10616{
82f0dd4b 10617 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
608406e2 10618 !nested_cpu_has_apic_reg_virt(vmcs12) &&
705699a1
WV
10619 !nested_cpu_has_vid(vmcs12) &&
10620 !nested_cpu_has_posted_intr(vmcs12))
f2b93280
WV
10621 return 0;
10622
10623 /*
10624 * If virtualize x2apic mode is enabled,
10625 * virtualize apic access must be disabled.
10626 */
82f0dd4b
WV
10627 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10628 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
f2b93280
WV
10629 return -EINVAL;
10630
608406e2
WV
10631 /*
10632 * If virtual interrupt delivery is enabled,
10633 * we must exit on external interrupts.
10634 */
10635 if (nested_cpu_has_vid(vmcs12) &&
10636 !nested_exit_on_intr(vcpu))
10637 return -EINVAL;
10638
705699a1
WV
10639 /*
10640 * bits 15:8 should be zero in posted_intr_nv,
10641 * the descriptor address has been already checked
10642 * in nested_get_vmcs12_pages.
10643 */
10644 if (nested_cpu_has_posted_intr(vmcs12) &&
10645 (!nested_cpu_has_vid(vmcs12) ||
10646 !nested_exit_intr_ack_set(vcpu) ||
10647 vmcs12->posted_intr_nv & 0xff00))
10648 return -EINVAL;
10649
f2b93280
WV
10650 /* tpr shadow is needed by all apicv features. */
10651 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10652 return -EINVAL;
10653
10654 return 0;
3af18d9c
WV
10655}
10656
e9ac033e
EK
10657static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10658 unsigned long count_field,
92d71bc6 10659 unsigned long addr_field)
ff651cb6 10660{
92d71bc6 10661 int maxphyaddr;
e9ac033e
EK
10662 u64 count, addr;
10663
10664 if (vmcs12_read_any(vcpu, count_field, &count) ||
10665 vmcs12_read_any(vcpu, addr_field, &addr)) {
10666 WARN_ON(1);
10667 return -EINVAL;
10668 }
10669 if (count == 0)
10670 return 0;
92d71bc6 10671 maxphyaddr = cpuid_maxphyaddr(vcpu);
e9ac033e
EK
10672 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10673 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
bbe41b95 10674 pr_debug_ratelimited(
e9ac033e
EK
10675 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10676 addr_field, maxphyaddr, count, addr);
10677 return -EINVAL;
10678 }
10679 return 0;
10680}
10681
10682static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10683 struct vmcs12 *vmcs12)
10684{
e9ac033e
EK
10685 if (vmcs12->vm_exit_msr_load_count == 0 &&
10686 vmcs12->vm_exit_msr_store_count == 0 &&
10687 vmcs12->vm_entry_msr_load_count == 0)
10688 return 0; /* Fast path */
e9ac033e 10689 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
92d71bc6 10690 VM_EXIT_MSR_LOAD_ADDR) ||
e9ac033e 10691 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
92d71bc6 10692 VM_EXIT_MSR_STORE_ADDR) ||
e9ac033e 10693 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
92d71bc6 10694 VM_ENTRY_MSR_LOAD_ADDR))
e9ac033e
EK
10695 return -EINVAL;
10696 return 0;
10697}
10698
c5f983f6
BD
10699static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10700 struct vmcs12 *vmcs12)
10701{
10702 u64 address = vmcs12->pml_address;
10703 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10704
10705 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10706 if (!nested_cpu_has_ept(vmcs12) ||
10707 !IS_ALIGNED(address, 4096) ||
10708 address >> maxphyaddr)
10709 return -EINVAL;
10710 }
10711
10712 return 0;
10713}
10714
e9ac033e
EK
10715static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10716 struct vmx_msr_entry *e)
10717{
10718 /* x2APIC MSR accesses are not allowed */
8a9781f7 10719 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
e9ac033e
EK
10720 return -EINVAL;
10721 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10722 e->index == MSR_IA32_UCODE_REV)
10723 return -EINVAL;
10724 if (e->reserved != 0)
ff651cb6
WV
10725 return -EINVAL;
10726 return 0;
10727}
10728
e9ac033e
EK
10729static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10730 struct vmx_msr_entry *e)
ff651cb6
WV
10731{
10732 if (e->index == MSR_FS_BASE ||
10733 e->index == MSR_GS_BASE ||
e9ac033e
EK
10734 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10735 nested_vmx_msr_check_common(vcpu, e))
10736 return -EINVAL;
10737 return 0;
10738}
10739
10740static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10741 struct vmx_msr_entry *e)
10742{
10743 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10744 nested_vmx_msr_check_common(vcpu, e))
ff651cb6
WV
10745 return -EINVAL;
10746 return 0;
10747}
10748
10749/*
10750 * Load guest's/host's msr at nested entry/exit.
10751 * return 0 for success, entry index for failure.
10752 */
10753static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10754{
10755 u32 i;
10756 struct vmx_msr_entry e;
10757 struct msr_data msr;
10758
10759 msr.host_initiated = false;
10760 for (i = 0; i < count; i++) {
54bf36aa
PB
10761 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10762 &e, sizeof(e))) {
bbe41b95 10763 pr_debug_ratelimited(
e9ac033e
EK
10764 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10765 __func__, i, gpa + i * sizeof(e));
ff651cb6 10766 goto fail;
e9ac033e
EK
10767 }
10768 if (nested_vmx_load_msr_check(vcpu, &e)) {
bbe41b95 10769 pr_debug_ratelimited(
e9ac033e
EK
10770 "%s check failed (%u, 0x%x, 0x%x)\n",
10771 __func__, i, e.index, e.reserved);
10772 goto fail;
10773 }
ff651cb6
WV
10774 msr.index = e.index;
10775 msr.data = e.value;
e9ac033e 10776 if (kvm_set_msr(vcpu, &msr)) {
bbe41b95 10777 pr_debug_ratelimited(
e9ac033e
EK
10778 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10779 __func__, i, e.index, e.value);
ff651cb6 10780 goto fail;
e9ac033e 10781 }
ff651cb6
WV
10782 }
10783 return 0;
10784fail:
10785 return i + 1;
10786}
10787
10788static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10789{
10790 u32 i;
10791 struct vmx_msr_entry e;
10792
10793 for (i = 0; i < count; i++) {
609e36d3 10794 struct msr_data msr_info;
54bf36aa
PB
10795 if (kvm_vcpu_read_guest(vcpu,
10796 gpa + i * sizeof(e),
10797 &e, 2 * sizeof(u32))) {
bbe41b95 10798 pr_debug_ratelimited(
e9ac033e
EK
10799 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10800 __func__, i, gpa + i * sizeof(e));
ff651cb6 10801 return -EINVAL;
e9ac033e
EK
10802 }
10803 if (nested_vmx_store_msr_check(vcpu, &e)) {
bbe41b95 10804 pr_debug_ratelimited(
e9ac033e
EK
10805 "%s check failed (%u, 0x%x, 0x%x)\n",
10806 __func__, i, e.index, e.reserved);
ff651cb6 10807 return -EINVAL;
e9ac033e 10808 }
609e36d3
PB
10809 msr_info.host_initiated = false;
10810 msr_info.index = e.index;
10811 if (kvm_get_msr(vcpu, &msr_info)) {
bbe41b95 10812 pr_debug_ratelimited(
e9ac033e
EK
10813 "%s cannot read MSR (%u, 0x%x)\n",
10814 __func__, i, e.index);
10815 return -EINVAL;
10816 }
54bf36aa
PB
10817 if (kvm_vcpu_write_guest(vcpu,
10818 gpa + i * sizeof(e) +
10819 offsetof(struct vmx_msr_entry, value),
10820 &msr_info.data, sizeof(msr_info.data))) {
bbe41b95 10821 pr_debug_ratelimited(
e9ac033e 10822 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
609e36d3 10823 __func__, i, e.index, msr_info.data);
e9ac033e
EK
10824 return -EINVAL;
10825 }
ff651cb6
WV
10826 }
10827 return 0;
10828}
10829
1dc35dac
LP
10830static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10831{
10832 unsigned long invalid_mask;
10833
10834 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10835 return (val & invalid_mask) == 0;
10836}
10837
9ed38ffa
LP
10838/*
10839 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10840 * emulating VM entry into a guest with EPT enabled.
10841 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10842 * is assigned to entry_failure_code on failure.
10843 */
10844static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
ca0bde28 10845 u32 *entry_failure_code)
9ed38ffa 10846{
9ed38ffa 10847 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
1dc35dac 10848 if (!nested_cr3_valid(vcpu, cr3)) {
9ed38ffa
LP
10849 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10850 return 1;
10851 }
10852
10853 /*
10854 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10855 * must not be dereferenced.
10856 */
10857 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10858 !nested_ept) {
10859 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10860 *entry_failure_code = ENTRY_FAIL_PDPTE;
10861 return 1;
10862 }
10863 }
10864
10865 vcpu->arch.cr3 = cr3;
10866 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10867 }
10868
10869 kvm_mmu_reset_context(vcpu);
10870 return 0;
10871}
10872
fe3ef05c
NHE
10873/*
10874 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10875 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
b4619660 10876 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
fe3ef05c
NHE
10877 * guest in a way that will both be appropriate to L1's requests, and our
10878 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10879 * function also has additional necessary side-effects, like setting various
10880 * vcpu->arch fields.
ee146c1c
LP
10881 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10882 * is assigned to entry_failure_code on failure.
fe3ef05c 10883 */
ee146c1c 10884static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
ca0bde28 10885 bool from_vmentry, u32 *entry_failure_code)
fe3ef05c
NHE
10886{
10887 struct vcpu_vmx *vmx = to_vmx(vcpu);
03efce6f 10888 u32 exec_control, vmcs12_exec_ctrl;
fe3ef05c
NHE
10889
10890 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10891 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10892 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10893 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10894 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10895 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10896 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10897 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10898 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10899 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10900 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10901 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10902 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10903 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10904 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10905 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10906 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10907 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10908 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10909 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10910 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10911 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10912 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10913 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10914 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10915 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10916 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10917 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10918 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10919 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10920 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10921 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10922 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10923 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10924 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10925 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10926
cf8b84f4
JM
10927 if (from_vmentry &&
10928 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2996fca0
JK
10929 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10930 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10931 } else {
10932 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10933 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10934 }
cf8b84f4
JM
10935 if (from_vmentry) {
10936 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10937 vmcs12->vm_entry_intr_info_field);
10938 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10939 vmcs12->vm_entry_exception_error_code);
10940 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10941 vmcs12->vm_entry_instruction_len);
10942 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10943 vmcs12->guest_interruptibility_info);
2d6144e3
WL
10944 vmx->loaded_vmcs->nmi_known_unmasked =
10945 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
cf8b84f4
JM
10946 } else {
10947 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10948 }
fe3ef05c 10949 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
63fbf59f 10950 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
fe3ef05c
NHE
10951 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10952 vmcs12->guest_pending_dbg_exceptions);
10953 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10954 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10955
81dc01f7
WL
10956 if (nested_cpu_has_xsaves(vmcs12))
10957 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
fe3ef05c
NHE
10958 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10959
f4124500 10960 exec_control = vmcs12->pin_based_vm_exec_control;
9314006d
PB
10961
10962 /* Preemption timer setting is only taken from vmcs01. */
705699a1 10963 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9314006d
PB
10964 exec_control |= vmcs_config.pin_based_exec_ctrl;
10965 if (vmx->hv_deadline_tsc == -1)
10966 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
705699a1 10967
9314006d 10968 /* Posted interrupts setting is only taken from vmcs12. */
705699a1 10969 if (nested_cpu_has_posted_intr(vmcs12)) {
705699a1
WV
10970 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10971 vmx->nested.pi_pending = false;
06a5524f 10972 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
6beb7bd5 10973 } else {
705699a1 10974 exec_control &= ~PIN_BASED_POSTED_INTR;
6beb7bd5 10975 }
705699a1 10976
f4124500 10977 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
fe3ef05c 10978
f4124500
JK
10979 vmx->nested.preemption_timer_expired = false;
10980 if (nested_cpu_has_preemption_timer(vmcs12))
10981 vmx_start_preemption_timer(vcpu);
0238ea91 10982
fe3ef05c
NHE
10983 /*
10984 * Whether page-faults are trapped is determined by a combination of
10985 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10986 * If enable_ept, L0 doesn't care about page faults and we should
10987 * set all of these to L1's desires. However, if !enable_ept, L0 does
10988 * care about (at least some) page faults, and because it is not easy
10989 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10990 * to exit on each and every L2 page fault. This is done by setting
10991 * MASK=MATCH=0 and (see below) EB.PF=1.
10992 * Note that below we don't need special code to set EB.PF beyond the
10993 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10994 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10995 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
fe3ef05c
NHE
10996 */
10997 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10998 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10999 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11000 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11001
11002 if (cpu_has_secondary_exec_ctrls()) {
80154d77 11003 exec_control = vmx->secondary_exec_control;
e2821620 11004
fe3ef05c 11005 /* Take the following fields only from vmcs12 */
696dfd95 11006 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
90a2db6d 11007 SECONDARY_EXEC_ENABLE_INVPCID |
b3a2a907 11008 SECONDARY_EXEC_RDTSCP |
3db13480 11009 SECONDARY_EXEC_XSAVES |
696dfd95 11010 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
27c42a1b
BD
11011 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11012 SECONDARY_EXEC_ENABLE_VMFUNC);
fe3ef05c 11013 if (nested_cpu_has(vmcs12,
03efce6f
BD
11014 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11015 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11016 ~SECONDARY_EXEC_ENABLE_PML;
11017 exec_control |= vmcs12_exec_ctrl;
11018 }
fe3ef05c 11019
27c42a1b
BD
11020 /* All VMFUNCs are currently emulated through L0 vmexits. */
11021 if (exec_control & SECONDARY_EXEC_ENABLE_VMFUNC)
11022 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11023
608406e2
WV
11024 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
11025 vmcs_write64(EOI_EXIT_BITMAP0,
11026 vmcs12->eoi_exit_bitmap0);
11027 vmcs_write64(EOI_EXIT_BITMAP1,
11028 vmcs12->eoi_exit_bitmap1);
11029 vmcs_write64(EOI_EXIT_BITMAP2,
11030 vmcs12->eoi_exit_bitmap2);
11031 vmcs_write64(EOI_EXIT_BITMAP3,
11032 vmcs12->eoi_exit_bitmap3);
11033 vmcs_write16(GUEST_INTR_STATUS,
11034 vmcs12->guest_intr_status);
11035 }
11036
6beb7bd5
JM
11037 /*
11038 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11039 * nested_get_vmcs12_pages will either fix it up or
11040 * remove the VM execution control.
11041 */
11042 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11043 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11044
fe3ef05c
NHE
11045 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11046 }
11047
11048
11049 /*
11050 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11051 * Some constant fields are set here by vmx_set_constant_host_state().
11052 * Other fields are different per CPU, and will be set later when
11053 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11054 */
a547c6db 11055 vmx_set_constant_host_state(vmx);
fe3ef05c 11056
83bafef1
JM
11057 /*
11058 * Set the MSR load/store lists to match L0's settings.
11059 */
11060 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6e3dedb6
KRW
11061 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11062 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11063 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11064 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
83bafef1 11065
fe3ef05c
NHE
11066 /*
11067 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11068 * entry, but only if the current (host) sp changed from the value
11069 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11070 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11071 * here we just force the write to happen on entry.
11072 */
11073 vmx->host_rsp = 0;
11074
11075 exec_control = vmx_exec_control(vmx); /* L0's desires */
11076 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11077 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11078 exec_control &= ~CPU_BASED_TPR_SHADOW;
11079 exec_control |= vmcs12->cpu_based_vm_exec_control;
a7c0b07d 11080
6beb7bd5
JM
11081 /*
11082 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11083 * nested_get_vmcs12_pages can't fix it up, the illegal value
11084 * will result in a VM entry failure.
11085 */
a7c0b07d 11086 if (exec_control & CPU_BASED_TPR_SHADOW) {
6beb7bd5 11087 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
a7c0b07d 11088 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
51aa68e7
JM
11089 } else {
11090#ifdef CONFIG_X86_64
11091 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11092 CPU_BASED_CR8_STORE_EXITING;
11093#endif
a7c0b07d
WL
11094 }
11095
fe3ef05c 11096 /*
3af18d9c 11097 * Merging of IO bitmap not currently supported.
fe3ef05c
NHE
11098 * Rather, exit every time.
11099 */
fe3ef05c
NHE
11100 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11101 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11102
11103 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11104
11105 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11106 * bitwise-or of what L1 wants to trap for L2, and what we want to
11107 * trap. Note that CR0.TS also needs updating - we do this later.
11108 */
11109 update_exception_bitmap(vcpu);
11110 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11111 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11112
8049d651
NHE
11113 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11114 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11115 * bits are further modified by vmx_set_efer() below.
11116 */
f4124500 11117 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
8049d651
NHE
11118
11119 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11120 * emulated by vmx_set_efer(), below.
11121 */
2961e876 11122 vm_entry_controls_init(vmx,
8049d651
NHE
11123 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11124 ~VM_ENTRY_IA32E_MODE) |
fe3ef05c
NHE
11125 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11126
cf8b84f4
JM
11127 if (from_vmentry &&
11128 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
fe3ef05c 11129 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
44811c02 11130 vcpu->arch.pat = vmcs12->guest_ia32_pat;
cf8b84f4 11131 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
fe3ef05c 11132 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
cf8b84f4 11133 }
fe3ef05c
NHE
11134
11135 set_cr4_guest_host_mask(vmx);
11136
cf8b84f4
JM
11137 if (from_vmentry &&
11138 vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
36be0b9d
PB
11139 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11140
f7f5542f
KA
11141 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11142
c95ba92a
PF
11143 if (kvm_has_tsc_control)
11144 decache_tsc_multiplier(vmx);
fe3ef05c 11145
4b0be90f
PB
11146 if (cpu_has_vmx_msr_bitmap())
11147 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
11148
fe3ef05c
NHE
11149 if (enable_vpid) {
11150 /*
5c614b35
WL
11151 * There is no direct mapping between vpid02 and vpid12, the
11152 * vpid02 is per-vCPU for L0 and reused while the value of
11153 * vpid12 is changed w/ one invvpid during nested vmentry.
11154 * The vpid12 is allocated by L1 for L2, so it will not
11155 * influence global bitmap(for vpid01 and vpid02 allocation)
11156 * even if spawn a lot of nested vCPUs.
fe3ef05c 11157 */
5c614b35
WL
11158 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
11159 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11160 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11161 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
11162 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
11163 }
11164 } else {
11165 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11166 vmx_flush_tlb(vcpu);
11167 }
11168
fe3ef05c
NHE
11169 }
11170
1fb883bb
LP
11171 if (enable_pml) {
11172 /*
11173 * Conceptually we want to copy the PML address and index from
11174 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11175 * since we always flush the log on each vmexit, this happens
11176 * to be equivalent to simply resetting the fields in vmcs02.
11177 */
11178 ASSERT(vmx->pml_pg);
11179 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11180 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11181 }
11182
155a97a3 11183 if (nested_cpu_has_ept(vmcs12)) {
ae1e2d10
PB
11184 if (nested_ept_init_mmu_context(vcpu)) {
11185 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11186 return 1;
11187 }
fb6c8198
JM
11188 } else if (nested_cpu_has2(vmcs12,
11189 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
5ad2874a 11190 vmx_flush_tlb(vcpu);
155a97a3
NHE
11191 }
11192
fe3ef05c 11193 /*
bd7e5b08
PB
11194 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11195 * bits which we consider mandatory enabled.
fe3ef05c
NHE
11196 * The CR0_READ_SHADOW is what L2 should have expected to read given
11197 * the specifications by L1; It's not enough to take
11198 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11199 * have more bits than L1 expected.
11200 */
11201 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11202 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11203
11204 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11205 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11206
cf8b84f4
JM
11207 if (from_vmentry &&
11208 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
5a6a9748
DM
11209 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11210 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11211 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11212 else
11213 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11214 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11215 vmx_set_efer(vcpu, vcpu->arch.efer);
11216
9ed38ffa 11217 /* Shadow page tables on either EPT or shadow page tables. */
7ad658b6 11218 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
9ed38ffa
LP
11219 entry_failure_code))
11220 return 1;
7ca29de2 11221
feaf0c7d
GN
11222 if (!enable_ept)
11223 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11224
3633cfc3
NHE
11225 /*
11226 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11227 */
11228 if (enable_ept) {
11229 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11230 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11231 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11232 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11233 }
11234
fe3ef05c
NHE
11235 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11236 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
ee146c1c 11237 return 0;
fe3ef05c
NHE
11238}
11239
ca0bde28 11240static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
cd232ad0 11241{
cd232ad0 11242 struct vcpu_vmx *vmx = to_vmx(vcpu);
7c177938 11243
6dfacadd 11244 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
ca0bde28
JM
11245 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11246 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
26539bd0 11247
56a20510
JM
11248 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11249 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11250
ca0bde28
JM
11251 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11252 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
7c177938 11253
76a19ac5
KS
11254 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11255 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11256
712b12d7
JM
11257 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11258 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11259
ca0bde28
JM
11260 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11261 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
f2b93280 11262
ca0bde28
JM
11263 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11264 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
e9ac033e 11265
c5f983f6
BD
11266 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11267 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11268
7c177938 11269 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
0115f9cb 11270 vmx->nested.nested_vmx_procbased_ctls_low,
b9c237bb 11271 vmx->nested.nested_vmx_procbased_ctls_high) ||
2e5b0bd9
JM
11272 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11273 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
11274 vmx->nested.nested_vmx_secondary_ctls_low,
11275 vmx->nested.nested_vmx_secondary_ctls_high)) ||
7c177938 11276 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
b9c237bb
WV
11277 vmx->nested.nested_vmx_pinbased_ctls_low,
11278 vmx->nested.nested_vmx_pinbased_ctls_high) ||
7c177938 11279 !vmx_control_verify(vmcs12->vm_exit_controls,
0115f9cb 11280 vmx->nested.nested_vmx_exit_ctls_low,
b9c237bb 11281 vmx->nested.nested_vmx_exit_ctls_high) ||
7c177938 11282 !vmx_control_verify(vmcs12->vm_entry_controls,
0115f9cb 11283 vmx->nested.nested_vmx_entry_ctls_low,
b9c237bb 11284 vmx->nested.nested_vmx_entry_ctls_high))
ca0bde28 11285 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
7c177938 11286
41ab9372
BD
11287 if (nested_cpu_has_vmfunc(vmcs12)) {
11288 if (vmcs12->vm_function_control &
11289 ~vmx->nested.nested_vmx_vmfunc_controls)
11290 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11291
11292 if (nested_cpu_has_eptp_switching(vmcs12)) {
11293 if (!nested_cpu_has_ept(vmcs12) ||
11294 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11295 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11296 }
11297 }
27c42a1b 11298
c7c2c709
JM
11299 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11300 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11301
3899152c 11302 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
1dc35dac 11303 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
ca0bde28
JM
11304 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11305 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11306
11307 return 0;
11308}
11309
11310static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11311 u32 *exit_qual)
11312{
11313 bool ia32e;
11314
11315 *exit_qual = ENTRY_FAIL_DEFAULT;
7c177938 11316
3899152c 11317 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
ca0bde28 11318 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
b428018a 11319 return 1;
ca0bde28
JM
11320
11321 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11322 vmcs12->vmcs_link_pointer != -1ull) {
11323 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
b428018a 11324 return 1;
7c177938
NHE
11325 }
11326
384bb783 11327 /*
cb0c8cda 11328 * If the load IA32_EFER VM-entry control is 1, the following checks
384bb783
JK
11329 * are performed on the field for the IA32_EFER MSR:
11330 * - Bits reserved in the IA32_EFER MSR must be 0.
11331 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11332 * the IA-32e mode guest VM-exit control. It must also be identical
11333 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11334 * CR0.PG) is 1.
11335 */
ca0bde28
JM
11336 if (to_vmx(vcpu)->nested.nested_run_pending &&
11337 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
384bb783
JK
11338 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11339 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11340 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11341 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
ca0bde28 11342 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
b428018a 11343 return 1;
384bb783
JK
11344 }
11345
11346 /*
11347 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11348 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11349 * the values of the LMA and LME bits in the field must each be that of
11350 * the host address-space size VM-exit control.
11351 */
11352 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11353 ia32e = (vmcs12->vm_exit_controls &
11354 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11355 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11356 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
ca0bde28 11357 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
b428018a 11358 return 1;
ca0bde28
JM
11359 }
11360
f1b026a3
WL
11361 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11362 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11363 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11364 return 1;
11365
ca0bde28
JM
11366 return 0;
11367}
11368
858e25c0
JM
11369static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11370{
11371 struct vcpu_vmx *vmx = to_vmx(vcpu);
11372 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
858e25c0
JM
11373 u32 msr_entry_idx;
11374 u32 exit_qual;
f7f5542f 11375 int r;
858e25c0 11376
858e25c0
JM
11377 enter_guest_mode(vcpu);
11378
11379 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11380 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11381
8819227c 11382 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
858e25c0
JM
11383 vmx_segment_cache_clear(vmx);
11384
f7f5542f
KA
11385 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11386 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11387
11388 r = EXIT_REASON_INVALID_STATE;
11389 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual))
11390 goto fail;
858e25c0
JM
11391
11392 nested_get_vmcs12_pages(vcpu, vmcs12);
11393
f7f5542f 11394 r = EXIT_REASON_MSR_LOAD_FAIL;
858e25c0
JM
11395 msr_entry_idx = nested_vmx_load_msr(vcpu,
11396 vmcs12->vm_entry_msr_load_addr,
11397 vmcs12->vm_entry_msr_load_count);
f7f5542f
KA
11398 if (msr_entry_idx)
11399 goto fail;
858e25c0 11400
858e25c0
JM
11401 /*
11402 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11403 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11404 * returned as far as L1 is concerned. It will only return (and set
11405 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11406 */
11407 return 0;
f7f5542f
KA
11408
11409fail:
11410 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11411 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11412 leave_guest_mode(vcpu);
11413 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11414 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11415 return 1;
858e25c0
JM
11416}
11417
ca0bde28
JM
11418/*
11419 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11420 * for running an L2 nested guest.
11421 */
11422static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11423{
11424 struct vmcs12 *vmcs12;
11425 struct vcpu_vmx *vmx = to_vmx(vcpu);
b3f1dfb6 11426 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
ca0bde28
JM
11427 u32 exit_qual;
11428 int ret;
11429
11430 if (!nested_vmx_check_permission(vcpu))
11431 return 1;
11432
11433 if (!nested_vmx_check_vmcs12(vcpu))
11434 goto out;
11435
11436 vmcs12 = get_vmcs12(vcpu);
11437
11438 if (enable_shadow_vmcs)
11439 copy_shadow_to_vmcs12(vmx);
11440
11441 /*
11442 * The nested entry process starts with enforcing various prerequisites
11443 * on vmcs12 as required by the Intel SDM, and act appropriately when
11444 * they fail: As the SDM explains, some conditions should cause the
11445 * instruction to fail, while others will cause the instruction to seem
11446 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11447 * To speed up the normal (success) code path, we should avoid checking
11448 * for misconfigurations which will anyway be caught by the processor
11449 * when using the merged vmcs02.
11450 */
b3f1dfb6
JM
11451 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11452 nested_vmx_failValid(vcpu,
11453 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11454 goto out;
11455 }
11456
ca0bde28
JM
11457 if (vmcs12->launch_state == launch) {
11458 nested_vmx_failValid(vcpu,
11459 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11460 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
11461 goto out;
11462 }
11463
11464 ret = check_vmentry_prereqs(vcpu, vmcs12);
11465 if (ret) {
11466 nested_vmx_failValid(vcpu, ret);
11467 goto out;
11468 }
11469
11470 /*
11471 * After this point, the trap flag no longer triggers a singlestep trap
11472 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11473 * This is not 100% correct; for performance reasons, we delegate most
11474 * of the checks on host state to the processor. If those fail,
11475 * the singlestep trap is missed.
11476 */
11477 skip_emulated_instruction(vcpu);
11478
11479 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11480 if (ret) {
11481 nested_vmx_entry_failure(vcpu, vmcs12,
11482 EXIT_REASON_INVALID_STATE, exit_qual);
11483 return 1;
384bb783
JK
11484 }
11485
7c177938
NHE
11486 /*
11487 * We're finally done with prerequisite checking, and can start with
11488 * the nested entry.
11489 */
11490
858e25c0
JM
11491 ret = enter_vmx_non_root_mode(vcpu, true);
11492 if (ret)
11493 return ret;
ff651cb6 11494
f0ace387
PB
11495 /* Hide L1D cache contents from the nested guest. */
11496 vmx->vcpu.arch.l1tf_flush_l1d = true;
11497
076381c4
CG
11498 /*
11499 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11500 * by event injection, halt vcpu.
11501 */
11502 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11503 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
5cb56059 11504 return kvm_vcpu_halt(vcpu);
6dfacadd 11505
7af40ad3
JK
11506 vmx->nested.nested_run_pending = 1;
11507
cd232ad0 11508 return 1;
eb277562
KH
11509
11510out:
6affcbed 11511 return kvm_skip_emulated_instruction(vcpu);
cd232ad0
NHE
11512}
11513
4704d0be
NHE
11514/*
11515 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11516 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11517 * This function returns the new value we should put in vmcs12.guest_cr0.
11518 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11519 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11520 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11521 * didn't trap the bit, because if L1 did, so would L0).
11522 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11523 * been modified by L2, and L1 knows it. So just leave the old value of
11524 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11525 * isn't relevant, because if L0 traps this bit it can set it to anything.
11526 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11527 * changed these bits, and therefore they need to be updated, but L0
11528 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11529 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11530 */
11531static inline unsigned long
11532vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11533{
11534 return
11535 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11536 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11537 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11538 vcpu->arch.cr0_guest_owned_bits));
11539}
11540
11541static inline unsigned long
11542vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11543{
11544 return
11545 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11546 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11547 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11548 vcpu->arch.cr4_guest_owned_bits));
11549}
11550
5f3d5799
JK
11551static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11552 struct vmcs12 *vmcs12)
11553{
11554 u32 idt_vectoring;
11555 unsigned int nr;
11556
664f8e26 11557 if (vcpu->arch.exception.injected) {
5f3d5799
JK
11558 nr = vcpu->arch.exception.nr;
11559 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11560
11561 if (kvm_exception_is_soft(nr)) {
11562 vmcs12->vm_exit_instruction_len =
11563 vcpu->arch.event_exit_inst_len;
11564 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11565 } else
11566 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11567
11568 if (vcpu->arch.exception.has_error_code) {
11569 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11570 vmcs12->idt_vectoring_error_code =
11571 vcpu->arch.exception.error_code;
11572 }
11573
11574 vmcs12->idt_vectoring_info_field = idt_vectoring;
cd2633c5 11575 } else if (vcpu->arch.nmi_injected) {
5f3d5799
JK
11576 vmcs12->idt_vectoring_info_field =
11577 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
11578 } else if (vcpu->arch.interrupt.pending) {
11579 nr = vcpu->arch.interrupt.nr;
11580 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11581
11582 if (vcpu->arch.interrupt.soft) {
11583 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11584 vmcs12->vm_entry_instruction_len =
11585 vcpu->arch.event_exit_inst_len;
11586 } else
11587 idt_vectoring |= INTR_TYPE_EXT_INTR;
11588
11589 vmcs12->idt_vectoring_info_field = idt_vectoring;
11590 }
11591}
11592
b6b8a145
JK
11593static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11594{
11595 struct vcpu_vmx *vmx = to_vmx(vcpu);
bfcf83b1 11596 unsigned long exit_qual;
917dc606
LA
11597 bool block_nested_events =
11598 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
acc9ab60 11599
bfcf83b1
WL
11600 if (vcpu->arch.exception.pending &&
11601 nested_vmx_check_exception(vcpu, &exit_qual)) {
917dc606 11602 if (block_nested_events)
bfcf83b1
WL
11603 return -EBUSY;
11604 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
bfcf83b1
WL
11605 return 0;
11606 }
11607
f4124500
JK
11608 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11609 vmx->nested.preemption_timer_expired) {
917dc606 11610 if (block_nested_events)
f4124500
JK
11611 return -EBUSY;
11612 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11613 return 0;
11614 }
11615
b6b8a145 11616 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
917dc606 11617 if (block_nested_events)
b6b8a145
JK
11618 return -EBUSY;
11619 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11620 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11621 INTR_INFO_VALID_MASK, 0);
11622 /*
11623 * The NMI-triggered VM exit counts as injection:
11624 * clear this one and block further NMIs.
11625 */
11626 vcpu->arch.nmi_pending = 0;
11627 vmx_set_nmi_mask(vcpu, true);
11628 return 0;
11629 }
11630
11631 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11632 nested_exit_on_intr(vcpu)) {
917dc606 11633 if (block_nested_events)
b6b8a145
JK
11634 return -EBUSY;
11635 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
705699a1 11636 return 0;
b6b8a145
JK
11637 }
11638
6342c50a
DH
11639 vmx_complete_nested_posted_interrupt(vcpu);
11640 return 0;
b6b8a145
JK
11641}
11642
f4124500
JK
11643static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11644{
11645 ktime_t remaining =
11646 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11647 u64 value;
11648
11649 if (ktime_to_ns(remaining) <= 0)
11650 return 0;
11651
11652 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11653 do_div(value, 1000000);
11654 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11655}
11656
4704d0be 11657/*
cf8b84f4
JM
11658 * Update the guest state fields of vmcs12 to reflect changes that
11659 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11660 * VM-entry controls is also updated, since this is really a guest
11661 * state bit.)
4704d0be 11662 */
cf8b84f4 11663static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4704d0be 11664{
4704d0be
NHE
11665 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11666 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11667
4704d0be
NHE
11668 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11669 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11670 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11671
11672 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11673 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11674 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11675 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11676 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11677 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11678 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11679 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11680 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11681 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11682 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11683 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11684 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11685 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11686 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11687 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11688 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11689 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11690 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11691 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11692 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11693 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11694 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11695 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11696 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11697 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11698 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11699 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11700 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11701 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11702 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11703 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11704 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11705 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11706 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11707 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11708
4704d0be
NHE
11709 vmcs12->guest_interruptibility_info =
11710 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11711 vmcs12->guest_pending_dbg_exceptions =
11712 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
3edf1e69
JK
11713 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11714 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11715 else
11716 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4704d0be 11717
f4124500
JK
11718 if (nested_cpu_has_preemption_timer(vmcs12)) {
11719 if (vmcs12->vm_exit_controls &
11720 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11721 vmcs12->vmx_preemption_timer_value =
11722 vmx_get_preemption_timer_value(vcpu);
11723 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11724 }
7854cbca 11725
3633cfc3
NHE
11726 /*
11727 * In some cases (usually, nested EPT), L2 is allowed to change its
11728 * own CR3 without exiting. If it has changed it, we must keep it.
11729 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11730 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11731 *
11732 * Additionally, restore L2's PDPTR to vmcs12.
11733 */
11734 if (enable_ept) {
f3531054 11735 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
3633cfc3
NHE
11736 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11737 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11738 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11739 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11740 }
11741
d281e13b 11742 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
119a9c01 11743
608406e2
WV
11744 if (nested_cpu_has_vid(vmcs12))
11745 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11746
c18911a2
JK
11747 vmcs12->vm_entry_controls =
11748 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
2961e876 11749 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
c18911a2 11750
2996fca0
JK
11751 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11752 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11753 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11754 }
11755
4704d0be
NHE
11756 /* TODO: These cannot have changed unless we have MSR bitmaps and
11757 * the relevant bit asks not to trap the change */
b8c07d55 11758 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
4704d0be 11759 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
10ba54a5
JK
11760 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11761 vmcs12->guest_ia32_efer = vcpu->arch.efer;
4704d0be
NHE
11762 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11763 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11764 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
a87036ad 11765 if (kvm_mpx_supported())
36be0b9d 11766 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
cf8b84f4
JM
11767}
11768
11769/*
11770 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11771 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11772 * and this function updates it to reflect the changes to the guest state while
11773 * L2 was running (and perhaps made some exits which were handled directly by L0
11774 * without going back to L1), and to reflect the exit reason.
11775 * Note that we do not have to copy here all VMCS fields, just those that
11776 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11777 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11778 * which already writes to vmcs12 directly.
11779 */
11780static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11781 u32 exit_reason, u32 exit_intr_info,
11782 unsigned long exit_qualification)
11783{
11784 /* update guest state fields: */
11785 sync_vmcs12(vcpu, vmcs12);
4704d0be
NHE
11786
11787 /* update exit information fields: */
11788
533558bc
JK
11789 vmcs12->vm_exit_reason = exit_reason;
11790 vmcs12->exit_qualification = exit_qualification;
533558bc 11791 vmcs12->vm_exit_intr_info = exit_intr_info;
7313c698 11792
5f3d5799 11793 vmcs12->idt_vectoring_info_field = 0;
4704d0be
NHE
11794 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11795 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11796
5f3d5799 11797 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
7cdc2d62
JM
11798 vmcs12->launch_state = 1;
11799
5f3d5799
JK
11800 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11801 * instead of reading the real value. */
4704d0be 11802 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
5f3d5799
JK
11803
11804 /*
11805 * Transfer the event that L0 or L1 may wanted to inject into
11806 * L2 to IDT_VECTORING_INFO_FIELD.
11807 */
11808 vmcs12_save_pending_event(vcpu, vmcs12);
11809 }
11810
11811 /*
11812 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11813 * preserved above and would only end up incorrectly in L1.
11814 */
11815 vcpu->arch.nmi_injected = false;
11816 kvm_clear_exception_queue(vcpu);
11817 kvm_clear_interrupt_queue(vcpu);
4704d0be
NHE
11818}
11819
5af41573
WL
11820static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11821 struct vmcs12 *vmcs12)
11822{
11823 u32 entry_failure_code;
11824
11825 nested_ept_uninit_mmu_context(vcpu);
11826
11827 /*
11828 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11829 * couldn't have changed.
11830 */
11831 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11832 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11833
11834 if (!enable_ept)
11835 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11836}
11837
4704d0be
NHE
11838/*
11839 * A part of what we need to when the nested L2 guest exits and we want to
11840 * run its L1 parent, is to reset L1's guest state to the host state specified
11841 * in vmcs12.
11842 * This function is to be called not only on normal nested exit, but also on
11843 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11844 * Failures During or After Loading Guest State").
11845 * This function should be called when the active VMCS is L1's (vmcs01).
11846 */
733568f9
JK
11847static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11848 struct vmcs12 *vmcs12)
4704d0be 11849{
21feb4eb
ACL
11850 struct kvm_segment seg;
11851
4704d0be
NHE
11852 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11853 vcpu->arch.efer = vmcs12->host_ia32_efer;
d1fa0352 11854 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4704d0be
NHE
11855 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11856 else
11857 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11858 vmx_set_efer(vcpu, vcpu->arch.efer);
11859
11860 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11861 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
1adfa76a 11862 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4704d0be
NHE
11863 /*
11864 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
bd7e5b08
PB
11865 * actually changed, because vmx_set_cr0 refers to efer set above.
11866 *
11867 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11868 * (KVM doesn't change it);
4704d0be 11869 */
bd7e5b08 11870 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
9e3e4dbf 11871 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4704d0be 11872
bd7e5b08 11873 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4704d0be 11874 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
8eb3f87d 11875 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4704d0be 11876
5af41573 11877 load_vmcs12_mmu_host_state(vcpu, vmcs12);
feaf0c7d 11878
4704d0be
NHE
11879 if (enable_vpid) {
11880 /*
11881 * Trivially support vpid by letting L2s share their parent
11882 * L1's vpid. TODO: move to a more elaborate solution, giving
11883 * each L2 its own vpid and exposing the vpid feature to L1.
11884 */
11885 vmx_flush_tlb(vcpu);
11886 }
06a5524f
WV
11887 /* Restore posted intr vector. */
11888 if (nested_cpu_has_posted_intr(vmcs12))
11889 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4704d0be
NHE
11890
11891 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11892 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11893 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11894 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11895 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
21f2d551
LP
11896 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11897 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4704d0be 11898
36be0b9d
PB
11899 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11900 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11901 vmcs_write64(GUEST_BNDCFGS, 0);
11902
44811c02 11903 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4704d0be 11904 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
44811c02
JK
11905 vcpu->arch.pat = vmcs12->host_ia32_pat;
11906 }
4704d0be
NHE
11907 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11908 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11909 vmcs12->host_ia32_perf_global_ctrl);
503cd0c5 11910
21feb4eb
ACL
11911 /* Set L1 segment info according to Intel SDM
11912 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11913 seg = (struct kvm_segment) {
11914 .base = 0,
11915 .limit = 0xFFFFFFFF,
11916 .selector = vmcs12->host_cs_selector,
11917 .type = 11,
11918 .present = 1,
11919 .s = 1,
11920 .g = 1
11921 };
11922 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11923 seg.l = 1;
11924 else
11925 seg.db = 1;
11926 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11927 seg = (struct kvm_segment) {
11928 .base = 0,
11929 .limit = 0xFFFFFFFF,
11930 .type = 3,
11931 .present = 1,
11932 .s = 1,
11933 .db = 1,
11934 .g = 1
11935 };
11936 seg.selector = vmcs12->host_ds_selector;
11937 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11938 seg.selector = vmcs12->host_es_selector;
11939 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11940 seg.selector = vmcs12->host_ss_selector;
11941 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11942 seg.selector = vmcs12->host_fs_selector;
11943 seg.base = vmcs12->host_fs_base;
11944 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11945 seg.selector = vmcs12->host_gs_selector;
11946 seg.base = vmcs12->host_gs_base;
11947 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11948 seg = (struct kvm_segment) {
205befd9 11949 .base = vmcs12->host_tr_base,
21feb4eb
ACL
11950 .limit = 0x67,
11951 .selector = vmcs12->host_tr_selector,
11952 .type = 11,
11953 .present = 1
11954 };
11955 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11956
503cd0c5
JK
11957 kvm_set_dr(vcpu, 7, 0x400);
11958 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
ff651cb6 11959
3af18d9c 11960 if (cpu_has_vmx_msr_bitmap())
4b0be90f 11961 vmx_update_msr_bitmap(vcpu);
3af18d9c 11962
ff651cb6
WV
11963 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11964 vmcs12->vm_exit_msr_load_count))
11965 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4704d0be
NHE
11966}
11967
11968/*
11969 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11970 * and modify vmcs12 to make it see what it would expect to see there if
11971 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11972 */
533558bc
JK
11973static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11974 u32 exit_intr_info,
11975 unsigned long exit_qualification)
4704d0be
NHE
11976{
11977 struct vcpu_vmx *vmx = to_vmx(vcpu);
4704d0be
NHE
11978 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11979
5f3d5799
JK
11980 /* trying to cancel vmlaunch/vmresume is a bug */
11981 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11982
4f350c6d
JM
11983 /*
11984 * The only expected VM-instruction error is "VM entry with
11985 * invalid control field(s)." Anything else indicates a
11986 * problem with L0.
11987 */
11988 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
11989 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
11990
4704d0be 11991 leave_guest_mode(vcpu);
4704d0be 11992
f7f5542f
KA
11993 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11994 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11995
4f350c6d 11996 if (likely(!vmx->fail)) {
72e9cbdb
LP
11997 if (exit_reason == -1)
11998 sync_vmcs12(vcpu, vmcs12);
11999 else
12000 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12001 exit_qualification);
ff651cb6 12002
4f350c6d
JM
12003 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12004 vmcs12->vm_exit_msr_store_count))
12005 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
12006 }
cf3215d9 12007
1279a6b1 12008 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
8391ce44
PB
12009 vm_entry_controls_reset_shadow(vmx);
12010 vm_exit_controls_reset_shadow(vmx);
36c3cc42
JK
12011 vmx_segment_cache_clear(vmx);
12012
9314006d 12013 /* Update any VMCS fields that might have changed while L2 ran */
6e3dedb6
KRW
12014 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12015 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
ea26e4ec 12016 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
9314006d
PB
12017 if (vmx->hv_deadline_tsc == -1)
12018 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12019 PIN_BASED_VMX_PREEMPTION_TIMER);
12020 else
12021 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12022 PIN_BASED_VMX_PREEMPTION_TIMER);
c95ba92a
PF
12023 if (kvm_has_tsc_control)
12024 decache_tsc_multiplier(vmx);
4704d0be 12025
dccbfcf5
RK
12026 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
12027 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
12028 vmx_set_virtual_x2apic_mode(vcpu,
12029 vcpu->arch.apic_base & X2APIC_ENABLE);
fb6c8198
JM
12030 } else if (!nested_cpu_has_ept(vmcs12) &&
12031 nested_cpu_has2(vmcs12,
12032 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
5ad2874a 12033 vmx_flush_tlb(vcpu);
dccbfcf5 12034 }
4704d0be
NHE
12035
12036 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12037 vmx->host_rsp = 0;
12038
12039 /* Unpin physical memory we referred to in vmcs02 */
12040 if (vmx->nested.apic_access_page) {
53a70daf 12041 kvm_release_page_dirty(vmx->nested.apic_access_page);
48d89b92 12042 vmx->nested.apic_access_page = NULL;
4704d0be 12043 }
a7c0b07d 12044 if (vmx->nested.virtual_apic_page) {
53a70daf 12045 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
48d89b92 12046 vmx->nested.virtual_apic_page = NULL;
a7c0b07d 12047 }
705699a1
WV
12048 if (vmx->nested.pi_desc_page) {
12049 kunmap(vmx->nested.pi_desc_page);
53a70daf 12050 kvm_release_page_dirty(vmx->nested.pi_desc_page);
705699a1
WV
12051 vmx->nested.pi_desc_page = NULL;
12052 vmx->nested.pi_desc = NULL;
12053 }
4704d0be 12054
38b99173
TC
12055 /*
12056 * We are now running in L2, mmu_notifier will force to reload the
12057 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12058 */
c83b6d15 12059 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
38b99173 12060
72e9cbdb 12061 if (enable_shadow_vmcs && exit_reason != -1)
012f83cb 12062 vmx->nested.sync_shadow_vmcs = true;
b6b8a145
JK
12063
12064 /* in case we halted in L2 */
12065 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4f350c6d
JM
12066
12067 if (likely(!vmx->fail)) {
12068 /*
12069 * TODO: SDM says that with acknowledge interrupt on
12070 * exit, bit 31 of the VM-exit interrupt information
12071 * (valid interrupt) is always set to 1 on
12072 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12073 * need kvm_cpu_has_interrupt(). See the commit
12074 * message for details.
12075 */
12076 if (nested_exit_intr_ack_set(vcpu) &&
12077 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12078 kvm_cpu_has_interrupt(vcpu)) {
12079 int irq = kvm_cpu_get_interrupt(vcpu);
12080 WARN_ON(irq < 0);
12081 vmcs12->vm_exit_intr_info = irq |
12082 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12083 }
12084
72e9cbdb
LP
12085 if (exit_reason != -1)
12086 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12087 vmcs12->exit_qualification,
12088 vmcs12->idt_vectoring_info_field,
12089 vmcs12->vm_exit_intr_info,
12090 vmcs12->vm_exit_intr_error_code,
12091 KVM_ISA_VMX);
4f350c6d
JM
12092
12093 load_vmcs12_host_state(vcpu, vmcs12);
12094
12095 return;
12096 }
12097
12098 /*
12099 * After an early L2 VM-entry failure, we're now back
12100 * in L1 which thinks it just finished a VMLAUNCH or
12101 * VMRESUME instruction, so we need to set the failure
12102 * flag and the VM-instruction error field of the VMCS
12103 * accordingly.
12104 */
12105 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
5af41573
WL
12106
12107 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12108
4f350c6d
JM
12109 /*
12110 * The emulated instruction was already skipped in
12111 * nested_vmx_run, but the updated RIP was never
12112 * written back to the vmcs01.
12113 */
12114 skip_emulated_instruction(vcpu);
12115 vmx->fail = 0;
4704d0be
NHE
12116}
12117
42124925
JK
12118/*
12119 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12120 */
12121static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12122{
2f707d97
WL
12123 if (is_guest_mode(vcpu)) {
12124 to_vmx(vcpu)->nested.nested_run_pending = 0;
533558bc 12125 nested_vmx_vmexit(vcpu, -1, 0, 0);
2f707d97 12126 }
42124925
JK
12127 free_nested(to_vmx(vcpu));
12128}
12129
7c177938
NHE
12130/*
12131 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12132 * 23.7 "VM-entry failures during or after loading guest state" (this also
12133 * lists the acceptable exit-reason and exit-qualification parameters).
12134 * It should only be called before L2 actually succeeded to run, and when
12135 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12136 */
12137static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12138 struct vmcs12 *vmcs12,
12139 u32 reason, unsigned long qualification)
12140{
12141 load_vmcs12_host_state(vcpu, vmcs12);
12142 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12143 vmcs12->exit_qualification = qualification;
12144 nested_vmx_succeed(vcpu);
012f83cb
AG
12145 if (enable_shadow_vmcs)
12146 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
7c177938
NHE
12147}
12148
8a76d7f2
JR
12149static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12150 struct x86_instruction_info *info,
12151 enum x86_intercept_stage stage)
12152{
12153 return X86EMUL_CONTINUE;
12154}
12155
64672c95
YJ
12156#ifdef CONFIG_X86_64
12157/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12158static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12159 u64 divisor, u64 *result)
12160{
12161 u64 low = a << shift, high = a >> (64 - shift);
12162
12163 /* To avoid the overflow on divq */
12164 if (high >= divisor)
12165 return 1;
12166
12167 /* Low hold the result, high hold rem which is discarded */
12168 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12169 "rm" (divisor), "0" (low), "1" (high));
12170 *result = low;
12171
12172 return 0;
12173}
12174
12175static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12176{
12177 struct vcpu_vmx *vmx = to_vmx(vcpu);
9175d2e9
PB
12178 u64 tscl = rdtsc();
12179 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12180 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
64672c95
YJ
12181
12182 /* Convert to host delta tsc if tsc scaling is enabled */
12183 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12184 u64_shl_div_u64(delta_tsc,
12185 kvm_tsc_scaling_ratio_frac_bits,
12186 vcpu->arch.tsc_scaling_ratio,
12187 &delta_tsc))
12188 return -ERANGE;
12189
12190 /*
12191 * If the delta tsc can't fit in the 32 bit after the multi shift,
12192 * we can't use the preemption timer.
12193 * It's possible that it fits on later vmentries, but checking
12194 * on every vmentry is costly so we just use an hrtimer.
12195 */
12196 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12197 return -ERANGE;
12198
12199 vmx->hv_deadline_tsc = tscl + delta_tsc;
12200 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12201 PIN_BASED_VMX_PREEMPTION_TIMER);
c8533544
WL
12202
12203 return delta_tsc == 0;
64672c95
YJ
12204}
12205
12206static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12207{
12208 struct vcpu_vmx *vmx = to_vmx(vcpu);
12209 vmx->hv_deadline_tsc = -1;
12210 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12211 PIN_BASED_VMX_PREEMPTION_TIMER);
12212}
12213#endif
12214
48d89b92 12215static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
ae97a3b8 12216{
b4a2d31d
RK
12217 if (ple_gap)
12218 shrink_ple_window(vcpu);
ae97a3b8
RK
12219}
12220
843e4330
KH
12221static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12222 struct kvm_memory_slot *slot)
12223{
12224 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12225 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12226}
12227
12228static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12229 struct kvm_memory_slot *slot)
12230{
12231 kvm_mmu_slot_set_dirty(kvm, slot);
12232}
12233
12234static void vmx_flush_log_dirty(struct kvm *kvm)
12235{
12236 kvm_flush_pml_buffers(kvm);
12237}
12238
c5f983f6
BD
12239static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12240{
12241 struct vmcs12 *vmcs12;
12242 struct vcpu_vmx *vmx = to_vmx(vcpu);
12243 gpa_t gpa;
12244 struct page *page = NULL;
12245 u64 *pml_address;
12246
12247 if (is_guest_mode(vcpu)) {
12248 WARN_ON_ONCE(vmx->nested.pml_full);
12249
12250 /*
12251 * Check if PML is enabled for the nested guest.
12252 * Whether eptp bit 6 is set is already checked
12253 * as part of A/D emulation.
12254 */
12255 vmcs12 = get_vmcs12(vcpu);
12256 if (!nested_cpu_has_pml(vmcs12))
12257 return 0;
12258
4769886b 12259 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
c5f983f6
BD
12260 vmx->nested.pml_full = true;
12261 return 1;
12262 }
12263
12264 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12265
5e2f30b7
DH
12266 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12267 if (is_error_page(page))
c5f983f6
BD
12268 return 0;
12269
12270 pml_address = kmap(page);
12271 pml_address[vmcs12->guest_pml_index--] = gpa;
12272 kunmap(page);
53a70daf 12273 kvm_release_page_clean(page);
c5f983f6
BD
12274 }
12275
12276 return 0;
12277}
12278
843e4330
KH
12279static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12280 struct kvm_memory_slot *memslot,
12281 gfn_t offset, unsigned long mask)
12282{
12283 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12284}
12285
cd39e117
PB
12286static void __pi_post_block(struct kvm_vcpu *vcpu)
12287{
12288 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12289 struct pi_desc old, new;
12290 unsigned int dest;
cd39e117
PB
12291
12292 do {
12293 old.control = new.control = pi_desc->control;
8b306e2f
PB
12294 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12295 "Wakeup handler not enabled while the VCPU is blocked\n");
cd39e117
PB
12296
12297 dest = cpu_physical_id(vcpu->cpu);
12298
12299 if (x2apic_enabled())
12300 new.ndst = dest;
12301 else
12302 new.ndst = (dest << 8) & 0xFF00;
12303
cd39e117
PB
12304 /* set 'NV' to 'notification vector' */
12305 new.nv = POSTED_INTR_VECTOR;
c0a1666b
PB
12306 } while (cmpxchg64(&pi_desc->control, old.control,
12307 new.control) != old.control);
cd39e117 12308
8b306e2f
PB
12309 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12310 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
cd39e117 12311 list_del(&vcpu->blocked_vcpu_list);
8b306e2f 12312 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
cd39e117
PB
12313 vcpu->pre_pcpu = -1;
12314 }
12315}
12316
bf9f6ac8
FW
12317/*
12318 * This routine does the following things for vCPU which is going
12319 * to be blocked if VT-d PI is enabled.
12320 * - Store the vCPU to the wakeup list, so when interrupts happen
12321 * we can find the right vCPU to wake up.
12322 * - Change the Posted-interrupt descriptor as below:
12323 * 'NDST' <-- vcpu->pre_pcpu
12324 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12325 * - If 'ON' is set during this process, which means at least one
12326 * interrupt is posted for this vCPU, we cannot block it, in
12327 * this case, return 1, otherwise, return 0.
12328 *
12329 */
bc22512b 12330static int pi_pre_block(struct kvm_vcpu *vcpu)
bf9f6ac8 12331{
bf9f6ac8
FW
12332 unsigned int dest;
12333 struct pi_desc old, new;
12334 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12335
12336 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
a0052191
YZ
12337 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12338 !kvm_vcpu_apicv_active(vcpu))
bf9f6ac8
FW
12339 return 0;
12340
8b306e2f
PB
12341 WARN_ON(irqs_disabled());
12342 local_irq_disable();
12343 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12344 vcpu->pre_pcpu = vcpu->cpu;
12345 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12346 list_add_tail(&vcpu->blocked_vcpu_list,
12347 &per_cpu(blocked_vcpu_on_cpu,
12348 vcpu->pre_pcpu));
12349 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12350 }
bf9f6ac8
FW
12351
12352 do {
12353 old.control = new.control = pi_desc->control;
12354
bf9f6ac8
FW
12355 WARN((pi_desc->sn == 1),
12356 "Warning: SN field of posted-interrupts "
12357 "is set before blocking\n");
12358
12359 /*
12360 * Since vCPU can be preempted during this process,
12361 * vcpu->cpu could be different with pre_pcpu, we
12362 * need to set pre_pcpu as the destination of wakeup
12363 * notification event, then we can find the right vCPU
12364 * to wakeup in wakeup handler if interrupts happen
12365 * when the vCPU is in blocked state.
12366 */
12367 dest = cpu_physical_id(vcpu->pre_pcpu);
12368
12369 if (x2apic_enabled())
12370 new.ndst = dest;
12371 else
12372 new.ndst = (dest << 8) & 0xFF00;
12373
12374 /* set 'NV' to 'wakeup vector' */
12375 new.nv = POSTED_INTR_WAKEUP_VECTOR;
c0a1666b
PB
12376 } while (cmpxchg64(&pi_desc->control, old.control,
12377 new.control) != old.control);
bf9f6ac8 12378
8b306e2f
PB
12379 /* We should not block the vCPU if an interrupt is posted for it. */
12380 if (pi_test_on(pi_desc) == 1)
12381 __pi_post_block(vcpu);
12382
12383 local_irq_enable();
12384 return (vcpu->pre_pcpu == -1);
bf9f6ac8
FW
12385}
12386
bc22512b
YJ
12387static int vmx_pre_block(struct kvm_vcpu *vcpu)
12388{
12389 if (pi_pre_block(vcpu))
12390 return 1;
12391
64672c95
YJ
12392 if (kvm_lapic_hv_timer_in_use(vcpu))
12393 kvm_lapic_switch_to_sw_timer(vcpu);
12394
bc22512b
YJ
12395 return 0;
12396}
12397
12398static void pi_post_block(struct kvm_vcpu *vcpu)
bf9f6ac8 12399{
8b306e2f 12400 if (vcpu->pre_pcpu == -1)
bf9f6ac8
FW
12401 return;
12402
8b306e2f
PB
12403 WARN_ON(irqs_disabled());
12404 local_irq_disable();
cd39e117 12405 __pi_post_block(vcpu);
8b306e2f 12406 local_irq_enable();
bf9f6ac8
FW
12407}
12408
bc22512b
YJ
12409static void vmx_post_block(struct kvm_vcpu *vcpu)
12410{
64672c95
YJ
12411 if (kvm_x86_ops->set_hv_timer)
12412 kvm_lapic_switch_to_hv_timer(vcpu);
12413
bc22512b
YJ
12414 pi_post_block(vcpu);
12415}
12416
efc64404
FW
12417/*
12418 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12419 *
12420 * @kvm: kvm
12421 * @host_irq: host irq of the interrupt
12422 * @guest_irq: gsi of the interrupt
12423 * @set: set or unset PI
12424 * returns 0 on success, < 0 on failure
12425 */
12426static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12427 uint32_t guest_irq, bool set)
12428{
12429 struct kvm_kernel_irq_routing_entry *e;
12430 struct kvm_irq_routing_table *irq_rt;
12431 struct kvm_lapic_irq irq;
12432 struct kvm_vcpu *vcpu;
12433 struct vcpu_data vcpu_info;
3a8b0677 12434 int idx, ret = 0;
efc64404
FW
12435
12436 if (!kvm_arch_has_assigned_device(kvm) ||
a0052191
YZ
12437 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12438 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
efc64404
FW
12439 return 0;
12440
12441 idx = srcu_read_lock(&kvm->irq_srcu);
12442 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
3a8b0677
JS
12443 if (guest_irq >= irq_rt->nr_rt_entries ||
12444 hlist_empty(&irq_rt->map[guest_irq])) {
12445 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12446 guest_irq, irq_rt->nr_rt_entries);
12447 goto out;
12448 }
efc64404
FW
12449
12450 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12451 if (e->type != KVM_IRQ_ROUTING_MSI)
12452 continue;
12453 /*
12454 * VT-d PI cannot support posting multicast/broadcast
12455 * interrupts to a vCPU, we still use interrupt remapping
12456 * for these kind of interrupts.
12457 *
12458 * For lowest-priority interrupts, we only support
12459 * those with single CPU as the destination, e.g. user
12460 * configures the interrupts via /proc/irq or uses
12461 * irqbalance to make the interrupts single-CPU.
12462 *
12463 * We will support full lowest-priority interrupt later.
12464 */
12465
37131313 12466 kvm_set_msi_irq(kvm, e, &irq);
23a1c257
FW
12467 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12468 /*
12469 * Make sure the IRTE is in remapped mode if
12470 * we don't handle it in posted mode.
12471 */
12472 ret = irq_set_vcpu_affinity(host_irq, NULL);
12473 if (ret < 0) {
12474 printk(KERN_INFO
12475 "failed to back to remapped mode, irq: %u\n",
12476 host_irq);
12477 goto out;
12478 }
12479
efc64404 12480 continue;
23a1c257 12481 }
efc64404
FW
12482
12483 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12484 vcpu_info.vector = irq.vector;
12485
4edf01c6 12486 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
efc64404
FW
12487 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12488
12489 if (set)
12490 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
dc91f2eb 12491 else
efc64404 12492 ret = irq_set_vcpu_affinity(host_irq, NULL);
efc64404
FW
12493
12494 if (ret < 0) {
12495 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12496 __func__);
12497 goto out;
12498 }
12499 }
12500
12501 ret = 0;
12502out:
12503 srcu_read_unlock(&kvm->irq_srcu, idx);
12504 return ret;
12505}
12506
c45dcc71
AR
12507static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12508{
12509 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12510 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12511 FEATURE_CONTROL_LMCE;
12512 else
12513 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12514 ~FEATURE_CONTROL_LMCE;
12515}
12516
72d7b374
LP
12517static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12518{
72e9cbdb
LP
12519 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12520 if (to_vmx(vcpu)->nested.nested_run_pending)
12521 return 0;
72d7b374
LP
12522 return 1;
12523}
12524
0234bf88
LP
12525static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12526{
72e9cbdb
LP
12527 struct vcpu_vmx *vmx = to_vmx(vcpu);
12528
12529 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12530 if (vmx->nested.smm.guest_mode)
12531 nested_vmx_vmexit(vcpu, -1, 0, 0);
12532
12533 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12534 vmx->nested.vmxon = false;
0234bf88
LP
12535 return 0;
12536}
12537
12538static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12539{
72e9cbdb
LP
12540 struct vcpu_vmx *vmx = to_vmx(vcpu);
12541 int ret;
12542
12543 if (vmx->nested.smm.vmxon) {
12544 vmx->nested.vmxon = true;
12545 vmx->nested.smm.vmxon = false;
12546 }
12547
12548 if (vmx->nested.smm.guest_mode) {
12549 vcpu->arch.hflags &= ~HF_SMM_MASK;
12550 ret = enter_vmx_non_root_mode(vcpu, false);
12551 vcpu->arch.hflags |= HF_SMM_MASK;
12552 if (ret)
12553 return ret;
12554
12555 vmx->nested.smm.guest_mode = false;
12556 }
0234bf88
LP
12557 return 0;
12558}
12559
cc3d967f
LP
12560static int enable_smi_window(struct kvm_vcpu *vcpu)
12561{
12562 return 0;
12563}
12564
404f6aac 12565static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
6aa8b732
AK
12566 .cpu_has_kvm_support = cpu_has_kvm_support,
12567 .disabled_by_bios = vmx_disabled_by_bios,
12568 .hardware_setup = hardware_setup,
12569 .hardware_unsetup = hardware_unsetup,
002c7f7c 12570 .check_processor_compatibility = vmx_check_processor_compat,
6aa8b732
AK
12571 .hardware_enable = hardware_enable,
12572 .hardware_disable = hardware_disable,
04547156 12573 .cpu_has_accelerated_tpr = report_flexpriority,
4d5c8a07 12574 .has_emulated_msr = vmx_has_emulated_msr,
6aa8b732 12575
275b72a9
KRW
12576 .vm_init = vmx_vm_init,
12577
6aa8b732
AK
12578 .vcpu_create = vmx_create_vcpu,
12579 .vcpu_free = vmx_free_vcpu,
04d2cc77 12580 .vcpu_reset = vmx_vcpu_reset,
6aa8b732 12581
04d2cc77 12582 .prepare_guest_switch = vmx_save_host_state,
6aa8b732
AK
12583 .vcpu_load = vmx_vcpu_load,
12584 .vcpu_put = vmx_vcpu_put,
12585
a96036b8 12586 .update_bp_intercept = update_exception_bitmap,
ab1bebf8 12587 .get_msr_feature = vmx_get_msr_feature,
6aa8b732
AK
12588 .get_msr = vmx_get_msr,
12589 .set_msr = vmx_set_msr,
12590 .get_segment_base = vmx_get_segment_base,
12591 .get_segment = vmx_get_segment,
12592 .set_segment = vmx_set_segment,
2e4d2653 12593 .get_cpl = vmx_get_cpl,
6aa8b732 12594 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
e8467fda 12595 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
aff48baa 12596 .decache_cr3 = vmx_decache_cr3,
25c4c276 12597 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
6aa8b732 12598 .set_cr0 = vmx_set_cr0,
6aa8b732
AK
12599 .set_cr3 = vmx_set_cr3,
12600 .set_cr4 = vmx_set_cr4,
6aa8b732 12601 .set_efer = vmx_set_efer,
6aa8b732
AK
12602 .get_idt = vmx_get_idt,
12603 .set_idt = vmx_set_idt,
12604 .get_gdt = vmx_get_gdt,
12605 .set_gdt = vmx_set_gdt,
73aaf249
JK
12606 .get_dr6 = vmx_get_dr6,
12607 .set_dr6 = vmx_set_dr6,
020df079 12608 .set_dr7 = vmx_set_dr7,
81908bf4 12609 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
5fdbf976 12610 .cache_reg = vmx_cache_reg,
6aa8b732
AK
12611 .get_rflags = vmx_get_rflags,
12612 .set_rflags = vmx_set_rflags,
be94f6b7 12613
6aa8b732 12614 .tlb_flush = vmx_flush_tlb,
6aa8b732 12615
6aa8b732 12616 .run = vmx_vcpu_run,
6062d012 12617 .handle_exit = vmx_handle_exit,
6aa8b732 12618 .skip_emulated_instruction = skip_emulated_instruction,
2809f5d2
GC
12619 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12620 .get_interrupt_shadow = vmx_get_interrupt_shadow,
102d8325 12621 .patch_hypercall = vmx_patch_hypercall,
2a8067f1 12622 .set_irq = vmx_inject_irq,
95ba8273 12623 .set_nmi = vmx_inject_nmi,
298101da 12624 .queue_exception = vmx_queue_exception,
b463a6f7 12625 .cancel_injection = vmx_cancel_injection,
78646121 12626 .interrupt_allowed = vmx_interrupt_allowed,
95ba8273 12627 .nmi_allowed = vmx_nmi_allowed,
3cfc3092
JK
12628 .get_nmi_mask = vmx_get_nmi_mask,
12629 .set_nmi_mask = vmx_set_nmi_mask,
95ba8273
GN
12630 .enable_nmi_window = enable_nmi_window,
12631 .enable_irq_window = enable_irq_window,
12632 .update_cr8_intercept = update_cr8_intercept,
8d14695f 12633 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
38b99173 12634 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
d62caabb
AS
12635 .get_enable_apicv = vmx_get_enable_apicv,
12636 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
c7c9c56c 12637 .load_eoi_exitmap = vmx_load_eoi_exitmap,
967235d3 12638 .apicv_post_state_restore = vmx_apicv_post_state_restore,
c7c9c56c
YZ
12639 .hwapic_irr_update = vmx_hwapic_irr_update,
12640 .hwapic_isr_update = vmx_hwapic_isr_update,
a20ed54d
YZ
12641 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12642 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
95ba8273 12643
cbc94022 12644 .set_tss_addr = vmx_set_tss_addr,
67253af5 12645 .get_tdp_level = get_ept_level,
4b12f0de 12646 .get_mt_mask = vmx_get_mt_mask,
229456fc 12647
586f9607 12648 .get_exit_info = vmx_get_exit_info,
586f9607 12649
17cc3935 12650 .get_lpage_level = vmx_get_lpage_level,
0e851880
SY
12651
12652 .cpuid_update = vmx_cpuid_update,
4e47c7a6
SY
12653
12654 .rdtscp_supported = vmx_rdtscp_supported,
ad756a16 12655 .invpcid_supported = vmx_invpcid_supported,
d4330ef2
JR
12656
12657 .set_supported_cpuid = vmx_set_supported_cpuid,
f5f48ee1
SY
12658
12659 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
99e3e30a 12660
f7f5542f 12661 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
99e3e30a 12662 .write_tsc_offset = vmx_write_tsc_offset,
1c97f0a0
JR
12663
12664 .set_tdp_cr3 = vmx_set_cr3,
8a76d7f2
JR
12665
12666 .check_intercept = vmx_check_intercept,
a547c6db 12667 .handle_external_intr = vmx_handle_external_intr,
da8999d3 12668 .mpx_supported = vmx_mpx_supported,
55412b2e 12669 .xsaves_supported = vmx_xsaves_supported,
b6b8a145
JK
12670
12671 .check_nested_events = vmx_check_nested_events,
ae97a3b8
RK
12672
12673 .sched_in = vmx_sched_in,
843e4330
KH
12674
12675 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12676 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12677 .flush_log_dirty = vmx_flush_log_dirty,
12678 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
c5f983f6 12679 .write_log_dirty = vmx_write_pml_buffer,
25462f7f 12680
bf9f6ac8
FW
12681 .pre_block = vmx_pre_block,
12682 .post_block = vmx_post_block,
12683
25462f7f 12684 .pmu_ops = &intel_pmu_ops,
efc64404
FW
12685
12686 .update_pi_irte = vmx_update_pi_irte,
64672c95
YJ
12687
12688#ifdef CONFIG_X86_64
12689 .set_hv_timer = vmx_set_hv_timer,
12690 .cancel_hv_timer = vmx_cancel_hv_timer,
12691#endif
c45dcc71
AR
12692
12693 .setup_mce = vmx_setup_mce,
0234bf88 12694
72d7b374 12695 .smi_allowed = vmx_smi_allowed,
0234bf88
LP
12696 .pre_enter_smm = vmx_pre_enter_smm,
12697 .pre_leave_smm = vmx_pre_leave_smm,
cc3d967f 12698 .enable_smi_window = enable_smi_window,
6aa8b732
AK
12699};
12700
b9cfedcd 12701static void vmx_cleanup_l1d_flush(void)
d665f9fc
PB
12702{
12703 if (vmx_l1d_flush_pages) {
12704 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
12705 vmx_l1d_flush_pages = NULL;
12706 }
b9cfedcd
TG
12707 /* Restore state so sysfs ignores VMX */
12708 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
1749555e
KRW
12709}
12710
1ead4979
TG
12711static void vmx_exit(void)
12712{
12713#ifdef CONFIG_KEXEC_CORE
12714 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
12715 synchronize_rcu();
12716#endif
12717
12718 kvm_exit();
12719
12720 vmx_cleanup_l1d_flush();
12721}
12722module_exit(vmx_exit)
12723
6aa8b732
AK
12724static int __init vmx_init(void)
12725{
1749555e
KRW
12726 int r;
12727
1ead4979
TG
12728 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
12729 __alignof__(struct vcpu_vmx), THIS_MODULE);
d665f9fc
PB
12730 if (r)
12731 return r;
1749555e 12732
1ead4979 12733 /*
2bcd5b01
TG
12734 * Must be called after kvm_init() so enable_ept is properly set
12735 * up. Hand the parameter mitigation value in which was stored in
12736 * the pre module init parser. If no parameter was given, it will
12737 * contain 'auto' which will be turned into the default 'cond'
12738 * mitigation mode.
1ead4979 12739 */
2bcd5b01
TG
12740 if (boot_cpu_has(X86_BUG_L1TF)) {
12741 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
12742 if (r) {
12743 vmx_exit();
12744 return r;
12745 }
d665f9fc 12746 }
25c5f225 12747
2965faa5 12748#ifdef CONFIG_KEXEC_CORE
8f536b76
ZY
12749 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12750 crash_vmclear_local_loaded_vmcss);
12751#endif
12752
fdef3ad1 12753 return 0;
6aa8b732 12754}
6aa8b732 12755module_init(vmx_init)