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