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