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