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