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