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