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