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