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