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