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