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