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