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