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