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