]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kvm/vmx.c
KVM: nVMX: Shadow-vmcs control fields/bits
[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"
e495606d 22
edf88417 23#include <linux/kvm_host.h>
6aa8b732 24#include <linux/module.h>
9d8f549d 25#include <linux/kernel.h>
6aa8b732
AK
26#include <linux/mm.h>
27#include <linux/highmem.h>
e8edc6e0 28#include <linux/sched.h>
c7addb90 29#include <linux/moduleparam.h>
e9bda3b3 30#include <linux/mod_devicetable.h>
229456fc 31#include <linux/ftrace_event.h>
5a0e3ad6 32#include <linux/slab.h>
cafd6659 33#include <linux/tboot.h>
5fdbf976 34#include "kvm_cache_regs.h"
35920a35 35#include "x86.h"
e495606d 36
6aa8b732 37#include <asm/io.h>
3b3be0d1 38#include <asm/desc.h>
13673a90 39#include <asm/vmx.h>
6210e37b 40#include <asm/virtext.h>
a0861c02 41#include <asm/mce.h>
2acf923e
DC
42#include <asm/i387.h>
43#include <asm/xcr.h>
d7cd9796 44#include <asm/perf_event.h>
8f536b76 45#include <asm/kexec.h>
6aa8b732 46
229456fc
MT
47#include "trace.h"
48
4ecac3fd 49#define __ex(x) __kvm_handle_fault_on_reboot(x)
5e520e62
AK
50#define __ex_clear(x, reg) \
51 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
4ecac3fd 52
6aa8b732
AK
53MODULE_AUTHOR("Qumranet");
54MODULE_LICENSE("GPL");
55
e9bda3b3
JT
56static const struct x86_cpu_id vmx_cpu_id[] = {
57 X86_FEATURE_MATCH(X86_FEATURE_VMX),
58 {}
59};
60MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
61
476bc001 62static bool __read_mostly enable_vpid = 1;
736caefe 63module_param_named(vpid, enable_vpid, bool, 0444);
2384d2b3 64
476bc001 65static bool __read_mostly flexpriority_enabled = 1;
736caefe 66module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
4c9fc8ef 67
476bc001 68static bool __read_mostly enable_ept = 1;
736caefe 69module_param_named(ept, enable_ept, bool, S_IRUGO);
d56f546d 70
476bc001 71static bool __read_mostly enable_unrestricted_guest = 1;
3a624e29
NK
72module_param_named(unrestricted_guest,
73 enable_unrestricted_guest, bool, S_IRUGO);
74
83c3a331
XH
75static bool __read_mostly enable_ept_ad_bits = 1;
76module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
77
a27685c3 78static bool __read_mostly emulate_invalid_guest_state = true;
c1f8bc04 79module_param(emulate_invalid_guest_state, bool, S_IRUGO);
04fa4d32 80
476bc001 81static bool __read_mostly vmm_exclusive = 1;
b923e62e
DX
82module_param(vmm_exclusive, bool, S_IRUGO);
83
476bc001 84static bool __read_mostly fasteoi = 1;
58fbbf26
KT
85module_param(fasteoi, bool, S_IRUGO);
86
5a71785d 87static bool __read_mostly enable_apicv = 1;
01e439be 88module_param(enable_apicv, bool, S_IRUGO);
83d4c286 89
801d3424
NHE
90/*
91 * If nested=1, nested virtualization is supported, i.e., guests may use
92 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
93 * use VMX instructions.
94 */
476bc001 95static bool __read_mostly nested = 0;
801d3424
NHE
96module_param(nested, bool, S_IRUGO);
97
5037878e
GN
98#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
99#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
cdc0e244
AK
100#define KVM_VM_CR0_ALWAYS_ON \
101 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
4c38609a
AK
102#define KVM_CR4_GUEST_OWNED_BITS \
103 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
104 | X86_CR4_OSXMMEXCPT)
105
cdc0e244
AK
106#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
107#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
108
78ac8b47
AK
109#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
110
4b8d54f9
ZE
111/*
112 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
113 * ple_gap: upper bound on the amount of time between two successive
114 * executions of PAUSE in a loop. Also indicate if ple enabled.
00c25bce 115 * According to test, this time is usually smaller than 128 cycles.
4b8d54f9
ZE
116 * ple_window: upper bound on the amount of time a guest is allowed to execute
117 * in a PAUSE loop. Tests indicate that most spinlocks are held for
118 * less than 2^12 cycles
119 * Time is measured based on a counter that runs at the same rate as the TSC,
120 * refer SDM volume 3b section 21.6.13 & 22.1.3.
121 */
00c25bce 122#define KVM_VMX_DEFAULT_PLE_GAP 128
4b8d54f9
ZE
123#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
124static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
125module_param(ple_gap, int, S_IRUGO);
126
127static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
128module_param(ple_window, int, S_IRUGO);
129
83287ea4
AK
130extern const ulong vmx_return;
131
8bf00a52 132#define NR_AUTOLOAD_MSRS 8
ff2f6fe9 133#define VMCS02_POOL_SIZE 1
61d2ef2c 134
a2fa3e9f
GH
135struct vmcs {
136 u32 revision_id;
137 u32 abort;
138 char data[0];
139};
140
d462b819
NHE
141/*
142 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
143 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
144 * loaded on this CPU (so we can clear them if the CPU goes down).
145 */
146struct loaded_vmcs {
147 struct vmcs *vmcs;
148 int cpu;
149 int launched;
150 struct list_head loaded_vmcss_on_cpu_link;
151};
152
26bb0981
AK
153struct shared_msr_entry {
154 unsigned index;
155 u64 data;
d5696725 156 u64 mask;
26bb0981
AK
157};
158
a9d30f33
NHE
159/*
160 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
161 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
162 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
163 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
164 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
165 * More than one of these structures may exist, if L1 runs multiple L2 guests.
166 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
167 * underlying hardware which will be used to run L2.
168 * This structure is packed to ensure that its layout is identical across
169 * machines (necessary for live migration).
170 * If there are changes in this struct, VMCS12_REVISION must be changed.
171 */
22bd0358 172typedef u64 natural_width;
a9d30f33
NHE
173struct __packed vmcs12 {
174 /* According to the Intel spec, a VMCS region must start with the
175 * following two fields. Then follow implementation-specific data.
176 */
177 u32 revision_id;
178 u32 abort;
22bd0358 179
27d6c865
NHE
180 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
181 u32 padding[7]; /* room for future expansion */
182
22bd0358
NHE
183 u64 io_bitmap_a;
184 u64 io_bitmap_b;
185 u64 msr_bitmap;
186 u64 vm_exit_msr_store_addr;
187 u64 vm_exit_msr_load_addr;
188 u64 vm_entry_msr_load_addr;
189 u64 tsc_offset;
190 u64 virtual_apic_page_addr;
191 u64 apic_access_addr;
192 u64 ept_pointer;
193 u64 guest_physical_address;
194 u64 vmcs_link_pointer;
195 u64 guest_ia32_debugctl;
196 u64 guest_ia32_pat;
197 u64 guest_ia32_efer;
198 u64 guest_ia32_perf_global_ctrl;
199 u64 guest_pdptr0;
200 u64 guest_pdptr1;
201 u64 guest_pdptr2;
202 u64 guest_pdptr3;
203 u64 host_ia32_pat;
204 u64 host_ia32_efer;
205 u64 host_ia32_perf_global_ctrl;
206 u64 padding64[8]; /* room for future expansion */
207 /*
208 * To allow migration of L1 (complete with its L2 guests) between
209 * machines of different natural widths (32 or 64 bit), we cannot have
210 * unsigned long fields with no explict size. We use u64 (aliased
211 * natural_width) instead. Luckily, x86 is little-endian.
212 */
213 natural_width cr0_guest_host_mask;
214 natural_width cr4_guest_host_mask;
215 natural_width cr0_read_shadow;
216 natural_width cr4_read_shadow;
217 natural_width cr3_target_value0;
218 natural_width cr3_target_value1;
219 natural_width cr3_target_value2;
220 natural_width cr3_target_value3;
221 natural_width exit_qualification;
222 natural_width guest_linear_address;
223 natural_width guest_cr0;
224 natural_width guest_cr3;
225 natural_width guest_cr4;
226 natural_width guest_es_base;
227 natural_width guest_cs_base;
228 natural_width guest_ss_base;
229 natural_width guest_ds_base;
230 natural_width guest_fs_base;
231 natural_width guest_gs_base;
232 natural_width guest_ldtr_base;
233 natural_width guest_tr_base;
234 natural_width guest_gdtr_base;
235 natural_width guest_idtr_base;
236 natural_width guest_dr7;
237 natural_width guest_rsp;
238 natural_width guest_rip;
239 natural_width guest_rflags;
240 natural_width guest_pending_dbg_exceptions;
241 natural_width guest_sysenter_esp;
242 natural_width guest_sysenter_eip;
243 natural_width host_cr0;
244 natural_width host_cr3;
245 natural_width host_cr4;
246 natural_width host_fs_base;
247 natural_width host_gs_base;
248 natural_width host_tr_base;
249 natural_width host_gdtr_base;
250 natural_width host_idtr_base;
251 natural_width host_ia32_sysenter_esp;
252 natural_width host_ia32_sysenter_eip;
253 natural_width host_rsp;
254 natural_width host_rip;
255 natural_width paddingl[8]; /* room for future expansion */
256 u32 pin_based_vm_exec_control;
257 u32 cpu_based_vm_exec_control;
258 u32 exception_bitmap;
259 u32 page_fault_error_code_mask;
260 u32 page_fault_error_code_match;
261 u32 cr3_target_count;
262 u32 vm_exit_controls;
263 u32 vm_exit_msr_store_count;
264 u32 vm_exit_msr_load_count;
265 u32 vm_entry_controls;
266 u32 vm_entry_msr_load_count;
267 u32 vm_entry_intr_info_field;
268 u32 vm_entry_exception_error_code;
269 u32 vm_entry_instruction_len;
270 u32 tpr_threshold;
271 u32 secondary_vm_exec_control;
272 u32 vm_instruction_error;
273 u32 vm_exit_reason;
274 u32 vm_exit_intr_info;
275 u32 vm_exit_intr_error_code;
276 u32 idt_vectoring_info_field;
277 u32 idt_vectoring_error_code;
278 u32 vm_exit_instruction_len;
279 u32 vmx_instruction_info;
280 u32 guest_es_limit;
281 u32 guest_cs_limit;
282 u32 guest_ss_limit;
283 u32 guest_ds_limit;
284 u32 guest_fs_limit;
285 u32 guest_gs_limit;
286 u32 guest_ldtr_limit;
287 u32 guest_tr_limit;
288 u32 guest_gdtr_limit;
289 u32 guest_idtr_limit;
290 u32 guest_es_ar_bytes;
291 u32 guest_cs_ar_bytes;
292 u32 guest_ss_ar_bytes;
293 u32 guest_ds_ar_bytes;
294 u32 guest_fs_ar_bytes;
295 u32 guest_gs_ar_bytes;
296 u32 guest_ldtr_ar_bytes;
297 u32 guest_tr_ar_bytes;
298 u32 guest_interruptibility_info;
299 u32 guest_activity_state;
300 u32 guest_sysenter_cs;
301 u32 host_ia32_sysenter_cs;
0238ea91
JK
302 u32 vmx_preemption_timer_value;
303 u32 padding32[7]; /* room for future expansion */
22bd0358
NHE
304 u16 virtual_processor_id;
305 u16 guest_es_selector;
306 u16 guest_cs_selector;
307 u16 guest_ss_selector;
308 u16 guest_ds_selector;
309 u16 guest_fs_selector;
310 u16 guest_gs_selector;
311 u16 guest_ldtr_selector;
312 u16 guest_tr_selector;
313 u16 host_es_selector;
314 u16 host_cs_selector;
315 u16 host_ss_selector;
316 u16 host_ds_selector;
317 u16 host_fs_selector;
318 u16 host_gs_selector;
319 u16 host_tr_selector;
a9d30f33
NHE
320};
321
322/*
323 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
324 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
325 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
326 */
327#define VMCS12_REVISION 0x11e57ed0
328
329/*
330 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
331 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
332 * current implementation, 4K are reserved to avoid future complications.
333 */
334#define VMCS12_SIZE 0x1000
335
ff2f6fe9
NHE
336/* Used to remember the last vmcs02 used for some recently used vmcs12s */
337struct vmcs02_list {
338 struct list_head list;
339 gpa_t vmptr;
340 struct loaded_vmcs vmcs02;
341};
342
ec378aee
NHE
343/*
344 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
345 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
346 */
347struct nested_vmx {
348 /* Has the level1 guest done vmxon? */
349 bool vmxon;
a9d30f33
NHE
350
351 /* The guest-physical address of the current VMCS L1 keeps for L2 */
352 gpa_t current_vmptr;
353 /* The host-usable pointer to the above */
354 struct page *current_vmcs12_page;
355 struct vmcs12 *current_vmcs12;
ff2f6fe9
NHE
356
357 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
358 struct list_head vmcs02_pool;
359 int vmcs02_num;
fe3ef05c 360 u64 vmcs01_tsc_offset;
644d711a
NHE
361 /* L2 must run next, and mustn't decide to exit to L1. */
362 bool nested_run_pending;
fe3ef05c
NHE
363 /*
364 * Guest pages referred to in vmcs02 with host-physical pointers, so
365 * we must keep them pinned while L2 runs.
366 */
367 struct page *apic_access_page;
ec378aee
NHE
368};
369
01e439be
YZ
370#define POSTED_INTR_ON 0
371/* Posted-Interrupt Descriptor */
372struct pi_desc {
373 u32 pir[8]; /* Posted interrupt requested */
374 u32 control; /* bit 0 of control is outstanding notification bit */
375 u32 rsvd[7];
376} __aligned(64);
377
a20ed54d
YZ
378static bool pi_test_and_set_on(struct pi_desc *pi_desc)
379{
380 return test_and_set_bit(POSTED_INTR_ON,
381 (unsigned long *)&pi_desc->control);
382}
383
384static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
385{
386 return test_and_clear_bit(POSTED_INTR_ON,
387 (unsigned long *)&pi_desc->control);
388}
389
390static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
391{
392 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
393}
394
a2fa3e9f 395struct vcpu_vmx {
fb3f0f51 396 struct kvm_vcpu vcpu;
313dbd49 397 unsigned long host_rsp;
29bd8a78 398 u8 fail;
69c73028 399 u8 cpl;
9d58b931 400 bool nmi_known_unmasked;
51aa01d1 401 u32 exit_intr_info;
1155f76a 402 u32 idt_vectoring_info;
6de12732 403 ulong rflags;
26bb0981 404 struct shared_msr_entry *guest_msrs;
a2fa3e9f
GH
405 int nmsrs;
406 int save_nmsrs;
a547c6db 407 unsigned long host_idt_base;
a2fa3e9f 408#ifdef CONFIG_X86_64
44ea2b17
AK
409 u64 msr_host_kernel_gs_base;
410 u64 msr_guest_kernel_gs_base;
a2fa3e9f 411#endif
d462b819
NHE
412 /*
413 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
414 * non-nested (L1) guest, it always points to vmcs01. For a nested
415 * guest (L2), it points to a different VMCS.
416 */
417 struct loaded_vmcs vmcs01;
418 struct loaded_vmcs *loaded_vmcs;
419 bool __launched; /* temporary, used in vmx_vcpu_run */
61d2ef2c
AK
420 struct msr_autoload {
421 unsigned nr;
422 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
423 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
424 } msr_autoload;
a2fa3e9f
GH
425 struct {
426 int loaded;
427 u16 fs_sel, gs_sel, ldt_sel;
b2da15ac
AK
428#ifdef CONFIG_X86_64
429 u16 ds_sel, es_sel;
430#endif
152d3f2f
LV
431 int gs_ldt_reload_needed;
432 int fs_reload_needed;
d77c26fc 433 } host_state;
9c8cba37 434 struct {
7ffd92c5 435 int vm86_active;
78ac8b47 436 ulong save_rflags;
f5f7b2fe
AK
437 struct kvm_segment segs[8];
438 } rmode;
439 struct {
440 u32 bitmask; /* 4 bits per segment (1 bit per field) */
7ffd92c5
AK
441 struct kvm_save_segment {
442 u16 selector;
443 unsigned long base;
444 u32 limit;
445 u32 ar;
f5f7b2fe 446 } seg[8];
2fb92db1 447 } segment_cache;
2384d2b3 448 int vpid;
04fa4d32 449 bool emulation_required;
3b86cd99
JK
450
451 /* Support for vnmi-less CPUs */
452 int soft_vnmi_blocked;
453 ktime_t entry_time;
454 s64 vnmi_blocked_time;
a0861c02 455 u32 exit_reason;
4e47c7a6
SY
456
457 bool rdtscp_enabled;
ec378aee 458
01e439be
YZ
459 /* Posted interrupt descriptor */
460 struct pi_desc pi_desc;
461
ec378aee
NHE
462 /* Support for a guest hypervisor (nested VMX) */
463 struct nested_vmx nested;
a2fa3e9f
GH
464};
465
2fb92db1
AK
466enum segment_cache_field {
467 SEG_FIELD_SEL = 0,
468 SEG_FIELD_BASE = 1,
469 SEG_FIELD_LIMIT = 2,
470 SEG_FIELD_AR = 3,
471
472 SEG_FIELD_NR = 4
473};
474
a2fa3e9f
GH
475static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
476{
fb3f0f51 477 return container_of(vcpu, struct vcpu_vmx, vcpu);
a2fa3e9f
GH
478}
479
22bd0358
NHE
480#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
481#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
482#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
483 [number##_HIGH] = VMCS12_OFFSET(name)+4
484
772e0318 485static const unsigned short vmcs_field_to_offset_table[] = {
22bd0358
NHE
486 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
487 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
488 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
489 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
490 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
491 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
492 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
493 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
494 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
495 FIELD(HOST_ES_SELECTOR, host_es_selector),
496 FIELD(HOST_CS_SELECTOR, host_cs_selector),
497 FIELD(HOST_SS_SELECTOR, host_ss_selector),
498 FIELD(HOST_DS_SELECTOR, host_ds_selector),
499 FIELD(HOST_FS_SELECTOR, host_fs_selector),
500 FIELD(HOST_GS_SELECTOR, host_gs_selector),
501 FIELD(HOST_TR_SELECTOR, host_tr_selector),
502 FIELD64(IO_BITMAP_A, io_bitmap_a),
503 FIELD64(IO_BITMAP_B, io_bitmap_b),
504 FIELD64(MSR_BITMAP, msr_bitmap),
505 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
506 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
507 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
508 FIELD64(TSC_OFFSET, tsc_offset),
509 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
510 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
511 FIELD64(EPT_POINTER, ept_pointer),
512 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
513 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
514 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
515 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
516 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
517 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
518 FIELD64(GUEST_PDPTR0, guest_pdptr0),
519 FIELD64(GUEST_PDPTR1, guest_pdptr1),
520 FIELD64(GUEST_PDPTR2, guest_pdptr2),
521 FIELD64(GUEST_PDPTR3, guest_pdptr3),
522 FIELD64(HOST_IA32_PAT, host_ia32_pat),
523 FIELD64(HOST_IA32_EFER, host_ia32_efer),
524 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
525 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
526 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
527 FIELD(EXCEPTION_BITMAP, exception_bitmap),
528 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
529 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
530 FIELD(CR3_TARGET_COUNT, cr3_target_count),
531 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
532 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
533 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
534 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
535 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
536 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
537 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
538 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
539 FIELD(TPR_THRESHOLD, tpr_threshold),
540 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
541 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
542 FIELD(VM_EXIT_REASON, vm_exit_reason),
543 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
544 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
545 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
546 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
547 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
548 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
549 FIELD(GUEST_ES_LIMIT, guest_es_limit),
550 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
551 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
552 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
553 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
554 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
555 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
556 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
557 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
558 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
559 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
560 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
561 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
562 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
563 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
564 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
565 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
566 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
567 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
568 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
569 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
570 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
0238ea91 571 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
22bd0358
NHE
572 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
573 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
574 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
575 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
576 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
577 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
578 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
579 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
580 FIELD(EXIT_QUALIFICATION, exit_qualification),
581 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
582 FIELD(GUEST_CR0, guest_cr0),
583 FIELD(GUEST_CR3, guest_cr3),
584 FIELD(GUEST_CR4, guest_cr4),
585 FIELD(GUEST_ES_BASE, guest_es_base),
586 FIELD(GUEST_CS_BASE, guest_cs_base),
587 FIELD(GUEST_SS_BASE, guest_ss_base),
588 FIELD(GUEST_DS_BASE, guest_ds_base),
589 FIELD(GUEST_FS_BASE, guest_fs_base),
590 FIELD(GUEST_GS_BASE, guest_gs_base),
591 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
592 FIELD(GUEST_TR_BASE, guest_tr_base),
593 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
594 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
595 FIELD(GUEST_DR7, guest_dr7),
596 FIELD(GUEST_RSP, guest_rsp),
597 FIELD(GUEST_RIP, guest_rip),
598 FIELD(GUEST_RFLAGS, guest_rflags),
599 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
600 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
601 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
602 FIELD(HOST_CR0, host_cr0),
603 FIELD(HOST_CR3, host_cr3),
604 FIELD(HOST_CR4, host_cr4),
605 FIELD(HOST_FS_BASE, host_fs_base),
606 FIELD(HOST_GS_BASE, host_gs_base),
607 FIELD(HOST_TR_BASE, host_tr_base),
608 FIELD(HOST_GDTR_BASE, host_gdtr_base),
609 FIELD(HOST_IDTR_BASE, host_idtr_base),
610 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
611 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
612 FIELD(HOST_RSP, host_rsp),
613 FIELD(HOST_RIP, host_rip),
614};
615static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
616
617static inline short vmcs_field_to_offset(unsigned long field)
618{
619 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
620 return -1;
621 return vmcs_field_to_offset_table[field];
622}
623
a9d30f33
NHE
624static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
625{
626 return to_vmx(vcpu)->nested.current_vmcs12;
627}
628
629static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
630{
631 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
32cad84f 632 if (is_error_page(page))
a9d30f33 633 return NULL;
32cad84f 634
a9d30f33
NHE
635 return page;
636}
637
638static void nested_release_page(struct page *page)
639{
640 kvm_release_page_dirty(page);
641}
642
643static void nested_release_page_clean(struct page *page)
644{
645 kvm_release_page_clean(page);
646}
647
4e1096d2 648static u64 construct_eptp(unsigned long root_hpa);
4610c9cc
DX
649static void kvm_cpu_vmxon(u64 addr);
650static void kvm_cpu_vmxoff(void);
aff48baa 651static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
776e58ea 652static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
b246dd5d
OW
653static void vmx_set_segment(struct kvm_vcpu *vcpu,
654 struct kvm_segment *var, int seg);
655static void vmx_get_segment(struct kvm_vcpu *vcpu,
656 struct kvm_segment *var, int seg);
d99e4152
GN
657static bool guest_state_valid(struct kvm_vcpu *vcpu);
658static u32 vmx_segment_access_rights(struct kvm_segment *var);
a20ed54d 659static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
75880a01 660
6aa8b732
AK
661static DEFINE_PER_CPU(struct vmcs *, vmxarea);
662static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
d462b819
NHE
663/*
664 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
665 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
666 */
667static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
3444d7da 668static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
6aa8b732 669
3e7c73e9
AK
670static unsigned long *vmx_io_bitmap_a;
671static unsigned long *vmx_io_bitmap_b;
5897297b
AK
672static unsigned long *vmx_msr_bitmap_legacy;
673static unsigned long *vmx_msr_bitmap_longmode;
8d14695f
YZ
674static unsigned long *vmx_msr_bitmap_legacy_x2apic;
675static unsigned long *vmx_msr_bitmap_longmode_x2apic;
fdef3ad1 676
110312c8 677static bool cpu_has_load_ia32_efer;
8bf00a52 678static bool cpu_has_load_perf_global_ctrl;
110312c8 679
2384d2b3
SY
680static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
681static DEFINE_SPINLOCK(vmx_vpid_lock);
682
1c3d14fe 683static struct vmcs_config {
6aa8b732
AK
684 int size;
685 int order;
686 u32 revision_id;
1c3d14fe
YS
687 u32 pin_based_exec_ctrl;
688 u32 cpu_based_exec_ctrl;
f78e0e2e 689 u32 cpu_based_2nd_exec_ctrl;
1c3d14fe
YS
690 u32 vmexit_ctrl;
691 u32 vmentry_ctrl;
692} vmcs_config;
6aa8b732 693
efff9e53 694static struct vmx_capability {
d56f546d
SY
695 u32 ept;
696 u32 vpid;
697} vmx_capability;
698
6aa8b732
AK
699#define VMX_SEGMENT_FIELD(seg) \
700 [VCPU_SREG_##seg] = { \
701 .selector = GUEST_##seg##_SELECTOR, \
702 .base = GUEST_##seg##_BASE, \
703 .limit = GUEST_##seg##_LIMIT, \
704 .ar_bytes = GUEST_##seg##_AR_BYTES, \
705 }
706
772e0318 707static const struct kvm_vmx_segment_field {
6aa8b732
AK
708 unsigned selector;
709 unsigned base;
710 unsigned limit;
711 unsigned ar_bytes;
712} kvm_vmx_segment_fields[] = {
713 VMX_SEGMENT_FIELD(CS),
714 VMX_SEGMENT_FIELD(DS),
715 VMX_SEGMENT_FIELD(ES),
716 VMX_SEGMENT_FIELD(FS),
717 VMX_SEGMENT_FIELD(GS),
718 VMX_SEGMENT_FIELD(SS),
719 VMX_SEGMENT_FIELD(TR),
720 VMX_SEGMENT_FIELD(LDTR),
721};
722
26bb0981
AK
723static u64 host_efer;
724
6de4f3ad
AK
725static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
726
4d56c8a7 727/*
8c06585d 728 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
4d56c8a7
AK
729 * away by decrementing the array size.
730 */
6aa8b732 731static const u32 vmx_msr_index[] = {
05b3e0c2 732#ifdef CONFIG_X86_64
44ea2b17 733 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
6aa8b732 734#endif
8c06585d 735 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
6aa8b732 736};
9d8f549d 737#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
6aa8b732 738
31299944 739static inline bool is_page_fault(u32 intr_info)
6aa8b732
AK
740{
741 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
742 INTR_INFO_VALID_MASK)) ==
8ab2d2e2 743 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
6aa8b732
AK
744}
745
31299944 746static inline bool is_no_device(u32 intr_info)
2ab455cc
AL
747{
748 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
749 INTR_INFO_VALID_MASK)) ==
8ab2d2e2 750 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
2ab455cc
AL
751}
752
31299944 753static inline bool is_invalid_opcode(u32 intr_info)
7aa81cc0
AL
754{
755 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
756 INTR_INFO_VALID_MASK)) ==
8ab2d2e2 757 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
7aa81cc0
AL
758}
759
31299944 760static inline bool is_external_interrupt(u32 intr_info)
6aa8b732
AK
761{
762 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
763 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
764}
765
31299944 766static inline bool is_machine_check(u32 intr_info)
a0861c02
AK
767{
768 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
769 INTR_INFO_VALID_MASK)) ==
770 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
771}
772
31299944 773static inline bool cpu_has_vmx_msr_bitmap(void)
25c5f225 774{
04547156 775 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
25c5f225
SY
776}
777
31299944 778static inline bool cpu_has_vmx_tpr_shadow(void)
6e5d865c 779{
04547156 780 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
6e5d865c
YS
781}
782
31299944 783static inline bool vm_need_tpr_shadow(struct kvm *kvm)
6e5d865c 784{
04547156 785 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
6e5d865c
YS
786}
787
31299944 788static inline bool cpu_has_secondary_exec_ctrls(void)
f78e0e2e 789{
04547156
SY
790 return vmcs_config.cpu_based_exec_ctrl &
791 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
f78e0e2e
SY
792}
793
774ead3a 794static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
f78e0e2e 795{
04547156
SY
796 return vmcs_config.cpu_based_2nd_exec_ctrl &
797 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
798}
799
8d14695f
YZ
800static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
801{
802 return vmcs_config.cpu_based_2nd_exec_ctrl &
803 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
804}
805
83d4c286
YZ
806static inline bool cpu_has_vmx_apic_register_virt(void)
807{
808 return vmcs_config.cpu_based_2nd_exec_ctrl &
809 SECONDARY_EXEC_APIC_REGISTER_VIRT;
810}
811
c7c9c56c
YZ
812static inline bool cpu_has_vmx_virtual_intr_delivery(void)
813{
814 return vmcs_config.cpu_based_2nd_exec_ctrl &
815 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
816}
817
01e439be
YZ
818static inline bool cpu_has_vmx_posted_intr(void)
819{
820 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
821}
822
823static inline bool cpu_has_vmx_apicv(void)
824{
825 return cpu_has_vmx_apic_register_virt() &&
826 cpu_has_vmx_virtual_intr_delivery() &&
827 cpu_has_vmx_posted_intr();
828}
829
04547156
SY
830static inline bool cpu_has_vmx_flexpriority(void)
831{
832 return cpu_has_vmx_tpr_shadow() &&
833 cpu_has_vmx_virtualize_apic_accesses();
f78e0e2e
SY
834}
835
e799794e
MT
836static inline bool cpu_has_vmx_ept_execute_only(void)
837{
31299944 838 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
e799794e
MT
839}
840
841static inline bool cpu_has_vmx_eptp_uncacheable(void)
842{
31299944 843 return vmx_capability.ept & VMX_EPTP_UC_BIT;
e799794e
MT
844}
845
846static inline bool cpu_has_vmx_eptp_writeback(void)
847{
31299944 848 return vmx_capability.ept & VMX_EPTP_WB_BIT;
e799794e
MT
849}
850
851static inline bool cpu_has_vmx_ept_2m_page(void)
852{
31299944 853 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
e799794e
MT
854}
855
878403b7
SY
856static inline bool cpu_has_vmx_ept_1g_page(void)
857{
31299944 858 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
878403b7
SY
859}
860
4bc9b982
SY
861static inline bool cpu_has_vmx_ept_4levels(void)
862{
863 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
864}
865
83c3a331
XH
866static inline bool cpu_has_vmx_ept_ad_bits(void)
867{
868 return vmx_capability.ept & VMX_EPT_AD_BIT;
869}
870
31299944 871static inline bool cpu_has_vmx_invept_context(void)
d56f546d 872{
31299944 873 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
d56f546d
SY
874}
875
31299944 876static inline bool cpu_has_vmx_invept_global(void)
d56f546d 877{
31299944 878 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
d56f546d
SY
879}
880
518c8aee
GJ
881static inline bool cpu_has_vmx_invvpid_single(void)
882{
883 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
884}
885
b9d762fa
GJ
886static inline bool cpu_has_vmx_invvpid_global(void)
887{
888 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
889}
890
31299944 891static inline bool cpu_has_vmx_ept(void)
d56f546d 892{
04547156
SY
893 return vmcs_config.cpu_based_2nd_exec_ctrl &
894 SECONDARY_EXEC_ENABLE_EPT;
d56f546d
SY
895}
896
31299944 897static inline bool cpu_has_vmx_unrestricted_guest(void)
3a624e29
NK
898{
899 return vmcs_config.cpu_based_2nd_exec_ctrl &
900 SECONDARY_EXEC_UNRESTRICTED_GUEST;
901}
902
31299944 903static inline bool cpu_has_vmx_ple(void)
4b8d54f9
ZE
904{
905 return vmcs_config.cpu_based_2nd_exec_ctrl &
906 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
907}
908
31299944 909static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
f78e0e2e 910{
6d3e435e 911 return flexpriority_enabled && irqchip_in_kernel(kvm);
f78e0e2e
SY
912}
913
31299944 914static inline bool cpu_has_vmx_vpid(void)
2384d2b3 915{
04547156
SY
916 return vmcs_config.cpu_based_2nd_exec_ctrl &
917 SECONDARY_EXEC_ENABLE_VPID;
2384d2b3
SY
918}
919
31299944 920static inline bool cpu_has_vmx_rdtscp(void)
4e47c7a6
SY
921{
922 return vmcs_config.cpu_based_2nd_exec_ctrl &
923 SECONDARY_EXEC_RDTSCP;
924}
925
ad756a16
MJ
926static inline bool cpu_has_vmx_invpcid(void)
927{
928 return vmcs_config.cpu_based_2nd_exec_ctrl &
929 SECONDARY_EXEC_ENABLE_INVPCID;
930}
931
31299944 932static inline bool cpu_has_virtual_nmis(void)
f08864b4
SY
933{
934 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
935}
936
f5f48ee1
SY
937static inline bool cpu_has_vmx_wbinvd_exit(void)
938{
939 return vmcs_config.cpu_based_2nd_exec_ctrl &
940 SECONDARY_EXEC_WBINVD_EXITING;
941}
942
04547156
SY
943static inline bool report_flexpriority(void)
944{
945 return flexpriority_enabled;
946}
947
fe3ef05c
NHE
948static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
949{
950 return vmcs12->cpu_based_vm_exec_control & bit;
951}
952
953static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
954{
955 return (vmcs12->cpu_based_vm_exec_control &
956 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
957 (vmcs12->secondary_vm_exec_control & bit);
958}
959
644d711a
NHE
960static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12,
961 struct kvm_vcpu *vcpu)
962{
963 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
964}
965
966static inline bool is_exception(u32 intr_info)
967{
968 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
969 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
970}
971
972static void nested_vmx_vmexit(struct kvm_vcpu *vcpu);
7c177938
NHE
973static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
974 struct vmcs12 *vmcs12,
975 u32 reason, unsigned long qualification);
976
8b9cf98c 977static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
7725f0ba
AK
978{
979 int i;
980
a2fa3e9f 981 for (i = 0; i < vmx->nmsrs; ++i)
26bb0981 982 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
a75beee6
ED
983 return i;
984 return -1;
985}
986
2384d2b3
SY
987static inline void __invvpid(int ext, u16 vpid, gva_t gva)
988{
989 struct {
990 u64 vpid : 16;
991 u64 rsvd : 48;
992 u64 gva;
993 } operand = { vpid, 0, gva };
994
4ecac3fd 995 asm volatile (__ex(ASM_VMX_INVVPID)
2384d2b3
SY
996 /* CF==1 or ZF==1 --> rc = -1 */
997 "; ja 1f ; ud2 ; 1:"
998 : : "a"(&operand), "c"(ext) : "cc", "memory");
999}
1000
1439442c
SY
1001static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1002{
1003 struct {
1004 u64 eptp, gpa;
1005 } operand = {eptp, gpa};
1006
4ecac3fd 1007 asm volatile (__ex(ASM_VMX_INVEPT)
1439442c
SY
1008 /* CF==1 or ZF==1 --> rc = -1 */
1009 "; ja 1f ; ud2 ; 1:\n"
1010 : : "a" (&operand), "c" (ext) : "cc", "memory");
1011}
1012
26bb0981 1013static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
a75beee6
ED
1014{
1015 int i;
1016
8b9cf98c 1017 i = __find_msr_index(vmx, msr);
a75beee6 1018 if (i >= 0)
a2fa3e9f 1019 return &vmx->guest_msrs[i];
8b6d44c7 1020 return NULL;
7725f0ba
AK
1021}
1022
6aa8b732
AK
1023static void vmcs_clear(struct vmcs *vmcs)
1024{
1025 u64 phys_addr = __pa(vmcs);
1026 u8 error;
1027
4ecac3fd 1028 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
16d8f72f 1029 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
6aa8b732
AK
1030 : "cc", "memory");
1031 if (error)
1032 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1033 vmcs, phys_addr);
1034}
1035
d462b819
NHE
1036static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1037{
1038 vmcs_clear(loaded_vmcs->vmcs);
1039 loaded_vmcs->cpu = -1;
1040 loaded_vmcs->launched = 0;
1041}
1042
7725b894
DX
1043static void vmcs_load(struct vmcs *vmcs)
1044{
1045 u64 phys_addr = __pa(vmcs);
1046 u8 error;
1047
1048 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
16d8f72f 1049 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
7725b894
DX
1050 : "cc", "memory");
1051 if (error)
2844d849 1052 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
7725b894
DX
1053 vmcs, phys_addr);
1054}
1055
8f536b76
ZY
1056#ifdef CONFIG_KEXEC
1057/*
1058 * This bitmap is used to indicate whether the vmclear
1059 * operation is enabled on all cpus. All disabled by
1060 * default.
1061 */
1062static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1063
1064static inline void crash_enable_local_vmclear(int cpu)
1065{
1066 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1067}
1068
1069static inline void crash_disable_local_vmclear(int cpu)
1070{
1071 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1072}
1073
1074static inline int crash_local_vmclear_enabled(int cpu)
1075{
1076 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1077}
1078
1079static void crash_vmclear_local_loaded_vmcss(void)
1080{
1081 int cpu = raw_smp_processor_id();
1082 struct loaded_vmcs *v;
1083
1084 if (!crash_local_vmclear_enabled(cpu))
1085 return;
1086
1087 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1088 loaded_vmcss_on_cpu_link)
1089 vmcs_clear(v->vmcs);
1090}
1091#else
1092static inline void crash_enable_local_vmclear(int cpu) { }
1093static inline void crash_disable_local_vmclear(int cpu) { }
1094#endif /* CONFIG_KEXEC */
1095
d462b819 1096static void __loaded_vmcs_clear(void *arg)
6aa8b732 1097{
d462b819 1098 struct loaded_vmcs *loaded_vmcs = arg;
d3b2c338 1099 int cpu = raw_smp_processor_id();
6aa8b732 1100
d462b819
NHE
1101 if (loaded_vmcs->cpu != cpu)
1102 return; /* vcpu migration can race with cpu offline */
1103 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
6aa8b732 1104 per_cpu(current_vmcs, cpu) = NULL;
8f536b76 1105 crash_disable_local_vmclear(cpu);
d462b819 1106 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
5a560f8b
XG
1107
1108 /*
1109 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1110 * is before setting loaded_vmcs->vcpu to -1 which is done in
1111 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1112 * then adds the vmcs into percpu list before it is deleted.
1113 */
1114 smp_wmb();
1115
d462b819 1116 loaded_vmcs_init(loaded_vmcs);
8f536b76 1117 crash_enable_local_vmclear(cpu);
6aa8b732
AK
1118}
1119
d462b819 1120static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
8d0be2b3 1121{
e6c7d321
XG
1122 int cpu = loaded_vmcs->cpu;
1123
1124 if (cpu != -1)
1125 smp_call_function_single(cpu,
1126 __loaded_vmcs_clear, loaded_vmcs, 1);
8d0be2b3
AK
1127}
1128
1760dd49 1129static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
2384d2b3
SY
1130{
1131 if (vmx->vpid == 0)
1132 return;
1133
518c8aee
GJ
1134 if (cpu_has_vmx_invvpid_single())
1135 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
2384d2b3
SY
1136}
1137
b9d762fa
GJ
1138static inline void vpid_sync_vcpu_global(void)
1139{
1140 if (cpu_has_vmx_invvpid_global())
1141 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1142}
1143
1144static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1145{
1146 if (cpu_has_vmx_invvpid_single())
1760dd49 1147 vpid_sync_vcpu_single(vmx);
b9d762fa
GJ
1148 else
1149 vpid_sync_vcpu_global();
1150}
1151
1439442c
SY
1152static inline void ept_sync_global(void)
1153{
1154 if (cpu_has_vmx_invept_global())
1155 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1156}
1157
1158static inline void ept_sync_context(u64 eptp)
1159{
089d034e 1160 if (enable_ept) {
1439442c
SY
1161 if (cpu_has_vmx_invept_context())
1162 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1163 else
1164 ept_sync_global();
1165 }
1166}
1167
96304217 1168static __always_inline unsigned long vmcs_readl(unsigned long field)
6aa8b732 1169{
5e520e62 1170 unsigned long value;
6aa8b732 1171
5e520e62
AK
1172 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1173 : "=a"(value) : "d"(field) : "cc");
6aa8b732
AK
1174 return value;
1175}
1176
96304217 1177static __always_inline u16 vmcs_read16(unsigned long field)
6aa8b732
AK
1178{
1179 return vmcs_readl(field);
1180}
1181
96304217 1182static __always_inline u32 vmcs_read32(unsigned long field)
6aa8b732
AK
1183{
1184 return vmcs_readl(field);
1185}
1186
96304217 1187static __always_inline u64 vmcs_read64(unsigned long field)
6aa8b732 1188{
05b3e0c2 1189#ifdef CONFIG_X86_64
6aa8b732
AK
1190 return vmcs_readl(field);
1191#else
1192 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1193#endif
1194}
1195
e52de1b8
AK
1196static noinline void vmwrite_error(unsigned long field, unsigned long value)
1197{
1198 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1199 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1200 dump_stack();
1201}
1202
6aa8b732
AK
1203static void vmcs_writel(unsigned long field, unsigned long value)
1204{
1205 u8 error;
1206
4ecac3fd 1207 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
d77c26fc 1208 : "=q"(error) : "a"(value), "d"(field) : "cc");
e52de1b8
AK
1209 if (unlikely(error))
1210 vmwrite_error(field, value);
6aa8b732
AK
1211}
1212
1213static void vmcs_write16(unsigned long field, u16 value)
1214{
1215 vmcs_writel(field, value);
1216}
1217
1218static void vmcs_write32(unsigned long field, u32 value)
1219{
1220 vmcs_writel(field, value);
1221}
1222
1223static void vmcs_write64(unsigned long field, u64 value)
1224{
6aa8b732 1225 vmcs_writel(field, value);
7682f2d0 1226#ifndef CONFIG_X86_64
6aa8b732
AK
1227 asm volatile ("");
1228 vmcs_writel(field+1, value >> 32);
1229#endif
1230}
1231
2ab455cc
AL
1232static void vmcs_clear_bits(unsigned long field, u32 mask)
1233{
1234 vmcs_writel(field, vmcs_readl(field) & ~mask);
1235}
1236
1237static void vmcs_set_bits(unsigned long field, u32 mask)
1238{
1239 vmcs_writel(field, vmcs_readl(field) | mask);
1240}
1241
2fb92db1
AK
1242static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1243{
1244 vmx->segment_cache.bitmask = 0;
1245}
1246
1247static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1248 unsigned field)
1249{
1250 bool ret;
1251 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1252
1253 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1254 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1255 vmx->segment_cache.bitmask = 0;
1256 }
1257 ret = vmx->segment_cache.bitmask & mask;
1258 vmx->segment_cache.bitmask |= mask;
1259 return ret;
1260}
1261
1262static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1263{
1264 u16 *p = &vmx->segment_cache.seg[seg].selector;
1265
1266 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1267 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1268 return *p;
1269}
1270
1271static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1272{
1273 ulong *p = &vmx->segment_cache.seg[seg].base;
1274
1275 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1276 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1277 return *p;
1278}
1279
1280static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1281{
1282 u32 *p = &vmx->segment_cache.seg[seg].limit;
1283
1284 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1285 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1286 return *p;
1287}
1288
1289static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1290{
1291 u32 *p = &vmx->segment_cache.seg[seg].ar;
1292
1293 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1294 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1295 return *p;
1296}
1297
abd3f2d6
AK
1298static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1299{
1300 u32 eb;
1301
fd7373cc
JK
1302 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1303 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1304 if ((vcpu->guest_debug &
1305 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1306 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1307 eb |= 1u << BP_VECTOR;
7ffd92c5 1308 if (to_vmx(vcpu)->rmode.vm86_active)
abd3f2d6 1309 eb = ~0;
089d034e 1310 if (enable_ept)
1439442c 1311 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
02daab21
AK
1312 if (vcpu->fpu_active)
1313 eb &= ~(1u << NM_VECTOR);
36cf24e0
NHE
1314
1315 /* When we are running a nested L2 guest and L1 specified for it a
1316 * certain exception bitmap, we must trap the same exceptions and pass
1317 * them to L1. When running L2, we will only handle the exceptions
1318 * specified above if L1 did not want them.
1319 */
1320 if (is_guest_mode(vcpu))
1321 eb |= get_vmcs12(vcpu)->exception_bitmap;
1322
abd3f2d6
AK
1323 vmcs_write32(EXCEPTION_BITMAP, eb);
1324}
1325
8bf00a52
GN
1326static void clear_atomic_switch_msr_special(unsigned long entry,
1327 unsigned long exit)
1328{
1329 vmcs_clear_bits(VM_ENTRY_CONTROLS, entry);
1330 vmcs_clear_bits(VM_EXIT_CONTROLS, exit);
1331}
1332
61d2ef2c
AK
1333static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1334{
1335 unsigned i;
1336 struct msr_autoload *m = &vmx->msr_autoload;
1337
8bf00a52
GN
1338 switch (msr) {
1339 case MSR_EFER:
1340 if (cpu_has_load_ia32_efer) {
1341 clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1342 VM_EXIT_LOAD_IA32_EFER);
1343 return;
1344 }
1345 break;
1346 case MSR_CORE_PERF_GLOBAL_CTRL:
1347 if (cpu_has_load_perf_global_ctrl) {
1348 clear_atomic_switch_msr_special(
1349 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1350 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1351 return;
1352 }
1353 break;
110312c8
AK
1354 }
1355
61d2ef2c
AK
1356 for (i = 0; i < m->nr; ++i)
1357 if (m->guest[i].index == msr)
1358 break;
1359
1360 if (i == m->nr)
1361 return;
1362 --m->nr;
1363 m->guest[i] = m->guest[m->nr];
1364 m->host[i] = m->host[m->nr];
1365 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1366 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1367}
1368
8bf00a52
GN
1369static void add_atomic_switch_msr_special(unsigned long entry,
1370 unsigned long exit, unsigned long guest_val_vmcs,
1371 unsigned long host_val_vmcs, u64 guest_val, u64 host_val)
1372{
1373 vmcs_write64(guest_val_vmcs, guest_val);
1374 vmcs_write64(host_val_vmcs, host_val);
1375 vmcs_set_bits(VM_ENTRY_CONTROLS, entry);
1376 vmcs_set_bits(VM_EXIT_CONTROLS, exit);
1377}
1378
61d2ef2c
AK
1379static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1380 u64 guest_val, u64 host_val)
1381{
1382 unsigned i;
1383 struct msr_autoload *m = &vmx->msr_autoload;
1384
8bf00a52
GN
1385 switch (msr) {
1386 case MSR_EFER:
1387 if (cpu_has_load_ia32_efer) {
1388 add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1389 VM_EXIT_LOAD_IA32_EFER,
1390 GUEST_IA32_EFER,
1391 HOST_IA32_EFER,
1392 guest_val, host_val);
1393 return;
1394 }
1395 break;
1396 case MSR_CORE_PERF_GLOBAL_CTRL:
1397 if (cpu_has_load_perf_global_ctrl) {
1398 add_atomic_switch_msr_special(
1399 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1400 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1401 GUEST_IA32_PERF_GLOBAL_CTRL,
1402 HOST_IA32_PERF_GLOBAL_CTRL,
1403 guest_val, host_val);
1404 return;
1405 }
1406 break;
110312c8
AK
1407 }
1408
61d2ef2c
AK
1409 for (i = 0; i < m->nr; ++i)
1410 if (m->guest[i].index == msr)
1411 break;
1412
e7fc6f93
GN
1413 if (i == NR_AUTOLOAD_MSRS) {
1414 printk_once(KERN_WARNING"Not enough mst switch entries. "
1415 "Can't add msr %x\n", msr);
1416 return;
1417 } else if (i == m->nr) {
61d2ef2c
AK
1418 ++m->nr;
1419 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1420 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1421 }
1422
1423 m->guest[i].index = msr;
1424 m->guest[i].value = guest_val;
1425 m->host[i].index = msr;
1426 m->host[i].value = host_val;
1427}
1428
33ed6329
AK
1429static void reload_tss(void)
1430{
33ed6329
AK
1431 /*
1432 * VT restores TR but not its size. Useless.
1433 */
d359192f 1434 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
a5f61300 1435 struct desc_struct *descs;
33ed6329 1436
d359192f 1437 descs = (void *)gdt->address;
33ed6329
AK
1438 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1439 load_TR_desc();
33ed6329
AK
1440}
1441
92c0d900 1442static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
2cc51560 1443{
3a34a881 1444 u64 guest_efer;
51c6cf66
AK
1445 u64 ignore_bits;
1446
f6801dff 1447 guest_efer = vmx->vcpu.arch.efer;
3a34a881 1448
51c6cf66 1449 /*
0fa06071 1450 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
51c6cf66
AK
1451 * outside long mode
1452 */
1453 ignore_bits = EFER_NX | EFER_SCE;
1454#ifdef CONFIG_X86_64
1455 ignore_bits |= EFER_LMA | EFER_LME;
1456 /* SCE is meaningful only in long mode on Intel */
1457 if (guest_efer & EFER_LMA)
1458 ignore_bits &= ~(u64)EFER_SCE;
1459#endif
51c6cf66
AK
1460 guest_efer &= ~ignore_bits;
1461 guest_efer |= host_efer & ignore_bits;
26bb0981 1462 vmx->guest_msrs[efer_offset].data = guest_efer;
d5696725 1463 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
84ad33ef
AK
1464
1465 clear_atomic_switch_msr(vmx, MSR_EFER);
1466 /* On ept, can't emulate nx, and must switch nx atomically */
1467 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1468 guest_efer = vmx->vcpu.arch.efer;
1469 if (!(guest_efer & EFER_LMA))
1470 guest_efer &= ~EFER_LME;
1471 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1472 return false;
1473 }
1474
26bb0981 1475 return true;
51c6cf66
AK
1476}
1477
2d49ec72
GN
1478static unsigned long segment_base(u16 selector)
1479{
d359192f 1480 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
2d49ec72
GN
1481 struct desc_struct *d;
1482 unsigned long table_base;
1483 unsigned long v;
1484
1485 if (!(selector & ~3))
1486 return 0;
1487
d359192f 1488 table_base = gdt->address;
2d49ec72
GN
1489
1490 if (selector & 4) { /* from ldt */
1491 u16 ldt_selector = kvm_read_ldt();
1492
1493 if (!(ldt_selector & ~3))
1494 return 0;
1495
1496 table_base = segment_base(ldt_selector);
1497 }
1498 d = (struct desc_struct *)(table_base + (selector & ~7));
1499 v = get_desc_base(d);
1500#ifdef CONFIG_X86_64
1501 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1502 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1503#endif
1504 return v;
1505}
1506
1507static inline unsigned long kvm_read_tr_base(void)
1508{
1509 u16 tr;
1510 asm("str %0" : "=g"(tr));
1511 return segment_base(tr);
1512}
1513
04d2cc77 1514static void vmx_save_host_state(struct kvm_vcpu *vcpu)
33ed6329 1515{
04d2cc77 1516 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981 1517 int i;
04d2cc77 1518
a2fa3e9f 1519 if (vmx->host_state.loaded)
33ed6329
AK
1520 return;
1521
a2fa3e9f 1522 vmx->host_state.loaded = 1;
33ed6329
AK
1523 /*
1524 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1525 * allow segment selectors with cpl > 0 or ti == 1.
1526 */
d6e88aec 1527 vmx->host_state.ldt_sel = kvm_read_ldt();
152d3f2f 1528 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
9581d442 1529 savesegment(fs, vmx->host_state.fs_sel);
152d3f2f 1530 if (!(vmx->host_state.fs_sel & 7)) {
a2fa3e9f 1531 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
152d3f2f
LV
1532 vmx->host_state.fs_reload_needed = 0;
1533 } else {
33ed6329 1534 vmcs_write16(HOST_FS_SELECTOR, 0);
152d3f2f 1535 vmx->host_state.fs_reload_needed = 1;
33ed6329 1536 }
9581d442 1537 savesegment(gs, vmx->host_state.gs_sel);
a2fa3e9f
GH
1538 if (!(vmx->host_state.gs_sel & 7))
1539 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
33ed6329
AK
1540 else {
1541 vmcs_write16(HOST_GS_SELECTOR, 0);
152d3f2f 1542 vmx->host_state.gs_ldt_reload_needed = 1;
33ed6329
AK
1543 }
1544
b2da15ac
AK
1545#ifdef CONFIG_X86_64
1546 savesegment(ds, vmx->host_state.ds_sel);
1547 savesegment(es, vmx->host_state.es_sel);
1548#endif
1549
33ed6329
AK
1550#ifdef CONFIG_X86_64
1551 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1552 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1553#else
a2fa3e9f
GH
1554 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1555 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
33ed6329 1556#endif
707c0874
AK
1557
1558#ifdef CONFIG_X86_64
c8770e7b
AK
1559 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1560 if (is_long_mode(&vmx->vcpu))
44ea2b17 1561 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
707c0874 1562#endif
26bb0981
AK
1563 for (i = 0; i < vmx->save_nmsrs; ++i)
1564 kvm_set_shared_msr(vmx->guest_msrs[i].index,
d5696725
AK
1565 vmx->guest_msrs[i].data,
1566 vmx->guest_msrs[i].mask);
33ed6329
AK
1567}
1568
a9b21b62 1569static void __vmx_load_host_state(struct vcpu_vmx *vmx)
33ed6329 1570{
a2fa3e9f 1571 if (!vmx->host_state.loaded)
33ed6329
AK
1572 return;
1573
e1beb1d3 1574 ++vmx->vcpu.stat.host_state_reload;
a2fa3e9f 1575 vmx->host_state.loaded = 0;
c8770e7b
AK
1576#ifdef CONFIG_X86_64
1577 if (is_long_mode(&vmx->vcpu))
1578 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1579#endif
152d3f2f 1580 if (vmx->host_state.gs_ldt_reload_needed) {
d6e88aec 1581 kvm_load_ldt(vmx->host_state.ldt_sel);
33ed6329 1582#ifdef CONFIG_X86_64
9581d442 1583 load_gs_index(vmx->host_state.gs_sel);
9581d442
AK
1584#else
1585 loadsegment(gs, vmx->host_state.gs_sel);
33ed6329 1586#endif
33ed6329 1587 }
0a77fe4c
AK
1588 if (vmx->host_state.fs_reload_needed)
1589 loadsegment(fs, vmx->host_state.fs_sel);
b2da15ac
AK
1590#ifdef CONFIG_X86_64
1591 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1592 loadsegment(ds, vmx->host_state.ds_sel);
1593 loadsegment(es, vmx->host_state.es_sel);
1594 }
b2da15ac 1595#endif
152d3f2f 1596 reload_tss();
44ea2b17 1597#ifdef CONFIG_X86_64
c8770e7b 1598 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
44ea2b17 1599#endif
b1a74bf8
SS
1600 /*
1601 * If the FPU is not active (through the host task or
1602 * the guest vcpu), then restore the cr0.TS bit.
1603 */
1604 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1605 stts();
3444d7da 1606 load_gdt(&__get_cpu_var(host_gdt));
33ed6329
AK
1607}
1608
a9b21b62
AK
1609static void vmx_load_host_state(struct vcpu_vmx *vmx)
1610{
1611 preempt_disable();
1612 __vmx_load_host_state(vmx);
1613 preempt_enable();
1614}
1615
6aa8b732
AK
1616/*
1617 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1618 * vcpu mutex is already taken.
1619 */
15ad7146 1620static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
6aa8b732 1621{
a2fa3e9f 1622 struct vcpu_vmx *vmx = to_vmx(vcpu);
4610c9cc 1623 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
6aa8b732 1624
4610c9cc
DX
1625 if (!vmm_exclusive)
1626 kvm_cpu_vmxon(phys_addr);
d462b819
NHE
1627 else if (vmx->loaded_vmcs->cpu != cpu)
1628 loaded_vmcs_clear(vmx->loaded_vmcs);
6aa8b732 1629
d462b819
NHE
1630 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1631 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1632 vmcs_load(vmx->loaded_vmcs->vmcs);
6aa8b732
AK
1633 }
1634
d462b819 1635 if (vmx->loaded_vmcs->cpu != cpu) {
d359192f 1636 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
6aa8b732
AK
1637 unsigned long sysenter_esp;
1638
a8eeb04a 1639 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
92fe13be 1640 local_irq_disable();
8f536b76 1641 crash_disable_local_vmclear(cpu);
5a560f8b
XG
1642
1643 /*
1644 * Read loaded_vmcs->cpu should be before fetching
1645 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1646 * See the comments in __loaded_vmcs_clear().
1647 */
1648 smp_rmb();
1649
d462b819
NHE
1650 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1651 &per_cpu(loaded_vmcss_on_cpu, cpu));
8f536b76 1652 crash_enable_local_vmclear(cpu);
92fe13be
DX
1653 local_irq_enable();
1654
6aa8b732
AK
1655 /*
1656 * Linux uses per-cpu TSS and GDT, so set these when switching
1657 * processors.
1658 */
d6e88aec 1659 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
d359192f 1660 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
6aa8b732
AK
1661
1662 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1663 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
d462b819 1664 vmx->loaded_vmcs->cpu = cpu;
6aa8b732 1665 }
6aa8b732
AK
1666}
1667
1668static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1669{
a9b21b62 1670 __vmx_load_host_state(to_vmx(vcpu));
4610c9cc 1671 if (!vmm_exclusive) {
d462b819
NHE
1672 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1673 vcpu->cpu = -1;
4610c9cc
DX
1674 kvm_cpu_vmxoff();
1675 }
6aa8b732
AK
1676}
1677
5fd86fcf
AK
1678static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1679{
81231c69
AK
1680 ulong cr0;
1681
5fd86fcf
AK
1682 if (vcpu->fpu_active)
1683 return;
1684 vcpu->fpu_active = 1;
81231c69
AK
1685 cr0 = vmcs_readl(GUEST_CR0);
1686 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1687 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1688 vmcs_writel(GUEST_CR0, cr0);
5fd86fcf 1689 update_exception_bitmap(vcpu);
edcafe3c 1690 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
36cf24e0
NHE
1691 if (is_guest_mode(vcpu))
1692 vcpu->arch.cr0_guest_owned_bits &=
1693 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
edcafe3c 1694 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
5fd86fcf
AK
1695}
1696
edcafe3c
AK
1697static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1698
fe3ef05c
NHE
1699/*
1700 * Return the cr0 value that a nested guest would read. This is a combination
1701 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1702 * its hypervisor (cr0_read_shadow).
1703 */
1704static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1705{
1706 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1707 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1708}
1709static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1710{
1711 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1712 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1713}
1714
5fd86fcf
AK
1715static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1716{
36cf24e0
NHE
1717 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1718 * set this *before* calling this function.
1719 */
edcafe3c 1720 vmx_decache_cr0_guest_bits(vcpu);
81231c69 1721 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
5fd86fcf 1722 update_exception_bitmap(vcpu);
edcafe3c
AK
1723 vcpu->arch.cr0_guest_owned_bits = 0;
1724 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
36cf24e0
NHE
1725 if (is_guest_mode(vcpu)) {
1726 /*
1727 * L1's specified read shadow might not contain the TS bit,
1728 * so now that we turned on shadowing of this bit, we need to
1729 * set this bit of the shadow. Like in nested_vmx_run we need
1730 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1731 * up-to-date here because we just decached cr0.TS (and we'll
1732 * only update vmcs12->guest_cr0 on nested exit).
1733 */
1734 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1735 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1736 (vcpu->arch.cr0 & X86_CR0_TS);
1737 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1738 } else
1739 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
5fd86fcf
AK
1740}
1741
6aa8b732
AK
1742static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1743{
78ac8b47 1744 unsigned long rflags, save_rflags;
345dcaa8 1745
6de12732
AK
1746 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1747 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1748 rflags = vmcs_readl(GUEST_RFLAGS);
1749 if (to_vmx(vcpu)->rmode.vm86_active) {
1750 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1751 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1752 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1753 }
1754 to_vmx(vcpu)->rflags = rflags;
78ac8b47 1755 }
6de12732 1756 return to_vmx(vcpu)->rflags;
6aa8b732
AK
1757}
1758
1759static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1760{
6de12732
AK
1761 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1762 to_vmx(vcpu)->rflags = rflags;
78ac8b47
AK
1763 if (to_vmx(vcpu)->rmode.vm86_active) {
1764 to_vmx(vcpu)->rmode.save_rflags = rflags;
053de044 1765 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
78ac8b47 1766 }
6aa8b732
AK
1767 vmcs_writel(GUEST_RFLAGS, rflags);
1768}
1769
2809f5d2
GC
1770static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1771{
1772 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1773 int ret = 0;
1774
1775 if (interruptibility & GUEST_INTR_STATE_STI)
48005f64 1776 ret |= KVM_X86_SHADOW_INT_STI;
2809f5d2 1777 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
48005f64 1778 ret |= KVM_X86_SHADOW_INT_MOV_SS;
2809f5d2
GC
1779
1780 return ret & mask;
1781}
1782
1783static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1784{
1785 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1786 u32 interruptibility = interruptibility_old;
1787
1788 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1789
48005f64 1790 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2809f5d2 1791 interruptibility |= GUEST_INTR_STATE_MOV_SS;
48005f64 1792 else if (mask & KVM_X86_SHADOW_INT_STI)
2809f5d2
GC
1793 interruptibility |= GUEST_INTR_STATE_STI;
1794
1795 if ((interruptibility != interruptibility_old))
1796 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1797}
1798
6aa8b732
AK
1799static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1800{
1801 unsigned long rip;
6aa8b732 1802
5fdbf976 1803 rip = kvm_rip_read(vcpu);
6aa8b732 1804 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5fdbf976 1805 kvm_rip_write(vcpu, rip);
6aa8b732 1806
2809f5d2
GC
1807 /* skipping an emulated instruction also counts */
1808 vmx_set_interrupt_shadow(vcpu, 0);
6aa8b732
AK
1809}
1810
0b6ac343
NHE
1811/*
1812 * KVM wants to inject page-faults which it got to the guest. This function
1813 * checks whether in a nested guest, we need to inject them to L1 or L2.
1814 * This function assumes it is called with the exit reason in vmcs02 being
1815 * a #PF exception (this is the only case in which KVM injects a #PF when L2
1816 * is running).
1817 */
1818static int nested_pf_handled(struct kvm_vcpu *vcpu)
1819{
1820 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1821
1822 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
95871901 1823 if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR)))
0b6ac343
NHE
1824 return 0;
1825
1826 nested_vmx_vmexit(vcpu);
1827 return 1;
1828}
1829
298101da 1830static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
ce7ddec4
JR
1831 bool has_error_code, u32 error_code,
1832 bool reinject)
298101da 1833{
77ab6db0 1834 struct vcpu_vmx *vmx = to_vmx(vcpu);
8ab2d2e2 1835 u32 intr_info = nr | INTR_INFO_VALID_MASK;
77ab6db0 1836
0b6ac343
NHE
1837 if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
1838 nested_pf_handled(vcpu))
1839 return;
1840
8ab2d2e2 1841 if (has_error_code) {
77ab6db0 1842 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
8ab2d2e2
JK
1843 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1844 }
77ab6db0 1845
7ffd92c5 1846 if (vmx->rmode.vm86_active) {
71f9833b
SH
1847 int inc_eip = 0;
1848 if (kvm_exception_is_soft(nr))
1849 inc_eip = vcpu->arch.event_exit_inst_len;
1850 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
a92601bb 1851 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
77ab6db0
JK
1852 return;
1853 }
1854
66fd3f7f
GN
1855 if (kvm_exception_is_soft(nr)) {
1856 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1857 vmx->vcpu.arch.event_exit_inst_len);
8ab2d2e2
JK
1858 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1859 } else
1860 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1861
1862 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
298101da
AK
1863}
1864
4e47c7a6
SY
1865static bool vmx_rdtscp_supported(void)
1866{
1867 return cpu_has_vmx_rdtscp();
1868}
1869
ad756a16
MJ
1870static bool vmx_invpcid_supported(void)
1871{
1872 return cpu_has_vmx_invpcid() && enable_ept;
1873}
1874
a75beee6
ED
1875/*
1876 * Swap MSR entry in host/guest MSR entry array.
1877 */
8b9cf98c 1878static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
a75beee6 1879{
26bb0981 1880 struct shared_msr_entry tmp;
a2fa3e9f
GH
1881
1882 tmp = vmx->guest_msrs[to];
1883 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1884 vmx->guest_msrs[from] = tmp;
a75beee6
ED
1885}
1886
8d14695f
YZ
1887static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
1888{
1889 unsigned long *msr_bitmap;
1890
1891 if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
1892 if (is_long_mode(vcpu))
1893 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
1894 else
1895 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
1896 } else {
1897 if (is_long_mode(vcpu))
1898 msr_bitmap = vmx_msr_bitmap_longmode;
1899 else
1900 msr_bitmap = vmx_msr_bitmap_legacy;
1901 }
1902
1903 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1904}
1905
e38aea3e
AK
1906/*
1907 * Set up the vmcs to automatically save and restore system
1908 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1909 * mode, as fiddling with msrs is very expensive.
1910 */
8b9cf98c 1911static void setup_msrs(struct vcpu_vmx *vmx)
e38aea3e 1912{
26bb0981 1913 int save_nmsrs, index;
e38aea3e 1914
a75beee6
ED
1915 save_nmsrs = 0;
1916#ifdef CONFIG_X86_64
8b9cf98c 1917 if (is_long_mode(&vmx->vcpu)) {
8b9cf98c 1918 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
a75beee6 1919 if (index >= 0)
8b9cf98c
RR
1920 move_msr_up(vmx, index, save_nmsrs++);
1921 index = __find_msr_index(vmx, MSR_LSTAR);
a75beee6 1922 if (index >= 0)
8b9cf98c
RR
1923 move_msr_up(vmx, index, save_nmsrs++);
1924 index = __find_msr_index(vmx, MSR_CSTAR);
a75beee6 1925 if (index >= 0)
8b9cf98c 1926 move_msr_up(vmx, index, save_nmsrs++);
4e47c7a6
SY
1927 index = __find_msr_index(vmx, MSR_TSC_AUX);
1928 if (index >= 0 && vmx->rdtscp_enabled)
1929 move_msr_up(vmx, index, save_nmsrs++);
a75beee6 1930 /*
8c06585d 1931 * MSR_STAR is only needed on long mode guests, and only
a75beee6
ED
1932 * if efer.sce is enabled.
1933 */
8c06585d 1934 index = __find_msr_index(vmx, MSR_STAR);
f6801dff 1935 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
8b9cf98c 1936 move_msr_up(vmx, index, save_nmsrs++);
a75beee6
ED
1937 }
1938#endif
92c0d900
AK
1939 index = __find_msr_index(vmx, MSR_EFER);
1940 if (index >= 0 && update_transition_efer(vmx, index))
26bb0981 1941 move_msr_up(vmx, index, save_nmsrs++);
e38aea3e 1942
26bb0981 1943 vmx->save_nmsrs = save_nmsrs;
5897297b 1944
8d14695f
YZ
1945 if (cpu_has_vmx_msr_bitmap())
1946 vmx_set_msr_bitmap(&vmx->vcpu);
e38aea3e
AK
1947}
1948
6aa8b732
AK
1949/*
1950 * reads and returns guest's timestamp counter "register"
1951 * guest_tsc = host_tsc + tsc_offset -- 21.3
1952 */
1953static u64 guest_read_tsc(void)
1954{
1955 u64 host_tsc, tsc_offset;
1956
1957 rdtscll(host_tsc);
1958 tsc_offset = vmcs_read64(TSC_OFFSET);
1959 return host_tsc + tsc_offset;
1960}
1961
d5c1785d
NHE
1962/*
1963 * Like guest_read_tsc, but always returns L1's notion of the timestamp
1964 * counter, even if a nested guest (L2) is currently running.
1965 */
886b470c 1966u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
d5c1785d 1967{
886b470c 1968 u64 tsc_offset;
d5c1785d 1969
d5c1785d
NHE
1970 tsc_offset = is_guest_mode(vcpu) ?
1971 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
1972 vmcs_read64(TSC_OFFSET);
1973 return host_tsc + tsc_offset;
1974}
1975
4051b188 1976/*
cc578287
ZA
1977 * Engage any workarounds for mis-matched TSC rates. Currently limited to
1978 * software catchup for faster rates on slower CPUs.
4051b188 1979 */
cc578287 1980static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
4051b188 1981{
cc578287
ZA
1982 if (!scale)
1983 return;
1984
1985 if (user_tsc_khz > tsc_khz) {
1986 vcpu->arch.tsc_catchup = 1;
1987 vcpu->arch.tsc_always_catchup = 1;
1988 } else
1989 WARN(1, "user requested TSC rate below hardware speed\n");
4051b188
JR
1990}
1991
ba904635
WA
1992static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
1993{
1994 return vmcs_read64(TSC_OFFSET);
1995}
1996
6aa8b732 1997/*
99e3e30a 1998 * writes 'offset' into guest's timestamp counter offset register
6aa8b732 1999 */
99e3e30a 2000static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
6aa8b732 2001{
27fc51b2 2002 if (is_guest_mode(vcpu)) {
7991825b 2003 /*
27fc51b2
NHE
2004 * We're here if L1 chose not to trap WRMSR to TSC. According
2005 * to the spec, this should set L1's TSC; The offset that L1
2006 * set for L2 remains unchanged, and still needs to be added
2007 * to the newly set TSC to get L2's TSC.
7991825b 2008 */
27fc51b2
NHE
2009 struct vmcs12 *vmcs12;
2010 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2011 /* recalculate vmcs02.TSC_OFFSET: */
2012 vmcs12 = get_vmcs12(vcpu);
2013 vmcs_write64(TSC_OFFSET, offset +
2014 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2015 vmcs12->tsc_offset : 0));
2016 } else {
2017 vmcs_write64(TSC_OFFSET, offset);
2018 }
6aa8b732
AK
2019}
2020
f1e2b260 2021static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
e48672fa
ZA
2022{
2023 u64 offset = vmcs_read64(TSC_OFFSET);
2024 vmcs_write64(TSC_OFFSET, offset + adjustment);
7991825b
NHE
2025 if (is_guest_mode(vcpu)) {
2026 /* Even when running L2, the adjustment needs to apply to L1 */
2027 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
2028 }
e48672fa
ZA
2029}
2030
857e4099
JR
2031static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2032{
2033 return target_tsc - native_read_tsc();
2034}
2035
801d3424
NHE
2036static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2037{
2038 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2039 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2040}
2041
2042/*
2043 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2044 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2045 * all guests if the "nested" module option is off, and can also be disabled
2046 * for a single guest by disabling its VMX cpuid bit.
2047 */
2048static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2049{
2050 return nested && guest_cpuid_has_vmx(vcpu);
2051}
2052
b87a51ae
NHE
2053/*
2054 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2055 * returned for the various VMX controls MSRs when nested VMX is enabled.
2056 * The same values should also be used to verify that vmcs12 control fields are
2057 * valid during nested entry from L1 to L2.
2058 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2059 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2060 * bit in the high half is on if the corresponding bit in the control field
2061 * may be on. See also vmx_control_verify().
2062 * TODO: allow these variables to be modified (downgraded) by module options
2063 * or other means.
2064 */
2065static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
2066static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
2067static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
2068static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
2069static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
c18911a2 2070static u32 nested_vmx_misc_low, nested_vmx_misc_high;
b87a51ae
NHE
2071static __init void nested_vmx_setup_ctls_msrs(void)
2072{
2073 /*
2074 * Note that as a general rule, the high half of the MSRs (bits in
2075 * the control fields which may be 1) should be initialized by the
2076 * intersection of the underlying hardware's MSR (i.e., features which
2077 * can be supported) and the list of features we want to expose -
2078 * because they are known to be properly supported in our code.
2079 * Also, usually, the low half of the MSRs (bits which must be 1) can
2080 * be set to 0, meaning that L1 may turn off any of these bits. The
2081 * reason is that if one of these bits is necessary, it will appear
2082 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2083 * fields of vmcs01 and vmcs02, will turn these bits off - and
2084 * nested_vmx_exit_handled() will not pass related exits to L1.
2085 * These rules have exceptions below.
2086 */
2087
2088 /* pin-based controls */
eabeaacc
JK
2089 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2090 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high);
b87a51ae
NHE
2091 /*
2092 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
2093 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
2094 */
eabeaacc
JK
2095 nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2096 nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK |
0238ea91
JK
2097 PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS |
2098 PIN_BASED_VMX_PREEMPTION_TIMER;
eabeaacc 2099 nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae 2100
33fb20c3
JK
2101 /*
2102 * Exit controls
2103 * If bit 55 of VMX_BASIC is off, bits 0-8 and 10, 11, 13, 14, 16 and
2104 * 17 must be 1.
2105 */
2106 nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
b6f1250e 2107 /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
b87a51ae
NHE
2108#ifdef CONFIG_X86_64
2109 nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
2110#else
2111 nested_vmx_exit_ctls_high = 0;
2112#endif
33fb20c3 2113 nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
2114
2115 /* entry controls */
2116 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2117 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
33fb20c3
JK
2118 /* If bit 55 of VMX_BASIC is off, bits 0-8 and 12 must be 1. */
2119 nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
2120 nested_vmx_entry_ctls_high &=
2121 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
33fb20c3 2122 nested_vmx_entry_ctls_high |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
b87a51ae
NHE
2123
2124 /* cpu-based controls */
2125 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2126 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
2127 nested_vmx_procbased_ctls_low = 0;
2128 nested_vmx_procbased_ctls_high &=
2129 CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
2130 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2131 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2132 CPU_BASED_CR3_STORE_EXITING |
2133#ifdef CONFIG_X86_64
2134 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2135#endif
2136 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2137 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
dbcb4e79 2138 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
d6851fbe 2139 CPU_BASED_PAUSE_EXITING |
b87a51ae
NHE
2140 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2141 /*
2142 * We can allow some features even when not supported by the
2143 * hardware. For example, L1 can specify an MSR bitmap - and we
2144 * can use it to avoid exits to L1 - even when L0 runs L2
2145 * without MSR bitmaps.
2146 */
2147 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
2148
2149 /* secondary cpu-based controls */
2150 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2151 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2152 nested_vmx_secondary_ctls_low = 0;
2153 nested_vmx_secondary_ctls_high &=
d6851fbe
JK
2154 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2155 SECONDARY_EXEC_WBINVD_EXITING;
c18911a2
JK
2156
2157 /* miscellaneous data */
2158 rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high);
0238ea91
JK
2159 nested_vmx_misc_low &= VMX_MISC_PREEMPTION_TIMER_RATE_MASK |
2160 VMX_MISC_SAVE_EFER_LMA;
c18911a2 2161 nested_vmx_misc_high = 0;
b87a51ae
NHE
2162}
2163
2164static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2165{
2166 /*
2167 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2168 */
2169 return ((control & high) | low) == control;
2170}
2171
2172static inline u64 vmx_control_msr(u32 low, u32 high)
2173{
2174 return low | ((u64)high << 32);
2175}
2176
2177/*
2178 * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
2179 * also let it use VMX-specific MSRs.
2180 * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
2181 * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
2182 * like all other MSRs).
2183 */
2184static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2185{
2186 if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
2187 msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
2188 /*
2189 * According to the spec, processors which do not support VMX
2190 * should throw a #GP(0) when VMX capability MSRs are read.
2191 */
2192 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
2193 return 1;
2194 }
2195
2196 switch (msr_index) {
2197 case MSR_IA32_FEATURE_CONTROL:
2198 *pdata = 0;
2199 break;
2200 case MSR_IA32_VMX_BASIC:
2201 /*
2202 * This MSR reports some information about VMX support. We
2203 * should return information about the VMX we emulate for the
2204 * guest, and the VMCS structure we give it - not about the
2205 * VMX support of the underlying hardware.
2206 */
2207 *pdata = VMCS12_REVISION |
2208 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2209 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2210 break;
2211 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2212 case MSR_IA32_VMX_PINBASED_CTLS:
2213 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2214 nested_vmx_pinbased_ctls_high);
2215 break;
2216 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2217 case MSR_IA32_VMX_PROCBASED_CTLS:
2218 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2219 nested_vmx_procbased_ctls_high);
2220 break;
2221 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2222 case MSR_IA32_VMX_EXIT_CTLS:
2223 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2224 nested_vmx_exit_ctls_high);
2225 break;
2226 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2227 case MSR_IA32_VMX_ENTRY_CTLS:
2228 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2229 nested_vmx_entry_ctls_high);
2230 break;
2231 case MSR_IA32_VMX_MISC:
c18911a2
JK
2232 *pdata = vmx_control_msr(nested_vmx_misc_low,
2233 nested_vmx_misc_high);
b87a51ae
NHE
2234 break;
2235 /*
2236 * These MSRs specify bits which the guest must keep fixed (on or off)
2237 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2238 * We picked the standard core2 setting.
2239 */
2240#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2241#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2242 case MSR_IA32_VMX_CR0_FIXED0:
2243 *pdata = VMXON_CR0_ALWAYSON;
2244 break;
2245 case MSR_IA32_VMX_CR0_FIXED1:
2246 *pdata = -1ULL;
2247 break;
2248 case MSR_IA32_VMX_CR4_FIXED0:
2249 *pdata = VMXON_CR4_ALWAYSON;
2250 break;
2251 case MSR_IA32_VMX_CR4_FIXED1:
2252 *pdata = -1ULL;
2253 break;
2254 case MSR_IA32_VMX_VMCS_ENUM:
2255 *pdata = 0x1f;
2256 break;
2257 case MSR_IA32_VMX_PROCBASED_CTLS2:
2258 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2259 nested_vmx_secondary_ctls_high);
2260 break;
2261 case MSR_IA32_VMX_EPT_VPID_CAP:
2262 /* Currently, no nested ept or nested vpid */
2263 *pdata = 0;
2264 break;
2265 default:
2266 return 0;
2267 }
2268
2269 return 1;
2270}
2271
2272static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
2273{
2274 if (!nested_vmx_allowed(vcpu))
2275 return 0;
2276
2277 if (msr_index == MSR_IA32_FEATURE_CONTROL)
2278 /* TODO: the right thing. */
2279 return 1;
2280 /*
2281 * No need to treat VMX capability MSRs specially: If we don't handle
2282 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
2283 */
2284 return 0;
2285}
2286
6aa8b732
AK
2287/*
2288 * Reads an msr value (of 'msr_index') into 'pdata'.
2289 * Returns 0 on success, non-0 otherwise.
2290 * Assumes vcpu_load() was already called.
2291 */
2292static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2293{
2294 u64 data;
26bb0981 2295 struct shared_msr_entry *msr;
6aa8b732
AK
2296
2297 if (!pdata) {
2298 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2299 return -EINVAL;
2300 }
2301
2302 switch (msr_index) {
05b3e0c2 2303#ifdef CONFIG_X86_64
6aa8b732
AK
2304 case MSR_FS_BASE:
2305 data = vmcs_readl(GUEST_FS_BASE);
2306 break;
2307 case MSR_GS_BASE:
2308 data = vmcs_readl(GUEST_GS_BASE);
2309 break;
44ea2b17
AK
2310 case MSR_KERNEL_GS_BASE:
2311 vmx_load_host_state(to_vmx(vcpu));
2312 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2313 break;
26bb0981 2314#endif
6aa8b732 2315 case MSR_EFER:
3bab1f5d 2316 return kvm_get_msr_common(vcpu, msr_index, pdata);
af24a4e4 2317 case MSR_IA32_TSC:
6aa8b732
AK
2318 data = guest_read_tsc();
2319 break;
2320 case MSR_IA32_SYSENTER_CS:
2321 data = vmcs_read32(GUEST_SYSENTER_CS);
2322 break;
2323 case MSR_IA32_SYSENTER_EIP:
f5b42c33 2324 data = vmcs_readl(GUEST_SYSENTER_EIP);
6aa8b732
AK
2325 break;
2326 case MSR_IA32_SYSENTER_ESP:
f5b42c33 2327 data = vmcs_readl(GUEST_SYSENTER_ESP);
6aa8b732 2328 break;
4e47c7a6
SY
2329 case MSR_TSC_AUX:
2330 if (!to_vmx(vcpu)->rdtscp_enabled)
2331 return 1;
2332 /* Otherwise falls through */
6aa8b732 2333 default:
b87a51ae
NHE
2334 if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
2335 return 0;
8b9cf98c 2336 msr = find_msr_entry(to_vmx(vcpu), msr_index);
3bab1f5d
AK
2337 if (msr) {
2338 data = msr->data;
2339 break;
6aa8b732 2340 }
3bab1f5d 2341 return kvm_get_msr_common(vcpu, msr_index, pdata);
6aa8b732
AK
2342 }
2343
2344 *pdata = data;
2345 return 0;
2346}
2347
2348/*
2349 * Writes msr value into into the appropriate "register".
2350 * Returns 0 on success, non-0 otherwise.
2351 * Assumes vcpu_load() was already called.
2352 */
8fe8ab46 2353static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
6aa8b732 2354{
a2fa3e9f 2355 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981 2356 struct shared_msr_entry *msr;
2cc51560 2357 int ret = 0;
8fe8ab46
WA
2358 u32 msr_index = msr_info->index;
2359 u64 data = msr_info->data;
2cc51560 2360
6aa8b732 2361 switch (msr_index) {
3bab1f5d 2362 case MSR_EFER:
8fe8ab46 2363 ret = kvm_set_msr_common(vcpu, msr_info);
2cc51560 2364 break;
16175a79 2365#ifdef CONFIG_X86_64
6aa8b732 2366 case MSR_FS_BASE:
2fb92db1 2367 vmx_segment_cache_clear(vmx);
6aa8b732
AK
2368 vmcs_writel(GUEST_FS_BASE, data);
2369 break;
2370 case MSR_GS_BASE:
2fb92db1 2371 vmx_segment_cache_clear(vmx);
6aa8b732
AK
2372 vmcs_writel(GUEST_GS_BASE, data);
2373 break;
44ea2b17
AK
2374 case MSR_KERNEL_GS_BASE:
2375 vmx_load_host_state(vmx);
2376 vmx->msr_guest_kernel_gs_base = data;
2377 break;
6aa8b732
AK
2378#endif
2379 case MSR_IA32_SYSENTER_CS:
2380 vmcs_write32(GUEST_SYSENTER_CS, data);
2381 break;
2382 case MSR_IA32_SYSENTER_EIP:
f5b42c33 2383 vmcs_writel(GUEST_SYSENTER_EIP, data);
6aa8b732
AK
2384 break;
2385 case MSR_IA32_SYSENTER_ESP:
f5b42c33 2386 vmcs_writel(GUEST_SYSENTER_ESP, data);
6aa8b732 2387 break;
af24a4e4 2388 case MSR_IA32_TSC:
8fe8ab46 2389 kvm_write_tsc(vcpu, msr_info);
6aa8b732 2390 break;
468d472f
SY
2391 case MSR_IA32_CR_PAT:
2392 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2393 vmcs_write64(GUEST_IA32_PAT, data);
2394 vcpu->arch.pat = data;
2395 break;
2396 }
8fe8ab46 2397 ret = kvm_set_msr_common(vcpu, msr_info);
4e47c7a6 2398 break;
ba904635
WA
2399 case MSR_IA32_TSC_ADJUST:
2400 ret = kvm_set_msr_common(vcpu, msr_info);
4e47c7a6
SY
2401 break;
2402 case MSR_TSC_AUX:
2403 if (!vmx->rdtscp_enabled)
2404 return 1;
2405 /* Check reserved bit, higher 32 bits should be zero */
2406 if ((data >> 32) != 0)
2407 return 1;
2408 /* Otherwise falls through */
6aa8b732 2409 default:
b87a51ae
NHE
2410 if (vmx_set_vmx_msr(vcpu, msr_index, data))
2411 break;
8b9cf98c 2412 msr = find_msr_entry(vmx, msr_index);
3bab1f5d
AK
2413 if (msr) {
2414 msr->data = data;
2225fd56
AK
2415 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2416 preempt_disable();
9ee73970
AK
2417 kvm_set_shared_msr(msr->index, msr->data,
2418 msr->mask);
2225fd56
AK
2419 preempt_enable();
2420 }
3bab1f5d 2421 break;
6aa8b732 2422 }
8fe8ab46 2423 ret = kvm_set_msr_common(vcpu, msr_info);
6aa8b732
AK
2424 }
2425
2cc51560 2426 return ret;
6aa8b732
AK
2427}
2428
5fdbf976 2429static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
6aa8b732 2430{
5fdbf976
MT
2431 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2432 switch (reg) {
2433 case VCPU_REGS_RSP:
2434 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2435 break;
2436 case VCPU_REGS_RIP:
2437 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2438 break;
6de4f3ad
AK
2439 case VCPU_EXREG_PDPTR:
2440 if (enable_ept)
2441 ept_save_pdptrs(vcpu);
2442 break;
5fdbf976
MT
2443 default:
2444 break;
2445 }
6aa8b732
AK
2446}
2447
6aa8b732
AK
2448static __init int cpu_has_kvm_support(void)
2449{
6210e37b 2450 return cpu_has_vmx();
6aa8b732
AK
2451}
2452
2453static __init int vmx_disabled_by_bios(void)
2454{
2455 u64 msr;
2456
2457 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
cafd6659 2458 if (msr & FEATURE_CONTROL_LOCKED) {
23f3e991 2459 /* launched w/ TXT and VMX disabled */
cafd6659
SW
2460 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2461 && tboot_enabled())
2462 return 1;
23f3e991 2463 /* launched w/o TXT and VMX only enabled w/ TXT */
cafd6659 2464 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
23f3e991 2465 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
f9335afe
SW
2466 && !tboot_enabled()) {
2467 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
23f3e991 2468 "activate TXT before enabling KVM\n");
cafd6659 2469 return 1;
f9335afe 2470 }
23f3e991
JC
2471 /* launched w/o TXT and VMX disabled */
2472 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2473 && !tboot_enabled())
2474 return 1;
cafd6659
SW
2475 }
2476
2477 return 0;
6aa8b732
AK
2478}
2479
7725b894
DX
2480static void kvm_cpu_vmxon(u64 addr)
2481{
2482 asm volatile (ASM_VMX_VMXON_RAX
2483 : : "a"(&addr), "m"(addr)
2484 : "memory", "cc");
2485}
2486
10474ae8 2487static int hardware_enable(void *garbage)
6aa8b732
AK
2488{
2489 int cpu = raw_smp_processor_id();
2490 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
cafd6659 2491 u64 old, test_bits;
6aa8b732 2492
10474ae8
AG
2493 if (read_cr4() & X86_CR4_VMXE)
2494 return -EBUSY;
2495
d462b819 2496 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
8f536b76
ZY
2497
2498 /*
2499 * Now we can enable the vmclear operation in kdump
2500 * since the loaded_vmcss_on_cpu list on this cpu
2501 * has been initialized.
2502 *
2503 * Though the cpu is not in VMX operation now, there
2504 * is no problem to enable the vmclear operation
2505 * for the loaded_vmcss_on_cpu list is empty!
2506 */
2507 crash_enable_local_vmclear(cpu);
2508
6aa8b732 2509 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
cafd6659
SW
2510
2511 test_bits = FEATURE_CONTROL_LOCKED;
2512 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2513 if (tboot_enabled())
2514 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2515
2516 if ((old & test_bits) != test_bits) {
6aa8b732 2517 /* enable and lock */
cafd6659
SW
2518 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2519 }
66aee91a 2520 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
10474ae8 2521
4610c9cc
DX
2522 if (vmm_exclusive) {
2523 kvm_cpu_vmxon(phys_addr);
2524 ept_sync_global();
2525 }
10474ae8 2526
3444d7da
AK
2527 store_gdt(&__get_cpu_var(host_gdt));
2528
10474ae8 2529 return 0;
6aa8b732
AK
2530}
2531
d462b819 2532static void vmclear_local_loaded_vmcss(void)
543e4243
AK
2533{
2534 int cpu = raw_smp_processor_id();
d462b819 2535 struct loaded_vmcs *v, *n;
543e4243 2536
d462b819
NHE
2537 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2538 loaded_vmcss_on_cpu_link)
2539 __loaded_vmcs_clear(v);
543e4243
AK
2540}
2541
710ff4a8
EH
2542
2543/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2544 * tricks.
2545 */
2546static void kvm_cpu_vmxoff(void)
6aa8b732 2547{
4ecac3fd 2548 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
6aa8b732
AK
2549}
2550
710ff4a8
EH
2551static void hardware_disable(void *garbage)
2552{
4610c9cc 2553 if (vmm_exclusive) {
d462b819 2554 vmclear_local_loaded_vmcss();
4610c9cc
DX
2555 kvm_cpu_vmxoff();
2556 }
7725b894 2557 write_cr4(read_cr4() & ~X86_CR4_VMXE);
710ff4a8
EH
2558}
2559
1c3d14fe 2560static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
d77c26fc 2561 u32 msr, u32 *result)
1c3d14fe
YS
2562{
2563 u32 vmx_msr_low, vmx_msr_high;
2564 u32 ctl = ctl_min | ctl_opt;
2565
2566 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2567
2568 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2569 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2570
2571 /* Ensure minimum (required) set of control bits are supported. */
2572 if (ctl_min & ~ctl)
002c7f7c 2573 return -EIO;
1c3d14fe
YS
2574
2575 *result = ctl;
2576 return 0;
2577}
2578
110312c8
AK
2579static __init bool allow_1_setting(u32 msr, u32 ctl)
2580{
2581 u32 vmx_msr_low, vmx_msr_high;
2582
2583 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2584 return vmx_msr_high & ctl;
2585}
2586
002c7f7c 2587static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
6aa8b732
AK
2588{
2589 u32 vmx_msr_low, vmx_msr_high;
d56f546d 2590 u32 min, opt, min2, opt2;
1c3d14fe
YS
2591 u32 _pin_based_exec_control = 0;
2592 u32 _cpu_based_exec_control = 0;
f78e0e2e 2593 u32 _cpu_based_2nd_exec_control = 0;
1c3d14fe
YS
2594 u32 _vmexit_control = 0;
2595 u32 _vmentry_control = 0;
2596
10166744 2597 min = CPU_BASED_HLT_EXITING |
1c3d14fe
YS
2598#ifdef CONFIG_X86_64
2599 CPU_BASED_CR8_LOAD_EXITING |
2600 CPU_BASED_CR8_STORE_EXITING |
2601#endif
d56f546d
SY
2602 CPU_BASED_CR3_LOAD_EXITING |
2603 CPU_BASED_CR3_STORE_EXITING |
1c3d14fe
YS
2604 CPU_BASED_USE_IO_BITMAPS |
2605 CPU_BASED_MOV_DR_EXITING |
a7052897 2606 CPU_BASED_USE_TSC_OFFSETING |
59708670
SY
2607 CPU_BASED_MWAIT_EXITING |
2608 CPU_BASED_MONITOR_EXITING |
fee84b07
AK
2609 CPU_BASED_INVLPG_EXITING |
2610 CPU_BASED_RDPMC_EXITING;
443381a8 2611
f78e0e2e 2612 opt = CPU_BASED_TPR_SHADOW |
25c5f225 2613 CPU_BASED_USE_MSR_BITMAPS |
f78e0e2e 2614 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1c3d14fe
YS
2615 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2616 &_cpu_based_exec_control) < 0)
002c7f7c 2617 return -EIO;
6e5d865c
YS
2618#ifdef CONFIG_X86_64
2619 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2620 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2621 ~CPU_BASED_CR8_STORE_EXITING;
2622#endif
f78e0e2e 2623 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
d56f546d
SY
2624 min2 = 0;
2625 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
8d14695f 2626 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2384d2b3 2627 SECONDARY_EXEC_WBINVD_EXITING |
d56f546d 2628 SECONDARY_EXEC_ENABLE_VPID |
3a624e29 2629 SECONDARY_EXEC_ENABLE_EPT |
4b8d54f9 2630 SECONDARY_EXEC_UNRESTRICTED_GUEST |
4e47c7a6 2631 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
ad756a16 2632 SECONDARY_EXEC_RDTSCP |
83d4c286 2633 SECONDARY_EXEC_ENABLE_INVPCID |
c7c9c56c
YZ
2634 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2635 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
d56f546d
SY
2636 if (adjust_vmx_controls(min2, opt2,
2637 MSR_IA32_VMX_PROCBASED_CTLS2,
f78e0e2e
SY
2638 &_cpu_based_2nd_exec_control) < 0)
2639 return -EIO;
2640 }
2641#ifndef CONFIG_X86_64
2642 if (!(_cpu_based_2nd_exec_control &
2643 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2644 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2645#endif
83d4c286
YZ
2646
2647 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2648 _cpu_based_2nd_exec_control &= ~(
8d14695f 2649 SECONDARY_EXEC_APIC_REGISTER_VIRT |
c7c9c56c
YZ
2650 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2651 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
83d4c286 2652
d56f546d 2653 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
a7052897
MT
2654 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2655 enabled */
5fff7d27
GN
2656 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2657 CPU_BASED_CR3_STORE_EXITING |
2658 CPU_BASED_INVLPG_EXITING);
d56f546d
SY
2659 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2660 vmx_capability.ept, vmx_capability.vpid);
2661 }
1c3d14fe
YS
2662
2663 min = 0;
2664#ifdef CONFIG_X86_64
2665 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2666#endif
a547c6db
YZ
2667 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
2668 VM_EXIT_ACK_INTR_ON_EXIT;
1c3d14fe
YS
2669 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2670 &_vmexit_control) < 0)
002c7f7c 2671 return -EIO;
1c3d14fe 2672
01e439be
YZ
2673 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2674 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
2675 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2676 &_pin_based_exec_control) < 0)
2677 return -EIO;
2678
2679 if (!(_cpu_based_2nd_exec_control &
2680 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
2681 !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
2682 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2683
468d472f
SY
2684 min = 0;
2685 opt = VM_ENTRY_LOAD_IA32_PAT;
1c3d14fe
YS
2686 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2687 &_vmentry_control) < 0)
002c7f7c 2688 return -EIO;
6aa8b732 2689
c68876fd 2690 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
1c3d14fe
YS
2691
2692 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2693 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
002c7f7c 2694 return -EIO;
1c3d14fe
YS
2695
2696#ifdef CONFIG_X86_64
2697 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2698 if (vmx_msr_high & (1u<<16))
002c7f7c 2699 return -EIO;
1c3d14fe
YS
2700#endif
2701
2702 /* Require Write-Back (WB) memory type for VMCS accesses. */
2703 if (((vmx_msr_high >> 18) & 15) != 6)
002c7f7c 2704 return -EIO;
1c3d14fe 2705
002c7f7c
YS
2706 vmcs_conf->size = vmx_msr_high & 0x1fff;
2707 vmcs_conf->order = get_order(vmcs_config.size);
2708 vmcs_conf->revision_id = vmx_msr_low;
1c3d14fe 2709
002c7f7c
YS
2710 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2711 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
f78e0e2e 2712 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
002c7f7c
YS
2713 vmcs_conf->vmexit_ctrl = _vmexit_control;
2714 vmcs_conf->vmentry_ctrl = _vmentry_control;
1c3d14fe 2715
110312c8
AK
2716 cpu_has_load_ia32_efer =
2717 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2718 VM_ENTRY_LOAD_IA32_EFER)
2719 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2720 VM_EXIT_LOAD_IA32_EFER);
2721
8bf00a52
GN
2722 cpu_has_load_perf_global_ctrl =
2723 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2724 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2725 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2726 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2727
2728 /*
2729 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2730 * but due to arrata below it can't be used. Workaround is to use
2731 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2732 *
2733 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2734 *
2735 * AAK155 (model 26)
2736 * AAP115 (model 30)
2737 * AAT100 (model 37)
2738 * BC86,AAY89,BD102 (model 44)
2739 * BA97 (model 46)
2740 *
2741 */
2742 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2743 switch (boot_cpu_data.x86_model) {
2744 case 26:
2745 case 30:
2746 case 37:
2747 case 44:
2748 case 46:
2749 cpu_has_load_perf_global_ctrl = false;
2750 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2751 "does not work properly. Using workaround\n");
2752 break;
2753 default:
2754 break;
2755 }
2756 }
2757
1c3d14fe 2758 return 0;
c68876fd 2759}
6aa8b732
AK
2760
2761static struct vmcs *alloc_vmcs_cpu(int cpu)
2762{
2763 int node = cpu_to_node(cpu);
2764 struct page *pages;
2765 struct vmcs *vmcs;
2766
6484eb3e 2767 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
6aa8b732
AK
2768 if (!pages)
2769 return NULL;
2770 vmcs = page_address(pages);
1c3d14fe
YS
2771 memset(vmcs, 0, vmcs_config.size);
2772 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
6aa8b732
AK
2773 return vmcs;
2774}
2775
2776static struct vmcs *alloc_vmcs(void)
2777{
d3b2c338 2778 return alloc_vmcs_cpu(raw_smp_processor_id());
6aa8b732
AK
2779}
2780
2781static void free_vmcs(struct vmcs *vmcs)
2782{
1c3d14fe 2783 free_pages((unsigned long)vmcs, vmcs_config.order);
6aa8b732
AK
2784}
2785
d462b819
NHE
2786/*
2787 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2788 */
2789static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2790{
2791 if (!loaded_vmcs->vmcs)
2792 return;
2793 loaded_vmcs_clear(loaded_vmcs);
2794 free_vmcs(loaded_vmcs->vmcs);
2795 loaded_vmcs->vmcs = NULL;
2796}
2797
39959588 2798static void free_kvm_area(void)
6aa8b732
AK
2799{
2800 int cpu;
2801
3230bb47 2802 for_each_possible_cpu(cpu) {
6aa8b732 2803 free_vmcs(per_cpu(vmxarea, cpu));
3230bb47
ZA
2804 per_cpu(vmxarea, cpu) = NULL;
2805 }
6aa8b732
AK
2806}
2807
6aa8b732
AK
2808static __init int alloc_kvm_area(void)
2809{
2810 int cpu;
2811
3230bb47 2812 for_each_possible_cpu(cpu) {
6aa8b732
AK
2813 struct vmcs *vmcs;
2814
2815 vmcs = alloc_vmcs_cpu(cpu);
2816 if (!vmcs) {
2817 free_kvm_area();
2818 return -ENOMEM;
2819 }
2820
2821 per_cpu(vmxarea, cpu) = vmcs;
2822 }
2823 return 0;
2824}
2825
2826static __init int hardware_setup(void)
2827{
002c7f7c
YS
2828 if (setup_vmcs_config(&vmcs_config) < 0)
2829 return -EIO;
50a37eb4
JR
2830
2831 if (boot_cpu_has(X86_FEATURE_NX))
2832 kvm_enable_efer_bits(EFER_NX);
2833
93ba03c2
SY
2834 if (!cpu_has_vmx_vpid())
2835 enable_vpid = 0;
2836
4bc9b982
SY
2837 if (!cpu_has_vmx_ept() ||
2838 !cpu_has_vmx_ept_4levels()) {
93ba03c2 2839 enable_ept = 0;
3a624e29 2840 enable_unrestricted_guest = 0;
83c3a331 2841 enable_ept_ad_bits = 0;
3a624e29
NK
2842 }
2843
83c3a331
XH
2844 if (!cpu_has_vmx_ept_ad_bits())
2845 enable_ept_ad_bits = 0;
2846
3a624e29
NK
2847 if (!cpu_has_vmx_unrestricted_guest())
2848 enable_unrestricted_guest = 0;
93ba03c2
SY
2849
2850 if (!cpu_has_vmx_flexpriority())
2851 flexpriority_enabled = 0;
2852
95ba8273
GN
2853 if (!cpu_has_vmx_tpr_shadow())
2854 kvm_x86_ops->update_cr8_intercept = NULL;
2855
54dee993
MT
2856 if (enable_ept && !cpu_has_vmx_ept_2m_page())
2857 kvm_disable_largepages();
2858
4b8d54f9
ZE
2859 if (!cpu_has_vmx_ple())
2860 ple_gap = 0;
2861
01e439be
YZ
2862 if (!cpu_has_vmx_apicv())
2863 enable_apicv = 0;
c7c9c56c 2864
01e439be 2865 if (enable_apicv)
c7c9c56c 2866 kvm_x86_ops->update_cr8_intercept = NULL;
a20ed54d 2867 else {
c7c9c56c 2868 kvm_x86_ops->hwapic_irr_update = NULL;
a20ed54d
YZ
2869 kvm_x86_ops->deliver_posted_interrupt = NULL;
2870 kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
2871 }
83d4c286 2872
b87a51ae
NHE
2873 if (nested)
2874 nested_vmx_setup_ctls_msrs();
2875
6aa8b732
AK
2876 return alloc_kvm_area();
2877}
2878
2879static __exit void hardware_unsetup(void)
2880{
2881 free_kvm_area();
2882}
2883
14168786
GN
2884static bool emulation_required(struct kvm_vcpu *vcpu)
2885{
2886 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2887}
2888
91b0aa2c 2889static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
d99e4152 2890 struct kvm_segment *save)
6aa8b732 2891{
d99e4152
GN
2892 if (!emulate_invalid_guest_state) {
2893 /*
2894 * CS and SS RPL should be equal during guest entry according
2895 * to VMX spec, but in reality it is not always so. Since vcpu
2896 * is in the middle of the transition from real mode to
2897 * protected mode it is safe to assume that RPL 0 is a good
2898 * default value.
2899 */
2900 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
2901 save->selector &= ~SELECTOR_RPL_MASK;
2902 save->dpl = save->selector & SELECTOR_RPL_MASK;
2903 save->s = 1;
6aa8b732 2904 }
d99e4152 2905 vmx_set_segment(vcpu, save, seg);
6aa8b732
AK
2906}
2907
2908static void enter_pmode(struct kvm_vcpu *vcpu)
2909{
2910 unsigned long flags;
a89a8fb9 2911 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732 2912
d99e4152
GN
2913 /*
2914 * Update real mode segment cache. It may be not up-to-date if sement
2915 * register was written while vcpu was in a guest mode.
2916 */
2917 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2918 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2919 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2920 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2921 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2922 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2923
7ffd92c5 2924 vmx->rmode.vm86_active = 0;
6aa8b732 2925
2fb92db1
AK
2926 vmx_segment_cache_clear(vmx);
2927
f5f7b2fe 2928 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
6aa8b732
AK
2929
2930 flags = vmcs_readl(GUEST_RFLAGS);
78ac8b47
AK
2931 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2932 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
6aa8b732
AK
2933 vmcs_writel(GUEST_RFLAGS, flags);
2934
66aee91a
RR
2935 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2936 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
6aa8b732
AK
2937
2938 update_exception_bitmap(vcpu);
2939
91b0aa2c
GN
2940 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2941 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2942 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2943 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2944 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2945 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
1f3141e8
GN
2946
2947 /* CPL is always 0 when CPU enters protected mode */
2948 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2949 vmx->cpl = 0;
6aa8b732
AK
2950}
2951
f5f7b2fe 2952static void fix_rmode_seg(int seg, struct kvm_segment *save)
6aa8b732 2953{
772e0318 2954 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
d99e4152
GN
2955 struct kvm_segment var = *save;
2956
2957 var.dpl = 0x3;
2958 if (seg == VCPU_SREG_CS)
2959 var.type = 0x3;
2960
2961 if (!emulate_invalid_guest_state) {
2962 var.selector = var.base >> 4;
2963 var.base = var.base & 0xffff0;
2964 var.limit = 0xffff;
2965 var.g = 0;
2966 var.db = 0;
2967 var.present = 1;
2968 var.s = 1;
2969 var.l = 0;
2970 var.unusable = 0;
2971 var.type = 0x3;
2972 var.avl = 0;
2973 if (save->base & 0xf)
2974 printk_once(KERN_WARNING "kvm: segment base is not "
2975 "paragraph aligned when entering "
2976 "protected mode (seg=%d)", seg);
2977 }
6aa8b732 2978
d99e4152
GN
2979 vmcs_write16(sf->selector, var.selector);
2980 vmcs_write32(sf->base, var.base);
2981 vmcs_write32(sf->limit, var.limit);
2982 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
6aa8b732
AK
2983}
2984
2985static void enter_rmode(struct kvm_vcpu *vcpu)
2986{
2987 unsigned long flags;
a89a8fb9 2988 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732 2989
f5f7b2fe
AK
2990 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2991 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2992 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2993 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2994 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
c6ad1153
GN
2995 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2996 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
f5f7b2fe 2997
7ffd92c5 2998 vmx->rmode.vm86_active = 1;
6aa8b732 2999
776e58ea
GN
3000 /*
3001 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
4918c6ca 3002 * vcpu. Warn the user that an update is overdue.
776e58ea 3003 */
4918c6ca 3004 if (!vcpu->kvm->arch.tss_addr)
776e58ea
GN
3005 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3006 "called before entering vcpu\n");
776e58ea 3007
2fb92db1
AK
3008 vmx_segment_cache_clear(vmx);
3009
4918c6ca 3010 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
6aa8b732 3011 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
6aa8b732
AK
3012 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3013
3014 flags = vmcs_readl(GUEST_RFLAGS);
78ac8b47 3015 vmx->rmode.save_rflags = flags;
6aa8b732 3016
053de044 3017 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
6aa8b732
AK
3018
3019 vmcs_writel(GUEST_RFLAGS, flags);
66aee91a 3020 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
6aa8b732
AK
3021 update_exception_bitmap(vcpu);
3022
d99e4152
GN
3023 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3024 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3025 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3026 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3027 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3028 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
b246dd5d 3029
8668a3c4 3030 kvm_mmu_reset_context(vcpu);
6aa8b732
AK
3031}
3032
401d10de
AS
3033static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3034{
3035 struct vcpu_vmx *vmx = to_vmx(vcpu);
26bb0981
AK
3036 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3037
3038 if (!msr)
3039 return;
401d10de 3040
44ea2b17
AK
3041 /*
3042 * Force kernel_gs_base reloading before EFER changes, as control
3043 * of this msr depends on is_long_mode().
3044 */
3045 vmx_load_host_state(to_vmx(vcpu));
f6801dff 3046 vcpu->arch.efer = efer;
401d10de
AS
3047 if (efer & EFER_LMA) {
3048 vmcs_write32(VM_ENTRY_CONTROLS,
3049 vmcs_read32(VM_ENTRY_CONTROLS) |
3050 VM_ENTRY_IA32E_MODE);
3051 msr->data = efer;
3052 } else {
3053 vmcs_write32(VM_ENTRY_CONTROLS,
3054 vmcs_read32(VM_ENTRY_CONTROLS) &
3055 ~VM_ENTRY_IA32E_MODE);
3056
3057 msr->data = efer & ~EFER_LME;
3058 }
3059 setup_msrs(vmx);
3060}
3061
05b3e0c2 3062#ifdef CONFIG_X86_64
6aa8b732
AK
3063
3064static void enter_lmode(struct kvm_vcpu *vcpu)
3065{
3066 u32 guest_tr_ar;
3067
2fb92db1
AK
3068 vmx_segment_cache_clear(to_vmx(vcpu));
3069
6aa8b732
AK
3070 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3071 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
bd80158a
JK
3072 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3073 __func__);
6aa8b732
AK
3074 vmcs_write32(GUEST_TR_AR_BYTES,
3075 (guest_tr_ar & ~AR_TYPE_MASK)
3076 | AR_TYPE_BUSY_64_TSS);
3077 }
da38f438 3078 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
6aa8b732
AK
3079}
3080
3081static void exit_lmode(struct kvm_vcpu *vcpu)
3082{
6aa8b732
AK
3083 vmcs_write32(VM_ENTRY_CONTROLS,
3084 vmcs_read32(VM_ENTRY_CONTROLS)
1e4e6e00 3085 & ~VM_ENTRY_IA32E_MODE);
da38f438 3086 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
6aa8b732
AK
3087}
3088
3089#endif
3090
2384d2b3
SY
3091static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3092{
b9d762fa 3093 vpid_sync_context(to_vmx(vcpu));
dd180b3e
XG
3094 if (enable_ept) {
3095 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3096 return;
4e1096d2 3097 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
dd180b3e 3098 }
2384d2b3
SY
3099}
3100
e8467fda
AK
3101static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3102{
3103 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3104
3105 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3106 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3107}
3108
aff48baa
AK
3109static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3110{
3111 if (enable_ept && is_paging(vcpu))
3112 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3113 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3114}
3115
25c4c276 3116static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
399badf3 3117{
fc78f519
AK
3118 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3119
3120 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3121 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
399badf3
AK
3122}
3123
1439442c
SY
3124static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3125{
6de4f3ad
AK
3126 if (!test_bit(VCPU_EXREG_PDPTR,
3127 (unsigned long *)&vcpu->arch.regs_dirty))
3128 return;
3129
1439442c 3130 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
ff03a073
JR
3131 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
3132 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
3133 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
3134 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
1439442c
SY
3135 }
3136}
3137
8f5d549f
AK
3138static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3139{
3140 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
ff03a073
JR
3141 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3142 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3143 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3144 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
8f5d549f 3145 }
6de4f3ad
AK
3146
3147 __set_bit(VCPU_EXREG_PDPTR,
3148 (unsigned long *)&vcpu->arch.regs_avail);
3149 __set_bit(VCPU_EXREG_PDPTR,
3150 (unsigned long *)&vcpu->arch.regs_dirty);
8f5d549f
AK
3151}
3152
5e1746d6 3153static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1439442c
SY
3154
3155static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3156 unsigned long cr0,
3157 struct kvm_vcpu *vcpu)
3158{
5233dd51
MT
3159 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3160 vmx_decache_cr3(vcpu);
1439442c
SY
3161 if (!(cr0 & X86_CR0_PG)) {
3162 /* From paging/starting to nonpaging */
3163 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
65267ea1 3164 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
1439442c
SY
3165 (CPU_BASED_CR3_LOAD_EXITING |
3166 CPU_BASED_CR3_STORE_EXITING));
3167 vcpu->arch.cr0 = cr0;
fc78f519 3168 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
1439442c
SY
3169 } else if (!is_paging(vcpu)) {
3170 /* From nonpaging to paging */
3171 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
65267ea1 3172 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
1439442c
SY
3173 ~(CPU_BASED_CR3_LOAD_EXITING |
3174 CPU_BASED_CR3_STORE_EXITING));
3175 vcpu->arch.cr0 = cr0;
fc78f519 3176 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
1439442c 3177 }
95eb84a7
SY
3178
3179 if (!(cr0 & X86_CR0_WP))
3180 *hw_cr0 &= ~X86_CR0_WP;
1439442c
SY
3181}
3182
6aa8b732
AK
3183static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3184{
7ffd92c5 3185 struct vcpu_vmx *vmx = to_vmx(vcpu);
3a624e29
NK
3186 unsigned long hw_cr0;
3187
5037878e 3188 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
3a624e29 3189 if (enable_unrestricted_guest)
5037878e 3190 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
218e763f 3191 else {
5037878e 3192 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
1439442c 3193
218e763f
GN
3194 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3195 enter_pmode(vcpu);
6aa8b732 3196
218e763f
GN
3197 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3198 enter_rmode(vcpu);
3199 }
6aa8b732 3200
05b3e0c2 3201#ifdef CONFIG_X86_64
f6801dff 3202 if (vcpu->arch.efer & EFER_LME) {
707d92fa 3203 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
6aa8b732 3204 enter_lmode(vcpu);
707d92fa 3205 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
6aa8b732
AK
3206 exit_lmode(vcpu);
3207 }
3208#endif
3209
089d034e 3210 if (enable_ept)
1439442c
SY
3211 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3212
02daab21 3213 if (!vcpu->fpu_active)
81231c69 3214 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
02daab21 3215
6aa8b732 3216 vmcs_writel(CR0_READ_SHADOW, cr0);
1439442c 3217 vmcs_writel(GUEST_CR0, hw_cr0);
ad312c7c 3218 vcpu->arch.cr0 = cr0;
14168786
GN
3219
3220 /* depends on vcpu->arch.cr0 to be set to a new value */
3221 vmx->emulation_required = emulation_required(vcpu);
6aa8b732
AK
3222}
3223
1439442c
SY
3224static u64 construct_eptp(unsigned long root_hpa)
3225{
3226 u64 eptp;
3227
3228 /* TODO write the value reading from MSR */
3229 eptp = VMX_EPT_DEFAULT_MT |
3230 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
b38f9934
XH
3231 if (enable_ept_ad_bits)
3232 eptp |= VMX_EPT_AD_ENABLE_BIT;
1439442c
SY
3233 eptp |= (root_hpa & PAGE_MASK);
3234
3235 return eptp;
3236}
3237
6aa8b732
AK
3238static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3239{
1439442c
SY
3240 unsigned long guest_cr3;
3241 u64 eptp;
3242
3243 guest_cr3 = cr3;
089d034e 3244 if (enable_ept) {
1439442c
SY
3245 eptp = construct_eptp(cr3);
3246 vmcs_write64(EPT_POINTER, eptp);
9f8fe504 3247 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
b927a3ce 3248 vcpu->kvm->arch.ept_identity_map_addr;
7c93be44 3249 ept_load_pdptrs(vcpu);
1439442c
SY
3250 }
3251
2384d2b3 3252 vmx_flush_tlb(vcpu);
1439442c 3253 vmcs_writel(GUEST_CR3, guest_cr3);
6aa8b732
AK
3254}
3255
5e1746d6 3256static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
6aa8b732 3257{
7ffd92c5 3258 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
1439442c
SY
3259 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3260
5e1746d6
NHE
3261 if (cr4 & X86_CR4_VMXE) {
3262 /*
3263 * To use VMXON (and later other VMX instructions), a guest
3264 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3265 * So basically the check on whether to allow nested VMX
3266 * is here.
3267 */
3268 if (!nested_vmx_allowed(vcpu))
3269 return 1;
1a0d74e6
JK
3270 }
3271 if (to_vmx(vcpu)->nested.vmxon &&
3272 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
5e1746d6
NHE
3273 return 1;
3274
ad312c7c 3275 vcpu->arch.cr4 = cr4;
bc23008b
AK
3276 if (enable_ept) {
3277 if (!is_paging(vcpu)) {
3278 hw_cr4 &= ~X86_CR4_PAE;
3279 hw_cr4 |= X86_CR4_PSE;
c08800a5
DX
3280 /*
3281 * SMEP is disabled if CPU is in non-paging mode in
3282 * hardware. However KVM always uses paging mode to
3283 * emulate guest non-paging mode with TDP.
3284 * To emulate this behavior, SMEP needs to be manually
3285 * disabled when guest switches to non-paging mode.
3286 */
3287 hw_cr4 &= ~X86_CR4_SMEP;
bc23008b
AK
3288 } else if (!(cr4 & X86_CR4_PAE)) {
3289 hw_cr4 &= ~X86_CR4_PAE;
3290 }
3291 }
1439442c
SY
3292
3293 vmcs_writel(CR4_READ_SHADOW, cr4);
3294 vmcs_writel(GUEST_CR4, hw_cr4);
5e1746d6 3295 return 0;
6aa8b732
AK
3296}
3297
6aa8b732
AK
3298static void vmx_get_segment(struct kvm_vcpu *vcpu,
3299 struct kvm_segment *var, int seg)
3300{
a9179499 3301 struct vcpu_vmx *vmx = to_vmx(vcpu);
6aa8b732
AK
3302 u32 ar;
3303
c6ad1153 3304 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
f5f7b2fe 3305 *var = vmx->rmode.segs[seg];
a9179499 3306 if (seg == VCPU_SREG_TR
2fb92db1 3307 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
f5f7b2fe 3308 return;
1390a28b
AK
3309 var->base = vmx_read_guest_seg_base(vmx, seg);
3310 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3311 return;
a9179499 3312 }
2fb92db1
AK
3313 var->base = vmx_read_guest_seg_base(vmx, seg);
3314 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3315 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3316 ar = vmx_read_guest_seg_ar(vmx, seg);
6aa8b732
AK
3317 var->type = ar & 15;
3318 var->s = (ar >> 4) & 1;
3319 var->dpl = (ar >> 5) & 3;
3320 var->present = (ar >> 7) & 1;
3321 var->avl = (ar >> 12) & 1;
3322 var->l = (ar >> 13) & 1;
3323 var->db = (ar >> 14) & 1;
3324 var->g = (ar >> 15) & 1;
3325 var->unusable = (ar >> 16) & 1;
3326}
3327
a9179499
AK
3328static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3329{
a9179499
AK
3330 struct kvm_segment s;
3331
3332 if (to_vmx(vcpu)->rmode.vm86_active) {
3333 vmx_get_segment(vcpu, &s, seg);
3334 return s.base;
3335 }
2fb92db1 3336 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
a9179499
AK
3337}
3338
b09408d0 3339static int vmx_get_cpl(struct kvm_vcpu *vcpu)
2e4d2653 3340{
b09408d0
MT
3341 struct vcpu_vmx *vmx = to_vmx(vcpu);
3342
3eeb3288 3343 if (!is_protmode(vcpu))
2e4d2653
IE
3344 return 0;
3345
f4c63e5d
AK
3346 if (!is_long_mode(vcpu)
3347 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
2e4d2653
IE
3348 return 3;
3349
69c73028
AK
3350 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
3351 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
b09408d0 3352 vmx->cpl = vmx_read_guest_seg_selector(vmx, VCPU_SREG_CS) & 3;
69c73028 3353 }
d881e6f6
AK
3354
3355 return vmx->cpl;
69c73028
AK
3356}
3357
3358
653e3108 3359static u32 vmx_segment_access_rights(struct kvm_segment *var)
6aa8b732 3360{
6aa8b732
AK
3361 u32 ar;
3362
f0495f9b 3363 if (var->unusable || !var->present)
6aa8b732
AK
3364 ar = 1 << 16;
3365 else {
3366 ar = var->type & 15;
3367 ar |= (var->s & 1) << 4;
3368 ar |= (var->dpl & 3) << 5;
3369 ar |= (var->present & 1) << 7;
3370 ar |= (var->avl & 1) << 12;
3371 ar |= (var->l & 1) << 13;
3372 ar |= (var->db & 1) << 14;
3373 ar |= (var->g & 1) << 15;
3374 }
653e3108
AK
3375
3376 return ar;
3377}
3378
3379static void vmx_set_segment(struct kvm_vcpu *vcpu,
3380 struct kvm_segment *var, int seg)
3381{
7ffd92c5 3382 struct vcpu_vmx *vmx = to_vmx(vcpu);
772e0318 3383 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
653e3108 3384
2fb92db1 3385 vmx_segment_cache_clear(vmx);
2f143240
GN
3386 if (seg == VCPU_SREG_CS)
3387 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2fb92db1 3388
1ecd50a9
GN
3389 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3390 vmx->rmode.segs[seg] = *var;
3391 if (seg == VCPU_SREG_TR)
3392 vmcs_write16(sf->selector, var->selector);
3393 else if (var->s)
3394 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
d99e4152 3395 goto out;
653e3108 3396 }
1ecd50a9 3397
653e3108
AK
3398 vmcs_writel(sf->base, var->base);
3399 vmcs_write32(sf->limit, var->limit);
3400 vmcs_write16(sf->selector, var->selector);
3a624e29
NK
3401
3402 /*
3403 * Fix the "Accessed" bit in AR field of segment registers for older
3404 * qemu binaries.
3405 * IA32 arch specifies that at the time of processor reset the
3406 * "Accessed" bit in the AR field of segment registers is 1. And qemu
0fa06071 3407 * is setting it to 0 in the userland code. This causes invalid guest
3a624e29
NK
3408 * state vmexit when "unrestricted guest" mode is turned on.
3409 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3410 * tree. Newer qemu binaries with that qemu fix would not need this
3411 * kvm hack.
3412 */
3413 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
f924d66d 3414 var->type |= 0x1; /* Accessed */
3a624e29 3415
f924d66d 3416 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
d99e4152
GN
3417
3418out:
14168786 3419 vmx->emulation_required |= emulation_required(vcpu);
6aa8b732
AK
3420}
3421
6aa8b732
AK
3422static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3423{
2fb92db1 3424 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
6aa8b732
AK
3425
3426 *db = (ar >> 14) & 1;
3427 *l = (ar >> 13) & 1;
3428}
3429
89a27f4d 3430static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 3431{
89a27f4d
GN
3432 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3433 dt->address = vmcs_readl(GUEST_IDTR_BASE);
6aa8b732
AK
3434}
3435
89a27f4d 3436static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 3437{
89a27f4d
GN
3438 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3439 vmcs_writel(GUEST_IDTR_BASE, dt->address);
6aa8b732
AK
3440}
3441
89a27f4d 3442static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 3443{
89a27f4d
GN
3444 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3445 dt->address = vmcs_readl(GUEST_GDTR_BASE);
6aa8b732
AK
3446}
3447
89a27f4d 3448static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
6aa8b732 3449{
89a27f4d
GN
3450 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3451 vmcs_writel(GUEST_GDTR_BASE, dt->address);
6aa8b732
AK
3452}
3453
648dfaa7
MG
3454static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3455{
3456 struct kvm_segment var;
3457 u32 ar;
3458
3459 vmx_get_segment(vcpu, &var, seg);
07f42f5f 3460 var.dpl = 0x3;
0647f4aa
GN
3461 if (seg == VCPU_SREG_CS)
3462 var.type = 0x3;
648dfaa7
MG
3463 ar = vmx_segment_access_rights(&var);
3464
3465 if (var.base != (var.selector << 4))
3466 return false;
89efbed0 3467 if (var.limit != 0xffff)
648dfaa7 3468 return false;
07f42f5f 3469 if (ar != 0xf3)
648dfaa7
MG
3470 return false;
3471
3472 return true;
3473}
3474
3475static bool code_segment_valid(struct kvm_vcpu *vcpu)
3476{
3477 struct kvm_segment cs;
3478 unsigned int cs_rpl;
3479
3480 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3481 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3482
1872a3f4
AK
3483 if (cs.unusable)
3484 return false;
648dfaa7
MG
3485 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3486 return false;
3487 if (!cs.s)
3488 return false;
1872a3f4 3489 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
648dfaa7
MG
3490 if (cs.dpl > cs_rpl)
3491 return false;
1872a3f4 3492 } else {
648dfaa7
MG
3493 if (cs.dpl != cs_rpl)
3494 return false;
3495 }
3496 if (!cs.present)
3497 return false;
3498
3499 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3500 return true;
3501}
3502
3503static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3504{
3505 struct kvm_segment ss;
3506 unsigned int ss_rpl;
3507
3508 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3509 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3510
1872a3f4
AK
3511 if (ss.unusable)
3512 return true;
3513 if (ss.type != 3 && ss.type != 7)
648dfaa7
MG
3514 return false;
3515 if (!ss.s)
3516 return false;
3517 if (ss.dpl != ss_rpl) /* DPL != RPL */
3518 return false;
3519 if (!ss.present)
3520 return false;
3521
3522 return true;
3523}
3524
3525static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3526{
3527 struct kvm_segment var;
3528 unsigned int rpl;
3529
3530 vmx_get_segment(vcpu, &var, seg);
3531 rpl = var.selector & SELECTOR_RPL_MASK;
3532
1872a3f4
AK
3533 if (var.unusable)
3534 return true;
648dfaa7
MG
3535 if (!var.s)
3536 return false;
3537 if (!var.present)
3538 return false;
3539 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3540 if (var.dpl < rpl) /* DPL < RPL */
3541 return false;
3542 }
3543
3544 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3545 * rights flags
3546 */
3547 return true;
3548}
3549
3550static bool tr_valid(struct kvm_vcpu *vcpu)
3551{
3552 struct kvm_segment tr;
3553
3554 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3555
1872a3f4
AK
3556 if (tr.unusable)
3557 return false;
648dfaa7
MG
3558 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3559 return false;
1872a3f4 3560 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
648dfaa7
MG
3561 return false;
3562 if (!tr.present)
3563 return false;
3564
3565 return true;
3566}
3567
3568static bool ldtr_valid(struct kvm_vcpu *vcpu)
3569{
3570 struct kvm_segment ldtr;
3571
3572 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3573
1872a3f4
AK
3574 if (ldtr.unusable)
3575 return true;
648dfaa7
MG
3576 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3577 return false;
3578 if (ldtr.type != 2)
3579 return false;
3580 if (!ldtr.present)
3581 return false;
3582
3583 return true;
3584}
3585
3586static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3587{
3588 struct kvm_segment cs, ss;
3589
3590 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3591 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3592
3593 return ((cs.selector & SELECTOR_RPL_MASK) ==
3594 (ss.selector & SELECTOR_RPL_MASK));
3595}
3596
3597/*
3598 * Check if guest state is valid. Returns true if valid, false if
3599 * not.
3600 * We assume that registers are always usable
3601 */
3602static bool guest_state_valid(struct kvm_vcpu *vcpu)
3603{
c5e97c80
GN
3604 if (enable_unrestricted_guest)
3605 return true;
3606
648dfaa7 3607 /* real mode guest state checks */
f13882d8 3608 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
648dfaa7
MG
3609 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3610 return false;
3611 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3612 return false;
3613 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3614 return false;
3615 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3616 return false;
3617 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3618 return false;
3619 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3620 return false;
3621 } else {
3622 /* protected mode guest state checks */
3623 if (!cs_ss_rpl_check(vcpu))
3624 return false;
3625 if (!code_segment_valid(vcpu))
3626 return false;
3627 if (!stack_segment_valid(vcpu))
3628 return false;
3629 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3630 return false;
3631 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3632 return false;
3633 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3634 return false;
3635 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3636 return false;
3637 if (!tr_valid(vcpu))
3638 return false;
3639 if (!ldtr_valid(vcpu))
3640 return false;
3641 }
3642 /* TODO:
3643 * - Add checks on RIP
3644 * - Add checks on RFLAGS
3645 */
3646
3647 return true;
3648}
3649
d77c26fc 3650static int init_rmode_tss(struct kvm *kvm)
6aa8b732 3651{
40dcaa9f 3652 gfn_t fn;
195aefde 3653 u16 data = 0;
40dcaa9f 3654 int r, idx, ret = 0;
6aa8b732 3655
40dcaa9f 3656 idx = srcu_read_lock(&kvm->srcu);
4918c6ca 3657 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
195aefde
IE
3658 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3659 if (r < 0)
10589a46 3660 goto out;
195aefde 3661 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
464d17c8
SY
3662 r = kvm_write_guest_page(kvm, fn++, &data,
3663 TSS_IOPB_BASE_OFFSET, sizeof(u16));
195aefde 3664 if (r < 0)
10589a46 3665 goto out;
195aefde
IE
3666 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3667 if (r < 0)
10589a46 3668 goto out;
195aefde
IE
3669 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3670 if (r < 0)
10589a46 3671 goto out;
195aefde 3672 data = ~0;
10589a46
MT
3673 r = kvm_write_guest_page(kvm, fn, &data,
3674 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3675 sizeof(u8));
195aefde 3676 if (r < 0)
10589a46
MT
3677 goto out;
3678
3679 ret = 1;
3680out:
40dcaa9f 3681 srcu_read_unlock(&kvm->srcu, idx);
10589a46 3682 return ret;
6aa8b732
AK
3683}
3684
b7ebfb05
SY
3685static int init_rmode_identity_map(struct kvm *kvm)
3686{
40dcaa9f 3687 int i, idx, r, ret;
b7ebfb05
SY
3688 pfn_t identity_map_pfn;
3689 u32 tmp;
3690
089d034e 3691 if (!enable_ept)
b7ebfb05
SY
3692 return 1;
3693 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3694 printk(KERN_ERR "EPT: identity-mapping pagetable "
3695 "haven't been allocated!\n");
3696 return 0;
3697 }
3698 if (likely(kvm->arch.ept_identity_pagetable_done))
3699 return 1;
3700 ret = 0;
b927a3ce 3701 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
40dcaa9f 3702 idx = srcu_read_lock(&kvm->srcu);
b7ebfb05
SY
3703 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3704 if (r < 0)
3705 goto out;
3706 /* Set up identity-mapping pagetable for EPT in real mode */
3707 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3708 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3709 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3710 r = kvm_write_guest_page(kvm, identity_map_pfn,
3711 &tmp, i * sizeof(tmp), sizeof(tmp));
3712 if (r < 0)
3713 goto out;
3714 }
3715 kvm->arch.ept_identity_pagetable_done = true;
3716 ret = 1;
3717out:
40dcaa9f 3718 srcu_read_unlock(&kvm->srcu, idx);
b7ebfb05
SY
3719 return ret;
3720}
3721
6aa8b732
AK
3722static void seg_setup(int seg)
3723{
772e0318 3724 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3a624e29 3725 unsigned int ar;
6aa8b732
AK
3726
3727 vmcs_write16(sf->selector, 0);
3728 vmcs_writel(sf->base, 0);
3729 vmcs_write32(sf->limit, 0xffff);
d54d07b2
GN
3730 ar = 0x93;
3731 if (seg == VCPU_SREG_CS)
3732 ar |= 0x08; /* code segment */
3a624e29
NK
3733
3734 vmcs_write32(sf->ar_bytes, ar);
6aa8b732
AK
3735}
3736
f78e0e2e
SY
3737static int alloc_apic_access_page(struct kvm *kvm)
3738{
4484141a 3739 struct page *page;
f78e0e2e
SY
3740 struct kvm_userspace_memory_region kvm_userspace_mem;
3741 int r = 0;
3742
79fac95e 3743 mutex_lock(&kvm->slots_lock);
bfc6d222 3744 if (kvm->arch.apic_access_page)
f78e0e2e
SY
3745 goto out;
3746 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3747 kvm_userspace_mem.flags = 0;
3748 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3749 kvm_userspace_mem.memory_size = PAGE_SIZE;
47ae31e2 3750 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
f78e0e2e
SY
3751 if (r)
3752 goto out;
72dc67a6 3753
4484141a
XG
3754 page = gfn_to_page(kvm, 0xfee00);
3755 if (is_error_page(page)) {
3756 r = -EFAULT;
3757 goto out;
3758 }
3759
3760 kvm->arch.apic_access_page = page;
f78e0e2e 3761out:
79fac95e 3762 mutex_unlock(&kvm->slots_lock);
f78e0e2e
SY
3763 return r;
3764}
3765
b7ebfb05
SY
3766static int alloc_identity_pagetable(struct kvm *kvm)
3767{
4484141a 3768 struct page *page;
b7ebfb05
SY
3769 struct kvm_userspace_memory_region kvm_userspace_mem;
3770 int r = 0;
3771
79fac95e 3772 mutex_lock(&kvm->slots_lock);
b7ebfb05
SY
3773 if (kvm->arch.ept_identity_pagetable)
3774 goto out;
3775 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3776 kvm_userspace_mem.flags = 0;
b927a3ce
SY
3777 kvm_userspace_mem.guest_phys_addr =
3778 kvm->arch.ept_identity_map_addr;
b7ebfb05 3779 kvm_userspace_mem.memory_size = PAGE_SIZE;
47ae31e2 3780 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
b7ebfb05
SY
3781 if (r)
3782 goto out;
3783
4484141a
XG
3784 page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
3785 if (is_error_page(page)) {
3786 r = -EFAULT;
3787 goto out;
3788 }
3789
3790 kvm->arch.ept_identity_pagetable = page;
b7ebfb05 3791out:
79fac95e 3792 mutex_unlock(&kvm->slots_lock);
b7ebfb05
SY
3793 return r;
3794}
3795
2384d2b3
SY
3796static void allocate_vpid(struct vcpu_vmx *vmx)
3797{
3798 int vpid;
3799
3800 vmx->vpid = 0;
919818ab 3801 if (!enable_vpid)
2384d2b3
SY
3802 return;
3803 spin_lock(&vmx_vpid_lock);
3804 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
3805 if (vpid < VMX_NR_VPIDS) {
3806 vmx->vpid = vpid;
3807 __set_bit(vpid, vmx_vpid_bitmap);
3808 }
3809 spin_unlock(&vmx_vpid_lock);
3810}
3811
cdbecfc3
LJ
3812static void free_vpid(struct vcpu_vmx *vmx)
3813{
3814 if (!enable_vpid)
3815 return;
3816 spin_lock(&vmx_vpid_lock);
3817 if (vmx->vpid != 0)
3818 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3819 spin_unlock(&vmx_vpid_lock);
3820}
3821
8d14695f
YZ
3822#define MSR_TYPE_R 1
3823#define MSR_TYPE_W 2
3824static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
3825 u32 msr, int type)
25c5f225 3826{
3e7c73e9 3827 int f = sizeof(unsigned long);
25c5f225
SY
3828
3829 if (!cpu_has_vmx_msr_bitmap())
3830 return;
3831
3832 /*
3833 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3834 * have the write-low and read-high bitmap offsets the wrong way round.
3835 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3836 */
25c5f225 3837 if (msr <= 0x1fff) {
8d14695f
YZ
3838 if (type & MSR_TYPE_R)
3839 /* read-low */
3840 __clear_bit(msr, msr_bitmap + 0x000 / f);
3841
3842 if (type & MSR_TYPE_W)
3843 /* write-low */
3844 __clear_bit(msr, msr_bitmap + 0x800 / f);
3845
25c5f225
SY
3846 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3847 msr &= 0x1fff;
8d14695f
YZ
3848 if (type & MSR_TYPE_R)
3849 /* read-high */
3850 __clear_bit(msr, msr_bitmap + 0x400 / f);
3851
3852 if (type & MSR_TYPE_W)
3853 /* write-high */
3854 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3855
3856 }
3857}
3858
3859static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
3860 u32 msr, int type)
3861{
3862 int f = sizeof(unsigned long);
3863
3864 if (!cpu_has_vmx_msr_bitmap())
3865 return;
3866
3867 /*
3868 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3869 * have the write-low and read-high bitmap offsets the wrong way round.
3870 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3871 */
3872 if (msr <= 0x1fff) {
3873 if (type & MSR_TYPE_R)
3874 /* read-low */
3875 __set_bit(msr, msr_bitmap + 0x000 / f);
3876
3877 if (type & MSR_TYPE_W)
3878 /* write-low */
3879 __set_bit(msr, msr_bitmap + 0x800 / f);
3880
3881 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3882 msr &= 0x1fff;
3883 if (type & MSR_TYPE_R)
3884 /* read-high */
3885 __set_bit(msr, msr_bitmap + 0x400 / f);
3886
3887 if (type & MSR_TYPE_W)
3888 /* write-high */
3889 __set_bit(msr, msr_bitmap + 0xc00 / f);
3890
25c5f225 3891 }
25c5f225
SY
3892}
3893
5897297b
AK
3894static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
3895{
3896 if (!longmode_only)
8d14695f
YZ
3897 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
3898 msr, MSR_TYPE_R | MSR_TYPE_W);
3899 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
3900 msr, MSR_TYPE_R | MSR_TYPE_W);
3901}
3902
3903static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
3904{
3905 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3906 msr, MSR_TYPE_R);
3907 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3908 msr, MSR_TYPE_R);
3909}
3910
3911static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
3912{
3913 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3914 msr, MSR_TYPE_R);
3915 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3916 msr, MSR_TYPE_R);
3917}
3918
3919static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
3920{
3921 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3922 msr, MSR_TYPE_W);
3923 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3924 msr, MSR_TYPE_W);
5897297b
AK
3925}
3926
01e439be
YZ
3927static int vmx_vm_has_apicv(struct kvm *kvm)
3928{
3929 return enable_apicv && irqchip_in_kernel(kvm);
3930}
3931
a20ed54d
YZ
3932/*
3933 * Send interrupt to vcpu via posted interrupt way.
3934 * 1. If target vcpu is running(non-root mode), send posted interrupt
3935 * notification to vcpu and hardware will sync PIR to vIRR atomically.
3936 * 2. If target vcpu isn't running(root mode), kick it to pick up the
3937 * interrupt from PIR in next vmentry.
3938 */
3939static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
3940{
3941 struct vcpu_vmx *vmx = to_vmx(vcpu);
3942 int r;
3943
3944 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
3945 return;
3946
3947 r = pi_test_and_set_on(&vmx->pi_desc);
3948 kvm_make_request(KVM_REQ_EVENT, vcpu);
6ffbbbba 3949#ifdef CONFIG_SMP
a20ed54d
YZ
3950 if (!r && (vcpu->mode == IN_GUEST_MODE))
3951 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
3952 POSTED_INTR_VECTOR);
3953 else
6ffbbbba 3954#endif
a20ed54d
YZ
3955 kvm_vcpu_kick(vcpu);
3956}
3957
3958static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
3959{
3960 struct vcpu_vmx *vmx = to_vmx(vcpu);
3961
3962 if (!pi_test_and_clear_on(&vmx->pi_desc))
3963 return;
3964
3965 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
3966}
3967
3968static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
3969{
3970 return;
3971}
3972
a3a8ff8e
NHE
3973/*
3974 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3975 * will not change in the lifetime of the guest.
3976 * Note that host-state that does change is set elsewhere. E.g., host-state
3977 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3978 */
a547c6db 3979static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
a3a8ff8e
NHE
3980{
3981 u32 low32, high32;
3982 unsigned long tmpl;
3983 struct desc_ptr dt;
3984
b1a74bf8 3985 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
a3a8ff8e
NHE
3986 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
3987 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
3988
3989 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
b2da15ac
AK
3990#ifdef CONFIG_X86_64
3991 /*
3992 * Load null selectors, so we can avoid reloading them in
3993 * __vmx_load_host_state(), in case userspace uses the null selectors
3994 * too (the expected case).
3995 */
3996 vmcs_write16(HOST_DS_SELECTOR, 0);
3997 vmcs_write16(HOST_ES_SELECTOR, 0);
3998#else
a3a8ff8e
NHE
3999 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4000 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
b2da15ac 4001#endif
a3a8ff8e
NHE
4002 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4003 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4004
4005 native_store_idt(&dt);
4006 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
a547c6db 4007 vmx->host_idt_base = dt.address;
a3a8ff8e 4008
83287ea4 4009 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
a3a8ff8e
NHE
4010
4011 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4012 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4013 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4014 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4015
4016 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4017 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4018 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4019 }
4020}
4021
bf8179a0
NHE
4022static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4023{
4024 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4025 if (enable_ept)
4026 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
fe3ef05c
NHE
4027 if (is_guest_mode(&vmx->vcpu))
4028 vmx->vcpu.arch.cr4_guest_owned_bits &=
4029 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
bf8179a0
NHE
4030 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4031}
4032
01e439be
YZ
4033static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4034{
4035 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4036
4037 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4038 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4039 return pin_based_exec_ctrl;
4040}
4041
bf8179a0
NHE
4042static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4043{
4044 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4045 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
4046 exec_control &= ~CPU_BASED_TPR_SHADOW;
4047#ifdef CONFIG_X86_64
4048 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4049 CPU_BASED_CR8_LOAD_EXITING;
4050#endif
4051 }
4052 if (!enable_ept)
4053 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4054 CPU_BASED_CR3_LOAD_EXITING |
4055 CPU_BASED_INVLPG_EXITING;
4056 return exec_control;
4057}
4058
4059static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4060{
4061 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4062 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4063 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4064 if (vmx->vpid == 0)
4065 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4066 if (!enable_ept) {
4067 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4068 enable_unrestricted_guest = 0;
ad756a16
MJ
4069 /* Enable INVPCID for non-ept guests may cause performance regression. */
4070 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
bf8179a0
NHE
4071 }
4072 if (!enable_unrestricted_guest)
4073 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4074 if (!ple_gap)
4075 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
c7c9c56c
YZ
4076 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4077 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4078 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
8d14695f 4079 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
bf8179a0
NHE
4080 return exec_control;
4081}
4082
ce88decf
XG
4083static void ept_set_mmio_spte_mask(void)
4084{
4085 /*
4086 * EPT Misconfigurations can be generated if the value of bits 2:0
4087 * of an EPT paging-structure entry is 110b (write/execute).
4088 * Also, magic bits (0xffull << 49) is set to quickly identify mmio
4089 * spte.
4090 */
4091 kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull);
4092}
4093
6aa8b732
AK
4094/*
4095 * Sets up the vmcs for emulated real mode.
4096 */
8b9cf98c 4097static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
6aa8b732 4098{
2e4ce7f5 4099#ifdef CONFIG_X86_64
6aa8b732 4100 unsigned long a;
2e4ce7f5 4101#endif
6aa8b732 4102 int i;
6aa8b732 4103
6aa8b732 4104 /* I/O */
3e7c73e9
AK
4105 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4106 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
6aa8b732 4107
25c5f225 4108 if (cpu_has_vmx_msr_bitmap())
5897297b 4109 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
25c5f225 4110
6aa8b732
AK
4111 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4112
6aa8b732 4113 /* Control */
01e439be 4114 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
6e5d865c 4115
bf8179a0 4116 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
6aa8b732 4117
83ff3b9d 4118 if (cpu_has_secondary_exec_ctrls()) {
bf8179a0
NHE
4119 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4120 vmx_secondary_exec_control(vmx));
83ff3b9d 4121 }
f78e0e2e 4122
01e439be 4123 if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
c7c9c56c
YZ
4124 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4125 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4126 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4127 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4128
4129 vmcs_write16(GUEST_INTR_STATUS, 0);
01e439be
YZ
4130
4131 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4132 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
c7c9c56c
YZ
4133 }
4134
4b8d54f9
ZE
4135 if (ple_gap) {
4136 vmcs_write32(PLE_GAP, ple_gap);
4137 vmcs_write32(PLE_WINDOW, ple_window);
4138 }
4139
c3707958
XG
4140 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4141 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
6aa8b732
AK
4142 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4143
9581d442
AK
4144 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4145 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
a547c6db 4146 vmx_set_constant_host_state(vmx);
05b3e0c2 4147#ifdef CONFIG_X86_64
6aa8b732
AK
4148 rdmsrl(MSR_FS_BASE, a);
4149 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4150 rdmsrl(MSR_GS_BASE, a);
4151 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4152#else
4153 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4154 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4155#endif
4156
2cc51560
ED
4157 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4158 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
61d2ef2c 4159 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
2cc51560 4160 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
61d2ef2c 4161 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
6aa8b732 4162
468d472f 4163 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
a3a8ff8e
NHE
4164 u32 msr_low, msr_high;
4165 u64 host_pat;
468d472f
SY
4166 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
4167 host_pat = msr_low | ((u64) msr_high << 32);
4168 /* Write the default value follow host pat */
4169 vmcs_write64(GUEST_IA32_PAT, host_pat);
4170 /* Keep arch.pat sync with GUEST_IA32_PAT */
4171 vmx->vcpu.arch.pat = host_pat;
4172 }
4173
6aa8b732
AK
4174 for (i = 0; i < NR_VMX_MSR; ++i) {
4175 u32 index = vmx_msr_index[i];
4176 u32 data_low, data_high;
a2fa3e9f 4177 int j = vmx->nmsrs;
6aa8b732
AK
4178
4179 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4180 continue;
432bd6cb
AK
4181 if (wrmsr_safe(index, data_low, data_high) < 0)
4182 continue;
26bb0981
AK
4183 vmx->guest_msrs[j].index = i;
4184 vmx->guest_msrs[j].data = 0;
d5696725 4185 vmx->guest_msrs[j].mask = -1ull;
a2fa3e9f 4186 ++vmx->nmsrs;
6aa8b732 4187 }
6aa8b732 4188
1c3d14fe 4189 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
6aa8b732
AK
4190
4191 /* 22.2.1, 20.8.1 */
1c3d14fe
YS
4192 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
4193
e00c8cf2 4194 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
bf8179a0 4195 set_cr4_guest_host_mask(vmx);
e00c8cf2
AK
4196
4197 return 0;
4198}
4199
57f252f2 4200static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
e00c8cf2
AK
4201{
4202 struct vcpu_vmx *vmx = to_vmx(vcpu);
4203 u64 msr;
e00c8cf2 4204
7ffd92c5 4205 vmx->rmode.vm86_active = 0;
e00c8cf2 4206
3b86cd99
JK
4207 vmx->soft_vnmi_blocked = 0;
4208
ad312c7c 4209 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
2d3ad1f4 4210 kvm_set_cr8(&vmx->vcpu, 0);
e00c8cf2 4211 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
c5af89b6 4212 if (kvm_vcpu_is_bsp(&vmx->vcpu))
e00c8cf2
AK
4213 msr |= MSR_IA32_APICBASE_BSP;
4214 kvm_set_apic_base(&vmx->vcpu, msr);
4215
2fb92db1
AK
4216 vmx_segment_cache_clear(vmx);
4217
5706be0d 4218 seg_setup(VCPU_SREG_CS);
66450a21 4219 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
04b66839 4220 vmcs_write32(GUEST_CS_BASE, 0xffff0000);
e00c8cf2
AK
4221
4222 seg_setup(VCPU_SREG_DS);
4223 seg_setup(VCPU_SREG_ES);
4224 seg_setup(VCPU_SREG_FS);
4225 seg_setup(VCPU_SREG_GS);
4226 seg_setup(VCPU_SREG_SS);
4227
4228 vmcs_write16(GUEST_TR_SELECTOR, 0);
4229 vmcs_writel(GUEST_TR_BASE, 0);
4230 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4231 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4232
4233 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4234 vmcs_writel(GUEST_LDTR_BASE, 0);
4235 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4236 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4237
4238 vmcs_write32(GUEST_SYSENTER_CS, 0);
4239 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4240 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4241
4242 vmcs_writel(GUEST_RFLAGS, 0x02);
66450a21 4243 kvm_rip_write(vcpu, 0xfff0);
e00c8cf2 4244
e00c8cf2
AK
4245 vmcs_writel(GUEST_GDTR_BASE, 0);
4246 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4247
4248 vmcs_writel(GUEST_IDTR_BASE, 0);
4249 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4250
443381a8 4251 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
e00c8cf2
AK
4252 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4253 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4254
e00c8cf2
AK
4255 /* Special registers */
4256 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4257
4258 setup_msrs(vmx);
4259
6aa8b732
AK
4260 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4261
f78e0e2e
SY
4262 if (cpu_has_vmx_tpr_shadow()) {
4263 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4264 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4265 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
afc20184 4266 __pa(vmx->vcpu.arch.apic->regs));
f78e0e2e
SY
4267 vmcs_write32(TPR_THRESHOLD, 0);
4268 }
4269
4270 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4271 vmcs_write64(APIC_ACCESS_ADDR,
bfc6d222 4272 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
6aa8b732 4273
01e439be
YZ
4274 if (vmx_vm_has_apicv(vcpu->kvm))
4275 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
4276
2384d2b3
SY
4277 if (vmx->vpid != 0)
4278 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4279
fa40052c 4280 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
4d4ec087 4281 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
8b9cf98c 4282 vmx_set_cr4(&vmx->vcpu, 0);
8b9cf98c 4283 vmx_set_efer(&vmx->vcpu, 0);
8b9cf98c
RR
4284 vmx_fpu_activate(&vmx->vcpu);
4285 update_exception_bitmap(&vmx->vcpu);
6aa8b732 4286
b9d762fa 4287 vpid_sync_context(vmx);
6aa8b732
AK
4288}
4289
b6f1250e
NHE
4290/*
4291 * In nested virtualization, check if L1 asked to exit on external interrupts.
4292 * For most existing hypervisors, this will always return true.
4293 */
4294static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4295{
4296 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4297 PIN_BASED_EXT_INTR_MASK;
4298}
4299
3b86cd99
JK
4300static void enable_irq_window(struct kvm_vcpu *vcpu)
4301{
4302 u32 cpu_based_vm_exec_control;
d6185f20
NHE
4303 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
4304 /*
4305 * We get here if vmx_interrupt_allowed() said we can't
4306 * inject to L1 now because L2 must run. Ask L2 to exit
4307 * right after entry, so we can inject to L1 more promptly.
b6f1250e 4308 */
d6185f20 4309 kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
b6f1250e 4310 return;
d6185f20 4311 }
3b86cd99
JK
4312
4313 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4314 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4315 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4316}
4317
4318static void enable_nmi_window(struct kvm_vcpu *vcpu)
4319{
4320 u32 cpu_based_vm_exec_control;
4321
4322 if (!cpu_has_virtual_nmis()) {
4323 enable_irq_window(vcpu);
4324 return;
4325 }
4326
30bd0c4c
AK
4327 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4328 enable_irq_window(vcpu);
4329 return;
4330 }
3b86cd99
JK
4331 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4332 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4333 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4334}
4335
66fd3f7f 4336static void vmx_inject_irq(struct kvm_vcpu *vcpu)
85f455f7 4337{
9c8cba37 4338 struct vcpu_vmx *vmx = to_vmx(vcpu);
66fd3f7f
GN
4339 uint32_t intr;
4340 int irq = vcpu->arch.interrupt.nr;
9c8cba37 4341
229456fc 4342 trace_kvm_inj_virq(irq);
2714d1d3 4343
fa89a817 4344 ++vcpu->stat.irq_injections;
7ffd92c5 4345 if (vmx->rmode.vm86_active) {
71f9833b
SH
4346 int inc_eip = 0;
4347 if (vcpu->arch.interrupt.soft)
4348 inc_eip = vcpu->arch.event_exit_inst_len;
4349 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
a92601bb 4350 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
85f455f7
ED
4351 return;
4352 }
66fd3f7f
GN
4353 intr = irq | INTR_INFO_VALID_MASK;
4354 if (vcpu->arch.interrupt.soft) {
4355 intr |= INTR_TYPE_SOFT_INTR;
4356 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4357 vmx->vcpu.arch.event_exit_inst_len);
4358 } else
4359 intr |= INTR_TYPE_EXT_INTR;
4360 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
85f455f7
ED
4361}
4362
f08864b4
SY
4363static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4364{
66a5a347
JK
4365 struct vcpu_vmx *vmx = to_vmx(vcpu);
4366
0b6ac343
NHE
4367 if (is_guest_mode(vcpu))
4368 return;
4369
3b86cd99
JK
4370 if (!cpu_has_virtual_nmis()) {
4371 /*
4372 * Tracking the NMI-blocked state in software is built upon
4373 * finding the next open IRQ window. This, in turn, depends on
4374 * well-behaving guests: They have to keep IRQs disabled at
4375 * least as long as the NMI handler runs. Otherwise we may
4376 * cause NMI nesting, maybe breaking the guest. But as this is
4377 * highly unlikely, we can live with the residual risk.
4378 */
4379 vmx->soft_vnmi_blocked = 1;
4380 vmx->vnmi_blocked_time = 0;
4381 }
4382
487b391d 4383 ++vcpu->stat.nmi_injections;
9d58b931 4384 vmx->nmi_known_unmasked = false;
7ffd92c5 4385 if (vmx->rmode.vm86_active) {
71f9833b 4386 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
a92601bb 4387 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
66a5a347
JK
4388 return;
4389 }
f08864b4
SY
4390 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4391 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
f08864b4
SY
4392}
4393
c4282df9 4394static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
33f089ca 4395{
3b86cd99 4396 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
c4282df9 4397 return 0;
33f089ca 4398
c4282df9 4399 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
30bd0c4c
AK
4400 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4401 | GUEST_INTR_STATE_NMI));
33f089ca
JK
4402}
4403
3cfc3092
JK
4404static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4405{
4406 if (!cpu_has_virtual_nmis())
4407 return to_vmx(vcpu)->soft_vnmi_blocked;
9d58b931
AK
4408 if (to_vmx(vcpu)->nmi_known_unmasked)
4409 return false;
c332c83a 4410 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
3cfc3092
JK
4411}
4412
4413static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4414{
4415 struct vcpu_vmx *vmx = to_vmx(vcpu);
4416
4417 if (!cpu_has_virtual_nmis()) {
4418 if (vmx->soft_vnmi_blocked != masked) {
4419 vmx->soft_vnmi_blocked = masked;
4420 vmx->vnmi_blocked_time = 0;
4421 }
4422 } else {
9d58b931 4423 vmx->nmi_known_unmasked = !masked;
3cfc3092
JK
4424 if (masked)
4425 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4426 GUEST_INTR_STATE_NMI);
4427 else
4428 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4429 GUEST_INTR_STATE_NMI);
4430 }
4431}
4432
78646121
GN
4433static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4434{
e8457c67 4435 if (is_guest_mode(vcpu)) {
51cfe38e 4436 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
e8457c67
JK
4437
4438 if (to_vmx(vcpu)->nested.nested_run_pending)
b6f1250e 4439 return 0;
e8457c67
JK
4440 if (nested_exit_on_intr(vcpu)) {
4441 nested_vmx_vmexit(vcpu);
4442 vmcs12->vm_exit_reason =
4443 EXIT_REASON_EXTERNAL_INTERRUPT;
4444 vmcs12->vm_exit_intr_info = 0;
4445 /*
4446 * fall through to normal code, but now in L1, not L2
4447 */
4448 }
b6f1250e
NHE
4449 }
4450
c4282df9
GN
4451 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
4452 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4453 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
78646121
GN
4454}
4455
cbc94022
IE
4456static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4457{
4458 int ret;
4459 struct kvm_userspace_memory_region tss_mem = {
6fe63979 4460 .slot = TSS_PRIVATE_MEMSLOT,
cbc94022
IE
4461 .guest_phys_addr = addr,
4462 .memory_size = PAGE_SIZE * 3,
4463 .flags = 0,
4464 };
4465
47ae31e2 4466 ret = kvm_set_memory_region(kvm, &tss_mem);
cbc94022
IE
4467 if (ret)
4468 return ret;
bfc6d222 4469 kvm->arch.tss_addr = addr;
93ea5388
GN
4470 if (!init_rmode_tss(kvm))
4471 return -ENOMEM;
4472
cbc94022
IE
4473 return 0;
4474}
4475
0ca1b4f4 4476static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
6aa8b732 4477{
77ab6db0 4478 switch (vec) {
77ab6db0 4479 case BP_VECTOR:
c573cd22
JK
4480 /*
4481 * Update instruction length as we may reinject the exception
4482 * from user space while in guest debugging mode.
4483 */
4484 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4485 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
d0bfb940 4486 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
0ca1b4f4
GN
4487 return false;
4488 /* fall through */
4489 case DB_VECTOR:
4490 if (vcpu->guest_debug &
4491 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4492 return false;
d0bfb940
JK
4493 /* fall through */
4494 case DE_VECTOR:
77ab6db0
JK
4495 case OF_VECTOR:
4496 case BR_VECTOR:
4497 case UD_VECTOR:
4498 case DF_VECTOR:
4499 case SS_VECTOR:
4500 case GP_VECTOR:
4501 case MF_VECTOR:
0ca1b4f4
GN
4502 return true;
4503 break;
77ab6db0 4504 }
0ca1b4f4
GN
4505 return false;
4506}
4507
4508static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4509 int vec, u32 err_code)
4510{
4511 /*
4512 * Instruction with address size override prefix opcode 0x67
4513 * Cause the #SS fault with 0 error code in VM86 mode.
4514 */
4515 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4516 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4517 if (vcpu->arch.halt_request) {
4518 vcpu->arch.halt_request = 0;
4519 return kvm_emulate_halt(vcpu);
4520 }
4521 return 1;
4522 }
4523 return 0;
4524 }
4525
4526 /*
4527 * Forward all other exceptions that are valid in real mode.
4528 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4529 * the required debugging infrastructure rework.
4530 */
4531 kvm_queue_exception(vcpu, vec);
4532 return 1;
6aa8b732
AK
4533}
4534
a0861c02
AK
4535/*
4536 * Trigger machine check on the host. We assume all the MSRs are already set up
4537 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4538 * We pass a fake environment to the machine check handler because we want
4539 * the guest to be always treated like user space, no matter what context
4540 * it used internally.
4541 */
4542static void kvm_machine_check(void)
4543{
4544#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4545 struct pt_regs regs = {
4546 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4547 .flags = X86_EFLAGS_IF,
4548 };
4549
4550 do_machine_check(&regs, 0);
4551#endif
4552}
4553
851ba692 4554static int handle_machine_check(struct kvm_vcpu *vcpu)
a0861c02
AK
4555{
4556 /* already handled by vcpu_run */
4557 return 1;
4558}
4559
851ba692 4560static int handle_exception(struct kvm_vcpu *vcpu)
6aa8b732 4561{
1155f76a 4562 struct vcpu_vmx *vmx = to_vmx(vcpu);
851ba692 4563 struct kvm_run *kvm_run = vcpu->run;
d0bfb940 4564 u32 intr_info, ex_no, error_code;
42dbaa5a 4565 unsigned long cr2, rip, dr6;
6aa8b732
AK
4566 u32 vect_info;
4567 enum emulation_result er;
4568
1155f76a 4569 vect_info = vmx->idt_vectoring_info;
88786475 4570 intr_info = vmx->exit_intr_info;
6aa8b732 4571
a0861c02 4572 if (is_machine_check(intr_info))
851ba692 4573 return handle_machine_check(vcpu);
a0861c02 4574
e4a41889 4575 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
1b6269db 4576 return 1; /* already handled by vmx_vcpu_run() */
2ab455cc
AL
4577
4578 if (is_no_device(intr_info)) {
5fd86fcf 4579 vmx_fpu_activate(vcpu);
2ab455cc
AL
4580 return 1;
4581 }
4582
7aa81cc0 4583 if (is_invalid_opcode(intr_info)) {
51d8b661 4584 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
7aa81cc0 4585 if (er != EMULATE_DONE)
7ee5d940 4586 kvm_queue_exception(vcpu, UD_VECTOR);
7aa81cc0
AL
4587 return 1;
4588 }
4589
6aa8b732 4590 error_code = 0;
2e11384c 4591 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
6aa8b732 4592 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
bf4ca23e
XG
4593
4594 /*
4595 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4596 * MMIO, it is better to report an internal error.
4597 * See the comments in vmx_handle_exit.
4598 */
4599 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4600 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4601 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4602 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4603 vcpu->run->internal.ndata = 2;
4604 vcpu->run->internal.data[0] = vect_info;
4605 vcpu->run->internal.data[1] = intr_info;
4606 return 0;
4607 }
4608
6aa8b732 4609 if (is_page_fault(intr_info)) {
1439442c 4610 /* EPT won't cause page fault directly */
cf3ace79 4611 BUG_ON(enable_ept);
6aa8b732 4612 cr2 = vmcs_readl(EXIT_QUALIFICATION);
229456fc
MT
4613 trace_kvm_page_fault(cr2, error_code);
4614
3298b75c 4615 if (kvm_event_needs_reinjection(vcpu))
577bdc49 4616 kvm_mmu_unprotect_page_virt(vcpu, cr2);
dc25e89e 4617 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
6aa8b732
AK
4618 }
4619
d0bfb940 4620 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
0ca1b4f4
GN
4621
4622 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4623 return handle_rmode_exception(vcpu, ex_no, error_code);
4624
42dbaa5a
JK
4625 switch (ex_no) {
4626 case DB_VECTOR:
4627 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4628 if (!(vcpu->guest_debug &
4629 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
4630 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
4631 kvm_queue_exception(vcpu, DB_VECTOR);
4632 return 1;
4633 }
4634 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4635 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4636 /* fall through */
4637 case BP_VECTOR:
c573cd22
JK
4638 /*
4639 * Update instruction length as we may reinject #BP from
4640 * user space while in guest debugging mode. Reading it for
4641 * #DB as well causes no harm, it is not used in that case.
4642 */
4643 vmx->vcpu.arch.event_exit_inst_len =
4644 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6aa8b732 4645 kvm_run->exit_reason = KVM_EXIT_DEBUG;
0a434bb2 4646 rip = kvm_rip_read(vcpu);
d0bfb940
JK
4647 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4648 kvm_run->debug.arch.exception = ex_no;
42dbaa5a
JK
4649 break;
4650 default:
d0bfb940
JK
4651 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4652 kvm_run->ex.exception = ex_no;
4653 kvm_run->ex.error_code = error_code;
42dbaa5a 4654 break;
6aa8b732 4655 }
6aa8b732
AK
4656 return 0;
4657}
4658
851ba692 4659static int handle_external_interrupt(struct kvm_vcpu *vcpu)
6aa8b732 4660{
1165f5fe 4661 ++vcpu->stat.irq_exits;
6aa8b732
AK
4662 return 1;
4663}
4664
851ba692 4665static int handle_triple_fault(struct kvm_vcpu *vcpu)
988ad74f 4666{
851ba692 4667 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
988ad74f
AK
4668 return 0;
4669}
6aa8b732 4670
851ba692 4671static int handle_io(struct kvm_vcpu *vcpu)
6aa8b732 4672{
bfdaab09 4673 unsigned long exit_qualification;
34c33d16 4674 int size, in, string;
039576c0 4675 unsigned port;
6aa8b732 4676
bfdaab09 4677 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
039576c0 4678 string = (exit_qualification & 16) != 0;
cf8f70bf 4679 in = (exit_qualification & 8) != 0;
e70669ab 4680
cf8f70bf 4681 ++vcpu->stat.io_exits;
e70669ab 4682
cf8f70bf 4683 if (string || in)
51d8b661 4684 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
e70669ab 4685
cf8f70bf
GN
4686 port = exit_qualification >> 16;
4687 size = (exit_qualification & 7) + 1;
e93f36bc 4688 skip_emulated_instruction(vcpu);
cf8f70bf
GN
4689
4690 return kvm_fast_pio_out(vcpu, size, port);
6aa8b732
AK
4691}
4692
102d8325
IM
4693static void
4694vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4695{
4696 /*
4697 * Patch in the VMCALL instruction:
4698 */
4699 hypercall[0] = 0x0f;
4700 hypercall[1] = 0x01;
4701 hypercall[2] = 0xc1;
102d8325
IM
4702}
4703
0fa06071 4704/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
eeadf9e7
NHE
4705static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4706{
eeadf9e7 4707 if (is_guest_mode(vcpu)) {
1a0d74e6
JK
4708 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4709 unsigned long orig_val = val;
4710
eeadf9e7
NHE
4711 /*
4712 * We get here when L2 changed cr0 in a way that did not change
4713 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
1a0d74e6
JK
4714 * but did change L0 shadowed bits. So we first calculate the
4715 * effective cr0 value that L1 would like to write into the
4716 * hardware. It consists of the L2-owned bits from the new
4717 * value combined with the L1-owned bits from L1's guest_cr0.
eeadf9e7 4718 */
1a0d74e6
JK
4719 val = (val & ~vmcs12->cr0_guest_host_mask) |
4720 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4721
4722 /* TODO: will have to take unrestricted guest mode into
4723 * account */
4724 if ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)
eeadf9e7 4725 return 1;
1a0d74e6
JK
4726
4727 if (kvm_set_cr0(vcpu, val))
4728 return 1;
4729 vmcs_writel(CR0_READ_SHADOW, orig_val);
eeadf9e7 4730 return 0;
1a0d74e6
JK
4731 } else {
4732 if (to_vmx(vcpu)->nested.vmxon &&
4733 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
4734 return 1;
eeadf9e7 4735 return kvm_set_cr0(vcpu, val);
1a0d74e6 4736 }
eeadf9e7
NHE
4737}
4738
4739static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4740{
4741 if (is_guest_mode(vcpu)) {
1a0d74e6
JK
4742 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4743 unsigned long orig_val = val;
4744
4745 /* analogously to handle_set_cr0 */
4746 val = (val & ~vmcs12->cr4_guest_host_mask) |
4747 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4748 if (kvm_set_cr4(vcpu, val))
eeadf9e7 4749 return 1;
1a0d74e6 4750 vmcs_writel(CR4_READ_SHADOW, orig_val);
eeadf9e7
NHE
4751 return 0;
4752 } else
4753 return kvm_set_cr4(vcpu, val);
4754}
4755
4756/* called to set cr0 as approriate for clts instruction exit. */
4757static void handle_clts(struct kvm_vcpu *vcpu)
4758{
4759 if (is_guest_mode(vcpu)) {
4760 /*
4761 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
4762 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
4763 * just pretend it's off (also in arch.cr0 for fpu_activate).
4764 */
4765 vmcs_writel(CR0_READ_SHADOW,
4766 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
4767 vcpu->arch.cr0 &= ~X86_CR0_TS;
4768 } else
4769 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4770}
4771
851ba692 4772static int handle_cr(struct kvm_vcpu *vcpu)
6aa8b732 4773{
229456fc 4774 unsigned long exit_qualification, val;
6aa8b732
AK
4775 int cr;
4776 int reg;
49a9b07e 4777 int err;
6aa8b732 4778
bfdaab09 4779 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6aa8b732
AK
4780 cr = exit_qualification & 15;
4781 reg = (exit_qualification >> 8) & 15;
4782 switch ((exit_qualification >> 4) & 3) {
4783 case 0: /* mov to cr */
229456fc
MT
4784 val = kvm_register_read(vcpu, reg);
4785 trace_kvm_cr_write(cr, val);
6aa8b732
AK
4786 switch (cr) {
4787 case 0:
eeadf9e7 4788 err = handle_set_cr0(vcpu, val);
db8fcefa 4789 kvm_complete_insn_gp(vcpu, err);
6aa8b732
AK
4790 return 1;
4791 case 3:
2390218b 4792 err = kvm_set_cr3(vcpu, val);
db8fcefa 4793 kvm_complete_insn_gp(vcpu, err);
6aa8b732
AK
4794 return 1;
4795 case 4:
eeadf9e7 4796 err = handle_set_cr4(vcpu, val);
db8fcefa 4797 kvm_complete_insn_gp(vcpu, err);
6aa8b732 4798 return 1;
0a5fff19
GN
4799 case 8: {
4800 u8 cr8_prev = kvm_get_cr8(vcpu);
4801 u8 cr8 = kvm_register_read(vcpu, reg);
eea1cff9 4802 err = kvm_set_cr8(vcpu, cr8);
db8fcefa 4803 kvm_complete_insn_gp(vcpu, err);
0a5fff19
GN
4804 if (irqchip_in_kernel(vcpu->kvm))
4805 return 1;
4806 if (cr8_prev <= cr8)
4807 return 1;
851ba692 4808 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
0a5fff19
GN
4809 return 0;
4810 }
4b8073e4 4811 }
6aa8b732 4812 break;
25c4c276 4813 case 2: /* clts */
eeadf9e7 4814 handle_clts(vcpu);
4d4ec087 4815 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
25c4c276 4816 skip_emulated_instruction(vcpu);
6b52d186 4817 vmx_fpu_activate(vcpu);
25c4c276 4818 return 1;
6aa8b732
AK
4819 case 1: /*mov from cr*/
4820 switch (cr) {
4821 case 3:
9f8fe504
AK
4822 val = kvm_read_cr3(vcpu);
4823 kvm_register_write(vcpu, reg, val);
4824 trace_kvm_cr_read(cr, val);
6aa8b732
AK
4825 skip_emulated_instruction(vcpu);
4826 return 1;
4827 case 8:
229456fc
MT
4828 val = kvm_get_cr8(vcpu);
4829 kvm_register_write(vcpu, reg, val);
4830 trace_kvm_cr_read(cr, val);
6aa8b732
AK
4831 skip_emulated_instruction(vcpu);
4832 return 1;
4833 }
4834 break;
4835 case 3: /* lmsw */
a1f83a74 4836 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
4d4ec087 4837 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
a1f83a74 4838 kvm_lmsw(vcpu, val);
6aa8b732
AK
4839
4840 skip_emulated_instruction(vcpu);
4841 return 1;
4842 default:
4843 break;
4844 }
851ba692 4845 vcpu->run->exit_reason = 0;
a737f256 4846 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
6aa8b732
AK
4847 (int)(exit_qualification >> 4) & 3, cr);
4848 return 0;
4849}
4850
851ba692 4851static int handle_dr(struct kvm_vcpu *vcpu)
6aa8b732 4852{
bfdaab09 4853 unsigned long exit_qualification;
6aa8b732
AK
4854 int dr, reg;
4855
f2483415 4856 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
0a79b009
AK
4857 if (!kvm_require_cpl(vcpu, 0))
4858 return 1;
42dbaa5a
JK
4859 dr = vmcs_readl(GUEST_DR7);
4860 if (dr & DR7_GD) {
4861 /*
4862 * As the vm-exit takes precedence over the debug trap, we
4863 * need to emulate the latter, either for the host or the
4864 * guest debugging itself.
4865 */
4866 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
851ba692
AK
4867 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
4868 vcpu->run->debug.arch.dr7 = dr;
4869 vcpu->run->debug.arch.pc =
42dbaa5a
JK
4870 vmcs_readl(GUEST_CS_BASE) +
4871 vmcs_readl(GUEST_RIP);
851ba692
AK
4872 vcpu->run->debug.arch.exception = DB_VECTOR;
4873 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
42dbaa5a
JK
4874 return 0;
4875 } else {
4876 vcpu->arch.dr7 &= ~DR7_GD;
4877 vcpu->arch.dr6 |= DR6_BD;
4878 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
4879 kvm_queue_exception(vcpu, DB_VECTOR);
4880 return 1;
4881 }
4882 }
4883
bfdaab09 4884 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
42dbaa5a
JK
4885 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4886 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4887 if (exit_qualification & TYPE_MOV_FROM_DR) {
020df079
GN
4888 unsigned long val;
4889 if (!kvm_get_dr(vcpu, dr, &val))
4890 kvm_register_write(vcpu, reg, val);
4891 } else
4892 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
6aa8b732
AK
4893 skip_emulated_instruction(vcpu);
4894 return 1;
4895}
4896
020df079
GN
4897static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
4898{
4899 vmcs_writel(GUEST_DR7, val);
4900}
4901
851ba692 4902static int handle_cpuid(struct kvm_vcpu *vcpu)
6aa8b732 4903{
06465c5a
AK
4904 kvm_emulate_cpuid(vcpu);
4905 return 1;
6aa8b732
AK
4906}
4907
851ba692 4908static int handle_rdmsr(struct kvm_vcpu *vcpu)
6aa8b732 4909{
ad312c7c 4910 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6aa8b732
AK
4911 u64 data;
4912
4913 if (vmx_get_msr(vcpu, ecx, &data)) {
59200273 4914 trace_kvm_msr_read_ex(ecx);
c1a5d4f9 4915 kvm_inject_gp(vcpu, 0);
6aa8b732
AK
4916 return 1;
4917 }
4918
229456fc 4919 trace_kvm_msr_read(ecx, data);
2714d1d3 4920
6aa8b732 4921 /* FIXME: handling of bits 32:63 of rax, rdx */
ad312c7c
ZX
4922 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
4923 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
6aa8b732
AK
4924 skip_emulated_instruction(vcpu);
4925 return 1;
4926}
4927
851ba692 4928static int handle_wrmsr(struct kvm_vcpu *vcpu)
6aa8b732 4929{
8fe8ab46 4930 struct msr_data msr;
ad312c7c
ZX
4931 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
4932 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
4933 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
6aa8b732 4934
8fe8ab46
WA
4935 msr.data = data;
4936 msr.index = ecx;
4937 msr.host_initiated = false;
4938 if (vmx_set_msr(vcpu, &msr) != 0) {
59200273 4939 trace_kvm_msr_write_ex(ecx, data);
c1a5d4f9 4940 kvm_inject_gp(vcpu, 0);
6aa8b732
AK
4941 return 1;
4942 }
4943
59200273 4944 trace_kvm_msr_write(ecx, data);
6aa8b732
AK
4945 skip_emulated_instruction(vcpu);
4946 return 1;
4947}
4948
851ba692 4949static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
6e5d865c 4950{
3842d135 4951 kvm_make_request(KVM_REQ_EVENT, vcpu);
6e5d865c
YS
4952 return 1;
4953}
4954
851ba692 4955static int handle_interrupt_window(struct kvm_vcpu *vcpu)
6aa8b732 4956{
85f455f7
ED
4957 u32 cpu_based_vm_exec_control;
4958
4959 /* clear pending irq */
4960 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4961 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
4962 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2714d1d3 4963
3842d135
AK
4964 kvm_make_request(KVM_REQ_EVENT, vcpu);
4965
a26bf12a 4966 ++vcpu->stat.irq_window_exits;
2714d1d3 4967
c1150d8c
DL
4968 /*
4969 * If the user space waits to inject interrupts, exit as soon as
4970 * possible
4971 */
8061823a 4972 if (!irqchip_in_kernel(vcpu->kvm) &&
851ba692 4973 vcpu->run->request_interrupt_window &&
8061823a 4974 !kvm_cpu_has_interrupt(vcpu)) {
851ba692 4975 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
c1150d8c
DL
4976 return 0;
4977 }
6aa8b732
AK
4978 return 1;
4979}
4980
851ba692 4981static int handle_halt(struct kvm_vcpu *vcpu)
6aa8b732
AK
4982{
4983 skip_emulated_instruction(vcpu);
d3bef15f 4984 return kvm_emulate_halt(vcpu);
6aa8b732
AK
4985}
4986
851ba692 4987static int handle_vmcall(struct kvm_vcpu *vcpu)
c21415e8 4988{
510043da 4989 skip_emulated_instruction(vcpu);
7aa81cc0
AL
4990 kvm_emulate_hypercall(vcpu);
4991 return 1;
c21415e8
IM
4992}
4993
ec25d5e6
GN
4994static int handle_invd(struct kvm_vcpu *vcpu)
4995{
51d8b661 4996 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
ec25d5e6
GN
4997}
4998
851ba692 4999static int handle_invlpg(struct kvm_vcpu *vcpu)
a7052897 5000{
f9c617f6 5001 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
a7052897
MT
5002
5003 kvm_mmu_invlpg(vcpu, exit_qualification);
5004 skip_emulated_instruction(vcpu);
5005 return 1;
5006}
5007
fee84b07
AK
5008static int handle_rdpmc(struct kvm_vcpu *vcpu)
5009{
5010 int err;
5011
5012 err = kvm_rdpmc(vcpu);
5013 kvm_complete_insn_gp(vcpu, err);
5014
5015 return 1;
5016}
5017
851ba692 5018static int handle_wbinvd(struct kvm_vcpu *vcpu)
e5edaa01
ED
5019{
5020 skip_emulated_instruction(vcpu);
f5f48ee1 5021 kvm_emulate_wbinvd(vcpu);
e5edaa01
ED
5022 return 1;
5023}
5024
2acf923e
DC
5025static int handle_xsetbv(struct kvm_vcpu *vcpu)
5026{
5027 u64 new_bv = kvm_read_edx_eax(vcpu);
5028 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5029
5030 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5031 skip_emulated_instruction(vcpu);
5032 return 1;
5033}
5034
851ba692 5035static int handle_apic_access(struct kvm_vcpu *vcpu)
f78e0e2e 5036{
58fbbf26
KT
5037 if (likely(fasteoi)) {
5038 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5039 int access_type, offset;
5040
5041 access_type = exit_qualification & APIC_ACCESS_TYPE;
5042 offset = exit_qualification & APIC_ACCESS_OFFSET;
5043 /*
5044 * Sane guest uses MOV to write EOI, with written value
5045 * not cared. So make a short-circuit here by avoiding
5046 * heavy instruction emulation.
5047 */
5048 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5049 (offset == APIC_EOI)) {
5050 kvm_lapic_set_eoi(vcpu);
5051 skip_emulated_instruction(vcpu);
5052 return 1;
5053 }
5054 }
51d8b661 5055 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
f78e0e2e
SY
5056}
5057
c7c9c56c
YZ
5058static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5059{
5060 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5061 int vector = exit_qualification & 0xff;
5062
5063 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5064 kvm_apic_set_eoi_accelerated(vcpu, vector);
5065 return 1;
5066}
5067
83d4c286
YZ
5068static int handle_apic_write(struct kvm_vcpu *vcpu)
5069{
5070 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5071 u32 offset = exit_qualification & 0xfff;
5072
5073 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5074 kvm_apic_write_nodecode(vcpu, offset);
5075 return 1;
5076}
5077
851ba692 5078static int handle_task_switch(struct kvm_vcpu *vcpu)
37817f29 5079{
60637aac 5080 struct vcpu_vmx *vmx = to_vmx(vcpu);
37817f29 5081 unsigned long exit_qualification;
e269fb21
JK
5082 bool has_error_code = false;
5083 u32 error_code = 0;
37817f29 5084 u16 tss_selector;
7f3d35fd 5085 int reason, type, idt_v, idt_index;
64a7ec06
GN
5086
5087 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
7f3d35fd 5088 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
64a7ec06 5089 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
37817f29
IE
5090
5091 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5092
5093 reason = (u32)exit_qualification >> 30;
64a7ec06
GN
5094 if (reason == TASK_SWITCH_GATE && idt_v) {
5095 switch (type) {
5096 case INTR_TYPE_NMI_INTR:
5097 vcpu->arch.nmi_injected = false;
654f06fc 5098 vmx_set_nmi_mask(vcpu, true);
64a7ec06
GN
5099 break;
5100 case INTR_TYPE_EXT_INTR:
66fd3f7f 5101 case INTR_TYPE_SOFT_INTR:
64a7ec06
GN
5102 kvm_clear_interrupt_queue(vcpu);
5103 break;
5104 case INTR_TYPE_HARD_EXCEPTION:
e269fb21
JK
5105 if (vmx->idt_vectoring_info &
5106 VECTORING_INFO_DELIVER_CODE_MASK) {
5107 has_error_code = true;
5108 error_code =
5109 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5110 }
5111 /* fall through */
64a7ec06
GN
5112 case INTR_TYPE_SOFT_EXCEPTION:
5113 kvm_clear_exception_queue(vcpu);
5114 break;
5115 default:
5116 break;
5117 }
60637aac 5118 }
37817f29
IE
5119 tss_selector = exit_qualification;
5120
64a7ec06
GN
5121 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5122 type != INTR_TYPE_EXT_INTR &&
5123 type != INTR_TYPE_NMI_INTR))
5124 skip_emulated_instruction(vcpu);
5125
7f3d35fd
KW
5126 if (kvm_task_switch(vcpu, tss_selector,
5127 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5128 has_error_code, error_code) == EMULATE_FAIL) {
acb54517
GN
5129 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5130 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5131 vcpu->run->internal.ndata = 0;
42dbaa5a 5132 return 0;
acb54517 5133 }
42dbaa5a
JK
5134
5135 /* clear all local breakpoint enable flags */
5136 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
5137
5138 /*
5139 * TODO: What about debug traps on tss switch?
5140 * Are we supposed to inject them and update dr6?
5141 */
5142
5143 return 1;
37817f29
IE
5144}
5145
851ba692 5146static int handle_ept_violation(struct kvm_vcpu *vcpu)
1439442c 5147{
f9c617f6 5148 unsigned long exit_qualification;
1439442c 5149 gpa_t gpa;
4f5982a5 5150 u32 error_code;
1439442c 5151 int gla_validity;
1439442c 5152
f9c617f6 5153 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
1439442c 5154
1439442c
SY
5155 gla_validity = (exit_qualification >> 7) & 0x3;
5156 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5157 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5158 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5159 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
f9c617f6 5160 vmcs_readl(GUEST_LINEAR_ADDRESS));
1439442c
SY
5161 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5162 (long unsigned int)exit_qualification);
851ba692
AK
5163 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5164 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
596ae895 5165 return 0;
1439442c
SY
5166 }
5167
5168 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
229456fc 5169 trace_kvm_page_fault(gpa, exit_qualification);
4f5982a5
XG
5170
5171 /* It is a write fault? */
5172 error_code = exit_qualification & (1U << 1);
5173 /* ept page table is present? */
5174 error_code |= (exit_qualification >> 3) & 0x1;
5175
5176 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
1439442c
SY
5177}
5178
68f89400
MT
5179static u64 ept_rsvd_mask(u64 spte, int level)
5180{
5181 int i;
5182 u64 mask = 0;
5183
5184 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
5185 mask |= (1ULL << i);
5186
5187 if (level > 2)
5188 /* bits 7:3 reserved */
5189 mask |= 0xf8;
5190 else if (level == 2) {
5191 if (spte & (1ULL << 7))
5192 /* 2MB ref, bits 20:12 reserved */
5193 mask |= 0x1ff000;
5194 else
5195 /* bits 6:3 reserved */
5196 mask |= 0x78;
5197 }
5198
5199 return mask;
5200}
5201
5202static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
5203 int level)
5204{
5205 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
5206
5207 /* 010b (write-only) */
5208 WARN_ON((spte & 0x7) == 0x2);
5209
5210 /* 110b (write/execute) */
5211 WARN_ON((spte & 0x7) == 0x6);
5212
5213 /* 100b (execute-only) and value not supported by logical processor */
5214 if (!cpu_has_vmx_ept_execute_only())
5215 WARN_ON((spte & 0x7) == 0x4);
5216
5217 /* not 000b */
5218 if ((spte & 0x7)) {
5219 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
5220
5221 if (rsvd_bits != 0) {
5222 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
5223 __func__, rsvd_bits);
5224 WARN_ON(1);
5225 }
5226
5227 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
5228 u64 ept_mem_type = (spte & 0x38) >> 3;
5229
5230 if (ept_mem_type == 2 || ept_mem_type == 3 ||
5231 ept_mem_type == 7) {
5232 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
5233 __func__, ept_mem_type);
5234 WARN_ON(1);
5235 }
5236 }
5237 }
5238}
5239
851ba692 5240static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
68f89400
MT
5241{
5242 u64 sptes[4];
ce88decf 5243 int nr_sptes, i, ret;
68f89400
MT
5244 gpa_t gpa;
5245
5246 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5247
ce88decf
XG
5248 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
5249 if (likely(ret == 1))
5250 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5251 EMULATE_DONE;
5252 if (unlikely(!ret))
5253 return 1;
5254
5255 /* It is the real ept misconfig */
68f89400
MT
5256 printk(KERN_ERR "EPT: Misconfiguration.\n");
5257 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
5258
5259 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
5260
5261 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
5262 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
5263
851ba692
AK
5264 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5265 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
68f89400
MT
5266
5267 return 0;
5268}
5269
851ba692 5270static int handle_nmi_window(struct kvm_vcpu *vcpu)
f08864b4
SY
5271{
5272 u32 cpu_based_vm_exec_control;
5273
5274 /* clear pending NMI */
5275 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5276 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5277 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5278 ++vcpu->stat.nmi_window_exits;
3842d135 5279 kvm_make_request(KVM_REQ_EVENT, vcpu);
f08864b4
SY
5280
5281 return 1;
5282}
5283
80ced186 5284static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
ea953ef0 5285{
8b3079a5
AK
5286 struct vcpu_vmx *vmx = to_vmx(vcpu);
5287 enum emulation_result err = EMULATE_DONE;
80ced186 5288 int ret = 1;
49e9d557
AK
5289 u32 cpu_exec_ctrl;
5290 bool intr_window_requested;
b8405c18 5291 unsigned count = 130;
49e9d557
AK
5292
5293 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5294 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
ea953ef0 5295
b8405c18 5296 while (!guest_state_valid(vcpu) && count-- != 0) {
bdea48e3 5297 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
49e9d557
AK
5298 return handle_interrupt_window(&vmx->vcpu);
5299
de87dcdd
AK
5300 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5301 return 1;
5302
991eebf9 5303 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
ea953ef0 5304
80ced186
MG
5305 if (err == EMULATE_DO_MMIO) {
5306 ret = 0;
5307 goto out;
5308 }
1d5a4d9b 5309
de5f70e0
AK
5310 if (err != EMULATE_DONE) {
5311 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5312 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5313 vcpu->run->internal.ndata = 0;
6d77dbfc 5314 return 0;
de5f70e0 5315 }
ea953ef0
MG
5316
5317 if (signal_pending(current))
80ced186 5318 goto out;
ea953ef0
MG
5319 if (need_resched())
5320 schedule();
5321 }
5322
14168786 5323 vmx->emulation_required = emulation_required(vcpu);
80ced186
MG
5324out:
5325 return ret;
ea953ef0
MG
5326}
5327
4b8d54f9
ZE
5328/*
5329 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5330 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5331 */
9fb41ba8 5332static int handle_pause(struct kvm_vcpu *vcpu)
4b8d54f9
ZE
5333{
5334 skip_emulated_instruction(vcpu);
5335 kvm_vcpu_on_spin(vcpu);
5336
5337 return 1;
5338}
5339
59708670
SY
5340static int handle_invalid_op(struct kvm_vcpu *vcpu)
5341{
5342 kvm_queue_exception(vcpu, UD_VECTOR);
5343 return 1;
5344}
5345
ff2f6fe9
NHE
5346/*
5347 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
5348 * We could reuse a single VMCS for all the L2 guests, but we also want the
5349 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
5350 * allows keeping them loaded on the processor, and in the future will allow
5351 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
5352 * every entry if they never change.
5353 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
5354 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
5355 *
5356 * The following functions allocate and free a vmcs02 in this pool.
5357 */
5358
5359/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
5360static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
5361{
5362 struct vmcs02_list *item;
5363 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5364 if (item->vmptr == vmx->nested.current_vmptr) {
5365 list_move(&item->list, &vmx->nested.vmcs02_pool);
5366 return &item->vmcs02;
5367 }
5368
5369 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
5370 /* Recycle the least recently used VMCS. */
5371 item = list_entry(vmx->nested.vmcs02_pool.prev,
5372 struct vmcs02_list, list);
5373 item->vmptr = vmx->nested.current_vmptr;
5374 list_move(&item->list, &vmx->nested.vmcs02_pool);
5375 return &item->vmcs02;
5376 }
5377
5378 /* Create a new VMCS */
0fa24ce3 5379 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
ff2f6fe9
NHE
5380 if (!item)
5381 return NULL;
5382 item->vmcs02.vmcs = alloc_vmcs();
5383 if (!item->vmcs02.vmcs) {
5384 kfree(item);
5385 return NULL;
5386 }
5387 loaded_vmcs_init(&item->vmcs02);
5388 item->vmptr = vmx->nested.current_vmptr;
5389 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5390 vmx->nested.vmcs02_num++;
5391 return &item->vmcs02;
5392}
5393
5394/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5395static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5396{
5397 struct vmcs02_list *item;
5398 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5399 if (item->vmptr == vmptr) {
5400 free_loaded_vmcs(&item->vmcs02);
5401 list_del(&item->list);
5402 kfree(item);
5403 vmx->nested.vmcs02_num--;
5404 return;
5405 }
5406}
5407
5408/*
5409 * Free all VMCSs saved for this vcpu, except the one pointed by
5410 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
5411 * currently used, if running L2), and vmcs01 when running L2.
5412 */
5413static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5414{
5415 struct vmcs02_list *item, *n;
5416 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
5417 if (vmx->loaded_vmcs != &item->vmcs02)
5418 free_loaded_vmcs(&item->vmcs02);
5419 list_del(&item->list);
5420 kfree(item);
5421 }
5422 vmx->nested.vmcs02_num = 0;
5423
5424 if (vmx->loaded_vmcs != &vmx->vmcs01)
5425 free_loaded_vmcs(&vmx->vmcs01);
5426}
5427
ec378aee
NHE
5428/*
5429 * Emulate the VMXON instruction.
5430 * Currently, we just remember that VMX is active, and do not save or even
5431 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
5432 * do not currently need to store anything in that guest-allocated memory
5433 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
5434 * argument is different from the VMXON pointer (which the spec says they do).
5435 */
5436static int handle_vmon(struct kvm_vcpu *vcpu)
5437{
5438 struct kvm_segment cs;
5439 struct vcpu_vmx *vmx = to_vmx(vcpu);
5440
5441 /* The Intel VMX Instruction Reference lists a bunch of bits that
5442 * are prerequisite to running VMXON, most notably cr4.VMXE must be
5443 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
5444 * Otherwise, we should fail with #UD. We test these now:
5445 */
5446 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
5447 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
5448 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5449 kvm_queue_exception(vcpu, UD_VECTOR);
5450 return 1;
5451 }
5452
5453 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5454 if (is_long_mode(vcpu) && !cs.l) {
5455 kvm_queue_exception(vcpu, UD_VECTOR);
5456 return 1;
5457 }
5458
5459 if (vmx_get_cpl(vcpu)) {
5460 kvm_inject_gp(vcpu, 0);
5461 return 1;
5462 }
5463
ff2f6fe9
NHE
5464 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
5465 vmx->nested.vmcs02_num = 0;
5466
ec378aee
NHE
5467 vmx->nested.vmxon = true;
5468
5469 skip_emulated_instruction(vcpu);
5470 return 1;
5471}
5472
5473/*
5474 * Intel's VMX Instruction Reference specifies a common set of prerequisites
5475 * for running VMX instructions (except VMXON, whose prerequisites are
5476 * slightly different). It also specifies what exception to inject otherwise.
5477 */
5478static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
5479{
5480 struct kvm_segment cs;
5481 struct vcpu_vmx *vmx = to_vmx(vcpu);
5482
5483 if (!vmx->nested.vmxon) {
5484 kvm_queue_exception(vcpu, UD_VECTOR);
5485 return 0;
5486 }
5487
5488 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5489 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
5490 (is_long_mode(vcpu) && !cs.l)) {
5491 kvm_queue_exception(vcpu, UD_VECTOR);
5492 return 0;
5493 }
5494
5495 if (vmx_get_cpl(vcpu)) {
5496 kvm_inject_gp(vcpu, 0);
5497 return 0;
5498 }
5499
5500 return 1;
5501}
5502
5503/*
5504 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
5505 * just stops using VMX.
5506 */
5507static void free_nested(struct vcpu_vmx *vmx)
5508{
5509 if (!vmx->nested.vmxon)
5510 return;
5511 vmx->nested.vmxon = false;
a9d30f33
NHE
5512 if (vmx->nested.current_vmptr != -1ull) {
5513 kunmap(vmx->nested.current_vmcs12_page);
5514 nested_release_page(vmx->nested.current_vmcs12_page);
5515 vmx->nested.current_vmptr = -1ull;
5516 vmx->nested.current_vmcs12 = NULL;
5517 }
fe3ef05c
NHE
5518 /* Unpin physical memory we referred to in current vmcs02 */
5519 if (vmx->nested.apic_access_page) {
5520 nested_release_page(vmx->nested.apic_access_page);
5521 vmx->nested.apic_access_page = 0;
5522 }
ff2f6fe9
NHE
5523
5524 nested_free_all_saved_vmcss(vmx);
ec378aee
NHE
5525}
5526
5527/* Emulate the VMXOFF instruction */
5528static int handle_vmoff(struct kvm_vcpu *vcpu)
5529{
5530 if (!nested_vmx_check_permission(vcpu))
5531 return 1;
5532 free_nested(to_vmx(vcpu));
5533 skip_emulated_instruction(vcpu);
5534 return 1;
5535}
5536
064aea77
NHE
5537/*
5538 * Decode the memory-address operand of a vmx instruction, as recorded on an
5539 * exit caused by such an instruction (run by a guest hypervisor).
5540 * On success, returns 0. When the operand is invalid, returns 1 and throws
5541 * #UD or #GP.
5542 */
5543static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5544 unsigned long exit_qualification,
5545 u32 vmx_instruction_info, gva_t *ret)
5546{
5547 /*
5548 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5549 * Execution", on an exit, vmx_instruction_info holds most of the
5550 * addressing components of the operand. Only the displacement part
5551 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5552 * For how an actual address is calculated from all these components,
5553 * refer to Vol. 1, "Operand Addressing".
5554 */
5555 int scaling = vmx_instruction_info & 3;
5556 int addr_size = (vmx_instruction_info >> 7) & 7;
5557 bool is_reg = vmx_instruction_info & (1u << 10);
5558 int seg_reg = (vmx_instruction_info >> 15) & 7;
5559 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5560 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5561 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5562 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5563
5564 if (is_reg) {
5565 kvm_queue_exception(vcpu, UD_VECTOR);
5566 return 1;
5567 }
5568
5569 /* Addr = segment_base + offset */
5570 /* offset = base + [index * scale] + displacement */
5571 *ret = vmx_get_segment_base(vcpu, seg_reg);
5572 if (base_is_valid)
5573 *ret += kvm_register_read(vcpu, base_reg);
5574 if (index_is_valid)
5575 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5576 *ret += exit_qualification; /* holds the displacement */
5577
5578 if (addr_size == 1) /* 32 bit */
5579 *ret &= 0xffffffff;
5580
5581 /*
5582 * TODO: throw #GP (and return 1) in various cases that the VM*
5583 * instructions require it - e.g., offset beyond segment limit,
5584 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5585 * address, and so on. Currently these are not checked.
5586 */
5587 return 0;
5588}
5589
0140caea
NHE
5590/*
5591 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5592 * set the success or error code of an emulated VMX instruction, as specified
5593 * by Vol 2B, VMX Instruction Reference, "Conventions".
5594 */
5595static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5596{
5597 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5598 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5599 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5600}
5601
5602static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5603{
5604 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5605 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5606 X86_EFLAGS_SF | X86_EFLAGS_OF))
5607 | X86_EFLAGS_CF);
5608}
5609
5610static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
5611 u32 vm_instruction_error)
5612{
5613 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5614 /*
5615 * failValid writes the error number to the current VMCS, which
5616 * can't be done there isn't a current VMCS.
5617 */
5618 nested_vmx_failInvalid(vcpu);
5619 return;
5620 }
5621 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5622 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5623 X86_EFLAGS_SF | X86_EFLAGS_OF))
5624 | X86_EFLAGS_ZF);
5625 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5626}
5627
27d6c865
NHE
5628/* Emulate the VMCLEAR instruction */
5629static int handle_vmclear(struct kvm_vcpu *vcpu)
5630{
5631 struct vcpu_vmx *vmx = to_vmx(vcpu);
5632 gva_t gva;
5633 gpa_t vmptr;
5634 struct vmcs12 *vmcs12;
5635 struct page *page;
5636 struct x86_exception e;
5637
5638 if (!nested_vmx_check_permission(vcpu))
5639 return 1;
5640
5641 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5642 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5643 return 1;
5644
5645 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5646 sizeof(vmptr), &e)) {
5647 kvm_inject_page_fault(vcpu, &e);
5648 return 1;
5649 }
5650
5651 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5652 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
5653 skip_emulated_instruction(vcpu);
5654 return 1;
5655 }
5656
5657 if (vmptr == vmx->nested.current_vmptr) {
5658 kunmap(vmx->nested.current_vmcs12_page);
5659 nested_release_page(vmx->nested.current_vmcs12_page);
5660 vmx->nested.current_vmptr = -1ull;
5661 vmx->nested.current_vmcs12 = NULL;
5662 }
5663
5664 page = nested_get_page(vcpu, vmptr);
5665 if (page == NULL) {
5666 /*
5667 * For accurate processor emulation, VMCLEAR beyond available
5668 * physical memory should do nothing at all. However, it is
5669 * possible that a nested vmx bug, not a guest hypervisor bug,
5670 * resulted in this case, so let's shut down before doing any
5671 * more damage:
5672 */
5673 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5674 return 1;
5675 }
5676 vmcs12 = kmap(page);
5677 vmcs12->launch_state = 0;
5678 kunmap(page);
5679 nested_release_page(page);
5680
5681 nested_free_vmcs02(vmx, vmptr);
5682
5683 skip_emulated_instruction(vcpu);
5684 nested_vmx_succeed(vcpu);
5685 return 1;
5686}
5687
cd232ad0
NHE
5688static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
5689
5690/* Emulate the VMLAUNCH instruction */
5691static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5692{
5693 return nested_vmx_run(vcpu, true);
5694}
5695
5696/* Emulate the VMRESUME instruction */
5697static int handle_vmresume(struct kvm_vcpu *vcpu)
5698{
5699
5700 return nested_vmx_run(vcpu, false);
5701}
5702
49f705c5
NHE
5703enum vmcs_field_type {
5704 VMCS_FIELD_TYPE_U16 = 0,
5705 VMCS_FIELD_TYPE_U64 = 1,
5706 VMCS_FIELD_TYPE_U32 = 2,
5707 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
5708};
5709
5710static inline int vmcs_field_type(unsigned long field)
5711{
5712 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
5713 return VMCS_FIELD_TYPE_U32;
5714 return (field >> 13) & 0x3 ;
5715}
5716
5717static inline int vmcs_field_readonly(unsigned long field)
5718{
5719 return (((field >> 10) & 0x3) == 1);
5720}
5721
5722/*
5723 * Read a vmcs12 field. Since these can have varying lengths and we return
5724 * one type, we chose the biggest type (u64) and zero-extend the return value
5725 * to that size. Note that the caller, handle_vmread, might need to use only
5726 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
5727 * 64-bit fields are to be returned).
5728 */
5729static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
5730 unsigned long field, u64 *ret)
5731{
5732 short offset = vmcs_field_to_offset(field);
5733 char *p;
5734
5735 if (offset < 0)
5736 return 0;
5737
5738 p = ((char *)(get_vmcs12(vcpu))) + offset;
5739
5740 switch (vmcs_field_type(field)) {
5741 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5742 *ret = *((natural_width *)p);
5743 return 1;
5744 case VMCS_FIELD_TYPE_U16:
5745 *ret = *((u16 *)p);
5746 return 1;
5747 case VMCS_FIELD_TYPE_U32:
5748 *ret = *((u32 *)p);
5749 return 1;
5750 case VMCS_FIELD_TYPE_U64:
5751 *ret = *((u64 *)p);
5752 return 1;
5753 default:
5754 return 0; /* can never happen. */
5755 }
5756}
5757
5758/*
5759 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
5760 * used before) all generate the same failure when it is missing.
5761 */
5762static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
5763{
5764 struct vcpu_vmx *vmx = to_vmx(vcpu);
5765 if (vmx->nested.current_vmptr == -1ull) {
5766 nested_vmx_failInvalid(vcpu);
5767 skip_emulated_instruction(vcpu);
5768 return 0;
5769 }
5770 return 1;
5771}
5772
5773static int handle_vmread(struct kvm_vcpu *vcpu)
5774{
5775 unsigned long field;
5776 u64 field_value;
5777 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5778 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5779 gva_t gva = 0;
5780
5781 if (!nested_vmx_check_permission(vcpu) ||
5782 !nested_vmx_check_vmcs12(vcpu))
5783 return 1;
5784
5785 /* Decode instruction info and find the field to read */
5786 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5787 /* Read the field, zero-extended to a u64 field_value */
5788 if (!vmcs12_read_any(vcpu, field, &field_value)) {
5789 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5790 skip_emulated_instruction(vcpu);
5791 return 1;
5792 }
5793 /*
5794 * Now copy part of this value to register or memory, as requested.
5795 * Note that the number of bits actually copied is 32 or 64 depending
5796 * on the guest's mode (32 or 64 bit), not on the given field's length.
5797 */
5798 if (vmx_instruction_info & (1u << 10)) {
5799 kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
5800 field_value);
5801 } else {
5802 if (get_vmx_mem_address(vcpu, exit_qualification,
5803 vmx_instruction_info, &gva))
5804 return 1;
5805 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
5806 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
5807 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
5808 }
5809
5810 nested_vmx_succeed(vcpu);
5811 skip_emulated_instruction(vcpu);
5812 return 1;
5813}
5814
5815
5816static int handle_vmwrite(struct kvm_vcpu *vcpu)
5817{
5818 unsigned long field;
5819 gva_t gva;
5820 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5821 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5822 char *p;
5823 short offset;
5824 /* The value to write might be 32 or 64 bits, depending on L1's long
5825 * mode, and eventually we need to write that into a field of several
5826 * possible lengths. The code below first zero-extends the value to 64
5827 * bit (field_value), and then copies only the approriate number of
5828 * bits into the vmcs12 field.
5829 */
5830 u64 field_value = 0;
5831 struct x86_exception e;
5832
5833 if (!nested_vmx_check_permission(vcpu) ||
5834 !nested_vmx_check_vmcs12(vcpu))
5835 return 1;
5836
5837 if (vmx_instruction_info & (1u << 10))
5838 field_value = kvm_register_read(vcpu,
5839 (((vmx_instruction_info) >> 3) & 0xf));
5840 else {
5841 if (get_vmx_mem_address(vcpu, exit_qualification,
5842 vmx_instruction_info, &gva))
5843 return 1;
5844 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
5845 &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
5846 kvm_inject_page_fault(vcpu, &e);
5847 return 1;
5848 }
5849 }
5850
5851
5852 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5853 if (vmcs_field_readonly(field)) {
5854 nested_vmx_failValid(vcpu,
5855 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
5856 skip_emulated_instruction(vcpu);
5857 return 1;
5858 }
5859
5860 offset = vmcs_field_to_offset(field);
5861 if (offset < 0) {
5862 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5863 skip_emulated_instruction(vcpu);
5864 return 1;
5865 }
5866 p = ((char *) get_vmcs12(vcpu)) + offset;
5867
5868 switch (vmcs_field_type(field)) {
5869 case VMCS_FIELD_TYPE_U16:
5870 *(u16 *)p = field_value;
5871 break;
5872 case VMCS_FIELD_TYPE_U32:
5873 *(u32 *)p = field_value;
5874 break;
5875 case VMCS_FIELD_TYPE_U64:
5876 *(u64 *)p = field_value;
5877 break;
5878 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5879 *(natural_width *)p = field_value;
5880 break;
5881 default:
5882 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5883 skip_emulated_instruction(vcpu);
5884 return 1;
5885 }
5886
5887 nested_vmx_succeed(vcpu);
5888 skip_emulated_instruction(vcpu);
5889 return 1;
5890}
5891
63846663
NHE
5892/* Emulate the VMPTRLD instruction */
5893static int handle_vmptrld(struct kvm_vcpu *vcpu)
5894{
5895 struct vcpu_vmx *vmx = to_vmx(vcpu);
5896 gva_t gva;
5897 gpa_t vmptr;
5898 struct x86_exception e;
5899
5900 if (!nested_vmx_check_permission(vcpu))
5901 return 1;
5902
5903 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5904 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5905 return 1;
5906
5907 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5908 sizeof(vmptr), &e)) {
5909 kvm_inject_page_fault(vcpu, &e);
5910 return 1;
5911 }
5912
5913 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5914 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
5915 skip_emulated_instruction(vcpu);
5916 return 1;
5917 }
5918
5919 if (vmx->nested.current_vmptr != vmptr) {
5920 struct vmcs12 *new_vmcs12;
5921 struct page *page;
5922 page = nested_get_page(vcpu, vmptr);
5923 if (page == NULL) {
5924 nested_vmx_failInvalid(vcpu);
5925 skip_emulated_instruction(vcpu);
5926 return 1;
5927 }
5928 new_vmcs12 = kmap(page);
5929 if (new_vmcs12->revision_id != VMCS12_REVISION) {
5930 kunmap(page);
5931 nested_release_page_clean(page);
5932 nested_vmx_failValid(vcpu,
5933 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5934 skip_emulated_instruction(vcpu);
5935 return 1;
5936 }
5937 if (vmx->nested.current_vmptr != -1ull) {
5938 kunmap(vmx->nested.current_vmcs12_page);
5939 nested_release_page(vmx->nested.current_vmcs12_page);
5940 }
5941
5942 vmx->nested.current_vmptr = vmptr;
5943 vmx->nested.current_vmcs12 = new_vmcs12;
5944 vmx->nested.current_vmcs12_page = page;
5945 }
5946
5947 nested_vmx_succeed(vcpu);
5948 skip_emulated_instruction(vcpu);
5949 return 1;
5950}
5951
6a4d7550
NHE
5952/* Emulate the VMPTRST instruction */
5953static int handle_vmptrst(struct kvm_vcpu *vcpu)
5954{
5955 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5956 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5957 gva_t vmcs_gva;
5958 struct x86_exception e;
5959
5960 if (!nested_vmx_check_permission(vcpu))
5961 return 1;
5962
5963 if (get_vmx_mem_address(vcpu, exit_qualification,
5964 vmx_instruction_info, &vmcs_gva))
5965 return 1;
5966 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
5967 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
5968 (void *)&to_vmx(vcpu)->nested.current_vmptr,
5969 sizeof(u64), &e)) {
5970 kvm_inject_page_fault(vcpu, &e);
5971 return 1;
5972 }
5973 nested_vmx_succeed(vcpu);
5974 skip_emulated_instruction(vcpu);
5975 return 1;
5976}
5977
6aa8b732
AK
5978/*
5979 * The exit handlers return 1 if the exit was handled fully and guest execution
5980 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5981 * to be done to userspace and return 0.
5982 */
772e0318 5983static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
6aa8b732
AK
5984 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
5985 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
988ad74f 5986 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
f08864b4 5987 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
6aa8b732 5988 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
6aa8b732
AK
5989 [EXIT_REASON_CR_ACCESS] = handle_cr,
5990 [EXIT_REASON_DR_ACCESS] = handle_dr,
5991 [EXIT_REASON_CPUID] = handle_cpuid,
5992 [EXIT_REASON_MSR_READ] = handle_rdmsr,
5993 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
5994 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
5995 [EXIT_REASON_HLT] = handle_halt,
ec25d5e6 5996 [EXIT_REASON_INVD] = handle_invd,
a7052897 5997 [EXIT_REASON_INVLPG] = handle_invlpg,
fee84b07 5998 [EXIT_REASON_RDPMC] = handle_rdpmc,
c21415e8 5999 [EXIT_REASON_VMCALL] = handle_vmcall,
27d6c865 6000 [EXIT_REASON_VMCLEAR] = handle_vmclear,
cd232ad0 6001 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
63846663 6002 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
6a4d7550 6003 [EXIT_REASON_VMPTRST] = handle_vmptrst,
49f705c5 6004 [EXIT_REASON_VMREAD] = handle_vmread,
cd232ad0 6005 [EXIT_REASON_VMRESUME] = handle_vmresume,
49f705c5 6006 [EXIT_REASON_VMWRITE] = handle_vmwrite,
ec378aee
NHE
6007 [EXIT_REASON_VMOFF] = handle_vmoff,
6008 [EXIT_REASON_VMON] = handle_vmon,
f78e0e2e
SY
6009 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
6010 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
83d4c286 6011 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
c7c9c56c 6012 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
e5edaa01 6013 [EXIT_REASON_WBINVD] = handle_wbinvd,
2acf923e 6014 [EXIT_REASON_XSETBV] = handle_xsetbv,
37817f29 6015 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
a0861c02 6016 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
68f89400
MT
6017 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
6018 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
4b8d54f9 6019 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
59708670
SY
6020 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
6021 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
6aa8b732
AK
6022};
6023
6024static const int kvm_vmx_max_exit_handlers =
50a3485c 6025 ARRAY_SIZE(kvm_vmx_exit_handlers);
6aa8b732 6026
908a7bdd
JK
6027static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
6028 struct vmcs12 *vmcs12)
6029{
6030 unsigned long exit_qualification;
6031 gpa_t bitmap, last_bitmap;
6032 unsigned int port;
6033 int size;
6034 u8 b;
6035
6036 if (nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING))
6037 return 1;
6038
6039 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
6040 return 0;
6041
6042 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6043
6044 port = exit_qualification >> 16;
6045 size = (exit_qualification & 7) + 1;
6046
6047 last_bitmap = (gpa_t)-1;
6048 b = -1;
6049
6050 while (size > 0) {
6051 if (port < 0x8000)
6052 bitmap = vmcs12->io_bitmap_a;
6053 else if (port < 0x10000)
6054 bitmap = vmcs12->io_bitmap_b;
6055 else
6056 return 1;
6057 bitmap += (port & 0x7fff) / 8;
6058
6059 if (last_bitmap != bitmap)
6060 if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
6061 return 1;
6062 if (b & (1 << (port & 7)))
6063 return 1;
6064
6065 port++;
6066 size--;
6067 last_bitmap = bitmap;
6068 }
6069
6070 return 0;
6071}
6072
644d711a
NHE
6073/*
6074 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
6075 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
6076 * disinterest in the current event (read or write a specific MSR) by using an
6077 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
6078 */
6079static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
6080 struct vmcs12 *vmcs12, u32 exit_reason)
6081{
6082 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
6083 gpa_t bitmap;
6084
cbd29cb6 6085 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
644d711a
NHE
6086 return 1;
6087
6088 /*
6089 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
6090 * for the four combinations of read/write and low/high MSR numbers.
6091 * First we need to figure out which of the four to use:
6092 */
6093 bitmap = vmcs12->msr_bitmap;
6094 if (exit_reason == EXIT_REASON_MSR_WRITE)
6095 bitmap += 2048;
6096 if (msr_index >= 0xc0000000) {
6097 msr_index -= 0xc0000000;
6098 bitmap += 1024;
6099 }
6100
6101 /* Then read the msr_index'th bit from this bitmap: */
6102 if (msr_index < 1024*8) {
6103 unsigned char b;
bd31a7f5
JK
6104 if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
6105 return 1;
644d711a
NHE
6106 return 1 & (b >> (msr_index & 7));
6107 } else
6108 return 1; /* let L1 handle the wrong parameter */
6109}
6110
6111/*
6112 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
6113 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
6114 * intercept (via guest_host_mask etc.) the current event.
6115 */
6116static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
6117 struct vmcs12 *vmcs12)
6118{
6119 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6120 int cr = exit_qualification & 15;
6121 int reg = (exit_qualification >> 8) & 15;
6122 unsigned long val = kvm_register_read(vcpu, reg);
6123
6124 switch ((exit_qualification >> 4) & 3) {
6125 case 0: /* mov to cr */
6126 switch (cr) {
6127 case 0:
6128 if (vmcs12->cr0_guest_host_mask &
6129 (val ^ vmcs12->cr0_read_shadow))
6130 return 1;
6131 break;
6132 case 3:
6133 if ((vmcs12->cr3_target_count >= 1 &&
6134 vmcs12->cr3_target_value0 == val) ||
6135 (vmcs12->cr3_target_count >= 2 &&
6136 vmcs12->cr3_target_value1 == val) ||
6137 (vmcs12->cr3_target_count >= 3 &&
6138 vmcs12->cr3_target_value2 == val) ||
6139 (vmcs12->cr3_target_count >= 4 &&
6140 vmcs12->cr3_target_value3 == val))
6141 return 0;
6142 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
6143 return 1;
6144 break;
6145 case 4:
6146 if (vmcs12->cr4_guest_host_mask &
6147 (vmcs12->cr4_read_shadow ^ val))
6148 return 1;
6149 break;
6150 case 8:
6151 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
6152 return 1;
6153 break;
6154 }
6155 break;
6156 case 2: /* clts */
6157 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
6158 (vmcs12->cr0_read_shadow & X86_CR0_TS))
6159 return 1;
6160 break;
6161 case 1: /* mov from cr */
6162 switch (cr) {
6163 case 3:
6164 if (vmcs12->cpu_based_vm_exec_control &
6165 CPU_BASED_CR3_STORE_EXITING)
6166 return 1;
6167 break;
6168 case 8:
6169 if (vmcs12->cpu_based_vm_exec_control &
6170 CPU_BASED_CR8_STORE_EXITING)
6171 return 1;
6172 break;
6173 }
6174 break;
6175 case 3: /* lmsw */
6176 /*
6177 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
6178 * cr0. Other attempted changes are ignored, with no exit.
6179 */
6180 if (vmcs12->cr0_guest_host_mask & 0xe &
6181 (val ^ vmcs12->cr0_read_shadow))
6182 return 1;
6183 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
6184 !(vmcs12->cr0_read_shadow & 0x1) &&
6185 (val & 0x1))
6186 return 1;
6187 break;
6188 }
6189 return 0;
6190}
6191
6192/*
6193 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
6194 * should handle it ourselves in L0 (and then continue L2). Only call this
6195 * when in is_guest_mode (L2).
6196 */
6197static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
6198{
644d711a
NHE
6199 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6200 struct vcpu_vmx *vmx = to_vmx(vcpu);
6201 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
957c897e 6202 u32 exit_reason = vmx->exit_reason;
644d711a
NHE
6203
6204 if (vmx->nested.nested_run_pending)
6205 return 0;
6206
6207 if (unlikely(vmx->fail)) {
bd80158a
JK
6208 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
6209 vmcs_read32(VM_INSTRUCTION_ERROR));
644d711a
NHE
6210 return 1;
6211 }
6212
6213 switch (exit_reason) {
6214 case EXIT_REASON_EXCEPTION_NMI:
6215 if (!is_exception(intr_info))
6216 return 0;
6217 else if (is_page_fault(intr_info))
6218 return enable_ept;
6219 return vmcs12->exception_bitmap &
6220 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
6221 case EXIT_REASON_EXTERNAL_INTERRUPT:
6222 return 0;
6223 case EXIT_REASON_TRIPLE_FAULT:
6224 return 1;
6225 case EXIT_REASON_PENDING_INTERRUPT:
3b656cf7 6226 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
644d711a 6227 case EXIT_REASON_NMI_WINDOW:
3b656cf7 6228 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
644d711a
NHE
6229 case EXIT_REASON_TASK_SWITCH:
6230 return 1;
6231 case EXIT_REASON_CPUID:
6232 return 1;
6233 case EXIT_REASON_HLT:
6234 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
6235 case EXIT_REASON_INVD:
6236 return 1;
6237 case EXIT_REASON_INVLPG:
6238 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6239 case EXIT_REASON_RDPMC:
6240 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
6241 case EXIT_REASON_RDTSC:
6242 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
6243 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
6244 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
6245 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
6246 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
6247 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
6248 /*
6249 * VMX instructions trap unconditionally. This allows L1 to
6250 * emulate them for its L2 guest, i.e., allows 3-level nesting!
6251 */
6252 return 1;
6253 case EXIT_REASON_CR_ACCESS:
6254 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
6255 case EXIT_REASON_DR_ACCESS:
6256 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
6257 case EXIT_REASON_IO_INSTRUCTION:
908a7bdd 6258 return nested_vmx_exit_handled_io(vcpu, vmcs12);
644d711a
NHE
6259 case EXIT_REASON_MSR_READ:
6260 case EXIT_REASON_MSR_WRITE:
6261 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
6262 case EXIT_REASON_INVALID_STATE:
6263 return 1;
6264 case EXIT_REASON_MWAIT_INSTRUCTION:
6265 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
6266 case EXIT_REASON_MONITOR_INSTRUCTION:
6267 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
6268 case EXIT_REASON_PAUSE_INSTRUCTION:
6269 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
6270 nested_cpu_has2(vmcs12,
6271 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
6272 case EXIT_REASON_MCE_DURING_VMENTRY:
6273 return 0;
6274 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6275 return 1;
6276 case EXIT_REASON_APIC_ACCESS:
6277 return nested_cpu_has2(vmcs12,
6278 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
6279 case EXIT_REASON_EPT_VIOLATION:
6280 case EXIT_REASON_EPT_MISCONFIG:
6281 return 0;
0238ea91
JK
6282 case EXIT_REASON_PREEMPTION_TIMER:
6283 return vmcs12->pin_based_vm_exec_control &
6284 PIN_BASED_VMX_PREEMPTION_TIMER;
644d711a
NHE
6285 case EXIT_REASON_WBINVD:
6286 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6287 case EXIT_REASON_XSETBV:
6288 return 1;
6289 default:
6290 return 1;
6291 }
6292}
6293
586f9607
AK
6294static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6295{
6296 *info1 = vmcs_readl(EXIT_QUALIFICATION);
6297 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6298}
6299
6aa8b732
AK
6300/*
6301 * The guest has exited. See if we can fix it or if we need userspace
6302 * assistance.
6303 */
851ba692 6304static int vmx_handle_exit(struct kvm_vcpu *vcpu)
6aa8b732 6305{
29bd8a78 6306 struct vcpu_vmx *vmx = to_vmx(vcpu);
a0861c02 6307 u32 exit_reason = vmx->exit_reason;
1155f76a 6308 u32 vectoring_info = vmx->idt_vectoring_info;
29bd8a78 6309
80ced186 6310 /* If guest state is invalid, start emulating */
14168786 6311 if (vmx->emulation_required)
80ced186 6312 return handle_invalid_guest_state(vcpu);
1d5a4d9b 6313
b6f1250e
NHE
6314 /*
6315 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
6316 * we did not inject a still-pending event to L1 now because of
6317 * nested_run_pending, we need to re-enable this bit.
6318 */
6319 if (vmx->nested.nested_run_pending)
6320 kvm_make_request(KVM_REQ_EVENT, vcpu);
6321
509c75ea
NHE
6322 if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH ||
6323 exit_reason == EXIT_REASON_VMRESUME))
644d711a
NHE
6324 vmx->nested.nested_run_pending = 1;
6325 else
6326 vmx->nested.nested_run_pending = 0;
6327
6328 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
6329 nested_vmx_vmexit(vcpu);
6330 return 1;
6331 }
6332
5120702e
MG
6333 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6334 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6335 vcpu->run->fail_entry.hardware_entry_failure_reason
6336 = exit_reason;
6337 return 0;
6338 }
6339
29bd8a78 6340 if (unlikely(vmx->fail)) {
851ba692
AK
6341 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6342 vcpu->run->fail_entry.hardware_entry_failure_reason
29bd8a78
AK
6343 = vmcs_read32(VM_INSTRUCTION_ERROR);
6344 return 0;
6345 }
6aa8b732 6346
b9bf6882
XG
6347 /*
6348 * Note:
6349 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6350 * delivery event since it indicates guest is accessing MMIO.
6351 * The vm-exit can be triggered again after return to guest that
6352 * will cause infinite loop.
6353 */
d77c26fc 6354 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
1439442c 6355 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
60637aac 6356 exit_reason != EXIT_REASON_EPT_VIOLATION &&
b9bf6882
XG
6357 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6358 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6359 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6360 vcpu->run->internal.ndata = 2;
6361 vcpu->run->internal.data[0] = vectoring_info;
6362 vcpu->run->internal.data[1] = exit_reason;
6363 return 0;
6364 }
3b86cd99 6365
644d711a
NHE
6366 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
6367 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
6368 get_vmcs12(vcpu), vcpu)))) {
c4282df9 6369 if (vmx_interrupt_allowed(vcpu)) {
3b86cd99 6370 vmx->soft_vnmi_blocked = 0;
3b86cd99 6371 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
4531220b 6372 vcpu->arch.nmi_pending) {
3b86cd99
JK
6373 /*
6374 * This CPU don't support us in finding the end of an
6375 * NMI-blocked window if the guest runs with IRQs
6376 * disabled. So we pull the trigger after 1 s of
6377 * futile waiting, but inform the user about this.
6378 */
6379 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6380 "state on VCPU %d after 1 s timeout\n",
6381 __func__, vcpu->vcpu_id);
6382 vmx->soft_vnmi_blocked = 0;
3b86cd99 6383 }
3b86cd99
JK
6384 }
6385
6aa8b732
AK
6386 if (exit_reason < kvm_vmx_max_exit_handlers
6387 && kvm_vmx_exit_handlers[exit_reason])
851ba692 6388 return kvm_vmx_exit_handlers[exit_reason](vcpu);
6aa8b732 6389 else {
851ba692
AK
6390 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6391 vcpu->run->hw.hardware_exit_reason = exit_reason;
6aa8b732
AK
6392 }
6393 return 0;
6394}
6395
95ba8273 6396static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
6e5d865c 6397{
95ba8273 6398 if (irr == -1 || tpr < irr) {
6e5d865c
YS
6399 vmcs_write32(TPR_THRESHOLD, 0);
6400 return;
6401 }
6402
95ba8273 6403 vmcs_write32(TPR_THRESHOLD, irr);
6e5d865c
YS
6404}
6405
8d14695f
YZ
6406static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
6407{
6408 u32 sec_exec_control;
6409
6410 /*
6411 * There is not point to enable virtualize x2apic without enable
6412 * apicv
6413 */
c7c9c56c
YZ
6414 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
6415 !vmx_vm_has_apicv(vcpu->kvm))
8d14695f
YZ
6416 return;
6417
6418 if (!vm_need_tpr_shadow(vcpu->kvm))
6419 return;
6420
6421 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6422
6423 if (set) {
6424 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6425 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6426 } else {
6427 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6428 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6429 }
6430 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
6431
6432 vmx_set_msr_bitmap(vcpu);
6433}
6434
c7c9c56c
YZ
6435static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
6436{
6437 u16 status;
6438 u8 old;
6439
6440 if (!vmx_vm_has_apicv(kvm))
6441 return;
6442
6443 if (isr == -1)
6444 isr = 0;
6445
6446 status = vmcs_read16(GUEST_INTR_STATUS);
6447 old = status >> 8;
6448 if (isr != old) {
6449 status &= 0xff;
6450 status |= isr << 8;
6451 vmcs_write16(GUEST_INTR_STATUS, status);
6452 }
6453}
6454
6455static void vmx_set_rvi(int vector)
6456{
6457 u16 status;
6458 u8 old;
6459
6460 status = vmcs_read16(GUEST_INTR_STATUS);
6461 old = (u8)status & 0xff;
6462 if ((u8)vector != old) {
6463 status &= ~0xff;
6464 status |= (u8)vector;
6465 vmcs_write16(GUEST_INTR_STATUS, status);
6466 }
6467}
6468
6469static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6470{
6471 if (max_irr == -1)
6472 return;
6473
6474 vmx_set_rvi(max_irr);
6475}
6476
6477static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
6478{
3d81bc7e
YZ
6479 if (!vmx_vm_has_apicv(vcpu->kvm))
6480 return;
6481
c7c9c56c
YZ
6482 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6483 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6484 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6485 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6486}
6487
51aa01d1 6488static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
cf393f75 6489{
00eba012
AK
6490 u32 exit_intr_info;
6491
6492 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
6493 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
6494 return;
6495
c5ca8e57 6496 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
00eba012 6497 exit_intr_info = vmx->exit_intr_info;
a0861c02
AK
6498
6499 /* Handle machine checks before interrupts are enabled */
00eba012 6500 if (is_machine_check(exit_intr_info))
a0861c02
AK
6501 kvm_machine_check();
6502
20f65983 6503 /* We need to handle NMIs before interrupts are enabled */
00eba012 6504 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
ff9d07a0
ZY
6505 (exit_intr_info & INTR_INFO_VALID_MASK)) {
6506 kvm_before_handle_nmi(&vmx->vcpu);
20f65983 6507 asm("int $2");
ff9d07a0
ZY
6508 kvm_after_handle_nmi(&vmx->vcpu);
6509 }
51aa01d1 6510}
20f65983 6511
a547c6db
YZ
6512static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
6513{
6514 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6515
6516 /*
6517 * If external interrupt exists, IF bit is set in rflags/eflags on the
6518 * interrupt stack frame, and interrupt will be enabled on a return
6519 * from interrupt handler.
6520 */
6521 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
6522 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
6523 unsigned int vector;
6524 unsigned long entry;
6525 gate_desc *desc;
6526 struct vcpu_vmx *vmx = to_vmx(vcpu);
6527#ifdef CONFIG_X86_64
6528 unsigned long tmp;
6529#endif
6530
6531 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6532 desc = (gate_desc *)vmx->host_idt_base + vector;
6533 entry = gate_offset(*desc);
6534 asm volatile(
6535#ifdef CONFIG_X86_64
6536 "mov %%" _ASM_SP ", %[sp]\n\t"
6537 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
6538 "push $%c[ss]\n\t"
6539 "push %[sp]\n\t"
6540#endif
6541 "pushf\n\t"
6542 "orl $0x200, (%%" _ASM_SP ")\n\t"
6543 __ASM_SIZE(push) " $%c[cs]\n\t"
6544 "call *%[entry]\n\t"
6545 :
6546#ifdef CONFIG_X86_64
6547 [sp]"=&r"(tmp)
6548#endif
6549 :
6550 [entry]"r"(entry),
6551 [ss]"i"(__KERNEL_DS),
6552 [cs]"i"(__KERNEL_CS)
6553 );
6554 } else
6555 local_irq_enable();
6556}
6557
51aa01d1
AK
6558static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6559{
c5ca8e57 6560 u32 exit_intr_info;
51aa01d1
AK
6561 bool unblock_nmi;
6562 u8 vector;
6563 bool idtv_info_valid;
6564
6565 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
20f65983 6566
cf393f75 6567 if (cpu_has_virtual_nmis()) {
9d58b931
AK
6568 if (vmx->nmi_known_unmasked)
6569 return;
c5ca8e57
AK
6570 /*
6571 * Can't use vmx->exit_intr_info since we're not sure what
6572 * the exit reason is.
6573 */
6574 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
cf393f75
AK
6575 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6576 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6577 /*
7b4a25cb 6578 * SDM 3: 27.7.1.2 (September 2008)
cf393f75
AK
6579 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6580 * a guest IRET fault.
7b4a25cb
GN
6581 * SDM 3: 23.2.2 (September 2008)
6582 * Bit 12 is undefined in any of the following cases:
6583 * If the VM exit sets the valid bit in the IDT-vectoring
6584 * information field.
6585 * If the VM exit is due to a double fault.
cf393f75 6586 */
7b4a25cb
GN
6587 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6588 vector != DF_VECTOR && !idtv_info_valid)
cf393f75
AK
6589 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6590 GUEST_INTR_STATE_NMI);
9d58b931
AK
6591 else
6592 vmx->nmi_known_unmasked =
6593 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6594 & GUEST_INTR_STATE_NMI);
3b86cd99
JK
6595 } else if (unlikely(vmx->soft_vnmi_blocked))
6596 vmx->vnmi_blocked_time +=
6597 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
51aa01d1
AK
6598}
6599
3ab66e8a 6600static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
83422e17
AK
6601 u32 idt_vectoring_info,
6602 int instr_len_field,
6603 int error_code_field)
51aa01d1 6604{
51aa01d1
AK
6605 u8 vector;
6606 int type;
6607 bool idtv_info_valid;
6608
6609 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
668f612f 6610
3ab66e8a
JK
6611 vcpu->arch.nmi_injected = false;
6612 kvm_clear_exception_queue(vcpu);
6613 kvm_clear_interrupt_queue(vcpu);
37b96e98
GN
6614
6615 if (!idtv_info_valid)
6616 return;
6617
3ab66e8a 6618 kvm_make_request(KVM_REQ_EVENT, vcpu);
3842d135 6619
668f612f
AK
6620 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6621 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
37b96e98 6622
64a7ec06 6623 switch (type) {
37b96e98 6624 case INTR_TYPE_NMI_INTR:
3ab66e8a 6625 vcpu->arch.nmi_injected = true;
668f612f 6626 /*
7b4a25cb 6627 * SDM 3: 27.7.1.2 (September 2008)
37b96e98
GN
6628 * Clear bit "block by NMI" before VM entry if a NMI
6629 * delivery faulted.
668f612f 6630 */
3ab66e8a 6631 vmx_set_nmi_mask(vcpu, false);
37b96e98 6632 break;
37b96e98 6633 case INTR_TYPE_SOFT_EXCEPTION:
3ab66e8a 6634 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
66fd3f7f
GN
6635 /* fall through */
6636 case INTR_TYPE_HARD_EXCEPTION:
35920a35 6637 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
83422e17 6638 u32 err = vmcs_read32(error_code_field);
3ab66e8a 6639 kvm_queue_exception_e(vcpu, vector, err);
35920a35 6640 } else
3ab66e8a 6641 kvm_queue_exception(vcpu, vector);
37b96e98 6642 break;
66fd3f7f 6643 case INTR_TYPE_SOFT_INTR:
3ab66e8a 6644 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
66fd3f7f 6645 /* fall through */
37b96e98 6646 case INTR_TYPE_EXT_INTR:
3ab66e8a 6647 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
37b96e98
GN
6648 break;
6649 default:
6650 break;
f7d9238f 6651 }
cf393f75
AK
6652}
6653
83422e17
AK
6654static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6655{
3ab66e8a 6656 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
83422e17
AK
6657 VM_EXIT_INSTRUCTION_LEN,
6658 IDT_VECTORING_ERROR_CODE);
6659}
6660
b463a6f7
AK
6661static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6662{
3ab66e8a 6663 __vmx_complete_interrupts(vcpu,
b463a6f7
AK
6664 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6665 VM_ENTRY_INSTRUCTION_LEN,
6666 VM_ENTRY_EXCEPTION_ERROR_CODE);
6667
6668 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6669}
6670
d7cd9796
GN
6671static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6672{
6673 int i, nr_msrs;
6674 struct perf_guest_switch_msr *msrs;
6675
6676 msrs = perf_guest_get_msrs(&nr_msrs);
6677
6678 if (!msrs)
6679 return;
6680
6681 for (i = 0; i < nr_msrs; i++)
6682 if (msrs[i].host == msrs[i].guest)
6683 clear_atomic_switch_msr(vmx, msrs[i].msr);
6684 else
6685 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
6686 msrs[i].host);
6687}
6688
a3b5ba49 6689static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
6aa8b732 6690{
a2fa3e9f 6691 struct vcpu_vmx *vmx = to_vmx(vcpu);
2a7921b7 6692 unsigned long debugctlmsr;
104f226b
AK
6693
6694 /* Record the guest's net vcpu time for enforced NMI injections. */
6695 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
6696 vmx->entry_time = ktime_get();
6697
6698 /* Don't enter VMX if guest state is invalid, let the exit handler
6699 start emulation until we arrive back to a valid state */
14168786 6700 if (vmx->emulation_required)
104f226b
AK
6701 return;
6702
6703 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
6704 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
6705 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
6706 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6707
6708 /* When single-stepping over STI and MOV SS, we must clear the
6709 * corresponding interruptibility bits in the guest state. Otherwise
6710 * vmentry fails as it then expects bit 14 (BS) in pending debug
6711 * exceptions being set, but that's not correct for the guest debugging
6712 * case. */
6713 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6714 vmx_set_interrupt_shadow(vcpu, 0);
6715
d7cd9796 6716 atomic_switch_perf_msrs(vmx);
2a7921b7 6717 debugctlmsr = get_debugctlmsr();
d7cd9796 6718
d462b819 6719 vmx->__launched = vmx->loaded_vmcs->launched;
104f226b 6720 asm(
6aa8b732 6721 /* Store host registers */
b188c81f
AK
6722 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
6723 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
6724 "push %%" _ASM_CX " \n\t"
6725 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
313dbd49 6726 "je 1f \n\t"
b188c81f 6727 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
4ecac3fd 6728 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
313dbd49 6729 "1: \n\t"
d3edefc0 6730 /* Reload cr2 if changed */
b188c81f
AK
6731 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
6732 "mov %%cr2, %%" _ASM_DX " \n\t"
6733 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
d3edefc0 6734 "je 2f \n\t"
b188c81f 6735 "mov %%" _ASM_AX", %%cr2 \n\t"
d3edefc0 6736 "2: \n\t"
6aa8b732 6737 /* Check if vmlaunch of vmresume is needed */
e08aa78a 6738 "cmpl $0, %c[launched](%0) \n\t"
6aa8b732 6739 /* Load guest registers. Don't clobber flags. */
b188c81f
AK
6740 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
6741 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
6742 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
6743 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
6744 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
6745 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
05b3e0c2 6746#ifdef CONFIG_X86_64
e08aa78a
AK
6747 "mov %c[r8](%0), %%r8 \n\t"
6748 "mov %c[r9](%0), %%r9 \n\t"
6749 "mov %c[r10](%0), %%r10 \n\t"
6750 "mov %c[r11](%0), %%r11 \n\t"
6751 "mov %c[r12](%0), %%r12 \n\t"
6752 "mov %c[r13](%0), %%r13 \n\t"
6753 "mov %c[r14](%0), %%r14 \n\t"
6754 "mov %c[r15](%0), %%r15 \n\t"
6aa8b732 6755#endif
b188c81f 6756 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
c801949d 6757
6aa8b732 6758 /* Enter guest mode */
83287ea4 6759 "jne 1f \n\t"
4ecac3fd 6760 __ex(ASM_VMX_VMLAUNCH) "\n\t"
83287ea4
AK
6761 "jmp 2f \n\t"
6762 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
6763 "2: "
6aa8b732 6764 /* Save guest registers, load host registers, keep flags */
b188c81f 6765 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
40712fae 6766 "pop %0 \n\t"
b188c81f
AK
6767 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
6768 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
6769 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
6770 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
6771 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
6772 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
6773 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
05b3e0c2 6774#ifdef CONFIG_X86_64
e08aa78a
AK
6775 "mov %%r8, %c[r8](%0) \n\t"
6776 "mov %%r9, %c[r9](%0) \n\t"
6777 "mov %%r10, %c[r10](%0) \n\t"
6778 "mov %%r11, %c[r11](%0) \n\t"
6779 "mov %%r12, %c[r12](%0) \n\t"
6780 "mov %%r13, %c[r13](%0) \n\t"
6781 "mov %%r14, %c[r14](%0) \n\t"
6782 "mov %%r15, %c[r15](%0) \n\t"
6aa8b732 6783#endif
b188c81f
AK
6784 "mov %%cr2, %%" _ASM_AX " \n\t"
6785 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
c801949d 6786
b188c81f 6787 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
e08aa78a 6788 "setbe %c[fail](%0) \n\t"
83287ea4
AK
6789 ".pushsection .rodata \n\t"
6790 ".global vmx_return \n\t"
6791 "vmx_return: " _ASM_PTR " 2b \n\t"
6792 ".popsection"
e08aa78a 6793 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
d462b819 6794 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
e08aa78a 6795 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
313dbd49 6796 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
ad312c7c
ZX
6797 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
6798 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
6799 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
6800 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
6801 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
6802 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
6803 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
05b3e0c2 6804#ifdef CONFIG_X86_64
ad312c7c
ZX
6805 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
6806 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
6807 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
6808 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
6809 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
6810 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
6811 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
6812 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
6aa8b732 6813#endif
40712fae
AK
6814 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
6815 [wordsize]"i"(sizeof(ulong))
c2036300
LV
6816 : "cc", "memory"
6817#ifdef CONFIG_X86_64
b188c81f 6818 , "rax", "rbx", "rdi", "rsi"
c2036300 6819 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
b188c81f
AK
6820#else
6821 , "eax", "ebx", "edi", "esi"
c2036300
LV
6822#endif
6823 );
6aa8b732 6824
2a7921b7
GN
6825 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
6826 if (debugctlmsr)
6827 update_debugctlmsr(debugctlmsr);
6828
aa67f609
AK
6829#ifndef CONFIG_X86_64
6830 /*
6831 * The sysexit path does not restore ds/es, so we must set them to
6832 * a reasonable value ourselves.
6833 *
6834 * We can't defer this to vmx_load_host_state() since that function
6835 * may be executed in interrupt context, which saves and restore segments
6836 * around it, nullifying its effect.
6837 */
6838 loadsegment(ds, __USER_DS);
6839 loadsegment(es, __USER_DS);
6840#endif
6841
6de4f3ad 6842 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
6de12732 6843 | (1 << VCPU_EXREG_RFLAGS)
69c73028 6844 | (1 << VCPU_EXREG_CPL)
aff48baa 6845 | (1 << VCPU_EXREG_PDPTR)
2fb92db1 6846 | (1 << VCPU_EXREG_SEGMENTS)
aff48baa 6847 | (1 << VCPU_EXREG_CR3));
5fdbf976
MT
6848 vcpu->arch.regs_dirty = 0;
6849
1155f76a
AK
6850 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
6851
d462b819 6852 vmx->loaded_vmcs->launched = 1;
1b6269db 6853
51aa01d1 6854 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
1e2b1dd7 6855 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
51aa01d1
AK
6856
6857 vmx_complete_atomic_exit(vmx);
6858 vmx_recover_nmi_blocking(vmx);
cf393f75 6859 vmx_complete_interrupts(vmx);
6aa8b732
AK
6860}
6861
6aa8b732
AK
6862static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6863{
fb3f0f51
RR
6864 struct vcpu_vmx *vmx = to_vmx(vcpu);
6865
cdbecfc3 6866 free_vpid(vmx);
ec378aee 6867 free_nested(vmx);
d462b819 6868 free_loaded_vmcs(vmx->loaded_vmcs);
fb3f0f51
RR
6869 kfree(vmx->guest_msrs);
6870 kvm_vcpu_uninit(vcpu);
a4770347 6871 kmem_cache_free(kvm_vcpu_cache, vmx);
6aa8b732
AK
6872}
6873
fb3f0f51 6874static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
6aa8b732 6875{
fb3f0f51 6876 int err;
c16f862d 6877 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
15ad7146 6878 int cpu;
6aa8b732 6879
a2fa3e9f 6880 if (!vmx)
fb3f0f51
RR
6881 return ERR_PTR(-ENOMEM);
6882
2384d2b3
SY
6883 allocate_vpid(vmx);
6884
fb3f0f51
RR
6885 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
6886 if (err)
6887 goto free_vcpu;
965b58a5 6888
a2fa3e9f 6889 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
be6d05cf 6890 err = -ENOMEM;
fb3f0f51 6891 if (!vmx->guest_msrs) {
fb3f0f51
RR
6892 goto uninit_vcpu;
6893 }
965b58a5 6894
d462b819
NHE
6895 vmx->loaded_vmcs = &vmx->vmcs01;
6896 vmx->loaded_vmcs->vmcs = alloc_vmcs();
6897 if (!vmx->loaded_vmcs->vmcs)
fb3f0f51 6898 goto free_msrs;
d462b819
NHE
6899 if (!vmm_exclusive)
6900 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
6901 loaded_vmcs_init(vmx->loaded_vmcs);
6902 if (!vmm_exclusive)
6903 kvm_cpu_vmxoff();
a2fa3e9f 6904
15ad7146
AK
6905 cpu = get_cpu();
6906 vmx_vcpu_load(&vmx->vcpu, cpu);
e48672fa 6907 vmx->vcpu.cpu = cpu;
8b9cf98c 6908 err = vmx_vcpu_setup(vmx);
fb3f0f51 6909 vmx_vcpu_put(&vmx->vcpu);
15ad7146 6910 put_cpu();
fb3f0f51
RR
6911 if (err)
6912 goto free_vmcs;
a63cb560 6913 if (vm_need_virtualize_apic_accesses(kvm)) {
be6d05cf
JK
6914 err = alloc_apic_access_page(kvm);
6915 if (err)
5e4a0b3c 6916 goto free_vmcs;
a63cb560 6917 }
fb3f0f51 6918
b927a3ce
SY
6919 if (enable_ept) {
6920 if (!kvm->arch.ept_identity_map_addr)
6921 kvm->arch.ept_identity_map_addr =
6922 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
93ea5388 6923 err = -ENOMEM;
b7ebfb05
SY
6924 if (alloc_identity_pagetable(kvm) != 0)
6925 goto free_vmcs;
93ea5388
GN
6926 if (!init_rmode_identity_map(kvm))
6927 goto free_vmcs;
b927a3ce 6928 }
b7ebfb05 6929
a9d30f33
NHE
6930 vmx->nested.current_vmptr = -1ull;
6931 vmx->nested.current_vmcs12 = NULL;
6932
fb3f0f51
RR
6933 return &vmx->vcpu;
6934
6935free_vmcs:
5f3fbc34 6936 free_loaded_vmcs(vmx->loaded_vmcs);
fb3f0f51 6937free_msrs:
fb3f0f51
RR
6938 kfree(vmx->guest_msrs);
6939uninit_vcpu:
6940 kvm_vcpu_uninit(&vmx->vcpu);
6941free_vcpu:
cdbecfc3 6942 free_vpid(vmx);
a4770347 6943 kmem_cache_free(kvm_vcpu_cache, vmx);
fb3f0f51 6944 return ERR_PTR(err);
6aa8b732
AK
6945}
6946
002c7f7c
YS
6947static void __init vmx_check_processor_compat(void *rtn)
6948{
6949 struct vmcs_config vmcs_conf;
6950
6951 *(int *)rtn = 0;
6952 if (setup_vmcs_config(&vmcs_conf) < 0)
6953 *(int *)rtn = -EIO;
6954 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6955 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6956 smp_processor_id());
6957 *(int *)rtn = -EIO;
6958 }
6959}
6960
67253af5
SY
6961static int get_ept_level(void)
6962{
6963 return VMX_EPT_DEFAULT_GAW + 1;
6964}
6965
4b12f0de 6966static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
64d4d521 6967{
4b12f0de
SY
6968 u64 ret;
6969
522c68c4
SY
6970 /* For VT-d and EPT combination
6971 * 1. MMIO: always map as UC
6972 * 2. EPT with VT-d:
6973 * a. VT-d without snooping control feature: can't guarantee the
6974 * result, try to trust guest.
6975 * b. VT-d with snooping control feature: snooping control feature of
6976 * VT-d engine can guarantee the cache correctness. Just set it
6977 * to WB to keep consistent with host. So the same as item 3.
a19a6d11 6978 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
522c68c4
SY
6979 * consistent with host MTRR
6980 */
4b12f0de
SY
6981 if (is_mmio)
6982 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
522c68c4
SY
6983 else if (vcpu->kvm->arch.iommu_domain &&
6984 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
6985 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
6986 VMX_EPT_MT_EPTE_SHIFT;
4b12f0de 6987 else
522c68c4 6988 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
a19a6d11 6989 | VMX_EPT_IPAT_BIT;
4b12f0de
SY
6990
6991 return ret;
64d4d521
SY
6992}
6993
17cc3935 6994static int vmx_get_lpage_level(void)
344f414f 6995{
878403b7
SY
6996 if (enable_ept && !cpu_has_vmx_ept_1g_page())
6997 return PT_DIRECTORY_LEVEL;
6998 else
6999 /* For shadow and EPT supported 1GB page */
7000 return PT_PDPE_LEVEL;
344f414f
JR
7001}
7002
0e851880
SY
7003static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
7004{
4e47c7a6
SY
7005 struct kvm_cpuid_entry2 *best;
7006 struct vcpu_vmx *vmx = to_vmx(vcpu);
7007 u32 exec_control;
7008
7009 vmx->rdtscp_enabled = false;
7010 if (vmx_rdtscp_supported()) {
7011 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7012 if (exec_control & SECONDARY_EXEC_RDTSCP) {
7013 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
7014 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
7015 vmx->rdtscp_enabled = true;
7016 else {
7017 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7018 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7019 exec_control);
7020 }
7021 }
7022 }
ad756a16 7023
ad756a16
MJ
7024 /* Exposing INVPCID only when PCID is exposed */
7025 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
7026 if (vmx_invpcid_supported() &&
4f977045 7027 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
ad756a16 7028 guest_cpuid_has_pcid(vcpu)) {
29282fde 7029 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
ad756a16
MJ
7030 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
7031 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7032 exec_control);
7033 } else {
29282fde
TI
7034 if (cpu_has_secondary_exec_ctrls()) {
7035 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7036 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
7037 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7038 exec_control);
7039 }
ad756a16 7040 if (best)
4f977045 7041 best->ebx &= ~bit(X86_FEATURE_INVPCID);
ad756a16 7042 }
0e851880
SY
7043}
7044
d4330ef2
JR
7045static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
7046{
7b8050f5
NHE
7047 if (func == 1 && nested)
7048 entry->ecx |= bit(X86_FEATURE_VMX);
d4330ef2
JR
7049}
7050
fe3ef05c
NHE
7051/*
7052 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
7053 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
7054 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
7055 * guest in a way that will both be appropriate to L1's requests, and our
7056 * needs. In addition to modifying the active vmcs (which is vmcs02), this
7057 * function also has additional necessary side-effects, like setting various
7058 * vcpu->arch fields.
7059 */
7060static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7061{
7062 struct vcpu_vmx *vmx = to_vmx(vcpu);
7063 u32 exec_control;
7064
7065 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
7066 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
7067 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
7068 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
7069 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
7070 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
7071 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
7072 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
7073 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
7074 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
7075 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
7076 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
7077 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
7078 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
7079 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
7080 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
7081 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
7082 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
7083 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
7084 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
7085 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
7086 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
7087 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
7088 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
7089 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
7090 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
7091 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
7092 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
7093 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
7094 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
7095 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
7096 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
7097 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
7098 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
7099 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
7100 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
7101
7102 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
7103 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
7104 vmcs12->vm_entry_intr_info_field);
7105 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
7106 vmcs12->vm_entry_exception_error_code);
7107 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
7108 vmcs12->vm_entry_instruction_len);
7109 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
7110 vmcs12->guest_interruptibility_info);
fe3ef05c 7111 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
503cd0c5 7112 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
fe3ef05c
NHE
7113 vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags);
7114 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
7115 vmcs12->guest_pending_dbg_exceptions);
7116 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
7117 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
7118
7119 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7120
7121 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
7122 (vmcs_config.pin_based_exec_ctrl |
7123 vmcs12->pin_based_vm_exec_control));
7124
0238ea91
JK
7125 if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
7126 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
7127 vmcs12->vmx_preemption_timer_value);
7128
fe3ef05c
NHE
7129 /*
7130 * Whether page-faults are trapped is determined by a combination of
7131 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
7132 * If enable_ept, L0 doesn't care about page faults and we should
7133 * set all of these to L1's desires. However, if !enable_ept, L0 does
7134 * care about (at least some) page faults, and because it is not easy
7135 * (if at all possible?) to merge L0 and L1's desires, we simply ask
7136 * to exit on each and every L2 page fault. This is done by setting
7137 * MASK=MATCH=0 and (see below) EB.PF=1.
7138 * Note that below we don't need special code to set EB.PF beyond the
7139 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
7140 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
7141 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
7142 *
7143 * A problem with this approach (when !enable_ept) is that L1 may be
7144 * injected with more page faults than it asked for. This could have
7145 * caused problems, but in practice existing hypervisors don't care.
7146 * To fix this, we will need to emulate the PFEC checking (on the L1
7147 * page tables), using walk_addr(), when injecting PFs to L1.
7148 */
7149 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
7150 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
7151 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
7152 enable_ept ? vmcs12->page_fault_error_code_match : 0);
7153
7154 if (cpu_has_secondary_exec_ctrls()) {
7155 u32 exec_control = vmx_secondary_exec_control(vmx);
7156 if (!vmx->rdtscp_enabled)
7157 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7158 /* Take the following fields only from vmcs12 */
7159 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7160 if (nested_cpu_has(vmcs12,
7161 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
7162 exec_control |= vmcs12->secondary_vm_exec_control;
7163
7164 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
7165 /*
7166 * Translate L1 physical address to host physical
7167 * address for vmcs02. Keep the page pinned, so this
7168 * physical address remains valid. We keep a reference
7169 * to it so we can release it later.
7170 */
7171 if (vmx->nested.apic_access_page) /* shouldn't happen */
7172 nested_release_page(vmx->nested.apic_access_page);
7173 vmx->nested.apic_access_page =
7174 nested_get_page(vcpu, vmcs12->apic_access_addr);
7175 /*
7176 * If translation failed, no matter: This feature asks
7177 * to exit when accessing the given address, and if it
7178 * can never be accessed, this feature won't do
7179 * anything anyway.
7180 */
7181 if (!vmx->nested.apic_access_page)
7182 exec_control &=
7183 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7184 else
7185 vmcs_write64(APIC_ACCESS_ADDR,
7186 page_to_phys(vmx->nested.apic_access_page));
7187 }
7188
7189 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
7190 }
7191
7192
7193 /*
7194 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
7195 * Some constant fields are set here by vmx_set_constant_host_state().
7196 * Other fields are different per CPU, and will be set later when
7197 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
7198 */
a547c6db 7199 vmx_set_constant_host_state(vmx);
fe3ef05c
NHE
7200
7201 /*
7202 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
7203 * entry, but only if the current (host) sp changed from the value
7204 * we wrote last (vmx->host_rsp). This cache is no longer relevant
7205 * if we switch vmcs, and rather than hold a separate cache per vmcs,
7206 * here we just force the write to happen on entry.
7207 */
7208 vmx->host_rsp = 0;
7209
7210 exec_control = vmx_exec_control(vmx); /* L0's desires */
7211 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
7212 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
7213 exec_control &= ~CPU_BASED_TPR_SHADOW;
7214 exec_control |= vmcs12->cpu_based_vm_exec_control;
7215 /*
7216 * Merging of IO and MSR bitmaps not currently supported.
7217 * Rather, exit every time.
7218 */
7219 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
7220 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
7221 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
7222
7223 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
7224
7225 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
7226 * bitwise-or of what L1 wants to trap for L2, and what we want to
7227 * trap. Note that CR0.TS also needs updating - we do this later.
7228 */
7229 update_exception_bitmap(vcpu);
7230 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
7231 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7232
7233 /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */
7234 vmcs_write32(VM_EXIT_CONTROLS,
7235 vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl);
7236 vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls |
7237 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
7238
7239 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)
7240 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
7241 else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
7242 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
7243
7244
7245 set_cr4_guest_host_mask(vmx);
7246
27fc51b2
NHE
7247 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
7248 vmcs_write64(TSC_OFFSET,
7249 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
7250 else
7251 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
fe3ef05c
NHE
7252
7253 if (enable_vpid) {
7254 /*
7255 * Trivially support vpid by letting L2s share their parent
7256 * L1's vpid. TODO: move to a more elaborate solution, giving
7257 * each L2 its own vpid and exposing the vpid feature to L1.
7258 */
7259 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
7260 vmx_flush_tlb(vcpu);
7261 }
7262
7263 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
7264 vcpu->arch.efer = vmcs12->guest_ia32_efer;
7265 if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
7266 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7267 else
7268 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7269 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
7270 vmx_set_efer(vcpu, vcpu->arch.efer);
7271
7272 /*
7273 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
7274 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
7275 * The CR0_READ_SHADOW is what L2 should have expected to read given
7276 * the specifications by L1; It's not enough to take
7277 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
7278 * have more bits than L1 expected.
7279 */
7280 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
7281 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
7282
7283 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
7284 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
7285
7286 /* shadow page tables on either EPT or shadow page tables */
7287 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
7288 kvm_mmu_reset_context(vcpu);
7289
7290 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
7291 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
7292}
7293
cd232ad0
NHE
7294/*
7295 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
7296 * for running an L2 nested guest.
7297 */
7298static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
7299{
7300 struct vmcs12 *vmcs12;
7301 struct vcpu_vmx *vmx = to_vmx(vcpu);
7302 int cpu;
7303 struct loaded_vmcs *vmcs02;
7304
7305 if (!nested_vmx_check_permission(vcpu) ||
7306 !nested_vmx_check_vmcs12(vcpu))
7307 return 1;
7308
7309 skip_emulated_instruction(vcpu);
7310 vmcs12 = get_vmcs12(vcpu);
7311
7c177938
NHE
7312 /*
7313 * The nested entry process starts with enforcing various prerequisites
7314 * on vmcs12 as required by the Intel SDM, and act appropriately when
7315 * they fail: As the SDM explains, some conditions should cause the
7316 * instruction to fail, while others will cause the instruction to seem
7317 * to succeed, but return an EXIT_REASON_INVALID_STATE.
7318 * To speed up the normal (success) code path, we should avoid checking
7319 * for misconfigurations which will anyway be caught by the processor
7320 * when using the merged vmcs02.
7321 */
7322 if (vmcs12->launch_state == launch) {
7323 nested_vmx_failValid(vcpu,
7324 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
7325 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
7326 return 1;
7327 }
7328
26539bd0
PB
7329 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE) {
7330 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7331 return 1;
7332 }
7333
7c177938
NHE
7334 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
7335 !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
7336 /*TODO: Also verify bits beyond physical address width are 0*/
7337 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7338 return 1;
7339 }
7340
7341 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
7342 !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
7343 /*TODO: Also verify bits beyond physical address width are 0*/
7344 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7345 return 1;
7346 }
7347
7348 if (vmcs12->vm_entry_msr_load_count > 0 ||
7349 vmcs12->vm_exit_msr_load_count > 0 ||
7350 vmcs12->vm_exit_msr_store_count > 0) {
bd80158a
JK
7351 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
7352 __func__);
7c177938
NHE
7353 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7354 return 1;
7355 }
7356
7357 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
7358 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
7359 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
7360 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
7361 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
7362 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
7363 !vmx_control_verify(vmcs12->vm_exit_controls,
7364 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
7365 !vmx_control_verify(vmcs12->vm_entry_controls,
7366 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
7367 {
7368 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7369 return 1;
7370 }
7371
7372 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7373 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7374 nested_vmx_failValid(vcpu,
7375 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
7376 return 1;
7377 }
7378
7379 if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7380 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7381 nested_vmx_entry_failure(vcpu, vmcs12,
7382 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
7383 return 1;
7384 }
7385 if (vmcs12->vmcs_link_pointer != -1ull) {
7386 nested_vmx_entry_failure(vcpu, vmcs12,
7387 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
7388 return 1;
7389 }
7390
7391 /*
7392 * We're finally done with prerequisite checking, and can start with
7393 * the nested entry.
7394 */
7395
cd232ad0
NHE
7396 vmcs02 = nested_get_current_vmcs02(vmx);
7397 if (!vmcs02)
7398 return -ENOMEM;
7399
7400 enter_guest_mode(vcpu);
7401
7402 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
7403
7404 cpu = get_cpu();
7405 vmx->loaded_vmcs = vmcs02;
7406 vmx_vcpu_put(vcpu);
7407 vmx_vcpu_load(vcpu, cpu);
7408 vcpu->cpu = cpu;
7409 put_cpu();
7410
36c3cc42
JK
7411 vmx_segment_cache_clear(vmx);
7412
cd232ad0
NHE
7413 vmcs12->launch_state = 1;
7414
7415 prepare_vmcs02(vcpu, vmcs12);
7416
7417 /*
7418 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
7419 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
7420 * returned as far as L1 is concerned. It will only return (and set
7421 * the success flag) when L2 exits (see nested_vmx_vmexit()).
7422 */
7423 return 1;
7424}
7425
4704d0be
NHE
7426/*
7427 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
7428 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
7429 * This function returns the new value we should put in vmcs12.guest_cr0.
7430 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
7431 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
7432 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
7433 * didn't trap the bit, because if L1 did, so would L0).
7434 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
7435 * been modified by L2, and L1 knows it. So just leave the old value of
7436 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
7437 * isn't relevant, because if L0 traps this bit it can set it to anything.
7438 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
7439 * changed these bits, and therefore they need to be updated, but L0
7440 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
7441 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
7442 */
7443static inline unsigned long
7444vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7445{
7446 return
7447 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
7448 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
7449 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
7450 vcpu->arch.cr0_guest_owned_bits));
7451}
7452
7453static inline unsigned long
7454vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7455{
7456 return
7457 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
7458 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
7459 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
7460 vcpu->arch.cr4_guest_owned_bits));
7461}
7462
5f3d5799
JK
7463static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
7464 struct vmcs12 *vmcs12)
7465{
7466 u32 idt_vectoring;
7467 unsigned int nr;
7468
7469 if (vcpu->arch.exception.pending) {
7470 nr = vcpu->arch.exception.nr;
7471 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
7472
7473 if (kvm_exception_is_soft(nr)) {
7474 vmcs12->vm_exit_instruction_len =
7475 vcpu->arch.event_exit_inst_len;
7476 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
7477 } else
7478 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
7479
7480 if (vcpu->arch.exception.has_error_code) {
7481 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
7482 vmcs12->idt_vectoring_error_code =
7483 vcpu->arch.exception.error_code;
7484 }
7485
7486 vmcs12->idt_vectoring_info_field = idt_vectoring;
7487 } else if (vcpu->arch.nmi_pending) {
7488 vmcs12->idt_vectoring_info_field =
7489 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
7490 } else if (vcpu->arch.interrupt.pending) {
7491 nr = vcpu->arch.interrupt.nr;
7492 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
7493
7494 if (vcpu->arch.interrupt.soft) {
7495 idt_vectoring |= INTR_TYPE_SOFT_INTR;
7496 vmcs12->vm_entry_instruction_len =
7497 vcpu->arch.event_exit_inst_len;
7498 } else
7499 idt_vectoring |= INTR_TYPE_EXT_INTR;
7500
7501 vmcs12->idt_vectoring_info_field = idt_vectoring;
7502 }
7503}
7504
4704d0be
NHE
7505/*
7506 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
7507 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
7508 * and this function updates it to reflect the changes to the guest state while
7509 * L2 was running (and perhaps made some exits which were handled directly by L0
7510 * without going back to L1), and to reflect the exit reason.
7511 * Note that we do not have to copy here all VMCS fields, just those that
7512 * could have changed by the L2 guest or the exit - i.e., the guest-state and
7513 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
7514 * which already writes to vmcs12 directly.
7515 */
733568f9 7516static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4704d0be
NHE
7517{
7518 /* update guest state fields: */
7519 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
7520 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
7521
7522 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
7523 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
7524 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
7525 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
7526
7527 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
7528 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
7529 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
7530 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
7531 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
7532 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
7533 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
7534 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
7535 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
7536 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
7537 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
7538 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
7539 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
7540 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
7541 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
7542 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
7543 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
7544 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
7545 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
7546 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
7547 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
7548 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
7549 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
7550 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
7551 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
7552 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
7553 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
7554 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
7555 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
7556 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
7557 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
7558 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
7559 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
7560 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
7561 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
7562 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
7563
4704d0be
NHE
7564 vmcs12->guest_interruptibility_info =
7565 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
7566 vmcs12->guest_pending_dbg_exceptions =
7567 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
7568
c18911a2
JK
7569 vmcs12->vm_entry_controls =
7570 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
7571 (vmcs_read32(VM_ENTRY_CONTROLS) & VM_ENTRY_IA32E_MODE);
7572
4704d0be
NHE
7573 /* TODO: These cannot have changed unless we have MSR bitmaps and
7574 * the relevant bit asks not to trap the change */
7575 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
b8c07d55 7576 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
4704d0be
NHE
7577 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
7578 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
7579 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
7580 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
7581
7582 /* update exit information fields: */
7583
957c897e 7584 vmcs12->vm_exit_reason = to_vmx(vcpu)->exit_reason;
4704d0be
NHE
7585 vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7586
7587 vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
c0d1c770
JK
7588 if ((vmcs12->vm_exit_intr_info &
7589 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
7590 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
7591 vmcs12->vm_exit_intr_error_code =
7592 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5f3d5799 7593 vmcs12->idt_vectoring_info_field = 0;
4704d0be
NHE
7594 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
7595 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7596
5f3d5799
JK
7597 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
7598 /* vm_entry_intr_info_field is cleared on exit. Emulate this
7599 * instead of reading the real value. */
4704d0be 7600 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
5f3d5799
JK
7601
7602 /*
7603 * Transfer the event that L0 or L1 may wanted to inject into
7604 * L2 to IDT_VECTORING_INFO_FIELD.
7605 */
7606 vmcs12_save_pending_event(vcpu, vmcs12);
7607 }
7608
7609 /*
7610 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
7611 * preserved above and would only end up incorrectly in L1.
7612 */
7613 vcpu->arch.nmi_injected = false;
7614 kvm_clear_exception_queue(vcpu);
7615 kvm_clear_interrupt_queue(vcpu);
4704d0be
NHE
7616}
7617
7618/*
7619 * A part of what we need to when the nested L2 guest exits and we want to
7620 * run its L1 parent, is to reset L1's guest state to the host state specified
7621 * in vmcs12.
7622 * This function is to be called not only on normal nested exit, but also on
7623 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
7624 * Failures During or After Loading Guest State").
7625 * This function should be called when the active VMCS is L1's (vmcs01).
7626 */
733568f9
JK
7627static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
7628 struct vmcs12 *vmcs12)
4704d0be
NHE
7629{
7630 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
7631 vcpu->arch.efer = vmcs12->host_ia32_efer;
7632 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
7633 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7634 else
7635 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7636 vmx_set_efer(vcpu, vcpu->arch.efer);
7637
7638 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
7639 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
c4627c72 7640 vmx_set_rflags(vcpu, X86_EFLAGS_BIT1);
4704d0be
NHE
7641 /*
7642 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
7643 * actually changed, because it depends on the current state of
7644 * fpu_active (which may have changed).
7645 * Note that vmx_set_cr0 refers to efer set above.
7646 */
7647 kvm_set_cr0(vcpu, vmcs12->host_cr0);
7648 /*
7649 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
7650 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
7651 * but we also need to update cr0_guest_host_mask and exception_bitmap.
7652 */
7653 update_exception_bitmap(vcpu);
7654 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
7655 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7656
7657 /*
7658 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
7659 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
7660 */
7661 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
7662 kvm_set_cr4(vcpu, vmcs12->host_cr4);
7663
7664 /* shadow page tables on either EPT or shadow page tables */
7665 kvm_set_cr3(vcpu, vmcs12->host_cr3);
7666 kvm_mmu_reset_context(vcpu);
7667
7668 if (enable_vpid) {
7669 /*
7670 * Trivially support vpid by letting L2s share their parent
7671 * L1's vpid. TODO: move to a more elaborate solution, giving
7672 * each L2 its own vpid and exposing the vpid feature to L1.
7673 */
7674 vmx_flush_tlb(vcpu);
7675 }
7676
7677
7678 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
7679 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
7680 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
7681 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
7682 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
7683 vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base);
7684 vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base);
7685 vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base);
7686 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector);
7687 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector);
7688 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector);
7689 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector);
7690 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector);
7691 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector);
7692 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector);
7693
7694 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT)
7695 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
7696 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
7697 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
7698 vmcs12->host_ia32_perf_global_ctrl);
503cd0c5
JK
7699
7700 kvm_set_dr(vcpu, 7, 0x400);
7701 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4704d0be
NHE
7702}
7703
7704/*
7705 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
7706 * and modify vmcs12 to make it see what it would expect to see there if
7707 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
7708 */
7709static void nested_vmx_vmexit(struct kvm_vcpu *vcpu)
7710{
7711 struct vcpu_vmx *vmx = to_vmx(vcpu);
7712 int cpu;
7713 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7714
5f3d5799
JK
7715 /* trying to cancel vmlaunch/vmresume is a bug */
7716 WARN_ON_ONCE(vmx->nested.nested_run_pending);
7717
4704d0be
NHE
7718 leave_guest_mode(vcpu);
7719 prepare_vmcs12(vcpu, vmcs12);
7720
7721 cpu = get_cpu();
7722 vmx->loaded_vmcs = &vmx->vmcs01;
7723 vmx_vcpu_put(vcpu);
7724 vmx_vcpu_load(vcpu, cpu);
7725 vcpu->cpu = cpu;
7726 put_cpu();
7727
36c3cc42
JK
7728 vmx_segment_cache_clear(vmx);
7729
4704d0be
NHE
7730 /* if no vmcs02 cache requested, remove the one we used */
7731 if (VMCS02_POOL_SIZE == 0)
7732 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
7733
7734 load_vmcs12_host_state(vcpu, vmcs12);
7735
27fc51b2 7736 /* Update TSC_OFFSET if TSC was changed while L2 ran */
4704d0be
NHE
7737 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
7738
7739 /* This is needed for same reason as it was needed in prepare_vmcs02 */
7740 vmx->host_rsp = 0;
7741
7742 /* Unpin physical memory we referred to in vmcs02 */
7743 if (vmx->nested.apic_access_page) {
7744 nested_release_page(vmx->nested.apic_access_page);
7745 vmx->nested.apic_access_page = 0;
7746 }
7747
7748 /*
7749 * Exiting from L2 to L1, we're now back to L1 which thinks it just
7750 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
7751 * success or failure flag accordingly.
7752 */
7753 if (unlikely(vmx->fail)) {
7754 vmx->fail = 0;
7755 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
7756 } else
7757 nested_vmx_succeed(vcpu);
7758}
7759
7c177938
NHE
7760/*
7761 * L1's failure to enter L2 is a subset of a normal exit, as explained in
7762 * 23.7 "VM-entry failures during or after loading guest state" (this also
7763 * lists the acceptable exit-reason and exit-qualification parameters).
7764 * It should only be called before L2 actually succeeded to run, and when
7765 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
7766 */
7767static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
7768 struct vmcs12 *vmcs12,
7769 u32 reason, unsigned long qualification)
7770{
7771 load_vmcs12_host_state(vcpu, vmcs12);
7772 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
7773 vmcs12->exit_qualification = qualification;
7774 nested_vmx_succeed(vcpu);
7775}
7776
8a76d7f2
JR
7777static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7778 struct x86_instruction_info *info,
7779 enum x86_intercept_stage stage)
7780{
7781 return X86EMUL_CONTINUE;
7782}
7783
cbdd1bea 7784static struct kvm_x86_ops vmx_x86_ops = {
6aa8b732
AK
7785 .cpu_has_kvm_support = cpu_has_kvm_support,
7786 .disabled_by_bios = vmx_disabled_by_bios,
7787 .hardware_setup = hardware_setup,
7788 .hardware_unsetup = hardware_unsetup,
002c7f7c 7789 .check_processor_compatibility = vmx_check_processor_compat,
6aa8b732
AK
7790 .hardware_enable = hardware_enable,
7791 .hardware_disable = hardware_disable,
04547156 7792 .cpu_has_accelerated_tpr = report_flexpriority,
6aa8b732
AK
7793
7794 .vcpu_create = vmx_create_vcpu,
7795 .vcpu_free = vmx_free_vcpu,
04d2cc77 7796 .vcpu_reset = vmx_vcpu_reset,
6aa8b732 7797
04d2cc77 7798 .prepare_guest_switch = vmx_save_host_state,
6aa8b732
AK
7799 .vcpu_load = vmx_vcpu_load,
7800 .vcpu_put = vmx_vcpu_put,
7801
c8639010 7802 .update_db_bp_intercept = update_exception_bitmap,
6aa8b732
AK
7803 .get_msr = vmx_get_msr,
7804 .set_msr = vmx_set_msr,
7805 .get_segment_base = vmx_get_segment_base,
7806 .get_segment = vmx_get_segment,
7807 .set_segment = vmx_set_segment,
2e4d2653 7808 .get_cpl = vmx_get_cpl,
6aa8b732 7809 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
e8467fda 7810 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
aff48baa 7811 .decache_cr3 = vmx_decache_cr3,
25c4c276 7812 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
6aa8b732 7813 .set_cr0 = vmx_set_cr0,
6aa8b732
AK
7814 .set_cr3 = vmx_set_cr3,
7815 .set_cr4 = vmx_set_cr4,
6aa8b732 7816 .set_efer = vmx_set_efer,
6aa8b732
AK
7817 .get_idt = vmx_get_idt,
7818 .set_idt = vmx_set_idt,
7819 .get_gdt = vmx_get_gdt,
7820 .set_gdt = vmx_set_gdt,
020df079 7821 .set_dr7 = vmx_set_dr7,
5fdbf976 7822 .cache_reg = vmx_cache_reg,
6aa8b732
AK
7823 .get_rflags = vmx_get_rflags,
7824 .set_rflags = vmx_set_rflags,
ebcbab4c 7825 .fpu_activate = vmx_fpu_activate,
02daab21 7826 .fpu_deactivate = vmx_fpu_deactivate,
6aa8b732
AK
7827
7828 .tlb_flush = vmx_flush_tlb,
6aa8b732 7829
6aa8b732 7830 .run = vmx_vcpu_run,
6062d012 7831 .handle_exit = vmx_handle_exit,
6aa8b732 7832 .skip_emulated_instruction = skip_emulated_instruction,
2809f5d2
GC
7833 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7834 .get_interrupt_shadow = vmx_get_interrupt_shadow,
102d8325 7835 .patch_hypercall = vmx_patch_hypercall,
2a8067f1 7836 .set_irq = vmx_inject_irq,
95ba8273 7837 .set_nmi = vmx_inject_nmi,
298101da 7838 .queue_exception = vmx_queue_exception,
b463a6f7 7839 .cancel_injection = vmx_cancel_injection,
78646121 7840 .interrupt_allowed = vmx_interrupt_allowed,
95ba8273 7841 .nmi_allowed = vmx_nmi_allowed,
3cfc3092
JK
7842 .get_nmi_mask = vmx_get_nmi_mask,
7843 .set_nmi_mask = vmx_set_nmi_mask,
95ba8273
GN
7844 .enable_nmi_window = enable_nmi_window,
7845 .enable_irq_window = enable_irq_window,
7846 .update_cr8_intercept = update_cr8_intercept,
8d14695f 7847 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
c7c9c56c
YZ
7848 .vm_has_apicv = vmx_vm_has_apicv,
7849 .load_eoi_exitmap = vmx_load_eoi_exitmap,
7850 .hwapic_irr_update = vmx_hwapic_irr_update,
7851 .hwapic_isr_update = vmx_hwapic_isr_update,
a20ed54d
YZ
7852 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7853 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
95ba8273 7854
cbc94022 7855 .set_tss_addr = vmx_set_tss_addr,
67253af5 7856 .get_tdp_level = get_ept_level,
4b12f0de 7857 .get_mt_mask = vmx_get_mt_mask,
229456fc 7858
586f9607 7859 .get_exit_info = vmx_get_exit_info,
586f9607 7860
17cc3935 7861 .get_lpage_level = vmx_get_lpage_level,
0e851880
SY
7862
7863 .cpuid_update = vmx_cpuid_update,
4e47c7a6
SY
7864
7865 .rdtscp_supported = vmx_rdtscp_supported,
ad756a16 7866 .invpcid_supported = vmx_invpcid_supported,
d4330ef2
JR
7867
7868 .set_supported_cpuid = vmx_set_supported_cpuid,
f5f48ee1
SY
7869
7870 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
99e3e30a 7871
4051b188 7872 .set_tsc_khz = vmx_set_tsc_khz,
ba904635 7873 .read_tsc_offset = vmx_read_tsc_offset,
99e3e30a 7874 .write_tsc_offset = vmx_write_tsc_offset,
e48672fa 7875 .adjust_tsc_offset = vmx_adjust_tsc_offset,
857e4099 7876 .compute_tsc_offset = vmx_compute_tsc_offset,
d5c1785d 7877 .read_l1_tsc = vmx_read_l1_tsc,
1c97f0a0
JR
7878
7879 .set_tdp_cr3 = vmx_set_cr3,
8a76d7f2
JR
7880
7881 .check_intercept = vmx_check_intercept,
a547c6db 7882 .handle_external_intr = vmx_handle_external_intr,
6aa8b732
AK
7883};
7884
7885static int __init vmx_init(void)
7886{
8d14695f 7887 int r, i, msr;
26bb0981
AK
7888
7889 rdmsrl_safe(MSR_EFER, &host_efer);
7890
7891 for (i = 0; i < NR_VMX_MSR; ++i)
7892 kvm_define_shared_msr(i, vmx_msr_index[i]);
fdef3ad1 7893
3e7c73e9 7894 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
fdef3ad1
HQ
7895 if (!vmx_io_bitmap_a)
7896 return -ENOMEM;
7897
2106a548
GC
7898 r = -ENOMEM;
7899
3e7c73e9 7900 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
2106a548 7901 if (!vmx_io_bitmap_b)
fdef3ad1 7902 goto out;
fdef3ad1 7903
5897297b 7904 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
2106a548 7905 if (!vmx_msr_bitmap_legacy)
25c5f225 7906 goto out1;
2106a548 7907
8d14695f
YZ
7908 vmx_msr_bitmap_legacy_x2apic =
7909 (unsigned long *)__get_free_page(GFP_KERNEL);
7910 if (!vmx_msr_bitmap_legacy_x2apic)
7911 goto out2;
25c5f225 7912
5897297b 7913 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
2106a548 7914 if (!vmx_msr_bitmap_longmode)
8d14695f 7915 goto out3;
2106a548 7916
8d14695f
YZ
7917 vmx_msr_bitmap_longmode_x2apic =
7918 (unsigned long *)__get_free_page(GFP_KERNEL);
7919 if (!vmx_msr_bitmap_longmode_x2apic)
7920 goto out4;
5897297b 7921
fdef3ad1
HQ
7922 /*
7923 * Allow direct access to the PC debug port (it is often used for I/O
7924 * delays, but the vmexits simply slow things down).
7925 */
3e7c73e9
AK
7926 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
7927 clear_bit(0x80, vmx_io_bitmap_a);
fdef3ad1 7928
3e7c73e9 7929 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
fdef3ad1 7930
5897297b
AK
7931 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
7932 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
25c5f225 7933
2384d2b3
SY
7934 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7935
0ee75bea
AK
7936 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
7937 __alignof__(struct vcpu_vmx), THIS_MODULE);
fdef3ad1 7938 if (r)
458f212e 7939 goto out5;
25c5f225 7940
8f536b76
ZY
7941#ifdef CONFIG_KEXEC
7942 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
7943 crash_vmclear_local_loaded_vmcss);
7944#endif
7945
5897297b
AK
7946 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
7947 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
7948 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
7949 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
7950 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
7951 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
8d14695f
YZ
7952 memcpy(vmx_msr_bitmap_legacy_x2apic,
7953 vmx_msr_bitmap_legacy, PAGE_SIZE);
7954 memcpy(vmx_msr_bitmap_longmode_x2apic,
7955 vmx_msr_bitmap_longmode, PAGE_SIZE);
7956
01e439be 7957 if (enable_apicv) {
8d14695f
YZ
7958 for (msr = 0x800; msr <= 0x8ff; msr++)
7959 vmx_disable_intercept_msr_read_x2apic(msr);
7960
7961 /* According SDM, in x2apic mode, the whole id reg is used.
7962 * But in KVM, it only use the highest eight bits. Need to
7963 * intercept it */
7964 vmx_enable_intercept_msr_read_x2apic(0x802);
7965 /* TMCCT */
7966 vmx_enable_intercept_msr_read_x2apic(0x839);
7967 /* TPR */
7968 vmx_disable_intercept_msr_write_x2apic(0x808);
c7c9c56c
YZ
7969 /* EOI */
7970 vmx_disable_intercept_msr_write_x2apic(0x80b);
7971 /* SELF-IPI */
7972 vmx_disable_intercept_msr_write_x2apic(0x83f);
8d14695f 7973 }
fdef3ad1 7974
089d034e 7975 if (enable_ept) {
3f6d8c8a
XH
7976 kvm_mmu_set_mask_ptes(0ull,
7977 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
7978 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
7979 0ull, VMX_EPT_EXECUTABLE_MASK);
ce88decf 7980 ept_set_mmio_spte_mask();
5fdbcb9d
SY
7981 kvm_enable_tdp();
7982 } else
7983 kvm_disable_tdp();
1439442c 7984
fdef3ad1
HQ
7985 return 0;
7986
458f212e
YZ
7987out5:
7988 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
8d14695f 7989out4:
5897297b 7990 free_page((unsigned long)vmx_msr_bitmap_longmode);
8d14695f
YZ
7991out3:
7992 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
25c5f225 7993out2:
5897297b 7994 free_page((unsigned long)vmx_msr_bitmap_legacy);
fdef3ad1 7995out1:
3e7c73e9 7996 free_page((unsigned long)vmx_io_bitmap_b);
fdef3ad1 7997out:
3e7c73e9 7998 free_page((unsigned long)vmx_io_bitmap_a);
fdef3ad1 7999 return r;
6aa8b732
AK
8000}
8001
8002static void __exit vmx_exit(void)
8003{
8d14695f
YZ
8004 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
8005 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
5897297b
AK
8006 free_page((unsigned long)vmx_msr_bitmap_legacy);
8007 free_page((unsigned long)vmx_msr_bitmap_longmode);
3e7c73e9
AK
8008 free_page((unsigned long)vmx_io_bitmap_b);
8009 free_page((unsigned long)vmx_io_bitmap_a);
fdef3ad1 8010
8f536b76
ZY
8011#ifdef CONFIG_KEXEC
8012 rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL);
8013 synchronize_rcu();
8014#endif
8015
cb498ea2 8016 kvm_exit();
6aa8b732
AK
8017}
8018
8019module_init(vmx_init)
8020module_exit(vmx_exit)