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