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