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