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