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