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