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