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