]>
Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * | |
4 | * This module enables machines with Intel VT-x extensions to run virtual | |
5 | * machines without emulation or binary translation. | |
6 | * | |
7 | * Copyright (C) 2006 Qumranet, Inc. | |
9611c187 | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
6aa8b732 AK |
9 | * |
10 | * Authors: | |
11 | * Avi Kivity <avi@qumranet.com> | |
12 | * Yaniv Kamay <yaniv@qumranet.com> | |
13 | * | |
14 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
15 | * the COPYING file in the top-level directory. | |
16 | * | |
17 | */ | |
18 | ||
85f455f7 | 19 | #include "irq.h" |
1d737c8a | 20 | #include "mmu.h" |
00b27a3e | 21 | #include "cpuid.h" |
d62caabb | 22 | #include "lapic.h" |
e495606d | 23 | |
edf88417 | 24 | #include <linux/kvm_host.h> |
6aa8b732 | 25 | #include <linux/module.h> |
9d8f549d | 26 | #include <linux/kernel.h> |
6aa8b732 AK |
27 | #include <linux/mm.h> |
28 | #include <linux/highmem.h> | |
e8edc6e0 | 29 | #include <linux/sched.h> |
c7addb90 | 30 | #include <linux/moduleparam.h> |
e9bda3b3 | 31 | #include <linux/mod_devicetable.h> |
af658dca | 32 | #include <linux/trace_events.h> |
5a0e3ad6 | 33 | #include <linux/slab.h> |
cafd6659 | 34 | #include <linux/tboot.h> |
f4124500 | 35 | #include <linux/hrtimer.h> |
c207aee4 | 36 | #include <linux/frame.h> |
5fdbf976 | 37 | #include "kvm_cache_regs.h" |
35920a35 | 38 | #include "x86.h" |
e495606d | 39 | |
28b835d6 | 40 | #include <asm/cpu.h> |
6aa8b732 | 41 | #include <asm/io.h> |
3b3be0d1 | 42 | #include <asm/desc.h> |
13673a90 | 43 | #include <asm/vmx.h> |
6210e37b | 44 | #include <asm/virtext.h> |
a0861c02 | 45 | #include <asm/mce.h> |
952f07ec | 46 | #include <asm/fpu/internal.h> |
d7cd9796 | 47 | #include <asm/perf_event.h> |
81908bf4 | 48 | #include <asm/debugreg.h> |
8f536b76 | 49 | #include <asm/kexec.h> |
dab2087d | 50 | #include <asm/apic.h> |
efc64404 | 51 | #include <asm/irq_remapping.h> |
d6e41f11 | 52 | #include <asm/mmu_context.h> |
95502b8b | 53 | #include <asm/microcode.h> |
20b509bf | 54 | #include <asm/spec-ctrl.h> |
6aa8b732 | 55 | |
229456fc | 56 | #include "trace.h" |
25462f7f | 57 | #include "pmu.h" |
229456fc | 58 | |
4ecac3fd | 59 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
5e520e62 AK |
60 | #define __ex_clear(x, reg) \ |
61 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) | |
4ecac3fd | 62 | |
6aa8b732 AK |
63 | MODULE_AUTHOR("Qumranet"); |
64 | MODULE_LICENSE("GPL"); | |
65 | ||
e9bda3b3 JT |
66 | static const struct x86_cpu_id vmx_cpu_id[] = { |
67 | X86_FEATURE_MATCH(X86_FEATURE_VMX), | |
68 | {} | |
69 | }; | |
70 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); | |
71 | ||
476bc001 | 72 | static bool __read_mostly enable_vpid = 1; |
736caefe | 73 | module_param_named(vpid, enable_vpid, bool, 0444); |
2384d2b3 | 74 | |
476bc001 | 75 | static bool __read_mostly flexpriority_enabled = 1; |
736caefe | 76 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
4c9fc8ef | 77 | |
476bc001 | 78 | static bool __read_mostly enable_ept = 1; |
736caefe | 79 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
d56f546d | 80 | |
476bc001 | 81 | static bool __read_mostly enable_unrestricted_guest = 1; |
3a624e29 NK |
82 | module_param_named(unrestricted_guest, |
83 | enable_unrestricted_guest, bool, S_IRUGO); | |
84 | ||
83c3a331 XH |
85 | static bool __read_mostly enable_ept_ad_bits = 1; |
86 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); | |
87 | ||
a27685c3 | 88 | static bool __read_mostly emulate_invalid_guest_state = true; |
c1f8bc04 | 89 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
04fa4d32 | 90 | |
476bc001 | 91 | static bool __read_mostly fasteoi = 1; |
58fbbf26 KT |
92 | module_param(fasteoi, bool, S_IRUGO); |
93 | ||
5a71785d | 94 | static bool __read_mostly enable_apicv = 1; |
01e439be | 95 | module_param(enable_apicv, bool, S_IRUGO); |
83d4c286 | 96 | |
abc4fc58 AG |
97 | static bool __read_mostly enable_shadow_vmcs = 1; |
98 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); | |
801d3424 NHE |
99 | /* |
100 | * If nested=1, nested virtualization is supported, i.e., guests may use | |
101 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not | |
102 | * use VMX instructions. | |
103 | */ | |
476bc001 | 104 | static bool __read_mostly nested = 0; |
801d3424 NHE |
105 | module_param(nested, bool, S_IRUGO); |
106 | ||
20300099 WL |
107 | static u64 __read_mostly host_xss; |
108 | ||
843e4330 KH |
109 | static bool __read_mostly enable_pml = 1; |
110 | module_param_named(pml, enable_pml, bool, S_IRUGO); | |
111 | ||
64903d61 HZ |
112 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
113 | ||
64672c95 YJ |
114 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
115 | static int __read_mostly cpu_preemption_timer_multi; | |
116 | static bool __read_mostly enable_preemption_timer = 1; | |
117 | #ifdef CONFIG_X86_64 | |
118 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); | |
119 | #endif | |
120 | ||
5037878e GN |
121 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
122 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE) | |
cdc0e244 AK |
123 | #define KVM_VM_CR0_ALWAYS_ON \ |
124 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) | |
4c38609a AK |
125 | #define KVM_CR4_GUEST_OWNED_BITS \ |
126 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ | |
52ce3c21 | 127 | | X86_CR4_OSXMMEXCPT | X86_CR4_TSD) |
4c38609a | 128 | |
cdc0e244 AK |
129 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
130 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) | |
131 | ||
78ac8b47 AK |
132 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
133 | ||
f4124500 JK |
134 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
135 | ||
16c2aec6 JD |
136 | /* |
137 | * Hyper-V requires all of these, so mark them as supported even though | |
138 | * they are just treated the same as all-context. | |
139 | */ | |
140 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ | |
141 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ | |
142 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ | |
143 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ | |
144 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) | |
145 | ||
4b8d54f9 ZE |
146 | /* |
147 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: | |
148 | * ple_gap: upper bound on the amount of time between two successive | |
149 | * executions of PAUSE in a loop. Also indicate if ple enabled. | |
00c25bce | 150 | * According to test, this time is usually smaller than 128 cycles. |
4b8d54f9 ZE |
151 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
152 | * in a PAUSE loop. Tests indicate that most spinlocks are held for | |
153 | * less than 2^12 cycles | |
154 | * Time is measured based on a counter that runs at the same rate as the TSC, | |
155 | * refer SDM volume 3b section 21.6.13 & 22.1.3. | |
156 | */ | |
b4a2d31d RK |
157 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
158 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 | |
159 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 | |
160 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 | |
161 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ | |
162 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW | |
163 | ||
4b8d54f9 ZE |
164 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
165 | module_param(ple_gap, int, S_IRUGO); | |
166 | ||
167 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; | |
168 | module_param(ple_window, int, S_IRUGO); | |
169 | ||
b4a2d31d RK |
170 | /* Default doubles per-vcpu window every exit. */ |
171 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; | |
172 | module_param(ple_window_grow, int, S_IRUGO); | |
173 | ||
174 | /* Default resets per-vcpu window every exit to ple_window. */ | |
175 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; | |
176 | module_param(ple_window_shrink, int, S_IRUGO); | |
177 | ||
178 | /* Default is to compute the maximum so we can never overflow. */ | |
179 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; | |
180 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; | |
181 | module_param(ple_window_max, int, S_IRUGO); | |
182 | ||
83287ea4 AK |
183 | extern const ulong vmx_return; |
184 | ||
8bf00a52 | 185 | #define NR_AUTOLOAD_MSRS 8 |
ff2f6fe9 | 186 | #define VMCS02_POOL_SIZE 1 |
61d2ef2c | 187 | |
a2fa3e9f GH |
188 | struct vmcs { |
189 | u32 revision_id; | |
190 | u32 abort; | |
191 | char data[0]; | |
192 | }; | |
193 | ||
d462b819 NHE |
194 | /* |
195 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also | |
196 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs | |
197 | * loaded on this CPU (so we can clear them if the CPU goes down). | |
198 | */ | |
199 | struct loaded_vmcs { | |
200 | struct vmcs *vmcs; | |
355f4fb1 | 201 | struct vmcs *shadow_vmcs; |
d462b819 | 202 | int cpu; |
4c4a6f79 PB |
203 | bool launched; |
204 | bool nmi_known_unmasked; | |
85f01a30 LP |
205 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
206 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ | |
9338f80f PB |
207 | /* Support for vnmi-less CPUs */ |
208 | int soft_vnmi_blocked; | |
209 | ktime_t entry_time; | |
210 | s64 vnmi_blocked_time; | |
d462b819 NHE |
211 | struct list_head loaded_vmcss_on_cpu_link; |
212 | }; | |
213 | ||
26bb0981 AK |
214 | struct shared_msr_entry { |
215 | unsigned index; | |
216 | u64 data; | |
d5696725 | 217 | u64 mask; |
26bb0981 AK |
218 | }; |
219 | ||
a9d30f33 NHE |
220 | /* |
221 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a | |
222 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has | |
223 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is | |
224 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, | |
225 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. | |
226 | * More than one of these structures may exist, if L1 runs multiple L2 guests. | |
227 | * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the | |
228 | * underlying hardware which will be used to run L2. | |
229 | * This structure is packed to ensure that its layout is identical across | |
230 | * machines (necessary for live migration). | |
231 | * If there are changes in this struct, VMCS12_REVISION must be changed. | |
232 | */ | |
22bd0358 | 233 | typedef u64 natural_width; |
a9d30f33 NHE |
234 | struct __packed vmcs12 { |
235 | /* According to the Intel spec, a VMCS region must start with the | |
236 | * following two fields. Then follow implementation-specific data. | |
237 | */ | |
238 | u32 revision_id; | |
239 | u32 abort; | |
22bd0358 | 240 | |
27d6c865 NHE |
241 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
242 | u32 padding[7]; /* room for future expansion */ | |
243 | ||
22bd0358 NHE |
244 | u64 io_bitmap_a; |
245 | u64 io_bitmap_b; | |
246 | u64 msr_bitmap; | |
247 | u64 vm_exit_msr_store_addr; | |
248 | u64 vm_exit_msr_load_addr; | |
249 | u64 vm_entry_msr_load_addr; | |
250 | u64 tsc_offset; | |
251 | u64 virtual_apic_page_addr; | |
252 | u64 apic_access_addr; | |
705699a1 | 253 | u64 posted_intr_desc_addr; |
22bd0358 | 254 | u64 ept_pointer; |
608406e2 WV |
255 | u64 eoi_exit_bitmap0; |
256 | u64 eoi_exit_bitmap1; | |
257 | u64 eoi_exit_bitmap2; | |
258 | u64 eoi_exit_bitmap3; | |
81dc01f7 | 259 | u64 xss_exit_bitmap; |
22bd0358 NHE |
260 | u64 guest_physical_address; |
261 | u64 vmcs_link_pointer; | |
c5f983f6 | 262 | u64 pml_address; |
22bd0358 NHE |
263 | u64 guest_ia32_debugctl; |
264 | u64 guest_ia32_pat; | |
265 | u64 guest_ia32_efer; | |
266 | u64 guest_ia32_perf_global_ctrl; | |
267 | u64 guest_pdptr0; | |
268 | u64 guest_pdptr1; | |
269 | u64 guest_pdptr2; | |
270 | u64 guest_pdptr3; | |
36be0b9d | 271 | u64 guest_bndcfgs; |
22bd0358 NHE |
272 | u64 host_ia32_pat; |
273 | u64 host_ia32_efer; | |
274 | u64 host_ia32_perf_global_ctrl; | |
275 | u64 padding64[8]; /* room for future expansion */ | |
276 | /* | |
277 | * To allow migration of L1 (complete with its L2 guests) between | |
278 | * machines of different natural widths (32 or 64 bit), we cannot have | |
279 | * unsigned long fields with no explict size. We use u64 (aliased | |
280 | * natural_width) instead. Luckily, x86 is little-endian. | |
281 | */ | |
282 | natural_width cr0_guest_host_mask; | |
283 | natural_width cr4_guest_host_mask; | |
284 | natural_width cr0_read_shadow; | |
285 | natural_width cr4_read_shadow; | |
286 | natural_width cr3_target_value0; | |
287 | natural_width cr3_target_value1; | |
288 | natural_width cr3_target_value2; | |
289 | natural_width cr3_target_value3; | |
290 | natural_width exit_qualification; | |
291 | natural_width guest_linear_address; | |
292 | natural_width guest_cr0; | |
293 | natural_width guest_cr3; | |
294 | natural_width guest_cr4; | |
295 | natural_width guest_es_base; | |
296 | natural_width guest_cs_base; | |
297 | natural_width guest_ss_base; | |
298 | natural_width guest_ds_base; | |
299 | natural_width guest_fs_base; | |
300 | natural_width guest_gs_base; | |
301 | natural_width guest_ldtr_base; | |
302 | natural_width guest_tr_base; | |
303 | natural_width guest_gdtr_base; | |
304 | natural_width guest_idtr_base; | |
305 | natural_width guest_dr7; | |
306 | natural_width guest_rsp; | |
307 | natural_width guest_rip; | |
308 | natural_width guest_rflags; | |
309 | natural_width guest_pending_dbg_exceptions; | |
310 | natural_width guest_sysenter_esp; | |
311 | natural_width guest_sysenter_eip; | |
312 | natural_width host_cr0; | |
313 | natural_width host_cr3; | |
314 | natural_width host_cr4; | |
315 | natural_width host_fs_base; | |
316 | natural_width host_gs_base; | |
317 | natural_width host_tr_base; | |
318 | natural_width host_gdtr_base; | |
319 | natural_width host_idtr_base; | |
320 | natural_width host_ia32_sysenter_esp; | |
321 | natural_width host_ia32_sysenter_eip; | |
322 | natural_width host_rsp; | |
323 | natural_width host_rip; | |
324 | natural_width paddingl[8]; /* room for future expansion */ | |
325 | u32 pin_based_vm_exec_control; | |
326 | u32 cpu_based_vm_exec_control; | |
327 | u32 exception_bitmap; | |
328 | u32 page_fault_error_code_mask; | |
329 | u32 page_fault_error_code_match; | |
330 | u32 cr3_target_count; | |
331 | u32 vm_exit_controls; | |
332 | u32 vm_exit_msr_store_count; | |
333 | u32 vm_exit_msr_load_count; | |
334 | u32 vm_entry_controls; | |
335 | u32 vm_entry_msr_load_count; | |
336 | u32 vm_entry_intr_info_field; | |
337 | u32 vm_entry_exception_error_code; | |
338 | u32 vm_entry_instruction_len; | |
339 | u32 tpr_threshold; | |
340 | u32 secondary_vm_exec_control; | |
341 | u32 vm_instruction_error; | |
342 | u32 vm_exit_reason; | |
343 | u32 vm_exit_intr_info; | |
344 | u32 vm_exit_intr_error_code; | |
345 | u32 idt_vectoring_info_field; | |
346 | u32 idt_vectoring_error_code; | |
347 | u32 vm_exit_instruction_len; | |
348 | u32 vmx_instruction_info; | |
349 | u32 guest_es_limit; | |
350 | u32 guest_cs_limit; | |
351 | u32 guest_ss_limit; | |
352 | u32 guest_ds_limit; | |
353 | u32 guest_fs_limit; | |
354 | u32 guest_gs_limit; | |
355 | u32 guest_ldtr_limit; | |
356 | u32 guest_tr_limit; | |
357 | u32 guest_gdtr_limit; | |
358 | u32 guest_idtr_limit; | |
359 | u32 guest_es_ar_bytes; | |
360 | u32 guest_cs_ar_bytes; | |
361 | u32 guest_ss_ar_bytes; | |
362 | u32 guest_ds_ar_bytes; | |
363 | u32 guest_fs_ar_bytes; | |
364 | u32 guest_gs_ar_bytes; | |
365 | u32 guest_ldtr_ar_bytes; | |
366 | u32 guest_tr_ar_bytes; | |
367 | u32 guest_interruptibility_info; | |
368 | u32 guest_activity_state; | |
369 | u32 guest_sysenter_cs; | |
370 | u32 host_ia32_sysenter_cs; | |
0238ea91 JK |
371 | u32 vmx_preemption_timer_value; |
372 | u32 padding32[7]; /* room for future expansion */ | |
22bd0358 | 373 | u16 virtual_processor_id; |
705699a1 | 374 | u16 posted_intr_nv; |
22bd0358 NHE |
375 | u16 guest_es_selector; |
376 | u16 guest_cs_selector; | |
377 | u16 guest_ss_selector; | |
378 | u16 guest_ds_selector; | |
379 | u16 guest_fs_selector; | |
380 | u16 guest_gs_selector; | |
381 | u16 guest_ldtr_selector; | |
382 | u16 guest_tr_selector; | |
608406e2 | 383 | u16 guest_intr_status; |
c5f983f6 | 384 | u16 guest_pml_index; |
22bd0358 NHE |
385 | u16 host_es_selector; |
386 | u16 host_cs_selector; | |
387 | u16 host_ss_selector; | |
388 | u16 host_ds_selector; | |
389 | u16 host_fs_selector; | |
390 | u16 host_gs_selector; | |
391 | u16 host_tr_selector; | |
a9d30f33 NHE |
392 | }; |
393 | ||
394 | /* | |
395 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or | |
396 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and | |
397 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. | |
398 | */ | |
399 | #define VMCS12_REVISION 0x11e57ed0 | |
400 | ||
401 | /* | |
402 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region | |
403 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the | |
404 | * current implementation, 4K are reserved to avoid future complications. | |
405 | */ | |
406 | #define VMCS12_SIZE 0x1000 | |
407 | ||
ff2f6fe9 NHE |
408 | /* Used to remember the last vmcs02 used for some recently used vmcs12s */ |
409 | struct vmcs02_list { | |
410 | struct list_head list; | |
411 | gpa_t vmptr; | |
412 | struct loaded_vmcs vmcs02; | |
413 | }; | |
414 | ||
ec378aee NHE |
415 | /* |
416 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need | |
417 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. | |
418 | */ | |
419 | struct nested_vmx { | |
420 | /* Has the level1 guest done vmxon? */ | |
421 | bool vmxon; | |
3573e22c | 422 | gpa_t vmxon_ptr; |
c5f983f6 | 423 | bool pml_full; |
a9d30f33 NHE |
424 | |
425 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ | |
426 | gpa_t current_vmptr; | |
4f2777bc DM |
427 | /* |
428 | * Cache of the guest's VMCS, existing outside of guest memory. | |
429 | * Loaded from guest memory during VMPTRLD. Flushed to guest | |
8ca44e88 | 430 | * memory during VMCLEAR and VMPTRLD. |
4f2777bc DM |
431 | */ |
432 | struct vmcs12 *cached_vmcs12; | |
012f83cb AG |
433 | /* |
434 | * Indicates if the shadow vmcs must be updated with the | |
435 | * data hold by vmcs12 | |
436 | */ | |
437 | bool sync_shadow_vmcs; | |
ff2f6fe9 NHE |
438 | |
439 | /* vmcs02_list cache of VMCSs recently used to run L2 guests */ | |
440 | struct list_head vmcs02_pool; | |
441 | int vmcs02_num; | |
dccbfcf5 | 442 | bool change_vmcs01_virtual_x2apic_mode; |
644d711a NHE |
443 | /* L2 must run next, and mustn't decide to exit to L1. */ |
444 | bool nested_run_pending; | |
fe3ef05c NHE |
445 | /* |
446 | * Guest pages referred to in vmcs02 with host-physical pointers, so | |
447 | * we must keep them pinned while L2 runs. | |
448 | */ | |
449 | struct page *apic_access_page; | |
a7c0b07d | 450 | struct page *virtual_apic_page; |
705699a1 WV |
451 | struct page *pi_desc_page; |
452 | struct pi_desc *pi_desc; | |
453 | bool pi_pending; | |
454 | u16 posted_intr_nv; | |
f4124500 | 455 | |
d048c098 RK |
456 | unsigned long *msr_bitmap; |
457 | ||
f4124500 JK |
458 | struct hrtimer preemption_timer; |
459 | bool preemption_timer_expired; | |
2996fca0 JK |
460 | |
461 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ | |
462 | u64 vmcs01_debugctl; | |
b9c237bb | 463 | |
5c614b35 WL |
464 | u16 vpid02; |
465 | u16 last_vpid; | |
466 | ||
0115f9cb DM |
467 | /* |
468 | * We only store the "true" versions of the VMX capability MSRs. We | |
469 | * generate the "non-true" versions by setting the must-be-1 bits | |
470 | * according to the SDM. | |
471 | */ | |
b9c237bb WV |
472 | u32 nested_vmx_procbased_ctls_low; |
473 | u32 nested_vmx_procbased_ctls_high; | |
b9c237bb WV |
474 | u32 nested_vmx_secondary_ctls_low; |
475 | u32 nested_vmx_secondary_ctls_high; | |
476 | u32 nested_vmx_pinbased_ctls_low; | |
477 | u32 nested_vmx_pinbased_ctls_high; | |
478 | u32 nested_vmx_exit_ctls_low; | |
479 | u32 nested_vmx_exit_ctls_high; | |
b9c237bb WV |
480 | u32 nested_vmx_entry_ctls_low; |
481 | u32 nested_vmx_entry_ctls_high; | |
b9c237bb WV |
482 | u32 nested_vmx_misc_low; |
483 | u32 nested_vmx_misc_high; | |
484 | u32 nested_vmx_ept_caps; | |
99b83ac8 | 485 | u32 nested_vmx_vpid_caps; |
62cc6b9d DM |
486 | u64 nested_vmx_basic; |
487 | u64 nested_vmx_cr0_fixed0; | |
488 | u64 nested_vmx_cr0_fixed1; | |
489 | u64 nested_vmx_cr4_fixed0; | |
490 | u64 nested_vmx_cr4_fixed1; | |
491 | u64 nested_vmx_vmcs_enum; | |
ec378aee NHE |
492 | }; |
493 | ||
01e439be | 494 | #define POSTED_INTR_ON 0 |
ebbfc765 FW |
495 | #define POSTED_INTR_SN 1 |
496 | ||
01e439be YZ |
497 | /* Posted-Interrupt Descriptor */ |
498 | struct pi_desc { | |
499 | u32 pir[8]; /* Posted interrupt requested */ | |
6ef1522f FW |
500 | union { |
501 | struct { | |
502 | /* bit 256 - Outstanding Notification */ | |
503 | u16 on : 1, | |
504 | /* bit 257 - Suppress Notification */ | |
505 | sn : 1, | |
506 | /* bit 271:258 - Reserved */ | |
507 | rsvd_1 : 14; | |
508 | /* bit 279:272 - Notification Vector */ | |
509 | u8 nv; | |
510 | /* bit 287:280 - Reserved */ | |
511 | u8 rsvd_2; | |
512 | /* bit 319:288 - Notification Destination */ | |
513 | u32 ndst; | |
514 | }; | |
515 | u64 control; | |
516 | }; | |
517 | u32 rsvd[6]; | |
01e439be YZ |
518 | } __aligned(64); |
519 | ||
a20ed54d YZ |
520 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
521 | { | |
522 | return test_and_set_bit(POSTED_INTR_ON, | |
523 | (unsigned long *)&pi_desc->control); | |
524 | } | |
525 | ||
526 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) | |
527 | { | |
528 | return test_and_clear_bit(POSTED_INTR_ON, | |
529 | (unsigned long *)&pi_desc->control); | |
530 | } | |
531 | ||
532 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) | |
533 | { | |
534 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); | |
535 | } | |
536 | ||
ebbfc765 FW |
537 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
538 | { | |
539 | return clear_bit(POSTED_INTR_SN, | |
540 | (unsigned long *)&pi_desc->control); | |
541 | } | |
542 | ||
543 | static inline void pi_set_sn(struct pi_desc *pi_desc) | |
544 | { | |
545 | return set_bit(POSTED_INTR_SN, | |
546 | (unsigned long *)&pi_desc->control); | |
547 | } | |
548 | ||
ad361091 PB |
549 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
550 | { | |
551 | clear_bit(POSTED_INTR_ON, | |
552 | (unsigned long *)&pi_desc->control); | |
553 | } | |
554 | ||
ebbfc765 FW |
555 | static inline int pi_test_on(struct pi_desc *pi_desc) |
556 | { | |
557 | return test_bit(POSTED_INTR_ON, | |
558 | (unsigned long *)&pi_desc->control); | |
559 | } | |
560 | ||
561 | static inline int pi_test_sn(struct pi_desc *pi_desc) | |
562 | { | |
563 | return test_bit(POSTED_INTR_SN, | |
564 | (unsigned long *)&pi_desc->control); | |
565 | } | |
566 | ||
a2fa3e9f | 567 | struct vcpu_vmx { |
fb3f0f51 | 568 | struct kvm_vcpu vcpu; |
313dbd49 | 569 | unsigned long host_rsp; |
29bd8a78 | 570 | u8 fail; |
51aa01d1 | 571 | u32 exit_intr_info; |
1155f76a | 572 | u32 idt_vectoring_info; |
6de12732 | 573 | ulong rflags; |
26bb0981 | 574 | struct shared_msr_entry *guest_msrs; |
a2fa3e9f GH |
575 | int nmsrs; |
576 | int save_nmsrs; | |
a547c6db | 577 | unsigned long host_idt_base; |
a2fa3e9f | 578 | #ifdef CONFIG_X86_64 |
44ea2b17 AK |
579 | u64 msr_host_kernel_gs_base; |
580 | u64 msr_guest_kernel_gs_base; | |
a2fa3e9f | 581 | #endif |
2961e876 GN |
582 | u32 vm_entry_controls_shadow; |
583 | u32 vm_exit_controls_shadow; | |
d462b819 NHE |
584 | /* |
585 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a | |
586 | * non-nested (L1) guest, it always points to vmcs01. For a nested | |
587 | * guest (L2), it points to a different VMCS. | |
588 | */ | |
589 | struct loaded_vmcs vmcs01; | |
590 | struct loaded_vmcs *loaded_vmcs; | |
591 | bool __launched; /* temporary, used in vmx_vcpu_run */ | |
61d2ef2c AK |
592 | struct msr_autoload { |
593 | unsigned nr; | |
594 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; | |
595 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; | |
596 | } msr_autoload; | |
a2fa3e9f GH |
597 | struct { |
598 | int loaded; | |
599 | u16 fs_sel, gs_sel, ldt_sel; | |
b2da15ac AK |
600 | #ifdef CONFIG_X86_64 |
601 | u16 ds_sel, es_sel; | |
602 | #endif | |
152d3f2f LV |
603 | int gs_ldt_reload_needed; |
604 | int fs_reload_needed; | |
da8999d3 | 605 | u64 msr_host_bndcfgs; |
d77c26fc | 606 | } host_state; |
9c8cba37 | 607 | struct { |
7ffd92c5 | 608 | int vm86_active; |
78ac8b47 | 609 | ulong save_rflags; |
f5f7b2fe AK |
610 | struct kvm_segment segs[8]; |
611 | } rmode; | |
612 | struct { | |
613 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ | |
7ffd92c5 AK |
614 | struct kvm_save_segment { |
615 | u16 selector; | |
616 | unsigned long base; | |
617 | u32 limit; | |
618 | u32 ar; | |
f5f7b2fe | 619 | } seg[8]; |
2fb92db1 | 620 | } segment_cache; |
2384d2b3 | 621 | int vpid; |
04fa4d32 | 622 | bool emulation_required; |
3b86cd99 | 623 | |
a0861c02 | 624 | u32 exit_reason; |
4e47c7a6 | 625 | |
01e439be YZ |
626 | /* Posted interrupt descriptor */ |
627 | struct pi_desc pi_desc; | |
628 | ||
ec378aee NHE |
629 | /* Support for a guest hypervisor (nested VMX) */ |
630 | struct nested_vmx nested; | |
a7653ecd RK |
631 | |
632 | /* Dynamic PLE window. */ | |
633 | int ple_window; | |
634 | bool ple_window_dirty; | |
843e4330 KH |
635 | |
636 | /* Support for PML */ | |
637 | #define PML_ENTITY_NUM 512 | |
638 | struct page *pml_pg; | |
2680d6da | 639 | |
64672c95 YJ |
640 | /* apic deadline value in host tsc */ |
641 | u64 hv_deadline_tsc; | |
642 | ||
2680d6da | 643 | u64 current_tsc_ratio; |
1be0e61c | 644 | |
1be0e61c | 645 | u32 host_pkru; |
3b84080b | 646 | |
37e4c997 HZ |
647 | /* |
648 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in | |
649 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included | |
650 | * in msr_ia32_feature_control_valid_bits. | |
651 | */ | |
3b84080b | 652 | u64 msr_ia32_feature_control; |
37e4c997 | 653 | u64 msr_ia32_feature_control_valid_bits; |
a2fa3e9f GH |
654 | }; |
655 | ||
2fb92db1 AK |
656 | enum segment_cache_field { |
657 | SEG_FIELD_SEL = 0, | |
658 | SEG_FIELD_BASE = 1, | |
659 | SEG_FIELD_LIMIT = 2, | |
660 | SEG_FIELD_AR = 3, | |
661 | ||
662 | SEG_FIELD_NR = 4 | |
663 | }; | |
664 | ||
a2fa3e9f GH |
665 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
666 | { | |
fb3f0f51 | 667 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
a2fa3e9f GH |
668 | } |
669 | ||
efc64404 FW |
670 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
671 | { | |
672 | return &(to_vmx(vcpu)->pi_desc); | |
673 | } | |
674 | ||
22bd0358 NHE |
675 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
676 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) | |
677 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ | |
678 | [number##_HIGH] = VMCS12_OFFSET(name)+4 | |
679 | ||
4607c2d7 | 680 | |
fe2b201b | 681 | static unsigned long shadow_read_only_fields[] = { |
4607c2d7 AG |
682 | /* |
683 | * We do NOT shadow fields that are modified when L0 | |
684 | * traps and emulates any vmx instruction (e.g. VMPTRLD, | |
685 | * VMXON...) executed by L1. | |
686 | * For example, VM_INSTRUCTION_ERROR is read | |
687 | * by L1 if a vmx instruction fails (part of the error path). | |
688 | * Note the code assumes this logic. If for some reason | |
689 | * we start shadowing these fields then we need to | |
690 | * force a shadow sync when L0 emulates vmx instructions | |
691 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified | |
692 | * by nested_vmx_failValid) | |
693 | */ | |
694 | VM_EXIT_REASON, | |
695 | VM_EXIT_INTR_INFO, | |
696 | VM_EXIT_INSTRUCTION_LEN, | |
697 | IDT_VECTORING_INFO_FIELD, | |
698 | IDT_VECTORING_ERROR_CODE, | |
699 | VM_EXIT_INTR_ERROR_CODE, | |
700 | EXIT_QUALIFICATION, | |
701 | GUEST_LINEAR_ADDRESS, | |
702 | GUEST_PHYSICAL_ADDRESS | |
703 | }; | |
fe2b201b | 704 | static int max_shadow_read_only_fields = |
4607c2d7 AG |
705 | ARRAY_SIZE(shadow_read_only_fields); |
706 | ||
fe2b201b | 707 | static unsigned long shadow_read_write_fields[] = { |
a7c0b07d | 708 | TPR_THRESHOLD, |
4607c2d7 AG |
709 | GUEST_RIP, |
710 | GUEST_RSP, | |
711 | GUEST_CR0, | |
712 | GUEST_CR3, | |
713 | GUEST_CR4, | |
714 | GUEST_INTERRUPTIBILITY_INFO, | |
715 | GUEST_RFLAGS, | |
716 | GUEST_CS_SELECTOR, | |
717 | GUEST_CS_AR_BYTES, | |
718 | GUEST_CS_LIMIT, | |
719 | GUEST_CS_BASE, | |
720 | GUEST_ES_BASE, | |
36be0b9d | 721 | GUEST_BNDCFGS, |
4607c2d7 AG |
722 | CR0_GUEST_HOST_MASK, |
723 | CR0_READ_SHADOW, | |
724 | CR4_READ_SHADOW, | |
725 | TSC_OFFSET, | |
726 | EXCEPTION_BITMAP, | |
727 | CPU_BASED_VM_EXEC_CONTROL, | |
728 | VM_ENTRY_EXCEPTION_ERROR_CODE, | |
729 | VM_ENTRY_INTR_INFO_FIELD, | |
730 | VM_ENTRY_INSTRUCTION_LEN, | |
731 | VM_ENTRY_EXCEPTION_ERROR_CODE, | |
732 | HOST_FS_BASE, | |
733 | HOST_GS_BASE, | |
734 | HOST_FS_SELECTOR, | |
735 | HOST_GS_SELECTOR | |
736 | }; | |
fe2b201b | 737 | static int max_shadow_read_write_fields = |
4607c2d7 AG |
738 | ARRAY_SIZE(shadow_read_write_fields); |
739 | ||
772e0318 | 740 | static const unsigned short vmcs_field_to_offset_table[] = { |
22bd0358 | 741 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
705699a1 | 742 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
22bd0358 NHE |
743 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
744 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), | |
745 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), | |
746 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), | |
747 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), | |
748 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), | |
749 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), | |
750 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), | |
608406e2 | 751 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
c5f983f6 | 752 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
22bd0358 NHE |
753 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
754 | FIELD(HOST_CS_SELECTOR, host_cs_selector), | |
755 | FIELD(HOST_SS_SELECTOR, host_ss_selector), | |
756 | FIELD(HOST_DS_SELECTOR, host_ds_selector), | |
757 | FIELD(HOST_FS_SELECTOR, host_fs_selector), | |
758 | FIELD(HOST_GS_SELECTOR, host_gs_selector), | |
759 | FIELD(HOST_TR_SELECTOR, host_tr_selector), | |
760 | FIELD64(IO_BITMAP_A, io_bitmap_a), | |
761 | FIELD64(IO_BITMAP_B, io_bitmap_b), | |
762 | FIELD64(MSR_BITMAP, msr_bitmap), | |
763 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), | |
764 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), | |
765 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), | |
766 | FIELD64(TSC_OFFSET, tsc_offset), | |
767 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), | |
768 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), | |
705699a1 | 769 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
22bd0358 | 770 | FIELD64(EPT_POINTER, ept_pointer), |
608406e2 WV |
771 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
772 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), | |
773 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), | |
774 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), | |
81dc01f7 | 775 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
22bd0358 NHE |
776 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
777 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), | |
c5f983f6 | 778 | FIELD64(PML_ADDRESS, pml_address), |
22bd0358 NHE |
779 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
780 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), | |
781 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), | |
782 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), | |
783 | FIELD64(GUEST_PDPTR0, guest_pdptr0), | |
784 | FIELD64(GUEST_PDPTR1, guest_pdptr1), | |
785 | FIELD64(GUEST_PDPTR2, guest_pdptr2), | |
786 | FIELD64(GUEST_PDPTR3, guest_pdptr3), | |
36be0b9d | 787 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
22bd0358 NHE |
788 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
789 | FIELD64(HOST_IA32_EFER, host_ia32_efer), | |
790 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), | |
791 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), | |
792 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), | |
793 | FIELD(EXCEPTION_BITMAP, exception_bitmap), | |
794 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), | |
795 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), | |
796 | FIELD(CR3_TARGET_COUNT, cr3_target_count), | |
797 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), | |
798 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), | |
799 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), | |
800 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), | |
801 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), | |
802 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), | |
803 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), | |
804 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), | |
805 | FIELD(TPR_THRESHOLD, tpr_threshold), | |
806 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), | |
807 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), | |
808 | FIELD(VM_EXIT_REASON, vm_exit_reason), | |
809 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), | |
810 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), | |
811 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), | |
812 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), | |
813 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), | |
814 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), | |
815 | FIELD(GUEST_ES_LIMIT, guest_es_limit), | |
816 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), | |
817 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), | |
818 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), | |
819 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), | |
820 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), | |
821 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), | |
822 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), | |
823 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), | |
824 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), | |
825 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), | |
826 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), | |
827 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), | |
828 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), | |
829 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), | |
830 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), | |
831 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), | |
832 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), | |
833 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), | |
834 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), | |
835 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), | |
836 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), | |
0238ea91 | 837 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
22bd0358 NHE |
838 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
839 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), | |
840 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), | |
841 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), | |
842 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), | |
843 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), | |
844 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), | |
845 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), | |
846 | FIELD(EXIT_QUALIFICATION, exit_qualification), | |
847 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), | |
848 | FIELD(GUEST_CR0, guest_cr0), | |
849 | FIELD(GUEST_CR3, guest_cr3), | |
850 | FIELD(GUEST_CR4, guest_cr4), | |
851 | FIELD(GUEST_ES_BASE, guest_es_base), | |
852 | FIELD(GUEST_CS_BASE, guest_cs_base), | |
853 | FIELD(GUEST_SS_BASE, guest_ss_base), | |
854 | FIELD(GUEST_DS_BASE, guest_ds_base), | |
855 | FIELD(GUEST_FS_BASE, guest_fs_base), | |
856 | FIELD(GUEST_GS_BASE, guest_gs_base), | |
857 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), | |
858 | FIELD(GUEST_TR_BASE, guest_tr_base), | |
859 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), | |
860 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), | |
861 | FIELD(GUEST_DR7, guest_dr7), | |
862 | FIELD(GUEST_RSP, guest_rsp), | |
863 | FIELD(GUEST_RIP, guest_rip), | |
864 | FIELD(GUEST_RFLAGS, guest_rflags), | |
865 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), | |
866 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), | |
867 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), | |
868 | FIELD(HOST_CR0, host_cr0), | |
869 | FIELD(HOST_CR3, host_cr3), | |
870 | FIELD(HOST_CR4, host_cr4), | |
871 | FIELD(HOST_FS_BASE, host_fs_base), | |
872 | FIELD(HOST_GS_BASE, host_gs_base), | |
873 | FIELD(HOST_TR_BASE, host_tr_base), | |
874 | FIELD(HOST_GDTR_BASE, host_gdtr_base), | |
875 | FIELD(HOST_IDTR_BASE, host_idtr_base), | |
876 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), | |
877 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), | |
878 | FIELD(HOST_RSP, host_rsp), | |
879 | FIELD(HOST_RIP, host_rip), | |
880 | }; | |
22bd0358 NHE |
881 | |
882 | static inline short vmcs_field_to_offset(unsigned long field) | |
883 | { | |
a2ae9df7 PB |
884 | BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX); |
885 | ||
886 | if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) || | |
887 | vmcs_field_to_offset_table[field] == 0) | |
888 | return -ENOENT; | |
889 | ||
22bd0358 NHE |
890 | return vmcs_field_to_offset_table[field]; |
891 | } | |
892 | ||
a9d30f33 NHE |
893 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
894 | { | |
4f2777bc | 895 | return to_vmx(vcpu)->nested.cached_vmcs12; |
a9d30f33 NHE |
896 | } |
897 | ||
898 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) | |
899 | { | |
54bf36aa | 900 | struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT); |
32cad84f | 901 | if (is_error_page(page)) |
a9d30f33 | 902 | return NULL; |
32cad84f | 903 | |
a9d30f33 NHE |
904 | return page; |
905 | } | |
906 | ||
907 | static void nested_release_page(struct page *page) | |
908 | { | |
909 | kvm_release_page_dirty(page); | |
910 | } | |
911 | ||
912 | static void nested_release_page_clean(struct page *page) | |
913 | { | |
914 | kvm_release_page_clean(page); | |
915 | } | |
916 | ||
995f00a6 | 917 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
bfd0a56b | 918 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
995f00a6 | 919 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
f53cd63c | 920 | static bool vmx_xsaves_supported(void); |
776e58ea | 921 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
b246dd5d OW |
922 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
923 | struct kvm_segment *var, int seg); | |
924 | static void vmx_get_segment(struct kvm_vcpu *vcpu, | |
925 | struct kvm_segment *var, int seg); | |
d99e4152 GN |
926 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
927 | static u32 vmx_segment_access_rights(struct kvm_segment *var); | |
c3114420 | 928 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx); |
16f5b903 | 929 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
a255d479 | 930 | static int alloc_identity_pagetable(struct kvm *kvm); |
b96fb439 PB |
931 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
932 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); | |
933 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, | |
934 | u16 error_code); | |
75880a01 | 935 | |
6aa8b732 AK |
936 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
937 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); | |
d462b819 NHE |
938 | /* |
939 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed | |
940 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. | |
941 | */ | |
942 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); | |
6aa8b732 | 943 | |
bf9f6ac8 FW |
944 | /* |
945 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we | |
946 | * can find which vCPU should be waken up. | |
947 | */ | |
948 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); | |
949 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); | |
950 | ||
23611332 RK |
951 | enum { |
952 | VMX_IO_BITMAP_A, | |
953 | VMX_IO_BITMAP_B, | |
954 | VMX_MSR_BITMAP_LEGACY, | |
955 | VMX_MSR_BITMAP_LONGMODE, | |
956 | VMX_MSR_BITMAP_LEGACY_X2APIC_APICV, | |
957 | VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV, | |
958 | VMX_MSR_BITMAP_LEGACY_X2APIC, | |
959 | VMX_MSR_BITMAP_LONGMODE_X2APIC, | |
960 | VMX_VMREAD_BITMAP, | |
961 | VMX_VMWRITE_BITMAP, | |
962 | VMX_BITMAP_NR | |
963 | }; | |
964 | ||
965 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; | |
966 | ||
967 | #define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A]) | |
968 | #define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B]) | |
969 | #define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY]) | |
970 | #define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE]) | |
971 | #define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV]) | |
972 | #define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV]) | |
973 | #define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC]) | |
974 | #define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC]) | |
975 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) | |
976 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) | |
fdef3ad1 | 977 | |
110312c8 | 978 | static bool cpu_has_load_ia32_efer; |
8bf00a52 | 979 | static bool cpu_has_load_perf_global_ctrl; |
110312c8 | 980 | |
2384d2b3 SY |
981 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
982 | static DEFINE_SPINLOCK(vmx_vpid_lock); | |
983 | ||
1c3d14fe | 984 | static struct vmcs_config { |
6aa8b732 AK |
985 | int size; |
986 | int order; | |
9ac7e3e8 | 987 | u32 basic_cap; |
6aa8b732 | 988 | u32 revision_id; |
1c3d14fe YS |
989 | u32 pin_based_exec_ctrl; |
990 | u32 cpu_based_exec_ctrl; | |
f78e0e2e | 991 | u32 cpu_based_2nd_exec_ctrl; |
1c3d14fe YS |
992 | u32 vmexit_ctrl; |
993 | u32 vmentry_ctrl; | |
994 | } vmcs_config; | |
6aa8b732 | 995 | |
efff9e53 | 996 | static struct vmx_capability { |
d56f546d SY |
997 | u32 ept; |
998 | u32 vpid; | |
999 | } vmx_capability; | |
1000 | ||
6aa8b732 AK |
1001 | #define VMX_SEGMENT_FIELD(seg) \ |
1002 | [VCPU_SREG_##seg] = { \ | |
1003 | .selector = GUEST_##seg##_SELECTOR, \ | |
1004 | .base = GUEST_##seg##_BASE, \ | |
1005 | .limit = GUEST_##seg##_LIMIT, \ | |
1006 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ | |
1007 | } | |
1008 | ||
772e0318 | 1009 | static const struct kvm_vmx_segment_field { |
6aa8b732 AK |
1010 | unsigned selector; |
1011 | unsigned base; | |
1012 | unsigned limit; | |
1013 | unsigned ar_bytes; | |
1014 | } kvm_vmx_segment_fields[] = { | |
1015 | VMX_SEGMENT_FIELD(CS), | |
1016 | VMX_SEGMENT_FIELD(DS), | |
1017 | VMX_SEGMENT_FIELD(ES), | |
1018 | VMX_SEGMENT_FIELD(FS), | |
1019 | VMX_SEGMENT_FIELD(GS), | |
1020 | VMX_SEGMENT_FIELD(SS), | |
1021 | VMX_SEGMENT_FIELD(TR), | |
1022 | VMX_SEGMENT_FIELD(LDTR), | |
1023 | }; | |
1024 | ||
26bb0981 AK |
1025 | static u64 host_efer; |
1026 | ||
6de4f3ad AK |
1027 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
1028 | ||
4d56c8a7 | 1029 | /* |
8c06585d | 1030 | * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it |
4d56c8a7 AK |
1031 | * away by decrementing the array size. |
1032 | */ | |
6aa8b732 | 1033 | static const u32 vmx_msr_index[] = { |
05b3e0c2 | 1034 | #ifdef CONFIG_X86_64 |
44ea2b17 | 1035 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
6aa8b732 | 1036 | #endif |
8c06585d | 1037 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
6aa8b732 | 1038 | }; |
6aa8b732 | 1039 | |
5bb16016 | 1040 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
6aa8b732 AK |
1041 | { |
1042 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1043 | INTR_INFO_VALID_MASK)) == | |
5bb16016 JK |
1044 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
1045 | } | |
1046 | ||
6f05485d JK |
1047 | static inline bool is_debug(u32 intr_info) |
1048 | { | |
1049 | return is_exception_n(intr_info, DB_VECTOR); | |
1050 | } | |
1051 | ||
1052 | static inline bool is_breakpoint(u32 intr_info) | |
1053 | { | |
1054 | return is_exception_n(intr_info, BP_VECTOR); | |
1055 | } | |
1056 | ||
5bb16016 JK |
1057 | static inline bool is_page_fault(u32 intr_info) |
1058 | { | |
1059 | return is_exception_n(intr_info, PF_VECTOR); | |
6aa8b732 AK |
1060 | } |
1061 | ||
31299944 | 1062 | static inline bool is_no_device(u32 intr_info) |
2ab455cc | 1063 | { |
5bb16016 | 1064 | return is_exception_n(intr_info, NM_VECTOR); |
2ab455cc AL |
1065 | } |
1066 | ||
31299944 | 1067 | static inline bool is_invalid_opcode(u32 intr_info) |
7aa81cc0 | 1068 | { |
5bb16016 | 1069 | return is_exception_n(intr_info, UD_VECTOR); |
7aa81cc0 AL |
1070 | } |
1071 | ||
31299944 | 1072 | static inline bool is_external_interrupt(u32 intr_info) |
6aa8b732 AK |
1073 | { |
1074 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
1075 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); | |
1076 | } | |
1077 | ||
31299944 | 1078 | static inline bool is_machine_check(u32 intr_info) |
a0861c02 AK |
1079 | { |
1080 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1081 | INTR_INFO_VALID_MASK)) == | |
1082 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); | |
1083 | } | |
1084 | ||
bde0d7f9 LT |
1085 | /* Undocumented: icebp/int1 */ |
1086 | static inline bool is_icebp(u32 intr_info) | |
1087 | { | |
1088 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
1089 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); | |
1090 | } | |
1091 | ||
31299944 | 1092 | static inline bool cpu_has_vmx_msr_bitmap(void) |
25c5f225 | 1093 | { |
04547156 | 1094 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
25c5f225 SY |
1095 | } |
1096 | ||
31299944 | 1097 | static inline bool cpu_has_vmx_tpr_shadow(void) |
6e5d865c | 1098 | { |
04547156 | 1099 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
6e5d865c YS |
1100 | } |
1101 | ||
35754c98 | 1102 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
6e5d865c | 1103 | { |
35754c98 | 1104 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
6e5d865c YS |
1105 | } |
1106 | ||
31299944 | 1107 | static inline bool cpu_has_secondary_exec_ctrls(void) |
f78e0e2e | 1108 | { |
04547156 SY |
1109 | return vmcs_config.cpu_based_exec_ctrl & |
1110 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
f78e0e2e SY |
1111 | } |
1112 | ||
774ead3a | 1113 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
f78e0e2e | 1114 | { |
04547156 SY |
1115 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1116 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
1117 | } | |
1118 | ||
8d14695f YZ |
1119 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
1120 | { | |
1121 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1122 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
1123 | } | |
1124 | ||
83d4c286 YZ |
1125 | static inline bool cpu_has_vmx_apic_register_virt(void) |
1126 | { | |
1127 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1128 | SECONDARY_EXEC_APIC_REGISTER_VIRT; | |
1129 | } | |
1130 | ||
c7c9c56c YZ |
1131 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
1132 | { | |
1133 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1134 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; | |
1135 | } | |
1136 | ||
64672c95 YJ |
1137 | /* |
1138 | * Comment's format: document - errata name - stepping - processor name. | |
1139 | * Refer from | |
1140 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp | |
1141 | */ | |
1142 | static u32 vmx_preemption_cpu_tfms[] = { | |
1143 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ | |
1144 | 0x000206E6, | |
1145 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ | |
1146 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ | |
1147 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1148 | 0x00020652, | |
1149 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1150 | 0x00020655, | |
1151 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ | |
1152 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ | |
1153 | /* | |
1154 | * 320767.pdf - AAP86 - B1 - | |
1155 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile | |
1156 | */ | |
1157 | 0x000106E5, | |
1158 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ | |
1159 | 0x000106A0, | |
1160 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ | |
1161 | 0x000106A1, | |
1162 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ | |
1163 | 0x000106A4, | |
1164 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ | |
1165 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ | |
1166 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ | |
1167 | 0x000106A5, | |
1168 | }; | |
1169 | ||
1170 | static inline bool cpu_has_broken_vmx_preemption_timer(void) | |
1171 | { | |
1172 | u32 eax = cpuid_eax(0x00000001), i; | |
1173 | ||
1174 | /* Clear the reserved bits */ | |
1175 | eax &= ~(0x3U << 14 | 0xfU << 28); | |
03f6a22a | 1176 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
64672c95 YJ |
1177 | if (eax == vmx_preemption_cpu_tfms[i]) |
1178 | return true; | |
1179 | ||
1180 | return false; | |
1181 | } | |
1182 | ||
1183 | static inline bool cpu_has_vmx_preemption_timer(void) | |
1184 | { | |
64672c95 YJ |
1185 | return vmcs_config.pin_based_exec_ctrl & |
1186 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1187 | } | |
1188 | ||
01e439be YZ |
1189 | static inline bool cpu_has_vmx_posted_intr(void) |
1190 | { | |
d6a858d1 PB |
1191 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
1192 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; | |
01e439be YZ |
1193 | } |
1194 | ||
1195 | static inline bool cpu_has_vmx_apicv(void) | |
1196 | { | |
1197 | return cpu_has_vmx_apic_register_virt() && | |
1198 | cpu_has_vmx_virtual_intr_delivery() && | |
1199 | cpu_has_vmx_posted_intr(); | |
1200 | } | |
1201 | ||
04547156 SY |
1202 | static inline bool cpu_has_vmx_flexpriority(void) |
1203 | { | |
1204 | return cpu_has_vmx_tpr_shadow() && | |
1205 | cpu_has_vmx_virtualize_apic_accesses(); | |
f78e0e2e SY |
1206 | } |
1207 | ||
e799794e MT |
1208 | static inline bool cpu_has_vmx_ept_execute_only(void) |
1209 | { | |
31299944 | 1210 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
e799794e MT |
1211 | } |
1212 | ||
e799794e MT |
1213 | static inline bool cpu_has_vmx_ept_2m_page(void) |
1214 | { | |
31299944 | 1215 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
e799794e MT |
1216 | } |
1217 | ||
878403b7 SY |
1218 | static inline bool cpu_has_vmx_ept_1g_page(void) |
1219 | { | |
31299944 | 1220 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
878403b7 SY |
1221 | } |
1222 | ||
4bc9b982 SY |
1223 | static inline bool cpu_has_vmx_ept_4levels(void) |
1224 | { | |
1225 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; | |
1226 | } | |
1227 | ||
83c3a331 XH |
1228 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
1229 | { | |
1230 | return vmx_capability.ept & VMX_EPT_AD_BIT; | |
1231 | } | |
1232 | ||
31299944 | 1233 | static inline bool cpu_has_vmx_invept_context(void) |
d56f546d | 1234 | { |
31299944 | 1235 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
d56f546d SY |
1236 | } |
1237 | ||
31299944 | 1238 | static inline bool cpu_has_vmx_invept_global(void) |
d56f546d | 1239 | { |
31299944 | 1240 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
d56f546d SY |
1241 | } |
1242 | ||
518c8aee GJ |
1243 | static inline bool cpu_has_vmx_invvpid_single(void) |
1244 | { | |
1245 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; | |
1246 | } | |
1247 | ||
b9d762fa GJ |
1248 | static inline bool cpu_has_vmx_invvpid_global(void) |
1249 | { | |
1250 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; | |
1251 | } | |
1252 | ||
08d839c4 WL |
1253 | static inline bool cpu_has_vmx_invvpid(void) |
1254 | { | |
1255 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; | |
1256 | } | |
1257 | ||
31299944 | 1258 | static inline bool cpu_has_vmx_ept(void) |
d56f546d | 1259 | { |
04547156 SY |
1260 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1261 | SECONDARY_EXEC_ENABLE_EPT; | |
d56f546d SY |
1262 | } |
1263 | ||
31299944 | 1264 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
3a624e29 NK |
1265 | { |
1266 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1267 | SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
1268 | } | |
1269 | ||
31299944 | 1270 | static inline bool cpu_has_vmx_ple(void) |
4b8d54f9 ZE |
1271 | { |
1272 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1273 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
1274 | } | |
1275 | ||
9ac7e3e8 JD |
1276 | static inline bool cpu_has_vmx_basic_inout(void) |
1277 | { | |
1278 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); | |
1279 | } | |
1280 | ||
35754c98 | 1281 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
f78e0e2e | 1282 | { |
35754c98 | 1283 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
f78e0e2e SY |
1284 | } |
1285 | ||
31299944 | 1286 | static inline bool cpu_has_vmx_vpid(void) |
2384d2b3 | 1287 | { |
04547156 SY |
1288 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1289 | SECONDARY_EXEC_ENABLE_VPID; | |
2384d2b3 SY |
1290 | } |
1291 | ||
31299944 | 1292 | static inline bool cpu_has_vmx_rdtscp(void) |
4e47c7a6 SY |
1293 | { |
1294 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1295 | SECONDARY_EXEC_RDTSCP; | |
1296 | } | |
1297 | ||
ad756a16 MJ |
1298 | static inline bool cpu_has_vmx_invpcid(void) |
1299 | { | |
1300 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1301 | SECONDARY_EXEC_ENABLE_INVPCID; | |
1302 | } | |
1303 | ||
9338f80f PB |
1304 | static inline bool cpu_has_virtual_nmis(void) |
1305 | { | |
1306 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; | |
1307 | } | |
1308 | ||
f5f48ee1 SY |
1309 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
1310 | { | |
1311 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1312 | SECONDARY_EXEC_WBINVD_EXITING; | |
1313 | } | |
1314 | ||
abc4fc58 AG |
1315 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
1316 | { | |
1317 | u64 vmx_msr; | |
1318 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
1319 | /* check if the cpu supports writing r/o exit information fields */ | |
1320 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) | |
1321 | return false; | |
1322 | ||
1323 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1324 | SECONDARY_EXEC_SHADOW_VMCS; | |
1325 | } | |
1326 | ||
843e4330 KH |
1327 | static inline bool cpu_has_vmx_pml(void) |
1328 | { | |
1329 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; | |
1330 | } | |
1331 | ||
64903d61 HZ |
1332 | static inline bool cpu_has_vmx_tsc_scaling(void) |
1333 | { | |
1334 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1335 | SECONDARY_EXEC_TSC_SCALING; | |
1336 | } | |
1337 | ||
04547156 SY |
1338 | static inline bool report_flexpriority(void) |
1339 | { | |
1340 | return flexpriority_enabled; | |
1341 | } | |
1342 | ||
c7c2c709 JM |
1343 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
1344 | { | |
1345 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low); | |
1346 | } | |
1347 | ||
fe3ef05c NHE |
1348 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
1349 | { | |
1350 | return vmcs12->cpu_based_vm_exec_control & bit; | |
1351 | } | |
1352 | ||
1353 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) | |
1354 | { | |
1355 | return (vmcs12->cpu_based_vm_exec_control & | |
1356 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && | |
1357 | (vmcs12->secondary_vm_exec_control & bit); | |
1358 | } | |
1359 | ||
f4124500 JK |
1360 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
1361 | { | |
1362 | return vmcs12->pin_based_vm_exec_control & | |
1363 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1364 | } | |
1365 | ||
155a97a3 NHE |
1366 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
1367 | { | |
1368 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); | |
1369 | } | |
1370 | ||
81dc01f7 WL |
1371 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
1372 | { | |
1373 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) && | |
1374 | vmx_xsaves_supported(); | |
1375 | } | |
1376 | ||
c5f983f6 BD |
1377 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
1378 | { | |
1379 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); | |
1380 | } | |
1381 | ||
f2b93280 WV |
1382 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
1383 | { | |
1384 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); | |
1385 | } | |
1386 | ||
5c614b35 WL |
1387 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
1388 | { | |
1389 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); | |
1390 | } | |
1391 | ||
82f0dd4b WV |
1392 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
1393 | { | |
1394 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); | |
1395 | } | |
1396 | ||
608406e2 WV |
1397 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
1398 | { | |
1399 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
1400 | } | |
1401 | ||
705699a1 WV |
1402 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
1403 | { | |
1404 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; | |
1405 | } | |
1406 | ||
ef85b673 | 1407 | static inline bool is_nmi(u32 intr_info) |
644d711a NHE |
1408 | { |
1409 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
ef85b673 | 1410 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
644d711a NHE |
1411 | } |
1412 | ||
533558bc JK |
1413 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
1414 | u32 exit_intr_info, | |
1415 | unsigned long exit_qualification); | |
7c177938 NHE |
1416 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
1417 | struct vmcs12 *vmcs12, | |
1418 | u32 reason, unsigned long qualification); | |
1419 | ||
8b9cf98c | 1420 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
7725f0ba AK |
1421 | { |
1422 | int i; | |
1423 | ||
a2fa3e9f | 1424 | for (i = 0; i < vmx->nmsrs; ++i) |
26bb0981 | 1425 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
a75beee6 ED |
1426 | return i; |
1427 | return -1; | |
1428 | } | |
1429 | ||
2384d2b3 SY |
1430 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
1431 | { | |
1432 | struct { | |
1433 | u64 vpid : 16; | |
1434 | u64 rsvd : 48; | |
1435 | u64 gva; | |
1436 | } operand = { vpid, 0, gva }; | |
1437 | ||
4ecac3fd | 1438 | asm volatile (__ex(ASM_VMX_INVVPID) |
2384d2b3 SY |
1439 | /* CF==1 or ZF==1 --> rc = -1 */ |
1440 | "; ja 1f ; ud2 ; 1:" | |
1441 | : : "a"(&operand), "c"(ext) : "cc", "memory"); | |
1442 | } | |
1443 | ||
1439442c SY |
1444 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
1445 | { | |
1446 | struct { | |
1447 | u64 eptp, gpa; | |
1448 | } operand = {eptp, gpa}; | |
1449 | ||
4ecac3fd | 1450 | asm volatile (__ex(ASM_VMX_INVEPT) |
1439442c SY |
1451 | /* CF==1 or ZF==1 --> rc = -1 */ |
1452 | "; ja 1f ; ud2 ; 1:\n" | |
1453 | : : "a" (&operand), "c" (ext) : "cc", "memory"); | |
1454 | } | |
1455 | ||
26bb0981 | 1456 | static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
a75beee6 ED |
1457 | { |
1458 | int i; | |
1459 | ||
8b9cf98c | 1460 | i = __find_msr_index(vmx, msr); |
a75beee6 | 1461 | if (i >= 0) |
a2fa3e9f | 1462 | return &vmx->guest_msrs[i]; |
8b6d44c7 | 1463 | return NULL; |
7725f0ba AK |
1464 | } |
1465 | ||
6aa8b732 AK |
1466 | static void vmcs_clear(struct vmcs *vmcs) |
1467 | { | |
1468 | u64 phys_addr = __pa(vmcs); | |
1469 | u8 error; | |
1470 | ||
4ecac3fd | 1471 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
16d8f72f | 1472 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
6aa8b732 AK |
1473 | : "cc", "memory"); |
1474 | if (error) | |
1475 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", | |
1476 | vmcs, phys_addr); | |
1477 | } | |
1478 | ||
d462b819 NHE |
1479 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
1480 | { | |
1481 | vmcs_clear(loaded_vmcs->vmcs); | |
355f4fb1 JM |
1482 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
1483 | vmcs_clear(loaded_vmcs->shadow_vmcs); | |
d462b819 NHE |
1484 | loaded_vmcs->cpu = -1; |
1485 | loaded_vmcs->launched = 0; | |
1486 | } | |
1487 | ||
7725b894 DX |
1488 | static void vmcs_load(struct vmcs *vmcs) |
1489 | { | |
1490 | u64 phys_addr = __pa(vmcs); | |
1491 | u8 error; | |
1492 | ||
1493 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" | |
16d8f72f | 1494 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
7725b894 DX |
1495 | : "cc", "memory"); |
1496 | if (error) | |
2844d849 | 1497 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
7725b894 | 1498 | vmcs, phys_addr); |
8f6a3159 | 1499 | |
7725b894 DX |
1500 | } |
1501 | ||
2965faa5 | 1502 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
1503 | /* |
1504 | * This bitmap is used to indicate whether the vmclear | |
1505 | * operation is enabled on all cpus. All disabled by | |
1506 | * default. | |
1507 | */ | |
1508 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; | |
1509 | ||
1510 | static inline void crash_enable_local_vmclear(int cpu) | |
1511 | { | |
1512 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1513 | } | |
1514 | ||
1515 | static inline void crash_disable_local_vmclear(int cpu) | |
1516 | { | |
1517 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1518 | } | |
1519 | ||
1520 | static inline int crash_local_vmclear_enabled(int cpu) | |
1521 | { | |
1522 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1523 | } | |
1524 | ||
1525 | static void crash_vmclear_local_loaded_vmcss(void) | |
1526 | { | |
1527 | int cpu = raw_smp_processor_id(); | |
1528 | struct loaded_vmcs *v; | |
1529 | ||
1530 | if (!crash_local_vmclear_enabled(cpu)) | |
1531 | return; | |
1532 | ||
1533 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), | |
1534 | loaded_vmcss_on_cpu_link) | |
1535 | vmcs_clear(v->vmcs); | |
1536 | } | |
1537 | #else | |
1538 | static inline void crash_enable_local_vmclear(int cpu) { } | |
1539 | static inline void crash_disable_local_vmclear(int cpu) { } | |
2965faa5 | 1540 | #endif /* CONFIG_KEXEC_CORE */ |
8f536b76 | 1541 | |
d462b819 | 1542 | static void __loaded_vmcs_clear(void *arg) |
6aa8b732 | 1543 | { |
d462b819 | 1544 | struct loaded_vmcs *loaded_vmcs = arg; |
d3b2c338 | 1545 | int cpu = raw_smp_processor_id(); |
6aa8b732 | 1546 | |
d462b819 NHE |
1547 | if (loaded_vmcs->cpu != cpu) |
1548 | return; /* vcpu migration can race with cpu offline */ | |
1549 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) | |
6aa8b732 | 1550 | per_cpu(current_vmcs, cpu) = NULL; |
8f536b76 | 1551 | crash_disable_local_vmclear(cpu); |
d462b819 | 1552 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
5a560f8b XG |
1553 | |
1554 | /* | |
1555 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link | |
1556 | * is before setting loaded_vmcs->vcpu to -1 which is done in | |
1557 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist | |
1558 | * then adds the vmcs into percpu list before it is deleted. | |
1559 | */ | |
1560 | smp_wmb(); | |
1561 | ||
d462b819 | 1562 | loaded_vmcs_init(loaded_vmcs); |
8f536b76 | 1563 | crash_enable_local_vmclear(cpu); |
6aa8b732 AK |
1564 | } |
1565 | ||
d462b819 | 1566 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
8d0be2b3 | 1567 | { |
e6c7d321 XG |
1568 | int cpu = loaded_vmcs->cpu; |
1569 | ||
1570 | if (cpu != -1) | |
1571 | smp_call_function_single(cpu, | |
1572 | __loaded_vmcs_clear, loaded_vmcs, 1); | |
8d0be2b3 AK |
1573 | } |
1574 | ||
dd5f5341 | 1575 | static inline void vpid_sync_vcpu_single(int vpid) |
2384d2b3 | 1576 | { |
dd5f5341 | 1577 | if (vpid == 0) |
2384d2b3 SY |
1578 | return; |
1579 | ||
518c8aee | 1580 | if (cpu_has_vmx_invvpid_single()) |
dd5f5341 | 1581 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
2384d2b3 SY |
1582 | } |
1583 | ||
b9d762fa GJ |
1584 | static inline void vpid_sync_vcpu_global(void) |
1585 | { | |
1586 | if (cpu_has_vmx_invvpid_global()) | |
1587 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); | |
1588 | } | |
1589 | ||
dd5f5341 | 1590 | static inline void vpid_sync_context(int vpid) |
b9d762fa GJ |
1591 | { |
1592 | if (cpu_has_vmx_invvpid_single()) | |
dd5f5341 | 1593 | vpid_sync_vcpu_single(vpid); |
b9d762fa GJ |
1594 | else |
1595 | vpid_sync_vcpu_global(); | |
1596 | } | |
1597 | ||
1439442c SY |
1598 | static inline void ept_sync_global(void) |
1599 | { | |
1600 | if (cpu_has_vmx_invept_global()) | |
1601 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); | |
1602 | } | |
1603 | ||
1604 | static inline void ept_sync_context(u64 eptp) | |
1605 | { | |
089d034e | 1606 | if (enable_ept) { |
1439442c SY |
1607 | if (cpu_has_vmx_invept_context()) |
1608 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); | |
1609 | else | |
1610 | ept_sync_global(); | |
1611 | } | |
1612 | } | |
1613 | ||
8a86aea9 PB |
1614 | static __always_inline void vmcs_check16(unsigned long field) |
1615 | { | |
1616 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1617 | "16-bit accessor invalid for 64-bit field"); | |
1618 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1619 | "16-bit accessor invalid for 64-bit high field"); | |
1620 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1621 | "16-bit accessor invalid for 32-bit high field"); | |
1622 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1623 | "16-bit accessor invalid for natural width field"); | |
1624 | } | |
1625 | ||
1626 | static __always_inline void vmcs_check32(unsigned long field) | |
1627 | { | |
1628 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1629 | "32-bit accessor invalid for 16-bit field"); | |
1630 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1631 | "32-bit accessor invalid for natural width field"); | |
1632 | } | |
1633 | ||
1634 | static __always_inline void vmcs_check64(unsigned long field) | |
1635 | { | |
1636 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1637 | "64-bit accessor invalid for 16-bit field"); | |
1638 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1639 | "64-bit accessor invalid for 64-bit high field"); | |
1640 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1641 | "64-bit accessor invalid for 32-bit field"); | |
1642 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1643 | "64-bit accessor invalid for natural width field"); | |
1644 | } | |
1645 | ||
1646 | static __always_inline void vmcs_checkl(unsigned long field) | |
1647 | { | |
1648 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1649 | "Natural width accessor invalid for 16-bit field"); | |
1650 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1651 | "Natural width accessor invalid for 64-bit field"); | |
1652 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1653 | "Natural width accessor invalid for 64-bit high field"); | |
1654 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1655 | "Natural width accessor invalid for 32-bit field"); | |
1656 | } | |
1657 | ||
1658 | static __always_inline unsigned long __vmcs_readl(unsigned long field) | |
6aa8b732 | 1659 | { |
5e520e62 | 1660 | unsigned long value; |
6aa8b732 | 1661 | |
5e520e62 AK |
1662 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
1663 | : "=a"(value) : "d"(field) : "cc"); | |
6aa8b732 AK |
1664 | return value; |
1665 | } | |
1666 | ||
96304217 | 1667 | static __always_inline u16 vmcs_read16(unsigned long field) |
6aa8b732 | 1668 | { |
8a86aea9 PB |
1669 | vmcs_check16(field); |
1670 | return __vmcs_readl(field); | |
6aa8b732 AK |
1671 | } |
1672 | ||
96304217 | 1673 | static __always_inline u32 vmcs_read32(unsigned long field) |
6aa8b732 | 1674 | { |
8a86aea9 PB |
1675 | vmcs_check32(field); |
1676 | return __vmcs_readl(field); | |
6aa8b732 AK |
1677 | } |
1678 | ||
96304217 | 1679 | static __always_inline u64 vmcs_read64(unsigned long field) |
6aa8b732 | 1680 | { |
8a86aea9 | 1681 | vmcs_check64(field); |
05b3e0c2 | 1682 | #ifdef CONFIG_X86_64 |
8a86aea9 | 1683 | return __vmcs_readl(field); |
6aa8b732 | 1684 | #else |
8a86aea9 | 1685 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
6aa8b732 AK |
1686 | #endif |
1687 | } | |
1688 | ||
8a86aea9 PB |
1689 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
1690 | { | |
1691 | vmcs_checkl(field); | |
1692 | return __vmcs_readl(field); | |
1693 | } | |
1694 | ||
e52de1b8 AK |
1695 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
1696 | { | |
1697 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", | |
1698 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); | |
1699 | dump_stack(); | |
1700 | } | |
1701 | ||
8a86aea9 | 1702 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
6aa8b732 AK |
1703 | { |
1704 | u8 error; | |
1705 | ||
4ecac3fd | 1706 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
d77c26fc | 1707 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
e52de1b8 AK |
1708 | if (unlikely(error)) |
1709 | vmwrite_error(field, value); | |
6aa8b732 AK |
1710 | } |
1711 | ||
8a86aea9 | 1712 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
6aa8b732 | 1713 | { |
8a86aea9 PB |
1714 | vmcs_check16(field); |
1715 | __vmcs_writel(field, value); | |
6aa8b732 AK |
1716 | } |
1717 | ||
8a86aea9 | 1718 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
6aa8b732 | 1719 | { |
8a86aea9 PB |
1720 | vmcs_check32(field); |
1721 | __vmcs_writel(field, value); | |
6aa8b732 AK |
1722 | } |
1723 | ||
8a86aea9 | 1724 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
6aa8b732 | 1725 | { |
8a86aea9 PB |
1726 | vmcs_check64(field); |
1727 | __vmcs_writel(field, value); | |
7682f2d0 | 1728 | #ifndef CONFIG_X86_64 |
6aa8b732 | 1729 | asm volatile (""); |
8a86aea9 | 1730 | __vmcs_writel(field+1, value >> 32); |
6aa8b732 AK |
1731 | #endif |
1732 | } | |
1733 | ||
8a86aea9 | 1734 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
2ab455cc | 1735 | { |
8a86aea9 PB |
1736 | vmcs_checkl(field); |
1737 | __vmcs_writel(field, value); | |
2ab455cc AL |
1738 | } |
1739 | ||
8a86aea9 | 1740 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
2ab455cc | 1741 | { |
8a86aea9 PB |
1742 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1743 | "vmcs_clear_bits does not support 64-bit fields"); | |
1744 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); | |
2ab455cc AL |
1745 | } |
1746 | ||
8a86aea9 | 1747 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
2ab455cc | 1748 | { |
8a86aea9 PB |
1749 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1750 | "vmcs_set_bits does not support 64-bit fields"); | |
1751 | __vmcs_writel(field, __vmcs_readl(field) | mask); | |
2ab455cc AL |
1752 | } |
1753 | ||
8391ce44 PB |
1754 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
1755 | { | |
1756 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); | |
1757 | } | |
1758 | ||
2961e876 GN |
1759 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
1760 | { | |
1761 | vmcs_write32(VM_ENTRY_CONTROLS, val); | |
1762 | vmx->vm_entry_controls_shadow = val; | |
1763 | } | |
1764 | ||
1765 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) | |
1766 | { | |
1767 | if (vmx->vm_entry_controls_shadow != val) | |
1768 | vm_entry_controls_init(vmx, val); | |
1769 | } | |
1770 | ||
1771 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) | |
1772 | { | |
1773 | return vmx->vm_entry_controls_shadow; | |
1774 | } | |
1775 | ||
1776 | ||
1777 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
1778 | { | |
1779 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); | |
1780 | } | |
1781 | ||
1782 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
1783 | { | |
1784 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); | |
1785 | } | |
1786 | ||
8391ce44 PB |
1787 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
1788 | { | |
1789 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); | |
1790 | } | |
1791 | ||
2961e876 GN |
1792 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
1793 | { | |
1794 | vmcs_write32(VM_EXIT_CONTROLS, val); | |
1795 | vmx->vm_exit_controls_shadow = val; | |
1796 | } | |
1797 | ||
1798 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) | |
1799 | { | |
1800 | if (vmx->vm_exit_controls_shadow != val) | |
1801 | vm_exit_controls_init(vmx, val); | |
1802 | } | |
1803 | ||
1804 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) | |
1805 | { | |
1806 | return vmx->vm_exit_controls_shadow; | |
1807 | } | |
1808 | ||
1809 | ||
1810 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
1811 | { | |
1812 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); | |
1813 | } | |
1814 | ||
1815 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
1816 | { | |
1817 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); | |
1818 | } | |
1819 | ||
2fb92db1 AK |
1820 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
1821 | { | |
1822 | vmx->segment_cache.bitmask = 0; | |
1823 | } | |
1824 | ||
1825 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, | |
1826 | unsigned field) | |
1827 | { | |
1828 | bool ret; | |
1829 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); | |
1830 | ||
1831 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { | |
1832 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); | |
1833 | vmx->segment_cache.bitmask = 0; | |
1834 | } | |
1835 | ret = vmx->segment_cache.bitmask & mask; | |
1836 | vmx->segment_cache.bitmask |= mask; | |
1837 | return ret; | |
1838 | } | |
1839 | ||
1840 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) | |
1841 | { | |
1842 | u16 *p = &vmx->segment_cache.seg[seg].selector; | |
1843 | ||
1844 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) | |
1845 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); | |
1846 | return *p; | |
1847 | } | |
1848 | ||
1849 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) | |
1850 | { | |
1851 | ulong *p = &vmx->segment_cache.seg[seg].base; | |
1852 | ||
1853 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) | |
1854 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); | |
1855 | return *p; | |
1856 | } | |
1857 | ||
1858 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) | |
1859 | { | |
1860 | u32 *p = &vmx->segment_cache.seg[seg].limit; | |
1861 | ||
1862 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) | |
1863 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); | |
1864 | return *p; | |
1865 | } | |
1866 | ||
1867 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) | |
1868 | { | |
1869 | u32 *p = &vmx->segment_cache.seg[seg].ar; | |
1870 | ||
1871 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) | |
1872 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); | |
1873 | return *p; | |
1874 | } | |
1875 | ||
abd3f2d6 AK |
1876 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
1877 | { | |
1878 | u32 eb; | |
1879 | ||
fd7373cc | 1880 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
bd7e5b08 | 1881 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
fd7373cc JK |
1882 | if ((vcpu->guest_debug & |
1883 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == | |
1884 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) | |
1885 | eb |= 1u << BP_VECTOR; | |
7ffd92c5 | 1886 | if (to_vmx(vcpu)->rmode.vm86_active) |
abd3f2d6 | 1887 | eb = ~0; |
089d034e | 1888 | if (enable_ept) |
1439442c | 1889 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
36cf24e0 NHE |
1890 | |
1891 | /* When we are running a nested L2 guest and L1 specified for it a | |
1892 | * certain exception bitmap, we must trap the same exceptions and pass | |
1893 | * them to L1. When running L2, we will only handle the exceptions | |
1894 | * specified above if L1 did not want them. | |
1895 | */ | |
1896 | if (is_guest_mode(vcpu)) | |
1897 | eb |= get_vmcs12(vcpu)->exception_bitmap; | |
1898 | ||
abd3f2d6 AK |
1899 | vmcs_write32(EXCEPTION_BITMAP, eb); |
1900 | } | |
1901 | ||
2961e876 GN |
1902 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
1903 | unsigned long entry, unsigned long exit) | |
8bf00a52 | 1904 | { |
2961e876 GN |
1905 | vm_entry_controls_clearbit(vmx, entry); |
1906 | vm_exit_controls_clearbit(vmx, exit); | |
8bf00a52 GN |
1907 | } |
1908 | ||
61d2ef2c AK |
1909 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
1910 | { | |
1911 | unsigned i; | |
1912 | struct msr_autoload *m = &vmx->msr_autoload; | |
1913 | ||
8bf00a52 GN |
1914 | switch (msr) { |
1915 | case MSR_EFER: | |
1916 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
1917 | clear_atomic_switch_msr_special(vmx, |
1918 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
1919 | VM_EXIT_LOAD_IA32_EFER); |
1920 | return; | |
1921 | } | |
1922 | break; | |
1923 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
1924 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 1925 | clear_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
1926 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
1927 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
1928 | return; | |
1929 | } | |
1930 | break; | |
110312c8 AK |
1931 | } |
1932 | ||
61d2ef2c AK |
1933 | for (i = 0; i < m->nr; ++i) |
1934 | if (m->guest[i].index == msr) | |
1935 | break; | |
1936 | ||
1937 | if (i == m->nr) | |
1938 | return; | |
1939 | --m->nr; | |
1940 | m->guest[i] = m->guest[m->nr]; | |
1941 | m->host[i] = m->host[m->nr]; | |
1942 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); | |
1943 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); | |
1944 | } | |
1945 | ||
2961e876 GN |
1946 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
1947 | unsigned long entry, unsigned long exit, | |
1948 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, | |
1949 | u64 guest_val, u64 host_val) | |
8bf00a52 GN |
1950 | { |
1951 | vmcs_write64(guest_val_vmcs, guest_val); | |
1952 | vmcs_write64(host_val_vmcs, host_val); | |
2961e876 GN |
1953 | vm_entry_controls_setbit(vmx, entry); |
1954 | vm_exit_controls_setbit(vmx, exit); | |
8bf00a52 GN |
1955 | } |
1956 | ||
61d2ef2c AK |
1957 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
1958 | u64 guest_val, u64 host_val) | |
1959 | { | |
1960 | unsigned i; | |
1961 | struct msr_autoload *m = &vmx->msr_autoload; | |
1962 | ||
8bf00a52 GN |
1963 | switch (msr) { |
1964 | case MSR_EFER: | |
1965 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
1966 | add_atomic_switch_msr_special(vmx, |
1967 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
1968 | VM_EXIT_LOAD_IA32_EFER, |
1969 | GUEST_IA32_EFER, | |
1970 | HOST_IA32_EFER, | |
1971 | guest_val, host_val); | |
1972 | return; | |
1973 | } | |
1974 | break; | |
1975 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
1976 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 1977 | add_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
1978 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
1979 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, | |
1980 | GUEST_IA32_PERF_GLOBAL_CTRL, | |
1981 | HOST_IA32_PERF_GLOBAL_CTRL, | |
1982 | guest_val, host_val); | |
1983 | return; | |
1984 | } | |
1985 | break; | |
7099e2e1 RK |
1986 | case MSR_IA32_PEBS_ENABLE: |
1987 | /* PEBS needs a quiescent period after being disabled (to write | |
1988 | * a record). Disabling PEBS through VMX MSR swapping doesn't | |
1989 | * provide that period, so a CPU could write host's record into | |
1990 | * guest's memory. | |
1991 | */ | |
1992 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); | |
110312c8 AK |
1993 | } |
1994 | ||
61d2ef2c AK |
1995 | for (i = 0; i < m->nr; ++i) |
1996 | if (m->guest[i].index == msr) | |
1997 | break; | |
1998 | ||
e7fc6f93 | 1999 | if (i == NR_AUTOLOAD_MSRS) { |
60266204 | 2000 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
e7fc6f93 GN |
2001 | "Can't add msr %x\n", msr); |
2002 | return; | |
2003 | } else if (i == m->nr) { | |
61d2ef2c AK |
2004 | ++m->nr; |
2005 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); | |
2006 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); | |
2007 | } | |
2008 | ||
2009 | m->guest[i].index = msr; | |
2010 | m->guest[i].value = guest_val; | |
2011 | m->host[i].index = msr; | |
2012 | m->host[i].value = host_val; | |
2013 | } | |
2014 | ||
92c0d900 | 2015 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
2cc51560 | 2016 | { |
844a5fe2 PB |
2017 | u64 guest_efer = vmx->vcpu.arch.efer; |
2018 | u64 ignore_bits = 0; | |
2019 | ||
2020 | if (!enable_ept) { | |
2021 | /* | |
2022 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing | |
2023 | * host CPUID is more efficient than testing guest CPUID | |
2024 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. | |
2025 | */ | |
2026 | if (boot_cpu_has(X86_FEATURE_SMEP)) | |
2027 | guest_efer |= EFER_NX; | |
2028 | else if (!(guest_efer & EFER_NX)) | |
2029 | ignore_bits |= EFER_NX; | |
2030 | } | |
3a34a881 | 2031 | |
51c6cf66 | 2032 | /* |
844a5fe2 | 2033 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
51c6cf66 | 2034 | */ |
844a5fe2 | 2035 | ignore_bits |= EFER_SCE; |
51c6cf66 AK |
2036 | #ifdef CONFIG_X86_64 |
2037 | ignore_bits |= EFER_LMA | EFER_LME; | |
2038 | /* SCE is meaningful only in long mode on Intel */ | |
2039 | if (guest_efer & EFER_LMA) | |
2040 | ignore_bits &= ~(u64)EFER_SCE; | |
2041 | #endif | |
84ad33ef AK |
2042 | |
2043 | clear_atomic_switch_msr(vmx, MSR_EFER); | |
f6577a5f AL |
2044 | |
2045 | /* | |
2046 | * On EPT, we can't emulate NX, so we must switch EFER atomically. | |
2047 | * On CPUs that support "load IA32_EFER", always switch EFER | |
2048 | * atomically, since it's faster than switching it manually. | |
2049 | */ | |
2050 | if (cpu_has_load_ia32_efer || | |
2051 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { | |
84ad33ef AK |
2052 | if (!(guest_efer & EFER_LMA)) |
2053 | guest_efer &= ~EFER_LME; | |
54b98bff AL |
2054 | if (guest_efer != host_efer) |
2055 | add_atomic_switch_msr(vmx, MSR_EFER, | |
2056 | guest_efer, host_efer); | |
84ad33ef | 2057 | return false; |
844a5fe2 PB |
2058 | } else { |
2059 | guest_efer &= ~ignore_bits; | |
2060 | guest_efer |= host_efer & ignore_bits; | |
2061 | ||
2062 | vmx->guest_msrs[efer_offset].data = guest_efer; | |
2063 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; | |
84ad33ef | 2064 | |
844a5fe2 PB |
2065 | return true; |
2066 | } | |
51c6cf66 AK |
2067 | } |
2068 | ||
e28baead AL |
2069 | #ifdef CONFIG_X86_32 |
2070 | /* | |
2071 | * On 32-bit kernels, VM exits still load the FS and GS bases from the | |
2072 | * VMCS rather than the segment table. KVM uses this helper to figure | |
2073 | * out the current bases to poke them into the VMCS before entry. | |
2074 | */ | |
2d49ec72 GN |
2075 | static unsigned long segment_base(u16 selector) |
2076 | { | |
8c2e41f7 | 2077 | struct desc_struct *table; |
2d49ec72 GN |
2078 | unsigned long v; |
2079 | ||
8c2e41f7 | 2080 | if (!(selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2081 | return 0; |
2082 | ||
45fc8757 | 2083 | table = get_current_gdt_ro(); |
2d49ec72 | 2084 | |
8c2e41f7 | 2085 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
2d49ec72 GN |
2086 | u16 ldt_selector = kvm_read_ldt(); |
2087 | ||
8c2e41f7 | 2088 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2089 | return 0; |
2090 | ||
8c2e41f7 | 2091 | table = (struct desc_struct *)segment_base(ldt_selector); |
2d49ec72 | 2092 | } |
8c2e41f7 | 2093 | v = get_desc_base(&table[selector >> 3]); |
2d49ec72 GN |
2094 | return v; |
2095 | } | |
e28baead | 2096 | #endif |
2d49ec72 | 2097 | |
04d2cc77 | 2098 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
33ed6329 | 2099 | { |
04d2cc77 | 2100 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 2101 | int i; |
04d2cc77 | 2102 | |
a2fa3e9f | 2103 | if (vmx->host_state.loaded) |
33ed6329 AK |
2104 | return; |
2105 | ||
a2fa3e9f | 2106 | vmx->host_state.loaded = 1; |
33ed6329 AK |
2107 | /* |
2108 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not | |
2109 | * allow segment selectors with cpl > 0 or ti == 1. | |
2110 | */ | |
d6e88aec | 2111 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
152d3f2f | 2112 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
9581d442 | 2113 | savesegment(fs, vmx->host_state.fs_sel); |
152d3f2f | 2114 | if (!(vmx->host_state.fs_sel & 7)) { |
a2fa3e9f | 2115 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
152d3f2f LV |
2116 | vmx->host_state.fs_reload_needed = 0; |
2117 | } else { | |
33ed6329 | 2118 | vmcs_write16(HOST_FS_SELECTOR, 0); |
152d3f2f | 2119 | vmx->host_state.fs_reload_needed = 1; |
33ed6329 | 2120 | } |
9581d442 | 2121 | savesegment(gs, vmx->host_state.gs_sel); |
a2fa3e9f GH |
2122 | if (!(vmx->host_state.gs_sel & 7)) |
2123 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); | |
33ed6329 AK |
2124 | else { |
2125 | vmcs_write16(HOST_GS_SELECTOR, 0); | |
152d3f2f | 2126 | vmx->host_state.gs_ldt_reload_needed = 1; |
33ed6329 AK |
2127 | } |
2128 | ||
b2da15ac AK |
2129 | #ifdef CONFIG_X86_64 |
2130 | savesegment(ds, vmx->host_state.ds_sel); | |
2131 | savesegment(es, vmx->host_state.es_sel); | |
2132 | #endif | |
2133 | ||
33ed6329 AK |
2134 | #ifdef CONFIG_X86_64 |
2135 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); | |
2136 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); | |
2137 | #else | |
a2fa3e9f GH |
2138 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
2139 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); | |
33ed6329 | 2140 | #endif |
707c0874 AK |
2141 | |
2142 | #ifdef CONFIG_X86_64 | |
c8770e7b AK |
2143 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
2144 | if (is_long_mode(&vmx->vcpu)) | |
44ea2b17 | 2145 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
707c0874 | 2146 | #endif |
da8999d3 LJ |
2147 | if (boot_cpu_has(X86_FEATURE_MPX)) |
2148 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
26bb0981 AK |
2149 | for (i = 0; i < vmx->save_nmsrs; ++i) |
2150 | kvm_set_shared_msr(vmx->guest_msrs[i].index, | |
d5696725 AK |
2151 | vmx->guest_msrs[i].data, |
2152 | vmx->guest_msrs[i].mask); | |
33ed6329 AK |
2153 | } |
2154 | ||
a9b21b62 | 2155 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
33ed6329 | 2156 | { |
a2fa3e9f | 2157 | if (!vmx->host_state.loaded) |
33ed6329 AK |
2158 | return; |
2159 | ||
e1beb1d3 | 2160 | ++vmx->vcpu.stat.host_state_reload; |
a2fa3e9f | 2161 | vmx->host_state.loaded = 0; |
c8770e7b AK |
2162 | #ifdef CONFIG_X86_64 |
2163 | if (is_long_mode(&vmx->vcpu)) | |
2164 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); | |
2165 | #endif | |
152d3f2f | 2166 | if (vmx->host_state.gs_ldt_reload_needed) { |
d6e88aec | 2167 | kvm_load_ldt(vmx->host_state.ldt_sel); |
33ed6329 | 2168 | #ifdef CONFIG_X86_64 |
9581d442 | 2169 | load_gs_index(vmx->host_state.gs_sel); |
9581d442 AK |
2170 | #else |
2171 | loadsegment(gs, vmx->host_state.gs_sel); | |
33ed6329 | 2172 | #endif |
33ed6329 | 2173 | } |
0a77fe4c AK |
2174 | if (vmx->host_state.fs_reload_needed) |
2175 | loadsegment(fs, vmx->host_state.fs_sel); | |
b2da15ac AK |
2176 | #ifdef CONFIG_X86_64 |
2177 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { | |
2178 | loadsegment(ds, vmx->host_state.ds_sel); | |
2179 | loadsegment(es, vmx->host_state.es_sel); | |
2180 | } | |
b2da15ac | 2181 | #endif |
b7ffc44d | 2182 | invalidate_tss_limit(); |
44ea2b17 | 2183 | #ifdef CONFIG_X86_64 |
c8770e7b | 2184 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
44ea2b17 | 2185 | #endif |
da8999d3 LJ |
2186 | if (vmx->host_state.msr_host_bndcfgs) |
2187 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
45fc8757 | 2188 | load_fixmap_gdt(raw_smp_processor_id()); |
33ed6329 AK |
2189 | } |
2190 | ||
a9b21b62 AK |
2191 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
2192 | { | |
2193 | preempt_disable(); | |
2194 | __vmx_load_host_state(vmx); | |
2195 | preempt_enable(); | |
2196 | } | |
2197 | ||
28b835d6 FW |
2198 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
2199 | { | |
2200 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2201 | struct pi_desc old, new; | |
2202 | unsigned int dest; | |
2203 | ||
5f19dac4 PB |
2204 | /* |
2205 | * In case of hot-plug or hot-unplug, we may have to undo | |
2206 | * vmx_vcpu_pi_put even if there is no assigned device. And we | |
2207 | * always keep PI.NDST up to date for simplicity: it makes the | |
2208 | * code easier, and CPU migration is not a fast path. | |
2209 | */ | |
2210 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) | |
2211 | return; | |
2212 | ||
2213 | /* | |
2214 | * First handle the simple case where no cmpxchg is necessary; just | |
2215 | * allow posting non-urgent interrupts. | |
2216 | * | |
2217 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change | |
2218 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler | |
2219 | * expects the VCPU to be on the blocked_vcpu_list that matches | |
2220 | * PI.NDST. | |
2221 | */ | |
2222 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || | |
2223 | vcpu->cpu == cpu) { | |
2224 | pi_clear_sn(pi_desc); | |
28b835d6 | 2225 | return; |
5f19dac4 | 2226 | } |
28b835d6 | 2227 | |
5f19dac4 | 2228 | /* The full case. */ |
28b835d6 FW |
2229 | do { |
2230 | old.control = new.control = pi_desc->control; | |
2231 | ||
5f19dac4 | 2232 | dest = cpu_physical_id(cpu); |
28b835d6 | 2233 | |
5f19dac4 PB |
2234 | if (x2apic_enabled()) |
2235 | new.ndst = dest; | |
2236 | else | |
2237 | new.ndst = (dest << 8) & 0xFF00; | |
28b835d6 | 2238 | |
28b835d6 | 2239 | new.sn = 0; |
84635f8b PB |
2240 | } while (cmpxchg64(&pi_desc->control, old.control, |
2241 | new.control) != old.control); | |
28b835d6 | 2242 | } |
1be0e61c | 2243 | |
c95ba92a PF |
2244 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
2245 | { | |
2246 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; | |
2247 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); | |
2248 | } | |
2249 | ||
6aa8b732 AK |
2250 | /* |
2251 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes | |
2252 | * vcpu mutex is already taken. | |
2253 | */ | |
15ad7146 | 2254 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
6aa8b732 | 2255 | { |
a2fa3e9f | 2256 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b80c76ec | 2257 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
6aa8b732 | 2258 | |
b80c76ec | 2259 | if (!already_loaded) { |
fe0e80be | 2260 | loaded_vmcs_clear(vmx->loaded_vmcs); |
92fe13be | 2261 | local_irq_disable(); |
8f536b76 | 2262 | crash_disable_local_vmclear(cpu); |
5a560f8b XG |
2263 | |
2264 | /* | |
2265 | * Read loaded_vmcs->cpu should be before fetching | |
2266 | * loaded_vmcs->loaded_vmcss_on_cpu_link. | |
2267 | * See the comments in __loaded_vmcs_clear(). | |
2268 | */ | |
2269 | smp_rmb(); | |
2270 | ||
d462b819 NHE |
2271 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
2272 | &per_cpu(loaded_vmcss_on_cpu, cpu)); | |
8f536b76 | 2273 | crash_enable_local_vmclear(cpu); |
92fe13be | 2274 | local_irq_enable(); |
b80c76ec JM |
2275 | } |
2276 | ||
2277 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { | |
2278 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; | |
2279 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
357b57d7 | 2280 | if (ibpb_inuse) |
8f6a3159 | 2281 | native_wrmsrl(MSR_IA32_PRED_CMD, FEATURE_SET_IBPB); |
b80c76ec JM |
2282 | } |
2283 | ||
2284 | if (!already_loaded) { | |
59c58ceb | 2285 | void *gdt = get_current_gdt_ro(); |
b80c76ec JM |
2286 | unsigned long sysenter_esp; |
2287 | ||
2288 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); | |
92fe13be | 2289 | |
6aa8b732 AK |
2290 | /* |
2291 | * Linux uses per-cpu TSS and GDT, so set these when switching | |
e0c23063 | 2292 | * processors. See 22.2.4. |
6aa8b732 | 2293 | */ |
e0c23063 | 2294 | vmcs_writel(HOST_TR_BASE, |
475b37e7 | 2295 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
59c58ceb | 2296 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
6aa8b732 | 2297 | |
b7ffc44d AL |
2298 | /* |
2299 | * VM exits change the host TR limit to 0x67 after a VM | |
2300 | * exit. This is okay, since 0x67 covers everything except | |
2301 | * the IO bitmap and have have code to handle the IO bitmap | |
2302 | * being lost after a VM exit. | |
2303 | */ | |
2304 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); | |
2305 | ||
6aa8b732 AK |
2306 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
2307 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ | |
ff2c3a18 | 2308 | |
d462b819 | 2309 | vmx->loaded_vmcs->cpu = cpu; |
6aa8b732 | 2310 | } |
28b835d6 | 2311 | |
2680d6da OH |
2312 | /* Setup TSC multiplier */ |
2313 | if (kvm_has_tsc_control && | |
c95ba92a PF |
2314 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
2315 | decache_tsc_multiplier(vmx); | |
2680d6da | 2316 | |
28b835d6 | 2317 | vmx_vcpu_pi_load(vcpu, cpu); |
1be0e61c | 2318 | vmx->host_pkru = read_pkru(); |
28b835d6 FW |
2319 | } |
2320 | ||
2321 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) | |
2322 | { | |
2323 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2324 | ||
2325 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
2326 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
2327 | !kvm_vcpu_apicv_active(vcpu)) | |
28b835d6 FW |
2328 | return; |
2329 | ||
2330 | /* Set SN when the vCPU is preempted */ | |
2331 | if (vcpu->preempted) | |
2332 | pi_set_sn(pi_desc); | |
6aa8b732 AK |
2333 | } |
2334 | ||
2335 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) | |
2336 | { | |
28b835d6 FW |
2337 | vmx_vcpu_pi_put(vcpu); |
2338 | ||
a9b21b62 | 2339 | __vmx_load_host_state(to_vmx(vcpu)); |
6aa8b732 AK |
2340 | } |
2341 | ||
f244deed WL |
2342 | static bool emulation_required(struct kvm_vcpu *vcpu) |
2343 | { | |
2344 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); | |
2345 | } | |
2346 | ||
edcafe3c AK |
2347 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
2348 | ||
fe3ef05c NHE |
2349 | /* |
2350 | * Return the cr0 value that a nested guest would read. This is a combination | |
2351 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by | |
2352 | * its hypervisor (cr0_read_shadow). | |
2353 | */ | |
2354 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) | |
2355 | { | |
2356 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | | |
2357 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); | |
2358 | } | |
2359 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) | |
2360 | { | |
2361 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | | |
2362 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); | |
2363 | } | |
2364 | ||
6aa8b732 AK |
2365 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
2366 | { | |
78ac8b47 | 2367 | unsigned long rflags, save_rflags; |
345dcaa8 | 2368 | |
6de12732 AK |
2369 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
2370 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); | |
2371 | rflags = vmcs_readl(GUEST_RFLAGS); | |
2372 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
2373 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2374 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; | |
2375 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2376 | } | |
2377 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 | 2378 | } |
6de12732 | 2379 | return to_vmx(vcpu)->rflags; |
6aa8b732 AK |
2380 | } |
2381 | ||
2382 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
2383 | { | |
f244deed WL |
2384 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
2385 | ||
6de12732 AK |
2386 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
2387 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 AK |
2388 | if (to_vmx(vcpu)->rmode.vm86_active) { |
2389 | to_vmx(vcpu)->rmode.save_rflags = rflags; | |
053de044 | 2390 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
78ac8b47 | 2391 | } |
6aa8b732 | 2392 | vmcs_writel(GUEST_RFLAGS, rflags); |
f244deed WL |
2393 | |
2394 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) | |
2395 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); | |
6aa8b732 AK |
2396 | } |
2397 | ||
37ccdcbe | 2398 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
2809f5d2 GC |
2399 | { |
2400 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2401 | int ret = 0; | |
2402 | ||
2403 | if (interruptibility & GUEST_INTR_STATE_STI) | |
48005f64 | 2404 | ret |= KVM_X86_SHADOW_INT_STI; |
2809f5d2 | 2405 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
48005f64 | 2406 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
2809f5d2 | 2407 | |
37ccdcbe | 2408 | return ret; |
2809f5d2 GC |
2409 | } |
2410 | ||
2411 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) | |
2412 | { | |
2413 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2414 | u32 interruptibility = interruptibility_old; | |
2415 | ||
2416 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); | |
2417 | ||
48005f64 | 2418 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
2809f5d2 | 2419 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
48005f64 | 2420 | else if (mask & KVM_X86_SHADOW_INT_STI) |
2809f5d2 GC |
2421 | interruptibility |= GUEST_INTR_STATE_STI; |
2422 | ||
2423 | if ((interruptibility != interruptibility_old)) | |
2424 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); | |
2425 | } | |
2426 | ||
6aa8b732 AK |
2427 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
2428 | { | |
2429 | unsigned long rip; | |
6aa8b732 | 2430 | |
5fdbf976 | 2431 | rip = kvm_rip_read(vcpu); |
6aa8b732 | 2432 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
5fdbf976 | 2433 | kvm_rip_write(vcpu, rip); |
6aa8b732 | 2434 | |
2809f5d2 GC |
2435 | /* skipping an emulated instruction also counts */ |
2436 | vmx_set_interrupt_shadow(vcpu, 0); | |
6aa8b732 AK |
2437 | } |
2438 | ||
b96fb439 PB |
2439 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
2440 | unsigned long exit_qual) | |
2441 | { | |
2442 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
2443 | unsigned int nr = vcpu->arch.exception.nr; | |
2444 | u32 intr_info = nr | INTR_INFO_VALID_MASK; | |
2445 | ||
2446 | if (vcpu->arch.exception.has_error_code) { | |
2447 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; | |
2448 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; | |
2449 | } | |
2450 | ||
2451 | if (kvm_exception_is_soft(nr)) | |
2452 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; | |
2453 | else | |
2454 | intr_info |= INTR_TYPE_HARD_EXCEPTION; | |
2455 | ||
2456 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && | |
2457 | vmx_get_nmi_mask(vcpu)) | |
2458 | intr_info |= INTR_INFO_UNBLOCK_NMI; | |
2459 | ||
2460 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); | |
2461 | } | |
2462 | ||
0b6ac343 NHE |
2463 | /* |
2464 | * KVM wants to inject page-faults which it got to the guest. This function | |
2465 | * checks whether in a nested guest, we need to inject them to L1 or L2. | |
0b6ac343 | 2466 | */ |
adfe20fb | 2467 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu) |
0b6ac343 NHE |
2468 | { |
2469 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
adfe20fb | 2470 | unsigned int nr = vcpu->arch.exception.nr; |
0b6ac343 | 2471 | |
b96fb439 PB |
2472 | if (nr == PF_VECTOR) { |
2473 | if (vcpu->arch.exception.nested_apf) { | |
2474 | nested_vmx_inject_exception_vmexit(vcpu, | |
2475 | vcpu->arch.apf.nested_apf_token); | |
2476 | return 1; | |
2477 | } | |
2478 | /* | |
2479 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. | |
2480 | * The fix is to add the ancillary datum (CR2 or DR6) to structs | |
2481 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 | |
2482 | * can be written only when inject_pending_event runs. This should be | |
2483 | * conditional on a new capability---if the capability is disabled, | |
2484 | * kvm_multiple_exception would write the ancillary information to | |
2485 | * CR2 or DR6, for backwards ABI-compatibility. | |
2486 | */ | |
2487 | if (nested_vmx_is_page_fault_vmexit(vmcs12, | |
2488 | vcpu->arch.exception.error_code)) { | |
2489 | nested_vmx_inject_exception_vmexit(vcpu, vcpu->arch.cr2); | |
2490 | return 1; | |
2491 | } | |
2492 | } else { | |
2493 | unsigned long exit_qual = 0; | |
2494 | if (nr == DB_VECTOR) | |
2495 | exit_qual = vcpu->arch.dr6; | |
0b6ac343 | 2496 | |
b96fb439 PB |
2497 | if (vmcs12->exception_bitmap & (1u << nr)) { |
2498 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); | |
2499 | return 1; | |
2500 | } | |
adfe20fb WL |
2501 | } |
2502 | ||
b96fb439 | 2503 | return 0; |
0b6ac343 NHE |
2504 | } |
2505 | ||
cfcd20e5 | 2506 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
298101da | 2507 | { |
77ab6db0 | 2508 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
cfcd20e5 WL |
2509 | unsigned nr = vcpu->arch.exception.nr; |
2510 | bool has_error_code = vcpu->arch.exception.has_error_code; | |
2511 | bool reinject = vcpu->arch.exception.reinject; | |
2512 | u32 error_code = vcpu->arch.exception.error_code; | |
8ab2d2e2 | 2513 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
77ab6db0 | 2514 | |
e011c663 | 2515 | if (!reinject && is_guest_mode(vcpu) && |
adfe20fb | 2516 | nested_vmx_check_exception(vcpu)) |
0b6ac343 NHE |
2517 | return; |
2518 | ||
8ab2d2e2 | 2519 | if (has_error_code) { |
77ab6db0 | 2520 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
8ab2d2e2 JK |
2521 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
2522 | } | |
77ab6db0 | 2523 | |
7ffd92c5 | 2524 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
2525 | int inc_eip = 0; |
2526 | if (kvm_exception_is_soft(nr)) | |
2527 | inc_eip = vcpu->arch.event_exit_inst_len; | |
2528 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) | |
a92601bb | 2529 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
77ab6db0 JK |
2530 | return; |
2531 | } | |
2532 | ||
66fd3f7f GN |
2533 | if (kvm_exception_is_soft(nr)) { |
2534 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
2535 | vmx->vcpu.arch.event_exit_inst_len); | |
8ab2d2e2 JK |
2536 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
2537 | } else | |
2538 | intr_info |= INTR_TYPE_HARD_EXCEPTION; | |
2539 | ||
2540 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); | |
298101da AK |
2541 | } |
2542 | ||
4e47c7a6 SY |
2543 | static bool vmx_rdtscp_supported(void) |
2544 | { | |
2545 | return cpu_has_vmx_rdtscp(); | |
2546 | } | |
2547 | ||
ad756a16 MJ |
2548 | static bool vmx_invpcid_supported(void) |
2549 | { | |
2550 | return cpu_has_vmx_invpcid() && enable_ept; | |
2551 | } | |
2552 | ||
a75beee6 ED |
2553 | /* |
2554 | * Swap MSR entry in host/guest MSR entry array. | |
2555 | */ | |
8b9cf98c | 2556 | static void move_msr_up(struct vcpu_vmx *vmx, int from, int to) |
a75beee6 | 2557 | { |
26bb0981 | 2558 | struct shared_msr_entry tmp; |
a2fa3e9f GH |
2559 | |
2560 | tmp = vmx->guest_msrs[to]; | |
2561 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; | |
2562 | vmx->guest_msrs[from] = tmp; | |
a75beee6 ED |
2563 | } |
2564 | ||
8d14695f YZ |
2565 | static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu) |
2566 | { | |
2567 | unsigned long *msr_bitmap; | |
2568 | ||
670125bd | 2569 | if (is_guest_mode(vcpu)) |
d048c098 | 2570 | msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap; |
3ce424e4 RK |
2571 | else if (cpu_has_secondary_exec_ctrls() && |
2572 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & | |
2573 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { | |
f6e90f9e WL |
2574 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) { |
2575 | if (is_long_mode(vcpu)) | |
c63e4563 | 2576 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv; |
f6e90f9e | 2577 | else |
c63e4563 | 2578 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv; |
f6e90f9e WL |
2579 | } else { |
2580 | if (is_long_mode(vcpu)) | |
c63e4563 | 2581 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic; |
f6e90f9e | 2582 | else |
c63e4563 | 2583 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic; |
f6e90f9e | 2584 | } |
8d14695f YZ |
2585 | } else { |
2586 | if (is_long_mode(vcpu)) | |
2587 | msr_bitmap = vmx_msr_bitmap_longmode; | |
2588 | else | |
2589 | msr_bitmap = vmx_msr_bitmap_legacy; | |
2590 | } | |
2591 | ||
2592 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); | |
2593 | } | |
2594 | ||
e38aea3e AK |
2595 | /* |
2596 | * Set up the vmcs to automatically save and restore system | |
2597 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy | |
2598 | * mode, as fiddling with msrs is very expensive. | |
2599 | */ | |
8b9cf98c | 2600 | static void setup_msrs(struct vcpu_vmx *vmx) |
e38aea3e | 2601 | { |
26bb0981 | 2602 | int save_nmsrs, index; |
e38aea3e | 2603 | |
a75beee6 ED |
2604 | save_nmsrs = 0; |
2605 | #ifdef CONFIG_X86_64 | |
8b9cf98c | 2606 | if (is_long_mode(&vmx->vcpu)) { |
8b9cf98c | 2607 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
a75beee6 | 2608 | if (index >= 0) |
8b9cf98c RR |
2609 | move_msr_up(vmx, index, save_nmsrs++); |
2610 | index = __find_msr_index(vmx, MSR_LSTAR); | |
a75beee6 | 2611 | if (index >= 0) |
8b9cf98c RR |
2612 | move_msr_up(vmx, index, save_nmsrs++); |
2613 | index = __find_msr_index(vmx, MSR_CSTAR); | |
a75beee6 | 2614 | if (index >= 0) |
8b9cf98c | 2615 | move_msr_up(vmx, index, save_nmsrs++); |
4e47c7a6 | 2616 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
1cea0ce6 | 2617 | if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu)) |
4e47c7a6 | 2618 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 | 2619 | /* |
8c06585d | 2620 | * MSR_STAR is only needed on long mode guests, and only |
a75beee6 ED |
2621 | * if efer.sce is enabled. |
2622 | */ | |
8c06585d | 2623 | index = __find_msr_index(vmx, MSR_STAR); |
f6801dff | 2624 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
8b9cf98c | 2625 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 ED |
2626 | } |
2627 | #endif | |
92c0d900 AK |
2628 | index = __find_msr_index(vmx, MSR_EFER); |
2629 | if (index >= 0 && update_transition_efer(vmx, index)) | |
26bb0981 | 2630 | move_msr_up(vmx, index, save_nmsrs++); |
e38aea3e | 2631 | |
26bb0981 | 2632 | vmx->save_nmsrs = save_nmsrs; |
5897297b | 2633 | |
8d14695f YZ |
2634 | if (cpu_has_vmx_msr_bitmap()) |
2635 | vmx_set_msr_bitmap(&vmx->vcpu); | |
e38aea3e AK |
2636 | } |
2637 | ||
6aa8b732 AK |
2638 | /* |
2639 | * reads and returns guest's timestamp counter "register" | |
be7b263e HZ |
2640 | * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset |
2641 | * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3 | |
6aa8b732 | 2642 | */ |
be7b263e | 2643 | static u64 guest_read_tsc(struct kvm_vcpu *vcpu) |
6aa8b732 AK |
2644 | { |
2645 | u64 host_tsc, tsc_offset; | |
2646 | ||
4ea1636b | 2647 | host_tsc = rdtsc(); |
6aa8b732 | 2648 | tsc_offset = vmcs_read64(TSC_OFFSET); |
be7b263e | 2649 | return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; |
6aa8b732 AK |
2650 | } |
2651 | ||
2652 | /* | |
99e3e30a | 2653 | * writes 'offset' into guest's timestamp counter offset register |
6aa8b732 | 2654 | */ |
99e3e30a | 2655 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
6aa8b732 | 2656 | { |
27fc51b2 | 2657 | if (is_guest_mode(vcpu)) { |
7991825b | 2658 | /* |
27fc51b2 NHE |
2659 | * We're here if L1 chose not to trap WRMSR to TSC. According |
2660 | * to the spec, this should set L1's TSC; The offset that L1 | |
2661 | * set for L2 remains unchanged, and still needs to be added | |
2662 | * to the newly set TSC to get L2's TSC. | |
7991825b | 2663 | */ |
27fc51b2 | 2664 | struct vmcs12 *vmcs12; |
27fc51b2 NHE |
2665 | /* recalculate vmcs02.TSC_OFFSET: */ |
2666 | vmcs12 = get_vmcs12(vcpu); | |
2667 | vmcs_write64(TSC_OFFSET, offset + | |
2668 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? | |
2669 | vmcs12->tsc_offset : 0)); | |
2670 | } else { | |
489223ed YY |
2671 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
2672 | vmcs_read64(TSC_OFFSET), offset); | |
27fc51b2 NHE |
2673 | vmcs_write64(TSC_OFFSET, offset); |
2674 | } | |
6aa8b732 AK |
2675 | } |
2676 | ||
801d3424 NHE |
2677 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
2678 | { | |
2679 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); | |
2680 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); | |
2681 | } | |
2682 | ||
2683 | /* | |
2684 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX | |
2685 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for | |
2686 | * all guests if the "nested" module option is off, and can also be disabled | |
2687 | * for a single guest by disabling its VMX cpuid bit. | |
2688 | */ | |
2689 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) | |
2690 | { | |
2691 | return nested && guest_cpuid_has_vmx(vcpu); | |
2692 | } | |
2693 | ||
b87a51ae NHE |
2694 | /* |
2695 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be | |
2696 | * returned for the various VMX controls MSRs when nested VMX is enabled. | |
2697 | * The same values should also be used to verify that vmcs12 control fields are | |
2698 | * valid during nested entry from L1 to L2. | |
2699 | * Each of these control msrs has a low and high 32-bit half: A low bit is on | |
2700 | * if the corresponding bit in the (32-bit) control field *must* be on, and a | |
2701 | * bit in the high half is on if the corresponding bit in the control field | |
2702 | * may be on. See also vmx_control_verify(). | |
b87a51ae | 2703 | */ |
b9c237bb | 2704 | static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) |
b87a51ae NHE |
2705 | { |
2706 | /* | |
2707 | * Note that as a general rule, the high half of the MSRs (bits in | |
2708 | * the control fields which may be 1) should be initialized by the | |
2709 | * intersection of the underlying hardware's MSR (i.e., features which | |
2710 | * can be supported) and the list of features we want to expose - | |
2711 | * because they are known to be properly supported in our code. | |
2712 | * Also, usually, the low half of the MSRs (bits which must be 1) can | |
2713 | * be set to 0, meaning that L1 may turn off any of these bits. The | |
2714 | * reason is that if one of these bits is necessary, it will appear | |
2715 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control | |
2716 | * fields of vmcs01 and vmcs02, will turn these bits off - and | |
7313c698 | 2717 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
b87a51ae NHE |
2718 | * These rules have exceptions below. |
2719 | */ | |
2720 | ||
2721 | /* pin-based controls */ | |
eabeaacc | 2722 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
b9c237bb WV |
2723 | vmx->nested.nested_vmx_pinbased_ctls_low, |
2724 | vmx->nested.nested_vmx_pinbased_ctls_high); | |
2725 | vmx->nested.nested_vmx_pinbased_ctls_low |= | |
2726 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
2727 | vmx->nested.nested_vmx_pinbased_ctls_high &= | |
2728 | PIN_BASED_EXT_INTR_MASK | | |
2729 | PIN_BASED_NMI_EXITING | | |
2730 | PIN_BASED_VIRTUAL_NMIS; | |
2731 | vmx->nested.nested_vmx_pinbased_ctls_high |= | |
2732 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | | |
0238ea91 | 2733 | PIN_BASED_VMX_PREEMPTION_TIMER; |
d62caabb | 2734 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) |
705699a1 WV |
2735 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
2736 | PIN_BASED_POSTED_INTR; | |
b87a51ae | 2737 | |
3dbcd8da | 2738 | /* exit controls */ |
c0dfee58 | 2739 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
b9c237bb WV |
2740 | vmx->nested.nested_vmx_exit_ctls_low, |
2741 | vmx->nested.nested_vmx_exit_ctls_high); | |
2742 | vmx->nested.nested_vmx_exit_ctls_low = | |
2743 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; | |
e0ba1a6f | 2744 | |
b9c237bb | 2745 | vmx->nested.nested_vmx_exit_ctls_high &= |
b87a51ae | 2746 | #ifdef CONFIG_X86_64 |
c0dfee58 | 2747 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
b87a51ae | 2748 | #endif |
f4124500 | 2749 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
b9c237bb WV |
2750 | vmx->nested.nested_vmx_exit_ctls_high |= |
2751 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | | |
f4124500 | 2752 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
e0ba1a6f BD |
2753 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
2754 | ||
a87036ad | 2755 | if (kvm_mpx_supported()) |
b9c237bb | 2756 | vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
b87a51ae | 2757 | |
2996fca0 | 2758 | /* We support free control of debug control saving. */ |
0115f9cb | 2759 | vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
2996fca0 | 2760 | |
b87a51ae NHE |
2761 | /* entry controls */ |
2762 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, | |
b9c237bb WV |
2763 | vmx->nested.nested_vmx_entry_ctls_low, |
2764 | vmx->nested.nested_vmx_entry_ctls_high); | |
2765 | vmx->nested.nested_vmx_entry_ctls_low = | |
2766 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; | |
2767 | vmx->nested.nested_vmx_entry_ctls_high &= | |
57435349 JK |
2768 | #ifdef CONFIG_X86_64 |
2769 | VM_ENTRY_IA32E_MODE | | |
2770 | #endif | |
2771 | VM_ENTRY_LOAD_IA32_PAT; | |
b9c237bb WV |
2772 | vmx->nested.nested_vmx_entry_ctls_high |= |
2773 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); | |
a87036ad | 2774 | if (kvm_mpx_supported()) |
b9c237bb | 2775 | vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
57435349 | 2776 | |
2996fca0 | 2777 | /* We support free control of debug control loading. */ |
0115f9cb | 2778 | vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
2996fca0 | 2779 | |
b87a51ae NHE |
2780 | /* cpu-based controls */ |
2781 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, | |
b9c237bb WV |
2782 | vmx->nested.nested_vmx_procbased_ctls_low, |
2783 | vmx->nested.nested_vmx_procbased_ctls_high); | |
2784 | vmx->nested.nested_vmx_procbased_ctls_low = | |
2785 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
2786 | vmx->nested.nested_vmx_procbased_ctls_high &= | |
a294c9bb JK |
2787 | CPU_BASED_VIRTUAL_INTR_PENDING | |
2788 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | | |
b87a51ae NHE |
2789 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
2790 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | | |
2791 | CPU_BASED_CR3_STORE_EXITING | | |
2792 | #ifdef CONFIG_X86_64 | |
2793 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | | |
2794 | #endif | |
2795 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | | |
5f3d45e7 MD |
2796 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
2797 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | | |
2798 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | | |
2799 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
b87a51ae NHE |
2800 | /* |
2801 | * We can allow some features even when not supported by the | |
2802 | * hardware. For example, L1 can specify an MSR bitmap - and we | |
2803 | * can use it to avoid exits to L1 - even when L0 runs L2 | |
2804 | * without MSR bitmaps. | |
2805 | */ | |
b9c237bb WV |
2806 | vmx->nested.nested_vmx_procbased_ctls_high |= |
2807 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | | |
560b7ee1 | 2808 | CPU_BASED_USE_MSR_BITMAPS; |
b87a51ae | 2809 | |
3dcdf3ec | 2810 | /* We support free control of CR3 access interception. */ |
0115f9cb | 2811 | vmx->nested.nested_vmx_procbased_ctls_low &= |
3dcdf3ec JK |
2812 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
2813 | ||
b87a51ae NHE |
2814 | /* secondary cpu-based controls */ |
2815 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, | |
b9c237bb WV |
2816 | vmx->nested.nested_vmx_secondary_ctls_low, |
2817 | vmx->nested.nested_vmx_secondary_ctls_high); | |
2818 | vmx->nested.nested_vmx_secondary_ctls_low = 0; | |
2819 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
a5f46457 | 2820 | SECONDARY_EXEC_RDRAND | SECONDARY_EXEC_RDSEED | |
d6851fbe | 2821 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
b3a2a907 | 2822 | SECONDARY_EXEC_RDTSCP | |
1b07304c | 2823 | SECONDARY_EXEC_DESC | |
f2b93280 | 2824 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
82f0dd4b | 2825 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
608406e2 | 2826 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
81dc01f7 | 2827 | SECONDARY_EXEC_WBINVD_EXITING | |
dfa169bb | 2828 | SECONDARY_EXEC_XSAVES; |
c18911a2 | 2829 | |
afa61f75 NHE |
2830 | if (enable_ept) { |
2831 | /* nested EPT: emulate EPT also to L1 */ | |
b9c237bb | 2832 | vmx->nested.nested_vmx_secondary_ctls_high |= |
0790ec17 | 2833 | SECONDARY_EXEC_ENABLE_EPT; |
b9c237bb | 2834 | vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
7db74265 | 2835 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
02120c45 BD |
2836 | if (cpu_has_vmx_ept_execute_only()) |
2837 | vmx->nested.nested_vmx_ept_caps |= | |
2838 | VMX_EPT_EXECUTE_ONLY_BIT; | |
b9c237bb | 2839 | vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept; |
45e11817 | 2840 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
7db74265 PB |
2841 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
2842 | VMX_EPT_1GB_PAGE_BIT; | |
03efce6f BD |
2843 | if (enable_ept_ad_bits) { |
2844 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2845 | SECONDARY_EXEC_ENABLE_PML; | |
7461fbc4 | 2846 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT; |
03efce6f | 2847 | } |
afa61f75 | 2848 | } else |
b9c237bb | 2849 | vmx->nested.nested_vmx_ept_caps = 0; |
afa61f75 | 2850 | |
ef697a71 PB |
2851 | /* |
2852 | * Old versions of KVM use the single-context version without | |
2853 | * checking for support, so declare that it is supported even | |
2854 | * though it is treated as global context. The alternative is | |
2855 | * not failing the single-context invvpid, and it is worse. | |
2856 | */ | |
63cb6d5f WL |
2857 | if (enable_vpid) { |
2858 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2859 | SECONDARY_EXEC_ENABLE_VPID; | |
089d7b6e | 2860 | vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT | |
bcdde302 | 2861 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
63cb6d5f | 2862 | } else |
089d7b6e | 2863 | vmx->nested.nested_vmx_vpid_caps = 0; |
99b83ac8 | 2864 | |
0790ec17 RK |
2865 | if (enable_unrestricted_guest) |
2866 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2867 | SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
2868 | ||
c18911a2 | 2869 | /* miscellaneous data */ |
b9c237bb WV |
2870 | rdmsr(MSR_IA32_VMX_MISC, |
2871 | vmx->nested.nested_vmx_misc_low, | |
2872 | vmx->nested.nested_vmx_misc_high); | |
2873 | vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; | |
2874 | vmx->nested.nested_vmx_misc_low |= | |
2875 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | | |
f4124500 | 2876 | VMX_MISC_ACTIVITY_HLT; |
b9c237bb | 2877 | vmx->nested.nested_vmx_misc_high = 0; |
62cc6b9d DM |
2878 | |
2879 | /* | |
2880 | * This MSR reports some information about VMX support. We | |
2881 | * should return information about the VMX we emulate for the | |
2882 | * guest, and the VMCS structure we give it - not about the | |
2883 | * VMX support of the underlying hardware. | |
2884 | */ | |
2885 | vmx->nested.nested_vmx_basic = | |
2886 | VMCS12_REVISION | | |
2887 | VMX_BASIC_TRUE_CTLS | | |
2888 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | | |
2889 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); | |
2890 | ||
2891 | if (cpu_has_vmx_basic_inout()) | |
2892 | vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT; | |
2893 | ||
2894 | /* | |
8322ebbb | 2895 | * These MSRs specify bits which the guest must keep fixed on |
62cc6b9d DM |
2896 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
2897 | * We picked the standard core2 setting. | |
2898 | */ | |
2899 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) | |
2900 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE | |
2901 | vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON; | |
62cc6b9d | 2902 | vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON; |
8322ebbb DM |
2903 | |
2904 | /* These MSRs specify bits which the guest must keep fixed off. */ | |
2905 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1); | |
2906 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1); | |
62cc6b9d DM |
2907 | |
2908 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ | |
2909 | vmx->nested.nested_vmx_vmcs_enum = 0x2e; | |
b87a51ae NHE |
2910 | } |
2911 | ||
3899152c DM |
2912 | /* |
2913 | * if fixed0[i] == 1: val[i] must be 1 | |
2914 | * if fixed1[i] == 0: val[i] must be 0 | |
2915 | */ | |
2916 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) | |
2917 | { | |
2918 | return ((val & fixed1) | fixed0) == val; | |
b87a51ae NHE |
2919 | } |
2920 | ||
2921 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) | |
2922 | { | |
3899152c | 2923 | return fixed_bits_valid(control, low, high); |
b87a51ae NHE |
2924 | } |
2925 | ||
2926 | static inline u64 vmx_control_msr(u32 low, u32 high) | |
2927 | { | |
2928 | return low | ((u64)high << 32); | |
2929 | } | |
2930 | ||
62cc6b9d DM |
2931 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
2932 | { | |
2933 | superset &= mask; | |
2934 | subset &= mask; | |
2935 | ||
2936 | return (superset | subset) == superset; | |
2937 | } | |
2938 | ||
2939 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) | |
2940 | { | |
2941 | const u64 feature_and_reserved = | |
2942 | /* feature (except bit 48; see below) */ | |
2943 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | | |
2944 | /* reserved */ | |
2945 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); | |
2946 | u64 vmx_basic = vmx->nested.nested_vmx_basic; | |
2947 | ||
2948 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) | |
2949 | return -EINVAL; | |
2950 | ||
2951 | /* | |
2952 | * KVM does not emulate a version of VMX that constrains physical | |
2953 | * addresses of VMX structures (e.g. VMCS) to 32-bits. | |
2954 | */ | |
2955 | if (data & BIT_ULL(48)) | |
2956 | return -EINVAL; | |
2957 | ||
2958 | if (vmx_basic_vmcs_revision_id(vmx_basic) != | |
2959 | vmx_basic_vmcs_revision_id(data)) | |
2960 | return -EINVAL; | |
2961 | ||
2962 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) | |
2963 | return -EINVAL; | |
2964 | ||
2965 | vmx->nested.nested_vmx_basic = data; | |
2966 | return 0; | |
2967 | } | |
2968 | ||
2969 | static int | |
2970 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
2971 | { | |
2972 | u64 supported; | |
2973 | u32 *lowp, *highp; | |
2974 | ||
2975 | switch (msr_index) { | |
2976 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
2977 | lowp = &vmx->nested.nested_vmx_pinbased_ctls_low; | |
2978 | highp = &vmx->nested.nested_vmx_pinbased_ctls_high; | |
2979 | break; | |
2980 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
2981 | lowp = &vmx->nested.nested_vmx_procbased_ctls_low; | |
2982 | highp = &vmx->nested.nested_vmx_procbased_ctls_high; | |
2983 | break; | |
2984 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
2985 | lowp = &vmx->nested.nested_vmx_exit_ctls_low; | |
2986 | highp = &vmx->nested.nested_vmx_exit_ctls_high; | |
2987 | break; | |
2988 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
2989 | lowp = &vmx->nested.nested_vmx_entry_ctls_low; | |
2990 | highp = &vmx->nested.nested_vmx_entry_ctls_high; | |
2991 | break; | |
2992 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
2993 | lowp = &vmx->nested.nested_vmx_secondary_ctls_low; | |
2994 | highp = &vmx->nested.nested_vmx_secondary_ctls_high; | |
2995 | break; | |
2996 | default: | |
2997 | BUG(); | |
2998 | } | |
2999 | ||
3000 | supported = vmx_control_msr(*lowp, *highp); | |
3001 | ||
3002 | /* Check must-be-1 bits are still 1. */ | |
3003 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) | |
3004 | return -EINVAL; | |
3005 | ||
3006 | /* Check must-be-0 bits are still 0. */ | |
3007 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) | |
3008 | return -EINVAL; | |
3009 | ||
3010 | *lowp = data; | |
3011 | *highp = data >> 32; | |
3012 | return 0; | |
3013 | } | |
3014 | ||
3015 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) | |
3016 | { | |
3017 | const u64 feature_and_reserved_bits = | |
3018 | /* feature */ | |
3019 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | | |
3020 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | | |
3021 | /* reserved */ | |
3022 | GENMASK_ULL(13, 9) | BIT_ULL(31); | |
3023 | u64 vmx_misc; | |
3024 | ||
3025 | vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low, | |
3026 | vmx->nested.nested_vmx_misc_high); | |
3027 | ||
3028 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) | |
3029 | return -EINVAL; | |
3030 | ||
3031 | if ((vmx->nested.nested_vmx_pinbased_ctls_high & | |
3032 | PIN_BASED_VMX_PREEMPTION_TIMER) && | |
3033 | vmx_misc_preemption_timer_rate(data) != | |
3034 | vmx_misc_preemption_timer_rate(vmx_misc)) | |
3035 | return -EINVAL; | |
3036 | ||
3037 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) | |
3038 | return -EINVAL; | |
3039 | ||
3040 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) | |
3041 | return -EINVAL; | |
3042 | ||
3043 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) | |
3044 | return -EINVAL; | |
3045 | ||
3046 | vmx->nested.nested_vmx_misc_low = data; | |
3047 | vmx->nested.nested_vmx_misc_high = data >> 32; | |
3048 | return 0; | |
3049 | } | |
3050 | ||
3051 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) | |
3052 | { | |
3053 | u64 vmx_ept_vpid_cap; | |
3054 | ||
3055 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps, | |
3056 | vmx->nested.nested_vmx_vpid_caps); | |
3057 | ||
3058 | /* Every bit is either reserved or a feature bit. */ | |
3059 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) | |
3060 | return -EINVAL; | |
3061 | ||
3062 | vmx->nested.nested_vmx_ept_caps = data; | |
3063 | vmx->nested.nested_vmx_vpid_caps = data >> 32; | |
3064 | return 0; | |
3065 | } | |
3066 | ||
3067 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
3068 | { | |
3069 | u64 *msr; | |
3070 | ||
3071 | switch (msr_index) { | |
3072 | case MSR_IA32_VMX_CR0_FIXED0: | |
3073 | msr = &vmx->nested.nested_vmx_cr0_fixed0; | |
3074 | break; | |
3075 | case MSR_IA32_VMX_CR4_FIXED0: | |
3076 | msr = &vmx->nested.nested_vmx_cr4_fixed0; | |
3077 | break; | |
3078 | default: | |
3079 | BUG(); | |
3080 | } | |
3081 | ||
3082 | /* | |
3083 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) | |
3084 | * must be 1 in the restored value. | |
3085 | */ | |
3086 | if (!is_bitwise_subset(data, *msr, -1ULL)) | |
3087 | return -EINVAL; | |
3088 | ||
3089 | *msr = data; | |
3090 | return 0; | |
3091 | } | |
3092 | ||
3093 | /* | |
3094 | * Called when userspace is restoring VMX MSRs. | |
3095 | * | |
3096 | * Returns 0 on success, non-0 otherwise. | |
3097 | */ | |
3098 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
b87a51ae | 3099 | { |
b9c237bb WV |
3100 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3101 | ||
b87a51ae | 3102 | switch (msr_index) { |
b87a51ae | 3103 | case MSR_IA32_VMX_BASIC: |
62cc6b9d DM |
3104 | return vmx_restore_vmx_basic(vmx, data); |
3105 | case MSR_IA32_VMX_PINBASED_CTLS: | |
3106 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
3107 | case MSR_IA32_VMX_EXIT_CTLS: | |
3108 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b87a51ae | 3109 | /* |
62cc6b9d DM |
3110 | * The "non-true" VMX capability MSRs are generated from the |
3111 | * "true" MSRs, so we do not support restoring them directly. | |
3112 | * | |
3113 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace | |
3114 | * should restore the "true" MSRs with the must-be-1 bits | |
3115 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND | |
3116 | * DEFAULT SETTINGS". | |
b87a51ae | 3117 | */ |
62cc6b9d DM |
3118 | return -EINVAL; |
3119 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3120 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3121 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3122 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3123 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
3124 | return vmx_restore_control_msr(vmx, msr_index, data); | |
3125 | case MSR_IA32_VMX_MISC: | |
3126 | return vmx_restore_vmx_misc(vmx, data); | |
3127 | case MSR_IA32_VMX_CR0_FIXED0: | |
3128 | case MSR_IA32_VMX_CR4_FIXED0: | |
3129 | return vmx_restore_fixed0_msr(vmx, msr_index, data); | |
3130 | case MSR_IA32_VMX_CR0_FIXED1: | |
3131 | case MSR_IA32_VMX_CR4_FIXED1: | |
3132 | /* | |
3133 | * These MSRs are generated based on the vCPU's CPUID, so we | |
3134 | * do not support restoring them directly. | |
3135 | */ | |
3136 | return -EINVAL; | |
3137 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
3138 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); | |
3139 | case MSR_IA32_VMX_VMCS_ENUM: | |
3140 | vmx->nested.nested_vmx_vmcs_enum = data; | |
3141 | return 0; | |
3142 | default: | |
b87a51ae | 3143 | /* |
62cc6b9d | 3144 | * The rest of the VMX capability MSRs do not support restore. |
b87a51ae | 3145 | */ |
62cc6b9d DM |
3146 | return -EINVAL; |
3147 | } | |
3148 | } | |
3149 | ||
3150 | /* Returns 0 on success, non-0 otherwise. */ | |
3151 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
3152 | { | |
3153 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
3154 | ||
3155 | switch (msr_index) { | |
3156 | case MSR_IA32_VMX_BASIC: | |
3157 | *pdata = vmx->nested.nested_vmx_basic; | |
b87a51ae NHE |
3158 | break; |
3159 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3160 | case MSR_IA32_VMX_PINBASED_CTLS: | |
b9c237bb WV |
3161 | *pdata = vmx_control_msr( |
3162 | vmx->nested.nested_vmx_pinbased_ctls_low, | |
3163 | vmx->nested.nested_vmx_pinbased_ctls_high); | |
0115f9cb DM |
3164 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
3165 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3166 | break; |
3167 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3168 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
b9c237bb WV |
3169 | *pdata = vmx_control_msr( |
3170 | vmx->nested.nested_vmx_procbased_ctls_low, | |
3171 | vmx->nested.nested_vmx_procbased_ctls_high); | |
0115f9cb DM |
3172 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
3173 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3174 | break; |
3175 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3176 | case MSR_IA32_VMX_EXIT_CTLS: | |
b9c237bb WV |
3177 | *pdata = vmx_control_msr( |
3178 | vmx->nested.nested_vmx_exit_ctls_low, | |
3179 | vmx->nested.nested_vmx_exit_ctls_high); | |
0115f9cb DM |
3180 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
3181 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3182 | break; |
3183 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3184 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b9c237bb WV |
3185 | *pdata = vmx_control_msr( |
3186 | vmx->nested.nested_vmx_entry_ctls_low, | |
3187 | vmx->nested.nested_vmx_entry_ctls_high); | |
0115f9cb DM |
3188 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
3189 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3190 | break; |
3191 | case MSR_IA32_VMX_MISC: | |
b9c237bb WV |
3192 | *pdata = vmx_control_msr( |
3193 | vmx->nested.nested_vmx_misc_low, | |
3194 | vmx->nested.nested_vmx_misc_high); | |
b87a51ae | 3195 | break; |
b87a51ae | 3196 | case MSR_IA32_VMX_CR0_FIXED0: |
62cc6b9d | 3197 | *pdata = vmx->nested.nested_vmx_cr0_fixed0; |
b87a51ae NHE |
3198 | break; |
3199 | case MSR_IA32_VMX_CR0_FIXED1: | |
62cc6b9d | 3200 | *pdata = vmx->nested.nested_vmx_cr0_fixed1; |
b87a51ae NHE |
3201 | break; |
3202 | case MSR_IA32_VMX_CR4_FIXED0: | |
62cc6b9d | 3203 | *pdata = vmx->nested.nested_vmx_cr4_fixed0; |
b87a51ae NHE |
3204 | break; |
3205 | case MSR_IA32_VMX_CR4_FIXED1: | |
62cc6b9d | 3206 | *pdata = vmx->nested.nested_vmx_cr4_fixed1; |
b87a51ae NHE |
3207 | break; |
3208 | case MSR_IA32_VMX_VMCS_ENUM: | |
62cc6b9d | 3209 | *pdata = vmx->nested.nested_vmx_vmcs_enum; |
b87a51ae NHE |
3210 | break; |
3211 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
b9c237bb WV |
3212 | *pdata = vmx_control_msr( |
3213 | vmx->nested.nested_vmx_secondary_ctls_low, | |
3214 | vmx->nested.nested_vmx_secondary_ctls_high); | |
b87a51ae NHE |
3215 | break; |
3216 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
089d7b6e WL |
3217 | *pdata = vmx->nested.nested_vmx_ept_caps | |
3218 | ((u64)vmx->nested.nested_vmx_vpid_caps << 32); | |
b87a51ae NHE |
3219 | break; |
3220 | default: | |
b87a51ae | 3221 | return 1; |
b3897a49 NHE |
3222 | } |
3223 | ||
b87a51ae NHE |
3224 | return 0; |
3225 | } | |
3226 | ||
37e4c997 HZ |
3227 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
3228 | uint64_t val) | |
3229 | { | |
3230 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; | |
3231 | ||
3232 | return !(val & ~valid_bits); | |
3233 | } | |
3234 | ||
6aa8b732 AK |
3235 | /* |
3236 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
3237 | * Returns 0 on success, non-0 otherwise. | |
3238 | * Assumes vcpu_load() was already called. | |
3239 | */ | |
609e36d3 | 3240 | static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3241 | { |
26bb0981 | 3242 | struct shared_msr_entry *msr; |
6aa8b732 | 3243 | |
609e36d3 | 3244 | switch (msr_info->index) { |
05b3e0c2 | 3245 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3246 | case MSR_FS_BASE: |
609e36d3 | 3247 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
6aa8b732 AK |
3248 | break; |
3249 | case MSR_GS_BASE: | |
609e36d3 | 3250 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
6aa8b732 | 3251 | break; |
44ea2b17 AK |
3252 | case MSR_KERNEL_GS_BASE: |
3253 | vmx_load_host_state(to_vmx(vcpu)); | |
609e36d3 | 3254 | msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
44ea2b17 | 3255 | break; |
26bb0981 | 3256 | #endif |
6aa8b732 | 3257 | case MSR_EFER: |
609e36d3 | 3258 | return kvm_get_msr_common(vcpu, msr_info); |
af24a4e4 | 3259 | case MSR_IA32_TSC: |
be7b263e | 3260 | msr_info->data = guest_read_tsc(vcpu); |
6aa8b732 | 3261 | break; |
95502b8b WW |
3262 | case MSR_IA32_SPEC_CTRL: |
3263 | msr_info->data = vcpu->arch.spec_ctrl; | |
3264 | break; | |
6aa8b732 | 3265 | case MSR_IA32_SYSENTER_CS: |
609e36d3 | 3266 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
6aa8b732 AK |
3267 | break; |
3268 | case MSR_IA32_SYSENTER_EIP: | |
609e36d3 | 3269 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
6aa8b732 AK |
3270 | break; |
3271 | case MSR_IA32_SYSENTER_ESP: | |
609e36d3 | 3272 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
6aa8b732 | 3273 | break; |
0dd376e7 | 3274 | case MSR_IA32_BNDCFGS: |
691bd434 HZ |
3275 | if (!kvm_mpx_supported() || |
3276 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) | |
93c4adc7 | 3277 | return 1; |
609e36d3 | 3278 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
0dd376e7 | 3279 | break; |
c45dcc71 AR |
3280 | case MSR_IA32_MCG_EXT_CTL: |
3281 | if (!msr_info->host_initiated && | |
3282 | !(to_vmx(vcpu)->msr_ia32_feature_control & | |
3283 | FEATURE_CONTROL_LMCE)) | |
cae50139 | 3284 | return 1; |
c45dcc71 AR |
3285 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
3286 | break; | |
cae50139 | 3287 | case MSR_IA32_FEATURE_CONTROL: |
3b84080b | 3288 | msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control; |
cae50139 JK |
3289 | break; |
3290 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
3291 | if (!nested_vmx_allowed(vcpu)) | |
3292 | return 1; | |
609e36d3 | 3293 | return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data); |
20300099 WL |
3294 | case MSR_IA32_XSS: |
3295 | if (!vmx_xsaves_supported()) | |
3296 | return 1; | |
609e36d3 | 3297 | msr_info->data = vcpu->arch.ia32_xss; |
20300099 | 3298 | break; |
4e47c7a6 | 3299 | case MSR_TSC_AUX: |
81b1b9ca | 3300 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
4e47c7a6 SY |
3301 | return 1; |
3302 | /* Otherwise falls through */ | |
6aa8b732 | 3303 | default: |
609e36d3 | 3304 | msr = find_msr_entry(to_vmx(vcpu), msr_info->index); |
3bab1f5d | 3305 | if (msr) { |
609e36d3 | 3306 | msr_info->data = msr->data; |
3bab1f5d | 3307 | break; |
6aa8b732 | 3308 | } |
609e36d3 | 3309 | return kvm_get_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3310 | } |
3311 | ||
6aa8b732 AK |
3312 | return 0; |
3313 | } | |
3314 | ||
cae50139 JK |
3315 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
3316 | ||
6aa8b732 AK |
3317 | /* |
3318 | * Writes msr value into into the appropriate "register". | |
3319 | * Returns 0 on success, non-0 otherwise. | |
3320 | * Assumes vcpu_load() was already called. | |
3321 | */ | |
8fe8ab46 | 3322 | static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3323 | { |
a2fa3e9f | 3324 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 3325 | struct shared_msr_entry *msr; |
2cc51560 | 3326 | int ret = 0; |
8fe8ab46 WA |
3327 | u32 msr_index = msr_info->index; |
3328 | u64 data = msr_info->data; | |
2cc51560 | 3329 | |
6aa8b732 | 3330 | switch (msr_index) { |
3bab1f5d | 3331 | case MSR_EFER: |
8fe8ab46 | 3332 | ret = kvm_set_msr_common(vcpu, msr_info); |
2cc51560 | 3333 | break; |
16175a79 | 3334 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3335 | case MSR_FS_BASE: |
2fb92db1 | 3336 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3337 | vmcs_writel(GUEST_FS_BASE, data); |
3338 | break; | |
3339 | case MSR_GS_BASE: | |
2fb92db1 | 3340 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3341 | vmcs_writel(GUEST_GS_BASE, data); |
3342 | break; | |
44ea2b17 AK |
3343 | case MSR_KERNEL_GS_BASE: |
3344 | vmx_load_host_state(vmx); | |
3345 | vmx->msr_guest_kernel_gs_base = data; | |
3346 | break; | |
6aa8b732 AK |
3347 | #endif |
3348 | case MSR_IA32_SYSENTER_CS: | |
3349 | vmcs_write32(GUEST_SYSENTER_CS, data); | |
3350 | break; | |
3351 | case MSR_IA32_SYSENTER_EIP: | |
f5b42c33 | 3352 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
6aa8b732 AK |
3353 | break; |
3354 | case MSR_IA32_SYSENTER_ESP: | |
f5b42c33 | 3355 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
6aa8b732 | 3356 | break; |
0dd376e7 | 3357 | case MSR_IA32_BNDCFGS: |
691bd434 HZ |
3358 | if (!kvm_mpx_supported() || |
3359 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) | |
93c4adc7 | 3360 | return 1; |
4531662d JM |
3361 | if (is_noncanonical_address(data & PAGE_MASK) || |
3362 | (data & MSR_IA32_BNDCFGS_RSVD)) | |
93c4adc7 | 3363 | return 1; |
0dd376e7 LJ |
3364 | vmcs_write64(GUEST_BNDCFGS, data); |
3365 | break; | |
af24a4e4 | 3366 | case MSR_IA32_TSC: |
8fe8ab46 | 3367 | kvm_write_tsc(vcpu, msr_info); |
6aa8b732 | 3368 | break; |
95502b8b | 3369 | case MSR_IA32_SPEC_CTRL: |
b71bbdaf KRW |
3370 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_RDS)) |
3371 | return 1; | |
3372 | ||
95502b8b WW |
3373 | vcpu->arch.spec_ctrl = msr_info->data; |
3374 | break; | |
468d472f SY |
3375 | case MSR_IA32_CR_PAT: |
3376 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { | |
4566654b NA |
3377 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
3378 | return 1; | |
468d472f SY |
3379 | vmcs_write64(GUEST_IA32_PAT, data); |
3380 | vcpu->arch.pat = data; | |
3381 | break; | |
3382 | } | |
8fe8ab46 | 3383 | ret = kvm_set_msr_common(vcpu, msr_info); |
4e47c7a6 | 3384 | break; |
ba904635 WA |
3385 | case MSR_IA32_TSC_ADJUST: |
3386 | ret = kvm_set_msr_common(vcpu, msr_info); | |
4e47c7a6 | 3387 | break; |
c45dcc71 AR |
3388 | case MSR_IA32_MCG_EXT_CTL: |
3389 | if ((!msr_info->host_initiated && | |
3390 | !(to_vmx(vcpu)->msr_ia32_feature_control & | |
3391 | FEATURE_CONTROL_LMCE)) || | |
3392 | (data & ~MCG_EXT_CTL_LMCE_EN)) | |
3393 | return 1; | |
3394 | vcpu->arch.mcg_ext_ctl = data; | |
3395 | break; | |
cae50139 | 3396 | case MSR_IA32_FEATURE_CONTROL: |
37e4c997 | 3397 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
3b84080b | 3398 | (to_vmx(vcpu)->msr_ia32_feature_control & |
cae50139 JK |
3399 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
3400 | return 1; | |
3b84080b | 3401 | vmx->msr_ia32_feature_control = data; |
cae50139 JK |
3402 | if (msr_info->host_initiated && data == 0) |
3403 | vmx_leave_nested(vcpu); | |
3404 | break; | |
3405 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
62cc6b9d DM |
3406 | if (!msr_info->host_initiated) |
3407 | return 1; /* they are read-only */ | |
3408 | if (!nested_vmx_allowed(vcpu)) | |
3409 | return 1; | |
3410 | return vmx_set_vmx_msr(vcpu, msr_index, data); | |
20300099 WL |
3411 | case MSR_IA32_XSS: |
3412 | if (!vmx_xsaves_supported()) | |
3413 | return 1; | |
3414 | /* | |
3415 | * The only supported bit as of Skylake is bit 8, but | |
3416 | * it is not supported on KVM. | |
3417 | */ | |
3418 | if (data != 0) | |
3419 | return 1; | |
3420 | vcpu->arch.ia32_xss = data; | |
3421 | if (vcpu->arch.ia32_xss != host_xss) | |
3422 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, | |
3423 | vcpu->arch.ia32_xss, host_xss); | |
3424 | else | |
3425 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); | |
3426 | break; | |
4e47c7a6 | 3427 | case MSR_TSC_AUX: |
81b1b9ca | 3428 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
4e47c7a6 SY |
3429 | return 1; |
3430 | /* Check reserved bit, higher 32 bits should be zero */ | |
3431 | if ((data >> 32) != 0) | |
3432 | return 1; | |
3433 | /* Otherwise falls through */ | |
6aa8b732 | 3434 | default: |
8b9cf98c | 3435 | msr = find_msr_entry(vmx, msr_index); |
3bab1f5d | 3436 | if (msr) { |
8b3c3104 | 3437 | u64 old_msr_data = msr->data; |
3bab1f5d | 3438 | msr->data = data; |
2225fd56 AK |
3439 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
3440 | preempt_disable(); | |
8b3c3104 AH |
3441 | ret = kvm_set_shared_msr(msr->index, msr->data, |
3442 | msr->mask); | |
2225fd56 | 3443 | preempt_enable(); |
8b3c3104 AH |
3444 | if (ret) |
3445 | msr->data = old_msr_data; | |
2225fd56 | 3446 | } |
3bab1f5d | 3447 | break; |
6aa8b732 | 3448 | } |
8fe8ab46 | 3449 | ret = kvm_set_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3450 | } |
3451 | ||
2cc51560 | 3452 | return ret; |
6aa8b732 AK |
3453 | } |
3454 | ||
5fdbf976 | 3455 | static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
6aa8b732 | 3456 | { |
5fdbf976 MT |
3457 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
3458 | switch (reg) { | |
3459 | case VCPU_REGS_RSP: | |
3460 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); | |
3461 | break; | |
3462 | case VCPU_REGS_RIP: | |
3463 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); | |
3464 | break; | |
6de4f3ad AK |
3465 | case VCPU_EXREG_PDPTR: |
3466 | if (enable_ept) | |
3467 | ept_save_pdptrs(vcpu); | |
3468 | break; | |
5fdbf976 MT |
3469 | default: |
3470 | break; | |
3471 | } | |
6aa8b732 AK |
3472 | } |
3473 | ||
6aa8b732 AK |
3474 | static __init int cpu_has_kvm_support(void) |
3475 | { | |
6210e37b | 3476 | return cpu_has_vmx(); |
6aa8b732 AK |
3477 | } |
3478 | ||
3479 | static __init int vmx_disabled_by_bios(void) | |
3480 | { | |
3481 | u64 msr; | |
3482 | ||
3483 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); | |
cafd6659 | 3484 | if (msr & FEATURE_CONTROL_LOCKED) { |
23f3e991 | 3485 | /* launched w/ TXT and VMX disabled */ |
cafd6659 SW |
3486 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
3487 | && tboot_enabled()) | |
3488 | return 1; | |
23f3e991 | 3489 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
cafd6659 | 3490 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
23f3e991 | 3491 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
f9335afe SW |
3492 | && !tboot_enabled()) { |
3493 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " | |
23f3e991 | 3494 | "activate TXT before enabling KVM\n"); |
cafd6659 | 3495 | return 1; |
f9335afe | 3496 | } |
23f3e991 JC |
3497 | /* launched w/o TXT and VMX disabled */ |
3498 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) | |
3499 | && !tboot_enabled()) | |
3500 | return 1; | |
cafd6659 SW |
3501 | } |
3502 | ||
3503 | return 0; | |
6aa8b732 AK |
3504 | } |
3505 | ||
7725b894 DX |
3506 | static void kvm_cpu_vmxon(u64 addr) |
3507 | { | |
fe0e80be | 3508 | cr4_set_bits(X86_CR4_VMXE); |
1c5ac21a AS |
3509 | intel_pt_handle_vmx(1); |
3510 | ||
7725b894 DX |
3511 | asm volatile (ASM_VMX_VMXON_RAX |
3512 | : : "a"(&addr), "m"(addr) | |
3513 | : "memory", "cc"); | |
3514 | } | |
3515 | ||
13a34e06 | 3516 | static int hardware_enable(void) |
6aa8b732 AK |
3517 | { |
3518 | int cpu = raw_smp_processor_id(); | |
3519 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); | |
cafd6659 | 3520 | u64 old, test_bits; |
6aa8b732 | 3521 | |
1e02ce4c | 3522 | if (cr4_read_shadow() & X86_CR4_VMXE) |
10474ae8 AG |
3523 | return -EBUSY; |
3524 | ||
d462b819 | 3525 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
bf9f6ac8 FW |
3526 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
3527 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
8f536b76 ZY |
3528 | |
3529 | /* | |
3530 | * Now we can enable the vmclear operation in kdump | |
3531 | * since the loaded_vmcss_on_cpu list on this cpu | |
3532 | * has been initialized. | |
3533 | * | |
3534 | * Though the cpu is not in VMX operation now, there | |
3535 | * is no problem to enable the vmclear operation | |
3536 | * for the loaded_vmcss_on_cpu list is empty! | |
3537 | */ | |
3538 | crash_enable_local_vmclear(cpu); | |
3539 | ||
6aa8b732 | 3540 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
cafd6659 SW |
3541 | |
3542 | test_bits = FEATURE_CONTROL_LOCKED; | |
3543 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
3544 | if (tboot_enabled()) | |
3545 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; | |
3546 | ||
3547 | if ((old & test_bits) != test_bits) { | |
6aa8b732 | 3548 | /* enable and lock */ |
cafd6659 SW |
3549 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
3550 | } | |
fe0e80be DH |
3551 | kvm_cpu_vmxon(phys_addr); |
3552 | ept_sync_global(); | |
10474ae8 AG |
3553 | |
3554 | return 0; | |
6aa8b732 AK |
3555 | } |
3556 | ||
d462b819 | 3557 | static void vmclear_local_loaded_vmcss(void) |
543e4243 AK |
3558 | { |
3559 | int cpu = raw_smp_processor_id(); | |
d462b819 | 3560 | struct loaded_vmcs *v, *n; |
543e4243 | 3561 | |
d462b819 NHE |
3562 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
3563 | loaded_vmcss_on_cpu_link) | |
3564 | __loaded_vmcs_clear(v); | |
543e4243 AK |
3565 | } |
3566 | ||
710ff4a8 EH |
3567 | |
3568 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() | |
3569 | * tricks. | |
3570 | */ | |
3571 | static void kvm_cpu_vmxoff(void) | |
6aa8b732 | 3572 | { |
4ecac3fd | 3573 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
1c5ac21a AS |
3574 | |
3575 | intel_pt_handle_vmx(0); | |
fe0e80be | 3576 | cr4_clear_bits(X86_CR4_VMXE); |
6aa8b732 AK |
3577 | } |
3578 | ||
13a34e06 | 3579 | static void hardware_disable(void) |
710ff4a8 | 3580 | { |
fe0e80be DH |
3581 | vmclear_local_loaded_vmcss(); |
3582 | kvm_cpu_vmxoff(); | |
710ff4a8 EH |
3583 | } |
3584 | ||
1c3d14fe | 3585 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
d77c26fc | 3586 | u32 msr, u32 *result) |
1c3d14fe YS |
3587 | { |
3588 | u32 vmx_msr_low, vmx_msr_high; | |
3589 | u32 ctl = ctl_min | ctl_opt; | |
3590 | ||
3591 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3592 | ||
3593 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ | |
3594 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ | |
3595 | ||
3596 | /* Ensure minimum (required) set of control bits are supported. */ | |
3597 | if (ctl_min & ~ctl) | |
002c7f7c | 3598 | return -EIO; |
1c3d14fe YS |
3599 | |
3600 | *result = ctl; | |
3601 | return 0; | |
3602 | } | |
3603 | ||
110312c8 AK |
3604 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
3605 | { | |
3606 | u32 vmx_msr_low, vmx_msr_high; | |
3607 | ||
3608 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3609 | return vmx_msr_high & ctl; | |
3610 | } | |
3611 | ||
002c7f7c | 3612 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
6aa8b732 AK |
3613 | { |
3614 | u32 vmx_msr_low, vmx_msr_high; | |
d56f546d | 3615 | u32 min, opt, min2, opt2; |
1c3d14fe YS |
3616 | u32 _pin_based_exec_control = 0; |
3617 | u32 _cpu_based_exec_control = 0; | |
f78e0e2e | 3618 | u32 _cpu_based_2nd_exec_control = 0; |
1c3d14fe YS |
3619 | u32 _vmexit_control = 0; |
3620 | u32 _vmentry_control = 0; | |
3621 | ||
10166744 | 3622 | min = CPU_BASED_HLT_EXITING | |
1c3d14fe YS |
3623 | #ifdef CONFIG_X86_64 |
3624 | CPU_BASED_CR8_LOAD_EXITING | | |
3625 | CPU_BASED_CR8_STORE_EXITING | | |
3626 | #endif | |
d56f546d SY |
3627 | CPU_BASED_CR3_LOAD_EXITING | |
3628 | CPU_BASED_CR3_STORE_EXITING | | |
1c3d14fe YS |
3629 | CPU_BASED_USE_IO_BITMAPS | |
3630 | CPU_BASED_MOV_DR_EXITING | | |
a7052897 | 3631 | CPU_BASED_USE_TSC_OFFSETING | |
fee84b07 AK |
3632 | CPU_BASED_INVLPG_EXITING | |
3633 | CPU_BASED_RDPMC_EXITING; | |
443381a8 | 3634 | |
668fffa3 MT |
3635 | if (!kvm_mwait_in_guest()) |
3636 | min |= CPU_BASED_MWAIT_EXITING | | |
3637 | CPU_BASED_MONITOR_EXITING; | |
3638 | ||
f78e0e2e | 3639 | opt = CPU_BASED_TPR_SHADOW | |
25c5f225 | 3640 | CPU_BASED_USE_MSR_BITMAPS | |
f78e0e2e | 3641 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
1c3d14fe YS |
3642 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
3643 | &_cpu_based_exec_control) < 0) | |
002c7f7c | 3644 | return -EIO; |
6e5d865c YS |
3645 | #ifdef CONFIG_X86_64 |
3646 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
3647 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & | |
3648 | ~CPU_BASED_CR8_STORE_EXITING; | |
3649 | #endif | |
f78e0e2e | 3650 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
d56f546d SY |
3651 | min2 = 0; |
3652 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | | |
8d14695f | 3653 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
2384d2b3 | 3654 | SECONDARY_EXEC_WBINVD_EXITING | |
d56f546d | 3655 | SECONDARY_EXEC_ENABLE_VPID | |
3a624e29 | 3656 | SECONDARY_EXEC_ENABLE_EPT | |
4b8d54f9 | 3657 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
4e47c7a6 | 3658 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
ad756a16 | 3659 | SECONDARY_EXEC_RDTSCP | |
83d4c286 | 3660 | SECONDARY_EXEC_ENABLE_INVPCID | |
c7c9c56c | 3661 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
abc4fc58 | 3662 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
20300099 | 3663 | SECONDARY_EXEC_SHADOW_VMCS | |
843e4330 | 3664 | SECONDARY_EXEC_XSAVES | |
8b3e34e4 | 3665 | SECONDARY_EXEC_ENABLE_PML | |
64903d61 | 3666 | SECONDARY_EXEC_TSC_SCALING; |
d56f546d SY |
3667 | if (adjust_vmx_controls(min2, opt2, |
3668 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
f78e0e2e SY |
3669 | &_cpu_based_2nd_exec_control) < 0) |
3670 | return -EIO; | |
3671 | } | |
3672 | #ifndef CONFIG_X86_64 | |
3673 | if (!(_cpu_based_2nd_exec_control & | |
3674 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
3675 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; | |
3676 | #endif | |
83d4c286 YZ |
3677 | |
3678 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
3679 | _cpu_based_2nd_exec_control &= ~( | |
8d14695f | 3680 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
c7c9c56c YZ |
3681 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
3682 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
83d4c286 | 3683 | |
d56f546d | 3684 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
a7052897 MT |
3685 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
3686 | enabled */ | |
5fff7d27 GN |
3687 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
3688 | CPU_BASED_CR3_STORE_EXITING | | |
3689 | CPU_BASED_INVLPG_EXITING); | |
d56f546d SY |
3690 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
3691 | vmx_capability.ept, vmx_capability.vpid); | |
3692 | } | |
1c3d14fe | 3693 | |
91fa0f8e | 3694 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
1c3d14fe YS |
3695 | #ifdef CONFIG_X86_64 |
3696 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; | |
3697 | #endif | |
a547c6db | 3698 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
91fa0f8e | 3699 | VM_EXIT_CLEAR_BNDCFGS; |
1c3d14fe YS |
3700 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
3701 | &_vmexit_control) < 0) | |
002c7f7c | 3702 | return -EIO; |
1c3d14fe | 3703 | |
9338f80f PB |
3704 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
3705 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | | |
3706 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
3707 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
3708 | &_pin_based_exec_control) < 0) | |
3709 | return -EIO; | |
3710 | ||
1c17c3e6 PB |
3711 | if (cpu_has_broken_vmx_preemption_timer()) |
3712 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be | 3713 | if (!(_cpu_based_2nd_exec_control & |
91fa0f8e | 3714 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
01e439be YZ |
3715 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
3716 | ||
c845f9c6 | 3717 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
da8999d3 | 3718 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
1c3d14fe YS |
3719 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
3720 | &_vmentry_control) < 0) | |
002c7f7c | 3721 | return -EIO; |
6aa8b732 | 3722 | |
c68876fd | 3723 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
1c3d14fe YS |
3724 | |
3725 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ | |
3726 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) | |
002c7f7c | 3727 | return -EIO; |
1c3d14fe YS |
3728 | |
3729 | #ifdef CONFIG_X86_64 | |
3730 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ | |
3731 | if (vmx_msr_high & (1u<<16)) | |
002c7f7c | 3732 | return -EIO; |
1c3d14fe YS |
3733 | #endif |
3734 | ||
3735 | /* Require Write-Back (WB) memory type for VMCS accesses. */ | |
3736 | if (((vmx_msr_high >> 18) & 15) != 6) | |
002c7f7c | 3737 | return -EIO; |
1c3d14fe | 3738 | |
002c7f7c | 3739 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
16cb0255 | 3740 | vmcs_conf->order = get_order(vmcs_conf->size); |
9ac7e3e8 | 3741 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
002c7f7c | 3742 | vmcs_conf->revision_id = vmx_msr_low; |
1c3d14fe | 3743 | |
002c7f7c YS |
3744 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
3745 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; | |
f78e0e2e | 3746 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
002c7f7c YS |
3747 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
3748 | vmcs_conf->vmentry_ctrl = _vmentry_control; | |
1c3d14fe | 3749 | |
110312c8 AK |
3750 | cpu_has_load_ia32_efer = |
3751 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
3752 | VM_ENTRY_LOAD_IA32_EFER) | |
3753 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
3754 | VM_EXIT_LOAD_IA32_EFER); | |
3755 | ||
8bf00a52 GN |
3756 | cpu_has_load_perf_global_ctrl = |
3757 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
3758 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
3759 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
3760 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
3761 | ||
3762 | /* | |
3763 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL | |
bb3541f1 | 3764 | * but due to errata below it can't be used. Workaround is to use |
8bf00a52 GN |
3765 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
3766 | * | |
3767 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] | |
3768 | * | |
3769 | * AAK155 (model 26) | |
3770 | * AAP115 (model 30) | |
3771 | * AAT100 (model 37) | |
3772 | * BC86,AAY89,BD102 (model 44) | |
3773 | * BA97 (model 46) | |
3774 | * | |
3775 | */ | |
3776 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { | |
3777 | switch (boot_cpu_data.x86_model) { | |
3778 | case 26: | |
3779 | case 30: | |
3780 | case 37: | |
3781 | case 44: | |
3782 | case 46: | |
3783 | cpu_has_load_perf_global_ctrl = false; | |
3784 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " | |
3785 | "does not work properly. Using workaround\n"); | |
3786 | break; | |
3787 | default: | |
3788 | break; | |
3789 | } | |
3790 | } | |
3791 | ||
782511b0 | 3792 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
20300099 WL |
3793 | rdmsrl(MSR_IA32_XSS, host_xss); |
3794 | ||
1c3d14fe | 3795 | return 0; |
c68876fd | 3796 | } |
6aa8b732 AK |
3797 | |
3798 | static struct vmcs *alloc_vmcs_cpu(int cpu) | |
3799 | { | |
3800 | int node = cpu_to_node(cpu); | |
3801 | struct page *pages; | |
3802 | struct vmcs *vmcs; | |
3803 | ||
96db800f | 3804 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
6aa8b732 AK |
3805 | if (!pages) |
3806 | return NULL; | |
3807 | vmcs = page_address(pages); | |
1c3d14fe YS |
3808 | memset(vmcs, 0, vmcs_config.size); |
3809 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ | |
6aa8b732 AK |
3810 | return vmcs; |
3811 | } | |
3812 | ||
3813 | static struct vmcs *alloc_vmcs(void) | |
3814 | { | |
d3b2c338 | 3815 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
6aa8b732 AK |
3816 | } |
3817 | ||
3818 | static void free_vmcs(struct vmcs *vmcs) | |
3819 | { | |
1c3d14fe | 3820 | free_pages((unsigned long)vmcs, vmcs_config.order); |
6aa8b732 AK |
3821 | } |
3822 | ||
d462b819 NHE |
3823 | /* |
3824 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded | |
3825 | */ | |
3826 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) | |
3827 | { | |
3828 | if (!loaded_vmcs->vmcs) | |
3829 | return; | |
3830 | loaded_vmcs_clear(loaded_vmcs); | |
3831 | free_vmcs(loaded_vmcs->vmcs); | |
3832 | loaded_vmcs->vmcs = NULL; | |
355f4fb1 | 3833 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
d462b819 NHE |
3834 | } |
3835 | ||
39959588 | 3836 | static void free_kvm_area(void) |
6aa8b732 AK |
3837 | { |
3838 | int cpu; | |
3839 | ||
3230bb47 | 3840 | for_each_possible_cpu(cpu) { |
6aa8b732 | 3841 | free_vmcs(per_cpu(vmxarea, cpu)); |
3230bb47 ZA |
3842 | per_cpu(vmxarea, cpu) = NULL; |
3843 | } | |
6aa8b732 AK |
3844 | } |
3845 | ||
85fd514e JM |
3846 | enum vmcs_field_type { |
3847 | VMCS_FIELD_TYPE_U16 = 0, | |
3848 | VMCS_FIELD_TYPE_U64 = 1, | |
3849 | VMCS_FIELD_TYPE_U32 = 2, | |
3850 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 | |
3851 | }; | |
3852 | ||
3853 | static inline int vmcs_field_type(unsigned long field) | |
3854 | { | |
3855 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ | |
3856 | return VMCS_FIELD_TYPE_U32; | |
3857 | return (field >> 13) & 0x3 ; | |
3858 | } | |
3859 | ||
3860 | static inline int vmcs_field_readonly(unsigned long field) | |
3861 | { | |
3862 | return (((field >> 10) & 0x3) == 1); | |
3863 | } | |
3864 | ||
fe2b201b BD |
3865 | static void init_vmcs_shadow_fields(void) |
3866 | { | |
3867 | int i, j; | |
3868 | ||
3869 | /* No checks for read only fields yet */ | |
3870 | ||
3871 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { | |
3872 | switch (shadow_read_write_fields[i]) { | |
3873 | case GUEST_BNDCFGS: | |
a87036ad | 3874 | if (!kvm_mpx_supported()) |
fe2b201b BD |
3875 | continue; |
3876 | break; | |
3877 | default: | |
3878 | break; | |
3879 | } | |
3880 | ||
3881 | if (j < i) | |
3882 | shadow_read_write_fields[j] = | |
3883 | shadow_read_write_fields[i]; | |
3884 | j++; | |
3885 | } | |
3886 | max_shadow_read_write_fields = j; | |
3887 | ||
3888 | /* shadowed fields guest access without vmexit */ | |
3889 | for (i = 0; i < max_shadow_read_write_fields; i++) { | |
85fd514e JM |
3890 | unsigned long field = shadow_read_write_fields[i]; |
3891 | ||
3892 | clear_bit(field, vmx_vmwrite_bitmap); | |
3893 | clear_bit(field, vmx_vmread_bitmap); | |
3894 | if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) { | |
3895 | clear_bit(field + 1, vmx_vmwrite_bitmap); | |
3896 | clear_bit(field + 1, vmx_vmread_bitmap); | |
3897 | } | |
3898 | } | |
3899 | for (i = 0; i < max_shadow_read_only_fields; i++) { | |
3900 | unsigned long field = shadow_read_only_fields[i]; | |
3901 | ||
3902 | clear_bit(field, vmx_vmread_bitmap); | |
3903 | if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) | |
3904 | clear_bit(field + 1, vmx_vmread_bitmap); | |
fe2b201b | 3905 | } |
fe2b201b BD |
3906 | } |
3907 | ||
6aa8b732 AK |
3908 | static __init int alloc_kvm_area(void) |
3909 | { | |
3910 | int cpu; | |
3911 | ||
3230bb47 | 3912 | for_each_possible_cpu(cpu) { |
6aa8b732 AK |
3913 | struct vmcs *vmcs; |
3914 | ||
3915 | vmcs = alloc_vmcs_cpu(cpu); | |
3916 | if (!vmcs) { | |
3917 | free_kvm_area(); | |
3918 | return -ENOMEM; | |
3919 | } | |
3920 | ||
3921 | per_cpu(vmxarea, cpu) = vmcs; | |
3922 | } | |
3923 | return 0; | |
3924 | } | |
3925 | ||
91b0aa2c | 3926 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
d99e4152 | 3927 | struct kvm_segment *save) |
6aa8b732 | 3928 | { |
d99e4152 GN |
3929 | if (!emulate_invalid_guest_state) { |
3930 | /* | |
3931 | * CS and SS RPL should be equal during guest entry according | |
3932 | * to VMX spec, but in reality it is not always so. Since vcpu | |
3933 | * is in the middle of the transition from real mode to | |
3934 | * protected mode it is safe to assume that RPL 0 is a good | |
3935 | * default value. | |
3936 | */ | |
3937 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) | |
b32a9918 NA |
3938 | save->selector &= ~SEGMENT_RPL_MASK; |
3939 | save->dpl = save->selector & SEGMENT_RPL_MASK; | |
d99e4152 | 3940 | save->s = 1; |
6aa8b732 | 3941 | } |
d99e4152 | 3942 | vmx_set_segment(vcpu, save, seg); |
6aa8b732 AK |
3943 | } |
3944 | ||
3945 | static void enter_pmode(struct kvm_vcpu *vcpu) | |
3946 | { | |
3947 | unsigned long flags; | |
a89a8fb9 | 3948 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 | 3949 | |
d99e4152 GN |
3950 | /* |
3951 | * Update real mode segment cache. It may be not up-to-date if sement | |
3952 | * register was written while vcpu was in a guest mode. | |
3953 | */ | |
3954 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
3955 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
3956 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
3957 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
3958 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); | |
3959 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
3960 | ||
7ffd92c5 | 3961 | vmx->rmode.vm86_active = 0; |
6aa8b732 | 3962 | |
2fb92db1 AK |
3963 | vmx_segment_cache_clear(vmx); |
3964 | ||
f5f7b2fe | 3965 | vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
6aa8b732 AK |
3966 | |
3967 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 AK |
3968 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
3969 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
6aa8b732 AK |
3970 | vmcs_writel(GUEST_RFLAGS, flags); |
3971 | ||
66aee91a RR |
3972 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
3973 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); | |
6aa8b732 AK |
3974 | |
3975 | update_exception_bitmap(vcpu); | |
3976 | ||
91b0aa2c GN |
3977 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
3978 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); | |
3979 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
3980 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
3981 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
3982 | fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
6aa8b732 AK |
3983 | } |
3984 | ||
f5f7b2fe | 3985 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
6aa8b732 | 3986 | { |
772e0318 | 3987 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
d99e4152 GN |
3988 | struct kvm_segment var = *save; |
3989 | ||
3990 | var.dpl = 0x3; | |
3991 | if (seg == VCPU_SREG_CS) | |
3992 | var.type = 0x3; | |
3993 | ||
3994 | if (!emulate_invalid_guest_state) { | |
3995 | var.selector = var.base >> 4; | |
3996 | var.base = var.base & 0xffff0; | |
3997 | var.limit = 0xffff; | |
3998 | var.g = 0; | |
3999 | var.db = 0; | |
4000 | var.present = 1; | |
4001 | var.s = 1; | |
4002 | var.l = 0; | |
4003 | var.unusable = 0; | |
4004 | var.type = 0x3; | |
4005 | var.avl = 0; | |
4006 | if (save->base & 0xf) | |
4007 | printk_once(KERN_WARNING "kvm: segment base is not " | |
4008 | "paragraph aligned when entering " | |
4009 | "protected mode (seg=%d)", seg); | |
4010 | } | |
6aa8b732 | 4011 | |
d99e4152 | 4012 | vmcs_write16(sf->selector, var.selector); |
96794e4e | 4013 | vmcs_writel(sf->base, var.base); |
d99e4152 GN |
4014 | vmcs_write32(sf->limit, var.limit); |
4015 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); | |
6aa8b732 AK |
4016 | } |
4017 | ||
4018 | static void enter_rmode(struct kvm_vcpu *vcpu) | |
4019 | { | |
4020 | unsigned long flags; | |
a89a8fb9 | 4021 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 | 4022 | |
f5f7b2fe AK |
4023 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
4024 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
4025 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
4026 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
4027 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
c6ad1153 GN |
4028 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
4029 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
f5f7b2fe | 4030 | |
7ffd92c5 | 4031 | vmx->rmode.vm86_active = 1; |
6aa8b732 | 4032 | |
776e58ea GN |
4033 | /* |
4034 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering | |
4918c6ca | 4035 | * vcpu. Warn the user that an update is overdue. |
776e58ea | 4036 | */ |
4918c6ca | 4037 | if (!vcpu->kvm->arch.tss_addr) |
776e58ea GN |
4038 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
4039 | "called before entering vcpu\n"); | |
776e58ea | 4040 | |
2fb92db1 AK |
4041 | vmx_segment_cache_clear(vmx); |
4042 | ||
4918c6ca | 4043 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
6aa8b732 | 4044 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
6aa8b732 AK |
4045 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
4046 | ||
4047 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 | 4048 | vmx->rmode.save_rflags = flags; |
6aa8b732 | 4049 | |
053de044 | 4050 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
6aa8b732 AK |
4051 | |
4052 | vmcs_writel(GUEST_RFLAGS, flags); | |
66aee91a | 4053 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
6aa8b732 AK |
4054 | update_exception_bitmap(vcpu); |
4055 | ||
d99e4152 GN |
4056 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
4057 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); | |
4058 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
4059 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
4060 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
4061 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
b246dd5d | 4062 | |
8668a3c4 | 4063 | kvm_mmu_reset_context(vcpu); |
6aa8b732 AK |
4064 | } |
4065 | ||
401d10de AS |
4066 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
4067 | { | |
4068 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
26bb0981 AK |
4069 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
4070 | ||
4071 | if (!msr) | |
4072 | return; | |
401d10de | 4073 | |
44ea2b17 AK |
4074 | /* |
4075 | * Force kernel_gs_base reloading before EFER changes, as control | |
4076 | * of this msr depends on is_long_mode(). | |
4077 | */ | |
4078 | vmx_load_host_state(to_vmx(vcpu)); | |
f6801dff | 4079 | vcpu->arch.efer = efer; |
401d10de | 4080 | if (efer & EFER_LMA) { |
2961e876 | 4081 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
4082 | msr->data = efer; |
4083 | } else { | |
2961e876 | 4084 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
4085 | |
4086 | msr->data = efer & ~EFER_LME; | |
4087 | } | |
4088 | setup_msrs(vmx); | |
4089 | } | |
4090 | ||
05b3e0c2 | 4091 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
4092 | |
4093 | static void enter_lmode(struct kvm_vcpu *vcpu) | |
4094 | { | |
4095 | u32 guest_tr_ar; | |
4096 | ||
2fb92db1 AK |
4097 | vmx_segment_cache_clear(to_vmx(vcpu)); |
4098 | ||
6aa8b732 | 4099 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
4d283ec9 | 4100 | if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) { |
bd80158a JK |
4101 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
4102 | __func__); | |
6aa8b732 | 4103 | vmcs_write32(GUEST_TR_AR_BYTES, |
4d283ec9 AL |
4104 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
4105 | | VMX_AR_TYPE_BUSY_64_TSS); | |
6aa8b732 | 4106 | } |
da38f438 | 4107 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
6aa8b732 AK |
4108 | } |
4109 | ||
4110 | static void exit_lmode(struct kvm_vcpu *vcpu) | |
4111 | { | |
2961e876 | 4112 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
da38f438 | 4113 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
6aa8b732 AK |
4114 | } |
4115 | ||
4116 | #endif | |
4117 | ||
dd5f5341 | 4118 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid) |
2384d2b3 | 4119 | { |
dd180b3e XG |
4120 | if (enable_ept) { |
4121 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) | |
4122 | return; | |
995f00a6 | 4123 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
f0b98c02 JM |
4124 | } else { |
4125 | vpid_sync_context(vpid); | |
dd180b3e | 4126 | } |
2384d2b3 SY |
4127 | } |
4128 | ||
dd5f5341 WL |
4129 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
4130 | { | |
4131 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid); | |
4132 | } | |
4133 | ||
fb6c8198 JM |
4134 | static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu) |
4135 | { | |
4136 | if (enable_ept) | |
4137 | vmx_flush_tlb(vcpu); | |
4138 | } | |
4139 | ||
e8467fda AK |
4140 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
4141 | { | |
4142 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; | |
4143 | ||
4144 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; | |
4145 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; | |
4146 | } | |
4147 | ||
aff48baa AK |
4148 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
4149 | { | |
4150 | if (enable_ept && is_paging(vcpu)) | |
4151 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); | |
4152 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
4153 | } | |
4154 | ||
25c4c276 | 4155 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
399badf3 | 4156 | { |
fc78f519 AK |
4157 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
4158 | ||
4159 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; | |
4160 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; | |
399badf3 AK |
4161 | } |
4162 | ||
1439442c SY |
4163 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
4164 | { | |
d0d538b9 GN |
4165 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4166 | ||
6de4f3ad AK |
4167 | if (!test_bit(VCPU_EXREG_PDPTR, |
4168 | (unsigned long *)&vcpu->arch.regs_dirty)) | |
4169 | return; | |
4170 | ||
1439442c | 4171 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
d0d538b9 GN |
4172 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
4173 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); | |
4174 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); | |
4175 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); | |
1439442c SY |
4176 | } |
4177 | } | |
4178 | ||
8f5d549f AK |
4179 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
4180 | { | |
d0d538b9 GN |
4181 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4182 | ||
8f5d549f | 4183 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
d0d538b9 GN |
4184 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
4185 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); | |
4186 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); | |
4187 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); | |
8f5d549f | 4188 | } |
6de4f3ad AK |
4189 | |
4190 | __set_bit(VCPU_EXREG_PDPTR, | |
4191 | (unsigned long *)&vcpu->arch.regs_avail); | |
4192 | __set_bit(VCPU_EXREG_PDPTR, | |
4193 | (unsigned long *)&vcpu->arch.regs_dirty); | |
8f5d549f AK |
4194 | } |
4195 | ||
3899152c DM |
4196 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
4197 | { | |
4198 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; | |
4199 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; | |
4200 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
4201 | ||
4202 | if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high & | |
4203 | SECONDARY_EXEC_UNRESTRICTED_GUEST && | |
4204 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) | |
4205 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); | |
4206 | ||
4207 | return fixed_bits_valid(val, fixed0, fixed1); | |
4208 | } | |
4209 | ||
4210 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4211 | { | |
4212 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; | |
4213 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; | |
4214 | ||
4215 | return fixed_bits_valid(val, fixed0, fixed1); | |
4216 | } | |
4217 | ||
4218 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4219 | { | |
4220 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0; | |
4221 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1; | |
4222 | ||
4223 | return fixed_bits_valid(val, fixed0, fixed1); | |
4224 | } | |
4225 | ||
4226 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ | |
4227 | #define nested_guest_cr4_valid nested_cr4_valid | |
4228 | #define nested_host_cr4_valid nested_cr4_valid | |
4229 | ||
5e1746d6 | 4230 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
1439442c SY |
4231 | |
4232 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, | |
4233 | unsigned long cr0, | |
4234 | struct kvm_vcpu *vcpu) | |
4235 | { | |
5233dd51 MT |
4236 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
4237 | vmx_decache_cr3(vcpu); | |
1439442c SY |
4238 | if (!(cr0 & X86_CR0_PG)) { |
4239 | /* From paging/starting to nonpaging */ | |
4240 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4241 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
1439442c SY |
4242 | (CPU_BASED_CR3_LOAD_EXITING | |
4243 | CPU_BASED_CR3_STORE_EXITING)); | |
4244 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4245 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c SY |
4246 | } else if (!is_paging(vcpu)) { |
4247 | /* From nonpaging to paging */ | |
4248 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4249 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
1439442c SY |
4250 | ~(CPU_BASED_CR3_LOAD_EXITING | |
4251 | CPU_BASED_CR3_STORE_EXITING)); | |
4252 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4253 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c | 4254 | } |
95eb84a7 SY |
4255 | |
4256 | if (!(cr0 & X86_CR0_WP)) | |
4257 | *hw_cr0 &= ~X86_CR0_WP; | |
1439442c SY |
4258 | } |
4259 | ||
6aa8b732 AK |
4260 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
4261 | { | |
7ffd92c5 | 4262 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3a624e29 NK |
4263 | unsigned long hw_cr0; |
4264 | ||
5037878e | 4265 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
3a624e29 | 4266 | if (enable_unrestricted_guest) |
5037878e | 4267 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
218e763f | 4268 | else { |
5037878e | 4269 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
1439442c | 4270 | |
218e763f GN |
4271 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
4272 | enter_pmode(vcpu); | |
6aa8b732 | 4273 | |
218e763f GN |
4274 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
4275 | enter_rmode(vcpu); | |
4276 | } | |
6aa8b732 | 4277 | |
05b3e0c2 | 4278 | #ifdef CONFIG_X86_64 |
f6801dff | 4279 | if (vcpu->arch.efer & EFER_LME) { |
707d92fa | 4280 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
6aa8b732 | 4281 | enter_lmode(vcpu); |
707d92fa | 4282 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
6aa8b732 AK |
4283 | exit_lmode(vcpu); |
4284 | } | |
4285 | #endif | |
4286 | ||
089d034e | 4287 | if (enable_ept) |
1439442c SY |
4288 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
4289 | ||
6aa8b732 | 4290 | vmcs_writel(CR0_READ_SHADOW, cr0); |
1439442c | 4291 | vmcs_writel(GUEST_CR0, hw_cr0); |
ad312c7c | 4292 | vcpu->arch.cr0 = cr0; |
14168786 GN |
4293 | |
4294 | /* depends on vcpu->arch.cr0 to be set to a new value */ | |
4295 | vmx->emulation_required = emulation_required(vcpu); | |
6aa8b732 AK |
4296 | } |
4297 | ||
995f00a6 | 4298 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
1439442c SY |
4299 | { |
4300 | u64 eptp; | |
4301 | ||
4302 | /* TODO write the value reading from MSR */ | |
4303 | eptp = VMX_EPT_DEFAULT_MT | | |
4304 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; | |
995f00a6 PF |
4305 | if (enable_ept_ad_bits && |
4306 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) | |
b38f9934 | 4307 | eptp |= VMX_EPT_AD_ENABLE_BIT; |
1439442c SY |
4308 | eptp |= (root_hpa & PAGE_MASK); |
4309 | ||
4310 | return eptp; | |
4311 | } | |
4312 | ||
6aa8b732 AK |
4313 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
4314 | { | |
1439442c SY |
4315 | unsigned long guest_cr3; |
4316 | u64 eptp; | |
4317 | ||
4318 | guest_cr3 = cr3; | |
089d034e | 4319 | if (enable_ept) { |
995f00a6 | 4320 | eptp = construct_eptp(vcpu, cr3); |
1439442c | 4321 | vmcs_write64(EPT_POINTER, eptp); |
59ab5a8f JK |
4322 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
4323 | guest_cr3 = kvm_read_cr3(vcpu); | |
4324 | else | |
4325 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; | |
7c93be44 | 4326 | ept_load_pdptrs(vcpu); |
1439442c SY |
4327 | } |
4328 | ||
2384d2b3 | 4329 | vmx_flush_tlb(vcpu); |
1439442c | 4330 | vmcs_writel(GUEST_CR3, guest_cr3); |
6aa8b732 AK |
4331 | } |
4332 | ||
5e1746d6 | 4333 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
6aa8b732 | 4334 | { |
085e68ee BS |
4335 | /* |
4336 | * Pass through host's Machine Check Enable value to hw_cr4, which | |
4337 | * is in force while we are in guest mode. Do not let guests control | |
4338 | * this bit, even if host CR4.MCE == 0. | |
4339 | */ | |
4340 | unsigned long hw_cr4 = | |
4341 | (cr4_read_shadow() & X86_CR4_MCE) | | |
4342 | (cr4 & ~X86_CR4_MCE) | | |
4343 | (to_vmx(vcpu)->rmode.vm86_active ? | |
4344 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); | |
1439442c | 4345 | |
5e1746d6 NHE |
4346 | if (cr4 & X86_CR4_VMXE) { |
4347 | /* | |
4348 | * To use VMXON (and later other VMX instructions), a guest | |
4349 | * must first be able to turn on cr4.VMXE (see handle_vmon()). | |
4350 | * So basically the check on whether to allow nested VMX | |
4351 | * is here. | |
4352 | */ | |
4353 | if (!nested_vmx_allowed(vcpu)) | |
4354 | return 1; | |
1a0d74e6 | 4355 | } |
3899152c DM |
4356 | |
4357 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) | |
5e1746d6 NHE |
4358 | return 1; |
4359 | ||
ad312c7c | 4360 | vcpu->arch.cr4 = cr4; |
bc23008b AK |
4361 | if (enable_ept) { |
4362 | if (!is_paging(vcpu)) { | |
4363 | hw_cr4 &= ~X86_CR4_PAE; | |
4364 | hw_cr4 |= X86_CR4_PSE; | |
4365 | } else if (!(cr4 & X86_CR4_PAE)) { | |
4366 | hw_cr4 &= ~X86_CR4_PAE; | |
4367 | } | |
4368 | } | |
1439442c | 4369 | |
656ec4a4 RK |
4370 | if (!enable_unrestricted_guest && !is_paging(vcpu)) |
4371 | /* | |
ddba2628 HH |
4372 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
4373 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs | |
4374 | * to be manually disabled when guest switches to non-paging | |
4375 | * mode. | |
4376 | * | |
4377 | * If !enable_unrestricted_guest, the CPU is always running | |
4378 | * with CR0.PG=1 and CR4 needs to be modified. | |
4379 | * If enable_unrestricted_guest, the CPU automatically | |
4380 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. | |
656ec4a4 | 4381 | */ |
ddba2628 | 4382 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
656ec4a4 | 4383 | |
1439442c SY |
4384 | vmcs_writel(CR4_READ_SHADOW, cr4); |
4385 | vmcs_writel(GUEST_CR4, hw_cr4); | |
5e1746d6 | 4386 | return 0; |
6aa8b732 AK |
4387 | } |
4388 | ||
6aa8b732 AK |
4389 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
4390 | struct kvm_segment *var, int seg) | |
4391 | { | |
a9179499 | 4392 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 AK |
4393 | u32 ar; |
4394 | ||
c6ad1153 | 4395 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
f5f7b2fe | 4396 | *var = vmx->rmode.segs[seg]; |
a9179499 | 4397 | if (seg == VCPU_SREG_TR |
2fb92db1 | 4398 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
f5f7b2fe | 4399 | return; |
1390a28b AK |
4400 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4401 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4402 | return; | |
a9179499 | 4403 | } |
2fb92db1 AK |
4404 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4405 | var->limit = vmx_read_guest_seg_limit(vmx, seg); | |
4406 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4407 | ar = vmx_read_guest_seg_ar(vmx, seg); | |
03617c18 | 4408 | var->unusable = (ar >> 16) & 1; |
6aa8b732 AK |
4409 | var->type = ar & 15; |
4410 | var->s = (ar >> 4) & 1; | |
4411 | var->dpl = (ar >> 5) & 3; | |
03617c18 GN |
4412 | /* |
4413 | * Some userspaces do not preserve unusable property. Since usable | |
4414 | * segment has to be present according to VMX spec we can use present | |
4415 | * property to amend userspace bug by making unusable segment always | |
4416 | * nonpresent. vmx_segment_access_rights() already marks nonpresent | |
4417 | * segment as unusable. | |
4418 | */ | |
4419 | var->present = !var->unusable; | |
6aa8b732 AK |
4420 | var->avl = (ar >> 12) & 1; |
4421 | var->l = (ar >> 13) & 1; | |
4422 | var->db = (ar >> 14) & 1; | |
4423 | var->g = (ar >> 15) & 1; | |
6aa8b732 AK |
4424 | } |
4425 | ||
a9179499 AK |
4426 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
4427 | { | |
a9179499 AK |
4428 | struct kvm_segment s; |
4429 | ||
4430 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
4431 | vmx_get_segment(vcpu, &s, seg); | |
4432 | return s.base; | |
4433 | } | |
2fb92db1 | 4434 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
a9179499 AK |
4435 | } |
4436 | ||
b09408d0 | 4437 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
2e4d2653 | 4438 | { |
b09408d0 MT |
4439 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
4440 | ||
ae9fedc7 | 4441 | if (unlikely(vmx->rmode.vm86_active)) |
2e4d2653 | 4442 | return 0; |
ae9fedc7 PB |
4443 | else { |
4444 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); | |
4d283ec9 | 4445 | return VMX_AR_DPL(ar); |
69c73028 | 4446 | } |
69c73028 AK |
4447 | } |
4448 | ||
653e3108 | 4449 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
6aa8b732 | 4450 | { |
6aa8b732 AK |
4451 | u32 ar; |
4452 | ||
f0495f9b | 4453 | if (var->unusable || !var->present) |
6aa8b732 AK |
4454 | ar = 1 << 16; |
4455 | else { | |
4456 | ar = var->type & 15; | |
4457 | ar |= (var->s & 1) << 4; | |
4458 | ar |= (var->dpl & 3) << 5; | |
4459 | ar |= (var->present & 1) << 7; | |
4460 | ar |= (var->avl & 1) << 12; | |
4461 | ar |= (var->l & 1) << 13; | |
4462 | ar |= (var->db & 1) << 14; | |
4463 | ar |= (var->g & 1) << 15; | |
4464 | } | |
653e3108 AK |
4465 | |
4466 | return ar; | |
4467 | } | |
4468 | ||
4469 | static void vmx_set_segment(struct kvm_vcpu *vcpu, | |
4470 | struct kvm_segment *var, int seg) | |
4471 | { | |
7ffd92c5 | 4472 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
772e0318 | 4473 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
653e3108 | 4474 | |
2fb92db1 AK |
4475 | vmx_segment_cache_clear(vmx); |
4476 | ||
1ecd50a9 GN |
4477 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
4478 | vmx->rmode.segs[seg] = *var; | |
4479 | if (seg == VCPU_SREG_TR) | |
4480 | vmcs_write16(sf->selector, var->selector); | |
4481 | else if (var->s) | |
4482 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); | |
d99e4152 | 4483 | goto out; |
653e3108 | 4484 | } |
1ecd50a9 | 4485 | |
653e3108 AK |
4486 | vmcs_writel(sf->base, var->base); |
4487 | vmcs_write32(sf->limit, var->limit); | |
4488 | vmcs_write16(sf->selector, var->selector); | |
3a624e29 NK |
4489 | |
4490 | /* | |
4491 | * Fix the "Accessed" bit in AR field of segment registers for older | |
4492 | * qemu binaries. | |
4493 | * IA32 arch specifies that at the time of processor reset the | |
4494 | * "Accessed" bit in the AR field of segment registers is 1. And qemu | |
0fa06071 | 4495 | * is setting it to 0 in the userland code. This causes invalid guest |
3a624e29 NK |
4496 | * state vmexit when "unrestricted guest" mode is turned on. |
4497 | * Fix for this setup issue in cpu_reset is being pushed in the qemu | |
4498 | * tree. Newer qemu binaries with that qemu fix would not need this | |
4499 | * kvm hack. | |
4500 | */ | |
4501 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) | |
f924d66d | 4502 | var->type |= 0x1; /* Accessed */ |
3a624e29 | 4503 | |
f924d66d | 4504 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
d99e4152 GN |
4505 | |
4506 | out: | |
98eb2f8b | 4507 | vmx->emulation_required = emulation_required(vcpu); |
6aa8b732 AK |
4508 | } |
4509 | ||
6aa8b732 AK |
4510 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
4511 | { | |
2fb92db1 | 4512 | u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS); |
6aa8b732 AK |
4513 | |
4514 | *db = (ar >> 14) & 1; | |
4515 | *l = (ar >> 13) & 1; | |
4516 | } | |
4517 | ||
89a27f4d | 4518 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4519 | { |
89a27f4d GN |
4520 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
4521 | dt->address = vmcs_readl(GUEST_IDTR_BASE); | |
6aa8b732 AK |
4522 | } |
4523 | ||
89a27f4d | 4524 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4525 | { |
89a27f4d GN |
4526 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
4527 | vmcs_writel(GUEST_IDTR_BASE, dt->address); | |
6aa8b732 AK |
4528 | } |
4529 | ||
89a27f4d | 4530 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4531 | { |
89a27f4d GN |
4532 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
4533 | dt->address = vmcs_readl(GUEST_GDTR_BASE); | |
6aa8b732 AK |
4534 | } |
4535 | ||
89a27f4d | 4536 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4537 | { |
89a27f4d GN |
4538 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
4539 | vmcs_writel(GUEST_GDTR_BASE, dt->address); | |
6aa8b732 AK |
4540 | } |
4541 | ||
648dfaa7 MG |
4542 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
4543 | { | |
4544 | struct kvm_segment var; | |
4545 | u32 ar; | |
4546 | ||
4547 | vmx_get_segment(vcpu, &var, seg); | |
07f42f5f | 4548 | var.dpl = 0x3; |
0647f4aa GN |
4549 | if (seg == VCPU_SREG_CS) |
4550 | var.type = 0x3; | |
648dfaa7 MG |
4551 | ar = vmx_segment_access_rights(&var); |
4552 | ||
4553 | if (var.base != (var.selector << 4)) | |
4554 | return false; | |
89efbed0 | 4555 | if (var.limit != 0xffff) |
648dfaa7 | 4556 | return false; |
07f42f5f | 4557 | if (ar != 0xf3) |
648dfaa7 MG |
4558 | return false; |
4559 | ||
4560 | return true; | |
4561 | } | |
4562 | ||
4563 | static bool code_segment_valid(struct kvm_vcpu *vcpu) | |
4564 | { | |
4565 | struct kvm_segment cs; | |
4566 | unsigned int cs_rpl; | |
4567 | ||
4568 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
b32a9918 | 4569 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4570 | |
1872a3f4 AK |
4571 | if (cs.unusable) |
4572 | return false; | |
4d283ec9 | 4573 | if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK)) |
648dfaa7 MG |
4574 | return false; |
4575 | if (!cs.s) | |
4576 | return false; | |
4d283ec9 | 4577 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
648dfaa7 MG |
4578 | if (cs.dpl > cs_rpl) |
4579 | return false; | |
1872a3f4 | 4580 | } else { |
648dfaa7 MG |
4581 | if (cs.dpl != cs_rpl) |
4582 | return false; | |
4583 | } | |
4584 | if (!cs.present) | |
4585 | return false; | |
4586 | ||
4587 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ | |
4588 | return true; | |
4589 | } | |
4590 | ||
4591 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) | |
4592 | { | |
4593 | struct kvm_segment ss; | |
4594 | unsigned int ss_rpl; | |
4595 | ||
4596 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
b32a9918 | 4597 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4598 | |
1872a3f4 AK |
4599 | if (ss.unusable) |
4600 | return true; | |
4601 | if (ss.type != 3 && ss.type != 7) | |
648dfaa7 MG |
4602 | return false; |
4603 | if (!ss.s) | |
4604 | return false; | |
4605 | if (ss.dpl != ss_rpl) /* DPL != RPL */ | |
4606 | return false; | |
4607 | if (!ss.present) | |
4608 | return false; | |
4609 | ||
4610 | return true; | |
4611 | } | |
4612 | ||
4613 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) | |
4614 | { | |
4615 | struct kvm_segment var; | |
4616 | unsigned int rpl; | |
4617 | ||
4618 | vmx_get_segment(vcpu, &var, seg); | |
b32a9918 | 4619 | rpl = var.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4620 | |
1872a3f4 AK |
4621 | if (var.unusable) |
4622 | return true; | |
648dfaa7 MG |
4623 | if (!var.s) |
4624 | return false; | |
4625 | if (!var.present) | |
4626 | return false; | |
4d283ec9 | 4627 | if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) { |
648dfaa7 MG |
4628 | if (var.dpl < rpl) /* DPL < RPL */ |
4629 | return false; | |
4630 | } | |
4631 | ||
4632 | /* TODO: Add other members to kvm_segment_field to allow checking for other access | |
4633 | * rights flags | |
4634 | */ | |
4635 | return true; | |
4636 | } | |
4637 | ||
4638 | static bool tr_valid(struct kvm_vcpu *vcpu) | |
4639 | { | |
4640 | struct kvm_segment tr; | |
4641 | ||
4642 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); | |
4643 | ||
1872a3f4 AK |
4644 | if (tr.unusable) |
4645 | return false; | |
b32a9918 | 4646 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 | 4647 | return false; |
1872a3f4 | 4648 | if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ |
648dfaa7 MG |
4649 | return false; |
4650 | if (!tr.present) | |
4651 | return false; | |
4652 | ||
4653 | return true; | |
4654 | } | |
4655 | ||
4656 | static bool ldtr_valid(struct kvm_vcpu *vcpu) | |
4657 | { | |
4658 | struct kvm_segment ldtr; | |
4659 | ||
4660 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); | |
4661 | ||
1872a3f4 AK |
4662 | if (ldtr.unusable) |
4663 | return true; | |
b32a9918 | 4664 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 MG |
4665 | return false; |
4666 | if (ldtr.type != 2) | |
4667 | return false; | |
4668 | if (!ldtr.present) | |
4669 | return false; | |
4670 | ||
4671 | return true; | |
4672 | } | |
4673 | ||
4674 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) | |
4675 | { | |
4676 | struct kvm_segment cs, ss; | |
4677 | ||
4678 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
4679 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
4680 | ||
b32a9918 NA |
4681 | return ((cs.selector & SEGMENT_RPL_MASK) == |
4682 | (ss.selector & SEGMENT_RPL_MASK)); | |
648dfaa7 MG |
4683 | } |
4684 | ||
4685 | /* | |
4686 | * Check if guest state is valid. Returns true if valid, false if | |
4687 | * not. | |
4688 | * We assume that registers are always usable | |
4689 | */ | |
4690 | static bool guest_state_valid(struct kvm_vcpu *vcpu) | |
4691 | { | |
c5e97c80 GN |
4692 | if (enable_unrestricted_guest) |
4693 | return true; | |
4694 | ||
648dfaa7 | 4695 | /* real mode guest state checks */ |
f13882d8 | 4696 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
648dfaa7 MG |
4697 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
4698 | return false; | |
4699 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) | |
4700 | return false; | |
4701 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) | |
4702 | return false; | |
4703 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) | |
4704 | return false; | |
4705 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) | |
4706 | return false; | |
4707 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) | |
4708 | return false; | |
4709 | } else { | |
4710 | /* protected mode guest state checks */ | |
4711 | if (!cs_ss_rpl_check(vcpu)) | |
4712 | return false; | |
4713 | if (!code_segment_valid(vcpu)) | |
4714 | return false; | |
4715 | if (!stack_segment_valid(vcpu)) | |
4716 | return false; | |
4717 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) | |
4718 | return false; | |
4719 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) | |
4720 | return false; | |
4721 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) | |
4722 | return false; | |
4723 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) | |
4724 | return false; | |
4725 | if (!tr_valid(vcpu)) | |
4726 | return false; | |
4727 | if (!ldtr_valid(vcpu)) | |
4728 | return false; | |
4729 | } | |
4730 | /* TODO: | |
4731 | * - Add checks on RIP | |
4732 | * - Add checks on RFLAGS | |
4733 | */ | |
4734 | ||
4735 | return true; | |
4736 | } | |
4737 | ||
5fa99cbe JM |
4738 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
4739 | { | |
4740 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); | |
4741 | } | |
4742 | ||
d77c26fc | 4743 | static int init_rmode_tss(struct kvm *kvm) |
6aa8b732 | 4744 | { |
40dcaa9f | 4745 | gfn_t fn; |
195aefde | 4746 | u16 data = 0; |
1f755a82 | 4747 | int idx, r; |
6aa8b732 | 4748 | |
40dcaa9f | 4749 | idx = srcu_read_lock(&kvm->srcu); |
4918c6ca | 4750 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
195aefde IE |
4751 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
4752 | if (r < 0) | |
10589a46 | 4753 | goto out; |
195aefde | 4754 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
464d17c8 SY |
4755 | r = kvm_write_guest_page(kvm, fn++, &data, |
4756 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); | |
195aefde | 4757 | if (r < 0) |
10589a46 | 4758 | goto out; |
195aefde IE |
4759 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
4760 | if (r < 0) | |
10589a46 | 4761 | goto out; |
195aefde IE |
4762 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
4763 | if (r < 0) | |
10589a46 | 4764 | goto out; |
195aefde | 4765 | data = ~0; |
10589a46 MT |
4766 | r = kvm_write_guest_page(kvm, fn, &data, |
4767 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, | |
4768 | sizeof(u8)); | |
10589a46 | 4769 | out: |
40dcaa9f | 4770 | srcu_read_unlock(&kvm->srcu, idx); |
1f755a82 | 4771 | return r; |
6aa8b732 AK |
4772 | } |
4773 | ||
b7ebfb05 SY |
4774 | static int init_rmode_identity_map(struct kvm *kvm) |
4775 | { | |
f51770ed | 4776 | int i, idx, r = 0; |
ba049e93 | 4777 | kvm_pfn_t identity_map_pfn; |
b7ebfb05 SY |
4778 | u32 tmp; |
4779 | ||
089d034e | 4780 | if (!enable_ept) |
f51770ed | 4781 | return 0; |
a255d479 TC |
4782 | |
4783 | /* Protect kvm->arch.ept_identity_pagetable_done. */ | |
4784 | mutex_lock(&kvm->slots_lock); | |
4785 | ||
f51770ed | 4786 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
a255d479 | 4787 | goto out2; |
a255d479 | 4788 | |
b927a3ce | 4789 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
a255d479 TC |
4790 | |
4791 | r = alloc_identity_pagetable(kvm); | |
f51770ed | 4792 | if (r < 0) |
a255d479 TC |
4793 | goto out2; |
4794 | ||
40dcaa9f | 4795 | idx = srcu_read_lock(&kvm->srcu); |
b7ebfb05 SY |
4796 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
4797 | if (r < 0) | |
4798 | goto out; | |
4799 | /* Set up identity-mapping pagetable for EPT in real mode */ | |
4800 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { | |
4801 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | | |
4802 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); | |
4803 | r = kvm_write_guest_page(kvm, identity_map_pfn, | |
4804 | &tmp, i * sizeof(tmp), sizeof(tmp)); | |
4805 | if (r < 0) | |
4806 | goto out; | |
4807 | } | |
4808 | kvm->arch.ept_identity_pagetable_done = true; | |
f51770ed | 4809 | |
b7ebfb05 | 4810 | out: |
40dcaa9f | 4811 | srcu_read_unlock(&kvm->srcu, idx); |
a255d479 TC |
4812 | |
4813 | out2: | |
4814 | mutex_unlock(&kvm->slots_lock); | |
f51770ed | 4815 | return r; |
b7ebfb05 SY |
4816 | } |
4817 | ||
6aa8b732 AK |
4818 | static void seg_setup(int seg) |
4819 | { | |
772e0318 | 4820 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
3a624e29 | 4821 | unsigned int ar; |
6aa8b732 AK |
4822 | |
4823 | vmcs_write16(sf->selector, 0); | |
4824 | vmcs_writel(sf->base, 0); | |
4825 | vmcs_write32(sf->limit, 0xffff); | |
d54d07b2 GN |
4826 | ar = 0x93; |
4827 | if (seg == VCPU_SREG_CS) | |
4828 | ar |= 0x08; /* code segment */ | |
3a624e29 NK |
4829 | |
4830 | vmcs_write32(sf->ar_bytes, ar); | |
6aa8b732 AK |
4831 | } |
4832 | ||
f78e0e2e SY |
4833 | static int alloc_apic_access_page(struct kvm *kvm) |
4834 | { | |
4484141a | 4835 | struct page *page; |
f78e0e2e SY |
4836 | int r = 0; |
4837 | ||
79fac95e | 4838 | mutex_lock(&kvm->slots_lock); |
c24ae0dc | 4839 | if (kvm->arch.apic_access_page_done) |
f78e0e2e | 4840 | goto out; |
1d8007bd PB |
4841 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
4842 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); | |
f78e0e2e SY |
4843 | if (r) |
4844 | goto out; | |
72dc67a6 | 4845 | |
73a6d941 | 4846 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
4484141a XG |
4847 | if (is_error_page(page)) { |
4848 | r = -EFAULT; | |
4849 | goto out; | |
4850 | } | |
4851 | ||
c24ae0dc TC |
4852 | /* |
4853 | * Do not pin the page in memory, so that memory hot-unplug | |
4854 | * is able to migrate it. | |
4855 | */ | |
4856 | put_page(page); | |
4857 | kvm->arch.apic_access_page_done = true; | |
f78e0e2e | 4858 | out: |
79fac95e | 4859 | mutex_unlock(&kvm->slots_lock); |
f78e0e2e SY |
4860 | return r; |
4861 | } | |
4862 | ||
b7ebfb05 SY |
4863 | static int alloc_identity_pagetable(struct kvm *kvm) |
4864 | { | |
a255d479 TC |
4865 | /* Called with kvm->slots_lock held. */ |
4866 | ||
b7ebfb05 SY |
4867 | int r = 0; |
4868 | ||
a255d479 TC |
4869 | BUG_ON(kvm->arch.ept_identity_pagetable_done); |
4870 | ||
1d8007bd PB |
4871 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
4872 | kvm->arch.ept_identity_map_addr, PAGE_SIZE); | |
b7ebfb05 | 4873 | |
b7ebfb05 SY |
4874 | return r; |
4875 | } | |
4876 | ||
991e7a0e | 4877 | static int allocate_vpid(void) |
2384d2b3 SY |
4878 | { |
4879 | int vpid; | |
4880 | ||
919818ab | 4881 | if (!enable_vpid) |
991e7a0e | 4882 | return 0; |
2384d2b3 SY |
4883 | spin_lock(&vmx_vpid_lock); |
4884 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); | |
991e7a0e | 4885 | if (vpid < VMX_NR_VPIDS) |
2384d2b3 | 4886 | __set_bit(vpid, vmx_vpid_bitmap); |
991e7a0e WL |
4887 | else |
4888 | vpid = 0; | |
2384d2b3 | 4889 | spin_unlock(&vmx_vpid_lock); |
991e7a0e | 4890 | return vpid; |
2384d2b3 SY |
4891 | } |
4892 | ||
991e7a0e | 4893 | static void free_vpid(int vpid) |
cdbecfc3 | 4894 | { |
991e7a0e | 4895 | if (!enable_vpid || vpid == 0) |
cdbecfc3 LJ |
4896 | return; |
4897 | spin_lock(&vmx_vpid_lock); | |
991e7a0e | 4898 | __clear_bit(vpid, vmx_vpid_bitmap); |
cdbecfc3 LJ |
4899 | spin_unlock(&vmx_vpid_lock); |
4900 | } | |
4901 | ||
8d14695f YZ |
4902 | #define MSR_TYPE_R 1 |
4903 | #define MSR_TYPE_W 2 | |
4904 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, | |
4905 | u32 msr, int type) | |
25c5f225 | 4906 | { |
3e7c73e9 | 4907 | int f = sizeof(unsigned long); |
25c5f225 SY |
4908 | |
4909 | if (!cpu_has_vmx_msr_bitmap()) | |
4910 | return; | |
4911 | ||
4912 | /* | |
4913 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
4914 | * have the write-low and read-high bitmap offsets the wrong way round. | |
4915 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
4916 | */ | |
25c5f225 | 4917 | if (msr <= 0x1fff) { |
8d14695f YZ |
4918 | if (type & MSR_TYPE_R) |
4919 | /* read-low */ | |
4920 | __clear_bit(msr, msr_bitmap + 0x000 / f); | |
4921 | ||
4922 | if (type & MSR_TYPE_W) | |
4923 | /* write-low */ | |
4924 | __clear_bit(msr, msr_bitmap + 0x800 / f); | |
4925 | ||
25c5f225 SY |
4926 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
4927 | msr &= 0x1fff; | |
8d14695f YZ |
4928 | if (type & MSR_TYPE_R) |
4929 | /* read-high */ | |
4930 | __clear_bit(msr, msr_bitmap + 0x400 / f); | |
4931 | ||
4932 | if (type & MSR_TYPE_W) | |
4933 | /* write-high */ | |
4934 | __clear_bit(msr, msr_bitmap + 0xc00 / f); | |
4935 | ||
4936 | } | |
4937 | } | |
4938 | ||
f2b93280 WV |
4939 | /* |
4940 | * If a msr is allowed by L0, we should check whether it is allowed by L1. | |
4941 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. | |
4942 | */ | |
4943 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, | |
4944 | unsigned long *msr_bitmap_nested, | |
4945 | u32 msr, int type) | |
4946 | { | |
4947 | int f = sizeof(unsigned long); | |
4948 | ||
4949 | if (!cpu_has_vmx_msr_bitmap()) { | |
4950 | WARN_ON(1); | |
4951 | return; | |
4952 | } | |
4953 | ||
4954 | /* | |
4955 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
4956 | * have the write-low and read-high bitmap offsets the wrong way round. | |
4957 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
4958 | */ | |
4959 | if (msr <= 0x1fff) { | |
4960 | if (type & MSR_TYPE_R && | |
4961 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) | |
4962 | /* read-low */ | |
4963 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); | |
4964 | ||
4965 | if (type & MSR_TYPE_W && | |
4966 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) | |
4967 | /* write-low */ | |
4968 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); | |
4969 | ||
4970 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
4971 | msr &= 0x1fff; | |
4972 | if (type & MSR_TYPE_R && | |
4973 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) | |
4974 | /* read-high */ | |
4975 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); | |
4976 | ||
4977 | if (type & MSR_TYPE_W && | |
4978 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) | |
4979 | /* write-high */ | |
4980 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); | |
4981 | ||
4982 | } | |
4983 | } | |
4984 | ||
5897297b AK |
4985 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
4986 | { | |
4987 | if (!longmode_only) | |
8d14695f YZ |
4988 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, |
4989 | msr, MSR_TYPE_R | MSR_TYPE_W); | |
4990 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, | |
4991 | msr, MSR_TYPE_R | MSR_TYPE_W); | |
4992 | } | |
4993 | ||
2e69f865 | 4994 | static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active) |
8d14695f | 4995 | { |
f6e90f9e | 4996 | if (apicv_active) { |
c63e4563 | 4997 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv, |
2e69f865 | 4998 | msr, type); |
c63e4563 | 4999 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv, |
2e69f865 | 5000 | msr, type); |
f6e90f9e | 5001 | } else { |
f6e90f9e | 5002 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
2e69f865 | 5003 | msr, type); |
f6e90f9e | 5004 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
2e69f865 | 5005 | msr, type); |
f6e90f9e | 5006 | } |
5897297b AK |
5007 | } |
5008 | ||
d62caabb | 5009 | static bool vmx_get_enable_apicv(void) |
d50ab6c1 | 5010 | { |
d62caabb | 5011 | return enable_apicv; |
d50ab6c1 PB |
5012 | } |
5013 | ||
c9f04407 DM |
5014 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
5015 | { | |
5016 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
5017 | gfn_t gfn; | |
5018 | ||
5019 | /* | |
5020 | * Don't need to mark the APIC access page dirty; it is never | |
5021 | * written to by the CPU during APIC virtualization. | |
5022 | */ | |
5023 | ||
5024 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { | |
5025 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; | |
5026 | kvm_vcpu_mark_page_dirty(vcpu, gfn); | |
5027 | } | |
5028 | ||
5029 | if (nested_cpu_has_posted_intr(vmcs12)) { | |
5030 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; | |
5031 | kvm_vcpu_mark_page_dirty(vcpu, gfn); | |
5032 | } | |
5033 | } | |
5034 | ||
5035 | ||
6342c50a | 5036 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
705699a1 WV |
5037 | { |
5038 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5039 | int max_irr; | |
5040 | void *vapic_page; | |
5041 | u16 status; | |
5042 | ||
c9f04407 DM |
5043 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
5044 | return; | |
705699a1 | 5045 | |
c9f04407 DM |
5046 | vmx->nested.pi_pending = false; |
5047 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) | |
5048 | return; | |
705699a1 | 5049 | |
c9f04407 DM |
5050 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
5051 | if (max_irr != 256) { | |
705699a1 | 5052 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
705699a1 WV |
5053 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page); |
5054 | kunmap(vmx->nested.virtual_apic_page); | |
5055 | ||
5056 | status = vmcs_read16(GUEST_INTR_STATUS); | |
5057 | if ((u8)max_irr > ((u8)status & 0xff)) { | |
5058 | status &= ~0xff; | |
5059 | status |= (u8)max_irr; | |
5060 | vmcs_write16(GUEST_INTR_STATUS, status); | |
5061 | } | |
5062 | } | |
c9f04407 DM |
5063 | |
5064 | nested_mark_vmcs12_pages_dirty(vcpu); | |
705699a1 WV |
5065 | } |
5066 | ||
06a5524f WV |
5067 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
5068 | bool nested) | |
21bc8dc5 RK |
5069 | { |
5070 | #ifdef CONFIG_SMP | |
06a5524f WV |
5071 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
5072 | ||
21bc8dc5 | 5073 | if (vcpu->mode == IN_GUEST_MODE) { |
28b835d6 | 5074 | /* |
948ae9ac HZ |
5075 | * The vector of interrupt to be delivered to vcpu had |
5076 | * been set in PIR before this function. | |
5077 | * | |
5078 | * Following cases will be reached in this block, and | |
5079 | * we always send a notification event in all cases as | |
5080 | * explained below. | |
5081 | * | |
5082 | * Case 1: vcpu keeps in non-root mode. Sending a | |
5083 | * notification event posts the interrupt to vcpu. | |
5084 | * | |
5085 | * Case 2: vcpu exits to root mode and is still | |
5086 | * runnable. PIR will be synced to vIRR before the | |
5087 | * next vcpu entry. Sending a notification event in | |
5088 | * this case has no effect, as vcpu is not in root | |
5089 | * mode. | |
28b835d6 | 5090 | * |
948ae9ac HZ |
5091 | * Case 3: vcpu exits to root mode and is blocked. |
5092 | * vcpu_block() has already synced PIR to vIRR and | |
5093 | * never blocks vcpu if vIRR is not cleared. Therefore, | |
5094 | * a blocked vcpu here does not wait for any requested | |
5095 | * interrupts in PIR, and sending a notification event | |
5096 | * which has no effect is safe here. | |
28b835d6 | 5097 | */ |
28b835d6 | 5098 | |
06a5524f | 5099 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
21bc8dc5 RK |
5100 | return true; |
5101 | } | |
5102 | #endif | |
5103 | return false; | |
5104 | } | |
5105 | ||
705699a1 WV |
5106 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
5107 | int vector) | |
5108 | { | |
5109 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5110 | ||
5111 | if (is_guest_mode(vcpu) && | |
5112 | vector == vmx->nested.posted_intr_nv) { | |
5113 | /* the PIR and ON have been set by L1. */ | |
06a5524f | 5114 | kvm_vcpu_trigger_posted_interrupt(vcpu, true); |
705699a1 WV |
5115 | /* |
5116 | * If a posted intr is not recognized by hardware, | |
5117 | * we will accomplish it in the next vmentry. | |
5118 | */ | |
5119 | vmx->nested.pi_pending = true; | |
5120 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
5121 | return 0; | |
5122 | } | |
5123 | return -1; | |
5124 | } | |
a20ed54d YZ |
5125 | /* |
5126 | * Send interrupt to vcpu via posted interrupt way. | |
5127 | * 1. If target vcpu is running(non-root mode), send posted interrupt | |
5128 | * notification to vcpu and hardware will sync PIR to vIRR atomically. | |
5129 | * 2. If target vcpu isn't running(root mode), kick it to pick up the | |
5130 | * interrupt from PIR in next vmentry. | |
5131 | */ | |
5132 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) | |
5133 | { | |
5134 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5135 | int r; | |
5136 | ||
705699a1 WV |
5137 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
5138 | if (!r) | |
5139 | return; | |
5140 | ||
a20ed54d YZ |
5141 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
5142 | return; | |
5143 | ||
b95234c8 PB |
5144 | /* If a previous notification has sent the IPI, nothing to do. */ |
5145 | if (pi_test_and_set_on(&vmx->pi_desc)) | |
5146 | return; | |
5147 | ||
06a5524f | 5148 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
a20ed54d YZ |
5149 | kvm_vcpu_kick(vcpu); |
5150 | } | |
5151 | ||
a3a8ff8e NHE |
5152 | /* |
5153 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that | |
5154 | * will not change in the lifetime of the guest. | |
5155 | * Note that host-state that does change is set elsewhere. E.g., host-state | |
5156 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. | |
5157 | */ | |
a547c6db | 5158 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
a3a8ff8e NHE |
5159 | { |
5160 | u32 low32, high32; | |
5161 | unsigned long tmpl; | |
5162 | struct desc_ptr dt; | |
d6e41f11 | 5163 | unsigned long cr0, cr3, cr4; |
a3a8ff8e | 5164 | |
04ac88ab AL |
5165 | cr0 = read_cr0(); |
5166 | WARN_ON(cr0 & X86_CR0_TS); | |
5167 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ | |
d6e41f11 AL |
5168 | |
5169 | /* | |
5170 | * Save the most likely value for this task's CR3 in the VMCS. | |
5171 | * We can't use __get_current_cr3_fast() because we're not atomic. | |
5172 | */ | |
6c690ee1 | 5173 | cr3 = __read_cr3(); |
d6e41f11 | 5174 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
85f01a30 | 5175 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
a3a8ff8e | 5176 | |
d974baa3 | 5177 | /* Save the most likely value for this task's CR4 in the VMCS. */ |
1e02ce4c | 5178 | cr4 = cr4_read_shadow(); |
d974baa3 | 5179 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
85f01a30 | 5180 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
d974baa3 | 5181 | |
a3a8ff8e | 5182 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
b2da15ac AK |
5183 | #ifdef CONFIG_X86_64 |
5184 | /* | |
5185 | * Load null selectors, so we can avoid reloading them in | |
5186 | * __vmx_load_host_state(), in case userspace uses the null selectors | |
5187 | * too (the expected case). | |
5188 | */ | |
5189 | vmcs_write16(HOST_DS_SELECTOR, 0); | |
5190 | vmcs_write16(HOST_ES_SELECTOR, 0); | |
5191 | #else | |
a3a8ff8e NHE |
5192 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5193 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ | |
b2da15ac | 5194 | #endif |
a3a8ff8e NHE |
5195 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5196 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ | |
5197 | ||
edf3ab00 | 5198 | store_idt(&dt); |
a3a8ff8e | 5199 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
a547c6db | 5200 | vmx->host_idt_base = dt.address; |
a3a8ff8e | 5201 | |
83287ea4 | 5202 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
a3a8ff8e NHE |
5203 | |
5204 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); | |
5205 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); | |
5206 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); | |
5207 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ | |
5208 | ||
5209 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { | |
5210 | rdmsr(MSR_IA32_CR_PAT, low32, high32); | |
5211 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); | |
5212 | } | |
5213 | } | |
5214 | ||
bf8179a0 NHE |
5215 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
5216 | { | |
5217 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; | |
5218 | if (enable_ept) | |
5219 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; | |
fe3ef05c NHE |
5220 | if (is_guest_mode(&vmx->vcpu)) |
5221 | vmx->vcpu.arch.cr4_guest_owned_bits &= | |
5222 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; | |
bf8179a0 NHE |
5223 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
5224 | } | |
5225 | ||
01e439be YZ |
5226 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
5227 | { | |
5228 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; | |
5229 | ||
d62caabb | 5230 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
01e439be | 5231 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
64672c95 YJ |
5232 | /* Enable the preemption timer dynamically */ |
5233 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
5234 | return pin_based_exec_ctrl; |
5235 | } | |
5236 | ||
d62caabb AS |
5237 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
5238 | { | |
5239 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5240 | ||
5241 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); | |
3ce424e4 RK |
5242 | if (cpu_has_secondary_exec_ctrls()) { |
5243 | if (kvm_vcpu_apicv_active(vcpu)) | |
5244 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
5245 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5246 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5247 | else | |
5248 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
5249 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5250 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5251 | } | |
5252 | ||
5253 | if (cpu_has_vmx_msr_bitmap()) | |
5254 | vmx_set_msr_bitmap(vcpu); | |
d62caabb AS |
5255 | } |
5256 | ||
bf8179a0 NHE |
5257 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
5258 | { | |
5259 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; | |
d16c293e PB |
5260 | |
5261 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) | |
5262 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; | |
5263 | ||
35754c98 | 5264 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
bf8179a0 NHE |
5265 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
5266 | #ifdef CONFIG_X86_64 | |
5267 | exec_control |= CPU_BASED_CR8_STORE_EXITING | | |
5268 | CPU_BASED_CR8_LOAD_EXITING; | |
5269 | #endif | |
5270 | } | |
5271 | if (!enable_ept) | |
5272 | exec_control |= CPU_BASED_CR3_STORE_EXITING | | |
5273 | CPU_BASED_CR3_LOAD_EXITING | | |
5274 | CPU_BASED_INVLPG_EXITING; | |
5275 | return exec_control; | |
5276 | } | |
5277 | ||
5278 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) | |
5279 | { | |
5280 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; | |
35754c98 | 5281 | if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu)) |
bf8179a0 NHE |
5282 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
5283 | if (vmx->vpid == 0) | |
5284 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; | |
5285 | if (!enable_ept) { | |
5286 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; | |
5287 | enable_unrestricted_guest = 0; | |
ad756a16 MJ |
5288 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
5289 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; | |
bf8179a0 NHE |
5290 | } |
5291 | if (!enable_unrestricted_guest) | |
5292 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
5293 | if (!ple_gap) | |
5294 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
d62caabb | 5295 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
c7c9c56c YZ |
5296 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
5297 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
8d14695f | 5298 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
abc4fc58 AG |
5299 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
5300 | (handle_vmptrld). | |
5301 | We can NOT enable shadow_vmcs here because we don't have yet | |
5302 | a current VMCS12 | |
5303 | */ | |
5304 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; | |
a3eaa864 KH |
5305 | |
5306 | if (!enable_pml) | |
5307 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; | |
843e4330 | 5308 | |
bf8179a0 NHE |
5309 | return exec_control; |
5310 | } | |
5311 | ||
ce88decf XG |
5312 | static void ept_set_mmio_spte_mask(void) |
5313 | { | |
5314 | /* | |
5315 | * EPT Misconfigurations can be generated if the value of bits 2:0 | |
5316 | * of an EPT paging-structure entry is 110b (write/execute). | |
ce88decf | 5317 | */ |
dcdca5fe PF |
5318 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
5319 | VMX_EPT_MISCONFIG_WX_VALUE); | |
ce88decf XG |
5320 | } |
5321 | ||
f53cd63c | 5322 | #define VMX_XSS_EXIT_BITMAP 0 |
6aa8b732 AK |
5323 | /* |
5324 | * Sets up the vmcs for emulated real mode. | |
5325 | */ | |
8b9cf98c | 5326 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
6aa8b732 | 5327 | { |
2e4ce7f5 | 5328 | #ifdef CONFIG_X86_64 |
6aa8b732 | 5329 | unsigned long a; |
2e4ce7f5 | 5330 | #endif |
6aa8b732 | 5331 | int i; |
6aa8b732 | 5332 | |
6aa8b732 | 5333 | /* I/O */ |
3e7c73e9 AK |
5334 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
5335 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); | |
6aa8b732 | 5336 | |
4607c2d7 AG |
5337 | if (enable_shadow_vmcs) { |
5338 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); | |
5339 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); | |
5340 | } | |
25c5f225 | 5341 | if (cpu_has_vmx_msr_bitmap()) |
5897297b | 5342 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
25c5f225 | 5343 | |
6aa8b732 AK |
5344 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
5345 | ||
6aa8b732 | 5346 | /* Control */ |
01e439be | 5347 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); |
64672c95 | 5348 | vmx->hv_deadline_tsc = -1; |
6e5d865c | 5349 | |
bf8179a0 | 5350 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
6aa8b732 | 5351 | |
dfa169bb | 5352 | if (cpu_has_secondary_exec_ctrls()) { |
bf8179a0 NHE |
5353 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
5354 | vmx_secondary_exec_control(vmx)); | |
dfa169bb | 5355 | } |
f78e0e2e | 5356 | |
d62caabb | 5357 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
c7c9c56c YZ |
5358 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
5359 | vmcs_write64(EOI_EXIT_BITMAP1, 0); | |
5360 | vmcs_write64(EOI_EXIT_BITMAP2, 0); | |
5361 | vmcs_write64(EOI_EXIT_BITMAP3, 0); | |
5362 | ||
5363 | vmcs_write16(GUEST_INTR_STATUS, 0); | |
01e439be | 5364 | |
0bcf261c | 5365 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
01e439be | 5366 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
c7c9c56c YZ |
5367 | } |
5368 | ||
4b8d54f9 ZE |
5369 | if (ple_gap) { |
5370 | vmcs_write32(PLE_GAP, ple_gap); | |
a7653ecd RK |
5371 | vmx->ple_window = ple_window; |
5372 | vmx->ple_window_dirty = true; | |
4b8d54f9 ZE |
5373 | } |
5374 | ||
c3707958 XG |
5375 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
5376 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); | |
6aa8b732 AK |
5377 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
5378 | ||
9581d442 AK |
5379 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
5380 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ | |
a547c6db | 5381 | vmx_set_constant_host_state(vmx); |
05b3e0c2 | 5382 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
5383 | rdmsrl(MSR_FS_BASE, a); |
5384 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ | |
5385 | rdmsrl(MSR_GS_BASE, a); | |
5386 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ | |
5387 | #else | |
5388 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ | |
5389 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ | |
5390 | #endif | |
5391 | ||
2cc51560 ED |
5392 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
5393 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); | |
61d2ef2c | 5394 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
2cc51560 | 5395 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
61d2ef2c | 5396 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
6aa8b732 | 5397 | |
74545705 RK |
5398 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
5399 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); | |
468d472f | 5400 | |
03916db9 | 5401 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
6aa8b732 AK |
5402 | u32 index = vmx_msr_index[i]; |
5403 | u32 data_low, data_high; | |
a2fa3e9f | 5404 | int j = vmx->nmsrs; |
6aa8b732 AK |
5405 | |
5406 | if (rdmsr_safe(index, &data_low, &data_high) < 0) | |
5407 | continue; | |
432bd6cb AK |
5408 | if (wrmsr_safe(index, data_low, data_high) < 0) |
5409 | continue; | |
26bb0981 AK |
5410 | vmx->guest_msrs[j].index = i; |
5411 | vmx->guest_msrs[j].data = 0; | |
d5696725 | 5412 | vmx->guest_msrs[j].mask = -1ull; |
a2fa3e9f | 5413 | ++vmx->nmsrs; |
6aa8b732 | 5414 | } |
6aa8b732 | 5415 | |
2961e876 GN |
5416 | |
5417 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); | |
6aa8b732 AK |
5418 | |
5419 | /* 22.2.1, 20.8.1 */ | |
2961e876 | 5420 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
1c3d14fe | 5421 | |
bd7e5b08 PB |
5422 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
5423 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); | |
5424 | ||
bf8179a0 | 5425 | set_cr4_guest_host_mask(vmx); |
e00c8cf2 | 5426 | |
f53cd63c WL |
5427 | if (vmx_xsaves_supported()) |
5428 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); | |
5429 | ||
4e59516a PF |
5430 | if (enable_pml) { |
5431 | ASSERT(vmx->pml_pg); | |
5432 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
5433 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
5434 | } | |
5435 | ||
e00c8cf2 AK |
5436 | return 0; |
5437 | } | |
5438 | ||
d28bc9dd | 5439 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
e00c8cf2 AK |
5440 | { |
5441 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
58cb628d | 5442 | struct msr_data apic_base_msr; |
d28bc9dd | 5443 | u64 cr0; |
e00c8cf2 | 5444 | |
7ffd92c5 | 5445 | vmx->rmode.vm86_active = 0; |
e00c8cf2 | 5446 | |
ad312c7c | 5447 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
d28bc9dd NA |
5448 | kvm_set_cr8(vcpu, 0); |
5449 | ||
5450 | if (!init_event) { | |
5451 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | | |
5452 | MSR_IA32_APICBASE_ENABLE; | |
5453 | if (kvm_vcpu_is_reset_bsp(vcpu)) | |
5454 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; | |
5455 | apic_base_msr.host_initiated = true; | |
5456 | kvm_set_apic_base(vcpu, &apic_base_msr); | |
5457 | } | |
e00c8cf2 | 5458 | |
2fb92db1 AK |
5459 | vmx_segment_cache_clear(vmx); |
5460 | ||
5706be0d | 5461 | seg_setup(VCPU_SREG_CS); |
66450a21 | 5462 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
f3531054 | 5463 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
e00c8cf2 AK |
5464 | |
5465 | seg_setup(VCPU_SREG_DS); | |
5466 | seg_setup(VCPU_SREG_ES); | |
5467 | seg_setup(VCPU_SREG_FS); | |
5468 | seg_setup(VCPU_SREG_GS); | |
5469 | seg_setup(VCPU_SREG_SS); | |
5470 | ||
5471 | vmcs_write16(GUEST_TR_SELECTOR, 0); | |
5472 | vmcs_writel(GUEST_TR_BASE, 0); | |
5473 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); | |
5474 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); | |
5475 | ||
5476 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); | |
5477 | vmcs_writel(GUEST_LDTR_BASE, 0); | |
5478 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); | |
5479 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); | |
5480 | ||
d28bc9dd NA |
5481 | if (!init_event) { |
5482 | vmcs_write32(GUEST_SYSENTER_CS, 0); | |
5483 | vmcs_writel(GUEST_SYSENTER_ESP, 0); | |
5484 | vmcs_writel(GUEST_SYSENTER_EIP, 0); | |
5485 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
5486 | } | |
e00c8cf2 AK |
5487 | |
5488 | vmcs_writel(GUEST_RFLAGS, 0x02); | |
66450a21 | 5489 | kvm_rip_write(vcpu, 0xfff0); |
e00c8cf2 | 5490 | |
e00c8cf2 AK |
5491 | vmcs_writel(GUEST_GDTR_BASE, 0); |
5492 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); | |
5493 | ||
5494 | vmcs_writel(GUEST_IDTR_BASE, 0); | |
5495 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); | |
5496 | ||
443381a8 | 5497 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
e00c8cf2 | 5498 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
f3531054 | 5499 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
e00c8cf2 | 5500 | |
e00c8cf2 AK |
5501 | setup_msrs(vmx); |
5502 | ||
6aa8b732 AK |
5503 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
5504 | ||
d28bc9dd | 5505 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
f78e0e2e | 5506 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
35754c98 | 5507 | if (cpu_need_tpr_shadow(vcpu)) |
f78e0e2e | 5508 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
d28bc9dd | 5509 | __pa(vcpu->arch.apic->regs)); |
f78e0e2e SY |
5510 | vmcs_write32(TPR_THRESHOLD, 0); |
5511 | } | |
5512 | ||
a73896cb | 5513 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
6aa8b732 | 5514 | |
d62caabb | 5515 | if (kvm_vcpu_apicv_active(vcpu)) |
01e439be YZ |
5516 | memset(&vmx->pi_desc, 0, sizeof(struct pi_desc)); |
5517 | ||
2384d2b3 SY |
5518 | if (vmx->vpid != 0) |
5519 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
5520 | ||
d28bc9dd | 5521 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
d28bc9dd | 5522 | vmx->vcpu.arch.cr0 = cr0; |
f2463247 | 5523 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
d28bc9dd | 5524 | vmx_set_cr4(vcpu, 0); |
5690891b | 5525 | vmx_set_efer(vcpu, 0); |
bd7e5b08 | 5526 | |
d28bc9dd | 5527 | update_exception_bitmap(vcpu); |
6aa8b732 | 5528 | |
dd5f5341 | 5529 | vpid_sync_context(vmx->vpid); |
6aa8b732 AK |
5530 | } |
5531 | ||
b6f1250e NHE |
5532 | /* |
5533 | * In nested virtualization, check if L1 asked to exit on external interrupts. | |
5534 | * For most existing hypervisors, this will always return true. | |
5535 | */ | |
5536 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) | |
5537 | { | |
5538 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & | |
5539 | PIN_BASED_EXT_INTR_MASK; | |
5540 | } | |
5541 | ||
77b0f5d6 BD |
5542 | /* |
5543 | * In nested virtualization, check if L1 has set | |
5544 | * VM_EXIT_ACK_INTR_ON_EXIT | |
5545 | */ | |
5546 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) | |
5547 | { | |
5548 | return get_vmcs12(vcpu)->vm_exit_controls & | |
5549 | VM_EXIT_ACK_INTR_ON_EXIT; | |
5550 | } | |
5551 | ||
ea8ceb83 JK |
5552 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
5553 | { | |
5554 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & | |
5555 | PIN_BASED_NMI_EXITING; | |
5556 | } | |
5557 | ||
c9a7953f | 5558 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 5559 | { |
47c0152e PB |
5560 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
5561 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
3b86cd99 JK |
5562 | } |
5563 | ||
c9a7953f | 5564 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 5565 | { |
9338f80f PB |
5566 | if (!cpu_has_virtual_nmis() || |
5567 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { | |
c9a7953f JK |
5568 | enable_irq_window(vcpu); |
5569 | return; | |
5570 | } | |
3b86cd99 | 5571 | |
47c0152e PB |
5572 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
5573 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
3b86cd99 JK |
5574 | } |
5575 | ||
66fd3f7f | 5576 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
85f455f7 | 5577 | { |
9c8cba37 | 5578 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
66fd3f7f GN |
5579 | uint32_t intr; |
5580 | int irq = vcpu->arch.interrupt.nr; | |
9c8cba37 | 5581 | |
229456fc | 5582 | trace_kvm_inj_virq(irq); |
2714d1d3 | 5583 | |
fa89a817 | 5584 | ++vcpu->stat.irq_injections; |
7ffd92c5 | 5585 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
5586 | int inc_eip = 0; |
5587 | if (vcpu->arch.interrupt.soft) | |
5588 | inc_eip = vcpu->arch.event_exit_inst_len; | |
5589 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) | |
a92601bb | 5590 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
85f455f7 ED |
5591 | return; |
5592 | } | |
66fd3f7f GN |
5593 | intr = irq | INTR_INFO_VALID_MASK; |
5594 | if (vcpu->arch.interrupt.soft) { | |
5595 | intr |= INTR_TYPE_SOFT_INTR; | |
5596 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
5597 | vmx->vcpu.arch.event_exit_inst_len); | |
5598 | } else | |
5599 | intr |= INTR_TYPE_EXT_INTR; | |
5600 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); | |
85f455f7 ED |
5601 | } |
5602 | ||
f08864b4 SY |
5603 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
5604 | { | |
66a5a347 JK |
5605 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
5606 | ||
9338f80f PB |
5607 | if (!cpu_has_virtual_nmis()) { |
5608 | /* | |
5609 | * Tracking the NMI-blocked state in software is built upon | |
5610 | * finding the next open IRQ window. This, in turn, depends on | |
5611 | * well-behaving guests: They have to keep IRQs disabled at | |
5612 | * least as long as the NMI handler runs. Otherwise we may | |
5613 | * cause NMI nesting, maybe breaking the guest. But as this is | |
5614 | * highly unlikely, we can live with the residual risk. | |
5615 | */ | |
5616 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; | |
5617 | vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
5618 | } | |
5619 | ||
4c4a6f79 PB |
5620 | ++vcpu->stat.nmi_injections; |
5621 | vmx->loaded_vmcs->nmi_known_unmasked = false; | |
3b86cd99 | 5622 | |
7ffd92c5 | 5623 | if (vmx->rmode.vm86_active) { |
71f9833b | 5624 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
a92601bb | 5625 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
66a5a347 JK |
5626 | return; |
5627 | } | |
c5a6d5f7 | 5628 | |
f08864b4 SY |
5629 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
5630 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); | |
f08864b4 SY |
5631 | } |
5632 | ||
3cfc3092 JK |
5633 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
5634 | { | |
4c4a6f79 PB |
5635 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
5636 | bool masked; | |
5637 | ||
9338f80f PB |
5638 | if (!cpu_has_virtual_nmis()) |
5639 | return vmx->loaded_vmcs->soft_vnmi_blocked; | |
4c4a6f79 | 5640 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
9d58b931 | 5641 | return false; |
4c4a6f79 PB |
5642 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
5643 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
5644 | return masked; | |
3cfc3092 JK |
5645 | } |
5646 | ||
5647 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) | |
5648 | { | |
5649 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5650 | ||
9338f80f PB |
5651 | if (!cpu_has_virtual_nmis()) { |
5652 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { | |
5653 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; | |
5654 | vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
5655 | } | |
5656 | } else { | |
5657 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
5658 | if (masked) | |
5659 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
5660 | GUEST_INTR_STATE_NMI); | |
5661 | else | |
5662 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, | |
5663 | GUEST_INTR_STATE_NMI); | |
5664 | } | |
3cfc3092 JK |
5665 | } |
5666 | ||
2505dc9f JK |
5667 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
5668 | { | |
b6b8a145 JK |
5669 | if (to_vmx(vcpu)->nested.nested_run_pending) |
5670 | return 0; | |
ea8ceb83 | 5671 | |
9338f80f PB |
5672 | if (!cpu_has_virtual_nmis() && |
5673 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) | |
5674 | return 0; | |
5675 | ||
2505dc9f JK |
5676 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
5677 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI | |
5678 | | GUEST_INTR_STATE_NMI)); | |
5679 | } | |
5680 | ||
78646121 GN |
5681 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
5682 | { | |
b6b8a145 JK |
5683 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
5684 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && | |
c4282df9 GN |
5685 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
5686 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); | |
78646121 GN |
5687 | } |
5688 | ||
cbc94022 IE |
5689 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
5690 | { | |
5691 | int ret; | |
cbc94022 | 5692 | |
1d8007bd PB |
5693 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
5694 | PAGE_SIZE * 3); | |
cbc94022 IE |
5695 | if (ret) |
5696 | return ret; | |
bfc6d222 | 5697 | kvm->arch.tss_addr = addr; |
1f755a82 | 5698 | return init_rmode_tss(kvm); |
cbc94022 IE |
5699 | } |
5700 | ||
0ca1b4f4 | 5701 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
6aa8b732 | 5702 | { |
77ab6db0 | 5703 | switch (vec) { |
77ab6db0 | 5704 | case BP_VECTOR: |
c573cd22 JK |
5705 | /* |
5706 | * Update instruction length as we may reinject the exception | |
5707 | * from user space while in guest debugging mode. | |
5708 | */ | |
5709 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = | |
5710 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
d0bfb940 | 5711 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
0ca1b4f4 GN |
5712 | return false; |
5713 | /* fall through */ | |
5714 | case DB_VECTOR: | |
5715 | if (vcpu->guest_debug & | |
5716 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
5717 | return false; | |
d0bfb940 JK |
5718 | /* fall through */ |
5719 | case DE_VECTOR: | |
77ab6db0 JK |
5720 | case OF_VECTOR: |
5721 | case BR_VECTOR: | |
5722 | case UD_VECTOR: | |
5723 | case DF_VECTOR: | |
5724 | case SS_VECTOR: | |
5725 | case GP_VECTOR: | |
5726 | case MF_VECTOR: | |
0ca1b4f4 GN |
5727 | return true; |
5728 | break; | |
77ab6db0 | 5729 | } |
0ca1b4f4 GN |
5730 | return false; |
5731 | } | |
5732 | ||
5733 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, | |
5734 | int vec, u32 err_code) | |
5735 | { | |
5736 | /* | |
5737 | * Instruction with address size override prefix opcode 0x67 | |
5738 | * Cause the #SS fault with 0 error code in VM86 mode. | |
5739 | */ | |
5740 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { | |
5741 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { | |
5742 | if (vcpu->arch.halt_request) { | |
5743 | vcpu->arch.halt_request = 0; | |
5cb56059 | 5744 | return kvm_vcpu_halt(vcpu); |
0ca1b4f4 GN |
5745 | } |
5746 | return 1; | |
5747 | } | |
5748 | return 0; | |
5749 | } | |
5750 | ||
5751 | /* | |
5752 | * Forward all other exceptions that are valid in real mode. | |
5753 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with | |
5754 | * the required debugging infrastructure rework. | |
5755 | */ | |
5756 | kvm_queue_exception(vcpu, vec); | |
5757 | return 1; | |
6aa8b732 AK |
5758 | } |
5759 | ||
a0861c02 AK |
5760 | /* |
5761 | * Trigger machine check on the host. We assume all the MSRs are already set up | |
5762 | * by the CPU and that we still run on the same CPU as the MCE occurred on. | |
5763 | * We pass a fake environment to the machine check handler because we want | |
5764 | * the guest to be always treated like user space, no matter what context | |
5765 | * it used internally. | |
5766 | */ | |
5767 | static void kvm_machine_check(void) | |
5768 | { | |
5769 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) | |
5770 | struct pt_regs regs = { | |
5771 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ | |
5772 | .flags = X86_EFLAGS_IF, | |
5773 | }; | |
5774 | ||
5775 | do_machine_check(®s, 0); | |
5776 | #endif | |
5777 | } | |
5778 | ||
851ba692 | 5779 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
a0861c02 AK |
5780 | { |
5781 | /* already handled by vcpu_run */ | |
5782 | return 1; | |
5783 | } | |
5784 | ||
851ba692 | 5785 | static int handle_exception(struct kvm_vcpu *vcpu) |
6aa8b732 | 5786 | { |
1155f76a | 5787 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
851ba692 | 5788 | struct kvm_run *kvm_run = vcpu->run; |
d0bfb940 | 5789 | u32 intr_info, ex_no, error_code; |
42dbaa5a | 5790 | unsigned long cr2, rip, dr6; |
6aa8b732 AK |
5791 | u32 vect_info; |
5792 | enum emulation_result er; | |
5793 | ||
1155f76a | 5794 | vect_info = vmx->idt_vectoring_info; |
88786475 | 5795 | intr_info = vmx->exit_intr_info; |
6aa8b732 | 5796 | |
a0861c02 | 5797 | if (is_machine_check(intr_info)) |
851ba692 | 5798 | return handle_machine_check(vcpu); |
a0861c02 | 5799 | |
ef85b673 | 5800 | if (is_nmi(intr_info)) |
1b6269db | 5801 | return 1; /* already handled by vmx_vcpu_run() */ |
2ab455cc | 5802 | |
7aa81cc0 | 5803 | if (is_invalid_opcode(intr_info)) { |
ae1f5767 JK |
5804 | if (is_guest_mode(vcpu)) { |
5805 | kvm_queue_exception(vcpu, UD_VECTOR); | |
5806 | return 1; | |
5807 | } | |
51d8b661 | 5808 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
7aa81cc0 | 5809 | if (er != EMULATE_DONE) |
7ee5d940 | 5810 | kvm_queue_exception(vcpu, UD_VECTOR); |
7aa81cc0 AL |
5811 | return 1; |
5812 | } | |
5813 | ||
6aa8b732 | 5814 | error_code = 0; |
2e11384c | 5815 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
6aa8b732 | 5816 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
bf4ca23e XG |
5817 | |
5818 | /* | |
5819 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing | |
5820 | * MMIO, it is better to report an internal error. | |
5821 | * See the comments in vmx_handle_exit. | |
5822 | */ | |
5823 | if ((vect_info & VECTORING_INFO_VALID_MASK) && | |
5824 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { | |
5825 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
5826 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; | |
80f0e95d | 5827 | vcpu->run->internal.ndata = 3; |
bf4ca23e XG |
5828 | vcpu->run->internal.data[0] = vect_info; |
5829 | vcpu->run->internal.data[1] = intr_info; | |
80f0e95d | 5830 | vcpu->run->internal.data[2] = error_code; |
bf4ca23e XG |
5831 | return 0; |
5832 | } | |
5833 | ||
6aa8b732 AK |
5834 | if (is_page_fault(intr_info)) { |
5835 | cr2 = vmcs_readl(EXIT_QUALIFICATION); | |
1261bfa3 WL |
5836 | /* EPT won't cause page fault directly */ |
5837 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); | |
5838 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0, | |
5839 | true); | |
6aa8b732 AK |
5840 | } |
5841 | ||
d0bfb940 | 5842 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
0ca1b4f4 GN |
5843 | |
5844 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) | |
5845 | return handle_rmode_exception(vcpu, ex_no, error_code); | |
5846 | ||
42dbaa5a | 5847 | switch (ex_no) { |
54a20552 EN |
5848 | case AC_VECTOR: |
5849 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); | |
5850 | return 1; | |
42dbaa5a JK |
5851 | case DB_VECTOR: |
5852 | dr6 = vmcs_readl(EXIT_QUALIFICATION); | |
5853 | if (!(vcpu->guest_debug & | |
5854 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { | |
8246bf52 | 5855 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 5856 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
bde0d7f9 | 5857 | if (is_icebp(intr_info)) |
fd2a445a HD |
5858 | skip_emulated_instruction(vcpu); |
5859 | ||
42dbaa5a JK |
5860 | kvm_queue_exception(vcpu, DB_VECTOR); |
5861 | return 1; | |
5862 | } | |
5863 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; | |
5864 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); | |
5865 | /* fall through */ | |
5866 | case BP_VECTOR: | |
c573cd22 JK |
5867 | /* |
5868 | * Update instruction length as we may reinject #BP from | |
5869 | * user space while in guest debugging mode. Reading it for | |
5870 | * #DB as well causes no harm, it is not used in that case. | |
5871 | */ | |
5872 | vmx->vcpu.arch.event_exit_inst_len = | |
5873 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
6aa8b732 | 5874 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
0a434bb2 | 5875 | rip = kvm_rip_read(vcpu); |
d0bfb940 JK |
5876 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
5877 | kvm_run->debug.arch.exception = ex_no; | |
42dbaa5a JK |
5878 | break; |
5879 | default: | |
d0bfb940 JK |
5880 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
5881 | kvm_run->ex.exception = ex_no; | |
5882 | kvm_run->ex.error_code = error_code; | |
42dbaa5a | 5883 | break; |
6aa8b732 | 5884 | } |
6aa8b732 AK |
5885 | return 0; |
5886 | } | |
5887 | ||
851ba692 | 5888 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
6aa8b732 | 5889 | { |
1165f5fe | 5890 | ++vcpu->stat.irq_exits; |
6aa8b732 AK |
5891 | return 1; |
5892 | } | |
5893 | ||
851ba692 | 5894 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
988ad74f | 5895 | { |
851ba692 | 5896 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
988ad74f AK |
5897 | return 0; |
5898 | } | |
6aa8b732 | 5899 | |
851ba692 | 5900 | static int handle_io(struct kvm_vcpu *vcpu) |
6aa8b732 | 5901 | { |
bfdaab09 | 5902 | unsigned long exit_qualification; |
6affcbed | 5903 | int size, in, string, ret; |
039576c0 | 5904 | unsigned port; |
6aa8b732 | 5905 | |
bfdaab09 | 5906 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
039576c0 | 5907 | string = (exit_qualification & 16) != 0; |
cf8f70bf | 5908 | in = (exit_qualification & 8) != 0; |
e70669ab | 5909 | |
cf8f70bf | 5910 | ++vcpu->stat.io_exits; |
e70669ab | 5911 | |
cf8f70bf | 5912 | if (string || in) |
51d8b661 | 5913 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
e70669ab | 5914 | |
cf8f70bf GN |
5915 | port = exit_qualification >> 16; |
5916 | size = (exit_qualification & 7) + 1; | |
cf8f70bf | 5917 | |
6affcbed KH |
5918 | ret = kvm_skip_emulated_instruction(vcpu); |
5919 | ||
5920 | /* | |
5921 | * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered | |
5922 | * KVM_EXIT_DEBUG here. | |
5923 | */ | |
5924 | return kvm_fast_pio_out(vcpu, size, port) && ret; | |
6aa8b732 AK |
5925 | } |
5926 | ||
102d8325 IM |
5927 | static void |
5928 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) | |
5929 | { | |
5930 | /* | |
5931 | * Patch in the VMCALL instruction: | |
5932 | */ | |
5933 | hypercall[0] = 0x0f; | |
5934 | hypercall[1] = 0x01; | |
5935 | hypercall[2] = 0xc1; | |
102d8325 IM |
5936 | } |
5937 | ||
0fa06071 | 5938 | /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ |
eeadf9e7 NHE |
5939 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
5940 | { | |
eeadf9e7 | 5941 | if (is_guest_mode(vcpu)) { |
1a0d74e6 JK |
5942 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
5943 | unsigned long orig_val = val; | |
5944 | ||
eeadf9e7 NHE |
5945 | /* |
5946 | * We get here when L2 changed cr0 in a way that did not change | |
5947 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), | |
1a0d74e6 JK |
5948 | * but did change L0 shadowed bits. So we first calculate the |
5949 | * effective cr0 value that L1 would like to write into the | |
5950 | * hardware. It consists of the L2-owned bits from the new | |
5951 | * value combined with the L1-owned bits from L1's guest_cr0. | |
eeadf9e7 | 5952 | */ |
1a0d74e6 JK |
5953 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
5954 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); | |
5955 | ||
3899152c | 5956 | if (!nested_guest_cr0_valid(vcpu, val)) |
eeadf9e7 | 5957 | return 1; |
1a0d74e6 JK |
5958 | |
5959 | if (kvm_set_cr0(vcpu, val)) | |
5960 | return 1; | |
5961 | vmcs_writel(CR0_READ_SHADOW, orig_val); | |
eeadf9e7 | 5962 | return 0; |
1a0d74e6 JK |
5963 | } else { |
5964 | if (to_vmx(vcpu)->nested.vmxon && | |
3899152c | 5965 | !nested_host_cr0_valid(vcpu, val)) |
1a0d74e6 | 5966 | return 1; |
3899152c | 5967 | |
eeadf9e7 | 5968 | return kvm_set_cr0(vcpu, val); |
1a0d74e6 | 5969 | } |
eeadf9e7 NHE |
5970 | } |
5971 | ||
5972 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) | |
5973 | { | |
5974 | if (is_guest_mode(vcpu)) { | |
1a0d74e6 JK |
5975 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
5976 | unsigned long orig_val = val; | |
5977 | ||
5978 | /* analogously to handle_set_cr0 */ | |
5979 | val = (val & ~vmcs12->cr4_guest_host_mask) | | |
5980 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); | |
5981 | if (kvm_set_cr4(vcpu, val)) | |
eeadf9e7 | 5982 | return 1; |
1a0d74e6 | 5983 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
eeadf9e7 NHE |
5984 | return 0; |
5985 | } else | |
5986 | return kvm_set_cr4(vcpu, val); | |
5987 | } | |
5988 | ||
851ba692 | 5989 | static int handle_cr(struct kvm_vcpu *vcpu) |
6aa8b732 | 5990 | { |
229456fc | 5991 | unsigned long exit_qualification, val; |
6aa8b732 AK |
5992 | int cr; |
5993 | int reg; | |
49a9b07e | 5994 | int err; |
6affcbed | 5995 | int ret; |
6aa8b732 | 5996 | |
bfdaab09 | 5997 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
6aa8b732 AK |
5998 | cr = exit_qualification & 15; |
5999 | reg = (exit_qualification >> 8) & 15; | |
6000 | switch ((exit_qualification >> 4) & 3) { | |
6001 | case 0: /* mov to cr */ | |
1e32c079 | 6002 | val = kvm_register_readl(vcpu, reg); |
229456fc | 6003 | trace_kvm_cr_write(cr, val); |
6aa8b732 AK |
6004 | switch (cr) { |
6005 | case 0: | |
eeadf9e7 | 6006 | err = handle_set_cr0(vcpu, val); |
6affcbed | 6007 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 6008 | case 3: |
2390218b | 6009 | err = kvm_set_cr3(vcpu, val); |
6affcbed | 6010 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 6011 | case 4: |
eeadf9e7 | 6012 | err = handle_set_cr4(vcpu, val); |
6affcbed | 6013 | return kvm_complete_insn_gp(vcpu, err); |
0a5fff19 GN |
6014 | case 8: { |
6015 | u8 cr8_prev = kvm_get_cr8(vcpu); | |
1e32c079 | 6016 | u8 cr8 = (u8)val; |
eea1cff9 | 6017 | err = kvm_set_cr8(vcpu, cr8); |
6affcbed | 6018 | ret = kvm_complete_insn_gp(vcpu, err); |
35754c98 | 6019 | if (lapic_in_kernel(vcpu)) |
6affcbed | 6020 | return ret; |
0a5fff19 | 6021 | if (cr8_prev <= cr8) |
6affcbed KH |
6022 | return ret; |
6023 | /* | |
6024 | * TODO: we might be squashing a | |
6025 | * KVM_GUESTDBG_SINGLESTEP-triggered | |
6026 | * KVM_EXIT_DEBUG here. | |
6027 | */ | |
851ba692 | 6028 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
0a5fff19 GN |
6029 | return 0; |
6030 | } | |
4b8073e4 | 6031 | } |
6aa8b732 | 6032 | break; |
25c4c276 | 6033 | case 2: /* clts */ |
bd7e5b08 PB |
6034 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
6035 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); | |
4d4ec087 | 6036 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
6affcbed | 6037 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6038 | case 1: /*mov from cr*/ |
6039 | switch (cr) { | |
6040 | case 3: | |
9f8fe504 AK |
6041 | val = kvm_read_cr3(vcpu); |
6042 | kvm_register_write(vcpu, reg, val); | |
6043 | trace_kvm_cr_read(cr, val); | |
6affcbed | 6044 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 | 6045 | case 8: |
229456fc MT |
6046 | val = kvm_get_cr8(vcpu); |
6047 | kvm_register_write(vcpu, reg, val); | |
6048 | trace_kvm_cr_read(cr, val); | |
6affcbed | 6049 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6050 | } |
6051 | break; | |
6052 | case 3: /* lmsw */ | |
a1f83a74 | 6053 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
4d4ec087 | 6054 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
a1f83a74 | 6055 | kvm_lmsw(vcpu, val); |
6aa8b732 | 6056 | |
6affcbed | 6057 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6058 | default: |
6059 | break; | |
6060 | } | |
851ba692 | 6061 | vcpu->run->exit_reason = 0; |
a737f256 | 6062 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
6aa8b732 AK |
6063 | (int)(exit_qualification >> 4) & 3, cr); |
6064 | return 0; | |
6065 | } | |
6066 | ||
851ba692 | 6067 | static int handle_dr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6068 | { |
bfdaab09 | 6069 | unsigned long exit_qualification; |
16f8a6f9 NA |
6070 | int dr, dr7, reg; |
6071 | ||
6072 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6073 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; | |
6074 | ||
6075 | /* First, if DR does not exist, trigger UD */ | |
6076 | if (!kvm_require_dr(vcpu, dr)) | |
6077 | return 1; | |
6aa8b732 | 6078 | |
f2483415 | 6079 | /* Do not handle if the CPL > 0, will trigger GP on re-entry */ |
0a79b009 AK |
6080 | if (!kvm_require_cpl(vcpu, 0)) |
6081 | return 1; | |
16f8a6f9 NA |
6082 | dr7 = vmcs_readl(GUEST_DR7); |
6083 | if (dr7 & DR7_GD) { | |
42dbaa5a JK |
6084 | /* |
6085 | * As the vm-exit takes precedence over the debug trap, we | |
6086 | * need to emulate the latter, either for the host or the | |
6087 | * guest debugging itself. | |
6088 | */ | |
6089 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { | |
851ba692 | 6090 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
16f8a6f9 | 6091 | vcpu->run->debug.arch.dr7 = dr7; |
82b32774 | 6092 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
851ba692 AK |
6093 | vcpu->run->debug.arch.exception = DB_VECTOR; |
6094 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; | |
42dbaa5a JK |
6095 | return 0; |
6096 | } else { | |
7305eb5d | 6097 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 6098 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
42dbaa5a JK |
6099 | kvm_queue_exception(vcpu, DB_VECTOR); |
6100 | return 1; | |
6101 | } | |
6102 | } | |
6103 | ||
81908bf4 | 6104 | if (vcpu->guest_debug == 0) { |
8f22372f PB |
6105 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6106 | CPU_BASED_MOV_DR_EXITING); | |
81908bf4 PB |
6107 | |
6108 | /* | |
6109 | * No more DR vmexits; force a reload of the debug registers | |
6110 | * and reenter on this instruction. The next vmexit will | |
6111 | * retrieve the full state of the debug registers. | |
6112 | */ | |
6113 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; | |
6114 | return 1; | |
6115 | } | |
6116 | ||
42dbaa5a JK |
6117 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
6118 | if (exit_qualification & TYPE_MOV_FROM_DR) { | |
020df079 | 6119 | unsigned long val; |
4c4d563b JK |
6120 | |
6121 | if (kvm_get_dr(vcpu, dr, &val)) | |
6122 | return 1; | |
6123 | kvm_register_write(vcpu, reg, val); | |
020df079 | 6124 | } else |
5777392e | 6125 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
4c4d563b JK |
6126 | return 1; |
6127 | ||
6affcbed | 6128 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6129 | } |
6130 | ||
73aaf249 JK |
6131 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
6132 | { | |
6133 | return vcpu->arch.dr6; | |
6134 | } | |
6135 | ||
6136 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) | |
6137 | { | |
6138 | } | |
6139 | ||
81908bf4 PB |
6140 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
6141 | { | |
81908bf4 PB |
6142 | get_debugreg(vcpu->arch.db[0], 0); |
6143 | get_debugreg(vcpu->arch.db[1], 1); | |
6144 | get_debugreg(vcpu->arch.db[2], 2); | |
6145 | get_debugreg(vcpu->arch.db[3], 3); | |
6146 | get_debugreg(vcpu->arch.dr6, 6); | |
6147 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); | |
6148 | ||
6149 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; | |
8f22372f | 6150 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING); |
81908bf4 PB |
6151 | } |
6152 | ||
020df079 GN |
6153 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
6154 | { | |
6155 | vmcs_writel(GUEST_DR7, val); | |
6156 | } | |
6157 | ||
851ba692 | 6158 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
6aa8b732 | 6159 | { |
6a908b62 | 6160 | return kvm_emulate_cpuid(vcpu); |
6aa8b732 AK |
6161 | } |
6162 | ||
851ba692 | 6163 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6164 | { |
ad312c7c | 6165 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
609e36d3 | 6166 | struct msr_data msr_info; |
6aa8b732 | 6167 | |
609e36d3 PB |
6168 | msr_info.index = ecx; |
6169 | msr_info.host_initiated = false; | |
95502b8b | 6170 | |
609e36d3 | 6171 | if (vmx_get_msr(vcpu, &msr_info)) { |
59200273 | 6172 | trace_kvm_msr_read_ex(ecx); |
c1a5d4f9 | 6173 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
6174 | return 1; |
6175 | } | |
6176 | ||
609e36d3 | 6177 | trace_kvm_msr_read(ecx, msr_info.data); |
2714d1d3 | 6178 | |
6aa8b732 | 6179 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
609e36d3 PB |
6180 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
6181 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; | |
6affcbed | 6182 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6183 | } |
6184 | ||
851ba692 | 6185 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6186 | { |
8fe8ab46 | 6187 | struct msr_data msr; |
ad312c7c ZX |
6188 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
6189 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) | |
6190 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); | |
6aa8b732 | 6191 | |
8fe8ab46 WA |
6192 | msr.data = data; |
6193 | msr.index = ecx; | |
6194 | msr.host_initiated = false; | |
854e8bb1 | 6195 | if (kvm_set_msr(vcpu, &msr) != 0) { |
59200273 | 6196 | trace_kvm_msr_write_ex(ecx, data); |
c1a5d4f9 | 6197 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
6198 | return 1; |
6199 | } | |
6200 | ||
59200273 | 6201 | trace_kvm_msr_write(ecx, data); |
6affcbed | 6202 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6203 | } |
6204 | ||
851ba692 | 6205 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
6e5d865c | 6206 | { |
eb90f341 | 6207 | kvm_apic_update_ppr(vcpu); |
6e5d865c YS |
6208 | return 1; |
6209 | } | |
6210 | ||
851ba692 | 6211 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
6aa8b732 | 6212 | { |
47c0152e PB |
6213 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6214 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
2714d1d3 | 6215 | |
3842d135 AK |
6216 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
6217 | ||
a26bf12a | 6218 | ++vcpu->stat.irq_window_exits; |
6aa8b732 AK |
6219 | return 1; |
6220 | } | |
6221 | ||
851ba692 | 6222 | static int handle_halt(struct kvm_vcpu *vcpu) |
6aa8b732 | 6223 | { |
d3bef15f | 6224 | return kvm_emulate_halt(vcpu); |
6aa8b732 AK |
6225 | } |
6226 | ||
851ba692 | 6227 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
c21415e8 | 6228 | { |
0d9c055e | 6229 | return kvm_emulate_hypercall(vcpu); |
c21415e8 IM |
6230 | } |
6231 | ||
ec25d5e6 GN |
6232 | static int handle_invd(struct kvm_vcpu *vcpu) |
6233 | { | |
51d8b661 | 6234 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
ec25d5e6 GN |
6235 | } |
6236 | ||
851ba692 | 6237 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
a7052897 | 6238 | { |
f9c617f6 | 6239 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
a7052897 MT |
6240 | |
6241 | kvm_mmu_invlpg(vcpu, exit_qualification); | |
6affcbed | 6242 | return kvm_skip_emulated_instruction(vcpu); |
a7052897 MT |
6243 | } |
6244 | ||
fee84b07 AK |
6245 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
6246 | { | |
6247 | int err; | |
6248 | ||
6249 | err = kvm_rdpmc(vcpu); | |
6affcbed | 6250 | return kvm_complete_insn_gp(vcpu, err); |
fee84b07 AK |
6251 | } |
6252 | ||
851ba692 | 6253 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
e5edaa01 | 6254 | { |
6affcbed | 6255 | return kvm_emulate_wbinvd(vcpu); |
e5edaa01 ED |
6256 | } |
6257 | ||
2acf923e DC |
6258 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
6259 | { | |
6260 | u64 new_bv = kvm_read_edx_eax(vcpu); | |
6261 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
6262 | ||
6263 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) | |
6affcbed | 6264 | return kvm_skip_emulated_instruction(vcpu); |
2acf923e DC |
6265 | return 1; |
6266 | } | |
6267 | ||
f53cd63c WL |
6268 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
6269 | { | |
6affcbed | 6270 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6271 | WARN(1, "this should never happen\n"); |
6272 | return 1; | |
6273 | } | |
6274 | ||
6275 | static int handle_xrstors(struct kvm_vcpu *vcpu) | |
6276 | { | |
6affcbed | 6277 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6278 | WARN(1, "this should never happen\n"); |
6279 | return 1; | |
6280 | } | |
6281 | ||
851ba692 | 6282 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
f78e0e2e | 6283 | { |
58fbbf26 KT |
6284 | if (likely(fasteoi)) { |
6285 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6286 | int access_type, offset; | |
6287 | ||
6288 | access_type = exit_qualification & APIC_ACCESS_TYPE; | |
6289 | offset = exit_qualification & APIC_ACCESS_OFFSET; | |
6290 | /* | |
6291 | * Sane guest uses MOV to write EOI, with written value | |
6292 | * not cared. So make a short-circuit here by avoiding | |
6293 | * heavy instruction emulation. | |
6294 | */ | |
6295 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && | |
6296 | (offset == APIC_EOI)) { | |
6297 | kvm_lapic_set_eoi(vcpu); | |
6affcbed | 6298 | return kvm_skip_emulated_instruction(vcpu); |
58fbbf26 KT |
6299 | } |
6300 | } | |
51d8b661 | 6301 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
f78e0e2e SY |
6302 | } |
6303 | ||
c7c9c56c YZ |
6304 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
6305 | { | |
6306 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6307 | int vector = exit_qualification & 0xff; | |
6308 | ||
6309 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ | |
6310 | kvm_apic_set_eoi_accelerated(vcpu, vector); | |
6311 | return 1; | |
6312 | } | |
6313 | ||
83d4c286 YZ |
6314 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
6315 | { | |
6316 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6317 | u32 offset = exit_qualification & 0xfff; | |
6318 | ||
6319 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ | |
6320 | kvm_apic_write_nodecode(vcpu, offset); | |
6321 | return 1; | |
6322 | } | |
6323 | ||
851ba692 | 6324 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
37817f29 | 6325 | { |
60637aac | 6326 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
37817f29 | 6327 | unsigned long exit_qualification; |
e269fb21 JK |
6328 | bool has_error_code = false; |
6329 | u32 error_code = 0; | |
37817f29 | 6330 | u16 tss_selector; |
7f3d35fd | 6331 | int reason, type, idt_v, idt_index; |
64a7ec06 GN |
6332 | |
6333 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); | |
7f3d35fd | 6334 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
64a7ec06 | 6335 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
37817f29 IE |
6336 | |
6337 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6338 | ||
6339 | reason = (u32)exit_qualification >> 30; | |
64a7ec06 GN |
6340 | if (reason == TASK_SWITCH_GATE && idt_v) { |
6341 | switch (type) { | |
6342 | case INTR_TYPE_NMI_INTR: | |
6343 | vcpu->arch.nmi_injected = false; | |
654f06fc | 6344 | vmx_set_nmi_mask(vcpu, true); |
64a7ec06 GN |
6345 | break; |
6346 | case INTR_TYPE_EXT_INTR: | |
66fd3f7f | 6347 | case INTR_TYPE_SOFT_INTR: |
64a7ec06 GN |
6348 | kvm_clear_interrupt_queue(vcpu); |
6349 | break; | |
6350 | case INTR_TYPE_HARD_EXCEPTION: | |
e269fb21 JK |
6351 | if (vmx->idt_vectoring_info & |
6352 | VECTORING_INFO_DELIVER_CODE_MASK) { | |
6353 | has_error_code = true; | |
6354 | error_code = | |
6355 | vmcs_read32(IDT_VECTORING_ERROR_CODE); | |
6356 | } | |
6357 | /* fall through */ | |
64a7ec06 GN |
6358 | case INTR_TYPE_SOFT_EXCEPTION: |
6359 | kvm_clear_exception_queue(vcpu); | |
6360 | break; | |
6361 | default: | |
6362 | break; | |
6363 | } | |
60637aac | 6364 | } |
37817f29 IE |
6365 | tss_selector = exit_qualification; |
6366 | ||
64a7ec06 GN |
6367 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
6368 | type != INTR_TYPE_EXT_INTR && | |
6369 | type != INTR_TYPE_NMI_INTR)) | |
6370 | skip_emulated_instruction(vcpu); | |
6371 | ||
7f3d35fd KW |
6372 | if (kvm_task_switch(vcpu, tss_selector, |
6373 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, | |
6374 | has_error_code, error_code) == EMULATE_FAIL) { | |
acb54517 GN |
6375 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
6376 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6377 | vcpu->run->internal.ndata = 0; | |
42dbaa5a | 6378 | return 0; |
acb54517 | 6379 | } |
42dbaa5a | 6380 | |
42dbaa5a JK |
6381 | /* |
6382 | * TODO: What about debug traps on tss switch? | |
6383 | * Are we supposed to inject them and update dr6? | |
6384 | */ | |
6385 | ||
6386 | return 1; | |
37817f29 IE |
6387 | } |
6388 | ||
851ba692 | 6389 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
1439442c | 6390 | { |
f9c617f6 | 6391 | unsigned long exit_qualification; |
1439442c | 6392 | gpa_t gpa; |
4f5982a5 | 6393 | u32 error_code; |
1439442c | 6394 | |
f9c617f6 | 6395 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
1439442c | 6396 | |
0be9c7a8 GN |
6397 | /* |
6398 | * EPT violation happened while executing iret from NMI, | |
6399 | * "blocked by NMI" bit has to be set before next VM entry. | |
6400 | * There are errata that may cause this bit to not be set: | |
6401 | * AAK134, BY25. | |
6402 | */ | |
bcd1c294 | 6403 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
9338f80f | 6404 | cpu_has_virtual_nmis() && |
bcd1c294 | 6405 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
0be9c7a8 GN |
6406 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
6407 | ||
1439442c | 6408 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
229456fc | 6409 | trace_kvm_page_fault(gpa, exit_qualification); |
4f5982a5 | 6410 | |
27959a44 | 6411 | /* Is it a read fault? */ |
ab22a473 | 6412 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
27959a44 JS |
6413 | ? PFERR_USER_MASK : 0; |
6414 | /* Is it a write fault? */ | |
ab22a473 | 6415 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
27959a44 JS |
6416 | ? PFERR_WRITE_MASK : 0; |
6417 | /* Is it a fetch fault? */ | |
ab22a473 | 6418 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
27959a44 JS |
6419 | ? PFERR_FETCH_MASK : 0; |
6420 | /* ept page table entry is present? */ | |
6421 | error_code |= (exit_qualification & | |
6422 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | | |
6423 | EPT_VIOLATION_EXECUTABLE)) | |
6424 | ? PFERR_PRESENT_MASK : 0; | |
4f5982a5 | 6425 | |
db1c056c | 6426 | vcpu->arch.gpa_available = true; |
25d92081 YZ |
6427 | vcpu->arch.exit_qualification = exit_qualification; |
6428 | ||
4f5982a5 | 6429 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
1439442c SY |
6430 | } |
6431 | ||
851ba692 | 6432 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
68f89400 | 6433 | { |
f735d4af | 6434 | int ret; |
68f89400 MT |
6435 | gpa_t gpa; |
6436 | ||
6437 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); | |
e32edf4f | 6438 | if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
931c33b1 | 6439 | trace_kvm_fast_mmio(gpa); |
6affcbed | 6440 | return kvm_skip_emulated_instruction(vcpu); |
68c3b4d1 | 6441 | } |
68f89400 | 6442 | |
450869d6 | 6443 | ret = handle_mmio_page_fault(vcpu, gpa, true); |
db1c056c | 6444 | vcpu->arch.gpa_available = true; |
b37fbea6 | 6445 | if (likely(ret == RET_MMIO_PF_EMULATE)) |
ce88decf XG |
6446 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
6447 | EMULATE_DONE; | |
f8f55942 XG |
6448 | |
6449 | if (unlikely(ret == RET_MMIO_PF_INVALID)) | |
6450 | return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0); | |
6451 | ||
b37fbea6 | 6452 | if (unlikely(ret == RET_MMIO_PF_RETRY)) |
ce88decf XG |
6453 | return 1; |
6454 | ||
6455 | /* It is the real ept misconfig */ | |
f735d4af | 6456 | WARN_ON(1); |
68f89400 | 6457 | |
851ba692 AK |
6458 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
6459 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; | |
68f89400 MT |
6460 | |
6461 | return 0; | |
6462 | } | |
6463 | ||
851ba692 | 6464 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
f08864b4 | 6465 | { |
47c0152e PB |
6466 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6467 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
f08864b4 | 6468 | ++vcpu->stat.nmi_window_exits; |
3842d135 | 6469 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
f08864b4 SY |
6470 | |
6471 | return 1; | |
6472 | } | |
6473 | ||
80ced186 | 6474 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
ea953ef0 | 6475 | { |
8b3079a5 AK |
6476 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6477 | enum emulation_result err = EMULATE_DONE; | |
80ced186 | 6478 | int ret = 1; |
49e9d557 AK |
6479 | u32 cpu_exec_ctrl; |
6480 | bool intr_window_requested; | |
b8405c18 | 6481 | unsigned count = 130; |
49e9d557 AK |
6482 | |
6483 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); | |
6484 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; | |
ea953ef0 | 6485 | |
98eb2f8b | 6486 | while (vmx->emulation_required && count-- != 0) { |
bdea48e3 | 6487 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
49e9d557 AK |
6488 | return handle_interrupt_window(&vmx->vcpu); |
6489 | ||
72875d8a | 6490 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
de87dcdd AK |
6491 | return 1; |
6492 | ||
991eebf9 | 6493 | err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE); |
ea953ef0 | 6494 | |
ac0a48c3 | 6495 | if (err == EMULATE_USER_EXIT) { |
94452b9e | 6496 | ++vcpu->stat.mmio_exits; |
80ced186 MG |
6497 | ret = 0; |
6498 | goto out; | |
6499 | } | |
1d5a4d9b | 6500 | |
de5f70e0 AK |
6501 | if (err != EMULATE_DONE) { |
6502 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
6503 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6504 | vcpu->run->internal.ndata = 0; | |
6d77dbfc | 6505 | return 0; |
de5f70e0 | 6506 | } |
ea953ef0 | 6507 | |
8d76c49e GN |
6508 | if (vcpu->arch.halt_request) { |
6509 | vcpu->arch.halt_request = 0; | |
5cb56059 | 6510 | ret = kvm_vcpu_halt(vcpu); |
8d76c49e GN |
6511 | goto out; |
6512 | } | |
6513 | ||
ea953ef0 | 6514 | if (signal_pending(current)) |
80ced186 | 6515 | goto out; |
ea953ef0 MG |
6516 | if (need_resched()) |
6517 | schedule(); | |
6518 | } | |
6519 | ||
80ced186 MG |
6520 | out: |
6521 | return ret; | |
ea953ef0 MG |
6522 | } |
6523 | ||
b4a2d31d RK |
6524 | static int __grow_ple_window(int val) |
6525 | { | |
6526 | if (ple_window_grow < 1) | |
6527 | return ple_window; | |
6528 | ||
6529 | val = min(val, ple_window_actual_max); | |
6530 | ||
6531 | if (ple_window_grow < ple_window) | |
6532 | val *= ple_window_grow; | |
6533 | else | |
6534 | val += ple_window_grow; | |
6535 | ||
6536 | return val; | |
6537 | } | |
6538 | ||
6539 | static int __shrink_ple_window(int val, int modifier, int minimum) | |
6540 | { | |
6541 | if (modifier < 1) | |
6542 | return ple_window; | |
6543 | ||
6544 | if (modifier < ple_window) | |
6545 | val /= modifier; | |
6546 | else | |
6547 | val -= modifier; | |
6548 | ||
6549 | return max(val, minimum); | |
6550 | } | |
6551 | ||
6552 | static void grow_ple_window(struct kvm_vcpu *vcpu) | |
6553 | { | |
6554 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6555 | int old = vmx->ple_window; | |
6556 | ||
6557 | vmx->ple_window = __grow_ple_window(old); | |
6558 | ||
6559 | if (vmx->ple_window != old) | |
6560 | vmx->ple_window_dirty = true; | |
7b46268d RK |
6561 | |
6562 | trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
6563 | } |
6564 | ||
6565 | static void shrink_ple_window(struct kvm_vcpu *vcpu) | |
6566 | { | |
6567 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6568 | int old = vmx->ple_window; | |
6569 | ||
6570 | vmx->ple_window = __shrink_ple_window(old, | |
6571 | ple_window_shrink, ple_window); | |
6572 | ||
6573 | if (vmx->ple_window != old) | |
6574 | vmx->ple_window_dirty = true; | |
7b46268d RK |
6575 | |
6576 | trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
6577 | } |
6578 | ||
6579 | /* | |
6580 | * ple_window_actual_max is computed to be one grow_ple_window() below | |
6581 | * ple_window_max. (See __grow_ple_window for the reason.) | |
6582 | * This prevents overflows, because ple_window_max is int. | |
6583 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in | |
6584 | * this process. | |
6585 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. | |
6586 | */ | |
6587 | static void update_ple_window_actual_max(void) | |
6588 | { | |
6589 | ple_window_actual_max = | |
6590 | __shrink_ple_window(max(ple_window_max, ple_window), | |
6591 | ple_window_grow, INT_MIN); | |
6592 | } | |
6593 | ||
bf9f6ac8 FW |
6594 | /* |
6595 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. | |
6596 | */ | |
6597 | static void wakeup_handler(void) | |
6598 | { | |
6599 | struct kvm_vcpu *vcpu; | |
6600 | int cpu = smp_processor_id(); | |
6601 | ||
6602 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
6603 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), | |
6604 | blocked_vcpu_list) { | |
6605 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
6606 | ||
6607 | if (pi_test_on(pi_desc) == 1) | |
6608 | kvm_vcpu_kick(vcpu); | |
6609 | } | |
6610 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
6611 | } | |
6612 | ||
f160c7b7 JS |
6613 | void vmx_enable_tdp(void) |
6614 | { | |
6615 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, | |
6616 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, | |
6617 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, | |
6618 | 0ull, VMX_EPT_EXECUTABLE_MASK, | |
6619 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, | |
995f00a6 | 6620 | VMX_EPT_RWX_MASK); |
f160c7b7 JS |
6621 | |
6622 | ept_set_mmio_spte_mask(); | |
6623 | kvm_enable_tdp(); | |
6624 | } | |
6625 | ||
f2c7648d TC |
6626 | static __init int hardware_setup(void) |
6627 | { | |
34a1cd60 TC |
6628 | int r = -ENOMEM, i, msr; |
6629 | ||
6630 | rdmsrl_safe(MSR_EFER, &host_efer); | |
6631 | ||
6632 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) | |
6633 | kvm_define_shared_msr(i, vmx_msr_index[i]); | |
6634 | ||
23611332 RK |
6635 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
6636 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); | |
6637 | if (!vmx_bitmap[i]) | |
6638 | goto out; | |
6639 | } | |
34a1cd60 TC |
6640 | |
6641 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); | |
34a1cd60 TC |
6642 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
6643 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); | |
6644 | ||
34a1cd60 | 6645 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
34a1cd60 TC |
6646 | |
6647 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); | |
6648 | ||
6649 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); | |
6650 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); | |
6651 | ||
34a1cd60 TC |
6652 | if (setup_vmcs_config(&vmcs_config) < 0) { |
6653 | r = -EIO; | |
23611332 | 6654 | goto out; |
baa03522 | 6655 | } |
f2c7648d TC |
6656 | |
6657 | if (boot_cpu_has(X86_FEATURE_NX)) | |
6658 | kvm_enable_efer_bits(EFER_NX); | |
6659 | ||
08d839c4 WL |
6660 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
6661 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) | |
f2c7648d | 6662 | enable_vpid = 0; |
08d839c4 | 6663 | |
f2c7648d TC |
6664 | if (!cpu_has_vmx_shadow_vmcs()) |
6665 | enable_shadow_vmcs = 0; | |
6666 | if (enable_shadow_vmcs) | |
6667 | init_vmcs_shadow_fields(); | |
6668 | ||
6669 | if (!cpu_has_vmx_ept() || | |
6670 | !cpu_has_vmx_ept_4levels()) { | |
6671 | enable_ept = 0; | |
6672 | enable_unrestricted_guest = 0; | |
6673 | enable_ept_ad_bits = 0; | |
6674 | } | |
6675 | ||
fce6ac4c | 6676 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
f2c7648d TC |
6677 | enable_ept_ad_bits = 0; |
6678 | ||
6679 | if (!cpu_has_vmx_unrestricted_guest()) | |
6680 | enable_unrestricted_guest = 0; | |
6681 | ||
ad15a296 | 6682 | if (!cpu_has_vmx_flexpriority()) |
f2c7648d TC |
6683 | flexpriority_enabled = 0; |
6684 | ||
ad15a296 PB |
6685 | /* |
6686 | * set_apic_access_page_addr() is used to reload apic access | |
6687 | * page upon invalidation. No need to do anything if not | |
6688 | * using the APIC_ACCESS_ADDR VMCS field. | |
6689 | */ | |
6690 | if (!flexpriority_enabled) | |
f2c7648d | 6691 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
f2c7648d TC |
6692 | |
6693 | if (!cpu_has_vmx_tpr_shadow()) | |
6694 | kvm_x86_ops->update_cr8_intercept = NULL; | |
6695 | ||
6696 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) | |
6697 | kvm_disable_largepages(); | |
6698 | ||
6699 | if (!cpu_has_vmx_ple()) | |
6700 | ple_gap = 0; | |
6701 | ||
76dfafd5 | 6702 | if (!cpu_has_vmx_apicv()) { |
f2c7648d | 6703 | enable_apicv = 0; |
76dfafd5 PB |
6704 | kvm_x86_ops->sync_pir_to_irr = NULL; |
6705 | } | |
f2c7648d | 6706 | |
64903d61 HZ |
6707 | if (cpu_has_vmx_tsc_scaling()) { |
6708 | kvm_has_tsc_control = true; | |
6709 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; | |
6710 | kvm_tsc_scaling_ratio_frac_bits = 48; | |
6711 | } | |
6712 | ||
baa03522 TC |
6713 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
6714 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); | |
6715 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); | |
6716 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); | |
6717 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); | |
6718 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); | |
95502b8b WW |
6719 | vmx_disable_intercept_for_msr(MSR_IA32_SPEC_CTRL, false); |
6720 | vmx_disable_intercept_for_msr(MSR_IA32_PRED_CMD, false); | |
baa03522 | 6721 | |
c63e4563 | 6722 | memcpy(vmx_msr_bitmap_legacy_x2apic_apicv, |
baa03522 | 6723 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
c63e4563 | 6724 | memcpy(vmx_msr_bitmap_longmode_x2apic_apicv, |
baa03522 | 6725 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
c63e4563 | 6726 | memcpy(vmx_msr_bitmap_legacy_x2apic, |
f6e90f9e | 6727 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
c63e4563 | 6728 | memcpy(vmx_msr_bitmap_longmode_x2apic, |
f6e90f9e | 6729 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
baa03522 | 6730 | |
04bb92e4 WL |
6731 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
6732 | ||
40d8338d RK |
6733 | for (msr = 0x800; msr <= 0x8ff; msr++) { |
6734 | if (msr == 0x839 /* TMCCT */) | |
6735 | continue; | |
2e69f865 | 6736 | vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true); |
40d8338d | 6737 | } |
3ce424e4 | 6738 | |
f6e90f9e | 6739 | /* |
2e69f865 RK |
6740 | * TPR reads and writes can be virtualized even if virtual interrupt |
6741 | * delivery is not in use. | |
f6e90f9e | 6742 | */ |
2e69f865 RK |
6743 | vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true); |
6744 | vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false); | |
3ce424e4 | 6745 | |
3ce424e4 | 6746 | /* EOI */ |
2e69f865 | 6747 | vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true); |
3ce424e4 | 6748 | /* SELF-IPI */ |
2e69f865 | 6749 | vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true); |
baa03522 | 6750 | |
f160c7b7 JS |
6751 | if (enable_ept) |
6752 | vmx_enable_tdp(); | |
6753 | else | |
baa03522 TC |
6754 | kvm_disable_tdp(); |
6755 | ||
6756 | update_ple_window_actual_max(); | |
6757 | ||
843e4330 KH |
6758 | /* |
6759 | * Only enable PML when hardware supports PML feature, and both EPT | |
6760 | * and EPT A/D bit features are enabled -- PML depends on them to work. | |
6761 | */ | |
6762 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) | |
6763 | enable_pml = 0; | |
6764 | ||
6765 | if (!enable_pml) { | |
6766 | kvm_x86_ops->slot_enable_log_dirty = NULL; | |
6767 | kvm_x86_ops->slot_disable_log_dirty = NULL; | |
6768 | kvm_x86_ops->flush_log_dirty = NULL; | |
6769 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; | |
6770 | } | |
6771 | ||
64672c95 YJ |
6772 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
6773 | u64 vmx_msr; | |
6774 | ||
6775 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
6776 | cpu_preemption_timer_multi = | |
6777 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; | |
6778 | } else { | |
6779 | kvm_x86_ops->set_hv_timer = NULL; | |
6780 | kvm_x86_ops->cancel_hv_timer = NULL; | |
6781 | } | |
6782 | ||
bf9f6ac8 FW |
6783 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
6784 | ||
c45dcc71 AR |
6785 | kvm_mce_cap_supported |= MCG_LMCE_P; |
6786 | ||
f2c7648d | 6787 | return alloc_kvm_area(); |
34a1cd60 | 6788 | |
34a1cd60 | 6789 | out: |
23611332 RK |
6790 | for (i = 0; i < VMX_BITMAP_NR; i++) |
6791 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 TC |
6792 | |
6793 | return r; | |
f2c7648d TC |
6794 | } |
6795 | ||
6796 | static __exit void hardware_unsetup(void) | |
6797 | { | |
23611332 RK |
6798 | int i; |
6799 | ||
6800 | for (i = 0; i < VMX_BITMAP_NR; i++) | |
6801 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 | 6802 | |
f2c7648d TC |
6803 | free_kvm_area(); |
6804 | } | |
6805 | ||
4b8d54f9 ZE |
6806 | /* |
6807 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE | |
6808 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. | |
6809 | */ | |
9fb41ba8 | 6810 | static int handle_pause(struct kvm_vcpu *vcpu) |
4b8d54f9 | 6811 | { |
b4a2d31d RK |
6812 | if (ple_gap) |
6813 | grow_ple_window(vcpu); | |
6814 | ||
4b8d54f9 | 6815 | kvm_vcpu_on_spin(vcpu); |
6affcbed | 6816 | return kvm_skip_emulated_instruction(vcpu); |
4b8d54f9 ZE |
6817 | } |
6818 | ||
87c00572 | 6819 | static int handle_nop(struct kvm_vcpu *vcpu) |
59708670 | 6820 | { |
6affcbed | 6821 | return kvm_skip_emulated_instruction(vcpu); |
59708670 SY |
6822 | } |
6823 | ||
87c00572 GS |
6824 | static int handle_mwait(struct kvm_vcpu *vcpu) |
6825 | { | |
6826 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); | |
6827 | return handle_nop(vcpu); | |
6828 | } | |
6829 | ||
5f3d45e7 MD |
6830 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
6831 | { | |
6832 | return 1; | |
6833 | } | |
6834 | ||
87c00572 GS |
6835 | static int handle_monitor(struct kvm_vcpu *vcpu) |
6836 | { | |
6837 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); | |
6838 | return handle_nop(vcpu); | |
6839 | } | |
6840 | ||
ff2f6fe9 NHE |
6841 | /* |
6842 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. | |
6843 | * We could reuse a single VMCS for all the L2 guests, but we also want the | |
6844 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this | |
6845 | * allows keeping them loaded on the processor, and in the future will allow | |
6846 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on | |
6847 | * every entry if they never change. | |
6848 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE | |
6849 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. | |
6850 | * | |
6851 | * The following functions allocate and free a vmcs02 in this pool. | |
6852 | */ | |
6853 | ||
6854 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ | |
6855 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) | |
6856 | { | |
6857 | struct vmcs02_list *item; | |
6858 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) | |
6859 | if (item->vmptr == vmx->nested.current_vmptr) { | |
6860 | list_move(&item->list, &vmx->nested.vmcs02_pool); | |
6861 | return &item->vmcs02; | |
6862 | } | |
6863 | ||
6864 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { | |
6865 | /* Recycle the least recently used VMCS. */ | |
d74c0e6b GT |
6866 | item = list_last_entry(&vmx->nested.vmcs02_pool, |
6867 | struct vmcs02_list, list); | |
ff2f6fe9 NHE |
6868 | item->vmptr = vmx->nested.current_vmptr; |
6869 | list_move(&item->list, &vmx->nested.vmcs02_pool); | |
6870 | return &item->vmcs02; | |
6871 | } | |
6872 | ||
6873 | /* Create a new VMCS */ | |
9338f80f | 6874 | item = kzalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
ff2f6fe9 NHE |
6875 | if (!item) |
6876 | return NULL; | |
6877 | item->vmcs02.vmcs = alloc_vmcs(); | |
355f4fb1 | 6878 | item->vmcs02.shadow_vmcs = NULL; |
ff2f6fe9 NHE |
6879 | if (!item->vmcs02.vmcs) { |
6880 | kfree(item); | |
6881 | return NULL; | |
6882 | } | |
6883 | loaded_vmcs_init(&item->vmcs02); | |
6884 | item->vmptr = vmx->nested.current_vmptr; | |
6885 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); | |
6886 | vmx->nested.vmcs02_num++; | |
6887 | return &item->vmcs02; | |
6888 | } | |
6889 | ||
6890 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ | |
6891 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) | |
6892 | { | |
6893 | struct vmcs02_list *item; | |
6894 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) | |
6895 | if (item->vmptr == vmptr) { | |
6896 | free_loaded_vmcs(&item->vmcs02); | |
6897 | list_del(&item->list); | |
6898 | kfree(item); | |
6899 | vmx->nested.vmcs02_num--; | |
6900 | return; | |
6901 | } | |
6902 | } | |
6903 | ||
6904 | /* | |
6905 | * Free all VMCSs saved for this vcpu, except the one pointed by | |
4fa7734c PB |
6906 | * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs |
6907 | * must be &vmx->vmcs01. | |
ff2f6fe9 NHE |
6908 | */ |
6909 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) | |
6910 | { | |
6911 | struct vmcs02_list *item, *n; | |
4fa7734c PB |
6912 | |
6913 | WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01); | |
ff2f6fe9 | 6914 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
4fa7734c PB |
6915 | /* |
6916 | * Something will leak if the above WARN triggers. Better than | |
6917 | * a use-after-free. | |
6918 | */ | |
6919 | if (vmx->loaded_vmcs == &item->vmcs02) | |
6920 | continue; | |
6921 | ||
6922 | free_loaded_vmcs(&item->vmcs02); | |
ff2f6fe9 NHE |
6923 | list_del(&item->list); |
6924 | kfree(item); | |
4fa7734c | 6925 | vmx->nested.vmcs02_num--; |
ff2f6fe9 | 6926 | } |
ff2f6fe9 NHE |
6927 | } |
6928 | ||
0658fbaa ACL |
6929 | /* |
6930 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), | |
6931 | * set the success or error code of an emulated VMX instruction, as specified | |
6932 | * by Vol 2B, VMX Instruction Reference, "Conventions". | |
6933 | */ | |
6934 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) | |
6935 | { | |
6936 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) | |
6937 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
6938 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); | |
6939 | } | |
6940 | ||
6941 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) | |
6942 | { | |
6943 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
6944 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | | |
6945 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
6946 | | X86_EFLAGS_CF); | |
6947 | } | |
6948 | ||
145c28dd | 6949 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
0658fbaa ACL |
6950 | u32 vm_instruction_error) |
6951 | { | |
6952 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { | |
6953 | /* | |
6954 | * failValid writes the error number to the current VMCS, which | |
6955 | * can't be done there isn't a current VMCS. | |
6956 | */ | |
6957 | nested_vmx_failInvalid(vcpu); | |
6958 | return; | |
6959 | } | |
6960 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
6961 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
6962 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
6963 | | X86_EFLAGS_ZF); | |
6964 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; | |
6965 | /* | |
6966 | * We don't need to force a shadow sync because | |
6967 | * VM_INSTRUCTION_ERROR is not shadowed | |
6968 | */ | |
6969 | } | |
145c28dd | 6970 | |
ff651cb6 WV |
6971 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
6972 | { | |
6973 | /* TODO: not to reset guest simply here. */ | |
6974 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); | |
bbe41b95 | 6975 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
ff651cb6 WV |
6976 | } |
6977 | ||
f4124500 JK |
6978 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
6979 | { | |
6980 | struct vcpu_vmx *vmx = | |
6981 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); | |
6982 | ||
6983 | vmx->nested.preemption_timer_expired = true; | |
6984 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); | |
6985 | kvm_vcpu_kick(&vmx->vcpu); | |
6986 | ||
6987 | return HRTIMER_NORESTART; | |
6988 | } | |
6989 | ||
19677e32 BD |
6990 | /* |
6991 | * Decode the memory-address operand of a vmx instruction, as recorded on an | |
6992 | * exit caused by such an instruction (run by a guest hypervisor). | |
6993 | * On success, returns 0. When the operand is invalid, returns 1 and throws | |
6994 | * #UD or #GP. | |
6995 | */ | |
6996 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, | |
6997 | unsigned long exit_qualification, | |
f9eb4af6 | 6998 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
19677e32 | 6999 | { |
f9eb4af6 EK |
7000 | gva_t off; |
7001 | bool exn; | |
7002 | struct kvm_segment s; | |
7003 | ||
19677e32 BD |
7004 | /* |
7005 | * According to Vol. 3B, "Information for VM Exits Due to Instruction | |
7006 | * Execution", on an exit, vmx_instruction_info holds most of the | |
7007 | * addressing components of the operand. Only the displacement part | |
7008 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). | |
7009 | * For how an actual address is calculated from all these components, | |
7010 | * refer to Vol. 1, "Operand Addressing". | |
7011 | */ | |
7012 | int scaling = vmx_instruction_info & 3; | |
7013 | int addr_size = (vmx_instruction_info >> 7) & 7; | |
7014 | bool is_reg = vmx_instruction_info & (1u << 10); | |
7015 | int seg_reg = (vmx_instruction_info >> 15) & 7; | |
7016 | int index_reg = (vmx_instruction_info >> 18) & 0xf; | |
7017 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); | |
7018 | int base_reg = (vmx_instruction_info >> 23) & 0xf; | |
7019 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); | |
7020 | ||
7021 | if (is_reg) { | |
7022 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7023 | return 1; | |
7024 | } | |
7025 | ||
7026 | /* Addr = segment_base + offset */ | |
7027 | /* offset = base + [index * scale] + displacement */ | |
f9eb4af6 | 7028 | off = exit_qualification; /* holds the displacement */ |
19677e32 | 7029 | if (base_is_valid) |
f9eb4af6 | 7030 | off += kvm_register_read(vcpu, base_reg); |
19677e32 | 7031 | if (index_is_valid) |
f9eb4af6 EK |
7032 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
7033 | vmx_get_segment(vcpu, &s, seg_reg); | |
7034 | *ret = s.base + off; | |
19677e32 BD |
7035 | |
7036 | if (addr_size == 1) /* 32 bit */ | |
7037 | *ret &= 0xffffffff; | |
7038 | ||
f9eb4af6 EK |
7039 | /* Checks for #GP/#SS exceptions. */ |
7040 | exn = false; | |
ff30ef40 QC |
7041 | if (is_long_mode(vcpu)) { |
7042 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a | |
7043 | * non-canonical form. This is the only check on the memory | |
7044 | * destination for long mode! | |
7045 | */ | |
7046 | exn = is_noncanonical_address(*ret); | |
7047 | } else if (is_protmode(vcpu)) { | |
f9eb4af6 EK |
7048 | /* Protected mode: apply checks for segment validity in the |
7049 | * following order: | |
7050 | * - segment type check (#GP(0) may be thrown) | |
7051 | * - usability check (#GP(0)/#SS(0)) | |
7052 | * - limit check (#GP(0)/#SS(0)) | |
7053 | */ | |
7054 | if (wr) | |
7055 | /* #GP(0) if the destination operand is located in a | |
7056 | * read-only data segment or any code segment. | |
7057 | */ | |
7058 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); | |
7059 | else | |
7060 | /* #GP(0) if the source operand is located in an | |
7061 | * execute-only code segment | |
7062 | */ | |
7063 | exn = ((s.type & 0xa) == 8); | |
ff30ef40 QC |
7064 | if (exn) { |
7065 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
7066 | return 1; | |
7067 | } | |
f9eb4af6 EK |
7068 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
7069 | */ | |
7070 | exn = (s.unusable != 0); | |
7071 | /* Protected mode: #GP(0)/#SS(0) if the memory | |
7072 | * operand is outside the segment limit. | |
7073 | */ | |
7074 | exn = exn || (off + sizeof(u64) > s.limit); | |
7075 | } | |
7076 | if (exn) { | |
7077 | kvm_queue_exception_e(vcpu, | |
7078 | seg_reg == VCPU_SREG_SS ? | |
7079 | SS_VECTOR : GP_VECTOR, | |
7080 | 0); | |
7081 | return 1; | |
7082 | } | |
7083 | ||
19677e32 BD |
7084 | return 0; |
7085 | } | |
7086 | ||
cbf71279 | 7087 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer) |
3573e22c BD |
7088 | { |
7089 | gva_t gva; | |
3573e22c | 7090 | struct x86_exception e; |
3573e22c BD |
7091 | |
7092 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 7093 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
3573e22c BD |
7094 | return 1; |
7095 | ||
cbf71279 RK |
7096 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer, |
7097 | sizeof(*vmpointer), &e)) { | |
3573e22c BD |
7098 | kvm_inject_page_fault(vcpu, &e); |
7099 | return 1; | |
7100 | } | |
7101 | ||
3573e22c BD |
7102 | return 0; |
7103 | } | |
7104 | ||
e29acc55 JM |
7105 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
7106 | { | |
7107 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7108 | struct vmcs *shadow_vmcs; | |
7109 | ||
7110 | if (cpu_has_vmx_msr_bitmap()) { | |
7111 | vmx->nested.msr_bitmap = | |
7112 | (unsigned long *)__get_free_page(GFP_KERNEL); | |
7113 | if (!vmx->nested.msr_bitmap) | |
7114 | goto out_msr_bitmap; | |
7115 | } | |
7116 | ||
7117 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); | |
7118 | if (!vmx->nested.cached_vmcs12) | |
7119 | goto out_cached_vmcs12; | |
7120 | ||
7121 | if (enable_shadow_vmcs) { | |
7122 | shadow_vmcs = alloc_vmcs(); | |
7123 | if (!shadow_vmcs) | |
7124 | goto out_shadow_vmcs; | |
7125 | /* mark vmcs as shadow */ | |
7126 | shadow_vmcs->revision_id |= (1u << 31); | |
7127 | /* init shadow vmcs */ | |
7128 | vmcs_clear(shadow_vmcs); | |
7129 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; | |
7130 | } | |
7131 | ||
7132 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); | |
7133 | vmx->nested.vmcs02_num = 0; | |
7134 | ||
7135 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, | |
7136 | HRTIMER_MODE_REL_PINNED); | |
7137 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; | |
7138 | ||
7139 | vmx->nested.vmxon = true; | |
7140 | return 0; | |
7141 | ||
7142 | out_shadow_vmcs: | |
7143 | kfree(vmx->nested.cached_vmcs12); | |
7144 | ||
7145 | out_cached_vmcs12: | |
7146 | free_page((unsigned long)vmx->nested.msr_bitmap); | |
7147 | ||
7148 | out_msr_bitmap: | |
7149 | return -ENOMEM; | |
7150 | } | |
7151 | ||
ec378aee NHE |
7152 | /* |
7153 | * Emulate the VMXON instruction. | |
7154 | * Currently, we just remember that VMX is active, and do not save or even | |
7155 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we | |
7156 | * do not currently need to store anything in that guest-allocated memory | |
7157 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their | |
7158 | * argument is different from the VMXON pointer (which the spec says they do). | |
7159 | */ | |
7160 | static int handle_vmon(struct kvm_vcpu *vcpu) | |
7161 | { | |
e29acc55 | 7162 | int ret; |
cbf71279 RK |
7163 | gpa_t vmptr; |
7164 | struct page *page; | |
ec378aee | 7165 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b3897a49 NHE |
7166 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
7167 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
ec378aee | 7168 | |
70f3aac9 JM |
7169 | /* |
7170 | * The Intel VMX Instruction Reference lists a bunch of bits that are | |
7171 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to | |
7172 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). | |
7173 | * Otherwise, we should fail with #UD. But most faulting conditions | |
7174 | * have already been checked by hardware, prior to the VM-exit for | |
7175 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has | |
7176 | * that bit set to 1 in non-root mode. | |
ec378aee | 7177 | */ |
70f3aac9 | 7178 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
ec378aee NHE |
7179 | kvm_queue_exception(vcpu, UD_VECTOR); |
7180 | return 1; | |
7181 | } | |
7182 | ||
145c28dd AG |
7183 | if (vmx->nested.vmxon) { |
7184 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); | |
6affcbed | 7185 | return kvm_skip_emulated_instruction(vcpu); |
145c28dd | 7186 | } |
b3897a49 | 7187 | |
3b84080b | 7188 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
b3897a49 NHE |
7189 | != VMXON_NEEDED_FEATURES) { |
7190 | kvm_inject_gp(vcpu, 0); | |
7191 | return 1; | |
7192 | } | |
7193 | ||
cbf71279 | 7194 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
21e7fbe7 | 7195 | return 1; |
cbf71279 RK |
7196 | |
7197 | /* | |
7198 | * SDM 3: 24.11.5 | |
7199 | * The first 4 bytes of VMXON region contain the supported | |
7200 | * VMCS revision identifier | |
7201 | * | |
7202 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; | |
7203 | * which replaces physical address width with 32 | |
7204 | */ | |
7205 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { | |
7206 | nested_vmx_failInvalid(vcpu); | |
7207 | return kvm_skip_emulated_instruction(vcpu); | |
7208 | } | |
7209 | ||
7210 | page = nested_get_page(vcpu, vmptr); | |
7211 | if (page == NULL) { | |
7212 | nested_vmx_failInvalid(vcpu); | |
7213 | return kvm_skip_emulated_instruction(vcpu); | |
7214 | } | |
7215 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { | |
7216 | kunmap(page); | |
7217 | nested_release_page_clean(page); | |
7218 | nested_vmx_failInvalid(vcpu); | |
7219 | return kvm_skip_emulated_instruction(vcpu); | |
7220 | } | |
7221 | kunmap(page); | |
7222 | nested_release_page_clean(page); | |
7223 | ||
7224 | vmx->nested.vmxon_ptr = vmptr; | |
e29acc55 JM |
7225 | ret = enter_vmx_operation(vcpu); |
7226 | if (ret) | |
7227 | return ret; | |
ec378aee | 7228 | |
a25eb114 | 7229 | nested_vmx_succeed(vcpu); |
6affcbed | 7230 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7231 | } |
7232 | ||
7233 | /* | |
7234 | * Intel's VMX Instruction Reference specifies a common set of prerequisites | |
7235 | * for running VMX instructions (except VMXON, whose prerequisites are | |
7236 | * slightly different). It also specifies what exception to inject otherwise. | |
70f3aac9 JM |
7237 | * Note that many of these exceptions have priority over VM exits, so they |
7238 | * don't have to be checked again here. | |
ec378aee NHE |
7239 | */ |
7240 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) | |
7241 | { | |
70f3aac9 | 7242 | if (!to_vmx(vcpu)->nested.vmxon) { |
ec378aee NHE |
7243 | kvm_queue_exception(vcpu, UD_VECTOR); |
7244 | return 0; | |
7245 | } | |
ec378aee NHE |
7246 | return 1; |
7247 | } | |
7248 | ||
8ca44e88 DM |
7249 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
7250 | { | |
7251 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); | |
7252 | vmcs_write64(VMCS_LINK_POINTER, -1ull); | |
7253 | } | |
7254 | ||
e7953d7f AG |
7255 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
7256 | { | |
9a2a05b9 PB |
7257 | if (vmx->nested.current_vmptr == -1ull) |
7258 | return; | |
7259 | ||
012f83cb | 7260 | if (enable_shadow_vmcs) { |
9a2a05b9 PB |
7261 | /* copy to memory all shadowed fields in case |
7262 | they were modified */ | |
7263 | copy_shadow_to_vmcs12(vmx); | |
7264 | vmx->nested.sync_shadow_vmcs = false; | |
8ca44e88 | 7265 | vmx_disable_shadow_vmcs(vmx); |
012f83cb | 7266 | } |
705699a1 | 7267 | vmx->nested.posted_intr_nv = -1; |
4f2777bc DM |
7268 | |
7269 | /* Flush VMCS12 to guest memory */ | |
9f744c59 PB |
7270 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
7271 | vmx->nested.current_vmptr >> PAGE_SHIFT, | |
7272 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); | |
4f2777bc | 7273 | |
9a2a05b9 | 7274 | vmx->nested.current_vmptr = -1ull; |
e7953d7f AG |
7275 | } |
7276 | ||
ec378aee NHE |
7277 | /* |
7278 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or | |
7279 | * just stops using VMX. | |
7280 | */ | |
7281 | static void free_nested(struct vcpu_vmx *vmx) | |
7282 | { | |
7283 | if (!vmx->nested.vmxon) | |
7284 | return; | |
9a2a05b9 | 7285 | |
ec378aee | 7286 | vmx->nested.vmxon = false; |
5c614b35 | 7287 | free_vpid(vmx->nested.vpid02); |
8ca44e88 DM |
7288 | vmx->nested.posted_intr_nv = -1; |
7289 | vmx->nested.current_vmptr = -1ull; | |
d048c098 RK |
7290 | if (vmx->nested.msr_bitmap) { |
7291 | free_page((unsigned long)vmx->nested.msr_bitmap); | |
7292 | vmx->nested.msr_bitmap = NULL; | |
7293 | } | |
355f4fb1 | 7294 | if (enable_shadow_vmcs) { |
8ca44e88 | 7295 | vmx_disable_shadow_vmcs(vmx); |
355f4fb1 JM |
7296 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
7297 | free_vmcs(vmx->vmcs01.shadow_vmcs); | |
7298 | vmx->vmcs01.shadow_vmcs = NULL; | |
7299 | } | |
4f2777bc | 7300 | kfree(vmx->nested.cached_vmcs12); |
fe3ef05c NHE |
7301 | /* Unpin physical memory we referred to in current vmcs02 */ |
7302 | if (vmx->nested.apic_access_page) { | |
7303 | nested_release_page(vmx->nested.apic_access_page); | |
48d89b92 | 7304 | vmx->nested.apic_access_page = NULL; |
fe3ef05c | 7305 | } |
a7c0b07d WL |
7306 | if (vmx->nested.virtual_apic_page) { |
7307 | nested_release_page(vmx->nested.virtual_apic_page); | |
48d89b92 | 7308 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 7309 | } |
705699a1 WV |
7310 | if (vmx->nested.pi_desc_page) { |
7311 | kunmap(vmx->nested.pi_desc_page); | |
7312 | nested_release_page(vmx->nested.pi_desc_page); | |
7313 | vmx->nested.pi_desc_page = NULL; | |
7314 | vmx->nested.pi_desc = NULL; | |
7315 | } | |
ff2f6fe9 NHE |
7316 | |
7317 | nested_free_all_saved_vmcss(vmx); | |
ec378aee NHE |
7318 | } |
7319 | ||
7320 | /* Emulate the VMXOFF instruction */ | |
7321 | static int handle_vmoff(struct kvm_vcpu *vcpu) | |
7322 | { | |
7323 | if (!nested_vmx_check_permission(vcpu)) | |
7324 | return 1; | |
7325 | free_nested(to_vmx(vcpu)); | |
a25eb114 | 7326 | nested_vmx_succeed(vcpu); |
6affcbed | 7327 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7328 | } |
7329 | ||
27d6c865 NHE |
7330 | /* Emulate the VMCLEAR instruction */ |
7331 | static int handle_vmclear(struct kvm_vcpu *vcpu) | |
7332 | { | |
7333 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
587d7e72 | 7334 | u32 zero = 0; |
27d6c865 | 7335 | gpa_t vmptr; |
27d6c865 NHE |
7336 | |
7337 | if (!nested_vmx_check_permission(vcpu)) | |
7338 | return 1; | |
7339 | ||
cbf71279 | 7340 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
27d6c865 | 7341 | return 1; |
27d6c865 | 7342 | |
cbf71279 RK |
7343 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
7344 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); | |
7345 | return kvm_skip_emulated_instruction(vcpu); | |
7346 | } | |
7347 | ||
7348 | if (vmptr == vmx->nested.vmxon_ptr) { | |
7349 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); | |
7350 | return kvm_skip_emulated_instruction(vcpu); | |
7351 | } | |
7352 | ||
9a2a05b9 | 7353 | if (vmptr == vmx->nested.current_vmptr) |
e7953d7f | 7354 | nested_release_vmcs12(vmx); |
27d6c865 | 7355 | |
587d7e72 JM |
7356 | kvm_vcpu_write_guest(vcpu, |
7357 | vmptr + offsetof(struct vmcs12, launch_state), | |
7358 | &zero, sizeof(zero)); | |
27d6c865 NHE |
7359 | |
7360 | nested_free_vmcs02(vmx, vmptr); | |
7361 | ||
27d6c865 | 7362 | nested_vmx_succeed(vcpu); |
6affcbed | 7363 | return kvm_skip_emulated_instruction(vcpu); |
27d6c865 NHE |
7364 | } |
7365 | ||
cd232ad0 NHE |
7366 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
7367 | ||
7368 | /* Emulate the VMLAUNCH instruction */ | |
7369 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) | |
7370 | { | |
7371 | return nested_vmx_run(vcpu, true); | |
7372 | } | |
7373 | ||
7374 | /* Emulate the VMRESUME instruction */ | |
7375 | static int handle_vmresume(struct kvm_vcpu *vcpu) | |
7376 | { | |
7377 | ||
7378 | return nested_vmx_run(vcpu, false); | |
7379 | } | |
7380 | ||
49f705c5 NHE |
7381 | /* |
7382 | * Read a vmcs12 field. Since these can have varying lengths and we return | |
7383 | * one type, we chose the biggest type (u64) and zero-extend the return value | |
7384 | * to that size. Note that the caller, handle_vmread, might need to use only | |
7385 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of | |
7386 | * 64-bit fields are to be returned). | |
7387 | */ | |
a2ae9df7 PB |
7388 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
7389 | unsigned long field, u64 *ret) | |
49f705c5 NHE |
7390 | { |
7391 | short offset = vmcs_field_to_offset(field); | |
7392 | char *p; | |
7393 | ||
7394 | if (offset < 0) | |
a2ae9df7 | 7395 | return offset; |
49f705c5 NHE |
7396 | |
7397 | p = ((char *)(get_vmcs12(vcpu))) + offset; | |
7398 | ||
7399 | switch (vmcs_field_type(field)) { | |
7400 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7401 | *ret = *((natural_width *)p); | |
a2ae9df7 | 7402 | return 0; |
49f705c5 NHE |
7403 | case VMCS_FIELD_TYPE_U16: |
7404 | *ret = *((u16 *)p); | |
a2ae9df7 | 7405 | return 0; |
49f705c5 NHE |
7406 | case VMCS_FIELD_TYPE_U32: |
7407 | *ret = *((u32 *)p); | |
a2ae9df7 | 7408 | return 0; |
49f705c5 NHE |
7409 | case VMCS_FIELD_TYPE_U64: |
7410 | *ret = *((u64 *)p); | |
a2ae9df7 | 7411 | return 0; |
49f705c5 | 7412 | default: |
a2ae9df7 PB |
7413 | WARN_ON(1); |
7414 | return -ENOENT; | |
49f705c5 NHE |
7415 | } |
7416 | } | |
7417 | ||
20b97fea | 7418 | |
a2ae9df7 PB |
7419 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
7420 | unsigned long field, u64 field_value){ | |
20b97fea AG |
7421 | short offset = vmcs_field_to_offset(field); |
7422 | char *p = ((char *) get_vmcs12(vcpu)) + offset; | |
7423 | if (offset < 0) | |
a2ae9df7 | 7424 | return offset; |
20b97fea AG |
7425 | |
7426 | switch (vmcs_field_type(field)) { | |
7427 | case VMCS_FIELD_TYPE_U16: | |
7428 | *(u16 *)p = field_value; | |
a2ae9df7 | 7429 | return 0; |
20b97fea AG |
7430 | case VMCS_FIELD_TYPE_U32: |
7431 | *(u32 *)p = field_value; | |
a2ae9df7 | 7432 | return 0; |
20b97fea AG |
7433 | case VMCS_FIELD_TYPE_U64: |
7434 | *(u64 *)p = field_value; | |
a2ae9df7 | 7435 | return 0; |
20b97fea AG |
7436 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
7437 | *(natural_width *)p = field_value; | |
a2ae9df7 | 7438 | return 0; |
20b97fea | 7439 | default: |
a2ae9df7 PB |
7440 | WARN_ON(1); |
7441 | return -ENOENT; | |
20b97fea AG |
7442 | } |
7443 | ||
7444 | } | |
7445 | ||
16f5b903 AG |
7446 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
7447 | { | |
7448 | int i; | |
7449 | unsigned long field; | |
7450 | u64 field_value; | |
355f4fb1 | 7451 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
c2bae893 MK |
7452 | const unsigned long *fields = shadow_read_write_fields; |
7453 | const int num_fields = max_shadow_read_write_fields; | |
16f5b903 | 7454 | |
282da870 JK |
7455 | preempt_disable(); |
7456 | ||
16f5b903 AG |
7457 | vmcs_load(shadow_vmcs); |
7458 | ||
7459 | for (i = 0; i < num_fields; i++) { | |
7460 | field = fields[i]; | |
7461 | switch (vmcs_field_type(field)) { | |
7462 | case VMCS_FIELD_TYPE_U16: | |
7463 | field_value = vmcs_read16(field); | |
7464 | break; | |
7465 | case VMCS_FIELD_TYPE_U32: | |
7466 | field_value = vmcs_read32(field); | |
7467 | break; | |
7468 | case VMCS_FIELD_TYPE_U64: | |
7469 | field_value = vmcs_read64(field); | |
7470 | break; | |
7471 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7472 | field_value = vmcs_readl(field); | |
7473 | break; | |
a2ae9df7 PB |
7474 | default: |
7475 | WARN_ON(1); | |
7476 | continue; | |
16f5b903 AG |
7477 | } |
7478 | vmcs12_write_any(&vmx->vcpu, field, field_value); | |
7479 | } | |
7480 | ||
7481 | vmcs_clear(shadow_vmcs); | |
7482 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
282da870 JK |
7483 | |
7484 | preempt_enable(); | |
16f5b903 AG |
7485 | } |
7486 | ||
c3114420 AG |
7487 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
7488 | { | |
c2bae893 MK |
7489 | const unsigned long *fields[] = { |
7490 | shadow_read_write_fields, | |
7491 | shadow_read_only_fields | |
c3114420 | 7492 | }; |
c2bae893 | 7493 | const int max_fields[] = { |
c3114420 AG |
7494 | max_shadow_read_write_fields, |
7495 | max_shadow_read_only_fields | |
7496 | }; | |
7497 | int i, q; | |
7498 | unsigned long field; | |
7499 | u64 field_value = 0; | |
355f4fb1 | 7500 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
c3114420 AG |
7501 | |
7502 | vmcs_load(shadow_vmcs); | |
7503 | ||
c2bae893 | 7504 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
c3114420 AG |
7505 | for (i = 0; i < max_fields[q]; i++) { |
7506 | field = fields[q][i]; | |
7507 | vmcs12_read_any(&vmx->vcpu, field, &field_value); | |
7508 | ||
7509 | switch (vmcs_field_type(field)) { | |
7510 | case VMCS_FIELD_TYPE_U16: | |
7511 | vmcs_write16(field, (u16)field_value); | |
7512 | break; | |
7513 | case VMCS_FIELD_TYPE_U32: | |
7514 | vmcs_write32(field, (u32)field_value); | |
7515 | break; | |
7516 | case VMCS_FIELD_TYPE_U64: | |
7517 | vmcs_write64(field, (u64)field_value); | |
7518 | break; | |
7519 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7520 | vmcs_writel(field, (long)field_value); | |
7521 | break; | |
a2ae9df7 PB |
7522 | default: |
7523 | WARN_ON(1); | |
7524 | break; | |
c3114420 AG |
7525 | } |
7526 | } | |
7527 | } | |
7528 | ||
7529 | vmcs_clear(shadow_vmcs); | |
7530 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
7531 | } | |
7532 | ||
49f705c5 NHE |
7533 | /* |
7534 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was | |
7535 | * used before) all generate the same failure when it is missing. | |
7536 | */ | |
7537 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) | |
7538 | { | |
7539 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7540 | if (vmx->nested.current_vmptr == -1ull) { | |
7541 | nested_vmx_failInvalid(vcpu); | |
49f705c5 NHE |
7542 | return 0; |
7543 | } | |
7544 | return 1; | |
7545 | } | |
7546 | ||
7547 | static int handle_vmread(struct kvm_vcpu *vcpu) | |
7548 | { | |
7549 | unsigned long field; | |
7550 | u64 field_value; | |
7551 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7552 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7553 | gva_t gva = 0; | |
7554 | ||
eb277562 | 7555 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
7556 | return 1; |
7557 | ||
6affcbed KH |
7558 | if (!nested_vmx_check_vmcs12(vcpu)) |
7559 | return kvm_skip_emulated_instruction(vcpu); | |
49f705c5 NHE |
7560 | |
7561 | /* Decode instruction info and find the field to read */ | |
27e6fb5d | 7562 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 | 7563 | /* Read the field, zero-extended to a u64 field_value */ |
a2ae9df7 | 7564 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
49f705c5 | 7565 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 7566 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7567 | } |
7568 | /* | |
7569 | * Now copy part of this value to register or memory, as requested. | |
7570 | * Note that the number of bits actually copied is 32 or 64 depending | |
7571 | * on the guest's mode (32 or 64 bit), not on the given field's length. | |
7572 | */ | |
7573 | if (vmx_instruction_info & (1u << 10)) { | |
27e6fb5d | 7574 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
49f705c5 NHE |
7575 | field_value); |
7576 | } else { | |
7577 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 7578 | vmx_instruction_info, true, &gva)) |
49f705c5 | 7579 | return 1; |
70f3aac9 | 7580 | /* _system ok, as hardware has verified cpl=0 */ |
49f705c5 NHE |
7581 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
7582 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); | |
7583 | } | |
7584 | ||
7585 | nested_vmx_succeed(vcpu); | |
6affcbed | 7586 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7587 | } |
7588 | ||
7589 | ||
7590 | static int handle_vmwrite(struct kvm_vcpu *vcpu) | |
7591 | { | |
7592 | unsigned long field; | |
7593 | gva_t gva; | |
7594 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7595 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
49f705c5 NHE |
7596 | /* The value to write might be 32 or 64 bits, depending on L1's long |
7597 | * mode, and eventually we need to write that into a field of several | |
7598 | * possible lengths. The code below first zero-extends the value to 64 | |
6a6256f9 | 7599 | * bit (field_value), and then copies only the appropriate number of |
49f705c5 NHE |
7600 | * bits into the vmcs12 field. |
7601 | */ | |
7602 | u64 field_value = 0; | |
7603 | struct x86_exception e; | |
7604 | ||
eb277562 | 7605 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
7606 | return 1; |
7607 | ||
6affcbed KH |
7608 | if (!nested_vmx_check_vmcs12(vcpu)) |
7609 | return kvm_skip_emulated_instruction(vcpu); | |
eb277562 | 7610 | |
49f705c5 | 7611 | if (vmx_instruction_info & (1u << 10)) |
27e6fb5d | 7612 | field_value = kvm_register_readl(vcpu, |
49f705c5 NHE |
7613 | (((vmx_instruction_info) >> 3) & 0xf)); |
7614 | else { | |
7615 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 7616 | vmx_instruction_info, false, &gva)) |
49f705c5 NHE |
7617 | return 1; |
7618 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, | |
27e6fb5d | 7619 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
49f705c5 NHE |
7620 | kvm_inject_page_fault(vcpu, &e); |
7621 | return 1; | |
7622 | } | |
7623 | } | |
7624 | ||
7625 | ||
27e6fb5d | 7626 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 NHE |
7627 | if (vmcs_field_readonly(field)) { |
7628 | nested_vmx_failValid(vcpu, | |
7629 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); | |
6affcbed | 7630 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7631 | } |
7632 | ||
a2ae9df7 | 7633 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
49f705c5 | 7634 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 7635 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7636 | } |
7637 | ||
7638 | nested_vmx_succeed(vcpu); | |
6affcbed | 7639 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7640 | } |
7641 | ||
a8bc284e JM |
7642 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
7643 | { | |
7644 | vmx->nested.current_vmptr = vmptr; | |
7645 | if (enable_shadow_vmcs) { | |
7646 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
7647 | SECONDARY_EXEC_SHADOW_VMCS); | |
7648 | vmcs_write64(VMCS_LINK_POINTER, | |
7649 | __pa(vmx->vmcs01.shadow_vmcs)); | |
7650 | vmx->nested.sync_shadow_vmcs = true; | |
7651 | } | |
7652 | } | |
7653 | ||
63846663 NHE |
7654 | /* Emulate the VMPTRLD instruction */ |
7655 | static int handle_vmptrld(struct kvm_vcpu *vcpu) | |
7656 | { | |
7657 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
63846663 | 7658 | gpa_t vmptr; |
63846663 NHE |
7659 | |
7660 | if (!nested_vmx_check_permission(vcpu)) | |
7661 | return 1; | |
7662 | ||
cbf71279 | 7663 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
63846663 | 7664 | return 1; |
63846663 | 7665 | |
cbf71279 RK |
7666 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
7667 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); | |
7668 | return kvm_skip_emulated_instruction(vcpu); | |
7669 | } | |
7670 | ||
7671 | if (vmptr == vmx->nested.vmxon_ptr) { | |
7672 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); | |
7673 | return kvm_skip_emulated_instruction(vcpu); | |
7674 | } | |
7675 | ||
63846663 NHE |
7676 | if (vmx->nested.current_vmptr != vmptr) { |
7677 | struct vmcs12 *new_vmcs12; | |
7678 | struct page *page; | |
7679 | page = nested_get_page(vcpu, vmptr); | |
7680 | if (page == NULL) { | |
7681 | nested_vmx_failInvalid(vcpu); | |
6affcbed | 7682 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
7683 | } |
7684 | new_vmcs12 = kmap(page); | |
7685 | if (new_vmcs12->revision_id != VMCS12_REVISION) { | |
7686 | kunmap(page); | |
7687 | nested_release_page_clean(page); | |
7688 | nested_vmx_failValid(vcpu, | |
7689 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); | |
6affcbed | 7690 | return kvm_skip_emulated_instruction(vcpu); |
63846663 | 7691 | } |
63846663 | 7692 | |
9a2a05b9 | 7693 | nested_release_vmcs12(vmx); |
4f2777bc DM |
7694 | /* |
7695 | * Load VMCS12 from guest memory since it is not already | |
7696 | * cached. | |
7697 | */ | |
9f744c59 PB |
7698 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
7699 | kunmap(page); | |
7700 | nested_release_page_clean(page); | |
7701 | ||
a8bc284e | 7702 | set_current_vmptr(vmx, vmptr); |
63846663 NHE |
7703 | } |
7704 | ||
7705 | nested_vmx_succeed(vcpu); | |
6affcbed | 7706 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
7707 | } |
7708 | ||
6a4d7550 NHE |
7709 | /* Emulate the VMPTRST instruction */ |
7710 | static int handle_vmptrst(struct kvm_vcpu *vcpu) | |
7711 | { | |
7712 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7713 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7714 | gva_t vmcs_gva; | |
7715 | struct x86_exception e; | |
7716 | ||
7717 | if (!nested_vmx_check_permission(vcpu)) | |
7718 | return 1; | |
7719 | ||
7720 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 7721 | vmx_instruction_info, true, &vmcs_gva)) |
6a4d7550 | 7722 | return 1; |
70f3aac9 | 7723 | /* ok to use *_system, as hardware has verified cpl=0 */ |
6a4d7550 NHE |
7724 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
7725 | (void *)&to_vmx(vcpu)->nested.current_vmptr, | |
7726 | sizeof(u64), &e)) { | |
7727 | kvm_inject_page_fault(vcpu, &e); | |
7728 | return 1; | |
7729 | } | |
7730 | nested_vmx_succeed(vcpu); | |
6affcbed | 7731 | return kvm_skip_emulated_instruction(vcpu); |
6a4d7550 NHE |
7732 | } |
7733 | ||
bfd0a56b NHE |
7734 | /* Emulate the INVEPT instruction */ |
7735 | static int handle_invept(struct kvm_vcpu *vcpu) | |
7736 | { | |
b9c237bb | 7737 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
bfd0a56b NHE |
7738 | u32 vmx_instruction_info, types; |
7739 | unsigned long type; | |
7740 | gva_t gva; | |
7741 | struct x86_exception e; | |
7742 | struct { | |
7743 | u64 eptp, gpa; | |
7744 | } operand; | |
bfd0a56b | 7745 | |
b9c237bb WV |
7746 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
7747 | SECONDARY_EXEC_ENABLE_EPT) || | |
7748 | !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { | |
bfd0a56b NHE |
7749 | kvm_queue_exception(vcpu, UD_VECTOR); |
7750 | return 1; | |
7751 | } | |
7752 | ||
7753 | if (!nested_vmx_check_permission(vcpu)) | |
7754 | return 1; | |
7755 | ||
bfd0a56b | 7756 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
27e6fb5d | 7757 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
bfd0a56b | 7758 | |
b9c237bb | 7759 | types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
bfd0a56b | 7760 | |
85c856b3 | 7761 | if (type >= 32 || !(types & (1 << type))) { |
bfd0a56b NHE |
7762 | nested_vmx_failValid(vcpu, |
7763 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 7764 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
7765 | } |
7766 | ||
7767 | /* According to the Intel VMX instruction reference, the memory | |
7768 | * operand is read even if it isn't needed (e.g., for type==global) | |
7769 | */ | |
7770 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 7771 | vmx_instruction_info, false, &gva)) |
bfd0a56b NHE |
7772 | return 1; |
7773 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, | |
7774 | sizeof(operand), &e)) { | |
7775 | kvm_inject_page_fault(vcpu, &e); | |
7776 | return 1; | |
7777 | } | |
7778 | ||
7779 | switch (type) { | |
bfd0a56b | 7780 | case VMX_EPT_EXTENT_GLOBAL: |
45e11817 BD |
7781 | /* |
7782 | * TODO: track mappings and invalidate | |
7783 | * single context requests appropriately | |
7784 | */ | |
7785 | case VMX_EPT_EXTENT_CONTEXT: | |
bfd0a56b | 7786 | kvm_mmu_sync_roots(vcpu); |
77c3913b | 7787 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
bfd0a56b NHE |
7788 | nested_vmx_succeed(vcpu); |
7789 | break; | |
7790 | default: | |
7791 | BUG_ON(1); | |
7792 | break; | |
7793 | } | |
7794 | ||
6affcbed | 7795 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
7796 | } |
7797 | ||
a642fc30 PM |
7798 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
7799 | { | |
99b83ac8 WL |
7800 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
7801 | u32 vmx_instruction_info; | |
7802 | unsigned long type, types; | |
7803 | gva_t gva; | |
7804 | struct x86_exception e; | |
40352605 JM |
7805 | struct { |
7806 | u64 vpid; | |
7807 | u64 gla; | |
7808 | } operand; | |
99b83ac8 WL |
7809 | |
7810 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & | |
7811 | SECONDARY_EXEC_ENABLE_VPID) || | |
7812 | !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) { | |
7813 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7814 | return 1; | |
7815 | } | |
7816 | ||
7817 | if (!nested_vmx_check_permission(vcpu)) | |
7818 | return 1; | |
7819 | ||
7820 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7821 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); | |
7822 | ||
bcdde302 JD |
7823 | types = (vmx->nested.nested_vmx_vpid_caps & |
7824 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; | |
99b83ac8 | 7825 | |
85c856b3 | 7826 | if (type >= 32 || !(types & (1 << type))) { |
99b83ac8 WL |
7827 | nested_vmx_failValid(vcpu, |
7828 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 7829 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
7830 | } |
7831 | ||
7832 | /* according to the intel vmx instruction reference, the memory | |
7833 | * operand is read even if it isn't needed (e.g., for type==global) | |
7834 | */ | |
7835 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
7836 | vmx_instruction_info, false, &gva)) | |
7837 | return 1; | |
40352605 JM |
7838 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
7839 | sizeof(operand), &e)) { | |
99b83ac8 WL |
7840 | kvm_inject_page_fault(vcpu, &e); |
7841 | return 1; | |
7842 | } | |
40352605 JM |
7843 | if (operand.vpid >> 16) { |
7844 | nested_vmx_failValid(vcpu, | |
7845 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
7846 | return kvm_skip_emulated_instruction(vcpu); | |
7847 | } | |
99b83ac8 WL |
7848 | |
7849 | switch (type) { | |
bcdde302 | 7850 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
40352605 JM |
7851 | if (is_noncanonical_address(operand.gla)) { |
7852 | nested_vmx_failValid(vcpu, | |
7853 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
7854 | return kvm_skip_emulated_instruction(vcpu); | |
7855 | } | |
7856 | /* fall through */ | |
ef697a71 | 7857 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
bcdde302 | 7858 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
40352605 | 7859 | if (!operand.vpid) { |
bcdde302 JD |
7860 | nested_vmx_failValid(vcpu, |
7861 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 7862 | return kvm_skip_emulated_instruction(vcpu); |
bcdde302 JD |
7863 | } |
7864 | break; | |
99b83ac8 | 7865 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
99b83ac8 WL |
7866 | break; |
7867 | default: | |
bcdde302 | 7868 | WARN_ON_ONCE(1); |
6affcbed | 7869 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
7870 | } |
7871 | ||
bcdde302 JD |
7872 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02); |
7873 | nested_vmx_succeed(vcpu); | |
7874 | ||
6affcbed | 7875 | return kvm_skip_emulated_instruction(vcpu); |
a642fc30 PM |
7876 | } |
7877 | ||
843e4330 KH |
7878 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
7879 | { | |
7880 | unsigned long exit_qualification; | |
7881 | ||
7882 | trace_kvm_pml_full(vcpu->vcpu_id); | |
7883 | ||
7884 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7885 | ||
7886 | /* | |
7887 | * PML buffer FULL happened while executing iret from NMI, | |
7888 | * "blocked by NMI" bit has to be set before next VM entry. | |
7889 | */ | |
7890 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && | |
9338f80f | 7891 | cpu_has_virtual_nmis() && |
843e4330 KH |
7892 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
7893 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
7894 | GUEST_INTR_STATE_NMI); | |
7895 | ||
7896 | /* | |
7897 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do | |
7898 | * here.., and there's no userspace involvement needed for PML. | |
7899 | */ | |
7900 | return 1; | |
7901 | } | |
7902 | ||
64672c95 YJ |
7903 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
7904 | { | |
7905 | kvm_lapic_expired_hv_timer(vcpu); | |
7906 | return 1; | |
7907 | } | |
7908 | ||
6aa8b732 AK |
7909 | /* |
7910 | * The exit handlers return 1 if the exit was handled fully and guest execution | |
7911 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs | |
7912 | * to be done to userspace and return 0. | |
7913 | */ | |
772e0318 | 7914 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
6aa8b732 AK |
7915 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
7916 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, | |
988ad74f | 7917 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
f08864b4 | 7918 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
6aa8b732 | 7919 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
6aa8b732 AK |
7920 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
7921 | [EXIT_REASON_DR_ACCESS] = handle_dr, | |
7922 | [EXIT_REASON_CPUID] = handle_cpuid, | |
7923 | [EXIT_REASON_MSR_READ] = handle_rdmsr, | |
7924 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, | |
7925 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, | |
7926 | [EXIT_REASON_HLT] = handle_halt, | |
ec25d5e6 | 7927 | [EXIT_REASON_INVD] = handle_invd, |
a7052897 | 7928 | [EXIT_REASON_INVLPG] = handle_invlpg, |
fee84b07 | 7929 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
c21415e8 | 7930 | [EXIT_REASON_VMCALL] = handle_vmcall, |
27d6c865 | 7931 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
cd232ad0 | 7932 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
63846663 | 7933 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
6a4d7550 | 7934 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
49f705c5 | 7935 | [EXIT_REASON_VMREAD] = handle_vmread, |
cd232ad0 | 7936 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
49f705c5 | 7937 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
ec378aee NHE |
7938 | [EXIT_REASON_VMOFF] = handle_vmoff, |
7939 | [EXIT_REASON_VMON] = handle_vmon, | |
f78e0e2e SY |
7940 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
7941 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, | |
83d4c286 | 7942 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
c7c9c56c | 7943 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
e5edaa01 | 7944 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
2acf923e | 7945 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
37817f29 | 7946 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
a0861c02 | 7947 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
68f89400 MT |
7948 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
7949 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, | |
4b8d54f9 | 7950 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
87c00572 | 7951 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
5f3d45e7 | 7952 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
87c00572 | 7953 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
bfd0a56b | 7954 | [EXIT_REASON_INVEPT] = handle_invept, |
a642fc30 | 7955 | [EXIT_REASON_INVVPID] = handle_invvpid, |
f53cd63c WL |
7956 | [EXIT_REASON_XSAVES] = handle_xsaves, |
7957 | [EXIT_REASON_XRSTORS] = handle_xrstors, | |
843e4330 | 7958 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
64672c95 | 7959 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
6aa8b732 AK |
7960 | }; |
7961 | ||
7962 | static const int kvm_vmx_max_exit_handlers = | |
50a3485c | 7963 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
6aa8b732 | 7964 | |
908a7bdd JK |
7965 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
7966 | struct vmcs12 *vmcs12) | |
7967 | { | |
7968 | unsigned long exit_qualification; | |
7969 | gpa_t bitmap, last_bitmap; | |
7970 | unsigned int port; | |
7971 | int size; | |
7972 | u8 b; | |
7973 | ||
908a7bdd | 7974 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
2f0a6397 | 7975 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
908a7bdd JK |
7976 | |
7977 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7978 | ||
7979 | port = exit_qualification >> 16; | |
7980 | size = (exit_qualification & 7) + 1; | |
7981 | ||
7982 | last_bitmap = (gpa_t)-1; | |
7983 | b = -1; | |
7984 | ||
7985 | while (size > 0) { | |
7986 | if (port < 0x8000) | |
7987 | bitmap = vmcs12->io_bitmap_a; | |
7988 | else if (port < 0x10000) | |
7989 | bitmap = vmcs12->io_bitmap_b; | |
7990 | else | |
1d804d07 | 7991 | return true; |
908a7bdd JK |
7992 | bitmap += (port & 0x7fff) / 8; |
7993 | ||
7994 | if (last_bitmap != bitmap) | |
54bf36aa | 7995 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
1d804d07 | 7996 | return true; |
908a7bdd | 7997 | if (b & (1 << (port & 7))) |
1d804d07 | 7998 | return true; |
908a7bdd JK |
7999 | |
8000 | port++; | |
8001 | size--; | |
8002 | last_bitmap = bitmap; | |
8003 | } | |
8004 | ||
1d804d07 | 8005 | return false; |
908a7bdd JK |
8006 | } |
8007 | ||
644d711a NHE |
8008 | /* |
8009 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, | |
8010 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed | |
8011 | * disinterest in the current event (read or write a specific MSR) by using an | |
8012 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. | |
8013 | */ | |
8014 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, | |
8015 | struct vmcs12 *vmcs12, u32 exit_reason) | |
8016 | { | |
8017 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; | |
8018 | gpa_t bitmap; | |
8019 | ||
cbd29cb6 | 8020 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
1d804d07 | 8021 | return true; |
644d711a NHE |
8022 | |
8023 | /* | |
8024 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, | |
8025 | * for the four combinations of read/write and low/high MSR numbers. | |
8026 | * First we need to figure out which of the four to use: | |
8027 | */ | |
8028 | bitmap = vmcs12->msr_bitmap; | |
8029 | if (exit_reason == EXIT_REASON_MSR_WRITE) | |
8030 | bitmap += 2048; | |
8031 | if (msr_index >= 0xc0000000) { | |
8032 | msr_index -= 0xc0000000; | |
8033 | bitmap += 1024; | |
8034 | } | |
8035 | ||
8036 | /* Then read the msr_index'th bit from this bitmap: */ | |
8037 | if (msr_index < 1024*8) { | |
8038 | unsigned char b; | |
54bf36aa | 8039 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
1d804d07 | 8040 | return true; |
644d711a NHE |
8041 | return 1 & (b >> (msr_index & 7)); |
8042 | } else | |
1d804d07 | 8043 | return true; /* let L1 handle the wrong parameter */ |
644d711a NHE |
8044 | } |
8045 | ||
8046 | /* | |
8047 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, | |
8048 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to | |
8049 | * intercept (via guest_host_mask etc.) the current event. | |
8050 | */ | |
8051 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, | |
8052 | struct vmcs12 *vmcs12) | |
8053 | { | |
8054 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8055 | int cr = exit_qualification & 15; | |
e1d39b17 JS |
8056 | int reg; |
8057 | unsigned long val; | |
644d711a NHE |
8058 | |
8059 | switch ((exit_qualification >> 4) & 3) { | |
8060 | case 0: /* mov to cr */ | |
e1d39b17 JS |
8061 | reg = (exit_qualification >> 8) & 15; |
8062 | val = kvm_register_readl(vcpu, reg); | |
644d711a NHE |
8063 | switch (cr) { |
8064 | case 0: | |
8065 | if (vmcs12->cr0_guest_host_mask & | |
8066 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 8067 | return true; |
644d711a NHE |
8068 | break; |
8069 | case 3: | |
8070 | if ((vmcs12->cr3_target_count >= 1 && | |
8071 | vmcs12->cr3_target_value0 == val) || | |
8072 | (vmcs12->cr3_target_count >= 2 && | |
8073 | vmcs12->cr3_target_value1 == val) || | |
8074 | (vmcs12->cr3_target_count >= 3 && | |
8075 | vmcs12->cr3_target_value2 == val) || | |
8076 | (vmcs12->cr3_target_count >= 4 && | |
8077 | vmcs12->cr3_target_value3 == val)) | |
1d804d07 | 8078 | return false; |
644d711a | 8079 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
1d804d07 | 8080 | return true; |
644d711a NHE |
8081 | break; |
8082 | case 4: | |
8083 | if (vmcs12->cr4_guest_host_mask & | |
8084 | (vmcs12->cr4_read_shadow ^ val)) | |
1d804d07 | 8085 | return true; |
644d711a NHE |
8086 | break; |
8087 | case 8: | |
8088 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) | |
1d804d07 | 8089 | return true; |
644d711a NHE |
8090 | break; |
8091 | } | |
8092 | break; | |
8093 | case 2: /* clts */ | |
8094 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && | |
8095 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) | |
1d804d07 | 8096 | return true; |
644d711a NHE |
8097 | break; |
8098 | case 1: /* mov from cr */ | |
8099 | switch (cr) { | |
8100 | case 3: | |
8101 | if (vmcs12->cpu_based_vm_exec_control & | |
8102 | CPU_BASED_CR3_STORE_EXITING) | |
1d804d07 | 8103 | return true; |
644d711a NHE |
8104 | break; |
8105 | case 8: | |
8106 | if (vmcs12->cpu_based_vm_exec_control & | |
8107 | CPU_BASED_CR8_STORE_EXITING) | |
1d804d07 | 8108 | return true; |
644d711a NHE |
8109 | break; |
8110 | } | |
8111 | break; | |
8112 | case 3: /* lmsw */ | |
8113 | /* | |
8114 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of | |
8115 | * cr0. Other attempted changes are ignored, with no exit. | |
8116 | */ | |
e1d39b17 | 8117 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
644d711a NHE |
8118 | if (vmcs12->cr0_guest_host_mask & 0xe & |
8119 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 8120 | return true; |
644d711a NHE |
8121 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
8122 | !(vmcs12->cr0_read_shadow & 0x1) && | |
8123 | (val & 0x1)) | |
1d804d07 | 8124 | return true; |
644d711a NHE |
8125 | break; |
8126 | } | |
1d804d07 | 8127 | return false; |
644d711a NHE |
8128 | } |
8129 | ||
8130 | /* | |
8131 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we | |
8132 | * should handle it ourselves in L0 (and then continue L2). Only call this | |
8133 | * when in is_guest_mode (L2). | |
8134 | */ | |
7313c698 | 8135 | static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason) |
644d711a | 8136 | { |
644d711a NHE |
8137 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
8138 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8139 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
8140 | ||
542060ea JK |
8141 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
8142 | vmcs_readl(EXIT_QUALIFICATION), | |
8143 | vmx->idt_vectoring_info, | |
8144 | intr_info, | |
8145 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
8146 | KVM_ISA_VMX); | |
8147 | ||
c9f04407 DM |
8148 | /* |
8149 | * The host physical addresses of some pages of guest memory | |
8150 | * are loaded into VMCS02 (e.g. L1's Virtual APIC Page). The CPU | |
8151 | * may write to these pages via their host physical address while | |
8152 | * L2 is running, bypassing any address-translation-based dirty | |
8153 | * tracking (e.g. EPT write protection). | |
8154 | * | |
8155 | * Mark them dirty on every exit from L2 to prevent them from | |
8156 | * getting out of sync with dirty tracking. | |
8157 | */ | |
8158 | nested_mark_vmcs12_pages_dirty(vcpu); | |
8159 | ||
644d711a | 8160 | if (vmx->nested.nested_run_pending) |
1d804d07 | 8161 | return false; |
644d711a NHE |
8162 | |
8163 | if (unlikely(vmx->fail)) { | |
bd80158a JK |
8164 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
8165 | vmcs_read32(VM_INSTRUCTION_ERROR)); | |
1d804d07 | 8166 | return true; |
644d711a NHE |
8167 | } |
8168 | ||
8169 | switch (exit_reason) { | |
8170 | case EXIT_REASON_EXCEPTION_NMI: | |
ef85b673 | 8171 | if (is_nmi(intr_info)) |
1d804d07 | 8172 | return false; |
644d711a | 8173 | else if (is_page_fault(intr_info)) |
52a5c155 | 8174 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
e504c909 | 8175 | else if (is_no_device(intr_info) && |
ccf9844e | 8176 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
1d804d07 | 8177 | return false; |
6f05485d JK |
8178 | else if (is_debug(intr_info) && |
8179 | vcpu->guest_debug & | |
8180 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
8181 | return false; | |
8182 | else if (is_breakpoint(intr_info) && | |
8183 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) | |
8184 | return false; | |
644d711a NHE |
8185 | return vmcs12->exception_bitmap & |
8186 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); | |
8187 | case EXIT_REASON_EXTERNAL_INTERRUPT: | |
1d804d07 | 8188 | return false; |
644d711a | 8189 | case EXIT_REASON_TRIPLE_FAULT: |
1d804d07 | 8190 | return true; |
644d711a | 8191 | case EXIT_REASON_PENDING_INTERRUPT: |
3b656cf7 | 8192 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
644d711a | 8193 | case EXIT_REASON_NMI_WINDOW: |
3b656cf7 | 8194 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
644d711a | 8195 | case EXIT_REASON_TASK_SWITCH: |
1d804d07 | 8196 | return true; |
644d711a | 8197 | case EXIT_REASON_CPUID: |
1d804d07 | 8198 | return true; |
644d711a NHE |
8199 | case EXIT_REASON_HLT: |
8200 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); | |
8201 | case EXIT_REASON_INVD: | |
1d804d07 | 8202 | return true; |
644d711a NHE |
8203 | case EXIT_REASON_INVLPG: |
8204 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); | |
8205 | case EXIT_REASON_RDPMC: | |
8206 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); | |
a5f46457 PB |
8207 | case EXIT_REASON_RDRAND: |
8208 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND); | |
8209 | case EXIT_REASON_RDSEED: | |
8210 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED); | |
b3a2a907 | 8211 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
644d711a NHE |
8212 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
8213 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: | |
8214 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: | |
8215 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: | |
8216 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: | |
8217 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: | |
a642fc30 | 8218 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
644d711a NHE |
8219 | /* |
8220 | * VMX instructions trap unconditionally. This allows L1 to | |
8221 | * emulate them for its L2 guest, i.e., allows 3-level nesting! | |
8222 | */ | |
1d804d07 | 8223 | return true; |
644d711a NHE |
8224 | case EXIT_REASON_CR_ACCESS: |
8225 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); | |
8226 | case EXIT_REASON_DR_ACCESS: | |
8227 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); | |
8228 | case EXIT_REASON_IO_INSTRUCTION: | |
908a7bdd | 8229 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
1b07304c PB |
8230 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
8231 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); | |
644d711a NHE |
8232 | case EXIT_REASON_MSR_READ: |
8233 | case EXIT_REASON_MSR_WRITE: | |
8234 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); | |
8235 | case EXIT_REASON_INVALID_STATE: | |
1d804d07 | 8236 | return true; |
644d711a NHE |
8237 | case EXIT_REASON_MWAIT_INSTRUCTION: |
8238 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); | |
5f3d45e7 MD |
8239 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
8240 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); | |
644d711a NHE |
8241 | case EXIT_REASON_MONITOR_INSTRUCTION: |
8242 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); | |
8243 | case EXIT_REASON_PAUSE_INSTRUCTION: | |
8244 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || | |
8245 | nested_cpu_has2(vmcs12, | |
8246 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); | |
8247 | case EXIT_REASON_MCE_DURING_VMENTRY: | |
1d804d07 | 8248 | return false; |
644d711a | 8249 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
a7c0b07d | 8250 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
644d711a NHE |
8251 | case EXIT_REASON_APIC_ACCESS: |
8252 | return nested_cpu_has2(vmcs12, | |
8253 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
82f0dd4b | 8254 | case EXIT_REASON_APIC_WRITE: |
608406e2 WV |
8255 | case EXIT_REASON_EOI_INDUCED: |
8256 | /* apic_write and eoi_induced should exit unconditionally. */ | |
1d804d07 | 8257 | return true; |
644d711a | 8258 | case EXIT_REASON_EPT_VIOLATION: |
2b1be677 NHE |
8259 | /* |
8260 | * L0 always deals with the EPT violation. If nested EPT is | |
8261 | * used, and the nested mmu code discovers that the address is | |
8262 | * missing in the guest EPT table (EPT12), the EPT violation | |
8263 | * will be injected with nested_ept_inject_page_fault() | |
8264 | */ | |
1d804d07 | 8265 | return false; |
644d711a | 8266 | case EXIT_REASON_EPT_MISCONFIG: |
2b1be677 NHE |
8267 | /* |
8268 | * L2 never uses directly L1's EPT, but rather L0's own EPT | |
8269 | * table (shadow on EPT) or a merged EPT table that L0 built | |
8270 | * (EPT on EPT). So any problems with the structure of the | |
8271 | * table is L0's fault. | |
8272 | */ | |
1d804d07 | 8273 | return false; |
644d711a NHE |
8274 | case EXIT_REASON_WBINVD: |
8275 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); | |
8276 | case EXIT_REASON_XSETBV: | |
1d804d07 | 8277 | return true; |
81dc01f7 WL |
8278 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
8279 | /* | |
8280 | * This should never happen, since it is not possible to | |
8281 | * set XSS to a non-zero value---neither in L1 nor in L2. | |
8282 | * If if it were, XSS would have to be checked against | |
8283 | * the XSS exit bitmap in vmcs12. | |
8284 | */ | |
8285 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); | |
55123e3c WL |
8286 | case EXIT_REASON_PREEMPTION_TIMER: |
8287 | return false; | |
ab007cc9 | 8288 | case EXIT_REASON_PML_FULL: |
03efce6f | 8289 | /* We emulate PML support to L1. */ |
ab007cc9 | 8290 | return false; |
644d711a | 8291 | default: |
1d804d07 | 8292 | return true; |
644d711a NHE |
8293 | } |
8294 | } | |
8295 | ||
7313c698 PB |
8296 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
8297 | { | |
8298 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
8299 | ||
8300 | /* | |
8301 | * At this point, the exit interruption info in exit_intr_info | |
8302 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT | |
8303 | * we need to query the in-kernel LAPIC. | |
8304 | */ | |
8305 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); | |
8306 | if ((exit_intr_info & | |
8307 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == | |
8308 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { | |
8309 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
8310 | vmcs12->vm_exit_intr_error_code = | |
8311 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); | |
8312 | } | |
8313 | ||
8314 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, | |
8315 | vmcs_readl(EXIT_QUALIFICATION)); | |
8316 | return 1; | |
8317 | } | |
8318 | ||
586f9607 AK |
8319 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
8320 | { | |
8321 | *info1 = vmcs_readl(EXIT_QUALIFICATION); | |
8322 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); | |
8323 | } | |
8324 | ||
a3eaa864 | 8325 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
843e4330 | 8326 | { |
a3eaa864 KH |
8327 | if (vmx->pml_pg) { |
8328 | __free_page(vmx->pml_pg); | |
8329 | vmx->pml_pg = NULL; | |
8330 | } | |
843e4330 KH |
8331 | } |
8332 | ||
54bf36aa | 8333 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
843e4330 | 8334 | { |
54bf36aa | 8335 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
843e4330 KH |
8336 | u64 *pml_buf; |
8337 | u16 pml_idx; | |
8338 | ||
8339 | pml_idx = vmcs_read16(GUEST_PML_INDEX); | |
8340 | ||
8341 | /* Do nothing if PML buffer is empty */ | |
8342 | if (pml_idx == (PML_ENTITY_NUM - 1)) | |
8343 | return; | |
8344 | ||
8345 | /* PML index always points to next available PML buffer entity */ | |
8346 | if (pml_idx >= PML_ENTITY_NUM) | |
8347 | pml_idx = 0; | |
8348 | else | |
8349 | pml_idx++; | |
8350 | ||
8351 | pml_buf = page_address(vmx->pml_pg); | |
8352 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { | |
8353 | u64 gpa; | |
8354 | ||
8355 | gpa = pml_buf[pml_idx]; | |
8356 | WARN_ON(gpa & (PAGE_SIZE - 1)); | |
54bf36aa | 8357 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
843e4330 KH |
8358 | } |
8359 | ||
8360 | /* reset PML index */ | |
8361 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
8362 | } | |
8363 | ||
8364 | /* | |
8365 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. | |
8366 | * Called before reporting dirty_bitmap to userspace. | |
8367 | */ | |
8368 | static void kvm_flush_pml_buffers(struct kvm *kvm) | |
8369 | { | |
8370 | int i; | |
8371 | struct kvm_vcpu *vcpu; | |
8372 | /* | |
8373 | * We only need to kick vcpu out of guest mode here, as PML buffer | |
8374 | * is flushed at beginning of all VMEXITs, and it's obvious that only | |
8375 | * vcpus running in guest are possible to have unflushed GPAs in PML | |
8376 | * buffer. | |
8377 | */ | |
8378 | kvm_for_each_vcpu(i, vcpu, kvm) | |
8379 | kvm_vcpu_kick(vcpu); | |
8380 | } | |
8381 | ||
4eb64dce PB |
8382 | static void vmx_dump_sel(char *name, uint32_t sel) |
8383 | { | |
8384 | pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", | |
96794e4e | 8385 | name, vmcs_read16(sel), |
4eb64dce PB |
8386 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
8387 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), | |
8388 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); | |
8389 | } | |
8390 | ||
8391 | static void vmx_dump_dtsel(char *name, uint32_t limit) | |
8392 | { | |
8393 | pr_err("%s limit=0x%08x, base=0x%016lx\n", | |
8394 | name, vmcs_read32(limit), | |
8395 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); | |
8396 | } | |
8397 | ||
8398 | static void dump_vmcs(void) | |
8399 | { | |
8400 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); | |
8401 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); | |
8402 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); | |
8403 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); | |
8404 | u32 secondary_exec_control = 0; | |
8405 | unsigned long cr4 = vmcs_readl(GUEST_CR4); | |
f3531054 | 8406 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
4eb64dce PB |
8407 | int i, n; |
8408 | ||
8409 | if (cpu_has_secondary_exec_ctrls()) | |
8410 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
8411 | ||
8412 | pr_err("*** Guest State ***\n"); | |
8413 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
8414 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), | |
8415 | vmcs_readl(CR0_GUEST_HOST_MASK)); | |
8416 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
8417 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); | |
8418 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); | |
8419 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && | |
8420 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) | |
8421 | { | |
845c5b40 PB |
8422 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
8423 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); | |
8424 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", | |
8425 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); | |
4eb64dce PB |
8426 | } |
8427 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", | |
8428 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); | |
8429 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", | |
8430 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); | |
8431 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
8432 | vmcs_readl(GUEST_SYSENTER_ESP), | |
8433 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); | |
8434 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); | |
8435 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); | |
8436 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); | |
8437 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); | |
8438 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); | |
8439 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); | |
8440 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); | |
8441 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); | |
8442 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); | |
8443 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); | |
8444 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || | |
8445 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) | |
845c5b40 PB |
8446 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
8447 | efer, vmcs_read64(GUEST_IA32_PAT)); | |
8448 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", | |
8449 | vmcs_read64(GUEST_IA32_DEBUGCTL), | |
4eb64dce PB |
8450 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
8451 | if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
845c5b40 PB |
8452 | pr_err("PerfGlobCtl = 0x%016llx\n", |
8453 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce | 8454 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
845c5b40 | 8455 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
4eb64dce PB |
8456 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
8457 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), | |
8458 | vmcs_read32(GUEST_ACTIVITY_STATE)); | |
8459 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) | |
8460 | pr_err("InterruptStatus = %04x\n", | |
8461 | vmcs_read16(GUEST_INTR_STATUS)); | |
8462 | ||
8463 | pr_err("*** Host State ***\n"); | |
8464 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", | |
8465 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); | |
8466 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", | |
8467 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), | |
8468 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), | |
8469 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), | |
8470 | vmcs_read16(HOST_TR_SELECTOR)); | |
8471 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", | |
8472 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), | |
8473 | vmcs_readl(HOST_TR_BASE)); | |
8474 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", | |
8475 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); | |
8476 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", | |
8477 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), | |
8478 | vmcs_readl(HOST_CR4)); | |
8479 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
8480 | vmcs_readl(HOST_IA32_SYSENTER_ESP), | |
8481 | vmcs_read32(HOST_IA32_SYSENTER_CS), | |
8482 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); | |
8483 | if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER)) | |
845c5b40 PB |
8484 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
8485 | vmcs_read64(HOST_IA32_EFER), | |
8486 | vmcs_read64(HOST_IA32_PAT)); | |
4eb64dce | 8487 | if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
845c5b40 PB |
8488 | pr_err("PerfGlobCtl = 0x%016llx\n", |
8489 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce PB |
8490 | |
8491 | pr_err("*** Control State ***\n"); | |
8492 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", | |
8493 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); | |
8494 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); | |
8495 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", | |
8496 | vmcs_read32(EXCEPTION_BITMAP), | |
8497 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), | |
8498 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); | |
8499 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", | |
8500 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), | |
8501 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), | |
8502 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); | |
8503 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", | |
8504 | vmcs_read32(VM_EXIT_INTR_INFO), | |
8505 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
8506 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); | |
8507 | pr_err(" reason=%08x qualification=%016lx\n", | |
8508 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); | |
8509 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", | |
8510 | vmcs_read32(IDT_VECTORING_INFO_FIELD), | |
8511 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); | |
845c5b40 | 8512 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
8cfe9866 | 8513 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
845c5b40 PB |
8514 | pr_err("TSC Multiplier = 0x%016llx\n", |
8515 | vmcs_read64(TSC_MULTIPLIER)); | |
4eb64dce PB |
8516 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
8517 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); | |
8518 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) | |
8519 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); | |
8520 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) | |
845c5b40 | 8521 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
4eb64dce PB |
8522 | n = vmcs_read32(CR3_TARGET_COUNT); |
8523 | for (i = 0; i + 1 < n; i += 4) | |
8524 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", | |
8525 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), | |
8526 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); | |
8527 | if (i < n) | |
8528 | pr_err("CR3 target%u=%016lx\n", | |
8529 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); | |
8530 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) | |
8531 | pr_err("PLE Gap=%08x Window=%08x\n", | |
8532 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); | |
8533 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) | |
8534 | pr_err("Virtual processor ID = 0x%04x\n", | |
8535 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); | |
8536 | } | |
8537 | ||
6aa8b732 AK |
8538 | /* |
8539 | * The guest has exited. See if we can fix it or if we need userspace | |
8540 | * assistance. | |
8541 | */ | |
851ba692 | 8542 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
6aa8b732 | 8543 | { |
29bd8a78 | 8544 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
a0861c02 | 8545 | u32 exit_reason = vmx->exit_reason; |
1155f76a | 8546 | u32 vectoring_info = vmx->idt_vectoring_info; |
29bd8a78 | 8547 | |
8b89fe1f | 8548 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
db1c056c | 8549 | vcpu->arch.gpa_available = false; |
8b89fe1f | 8550 | |
843e4330 KH |
8551 | /* |
8552 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more | |
8553 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before | |
8554 | * querying dirty_bitmap, we only need to kick all vcpus out of guest | |
8555 | * mode as if vcpus is in root mode, the PML buffer must has been | |
8556 | * flushed already. | |
8557 | */ | |
8558 | if (enable_pml) | |
54bf36aa | 8559 | vmx_flush_pml_buffer(vcpu); |
843e4330 | 8560 | |
80ced186 | 8561 | /* If guest state is invalid, start emulating */ |
14168786 | 8562 | if (vmx->emulation_required) |
80ced186 | 8563 | return handle_invalid_guest_state(vcpu); |
1d5a4d9b | 8564 | |
7313c698 PB |
8565 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
8566 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); | |
644d711a | 8567 | |
5120702e | 8568 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
4eb64dce | 8569 | dump_vmcs(); |
5120702e MG |
8570 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
8571 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
8572 | = exit_reason; | |
8573 | return 0; | |
8574 | } | |
8575 | ||
29bd8a78 | 8576 | if (unlikely(vmx->fail)) { |
851ba692 AK |
8577 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
8578 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
29bd8a78 AK |
8579 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
8580 | return 0; | |
8581 | } | |
6aa8b732 | 8582 | |
b9bf6882 XG |
8583 | /* |
8584 | * Note: | |
8585 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by | |
8586 | * delivery event since it indicates guest is accessing MMIO. | |
8587 | * The vm-exit can be triggered again after return to guest that | |
8588 | * will cause infinite loop. | |
8589 | */ | |
d77c26fc | 8590 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
1439442c | 8591 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
60637aac | 8592 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
b244c9fc | 8593 | exit_reason != EXIT_REASON_PML_FULL && |
b9bf6882 XG |
8594 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
8595 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
8596 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; | |
70bcd708 | 8597 | vcpu->run->internal.ndata = 3; |
b9bf6882 XG |
8598 | vcpu->run->internal.data[0] = vectoring_info; |
8599 | vcpu->run->internal.data[1] = exit_reason; | |
70bcd708 PB |
8600 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
8601 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { | |
8602 | vcpu->run->internal.ndata++; | |
8603 | vcpu->run->internal.data[3] = | |
8604 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); | |
8605 | } | |
b9bf6882 XG |
8606 | return 0; |
8607 | } | |
3b86cd99 | 8608 | |
9338f80f PB |
8609 | if (unlikely(!cpu_has_virtual_nmis() && |
8610 | vmx->loaded_vmcs->soft_vnmi_blocked)) { | |
8611 | if (vmx_interrupt_allowed(vcpu)) { | |
8612 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
8613 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && | |
8614 | vcpu->arch.nmi_pending) { | |
8615 | /* | |
8616 | * This CPU don't support us in finding the end of an | |
8617 | * NMI-blocked window if the guest runs with IRQs | |
8618 | * disabled. So we pull the trigger after 1 s of | |
8619 | * futile waiting, but inform the user about this. | |
8620 | */ | |
8621 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " | |
8622 | "state on VCPU %d after 1 s timeout\n", | |
8623 | __func__, vcpu->vcpu_id); | |
8624 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
8625 | } | |
8626 | } | |
8627 | ||
6aa8b732 AK |
8628 | if (exit_reason < kvm_vmx_max_exit_handlers |
8629 | && kvm_vmx_exit_handlers[exit_reason]) | |
851ba692 | 8630 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
6aa8b732 | 8631 | else { |
6c6c5e03 RK |
8632 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
8633 | exit_reason); | |
2bc19dc3 MT |
8634 | kvm_queue_exception(vcpu, UD_VECTOR); |
8635 | return 1; | |
6aa8b732 | 8636 | } |
6aa8b732 AK |
8637 | } |
8638 | ||
95ba8273 | 8639 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
6e5d865c | 8640 | { |
a7c0b07d WL |
8641 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
8642 | ||
8643 | if (is_guest_mode(vcpu) && | |
8644 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
8645 | return; | |
8646 | ||
95ba8273 | 8647 | if (irr == -1 || tpr < irr) { |
6e5d865c YS |
8648 | vmcs_write32(TPR_THRESHOLD, 0); |
8649 | return; | |
8650 | } | |
8651 | ||
95ba8273 | 8652 | vmcs_write32(TPR_THRESHOLD, irr); |
6e5d865c YS |
8653 | } |
8654 | ||
8d14695f YZ |
8655 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
8656 | { | |
8657 | u32 sec_exec_control; | |
8658 | ||
dccbfcf5 RK |
8659 | /* Postpone execution until vmcs01 is the current VMCS. */ |
8660 | if (is_guest_mode(vcpu)) { | |
8661 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; | |
8662 | return; | |
8663 | } | |
8664 | ||
f6e90f9e | 8665 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
8d14695f YZ |
8666 | return; |
8667 | ||
35754c98 | 8668 | if (!cpu_need_tpr_shadow(vcpu)) |
8d14695f YZ |
8669 | return; |
8670 | ||
8671 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
8672 | ||
8673 | if (set) { | |
8674 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
8675 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
8676 | } else { | |
8677 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
8678 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
fb6c8198 | 8679 | vmx_flush_tlb_ept_only(vcpu); |
8d14695f YZ |
8680 | } |
8681 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); | |
8682 | ||
8683 | vmx_set_msr_bitmap(vcpu); | |
8684 | } | |
8685 | ||
38b99173 TC |
8686 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
8687 | { | |
8688 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8689 | ||
8690 | /* | |
8691 | * Currently we do not handle the nested case where L2 has an | |
8692 | * APIC access page of its own; that page is still pinned. | |
8693 | * Hence, we skip the case where the VCPU is in guest mode _and_ | |
8694 | * L1 prepared an APIC access page for L2. | |
8695 | * | |
8696 | * For the case where L1 and L2 share the same APIC access page | |
8697 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear | |
8698 | * in the vmcs12), this function will only update either the vmcs01 | |
8699 | * or the vmcs02. If the former, the vmcs02 will be updated by | |
8700 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in | |
8701 | * the next L2->L1 exit. | |
8702 | */ | |
8703 | if (!is_guest_mode(vcpu) || | |
4f2777bc | 8704 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
fb6c8198 | 8705 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
38b99173 | 8706 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
fb6c8198 JM |
8707 | vmx_flush_tlb_ept_only(vcpu); |
8708 | } | |
38b99173 TC |
8709 | } |
8710 | ||
67c9dddc | 8711 | static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr) |
c7c9c56c YZ |
8712 | { |
8713 | u16 status; | |
8714 | u8 old; | |
8715 | ||
67c9dddc PB |
8716 | if (max_isr == -1) |
8717 | max_isr = 0; | |
c7c9c56c YZ |
8718 | |
8719 | status = vmcs_read16(GUEST_INTR_STATUS); | |
8720 | old = status >> 8; | |
67c9dddc | 8721 | if (max_isr != old) { |
c7c9c56c | 8722 | status &= 0xff; |
67c9dddc | 8723 | status |= max_isr << 8; |
c7c9c56c YZ |
8724 | vmcs_write16(GUEST_INTR_STATUS, status); |
8725 | } | |
8726 | } | |
8727 | ||
8728 | static void vmx_set_rvi(int vector) | |
8729 | { | |
8730 | u16 status; | |
8731 | u8 old; | |
8732 | ||
4114c27d WW |
8733 | if (vector == -1) |
8734 | vector = 0; | |
8735 | ||
c7c9c56c YZ |
8736 | status = vmcs_read16(GUEST_INTR_STATUS); |
8737 | old = (u8)status & 0xff; | |
8738 | if ((u8)vector != old) { | |
8739 | status &= ~0xff; | |
8740 | status |= (u8)vector; | |
8741 | vmcs_write16(GUEST_INTR_STATUS, status); | |
8742 | } | |
8743 | } | |
8744 | ||
8745 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) | |
8746 | { | |
4114c27d WW |
8747 | if (!is_guest_mode(vcpu)) { |
8748 | vmx_set_rvi(max_irr); | |
8749 | return; | |
8750 | } | |
8751 | ||
c7c9c56c YZ |
8752 | if (max_irr == -1) |
8753 | return; | |
8754 | ||
963fee16 | 8755 | /* |
4114c27d WW |
8756 | * In guest mode. If a vmexit is needed, vmx_check_nested_events |
8757 | * handles it. | |
963fee16 | 8758 | */ |
4114c27d | 8759 | if (nested_exit_on_intr(vcpu)) |
963fee16 WL |
8760 | return; |
8761 | ||
963fee16 | 8762 | /* |
4114c27d | 8763 | * Else, fall back to pre-APICv interrupt injection since L2 |
963fee16 WL |
8764 | * is run without virtual interrupt delivery. |
8765 | */ | |
8766 | if (!kvm_event_needs_reinjection(vcpu) && | |
8767 | vmx_interrupt_allowed(vcpu)) { | |
8768 | kvm_queue_interrupt(vcpu, max_irr, false); | |
8769 | vmx_inject_irq(vcpu); | |
8770 | } | |
c7c9c56c YZ |
8771 | } |
8772 | ||
76dfafd5 | 8773 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
810e6def PB |
8774 | { |
8775 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
76dfafd5 | 8776 | int max_irr; |
810e6def | 8777 | |
76dfafd5 PB |
8778 | WARN_ON(!vcpu->arch.apicv_active); |
8779 | if (pi_test_on(&vmx->pi_desc)) { | |
8780 | pi_clear_on(&vmx->pi_desc); | |
8781 | /* | |
8782 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. | |
8783 | * But on x86 this is just a compiler barrier anyway. | |
8784 | */ | |
8785 | smp_mb__after_atomic(); | |
8786 | max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); | |
8787 | } else { | |
8788 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
8789 | } | |
8790 | vmx_hwapic_irr_update(vcpu, max_irr); | |
8791 | return max_irr; | |
810e6def PB |
8792 | } |
8793 | ||
6308630b | 8794 | static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) |
c7c9c56c | 8795 | { |
d62caabb | 8796 | if (!kvm_vcpu_apicv_active(vcpu)) |
3d81bc7e YZ |
8797 | return; |
8798 | ||
c7c9c56c YZ |
8799 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
8800 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); | |
8801 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); | |
8802 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); | |
8803 | } | |
8804 | ||
967235d3 PB |
8805 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
8806 | { | |
8807 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8808 | ||
8809 | pi_clear_on(&vmx->pi_desc); | |
8810 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); | |
8811 | } | |
8812 | ||
51aa01d1 | 8813 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
cf393f75 | 8814 | { |
48ae0fb4 JM |
8815 | u32 exit_intr_info = 0; |
8816 | u16 basic_exit_reason = (u16)vmx->exit_reason; | |
00eba012 | 8817 | |
48ae0fb4 JM |
8818 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
8819 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) | |
00eba012 AK |
8820 | return; |
8821 | ||
48ae0fb4 JM |
8822 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
8823 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
8824 | vmx->exit_intr_info = exit_intr_info; | |
a0861c02 | 8825 | |
1261bfa3 WL |
8826 | /* if exit due to PF check for async PF */ |
8827 | if (is_page_fault(exit_intr_info)) | |
8828 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); | |
8829 | ||
a0861c02 | 8830 | /* Handle machine checks before interrupts are enabled */ |
48ae0fb4 JM |
8831 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
8832 | is_machine_check(exit_intr_info)) | |
a0861c02 AK |
8833 | kvm_machine_check(); |
8834 | ||
20f65983 | 8835 | /* We need to handle NMIs before interrupts are enabled */ |
ef85b673 | 8836 | if (is_nmi(exit_intr_info)) { |
ff9d07a0 | 8837 | kvm_before_handle_nmi(&vmx->vcpu); |
20f65983 | 8838 | asm("int $2"); |
ff9d07a0 ZY |
8839 | kvm_after_handle_nmi(&vmx->vcpu); |
8840 | } | |
51aa01d1 | 8841 | } |
20f65983 | 8842 | |
a547c6db YZ |
8843 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
8844 | { | |
8845 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
8846 | ||
a547c6db YZ |
8847 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
8848 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { | |
8849 | unsigned int vector; | |
8850 | unsigned long entry; | |
8851 | gate_desc *desc; | |
8852 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8853 | #ifdef CONFIG_X86_64 | |
8854 | unsigned long tmp; | |
8855 | #endif | |
8856 | ||
8857 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
8858 | desc = (gate_desc *)vmx->host_idt_base + vector; | |
587719b1 | 8859 | entry = gate_offset(desc); |
a547c6db YZ |
8860 | asm volatile( |
8861 | #ifdef CONFIG_X86_64 | |
8862 | "mov %%" _ASM_SP ", %[sp]\n\t" | |
8863 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" | |
8864 | "push $%c[ss]\n\t" | |
8865 | "push %[sp]\n\t" | |
8866 | #endif | |
8867 | "pushf\n\t" | |
a547c6db | 8868 | __ASM_SIZE(push) " $%c[cs]\n\t" |
e4174080 | 8869 | CALL_NOSPEC |
a547c6db YZ |
8870 | : |
8871 | #ifdef CONFIG_X86_64 | |
3f62de5f | 8872 | [sp]"=&r"(tmp), |
a547c6db | 8873 | #endif |
0d3109a1 | 8874 | ASM_CALL_CONSTRAINT |
a547c6db | 8875 | : |
e4174080 | 8876 | THUNK_TARGET(entry), |
a547c6db YZ |
8877 | [ss]"i"(__KERNEL_DS), |
8878 | [cs]"i"(__KERNEL_CS) | |
8879 | ); | |
f2485b3e | 8880 | } |
a547c6db | 8881 | } |
c207aee4 | 8882 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
a547c6db | 8883 | |
6d396b55 PB |
8884 | static bool vmx_has_high_real_mode_segbase(void) |
8885 | { | |
8886 | return enable_unrestricted_guest || emulate_invalid_guest_state; | |
8887 | } | |
8888 | ||
da8999d3 LJ |
8889 | static bool vmx_mpx_supported(void) |
8890 | { | |
8891 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && | |
8892 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); | |
8893 | } | |
8894 | ||
55412b2e WL |
8895 | static bool vmx_xsaves_supported(void) |
8896 | { | |
8897 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
8898 | SECONDARY_EXEC_XSAVES; | |
8899 | } | |
8900 | ||
51aa01d1 AK |
8901 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
8902 | { | |
c5ca8e57 | 8903 | u32 exit_intr_info; |
51aa01d1 AK |
8904 | bool unblock_nmi; |
8905 | u8 vector; | |
8906 | bool idtv_info_valid; | |
8907 | ||
8908 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
20f65983 | 8909 | |
9338f80f PB |
8910 | if (cpu_has_virtual_nmis()) { |
8911 | if (vmx->loaded_vmcs->nmi_known_unmasked) | |
8912 | return; | |
8913 | /* | |
8914 | * Can't use vmx->exit_intr_info since we're not sure what | |
8915 | * the exit reason is. | |
8916 | */ | |
8917 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
8918 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; | |
8919 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
8920 | /* | |
8921 | * SDM 3: 27.7.1.2 (September 2008) | |
8922 | * Re-set bit "block by NMI" before VM entry if vmexit caused by | |
8923 | * a guest IRET fault. | |
8924 | * SDM 3: 23.2.2 (September 2008) | |
8925 | * Bit 12 is undefined in any of the following cases: | |
8926 | * If the VM exit sets the valid bit in the IDT-vectoring | |
8927 | * information field. | |
8928 | * If the VM exit is due to a double fault. | |
8929 | */ | |
8930 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && | |
8931 | vector != DF_VECTOR && !idtv_info_valid) | |
8932 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
8933 | GUEST_INTR_STATE_NMI); | |
8934 | else | |
8935 | vmx->loaded_vmcs->nmi_known_unmasked = | |
8936 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) | |
8937 | & GUEST_INTR_STATE_NMI); | |
8938 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) | |
8939 | vmx->loaded_vmcs->vnmi_blocked_time += | |
8940 | ktime_to_ns(ktime_sub(ktime_get(), | |
8941 | vmx->loaded_vmcs->entry_time)); | |
51aa01d1 AK |
8942 | } |
8943 | ||
3ab66e8a | 8944 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
83422e17 AK |
8945 | u32 idt_vectoring_info, |
8946 | int instr_len_field, | |
8947 | int error_code_field) | |
51aa01d1 | 8948 | { |
51aa01d1 AK |
8949 | u8 vector; |
8950 | int type; | |
8951 | bool idtv_info_valid; | |
8952 | ||
8953 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
668f612f | 8954 | |
3ab66e8a JK |
8955 | vcpu->arch.nmi_injected = false; |
8956 | kvm_clear_exception_queue(vcpu); | |
8957 | kvm_clear_interrupt_queue(vcpu); | |
37b96e98 GN |
8958 | |
8959 | if (!idtv_info_valid) | |
8960 | return; | |
8961 | ||
3ab66e8a | 8962 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
3842d135 | 8963 | |
668f612f AK |
8964 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
8965 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; | |
37b96e98 | 8966 | |
64a7ec06 | 8967 | switch (type) { |
37b96e98 | 8968 | case INTR_TYPE_NMI_INTR: |
3ab66e8a | 8969 | vcpu->arch.nmi_injected = true; |
668f612f | 8970 | /* |
7b4a25cb | 8971 | * SDM 3: 27.7.1.2 (September 2008) |
37b96e98 GN |
8972 | * Clear bit "block by NMI" before VM entry if a NMI |
8973 | * delivery faulted. | |
668f612f | 8974 | */ |
3ab66e8a | 8975 | vmx_set_nmi_mask(vcpu, false); |
37b96e98 | 8976 | break; |
37b96e98 | 8977 | case INTR_TYPE_SOFT_EXCEPTION: |
3ab66e8a | 8978 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f GN |
8979 | /* fall through */ |
8980 | case INTR_TYPE_HARD_EXCEPTION: | |
35920a35 | 8981 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
83422e17 | 8982 | u32 err = vmcs_read32(error_code_field); |
851eb667 | 8983 | kvm_requeue_exception_e(vcpu, vector, err); |
35920a35 | 8984 | } else |
851eb667 | 8985 | kvm_requeue_exception(vcpu, vector); |
37b96e98 | 8986 | break; |
66fd3f7f | 8987 | case INTR_TYPE_SOFT_INTR: |
3ab66e8a | 8988 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f | 8989 | /* fall through */ |
37b96e98 | 8990 | case INTR_TYPE_EXT_INTR: |
3ab66e8a | 8991 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
37b96e98 GN |
8992 | break; |
8993 | default: | |
8994 | break; | |
f7d9238f | 8995 | } |
cf393f75 AK |
8996 | } |
8997 | ||
83422e17 AK |
8998 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
8999 | { | |
3ab66e8a | 9000 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
83422e17 AK |
9001 | VM_EXIT_INSTRUCTION_LEN, |
9002 | IDT_VECTORING_ERROR_CODE); | |
9003 | } | |
9004 | ||
b463a6f7 AK |
9005 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
9006 | { | |
3ab66e8a | 9007 | __vmx_complete_interrupts(vcpu, |
b463a6f7 AK |
9008 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
9009 | VM_ENTRY_INSTRUCTION_LEN, | |
9010 | VM_ENTRY_EXCEPTION_ERROR_CODE); | |
9011 | ||
9012 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
9013 | } | |
9014 | ||
d7cd9796 GN |
9015 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
9016 | { | |
9017 | int i, nr_msrs; | |
9018 | struct perf_guest_switch_msr *msrs; | |
9019 | ||
9020 | msrs = perf_guest_get_msrs(&nr_msrs); | |
9021 | ||
9022 | if (!msrs) | |
9023 | return; | |
9024 | ||
9025 | for (i = 0; i < nr_msrs; i++) | |
9026 | if (msrs[i].host == msrs[i].guest) | |
9027 | clear_atomic_switch_msr(vmx, msrs[i].msr); | |
9028 | else | |
9029 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, | |
9030 | msrs[i].host); | |
9031 | } | |
9032 | ||
33365e7a | 9033 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
64672c95 YJ |
9034 | { |
9035 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9036 | u64 tscl; | |
9037 | u32 delta_tsc; | |
9038 | ||
9039 | if (vmx->hv_deadline_tsc == -1) | |
9040 | return; | |
9041 | ||
9042 | tscl = rdtsc(); | |
9043 | if (vmx->hv_deadline_tsc > tscl) | |
9044 | /* sure to be 32 bit only because checked on set_hv_timer */ | |
9045 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> | |
9046 | cpu_preemption_timer_multi); | |
9047 | else | |
9048 | delta_tsc = 0; | |
9049 | ||
9050 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); | |
9051 | } | |
9052 | ||
a3b5ba49 | 9053 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
6aa8b732 | 9054 | { |
a2fa3e9f | 9055 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
d6e41f11 | 9056 | unsigned long debugctlmsr, cr3, cr4; |
104f226b | 9057 | |
9338f80f PB |
9058 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
9059 | if (unlikely(!cpu_has_virtual_nmis() && | |
9060 | vmx->loaded_vmcs->soft_vnmi_blocked)) | |
9061 | vmx->loaded_vmcs->entry_time = ktime_get(); | |
9062 | ||
104f226b AK |
9063 | /* Don't enter VMX if guest state is invalid, let the exit handler |
9064 | start emulation until we arrive back to a valid state */ | |
14168786 | 9065 | if (vmx->emulation_required) |
104f226b AK |
9066 | return; |
9067 | ||
a7653ecd RK |
9068 | if (vmx->ple_window_dirty) { |
9069 | vmx->ple_window_dirty = false; | |
9070 | vmcs_write32(PLE_WINDOW, vmx->ple_window); | |
9071 | } | |
9072 | ||
012f83cb AG |
9073 | if (vmx->nested.sync_shadow_vmcs) { |
9074 | copy_vmcs12_to_shadow(vmx); | |
9075 | vmx->nested.sync_shadow_vmcs = false; | |
9076 | } | |
9077 | ||
104f226b AK |
9078 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
9079 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); | |
9080 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) | |
9081 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); | |
9082 | ||
d6e41f11 | 9083 | cr3 = __get_current_cr3_fast(); |
85f01a30 | 9084 | if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) { |
d6e41f11 | 9085 | vmcs_writel(HOST_CR3, cr3); |
85f01a30 | 9086 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
d6e41f11 AL |
9087 | } |
9088 | ||
1e02ce4c | 9089 | cr4 = cr4_read_shadow(); |
85f01a30 | 9090 | if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) { |
d974baa3 | 9091 | vmcs_writel(HOST_CR4, cr4); |
85f01a30 | 9092 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
d974baa3 AL |
9093 | } |
9094 | ||
104f226b AK |
9095 | /* When single-stepping over STI and MOV SS, we must clear the |
9096 | * corresponding interruptibility bits in the guest state. Otherwise | |
9097 | * vmentry fails as it then expects bit 14 (BS) in pending debug | |
9098 | * exceptions being set, but that's not correct for the guest debugging | |
9099 | * case. */ | |
9100 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) | |
9101 | vmx_set_interrupt_shadow(vcpu, 0); | |
9102 | ||
b9dd21e1 PB |
9103 | if (static_cpu_has(X86_FEATURE_PKU) && |
9104 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && | |
9105 | vcpu->arch.pkru != vmx->host_pkru) | |
9106 | __write_pkru(vcpu->arch.pkru); | |
1be0e61c | 9107 | |
d7cd9796 | 9108 | atomic_switch_perf_msrs(vmx); |
dd2f70b2 | 9109 | |
fe170612 KRW |
9110 | /* SMB: Ignore ibrs_inuse but rely on vcpu value */ |
9111 | x86_spec_ctrl_set_guest(vcpu->arch.spec_ctrl); | |
dd2f70b2 | 9112 | |
2a7921b7 | 9113 | debugctlmsr = get_debugctlmsr(); |
d7cd9796 | 9114 | |
64672c95 YJ |
9115 | vmx_arm_hv_timer(vcpu); |
9116 | ||
d462b819 | 9117 | vmx->__launched = vmx->loaded_vmcs->launched; |
104f226b | 9118 | asm( |
6aa8b732 | 9119 | /* Store host registers */ |
b188c81f AK |
9120 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
9121 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ | |
9122 | "push %%" _ASM_CX " \n\t" | |
9123 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" | |
313dbd49 | 9124 | "je 1f \n\t" |
b188c81f | 9125 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
4ecac3fd | 9126 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
313dbd49 | 9127 | "1: \n\t" |
d3edefc0 | 9128 | /* Reload cr2 if changed */ |
b188c81f AK |
9129 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
9130 | "mov %%cr2, %%" _ASM_DX " \n\t" | |
9131 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" | |
d3edefc0 | 9132 | "je 2f \n\t" |
b188c81f | 9133 | "mov %%" _ASM_AX", %%cr2 \n\t" |
d3edefc0 | 9134 | "2: \n\t" |
6aa8b732 | 9135 | /* Check if vmlaunch of vmresume is needed */ |
e08aa78a | 9136 | "cmpl $0, %c[launched](%0) \n\t" |
6aa8b732 | 9137 | /* Load guest registers. Don't clobber flags. */ |
b188c81f AK |
9138 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
9139 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" | |
9140 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" | |
9141 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" | |
9142 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" | |
9143 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" | |
05b3e0c2 | 9144 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
9145 | "mov %c[r8](%0), %%r8 \n\t" |
9146 | "mov %c[r9](%0), %%r9 \n\t" | |
9147 | "mov %c[r10](%0), %%r10 \n\t" | |
9148 | "mov %c[r11](%0), %%r11 \n\t" | |
9149 | "mov %c[r12](%0), %%r12 \n\t" | |
9150 | "mov %c[r13](%0), %%r13 \n\t" | |
9151 | "mov %c[r14](%0), %%r14 \n\t" | |
9152 | "mov %c[r15](%0), %%r15 \n\t" | |
6aa8b732 | 9153 | #endif |
b188c81f | 9154 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
c801949d | 9155 | |
6aa8b732 | 9156 | /* Enter guest mode */ |
83287ea4 | 9157 | "jne 1f \n\t" |
4ecac3fd | 9158 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
83287ea4 AK |
9159 | "jmp 2f \n\t" |
9160 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" | |
9161 | "2: " | |
6aa8b732 | 9162 | /* Save guest registers, load host registers, keep flags */ |
b188c81f | 9163 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
40712fae | 9164 | "pop %0 \n\t" |
65671e7a | 9165 | "setbe %c[fail](%0)\n\t" |
b188c81f AK |
9166 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
9167 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" | |
9168 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" | |
9169 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" | |
9170 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" | |
9171 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" | |
9172 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" | |
05b3e0c2 | 9173 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
9174 | "mov %%r8, %c[r8](%0) \n\t" |
9175 | "mov %%r9, %c[r9](%0) \n\t" | |
9176 | "mov %%r10, %c[r10](%0) \n\t" | |
9177 | "mov %%r11, %c[r11](%0) \n\t" | |
9178 | "mov %%r12, %c[r12](%0) \n\t" | |
9179 | "mov %%r13, %c[r13](%0) \n\t" | |
9180 | "mov %%r14, %c[r14](%0) \n\t" | |
9181 | "mov %%r15, %c[r15](%0) \n\t" | |
65671e7a JM |
9182 | "xor %%r8d, %%r8d \n\t" |
9183 | "xor %%r9d, %%r9d \n\t" | |
9184 | "xor %%r10d, %%r10d \n\t" | |
9185 | "xor %%r11d, %%r11d \n\t" | |
9186 | "xor %%r12d, %%r12d \n\t" | |
9187 | "xor %%r13d, %%r13d \n\t" | |
9188 | "xor %%r14d, %%r14d \n\t" | |
9189 | "xor %%r15d, %%r15d \n\t" | |
6aa8b732 | 9190 | #endif |
b188c81f AK |
9191 | "mov %%cr2, %%" _ASM_AX " \n\t" |
9192 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" | |
c801949d | 9193 | |
65671e7a JM |
9194 | "xor %%eax, %%eax \n\t" |
9195 | "xor %%ebx, %%ebx \n\t" | |
9196 | "xor %%esi, %%esi \n\t" | |
9197 | "xor %%edi, %%edi \n\t" | |
b188c81f | 9198 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
83287ea4 AK |
9199 | ".pushsection .rodata \n\t" |
9200 | ".global vmx_return \n\t" | |
9201 | "vmx_return: " _ASM_PTR " 2b \n\t" | |
9202 | ".popsection" | |
e08aa78a | 9203 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
d462b819 | 9204 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
e08aa78a | 9205 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
313dbd49 | 9206 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
ad312c7c ZX |
9207 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
9208 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), | |
9209 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), | |
9210 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), | |
9211 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), | |
9212 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), | |
9213 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), | |
05b3e0c2 | 9214 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
9215 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
9216 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), | |
9217 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), | |
9218 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), | |
9219 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), | |
9220 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), | |
9221 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), | |
9222 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), | |
6aa8b732 | 9223 | #endif |
40712fae AK |
9224 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
9225 | [wordsize]"i"(sizeof(ulong)) | |
c2036300 LV |
9226 | : "cc", "memory" |
9227 | #ifdef CONFIG_X86_64 | |
b188c81f | 9228 | , "rax", "rbx", "rdi", "rsi" |
c2036300 | 9229 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
b188c81f AK |
9230 | #else |
9231 | , "eax", "ebx", "edi", "esi" | |
c2036300 LV |
9232 | #endif |
9233 | ); | |
6aa8b732 | 9234 | |
fe170612 KRW |
9235 | x86_spec_ctrl_restore_host(vcpu->arch.spec_ctrl); |
9236 | ||
8947886c DW |
9237 | /* Eliminate branch target predictions from guest mode */ |
9238 | vmexit_fill_RSB(); | |
9239 | ||
2a7921b7 GN |
9240 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
9241 | if (debugctlmsr) | |
9242 | update_debugctlmsr(debugctlmsr); | |
9243 | ||
aa67f609 AK |
9244 | #ifndef CONFIG_X86_64 |
9245 | /* | |
9246 | * The sysexit path does not restore ds/es, so we must set them to | |
9247 | * a reasonable value ourselves. | |
9248 | * | |
9249 | * We can't defer this to vmx_load_host_state() since that function | |
9250 | * may be executed in interrupt context, which saves and restore segments | |
9251 | * around it, nullifying its effect. | |
9252 | */ | |
9253 | loadsegment(ds, __USER_DS); | |
9254 | loadsegment(es, __USER_DS); | |
9255 | #endif | |
9256 | ||
6de4f3ad | 9257 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
6de12732 | 9258 | | (1 << VCPU_EXREG_RFLAGS) |
aff48baa | 9259 | | (1 << VCPU_EXREG_PDPTR) |
2fb92db1 | 9260 | | (1 << VCPU_EXREG_SEGMENTS) |
aff48baa | 9261 | | (1 << VCPU_EXREG_CR3)); |
5fdbf976 MT |
9262 | vcpu->arch.regs_dirty = 0; |
9263 | ||
1155f76a AK |
9264 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
9265 | ||
d462b819 | 9266 | vmx->loaded_vmcs->launched = 1; |
1b6269db | 9267 | |
51aa01d1 | 9268 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
51aa01d1 | 9269 | |
1be0e61c XG |
9270 | /* |
9271 | * eager fpu is enabled if PKEY is supported and CR4 is switched | |
9272 | * back on host, so it is safe to read guest PKRU from current | |
9273 | * XSAVE. | |
9274 | */ | |
b9dd21e1 PB |
9275 | if (static_cpu_has(X86_FEATURE_PKU) && |
9276 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { | |
9277 | vcpu->arch.pkru = __read_pkru(); | |
9278 | if (vcpu->arch.pkru != vmx->host_pkru) | |
1be0e61c | 9279 | __write_pkru(vmx->host_pkru); |
1be0e61c XG |
9280 | } |
9281 | ||
e0b890d3 GN |
9282 | /* |
9283 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if | |
9284 | * we did not inject a still-pending event to L1 now because of | |
9285 | * nested_run_pending, we need to re-enable this bit. | |
9286 | */ | |
9287 | if (vmx->nested.nested_run_pending) | |
9288 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
9289 | ||
9290 | vmx->nested.nested_run_pending = 0; | |
9291 | ||
51aa01d1 AK |
9292 | vmx_complete_atomic_exit(vmx); |
9293 | vmx_recover_nmi_blocking(vmx); | |
cf393f75 | 9294 | vmx_complete_interrupts(vmx); |
6aa8b732 | 9295 | } |
c207aee4 | 9296 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
6aa8b732 | 9297 | |
1279a6b1 | 9298 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
4fa7734c PB |
9299 | { |
9300 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9301 | int cpu; | |
9302 | ||
1279a6b1 | 9303 | if (vmx->loaded_vmcs == vmcs) |
4fa7734c PB |
9304 | return; |
9305 | ||
9306 | cpu = get_cpu(); | |
1279a6b1 | 9307 | vmx->loaded_vmcs = vmcs; |
4fa7734c PB |
9308 | vmx_vcpu_put(vcpu); |
9309 | vmx_vcpu_load(vcpu, cpu); | |
9310 | vcpu->cpu = cpu; | |
9311 | put_cpu(); | |
9312 | } | |
9313 | ||
2f1fe811 JM |
9314 | /* |
9315 | * Ensure that the current vmcs of the logical processor is the | |
9316 | * vmcs01 of the vcpu before calling free_nested(). | |
9317 | */ | |
9318 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) | |
9319 | { | |
9320 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9321 | int r; | |
9322 | ||
9323 | r = vcpu_load(vcpu); | |
9324 | BUG_ON(r); | |
1279a6b1 | 9325 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
2f1fe811 JM |
9326 | free_nested(vmx); |
9327 | vcpu_put(vcpu); | |
9328 | } | |
9329 | ||
6aa8b732 AK |
9330 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
9331 | { | |
fb3f0f51 RR |
9332 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
9333 | ||
843e4330 | 9334 | if (enable_pml) |
a3eaa864 | 9335 | vmx_destroy_pml_buffer(vmx); |
991e7a0e | 9336 | free_vpid(vmx->vpid); |
4fa7734c | 9337 | leave_guest_mode(vcpu); |
2f1fe811 | 9338 | vmx_free_vcpu_nested(vcpu); |
4fa7734c | 9339 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 RR |
9340 | kfree(vmx->guest_msrs); |
9341 | kvm_vcpu_uninit(vcpu); | |
a4770347 | 9342 | kmem_cache_free(kvm_vcpu_cache, vmx); |
6aa8b732 AK |
9343 | } |
9344 | ||
fb3f0f51 | 9345 | static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) |
6aa8b732 | 9346 | { |
fb3f0f51 | 9347 | int err; |
c16f862d | 9348 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
15ad7146 | 9349 | int cpu; |
6aa8b732 | 9350 | |
a2fa3e9f | 9351 | if (!vmx) |
fb3f0f51 RR |
9352 | return ERR_PTR(-ENOMEM); |
9353 | ||
991e7a0e | 9354 | vmx->vpid = allocate_vpid(); |
2384d2b3 | 9355 | |
fb3f0f51 RR |
9356 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
9357 | if (err) | |
9358 | goto free_vcpu; | |
965b58a5 | 9359 | |
4e59516a PF |
9360 | err = -ENOMEM; |
9361 | ||
9362 | /* | |
9363 | * If PML is turned on, failure on enabling PML just results in failure | |
9364 | * of creating the vcpu, therefore we can simplify PML logic (by | |
9365 | * avoiding dealing with cases, such as enabling PML partially on vcpus | |
9366 | * for the guest, etc. | |
9367 | */ | |
9368 | if (enable_pml) { | |
9369 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
9370 | if (!vmx->pml_pg) | |
9371 | goto uninit_vcpu; | |
9372 | } | |
9373 | ||
a2fa3e9f | 9374 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
03916db9 PB |
9375 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
9376 | > PAGE_SIZE); | |
0123be42 | 9377 | |
4e59516a PF |
9378 | if (!vmx->guest_msrs) |
9379 | goto free_pml; | |
965b58a5 | 9380 | |
d462b819 NHE |
9381 | vmx->loaded_vmcs = &vmx->vmcs01; |
9382 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); | |
355f4fb1 | 9383 | vmx->loaded_vmcs->shadow_vmcs = NULL; |
d462b819 | 9384 | if (!vmx->loaded_vmcs->vmcs) |
fb3f0f51 | 9385 | goto free_msrs; |
d462b819 | 9386 | loaded_vmcs_init(vmx->loaded_vmcs); |
a2fa3e9f | 9387 | |
15ad7146 AK |
9388 | cpu = get_cpu(); |
9389 | vmx_vcpu_load(&vmx->vcpu, cpu); | |
e48672fa | 9390 | vmx->vcpu.cpu = cpu; |
8b9cf98c | 9391 | err = vmx_vcpu_setup(vmx); |
fb3f0f51 | 9392 | vmx_vcpu_put(&vmx->vcpu); |
15ad7146 | 9393 | put_cpu(); |
fb3f0f51 RR |
9394 | if (err) |
9395 | goto free_vmcs; | |
35754c98 | 9396 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
be6d05cf JK |
9397 | err = alloc_apic_access_page(kvm); |
9398 | if (err) | |
5e4a0b3c | 9399 | goto free_vmcs; |
a63cb560 | 9400 | } |
fb3f0f51 | 9401 | |
b927a3ce SY |
9402 | if (enable_ept) { |
9403 | if (!kvm->arch.ept_identity_map_addr) | |
9404 | kvm->arch.ept_identity_map_addr = | |
9405 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; | |
f51770ed TC |
9406 | err = init_rmode_identity_map(kvm); |
9407 | if (err) | |
93ea5388 | 9408 | goto free_vmcs; |
b927a3ce | 9409 | } |
b7ebfb05 | 9410 | |
5c614b35 | 9411 | if (nested) { |
b9c237bb | 9412 | nested_vmx_setup_ctls_msrs(vmx); |
5c614b35 WL |
9413 | vmx->nested.vpid02 = allocate_vpid(); |
9414 | } | |
b9c237bb | 9415 | |
705699a1 | 9416 | vmx->nested.posted_intr_nv = -1; |
a9d30f33 | 9417 | vmx->nested.current_vmptr = -1ull; |
a9d30f33 | 9418 | |
37e4c997 HZ |
9419 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
9420 | ||
5f19dac4 PB |
9421 | /* |
9422 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR | |
9423 | * or POSTED_INTR_WAKEUP_VECTOR. | |
9424 | */ | |
9425 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; | |
9426 | vmx->pi_desc.sn = 1; | |
9427 | ||
fb3f0f51 RR |
9428 | return &vmx->vcpu; |
9429 | ||
9430 | free_vmcs: | |
5c614b35 | 9431 | free_vpid(vmx->nested.vpid02); |
5f3fbc34 | 9432 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 | 9433 | free_msrs: |
fb3f0f51 | 9434 | kfree(vmx->guest_msrs); |
4e59516a PF |
9435 | free_pml: |
9436 | vmx_destroy_pml_buffer(vmx); | |
fb3f0f51 RR |
9437 | uninit_vcpu: |
9438 | kvm_vcpu_uninit(&vmx->vcpu); | |
9439 | free_vcpu: | |
991e7a0e | 9440 | free_vpid(vmx->vpid); |
a4770347 | 9441 | kmem_cache_free(kvm_vcpu_cache, vmx); |
fb3f0f51 | 9442 | return ERR_PTR(err); |
6aa8b732 AK |
9443 | } |
9444 | ||
002c7f7c YS |
9445 | static void __init vmx_check_processor_compat(void *rtn) |
9446 | { | |
9447 | struct vmcs_config vmcs_conf; | |
9448 | ||
9449 | *(int *)rtn = 0; | |
9450 | if (setup_vmcs_config(&vmcs_conf) < 0) | |
9451 | *(int *)rtn = -EIO; | |
9452 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { | |
9453 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", | |
9454 | smp_processor_id()); | |
9455 | *(int *)rtn = -EIO; | |
9456 | } | |
9457 | } | |
9458 | ||
67253af5 SY |
9459 | static int get_ept_level(void) |
9460 | { | |
9461 | return VMX_EPT_DEFAULT_GAW + 1; | |
9462 | } | |
9463 | ||
4b12f0de | 9464 | static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
64d4d521 | 9465 | { |
b18d5431 XG |
9466 | u8 cache; |
9467 | u64 ipat = 0; | |
4b12f0de | 9468 | |
522c68c4 | 9469 | /* For VT-d and EPT combination |
606decd6 | 9470 | * 1. MMIO: always map as UC |
522c68c4 SY |
9471 | * 2. EPT with VT-d: |
9472 | * a. VT-d without snooping control feature: can't guarantee the | |
606decd6 | 9473 | * result, try to trust guest. |
522c68c4 SY |
9474 | * b. VT-d with snooping control feature: snooping control feature of |
9475 | * VT-d engine can guarantee the cache correctness. Just set it | |
9476 | * to WB to keep consistent with host. So the same as item 3. | |
a19a6d11 | 9477 | * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep |
522c68c4 SY |
9478 | * consistent with host MTRR |
9479 | */ | |
606decd6 PB |
9480 | if (is_mmio) { |
9481 | cache = MTRR_TYPE_UNCACHABLE; | |
9482 | goto exit; | |
9483 | } | |
9484 | ||
9485 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { | |
b18d5431 XG |
9486 | ipat = VMX_EPT_IPAT_BIT; |
9487 | cache = MTRR_TYPE_WRBACK; | |
9488 | goto exit; | |
9489 | } | |
9490 | ||
9491 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { | |
9492 | ipat = VMX_EPT_IPAT_BIT; | |
0da029ed | 9493 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
fb279950 XG |
9494 | cache = MTRR_TYPE_WRBACK; |
9495 | else | |
9496 | cache = MTRR_TYPE_UNCACHABLE; | |
b18d5431 XG |
9497 | goto exit; |
9498 | } | |
9499 | ||
ff53604b | 9500 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
b18d5431 XG |
9501 | |
9502 | exit: | |
9503 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; | |
64d4d521 SY |
9504 | } |
9505 | ||
17cc3935 | 9506 | static int vmx_get_lpage_level(void) |
344f414f | 9507 | { |
878403b7 SY |
9508 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
9509 | return PT_DIRECTORY_LEVEL; | |
9510 | else | |
9511 | /* For shadow and EPT supported 1GB page */ | |
9512 | return PT_PDPE_LEVEL; | |
344f414f JR |
9513 | } |
9514 | ||
feda805f XG |
9515 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
9516 | { | |
9517 | /* | |
9518 | * These bits in the secondary execution controls field | |
9519 | * are dynamic, the others are mostly based on the hypervisor | |
9520 | * architecture and the guest's CPUID. Do not touch the | |
9521 | * dynamic bits. | |
9522 | */ | |
9523 | u32 mask = | |
9524 | SECONDARY_EXEC_SHADOW_VMCS | | |
9525 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | | |
9526 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
9527 | ||
9528 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
9529 | ||
9530 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, | |
9531 | (new_ctl & ~mask) | (cur_ctl & mask)); | |
9532 | } | |
9533 | ||
8322ebbb DM |
9534 | /* |
9535 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits | |
9536 | * (indicating "allowed-1") if they are supported in the guest's CPUID. | |
9537 | */ | |
9538 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) | |
9539 | { | |
9540 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9541 | struct kvm_cpuid_entry2 *entry; | |
9542 | ||
9543 | vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff; | |
9544 | vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE; | |
9545 | ||
9546 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ | |
9547 | if (entry && (entry->_reg & (_cpuid_mask))) \ | |
9548 | vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \ | |
9549 | } while (0) | |
9550 | ||
9551 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); | |
9552 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); | |
9553 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); | |
9554 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); | |
9555 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); | |
9556 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); | |
9557 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); | |
9558 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); | |
9559 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); | |
9560 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); | |
9561 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); | |
9562 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); | |
9563 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); | |
9564 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); | |
9565 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); | |
9566 | ||
9567 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); | |
9568 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); | |
9569 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); | |
9570 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); | |
9571 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); | |
9572 | /* TODO: Use X86_CR4_UMIP and X86_FEATURE_UMIP macros */ | |
9573 | cr4_fixed1_update(bit(11), ecx, bit(2)); | |
9574 | ||
9575 | #undef cr4_fixed1_update | |
9576 | } | |
9577 | ||
0e851880 SY |
9578 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
9579 | { | |
4e47c7a6 SY |
9580 | struct kvm_cpuid_entry2 *best; |
9581 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
feda805f | 9582 | u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx); |
4e47c7a6 | 9583 | |
4e47c7a6 | 9584 | if (vmx_rdtscp_supported()) { |
1cea0ce6 XG |
9585 | bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu); |
9586 | if (!rdtscp_enabled) | |
feda805f | 9587 | secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP; |
f36201e5 | 9588 | |
8b97265a | 9589 | if (nested) { |
1cea0ce6 | 9590 | if (rdtscp_enabled) |
8b97265a PB |
9591 | vmx->nested.nested_vmx_secondary_ctls_high |= |
9592 | SECONDARY_EXEC_RDTSCP; | |
9593 | else | |
9594 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
9595 | ~SECONDARY_EXEC_RDTSCP; | |
9596 | } | |
4e47c7a6 | 9597 | } |
ad756a16 | 9598 | |
ad756a16 MJ |
9599 | /* Exposing INVPCID only when PCID is exposed */ |
9600 | best = kvm_find_cpuid_entry(vcpu, 0x7, 0); | |
9601 | if (vmx_invpcid_supported() && | |
29541bb8 XG |
9602 | (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) || |
9603 | !guest_cpuid_has_pcid(vcpu))) { | |
feda805f | 9604 | secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
29541bb8 | 9605 | |
ad756a16 | 9606 | if (best) |
4f977045 | 9607 | best->ebx &= ~bit(X86_FEATURE_INVPCID); |
ad756a16 | 9608 | } |
8b3e34e4 | 9609 | |
45bdbcfd HH |
9610 | if (cpu_has_secondary_exec_ctrls()) |
9611 | vmcs_set_secondary_exec_control(secondary_exec_ctl); | |
feda805f | 9612 | |
37e4c997 HZ |
9613 | if (nested_vmx_allowed(vcpu)) |
9614 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
9615 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
9616 | else | |
9617 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
9618 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
8322ebbb DM |
9619 | |
9620 | if (nested_vmx_allowed(vcpu)) | |
9621 | nested_vmx_cr_fixed1_bits_update(vcpu); | |
0e851880 SY |
9622 | } |
9623 | ||
d4330ef2 JR |
9624 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
9625 | { | |
7b8050f5 NHE |
9626 | if (func == 1 && nested) |
9627 | entry->ecx |= bit(X86_FEATURE_VMX); | |
d4330ef2 JR |
9628 | } |
9629 | ||
25d92081 YZ |
9630 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
9631 | struct x86_exception *fault) | |
9632 | { | |
533558bc | 9633 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
c5f983f6 | 9634 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
533558bc | 9635 | u32 exit_reason; |
c5f983f6 | 9636 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
25d92081 | 9637 | |
c5f983f6 BD |
9638 | if (vmx->nested.pml_full) { |
9639 | exit_reason = EXIT_REASON_PML_FULL; | |
9640 | vmx->nested.pml_full = false; | |
9641 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; | |
9642 | } else if (fault->error_code & PFERR_RSVD_MASK) | |
533558bc | 9643 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
25d92081 | 9644 | else |
533558bc | 9645 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
c5f983f6 BD |
9646 | |
9647 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); | |
25d92081 YZ |
9648 | vmcs12->guest_physical_address = fault->address; |
9649 | } | |
9650 | ||
995f00a6 PF |
9651 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
9652 | { | |
9653 | return nested_ept_get_cr3(vcpu) & VMX_EPT_AD_ENABLE_BIT; | |
9654 | } | |
9655 | ||
155a97a3 NHE |
9656 | /* Callbacks for nested_ept_init_mmu_context: */ |
9657 | ||
9658 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) | |
9659 | { | |
9660 | /* return the page table to be shadowed - in our case, EPT12 */ | |
9661 | return get_vmcs12(vcpu)->ept_pointer; | |
9662 | } | |
9663 | ||
ae1e2d10 | 9664 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
155a97a3 | 9665 | { |
995f00a6 | 9666 | bool wants_ad; |
ae1e2d10 | 9667 | |
ad896af0 | 9668 | WARN_ON(mmu_is_nested(vcpu)); |
995f00a6 PF |
9669 | wants_ad = nested_ept_ad_enabled(vcpu); |
9670 | if (wants_ad && !enable_ept_ad_bits) | |
ae1e2d10 PB |
9671 | return 1; |
9672 | ||
9673 | kvm_mmu_unload(vcpu); | |
ad896af0 | 9674 | kvm_init_shadow_ept_mmu(vcpu, |
b9c237bb | 9675 | to_vmx(vcpu)->nested.nested_vmx_ept_caps & |
ae1e2d10 | 9676 | VMX_EPT_EXECUTE_ONLY_BIT, |
995f00a6 | 9677 | wants_ad); |
155a97a3 NHE |
9678 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
9679 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; | |
9680 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; | |
9681 | ||
9682 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; | |
ae1e2d10 | 9683 | return 0; |
155a97a3 NHE |
9684 | } |
9685 | ||
9686 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) | |
9687 | { | |
9688 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; | |
9689 | } | |
9690 | ||
19d5f10b EK |
9691 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
9692 | u16 error_code) | |
9693 | { | |
9694 | bool inequality, bit; | |
9695 | ||
9696 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; | |
9697 | inequality = | |
9698 | (error_code & vmcs12->page_fault_error_code_mask) != | |
9699 | vmcs12->page_fault_error_code_match; | |
9700 | return inequality ^ bit; | |
9701 | } | |
9702 | ||
feaf0c7d GN |
9703 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
9704 | struct x86_exception *fault) | |
9705 | { | |
9706 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
9707 | ||
9708 | WARN_ON(!is_guest_mode(vcpu)); | |
9709 | ||
7313c698 | 9710 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) { |
b96fb439 PB |
9711 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
9712 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, | |
9713 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | | |
9714 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, | |
9715 | fault->address); | |
7313c698 | 9716 | } else { |
feaf0c7d | 9717 | kvm_inject_page_fault(vcpu, fault); |
7313c698 | 9718 | } |
feaf0c7d GN |
9719 | } |
9720 | ||
6beb7bd5 JM |
9721 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
9722 | struct vmcs12 *vmcs12); | |
9723 | ||
9724 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, | |
a2bcba50 WL |
9725 | struct vmcs12 *vmcs12) |
9726 | { | |
9727 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6beb7bd5 | 9728 | u64 hpa; |
a2bcba50 WL |
9729 | |
9730 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
a2bcba50 WL |
9731 | /* |
9732 | * Translate L1 physical address to host physical | |
9733 | * address for vmcs02. Keep the page pinned, so this | |
9734 | * physical address remains valid. We keep a reference | |
9735 | * to it so we can release it later. | |
9736 | */ | |
9737 | if (vmx->nested.apic_access_page) /* shouldn't happen */ | |
9738 | nested_release_page(vmx->nested.apic_access_page); | |
9739 | vmx->nested.apic_access_page = | |
9740 | nested_get_page(vcpu, vmcs12->apic_access_addr); | |
6beb7bd5 JM |
9741 | /* |
9742 | * If translation failed, no matter: This feature asks | |
9743 | * to exit when accessing the given address, and if it | |
9744 | * can never be accessed, this feature won't do | |
9745 | * anything anyway. | |
9746 | */ | |
9747 | if (vmx->nested.apic_access_page) { | |
9748 | hpa = page_to_phys(vmx->nested.apic_access_page); | |
9749 | vmcs_write64(APIC_ACCESS_ADDR, hpa); | |
9750 | } else { | |
9751 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
9752 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
9753 | } | |
9754 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && | |
9755 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { | |
9756 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
9757 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
9758 | kvm_vcpu_reload_apic_access_page(vcpu); | |
a2bcba50 | 9759 | } |
a7c0b07d WL |
9760 | |
9761 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { | |
a7c0b07d WL |
9762 | if (vmx->nested.virtual_apic_page) /* shouldn't happen */ |
9763 | nested_release_page(vmx->nested.virtual_apic_page); | |
9764 | vmx->nested.virtual_apic_page = | |
9765 | nested_get_page(vcpu, vmcs12->virtual_apic_page_addr); | |
9766 | ||
9767 | /* | |
6beb7bd5 JM |
9768 | * If translation failed, VM entry will fail because |
9769 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. | |
9770 | * Failing the vm entry is _not_ what the processor | |
9771 | * does but it's basically the only possibility we | |
9772 | * have. We could still enter the guest if CR8 load | |
9773 | * exits are enabled, CR8 store exits are enabled, and | |
9774 | * virtualize APIC access is disabled; in this case | |
9775 | * the processor would never use the TPR shadow and we | |
9776 | * could simply clear the bit from the execution | |
9777 | * control. But such a configuration is useless, so | |
9778 | * let's keep the code simple. | |
a7c0b07d | 9779 | */ |
6beb7bd5 JM |
9780 | if (vmx->nested.virtual_apic_page) { |
9781 | hpa = page_to_phys(vmx->nested.virtual_apic_page); | |
9782 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); | |
9783 | } | |
a7c0b07d WL |
9784 | } |
9785 | ||
705699a1 | 9786 | if (nested_cpu_has_posted_intr(vmcs12)) { |
705699a1 WV |
9787 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
9788 | kunmap(vmx->nested.pi_desc_page); | |
9789 | nested_release_page(vmx->nested.pi_desc_page); | |
9790 | } | |
9791 | vmx->nested.pi_desc_page = | |
9792 | nested_get_page(vcpu, vmcs12->posted_intr_desc_addr); | |
705699a1 WV |
9793 | vmx->nested.pi_desc = |
9794 | (struct pi_desc *)kmap(vmx->nested.pi_desc_page); | |
9795 | if (!vmx->nested.pi_desc) { | |
9796 | nested_release_page_clean(vmx->nested.pi_desc_page); | |
6beb7bd5 | 9797 | return; |
705699a1 WV |
9798 | } |
9799 | vmx->nested.pi_desc = | |
9800 | (struct pi_desc *)((void *)vmx->nested.pi_desc + | |
9801 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
9802 | (PAGE_SIZE - 1))); | |
6beb7bd5 JM |
9803 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
9804 | page_to_phys(vmx->nested.pi_desc_page) + | |
9805 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
9806 | (PAGE_SIZE - 1))); | |
705699a1 | 9807 | } |
6beb7bd5 JM |
9808 | if (cpu_has_vmx_msr_bitmap() && |
9809 | nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) && | |
9810 | nested_vmx_merge_msr_bitmap(vcpu, vmcs12)) | |
9811 | ; | |
9812 | else | |
9813 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, | |
9814 | CPU_BASED_USE_MSR_BITMAPS); | |
a2bcba50 WL |
9815 | } |
9816 | ||
f4124500 JK |
9817 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
9818 | { | |
9819 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; | |
9820 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9821 | ||
9822 | if (vcpu->arch.virtual_tsc_khz == 0) | |
9823 | return; | |
9824 | ||
9825 | /* Make sure short timeouts reliably trigger an immediate vmexit. | |
9826 | * hrtimer_start does not guarantee this. */ | |
9827 | if (preemption_timeout <= 1) { | |
9828 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); | |
9829 | return; | |
9830 | } | |
9831 | ||
9832 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
9833 | preemption_timeout *= 1000000; | |
9834 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); | |
9835 | hrtimer_start(&vmx->nested.preemption_timer, | |
9836 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); | |
9837 | } | |
9838 | ||
56a20510 JM |
9839 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
9840 | struct vmcs12 *vmcs12) | |
9841 | { | |
9842 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) | |
9843 | return 0; | |
9844 | ||
9845 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || | |
9846 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) | |
9847 | return -EINVAL; | |
9848 | ||
9849 | return 0; | |
9850 | } | |
9851 | ||
3af18d9c WV |
9852 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
9853 | struct vmcs12 *vmcs12) | |
9854 | { | |
3af18d9c WV |
9855 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
9856 | return 0; | |
9857 | ||
5fa99cbe | 9858 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
3af18d9c WV |
9859 | return -EINVAL; |
9860 | ||
9861 | return 0; | |
9862 | } | |
9863 | ||
9864 | /* | |
9865 | * Merge L0's and L1's MSR bitmap, return false to indicate that | |
9866 | * we do not use the hardware. | |
9867 | */ | |
9868 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, | |
9869 | struct vmcs12 *vmcs12) | |
9870 | { | |
82f0dd4b | 9871 | int msr; |
f2b93280 | 9872 | struct page *page; |
d048c098 RK |
9873 | unsigned long *msr_bitmap_l1; |
9874 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap; | |
f2b93280 | 9875 | |
d048c098 | 9876 | /* This shortcut is ok because we support only x2APIC MSRs so far. */ |
f2b93280 WV |
9877 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12)) |
9878 | return false; | |
9879 | ||
9880 | page = nested_get_page(vcpu, vmcs12->msr_bitmap); | |
05d8d346 | 9881 | if (!page) |
f2b93280 | 9882 | return false; |
d048c098 | 9883 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
f2b93280 | 9884 | |
d048c098 RK |
9885 | memset(msr_bitmap_l0, 0xff, PAGE_SIZE); |
9886 | ||
f2b93280 | 9887 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
82f0dd4b WV |
9888 | if (nested_cpu_has_apic_reg_virt(vmcs12)) |
9889 | for (msr = 0x800; msr <= 0x8ff; msr++) | |
9890 | nested_vmx_disable_intercept_for_msr( | |
d048c098 | 9891 | msr_bitmap_l1, msr_bitmap_l0, |
82f0dd4b | 9892 | msr, MSR_TYPE_R); |
d048c098 RK |
9893 | |
9894 | nested_vmx_disable_intercept_for_msr( | |
9895 | msr_bitmap_l1, msr_bitmap_l0, | |
f2b93280 WV |
9896 | APIC_BASE_MSR + (APIC_TASKPRI >> 4), |
9897 | MSR_TYPE_R | MSR_TYPE_W); | |
d048c098 | 9898 | |
608406e2 | 9899 | if (nested_cpu_has_vid(vmcs12)) { |
608406e2 | 9900 | nested_vmx_disable_intercept_for_msr( |
d048c098 | 9901 | msr_bitmap_l1, msr_bitmap_l0, |
608406e2 WV |
9902 | APIC_BASE_MSR + (APIC_EOI >> 4), |
9903 | MSR_TYPE_W); | |
9904 | nested_vmx_disable_intercept_for_msr( | |
d048c098 | 9905 | msr_bitmap_l1, msr_bitmap_l0, |
608406e2 WV |
9906 | APIC_BASE_MSR + (APIC_SELF_IPI >> 4), |
9907 | MSR_TYPE_W); | |
9908 | } | |
82f0dd4b | 9909 | } |
f2b93280 WV |
9910 | kunmap(page); |
9911 | nested_release_page_clean(page); | |
9912 | ||
9913 | return true; | |
9914 | } | |
9915 | ||
9916 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, | |
9917 | struct vmcs12 *vmcs12) | |
9918 | { | |
82f0dd4b | 9919 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
608406e2 | 9920 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
705699a1 WV |
9921 | !nested_cpu_has_vid(vmcs12) && |
9922 | !nested_cpu_has_posted_intr(vmcs12)) | |
f2b93280 WV |
9923 | return 0; |
9924 | ||
9925 | /* | |
9926 | * If virtualize x2apic mode is enabled, | |
9927 | * virtualize apic access must be disabled. | |
9928 | */ | |
82f0dd4b WV |
9929 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
9930 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
f2b93280 WV |
9931 | return -EINVAL; |
9932 | ||
608406e2 WV |
9933 | /* |
9934 | * If virtual interrupt delivery is enabled, | |
9935 | * we must exit on external interrupts. | |
9936 | */ | |
9937 | if (nested_cpu_has_vid(vmcs12) && | |
9938 | !nested_exit_on_intr(vcpu)) | |
9939 | return -EINVAL; | |
9940 | ||
705699a1 WV |
9941 | /* |
9942 | * bits 15:8 should be zero in posted_intr_nv, | |
9943 | * the descriptor address has been already checked | |
9944 | * in nested_get_vmcs12_pages. | |
9945 | */ | |
9946 | if (nested_cpu_has_posted_intr(vmcs12) && | |
9947 | (!nested_cpu_has_vid(vmcs12) || | |
9948 | !nested_exit_intr_ack_set(vcpu) || | |
9949 | vmcs12->posted_intr_nv & 0xff00)) | |
9950 | return -EINVAL; | |
9951 | ||
f2b93280 WV |
9952 | /* tpr shadow is needed by all apicv features. */ |
9953 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
9954 | return -EINVAL; | |
9955 | ||
9956 | return 0; | |
3af18d9c WV |
9957 | } |
9958 | ||
e9ac033e EK |
9959 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
9960 | unsigned long count_field, | |
92d71bc6 | 9961 | unsigned long addr_field) |
ff651cb6 | 9962 | { |
92d71bc6 | 9963 | int maxphyaddr; |
e9ac033e EK |
9964 | u64 count, addr; |
9965 | ||
9966 | if (vmcs12_read_any(vcpu, count_field, &count) || | |
9967 | vmcs12_read_any(vcpu, addr_field, &addr)) { | |
9968 | WARN_ON(1); | |
9969 | return -EINVAL; | |
9970 | } | |
9971 | if (count == 0) | |
9972 | return 0; | |
92d71bc6 | 9973 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
e9ac033e EK |
9974 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
9975 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { | |
bbe41b95 | 9976 | pr_debug_ratelimited( |
e9ac033e EK |
9977 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
9978 | addr_field, maxphyaddr, count, addr); | |
9979 | return -EINVAL; | |
9980 | } | |
9981 | return 0; | |
9982 | } | |
9983 | ||
9984 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, | |
9985 | struct vmcs12 *vmcs12) | |
9986 | { | |
e9ac033e EK |
9987 | if (vmcs12->vm_exit_msr_load_count == 0 && |
9988 | vmcs12->vm_exit_msr_store_count == 0 && | |
9989 | vmcs12->vm_entry_msr_load_count == 0) | |
9990 | return 0; /* Fast path */ | |
e9ac033e | 9991 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
92d71bc6 | 9992 | VM_EXIT_MSR_LOAD_ADDR) || |
e9ac033e | 9993 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
92d71bc6 | 9994 | VM_EXIT_MSR_STORE_ADDR) || |
e9ac033e | 9995 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
92d71bc6 | 9996 | VM_ENTRY_MSR_LOAD_ADDR)) |
e9ac033e EK |
9997 | return -EINVAL; |
9998 | return 0; | |
9999 | } | |
10000 | ||
c5f983f6 BD |
10001 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
10002 | struct vmcs12 *vmcs12) | |
10003 | { | |
10004 | u64 address = vmcs12->pml_address; | |
10005 | int maxphyaddr = cpuid_maxphyaddr(vcpu); | |
10006 | ||
10007 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { | |
10008 | if (!nested_cpu_has_ept(vmcs12) || | |
10009 | !IS_ALIGNED(address, 4096) || | |
10010 | address >> maxphyaddr) | |
10011 | return -EINVAL; | |
10012 | } | |
10013 | ||
10014 | return 0; | |
10015 | } | |
10016 | ||
e9ac033e EK |
10017 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
10018 | struct vmx_msr_entry *e) | |
10019 | { | |
10020 | /* x2APIC MSR accesses are not allowed */ | |
8a9781f7 | 10021 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
e9ac033e EK |
10022 | return -EINVAL; |
10023 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ | |
10024 | e->index == MSR_IA32_UCODE_REV) | |
10025 | return -EINVAL; | |
10026 | if (e->reserved != 0) | |
ff651cb6 WV |
10027 | return -EINVAL; |
10028 | return 0; | |
10029 | } | |
10030 | ||
e9ac033e EK |
10031 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
10032 | struct vmx_msr_entry *e) | |
ff651cb6 WV |
10033 | { |
10034 | if (e->index == MSR_FS_BASE || | |
10035 | e->index == MSR_GS_BASE || | |
e9ac033e EK |
10036 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
10037 | nested_vmx_msr_check_common(vcpu, e)) | |
10038 | return -EINVAL; | |
10039 | return 0; | |
10040 | } | |
10041 | ||
10042 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, | |
10043 | struct vmx_msr_entry *e) | |
10044 | { | |
10045 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ | |
10046 | nested_vmx_msr_check_common(vcpu, e)) | |
ff651cb6 WV |
10047 | return -EINVAL; |
10048 | return 0; | |
10049 | } | |
10050 | ||
10051 | /* | |
10052 | * Load guest's/host's msr at nested entry/exit. | |
10053 | * return 0 for success, entry index for failure. | |
10054 | */ | |
10055 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
10056 | { | |
10057 | u32 i; | |
10058 | struct vmx_msr_entry e; | |
10059 | struct msr_data msr; | |
10060 | ||
10061 | msr.host_initiated = false; | |
10062 | for (i = 0; i < count; i++) { | |
54bf36aa PB |
10063 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
10064 | &e, sizeof(e))) { | |
bbe41b95 | 10065 | pr_debug_ratelimited( |
e9ac033e EK |
10066 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
10067 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 10068 | goto fail; |
e9ac033e EK |
10069 | } |
10070 | if (nested_vmx_load_msr_check(vcpu, &e)) { | |
bbe41b95 | 10071 | pr_debug_ratelimited( |
e9ac033e EK |
10072 | "%s check failed (%u, 0x%x, 0x%x)\n", |
10073 | __func__, i, e.index, e.reserved); | |
10074 | goto fail; | |
10075 | } | |
ff651cb6 WV |
10076 | msr.index = e.index; |
10077 | msr.data = e.value; | |
e9ac033e | 10078 | if (kvm_set_msr(vcpu, &msr)) { |
bbe41b95 | 10079 | pr_debug_ratelimited( |
e9ac033e EK |
10080 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
10081 | __func__, i, e.index, e.value); | |
ff651cb6 | 10082 | goto fail; |
e9ac033e | 10083 | } |
ff651cb6 WV |
10084 | } |
10085 | return 0; | |
10086 | fail: | |
10087 | return i + 1; | |
10088 | } | |
10089 | ||
10090 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
10091 | { | |
10092 | u32 i; | |
10093 | struct vmx_msr_entry e; | |
10094 | ||
10095 | for (i = 0; i < count; i++) { | |
609e36d3 | 10096 | struct msr_data msr_info; |
54bf36aa PB |
10097 | if (kvm_vcpu_read_guest(vcpu, |
10098 | gpa + i * sizeof(e), | |
10099 | &e, 2 * sizeof(u32))) { | |
bbe41b95 | 10100 | pr_debug_ratelimited( |
e9ac033e EK |
10101 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
10102 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 10103 | return -EINVAL; |
e9ac033e EK |
10104 | } |
10105 | if (nested_vmx_store_msr_check(vcpu, &e)) { | |
bbe41b95 | 10106 | pr_debug_ratelimited( |
e9ac033e EK |
10107 | "%s check failed (%u, 0x%x, 0x%x)\n", |
10108 | __func__, i, e.index, e.reserved); | |
ff651cb6 | 10109 | return -EINVAL; |
e9ac033e | 10110 | } |
609e36d3 PB |
10111 | msr_info.host_initiated = false; |
10112 | msr_info.index = e.index; | |
10113 | if (kvm_get_msr(vcpu, &msr_info)) { | |
bbe41b95 | 10114 | pr_debug_ratelimited( |
e9ac033e EK |
10115 | "%s cannot read MSR (%u, 0x%x)\n", |
10116 | __func__, i, e.index); | |
10117 | return -EINVAL; | |
10118 | } | |
54bf36aa PB |
10119 | if (kvm_vcpu_write_guest(vcpu, |
10120 | gpa + i * sizeof(e) + | |
10121 | offsetof(struct vmx_msr_entry, value), | |
10122 | &msr_info.data, sizeof(msr_info.data))) { | |
bbe41b95 | 10123 | pr_debug_ratelimited( |
e9ac033e | 10124 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
609e36d3 | 10125 | __func__, i, e.index, msr_info.data); |
e9ac033e EK |
10126 | return -EINVAL; |
10127 | } | |
ff651cb6 WV |
10128 | } |
10129 | return 0; | |
10130 | } | |
10131 | ||
1dc35dac LP |
10132 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
10133 | { | |
10134 | unsigned long invalid_mask; | |
10135 | ||
10136 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); | |
10137 | return (val & invalid_mask) == 0; | |
10138 | } | |
10139 | ||
9ed38ffa LP |
10140 | /* |
10141 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are | |
10142 | * emulating VM entry into a guest with EPT enabled. | |
10143 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code | |
10144 | * is assigned to entry_failure_code on failure. | |
10145 | */ | |
10146 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept, | |
ca0bde28 | 10147 | u32 *entry_failure_code) |
9ed38ffa | 10148 | { |
9ed38ffa | 10149 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
1dc35dac | 10150 | if (!nested_cr3_valid(vcpu, cr3)) { |
9ed38ffa LP |
10151 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
10152 | return 1; | |
10153 | } | |
10154 | ||
10155 | /* | |
10156 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and | |
10157 | * must not be dereferenced. | |
10158 | */ | |
10159 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && | |
10160 | !nested_ept) { | |
10161 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { | |
10162 | *entry_failure_code = ENTRY_FAIL_PDPTE; | |
10163 | return 1; | |
10164 | } | |
10165 | } | |
10166 | ||
10167 | vcpu->arch.cr3 = cr3; | |
10168 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
10169 | } | |
10170 | ||
10171 | kvm_mmu_reset_context(vcpu); | |
10172 | return 0; | |
10173 | } | |
10174 | ||
fe3ef05c NHE |
10175 | /* |
10176 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested | |
10177 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it | |
b4619660 | 10178 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
fe3ef05c NHE |
10179 | * guest in a way that will both be appropriate to L1's requests, and our |
10180 | * needs. In addition to modifying the active vmcs (which is vmcs02), this | |
10181 | * function also has additional necessary side-effects, like setting various | |
10182 | * vcpu->arch fields. | |
ee146c1c LP |
10183 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
10184 | * is assigned to entry_failure_code on failure. | |
fe3ef05c | 10185 | */ |
ee146c1c | 10186 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
ca0bde28 | 10187 | bool from_vmentry, u32 *entry_failure_code) |
fe3ef05c NHE |
10188 | { |
10189 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
03efce6f | 10190 | u32 exec_control, vmcs12_exec_ctrl; |
fe3ef05c NHE |
10191 | |
10192 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); | |
10193 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); | |
10194 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); | |
10195 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); | |
10196 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); | |
10197 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); | |
10198 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); | |
10199 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); | |
10200 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); | |
10201 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); | |
10202 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); | |
10203 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); | |
10204 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); | |
10205 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); | |
10206 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); | |
10207 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); | |
10208 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); | |
10209 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); | |
10210 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); | |
10211 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); | |
10212 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); | |
10213 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); | |
10214 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); | |
10215 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); | |
10216 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); | |
10217 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); | |
10218 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); | |
10219 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); | |
10220 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); | |
10221 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); | |
10222 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); | |
10223 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); | |
10224 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); | |
10225 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); | |
10226 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); | |
10227 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); | |
10228 | ||
cf8b84f4 JM |
10229 | if (from_vmentry && |
10230 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { | |
2996fca0 JK |
10231 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
10232 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); | |
10233 | } else { | |
10234 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); | |
10235 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); | |
10236 | } | |
cf8b84f4 JM |
10237 | if (from_vmentry) { |
10238 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, | |
10239 | vmcs12->vm_entry_intr_info_field); | |
10240 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, | |
10241 | vmcs12->vm_entry_exception_error_code); | |
10242 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
10243 | vmcs12->vm_entry_instruction_len); | |
10244 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, | |
10245 | vmcs12->guest_interruptibility_info); | |
2d6144e3 WL |
10246 | vmx->loaded_vmcs->nmi_known_unmasked = |
10247 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); | |
cf8b84f4 JM |
10248 | } else { |
10249 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
10250 | } | |
fe3ef05c | 10251 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
63fbf59f | 10252 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
fe3ef05c NHE |
10253 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
10254 | vmcs12->guest_pending_dbg_exceptions); | |
10255 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); | |
10256 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); | |
10257 | ||
81dc01f7 WL |
10258 | if (nested_cpu_has_xsaves(vmcs12)) |
10259 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); | |
fe3ef05c NHE |
10260 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
10261 | ||
f4124500 | 10262 | exec_control = vmcs12->pin_based_vm_exec_control; |
9314006d PB |
10263 | |
10264 | /* Preemption timer setting is only taken from vmcs01. */ | |
705699a1 | 10265 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
9314006d PB |
10266 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
10267 | if (vmx->hv_deadline_tsc == -1) | |
10268 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
705699a1 | 10269 | |
9314006d | 10270 | /* Posted interrupts setting is only taken from vmcs12. */ |
705699a1 | 10271 | if (nested_cpu_has_posted_intr(vmcs12)) { |
705699a1 WV |
10272 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
10273 | vmx->nested.pi_pending = false; | |
06a5524f | 10274 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
6beb7bd5 | 10275 | } else { |
705699a1 | 10276 | exec_control &= ~PIN_BASED_POSTED_INTR; |
6beb7bd5 | 10277 | } |
705699a1 | 10278 | |
f4124500 | 10279 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
fe3ef05c | 10280 | |
f4124500 JK |
10281 | vmx->nested.preemption_timer_expired = false; |
10282 | if (nested_cpu_has_preemption_timer(vmcs12)) | |
10283 | vmx_start_preemption_timer(vcpu); | |
0238ea91 | 10284 | |
fe3ef05c NHE |
10285 | /* |
10286 | * Whether page-faults are trapped is determined by a combination of | |
10287 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. | |
10288 | * If enable_ept, L0 doesn't care about page faults and we should | |
10289 | * set all of these to L1's desires. However, if !enable_ept, L0 does | |
10290 | * care about (at least some) page faults, and because it is not easy | |
10291 | * (if at all possible?) to merge L0 and L1's desires, we simply ask | |
10292 | * to exit on each and every L2 page fault. This is done by setting | |
10293 | * MASK=MATCH=0 and (see below) EB.PF=1. | |
10294 | * Note that below we don't need special code to set EB.PF beyond the | |
10295 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, | |
10296 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when | |
10297 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. | |
fe3ef05c NHE |
10298 | */ |
10299 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, | |
10300 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); | |
10301 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, | |
10302 | enable_ept ? vmcs12->page_fault_error_code_match : 0); | |
10303 | ||
10304 | if (cpu_has_secondary_exec_ctrls()) { | |
f4124500 | 10305 | exec_control = vmx_secondary_exec_control(vmx); |
e2821620 | 10306 | |
fe3ef05c | 10307 | /* Take the following fields only from vmcs12 */ |
696dfd95 | 10308 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
b3a2a907 | 10309 | SECONDARY_EXEC_RDTSCP | |
696dfd95 | 10310 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
dfa169bb | 10311 | SECONDARY_EXEC_APIC_REGISTER_VIRT); |
fe3ef05c | 10312 | if (nested_cpu_has(vmcs12, |
03efce6f BD |
10313 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
10314 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & | |
10315 | ~SECONDARY_EXEC_ENABLE_PML; | |
10316 | exec_control |= vmcs12_exec_ctrl; | |
10317 | } | |
fe3ef05c | 10318 | |
608406e2 WV |
10319 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { |
10320 | vmcs_write64(EOI_EXIT_BITMAP0, | |
10321 | vmcs12->eoi_exit_bitmap0); | |
10322 | vmcs_write64(EOI_EXIT_BITMAP1, | |
10323 | vmcs12->eoi_exit_bitmap1); | |
10324 | vmcs_write64(EOI_EXIT_BITMAP2, | |
10325 | vmcs12->eoi_exit_bitmap2); | |
10326 | vmcs_write64(EOI_EXIT_BITMAP3, | |
10327 | vmcs12->eoi_exit_bitmap3); | |
10328 | vmcs_write16(GUEST_INTR_STATUS, | |
10329 | vmcs12->guest_intr_status); | |
10330 | } | |
10331 | ||
6beb7bd5 JM |
10332 | /* |
10333 | * Write an illegal value to APIC_ACCESS_ADDR. Later, | |
10334 | * nested_get_vmcs12_pages will either fix it up or | |
10335 | * remove the VM execution control. | |
10336 | */ | |
10337 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) | |
10338 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); | |
10339 | ||
fe3ef05c NHE |
10340 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
10341 | } | |
10342 | ||
10343 | ||
10344 | /* | |
10345 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) | |
10346 | * Some constant fields are set here by vmx_set_constant_host_state(). | |
10347 | * Other fields are different per CPU, and will be set later when | |
10348 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. | |
10349 | */ | |
a547c6db | 10350 | vmx_set_constant_host_state(vmx); |
fe3ef05c | 10351 | |
83bafef1 JM |
10352 | /* |
10353 | * Set the MSR load/store lists to match L0's settings. | |
10354 | */ | |
10355 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); | |
10356 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
10357 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); | |
10358 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
10359 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); | |
10360 | ||
fe3ef05c NHE |
10361 | /* |
10362 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before | |
10363 | * entry, but only if the current (host) sp changed from the value | |
10364 | * we wrote last (vmx->host_rsp). This cache is no longer relevant | |
10365 | * if we switch vmcs, and rather than hold a separate cache per vmcs, | |
10366 | * here we just force the write to happen on entry. | |
10367 | */ | |
10368 | vmx->host_rsp = 0; | |
10369 | ||
10370 | exec_control = vmx_exec_control(vmx); /* L0's desires */ | |
10371 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; | |
10372 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; | |
10373 | exec_control &= ~CPU_BASED_TPR_SHADOW; | |
10374 | exec_control |= vmcs12->cpu_based_vm_exec_control; | |
a7c0b07d | 10375 | |
6beb7bd5 JM |
10376 | /* |
10377 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if | |
10378 | * nested_get_vmcs12_pages can't fix it up, the illegal value | |
10379 | * will result in a VM entry failure. | |
10380 | */ | |
a7c0b07d | 10381 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
6beb7bd5 | 10382 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
a7c0b07d | 10383 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
20414bb5 JM |
10384 | } else { |
10385 | #ifdef CONFIG_X86_64 | |
10386 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | | |
10387 | CPU_BASED_CR8_STORE_EXITING; | |
10388 | #endif | |
a7c0b07d WL |
10389 | } |
10390 | ||
fe3ef05c | 10391 | /* |
3af18d9c | 10392 | * Merging of IO bitmap not currently supported. |
fe3ef05c NHE |
10393 | * Rather, exit every time. |
10394 | */ | |
fe3ef05c NHE |
10395 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
10396 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; | |
10397 | ||
10398 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); | |
10399 | ||
10400 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the | |
10401 | * bitwise-or of what L1 wants to trap for L2, and what we want to | |
10402 | * trap. Note that CR0.TS also needs updating - we do this later. | |
10403 | */ | |
10404 | update_exception_bitmap(vcpu); | |
10405 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; | |
10406 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); | |
10407 | ||
8049d651 NHE |
10408 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
10409 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER | |
10410 | * bits are further modified by vmx_set_efer() below. | |
10411 | */ | |
f4124500 | 10412 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
8049d651 NHE |
10413 | |
10414 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are | |
10415 | * emulated by vmx_set_efer(), below. | |
10416 | */ | |
2961e876 | 10417 | vm_entry_controls_init(vmx, |
8049d651 NHE |
10418 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
10419 | ~VM_ENTRY_IA32E_MODE) | | |
fe3ef05c NHE |
10420 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
10421 | ||
cf8b84f4 JM |
10422 | if (from_vmentry && |
10423 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { | |
fe3ef05c | 10424 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
44811c02 | 10425 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
cf8b84f4 | 10426 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
fe3ef05c | 10427 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
cf8b84f4 | 10428 | } |
fe3ef05c NHE |
10429 | |
10430 | set_cr4_guest_host_mask(vmx); | |
10431 | ||
cf8b84f4 JM |
10432 | if (from_vmentry && |
10433 | vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) | |
36be0b9d PB |
10434 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
10435 | ||
27fc51b2 NHE |
10436 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
10437 | vmcs_write64(TSC_OFFSET, | |
ea26e4ec | 10438 | vcpu->arch.tsc_offset + vmcs12->tsc_offset); |
27fc51b2 | 10439 | else |
ea26e4ec | 10440 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
c95ba92a PF |
10441 | if (kvm_has_tsc_control) |
10442 | decache_tsc_multiplier(vmx); | |
fe3ef05c NHE |
10443 | |
10444 | if (enable_vpid) { | |
10445 | /* | |
5c614b35 WL |
10446 | * There is no direct mapping between vpid02 and vpid12, the |
10447 | * vpid02 is per-vCPU for L0 and reused while the value of | |
10448 | * vpid12 is changed w/ one invvpid during nested vmentry. | |
10449 | * The vpid12 is allocated by L1 for L2, so it will not | |
10450 | * influence global bitmap(for vpid01 and vpid02 allocation) | |
10451 | * even if spawn a lot of nested vCPUs. | |
fe3ef05c | 10452 | */ |
5c614b35 WL |
10453 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
10454 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); | |
10455 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { | |
10456 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; | |
10457 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); | |
10458 | } | |
10459 | } else { | |
10460 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
10461 | vmx_flush_tlb(vcpu); | |
10462 | } | |
10463 | ||
fe3ef05c NHE |
10464 | } |
10465 | ||
1fb883bb LP |
10466 | if (enable_pml) { |
10467 | /* | |
10468 | * Conceptually we want to copy the PML address and index from | |
10469 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, | |
10470 | * since we always flush the log on each vmexit, this happens | |
10471 | * to be equivalent to simply resetting the fields in vmcs02. | |
10472 | */ | |
10473 | ASSERT(vmx->pml_pg); | |
10474 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
10475 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
10476 | } | |
10477 | ||
155a97a3 | 10478 | if (nested_cpu_has_ept(vmcs12)) { |
ae1e2d10 PB |
10479 | if (nested_ept_init_mmu_context(vcpu)) { |
10480 | *entry_failure_code = ENTRY_FAIL_DEFAULT; | |
10481 | return 1; | |
10482 | } | |
fb6c8198 JM |
10483 | } else if (nested_cpu_has2(vmcs12, |
10484 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
10485 | vmx_flush_tlb_ept_only(vcpu); | |
155a97a3 NHE |
10486 | } |
10487 | ||
fe3ef05c | 10488 | /* |
bd7e5b08 PB |
10489 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
10490 | * bits which we consider mandatory enabled. | |
fe3ef05c NHE |
10491 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
10492 | * the specifications by L1; It's not enough to take | |
10493 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we | |
10494 | * have more bits than L1 expected. | |
10495 | */ | |
10496 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); | |
10497 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); | |
10498 | ||
10499 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); | |
10500 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); | |
10501 | ||
cf8b84f4 JM |
10502 | if (from_vmentry && |
10503 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) | |
5a6a9748 DM |
10504 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
10505 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) | |
10506 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); | |
10507 | else | |
10508 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
10509 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ | |
10510 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
10511 | ||
9ed38ffa | 10512 | /* Shadow page tables on either EPT or shadow page tables. */ |
7ad658b6 | 10513 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
9ed38ffa LP |
10514 | entry_failure_code)) |
10515 | return 1; | |
7ca29de2 | 10516 | |
feaf0c7d GN |
10517 | if (!enable_ept) |
10518 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; | |
10519 | ||
3633cfc3 NHE |
10520 | /* |
10521 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 | |
10522 | */ | |
10523 | if (enable_ept) { | |
10524 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); | |
10525 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); | |
10526 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); | |
10527 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); | |
10528 | } | |
10529 | ||
fe3ef05c NHE |
10530 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
10531 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); | |
ee146c1c | 10532 | return 0; |
fe3ef05c NHE |
10533 | } |
10534 | ||
ca0bde28 | 10535 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
cd232ad0 | 10536 | { |
cd232ad0 | 10537 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
7c177938 | 10538 | |
6dfacadd | 10539 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
ca0bde28 JM |
10540 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
10541 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
26539bd0 | 10542 | |
56a20510 JM |
10543 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
10544 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10545 | ||
ca0bde28 JM |
10546 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
10547 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
7c177938 | 10548 | |
ca0bde28 JM |
10549 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
10550 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
f2b93280 | 10551 | |
ca0bde28 JM |
10552 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
10553 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
e9ac033e | 10554 | |
c5f983f6 BD |
10555 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
10556 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10557 | ||
7c177938 | 10558 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
0115f9cb | 10559 | vmx->nested.nested_vmx_procbased_ctls_low, |
b9c237bb | 10560 | vmx->nested.nested_vmx_procbased_ctls_high) || |
2e5b0bd9 JM |
10561 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
10562 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, | |
10563 | vmx->nested.nested_vmx_secondary_ctls_low, | |
10564 | vmx->nested.nested_vmx_secondary_ctls_high)) || | |
7c177938 | 10565 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
b9c237bb WV |
10566 | vmx->nested.nested_vmx_pinbased_ctls_low, |
10567 | vmx->nested.nested_vmx_pinbased_ctls_high) || | |
7c177938 | 10568 | !vmx_control_verify(vmcs12->vm_exit_controls, |
0115f9cb | 10569 | vmx->nested.nested_vmx_exit_ctls_low, |
b9c237bb | 10570 | vmx->nested.nested_vmx_exit_ctls_high) || |
7c177938 | 10571 | !vmx_control_verify(vmcs12->vm_entry_controls, |
0115f9cb | 10572 | vmx->nested.nested_vmx_entry_ctls_low, |
b9c237bb | 10573 | vmx->nested.nested_vmx_entry_ctls_high)) |
ca0bde28 | 10574 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
7c177938 | 10575 | |
c7c2c709 JM |
10576 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
10577 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10578 | ||
3899152c | 10579 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
1dc35dac | 10580 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
ca0bde28 JM |
10581 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
10582 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; | |
10583 | ||
10584 | return 0; | |
10585 | } | |
10586 | ||
10587 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
10588 | u32 *exit_qual) | |
10589 | { | |
10590 | bool ia32e; | |
10591 | ||
10592 | *exit_qual = ENTRY_FAIL_DEFAULT; | |
7c177938 | 10593 | |
3899152c | 10594 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
ca0bde28 | 10595 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
b428018a | 10596 | return 1; |
ca0bde28 JM |
10597 | |
10598 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) && | |
10599 | vmcs12->vmcs_link_pointer != -1ull) { | |
10600 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; | |
b428018a | 10601 | return 1; |
7c177938 NHE |
10602 | } |
10603 | ||
384bb783 | 10604 | /* |
cb0c8cda | 10605 | * If the load IA32_EFER VM-entry control is 1, the following checks |
384bb783 JK |
10606 | * are performed on the field for the IA32_EFER MSR: |
10607 | * - Bits reserved in the IA32_EFER MSR must be 0. | |
10608 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of | |
10609 | * the IA-32e mode guest VM-exit control. It must also be identical | |
10610 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to | |
10611 | * CR0.PG) is 1. | |
10612 | */ | |
ca0bde28 JM |
10613 | if (to_vmx(vcpu)->nested.nested_run_pending && |
10614 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { | |
384bb783 JK |
10615 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
10616 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || | |
10617 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || | |
10618 | ((vmcs12->guest_cr0 & X86_CR0_PG) && | |
ca0bde28 | 10619 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
b428018a | 10620 | return 1; |
384bb783 JK |
10621 | } |
10622 | ||
10623 | /* | |
10624 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the | |
10625 | * IA32_EFER MSR must be 0 in the field for that register. In addition, | |
10626 | * the values of the LMA and LME bits in the field must each be that of | |
10627 | * the host address-space size VM-exit control. | |
10628 | */ | |
10629 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { | |
10630 | ia32e = (vmcs12->vm_exit_controls & | |
10631 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; | |
10632 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || | |
10633 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || | |
ca0bde28 | 10634 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
b428018a | 10635 | return 1; |
ca0bde28 JM |
10636 | } |
10637 | ||
10638 | return 0; | |
10639 | } | |
10640 | ||
858e25c0 JM |
10641 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry) |
10642 | { | |
10643 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10644 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
10645 | struct loaded_vmcs *vmcs02; | |
858e25c0 JM |
10646 | u32 msr_entry_idx; |
10647 | u32 exit_qual; | |
10648 | ||
10649 | vmcs02 = nested_get_current_vmcs02(vmx); | |
10650 | if (!vmcs02) | |
10651 | return -ENOMEM; | |
10652 | ||
10653 | enter_guest_mode(vcpu); | |
10654 | ||
10655 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) | |
10656 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
10657 | ||
1279a6b1 | 10658 | vmx_switch_vmcs(vcpu, vmcs02); |
858e25c0 JM |
10659 | vmx_segment_cache_clear(vmx); |
10660 | ||
10661 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) { | |
10662 | leave_guest_mode(vcpu); | |
1279a6b1 | 10663 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
858e25c0 JM |
10664 | nested_vmx_entry_failure(vcpu, vmcs12, |
10665 | EXIT_REASON_INVALID_STATE, exit_qual); | |
10666 | return 1; | |
10667 | } | |
10668 | ||
10669 | nested_get_vmcs12_pages(vcpu, vmcs12); | |
10670 | ||
10671 | msr_entry_idx = nested_vmx_load_msr(vcpu, | |
10672 | vmcs12->vm_entry_msr_load_addr, | |
10673 | vmcs12->vm_entry_msr_load_count); | |
10674 | if (msr_entry_idx) { | |
10675 | leave_guest_mode(vcpu); | |
1279a6b1 | 10676 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
858e25c0 JM |
10677 | nested_vmx_entry_failure(vcpu, vmcs12, |
10678 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); | |
10679 | return 1; | |
10680 | } | |
10681 | ||
858e25c0 JM |
10682 | /* |
10683 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point | |
10684 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet | |
10685 | * returned as far as L1 is concerned. It will only return (and set | |
10686 | * the success flag) when L2 exits (see nested_vmx_vmexit()). | |
10687 | */ | |
10688 | return 0; | |
10689 | } | |
10690 | ||
ca0bde28 JM |
10691 | /* |
10692 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 | |
10693 | * for running an L2 nested guest. | |
10694 | */ | |
10695 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) | |
10696 | { | |
10697 | struct vmcs12 *vmcs12; | |
10698 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
b3f1dfb6 | 10699 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
ca0bde28 JM |
10700 | u32 exit_qual; |
10701 | int ret; | |
10702 | ||
10703 | if (!nested_vmx_check_permission(vcpu)) | |
10704 | return 1; | |
10705 | ||
10706 | if (!nested_vmx_check_vmcs12(vcpu)) | |
10707 | goto out; | |
10708 | ||
10709 | vmcs12 = get_vmcs12(vcpu); | |
10710 | ||
10711 | if (enable_shadow_vmcs) | |
10712 | copy_shadow_to_vmcs12(vmx); | |
10713 | ||
10714 | /* | |
10715 | * The nested entry process starts with enforcing various prerequisites | |
10716 | * on vmcs12 as required by the Intel SDM, and act appropriately when | |
10717 | * they fail: As the SDM explains, some conditions should cause the | |
10718 | * instruction to fail, while others will cause the instruction to seem | |
10719 | * to succeed, but return an EXIT_REASON_INVALID_STATE. | |
10720 | * To speed up the normal (success) code path, we should avoid checking | |
10721 | * for misconfigurations which will anyway be caught by the processor | |
10722 | * when using the merged vmcs02. | |
10723 | */ | |
b3f1dfb6 JM |
10724 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
10725 | nested_vmx_failValid(vcpu, | |
10726 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); | |
10727 | goto out; | |
10728 | } | |
10729 | ||
ca0bde28 JM |
10730 | if (vmcs12->launch_state == launch) { |
10731 | nested_vmx_failValid(vcpu, | |
10732 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS | |
10733 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); | |
10734 | goto out; | |
10735 | } | |
10736 | ||
10737 | ret = check_vmentry_prereqs(vcpu, vmcs12); | |
10738 | if (ret) { | |
10739 | nested_vmx_failValid(vcpu, ret); | |
10740 | goto out; | |
10741 | } | |
10742 | ||
10743 | /* | |
10744 | * After this point, the trap flag no longer triggers a singlestep trap | |
10745 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. | |
10746 | * This is not 100% correct; for performance reasons, we delegate most | |
10747 | * of the checks on host state to the processor. If those fail, | |
10748 | * the singlestep trap is missed. | |
10749 | */ | |
10750 | skip_emulated_instruction(vcpu); | |
10751 | ||
10752 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); | |
10753 | if (ret) { | |
10754 | nested_vmx_entry_failure(vcpu, vmcs12, | |
10755 | EXIT_REASON_INVALID_STATE, exit_qual); | |
10756 | return 1; | |
384bb783 JK |
10757 | } |
10758 | ||
7c177938 NHE |
10759 | /* |
10760 | * We're finally done with prerequisite checking, and can start with | |
10761 | * the nested entry. | |
10762 | */ | |
10763 | ||
858e25c0 JM |
10764 | ret = enter_vmx_non_root_mode(vcpu, true); |
10765 | if (ret) | |
10766 | return ret; | |
ff651cb6 | 10767 | |
6dfacadd | 10768 | if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) |
5cb56059 | 10769 | return kvm_vcpu_halt(vcpu); |
6dfacadd | 10770 | |
7af40ad3 JK |
10771 | vmx->nested.nested_run_pending = 1; |
10772 | ||
cd232ad0 | 10773 | return 1; |
eb277562 KH |
10774 | |
10775 | out: | |
6affcbed | 10776 | return kvm_skip_emulated_instruction(vcpu); |
cd232ad0 NHE |
10777 | } |
10778 | ||
4704d0be NHE |
10779 | /* |
10780 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date | |
10781 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). | |
10782 | * This function returns the new value we should put in vmcs12.guest_cr0. | |
10783 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, | |
10784 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now | |
10785 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 | |
10786 | * didn't trap the bit, because if L1 did, so would L0). | |
10787 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have | |
10788 | * been modified by L2, and L1 knows it. So just leave the old value of | |
10789 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 | |
10790 | * isn't relevant, because if L0 traps this bit it can set it to anything. | |
10791 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have | |
10792 | * changed these bits, and therefore they need to be updated, but L0 | |
10793 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather | |
10794 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. | |
10795 | */ | |
10796 | static inline unsigned long | |
10797 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
10798 | { | |
10799 | return | |
10800 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | | |
10801 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | | |
10802 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | | |
10803 | vcpu->arch.cr0_guest_owned_bits)); | |
10804 | } | |
10805 | ||
10806 | static inline unsigned long | |
10807 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
10808 | { | |
10809 | return | |
10810 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | | |
10811 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | | |
10812 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | | |
10813 | vcpu->arch.cr4_guest_owned_bits)); | |
10814 | } | |
10815 | ||
5f3d5799 JK |
10816 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
10817 | struct vmcs12 *vmcs12) | |
10818 | { | |
10819 | u32 idt_vectoring; | |
10820 | unsigned int nr; | |
10821 | ||
851eb667 | 10822 | if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) { |
5f3d5799 JK |
10823 | nr = vcpu->arch.exception.nr; |
10824 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
10825 | ||
10826 | if (kvm_exception_is_soft(nr)) { | |
10827 | vmcs12->vm_exit_instruction_len = | |
10828 | vcpu->arch.event_exit_inst_len; | |
10829 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; | |
10830 | } else | |
10831 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; | |
10832 | ||
10833 | if (vcpu->arch.exception.has_error_code) { | |
10834 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; | |
10835 | vmcs12->idt_vectoring_error_code = | |
10836 | vcpu->arch.exception.error_code; | |
10837 | } | |
10838 | ||
10839 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
cd2633c5 | 10840 | } else if (vcpu->arch.nmi_injected) { |
5f3d5799 JK |
10841 | vmcs12->idt_vectoring_info_field = |
10842 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; | |
10843 | } else if (vcpu->arch.interrupt.pending) { | |
10844 | nr = vcpu->arch.interrupt.nr; | |
10845 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
10846 | ||
10847 | if (vcpu->arch.interrupt.soft) { | |
10848 | idt_vectoring |= INTR_TYPE_SOFT_INTR; | |
10849 | vmcs12->vm_entry_instruction_len = | |
10850 | vcpu->arch.event_exit_inst_len; | |
10851 | } else | |
10852 | idt_vectoring |= INTR_TYPE_EXT_INTR; | |
10853 | ||
10854 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
10855 | } | |
10856 | } | |
10857 | ||
b6b8a145 JK |
10858 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
10859 | { | |
10860 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10861 | ||
acc9ab60 WL |
10862 | if (vcpu->arch.exception.pending || |
10863 | vcpu->arch.nmi_injected || | |
10864 | vcpu->arch.interrupt.pending) | |
10865 | return -EBUSY; | |
10866 | ||
f4124500 JK |
10867 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
10868 | vmx->nested.preemption_timer_expired) { | |
10869 | if (vmx->nested.nested_run_pending) | |
10870 | return -EBUSY; | |
10871 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); | |
10872 | return 0; | |
10873 | } | |
10874 | ||
b6b8a145 | 10875 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
acc9ab60 | 10876 | if (vmx->nested.nested_run_pending) |
b6b8a145 JK |
10877 | return -EBUSY; |
10878 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, | |
10879 | NMI_VECTOR | INTR_TYPE_NMI_INTR | | |
10880 | INTR_INFO_VALID_MASK, 0); | |
10881 | /* | |
10882 | * The NMI-triggered VM exit counts as injection: | |
10883 | * clear this one and block further NMIs. | |
10884 | */ | |
10885 | vcpu->arch.nmi_pending = 0; | |
10886 | vmx_set_nmi_mask(vcpu, true); | |
10887 | return 0; | |
10888 | } | |
10889 | ||
10890 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && | |
10891 | nested_exit_on_intr(vcpu)) { | |
10892 | if (vmx->nested.nested_run_pending) | |
10893 | return -EBUSY; | |
10894 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); | |
705699a1 | 10895 | return 0; |
b6b8a145 JK |
10896 | } |
10897 | ||
6342c50a DH |
10898 | vmx_complete_nested_posted_interrupt(vcpu); |
10899 | return 0; | |
b6b8a145 JK |
10900 | } |
10901 | ||
f4124500 JK |
10902 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
10903 | { | |
10904 | ktime_t remaining = | |
10905 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); | |
10906 | u64 value; | |
10907 | ||
10908 | if (ktime_to_ns(remaining) <= 0) | |
10909 | return 0; | |
10910 | ||
10911 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; | |
10912 | do_div(value, 1000000); | |
10913 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
10914 | } | |
10915 | ||
4704d0be | 10916 | /* |
cf8b84f4 JM |
10917 | * Update the guest state fields of vmcs12 to reflect changes that |
10918 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the | |
10919 | * VM-entry controls is also updated, since this is really a guest | |
10920 | * state bit.) | |
4704d0be | 10921 | */ |
cf8b84f4 | 10922 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
4704d0be | 10923 | { |
4704d0be NHE |
10924 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
10925 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); | |
10926 | ||
4704d0be NHE |
10927 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
10928 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); | |
10929 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); | |
10930 | ||
10931 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); | |
10932 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); | |
10933 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); | |
10934 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); | |
10935 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); | |
10936 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); | |
10937 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); | |
10938 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); | |
10939 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); | |
10940 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); | |
10941 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); | |
10942 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); | |
10943 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); | |
10944 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); | |
10945 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); | |
10946 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); | |
10947 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); | |
10948 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); | |
10949 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); | |
10950 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); | |
10951 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); | |
10952 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); | |
10953 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); | |
10954 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); | |
10955 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); | |
10956 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); | |
10957 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); | |
10958 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); | |
10959 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); | |
10960 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); | |
10961 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); | |
10962 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); | |
10963 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); | |
10964 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); | |
10965 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); | |
10966 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); | |
10967 | ||
4704d0be NHE |
10968 | vmcs12->guest_interruptibility_info = |
10969 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
10970 | vmcs12->guest_pending_dbg_exceptions = | |
10971 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); | |
3edf1e69 JK |
10972 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
10973 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; | |
10974 | else | |
10975 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; | |
4704d0be | 10976 | |
f4124500 JK |
10977 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
10978 | if (vmcs12->vm_exit_controls & | |
10979 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) | |
10980 | vmcs12->vmx_preemption_timer_value = | |
10981 | vmx_get_preemption_timer_value(vcpu); | |
10982 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); | |
10983 | } | |
7854cbca | 10984 | |
3633cfc3 NHE |
10985 | /* |
10986 | * In some cases (usually, nested EPT), L2 is allowed to change its | |
10987 | * own CR3 without exiting. If it has changed it, we must keep it. | |
10988 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined | |
10989 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. | |
10990 | * | |
10991 | * Additionally, restore L2's PDPTR to vmcs12. | |
10992 | */ | |
10993 | if (enable_ept) { | |
f3531054 | 10994 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
3633cfc3 NHE |
10995 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
10996 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); | |
10997 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); | |
10998 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); | |
10999 | } | |
11000 | ||
d281e13b | 11001 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
119a9c01 | 11002 | |
608406e2 WV |
11003 | if (nested_cpu_has_vid(vmcs12)) |
11004 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); | |
11005 | ||
c18911a2 JK |
11006 | vmcs12->vm_entry_controls = |
11007 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | | |
2961e876 | 11008 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
c18911a2 | 11009 | |
2996fca0 JK |
11010 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
11011 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); | |
11012 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
11013 | } | |
11014 | ||
4704d0be NHE |
11015 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
11016 | * the relevant bit asks not to trap the change */ | |
b8c07d55 | 11017 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
4704d0be | 11018 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
10ba54a5 JK |
11019 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
11020 | vmcs12->guest_ia32_efer = vcpu->arch.efer; | |
4704d0be NHE |
11021 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
11022 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); | |
11023 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); | |
a87036ad | 11024 | if (kvm_mpx_supported()) |
36be0b9d | 11025 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
cf8b84f4 JM |
11026 | } |
11027 | ||
11028 | /* | |
11029 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits | |
11030 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), | |
11031 | * and this function updates it to reflect the changes to the guest state while | |
11032 | * L2 was running (and perhaps made some exits which were handled directly by L0 | |
11033 | * without going back to L1), and to reflect the exit reason. | |
11034 | * Note that we do not have to copy here all VMCS fields, just those that | |
11035 | * could have changed by the L2 guest or the exit - i.e., the guest-state and | |
11036 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, | |
11037 | * which already writes to vmcs12 directly. | |
11038 | */ | |
11039 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
11040 | u32 exit_reason, u32 exit_intr_info, | |
11041 | unsigned long exit_qualification) | |
11042 | { | |
11043 | /* update guest state fields: */ | |
11044 | sync_vmcs12(vcpu, vmcs12); | |
4704d0be NHE |
11045 | |
11046 | /* update exit information fields: */ | |
11047 | ||
533558bc JK |
11048 | vmcs12->vm_exit_reason = exit_reason; |
11049 | vmcs12->exit_qualification = exit_qualification; | |
533558bc | 11050 | vmcs12->vm_exit_intr_info = exit_intr_info; |
7313c698 | 11051 | |
5f3d5799 | 11052 | vmcs12->idt_vectoring_info_field = 0; |
4704d0be NHE |
11053 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
11054 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
11055 | ||
5f3d5799 | 11056 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
7cdc2d62 JM |
11057 | vmcs12->launch_state = 1; |
11058 | ||
5f3d5799 JK |
11059 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
11060 | * instead of reading the real value. */ | |
4704d0be | 11061 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
5f3d5799 JK |
11062 | |
11063 | /* | |
11064 | * Transfer the event that L0 or L1 may wanted to inject into | |
11065 | * L2 to IDT_VECTORING_INFO_FIELD. | |
11066 | */ | |
11067 | vmcs12_save_pending_event(vcpu, vmcs12); | |
11068 | } | |
11069 | ||
11070 | /* | |
11071 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is | |
11072 | * preserved above and would only end up incorrectly in L1. | |
11073 | */ | |
11074 | vcpu->arch.nmi_injected = false; | |
11075 | kvm_clear_exception_queue(vcpu); | |
11076 | kvm_clear_interrupt_queue(vcpu); | |
4704d0be NHE |
11077 | } |
11078 | ||
11079 | /* | |
11080 | * A part of what we need to when the nested L2 guest exits and we want to | |
11081 | * run its L1 parent, is to reset L1's guest state to the host state specified | |
11082 | * in vmcs12. | |
11083 | * This function is to be called not only on normal nested exit, but also on | |
11084 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry | |
11085 | * Failures During or After Loading Guest State"). | |
11086 | * This function should be called when the active VMCS is L1's (vmcs01). | |
11087 | */ | |
733568f9 JK |
11088 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
11089 | struct vmcs12 *vmcs12) | |
4704d0be | 11090 | { |
21feb4eb | 11091 | struct kvm_segment seg; |
ca0bde28 | 11092 | u32 entry_failure_code; |
21feb4eb | 11093 | |
4704d0be NHE |
11094 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
11095 | vcpu->arch.efer = vmcs12->host_ia32_efer; | |
d1fa0352 | 11096 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
4704d0be NHE |
11097 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
11098 | else | |
11099 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
11100 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
11101 | ||
11102 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); | |
11103 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); | |
1adfa76a | 11104 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
4704d0be NHE |
11105 | /* |
11106 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't | |
bd7e5b08 PB |
11107 | * actually changed, because vmx_set_cr0 refers to efer set above. |
11108 | * | |
11109 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 | |
11110 | * (KVM doesn't change it); | |
4704d0be | 11111 | */ |
bd7e5b08 | 11112 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
9e3e4dbf | 11113 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
4704d0be | 11114 | |
bd7e5b08 | 11115 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
4704d0be | 11116 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
24c90373 | 11117 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
4704d0be | 11118 | |
29bf08f1 | 11119 | nested_ept_uninit_mmu_context(vcpu); |
155a97a3 | 11120 | |
1dc35dac LP |
11121 | /* |
11122 | * Only PDPTE load can fail as the value of cr3 was checked on entry and | |
11123 | * couldn't have changed. | |
11124 | */ | |
11125 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) | |
11126 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); | |
4704d0be | 11127 | |
feaf0c7d GN |
11128 | if (!enable_ept) |
11129 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; | |
11130 | ||
4704d0be NHE |
11131 | if (enable_vpid) { |
11132 | /* | |
11133 | * Trivially support vpid by letting L2s share their parent | |
11134 | * L1's vpid. TODO: move to a more elaborate solution, giving | |
11135 | * each L2 its own vpid and exposing the vpid feature to L1. | |
11136 | */ | |
11137 | vmx_flush_tlb(vcpu); | |
11138 | } | |
06a5524f WV |
11139 | /* Restore posted intr vector. */ |
11140 | if (nested_cpu_has_posted_intr(vmcs12)) | |
11141 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); | |
4704d0be NHE |
11142 | |
11143 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); | |
11144 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); | |
11145 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); | |
11146 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); | |
11147 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); | |
4704d0be | 11148 | |
36be0b9d PB |
11149 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
11150 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) | |
11151 | vmcs_write64(GUEST_BNDCFGS, 0); | |
11152 | ||
44811c02 | 11153 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
4704d0be | 11154 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
44811c02 JK |
11155 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
11156 | } | |
4704d0be NHE |
11157 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
11158 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, | |
11159 | vmcs12->host_ia32_perf_global_ctrl); | |
503cd0c5 | 11160 | |
21feb4eb ACL |
11161 | /* Set L1 segment info according to Intel SDM |
11162 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ | |
11163 | seg = (struct kvm_segment) { | |
11164 | .base = 0, | |
11165 | .limit = 0xFFFFFFFF, | |
11166 | .selector = vmcs12->host_cs_selector, | |
11167 | .type = 11, | |
11168 | .present = 1, | |
11169 | .s = 1, | |
11170 | .g = 1 | |
11171 | }; | |
11172 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) | |
11173 | seg.l = 1; | |
11174 | else | |
11175 | seg.db = 1; | |
11176 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); | |
11177 | seg = (struct kvm_segment) { | |
11178 | .base = 0, | |
11179 | .limit = 0xFFFFFFFF, | |
11180 | .type = 3, | |
11181 | .present = 1, | |
11182 | .s = 1, | |
11183 | .db = 1, | |
11184 | .g = 1 | |
11185 | }; | |
11186 | seg.selector = vmcs12->host_ds_selector; | |
11187 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); | |
11188 | seg.selector = vmcs12->host_es_selector; | |
11189 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); | |
11190 | seg.selector = vmcs12->host_ss_selector; | |
11191 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); | |
11192 | seg.selector = vmcs12->host_fs_selector; | |
11193 | seg.base = vmcs12->host_fs_base; | |
11194 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); | |
11195 | seg.selector = vmcs12->host_gs_selector; | |
11196 | seg.base = vmcs12->host_gs_base; | |
11197 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); | |
11198 | seg = (struct kvm_segment) { | |
205befd9 | 11199 | .base = vmcs12->host_tr_base, |
21feb4eb ACL |
11200 | .limit = 0x67, |
11201 | .selector = vmcs12->host_tr_selector, | |
11202 | .type = 11, | |
11203 | .present = 1 | |
11204 | }; | |
11205 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); | |
11206 | ||
503cd0c5 JK |
11207 | kvm_set_dr(vcpu, 7, 0x400); |
11208 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
ff651cb6 | 11209 | |
3af18d9c WV |
11210 | if (cpu_has_vmx_msr_bitmap()) |
11211 | vmx_set_msr_bitmap(vcpu); | |
11212 | ||
ff651cb6 WV |
11213 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
11214 | vmcs12->vm_exit_msr_load_count)) | |
11215 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); | |
4704d0be NHE |
11216 | } |
11217 | ||
11218 | /* | |
11219 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 | |
11220 | * and modify vmcs12 to make it see what it would expect to see there if | |
11221 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) | |
11222 | */ | |
533558bc JK |
11223 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
11224 | u32 exit_intr_info, | |
11225 | unsigned long exit_qualification) | |
4704d0be NHE |
11226 | { |
11227 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
4704d0be | 11228 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
cf3215d9 | 11229 | u32 vm_inst_error = 0; |
4704d0be | 11230 | |
5f3d5799 JK |
11231 | /* trying to cancel vmlaunch/vmresume is a bug */ |
11232 | WARN_ON_ONCE(vmx->nested.nested_run_pending); | |
11233 | ||
4704d0be | 11234 | leave_guest_mode(vcpu); |
533558bc JK |
11235 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
11236 | exit_qualification); | |
4704d0be | 11237 | |
ff651cb6 WV |
11238 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
11239 | vmcs12->vm_exit_msr_store_count)) | |
11240 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); | |
11241 | ||
cf3215d9 JM |
11242 | if (unlikely(vmx->fail)) |
11243 | vm_inst_error = vmcs_read32(VM_INSTRUCTION_ERROR); | |
11244 | ||
1279a6b1 | 11245 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
f3380ca5 | 11246 | |
6550c4df WL |
11247 | /* |
11248 | * TODO: SDM says that with acknowledge interrupt on exit, bit 31 of | |
11249 | * the VM-exit interrupt information (valid interrupt) is always set to | |
11250 | * 1 on EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't need | |
11251 | * kvm_cpu_has_interrupt(). See the commit message for details. | |
11252 | */ | |
11253 | if (nested_exit_intr_ack_set(vcpu) && | |
11254 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && | |
11255 | kvm_cpu_has_interrupt(vcpu)) { | |
77b0f5d6 BD |
11256 | int irq = kvm_cpu_get_interrupt(vcpu); |
11257 | WARN_ON(irq < 0); | |
11258 | vmcs12->vm_exit_intr_info = irq | | |
11259 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; | |
11260 | } | |
11261 | ||
542060ea JK |
11262 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
11263 | vmcs12->exit_qualification, | |
11264 | vmcs12->idt_vectoring_info_field, | |
11265 | vmcs12->vm_exit_intr_info, | |
11266 | vmcs12->vm_exit_intr_error_code, | |
11267 | KVM_ISA_VMX); | |
4704d0be | 11268 | |
8391ce44 PB |
11269 | vm_entry_controls_reset_shadow(vmx); |
11270 | vm_exit_controls_reset_shadow(vmx); | |
36c3cc42 JK |
11271 | vmx_segment_cache_clear(vmx); |
11272 | ||
4704d0be NHE |
11273 | /* if no vmcs02 cache requested, remove the one we used */ |
11274 | if (VMCS02_POOL_SIZE == 0) | |
11275 | nested_free_vmcs02(vmx, vmx->nested.current_vmptr); | |
11276 | ||
11277 | load_vmcs12_host_state(vcpu, vmcs12); | |
11278 | ||
9314006d | 11279 | /* Update any VMCS fields that might have changed while L2 ran */ |
83bafef1 JM |
11280 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
11281 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
ea26e4ec | 11282 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
9314006d PB |
11283 | if (vmx->hv_deadline_tsc == -1) |
11284 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11285 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
11286 | else | |
11287 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11288 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c95ba92a PF |
11289 | if (kvm_has_tsc_control) |
11290 | decache_tsc_multiplier(vmx); | |
4704d0be | 11291 | |
dccbfcf5 RK |
11292 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
11293 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; | |
11294 | vmx_set_virtual_x2apic_mode(vcpu, | |
11295 | vcpu->arch.apic_base & X2APIC_ENABLE); | |
fb6c8198 JM |
11296 | } else if (!nested_cpu_has_ept(vmcs12) && |
11297 | nested_cpu_has2(vmcs12, | |
11298 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
11299 | vmx_flush_tlb_ept_only(vcpu); | |
dccbfcf5 | 11300 | } |
4704d0be NHE |
11301 | |
11302 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ | |
11303 | vmx->host_rsp = 0; | |
11304 | ||
11305 | /* Unpin physical memory we referred to in vmcs02 */ | |
11306 | if (vmx->nested.apic_access_page) { | |
11307 | nested_release_page(vmx->nested.apic_access_page); | |
48d89b92 | 11308 | vmx->nested.apic_access_page = NULL; |
4704d0be | 11309 | } |
a7c0b07d WL |
11310 | if (vmx->nested.virtual_apic_page) { |
11311 | nested_release_page(vmx->nested.virtual_apic_page); | |
48d89b92 | 11312 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 11313 | } |
705699a1 WV |
11314 | if (vmx->nested.pi_desc_page) { |
11315 | kunmap(vmx->nested.pi_desc_page); | |
11316 | nested_release_page(vmx->nested.pi_desc_page); | |
11317 | vmx->nested.pi_desc_page = NULL; | |
11318 | vmx->nested.pi_desc = NULL; | |
11319 | } | |
4704d0be | 11320 | |
38b99173 TC |
11321 | /* |
11322 | * We are now running in L2, mmu_notifier will force to reload the | |
11323 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. | |
11324 | */ | |
c83b6d15 | 11325 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
38b99173 | 11326 | |
4704d0be NHE |
11327 | /* |
11328 | * Exiting from L2 to L1, we're now back to L1 which thinks it just | |
11329 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the | |
11330 | * success or failure flag accordingly. | |
11331 | */ | |
11332 | if (unlikely(vmx->fail)) { | |
11333 | vmx->fail = 0; | |
cf3215d9 | 11334 | nested_vmx_failValid(vcpu, vm_inst_error); |
4704d0be NHE |
11335 | } else |
11336 | nested_vmx_succeed(vcpu); | |
012f83cb AG |
11337 | if (enable_shadow_vmcs) |
11338 | vmx->nested.sync_shadow_vmcs = true; | |
b6b8a145 JK |
11339 | |
11340 | /* in case we halted in L2 */ | |
11341 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
4704d0be NHE |
11342 | } |
11343 | ||
42124925 JK |
11344 | /* |
11345 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. | |
11346 | */ | |
11347 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) | |
11348 | { | |
2f707d97 WL |
11349 | if (is_guest_mode(vcpu)) { |
11350 | to_vmx(vcpu)->nested.nested_run_pending = 0; | |
533558bc | 11351 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
2f707d97 | 11352 | } |
42124925 JK |
11353 | free_nested(to_vmx(vcpu)); |
11354 | } | |
11355 | ||
7c177938 NHE |
11356 | /* |
11357 | * L1's failure to enter L2 is a subset of a normal exit, as explained in | |
11358 | * 23.7 "VM-entry failures during or after loading guest state" (this also | |
11359 | * lists the acceptable exit-reason and exit-qualification parameters). | |
11360 | * It should only be called before L2 actually succeeded to run, and when | |
11361 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). | |
11362 | */ | |
11363 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, | |
11364 | struct vmcs12 *vmcs12, | |
11365 | u32 reason, unsigned long qualification) | |
11366 | { | |
11367 | load_vmcs12_host_state(vcpu, vmcs12); | |
11368 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; | |
11369 | vmcs12->exit_qualification = qualification; | |
11370 | nested_vmx_succeed(vcpu); | |
012f83cb AG |
11371 | if (enable_shadow_vmcs) |
11372 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; | |
7c177938 NHE |
11373 | } |
11374 | ||
8a76d7f2 JR |
11375 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
11376 | struct x86_instruction_info *info, | |
11377 | enum x86_intercept_stage stage) | |
11378 | { | |
11379 | return X86EMUL_CONTINUE; | |
11380 | } | |
11381 | ||
64672c95 YJ |
11382 | #ifdef CONFIG_X86_64 |
11383 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ | |
11384 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, | |
11385 | u64 divisor, u64 *result) | |
11386 | { | |
11387 | u64 low = a << shift, high = a >> (64 - shift); | |
11388 | ||
11389 | /* To avoid the overflow on divq */ | |
11390 | if (high >= divisor) | |
11391 | return 1; | |
11392 | ||
11393 | /* Low hold the result, high hold rem which is discarded */ | |
11394 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : | |
11395 | "rm" (divisor), "0" (low), "1" (high)); | |
11396 | *result = low; | |
11397 | ||
11398 | return 0; | |
11399 | } | |
11400 | ||
11401 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) | |
11402 | { | |
11403 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9175d2e9 PB |
11404 | u64 tscl = rdtsc(); |
11405 | u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); | |
11406 | u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; | |
64672c95 YJ |
11407 | |
11408 | /* Convert to host delta tsc if tsc scaling is enabled */ | |
11409 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && | |
11410 | u64_shl_div_u64(delta_tsc, | |
11411 | kvm_tsc_scaling_ratio_frac_bits, | |
11412 | vcpu->arch.tsc_scaling_ratio, | |
11413 | &delta_tsc)) | |
11414 | return -ERANGE; | |
11415 | ||
11416 | /* | |
11417 | * If the delta tsc can't fit in the 32 bit after the multi shift, | |
11418 | * we can't use the preemption timer. | |
11419 | * It's possible that it fits on later vmentries, but checking | |
11420 | * on every vmentry is costly so we just use an hrtimer. | |
11421 | */ | |
11422 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) | |
11423 | return -ERANGE; | |
11424 | ||
11425 | vmx->hv_deadline_tsc = tscl + delta_tsc; | |
11426 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11427 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c8533544 WL |
11428 | |
11429 | return delta_tsc == 0; | |
64672c95 YJ |
11430 | } |
11431 | ||
11432 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) | |
11433 | { | |
11434 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
11435 | vmx->hv_deadline_tsc = -1; | |
11436 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11437 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
11438 | } | |
11439 | #endif | |
11440 | ||
48d89b92 | 11441 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
ae97a3b8 | 11442 | { |
b4a2d31d RK |
11443 | if (ple_gap) |
11444 | shrink_ple_window(vcpu); | |
ae97a3b8 RK |
11445 | } |
11446 | ||
843e4330 KH |
11447 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
11448 | struct kvm_memory_slot *slot) | |
11449 | { | |
11450 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); | |
11451 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); | |
11452 | } | |
11453 | ||
11454 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, | |
11455 | struct kvm_memory_slot *slot) | |
11456 | { | |
11457 | kvm_mmu_slot_set_dirty(kvm, slot); | |
11458 | } | |
11459 | ||
11460 | static void vmx_flush_log_dirty(struct kvm *kvm) | |
11461 | { | |
11462 | kvm_flush_pml_buffers(kvm); | |
11463 | } | |
11464 | ||
c5f983f6 BD |
11465 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
11466 | { | |
11467 | struct vmcs12 *vmcs12; | |
11468 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
11469 | gpa_t gpa; | |
11470 | struct page *page = NULL; | |
11471 | u64 *pml_address; | |
11472 | ||
11473 | if (is_guest_mode(vcpu)) { | |
11474 | WARN_ON_ONCE(vmx->nested.pml_full); | |
11475 | ||
11476 | /* | |
11477 | * Check if PML is enabled for the nested guest. | |
11478 | * Whether eptp bit 6 is set is already checked | |
11479 | * as part of A/D emulation. | |
11480 | */ | |
11481 | vmcs12 = get_vmcs12(vcpu); | |
11482 | if (!nested_cpu_has_pml(vmcs12)) | |
11483 | return 0; | |
11484 | ||
4769886b | 11485 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
c5f983f6 BD |
11486 | vmx->nested.pml_full = true; |
11487 | return 1; | |
11488 | } | |
11489 | ||
11490 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; | |
11491 | ||
11492 | page = nested_get_page(vcpu, vmcs12->pml_address); | |
11493 | if (!page) | |
11494 | return 0; | |
11495 | ||
11496 | pml_address = kmap(page); | |
11497 | pml_address[vmcs12->guest_pml_index--] = gpa; | |
11498 | kunmap(page); | |
11499 | nested_release_page_clean(page); | |
11500 | } | |
11501 | ||
11502 | return 0; | |
11503 | } | |
11504 | ||
843e4330 KH |
11505 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
11506 | struct kvm_memory_slot *memslot, | |
11507 | gfn_t offset, unsigned long mask) | |
11508 | { | |
11509 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); | |
11510 | } | |
11511 | ||
da68e260 PB |
11512 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
11513 | { | |
11514 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
11515 | struct pi_desc old, new; | |
11516 | unsigned int dest; | |
da68e260 PB |
11517 | |
11518 | do { | |
11519 | old.control = new.control = pi_desc->control; | |
3f74e7f3 PB |
11520 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
11521 | "Wakeup handler not enabled while the VCPU is blocked\n"); | |
da68e260 PB |
11522 | |
11523 | dest = cpu_physical_id(vcpu->cpu); | |
11524 | ||
11525 | if (x2apic_enabled()) | |
11526 | new.ndst = dest; | |
11527 | else | |
11528 | new.ndst = (dest << 8) & 0xFF00; | |
11529 | ||
da68e260 PB |
11530 | /* set 'NV' to 'notification vector' */ |
11531 | new.nv = POSTED_INTR_VECTOR; | |
84635f8b PB |
11532 | } while (cmpxchg64(&pi_desc->control, old.control, |
11533 | new.control) != old.control); | |
da68e260 | 11534 | |
3f74e7f3 PB |
11535 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
11536 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
da68e260 | 11537 | list_del(&vcpu->blocked_vcpu_list); |
3f74e7f3 | 11538 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
da68e260 PB |
11539 | vcpu->pre_pcpu = -1; |
11540 | } | |
11541 | } | |
11542 | ||
bf9f6ac8 FW |
11543 | /* |
11544 | * This routine does the following things for vCPU which is going | |
11545 | * to be blocked if VT-d PI is enabled. | |
11546 | * - Store the vCPU to the wakeup list, so when interrupts happen | |
11547 | * we can find the right vCPU to wake up. | |
11548 | * - Change the Posted-interrupt descriptor as below: | |
11549 | * 'NDST' <-- vcpu->pre_pcpu | |
11550 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR | |
11551 | * - If 'ON' is set during this process, which means at least one | |
11552 | * interrupt is posted for this vCPU, we cannot block it, in | |
11553 | * this case, return 1, otherwise, return 0. | |
11554 | * | |
11555 | */ | |
bc22512b | 11556 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
bf9f6ac8 | 11557 | { |
bf9f6ac8 FW |
11558 | unsigned int dest; |
11559 | struct pi_desc old, new; | |
11560 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
11561 | ||
11562 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
11563 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
11564 | !kvm_vcpu_apicv_active(vcpu)) | |
bf9f6ac8 FW |
11565 | return 0; |
11566 | ||
3f74e7f3 PB |
11567 | WARN_ON(irqs_disabled()); |
11568 | local_irq_disable(); | |
11569 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { | |
11570 | vcpu->pre_pcpu = vcpu->cpu; | |
11571 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
11572 | list_add_tail(&vcpu->blocked_vcpu_list, | |
11573 | &per_cpu(blocked_vcpu_on_cpu, | |
11574 | vcpu->pre_pcpu)); | |
11575 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
11576 | } | |
bf9f6ac8 FW |
11577 | |
11578 | do { | |
11579 | old.control = new.control = pi_desc->control; | |
11580 | ||
bf9f6ac8 FW |
11581 | WARN((pi_desc->sn == 1), |
11582 | "Warning: SN field of posted-interrupts " | |
11583 | "is set before blocking\n"); | |
11584 | ||
11585 | /* | |
11586 | * Since vCPU can be preempted during this process, | |
11587 | * vcpu->cpu could be different with pre_pcpu, we | |
11588 | * need to set pre_pcpu as the destination of wakeup | |
11589 | * notification event, then we can find the right vCPU | |
11590 | * to wakeup in wakeup handler if interrupts happen | |
11591 | * when the vCPU is in blocked state. | |
11592 | */ | |
11593 | dest = cpu_physical_id(vcpu->pre_pcpu); | |
11594 | ||
11595 | if (x2apic_enabled()) | |
11596 | new.ndst = dest; | |
11597 | else | |
11598 | new.ndst = (dest << 8) & 0xFF00; | |
11599 | ||
11600 | /* set 'NV' to 'wakeup vector' */ | |
11601 | new.nv = POSTED_INTR_WAKEUP_VECTOR; | |
84635f8b PB |
11602 | } while (cmpxchg64(&pi_desc->control, old.control, |
11603 | new.control) != old.control); | |
bf9f6ac8 | 11604 | |
3f74e7f3 PB |
11605 | /* We should not block the vCPU if an interrupt is posted for it. */ |
11606 | if (pi_test_on(pi_desc) == 1) | |
11607 | __pi_post_block(vcpu); | |
11608 | ||
11609 | local_irq_enable(); | |
11610 | return (vcpu->pre_pcpu == -1); | |
bf9f6ac8 FW |
11611 | } |
11612 | ||
bc22512b YJ |
11613 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
11614 | { | |
11615 | if (pi_pre_block(vcpu)) | |
11616 | return 1; | |
11617 | ||
64672c95 YJ |
11618 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
11619 | kvm_lapic_switch_to_sw_timer(vcpu); | |
11620 | ||
bc22512b YJ |
11621 | return 0; |
11622 | } | |
11623 | ||
11624 | static void pi_post_block(struct kvm_vcpu *vcpu) | |
bf9f6ac8 | 11625 | { |
3f74e7f3 | 11626 | if (vcpu->pre_pcpu == -1) |
bf9f6ac8 FW |
11627 | return; |
11628 | ||
3f74e7f3 PB |
11629 | WARN_ON(irqs_disabled()); |
11630 | local_irq_disable(); | |
da68e260 | 11631 | __pi_post_block(vcpu); |
3f74e7f3 | 11632 | local_irq_enable(); |
bf9f6ac8 FW |
11633 | } |
11634 | ||
bc22512b YJ |
11635 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
11636 | { | |
64672c95 YJ |
11637 | if (kvm_x86_ops->set_hv_timer) |
11638 | kvm_lapic_switch_to_hv_timer(vcpu); | |
11639 | ||
bc22512b YJ |
11640 | pi_post_block(vcpu); |
11641 | } | |
11642 | ||
efc64404 FW |
11643 | /* |
11644 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts | |
11645 | * | |
11646 | * @kvm: kvm | |
11647 | * @host_irq: host irq of the interrupt | |
11648 | * @guest_irq: gsi of the interrupt | |
11649 | * @set: set or unset PI | |
11650 | * returns 0 on success, < 0 on failure | |
11651 | */ | |
11652 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, | |
11653 | uint32_t guest_irq, bool set) | |
11654 | { | |
11655 | struct kvm_kernel_irq_routing_entry *e; | |
11656 | struct kvm_irq_routing_table *irq_rt; | |
11657 | struct kvm_lapic_irq irq; | |
11658 | struct kvm_vcpu *vcpu; | |
11659 | struct vcpu_data vcpu_info; | |
353b0c9e | 11660 | int idx, ret = 0; |
efc64404 FW |
11661 | |
11662 | if (!kvm_arch_has_assigned_device(kvm) || | |
a0052191 YZ |
11663 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
11664 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) | |
efc64404 FW |
11665 | return 0; |
11666 | ||
11667 | idx = srcu_read_lock(&kvm->irq_srcu); | |
11668 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); | |
353b0c9e JS |
11669 | if (guest_irq >= irq_rt->nr_rt_entries || |
11670 | hlist_empty(&irq_rt->map[guest_irq])) { | |
11671 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", | |
11672 | guest_irq, irq_rt->nr_rt_entries); | |
11673 | goto out; | |
11674 | } | |
efc64404 FW |
11675 | |
11676 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { | |
11677 | if (e->type != KVM_IRQ_ROUTING_MSI) | |
11678 | continue; | |
11679 | /* | |
11680 | * VT-d PI cannot support posting multicast/broadcast | |
11681 | * interrupts to a vCPU, we still use interrupt remapping | |
11682 | * for these kind of interrupts. | |
11683 | * | |
11684 | * For lowest-priority interrupts, we only support | |
11685 | * those with single CPU as the destination, e.g. user | |
11686 | * configures the interrupts via /proc/irq or uses | |
11687 | * irqbalance to make the interrupts single-CPU. | |
11688 | * | |
11689 | * We will support full lowest-priority interrupt later. | |
11690 | */ | |
11691 | ||
37131313 | 11692 | kvm_set_msi_irq(kvm, e, &irq); |
23a1c257 FW |
11693 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
11694 | /* | |
11695 | * Make sure the IRTE is in remapped mode if | |
11696 | * we don't handle it in posted mode. | |
11697 | */ | |
11698 | ret = irq_set_vcpu_affinity(host_irq, NULL); | |
11699 | if (ret < 0) { | |
11700 | printk(KERN_INFO | |
11701 | "failed to back to remapped mode, irq: %u\n", | |
11702 | host_irq); | |
11703 | goto out; | |
11704 | } | |
11705 | ||
efc64404 | 11706 | continue; |
23a1c257 | 11707 | } |
efc64404 FW |
11708 | |
11709 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); | |
11710 | vcpu_info.vector = irq.vector; | |
11711 | ||
b6ce9780 | 11712 | trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi, |
efc64404 FW |
11713 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
11714 | ||
11715 | if (set) | |
11716 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); | |
2ebaaf89 | 11717 | else |
efc64404 | 11718 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
efc64404 FW |
11719 | |
11720 | if (ret < 0) { | |
11721 | printk(KERN_INFO "%s: failed to update PI IRTE\n", | |
11722 | __func__); | |
11723 | goto out; | |
11724 | } | |
11725 | } | |
11726 | ||
11727 | ret = 0; | |
11728 | out: | |
11729 | srcu_read_unlock(&kvm->irq_srcu, idx); | |
11730 | return ret; | |
11731 | } | |
11732 | ||
c45dcc71 AR |
11733 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
11734 | { | |
11735 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) | |
11736 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
11737 | FEATURE_CONTROL_LMCE; | |
11738 | else | |
11739 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
11740 | ~FEATURE_CONTROL_LMCE; | |
11741 | } | |
11742 | ||
404f6aac | 11743 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
6aa8b732 AK |
11744 | .cpu_has_kvm_support = cpu_has_kvm_support, |
11745 | .disabled_by_bios = vmx_disabled_by_bios, | |
11746 | .hardware_setup = hardware_setup, | |
11747 | .hardware_unsetup = hardware_unsetup, | |
002c7f7c | 11748 | .check_processor_compatibility = vmx_check_processor_compat, |
6aa8b732 AK |
11749 | .hardware_enable = hardware_enable, |
11750 | .hardware_disable = hardware_disable, | |
04547156 | 11751 | .cpu_has_accelerated_tpr = report_flexpriority, |
6d396b55 | 11752 | .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, |
6aa8b732 AK |
11753 | |
11754 | .vcpu_create = vmx_create_vcpu, | |
11755 | .vcpu_free = vmx_free_vcpu, | |
04d2cc77 | 11756 | .vcpu_reset = vmx_vcpu_reset, |
6aa8b732 | 11757 | |
04d2cc77 | 11758 | .prepare_guest_switch = vmx_save_host_state, |
6aa8b732 AK |
11759 | .vcpu_load = vmx_vcpu_load, |
11760 | .vcpu_put = vmx_vcpu_put, | |
11761 | ||
a96036b8 | 11762 | .update_bp_intercept = update_exception_bitmap, |
6aa8b732 AK |
11763 | .get_msr = vmx_get_msr, |
11764 | .set_msr = vmx_set_msr, | |
11765 | .get_segment_base = vmx_get_segment_base, | |
11766 | .get_segment = vmx_get_segment, | |
11767 | .set_segment = vmx_set_segment, | |
2e4d2653 | 11768 | .get_cpl = vmx_get_cpl, |
6aa8b732 | 11769 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
e8467fda | 11770 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
aff48baa | 11771 | .decache_cr3 = vmx_decache_cr3, |
25c4c276 | 11772 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
6aa8b732 | 11773 | .set_cr0 = vmx_set_cr0, |
6aa8b732 AK |
11774 | .set_cr3 = vmx_set_cr3, |
11775 | .set_cr4 = vmx_set_cr4, | |
6aa8b732 | 11776 | .set_efer = vmx_set_efer, |
6aa8b732 AK |
11777 | .get_idt = vmx_get_idt, |
11778 | .set_idt = vmx_set_idt, | |
11779 | .get_gdt = vmx_get_gdt, | |
11780 | .set_gdt = vmx_set_gdt, | |
73aaf249 JK |
11781 | .get_dr6 = vmx_get_dr6, |
11782 | .set_dr6 = vmx_set_dr6, | |
020df079 | 11783 | .set_dr7 = vmx_set_dr7, |
81908bf4 | 11784 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
5fdbf976 | 11785 | .cache_reg = vmx_cache_reg, |
6aa8b732 AK |
11786 | .get_rflags = vmx_get_rflags, |
11787 | .set_rflags = vmx_set_rflags, | |
be94f6b7 | 11788 | |
6aa8b732 | 11789 | .tlb_flush = vmx_flush_tlb, |
6aa8b732 | 11790 | |
6aa8b732 | 11791 | .run = vmx_vcpu_run, |
6062d012 | 11792 | .handle_exit = vmx_handle_exit, |
6aa8b732 | 11793 | .skip_emulated_instruction = skip_emulated_instruction, |
2809f5d2 GC |
11794 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
11795 | .get_interrupt_shadow = vmx_get_interrupt_shadow, | |
102d8325 | 11796 | .patch_hypercall = vmx_patch_hypercall, |
2a8067f1 | 11797 | .set_irq = vmx_inject_irq, |
95ba8273 | 11798 | .set_nmi = vmx_inject_nmi, |
298101da | 11799 | .queue_exception = vmx_queue_exception, |
b463a6f7 | 11800 | .cancel_injection = vmx_cancel_injection, |
78646121 | 11801 | .interrupt_allowed = vmx_interrupt_allowed, |
95ba8273 | 11802 | .nmi_allowed = vmx_nmi_allowed, |
3cfc3092 JK |
11803 | .get_nmi_mask = vmx_get_nmi_mask, |
11804 | .set_nmi_mask = vmx_set_nmi_mask, | |
95ba8273 GN |
11805 | .enable_nmi_window = enable_nmi_window, |
11806 | .enable_irq_window = enable_irq_window, | |
11807 | .update_cr8_intercept = update_cr8_intercept, | |
8d14695f | 11808 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
38b99173 | 11809 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
d62caabb AS |
11810 | .get_enable_apicv = vmx_get_enable_apicv, |
11811 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, | |
c7c9c56c | 11812 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
967235d3 | 11813 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
c7c9c56c YZ |
11814 | .hwapic_irr_update = vmx_hwapic_irr_update, |
11815 | .hwapic_isr_update = vmx_hwapic_isr_update, | |
a20ed54d YZ |
11816 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
11817 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, | |
95ba8273 | 11818 | |
cbc94022 | 11819 | .set_tss_addr = vmx_set_tss_addr, |
67253af5 | 11820 | .get_tdp_level = get_ept_level, |
4b12f0de | 11821 | .get_mt_mask = vmx_get_mt_mask, |
229456fc | 11822 | |
586f9607 | 11823 | .get_exit_info = vmx_get_exit_info, |
586f9607 | 11824 | |
17cc3935 | 11825 | .get_lpage_level = vmx_get_lpage_level, |
0e851880 SY |
11826 | |
11827 | .cpuid_update = vmx_cpuid_update, | |
4e47c7a6 SY |
11828 | |
11829 | .rdtscp_supported = vmx_rdtscp_supported, | |
ad756a16 | 11830 | .invpcid_supported = vmx_invpcid_supported, |
d4330ef2 JR |
11831 | |
11832 | .set_supported_cpuid = vmx_set_supported_cpuid, | |
f5f48ee1 SY |
11833 | |
11834 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, | |
99e3e30a ZA |
11835 | |
11836 | .write_tsc_offset = vmx_write_tsc_offset, | |
1c97f0a0 JR |
11837 | |
11838 | .set_tdp_cr3 = vmx_set_cr3, | |
8a76d7f2 JR |
11839 | |
11840 | .check_intercept = vmx_check_intercept, | |
a547c6db | 11841 | .handle_external_intr = vmx_handle_external_intr, |
da8999d3 | 11842 | .mpx_supported = vmx_mpx_supported, |
55412b2e | 11843 | .xsaves_supported = vmx_xsaves_supported, |
b6b8a145 JK |
11844 | |
11845 | .check_nested_events = vmx_check_nested_events, | |
ae97a3b8 RK |
11846 | |
11847 | .sched_in = vmx_sched_in, | |
843e4330 KH |
11848 | |
11849 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, | |
11850 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, | |
11851 | .flush_log_dirty = vmx_flush_log_dirty, | |
11852 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, | |
c5f983f6 | 11853 | .write_log_dirty = vmx_write_pml_buffer, |
25462f7f | 11854 | |
bf9f6ac8 FW |
11855 | .pre_block = vmx_pre_block, |
11856 | .post_block = vmx_post_block, | |
11857 | ||
25462f7f | 11858 | .pmu_ops = &intel_pmu_ops, |
efc64404 FW |
11859 | |
11860 | .update_pi_irte = vmx_update_pi_irte, | |
64672c95 YJ |
11861 | |
11862 | #ifdef CONFIG_X86_64 | |
11863 | .set_hv_timer = vmx_set_hv_timer, | |
11864 | .cancel_hv_timer = vmx_cancel_hv_timer, | |
11865 | #endif | |
c45dcc71 AR |
11866 | |
11867 | .setup_mce = vmx_setup_mce, | |
6aa8b732 AK |
11868 | }; |
11869 | ||
11870 | static int __init vmx_init(void) | |
11871 | { | |
34a1cd60 TC |
11872 | int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
11873 | __alignof__(struct vcpu_vmx), THIS_MODULE); | |
fdef3ad1 | 11874 | if (r) |
34a1cd60 | 11875 | return r; |
25c5f225 | 11876 | |
2965faa5 | 11877 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
11878 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
11879 | crash_vmclear_local_loaded_vmcss); | |
11880 | #endif | |
11881 | ||
fdef3ad1 | 11882 | return 0; |
6aa8b732 AK |
11883 | } |
11884 | ||
11885 | static void __exit vmx_exit(void) | |
11886 | { | |
2965faa5 | 11887 | #ifdef CONFIG_KEXEC_CORE |
3b63a43f | 11888 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
8f536b76 ZY |
11889 | synchronize_rcu(); |
11890 | #endif | |
11891 | ||
cb498ea2 | 11892 | kvm_exit(); |
6aa8b732 AK |
11893 | } |
11894 | ||
11895 | module_init(vmx_init) | |
11896 | module_exit(vmx_exit) |