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