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