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> |
085331df | 37 | #include <linux/nospec.h> |
5fdbf976 | 38 | #include "kvm_cache_regs.h" |
35920a35 | 39 | #include "x86.h" |
e495606d | 40 | |
28b835d6 | 41 | #include <asm/cpu.h> |
6aa8b732 | 42 | #include <asm/io.h> |
3b3be0d1 | 43 | #include <asm/desc.h> |
13673a90 | 44 | #include <asm/vmx.h> |
6210e37b | 45 | #include <asm/virtext.h> |
a0861c02 | 46 | #include <asm/mce.h> |
952f07ec | 47 | #include <asm/fpu/internal.h> |
d7cd9796 | 48 | #include <asm/perf_event.h> |
81908bf4 | 49 | #include <asm/debugreg.h> |
8f536b76 | 50 | #include <asm/kexec.h> |
dab2087d | 51 | #include <asm/apic.h> |
efc64404 | 52 | #include <asm/irq_remapping.h> |
d6e41f11 | 53 | #include <asm/mmu_context.h> |
28a27752 | 54 | #include <asm/spec-ctrl.h> |
773e8a04 | 55 | #include <asm/mshyperv.h> |
6aa8b732 | 56 | |
229456fc | 57 | #include "trace.h" |
25462f7f | 58 | #include "pmu.h" |
773e8a04 | 59 | #include "vmx_evmcs.h" |
229456fc | 60 | |
4ecac3fd | 61 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
5e520e62 AK |
62 | #define __ex_clear(x, reg) \ |
63 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) | |
4ecac3fd | 64 | |
6aa8b732 AK |
65 | MODULE_AUTHOR("Qumranet"); |
66 | MODULE_LICENSE("GPL"); | |
67 | ||
e9bda3b3 JT |
68 | static const struct x86_cpu_id vmx_cpu_id[] = { |
69 | X86_FEATURE_MATCH(X86_FEATURE_VMX), | |
70 | {} | |
71 | }; | |
72 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); | |
73 | ||
476bc001 | 74 | static bool __read_mostly enable_vpid = 1; |
736caefe | 75 | module_param_named(vpid, enable_vpid, bool, 0444); |
2384d2b3 | 76 | |
d02fcf50 PB |
77 | static bool __read_mostly enable_vnmi = 1; |
78 | module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); | |
79 | ||
476bc001 | 80 | static bool __read_mostly flexpriority_enabled = 1; |
736caefe | 81 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
4c9fc8ef | 82 | |
476bc001 | 83 | static bool __read_mostly enable_ept = 1; |
736caefe | 84 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
d56f546d | 85 | |
476bc001 | 86 | static bool __read_mostly enable_unrestricted_guest = 1; |
3a624e29 NK |
87 | module_param_named(unrestricted_guest, |
88 | enable_unrestricted_guest, bool, S_IRUGO); | |
89 | ||
83c3a331 XH |
90 | static bool __read_mostly enable_ept_ad_bits = 1; |
91 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); | |
92 | ||
a27685c3 | 93 | static bool __read_mostly emulate_invalid_guest_state = true; |
c1f8bc04 | 94 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
04fa4d32 | 95 | |
476bc001 | 96 | static bool __read_mostly fasteoi = 1; |
58fbbf26 KT |
97 | module_param(fasteoi, bool, S_IRUGO); |
98 | ||
5a71785d | 99 | static bool __read_mostly enable_apicv = 1; |
01e439be | 100 | module_param(enable_apicv, bool, S_IRUGO); |
83d4c286 | 101 | |
abc4fc58 AG |
102 | static bool __read_mostly enable_shadow_vmcs = 1; |
103 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); | |
801d3424 NHE |
104 | /* |
105 | * If nested=1, nested virtualization is supported, i.e., guests may use | |
106 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not | |
107 | * use VMX instructions. | |
108 | */ | |
476bc001 | 109 | static bool __read_mostly nested = 0; |
801d3424 NHE |
110 | module_param(nested, bool, S_IRUGO); |
111 | ||
20300099 WL |
112 | static u64 __read_mostly host_xss; |
113 | ||
843e4330 KH |
114 | static bool __read_mostly enable_pml = 1; |
115 | module_param_named(pml, enable_pml, bool, S_IRUGO); | |
116 | ||
904e14fb PB |
117 | #define MSR_TYPE_R 1 |
118 | #define MSR_TYPE_W 2 | |
119 | #define MSR_TYPE_RW 3 | |
120 | ||
121 | #define MSR_BITMAP_MODE_X2APIC 1 | |
122 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 | |
123 | #define MSR_BITMAP_MODE_LM 4 | |
124 | ||
64903d61 HZ |
125 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
126 | ||
64672c95 YJ |
127 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
128 | static int __read_mostly cpu_preemption_timer_multi; | |
129 | static bool __read_mostly enable_preemption_timer = 1; | |
130 | #ifdef CONFIG_X86_64 | |
131 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); | |
132 | #endif | |
133 | ||
5037878e | 134 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
1706bd0c SC |
135 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE |
136 | #define KVM_VM_CR0_ALWAYS_ON \ | |
137 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \ | |
138 | X86_CR0_WP | X86_CR0_PG | X86_CR0_PE) | |
4c38609a AK |
139 | #define KVM_CR4_GUEST_OWNED_BITS \ |
140 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ | |
fd8cb433 | 141 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
4c38609a | 142 | |
5dc1f044 | 143 | #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE |
cdc0e244 AK |
144 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
145 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) | |
146 | ||
78ac8b47 AK |
147 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
148 | ||
f4124500 JK |
149 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
150 | ||
16c2aec6 JD |
151 | /* |
152 | * Hyper-V requires all of these, so mark them as supported even though | |
153 | * they are just treated the same as all-context. | |
154 | */ | |
155 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ | |
156 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ | |
157 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ | |
158 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ | |
159 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) | |
160 | ||
4b8d54f9 ZE |
161 | /* |
162 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: | |
163 | * ple_gap: upper bound on the amount of time between two successive | |
164 | * executions of PAUSE in a loop. Also indicate if ple enabled. | |
00c25bce | 165 | * According to test, this time is usually smaller than 128 cycles. |
4b8d54f9 ZE |
166 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
167 | * in a PAUSE loop. Tests indicate that most spinlocks are held for | |
168 | * less than 2^12 cycles | |
169 | * Time is measured based on a counter that runs at the same rate as the TSC, | |
170 | * refer SDM volume 3b section 21.6.13 & 22.1.3. | |
171 | */ | |
c8e88717 | 172 | static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP; |
b4a2d31d | 173 | |
7fbc85a5 BM |
174 | static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
175 | module_param(ple_window, uint, 0444); | |
4b8d54f9 | 176 | |
b4a2d31d | 177 | /* Default doubles per-vcpu window every exit. */ |
c8e88717 | 178 | static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
7fbc85a5 | 179 | module_param(ple_window_grow, uint, 0444); |
b4a2d31d RK |
180 | |
181 | /* Default resets per-vcpu window every exit to ple_window. */ | |
c8e88717 | 182 | static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
7fbc85a5 | 183 | module_param(ple_window_shrink, uint, 0444); |
b4a2d31d RK |
184 | |
185 | /* Default is to compute the maximum so we can never overflow. */ | |
7fbc85a5 BM |
186 | static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
187 | module_param(ple_window_max, uint, 0444); | |
b4a2d31d | 188 | |
83287ea4 AK |
189 | extern const ulong vmx_return; |
190 | ||
40bbb9d0 SC |
191 | struct kvm_vmx { |
192 | struct kvm kvm; | |
193 | ||
194 | unsigned int tss_addr; | |
195 | bool ept_identity_pagetable_done; | |
196 | gpa_t ept_identity_map_addr; | |
197 | }; | |
198 | ||
8bf00a52 | 199 | #define NR_AUTOLOAD_MSRS 8 |
61d2ef2c | 200 | |
a2fa3e9f GH |
201 | struct vmcs { |
202 | u32 revision_id; | |
203 | u32 abort; | |
204 | char data[0]; | |
205 | }; | |
206 | ||
d462b819 NHE |
207 | /* |
208 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also | |
209 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs | |
210 | * loaded on this CPU (so we can clear them if the CPU goes down). | |
211 | */ | |
212 | struct loaded_vmcs { | |
213 | struct vmcs *vmcs; | |
355f4fb1 | 214 | struct vmcs *shadow_vmcs; |
d462b819 | 215 | int cpu; |
4c4a6f79 PB |
216 | bool launched; |
217 | bool nmi_known_unmasked; | |
44889942 LP |
218 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
219 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ | |
8a1b4392 PB |
220 | /* Support for vnmi-less CPUs */ |
221 | int soft_vnmi_blocked; | |
222 | ktime_t entry_time; | |
223 | s64 vnmi_blocked_time; | |
904e14fb | 224 | unsigned long *msr_bitmap; |
d462b819 NHE |
225 | struct list_head loaded_vmcss_on_cpu_link; |
226 | }; | |
227 | ||
26bb0981 AK |
228 | struct shared_msr_entry { |
229 | unsigned index; | |
230 | u64 data; | |
d5696725 | 231 | u64 mask; |
26bb0981 AK |
232 | }; |
233 | ||
a9d30f33 NHE |
234 | /* |
235 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a | |
236 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has | |
237 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is | |
238 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, | |
239 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. | |
240 | * More than one of these structures may exist, if L1 runs multiple L2 guests. | |
de3a0021 | 241 | * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the |
a9d30f33 NHE |
242 | * underlying hardware which will be used to run L2. |
243 | * This structure is packed to ensure that its layout is identical across | |
244 | * machines (necessary for live migration). | |
245 | * If there are changes in this struct, VMCS12_REVISION must be changed. | |
246 | */ | |
22bd0358 | 247 | typedef u64 natural_width; |
a9d30f33 NHE |
248 | struct __packed vmcs12 { |
249 | /* According to the Intel spec, a VMCS region must start with the | |
250 | * following two fields. Then follow implementation-specific data. | |
251 | */ | |
252 | u32 revision_id; | |
253 | u32 abort; | |
22bd0358 | 254 | |
27d6c865 NHE |
255 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
256 | u32 padding[7]; /* room for future expansion */ | |
257 | ||
22bd0358 NHE |
258 | u64 io_bitmap_a; |
259 | u64 io_bitmap_b; | |
260 | u64 msr_bitmap; | |
261 | u64 vm_exit_msr_store_addr; | |
262 | u64 vm_exit_msr_load_addr; | |
263 | u64 vm_entry_msr_load_addr; | |
264 | u64 tsc_offset; | |
265 | u64 virtual_apic_page_addr; | |
266 | u64 apic_access_addr; | |
705699a1 | 267 | u64 posted_intr_desc_addr; |
27c42a1b | 268 | u64 vm_function_control; |
22bd0358 | 269 | u64 ept_pointer; |
608406e2 WV |
270 | u64 eoi_exit_bitmap0; |
271 | u64 eoi_exit_bitmap1; | |
272 | u64 eoi_exit_bitmap2; | |
273 | u64 eoi_exit_bitmap3; | |
41ab9372 | 274 | u64 eptp_list_address; |
81dc01f7 | 275 | u64 xss_exit_bitmap; |
22bd0358 NHE |
276 | u64 guest_physical_address; |
277 | u64 vmcs_link_pointer; | |
c5f983f6 | 278 | u64 pml_address; |
22bd0358 NHE |
279 | u64 guest_ia32_debugctl; |
280 | u64 guest_ia32_pat; | |
281 | u64 guest_ia32_efer; | |
282 | u64 guest_ia32_perf_global_ctrl; | |
283 | u64 guest_pdptr0; | |
284 | u64 guest_pdptr1; | |
285 | u64 guest_pdptr2; | |
286 | u64 guest_pdptr3; | |
36be0b9d | 287 | u64 guest_bndcfgs; |
22bd0358 NHE |
288 | u64 host_ia32_pat; |
289 | u64 host_ia32_efer; | |
290 | u64 host_ia32_perf_global_ctrl; | |
291 | u64 padding64[8]; /* room for future expansion */ | |
292 | /* | |
293 | * To allow migration of L1 (complete with its L2 guests) between | |
294 | * machines of different natural widths (32 or 64 bit), we cannot have | |
295 | * unsigned long fields with no explict size. We use u64 (aliased | |
296 | * natural_width) instead. Luckily, x86 is little-endian. | |
297 | */ | |
298 | natural_width cr0_guest_host_mask; | |
299 | natural_width cr4_guest_host_mask; | |
300 | natural_width cr0_read_shadow; | |
301 | natural_width cr4_read_shadow; | |
302 | natural_width cr3_target_value0; | |
303 | natural_width cr3_target_value1; | |
304 | natural_width cr3_target_value2; | |
305 | natural_width cr3_target_value3; | |
306 | natural_width exit_qualification; | |
307 | natural_width guest_linear_address; | |
308 | natural_width guest_cr0; | |
309 | natural_width guest_cr3; | |
310 | natural_width guest_cr4; | |
311 | natural_width guest_es_base; | |
312 | natural_width guest_cs_base; | |
313 | natural_width guest_ss_base; | |
314 | natural_width guest_ds_base; | |
315 | natural_width guest_fs_base; | |
316 | natural_width guest_gs_base; | |
317 | natural_width guest_ldtr_base; | |
318 | natural_width guest_tr_base; | |
319 | natural_width guest_gdtr_base; | |
320 | natural_width guest_idtr_base; | |
321 | natural_width guest_dr7; | |
322 | natural_width guest_rsp; | |
323 | natural_width guest_rip; | |
324 | natural_width guest_rflags; | |
325 | natural_width guest_pending_dbg_exceptions; | |
326 | natural_width guest_sysenter_esp; | |
327 | natural_width guest_sysenter_eip; | |
328 | natural_width host_cr0; | |
329 | natural_width host_cr3; | |
330 | natural_width host_cr4; | |
331 | natural_width host_fs_base; | |
332 | natural_width host_gs_base; | |
333 | natural_width host_tr_base; | |
334 | natural_width host_gdtr_base; | |
335 | natural_width host_idtr_base; | |
336 | natural_width host_ia32_sysenter_esp; | |
337 | natural_width host_ia32_sysenter_eip; | |
338 | natural_width host_rsp; | |
339 | natural_width host_rip; | |
340 | natural_width paddingl[8]; /* room for future expansion */ | |
341 | u32 pin_based_vm_exec_control; | |
342 | u32 cpu_based_vm_exec_control; | |
343 | u32 exception_bitmap; | |
344 | u32 page_fault_error_code_mask; | |
345 | u32 page_fault_error_code_match; | |
346 | u32 cr3_target_count; | |
347 | u32 vm_exit_controls; | |
348 | u32 vm_exit_msr_store_count; | |
349 | u32 vm_exit_msr_load_count; | |
350 | u32 vm_entry_controls; | |
351 | u32 vm_entry_msr_load_count; | |
352 | u32 vm_entry_intr_info_field; | |
353 | u32 vm_entry_exception_error_code; | |
354 | u32 vm_entry_instruction_len; | |
355 | u32 tpr_threshold; | |
356 | u32 secondary_vm_exec_control; | |
357 | u32 vm_instruction_error; | |
358 | u32 vm_exit_reason; | |
359 | u32 vm_exit_intr_info; | |
360 | u32 vm_exit_intr_error_code; | |
361 | u32 idt_vectoring_info_field; | |
362 | u32 idt_vectoring_error_code; | |
363 | u32 vm_exit_instruction_len; | |
364 | u32 vmx_instruction_info; | |
365 | u32 guest_es_limit; | |
366 | u32 guest_cs_limit; | |
367 | u32 guest_ss_limit; | |
368 | u32 guest_ds_limit; | |
369 | u32 guest_fs_limit; | |
370 | u32 guest_gs_limit; | |
371 | u32 guest_ldtr_limit; | |
372 | u32 guest_tr_limit; | |
373 | u32 guest_gdtr_limit; | |
374 | u32 guest_idtr_limit; | |
375 | u32 guest_es_ar_bytes; | |
376 | u32 guest_cs_ar_bytes; | |
377 | u32 guest_ss_ar_bytes; | |
378 | u32 guest_ds_ar_bytes; | |
379 | u32 guest_fs_ar_bytes; | |
380 | u32 guest_gs_ar_bytes; | |
381 | u32 guest_ldtr_ar_bytes; | |
382 | u32 guest_tr_ar_bytes; | |
383 | u32 guest_interruptibility_info; | |
384 | u32 guest_activity_state; | |
385 | u32 guest_sysenter_cs; | |
386 | u32 host_ia32_sysenter_cs; | |
0238ea91 JK |
387 | u32 vmx_preemption_timer_value; |
388 | u32 padding32[7]; /* room for future expansion */ | |
22bd0358 | 389 | u16 virtual_processor_id; |
705699a1 | 390 | u16 posted_intr_nv; |
22bd0358 NHE |
391 | u16 guest_es_selector; |
392 | u16 guest_cs_selector; | |
393 | u16 guest_ss_selector; | |
394 | u16 guest_ds_selector; | |
395 | u16 guest_fs_selector; | |
396 | u16 guest_gs_selector; | |
397 | u16 guest_ldtr_selector; | |
398 | u16 guest_tr_selector; | |
608406e2 | 399 | u16 guest_intr_status; |
c5f983f6 | 400 | u16 guest_pml_index; |
22bd0358 NHE |
401 | u16 host_es_selector; |
402 | u16 host_cs_selector; | |
403 | u16 host_ss_selector; | |
404 | u16 host_ds_selector; | |
405 | u16 host_fs_selector; | |
406 | u16 host_gs_selector; | |
407 | u16 host_tr_selector; | |
a9d30f33 NHE |
408 | }; |
409 | ||
410 | /* | |
411 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or | |
412 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and | |
413 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. | |
414 | */ | |
415 | #define VMCS12_REVISION 0x11e57ed0 | |
416 | ||
417 | /* | |
418 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region | |
419 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the | |
420 | * current implementation, 4K are reserved to avoid future complications. | |
421 | */ | |
422 | #define VMCS12_SIZE 0x1000 | |
423 | ||
5b15706d JM |
424 | /* |
425 | * VMCS12_MAX_FIELD_INDEX is the highest index value used in any | |
426 | * supported VMCS12 field encoding. | |
427 | */ | |
428 | #define VMCS12_MAX_FIELD_INDEX 0x17 | |
429 | ||
6677f3da PB |
430 | struct nested_vmx_msrs { |
431 | /* | |
432 | * We only store the "true" versions of the VMX capability MSRs. We | |
433 | * generate the "non-true" versions by setting the must-be-1 bits | |
434 | * according to the SDM. | |
435 | */ | |
436 | u32 procbased_ctls_low; | |
437 | u32 procbased_ctls_high; | |
438 | u32 secondary_ctls_low; | |
439 | u32 secondary_ctls_high; | |
440 | u32 pinbased_ctls_low; | |
441 | u32 pinbased_ctls_high; | |
442 | u32 exit_ctls_low; | |
443 | u32 exit_ctls_high; | |
444 | u32 entry_ctls_low; | |
445 | u32 entry_ctls_high; | |
446 | u32 misc_low; | |
447 | u32 misc_high; | |
448 | u32 ept_caps; | |
449 | u32 vpid_caps; | |
450 | u64 basic; | |
451 | u64 cr0_fixed0; | |
452 | u64 cr0_fixed1; | |
453 | u64 cr4_fixed0; | |
454 | u64 cr4_fixed1; | |
455 | u64 vmcs_enum; | |
456 | u64 vmfunc_controls; | |
457 | }; | |
458 | ||
ec378aee NHE |
459 | /* |
460 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need | |
461 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. | |
462 | */ | |
463 | struct nested_vmx { | |
464 | /* Has the level1 guest done vmxon? */ | |
465 | bool vmxon; | |
3573e22c | 466 | gpa_t vmxon_ptr; |
c5f983f6 | 467 | bool pml_full; |
a9d30f33 NHE |
468 | |
469 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ | |
470 | gpa_t current_vmptr; | |
4f2777bc DM |
471 | /* |
472 | * Cache of the guest's VMCS, existing outside of guest memory. | |
473 | * Loaded from guest memory during VMPTRLD. Flushed to guest | |
8ca44e88 | 474 | * memory during VMCLEAR and VMPTRLD. |
4f2777bc DM |
475 | */ |
476 | struct vmcs12 *cached_vmcs12; | |
012f83cb AG |
477 | /* |
478 | * Indicates if the shadow vmcs must be updated with the | |
479 | * data hold by vmcs12 | |
480 | */ | |
481 | bool sync_shadow_vmcs; | |
74a497fa | 482 | bool dirty_vmcs12; |
ff2f6fe9 | 483 | |
dccbfcf5 | 484 | bool change_vmcs01_virtual_x2apic_mode; |
644d711a NHE |
485 | /* L2 must run next, and mustn't decide to exit to L1. */ |
486 | bool nested_run_pending; | |
de3a0021 JM |
487 | |
488 | struct loaded_vmcs vmcs02; | |
489 | ||
fe3ef05c | 490 | /* |
de3a0021 JM |
491 | * Guest pages referred to in the vmcs02 with host-physical |
492 | * pointers, so we must keep them pinned while L2 runs. | |
fe3ef05c NHE |
493 | */ |
494 | struct page *apic_access_page; | |
a7c0b07d | 495 | struct page *virtual_apic_page; |
705699a1 WV |
496 | struct page *pi_desc_page; |
497 | struct pi_desc *pi_desc; | |
498 | bool pi_pending; | |
499 | u16 posted_intr_nv; | |
f4124500 JK |
500 | |
501 | struct hrtimer preemption_timer; | |
502 | bool preemption_timer_expired; | |
2996fca0 JK |
503 | |
504 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ | |
505 | u64 vmcs01_debugctl; | |
b9c237bb | 506 | |
5c614b35 WL |
507 | u16 vpid02; |
508 | u16 last_vpid; | |
509 | ||
6677f3da | 510 | struct nested_vmx_msrs msrs; |
72e9cbdb LP |
511 | |
512 | /* SMM related state */ | |
513 | struct { | |
514 | /* in VMX operation on SMM entry? */ | |
515 | bool vmxon; | |
516 | /* in guest mode on SMM entry? */ | |
517 | bool guest_mode; | |
518 | } smm; | |
ec378aee NHE |
519 | }; |
520 | ||
01e439be | 521 | #define POSTED_INTR_ON 0 |
ebbfc765 FW |
522 | #define POSTED_INTR_SN 1 |
523 | ||
01e439be YZ |
524 | /* Posted-Interrupt Descriptor */ |
525 | struct pi_desc { | |
526 | u32 pir[8]; /* Posted interrupt requested */ | |
6ef1522f FW |
527 | union { |
528 | struct { | |
529 | /* bit 256 - Outstanding Notification */ | |
530 | u16 on : 1, | |
531 | /* bit 257 - Suppress Notification */ | |
532 | sn : 1, | |
533 | /* bit 271:258 - Reserved */ | |
534 | rsvd_1 : 14; | |
535 | /* bit 279:272 - Notification Vector */ | |
536 | u8 nv; | |
537 | /* bit 287:280 - Reserved */ | |
538 | u8 rsvd_2; | |
539 | /* bit 319:288 - Notification Destination */ | |
540 | u32 ndst; | |
541 | }; | |
542 | u64 control; | |
543 | }; | |
544 | u32 rsvd[6]; | |
01e439be YZ |
545 | } __aligned(64); |
546 | ||
a20ed54d YZ |
547 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
548 | { | |
549 | return test_and_set_bit(POSTED_INTR_ON, | |
550 | (unsigned long *)&pi_desc->control); | |
551 | } | |
552 | ||
553 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) | |
554 | { | |
555 | return test_and_clear_bit(POSTED_INTR_ON, | |
556 | (unsigned long *)&pi_desc->control); | |
557 | } | |
558 | ||
559 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) | |
560 | { | |
561 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); | |
562 | } | |
563 | ||
ebbfc765 FW |
564 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
565 | { | |
566 | return clear_bit(POSTED_INTR_SN, | |
567 | (unsigned long *)&pi_desc->control); | |
568 | } | |
569 | ||
570 | static inline void pi_set_sn(struct pi_desc *pi_desc) | |
571 | { | |
572 | return set_bit(POSTED_INTR_SN, | |
573 | (unsigned long *)&pi_desc->control); | |
574 | } | |
575 | ||
ad361091 PB |
576 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
577 | { | |
578 | clear_bit(POSTED_INTR_ON, | |
579 | (unsigned long *)&pi_desc->control); | |
580 | } | |
581 | ||
ebbfc765 FW |
582 | static inline int pi_test_on(struct pi_desc *pi_desc) |
583 | { | |
584 | return test_bit(POSTED_INTR_ON, | |
585 | (unsigned long *)&pi_desc->control); | |
586 | } | |
587 | ||
588 | static inline int pi_test_sn(struct pi_desc *pi_desc) | |
589 | { | |
590 | return test_bit(POSTED_INTR_SN, | |
591 | (unsigned long *)&pi_desc->control); | |
592 | } | |
593 | ||
a2fa3e9f | 594 | struct vcpu_vmx { |
fb3f0f51 | 595 | struct kvm_vcpu vcpu; |
313dbd49 | 596 | unsigned long host_rsp; |
29bd8a78 | 597 | u8 fail; |
904e14fb | 598 | u8 msr_bitmap_mode; |
51aa01d1 | 599 | u32 exit_intr_info; |
1155f76a | 600 | u32 idt_vectoring_info; |
6de12732 | 601 | ulong rflags; |
26bb0981 | 602 | struct shared_msr_entry *guest_msrs; |
a2fa3e9f GH |
603 | int nmsrs; |
604 | int save_nmsrs; | |
a547c6db | 605 | unsigned long host_idt_base; |
a2fa3e9f | 606 | #ifdef CONFIG_X86_64 |
44ea2b17 AK |
607 | u64 msr_host_kernel_gs_base; |
608 | u64 msr_guest_kernel_gs_base; | |
a2fa3e9f | 609 | #endif |
15d45071 | 610 | |
28c1c9fa | 611 | u64 arch_capabilities; |
d28b387f | 612 | u64 spec_ctrl; |
28c1c9fa | 613 | |
2961e876 GN |
614 | u32 vm_entry_controls_shadow; |
615 | u32 vm_exit_controls_shadow; | |
80154d77 PB |
616 | u32 secondary_exec_control; |
617 | ||
d462b819 NHE |
618 | /* |
619 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a | |
620 | * non-nested (L1) guest, it always points to vmcs01. For a nested | |
621 | * guest (L2), it points to a different VMCS. | |
622 | */ | |
623 | struct loaded_vmcs vmcs01; | |
624 | struct loaded_vmcs *loaded_vmcs; | |
625 | bool __launched; /* temporary, used in vmx_vcpu_run */ | |
61d2ef2c AK |
626 | struct msr_autoload { |
627 | unsigned nr; | |
628 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; | |
629 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; | |
630 | } msr_autoload; | |
a2fa3e9f GH |
631 | struct { |
632 | int loaded; | |
633 | u16 fs_sel, gs_sel, ldt_sel; | |
b2da15ac AK |
634 | #ifdef CONFIG_X86_64 |
635 | u16 ds_sel, es_sel; | |
636 | #endif | |
152d3f2f LV |
637 | int gs_ldt_reload_needed; |
638 | int fs_reload_needed; | |
da8999d3 | 639 | u64 msr_host_bndcfgs; |
d77c26fc | 640 | } host_state; |
9c8cba37 | 641 | struct { |
7ffd92c5 | 642 | int vm86_active; |
78ac8b47 | 643 | ulong save_rflags; |
f5f7b2fe AK |
644 | struct kvm_segment segs[8]; |
645 | } rmode; | |
646 | struct { | |
647 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ | |
7ffd92c5 AK |
648 | struct kvm_save_segment { |
649 | u16 selector; | |
650 | unsigned long base; | |
651 | u32 limit; | |
652 | u32 ar; | |
f5f7b2fe | 653 | } seg[8]; |
2fb92db1 | 654 | } segment_cache; |
2384d2b3 | 655 | int vpid; |
04fa4d32 | 656 | bool emulation_required; |
3b86cd99 | 657 | |
a0861c02 | 658 | u32 exit_reason; |
4e47c7a6 | 659 | |
01e439be YZ |
660 | /* Posted interrupt descriptor */ |
661 | struct pi_desc pi_desc; | |
662 | ||
ec378aee NHE |
663 | /* Support for a guest hypervisor (nested VMX) */ |
664 | struct nested_vmx nested; | |
a7653ecd RK |
665 | |
666 | /* Dynamic PLE window. */ | |
667 | int ple_window; | |
668 | bool ple_window_dirty; | |
843e4330 KH |
669 | |
670 | /* Support for PML */ | |
671 | #define PML_ENTITY_NUM 512 | |
672 | struct page *pml_pg; | |
2680d6da | 673 | |
64672c95 YJ |
674 | /* apic deadline value in host tsc */ |
675 | u64 hv_deadline_tsc; | |
676 | ||
2680d6da | 677 | u64 current_tsc_ratio; |
1be0e61c | 678 | |
1be0e61c | 679 | u32 host_pkru; |
3b84080b | 680 | |
74c55931 WL |
681 | unsigned long host_debugctlmsr; |
682 | ||
37e4c997 HZ |
683 | /* |
684 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in | |
685 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included | |
686 | * in msr_ia32_feature_control_valid_bits. | |
687 | */ | |
3b84080b | 688 | u64 msr_ia32_feature_control; |
37e4c997 | 689 | u64 msr_ia32_feature_control_valid_bits; |
a2fa3e9f GH |
690 | }; |
691 | ||
2fb92db1 AK |
692 | enum segment_cache_field { |
693 | SEG_FIELD_SEL = 0, | |
694 | SEG_FIELD_BASE = 1, | |
695 | SEG_FIELD_LIMIT = 2, | |
696 | SEG_FIELD_AR = 3, | |
697 | ||
698 | SEG_FIELD_NR = 4 | |
699 | }; | |
700 | ||
40bbb9d0 SC |
701 | static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm) |
702 | { | |
703 | return container_of(kvm, struct kvm_vmx, kvm); | |
704 | } | |
705 | ||
a2fa3e9f GH |
706 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
707 | { | |
fb3f0f51 | 708 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
a2fa3e9f GH |
709 | } |
710 | ||
efc64404 FW |
711 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
712 | { | |
713 | return &(to_vmx(vcpu)->pi_desc); | |
714 | } | |
715 | ||
58e9ffae | 716 | #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) |
22bd0358 | 717 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
58e9ffae JM |
718 | #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name) |
719 | #define FIELD64(number, name) \ | |
720 | FIELD(number, name), \ | |
721 | [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32) | |
22bd0358 | 722 | |
4607c2d7 | 723 | |
44900ba6 | 724 | static u16 shadow_read_only_fields[] = { |
c9e9deae PB |
725 | #define SHADOW_FIELD_RO(x) x, |
726 | #include "vmx_shadow_fields.h" | |
4607c2d7 | 727 | }; |
fe2b201b | 728 | static int max_shadow_read_only_fields = |
4607c2d7 AG |
729 | ARRAY_SIZE(shadow_read_only_fields); |
730 | ||
44900ba6 | 731 | static u16 shadow_read_write_fields[] = { |
c9e9deae PB |
732 | #define SHADOW_FIELD_RW(x) x, |
733 | #include "vmx_shadow_fields.h" | |
4607c2d7 | 734 | }; |
fe2b201b | 735 | static int max_shadow_read_write_fields = |
4607c2d7 AG |
736 | ARRAY_SIZE(shadow_read_write_fields); |
737 | ||
772e0318 | 738 | static const unsigned short vmcs_field_to_offset_table[] = { |
22bd0358 | 739 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
705699a1 | 740 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
22bd0358 NHE |
741 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
742 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), | |
743 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), | |
744 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), | |
745 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), | |
746 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), | |
747 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), | |
748 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), | |
608406e2 | 749 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
c5f983f6 | 750 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
22bd0358 NHE |
751 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
752 | FIELD(HOST_CS_SELECTOR, host_cs_selector), | |
753 | FIELD(HOST_SS_SELECTOR, host_ss_selector), | |
754 | FIELD(HOST_DS_SELECTOR, host_ds_selector), | |
755 | FIELD(HOST_FS_SELECTOR, host_fs_selector), | |
756 | FIELD(HOST_GS_SELECTOR, host_gs_selector), | |
757 | FIELD(HOST_TR_SELECTOR, host_tr_selector), | |
758 | FIELD64(IO_BITMAP_A, io_bitmap_a), | |
759 | FIELD64(IO_BITMAP_B, io_bitmap_b), | |
760 | FIELD64(MSR_BITMAP, msr_bitmap), | |
761 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), | |
762 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), | |
763 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), | |
764 | FIELD64(TSC_OFFSET, tsc_offset), | |
765 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), | |
766 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), | |
705699a1 | 767 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
27c42a1b | 768 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
22bd0358 | 769 | FIELD64(EPT_POINTER, ept_pointer), |
608406e2 WV |
770 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
771 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), | |
772 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), | |
773 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), | |
41ab9372 | 774 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
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 | { | |
085331df DW |
884 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
885 | unsigned short offset; | |
58e9ffae JM |
886 | unsigned index; |
887 | ||
888 | if (field >> 15) | |
889 | return -ENOENT; | |
a2ae9df7 | 890 | |
58e9ffae | 891 | index = ROL16(field, 6); |
15303ba5 | 892 | if (index >= size) |
75f139aa AH |
893 | return -ENOENT; |
894 | ||
15303ba5 LT |
895 | index = array_index_nospec(index, size); |
896 | offset = vmcs_field_to_offset_table[index]; | |
085331df | 897 | if (offset == 0) |
a2ae9df7 | 898 | return -ENOENT; |
085331df | 899 | return offset; |
22bd0358 NHE |
900 | } |
901 | ||
a9d30f33 NHE |
902 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
903 | { | |
4f2777bc | 904 | return to_vmx(vcpu)->nested.cached_vmcs12; |
a9d30f33 NHE |
905 | } |
906 | ||
995f00a6 | 907 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
bfd0a56b | 908 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
995f00a6 | 909 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
f53cd63c | 910 | static bool vmx_xsaves_supported(void); |
b246dd5d OW |
911 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
912 | struct kvm_segment *var, int seg); | |
913 | static void vmx_get_segment(struct kvm_vcpu *vcpu, | |
914 | struct kvm_segment *var, int seg); | |
d99e4152 GN |
915 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
916 | static u32 vmx_segment_access_rights(struct kvm_segment *var); | |
16f5b903 | 917 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
b96fb439 PB |
918 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
919 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); | |
920 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, | |
921 | u16 error_code); | |
904e14fb | 922 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
15d45071 AR |
923 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
924 | u32 msr, int type); | |
75880a01 | 925 | |
6aa8b732 AK |
926 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
927 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); | |
d462b819 NHE |
928 | /* |
929 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed | |
930 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. | |
931 | */ | |
932 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); | |
6aa8b732 | 933 | |
bf9f6ac8 FW |
934 | /* |
935 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we | |
936 | * can find which vCPU should be waken up. | |
937 | */ | |
938 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); | |
939 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); | |
940 | ||
23611332 | 941 | enum { |
23611332 RK |
942 | VMX_VMREAD_BITMAP, |
943 | VMX_VMWRITE_BITMAP, | |
944 | VMX_BITMAP_NR | |
945 | }; | |
946 | ||
947 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; | |
948 | ||
23611332 RK |
949 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
950 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) | |
fdef3ad1 | 951 | |
110312c8 | 952 | static bool cpu_has_load_ia32_efer; |
8bf00a52 | 953 | static bool cpu_has_load_perf_global_ctrl; |
110312c8 | 954 | |
2384d2b3 SY |
955 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
956 | static DEFINE_SPINLOCK(vmx_vpid_lock); | |
957 | ||
1c3d14fe | 958 | static struct vmcs_config { |
6aa8b732 AK |
959 | int size; |
960 | int order; | |
9ac7e3e8 | 961 | u32 basic_cap; |
6aa8b732 | 962 | u32 revision_id; |
1c3d14fe YS |
963 | u32 pin_based_exec_ctrl; |
964 | u32 cpu_based_exec_ctrl; | |
f78e0e2e | 965 | u32 cpu_based_2nd_exec_ctrl; |
1c3d14fe YS |
966 | u32 vmexit_ctrl; |
967 | u32 vmentry_ctrl; | |
1389309c | 968 | struct nested_vmx_msrs nested; |
1c3d14fe | 969 | } vmcs_config; |
6aa8b732 | 970 | |
efff9e53 | 971 | static struct vmx_capability { |
d56f546d SY |
972 | u32 ept; |
973 | u32 vpid; | |
974 | } vmx_capability; | |
975 | ||
6aa8b732 AK |
976 | #define VMX_SEGMENT_FIELD(seg) \ |
977 | [VCPU_SREG_##seg] = { \ | |
978 | .selector = GUEST_##seg##_SELECTOR, \ | |
979 | .base = GUEST_##seg##_BASE, \ | |
980 | .limit = GUEST_##seg##_LIMIT, \ | |
981 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ | |
982 | } | |
983 | ||
772e0318 | 984 | static const struct kvm_vmx_segment_field { |
6aa8b732 AK |
985 | unsigned selector; |
986 | unsigned base; | |
987 | unsigned limit; | |
988 | unsigned ar_bytes; | |
989 | } kvm_vmx_segment_fields[] = { | |
990 | VMX_SEGMENT_FIELD(CS), | |
991 | VMX_SEGMENT_FIELD(DS), | |
992 | VMX_SEGMENT_FIELD(ES), | |
993 | VMX_SEGMENT_FIELD(FS), | |
994 | VMX_SEGMENT_FIELD(GS), | |
995 | VMX_SEGMENT_FIELD(SS), | |
996 | VMX_SEGMENT_FIELD(TR), | |
997 | VMX_SEGMENT_FIELD(LDTR), | |
998 | }; | |
999 | ||
26bb0981 AK |
1000 | static u64 host_efer; |
1001 | ||
6de4f3ad AK |
1002 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
1003 | ||
4d56c8a7 | 1004 | /* |
8c06585d | 1005 | * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it |
4d56c8a7 AK |
1006 | * away by decrementing the array size. |
1007 | */ | |
6aa8b732 | 1008 | static const u32 vmx_msr_index[] = { |
05b3e0c2 | 1009 | #ifdef CONFIG_X86_64 |
44ea2b17 | 1010 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
6aa8b732 | 1011 | #endif |
8c06585d | 1012 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
6aa8b732 | 1013 | }; |
6aa8b732 | 1014 | |
773e8a04 VK |
1015 | DEFINE_STATIC_KEY_FALSE(enable_evmcs); |
1016 | ||
1017 | #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) | |
1018 | ||
1019 | #define KVM_EVMCS_VERSION 1 | |
1020 | ||
1021 | #if IS_ENABLED(CONFIG_HYPERV) | |
1022 | static bool __read_mostly enlightened_vmcs = true; | |
1023 | module_param(enlightened_vmcs, bool, 0444); | |
1024 | ||
1025 | static inline void evmcs_write64(unsigned long field, u64 value) | |
1026 | { | |
1027 | u16 clean_field; | |
1028 | int offset = get_evmcs_offset(field, &clean_field); | |
1029 | ||
1030 | if (offset < 0) | |
1031 | return; | |
1032 | ||
1033 | *(u64 *)((char *)current_evmcs + offset) = value; | |
1034 | ||
1035 | current_evmcs->hv_clean_fields &= ~clean_field; | |
1036 | } | |
1037 | ||
1038 | static inline void evmcs_write32(unsigned long field, u32 value) | |
1039 | { | |
1040 | u16 clean_field; | |
1041 | int offset = get_evmcs_offset(field, &clean_field); | |
1042 | ||
1043 | if (offset < 0) | |
1044 | return; | |
1045 | ||
1046 | *(u32 *)((char *)current_evmcs + offset) = value; | |
1047 | current_evmcs->hv_clean_fields &= ~clean_field; | |
1048 | } | |
1049 | ||
1050 | static inline void evmcs_write16(unsigned long field, u16 value) | |
1051 | { | |
1052 | u16 clean_field; | |
1053 | int offset = get_evmcs_offset(field, &clean_field); | |
1054 | ||
1055 | if (offset < 0) | |
1056 | return; | |
1057 | ||
1058 | *(u16 *)((char *)current_evmcs + offset) = value; | |
1059 | current_evmcs->hv_clean_fields &= ~clean_field; | |
1060 | } | |
1061 | ||
1062 | static inline u64 evmcs_read64(unsigned long field) | |
1063 | { | |
1064 | int offset = get_evmcs_offset(field, NULL); | |
1065 | ||
1066 | if (offset < 0) | |
1067 | return 0; | |
1068 | ||
1069 | return *(u64 *)((char *)current_evmcs + offset); | |
1070 | } | |
1071 | ||
1072 | static inline u32 evmcs_read32(unsigned long field) | |
1073 | { | |
1074 | int offset = get_evmcs_offset(field, NULL); | |
1075 | ||
1076 | if (offset < 0) | |
1077 | return 0; | |
1078 | ||
1079 | return *(u32 *)((char *)current_evmcs + offset); | |
1080 | } | |
1081 | ||
1082 | static inline u16 evmcs_read16(unsigned long field) | |
1083 | { | |
1084 | int offset = get_evmcs_offset(field, NULL); | |
1085 | ||
1086 | if (offset < 0) | |
1087 | return 0; | |
1088 | ||
1089 | return *(u16 *)((char *)current_evmcs + offset); | |
1090 | } | |
1091 | ||
1092 | static void evmcs_load(u64 phys_addr) | |
1093 | { | |
1094 | struct hv_vp_assist_page *vp_ap = | |
1095 | hv_get_vp_assist_page(smp_processor_id()); | |
1096 | ||
1097 | vp_ap->current_nested_vmcs = phys_addr; | |
1098 | vp_ap->enlighten_vmentry = 1; | |
1099 | } | |
1100 | ||
1101 | static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) | |
1102 | { | |
1103 | /* | |
1104 | * Enlightened VMCSv1 doesn't support these: | |
1105 | * | |
1106 | * POSTED_INTR_NV = 0x00000002, | |
1107 | * GUEST_INTR_STATUS = 0x00000810, | |
1108 | * APIC_ACCESS_ADDR = 0x00002014, | |
1109 | * POSTED_INTR_DESC_ADDR = 0x00002016, | |
1110 | * EOI_EXIT_BITMAP0 = 0x0000201c, | |
1111 | * EOI_EXIT_BITMAP1 = 0x0000201e, | |
1112 | * EOI_EXIT_BITMAP2 = 0x00002020, | |
1113 | * EOI_EXIT_BITMAP3 = 0x00002022, | |
1114 | */ | |
1115 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; | |
1116 | vmcs_conf->cpu_based_2nd_exec_ctrl &= | |
1117 | ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; | |
1118 | vmcs_conf->cpu_based_2nd_exec_ctrl &= | |
1119 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
1120 | vmcs_conf->cpu_based_2nd_exec_ctrl &= | |
1121 | ~SECONDARY_EXEC_APIC_REGISTER_VIRT; | |
1122 | ||
1123 | /* | |
1124 | * GUEST_PML_INDEX = 0x00000812, | |
1125 | * PML_ADDRESS = 0x0000200e, | |
1126 | */ | |
1127 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML; | |
1128 | ||
1129 | /* VM_FUNCTION_CONTROL = 0x00002018, */ | |
1130 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC; | |
1131 | ||
1132 | /* | |
1133 | * EPTP_LIST_ADDRESS = 0x00002024, | |
1134 | * VMREAD_BITMAP = 0x00002026, | |
1135 | * VMWRITE_BITMAP = 0x00002028, | |
1136 | */ | |
1137 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS; | |
1138 | ||
1139 | /* | |
1140 | * TSC_MULTIPLIER = 0x00002032, | |
1141 | */ | |
1142 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING; | |
1143 | ||
1144 | /* | |
1145 | * PLE_GAP = 0x00004020, | |
1146 | * PLE_WINDOW = 0x00004022, | |
1147 | */ | |
1148 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
1149 | ||
1150 | /* | |
1151 | * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E, | |
1152 | */ | |
1153 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
1154 | ||
1155 | /* | |
1156 | * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808, | |
1157 | * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04, | |
1158 | */ | |
1159 | vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; | |
1160 | vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; | |
1161 | ||
1162 | /* | |
1163 | * Currently unsupported in KVM: | |
1164 | * GUEST_IA32_RTIT_CTL = 0x00002814, | |
1165 | */ | |
1166 | } | |
1167 | #else /* !IS_ENABLED(CONFIG_HYPERV) */ | |
1168 | static inline void evmcs_write64(unsigned long field, u64 value) {} | |
1169 | static inline void evmcs_write32(unsigned long field, u32 value) {} | |
1170 | static inline void evmcs_write16(unsigned long field, u16 value) {} | |
1171 | static inline u64 evmcs_read64(unsigned long field) { return 0; } | |
1172 | static inline u32 evmcs_read32(unsigned long field) { return 0; } | |
1173 | static inline u16 evmcs_read16(unsigned long field) { return 0; } | |
1174 | static inline void evmcs_load(u64 phys_addr) {} | |
1175 | static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {} | |
1176 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ | |
1177 | ||
5bb16016 | 1178 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
6aa8b732 AK |
1179 | { |
1180 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1181 | INTR_INFO_VALID_MASK)) == | |
5bb16016 JK |
1182 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
1183 | } | |
1184 | ||
6f05485d JK |
1185 | static inline bool is_debug(u32 intr_info) |
1186 | { | |
1187 | return is_exception_n(intr_info, DB_VECTOR); | |
1188 | } | |
1189 | ||
1190 | static inline bool is_breakpoint(u32 intr_info) | |
1191 | { | |
1192 | return is_exception_n(intr_info, BP_VECTOR); | |
1193 | } | |
1194 | ||
5bb16016 JK |
1195 | static inline bool is_page_fault(u32 intr_info) |
1196 | { | |
1197 | return is_exception_n(intr_info, PF_VECTOR); | |
6aa8b732 AK |
1198 | } |
1199 | ||
31299944 | 1200 | static inline bool is_no_device(u32 intr_info) |
2ab455cc | 1201 | { |
5bb16016 | 1202 | return is_exception_n(intr_info, NM_VECTOR); |
2ab455cc AL |
1203 | } |
1204 | ||
31299944 | 1205 | static inline bool is_invalid_opcode(u32 intr_info) |
7aa81cc0 | 1206 | { |
5bb16016 | 1207 | return is_exception_n(intr_info, UD_VECTOR); |
7aa81cc0 AL |
1208 | } |
1209 | ||
9e869480 LA |
1210 | static inline bool is_gp_fault(u32 intr_info) |
1211 | { | |
1212 | return is_exception_n(intr_info, GP_VECTOR); | |
1213 | } | |
1214 | ||
31299944 | 1215 | static inline bool is_external_interrupt(u32 intr_info) |
6aa8b732 AK |
1216 | { |
1217 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
1218 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); | |
1219 | } | |
1220 | ||
31299944 | 1221 | static inline bool is_machine_check(u32 intr_info) |
a0861c02 AK |
1222 | { |
1223 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1224 | INTR_INFO_VALID_MASK)) == | |
1225 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); | |
1226 | } | |
1227 | ||
32d43cd3 LT |
1228 | /* Undocumented: icebp/int1 */ |
1229 | static inline bool is_icebp(u32 intr_info) | |
1230 | { | |
1231 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
1232 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); | |
1233 | } | |
1234 | ||
31299944 | 1235 | static inline bool cpu_has_vmx_msr_bitmap(void) |
25c5f225 | 1236 | { |
04547156 | 1237 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
25c5f225 SY |
1238 | } |
1239 | ||
31299944 | 1240 | static inline bool cpu_has_vmx_tpr_shadow(void) |
6e5d865c | 1241 | { |
04547156 | 1242 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
6e5d865c YS |
1243 | } |
1244 | ||
35754c98 | 1245 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
6e5d865c | 1246 | { |
35754c98 | 1247 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
6e5d865c YS |
1248 | } |
1249 | ||
31299944 | 1250 | static inline bool cpu_has_secondary_exec_ctrls(void) |
f78e0e2e | 1251 | { |
04547156 SY |
1252 | return vmcs_config.cpu_based_exec_ctrl & |
1253 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
f78e0e2e SY |
1254 | } |
1255 | ||
774ead3a | 1256 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
f78e0e2e | 1257 | { |
04547156 SY |
1258 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1259 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
1260 | } | |
1261 | ||
8d14695f YZ |
1262 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
1263 | { | |
1264 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1265 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
1266 | } | |
1267 | ||
83d4c286 YZ |
1268 | static inline bool cpu_has_vmx_apic_register_virt(void) |
1269 | { | |
1270 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1271 | SECONDARY_EXEC_APIC_REGISTER_VIRT; | |
1272 | } | |
1273 | ||
c7c9c56c YZ |
1274 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
1275 | { | |
1276 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1277 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; | |
1278 | } | |
1279 | ||
64672c95 YJ |
1280 | /* |
1281 | * Comment's format: document - errata name - stepping - processor name. | |
1282 | * Refer from | |
1283 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp | |
1284 | */ | |
1285 | static u32 vmx_preemption_cpu_tfms[] = { | |
1286 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ | |
1287 | 0x000206E6, | |
1288 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ | |
1289 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ | |
1290 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1291 | 0x00020652, | |
1292 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1293 | 0x00020655, | |
1294 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ | |
1295 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ | |
1296 | /* | |
1297 | * 320767.pdf - AAP86 - B1 - | |
1298 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile | |
1299 | */ | |
1300 | 0x000106E5, | |
1301 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ | |
1302 | 0x000106A0, | |
1303 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ | |
1304 | 0x000106A1, | |
1305 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ | |
1306 | 0x000106A4, | |
1307 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ | |
1308 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ | |
1309 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ | |
1310 | 0x000106A5, | |
1311 | }; | |
1312 | ||
1313 | static inline bool cpu_has_broken_vmx_preemption_timer(void) | |
1314 | { | |
1315 | u32 eax = cpuid_eax(0x00000001), i; | |
1316 | ||
1317 | /* Clear the reserved bits */ | |
1318 | eax &= ~(0x3U << 14 | 0xfU << 28); | |
03f6a22a | 1319 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
64672c95 YJ |
1320 | if (eax == vmx_preemption_cpu_tfms[i]) |
1321 | return true; | |
1322 | ||
1323 | return false; | |
1324 | } | |
1325 | ||
1326 | static inline bool cpu_has_vmx_preemption_timer(void) | |
1327 | { | |
64672c95 YJ |
1328 | return vmcs_config.pin_based_exec_ctrl & |
1329 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1330 | } | |
1331 | ||
01e439be YZ |
1332 | static inline bool cpu_has_vmx_posted_intr(void) |
1333 | { | |
d6a858d1 PB |
1334 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
1335 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; | |
01e439be YZ |
1336 | } |
1337 | ||
1338 | static inline bool cpu_has_vmx_apicv(void) | |
1339 | { | |
1340 | return cpu_has_vmx_apic_register_virt() && | |
1341 | cpu_has_vmx_virtual_intr_delivery() && | |
1342 | cpu_has_vmx_posted_intr(); | |
1343 | } | |
1344 | ||
04547156 SY |
1345 | static inline bool cpu_has_vmx_flexpriority(void) |
1346 | { | |
1347 | return cpu_has_vmx_tpr_shadow() && | |
1348 | cpu_has_vmx_virtualize_apic_accesses(); | |
f78e0e2e SY |
1349 | } |
1350 | ||
e799794e MT |
1351 | static inline bool cpu_has_vmx_ept_execute_only(void) |
1352 | { | |
31299944 | 1353 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
e799794e MT |
1354 | } |
1355 | ||
e799794e MT |
1356 | static inline bool cpu_has_vmx_ept_2m_page(void) |
1357 | { | |
31299944 | 1358 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
e799794e MT |
1359 | } |
1360 | ||
878403b7 SY |
1361 | static inline bool cpu_has_vmx_ept_1g_page(void) |
1362 | { | |
31299944 | 1363 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
878403b7 SY |
1364 | } |
1365 | ||
4bc9b982 SY |
1366 | static inline bool cpu_has_vmx_ept_4levels(void) |
1367 | { | |
1368 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; | |
1369 | } | |
1370 | ||
42aa53b4 DH |
1371 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
1372 | { | |
1373 | return vmx_capability.ept & VMX_EPTP_WB_BIT; | |
1374 | } | |
1375 | ||
855feb67 YZ |
1376 | static inline bool cpu_has_vmx_ept_5levels(void) |
1377 | { | |
1378 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; | |
1379 | } | |
1380 | ||
83c3a331 XH |
1381 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
1382 | { | |
1383 | return vmx_capability.ept & VMX_EPT_AD_BIT; | |
1384 | } | |
1385 | ||
31299944 | 1386 | static inline bool cpu_has_vmx_invept_context(void) |
d56f546d | 1387 | { |
31299944 | 1388 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
d56f546d SY |
1389 | } |
1390 | ||
31299944 | 1391 | static inline bool cpu_has_vmx_invept_global(void) |
d56f546d | 1392 | { |
31299944 | 1393 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
d56f546d SY |
1394 | } |
1395 | ||
518c8aee GJ |
1396 | static inline bool cpu_has_vmx_invvpid_single(void) |
1397 | { | |
1398 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; | |
1399 | } | |
1400 | ||
b9d762fa GJ |
1401 | static inline bool cpu_has_vmx_invvpid_global(void) |
1402 | { | |
1403 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; | |
1404 | } | |
1405 | ||
08d839c4 WL |
1406 | static inline bool cpu_has_vmx_invvpid(void) |
1407 | { | |
1408 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; | |
1409 | } | |
1410 | ||
31299944 | 1411 | static inline bool cpu_has_vmx_ept(void) |
d56f546d | 1412 | { |
04547156 SY |
1413 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1414 | SECONDARY_EXEC_ENABLE_EPT; | |
d56f546d SY |
1415 | } |
1416 | ||
31299944 | 1417 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
3a624e29 NK |
1418 | { |
1419 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1420 | SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
1421 | } | |
1422 | ||
31299944 | 1423 | static inline bool cpu_has_vmx_ple(void) |
4b8d54f9 ZE |
1424 | { |
1425 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1426 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
1427 | } | |
1428 | ||
9ac7e3e8 JD |
1429 | static inline bool cpu_has_vmx_basic_inout(void) |
1430 | { | |
1431 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); | |
1432 | } | |
1433 | ||
35754c98 | 1434 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
f78e0e2e | 1435 | { |
35754c98 | 1436 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
f78e0e2e SY |
1437 | } |
1438 | ||
31299944 | 1439 | static inline bool cpu_has_vmx_vpid(void) |
2384d2b3 | 1440 | { |
04547156 SY |
1441 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1442 | SECONDARY_EXEC_ENABLE_VPID; | |
2384d2b3 SY |
1443 | } |
1444 | ||
31299944 | 1445 | static inline bool cpu_has_vmx_rdtscp(void) |
4e47c7a6 SY |
1446 | { |
1447 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1448 | SECONDARY_EXEC_RDTSCP; | |
1449 | } | |
1450 | ||
ad756a16 MJ |
1451 | static inline bool cpu_has_vmx_invpcid(void) |
1452 | { | |
1453 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1454 | SECONDARY_EXEC_ENABLE_INVPCID; | |
1455 | } | |
1456 | ||
8a1b4392 PB |
1457 | static inline bool cpu_has_virtual_nmis(void) |
1458 | { | |
1459 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; | |
1460 | } | |
1461 | ||
f5f48ee1 SY |
1462 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
1463 | { | |
1464 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1465 | SECONDARY_EXEC_WBINVD_EXITING; | |
1466 | } | |
1467 | ||
abc4fc58 AG |
1468 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
1469 | { | |
1470 | u64 vmx_msr; | |
1471 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
1472 | /* check if the cpu supports writing r/o exit information fields */ | |
1473 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) | |
1474 | return false; | |
1475 | ||
1476 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1477 | SECONDARY_EXEC_SHADOW_VMCS; | |
1478 | } | |
1479 | ||
843e4330 KH |
1480 | static inline bool cpu_has_vmx_pml(void) |
1481 | { | |
1482 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; | |
1483 | } | |
1484 | ||
64903d61 HZ |
1485 | static inline bool cpu_has_vmx_tsc_scaling(void) |
1486 | { | |
1487 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1488 | SECONDARY_EXEC_TSC_SCALING; | |
1489 | } | |
1490 | ||
2a499e49 BD |
1491 | static inline bool cpu_has_vmx_vmfunc(void) |
1492 | { | |
1493 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1494 | SECONDARY_EXEC_ENABLE_VMFUNC; | |
1495 | } | |
1496 | ||
04547156 SY |
1497 | static inline bool report_flexpriority(void) |
1498 | { | |
1499 | return flexpriority_enabled; | |
1500 | } | |
1501 | ||
c7c2c709 JM |
1502 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
1503 | { | |
6677f3da | 1504 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low); |
c7c2c709 JM |
1505 | } |
1506 | ||
fe3ef05c NHE |
1507 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
1508 | { | |
1509 | return vmcs12->cpu_based_vm_exec_control & bit; | |
1510 | } | |
1511 | ||
1512 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) | |
1513 | { | |
1514 | return (vmcs12->cpu_based_vm_exec_control & | |
1515 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && | |
1516 | (vmcs12->secondary_vm_exec_control & bit); | |
1517 | } | |
1518 | ||
f4124500 JK |
1519 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
1520 | { | |
1521 | return vmcs12->pin_based_vm_exec_control & | |
1522 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1523 | } | |
1524 | ||
0c7f650e KS |
1525 | static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12) |
1526 | { | |
1527 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING; | |
1528 | } | |
1529 | ||
1530 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) | |
1531 | { | |
1532 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; | |
1533 | } | |
1534 | ||
155a97a3 NHE |
1535 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
1536 | { | |
1537 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); | |
1538 | } | |
1539 | ||
81dc01f7 WL |
1540 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
1541 | { | |
3db13480 | 1542 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
81dc01f7 WL |
1543 | } |
1544 | ||
c5f983f6 BD |
1545 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
1546 | { | |
1547 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); | |
1548 | } | |
1549 | ||
f2b93280 WV |
1550 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
1551 | { | |
1552 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); | |
1553 | } | |
1554 | ||
5c614b35 WL |
1555 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
1556 | { | |
1557 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); | |
1558 | } | |
1559 | ||
82f0dd4b WV |
1560 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
1561 | { | |
1562 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); | |
1563 | } | |
1564 | ||
608406e2 WV |
1565 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
1566 | { | |
1567 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
1568 | } | |
1569 | ||
705699a1 WV |
1570 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
1571 | { | |
1572 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; | |
1573 | } | |
1574 | ||
27c42a1b BD |
1575 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
1576 | { | |
1577 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); | |
1578 | } | |
1579 | ||
41ab9372 BD |
1580 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
1581 | { | |
1582 | return nested_cpu_has_vmfunc(vmcs12) && | |
1583 | (vmcs12->vm_function_control & | |
1584 | VMX_VMFUNC_EPTP_SWITCHING); | |
1585 | } | |
1586 | ||
ef85b673 | 1587 | static inline bool is_nmi(u32 intr_info) |
644d711a NHE |
1588 | { |
1589 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
ef85b673 | 1590 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
644d711a NHE |
1591 | } |
1592 | ||
533558bc JK |
1593 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
1594 | u32 exit_intr_info, | |
1595 | unsigned long exit_qualification); | |
7c177938 NHE |
1596 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
1597 | struct vmcs12 *vmcs12, | |
1598 | u32 reason, unsigned long qualification); | |
1599 | ||
8b9cf98c | 1600 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
7725f0ba AK |
1601 | { |
1602 | int i; | |
1603 | ||
a2fa3e9f | 1604 | for (i = 0; i < vmx->nmsrs; ++i) |
26bb0981 | 1605 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
a75beee6 ED |
1606 | return i; |
1607 | return -1; | |
1608 | } | |
1609 | ||
2384d2b3 SY |
1610 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
1611 | { | |
1612 | struct { | |
1613 | u64 vpid : 16; | |
1614 | u64 rsvd : 48; | |
1615 | u64 gva; | |
1616 | } operand = { vpid, 0, gva }; | |
1617 | ||
4ecac3fd | 1618 | asm volatile (__ex(ASM_VMX_INVVPID) |
2384d2b3 SY |
1619 | /* CF==1 or ZF==1 --> rc = -1 */ |
1620 | "; ja 1f ; ud2 ; 1:" | |
1621 | : : "a"(&operand), "c"(ext) : "cc", "memory"); | |
1622 | } | |
1623 | ||
1439442c SY |
1624 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
1625 | { | |
1626 | struct { | |
1627 | u64 eptp, gpa; | |
1628 | } operand = {eptp, gpa}; | |
1629 | ||
4ecac3fd | 1630 | asm volatile (__ex(ASM_VMX_INVEPT) |
1439442c SY |
1631 | /* CF==1 or ZF==1 --> rc = -1 */ |
1632 | "; ja 1f ; ud2 ; 1:\n" | |
1633 | : : "a" (&operand), "c" (ext) : "cc", "memory"); | |
1634 | } | |
1635 | ||
26bb0981 | 1636 | static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
a75beee6 ED |
1637 | { |
1638 | int i; | |
1639 | ||
8b9cf98c | 1640 | i = __find_msr_index(vmx, msr); |
a75beee6 | 1641 | if (i >= 0) |
a2fa3e9f | 1642 | return &vmx->guest_msrs[i]; |
8b6d44c7 | 1643 | return NULL; |
7725f0ba AK |
1644 | } |
1645 | ||
6aa8b732 AK |
1646 | static void vmcs_clear(struct vmcs *vmcs) |
1647 | { | |
1648 | u64 phys_addr = __pa(vmcs); | |
1649 | u8 error; | |
1650 | ||
4ecac3fd | 1651 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
16d8f72f | 1652 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
6aa8b732 AK |
1653 | : "cc", "memory"); |
1654 | if (error) | |
1655 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", | |
1656 | vmcs, phys_addr); | |
1657 | } | |
1658 | ||
d462b819 NHE |
1659 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
1660 | { | |
1661 | vmcs_clear(loaded_vmcs->vmcs); | |
355f4fb1 JM |
1662 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
1663 | vmcs_clear(loaded_vmcs->shadow_vmcs); | |
d462b819 NHE |
1664 | loaded_vmcs->cpu = -1; |
1665 | loaded_vmcs->launched = 0; | |
1666 | } | |
1667 | ||
7725b894 DX |
1668 | static void vmcs_load(struct vmcs *vmcs) |
1669 | { | |
1670 | u64 phys_addr = __pa(vmcs); | |
1671 | u8 error; | |
1672 | ||
773e8a04 VK |
1673 | if (static_branch_unlikely(&enable_evmcs)) |
1674 | return evmcs_load(phys_addr); | |
1675 | ||
7725b894 | 1676 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
16d8f72f | 1677 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
7725b894 DX |
1678 | : "cc", "memory"); |
1679 | if (error) | |
2844d849 | 1680 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
7725b894 DX |
1681 | vmcs, phys_addr); |
1682 | } | |
1683 | ||
2965faa5 | 1684 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
1685 | /* |
1686 | * This bitmap is used to indicate whether the vmclear | |
1687 | * operation is enabled on all cpus. All disabled by | |
1688 | * default. | |
1689 | */ | |
1690 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; | |
1691 | ||
1692 | static inline void crash_enable_local_vmclear(int cpu) | |
1693 | { | |
1694 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1695 | } | |
1696 | ||
1697 | static inline void crash_disable_local_vmclear(int cpu) | |
1698 | { | |
1699 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1700 | } | |
1701 | ||
1702 | static inline int crash_local_vmclear_enabled(int cpu) | |
1703 | { | |
1704 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1705 | } | |
1706 | ||
1707 | static void crash_vmclear_local_loaded_vmcss(void) | |
1708 | { | |
1709 | int cpu = raw_smp_processor_id(); | |
1710 | struct loaded_vmcs *v; | |
1711 | ||
1712 | if (!crash_local_vmclear_enabled(cpu)) | |
1713 | return; | |
1714 | ||
1715 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), | |
1716 | loaded_vmcss_on_cpu_link) | |
1717 | vmcs_clear(v->vmcs); | |
1718 | } | |
1719 | #else | |
1720 | static inline void crash_enable_local_vmclear(int cpu) { } | |
1721 | static inline void crash_disable_local_vmclear(int cpu) { } | |
2965faa5 | 1722 | #endif /* CONFIG_KEXEC_CORE */ |
8f536b76 | 1723 | |
d462b819 | 1724 | static void __loaded_vmcs_clear(void *arg) |
6aa8b732 | 1725 | { |
d462b819 | 1726 | struct loaded_vmcs *loaded_vmcs = arg; |
d3b2c338 | 1727 | int cpu = raw_smp_processor_id(); |
6aa8b732 | 1728 | |
d462b819 NHE |
1729 | if (loaded_vmcs->cpu != cpu) |
1730 | return; /* vcpu migration can race with cpu offline */ | |
1731 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) | |
6aa8b732 | 1732 | per_cpu(current_vmcs, cpu) = NULL; |
8f536b76 | 1733 | crash_disable_local_vmclear(cpu); |
d462b819 | 1734 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
5a560f8b XG |
1735 | |
1736 | /* | |
1737 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link | |
1738 | * is before setting loaded_vmcs->vcpu to -1 which is done in | |
1739 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist | |
1740 | * then adds the vmcs into percpu list before it is deleted. | |
1741 | */ | |
1742 | smp_wmb(); | |
1743 | ||
d462b819 | 1744 | loaded_vmcs_init(loaded_vmcs); |
8f536b76 | 1745 | crash_enable_local_vmclear(cpu); |
6aa8b732 AK |
1746 | } |
1747 | ||
d462b819 | 1748 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
8d0be2b3 | 1749 | { |
e6c7d321 XG |
1750 | int cpu = loaded_vmcs->cpu; |
1751 | ||
1752 | if (cpu != -1) | |
1753 | smp_call_function_single(cpu, | |
1754 | __loaded_vmcs_clear, loaded_vmcs, 1); | |
8d0be2b3 AK |
1755 | } |
1756 | ||
dd5f5341 | 1757 | static inline void vpid_sync_vcpu_single(int vpid) |
2384d2b3 | 1758 | { |
dd5f5341 | 1759 | if (vpid == 0) |
2384d2b3 SY |
1760 | return; |
1761 | ||
518c8aee | 1762 | if (cpu_has_vmx_invvpid_single()) |
dd5f5341 | 1763 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
2384d2b3 SY |
1764 | } |
1765 | ||
b9d762fa GJ |
1766 | static inline void vpid_sync_vcpu_global(void) |
1767 | { | |
1768 | if (cpu_has_vmx_invvpid_global()) | |
1769 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); | |
1770 | } | |
1771 | ||
dd5f5341 | 1772 | static inline void vpid_sync_context(int vpid) |
b9d762fa GJ |
1773 | { |
1774 | if (cpu_has_vmx_invvpid_single()) | |
dd5f5341 | 1775 | vpid_sync_vcpu_single(vpid); |
b9d762fa GJ |
1776 | else |
1777 | vpid_sync_vcpu_global(); | |
1778 | } | |
1779 | ||
1439442c SY |
1780 | static inline void ept_sync_global(void) |
1781 | { | |
f5f51586 | 1782 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
1439442c SY |
1783 | } |
1784 | ||
1785 | static inline void ept_sync_context(u64 eptp) | |
1786 | { | |
0e1252dc DH |
1787 | if (cpu_has_vmx_invept_context()) |
1788 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); | |
1789 | else | |
1790 | ept_sync_global(); | |
1439442c SY |
1791 | } |
1792 | ||
8a86aea9 PB |
1793 | static __always_inline void vmcs_check16(unsigned long field) |
1794 | { | |
1795 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1796 | "16-bit accessor invalid for 64-bit field"); | |
1797 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1798 | "16-bit accessor invalid for 64-bit high field"); | |
1799 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1800 | "16-bit accessor invalid for 32-bit high field"); | |
1801 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1802 | "16-bit accessor invalid for natural width field"); | |
1803 | } | |
1804 | ||
1805 | static __always_inline void vmcs_check32(unsigned long field) | |
1806 | { | |
1807 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1808 | "32-bit accessor invalid for 16-bit field"); | |
1809 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1810 | "32-bit accessor invalid for natural width field"); | |
1811 | } | |
1812 | ||
1813 | static __always_inline void vmcs_check64(unsigned long field) | |
1814 | { | |
1815 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1816 | "64-bit accessor invalid for 16-bit field"); | |
1817 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1818 | "64-bit accessor invalid for 64-bit high field"); | |
1819 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1820 | "64-bit accessor invalid for 32-bit field"); | |
1821 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1822 | "64-bit accessor invalid for natural width field"); | |
1823 | } | |
1824 | ||
1825 | static __always_inline void vmcs_checkl(unsigned long field) | |
1826 | { | |
1827 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1828 | "Natural width accessor invalid for 16-bit field"); | |
1829 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1830 | "Natural width accessor invalid for 64-bit field"); | |
1831 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1832 | "Natural width accessor invalid for 64-bit high field"); | |
1833 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1834 | "Natural width accessor invalid for 32-bit field"); | |
1835 | } | |
1836 | ||
1837 | static __always_inline unsigned long __vmcs_readl(unsigned long field) | |
6aa8b732 | 1838 | { |
5e520e62 | 1839 | unsigned long value; |
6aa8b732 | 1840 | |
5e520e62 AK |
1841 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
1842 | : "=a"(value) : "d"(field) : "cc"); | |
6aa8b732 AK |
1843 | return value; |
1844 | } | |
1845 | ||
96304217 | 1846 | static __always_inline u16 vmcs_read16(unsigned long field) |
6aa8b732 | 1847 | { |
8a86aea9 | 1848 | vmcs_check16(field); |
773e8a04 VK |
1849 | if (static_branch_unlikely(&enable_evmcs)) |
1850 | return evmcs_read16(field); | |
8a86aea9 | 1851 | return __vmcs_readl(field); |
6aa8b732 AK |
1852 | } |
1853 | ||
96304217 | 1854 | static __always_inline u32 vmcs_read32(unsigned long field) |
6aa8b732 | 1855 | { |
8a86aea9 | 1856 | vmcs_check32(field); |
773e8a04 VK |
1857 | if (static_branch_unlikely(&enable_evmcs)) |
1858 | return evmcs_read32(field); | |
8a86aea9 | 1859 | return __vmcs_readl(field); |
6aa8b732 AK |
1860 | } |
1861 | ||
96304217 | 1862 | static __always_inline u64 vmcs_read64(unsigned long field) |
6aa8b732 | 1863 | { |
8a86aea9 | 1864 | vmcs_check64(field); |
773e8a04 VK |
1865 | if (static_branch_unlikely(&enable_evmcs)) |
1866 | return evmcs_read64(field); | |
05b3e0c2 | 1867 | #ifdef CONFIG_X86_64 |
8a86aea9 | 1868 | return __vmcs_readl(field); |
6aa8b732 | 1869 | #else |
8a86aea9 | 1870 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
6aa8b732 AK |
1871 | #endif |
1872 | } | |
1873 | ||
8a86aea9 PB |
1874 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
1875 | { | |
1876 | vmcs_checkl(field); | |
773e8a04 VK |
1877 | if (static_branch_unlikely(&enable_evmcs)) |
1878 | return evmcs_read64(field); | |
8a86aea9 PB |
1879 | return __vmcs_readl(field); |
1880 | } | |
1881 | ||
e52de1b8 AK |
1882 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
1883 | { | |
1884 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", | |
1885 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); | |
1886 | dump_stack(); | |
1887 | } | |
1888 | ||
8a86aea9 | 1889 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
6aa8b732 AK |
1890 | { |
1891 | u8 error; | |
1892 | ||
4ecac3fd | 1893 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
d77c26fc | 1894 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
e52de1b8 AK |
1895 | if (unlikely(error)) |
1896 | vmwrite_error(field, value); | |
6aa8b732 AK |
1897 | } |
1898 | ||
8a86aea9 | 1899 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
6aa8b732 | 1900 | { |
8a86aea9 | 1901 | vmcs_check16(field); |
773e8a04 VK |
1902 | if (static_branch_unlikely(&enable_evmcs)) |
1903 | return evmcs_write16(field, value); | |
1904 | ||
8a86aea9 | 1905 | __vmcs_writel(field, value); |
6aa8b732 AK |
1906 | } |
1907 | ||
8a86aea9 | 1908 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
6aa8b732 | 1909 | { |
8a86aea9 | 1910 | vmcs_check32(field); |
773e8a04 VK |
1911 | if (static_branch_unlikely(&enable_evmcs)) |
1912 | return evmcs_write32(field, value); | |
1913 | ||
8a86aea9 | 1914 | __vmcs_writel(field, value); |
6aa8b732 AK |
1915 | } |
1916 | ||
8a86aea9 | 1917 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
6aa8b732 | 1918 | { |
8a86aea9 | 1919 | vmcs_check64(field); |
773e8a04 VK |
1920 | if (static_branch_unlikely(&enable_evmcs)) |
1921 | return evmcs_write64(field, value); | |
1922 | ||
8a86aea9 | 1923 | __vmcs_writel(field, value); |
7682f2d0 | 1924 | #ifndef CONFIG_X86_64 |
6aa8b732 | 1925 | asm volatile (""); |
8a86aea9 | 1926 | __vmcs_writel(field+1, value >> 32); |
6aa8b732 AK |
1927 | #endif |
1928 | } | |
1929 | ||
8a86aea9 | 1930 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
2ab455cc | 1931 | { |
8a86aea9 | 1932 | vmcs_checkl(field); |
773e8a04 VK |
1933 | if (static_branch_unlikely(&enable_evmcs)) |
1934 | return evmcs_write64(field, value); | |
1935 | ||
8a86aea9 | 1936 | __vmcs_writel(field, value); |
2ab455cc AL |
1937 | } |
1938 | ||
8a86aea9 | 1939 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
2ab455cc | 1940 | { |
8a86aea9 PB |
1941 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1942 | "vmcs_clear_bits does not support 64-bit fields"); | |
773e8a04 VK |
1943 | if (static_branch_unlikely(&enable_evmcs)) |
1944 | return evmcs_write32(field, evmcs_read32(field) & ~mask); | |
1945 | ||
8a86aea9 | 1946 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
2ab455cc AL |
1947 | } |
1948 | ||
8a86aea9 | 1949 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
2ab455cc | 1950 | { |
8a86aea9 PB |
1951 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1952 | "vmcs_set_bits does not support 64-bit fields"); | |
773e8a04 VK |
1953 | if (static_branch_unlikely(&enable_evmcs)) |
1954 | return evmcs_write32(field, evmcs_read32(field) | mask); | |
1955 | ||
8a86aea9 | 1956 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
2ab455cc AL |
1957 | } |
1958 | ||
8391ce44 PB |
1959 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
1960 | { | |
1961 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); | |
1962 | } | |
1963 | ||
2961e876 GN |
1964 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
1965 | { | |
1966 | vmcs_write32(VM_ENTRY_CONTROLS, val); | |
1967 | vmx->vm_entry_controls_shadow = val; | |
1968 | } | |
1969 | ||
1970 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) | |
1971 | { | |
1972 | if (vmx->vm_entry_controls_shadow != val) | |
1973 | vm_entry_controls_init(vmx, val); | |
1974 | } | |
1975 | ||
1976 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) | |
1977 | { | |
1978 | return vmx->vm_entry_controls_shadow; | |
1979 | } | |
1980 | ||
1981 | ||
1982 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
1983 | { | |
1984 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); | |
1985 | } | |
1986 | ||
1987 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
1988 | { | |
1989 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); | |
1990 | } | |
1991 | ||
8391ce44 PB |
1992 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
1993 | { | |
1994 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); | |
1995 | } | |
1996 | ||
2961e876 GN |
1997 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
1998 | { | |
1999 | vmcs_write32(VM_EXIT_CONTROLS, val); | |
2000 | vmx->vm_exit_controls_shadow = val; | |
2001 | } | |
2002 | ||
2003 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) | |
2004 | { | |
2005 | if (vmx->vm_exit_controls_shadow != val) | |
2006 | vm_exit_controls_init(vmx, val); | |
2007 | } | |
2008 | ||
2009 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) | |
2010 | { | |
2011 | return vmx->vm_exit_controls_shadow; | |
2012 | } | |
2013 | ||
2014 | ||
2015 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
2016 | { | |
2017 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); | |
2018 | } | |
2019 | ||
2020 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
2021 | { | |
2022 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); | |
2023 | } | |
2024 | ||
2fb92db1 AK |
2025 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
2026 | { | |
2027 | vmx->segment_cache.bitmask = 0; | |
2028 | } | |
2029 | ||
2030 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, | |
2031 | unsigned field) | |
2032 | { | |
2033 | bool ret; | |
2034 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); | |
2035 | ||
2036 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { | |
2037 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); | |
2038 | vmx->segment_cache.bitmask = 0; | |
2039 | } | |
2040 | ret = vmx->segment_cache.bitmask & mask; | |
2041 | vmx->segment_cache.bitmask |= mask; | |
2042 | return ret; | |
2043 | } | |
2044 | ||
2045 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) | |
2046 | { | |
2047 | u16 *p = &vmx->segment_cache.seg[seg].selector; | |
2048 | ||
2049 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) | |
2050 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); | |
2051 | return *p; | |
2052 | } | |
2053 | ||
2054 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) | |
2055 | { | |
2056 | ulong *p = &vmx->segment_cache.seg[seg].base; | |
2057 | ||
2058 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) | |
2059 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); | |
2060 | return *p; | |
2061 | } | |
2062 | ||
2063 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) | |
2064 | { | |
2065 | u32 *p = &vmx->segment_cache.seg[seg].limit; | |
2066 | ||
2067 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) | |
2068 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); | |
2069 | return *p; | |
2070 | } | |
2071 | ||
2072 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) | |
2073 | { | |
2074 | u32 *p = &vmx->segment_cache.seg[seg].ar; | |
2075 | ||
2076 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) | |
2077 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); | |
2078 | return *p; | |
2079 | } | |
2080 | ||
abd3f2d6 AK |
2081 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
2082 | { | |
2083 | u32 eb; | |
2084 | ||
fd7373cc | 2085 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
bd7e5b08 | 2086 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
9e869480 LA |
2087 | /* |
2088 | * Guest access to VMware backdoor ports could legitimately | |
2089 | * trigger #GP because of TSS I/O permission bitmap. | |
2090 | * We intercept those #GP and allow access to them anyway | |
2091 | * as VMware does. | |
2092 | */ | |
2093 | if (enable_vmware_backdoor) | |
2094 | eb |= (1u << GP_VECTOR); | |
fd7373cc JK |
2095 | if ((vcpu->guest_debug & |
2096 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == | |
2097 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) | |
2098 | eb |= 1u << BP_VECTOR; | |
7ffd92c5 | 2099 | if (to_vmx(vcpu)->rmode.vm86_active) |
abd3f2d6 | 2100 | eb = ~0; |
089d034e | 2101 | if (enable_ept) |
1439442c | 2102 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
36cf24e0 NHE |
2103 | |
2104 | /* When we are running a nested L2 guest and L1 specified for it a | |
2105 | * certain exception bitmap, we must trap the same exceptions and pass | |
2106 | * them to L1. When running L2, we will only handle the exceptions | |
2107 | * specified above if L1 did not want them. | |
2108 | */ | |
2109 | if (is_guest_mode(vcpu)) | |
2110 | eb |= get_vmcs12(vcpu)->exception_bitmap; | |
2111 | ||
abd3f2d6 AK |
2112 | vmcs_write32(EXCEPTION_BITMAP, eb); |
2113 | } | |
2114 | ||
d28b387f KA |
2115 | /* |
2116 | * Check if MSR is intercepted for currently loaded MSR bitmap. | |
2117 | */ | |
2118 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) | |
2119 | { | |
2120 | unsigned long *msr_bitmap; | |
2121 | int f = sizeof(unsigned long); | |
2122 | ||
2123 | if (!cpu_has_vmx_msr_bitmap()) | |
2124 | return true; | |
2125 | ||
2126 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; | |
2127 | ||
2128 | if (msr <= 0x1fff) { | |
2129 | return !!test_bit(msr, msr_bitmap + 0x800 / f); | |
2130 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
2131 | msr &= 0x1fff; | |
2132 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); | |
2133 | } | |
2134 | ||
2135 | return true; | |
2136 | } | |
2137 | ||
15d45071 AR |
2138 | /* |
2139 | * Check if MSR is intercepted for L01 MSR bitmap. | |
2140 | */ | |
2141 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) | |
2142 | { | |
2143 | unsigned long *msr_bitmap; | |
2144 | int f = sizeof(unsigned long); | |
2145 | ||
2146 | if (!cpu_has_vmx_msr_bitmap()) | |
2147 | return true; | |
2148 | ||
2149 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; | |
2150 | ||
2151 | if (msr <= 0x1fff) { | |
2152 | return !!test_bit(msr, msr_bitmap + 0x800 / f); | |
2153 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
2154 | msr &= 0x1fff; | |
2155 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); | |
2156 | } | |
2157 | ||
2158 | return true; | |
2159 | } | |
2160 | ||
2961e876 GN |
2161 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
2162 | unsigned long entry, unsigned long exit) | |
8bf00a52 | 2163 | { |
2961e876 GN |
2164 | vm_entry_controls_clearbit(vmx, entry); |
2165 | vm_exit_controls_clearbit(vmx, exit); | |
8bf00a52 GN |
2166 | } |
2167 | ||
61d2ef2c AK |
2168 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
2169 | { | |
2170 | unsigned i; | |
2171 | struct msr_autoload *m = &vmx->msr_autoload; | |
2172 | ||
8bf00a52 GN |
2173 | switch (msr) { |
2174 | case MSR_EFER: | |
2175 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
2176 | clear_atomic_switch_msr_special(vmx, |
2177 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
2178 | VM_EXIT_LOAD_IA32_EFER); |
2179 | return; | |
2180 | } | |
2181 | break; | |
2182 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
2183 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 2184 | clear_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
2185 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
2186 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
2187 | return; | |
2188 | } | |
2189 | break; | |
110312c8 AK |
2190 | } |
2191 | ||
61d2ef2c AK |
2192 | for (i = 0; i < m->nr; ++i) |
2193 | if (m->guest[i].index == msr) | |
2194 | break; | |
2195 | ||
2196 | if (i == m->nr) | |
2197 | return; | |
2198 | --m->nr; | |
2199 | m->guest[i] = m->guest[m->nr]; | |
2200 | m->host[i] = m->host[m->nr]; | |
2201 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); | |
2202 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); | |
2203 | } | |
2204 | ||
2961e876 GN |
2205 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
2206 | unsigned long entry, unsigned long exit, | |
2207 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, | |
2208 | u64 guest_val, u64 host_val) | |
8bf00a52 GN |
2209 | { |
2210 | vmcs_write64(guest_val_vmcs, guest_val); | |
2211 | vmcs_write64(host_val_vmcs, host_val); | |
2961e876 GN |
2212 | vm_entry_controls_setbit(vmx, entry); |
2213 | vm_exit_controls_setbit(vmx, exit); | |
8bf00a52 GN |
2214 | } |
2215 | ||
61d2ef2c AK |
2216 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
2217 | u64 guest_val, u64 host_val) | |
2218 | { | |
2219 | unsigned i; | |
2220 | struct msr_autoload *m = &vmx->msr_autoload; | |
2221 | ||
8bf00a52 GN |
2222 | switch (msr) { |
2223 | case MSR_EFER: | |
2224 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
2225 | add_atomic_switch_msr_special(vmx, |
2226 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
2227 | VM_EXIT_LOAD_IA32_EFER, |
2228 | GUEST_IA32_EFER, | |
2229 | HOST_IA32_EFER, | |
2230 | guest_val, host_val); | |
2231 | return; | |
2232 | } | |
2233 | break; | |
2234 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
2235 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 2236 | add_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
2237 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
2238 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, | |
2239 | GUEST_IA32_PERF_GLOBAL_CTRL, | |
2240 | HOST_IA32_PERF_GLOBAL_CTRL, | |
2241 | guest_val, host_val); | |
2242 | return; | |
2243 | } | |
2244 | break; | |
7099e2e1 RK |
2245 | case MSR_IA32_PEBS_ENABLE: |
2246 | /* PEBS needs a quiescent period after being disabled (to write | |
2247 | * a record). Disabling PEBS through VMX MSR swapping doesn't | |
2248 | * provide that period, so a CPU could write host's record into | |
2249 | * guest's memory. | |
2250 | */ | |
2251 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); | |
110312c8 AK |
2252 | } |
2253 | ||
61d2ef2c AK |
2254 | for (i = 0; i < m->nr; ++i) |
2255 | if (m->guest[i].index == msr) | |
2256 | break; | |
2257 | ||
e7fc6f93 | 2258 | if (i == NR_AUTOLOAD_MSRS) { |
60266204 | 2259 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
e7fc6f93 GN |
2260 | "Can't add msr %x\n", msr); |
2261 | return; | |
2262 | } else if (i == m->nr) { | |
61d2ef2c AK |
2263 | ++m->nr; |
2264 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); | |
2265 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); | |
2266 | } | |
2267 | ||
2268 | m->guest[i].index = msr; | |
2269 | m->guest[i].value = guest_val; | |
2270 | m->host[i].index = msr; | |
2271 | m->host[i].value = host_val; | |
2272 | } | |
2273 | ||
92c0d900 | 2274 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
2cc51560 | 2275 | { |
844a5fe2 PB |
2276 | u64 guest_efer = vmx->vcpu.arch.efer; |
2277 | u64 ignore_bits = 0; | |
2278 | ||
2279 | if (!enable_ept) { | |
2280 | /* | |
2281 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing | |
2282 | * host CPUID is more efficient than testing guest CPUID | |
2283 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. | |
2284 | */ | |
2285 | if (boot_cpu_has(X86_FEATURE_SMEP)) | |
2286 | guest_efer |= EFER_NX; | |
2287 | else if (!(guest_efer & EFER_NX)) | |
2288 | ignore_bits |= EFER_NX; | |
2289 | } | |
3a34a881 | 2290 | |
51c6cf66 | 2291 | /* |
844a5fe2 | 2292 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
51c6cf66 | 2293 | */ |
844a5fe2 | 2294 | ignore_bits |= EFER_SCE; |
51c6cf66 AK |
2295 | #ifdef CONFIG_X86_64 |
2296 | ignore_bits |= EFER_LMA | EFER_LME; | |
2297 | /* SCE is meaningful only in long mode on Intel */ | |
2298 | if (guest_efer & EFER_LMA) | |
2299 | ignore_bits &= ~(u64)EFER_SCE; | |
2300 | #endif | |
84ad33ef AK |
2301 | |
2302 | clear_atomic_switch_msr(vmx, MSR_EFER); | |
f6577a5f AL |
2303 | |
2304 | /* | |
2305 | * On EPT, we can't emulate NX, so we must switch EFER atomically. | |
2306 | * On CPUs that support "load IA32_EFER", always switch EFER | |
2307 | * atomically, since it's faster than switching it manually. | |
2308 | */ | |
2309 | if (cpu_has_load_ia32_efer || | |
2310 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { | |
84ad33ef AK |
2311 | if (!(guest_efer & EFER_LMA)) |
2312 | guest_efer &= ~EFER_LME; | |
54b98bff AL |
2313 | if (guest_efer != host_efer) |
2314 | add_atomic_switch_msr(vmx, MSR_EFER, | |
2315 | guest_efer, host_efer); | |
84ad33ef | 2316 | return false; |
844a5fe2 PB |
2317 | } else { |
2318 | guest_efer &= ~ignore_bits; | |
2319 | guest_efer |= host_efer & ignore_bits; | |
2320 | ||
2321 | vmx->guest_msrs[efer_offset].data = guest_efer; | |
2322 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; | |
84ad33ef | 2323 | |
844a5fe2 PB |
2324 | return true; |
2325 | } | |
51c6cf66 AK |
2326 | } |
2327 | ||
e28baead AL |
2328 | #ifdef CONFIG_X86_32 |
2329 | /* | |
2330 | * On 32-bit kernels, VM exits still load the FS and GS bases from the | |
2331 | * VMCS rather than the segment table. KVM uses this helper to figure | |
2332 | * out the current bases to poke them into the VMCS before entry. | |
2333 | */ | |
2d49ec72 GN |
2334 | static unsigned long segment_base(u16 selector) |
2335 | { | |
8c2e41f7 | 2336 | struct desc_struct *table; |
2d49ec72 GN |
2337 | unsigned long v; |
2338 | ||
8c2e41f7 | 2339 | if (!(selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2340 | return 0; |
2341 | ||
45fc8757 | 2342 | table = get_current_gdt_ro(); |
2d49ec72 | 2343 | |
8c2e41f7 | 2344 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
2d49ec72 GN |
2345 | u16 ldt_selector = kvm_read_ldt(); |
2346 | ||
8c2e41f7 | 2347 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2348 | return 0; |
2349 | ||
8c2e41f7 | 2350 | table = (struct desc_struct *)segment_base(ldt_selector); |
2d49ec72 | 2351 | } |
8c2e41f7 | 2352 | v = get_desc_base(&table[selector >> 3]); |
2d49ec72 GN |
2353 | return v; |
2354 | } | |
e28baead | 2355 | #endif |
2d49ec72 | 2356 | |
04d2cc77 | 2357 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
33ed6329 | 2358 | { |
04d2cc77 | 2359 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
51e8a8cc | 2360 | #ifdef CONFIG_X86_64 |
35060ed6 | 2361 | int cpu = raw_smp_processor_id(); |
51e8a8cc | 2362 | #endif |
26bb0981 | 2363 | int i; |
04d2cc77 | 2364 | |
a2fa3e9f | 2365 | if (vmx->host_state.loaded) |
33ed6329 AK |
2366 | return; |
2367 | ||
a2fa3e9f | 2368 | vmx->host_state.loaded = 1; |
33ed6329 AK |
2369 | /* |
2370 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not | |
2371 | * allow segment selectors with cpl > 0 or ti == 1. | |
2372 | */ | |
d6e88aec | 2373 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
152d3f2f | 2374 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
42b933b5 VK |
2375 | |
2376 | #ifdef CONFIG_X86_64 | |
2377 | save_fsgs_for_kvm(); | |
2378 | vmx->host_state.fs_sel = current->thread.fsindex; | |
2379 | vmx->host_state.gs_sel = current->thread.gsindex; | |
2380 | #else | |
9581d442 | 2381 | savesegment(fs, vmx->host_state.fs_sel); |
42b933b5 VK |
2382 | savesegment(gs, vmx->host_state.gs_sel); |
2383 | #endif | |
152d3f2f | 2384 | if (!(vmx->host_state.fs_sel & 7)) { |
a2fa3e9f | 2385 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
152d3f2f LV |
2386 | vmx->host_state.fs_reload_needed = 0; |
2387 | } else { | |
33ed6329 | 2388 | vmcs_write16(HOST_FS_SELECTOR, 0); |
152d3f2f | 2389 | vmx->host_state.fs_reload_needed = 1; |
33ed6329 | 2390 | } |
a2fa3e9f GH |
2391 | if (!(vmx->host_state.gs_sel & 7)) |
2392 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); | |
33ed6329 AK |
2393 | else { |
2394 | vmcs_write16(HOST_GS_SELECTOR, 0); | |
152d3f2f | 2395 | vmx->host_state.gs_ldt_reload_needed = 1; |
33ed6329 AK |
2396 | } |
2397 | ||
b2da15ac AK |
2398 | #ifdef CONFIG_X86_64 |
2399 | savesegment(ds, vmx->host_state.ds_sel); | |
2400 | savesegment(es, vmx->host_state.es_sel); | |
b2da15ac | 2401 | |
42b933b5 | 2402 | vmcs_writel(HOST_FS_BASE, current->thread.fsbase); |
35060ed6 | 2403 | vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu)); |
707c0874 | 2404 | |
42b933b5 | 2405 | vmx->msr_host_kernel_gs_base = current->thread.gsbase; |
c8770e7b | 2406 | if (is_long_mode(&vmx->vcpu)) |
44ea2b17 | 2407 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
4fde8d57 PB |
2408 | #else |
2409 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); | |
2410 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); | |
707c0874 | 2411 | #endif |
da8999d3 LJ |
2412 | if (boot_cpu_has(X86_FEATURE_MPX)) |
2413 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
26bb0981 AK |
2414 | for (i = 0; i < vmx->save_nmsrs; ++i) |
2415 | kvm_set_shared_msr(vmx->guest_msrs[i].index, | |
d5696725 AK |
2416 | vmx->guest_msrs[i].data, |
2417 | vmx->guest_msrs[i].mask); | |
33ed6329 AK |
2418 | } |
2419 | ||
a9b21b62 | 2420 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
33ed6329 | 2421 | { |
a2fa3e9f | 2422 | if (!vmx->host_state.loaded) |
33ed6329 AK |
2423 | return; |
2424 | ||
e1beb1d3 | 2425 | ++vmx->vcpu.stat.host_state_reload; |
a2fa3e9f | 2426 | vmx->host_state.loaded = 0; |
c8770e7b AK |
2427 | #ifdef CONFIG_X86_64 |
2428 | if (is_long_mode(&vmx->vcpu)) | |
2429 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); | |
2430 | #endif | |
152d3f2f | 2431 | if (vmx->host_state.gs_ldt_reload_needed) { |
d6e88aec | 2432 | kvm_load_ldt(vmx->host_state.ldt_sel); |
33ed6329 | 2433 | #ifdef CONFIG_X86_64 |
9581d442 | 2434 | load_gs_index(vmx->host_state.gs_sel); |
9581d442 AK |
2435 | #else |
2436 | loadsegment(gs, vmx->host_state.gs_sel); | |
33ed6329 | 2437 | #endif |
33ed6329 | 2438 | } |
0a77fe4c AK |
2439 | if (vmx->host_state.fs_reload_needed) |
2440 | loadsegment(fs, vmx->host_state.fs_sel); | |
b2da15ac AK |
2441 | #ifdef CONFIG_X86_64 |
2442 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { | |
2443 | loadsegment(ds, vmx->host_state.ds_sel); | |
2444 | loadsegment(es, vmx->host_state.es_sel); | |
2445 | } | |
b2da15ac | 2446 | #endif |
b7ffc44d | 2447 | invalidate_tss_limit(); |
44ea2b17 | 2448 | #ifdef CONFIG_X86_64 |
c8770e7b | 2449 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
44ea2b17 | 2450 | #endif |
da8999d3 LJ |
2451 | if (vmx->host_state.msr_host_bndcfgs) |
2452 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
45fc8757 | 2453 | load_fixmap_gdt(raw_smp_processor_id()); |
33ed6329 AK |
2454 | } |
2455 | ||
a9b21b62 AK |
2456 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
2457 | { | |
2458 | preempt_disable(); | |
2459 | __vmx_load_host_state(vmx); | |
2460 | preempt_enable(); | |
2461 | } | |
2462 | ||
28b835d6 FW |
2463 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
2464 | { | |
2465 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2466 | struct pi_desc old, new; | |
2467 | unsigned int dest; | |
2468 | ||
31afb2ea PB |
2469 | /* |
2470 | * In case of hot-plug or hot-unplug, we may have to undo | |
2471 | * vmx_vcpu_pi_put even if there is no assigned device. And we | |
2472 | * always keep PI.NDST up to date for simplicity: it makes the | |
2473 | * code easier, and CPU migration is not a fast path. | |
2474 | */ | |
2475 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) | |
28b835d6 FW |
2476 | return; |
2477 | ||
31afb2ea PB |
2478 | /* |
2479 | * First handle the simple case where no cmpxchg is necessary; just | |
2480 | * allow posting non-urgent interrupts. | |
2481 | * | |
2482 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change | |
2483 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler | |
2484 | * expects the VCPU to be on the blocked_vcpu_list that matches | |
2485 | * PI.NDST. | |
2486 | */ | |
2487 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || | |
2488 | vcpu->cpu == cpu) { | |
2489 | pi_clear_sn(pi_desc); | |
28b835d6 | 2490 | return; |
31afb2ea | 2491 | } |
28b835d6 | 2492 | |
31afb2ea | 2493 | /* The full case. */ |
28b835d6 FW |
2494 | do { |
2495 | old.control = new.control = pi_desc->control; | |
2496 | ||
31afb2ea | 2497 | dest = cpu_physical_id(cpu); |
28b835d6 | 2498 | |
31afb2ea PB |
2499 | if (x2apic_enabled()) |
2500 | new.ndst = dest; | |
2501 | else | |
2502 | new.ndst = (dest << 8) & 0xFF00; | |
28b835d6 | 2503 | |
28b835d6 | 2504 | new.sn = 0; |
c0a1666b PB |
2505 | } while (cmpxchg64(&pi_desc->control, old.control, |
2506 | new.control) != old.control); | |
28b835d6 | 2507 | } |
1be0e61c | 2508 | |
c95ba92a PF |
2509 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
2510 | { | |
2511 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; | |
2512 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); | |
2513 | } | |
2514 | ||
6aa8b732 AK |
2515 | /* |
2516 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes | |
2517 | * vcpu mutex is already taken. | |
2518 | */ | |
15ad7146 | 2519 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
6aa8b732 | 2520 | { |
a2fa3e9f | 2521 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b80c76ec | 2522 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
6aa8b732 | 2523 | |
b80c76ec | 2524 | if (!already_loaded) { |
fe0e80be | 2525 | loaded_vmcs_clear(vmx->loaded_vmcs); |
92fe13be | 2526 | local_irq_disable(); |
8f536b76 | 2527 | crash_disable_local_vmclear(cpu); |
5a560f8b XG |
2528 | |
2529 | /* | |
2530 | * Read loaded_vmcs->cpu should be before fetching | |
2531 | * loaded_vmcs->loaded_vmcss_on_cpu_link. | |
2532 | * See the comments in __loaded_vmcs_clear(). | |
2533 | */ | |
2534 | smp_rmb(); | |
2535 | ||
d462b819 NHE |
2536 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
2537 | &per_cpu(loaded_vmcss_on_cpu, cpu)); | |
8f536b76 | 2538 | crash_enable_local_vmclear(cpu); |
92fe13be | 2539 | local_irq_enable(); |
b80c76ec JM |
2540 | } |
2541 | ||
2542 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { | |
2543 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; | |
2544 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
15d45071 | 2545 | indirect_branch_prediction_barrier(); |
b80c76ec JM |
2546 | } |
2547 | ||
2548 | if (!already_loaded) { | |
59c58ceb | 2549 | void *gdt = get_current_gdt_ro(); |
b80c76ec JM |
2550 | unsigned long sysenter_esp; |
2551 | ||
2552 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); | |
92fe13be | 2553 | |
6aa8b732 AK |
2554 | /* |
2555 | * Linux uses per-cpu TSS and GDT, so set these when switching | |
e0c23063 | 2556 | * processors. See 22.2.4. |
6aa8b732 | 2557 | */ |
e0c23063 | 2558 | vmcs_writel(HOST_TR_BASE, |
72f5e08d | 2559 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
59c58ceb | 2560 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
6aa8b732 | 2561 | |
b7ffc44d AL |
2562 | /* |
2563 | * VM exits change the host TR limit to 0x67 after a VM | |
2564 | * exit. This is okay, since 0x67 covers everything except | |
2565 | * the IO bitmap and have have code to handle the IO bitmap | |
2566 | * being lost after a VM exit. | |
2567 | */ | |
2568 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); | |
2569 | ||
6aa8b732 AK |
2570 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
2571 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ | |
ff2c3a18 | 2572 | |
d462b819 | 2573 | vmx->loaded_vmcs->cpu = cpu; |
6aa8b732 | 2574 | } |
28b835d6 | 2575 | |
2680d6da OH |
2576 | /* Setup TSC multiplier */ |
2577 | if (kvm_has_tsc_control && | |
c95ba92a PF |
2578 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
2579 | decache_tsc_multiplier(vmx); | |
2680d6da | 2580 | |
28b835d6 | 2581 | vmx_vcpu_pi_load(vcpu, cpu); |
1be0e61c | 2582 | vmx->host_pkru = read_pkru(); |
74c55931 | 2583 | vmx->host_debugctlmsr = get_debugctlmsr(); |
28b835d6 FW |
2584 | } |
2585 | ||
2586 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) | |
2587 | { | |
2588 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2589 | ||
2590 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
2591 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
2592 | !kvm_vcpu_apicv_active(vcpu)) | |
28b835d6 FW |
2593 | return; |
2594 | ||
2595 | /* Set SN when the vCPU is preempted */ | |
2596 | if (vcpu->preempted) | |
2597 | pi_set_sn(pi_desc); | |
6aa8b732 AK |
2598 | } |
2599 | ||
2600 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) | |
2601 | { | |
28b835d6 FW |
2602 | vmx_vcpu_pi_put(vcpu); |
2603 | ||
a9b21b62 | 2604 | __vmx_load_host_state(to_vmx(vcpu)); |
6aa8b732 AK |
2605 | } |
2606 | ||
f244deed WL |
2607 | static bool emulation_required(struct kvm_vcpu *vcpu) |
2608 | { | |
2609 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); | |
2610 | } | |
2611 | ||
edcafe3c AK |
2612 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
2613 | ||
fe3ef05c NHE |
2614 | /* |
2615 | * Return the cr0 value that a nested guest would read. This is a combination | |
2616 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by | |
2617 | * its hypervisor (cr0_read_shadow). | |
2618 | */ | |
2619 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) | |
2620 | { | |
2621 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | | |
2622 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); | |
2623 | } | |
2624 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) | |
2625 | { | |
2626 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | | |
2627 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); | |
2628 | } | |
2629 | ||
6aa8b732 AK |
2630 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
2631 | { | |
78ac8b47 | 2632 | unsigned long rflags, save_rflags; |
345dcaa8 | 2633 | |
6de12732 AK |
2634 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
2635 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); | |
2636 | rflags = vmcs_readl(GUEST_RFLAGS); | |
2637 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
2638 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2639 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; | |
2640 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2641 | } | |
2642 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 | 2643 | } |
6de12732 | 2644 | return to_vmx(vcpu)->rflags; |
6aa8b732 AK |
2645 | } |
2646 | ||
2647 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
2648 | { | |
f244deed WL |
2649 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
2650 | ||
6de12732 AK |
2651 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
2652 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 AK |
2653 | if (to_vmx(vcpu)->rmode.vm86_active) { |
2654 | to_vmx(vcpu)->rmode.save_rflags = rflags; | |
053de044 | 2655 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
78ac8b47 | 2656 | } |
6aa8b732 | 2657 | vmcs_writel(GUEST_RFLAGS, rflags); |
f244deed WL |
2658 | |
2659 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) | |
2660 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); | |
6aa8b732 AK |
2661 | } |
2662 | ||
37ccdcbe | 2663 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
2809f5d2 GC |
2664 | { |
2665 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2666 | int ret = 0; | |
2667 | ||
2668 | if (interruptibility & GUEST_INTR_STATE_STI) | |
48005f64 | 2669 | ret |= KVM_X86_SHADOW_INT_STI; |
2809f5d2 | 2670 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
48005f64 | 2671 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
2809f5d2 | 2672 | |
37ccdcbe | 2673 | return ret; |
2809f5d2 GC |
2674 | } |
2675 | ||
2676 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) | |
2677 | { | |
2678 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2679 | u32 interruptibility = interruptibility_old; | |
2680 | ||
2681 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); | |
2682 | ||
48005f64 | 2683 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
2809f5d2 | 2684 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
48005f64 | 2685 | else if (mask & KVM_X86_SHADOW_INT_STI) |
2809f5d2 GC |
2686 | interruptibility |= GUEST_INTR_STATE_STI; |
2687 | ||
2688 | if ((interruptibility != interruptibility_old)) | |
2689 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); | |
2690 | } | |
2691 | ||
6aa8b732 AK |
2692 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
2693 | { | |
2694 | unsigned long rip; | |
6aa8b732 | 2695 | |
5fdbf976 | 2696 | rip = kvm_rip_read(vcpu); |
6aa8b732 | 2697 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
5fdbf976 | 2698 | kvm_rip_write(vcpu, rip); |
6aa8b732 | 2699 | |
2809f5d2 GC |
2700 | /* skipping an emulated instruction also counts */ |
2701 | vmx_set_interrupt_shadow(vcpu, 0); | |
6aa8b732 AK |
2702 | } |
2703 | ||
b96fb439 PB |
2704 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
2705 | unsigned long exit_qual) | |
2706 | { | |
2707 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
2708 | unsigned int nr = vcpu->arch.exception.nr; | |
2709 | u32 intr_info = nr | INTR_INFO_VALID_MASK; | |
2710 | ||
2711 | if (vcpu->arch.exception.has_error_code) { | |
2712 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; | |
2713 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; | |
2714 | } | |
2715 | ||
2716 | if (kvm_exception_is_soft(nr)) | |
2717 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; | |
2718 | else | |
2719 | intr_info |= INTR_TYPE_HARD_EXCEPTION; | |
2720 | ||
2721 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && | |
2722 | vmx_get_nmi_mask(vcpu)) | |
2723 | intr_info |= INTR_INFO_UNBLOCK_NMI; | |
2724 | ||
2725 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); | |
2726 | } | |
2727 | ||
0b6ac343 NHE |
2728 | /* |
2729 | * KVM wants to inject page-faults which it got to the guest. This function | |
2730 | * checks whether in a nested guest, we need to inject them to L1 or L2. | |
0b6ac343 | 2731 | */ |
bfcf83b1 | 2732 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
0b6ac343 NHE |
2733 | { |
2734 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
adfe20fb | 2735 | unsigned int nr = vcpu->arch.exception.nr; |
0b6ac343 | 2736 | |
b96fb439 PB |
2737 | if (nr == PF_VECTOR) { |
2738 | if (vcpu->arch.exception.nested_apf) { | |
bfcf83b1 | 2739 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
b96fb439 PB |
2740 | return 1; |
2741 | } | |
2742 | /* | |
2743 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. | |
2744 | * The fix is to add the ancillary datum (CR2 or DR6) to structs | |
2745 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 | |
2746 | * can be written only when inject_pending_event runs. This should be | |
2747 | * conditional on a new capability---if the capability is disabled, | |
2748 | * kvm_multiple_exception would write the ancillary information to | |
2749 | * CR2 or DR6, for backwards ABI-compatibility. | |
2750 | */ | |
2751 | if (nested_vmx_is_page_fault_vmexit(vmcs12, | |
2752 | vcpu->arch.exception.error_code)) { | |
bfcf83b1 | 2753 | *exit_qual = vcpu->arch.cr2; |
b96fb439 PB |
2754 | return 1; |
2755 | } | |
2756 | } else { | |
b96fb439 | 2757 | if (vmcs12->exception_bitmap & (1u << nr)) { |
bfcf83b1 WL |
2758 | if (nr == DB_VECTOR) |
2759 | *exit_qual = vcpu->arch.dr6; | |
2760 | else | |
2761 | *exit_qual = 0; | |
b96fb439 PB |
2762 | return 1; |
2763 | } | |
adfe20fb WL |
2764 | } |
2765 | ||
b96fb439 | 2766 | return 0; |
0b6ac343 NHE |
2767 | } |
2768 | ||
caa057a2 WL |
2769 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
2770 | { | |
2771 | /* | |
2772 | * Ensure that we clear the HLT state in the VMCS. We don't need to | |
2773 | * explicitly skip the instruction because if the HLT state is set, | |
2774 | * then the instruction is already executing and RIP has already been | |
2775 | * advanced. | |
2776 | */ | |
2777 | if (kvm_hlt_in_guest(vcpu->kvm) && | |
2778 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) | |
2779 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); | |
2780 | } | |
2781 | ||
cfcd20e5 | 2782 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
298101da | 2783 | { |
77ab6db0 | 2784 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
cfcd20e5 WL |
2785 | unsigned nr = vcpu->arch.exception.nr; |
2786 | bool has_error_code = vcpu->arch.exception.has_error_code; | |
cfcd20e5 | 2787 | u32 error_code = vcpu->arch.exception.error_code; |
8ab2d2e2 | 2788 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
77ab6db0 | 2789 | |
8ab2d2e2 | 2790 | if (has_error_code) { |
77ab6db0 | 2791 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
8ab2d2e2 JK |
2792 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
2793 | } | |
77ab6db0 | 2794 | |
7ffd92c5 | 2795 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
2796 | int inc_eip = 0; |
2797 | if (kvm_exception_is_soft(nr)) | |
2798 | inc_eip = vcpu->arch.event_exit_inst_len; | |
2799 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) | |
a92601bb | 2800 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
77ab6db0 JK |
2801 | return; |
2802 | } | |
2803 | ||
add5ff7a SC |
2804 | WARN_ON_ONCE(vmx->emulation_required); |
2805 | ||
66fd3f7f GN |
2806 | if (kvm_exception_is_soft(nr)) { |
2807 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
2808 | vmx->vcpu.arch.event_exit_inst_len); | |
8ab2d2e2 JK |
2809 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
2810 | } else | |
2811 | intr_info |= INTR_TYPE_HARD_EXCEPTION; | |
2812 | ||
2813 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); | |
caa057a2 WL |
2814 | |
2815 | vmx_clear_hlt(vcpu); | |
298101da AK |
2816 | } |
2817 | ||
4e47c7a6 SY |
2818 | static bool vmx_rdtscp_supported(void) |
2819 | { | |
2820 | return cpu_has_vmx_rdtscp(); | |
2821 | } | |
2822 | ||
ad756a16 MJ |
2823 | static bool vmx_invpcid_supported(void) |
2824 | { | |
2825 | return cpu_has_vmx_invpcid() && enable_ept; | |
2826 | } | |
2827 | ||
a75beee6 ED |
2828 | /* |
2829 | * Swap MSR entry in host/guest MSR entry array. | |
2830 | */ | |
8b9cf98c | 2831 | static void move_msr_up(struct vcpu_vmx *vmx, int from, int to) |
a75beee6 | 2832 | { |
26bb0981 | 2833 | struct shared_msr_entry tmp; |
a2fa3e9f GH |
2834 | |
2835 | tmp = vmx->guest_msrs[to]; | |
2836 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; | |
2837 | vmx->guest_msrs[from] = tmp; | |
a75beee6 ED |
2838 | } |
2839 | ||
e38aea3e AK |
2840 | /* |
2841 | * Set up the vmcs to automatically save and restore system | |
2842 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy | |
2843 | * mode, as fiddling with msrs is very expensive. | |
2844 | */ | |
8b9cf98c | 2845 | static void setup_msrs(struct vcpu_vmx *vmx) |
e38aea3e | 2846 | { |
26bb0981 | 2847 | int save_nmsrs, index; |
e38aea3e | 2848 | |
a75beee6 ED |
2849 | save_nmsrs = 0; |
2850 | #ifdef CONFIG_X86_64 | |
8b9cf98c | 2851 | if (is_long_mode(&vmx->vcpu)) { |
8b9cf98c | 2852 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
a75beee6 | 2853 | if (index >= 0) |
8b9cf98c RR |
2854 | move_msr_up(vmx, index, save_nmsrs++); |
2855 | index = __find_msr_index(vmx, MSR_LSTAR); | |
a75beee6 | 2856 | if (index >= 0) |
8b9cf98c RR |
2857 | move_msr_up(vmx, index, save_nmsrs++); |
2858 | index = __find_msr_index(vmx, MSR_CSTAR); | |
a75beee6 | 2859 | if (index >= 0) |
8b9cf98c | 2860 | move_msr_up(vmx, index, save_nmsrs++); |
4e47c7a6 | 2861 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
d6321d49 | 2862 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
4e47c7a6 | 2863 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 | 2864 | /* |
8c06585d | 2865 | * MSR_STAR is only needed on long mode guests, and only |
a75beee6 ED |
2866 | * if efer.sce is enabled. |
2867 | */ | |
8c06585d | 2868 | index = __find_msr_index(vmx, MSR_STAR); |
f6801dff | 2869 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
8b9cf98c | 2870 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 ED |
2871 | } |
2872 | #endif | |
92c0d900 AK |
2873 | index = __find_msr_index(vmx, MSR_EFER); |
2874 | if (index >= 0 && update_transition_efer(vmx, index)) | |
26bb0981 | 2875 | move_msr_up(vmx, index, save_nmsrs++); |
e38aea3e | 2876 | |
26bb0981 | 2877 | vmx->save_nmsrs = save_nmsrs; |
5897297b | 2878 | |
8d14695f | 2879 | if (cpu_has_vmx_msr_bitmap()) |
904e14fb | 2880 | vmx_update_msr_bitmap(&vmx->vcpu); |
e38aea3e AK |
2881 | } |
2882 | ||
e79f245d | 2883 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
6aa8b732 | 2884 | { |
e79f245d | 2885 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
6aa8b732 | 2886 | |
e79f245d KA |
2887 | if (is_guest_mode(vcpu) && |
2888 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) | |
2889 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; | |
2890 | ||
2891 | return vcpu->arch.tsc_offset; | |
6aa8b732 AK |
2892 | } |
2893 | ||
2894 | /* | |
99e3e30a | 2895 | * writes 'offset' into guest's timestamp counter offset register |
6aa8b732 | 2896 | */ |
99e3e30a | 2897 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
6aa8b732 | 2898 | { |
27fc51b2 | 2899 | if (is_guest_mode(vcpu)) { |
7991825b | 2900 | /* |
27fc51b2 NHE |
2901 | * We're here if L1 chose not to trap WRMSR to TSC. According |
2902 | * to the spec, this should set L1's TSC; The offset that L1 | |
2903 | * set for L2 remains unchanged, and still needs to be added | |
2904 | * to the newly set TSC to get L2's TSC. | |
7991825b | 2905 | */ |
27fc51b2 | 2906 | struct vmcs12 *vmcs12; |
27fc51b2 NHE |
2907 | /* recalculate vmcs02.TSC_OFFSET: */ |
2908 | vmcs12 = get_vmcs12(vcpu); | |
2909 | vmcs_write64(TSC_OFFSET, offset + | |
2910 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? | |
2911 | vmcs12->tsc_offset : 0)); | |
2912 | } else { | |
489223ed YY |
2913 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
2914 | vmcs_read64(TSC_OFFSET), offset); | |
27fc51b2 NHE |
2915 | vmcs_write64(TSC_OFFSET, offset); |
2916 | } | |
6aa8b732 AK |
2917 | } |
2918 | ||
801d3424 NHE |
2919 | /* |
2920 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX | |
2921 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for | |
2922 | * all guests if the "nested" module option is off, and can also be disabled | |
2923 | * for a single guest by disabling its VMX cpuid bit. | |
2924 | */ | |
2925 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) | |
2926 | { | |
d6321d49 | 2927 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
801d3424 NHE |
2928 | } |
2929 | ||
b87a51ae NHE |
2930 | /* |
2931 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be | |
2932 | * returned for the various VMX controls MSRs when nested VMX is enabled. | |
2933 | * The same values should also be used to verify that vmcs12 control fields are | |
2934 | * valid during nested entry from L1 to L2. | |
2935 | * Each of these control msrs has a low and high 32-bit half: A low bit is on | |
2936 | * if the corresponding bit in the (32-bit) control field *must* be on, and a | |
2937 | * bit in the high half is on if the corresponding bit in the control field | |
2938 | * may be on. See also vmx_control_verify(). | |
b87a51ae | 2939 | */ |
6677f3da | 2940 | static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv) |
b87a51ae | 2941 | { |
1389309c PB |
2942 | if (!nested) { |
2943 | memset(msrs, 0, sizeof(*msrs)); | |
2944 | return; | |
2945 | } | |
2946 | ||
b87a51ae NHE |
2947 | /* |
2948 | * Note that as a general rule, the high half of the MSRs (bits in | |
2949 | * the control fields which may be 1) should be initialized by the | |
2950 | * intersection of the underlying hardware's MSR (i.e., features which | |
2951 | * can be supported) and the list of features we want to expose - | |
2952 | * because they are known to be properly supported in our code. | |
2953 | * Also, usually, the low half of the MSRs (bits which must be 1) can | |
2954 | * be set to 0, meaning that L1 may turn off any of these bits. The | |
2955 | * reason is that if one of these bits is necessary, it will appear | |
2956 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control | |
2957 | * fields of vmcs01 and vmcs02, will turn these bits off - and | |
7313c698 | 2958 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
b87a51ae NHE |
2959 | * These rules have exceptions below. |
2960 | */ | |
2961 | ||
2962 | /* pin-based controls */ | |
eabeaacc | 2963 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
6677f3da PB |
2964 | msrs->pinbased_ctls_low, |
2965 | msrs->pinbased_ctls_high); | |
2966 | msrs->pinbased_ctls_low |= | |
b9c237bb | 2967 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
6677f3da | 2968 | msrs->pinbased_ctls_high &= |
b9c237bb WV |
2969 | PIN_BASED_EXT_INTR_MASK | |
2970 | PIN_BASED_NMI_EXITING | | |
1389309c PB |
2971 | PIN_BASED_VIRTUAL_NMIS | |
2972 | (apicv ? PIN_BASED_POSTED_INTR : 0); | |
6677f3da | 2973 | msrs->pinbased_ctls_high |= |
b9c237bb | 2974 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
0238ea91 | 2975 | PIN_BASED_VMX_PREEMPTION_TIMER; |
b87a51ae | 2976 | |
3dbcd8da | 2977 | /* exit controls */ |
c0dfee58 | 2978 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
6677f3da PB |
2979 | msrs->exit_ctls_low, |
2980 | msrs->exit_ctls_high); | |
2981 | msrs->exit_ctls_low = | |
b9c237bb | 2982 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
e0ba1a6f | 2983 | |
6677f3da | 2984 | msrs->exit_ctls_high &= |
b87a51ae | 2985 | #ifdef CONFIG_X86_64 |
c0dfee58 | 2986 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
b87a51ae | 2987 | #endif |
f4124500 | 2988 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
6677f3da | 2989 | msrs->exit_ctls_high |= |
b9c237bb | 2990 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
f4124500 | 2991 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
e0ba1a6f BD |
2992 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
2993 | ||
a87036ad | 2994 | if (kvm_mpx_supported()) |
6677f3da | 2995 | msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
b87a51ae | 2996 | |
2996fca0 | 2997 | /* We support free control of debug control saving. */ |
6677f3da | 2998 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
2996fca0 | 2999 | |
b87a51ae NHE |
3000 | /* entry controls */ |
3001 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, | |
6677f3da PB |
3002 | msrs->entry_ctls_low, |
3003 | msrs->entry_ctls_high); | |
3004 | msrs->entry_ctls_low = | |
b9c237bb | 3005 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
6677f3da | 3006 | msrs->entry_ctls_high &= |
57435349 JK |
3007 | #ifdef CONFIG_X86_64 |
3008 | VM_ENTRY_IA32E_MODE | | |
3009 | #endif | |
3010 | VM_ENTRY_LOAD_IA32_PAT; | |
6677f3da | 3011 | msrs->entry_ctls_high |= |
b9c237bb | 3012 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
a87036ad | 3013 | if (kvm_mpx_supported()) |
6677f3da | 3014 | msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
57435349 | 3015 | |
2996fca0 | 3016 | /* We support free control of debug control loading. */ |
6677f3da | 3017 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
2996fca0 | 3018 | |
b87a51ae NHE |
3019 | /* cpu-based controls */ |
3020 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, | |
6677f3da PB |
3021 | msrs->procbased_ctls_low, |
3022 | msrs->procbased_ctls_high); | |
3023 | msrs->procbased_ctls_low = | |
b9c237bb | 3024 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
6677f3da | 3025 | msrs->procbased_ctls_high &= |
a294c9bb JK |
3026 | CPU_BASED_VIRTUAL_INTR_PENDING | |
3027 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | | |
b87a51ae NHE |
3028 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
3029 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | | |
3030 | CPU_BASED_CR3_STORE_EXITING | | |
3031 | #ifdef CONFIG_X86_64 | |
3032 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | | |
3033 | #endif | |
3034 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | | |
5f3d45e7 MD |
3035 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
3036 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | | |
3037 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | | |
3038 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
b87a51ae NHE |
3039 | /* |
3040 | * We can allow some features even when not supported by the | |
3041 | * hardware. For example, L1 can specify an MSR bitmap - and we | |
3042 | * can use it to avoid exits to L1 - even when L0 runs L2 | |
3043 | * without MSR bitmaps. | |
3044 | */ | |
6677f3da | 3045 | msrs->procbased_ctls_high |= |
b9c237bb | 3046 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
560b7ee1 | 3047 | CPU_BASED_USE_MSR_BITMAPS; |
b87a51ae | 3048 | |
3dcdf3ec | 3049 | /* We support free control of CR3 access interception. */ |
6677f3da | 3050 | msrs->procbased_ctls_low &= |
3dcdf3ec JK |
3051 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
3052 | ||
80154d77 PB |
3053 | /* |
3054 | * secondary cpu-based controls. Do not include those that | |
3055 | * depend on CPUID bits, they are added later by vmx_cpuid_update. | |
3056 | */ | |
b87a51ae | 3057 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
6677f3da PB |
3058 | msrs->secondary_ctls_low, |
3059 | msrs->secondary_ctls_high); | |
3060 | msrs->secondary_ctls_low = 0; | |
3061 | msrs->secondary_ctls_high &= | |
d6851fbe | 3062 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
1b07304c | 3063 | SECONDARY_EXEC_DESC | |
f2b93280 | 3064 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
82f0dd4b | 3065 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
608406e2 | 3066 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
3db13480 | 3067 | SECONDARY_EXEC_WBINVD_EXITING; |
c18911a2 | 3068 | |
afa61f75 NHE |
3069 | if (enable_ept) { |
3070 | /* nested EPT: emulate EPT also to L1 */ | |
6677f3da | 3071 | msrs->secondary_ctls_high |= |
0790ec17 | 3072 | SECONDARY_EXEC_ENABLE_EPT; |
6677f3da | 3073 | msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
7db74265 | 3074 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
02120c45 | 3075 | if (cpu_has_vmx_ept_execute_only()) |
6677f3da | 3076 | msrs->ept_caps |= |
02120c45 | 3077 | VMX_EPT_EXECUTE_ONLY_BIT; |
6677f3da PB |
3078 | msrs->ept_caps &= vmx_capability.ept; |
3079 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | | |
7db74265 PB |
3080 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
3081 | VMX_EPT_1GB_PAGE_BIT; | |
03efce6f | 3082 | if (enable_ept_ad_bits) { |
6677f3da | 3083 | msrs->secondary_ctls_high |= |
03efce6f | 3084 | SECONDARY_EXEC_ENABLE_PML; |
6677f3da | 3085 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
03efce6f | 3086 | } |
1c13bffd | 3087 | } |
afa61f75 | 3088 | |
27c42a1b | 3089 | if (cpu_has_vmx_vmfunc()) { |
6677f3da | 3090 | msrs->secondary_ctls_high |= |
27c42a1b | 3091 | SECONDARY_EXEC_ENABLE_VMFUNC; |
41ab9372 BD |
3092 | /* |
3093 | * Advertise EPTP switching unconditionally | |
3094 | * since we emulate it | |
3095 | */ | |
575b3a2c | 3096 | if (enable_ept) |
6677f3da | 3097 | msrs->vmfunc_controls = |
575b3a2c | 3098 | VMX_VMFUNC_EPTP_SWITCHING; |
27c42a1b BD |
3099 | } |
3100 | ||
ef697a71 PB |
3101 | /* |
3102 | * Old versions of KVM use the single-context version without | |
3103 | * checking for support, so declare that it is supported even | |
3104 | * though it is treated as global context. The alternative is | |
3105 | * not failing the single-context invvpid, and it is worse. | |
3106 | */ | |
63cb6d5f | 3107 | if (enable_vpid) { |
6677f3da | 3108 | msrs->secondary_ctls_high |= |
63cb6d5f | 3109 | SECONDARY_EXEC_ENABLE_VPID; |
6677f3da | 3110 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
bcdde302 | 3111 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
1c13bffd | 3112 | } |
99b83ac8 | 3113 | |
0790ec17 | 3114 | if (enable_unrestricted_guest) |
6677f3da | 3115 | msrs->secondary_ctls_high |= |
0790ec17 RK |
3116 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
3117 | ||
c18911a2 | 3118 | /* miscellaneous data */ |
b9c237bb | 3119 | rdmsr(MSR_IA32_VMX_MISC, |
6677f3da PB |
3120 | msrs->misc_low, |
3121 | msrs->misc_high); | |
3122 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; | |
3123 | msrs->misc_low |= | |
b9c237bb | 3124 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
f4124500 | 3125 | VMX_MISC_ACTIVITY_HLT; |
6677f3da | 3126 | msrs->misc_high = 0; |
62cc6b9d DM |
3127 | |
3128 | /* | |
3129 | * This MSR reports some information about VMX support. We | |
3130 | * should return information about the VMX we emulate for the | |
3131 | * guest, and the VMCS structure we give it - not about the | |
3132 | * VMX support of the underlying hardware. | |
3133 | */ | |
6677f3da | 3134 | msrs->basic = |
62cc6b9d DM |
3135 | VMCS12_REVISION | |
3136 | VMX_BASIC_TRUE_CTLS | | |
3137 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | | |
3138 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); | |
3139 | ||
3140 | if (cpu_has_vmx_basic_inout()) | |
6677f3da | 3141 | msrs->basic |= VMX_BASIC_INOUT; |
62cc6b9d DM |
3142 | |
3143 | /* | |
8322ebbb | 3144 | * These MSRs specify bits which the guest must keep fixed on |
62cc6b9d DM |
3145 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
3146 | * We picked the standard core2 setting. | |
3147 | */ | |
3148 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) | |
3149 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE | |
6677f3da PB |
3150 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
3151 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; | |
8322ebbb DM |
3152 | |
3153 | /* These MSRs specify bits which the guest must keep fixed off. */ | |
6677f3da PB |
3154 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
3155 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); | |
62cc6b9d DM |
3156 | |
3157 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ | |
6677f3da | 3158 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
b87a51ae NHE |
3159 | } |
3160 | ||
3899152c DM |
3161 | /* |
3162 | * if fixed0[i] == 1: val[i] must be 1 | |
3163 | * if fixed1[i] == 0: val[i] must be 0 | |
3164 | */ | |
3165 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) | |
3166 | { | |
3167 | return ((val & fixed1) | fixed0) == val; | |
b87a51ae NHE |
3168 | } |
3169 | ||
3170 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) | |
3171 | { | |
3899152c | 3172 | return fixed_bits_valid(control, low, high); |
b87a51ae NHE |
3173 | } |
3174 | ||
3175 | static inline u64 vmx_control_msr(u32 low, u32 high) | |
3176 | { | |
3177 | return low | ((u64)high << 32); | |
3178 | } | |
3179 | ||
62cc6b9d DM |
3180 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
3181 | { | |
3182 | superset &= mask; | |
3183 | subset &= mask; | |
3184 | ||
3185 | return (superset | subset) == superset; | |
3186 | } | |
3187 | ||
3188 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) | |
3189 | { | |
3190 | const u64 feature_and_reserved = | |
3191 | /* feature (except bit 48; see below) */ | |
3192 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | | |
3193 | /* reserved */ | |
3194 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); | |
6677f3da | 3195 | u64 vmx_basic = vmx->nested.msrs.basic; |
62cc6b9d DM |
3196 | |
3197 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) | |
3198 | return -EINVAL; | |
3199 | ||
3200 | /* | |
3201 | * KVM does not emulate a version of VMX that constrains physical | |
3202 | * addresses of VMX structures (e.g. VMCS) to 32-bits. | |
3203 | */ | |
3204 | if (data & BIT_ULL(48)) | |
3205 | return -EINVAL; | |
3206 | ||
3207 | if (vmx_basic_vmcs_revision_id(vmx_basic) != | |
3208 | vmx_basic_vmcs_revision_id(data)) | |
3209 | return -EINVAL; | |
3210 | ||
3211 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) | |
3212 | return -EINVAL; | |
3213 | ||
6677f3da | 3214 | vmx->nested.msrs.basic = data; |
62cc6b9d DM |
3215 | return 0; |
3216 | } | |
3217 | ||
3218 | static int | |
3219 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
3220 | { | |
3221 | u64 supported; | |
3222 | u32 *lowp, *highp; | |
3223 | ||
3224 | switch (msr_index) { | |
3225 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
6677f3da PB |
3226 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
3227 | highp = &vmx->nested.msrs.pinbased_ctls_high; | |
62cc6b9d DM |
3228 | break; |
3229 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
6677f3da PB |
3230 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
3231 | highp = &vmx->nested.msrs.procbased_ctls_high; | |
62cc6b9d DM |
3232 | break; |
3233 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
6677f3da PB |
3234 | lowp = &vmx->nested.msrs.exit_ctls_low; |
3235 | highp = &vmx->nested.msrs.exit_ctls_high; | |
62cc6b9d DM |
3236 | break; |
3237 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
6677f3da PB |
3238 | lowp = &vmx->nested.msrs.entry_ctls_low; |
3239 | highp = &vmx->nested.msrs.entry_ctls_high; | |
62cc6b9d DM |
3240 | break; |
3241 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
6677f3da PB |
3242 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
3243 | highp = &vmx->nested.msrs.secondary_ctls_high; | |
62cc6b9d DM |
3244 | break; |
3245 | default: | |
3246 | BUG(); | |
3247 | } | |
3248 | ||
3249 | supported = vmx_control_msr(*lowp, *highp); | |
3250 | ||
3251 | /* Check must-be-1 bits are still 1. */ | |
3252 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) | |
3253 | return -EINVAL; | |
3254 | ||
3255 | /* Check must-be-0 bits are still 0. */ | |
3256 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) | |
3257 | return -EINVAL; | |
3258 | ||
3259 | *lowp = data; | |
3260 | *highp = data >> 32; | |
3261 | return 0; | |
3262 | } | |
3263 | ||
3264 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) | |
3265 | { | |
3266 | const u64 feature_and_reserved_bits = | |
3267 | /* feature */ | |
3268 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | | |
3269 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | | |
3270 | /* reserved */ | |
3271 | GENMASK_ULL(13, 9) | BIT_ULL(31); | |
3272 | u64 vmx_misc; | |
3273 | ||
6677f3da PB |
3274 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
3275 | vmx->nested.msrs.misc_high); | |
62cc6b9d DM |
3276 | |
3277 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) | |
3278 | return -EINVAL; | |
3279 | ||
6677f3da | 3280 | if ((vmx->nested.msrs.pinbased_ctls_high & |
62cc6b9d DM |
3281 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
3282 | vmx_misc_preemption_timer_rate(data) != | |
3283 | vmx_misc_preemption_timer_rate(vmx_misc)) | |
3284 | return -EINVAL; | |
3285 | ||
3286 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) | |
3287 | return -EINVAL; | |
3288 | ||
3289 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) | |
3290 | return -EINVAL; | |
3291 | ||
3292 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) | |
3293 | return -EINVAL; | |
3294 | ||
6677f3da PB |
3295 | vmx->nested.msrs.misc_low = data; |
3296 | vmx->nested.msrs.misc_high = data >> 32; | |
62cc6b9d DM |
3297 | return 0; |
3298 | } | |
3299 | ||
3300 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) | |
3301 | { | |
3302 | u64 vmx_ept_vpid_cap; | |
3303 | ||
6677f3da PB |
3304 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
3305 | vmx->nested.msrs.vpid_caps); | |
62cc6b9d DM |
3306 | |
3307 | /* Every bit is either reserved or a feature bit. */ | |
3308 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) | |
3309 | return -EINVAL; | |
3310 | ||
6677f3da PB |
3311 | vmx->nested.msrs.ept_caps = data; |
3312 | vmx->nested.msrs.vpid_caps = data >> 32; | |
62cc6b9d DM |
3313 | return 0; |
3314 | } | |
3315 | ||
3316 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
3317 | { | |
3318 | u64 *msr; | |
3319 | ||
3320 | switch (msr_index) { | |
3321 | case MSR_IA32_VMX_CR0_FIXED0: | |
6677f3da | 3322 | msr = &vmx->nested.msrs.cr0_fixed0; |
62cc6b9d DM |
3323 | break; |
3324 | case MSR_IA32_VMX_CR4_FIXED0: | |
6677f3da | 3325 | msr = &vmx->nested.msrs.cr4_fixed0; |
62cc6b9d DM |
3326 | break; |
3327 | default: | |
3328 | BUG(); | |
3329 | } | |
3330 | ||
3331 | /* | |
3332 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) | |
3333 | * must be 1 in the restored value. | |
3334 | */ | |
3335 | if (!is_bitwise_subset(data, *msr, -1ULL)) | |
3336 | return -EINVAL; | |
3337 | ||
3338 | *msr = data; | |
3339 | return 0; | |
3340 | } | |
3341 | ||
3342 | /* | |
3343 | * Called when userspace is restoring VMX MSRs. | |
3344 | * | |
3345 | * Returns 0 on success, non-0 otherwise. | |
3346 | */ | |
3347 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
b87a51ae | 3348 | { |
b9c237bb WV |
3349 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3350 | ||
b87a51ae | 3351 | switch (msr_index) { |
b87a51ae | 3352 | case MSR_IA32_VMX_BASIC: |
62cc6b9d DM |
3353 | return vmx_restore_vmx_basic(vmx, data); |
3354 | case MSR_IA32_VMX_PINBASED_CTLS: | |
3355 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
3356 | case MSR_IA32_VMX_EXIT_CTLS: | |
3357 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b87a51ae | 3358 | /* |
62cc6b9d DM |
3359 | * The "non-true" VMX capability MSRs are generated from the |
3360 | * "true" MSRs, so we do not support restoring them directly. | |
3361 | * | |
3362 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace | |
3363 | * should restore the "true" MSRs with the must-be-1 bits | |
3364 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND | |
3365 | * DEFAULT SETTINGS". | |
b87a51ae | 3366 | */ |
62cc6b9d DM |
3367 | return -EINVAL; |
3368 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3369 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3370 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3371 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3372 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
3373 | return vmx_restore_control_msr(vmx, msr_index, data); | |
3374 | case MSR_IA32_VMX_MISC: | |
3375 | return vmx_restore_vmx_misc(vmx, data); | |
3376 | case MSR_IA32_VMX_CR0_FIXED0: | |
3377 | case MSR_IA32_VMX_CR4_FIXED0: | |
3378 | return vmx_restore_fixed0_msr(vmx, msr_index, data); | |
3379 | case MSR_IA32_VMX_CR0_FIXED1: | |
3380 | case MSR_IA32_VMX_CR4_FIXED1: | |
3381 | /* | |
3382 | * These MSRs are generated based on the vCPU's CPUID, so we | |
3383 | * do not support restoring them directly. | |
3384 | */ | |
3385 | return -EINVAL; | |
3386 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
3387 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); | |
3388 | case MSR_IA32_VMX_VMCS_ENUM: | |
6677f3da | 3389 | vmx->nested.msrs.vmcs_enum = data; |
62cc6b9d DM |
3390 | return 0; |
3391 | default: | |
b87a51ae | 3392 | /* |
62cc6b9d | 3393 | * The rest of the VMX capability MSRs do not support restore. |
b87a51ae | 3394 | */ |
62cc6b9d DM |
3395 | return -EINVAL; |
3396 | } | |
3397 | } | |
3398 | ||
3399 | /* Returns 0 on success, non-0 otherwise. */ | |
6677f3da | 3400 | static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
62cc6b9d | 3401 | { |
62cc6b9d DM |
3402 | switch (msr_index) { |
3403 | case MSR_IA32_VMX_BASIC: | |
6677f3da | 3404 | *pdata = msrs->basic; |
b87a51ae NHE |
3405 | break; |
3406 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3407 | case MSR_IA32_VMX_PINBASED_CTLS: | |
b9c237bb | 3408 | *pdata = vmx_control_msr( |
6677f3da PB |
3409 | msrs->pinbased_ctls_low, |
3410 | msrs->pinbased_ctls_high); | |
0115f9cb DM |
3411 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
3412 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3413 | break; |
3414 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3415 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
b9c237bb | 3416 | *pdata = vmx_control_msr( |
6677f3da PB |
3417 | msrs->procbased_ctls_low, |
3418 | msrs->procbased_ctls_high); | |
0115f9cb DM |
3419 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
3420 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3421 | break; |
3422 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3423 | case MSR_IA32_VMX_EXIT_CTLS: | |
b9c237bb | 3424 | *pdata = vmx_control_msr( |
6677f3da PB |
3425 | msrs->exit_ctls_low, |
3426 | msrs->exit_ctls_high); | |
0115f9cb DM |
3427 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
3428 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3429 | break; |
3430 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3431 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b9c237bb | 3432 | *pdata = vmx_control_msr( |
6677f3da PB |
3433 | msrs->entry_ctls_low, |
3434 | msrs->entry_ctls_high); | |
0115f9cb DM |
3435 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
3436 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3437 | break; |
3438 | case MSR_IA32_VMX_MISC: | |
b9c237bb | 3439 | *pdata = vmx_control_msr( |
6677f3da PB |
3440 | msrs->misc_low, |
3441 | msrs->misc_high); | |
b87a51ae | 3442 | break; |
b87a51ae | 3443 | case MSR_IA32_VMX_CR0_FIXED0: |
6677f3da | 3444 | *pdata = msrs->cr0_fixed0; |
b87a51ae NHE |
3445 | break; |
3446 | case MSR_IA32_VMX_CR0_FIXED1: | |
6677f3da | 3447 | *pdata = msrs->cr0_fixed1; |
b87a51ae NHE |
3448 | break; |
3449 | case MSR_IA32_VMX_CR4_FIXED0: | |
6677f3da | 3450 | *pdata = msrs->cr4_fixed0; |
b87a51ae NHE |
3451 | break; |
3452 | case MSR_IA32_VMX_CR4_FIXED1: | |
6677f3da | 3453 | *pdata = msrs->cr4_fixed1; |
b87a51ae NHE |
3454 | break; |
3455 | case MSR_IA32_VMX_VMCS_ENUM: | |
6677f3da | 3456 | *pdata = msrs->vmcs_enum; |
b87a51ae NHE |
3457 | break; |
3458 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
b9c237bb | 3459 | *pdata = vmx_control_msr( |
6677f3da PB |
3460 | msrs->secondary_ctls_low, |
3461 | msrs->secondary_ctls_high); | |
b87a51ae NHE |
3462 | break; |
3463 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
6677f3da PB |
3464 | *pdata = msrs->ept_caps | |
3465 | ((u64)msrs->vpid_caps << 32); | |
b87a51ae | 3466 | break; |
27c42a1b | 3467 | case MSR_IA32_VMX_VMFUNC: |
6677f3da | 3468 | *pdata = msrs->vmfunc_controls; |
27c42a1b | 3469 | break; |
b87a51ae | 3470 | default: |
b87a51ae | 3471 | return 1; |
b3897a49 NHE |
3472 | } |
3473 | ||
b87a51ae NHE |
3474 | return 0; |
3475 | } | |
3476 | ||
37e4c997 HZ |
3477 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
3478 | uint64_t val) | |
3479 | { | |
3480 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; | |
3481 | ||
3482 | return !(val & ~valid_bits); | |
3483 | } | |
3484 | ||
801e459a TL |
3485 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
3486 | { | |
1389309c PB |
3487 | switch (msr->index) { |
3488 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
3489 | if (!nested) | |
3490 | return 1; | |
3491 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); | |
3492 | default: | |
3493 | return 1; | |
3494 | } | |
3495 | ||
3496 | return 0; | |
801e459a TL |
3497 | } |
3498 | ||
6aa8b732 AK |
3499 | /* |
3500 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
3501 | * Returns 0 on success, non-0 otherwise. | |
3502 | * Assumes vcpu_load() was already called. | |
3503 | */ | |
609e36d3 | 3504 | static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3505 | { |
a6cb099a | 3506 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 3507 | struct shared_msr_entry *msr; |
6aa8b732 | 3508 | |
609e36d3 | 3509 | switch (msr_info->index) { |
05b3e0c2 | 3510 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3511 | case MSR_FS_BASE: |
609e36d3 | 3512 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
6aa8b732 AK |
3513 | break; |
3514 | case MSR_GS_BASE: | |
609e36d3 | 3515 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
6aa8b732 | 3516 | break; |
44ea2b17 | 3517 | case MSR_KERNEL_GS_BASE: |
a6cb099a BP |
3518 | vmx_load_host_state(vmx); |
3519 | msr_info->data = vmx->msr_guest_kernel_gs_base; | |
44ea2b17 | 3520 | break; |
26bb0981 | 3521 | #endif |
6aa8b732 | 3522 | case MSR_EFER: |
609e36d3 | 3523 | return kvm_get_msr_common(vcpu, msr_info); |
d28b387f KA |
3524 | case MSR_IA32_SPEC_CTRL: |
3525 | if (!msr_info->host_initiated && | |
3526 | !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) && | |
da39556f | 3527 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) && |
9f65fb29 | 3528 | !guest_cpuid_has(vcpu, X86_FEATURE_SSBD)) |
d28b387f KA |
3529 | return 1; |
3530 | ||
3531 | msr_info->data = to_vmx(vcpu)->spec_ctrl; | |
3532 | break; | |
28c1c9fa KA |
3533 | case MSR_IA32_ARCH_CAPABILITIES: |
3534 | if (!msr_info->host_initiated && | |
3535 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) | |
3536 | return 1; | |
3537 | msr_info->data = to_vmx(vcpu)->arch_capabilities; | |
3538 | break; | |
6aa8b732 | 3539 | case MSR_IA32_SYSENTER_CS: |
609e36d3 | 3540 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
6aa8b732 AK |
3541 | break; |
3542 | case MSR_IA32_SYSENTER_EIP: | |
609e36d3 | 3543 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
6aa8b732 AK |
3544 | break; |
3545 | case MSR_IA32_SYSENTER_ESP: | |
609e36d3 | 3546 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
6aa8b732 | 3547 | break; |
0dd376e7 | 3548 | case MSR_IA32_BNDCFGS: |
691bd434 | 3549 | if (!kvm_mpx_supported() || |
d6321d49 RK |
3550 | (!msr_info->host_initiated && |
3551 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) | |
93c4adc7 | 3552 | return 1; |
609e36d3 | 3553 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
0dd376e7 | 3554 | break; |
c45dcc71 AR |
3555 | case MSR_IA32_MCG_EXT_CTL: |
3556 | if (!msr_info->host_initiated && | |
a6cb099a | 3557 | !(vmx->msr_ia32_feature_control & |
c45dcc71 | 3558 | FEATURE_CONTROL_LMCE)) |
cae50139 | 3559 | return 1; |
c45dcc71 AR |
3560 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
3561 | break; | |
cae50139 | 3562 | case MSR_IA32_FEATURE_CONTROL: |
a6cb099a | 3563 | msr_info->data = vmx->msr_ia32_feature_control; |
cae50139 JK |
3564 | break; |
3565 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
3566 | if (!nested_vmx_allowed(vcpu)) | |
3567 | return 1; | |
6677f3da PB |
3568 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
3569 | &msr_info->data); | |
20300099 WL |
3570 | case MSR_IA32_XSS: |
3571 | if (!vmx_xsaves_supported()) | |
3572 | return 1; | |
609e36d3 | 3573 | msr_info->data = vcpu->arch.ia32_xss; |
20300099 | 3574 | break; |
4e47c7a6 | 3575 | case MSR_TSC_AUX: |
d6321d49 RK |
3576 | if (!msr_info->host_initiated && |
3577 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) | |
4e47c7a6 SY |
3578 | return 1; |
3579 | /* Otherwise falls through */ | |
6aa8b732 | 3580 | default: |
a6cb099a | 3581 | msr = find_msr_entry(vmx, msr_info->index); |
3bab1f5d | 3582 | if (msr) { |
609e36d3 | 3583 | msr_info->data = msr->data; |
3bab1f5d | 3584 | break; |
6aa8b732 | 3585 | } |
609e36d3 | 3586 | return kvm_get_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3587 | } |
3588 | ||
6aa8b732 AK |
3589 | return 0; |
3590 | } | |
3591 | ||
cae50139 JK |
3592 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
3593 | ||
6aa8b732 AK |
3594 | /* |
3595 | * Writes msr value into into the appropriate "register". | |
3596 | * Returns 0 on success, non-0 otherwise. | |
3597 | * Assumes vcpu_load() was already called. | |
3598 | */ | |
8fe8ab46 | 3599 | static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3600 | { |
a2fa3e9f | 3601 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 3602 | struct shared_msr_entry *msr; |
2cc51560 | 3603 | int ret = 0; |
8fe8ab46 WA |
3604 | u32 msr_index = msr_info->index; |
3605 | u64 data = msr_info->data; | |
2cc51560 | 3606 | |
6aa8b732 | 3607 | switch (msr_index) { |
3bab1f5d | 3608 | case MSR_EFER: |
8fe8ab46 | 3609 | ret = kvm_set_msr_common(vcpu, msr_info); |
2cc51560 | 3610 | break; |
16175a79 | 3611 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3612 | case MSR_FS_BASE: |
2fb92db1 | 3613 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3614 | vmcs_writel(GUEST_FS_BASE, data); |
3615 | break; | |
3616 | case MSR_GS_BASE: | |
2fb92db1 | 3617 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3618 | vmcs_writel(GUEST_GS_BASE, data); |
3619 | break; | |
44ea2b17 AK |
3620 | case MSR_KERNEL_GS_BASE: |
3621 | vmx_load_host_state(vmx); | |
3622 | vmx->msr_guest_kernel_gs_base = data; | |
3623 | break; | |
6aa8b732 AK |
3624 | #endif |
3625 | case MSR_IA32_SYSENTER_CS: | |
3626 | vmcs_write32(GUEST_SYSENTER_CS, data); | |
3627 | break; | |
3628 | case MSR_IA32_SYSENTER_EIP: | |
f5b42c33 | 3629 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
6aa8b732 AK |
3630 | break; |
3631 | case MSR_IA32_SYSENTER_ESP: | |
f5b42c33 | 3632 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
6aa8b732 | 3633 | break; |
0dd376e7 | 3634 | case MSR_IA32_BNDCFGS: |
691bd434 | 3635 | if (!kvm_mpx_supported() || |
d6321d49 RK |
3636 | (!msr_info->host_initiated && |
3637 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) | |
93c4adc7 | 3638 | return 1; |
fd8cb433 | 3639 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
4531662d | 3640 | (data & MSR_IA32_BNDCFGS_RSVD)) |
93c4adc7 | 3641 | return 1; |
0dd376e7 LJ |
3642 | vmcs_write64(GUEST_BNDCFGS, data); |
3643 | break; | |
d28b387f KA |
3644 | case MSR_IA32_SPEC_CTRL: |
3645 | if (!msr_info->host_initiated && | |
3646 | !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) && | |
da39556f | 3647 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) && |
9f65fb29 | 3648 | !guest_cpuid_has(vcpu, X86_FEATURE_SSBD)) |
d28b387f KA |
3649 | return 1; |
3650 | ||
3651 | /* The STIBP bit doesn't fault even if it's not advertised */ | |
9f65fb29 | 3652 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
d28b387f KA |
3653 | return 1; |
3654 | ||
3655 | vmx->spec_ctrl = data; | |
3656 | ||
3657 | if (!data) | |
3658 | break; | |
3659 | ||
3660 | /* | |
3661 | * For non-nested: | |
3662 | * When it's written (to non-zero) for the first time, pass | |
3663 | * it through. | |
3664 | * | |
3665 | * For nested: | |
3666 | * The handling of the MSR bitmap for L2 guests is done in | |
3667 | * nested_vmx_merge_msr_bitmap. We should not touch the | |
3668 | * vmcs02.msr_bitmap here since it gets completely overwritten | |
3669 | * in the merging. We update the vmcs01 here for L1 as well | |
3670 | * since it will end up touching the MSR anyway now. | |
3671 | */ | |
3672 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, | |
3673 | MSR_IA32_SPEC_CTRL, | |
3674 | MSR_TYPE_RW); | |
3675 | break; | |
15d45071 AR |
3676 | case MSR_IA32_PRED_CMD: |
3677 | if (!msr_info->host_initiated && | |
3678 | !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) && | |
3679 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) | |
3680 | return 1; | |
3681 | ||
3682 | if (data & ~PRED_CMD_IBPB) | |
3683 | return 1; | |
3684 | ||
3685 | if (!data) | |
3686 | break; | |
3687 | ||
3688 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); | |
3689 | ||
3690 | /* | |
3691 | * For non-nested: | |
3692 | * When it's written (to non-zero) for the first time, pass | |
3693 | * it through. | |
3694 | * | |
3695 | * For nested: | |
3696 | * The handling of the MSR bitmap for L2 guests is done in | |
3697 | * nested_vmx_merge_msr_bitmap. We should not touch the | |
3698 | * vmcs02.msr_bitmap here since it gets completely overwritten | |
3699 | * in the merging. | |
3700 | */ | |
3701 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, | |
3702 | MSR_TYPE_W); | |
3703 | break; | |
28c1c9fa KA |
3704 | case MSR_IA32_ARCH_CAPABILITIES: |
3705 | if (!msr_info->host_initiated) | |
3706 | return 1; | |
3707 | vmx->arch_capabilities = data; | |
3708 | break; | |
468d472f SY |
3709 | case MSR_IA32_CR_PAT: |
3710 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { | |
4566654b NA |
3711 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
3712 | return 1; | |
468d472f SY |
3713 | vmcs_write64(GUEST_IA32_PAT, data); |
3714 | vcpu->arch.pat = data; | |
3715 | break; | |
3716 | } | |
8fe8ab46 | 3717 | ret = kvm_set_msr_common(vcpu, msr_info); |
4e47c7a6 | 3718 | break; |
ba904635 WA |
3719 | case MSR_IA32_TSC_ADJUST: |
3720 | ret = kvm_set_msr_common(vcpu, msr_info); | |
4e47c7a6 | 3721 | break; |
c45dcc71 AR |
3722 | case MSR_IA32_MCG_EXT_CTL: |
3723 | if ((!msr_info->host_initiated && | |
3724 | !(to_vmx(vcpu)->msr_ia32_feature_control & | |
3725 | FEATURE_CONTROL_LMCE)) || | |
3726 | (data & ~MCG_EXT_CTL_LMCE_EN)) | |
3727 | return 1; | |
3728 | vcpu->arch.mcg_ext_ctl = data; | |
3729 | break; | |
cae50139 | 3730 | case MSR_IA32_FEATURE_CONTROL: |
37e4c997 | 3731 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
3b84080b | 3732 | (to_vmx(vcpu)->msr_ia32_feature_control & |
cae50139 JK |
3733 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
3734 | return 1; | |
3b84080b | 3735 | vmx->msr_ia32_feature_control = data; |
cae50139 JK |
3736 | if (msr_info->host_initiated && data == 0) |
3737 | vmx_leave_nested(vcpu); | |
3738 | break; | |
3739 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
62cc6b9d DM |
3740 | if (!msr_info->host_initiated) |
3741 | return 1; /* they are read-only */ | |
3742 | if (!nested_vmx_allowed(vcpu)) | |
3743 | return 1; | |
3744 | return vmx_set_vmx_msr(vcpu, msr_index, data); | |
20300099 WL |
3745 | case MSR_IA32_XSS: |
3746 | if (!vmx_xsaves_supported()) | |
3747 | return 1; | |
3748 | /* | |
3749 | * The only supported bit as of Skylake is bit 8, but | |
3750 | * it is not supported on KVM. | |
3751 | */ | |
3752 | if (data != 0) | |
3753 | return 1; | |
3754 | vcpu->arch.ia32_xss = data; | |
3755 | if (vcpu->arch.ia32_xss != host_xss) | |
3756 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, | |
3757 | vcpu->arch.ia32_xss, host_xss); | |
3758 | else | |
3759 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); | |
3760 | break; | |
4e47c7a6 | 3761 | case MSR_TSC_AUX: |
d6321d49 RK |
3762 | if (!msr_info->host_initiated && |
3763 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) | |
4e47c7a6 SY |
3764 | return 1; |
3765 | /* Check reserved bit, higher 32 bits should be zero */ | |
3766 | if ((data >> 32) != 0) | |
3767 | return 1; | |
3768 | /* Otherwise falls through */ | |
6aa8b732 | 3769 | default: |
8b9cf98c | 3770 | msr = find_msr_entry(vmx, msr_index); |
3bab1f5d | 3771 | if (msr) { |
8b3c3104 | 3772 | u64 old_msr_data = msr->data; |
3bab1f5d | 3773 | msr->data = data; |
2225fd56 AK |
3774 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
3775 | preempt_disable(); | |
8b3c3104 AH |
3776 | ret = kvm_set_shared_msr(msr->index, msr->data, |
3777 | msr->mask); | |
2225fd56 | 3778 | preempt_enable(); |
8b3c3104 AH |
3779 | if (ret) |
3780 | msr->data = old_msr_data; | |
2225fd56 | 3781 | } |
3bab1f5d | 3782 | break; |
6aa8b732 | 3783 | } |
8fe8ab46 | 3784 | ret = kvm_set_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3785 | } |
3786 | ||
2cc51560 | 3787 | return ret; |
6aa8b732 AK |
3788 | } |
3789 | ||
5fdbf976 | 3790 | static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
6aa8b732 | 3791 | { |
5fdbf976 MT |
3792 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
3793 | switch (reg) { | |
3794 | case VCPU_REGS_RSP: | |
3795 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); | |
3796 | break; | |
3797 | case VCPU_REGS_RIP: | |
3798 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); | |
3799 | break; | |
6de4f3ad AK |
3800 | case VCPU_EXREG_PDPTR: |
3801 | if (enable_ept) | |
3802 | ept_save_pdptrs(vcpu); | |
3803 | break; | |
5fdbf976 MT |
3804 | default: |
3805 | break; | |
3806 | } | |
6aa8b732 AK |
3807 | } |
3808 | ||
6aa8b732 AK |
3809 | static __init int cpu_has_kvm_support(void) |
3810 | { | |
6210e37b | 3811 | return cpu_has_vmx(); |
6aa8b732 AK |
3812 | } |
3813 | ||
3814 | static __init int vmx_disabled_by_bios(void) | |
3815 | { | |
3816 | u64 msr; | |
3817 | ||
3818 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); | |
cafd6659 | 3819 | if (msr & FEATURE_CONTROL_LOCKED) { |
23f3e991 | 3820 | /* launched w/ TXT and VMX disabled */ |
cafd6659 SW |
3821 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
3822 | && tboot_enabled()) | |
3823 | return 1; | |
23f3e991 | 3824 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
cafd6659 | 3825 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
23f3e991 | 3826 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
f9335afe SW |
3827 | && !tboot_enabled()) { |
3828 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " | |
23f3e991 | 3829 | "activate TXT before enabling KVM\n"); |
cafd6659 | 3830 | return 1; |
f9335afe | 3831 | } |
23f3e991 JC |
3832 | /* launched w/o TXT and VMX disabled */ |
3833 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) | |
3834 | && !tboot_enabled()) | |
3835 | return 1; | |
cafd6659 SW |
3836 | } |
3837 | ||
3838 | return 0; | |
6aa8b732 AK |
3839 | } |
3840 | ||
7725b894 DX |
3841 | static void kvm_cpu_vmxon(u64 addr) |
3842 | { | |
fe0e80be | 3843 | cr4_set_bits(X86_CR4_VMXE); |
1c5ac21a AS |
3844 | intel_pt_handle_vmx(1); |
3845 | ||
7725b894 DX |
3846 | asm volatile (ASM_VMX_VMXON_RAX |
3847 | : : "a"(&addr), "m"(addr) | |
3848 | : "memory", "cc"); | |
3849 | } | |
3850 | ||
13a34e06 | 3851 | static int hardware_enable(void) |
6aa8b732 AK |
3852 | { |
3853 | int cpu = raw_smp_processor_id(); | |
3854 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); | |
cafd6659 | 3855 | u64 old, test_bits; |
6aa8b732 | 3856 | |
1e02ce4c | 3857 | if (cr4_read_shadow() & X86_CR4_VMXE) |
10474ae8 AG |
3858 | return -EBUSY; |
3859 | ||
773e8a04 VK |
3860 | /* |
3861 | * This can happen if we hot-added a CPU but failed to allocate | |
3862 | * VP assist page for it. | |
3863 | */ | |
3864 | if (static_branch_unlikely(&enable_evmcs) && | |
3865 | !hv_get_vp_assist_page(cpu)) | |
3866 | return -EFAULT; | |
3867 | ||
d462b819 | 3868 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
bf9f6ac8 FW |
3869 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
3870 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
8f536b76 ZY |
3871 | |
3872 | /* | |
3873 | * Now we can enable the vmclear operation in kdump | |
3874 | * since the loaded_vmcss_on_cpu list on this cpu | |
3875 | * has been initialized. | |
3876 | * | |
3877 | * Though the cpu is not in VMX operation now, there | |
3878 | * is no problem to enable the vmclear operation | |
3879 | * for the loaded_vmcss_on_cpu list is empty! | |
3880 | */ | |
3881 | crash_enable_local_vmclear(cpu); | |
3882 | ||
6aa8b732 | 3883 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
cafd6659 SW |
3884 | |
3885 | test_bits = FEATURE_CONTROL_LOCKED; | |
3886 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
3887 | if (tboot_enabled()) | |
3888 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; | |
3889 | ||
3890 | if ((old & test_bits) != test_bits) { | |
6aa8b732 | 3891 | /* enable and lock */ |
cafd6659 SW |
3892 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
3893 | } | |
fe0e80be | 3894 | kvm_cpu_vmxon(phys_addr); |
fdf288bf DH |
3895 | if (enable_ept) |
3896 | ept_sync_global(); | |
10474ae8 AG |
3897 | |
3898 | return 0; | |
6aa8b732 AK |
3899 | } |
3900 | ||
d462b819 | 3901 | static void vmclear_local_loaded_vmcss(void) |
543e4243 AK |
3902 | { |
3903 | int cpu = raw_smp_processor_id(); | |
d462b819 | 3904 | struct loaded_vmcs *v, *n; |
543e4243 | 3905 | |
d462b819 NHE |
3906 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
3907 | loaded_vmcss_on_cpu_link) | |
3908 | __loaded_vmcs_clear(v); | |
543e4243 AK |
3909 | } |
3910 | ||
710ff4a8 EH |
3911 | |
3912 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() | |
3913 | * tricks. | |
3914 | */ | |
3915 | static void kvm_cpu_vmxoff(void) | |
6aa8b732 | 3916 | { |
4ecac3fd | 3917 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
1c5ac21a AS |
3918 | |
3919 | intel_pt_handle_vmx(0); | |
fe0e80be | 3920 | cr4_clear_bits(X86_CR4_VMXE); |
6aa8b732 AK |
3921 | } |
3922 | ||
13a34e06 | 3923 | static void hardware_disable(void) |
710ff4a8 | 3924 | { |
fe0e80be DH |
3925 | vmclear_local_loaded_vmcss(); |
3926 | kvm_cpu_vmxoff(); | |
710ff4a8 EH |
3927 | } |
3928 | ||
1c3d14fe | 3929 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
d77c26fc | 3930 | u32 msr, u32 *result) |
1c3d14fe YS |
3931 | { |
3932 | u32 vmx_msr_low, vmx_msr_high; | |
3933 | u32 ctl = ctl_min | ctl_opt; | |
3934 | ||
3935 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3936 | ||
3937 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ | |
3938 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ | |
3939 | ||
3940 | /* Ensure minimum (required) set of control bits are supported. */ | |
3941 | if (ctl_min & ~ctl) | |
002c7f7c | 3942 | return -EIO; |
1c3d14fe YS |
3943 | |
3944 | *result = ctl; | |
3945 | return 0; | |
3946 | } | |
3947 | ||
110312c8 AK |
3948 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
3949 | { | |
3950 | u32 vmx_msr_low, vmx_msr_high; | |
3951 | ||
3952 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3953 | return vmx_msr_high & ctl; | |
3954 | } | |
3955 | ||
002c7f7c | 3956 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
6aa8b732 AK |
3957 | { |
3958 | u32 vmx_msr_low, vmx_msr_high; | |
d56f546d | 3959 | u32 min, opt, min2, opt2; |
1c3d14fe YS |
3960 | u32 _pin_based_exec_control = 0; |
3961 | u32 _cpu_based_exec_control = 0; | |
f78e0e2e | 3962 | u32 _cpu_based_2nd_exec_control = 0; |
1c3d14fe YS |
3963 | u32 _vmexit_control = 0; |
3964 | u32 _vmentry_control = 0; | |
3965 | ||
1389309c | 3966 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
10166744 | 3967 | min = CPU_BASED_HLT_EXITING | |
1c3d14fe YS |
3968 | #ifdef CONFIG_X86_64 |
3969 | CPU_BASED_CR8_LOAD_EXITING | | |
3970 | CPU_BASED_CR8_STORE_EXITING | | |
3971 | #endif | |
d56f546d SY |
3972 | CPU_BASED_CR3_LOAD_EXITING | |
3973 | CPU_BASED_CR3_STORE_EXITING | | |
8eb73e2d | 3974 | CPU_BASED_UNCOND_IO_EXITING | |
1c3d14fe | 3975 | CPU_BASED_MOV_DR_EXITING | |
a7052897 | 3976 | CPU_BASED_USE_TSC_OFFSETING | |
4d5422ce WL |
3977 | CPU_BASED_MWAIT_EXITING | |
3978 | CPU_BASED_MONITOR_EXITING | | |
fee84b07 AK |
3979 | CPU_BASED_INVLPG_EXITING | |
3980 | CPU_BASED_RDPMC_EXITING; | |
443381a8 | 3981 | |
f78e0e2e | 3982 | opt = CPU_BASED_TPR_SHADOW | |
25c5f225 | 3983 | CPU_BASED_USE_MSR_BITMAPS | |
f78e0e2e | 3984 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
1c3d14fe YS |
3985 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
3986 | &_cpu_based_exec_control) < 0) | |
002c7f7c | 3987 | return -EIO; |
6e5d865c YS |
3988 | #ifdef CONFIG_X86_64 |
3989 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
3990 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & | |
3991 | ~CPU_BASED_CR8_STORE_EXITING; | |
3992 | #endif | |
f78e0e2e | 3993 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
d56f546d SY |
3994 | min2 = 0; |
3995 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | | |
8d14695f | 3996 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
2384d2b3 | 3997 | SECONDARY_EXEC_WBINVD_EXITING | |
d56f546d | 3998 | SECONDARY_EXEC_ENABLE_VPID | |
3a624e29 | 3999 | SECONDARY_EXEC_ENABLE_EPT | |
4b8d54f9 | 4000 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
4e47c7a6 | 4001 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
0367f205 | 4002 | SECONDARY_EXEC_DESC | |
ad756a16 | 4003 | SECONDARY_EXEC_RDTSCP | |
83d4c286 | 4004 | SECONDARY_EXEC_ENABLE_INVPCID | |
c7c9c56c | 4005 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
abc4fc58 | 4006 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
20300099 | 4007 | SECONDARY_EXEC_SHADOW_VMCS | |
843e4330 | 4008 | SECONDARY_EXEC_XSAVES | |
736fdf72 DH |
4009 | SECONDARY_EXEC_RDSEED_EXITING | |
4010 | SECONDARY_EXEC_RDRAND_EXITING | | |
8b3e34e4 | 4011 | SECONDARY_EXEC_ENABLE_PML | |
2a499e49 BD |
4012 | SECONDARY_EXEC_TSC_SCALING | |
4013 | SECONDARY_EXEC_ENABLE_VMFUNC; | |
d56f546d SY |
4014 | if (adjust_vmx_controls(min2, opt2, |
4015 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
f78e0e2e SY |
4016 | &_cpu_based_2nd_exec_control) < 0) |
4017 | return -EIO; | |
4018 | } | |
4019 | #ifndef CONFIG_X86_64 | |
4020 | if (!(_cpu_based_2nd_exec_control & | |
4021 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
4022 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; | |
4023 | #endif | |
83d4c286 YZ |
4024 | |
4025 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
4026 | _cpu_based_2nd_exec_control &= ~( | |
8d14695f | 4027 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
c7c9c56c YZ |
4028 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
4029 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
83d4c286 | 4030 | |
61f1dd90 WL |
4031 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
4032 | &vmx_capability.ept, &vmx_capability.vpid); | |
4033 | ||
d56f546d | 4034 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
a7052897 MT |
4035 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
4036 | enabled */ | |
5fff7d27 GN |
4037 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
4038 | CPU_BASED_CR3_STORE_EXITING | | |
4039 | CPU_BASED_INVLPG_EXITING); | |
61f1dd90 WL |
4040 | } else if (vmx_capability.ept) { |
4041 | vmx_capability.ept = 0; | |
4042 | pr_warn_once("EPT CAP should not exist if not support " | |
4043 | "1-setting enable EPT VM-execution control\n"); | |
4044 | } | |
4045 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && | |
4046 | vmx_capability.vpid) { | |
4047 | vmx_capability.vpid = 0; | |
4048 | pr_warn_once("VPID CAP should not exist if not support " | |
4049 | "1-setting enable VPID VM-execution control\n"); | |
d56f546d | 4050 | } |
1c3d14fe | 4051 | |
91fa0f8e | 4052 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
1c3d14fe YS |
4053 | #ifdef CONFIG_X86_64 |
4054 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; | |
4055 | #endif | |
a547c6db | 4056 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
91fa0f8e | 4057 | VM_EXIT_CLEAR_BNDCFGS; |
1c3d14fe YS |
4058 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
4059 | &_vmexit_control) < 0) | |
002c7f7c | 4060 | return -EIO; |
1c3d14fe | 4061 | |
8a1b4392 PB |
4062 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
4063 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | | |
4064 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
4065 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
4066 | &_pin_based_exec_control) < 0) | |
4067 | return -EIO; | |
4068 | ||
1c17c3e6 PB |
4069 | if (cpu_has_broken_vmx_preemption_timer()) |
4070 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be | 4071 | if (!(_cpu_based_2nd_exec_control & |
91fa0f8e | 4072 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
01e439be YZ |
4073 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
4074 | ||
c845f9c6 | 4075 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
da8999d3 | 4076 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
1c3d14fe YS |
4077 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
4078 | &_vmentry_control) < 0) | |
002c7f7c | 4079 | return -EIO; |
6aa8b732 | 4080 | |
c68876fd | 4081 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
1c3d14fe YS |
4082 | |
4083 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ | |
4084 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) | |
002c7f7c | 4085 | return -EIO; |
1c3d14fe YS |
4086 | |
4087 | #ifdef CONFIG_X86_64 | |
4088 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ | |
4089 | if (vmx_msr_high & (1u<<16)) | |
002c7f7c | 4090 | return -EIO; |
1c3d14fe YS |
4091 | #endif |
4092 | ||
4093 | /* Require Write-Back (WB) memory type for VMCS accesses. */ | |
4094 | if (((vmx_msr_high >> 18) & 15) != 6) | |
002c7f7c | 4095 | return -EIO; |
1c3d14fe | 4096 | |
002c7f7c | 4097 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
16cb0255 | 4098 | vmcs_conf->order = get_order(vmcs_conf->size); |
9ac7e3e8 | 4099 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
773e8a04 VK |
4100 | |
4101 | /* KVM supports Enlightened VMCS v1 only */ | |
4102 | if (static_branch_unlikely(&enable_evmcs)) | |
4103 | vmcs_conf->revision_id = KVM_EVMCS_VERSION; | |
4104 | else | |
4105 | vmcs_conf->revision_id = vmx_msr_low; | |
1c3d14fe | 4106 | |
002c7f7c YS |
4107 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
4108 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; | |
f78e0e2e | 4109 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
002c7f7c YS |
4110 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
4111 | vmcs_conf->vmentry_ctrl = _vmentry_control; | |
1c3d14fe | 4112 | |
773e8a04 VK |
4113 | if (static_branch_unlikely(&enable_evmcs)) |
4114 | evmcs_sanitize_exec_ctrls(vmcs_conf); | |
4115 | ||
110312c8 AK |
4116 | cpu_has_load_ia32_efer = |
4117 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
4118 | VM_ENTRY_LOAD_IA32_EFER) | |
4119 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
4120 | VM_EXIT_LOAD_IA32_EFER); | |
4121 | ||
8bf00a52 GN |
4122 | cpu_has_load_perf_global_ctrl = |
4123 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
4124 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
4125 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
4126 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
4127 | ||
4128 | /* | |
4129 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL | |
bb3541f1 | 4130 | * but due to errata below it can't be used. Workaround is to use |
8bf00a52 GN |
4131 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
4132 | * | |
4133 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] | |
4134 | * | |
4135 | * AAK155 (model 26) | |
4136 | * AAP115 (model 30) | |
4137 | * AAT100 (model 37) | |
4138 | * BC86,AAY89,BD102 (model 44) | |
4139 | * BA97 (model 46) | |
4140 | * | |
4141 | */ | |
4142 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { | |
4143 | switch (boot_cpu_data.x86_model) { | |
4144 | case 26: | |
4145 | case 30: | |
4146 | case 37: | |
4147 | case 44: | |
4148 | case 46: | |
4149 | cpu_has_load_perf_global_ctrl = false; | |
4150 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " | |
4151 | "does not work properly. Using workaround\n"); | |
4152 | break; | |
4153 | default: | |
4154 | break; | |
4155 | } | |
4156 | } | |
4157 | ||
782511b0 | 4158 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
20300099 WL |
4159 | rdmsrl(MSR_IA32_XSS, host_xss); |
4160 | ||
1c3d14fe | 4161 | return 0; |
c68876fd | 4162 | } |
6aa8b732 AK |
4163 | |
4164 | static struct vmcs *alloc_vmcs_cpu(int cpu) | |
4165 | { | |
4166 | int node = cpu_to_node(cpu); | |
4167 | struct page *pages; | |
4168 | struct vmcs *vmcs; | |
4169 | ||
96db800f | 4170 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
6aa8b732 AK |
4171 | if (!pages) |
4172 | return NULL; | |
4173 | vmcs = page_address(pages); | |
1c3d14fe YS |
4174 | memset(vmcs, 0, vmcs_config.size); |
4175 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ | |
6aa8b732 AK |
4176 | return vmcs; |
4177 | } | |
4178 | ||
6aa8b732 AK |
4179 | static void free_vmcs(struct vmcs *vmcs) |
4180 | { | |
1c3d14fe | 4181 | free_pages((unsigned long)vmcs, vmcs_config.order); |
6aa8b732 AK |
4182 | } |
4183 | ||
d462b819 NHE |
4184 | /* |
4185 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded | |
4186 | */ | |
4187 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) | |
4188 | { | |
4189 | if (!loaded_vmcs->vmcs) | |
4190 | return; | |
4191 | loaded_vmcs_clear(loaded_vmcs); | |
4192 | free_vmcs(loaded_vmcs->vmcs); | |
4193 | loaded_vmcs->vmcs = NULL; | |
904e14fb PB |
4194 | if (loaded_vmcs->msr_bitmap) |
4195 | free_page((unsigned long)loaded_vmcs->msr_bitmap); | |
355f4fb1 | 4196 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
d462b819 NHE |
4197 | } |
4198 | ||
f21f165e PB |
4199 | static struct vmcs *alloc_vmcs(void) |
4200 | { | |
4201 | return alloc_vmcs_cpu(raw_smp_processor_id()); | |
4202 | } | |
4203 | ||
4204 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) | |
4205 | { | |
4206 | loaded_vmcs->vmcs = alloc_vmcs(); | |
4207 | if (!loaded_vmcs->vmcs) | |
4208 | return -ENOMEM; | |
4209 | ||
4210 | loaded_vmcs->shadow_vmcs = NULL; | |
4211 | loaded_vmcs_init(loaded_vmcs); | |
904e14fb PB |
4212 | |
4213 | if (cpu_has_vmx_msr_bitmap()) { | |
4214 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); | |
4215 | if (!loaded_vmcs->msr_bitmap) | |
4216 | goto out_vmcs; | |
4217 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); | |
4218 | } | |
f21f165e | 4219 | return 0; |
904e14fb PB |
4220 | |
4221 | out_vmcs: | |
4222 | free_loaded_vmcs(loaded_vmcs); | |
4223 | return -ENOMEM; | |
f21f165e PB |
4224 | } |
4225 | ||
39959588 | 4226 | static void free_kvm_area(void) |
6aa8b732 AK |
4227 | { |
4228 | int cpu; | |
4229 | ||
3230bb47 | 4230 | for_each_possible_cpu(cpu) { |
6aa8b732 | 4231 | free_vmcs(per_cpu(vmxarea, cpu)); |
3230bb47 ZA |
4232 | per_cpu(vmxarea, cpu) = NULL; |
4233 | } | |
6aa8b732 AK |
4234 | } |
4235 | ||
d37f4267 JM |
4236 | enum vmcs_field_width { |
4237 | VMCS_FIELD_WIDTH_U16 = 0, | |
4238 | VMCS_FIELD_WIDTH_U64 = 1, | |
4239 | VMCS_FIELD_WIDTH_U32 = 2, | |
4240 | VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3 | |
85fd514e JM |
4241 | }; |
4242 | ||
d37f4267 | 4243 | static inline int vmcs_field_width(unsigned long field) |
85fd514e JM |
4244 | { |
4245 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ | |
d37f4267 | 4246 | return VMCS_FIELD_WIDTH_U32; |
85fd514e JM |
4247 | return (field >> 13) & 0x3 ; |
4248 | } | |
4249 | ||
4250 | static inline int vmcs_field_readonly(unsigned long field) | |
4251 | { | |
4252 | return (((field >> 10) & 0x3) == 1); | |
4253 | } | |
4254 | ||
fe2b201b BD |
4255 | static void init_vmcs_shadow_fields(void) |
4256 | { | |
4257 | int i, j; | |
4258 | ||
44900ba6 PB |
4259 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
4260 | u16 field = shadow_read_only_fields[i]; | |
d37f4267 | 4261 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
44900ba6 PB |
4262 | (i + 1 == max_shadow_read_only_fields || |
4263 | shadow_read_only_fields[i + 1] != field + 1)) | |
4264 | pr_err("Missing field from shadow_read_only_field %x\n", | |
4265 | field + 1); | |
4266 | ||
4267 | clear_bit(field, vmx_vmread_bitmap); | |
4268 | #ifdef CONFIG_X86_64 | |
4269 | if (field & 1) | |
4270 | continue; | |
4271 | #endif | |
4272 | if (j < i) | |
4273 | shadow_read_only_fields[j] = field; | |
4274 | j++; | |
4275 | } | |
4276 | max_shadow_read_only_fields = j; | |
fe2b201b BD |
4277 | |
4278 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { | |
44900ba6 | 4279 | u16 field = shadow_read_write_fields[i]; |
d37f4267 | 4280 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
44900ba6 PB |
4281 | (i + 1 == max_shadow_read_write_fields || |
4282 | shadow_read_write_fields[i + 1] != field + 1)) | |
4283 | pr_err("Missing field from shadow_read_write_field %x\n", | |
4284 | field + 1); | |
4285 | ||
c5d167b2 PB |
4286 | /* |
4287 | * PML and the preemption timer can be emulated, but the | |
4288 | * processor cannot vmwrite to fields that don't exist | |
4289 | * on bare metal. | |
4290 | */ | |
44900ba6 | 4291 | switch (field) { |
c5d167b2 PB |
4292 | case GUEST_PML_INDEX: |
4293 | if (!cpu_has_vmx_pml()) | |
4294 | continue; | |
4295 | break; | |
4296 | case VMX_PREEMPTION_TIMER_VALUE: | |
4297 | if (!cpu_has_vmx_preemption_timer()) | |
4298 | continue; | |
4299 | break; | |
4300 | case GUEST_INTR_STATUS: | |
4301 | if (!cpu_has_vmx_apicv()) | |
fe2b201b BD |
4302 | continue; |
4303 | break; | |
4304 | default: | |
4305 | break; | |
4306 | } | |
4307 | ||
44900ba6 PB |
4308 | clear_bit(field, vmx_vmwrite_bitmap); |
4309 | clear_bit(field, vmx_vmread_bitmap); | |
4310 | #ifdef CONFIG_X86_64 | |
4311 | if (field & 1) | |
4312 | continue; | |
4313 | #endif | |
fe2b201b | 4314 | if (j < i) |
44900ba6 | 4315 | shadow_read_write_fields[j] = field; |
fe2b201b BD |
4316 | j++; |
4317 | } | |
4318 | max_shadow_read_write_fields = j; | |
fe2b201b BD |
4319 | } |
4320 | ||
6aa8b732 AK |
4321 | static __init int alloc_kvm_area(void) |
4322 | { | |
4323 | int cpu; | |
4324 | ||
3230bb47 | 4325 | for_each_possible_cpu(cpu) { |
6aa8b732 AK |
4326 | struct vmcs *vmcs; |
4327 | ||
4328 | vmcs = alloc_vmcs_cpu(cpu); | |
4329 | if (!vmcs) { | |
4330 | free_kvm_area(); | |
4331 | return -ENOMEM; | |
4332 | } | |
4333 | ||
4334 | per_cpu(vmxarea, cpu) = vmcs; | |
4335 | } | |
4336 | return 0; | |
4337 | } | |
4338 | ||
91b0aa2c | 4339 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
d99e4152 | 4340 | struct kvm_segment *save) |
6aa8b732 | 4341 | { |
d99e4152 GN |
4342 | if (!emulate_invalid_guest_state) { |
4343 | /* | |
4344 | * CS and SS RPL should be equal during guest entry according | |
4345 | * to VMX spec, but in reality it is not always so. Since vcpu | |
4346 | * is in the middle of the transition from real mode to | |
4347 | * protected mode it is safe to assume that RPL 0 is a good | |
4348 | * default value. | |
4349 | */ | |
4350 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) | |
b32a9918 NA |
4351 | save->selector &= ~SEGMENT_RPL_MASK; |
4352 | save->dpl = save->selector & SEGMENT_RPL_MASK; | |
d99e4152 | 4353 | save->s = 1; |
6aa8b732 | 4354 | } |
d99e4152 | 4355 | vmx_set_segment(vcpu, save, seg); |
6aa8b732 AK |
4356 | } |
4357 | ||
4358 | static void enter_pmode(struct kvm_vcpu *vcpu) | |
4359 | { | |
4360 | unsigned long flags; | |
a89a8fb9 | 4361 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 | 4362 | |
d99e4152 GN |
4363 | /* |
4364 | * Update real mode segment cache. It may be not up-to-date if sement | |
4365 | * register was written while vcpu was in a guest mode. | |
4366 | */ | |
4367 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
4368 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
4369 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
4370 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
4371 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); | |
4372 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
4373 | ||
7ffd92c5 | 4374 | vmx->rmode.vm86_active = 0; |
6aa8b732 | 4375 | |
2fb92db1 AK |
4376 | vmx_segment_cache_clear(vmx); |
4377 | ||
f5f7b2fe | 4378 | vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
6aa8b732 AK |
4379 | |
4380 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 AK |
4381 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
4382 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
6aa8b732 AK |
4383 | vmcs_writel(GUEST_RFLAGS, flags); |
4384 | ||
66aee91a RR |
4385 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
4386 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); | |
6aa8b732 AK |
4387 | |
4388 | update_exception_bitmap(vcpu); | |
4389 | ||
91b0aa2c GN |
4390 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
4391 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); | |
4392 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
4393 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
4394 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
4395 | fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
6aa8b732 AK |
4396 | } |
4397 | ||
f5f7b2fe | 4398 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
6aa8b732 | 4399 | { |
772e0318 | 4400 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
d99e4152 GN |
4401 | struct kvm_segment var = *save; |
4402 | ||
4403 | var.dpl = 0x3; | |
4404 | if (seg == VCPU_SREG_CS) | |
4405 | var.type = 0x3; | |
4406 | ||
4407 | if (!emulate_invalid_guest_state) { | |
4408 | var.selector = var.base >> 4; | |
4409 | var.base = var.base & 0xffff0; | |
4410 | var.limit = 0xffff; | |
4411 | var.g = 0; | |
4412 | var.db = 0; | |
4413 | var.present = 1; | |
4414 | var.s = 1; | |
4415 | var.l = 0; | |
4416 | var.unusable = 0; | |
4417 | var.type = 0x3; | |
4418 | var.avl = 0; | |
4419 | if (save->base & 0xf) | |
4420 | printk_once(KERN_WARNING "kvm: segment base is not " | |
4421 | "paragraph aligned when entering " | |
4422 | "protected mode (seg=%d)", seg); | |
4423 | } | |
6aa8b732 | 4424 | |
d99e4152 | 4425 | vmcs_write16(sf->selector, var.selector); |
96794e4e | 4426 | vmcs_writel(sf->base, var.base); |
d99e4152 GN |
4427 | vmcs_write32(sf->limit, var.limit); |
4428 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); | |
6aa8b732 AK |
4429 | } |
4430 | ||
4431 | static void enter_rmode(struct kvm_vcpu *vcpu) | |
4432 | { | |
4433 | unsigned long flags; | |
a89a8fb9 | 4434 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
40bbb9d0 | 4435 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
6aa8b732 | 4436 | |
f5f7b2fe AK |
4437 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
4438 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
4439 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
4440 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
4441 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
c6ad1153 GN |
4442 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
4443 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
f5f7b2fe | 4444 | |
7ffd92c5 | 4445 | vmx->rmode.vm86_active = 1; |
6aa8b732 | 4446 | |
776e58ea GN |
4447 | /* |
4448 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering | |
4918c6ca | 4449 | * vcpu. Warn the user that an update is overdue. |
776e58ea | 4450 | */ |
40bbb9d0 | 4451 | if (!kvm_vmx->tss_addr) |
776e58ea GN |
4452 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
4453 | "called before entering vcpu\n"); | |
776e58ea | 4454 | |
2fb92db1 AK |
4455 | vmx_segment_cache_clear(vmx); |
4456 | ||
40bbb9d0 | 4457 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
6aa8b732 | 4458 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
6aa8b732 AK |
4459 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
4460 | ||
4461 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 | 4462 | vmx->rmode.save_rflags = flags; |
6aa8b732 | 4463 | |
053de044 | 4464 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
6aa8b732 AK |
4465 | |
4466 | vmcs_writel(GUEST_RFLAGS, flags); | |
66aee91a | 4467 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
6aa8b732 AK |
4468 | update_exception_bitmap(vcpu); |
4469 | ||
d99e4152 GN |
4470 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
4471 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); | |
4472 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
4473 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
4474 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
4475 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
b246dd5d | 4476 | |
8668a3c4 | 4477 | kvm_mmu_reset_context(vcpu); |
6aa8b732 AK |
4478 | } |
4479 | ||
401d10de AS |
4480 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
4481 | { | |
4482 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
26bb0981 AK |
4483 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
4484 | ||
4485 | if (!msr) | |
4486 | return; | |
401d10de | 4487 | |
44ea2b17 AK |
4488 | /* |
4489 | * Force kernel_gs_base reloading before EFER changes, as control | |
4490 | * of this msr depends on is_long_mode(). | |
4491 | */ | |
4492 | vmx_load_host_state(to_vmx(vcpu)); | |
f6801dff | 4493 | vcpu->arch.efer = efer; |
401d10de | 4494 | if (efer & EFER_LMA) { |
2961e876 | 4495 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
4496 | msr->data = efer; |
4497 | } else { | |
2961e876 | 4498 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
4499 | |
4500 | msr->data = efer & ~EFER_LME; | |
4501 | } | |
4502 | setup_msrs(vmx); | |
4503 | } | |
4504 | ||
05b3e0c2 | 4505 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
4506 | |
4507 | static void enter_lmode(struct kvm_vcpu *vcpu) | |
4508 | { | |
4509 | u32 guest_tr_ar; | |
4510 | ||
2fb92db1 AK |
4511 | vmx_segment_cache_clear(to_vmx(vcpu)); |
4512 | ||
6aa8b732 | 4513 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
4d283ec9 | 4514 | if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) { |
bd80158a JK |
4515 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
4516 | __func__); | |
6aa8b732 | 4517 | vmcs_write32(GUEST_TR_AR_BYTES, |
4d283ec9 AL |
4518 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
4519 | | VMX_AR_TYPE_BUSY_64_TSS); | |
6aa8b732 | 4520 | } |
da38f438 | 4521 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
6aa8b732 AK |
4522 | } |
4523 | ||
4524 | static void exit_lmode(struct kvm_vcpu *vcpu) | |
4525 | { | |
2961e876 | 4526 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
da38f438 | 4527 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
6aa8b732 AK |
4528 | } |
4529 | ||
4530 | #endif | |
4531 | ||
c2ba05cc WL |
4532 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
4533 | bool invalidate_gpa) | |
2384d2b3 | 4534 | { |
c2ba05cc | 4535 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
dd180b3e XG |
4536 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
4537 | return; | |
995f00a6 | 4538 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
f0b98c02 JM |
4539 | } else { |
4540 | vpid_sync_context(vpid); | |
dd180b3e | 4541 | } |
2384d2b3 SY |
4542 | } |
4543 | ||
c2ba05cc | 4544 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
dd5f5341 | 4545 | { |
c2ba05cc | 4546 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
dd5f5341 WL |
4547 | } |
4548 | ||
e8467fda AK |
4549 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
4550 | { | |
4551 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; | |
4552 | ||
4553 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; | |
4554 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; | |
4555 | } | |
4556 | ||
aff48baa AK |
4557 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
4558 | { | |
b4d18517 | 4559 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
aff48baa AK |
4560 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
4561 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
4562 | } | |
4563 | ||
25c4c276 | 4564 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
399badf3 | 4565 | { |
fc78f519 AK |
4566 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
4567 | ||
4568 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; | |
4569 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; | |
399badf3 AK |
4570 | } |
4571 | ||
1439442c SY |
4572 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
4573 | { | |
d0d538b9 GN |
4574 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4575 | ||
6de4f3ad AK |
4576 | if (!test_bit(VCPU_EXREG_PDPTR, |
4577 | (unsigned long *)&vcpu->arch.regs_dirty)) | |
4578 | return; | |
4579 | ||
1439442c | 4580 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
d0d538b9 GN |
4581 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
4582 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); | |
4583 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); | |
4584 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); | |
1439442c SY |
4585 | } |
4586 | } | |
4587 | ||
8f5d549f AK |
4588 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
4589 | { | |
d0d538b9 GN |
4590 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4591 | ||
8f5d549f | 4592 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
d0d538b9 GN |
4593 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
4594 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); | |
4595 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); | |
4596 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); | |
8f5d549f | 4597 | } |
6de4f3ad AK |
4598 | |
4599 | __set_bit(VCPU_EXREG_PDPTR, | |
4600 | (unsigned long *)&vcpu->arch.regs_avail); | |
4601 | __set_bit(VCPU_EXREG_PDPTR, | |
4602 | (unsigned long *)&vcpu->arch.regs_dirty); | |
8f5d549f AK |
4603 | } |
4604 | ||
3899152c DM |
4605 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
4606 | { | |
6677f3da PB |
4607 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
4608 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; | |
3899152c DM |
4609 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
4610 | ||
6677f3da | 4611 | if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
3899152c DM |
4612 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
4613 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) | |
4614 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); | |
4615 | ||
4616 | return fixed_bits_valid(val, fixed0, fixed1); | |
4617 | } | |
4618 | ||
4619 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4620 | { | |
6677f3da PB |
4621 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
4622 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; | |
3899152c DM |
4623 | |
4624 | return fixed_bits_valid(val, fixed0, fixed1); | |
4625 | } | |
4626 | ||
4627 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4628 | { | |
6677f3da PB |
4629 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0; |
4630 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1; | |
3899152c DM |
4631 | |
4632 | return fixed_bits_valid(val, fixed0, fixed1); | |
4633 | } | |
4634 | ||
4635 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ | |
4636 | #define nested_guest_cr4_valid nested_cr4_valid | |
4637 | #define nested_host_cr4_valid nested_cr4_valid | |
4638 | ||
5e1746d6 | 4639 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
1439442c SY |
4640 | |
4641 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, | |
4642 | unsigned long cr0, | |
4643 | struct kvm_vcpu *vcpu) | |
4644 | { | |
5233dd51 MT |
4645 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
4646 | vmx_decache_cr3(vcpu); | |
1439442c SY |
4647 | if (!(cr0 & X86_CR0_PG)) { |
4648 | /* From paging/starting to nonpaging */ | |
4649 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4650 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
1439442c SY |
4651 | (CPU_BASED_CR3_LOAD_EXITING | |
4652 | CPU_BASED_CR3_STORE_EXITING)); | |
4653 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4654 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c SY |
4655 | } else if (!is_paging(vcpu)) { |
4656 | /* From nonpaging to paging */ | |
4657 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4658 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
1439442c SY |
4659 | ~(CPU_BASED_CR3_LOAD_EXITING | |
4660 | CPU_BASED_CR3_STORE_EXITING)); | |
4661 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4662 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c | 4663 | } |
95eb84a7 SY |
4664 | |
4665 | if (!(cr0 & X86_CR0_WP)) | |
4666 | *hw_cr0 &= ~X86_CR0_WP; | |
1439442c SY |
4667 | } |
4668 | ||
6aa8b732 AK |
4669 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
4670 | { | |
7ffd92c5 | 4671 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3a624e29 NK |
4672 | unsigned long hw_cr0; |
4673 | ||
5037878e | 4674 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
3a624e29 | 4675 | if (enable_unrestricted_guest) |
5037878e | 4676 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
218e763f | 4677 | else { |
5037878e | 4678 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
1439442c | 4679 | |
218e763f GN |
4680 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
4681 | enter_pmode(vcpu); | |
6aa8b732 | 4682 | |
218e763f GN |
4683 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
4684 | enter_rmode(vcpu); | |
4685 | } | |
6aa8b732 | 4686 | |
05b3e0c2 | 4687 | #ifdef CONFIG_X86_64 |
f6801dff | 4688 | if (vcpu->arch.efer & EFER_LME) { |
707d92fa | 4689 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
6aa8b732 | 4690 | enter_lmode(vcpu); |
707d92fa | 4691 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
6aa8b732 AK |
4692 | exit_lmode(vcpu); |
4693 | } | |
4694 | #endif | |
4695 | ||
b4d18517 | 4696 | if (enable_ept && !enable_unrestricted_guest) |
1439442c SY |
4697 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
4698 | ||
6aa8b732 | 4699 | vmcs_writel(CR0_READ_SHADOW, cr0); |
1439442c | 4700 | vmcs_writel(GUEST_CR0, hw_cr0); |
ad312c7c | 4701 | vcpu->arch.cr0 = cr0; |
14168786 GN |
4702 | |
4703 | /* depends on vcpu->arch.cr0 to be set to a new value */ | |
4704 | vmx->emulation_required = emulation_required(vcpu); | |
6aa8b732 AK |
4705 | } |
4706 | ||
855feb67 YZ |
4707 | static int get_ept_level(struct kvm_vcpu *vcpu) |
4708 | { | |
4709 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) | |
4710 | return 5; | |
4711 | return 4; | |
4712 | } | |
4713 | ||
995f00a6 | 4714 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
1439442c | 4715 | { |
855feb67 YZ |
4716 | u64 eptp = VMX_EPTP_MT_WB; |
4717 | ||
4718 | eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4; | |
1439442c | 4719 | |
995f00a6 PF |
4720 | if (enable_ept_ad_bits && |
4721 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) | |
bb97a016 | 4722 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
1439442c SY |
4723 | eptp |= (root_hpa & PAGE_MASK); |
4724 | ||
4725 | return eptp; | |
4726 | } | |
4727 | ||
6aa8b732 AK |
4728 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
4729 | { | |
1439442c SY |
4730 | unsigned long guest_cr3; |
4731 | u64 eptp; | |
4732 | ||
4733 | guest_cr3 = cr3; | |
089d034e | 4734 | if (enable_ept) { |
995f00a6 | 4735 | eptp = construct_eptp(vcpu, cr3); |
1439442c | 4736 | vmcs_write64(EPT_POINTER, eptp); |
e90008df SC |
4737 | if (enable_unrestricted_guest || is_paging(vcpu) || |
4738 | is_guest_mode(vcpu)) | |
59ab5a8f JK |
4739 | guest_cr3 = kvm_read_cr3(vcpu); |
4740 | else | |
40bbb9d0 | 4741 | guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr; |
7c93be44 | 4742 | ept_load_pdptrs(vcpu); |
1439442c SY |
4743 | } |
4744 | ||
c2ba05cc | 4745 | vmx_flush_tlb(vcpu, true); |
1439442c | 4746 | vmcs_writel(GUEST_CR3, guest_cr3); |
6aa8b732 AK |
4747 | } |
4748 | ||
5e1746d6 | 4749 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
6aa8b732 | 4750 | { |
085e68ee BS |
4751 | /* |
4752 | * Pass through host's Machine Check Enable value to hw_cr4, which | |
4753 | * is in force while we are in guest mode. Do not let guests control | |
4754 | * this bit, even if host CR4.MCE == 0. | |
4755 | */ | |
5dc1f044 SC |
4756 | unsigned long hw_cr4; |
4757 | ||
4758 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); | |
4759 | if (enable_unrestricted_guest) | |
4760 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; | |
4761 | else if (to_vmx(vcpu)->rmode.vm86_active) | |
4762 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; | |
4763 | else | |
4764 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; | |
1439442c | 4765 | |
0367f205 PB |
4766 | if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) { |
4767 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
4768 | SECONDARY_EXEC_DESC); | |
4769 | hw_cr4 &= ~X86_CR4_UMIP; | |
99158246 RK |
4770 | } else if (!is_guest_mode(vcpu) || |
4771 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) | |
0367f205 PB |
4772 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
4773 | SECONDARY_EXEC_DESC); | |
4774 | ||
5e1746d6 NHE |
4775 | if (cr4 & X86_CR4_VMXE) { |
4776 | /* | |
4777 | * To use VMXON (and later other VMX instructions), a guest | |
4778 | * must first be able to turn on cr4.VMXE (see handle_vmon()). | |
4779 | * So basically the check on whether to allow nested VMX | |
4780 | * is here. | |
4781 | */ | |
4782 | if (!nested_vmx_allowed(vcpu)) | |
4783 | return 1; | |
1a0d74e6 | 4784 | } |
3899152c DM |
4785 | |
4786 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) | |
5e1746d6 NHE |
4787 | return 1; |
4788 | ||
ad312c7c | 4789 | vcpu->arch.cr4 = cr4; |
5dc1f044 SC |
4790 | |
4791 | if (!enable_unrestricted_guest) { | |
4792 | if (enable_ept) { | |
4793 | if (!is_paging(vcpu)) { | |
4794 | hw_cr4 &= ~X86_CR4_PAE; | |
4795 | hw_cr4 |= X86_CR4_PSE; | |
4796 | } else if (!(cr4 & X86_CR4_PAE)) { | |
4797 | hw_cr4 &= ~X86_CR4_PAE; | |
4798 | } | |
bc23008b | 4799 | } |
1439442c | 4800 | |
656ec4a4 | 4801 | /* |
ddba2628 HH |
4802 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
4803 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs | |
4804 | * to be manually disabled when guest switches to non-paging | |
4805 | * mode. | |
4806 | * | |
4807 | * If !enable_unrestricted_guest, the CPU is always running | |
4808 | * with CR0.PG=1 and CR4 needs to be modified. | |
4809 | * If enable_unrestricted_guest, the CPU automatically | |
4810 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. | |
656ec4a4 | 4811 | */ |
5dc1f044 SC |
4812 | if (!is_paging(vcpu)) |
4813 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); | |
4814 | } | |
656ec4a4 | 4815 | |
1439442c SY |
4816 | vmcs_writel(CR4_READ_SHADOW, cr4); |
4817 | vmcs_writel(GUEST_CR4, hw_cr4); | |
5e1746d6 | 4818 | return 0; |
6aa8b732 AK |
4819 | } |
4820 | ||
6aa8b732 AK |
4821 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
4822 | struct kvm_segment *var, int seg) | |
4823 | { | |
a9179499 | 4824 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 AK |
4825 | u32 ar; |
4826 | ||
c6ad1153 | 4827 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
f5f7b2fe | 4828 | *var = vmx->rmode.segs[seg]; |
a9179499 | 4829 | if (seg == VCPU_SREG_TR |
2fb92db1 | 4830 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
f5f7b2fe | 4831 | return; |
1390a28b AK |
4832 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4833 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4834 | return; | |
a9179499 | 4835 | } |
2fb92db1 AK |
4836 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4837 | var->limit = vmx_read_guest_seg_limit(vmx, seg); | |
4838 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4839 | ar = vmx_read_guest_seg_ar(vmx, seg); | |
03617c18 | 4840 | var->unusable = (ar >> 16) & 1; |
6aa8b732 AK |
4841 | var->type = ar & 15; |
4842 | var->s = (ar >> 4) & 1; | |
4843 | var->dpl = (ar >> 5) & 3; | |
03617c18 GN |
4844 | /* |
4845 | * Some userspaces do not preserve unusable property. Since usable | |
4846 | * segment has to be present according to VMX spec we can use present | |
4847 | * property to amend userspace bug by making unusable segment always | |
4848 | * nonpresent. vmx_segment_access_rights() already marks nonpresent | |
4849 | * segment as unusable. | |
4850 | */ | |
4851 | var->present = !var->unusable; | |
6aa8b732 AK |
4852 | var->avl = (ar >> 12) & 1; |
4853 | var->l = (ar >> 13) & 1; | |
4854 | var->db = (ar >> 14) & 1; | |
4855 | var->g = (ar >> 15) & 1; | |
6aa8b732 AK |
4856 | } |
4857 | ||
a9179499 AK |
4858 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
4859 | { | |
a9179499 AK |
4860 | struct kvm_segment s; |
4861 | ||
4862 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
4863 | vmx_get_segment(vcpu, &s, seg); | |
4864 | return s.base; | |
4865 | } | |
2fb92db1 | 4866 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
a9179499 AK |
4867 | } |
4868 | ||
b09408d0 | 4869 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
2e4d2653 | 4870 | { |
b09408d0 MT |
4871 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
4872 | ||
ae9fedc7 | 4873 | if (unlikely(vmx->rmode.vm86_active)) |
2e4d2653 | 4874 | return 0; |
ae9fedc7 PB |
4875 | else { |
4876 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); | |
4d283ec9 | 4877 | return VMX_AR_DPL(ar); |
69c73028 | 4878 | } |
69c73028 AK |
4879 | } |
4880 | ||
653e3108 | 4881 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
6aa8b732 | 4882 | { |
6aa8b732 AK |
4883 | u32 ar; |
4884 | ||
f0495f9b | 4885 | if (var->unusable || !var->present) |
6aa8b732 AK |
4886 | ar = 1 << 16; |
4887 | else { | |
4888 | ar = var->type & 15; | |
4889 | ar |= (var->s & 1) << 4; | |
4890 | ar |= (var->dpl & 3) << 5; | |
4891 | ar |= (var->present & 1) << 7; | |
4892 | ar |= (var->avl & 1) << 12; | |
4893 | ar |= (var->l & 1) << 13; | |
4894 | ar |= (var->db & 1) << 14; | |
4895 | ar |= (var->g & 1) << 15; | |
4896 | } | |
653e3108 AK |
4897 | |
4898 | return ar; | |
4899 | } | |
4900 | ||
4901 | static void vmx_set_segment(struct kvm_vcpu *vcpu, | |
4902 | struct kvm_segment *var, int seg) | |
4903 | { | |
7ffd92c5 | 4904 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
772e0318 | 4905 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
653e3108 | 4906 | |
2fb92db1 AK |
4907 | vmx_segment_cache_clear(vmx); |
4908 | ||
1ecd50a9 GN |
4909 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
4910 | vmx->rmode.segs[seg] = *var; | |
4911 | if (seg == VCPU_SREG_TR) | |
4912 | vmcs_write16(sf->selector, var->selector); | |
4913 | else if (var->s) | |
4914 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); | |
d99e4152 | 4915 | goto out; |
653e3108 | 4916 | } |
1ecd50a9 | 4917 | |
653e3108 AK |
4918 | vmcs_writel(sf->base, var->base); |
4919 | vmcs_write32(sf->limit, var->limit); | |
4920 | vmcs_write16(sf->selector, var->selector); | |
3a624e29 NK |
4921 | |
4922 | /* | |
4923 | * Fix the "Accessed" bit in AR field of segment registers for older | |
4924 | * qemu binaries. | |
4925 | * IA32 arch specifies that at the time of processor reset the | |
4926 | * "Accessed" bit in the AR field of segment registers is 1. And qemu | |
0fa06071 | 4927 | * is setting it to 0 in the userland code. This causes invalid guest |
3a624e29 NK |
4928 | * state vmexit when "unrestricted guest" mode is turned on. |
4929 | * Fix for this setup issue in cpu_reset is being pushed in the qemu | |
4930 | * tree. Newer qemu binaries with that qemu fix would not need this | |
4931 | * kvm hack. | |
4932 | */ | |
4933 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) | |
f924d66d | 4934 | var->type |= 0x1; /* Accessed */ |
3a624e29 | 4935 | |
f924d66d | 4936 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
d99e4152 GN |
4937 | |
4938 | out: | |
98eb2f8b | 4939 | vmx->emulation_required = emulation_required(vcpu); |
6aa8b732 AK |
4940 | } |
4941 | ||
6aa8b732 AK |
4942 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
4943 | { | |
2fb92db1 | 4944 | u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS); |
6aa8b732 AK |
4945 | |
4946 | *db = (ar >> 14) & 1; | |
4947 | *l = (ar >> 13) & 1; | |
4948 | } | |
4949 | ||
89a27f4d | 4950 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4951 | { |
89a27f4d GN |
4952 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
4953 | dt->address = vmcs_readl(GUEST_IDTR_BASE); | |
6aa8b732 AK |
4954 | } |
4955 | ||
89a27f4d | 4956 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4957 | { |
89a27f4d GN |
4958 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
4959 | vmcs_writel(GUEST_IDTR_BASE, dt->address); | |
6aa8b732 AK |
4960 | } |
4961 | ||
89a27f4d | 4962 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4963 | { |
89a27f4d GN |
4964 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
4965 | dt->address = vmcs_readl(GUEST_GDTR_BASE); | |
6aa8b732 AK |
4966 | } |
4967 | ||
89a27f4d | 4968 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4969 | { |
89a27f4d GN |
4970 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
4971 | vmcs_writel(GUEST_GDTR_BASE, dt->address); | |
6aa8b732 AK |
4972 | } |
4973 | ||
648dfaa7 MG |
4974 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
4975 | { | |
4976 | struct kvm_segment var; | |
4977 | u32 ar; | |
4978 | ||
4979 | vmx_get_segment(vcpu, &var, seg); | |
07f42f5f | 4980 | var.dpl = 0x3; |
0647f4aa GN |
4981 | if (seg == VCPU_SREG_CS) |
4982 | var.type = 0x3; | |
648dfaa7 MG |
4983 | ar = vmx_segment_access_rights(&var); |
4984 | ||
4985 | if (var.base != (var.selector << 4)) | |
4986 | return false; | |
89efbed0 | 4987 | if (var.limit != 0xffff) |
648dfaa7 | 4988 | return false; |
07f42f5f | 4989 | if (ar != 0xf3) |
648dfaa7 MG |
4990 | return false; |
4991 | ||
4992 | return true; | |
4993 | } | |
4994 | ||
4995 | static bool code_segment_valid(struct kvm_vcpu *vcpu) | |
4996 | { | |
4997 | struct kvm_segment cs; | |
4998 | unsigned int cs_rpl; | |
4999 | ||
5000 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
b32a9918 | 5001 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 5002 | |
1872a3f4 AK |
5003 | if (cs.unusable) |
5004 | return false; | |
4d283ec9 | 5005 | if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK)) |
648dfaa7 MG |
5006 | return false; |
5007 | if (!cs.s) | |
5008 | return false; | |
4d283ec9 | 5009 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
648dfaa7 MG |
5010 | if (cs.dpl > cs_rpl) |
5011 | return false; | |
1872a3f4 | 5012 | } else { |
648dfaa7 MG |
5013 | if (cs.dpl != cs_rpl) |
5014 | return false; | |
5015 | } | |
5016 | if (!cs.present) | |
5017 | return false; | |
5018 | ||
5019 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ | |
5020 | return true; | |
5021 | } | |
5022 | ||
5023 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) | |
5024 | { | |
5025 | struct kvm_segment ss; | |
5026 | unsigned int ss_rpl; | |
5027 | ||
5028 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
b32a9918 | 5029 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 5030 | |
1872a3f4 AK |
5031 | if (ss.unusable) |
5032 | return true; | |
5033 | if (ss.type != 3 && ss.type != 7) | |
648dfaa7 MG |
5034 | return false; |
5035 | if (!ss.s) | |
5036 | return false; | |
5037 | if (ss.dpl != ss_rpl) /* DPL != RPL */ | |
5038 | return false; | |
5039 | if (!ss.present) | |
5040 | return false; | |
5041 | ||
5042 | return true; | |
5043 | } | |
5044 | ||
5045 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) | |
5046 | { | |
5047 | struct kvm_segment var; | |
5048 | unsigned int rpl; | |
5049 | ||
5050 | vmx_get_segment(vcpu, &var, seg); | |
b32a9918 | 5051 | rpl = var.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 5052 | |
1872a3f4 AK |
5053 | if (var.unusable) |
5054 | return true; | |
648dfaa7 MG |
5055 | if (!var.s) |
5056 | return false; | |
5057 | if (!var.present) | |
5058 | return false; | |
4d283ec9 | 5059 | if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) { |
648dfaa7 MG |
5060 | if (var.dpl < rpl) /* DPL < RPL */ |
5061 | return false; | |
5062 | } | |
5063 | ||
5064 | /* TODO: Add other members to kvm_segment_field to allow checking for other access | |
5065 | * rights flags | |
5066 | */ | |
5067 | return true; | |
5068 | } | |
5069 | ||
5070 | static bool tr_valid(struct kvm_vcpu *vcpu) | |
5071 | { | |
5072 | struct kvm_segment tr; | |
5073 | ||
5074 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); | |
5075 | ||
1872a3f4 AK |
5076 | if (tr.unusable) |
5077 | return false; | |
b32a9918 | 5078 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 | 5079 | return false; |
1872a3f4 | 5080 | if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ |
648dfaa7 MG |
5081 | return false; |
5082 | if (!tr.present) | |
5083 | return false; | |
5084 | ||
5085 | return true; | |
5086 | } | |
5087 | ||
5088 | static bool ldtr_valid(struct kvm_vcpu *vcpu) | |
5089 | { | |
5090 | struct kvm_segment ldtr; | |
5091 | ||
5092 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); | |
5093 | ||
1872a3f4 AK |
5094 | if (ldtr.unusable) |
5095 | return true; | |
b32a9918 | 5096 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 MG |
5097 | return false; |
5098 | if (ldtr.type != 2) | |
5099 | return false; | |
5100 | if (!ldtr.present) | |
5101 | return false; | |
5102 | ||
5103 | return true; | |
5104 | } | |
5105 | ||
5106 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) | |
5107 | { | |
5108 | struct kvm_segment cs, ss; | |
5109 | ||
5110 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
5111 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
5112 | ||
b32a9918 NA |
5113 | return ((cs.selector & SEGMENT_RPL_MASK) == |
5114 | (ss.selector & SEGMENT_RPL_MASK)); | |
648dfaa7 MG |
5115 | } |
5116 | ||
5117 | /* | |
5118 | * Check if guest state is valid. Returns true if valid, false if | |
5119 | * not. | |
5120 | * We assume that registers are always usable | |
5121 | */ | |
5122 | static bool guest_state_valid(struct kvm_vcpu *vcpu) | |
5123 | { | |
c5e97c80 GN |
5124 | if (enable_unrestricted_guest) |
5125 | return true; | |
5126 | ||
648dfaa7 | 5127 | /* real mode guest state checks */ |
f13882d8 | 5128 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
648dfaa7 MG |
5129 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
5130 | return false; | |
5131 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) | |
5132 | return false; | |
5133 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) | |
5134 | return false; | |
5135 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) | |
5136 | return false; | |
5137 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) | |
5138 | return false; | |
5139 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) | |
5140 | return false; | |
5141 | } else { | |
5142 | /* protected mode guest state checks */ | |
5143 | if (!cs_ss_rpl_check(vcpu)) | |
5144 | return false; | |
5145 | if (!code_segment_valid(vcpu)) | |
5146 | return false; | |
5147 | if (!stack_segment_valid(vcpu)) | |
5148 | return false; | |
5149 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) | |
5150 | return false; | |
5151 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) | |
5152 | return false; | |
5153 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) | |
5154 | return false; | |
5155 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) | |
5156 | return false; | |
5157 | if (!tr_valid(vcpu)) | |
5158 | return false; | |
5159 | if (!ldtr_valid(vcpu)) | |
5160 | return false; | |
5161 | } | |
5162 | /* TODO: | |
5163 | * - Add checks on RIP | |
5164 | * - Add checks on RFLAGS | |
5165 | */ | |
5166 | ||
5167 | return true; | |
5168 | } | |
5169 | ||
5fa99cbe JM |
5170 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
5171 | { | |
5172 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); | |
5173 | } | |
5174 | ||
d77c26fc | 5175 | static int init_rmode_tss(struct kvm *kvm) |
6aa8b732 | 5176 | { |
40dcaa9f | 5177 | gfn_t fn; |
195aefde | 5178 | u16 data = 0; |
1f755a82 | 5179 | int idx, r; |
6aa8b732 | 5180 | |
40dcaa9f | 5181 | idx = srcu_read_lock(&kvm->srcu); |
40bbb9d0 | 5182 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
195aefde IE |
5183 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
5184 | if (r < 0) | |
10589a46 | 5185 | goto out; |
195aefde | 5186 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
464d17c8 SY |
5187 | r = kvm_write_guest_page(kvm, fn++, &data, |
5188 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); | |
195aefde | 5189 | if (r < 0) |
10589a46 | 5190 | goto out; |
195aefde IE |
5191 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
5192 | if (r < 0) | |
10589a46 | 5193 | goto out; |
195aefde IE |
5194 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
5195 | if (r < 0) | |
10589a46 | 5196 | goto out; |
195aefde | 5197 | data = ~0; |
10589a46 MT |
5198 | r = kvm_write_guest_page(kvm, fn, &data, |
5199 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, | |
5200 | sizeof(u8)); | |
10589a46 | 5201 | out: |
40dcaa9f | 5202 | srcu_read_unlock(&kvm->srcu, idx); |
1f755a82 | 5203 | return r; |
6aa8b732 AK |
5204 | } |
5205 | ||
b7ebfb05 SY |
5206 | static int init_rmode_identity_map(struct kvm *kvm) |
5207 | { | |
40bbb9d0 | 5208 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
f51770ed | 5209 | int i, idx, r = 0; |
ba049e93 | 5210 | kvm_pfn_t identity_map_pfn; |
b7ebfb05 SY |
5211 | u32 tmp; |
5212 | ||
40bbb9d0 | 5213 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
a255d479 TC |
5214 | mutex_lock(&kvm->slots_lock); |
5215 | ||
40bbb9d0 | 5216 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
a255d479 | 5217 | goto out2; |
a255d479 | 5218 | |
40bbb9d0 SC |
5219 | if (!kvm_vmx->ept_identity_map_addr) |
5220 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; | |
5221 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; | |
a255d479 | 5222 | |
d8a6e365 | 5223 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
40bbb9d0 | 5224 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
f51770ed | 5225 | if (r < 0) |
a255d479 TC |
5226 | goto out2; |
5227 | ||
40dcaa9f | 5228 | idx = srcu_read_lock(&kvm->srcu); |
b7ebfb05 SY |
5229 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
5230 | if (r < 0) | |
5231 | goto out; | |
5232 | /* Set up identity-mapping pagetable for EPT in real mode */ | |
5233 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { | |
5234 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | | |
5235 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); | |
5236 | r = kvm_write_guest_page(kvm, identity_map_pfn, | |
5237 | &tmp, i * sizeof(tmp), sizeof(tmp)); | |
5238 | if (r < 0) | |
5239 | goto out; | |
5240 | } | |
40bbb9d0 | 5241 | kvm_vmx->ept_identity_pagetable_done = true; |
f51770ed | 5242 | |
b7ebfb05 | 5243 | out: |
40dcaa9f | 5244 | srcu_read_unlock(&kvm->srcu, idx); |
a255d479 TC |
5245 | |
5246 | out2: | |
5247 | mutex_unlock(&kvm->slots_lock); | |
f51770ed | 5248 | return r; |
b7ebfb05 SY |
5249 | } |
5250 | ||
6aa8b732 AK |
5251 | static void seg_setup(int seg) |
5252 | { | |
772e0318 | 5253 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
3a624e29 | 5254 | unsigned int ar; |
6aa8b732 AK |
5255 | |
5256 | vmcs_write16(sf->selector, 0); | |
5257 | vmcs_writel(sf->base, 0); | |
5258 | vmcs_write32(sf->limit, 0xffff); | |
d54d07b2 GN |
5259 | ar = 0x93; |
5260 | if (seg == VCPU_SREG_CS) | |
5261 | ar |= 0x08; /* code segment */ | |
3a624e29 NK |
5262 | |
5263 | vmcs_write32(sf->ar_bytes, ar); | |
6aa8b732 AK |
5264 | } |
5265 | ||
f78e0e2e SY |
5266 | static int alloc_apic_access_page(struct kvm *kvm) |
5267 | { | |
4484141a | 5268 | struct page *page; |
f78e0e2e SY |
5269 | int r = 0; |
5270 | ||
79fac95e | 5271 | mutex_lock(&kvm->slots_lock); |
c24ae0dc | 5272 | if (kvm->arch.apic_access_page_done) |
f78e0e2e | 5273 | goto out; |
1d8007bd PB |
5274 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
5275 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); | |
f78e0e2e SY |
5276 | if (r) |
5277 | goto out; | |
72dc67a6 | 5278 | |
73a6d941 | 5279 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
4484141a XG |
5280 | if (is_error_page(page)) { |
5281 | r = -EFAULT; | |
5282 | goto out; | |
5283 | } | |
5284 | ||
c24ae0dc TC |
5285 | /* |
5286 | * Do not pin the page in memory, so that memory hot-unplug | |
5287 | * is able to migrate it. | |
5288 | */ | |
5289 | put_page(page); | |
5290 | kvm->arch.apic_access_page_done = true; | |
f78e0e2e | 5291 | out: |
79fac95e | 5292 | mutex_unlock(&kvm->slots_lock); |
f78e0e2e SY |
5293 | return r; |
5294 | } | |
5295 | ||
991e7a0e | 5296 | static int allocate_vpid(void) |
2384d2b3 SY |
5297 | { |
5298 | int vpid; | |
5299 | ||
919818ab | 5300 | if (!enable_vpid) |
991e7a0e | 5301 | return 0; |
2384d2b3 SY |
5302 | spin_lock(&vmx_vpid_lock); |
5303 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); | |
991e7a0e | 5304 | if (vpid < VMX_NR_VPIDS) |
2384d2b3 | 5305 | __set_bit(vpid, vmx_vpid_bitmap); |
991e7a0e WL |
5306 | else |
5307 | vpid = 0; | |
2384d2b3 | 5308 | spin_unlock(&vmx_vpid_lock); |
991e7a0e | 5309 | return vpid; |
2384d2b3 SY |
5310 | } |
5311 | ||
991e7a0e | 5312 | static void free_vpid(int vpid) |
cdbecfc3 | 5313 | { |
991e7a0e | 5314 | if (!enable_vpid || vpid == 0) |
cdbecfc3 LJ |
5315 | return; |
5316 | spin_lock(&vmx_vpid_lock); | |
991e7a0e | 5317 | __clear_bit(vpid, vmx_vpid_bitmap); |
cdbecfc3 LJ |
5318 | spin_unlock(&vmx_vpid_lock); |
5319 | } | |
5320 | ||
904e14fb PB |
5321 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
5322 | u32 msr, int type) | |
25c5f225 | 5323 | { |
3e7c73e9 | 5324 | int f = sizeof(unsigned long); |
25c5f225 SY |
5325 | |
5326 | if (!cpu_has_vmx_msr_bitmap()) | |
5327 | return; | |
5328 | ||
5329 | /* | |
5330 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
5331 | * have the write-low and read-high bitmap offsets the wrong way round. | |
5332 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
5333 | */ | |
25c5f225 | 5334 | if (msr <= 0x1fff) { |
8d14695f YZ |
5335 | if (type & MSR_TYPE_R) |
5336 | /* read-low */ | |
5337 | __clear_bit(msr, msr_bitmap + 0x000 / f); | |
5338 | ||
5339 | if (type & MSR_TYPE_W) | |
5340 | /* write-low */ | |
5341 | __clear_bit(msr, msr_bitmap + 0x800 / f); | |
5342 | ||
25c5f225 SY |
5343 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
5344 | msr &= 0x1fff; | |
8d14695f YZ |
5345 | if (type & MSR_TYPE_R) |
5346 | /* read-high */ | |
5347 | __clear_bit(msr, msr_bitmap + 0x400 / f); | |
5348 | ||
5349 | if (type & MSR_TYPE_W) | |
5350 | /* write-high */ | |
5351 | __clear_bit(msr, msr_bitmap + 0xc00 / f); | |
5352 | ||
5353 | } | |
5354 | } | |
5355 | ||
904e14fb PB |
5356 | static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
5357 | u32 msr, int type) | |
5358 | { | |
5359 | int f = sizeof(unsigned long); | |
5360 | ||
5361 | if (!cpu_has_vmx_msr_bitmap()) | |
5362 | return; | |
5363 | ||
5364 | /* | |
5365 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
5366 | * have the write-low and read-high bitmap offsets the wrong way round. | |
5367 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
5368 | */ | |
5369 | if (msr <= 0x1fff) { | |
5370 | if (type & MSR_TYPE_R) | |
5371 | /* read-low */ | |
5372 | __set_bit(msr, msr_bitmap + 0x000 / f); | |
5373 | ||
5374 | if (type & MSR_TYPE_W) | |
5375 | /* write-low */ | |
5376 | __set_bit(msr, msr_bitmap + 0x800 / f); | |
5377 | ||
5378 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
5379 | msr &= 0x1fff; | |
5380 | if (type & MSR_TYPE_R) | |
5381 | /* read-high */ | |
5382 | __set_bit(msr, msr_bitmap + 0x400 / f); | |
5383 | ||
5384 | if (type & MSR_TYPE_W) | |
5385 | /* write-high */ | |
5386 | __set_bit(msr, msr_bitmap + 0xc00 / f); | |
5387 | ||
5388 | } | |
5389 | } | |
5390 | ||
5391 | static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap, | |
5392 | u32 msr, int type, bool value) | |
5393 | { | |
5394 | if (value) | |
5395 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); | |
5396 | else | |
5397 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); | |
5398 | } | |
5399 | ||
f2b93280 WV |
5400 | /* |
5401 | * If a msr is allowed by L0, we should check whether it is allowed by L1. | |
5402 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. | |
5403 | */ | |
5404 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, | |
5405 | unsigned long *msr_bitmap_nested, | |
5406 | u32 msr, int type) | |
5407 | { | |
5408 | int f = sizeof(unsigned long); | |
5409 | ||
f2b93280 WV |
5410 | /* |
5411 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
5412 | * have the write-low and read-high bitmap offsets the wrong way round. | |
5413 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
5414 | */ | |
5415 | if (msr <= 0x1fff) { | |
5416 | if (type & MSR_TYPE_R && | |
5417 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) | |
5418 | /* read-low */ | |
5419 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); | |
5420 | ||
5421 | if (type & MSR_TYPE_W && | |
5422 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) | |
5423 | /* write-low */ | |
5424 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); | |
5425 | ||
5426 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
5427 | msr &= 0x1fff; | |
5428 | if (type & MSR_TYPE_R && | |
5429 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) | |
5430 | /* read-high */ | |
5431 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); | |
5432 | ||
5433 | if (type & MSR_TYPE_W && | |
5434 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) | |
5435 | /* write-high */ | |
5436 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); | |
5437 | ||
5438 | } | |
5439 | } | |
5440 | ||
904e14fb | 5441 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
5897297b | 5442 | { |
904e14fb PB |
5443 | u8 mode = 0; |
5444 | ||
5445 | if (cpu_has_secondary_exec_ctrls() && | |
5446 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & | |
5447 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { | |
5448 | mode |= MSR_BITMAP_MODE_X2APIC; | |
5449 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) | |
5450 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; | |
5451 | } | |
5452 | ||
5453 | if (is_long_mode(vcpu)) | |
5454 | mode |= MSR_BITMAP_MODE_LM; | |
5455 | ||
5456 | return mode; | |
8d14695f YZ |
5457 | } |
5458 | ||
904e14fb PB |
5459 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
5460 | ||
5461 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, | |
5462 | u8 mode) | |
8d14695f | 5463 | { |
904e14fb PB |
5464 | int msr; |
5465 | ||
5466 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { | |
5467 | unsigned word = msr / BITS_PER_LONG; | |
5468 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; | |
5469 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; | |
5470 | } | |
5471 | ||
5472 | if (mode & MSR_BITMAP_MODE_X2APIC) { | |
5473 | /* | |
5474 | * TPR reads and writes can be virtualized even if virtual interrupt | |
5475 | * delivery is not in use. | |
5476 | */ | |
5477 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); | |
5478 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { | |
5479 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); | |
5480 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); | |
5481 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); | |
5482 | } | |
f6e90f9e | 5483 | } |
5897297b AK |
5484 | } |
5485 | ||
904e14fb PB |
5486 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
5487 | { | |
5488 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5489 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; | |
5490 | u8 mode = vmx_msr_bitmap_mode(vcpu); | |
5491 | u8 changed = mode ^ vmx->msr_bitmap_mode; | |
5492 | ||
5493 | if (!changed) | |
5494 | return; | |
5495 | ||
5496 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, | |
5497 | !(mode & MSR_BITMAP_MODE_LM)); | |
5498 | ||
5499 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) | |
5500 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); | |
5501 | ||
5502 | vmx->msr_bitmap_mode = mode; | |
5503 | } | |
5504 | ||
b2a05fef | 5505 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
d50ab6c1 | 5506 | { |
d62caabb | 5507 | return enable_apicv; |
d50ab6c1 PB |
5508 | } |
5509 | ||
c9f04407 DM |
5510 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
5511 | { | |
5512 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
5513 | gfn_t gfn; | |
5514 | ||
5515 | /* | |
5516 | * Don't need to mark the APIC access page dirty; it is never | |
5517 | * written to by the CPU during APIC virtualization. | |
5518 | */ | |
5519 | ||
5520 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { | |
5521 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; | |
5522 | kvm_vcpu_mark_page_dirty(vcpu, gfn); | |
5523 | } | |
5524 | ||
5525 | if (nested_cpu_has_posted_intr(vmcs12)) { | |
5526 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; | |
5527 | kvm_vcpu_mark_page_dirty(vcpu, gfn); | |
5528 | } | |
5529 | } | |
5530 | ||
5531 | ||
6342c50a | 5532 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
705699a1 WV |
5533 | { |
5534 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5535 | int max_irr; | |
5536 | void *vapic_page; | |
5537 | u16 status; | |
5538 | ||
c9f04407 DM |
5539 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
5540 | return; | |
705699a1 | 5541 | |
c9f04407 DM |
5542 | vmx->nested.pi_pending = false; |
5543 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) | |
5544 | return; | |
705699a1 | 5545 | |
c9f04407 DM |
5546 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
5547 | if (max_irr != 256) { | |
705699a1 | 5548 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
e7387b0e LA |
5549 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
5550 | vapic_page, &max_irr); | |
705699a1 WV |
5551 | kunmap(vmx->nested.virtual_apic_page); |
5552 | ||
5553 | status = vmcs_read16(GUEST_INTR_STATUS); | |
5554 | if ((u8)max_irr > ((u8)status & 0xff)) { | |
5555 | status &= ~0xff; | |
5556 | status |= (u8)max_irr; | |
5557 | vmcs_write16(GUEST_INTR_STATUS, status); | |
5558 | } | |
5559 | } | |
c9f04407 DM |
5560 | |
5561 | nested_mark_vmcs12_pages_dirty(vcpu); | |
705699a1 WV |
5562 | } |
5563 | ||
06a5524f WV |
5564 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
5565 | bool nested) | |
21bc8dc5 RK |
5566 | { |
5567 | #ifdef CONFIG_SMP | |
06a5524f WV |
5568 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
5569 | ||
21bc8dc5 | 5570 | if (vcpu->mode == IN_GUEST_MODE) { |
28b835d6 | 5571 | /* |
5753743f HZ |
5572 | * The vector of interrupt to be delivered to vcpu had |
5573 | * been set in PIR before this function. | |
5574 | * | |
5575 | * Following cases will be reached in this block, and | |
5576 | * we always send a notification event in all cases as | |
5577 | * explained below. | |
5578 | * | |
5579 | * Case 1: vcpu keeps in non-root mode. Sending a | |
5580 | * notification event posts the interrupt to vcpu. | |
5581 | * | |
5582 | * Case 2: vcpu exits to root mode and is still | |
5583 | * runnable. PIR will be synced to vIRR before the | |
5584 | * next vcpu entry. Sending a notification event in | |
5585 | * this case has no effect, as vcpu is not in root | |
5586 | * mode. | |
28b835d6 | 5587 | * |
5753743f HZ |
5588 | * Case 3: vcpu exits to root mode and is blocked. |
5589 | * vcpu_block() has already synced PIR to vIRR and | |
5590 | * never blocks vcpu if vIRR is not cleared. Therefore, | |
5591 | * a blocked vcpu here does not wait for any requested | |
5592 | * interrupts in PIR, and sending a notification event | |
5593 | * which has no effect is safe here. | |
28b835d6 | 5594 | */ |
28b835d6 | 5595 | |
06a5524f | 5596 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
21bc8dc5 RK |
5597 | return true; |
5598 | } | |
5599 | #endif | |
5600 | return false; | |
5601 | } | |
5602 | ||
705699a1 WV |
5603 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
5604 | int vector) | |
5605 | { | |
5606 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5607 | ||
5608 | if (is_guest_mode(vcpu) && | |
5609 | vector == vmx->nested.posted_intr_nv) { | |
705699a1 WV |
5610 | /* |
5611 | * If a posted intr is not recognized by hardware, | |
5612 | * we will accomplish it in the next vmentry. | |
5613 | */ | |
5614 | vmx->nested.pi_pending = true; | |
5615 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
6b697711 LA |
5616 | /* the PIR and ON have been set by L1. */ |
5617 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) | |
5618 | kvm_vcpu_kick(vcpu); | |
705699a1 WV |
5619 | return 0; |
5620 | } | |
5621 | return -1; | |
5622 | } | |
a20ed54d YZ |
5623 | /* |
5624 | * Send interrupt to vcpu via posted interrupt way. | |
5625 | * 1. If target vcpu is running(non-root mode), send posted interrupt | |
5626 | * notification to vcpu and hardware will sync PIR to vIRR atomically. | |
5627 | * 2. If target vcpu isn't running(root mode), kick it to pick up the | |
5628 | * interrupt from PIR in next vmentry. | |
5629 | */ | |
5630 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) | |
5631 | { | |
5632 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5633 | int r; | |
5634 | ||
705699a1 WV |
5635 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
5636 | if (!r) | |
5637 | return; | |
5638 | ||
a20ed54d YZ |
5639 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
5640 | return; | |
5641 | ||
b95234c8 PB |
5642 | /* If a previous notification has sent the IPI, nothing to do. */ |
5643 | if (pi_test_and_set_on(&vmx->pi_desc)) | |
5644 | return; | |
5645 | ||
06a5524f | 5646 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
a20ed54d YZ |
5647 | kvm_vcpu_kick(vcpu); |
5648 | } | |
5649 | ||
a3a8ff8e NHE |
5650 | /* |
5651 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that | |
5652 | * will not change in the lifetime of the guest. | |
5653 | * Note that host-state that does change is set elsewhere. E.g., host-state | |
5654 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. | |
5655 | */ | |
a547c6db | 5656 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
a3a8ff8e NHE |
5657 | { |
5658 | u32 low32, high32; | |
5659 | unsigned long tmpl; | |
5660 | struct desc_ptr dt; | |
d6e41f11 | 5661 | unsigned long cr0, cr3, cr4; |
a3a8ff8e | 5662 | |
04ac88ab AL |
5663 | cr0 = read_cr0(); |
5664 | WARN_ON(cr0 & X86_CR0_TS); | |
5665 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ | |
d6e41f11 AL |
5666 | |
5667 | /* | |
5668 | * Save the most likely value for this task's CR3 in the VMCS. | |
5669 | * We can't use __get_current_cr3_fast() because we're not atomic. | |
5670 | */ | |
6c690ee1 | 5671 | cr3 = __read_cr3(); |
d6e41f11 | 5672 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
44889942 | 5673 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
a3a8ff8e | 5674 | |
d974baa3 | 5675 | /* Save the most likely value for this task's CR4 in the VMCS. */ |
1e02ce4c | 5676 | cr4 = cr4_read_shadow(); |
d974baa3 | 5677 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
44889942 | 5678 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
d974baa3 | 5679 | |
a3a8ff8e | 5680 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
b2da15ac AK |
5681 | #ifdef CONFIG_X86_64 |
5682 | /* | |
5683 | * Load null selectors, so we can avoid reloading them in | |
5684 | * __vmx_load_host_state(), in case userspace uses the null selectors | |
5685 | * too (the expected case). | |
5686 | */ | |
5687 | vmcs_write16(HOST_DS_SELECTOR, 0); | |
5688 | vmcs_write16(HOST_ES_SELECTOR, 0); | |
5689 | #else | |
a3a8ff8e NHE |
5690 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5691 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ | |
b2da15ac | 5692 | #endif |
a3a8ff8e NHE |
5693 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5694 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ | |
5695 | ||
87930019 | 5696 | store_idt(&dt); |
a3a8ff8e | 5697 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
a547c6db | 5698 | vmx->host_idt_base = dt.address; |
a3a8ff8e | 5699 | |
83287ea4 | 5700 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
a3a8ff8e NHE |
5701 | |
5702 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); | |
5703 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); | |
5704 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); | |
5705 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ | |
5706 | ||
5707 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { | |
5708 | rdmsr(MSR_IA32_CR_PAT, low32, high32); | |
5709 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); | |
5710 | } | |
5711 | } | |
5712 | ||
bf8179a0 NHE |
5713 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
5714 | { | |
5715 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; | |
5716 | if (enable_ept) | |
5717 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; | |
fe3ef05c NHE |
5718 | if (is_guest_mode(&vmx->vcpu)) |
5719 | vmx->vcpu.arch.cr4_guest_owned_bits &= | |
5720 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; | |
bf8179a0 NHE |
5721 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
5722 | } | |
5723 | ||
01e439be YZ |
5724 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
5725 | { | |
5726 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; | |
5727 | ||
d62caabb | 5728 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
01e439be | 5729 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
d02fcf50 PB |
5730 | |
5731 | if (!enable_vnmi) | |
5732 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; | |
5733 | ||
64672c95 YJ |
5734 | /* Enable the preemption timer dynamically */ |
5735 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
5736 | return pin_based_exec_ctrl; |
5737 | } | |
5738 | ||
d62caabb AS |
5739 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
5740 | { | |
5741 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5742 | ||
5743 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); | |
3ce424e4 RK |
5744 | if (cpu_has_secondary_exec_ctrls()) { |
5745 | if (kvm_vcpu_apicv_active(vcpu)) | |
5746 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
5747 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5748 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5749 | else | |
5750 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
5751 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5752 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5753 | } | |
5754 | ||
5755 | if (cpu_has_vmx_msr_bitmap()) | |
904e14fb | 5756 | vmx_update_msr_bitmap(vcpu); |
d62caabb AS |
5757 | } |
5758 | ||
bf8179a0 NHE |
5759 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
5760 | { | |
5761 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; | |
d16c293e PB |
5762 | |
5763 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) | |
5764 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; | |
5765 | ||
35754c98 | 5766 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
bf8179a0 NHE |
5767 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
5768 | #ifdef CONFIG_X86_64 | |
5769 | exec_control |= CPU_BASED_CR8_STORE_EXITING | | |
5770 | CPU_BASED_CR8_LOAD_EXITING; | |
5771 | #endif | |
5772 | } | |
5773 | if (!enable_ept) | |
5774 | exec_control |= CPU_BASED_CR3_STORE_EXITING | | |
5775 | CPU_BASED_CR3_LOAD_EXITING | | |
5776 | CPU_BASED_INVLPG_EXITING; | |
4d5422ce WL |
5777 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
5778 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | | |
5779 | CPU_BASED_MONITOR_EXITING); | |
caa057a2 WL |
5780 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
5781 | exec_control &= ~CPU_BASED_HLT_EXITING; | |
bf8179a0 NHE |
5782 | return exec_control; |
5783 | } | |
5784 | ||
45ec368c | 5785 | static bool vmx_rdrand_supported(void) |
bf8179a0 | 5786 | { |
45ec368c | 5787 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
736fdf72 | 5788 | SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5789 | } |
5790 | ||
75f4fc8d JM |
5791 | static bool vmx_rdseed_supported(void) |
5792 | { | |
5793 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
736fdf72 | 5794 | SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5795 | } |
5796 | ||
80154d77 | 5797 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
bf8179a0 | 5798 | { |
80154d77 PB |
5799 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
5800 | ||
bf8179a0 | 5801 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
0367f205 | 5802 | |
80154d77 | 5803 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
bf8179a0 NHE |
5804 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
5805 | if (vmx->vpid == 0) | |
5806 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; | |
5807 | if (!enable_ept) { | |
5808 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; | |
5809 | enable_unrestricted_guest = 0; | |
ad756a16 MJ |
5810 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
5811 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; | |
bf8179a0 NHE |
5812 | } |
5813 | if (!enable_unrestricted_guest) | |
5814 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
b31c114b | 5815 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
bf8179a0 | 5816 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
80154d77 | 5817 | if (!kvm_vcpu_apicv_active(vcpu)) |
c7c9c56c YZ |
5818 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
5819 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
8d14695f | 5820 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
0367f205 PB |
5821 | |
5822 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, | |
5823 | * in vmx_set_cr4. */ | |
5824 | exec_control &= ~SECONDARY_EXEC_DESC; | |
5825 | ||
abc4fc58 AG |
5826 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
5827 | (handle_vmptrld). | |
5828 | We can NOT enable shadow_vmcs here because we don't have yet | |
5829 | a current VMCS12 | |
5830 | */ | |
5831 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; | |
a3eaa864 KH |
5832 | |
5833 | if (!enable_pml) | |
5834 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; | |
843e4330 | 5835 | |
3db13480 PB |
5836 | if (vmx_xsaves_supported()) { |
5837 | /* Exposing XSAVES only when XSAVE is exposed */ | |
5838 | bool xsaves_enabled = | |
5839 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && | |
5840 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); | |
5841 | ||
5842 | if (!xsaves_enabled) | |
5843 | exec_control &= ~SECONDARY_EXEC_XSAVES; | |
5844 | ||
5845 | if (nested) { | |
5846 | if (xsaves_enabled) | |
6677f3da | 5847 | vmx->nested.msrs.secondary_ctls_high |= |
3db13480 PB |
5848 | SECONDARY_EXEC_XSAVES; |
5849 | else | |
6677f3da | 5850 | vmx->nested.msrs.secondary_ctls_high &= |
3db13480 PB |
5851 | ~SECONDARY_EXEC_XSAVES; |
5852 | } | |
5853 | } | |
5854 | ||
80154d77 PB |
5855 | if (vmx_rdtscp_supported()) { |
5856 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); | |
5857 | if (!rdtscp_enabled) | |
5858 | exec_control &= ~SECONDARY_EXEC_RDTSCP; | |
5859 | ||
5860 | if (nested) { | |
5861 | if (rdtscp_enabled) | |
6677f3da | 5862 | vmx->nested.msrs.secondary_ctls_high |= |
80154d77 PB |
5863 | SECONDARY_EXEC_RDTSCP; |
5864 | else | |
6677f3da | 5865 | vmx->nested.msrs.secondary_ctls_high &= |
80154d77 PB |
5866 | ~SECONDARY_EXEC_RDTSCP; |
5867 | } | |
5868 | } | |
5869 | ||
5870 | if (vmx_invpcid_supported()) { | |
5871 | /* Exposing INVPCID only when PCID is exposed */ | |
5872 | bool invpcid_enabled = | |
5873 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && | |
5874 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); | |
5875 | ||
5876 | if (!invpcid_enabled) { | |
5877 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; | |
5878 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); | |
5879 | } | |
5880 | ||
5881 | if (nested) { | |
5882 | if (invpcid_enabled) | |
6677f3da | 5883 | vmx->nested.msrs.secondary_ctls_high |= |
80154d77 PB |
5884 | SECONDARY_EXEC_ENABLE_INVPCID; |
5885 | else | |
6677f3da | 5886 | vmx->nested.msrs.secondary_ctls_high &= |
80154d77 PB |
5887 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
5888 | } | |
5889 | } | |
5890 | ||
45ec368c JM |
5891 | if (vmx_rdrand_supported()) { |
5892 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); | |
5893 | if (rdrand_enabled) | |
736fdf72 | 5894 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5895 | |
5896 | if (nested) { | |
5897 | if (rdrand_enabled) | |
6677f3da | 5898 | vmx->nested.msrs.secondary_ctls_high |= |
736fdf72 | 5899 | SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c | 5900 | else |
6677f3da | 5901 | vmx->nested.msrs.secondary_ctls_high &= |
736fdf72 | 5902 | ~SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5903 | } |
5904 | } | |
5905 | ||
75f4fc8d JM |
5906 | if (vmx_rdseed_supported()) { |
5907 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); | |
5908 | if (rdseed_enabled) | |
736fdf72 | 5909 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5910 | |
5911 | if (nested) { | |
5912 | if (rdseed_enabled) | |
6677f3da | 5913 | vmx->nested.msrs.secondary_ctls_high |= |
736fdf72 | 5914 | SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d | 5915 | else |
6677f3da | 5916 | vmx->nested.msrs.secondary_ctls_high &= |
736fdf72 | 5917 | ~SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5918 | } |
5919 | } | |
5920 | ||
80154d77 | 5921 | vmx->secondary_exec_control = exec_control; |
bf8179a0 NHE |
5922 | } |
5923 | ||
ce88decf XG |
5924 | static void ept_set_mmio_spte_mask(void) |
5925 | { | |
5926 | /* | |
5927 | * EPT Misconfigurations can be generated if the value of bits 2:0 | |
5928 | * of an EPT paging-structure entry is 110b (write/execute). | |
ce88decf | 5929 | */ |
dcdca5fe PF |
5930 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
5931 | VMX_EPT_MISCONFIG_WX_VALUE); | |
ce88decf XG |
5932 | } |
5933 | ||
f53cd63c | 5934 | #define VMX_XSS_EXIT_BITMAP 0 |
6aa8b732 AK |
5935 | /* |
5936 | * Sets up the vmcs for emulated real mode. | |
5937 | */ | |
12d79917 | 5938 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
6aa8b732 | 5939 | { |
2e4ce7f5 | 5940 | #ifdef CONFIG_X86_64 |
6aa8b732 | 5941 | unsigned long a; |
2e4ce7f5 | 5942 | #endif |
6aa8b732 | 5943 | int i; |
6aa8b732 | 5944 | |
4607c2d7 AG |
5945 | if (enable_shadow_vmcs) { |
5946 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); | |
5947 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); | |
5948 | } | |
25c5f225 | 5949 | if (cpu_has_vmx_msr_bitmap()) |
904e14fb | 5950 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
25c5f225 | 5951 | |
6aa8b732 AK |
5952 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
5953 | ||
6aa8b732 | 5954 | /* Control */ |
01e439be | 5955 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); |
64672c95 | 5956 | vmx->hv_deadline_tsc = -1; |
6e5d865c | 5957 | |
bf8179a0 | 5958 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
6aa8b732 | 5959 | |
dfa169bb | 5960 | if (cpu_has_secondary_exec_ctrls()) { |
80154d77 | 5961 | vmx_compute_secondary_exec_control(vmx); |
bf8179a0 | 5962 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
80154d77 | 5963 | vmx->secondary_exec_control); |
dfa169bb | 5964 | } |
f78e0e2e | 5965 | |
d62caabb | 5966 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
c7c9c56c YZ |
5967 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
5968 | vmcs_write64(EOI_EXIT_BITMAP1, 0); | |
5969 | vmcs_write64(EOI_EXIT_BITMAP2, 0); | |
5970 | vmcs_write64(EOI_EXIT_BITMAP3, 0); | |
5971 | ||
5972 | vmcs_write16(GUEST_INTR_STATUS, 0); | |
01e439be | 5973 | |
0bcf261c | 5974 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
01e439be | 5975 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
c7c9c56c YZ |
5976 | } |
5977 | ||
b31c114b | 5978 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
4b8d54f9 | 5979 | vmcs_write32(PLE_GAP, ple_gap); |
a7653ecd RK |
5980 | vmx->ple_window = ple_window; |
5981 | vmx->ple_window_dirty = true; | |
4b8d54f9 ZE |
5982 | } |
5983 | ||
c3707958 XG |
5984 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
5985 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); | |
6aa8b732 AK |
5986 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
5987 | ||
9581d442 AK |
5988 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
5989 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ | |
a547c6db | 5990 | vmx_set_constant_host_state(vmx); |
05b3e0c2 | 5991 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
5992 | rdmsrl(MSR_FS_BASE, a); |
5993 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ | |
5994 | rdmsrl(MSR_GS_BASE, a); | |
5995 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ | |
5996 | #else | |
5997 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ | |
5998 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ | |
5999 | #endif | |
6000 | ||
2a499e49 BD |
6001 | if (cpu_has_vmx_vmfunc()) |
6002 | vmcs_write64(VM_FUNCTION_CONTROL, 0); | |
6003 | ||
2cc51560 ED |
6004 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
6005 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); | |
61d2ef2c | 6006 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
2cc51560 | 6007 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
61d2ef2c | 6008 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
6aa8b732 | 6009 | |
74545705 RK |
6010 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
6011 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); | |
468d472f | 6012 | |
03916db9 | 6013 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
6aa8b732 AK |
6014 | u32 index = vmx_msr_index[i]; |
6015 | u32 data_low, data_high; | |
a2fa3e9f | 6016 | int j = vmx->nmsrs; |
6aa8b732 AK |
6017 | |
6018 | if (rdmsr_safe(index, &data_low, &data_high) < 0) | |
6019 | continue; | |
432bd6cb AK |
6020 | if (wrmsr_safe(index, data_low, data_high) < 0) |
6021 | continue; | |
26bb0981 AK |
6022 | vmx->guest_msrs[j].index = i; |
6023 | vmx->guest_msrs[j].data = 0; | |
d5696725 | 6024 | vmx->guest_msrs[j].mask = -1ull; |
a2fa3e9f | 6025 | ++vmx->nmsrs; |
6aa8b732 | 6026 | } |
6aa8b732 | 6027 | |
28c1c9fa KA |
6028 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
6029 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities); | |
2961e876 GN |
6030 | |
6031 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); | |
6aa8b732 AK |
6032 | |
6033 | /* 22.2.1, 20.8.1 */ | |
2961e876 | 6034 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
1c3d14fe | 6035 | |
bd7e5b08 PB |
6036 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
6037 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); | |
6038 | ||
bf8179a0 | 6039 | set_cr4_guest_host_mask(vmx); |
e00c8cf2 | 6040 | |
f53cd63c WL |
6041 | if (vmx_xsaves_supported()) |
6042 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); | |
6043 | ||
4e59516a PF |
6044 | if (enable_pml) { |
6045 | ASSERT(vmx->pml_pg); | |
6046 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
6047 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
6048 | } | |
e00c8cf2 AK |
6049 | } |
6050 | ||
d28bc9dd | 6051 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
e00c8cf2 AK |
6052 | { |
6053 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
58cb628d | 6054 | struct msr_data apic_base_msr; |
d28bc9dd | 6055 | u64 cr0; |
e00c8cf2 | 6056 | |
7ffd92c5 | 6057 | vmx->rmode.vm86_active = 0; |
d28b387f | 6058 | vmx->spec_ctrl = 0; |
e00c8cf2 | 6059 | |
518e7b94 | 6060 | vcpu->arch.microcode_version = 0x100000000ULL; |
ad312c7c | 6061 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
d28bc9dd NA |
6062 | kvm_set_cr8(vcpu, 0); |
6063 | ||
6064 | if (!init_event) { | |
6065 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | | |
6066 | MSR_IA32_APICBASE_ENABLE; | |
6067 | if (kvm_vcpu_is_reset_bsp(vcpu)) | |
6068 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; | |
6069 | apic_base_msr.host_initiated = true; | |
6070 | kvm_set_apic_base(vcpu, &apic_base_msr); | |
6071 | } | |
e00c8cf2 | 6072 | |
2fb92db1 AK |
6073 | vmx_segment_cache_clear(vmx); |
6074 | ||
5706be0d | 6075 | seg_setup(VCPU_SREG_CS); |
66450a21 | 6076 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
f3531054 | 6077 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
e00c8cf2 AK |
6078 | |
6079 | seg_setup(VCPU_SREG_DS); | |
6080 | seg_setup(VCPU_SREG_ES); | |
6081 | seg_setup(VCPU_SREG_FS); | |
6082 | seg_setup(VCPU_SREG_GS); | |
6083 | seg_setup(VCPU_SREG_SS); | |
6084 | ||
6085 | vmcs_write16(GUEST_TR_SELECTOR, 0); | |
6086 | vmcs_writel(GUEST_TR_BASE, 0); | |
6087 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); | |
6088 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); | |
6089 | ||
6090 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); | |
6091 | vmcs_writel(GUEST_LDTR_BASE, 0); | |
6092 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); | |
6093 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); | |
6094 | ||
d28bc9dd NA |
6095 | if (!init_event) { |
6096 | vmcs_write32(GUEST_SYSENTER_CS, 0); | |
6097 | vmcs_writel(GUEST_SYSENTER_ESP, 0); | |
6098 | vmcs_writel(GUEST_SYSENTER_EIP, 0); | |
6099 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
6100 | } | |
e00c8cf2 | 6101 | |
c37c2873 | 6102 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
66450a21 | 6103 | kvm_rip_write(vcpu, 0xfff0); |
e00c8cf2 | 6104 | |
e00c8cf2 AK |
6105 | vmcs_writel(GUEST_GDTR_BASE, 0); |
6106 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); | |
6107 | ||
6108 | vmcs_writel(GUEST_IDTR_BASE, 0); | |
6109 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); | |
6110 | ||
443381a8 | 6111 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
e00c8cf2 | 6112 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
f3531054 | 6113 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
a554d207 WL |
6114 | if (kvm_mpx_supported()) |
6115 | vmcs_write64(GUEST_BNDCFGS, 0); | |
e00c8cf2 | 6116 | |
e00c8cf2 AK |
6117 | setup_msrs(vmx); |
6118 | ||
6aa8b732 AK |
6119 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
6120 | ||
d28bc9dd | 6121 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
f78e0e2e | 6122 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
35754c98 | 6123 | if (cpu_need_tpr_shadow(vcpu)) |
f78e0e2e | 6124 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
d28bc9dd | 6125 | __pa(vcpu->arch.apic->regs)); |
f78e0e2e SY |
6126 | vmcs_write32(TPR_THRESHOLD, 0); |
6127 | } | |
6128 | ||
a73896cb | 6129 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
6aa8b732 | 6130 | |
2384d2b3 SY |
6131 | if (vmx->vpid != 0) |
6132 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
6133 | ||
d28bc9dd | 6134 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
d28bc9dd | 6135 | vmx->vcpu.arch.cr0 = cr0; |
f2463247 | 6136 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
d28bc9dd | 6137 | vmx_set_cr4(vcpu, 0); |
5690891b | 6138 | vmx_set_efer(vcpu, 0); |
bd7e5b08 | 6139 | |
d28bc9dd | 6140 | update_exception_bitmap(vcpu); |
6aa8b732 | 6141 | |
dd5f5341 | 6142 | vpid_sync_context(vmx->vpid); |
caa057a2 WL |
6143 | if (init_event) |
6144 | vmx_clear_hlt(vcpu); | |
6aa8b732 AK |
6145 | } |
6146 | ||
b6f1250e NHE |
6147 | /* |
6148 | * In nested virtualization, check if L1 asked to exit on external interrupts. | |
6149 | * For most existing hypervisors, this will always return true. | |
6150 | */ | |
6151 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) | |
6152 | { | |
6153 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & | |
6154 | PIN_BASED_EXT_INTR_MASK; | |
6155 | } | |
6156 | ||
77b0f5d6 BD |
6157 | /* |
6158 | * In nested virtualization, check if L1 has set | |
6159 | * VM_EXIT_ACK_INTR_ON_EXIT | |
6160 | */ | |
6161 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) | |
6162 | { | |
6163 | return get_vmcs12(vcpu)->vm_exit_controls & | |
6164 | VM_EXIT_ACK_INTR_ON_EXIT; | |
6165 | } | |
6166 | ||
ea8ceb83 JK |
6167 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
6168 | { | |
0c7f650e | 6169 | return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu)); |
ea8ceb83 JK |
6170 | } |
6171 | ||
c9a7953f | 6172 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 6173 | { |
47c0152e PB |
6174 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
6175 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
3b86cd99 JK |
6176 | } |
6177 | ||
c9a7953f | 6178 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 6179 | { |
d02fcf50 | 6180 | if (!enable_vnmi || |
8a1b4392 | 6181 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
c9a7953f JK |
6182 | enable_irq_window(vcpu); |
6183 | return; | |
6184 | } | |
3b86cd99 | 6185 | |
47c0152e PB |
6186 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
6187 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
3b86cd99 JK |
6188 | } |
6189 | ||
66fd3f7f | 6190 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
85f455f7 | 6191 | { |
9c8cba37 | 6192 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
66fd3f7f GN |
6193 | uint32_t intr; |
6194 | int irq = vcpu->arch.interrupt.nr; | |
9c8cba37 | 6195 | |
229456fc | 6196 | trace_kvm_inj_virq(irq); |
2714d1d3 | 6197 | |
fa89a817 | 6198 | ++vcpu->stat.irq_injections; |
7ffd92c5 | 6199 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
6200 | int inc_eip = 0; |
6201 | if (vcpu->arch.interrupt.soft) | |
6202 | inc_eip = vcpu->arch.event_exit_inst_len; | |
6203 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) | |
a92601bb | 6204 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
85f455f7 ED |
6205 | return; |
6206 | } | |
66fd3f7f GN |
6207 | intr = irq | INTR_INFO_VALID_MASK; |
6208 | if (vcpu->arch.interrupt.soft) { | |
6209 | intr |= INTR_TYPE_SOFT_INTR; | |
6210 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
6211 | vmx->vcpu.arch.event_exit_inst_len); | |
6212 | } else | |
6213 | intr |= INTR_TYPE_EXT_INTR; | |
6214 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); | |
caa057a2 WL |
6215 | |
6216 | vmx_clear_hlt(vcpu); | |
85f455f7 ED |
6217 | } |
6218 | ||
f08864b4 SY |
6219 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
6220 | { | |
66a5a347 JK |
6221 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6222 | ||
d02fcf50 | 6223 | if (!enable_vnmi) { |
8a1b4392 PB |
6224 | /* |
6225 | * Tracking the NMI-blocked state in software is built upon | |
6226 | * finding the next open IRQ window. This, in turn, depends on | |
6227 | * well-behaving guests: They have to keep IRQs disabled at | |
6228 | * least as long as the NMI handler runs. Otherwise we may | |
6229 | * cause NMI nesting, maybe breaking the guest. But as this is | |
6230 | * highly unlikely, we can live with the residual risk. | |
6231 | */ | |
6232 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; | |
6233 | vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
6234 | } | |
6235 | ||
4c4a6f79 PB |
6236 | ++vcpu->stat.nmi_injections; |
6237 | vmx->loaded_vmcs->nmi_known_unmasked = false; | |
3b86cd99 | 6238 | |
7ffd92c5 | 6239 | if (vmx->rmode.vm86_active) { |
71f9833b | 6240 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
a92601bb | 6241 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
66a5a347 JK |
6242 | return; |
6243 | } | |
c5a6d5f7 | 6244 | |
f08864b4 SY |
6245 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
6246 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); | |
caa057a2 WL |
6247 | |
6248 | vmx_clear_hlt(vcpu); | |
f08864b4 SY |
6249 | } |
6250 | ||
3cfc3092 JK |
6251 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
6252 | { | |
4c4a6f79 PB |
6253 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6254 | bool masked; | |
6255 | ||
d02fcf50 | 6256 | if (!enable_vnmi) |
8a1b4392 | 6257 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
4c4a6f79 | 6258 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
9d58b931 | 6259 | return false; |
4c4a6f79 PB |
6260 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
6261 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
6262 | return masked; | |
3cfc3092 JK |
6263 | } |
6264 | ||
6265 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) | |
6266 | { | |
6267 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6268 | ||
d02fcf50 | 6269 | if (!enable_vnmi) { |
8a1b4392 PB |
6270 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
6271 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; | |
6272 | vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
6273 | } | |
6274 | } else { | |
6275 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
6276 | if (masked) | |
6277 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
6278 | GUEST_INTR_STATE_NMI); | |
6279 | else | |
6280 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, | |
6281 | GUEST_INTR_STATE_NMI); | |
6282 | } | |
3cfc3092 JK |
6283 | } |
6284 | ||
2505dc9f JK |
6285 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
6286 | { | |
b6b8a145 JK |
6287 | if (to_vmx(vcpu)->nested.nested_run_pending) |
6288 | return 0; | |
ea8ceb83 | 6289 | |
d02fcf50 | 6290 | if (!enable_vnmi && |
8a1b4392 PB |
6291 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
6292 | return 0; | |
6293 | ||
2505dc9f JK |
6294 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
6295 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI | |
6296 | | GUEST_INTR_STATE_NMI)); | |
6297 | } | |
6298 | ||
78646121 GN |
6299 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
6300 | { | |
b6b8a145 JK |
6301 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
6302 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && | |
c4282df9 GN |
6303 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
6304 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); | |
78646121 GN |
6305 | } |
6306 | ||
cbc94022 IE |
6307 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
6308 | { | |
6309 | int ret; | |
cbc94022 | 6310 | |
f7eaeb0a SC |
6311 | if (enable_unrestricted_guest) |
6312 | return 0; | |
6313 | ||
1d8007bd PB |
6314 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
6315 | PAGE_SIZE * 3); | |
cbc94022 IE |
6316 | if (ret) |
6317 | return ret; | |
40bbb9d0 | 6318 | to_kvm_vmx(kvm)->tss_addr = addr; |
1f755a82 | 6319 | return init_rmode_tss(kvm); |
cbc94022 IE |
6320 | } |
6321 | ||
2ac52ab8 SC |
6322 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
6323 | { | |
40bbb9d0 | 6324 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
2ac52ab8 SC |
6325 | return 0; |
6326 | } | |
6327 | ||
0ca1b4f4 | 6328 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
6aa8b732 | 6329 | { |
77ab6db0 | 6330 | switch (vec) { |
77ab6db0 | 6331 | case BP_VECTOR: |
c573cd22 JK |
6332 | /* |
6333 | * Update instruction length as we may reinject the exception | |
6334 | * from user space while in guest debugging mode. | |
6335 | */ | |
6336 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = | |
6337 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
d0bfb940 | 6338 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
0ca1b4f4 GN |
6339 | return false; |
6340 | /* fall through */ | |
6341 | case DB_VECTOR: | |
6342 | if (vcpu->guest_debug & | |
6343 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
6344 | return false; | |
d0bfb940 JK |
6345 | /* fall through */ |
6346 | case DE_VECTOR: | |
77ab6db0 JK |
6347 | case OF_VECTOR: |
6348 | case BR_VECTOR: | |
6349 | case UD_VECTOR: | |
6350 | case DF_VECTOR: | |
6351 | case SS_VECTOR: | |
6352 | case GP_VECTOR: | |
6353 | case MF_VECTOR: | |
0ca1b4f4 GN |
6354 | return true; |
6355 | break; | |
77ab6db0 | 6356 | } |
0ca1b4f4 GN |
6357 | return false; |
6358 | } | |
6359 | ||
6360 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, | |
6361 | int vec, u32 err_code) | |
6362 | { | |
6363 | /* | |
6364 | * Instruction with address size override prefix opcode 0x67 | |
6365 | * Cause the #SS fault with 0 error code in VM86 mode. | |
6366 | */ | |
6367 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { | |
6368 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { | |
6369 | if (vcpu->arch.halt_request) { | |
6370 | vcpu->arch.halt_request = 0; | |
5cb56059 | 6371 | return kvm_vcpu_halt(vcpu); |
0ca1b4f4 GN |
6372 | } |
6373 | return 1; | |
6374 | } | |
6375 | return 0; | |
6376 | } | |
6377 | ||
6378 | /* | |
6379 | * Forward all other exceptions that are valid in real mode. | |
6380 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with | |
6381 | * the required debugging infrastructure rework. | |
6382 | */ | |
6383 | kvm_queue_exception(vcpu, vec); | |
6384 | return 1; | |
6aa8b732 AK |
6385 | } |
6386 | ||
a0861c02 AK |
6387 | /* |
6388 | * Trigger machine check on the host. We assume all the MSRs are already set up | |
6389 | * by the CPU and that we still run on the same CPU as the MCE occurred on. | |
6390 | * We pass a fake environment to the machine check handler because we want | |
6391 | * the guest to be always treated like user space, no matter what context | |
6392 | * it used internally. | |
6393 | */ | |
6394 | static void kvm_machine_check(void) | |
6395 | { | |
6396 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) | |
6397 | struct pt_regs regs = { | |
6398 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ | |
6399 | .flags = X86_EFLAGS_IF, | |
6400 | }; | |
6401 | ||
6402 | do_machine_check(®s, 0); | |
6403 | #endif | |
6404 | } | |
6405 | ||
851ba692 | 6406 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
a0861c02 AK |
6407 | { |
6408 | /* already handled by vcpu_run */ | |
6409 | return 1; | |
6410 | } | |
6411 | ||
851ba692 | 6412 | static int handle_exception(struct kvm_vcpu *vcpu) |
6aa8b732 | 6413 | { |
1155f76a | 6414 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
851ba692 | 6415 | struct kvm_run *kvm_run = vcpu->run; |
d0bfb940 | 6416 | u32 intr_info, ex_no, error_code; |
42dbaa5a | 6417 | unsigned long cr2, rip, dr6; |
6aa8b732 AK |
6418 | u32 vect_info; |
6419 | enum emulation_result er; | |
6420 | ||
1155f76a | 6421 | vect_info = vmx->idt_vectoring_info; |
88786475 | 6422 | intr_info = vmx->exit_intr_info; |
6aa8b732 | 6423 | |
a0861c02 | 6424 | if (is_machine_check(intr_info)) |
851ba692 | 6425 | return handle_machine_check(vcpu); |
a0861c02 | 6426 | |
ef85b673 | 6427 | if (is_nmi(intr_info)) |
1b6269db | 6428 | return 1; /* already handled by vmx_vcpu_run() */ |
2ab455cc | 6429 | |
082d06ed WL |
6430 | if (is_invalid_opcode(intr_info)) |
6431 | return handle_ud(vcpu); | |
7aa81cc0 | 6432 | |
6aa8b732 | 6433 | error_code = 0; |
2e11384c | 6434 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
6aa8b732 | 6435 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
bf4ca23e | 6436 | |
9e869480 LA |
6437 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
6438 | WARN_ON_ONCE(!enable_vmware_backdoor); | |
6439 | er = emulate_instruction(vcpu, | |
6440 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); | |
6441 | if (er == EMULATE_USER_EXIT) | |
6442 | return 0; | |
6443 | else if (er != EMULATE_DONE) | |
6444 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); | |
6445 | return 1; | |
6446 | } | |
6447 | ||
bf4ca23e XG |
6448 | /* |
6449 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing | |
6450 | * MMIO, it is better to report an internal error. | |
6451 | * See the comments in vmx_handle_exit. | |
6452 | */ | |
6453 | if ((vect_info & VECTORING_INFO_VALID_MASK) && | |
6454 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { | |
6455 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
6456 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; | |
80f0e95d | 6457 | vcpu->run->internal.ndata = 3; |
bf4ca23e XG |
6458 | vcpu->run->internal.data[0] = vect_info; |
6459 | vcpu->run->internal.data[1] = intr_info; | |
80f0e95d | 6460 | vcpu->run->internal.data[2] = error_code; |
bf4ca23e XG |
6461 | return 0; |
6462 | } | |
6463 | ||
6aa8b732 AK |
6464 | if (is_page_fault(intr_info)) { |
6465 | cr2 = vmcs_readl(EXIT_QUALIFICATION); | |
1261bfa3 WL |
6466 | /* EPT won't cause page fault directly */ |
6467 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); | |
d0006530 | 6468 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
6aa8b732 AK |
6469 | } |
6470 | ||
d0bfb940 | 6471 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
0ca1b4f4 GN |
6472 | |
6473 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) | |
6474 | return handle_rmode_exception(vcpu, ex_no, error_code); | |
6475 | ||
42dbaa5a | 6476 | switch (ex_no) { |
54a20552 EN |
6477 | case AC_VECTOR: |
6478 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); | |
6479 | return 1; | |
42dbaa5a JK |
6480 | case DB_VECTOR: |
6481 | dr6 = vmcs_readl(EXIT_QUALIFICATION); | |
6482 | if (!(vcpu->guest_debug & | |
6483 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { | |
8246bf52 | 6484 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 6485 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
32d43cd3 | 6486 | if (is_icebp(intr_info)) |
fd2a445a HD |
6487 | skip_emulated_instruction(vcpu); |
6488 | ||
42dbaa5a JK |
6489 | kvm_queue_exception(vcpu, DB_VECTOR); |
6490 | return 1; | |
6491 | } | |
6492 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; | |
6493 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); | |
6494 | /* fall through */ | |
6495 | case BP_VECTOR: | |
c573cd22 JK |
6496 | /* |
6497 | * Update instruction length as we may reinject #BP from | |
6498 | * user space while in guest debugging mode. Reading it for | |
6499 | * #DB as well causes no harm, it is not used in that case. | |
6500 | */ | |
6501 | vmx->vcpu.arch.event_exit_inst_len = | |
6502 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
6aa8b732 | 6503 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
0a434bb2 | 6504 | rip = kvm_rip_read(vcpu); |
d0bfb940 JK |
6505 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
6506 | kvm_run->debug.arch.exception = ex_no; | |
42dbaa5a JK |
6507 | break; |
6508 | default: | |
d0bfb940 JK |
6509 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
6510 | kvm_run->ex.exception = ex_no; | |
6511 | kvm_run->ex.error_code = error_code; | |
42dbaa5a | 6512 | break; |
6aa8b732 | 6513 | } |
6aa8b732 AK |
6514 | return 0; |
6515 | } | |
6516 | ||
851ba692 | 6517 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
6aa8b732 | 6518 | { |
1165f5fe | 6519 | ++vcpu->stat.irq_exits; |
6aa8b732 AK |
6520 | return 1; |
6521 | } | |
6522 | ||
851ba692 | 6523 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
988ad74f | 6524 | { |
851ba692 | 6525 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
bbeac283 | 6526 | vcpu->mmio_needed = 0; |
988ad74f AK |
6527 | return 0; |
6528 | } | |
6aa8b732 | 6529 | |
851ba692 | 6530 | static int handle_io(struct kvm_vcpu *vcpu) |
6aa8b732 | 6531 | { |
bfdaab09 | 6532 | unsigned long exit_qualification; |
dca7f128 | 6533 | int size, in, string; |
039576c0 | 6534 | unsigned port; |
6aa8b732 | 6535 | |
bfdaab09 | 6536 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
039576c0 | 6537 | string = (exit_qualification & 16) != 0; |
e70669ab | 6538 | |
cf8f70bf | 6539 | ++vcpu->stat.io_exits; |
e70669ab | 6540 | |
432baf60 | 6541 | if (string) |
51d8b661 | 6542 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
e70669ab | 6543 | |
cf8f70bf GN |
6544 | port = exit_qualification >> 16; |
6545 | size = (exit_qualification & 7) + 1; | |
432baf60 | 6546 | in = (exit_qualification & 8) != 0; |
cf8f70bf | 6547 | |
dca7f128 | 6548 | return kvm_fast_pio(vcpu, size, port, in); |
6aa8b732 AK |
6549 | } |
6550 | ||
102d8325 IM |
6551 | static void |
6552 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) | |
6553 | { | |
6554 | /* | |
6555 | * Patch in the VMCALL instruction: | |
6556 | */ | |
6557 | hypercall[0] = 0x0f; | |
6558 | hypercall[1] = 0x01; | |
6559 | hypercall[2] = 0xc1; | |
102d8325 IM |
6560 | } |
6561 | ||
0fa06071 | 6562 | /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ |
eeadf9e7 NHE |
6563 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
6564 | { | |
eeadf9e7 | 6565 | if (is_guest_mode(vcpu)) { |
1a0d74e6 JK |
6566 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
6567 | unsigned long orig_val = val; | |
6568 | ||
eeadf9e7 NHE |
6569 | /* |
6570 | * We get here when L2 changed cr0 in a way that did not change | |
6571 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), | |
1a0d74e6 JK |
6572 | * but did change L0 shadowed bits. So we first calculate the |
6573 | * effective cr0 value that L1 would like to write into the | |
6574 | * hardware. It consists of the L2-owned bits from the new | |
6575 | * value combined with the L1-owned bits from L1's guest_cr0. | |
eeadf9e7 | 6576 | */ |
1a0d74e6 JK |
6577 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
6578 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); | |
6579 | ||
3899152c | 6580 | if (!nested_guest_cr0_valid(vcpu, val)) |
eeadf9e7 | 6581 | return 1; |
1a0d74e6 JK |
6582 | |
6583 | if (kvm_set_cr0(vcpu, val)) | |
6584 | return 1; | |
6585 | vmcs_writel(CR0_READ_SHADOW, orig_val); | |
eeadf9e7 | 6586 | return 0; |
1a0d74e6 JK |
6587 | } else { |
6588 | if (to_vmx(vcpu)->nested.vmxon && | |
3899152c | 6589 | !nested_host_cr0_valid(vcpu, val)) |
1a0d74e6 | 6590 | return 1; |
3899152c | 6591 | |
eeadf9e7 | 6592 | return kvm_set_cr0(vcpu, val); |
1a0d74e6 | 6593 | } |
eeadf9e7 NHE |
6594 | } |
6595 | ||
6596 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) | |
6597 | { | |
6598 | if (is_guest_mode(vcpu)) { | |
1a0d74e6 JK |
6599 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
6600 | unsigned long orig_val = val; | |
6601 | ||
6602 | /* analogously to handle_set_cr0 */ | |
6603 | val = (val & ~vmcs12->cr4_guest_host_mask) | | |
6604 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); | |
6605 | if (kvm_set_cr4(vcpu, val)) | |
eeadf9e7 | 6606 | return 1; |
1a0d74e6 | 6607 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
eeadf9e7 NHE |
6608 | return 0; |
6609 | } else | |
6610 | return kvm_set_cr4(vcpu, val); | |
6611 | } | |
6612 | ||
0367f205 PB |
6613 | static int handle_desc(struct kvm_vcpu *vcpu) |
6614 | { | |
6615 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); | |
6616 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; | |
6617 | } | |
6618 | ||
851ba692 | 6619 | static int handle_cr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6620 | { |
229456fc | 6621 | unsigned long exit_qualification, val; |
6aa8b732 AK |
6622 | int cr; |
6623 | int reg; | |
49a9b07e | 6624 | int err; |
6affcbed | 6625 | int ret; |
6aa8b732 | 6626 | |
bfdaab09 | 6627 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
6aa8b732 AK |
6628 | cr = exit_qualification & 15; |
6629 | reg = (exit_qualification >> 8) & 15; | |
6630 | switch ((exit_qualification >> 4) & 3) { | |
6631 | case 0: /* mov to cr */ | |
1e32c079 | 6632 | val = kvm_register_readl(vcpu, reg); |
229456fc | 6633 | trace_kvm_cr_write(cr, val); |
6aa8b732 AK |
6634 | switch (cr) { |
6635 | case 0: | |
eeadf9e7 | 6636 | err = handle_set_cr0(vcpu, val); |
6affcbed | 6637 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 6638 | case 3: |
e1de91cc | 6639 | WARN_ON_ONCE(enable_unrestricted_guest); |
2390218b | 6640 | err = kvm_set_cr3(vcpu, val); |
6affcbed | 6641 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 6642 | case 4: |
eeadf9e7 | 6643 | err = handle_set_cr4(vcpu, val); |
6affcbed | 6644 | return kvm_complete_insn_gp(vcpu, err); |
0a5fff19 GN |
6645 | case 8: { |
6646 | u8 cr8_prev = kvm_get_cr8(vcpu); | |
1e32c079 | 6647 | u8 cr8 = (u8)val; |
eea1cff9 | 6648 | err = kvm_set_cr8(vcpu, cr8); |
6affcbed | 6649 | ret = kvm_complete_insn_gp(vcpu, err); |
35754c98 | 6650 | if (lapic_in_kernel(vcpu)) |
6affcbed | 6651 | return ret; |
0a5fff19 | 6652 | if (cr8_prev <= cr8) |
6affcbed KH |
6653 | return ret; |
6654 | /* | |
6655 | * TODO: we might be squashing a | |
6656 | * KVM_GUESTDBG_SINGLESTEP-triggered | |
6657 | * KVM_EXIT_DEBUG here. | |
6658 | */ | |
851ba692 | 6659 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
0a5fff19 GN |
6660 | return 0; |
6661 | } | |
4b8073e4 | 6662 | } |
6aa8b732 | 6663 | break; |
25c4c276 | 6664 | case 2: /* clts */ |
bd7e5b08 PB |
6665 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
6666 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); | |
4d4ec087 | 6667 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
6affcbed | 6668 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6669 | case 1: /*mov from cr*/ |
6670 | switch (cr) { | |
6671 | case 3: | |
e1de91cc | 6672 | WARN_ON_ONCE(enable_unrestricted_guest); |
9f8fe504 AK |
6673 | val = kvm_read_cr3(vcpu); |
6674 | kvm_register_write(vcpu, reg, val); | |
6675 | trace_kvm_cr_read(cr, val); | |
6affcbed | 6676 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 | 6677 | case 8: |
229456fc MT |
6678 | val = kvm_get_cr8(vcpu); |
6679 | kvm_register_write(vcpu, reg, val); | |
6680 | trace_kvm_cr_read(cr, val); | |
6affcbed | 6681 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6682 | } |
6683 | break; | |
6684 | case 3: /* lmsw */ | |
a1f83a74 | 6685 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
4d4ec087 | 6686 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
a1f83a74 | 6687 | kvm_lmsw(vcpu, val); |
6aa8b732 | 6688 | |
6affcbed | 6689 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6690 | default: |
6691 | break; | |
6692 | } | |
851ba692 | 6693 | vcpu->run->exit_reason = 0; |
a737f256 | 6694 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
6aa8b732 AK |
6695 | (int)(exit_qualification >> 4) & 3, cr); |
6696 | return 0; | |
6697 | } | |
6698 | ||
851ba692 | 6699 | static int handle_dr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6700 | { |
bfdaab09 | 6701 | unsigned long exit_qualification; |
16f8a6f9 NA |
6702 | int dr, dr7, reg; |
6703 | ||
6704 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6705 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; | |
6706 | ||
6707 | /* First, if DR does not exist, trigger UD */ | |
6708 | if (!kvm_require_dr(vcpu, dr)) | |
6709 | return 1; | |
6aa8b732 | 6710 | |
f2483415 | 6711 | /* Do not handle if the CPL > 0, will trigger GP on re-entry */ |
0a79b009 AK |
6712 | if (!kvm_require_cpl(vcpu, 0)) |
6713 | return 1; | |
16f8a6f9 NA |
6714 | dr7 = vmcs_readl(GUEST_DR7); |
6715 | if (dr7 & DR7_GD) { | |
42dbaa5a JK |
6716 | /* |
6717 | * As the vm-exit takes precedence over the debug trap, we | |
6718 | * need to emulate the latter, either for the host or the | |
6719 | * guest debugging itself. | |
6720 | */ | |
6721 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { | |
851ba692 | 6722 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
16f8a6f9 | 6723 | vcpu->run->debug.arch.dr7 = dr7; |
82b32774 | 6724 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
851ba692 AK |
6725 | vcpu->run->debug.arch.exception = DB_VECTOR; |
6726 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; | |
42dbaa5a JK |
6727 | return 0; |
6728 | } else { | |
7305eb5d | 6729 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 6730 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
42dbaa5a JK |
6731 | kvm_queue_exception(vcpu, DB_VECTOR); |
6732 | return 1; | |
6733 | } | |
6734 | } | |
6735 | ||
81908bf4 | 6736 | if (vcpu->guest_debug == 0) { |
8f22372f PB |
6737 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6738 | CPU_BASED_MOV_DR_EXITING); | |
81908bf4 PB |
6739 | |
6740 | /* | |
6741 | * No more DR vmexits; force a reload of the debug registers | |
6742 | * and reenter on this instruction. The next vmexit will | |
6743 | * retrieve the full state of the debug registers. | |
6744 | */ | |
6745 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; | |
6746 | return 1; | |
6747 | } | |
6748 | ||
42dbaa5a JK |
6749 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
6750 | if (exit_qualification & TYPE_MOV_FROM_DR) { | |
020df079 | 6751 | unsigned long val; |
4c4d563b JK |
6752 | |
6753 | if (kvm_get_dr(vcpu, dr, &val)) | |
6754 | return 1; | |
6755 | kvm_register_write(vcpu, reg, val); | |
020df079 | 6756 | } else |
5777392e | 6757 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
4c4d563b JK |
6758 | return 1; |
6759 | ||
6affcbed | 6760 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6761 | } |
6762 | ||
73aaf249 JK |
6763 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
6764 | { | |
6765 | return vcpu->arch.dr6; | |
6766 | } | |
6767 | ||
6768 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) | |
6769 | { | |
6770 | } | |
6771 | ||
81908bf4 PB |
6772 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
6773 | { | |
81908bf4 PB |
6774 | get_debugreg(vcpu->arch.db[0], 0); |
6775 | get_debugreg(vcpu->arch.db[1], 1); | |
6776 | get_debugreg(vcpu->arch.db[2], 2); | |
6777 | get_debugreg(vcpu->arch.db[3], 3); | |
6778 | get_debugreg(vcpu->arch.dr6, 6); | |
6779 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); | |
6780 | ||
6781 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; | |
8f22372f | 6782 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING); |
81908bf4 PB |
6783 | } |
6784 | ||
020df079 GN |
6785 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
6786 | { | |
6787 | vmcs_writel(GUEST_DR7, val); | |
6788 | } | |
6789 | ||
851ba692 | 6790 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
6aa8b732 | 6791 | { |
6a908b62 | 6792 | return kvm_emulate_cpuid(vcpu); |
6aa8b732 AK |
6793 | } |
6794 | ||
851ba692 | 6795 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6796 | { |
ad312c7c | 6797 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
609e36d3 | 6798 | struct msr_data msr_info; |
6aa8b732 | 6799 | |
609e36d3 PB |
6800 | msr_info.index = ecx; |
6801 | msr_info.host_initiated = false; | |
6802 | if (vmx_get_msr(vcpu, &msr_info)) { | |
59200273 | 6803 | trace_kvm_msr_read_ex(ecx); |
c1a5d4f9 | 6804 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
6805 | return 1; |
6806 | } | |
6807 | ||
609e36d3 | 6808 | trace_kvm_msr_read(ecx, msr_info.data); |
2714d1d3 | 6809 | |
6aa8b732 | 6810 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
609e36d3 PB |
6811 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
6812 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; | |
6affcbed | 6813 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6814 | } |
6815 | ||
851ba692 | 6816 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6817 | { |
8fe8ab46 | 6818 | struct msr_data msr; |
ad312c7c ZX |
6819 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
6820 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) | |
6821 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); | |
6aa8b732 | 6822 | |
8fe8ab46 WA |
6823 | msr.data = data; |
6824 | msr.index = ecx; | |
6825 | msr.host_initiated = false; | |
854e8bb1 | 6826 | if (kvm_set_msr(vcpu, &msr) != 0) { |
59200273 | 6827 | trace_kvm_msr_write_ex(ecx, data); |
c1a5d4f9 | 6828 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
6829 | return 1; |
6830 | } | |
6831 | ||
59200273 | 6832 | trace_kvm_msr_write(ecx, data); |
6affcbed | 6833 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6834 | } |
6835 | ||
851ba692 | 6836 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
6e5d865c | 6837 | { |
eb90f341 | 6838 | kvm_apic_update_ppr(vcpu); |
6e5d865c YS |
6839 | return 1; |
6840 | } | |
6841 | ||
851ba692 | 6842 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
6aa8b732 | 6843 | { |
47c0152e PB |
6844 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6845 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
2714d1d3 | 6846 | |
3842d135 AK |
6847 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
6848 | ||
a26bf12a | 6849 | ++vcpu->stat.irq_window_exits; |
6aa8b732 AK |
6850 | return 1; |
6851 | } | |
6852 | ||
851ba692 | 6853 | static int handle_halt(struct kvm_vcpu *vcpu) |
6aa8b732 | 6854 | { |
d3bef15f | 6855 | return kvm_emulate_halt(vcpu); |
6aa8b732 AK |
6856 | } |
6857 | ||
851ba692 | 6858 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
c21415e8 | 6859 | { |
0d9c055e | 6860 | return kvm_emulate_hypercall(vcpu); |
c21415e8 IM |
6861 | } |
6862 | ||
ec25d5e6 GN |
6863 | static int handle_invd(struct kvm_vcpu *vcpu) |
6864 | { | |
51d8b661 | 6865 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
ec25d5e6 GN |
6866 | } |
6867 | ||
851ba692 | 6868 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
a7052897 | 6869 | { |
f9c617f6 | 6870 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
a7052897 MT |
6871 | |
6872 | kvm_mmu_invlpg(vcpu, exit_qualification); | |
6affcbed | 6873 | return kvm_skip_emulated_instruction(vcpu); |
a7052897 MT |
6874 | } |
6875 | ||
fee84b07 AK |
6876 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
6877 | { | |
6878 | int err; | |
6879 | ||
6880 | err = kvm_rdpmc(vcpu); | |
6affcbed | 6881 | return kvm_complete_insn_gp(vcpu, err); |
fee84b07 AK |
6882 | } |
6883 | ||
851ba692 | 6884 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
e5edaa01 | 6885 | { |
6affcbed | 6886 | return kvm_emulate_wbinvd(vcpu); |
e5edaa01 ED |
6887 | } |
6888 | ||
2acf923e DC |
6889 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
6890 | { | |
6891 | u64 new_bv = kvm_read_edx_eax(vcpu); | |
6892 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
6893 | ||
6894 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) | |
6affcbed | 6895 | return kvm_skip_emulated_instruction(vcpu); |
2acf923e DC |
6896 | return 1; |
6897 | } | |
6898 | ||
f53cd63c WL |
6899 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
6900 | { | |
6affcbed | 6901 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6902 | WARN(1, "this should never happen\n"); |
6903 | return 1; | |
6904 | } | |
6905 | ||
6906 | static int handle_xrstors(struct kvm_vcpu *vcpu) | |
6907 | { | |
6affcbed | 6908 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6909 | WARN(1, "this should never happen\n"); |
6910 | return 1; | |
6911 | } | |
6912 | ||
851ba692 | 6913 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
f78e0e2e | 6914 | { |
58fbbf26 KT |
6915 | if (likely(fasteoi)) { |
6916 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6917 | int access_type, offset; | |
6918 | ||
6919 | access_type = exit_qualification & APIC_ACCESS_TYPE; | |
6920 | offset = exit_qualification & APIC_ACCESS_OFFSET; | |
6921 | /* | |
6922 | * Sane guest uses MOV to write EOI, with written value | |
6923 | * not cared. So make a short-circuit here by avoiding | |
6924 | * heavy instruction emulation. | |
6925 | */ | |
6926 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && | |
6927 | (offset == APIC_EOI)) { | |
6928 | kvm_lapic_set_eoi(vcpu); | |
6affcbed | 6929 | return kvm_skip_emulated_instruction(vcpu); |
58fbbf26 KT |
6930 | } |
6931 | } | |
51d8b661 | 6932 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
f78e0e2e SY |
6933 | } |
6934 | ||
c7c9c56c YZ |
6935 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
6936 | { | |
6937 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6938 | int vector = exit_qualification & 0xff; | |
6939 | ||
6940 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ | |
6941 | kvm_apic_set_eoi_accelerated(vcpu, vector); | |
6942 | return 1; | |
6943 | } | |
6944 | ||
83d4c286 YZ |
6945 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
6946 | { | |
6947 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6948 | u32 offset = exit_qualification & 0xfff; | |
6949 | ||
6950 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ | |
6951 | kvm_apic_write_nodecode(vcpu, offset); | |
6952 | return 1; | |
6953 | } | |
6954 | ||
851ba692 | 6955 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
37817f29 | 6956 | { |
60637aac | 6957 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
37817f29 | 6958 | unsigned long exit_qualification; |
e269fb21 JK |
6959 | bool has_error_code = false; |
6960 | u32 error_code = 0; | |
37817f29 | 6961 | u16 tss_selector; |
7f3d35fd | 6962 | int reason, type, idt_v, idt_index; |
64a7ec06 GN |
6963 | |
6964 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); | |
7f3d35fd | 6965 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
64a7ec06 | 6966 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
37817f29 IE |
6967 | |
6968 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6969 | ||
6970 | reason = (u32)exit_qualification >> 30; | |
64a7ec06 GN |
6971 | if (reason == TASK_SWITCH_GATE && idt_v) { |
6972 | switch (type) { | |
6973 | case INTR_TYPE_NMI_INTR: | |
6974 | vcpu->arch.nmi_injected = false; | |
654f06fc | 6975 | vmx_set_nmi_mask(vcpu, true); |
64a7ec06 GN |
6976 | break; |
6977 | case INTR_TYPE_EXT_INTR: | |
66fd3f7f | 6978 | case INTR_TYPE_SOFT_INTR: |
64a7ec06 GN |
6979 | kvm_clear_interrupt_queue(vcpu); |
6980 | break; | |
6981 | case INTR_TYPE_HARD_EXCEPTION: | |
e269fb21 JK |
6982 | if (vmx->idt_vectoring_info & |
6983 | VECTORING_INFO_DELIVER_CODE_MASK) { | |
6984 | has_error_code = true; | |
6985 | error_code = | |
6986 | vmcs_read32(IDT_VECTORING_ERROR_CODE); | |
6987 | } | |
6988 | /* fall through */ | |
64a7ec06 GN |
6989 | case INTR_TYPE_SOFT_EXCEPTION: |
6990 | kvm_clear_exception_queue(vcpu); | |
6991 | break; | |
6992 | default: | |
6993 | break; | |
6994 | } | |
60637aac | 6995 | } |
37817f29 IE |
6996 | tss_selector = exit_qualification; |
6997 | ||
64a7ec06 GN |
6998 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
6999 | type != INTR_TYPE_EXT_INTR && | |
7000 | type != INTR_TYPE_NMI_INTR)) | |
7001 | skip_emulated_instruction(vcpu); | |
7002 | ||
7f3d35fd KW |
7003 | if (kvm_task_switch(vcpu, tss_selector, |
7004 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, | |
7005 | has_error_code, error_code) == EMULATE_FAIL) { | |
acb54517 GN |
7006 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
7007 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
7008 | vcpu->run->internal.ndata = 0; | |
42dbaa5a | 7009 | return 0; |
acb54517 | 7010 | } |
42dbaa5a | 7011 | |
42dbaa5a JK |
7012 | /* |
7013 | * TODO: What about debug traps on tss switch? | |
7014 | * Are we supposed to inject them and update dr6? | |
7015 | */ | |
7016 | ||
7017 | return 1; | |
37817f29 IE |
7018 | } |
7019 | ||
851ba692 | 7020 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
1439442c | 7021 | { |
f9c617f6 | 7022 | unsigned long exit_qualification; |
1439442c | 7023 | gpa_t gpa; |
eebed243 | 7024 | u64 error_code; |
1439442c | 7025 | |
f9c617f6 | 7026 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
1439442c | 7027 | |
0be9c7a8 GN |
7028 | /* |
7029 | * EPT violation happened while executing iret from NMI, | |
7030 | * "blocked by NMI" bit has to be set before next VM entry. | |
7031 | * There are errata that may cause this bit to not be set: | |
7032 | * AAK134, BY25. | |
7033 | */ | |
bcd1c294 | 7034 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
d02fcf50 | 7035 | enable_vnmi && |
bcd1c294 | 7036 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
0be9c7a8 GN |
7037 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
7038 | ||
1439442c | 7039 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
229456fc | 7040 | trace_kvm_page_fault(gpa, exit_qualification); |
4f5982a5 | 7041 | |
27959a44 | 7042 | /* Is it a read fault? */ |
ab22a473 | 7043 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
27959a44 JS |
7044 | ? PFERR_USER_MASK : 0; |
7045 | /* Is it a write fault? */ | |
ab22a473 | 7046 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
27959a44 JS |
7047 | ? PFERR_WRITE_MASK : 0; |
7048 | /* Is it a fetch fault? */ | |
ab22a473 | 7049 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
27959a44 JS |
7050 | ? PFERR_FETCH_MASK : 0; |
7051 | /* ept page table entry is present? */ | |
7052 | error_code |= (exit_qualification & | |
7053 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | | |
7054 | EPT_VIOLATION_EXECUTABLE)) | |
7055 | ? PFERR_PRESENT_MASK : 0; | |
4f5982a5 | 7056 | |
eebed243 PB |
7057 | error_code |= (exit_qualification & 0x100) != 0 ? |
7058 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; | |
25d92081 | 7059 | |
25d92081 | 7060 | vcpu->arch.exit_qualification = exit_qualification; |
4f5982a5 | 7061 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
1439442c SY |
7062 | } |
7063 | ||
851ba692 | 7064 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
68f89400 | 7065 | { |
68f89400 MT |
7066 | gpa_t gpa; |
7067 | ||
9034e6e8 PB |
7068 | /* |
7069 | * A nested guest cannot optimize MMIO vmexits, because we have an | |
7070 | * nGPA here instead of the required GPA. | |
7071 | */ | |
68f89400 | 7072 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
9034e6e8 PB |
7073 | if (!is_guest_mode(vcpu) && |
7074 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { | |
931c33b1 | 7075 | trace_kvm_fast_mmio(gpa); |
d391f120 VK |
7076 | /* |
7077 | * Doing kvm_skip_emulated_instruction() depends on undefined | |
7078 | * behavior: Intel's manual doesn't mandate | |
7079 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG | |
7080 | * occurs and while on real hardware it was observed to be set, | |
7081 | * other hypervisors (namely Hyper-V) don't set it, we end up | |
7082 | * advancing IP with some random value. Disable fast mmio when | |
7083 | * running nested and keep it for real hardware in hope that | |
7084 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. | |
7085 | */ | |
7086 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) | |
7087 | return kvm_skip_emulated_instruction(vcpu); | |
7088 | else | |
7089 | return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP, | |
7090 | NULL, 0) == EMULATE_DONE; | |
68c3b4d1 | 7091 | } |
68f89400 | 7092 | |
c75d0edc | 7093 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
68f89400 MT |
7094 | } |
7095 | ||
851ba692 | 7096 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
f08864b4 | 7097 | { |
d02fcf50 | 7098 | WARN_ON_ONCE(!enable_vnmi); |
47c0152e PB |
7099 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
7100 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
f08864b4 | 7101 | ++vcpu->stat.nmi_window_exits; |
3842d135 | 7102 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
f08864b4 SY |
7103 | |
7104 | return 1; | |
7105 | } | |
7106 | ||
80ced186 | 7107 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
ea953ef0 | 7108 | { |
8b3079a5 AK |
7109 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
7110 | enum emulation_result err = EMULATE_DONE; | |
80ced186 | 7111 | int ret = 1; |
49e9d557 AK |
7112 | u32 cpu_exec_ctrl; |
7113 | bool intr_window_requested; | |
b8405c18 | 7114 | unsigned count = 130; |
49e9d557 | 7115 | |
2bb8cafe SC |
7116 | /* |
7117 | * We should never reach the point where we are emulating L2 | |
7118 | * due to invalid guest state as that means we incorrectly | |
7119 | * allowed a nested VMEntry with an invalid vmcs12. | |
7120 | */ | |
7121 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); | |
7122 | ||
49e9d557 AK |
7123 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
7124 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; | |
ea953ef0 | 7125 | |
98eb2f8b | 7126 | while (vmx->emulation_required && count-- != 0) { |
bdea48e3 | 7127 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
49e9d557 AK |
7128 | return handle_interrupt_window(&vmx->vcpu); |
7129 | ||
72875d8a | 7130 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
de87dcdd AK |
7131 | return 1; |
7132 | ||
9b8ae637 | 7133 | err = emulate_instruction(vcpu, 0); |
ea953ef0 | 7134 | |
ac0a48c3 | 7135 | if (err == EMULATE_USER_EXIT) { |
94452b9e | 7136 | ++vcpu->stat.mmio_exits; |
80ced186 MG |
7137 | ret = 0; |
7138 | goto out; | |
7139 | } | |
1d5a4d9b | 7140 | |
add5ff7a SC |
7141 | if (err != EMULATE_DONE) |
7142 | goto emulation_error; | |
7143 | ||
7144 | if (vmx->emulation_required && !vmx->rmode.vm86_active && | |
7145 | vcpu->arch.exception.pending) | |
7146 | goto emulation_error; | |
ea953ef0 | 7147 | |
8d76c49e GN |
7148 | if (vcpu->arch.halt_request) { |
7149 | vcpu->arch.halt_request = 0; | |
5cb56059 | 7150 | ret = kvm_vcpu_halt(vcpu); |
8d76c49e GN |
7151 | goto out; |
7152 | } | |
7153 | ||
ea953ef0 | 7154 | if (signal_pending(current)) |
80ced186 | 7155 | goto out; |
ea953ef0 MG |
7156 | if (need_resched()) |
7157 | schedule(); | |
7158 | } | |
7159 | ||
80ced186 MG |
7160 | out: |
7161 | return ret; | |
b4a2d31d | 7162 | |
add5ff7a SC |
7163 | emulation_error: |
7164 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
7165 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
7166 | vcpu->run->internal.ndata = 0; | |
7167 | return 0; | |
b4a2d31d RK |
7168 | } |
7169 | ||
7170 | static void grow_ple_window(struct kvm_vcpu *vcpu) | |
7171 | { | |
7172 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7173 | int old = vmx->ple_window; | |
7174 | ||
c8e88717 BM |
7175 | vmx->ple_window = __grow_ple_window(old, ple_window, |
7176 | ple_window_grow, | |
7177 | ple_window_max); | |
b4a2d31d RK |
7178 | |
7179 | if (vmx->ple_window != old) | |
7180 | vmx->ple_window_dirty = true; | |
7b46268d RK |
7181 | |
7182 | trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
7183 | } |
7184 | ||
7185 | static void shrink_ple_window(struct kvm_vcpu *vcpu) | |
7186 | { | |
7187 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7188 | int old = vmx->ple_window; | |
7189 | ||
c8e88717 BM |
7190 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
7191 | ple_window_shrink, | |
7192 | ple_window); | |
b4a2d31d RK |
7193 | |
7194 | if (vmx->ple_window != old) | |
7195 | vmx->ple_window_dirty = true; | |
7b46268d RK |
7196 | |
7197 | trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
7198 | } |
7199 | ||
bf9f6ac8 FW |
7200 | /* |
7201 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. | |
7202 | */ | |
7203 | static void wakeup_handler(void) | |
7204 | { | |
7205 | struct kvm_vcpu *vcpu; | |
7206 | int cpu = smp_processor_id(); | |
7207 | ||
7208 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
7209 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), | |
7210 | blocked_vcpu_list) { | |
7211 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
7212 | ||
7213 | if (pi_test_on(pi_desc) == 1) | |
7214 | kvm_vcpu_kick(vcpu); | |
7215 | } | |
7216 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
7217 | } | |
7218 | ||
e01bca2f | 7219 | static void vmx_enable_tdp(void) |
f160c7b7 JS |
7220 | { |
7221 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, | |
7222 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, | |
7223 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, | |
7224 | 0ull, VMX_EPT_EXECUTABLE_MASK, | |
7225 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, | |
d0ec49d4 | 7226 | VMX_EPT_RWX_MASK, 0ull); |
f160c7b7 JS |
7227 | |
7228 | ept_set_mmio_spte_mask(); | |
7229 | kvm_enable_tdp(); | |
7230 | } | |
7231 | ||
f2c7648d TC |
7232 | static __init int hardware_setup(void) |
7233 | { | |
904e14fb | 7234 | int r = -ENOMEM, i; |
34a1cd60 TC |
7235 | |
7236 | rdmsrl_safe(MSR_EFER, &host_efer); | |
7237 | ||
7238 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) | |
7239 | kvm_define_shared_msr(i, vmx_msr_index[i]); | |
7240 | ||
23611332 RK |
7241 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
7242 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); | |
7243 | if (!vmx_bitmap[i]) | |
7244 | goto out; | |
7245 | } | |
34a1cd60 | 7246 | |
34a1cd60 TC |
7247 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
7248 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); | |
7249 | ||
34a1cd60 TC |
7250 | if (setup_vmcs_config(&vmcs_config) < 0) { |
7251 | r = -EIO; | |
23611332 | 7252 | goto out; |
baa03522 | 7253 | } |
f2c7648d TC |
7254 | |
7255 | if (boot_cpu_has(X86_FEATURE_NX)) | |
7256 | kvm_enable_efer_bits(EFER_NX); | |
7257 | ||
08d839c4 WL |
7258 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
7259 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) | |
f2c7648d | 7260 | enable_vpid = 0; |
08d839c4 | 7261 | |
f2c7648d | 7262 | if (!cpu_has_vmx_ept() || |
42aa53b4 | 7263 | !cpu_has_vmx_ept_4levels() || |
f5f51586 | 7264 | !cpu_has_vmx_ept_mt_wb() || |
8ad8182e | 7265 | !cpu_has_vmx_invept_global()) |
f2c7648d | 7266 | enable_ept = 0; |
f2c7648d | 7267 | |
fce6ac4c | 7268 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
f2c7648d TC |
7269 | enable_ept_ad_bits = 0; |
7270 | ||
8ad8182e | 7271 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
f2c7648d TC |
7272 | enable_unrestricted_guest = 0; |
7273 | ||
ad15a296 | 7274 | if (!cpu_has_vmx_flexpriority()) |
f2c7648d TC |
7275 | flexpriority_enabled = 0; |
7276 | ||
d02fcf50 PB |
7277 | if (!cpu_has_virtual_nmis()) |
7278 | enable_vnmi = 0; | |
7279 | ||
ad15a296 PB |
7280 | /* |
7281 | * set_apic_access_page_addr() is used to reload apic access | |
7282 | * page upon invalidation. No need to do anything if not | |
7283 | * using the APIC_ACCESS_ADDR VMCS field. | |
7284 | */ | |
7285 | if (!flexpriority_enabled) | |
f2c7648d | 7286 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
f2c7648d TC |
7287 | |
7288 | if (!cpu_has_vmx_tpr_shadow()) | |
7289 | kvm_x86_ops->update_cr8_intercept = NULL; | |
7290 | ||
7291 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) | |
7292 | kvm_disable_largepages(); | |
7293 | ||
0f107682 | 7294 | if (!cpu_has_vmx_ple()) { |
f2c7648d | 7295 | ple_gap = 0; |
0f107682 WL |
7296 | ple_window = 0; |
7297 | ple_window_grow = 0; | |
7298 | ple_window_max = 0; | |
7299 | ple_window_shrink = 0; | |
7300 | } | |
f2c7648d | 7301 | |
76dfafd5 | 7302 | if (!cpu_has_vmx_apicv()) { |
f2c7648d | 7303 | enable_apicv = 0; |
76dfafd5 PB |
7304 | kvm_x86_ops->sync_pir_to_irr = NULL; |
7305 | } | |
f2c7648d | 7306 | |
64903d61 HZ |
7307 | if (cpu_has_vmx_tsc_scaling()) { |
7308 | kvm_has_tsc_control = true; | |
7309 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; | |
7310 | kvm_tsc_scaling_ratio_frac_bits = 48; | |
7311 | } | |
7312 | ||
04bb92e4 WL |
7313 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
7314 | ||
f160c7b7 JS |
7315 | if (enable_ept) |
7316 | vmx_enable_tdp(); | |
7317 | else | |
baa03522 TC |
7318 | kvm_disable_tdp(); |
7319 | ||
843e4330 KH |
7320 | /* |
7321 | * Only enable PML when hardware supports PML feature, and both EPT | |
7322 | * and EPT A/D bit features are enabled -- PML depends on them to work. | |
7323 | */ | |
7324 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) | |
7325 | enable_pml = 0; | |
7326 | ||
7327 | if (!enable_pml) { | |
7328 | kvm_x86_ops->slot_enable_log_dirty = NULL; | |
7329 | kvm_x86_ops->slot_disable_log_dirty = NULL; | |
7330 | kvm_x86_ops->flush_log_dirty = NULL; | |
7331 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; | |
7332 | } | |
7333 | ||
64672c95 YJ |
7334 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
7335 | u64 vmx_msr; | |
7336 | ||
7337 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
7338 | cpu_preemption_timer_multi = | |
7339 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; | |
7340 | } else { | |
7341 | kvm_x86_ops->set_hv_timer = NULL; | |
7342 | kvm_x86_ops->cancel_hv_timer = NULL; | |
7343 | } | |
7344 | ||
c5d167b2 PB |
7345 | if (!cpu_has_vmx_shadow_vmcs()) |
7346 | enable_shadow_vmcs = 0; | |
7347 | if (enable_shadow_vmcs) | |
7348 | init_vmcs_shadow_fields(); | |
7349 | ||
bf9f6ac8 | 7350 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
1389309c | 7351 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv); |
bf9f6ac8 | 7352 | |
c45dcc71 AR |
7353 | kvm_mce_cap_supported |= MCG_LMCE_P; |
7354 | ||
f2c7648d | 7355 | return alloc_kvm_area(); |
34a1cd60 | 7356 | |
34a1cd60 | 7357 | out: |
23611332 RK |
7358 | for (i = 0; i < VMX_BITMAP_NR; i++) |
7359 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 TC |
7360 | |
7361 | return r; | |
f2c7648d TC |
7362 | } |
7363 | ||
7364 | static __exit void hardware_unsetup(void) | |
7365 | { | |
23611332 RK |
7366 | int i; |
7367 | ||
7368 | for (i = 0; i < VMX_BITMAP_NR; i++) | |
7369 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 | 7370 | |
f2c7648d TC |
7371 | free_kvm_area(); |
7372 | } | |
7373 | ||
4b8d54f9 ZE |
7374 | /* |
7375 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE | |
7376 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. | |
7377 | */ | |
9fb41ba8 | 7378 | static int handle_pause(struct kvm_vcpu *vcpu) |
4b8d54f9 | 7379 | { |
b31c114b | 7380 | if (!kvm_pause_in_guest(vcpu->kvm)) |
b4a2d31d RK |
7381 | grow_ple_window(vcpu); |
7382 | ||
de63ad4c LM |
7383 | /* |
7384 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" | |
7385 | * VM-execution control is ignored if CPL > 0. OTOH, KVM | |
7386 | * never set PAUSE_EXITING and just set PLE if supported, | |
7387 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. | |
7388 | */ | |
7389 | kvm_vcpu_on_spin(vcpu, true); | |
6affcbed | 7390 | return kvm_skip_emulated_instruction(vcpu); |
4b8d54f9 ZE |
7391 | } |
7392 | ||
87c00572 | 7393 | static int handle_nop(struct kvm_vcpu *vcpu) |
59708670 | 7394 | { |
6affcbed | 7395 | return kvm_skip_emulated_instruction(vcpu); |
59708670 SY |
7396 | } |
7397 | ||
87c00572 GS |
7398 | static int handle_mwait(struct kvm_vcpu *vcpu) |
7399 | { | |
7400 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); | |
7401 | return handle_nop(vcpu); | |
7402 | } | |
7403 | ||
45ec368c JM |
7404 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
7405 | { | |
7406 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7407 | return 1; | |
7408 | } | |
7409 | ||
5f3d45e7 MD |
7410 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
7411 | { | |
7412 | return 1; | |
7413 | } | |
7414 | ||
87c00572 GS |
7415 | static int handle_monitor(struct kvm_vcpu *vcpu) |
7416 | { | |
7417 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); | |
7418 | return handle_nop(vcpu); | |
7419 | } | |
7420 | ||
0658fbaa ACL |
7421 | /* |
7422 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), | |
7423 | * set the success or error code of an emulated VMX instruction, as specified | |
7424 | * by Vol 2B, VMX Instruction Reference, "Conventions". | |
7425 | */ | |
7426 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) | |
7427 | { | |
7428 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) | |
7429 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
7430 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); | |
7431 | } | |
7432 | ||
7433 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) | |
7434 | { | |
7435 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
7436 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | | |
7437 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
7438 | | X86_EFLAGS_CF); | |
7439 | } | |
7440 | ||
145c28dd | 7441 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
0658fbaa ACL |
7442 | u32 vm_instruction_error) |
7443 | { | |
7444 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { | |
7445 | /* | |
7446 | * failValid writes the error number to the current VMCS, which | |
7447 | * can't be done there isn't a current VMCS. | |
7448 | */ | |
7449 | nested_vmx_failInvalid(vcpu); | |
7450 | return; | |
7451 | } | |
7452 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
7453 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
7454 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
7455 | | X86_EFLAGS_ZF); | |
7456 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; | |
7457 | /* | |
7458 | * We don't need to force a shadow sync because | |
7459 | * VM_INSTRUCTION_ERROR is not shadowed | |
7460 | */ | |
7461 | } | |
145c28dd | 7462 | |
ff651cb6 WV |
7463 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
7464 | { | |
7465 | /* TODO: not to reset guest simply here. */ | |
7466 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); | |
bbe41b95 | 7467 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
ff651cb6 WV |
7468 | } |
7469 | ||
f4124500 JK |
7470 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
7471 | { | |
7472 | struct vcpu_vmx *vmx = | |
7473 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); | |
7474 | ||
7475 | vmx->nested.preemption_timer_expired = true; | |
7476 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); | |
7477 | kvm_vcpu_kick(&vmx->vcpu); | |
7478 | ||
7479 | return HRTIMER_NORESTART; | |
7480 | } | |
7481 | ||
19677e32 BD |
7482 | /* |
7483 | * Decode the memory-address operand of a vmx instruction, as recorded on an | |
7484 | * exit caused by such an instruction (run by a guest hypervisor). | |
7485 | * On success, returns 0. When the operand is invalid, returns 1 and throws | |
7486 | * #UD or #GP. | |
7487 | */ | |
7488 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, | |
7489 | unsigned long exit_qualification, | |
f9eb4af6 | 7490 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
19677e32 | 7491 | { |
f9eb4af6 EK |
7492 | gva_t off; |
7493 | bool exn; | |
7494 | struct kvm_segment s; | |
7495 | ||
19677e32 BD |
7496 | /* |
7497 | * According to Vol. 3B, "Information for VM Exits Due to Instruction | |
7498 | * Execution", on an exit, vmx_instruction_info holds most of the | |
7499 | * addressing components of the operand. Only the displacement part | |
7500 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). | |
7501 | * For how an actual address is calculated from all these components, | |
7502 | * refer to Vol. 1, "Operand Addressing". | |
7503 | */ | |
7504 | int scaling = vmx_instruction_info & 3; | |
7505 | int addr_size = (vmx_instruction_info >> 7) & 7; | |
7506 | bool is_reg = vmx_instruction_info & (1u << 10); | |
7507 | int seg_reg = (vmx_instruction_info >> 15) & 7; | |
7508 | int index_reg = (vmx_instruction_info >> 18) & 0xf; | |
7509 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); | |
7510 | int base_reg = (vmx_instruction_info >> 23) & 0xf; | |
7511 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); | |
7512 | ||
7513 | if (is_reg) { | |
7514 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7515 | return 1; | |
7516 | } | |
7517 | ||
7518 | /* Addr = segment_base + offset */ | |
7519 | /* offset = base + [index * scale] + displacement */ | |
f9eb4af6 | 7520 | off = exit_qualification; /* holds the displacement */ |
19677e32 | 7521 | if (base_is_valid) |
f9eb4af6 | 7522 | off += kvm_register_read(vcpu, base_reg); |
19677e32 | 7523 | if (index_is_valid) |
f9eb4af6 EK |
7524 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
7525 | vmx_get_segment(vcpu, &s, seg_reg); | |
7526 | *ret = s.base + off; | |
19677e32 BD |
7527 | |
7528 | if (addr_size == 1) /* 32 bit */ | |
7529 | *ret &= 0xffffffff; | |
7530 | ||
f9eb4af6 EK |
7531 | /* Checks for #GP/#SS exceptions. */ |
7532 | exn = false; | |
ff30ef40 QC |
7533 | if (is_long_mode(vcpu)) { |
7534 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a | |
7535 | * non-canonical form. This is the only check on the memory | |
7536 | * destination for long mode! | |
7537 | */ | |
fd8cb433 | 7538 | exn = is_noncanonical_address(*ret, vcpu); |
ff30ef40 | 7539 | } else if (is_protmode(vcpu)) { |
f9eb4af6 EK |
7540 | /* Protected mode: apply checks for segment validity in the |
7541 | * following order: | |
7542 | * - segment type check (#GP(0) may be thrown) | |
7543 | * - usability check (#GP(0)/#SS(0)) | |
7544 | * - limit check (#GP(0)/#SS(0)) | |
7545 | */ | |
7546 | if (wr) | |
7547 | /* #GP(0) if the destination operand is located in a | |
7548 | * read-only data segment or any code segment. | |
7549 | */ | |
7550 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); | |
7551 | else | |
7552 | /* #GP(0) if the source operand is located in an | |
7553 | * execute-only code segment | |
7554 | */ | |
7555 | exn = ((s.type & 0xa) == 8); | |
ff30ef40 QC |
7556 | if (exn) { |
7557 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
7558 | return 1; | |
7559 | } | |
f9eb4af6 EK |
7560 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
7561 | */ | |
7562 | exn = (s.unusable != 0); | |
7563 | /* Protected mode: #GP(0)/#SS(0) if the memory | |
7564 | * operand is outside the segment limit. | |
7565 | */ | |
7566 | exn = exn || (off + sizeof(u64) > s.limit); | |
7567 | } | |
7568 | if (exn) { | |
7569 | kvm_queue_exception_e(vcpu, | |
7570 | seg_reg == VCPU_SREG_SS ? | |
7571 | SS_VECTOR : GP_VECTOR, | |
7572 | 0); | |
7573 | return 1; | |
7574 | } | |
7575 | ||
19677e32 BD |
7576 | return 0; |
7577 | } | |
7578 | ||
cbf71279 | 7579 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer) |
3573e22c BD |
7580 | { |
7581 | gva_t gva; | |
3573e22c | 7582 | struct x86_exception e; |
3573e22c BD |
7583 | |
7584 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 7585 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
3573e22c BD |
7586 | return 1; |
7587 | ||
cbf71279 RK |
7588 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer, |
7589 | sizeof(*vmpointer), &e)) { | |
3573e22c BD |
7590 | kvm_inject_page_fault(vcpu, &e); |
7591 | return 1; | |
7592 | } | |
7593 | ||
3573e22c BD |
7594 | return 0; |
7595 | } | |
7596 | ||
e29acc55 JM |
7597 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
7598 | { | |
7599 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7600 | struct vmcs *shadow_vmcs; | |
f21f165e | 7601 | int r; |
e29acc55 | 7602 | |
f21f165e PB |
7603 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
7604 | if (r < 0) | |
de3a0021 | 7605 | goto out_vmcs02; |
e29acc55 JM |
7606 | |
7607 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); | |
7608 | if (!vmx->nested.cached_vmcs12) | |
7609 | goto out_cached_vmcs12; | |
7610 | ||
7611 | if (enable_shadow_vmcs) { | |
7612 | shadow_vmcs = alloc_vmcs(); | |
7613 | if (!shadow_vmcs) | |
7614 | goto out_shadow_vmcs; | |
7615 | /* mark vmcs as shadow */ | |
7616 | shadow_vmcs->revision_id |= (1u << 31); | |
7617 | /* init shadow vmcs */ | |
7618 | vmcs_clear(shadow_vmcs); | |
7619 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; | |
7620 | } | |
7621 | ||
e29acc55 JM |
7622 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
7623 | HRTIMER_MODE_REL_PINNED); | |
7624 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; | |
7625 | ||
7626 | vmx->nested.vmxon = true; | |
7627 | return 0; | |
7628 | ||
7629 | out_shadow_vmcs: | |
7630 | kfree(vmx->nested.cached_vmcs12); | |
7631 | ||
7632 | out_cached_vmcs12: | |
de3a0021 | 7633 | free_loaded_vmcs(&vmx->nested.vmcs02); |
e29acc55 | 7634 | |
de3a0021 | 7635 | out_vmcs02: |
e29acc55 JM |
7636 | return -ENOMEM; |
7637 | } | |
7638 | ||
ec378aee NHE |
7639 | /* |
7640 | * Emulate the VMXON instruction. | |
7641 | * Currently, we just remember that VMX is active, and do not save or even | |
7642 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we | |
7643 | * do not currently need to store anything in that guest-allocated memory | |
7644 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their | |
7645 | * argument is different from the VMXON pointer (which the spec says they do). | |
7646 | */ | |
7647 | static int handle_vmon(struct kvm_vcpu *vcpu) | |
7648 | { | |
e29acc55 | 7649 | int ret; |
cbf71279 RK |
7650 | gpa_t vmptr; |
7651 | struct page *page; | |
ec378aee | 7652 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b3897a49 NHE |
7653 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
7654 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
ec378aee | 7655 | |
70f3aac9 JM |
7656 | /* |
7657 | * The Intel VMX Instruction Reference lists a bunch of bits that are | |
7658 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to | |
7659 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). | |
7660 | * Otherwise, we should fail with #UD. But most faulting conditions | |
7661 | * have already been checked by hardware, prior to the VM-exit for | |
7662 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has | |
7663 | * that bit set to 1 in non-root mode. | |
ec378aee | 7664 | */ |
70f3aac9 | 7665 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
ec378aee NHE |
7666 | kvm_queue_exception(vcpu, UD_VECTOR); |
7667 | return 1; | |
7668 | } | |
7669 | ||
145c28dd AG |
7670 | if (vmx->nested.vmxon) { |
7671 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); | |
6affcbed | 7672 | return kvm_skip_emulated_instruction(vcpu); |
145c28dd | 7673 | } |
b3897a49 | 7674 | |
3b84080b | 7675 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
b3897a49 NHE |
7676 | != VMXON_NEEDED_FEATURES) { |
7677 | kvm_inject_gp(vcpu, 0); | |
7678 | return 1; | |
7679 | } | |
7680 | ||
cbf71279 | 7681 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
21e7fbe7 | 7682 | return 1; |
cbf71279 RK |
7683 | |
7684 | /* | |
7685 | * SDM 3: 24.11.5 | |
7686 | * The first 4 bytes of VMXON region contain the supported | |
7687 | * VMCS revision identifier | |
7688 | * | |
7689 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; | |
7690 | * which replaces physical address width with 32 | |
7691 | */ | |
7692 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { | |
7693 | nested_vmx_failInvalid(vcpu); | |
7694 | return kvm_skip_emulated_instruction(vcpu); | |
7695 | } | |
7696 | ||
5e2f30b7 DH |
7697 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
7698 | if (is_error_page(page)) { | |
cbf71279 RK |
7699 | nested_vmx_failInvalid(vcpu); |
7700 | return kvm_skip_emulated_instruction(vcpu); | |
7701 | } | |
7702 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { | |
7703 | kunmap(page); | |
53a70daf | 7704 | kvm_release_page_clean(page); |
cbf71279 RK |
7705 | nested_vmx_failInvalid(vcpu); |
7706 | return kvm_skip_emulated_instruction(vcpu); | |
7707 | } | |
7708 | kunmap(page); | |
53a70daf | 7709 | kvm_release_page_clean(page); |
cbf71279 RK |
7710 | |
7711 | vmx->nested.vmxon_ptr = vmptr; | |
e29acc55 JM |
7712 | ret = enter_vmx_operation(vcpu); |
7713 | if (ret) | |
7714 | return ret; | |
ec378aee | 7715 | |
a25eb114 | 7716 | nested_vmx_succeed(vcpu); |
6affcbed | 7717 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7718 | } |
7719 | ||
7720 | /* | |
7721 | * Intel's VMX Instruction Reference specifies a common set of prerequisites | |
7722 | * for running VMX instructions (except VMXON, whose prerequisites are | |
7723 | * slightly different). It also specifies what exception to inject otherwise. | |
70f3aac9 JM |
7724 | * Note that many of these exceptions have priority over VM exits, so they |
7725 | * don't have to be checked again here. | |
ec378aee NHE |
7726 | */ |
7727 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) | |
7728 | { | |
70f3aac9 | 7729 | if (!to_vmx(vcpu)->nested.vmxon) { |
ec378aee NHE |
7730 | kvm_queue_exception(vcpu, UD_VECTOR); |
7731 | return 0; | |
7732 | } | |
ec378aee NHE |
7733 | return 1; |
7734 | } | |
7735 | ||
8ca44e88 DM |
7736 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
7737 | { | |
7738 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); | |
7739 | vmcs_write64(VMCS_LINK_POINTER, -1ull); | |
7740 | } | |
7741 | ||
e7953d7f AG |
7742 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
7743 | { | |
9a2a05b9 PB |
7744 | if (vmx->nested.current_vmptr == -1ull) |
7745 | return; | |
7746 | ||
012f83cb | 7747 | if (enable_shadow_vmcs) { |
9a2a05b9 PB |
7748 | /* copy to memory all shadowed fields in case |
7749 | they were modified */ | |
7750 | copy_shadow_to_vmcs12(vmx); | |
7751 | vmx->nested.sync_shadow_vmcs = false; | |
8ca44e88 | 7752 | vmx_disable_shadow_vmcs(vmx); |
012f83cb | 7753 | } |
705699a1 | 7754 | vmx->nested.posted_intr_nv = -1; |
4f2777bc DM |
7755 | |
7756 | /* Flush VMCS12 to guest memory */ | |
9f744c59 PB |
7757 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
7758 | vmx->nested.current_vmptr >> PAGE_SHIFT, | |
7759 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); | |
4f2777bc | 7760 | |
9a2a05b9 | 7761 | vmx->nested.current_vmptr = -1ull; |
e7953d7f AG |
7762 | } |
7763 | ||
ec378aee NHE |
7764 | /* |
7765 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or | |
7766 | * just stops using VMX. | |
7767 | */ | |
7768 | static void free_nested(struct vcpu_vmx *vmx) | |
7769 | { | |
b7455825 | 7770 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
ec378aee | 7771 | return; |
9a2a05b9 | 7772 | |
ec378aee | 7773 | vmx->nested.vmxon = false; |
b7455825 | 7774 | vmx->nested.smm.vmxon = false; |
5c614b35 | 7775 | free_vpid(vmx->nested.vpid02); |
8ca44e88 DM |
7776 | vmx->nested.posted_intr_nv = -1; |
7777 | vmx->nested.current_vmptr = -1ull; | |
355f4fb1 | 7778 | if (enable_shadow_vmcs) { |
8ca44e88 | 7779 | vmx_disable_shadow_vmcs(vmx); |
355f4fb1 JM |
7780 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
7781 | free_vmcs(vmx->vmcs01.shadow_vmcs); | |
7782 | vmx->vmcs01.shadow_vmcs = NULL; | |
7783 | } | |
4f2777bc | 7784 | kfree(vmx->nested.cached_vmcs12); |
de3a0021 | 7785 | /* Unpin physical memory we referred to in the vmcs02 */ |
fe3ef05c | 7786 | if (vmx->nested.apic_access_page) { |
53a70daf | 7787 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
48d89b92 | 7788 | vmx->nested.apic_access_page = NULL; |
fe3ef05c | 7789 | } |
a7c0b07d | 7790 | if (vmx->nested.virtual_apic_page) { |
53a70daf | 7791 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
48d89b92 | 7792 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 7793 | } |
705699a1 WV |
7794 | if (vmx->nested.pi_desc_page) { |
7795 | kunmap(vmx->nested.pi_desc_page); | |
53a70daf | 7796 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
705699a1 WV |
7797 | vmx->nested.pi_desc_page = NULL; |
7798 | vmx->nested.pi_desc = NULL; | |
7799 | } | |
ff2f6fe9 | 7800 | |
de3a0021 | 7801 | free_loaded_vmcs(&vmx->nested.vmcs02); |
ec378aee NHE |
7802 | } |
7803 | ||
7804 | /* Emulate the VMXOFF instruction */ | |
7805 | static int handle_vmoff(struct kvm_vcpu *vcpu) | |
7806 | { | |
7807 | if (!nested_vmx_check_permission(vcpu)) | |
7808 | return 1; | |
7809 | free_nested(to_vmx(vcpu)); | |
a25eb114 | 7810 | nested_vmx_succeed(vcpu); |
6affcbed | 7811 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7812 | } |
7813 | ||
27d6c865 NHE |
7814 | /* Emulate the VMCLEAR instruction */ |
7815 | static int handle_vmclear(struct kvm_vcpu *vcpu) | |
7816 | { | |
7817 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
587d7e72 | 7818 | u32 zero = 0; |
27d6c865 | 7819 | gpa_t vmptr; |
27d6c865 NHE |
7820 | |
7821 | if (!nested_vmx_check_permission(vcpu)) | |
7822 | return 1; | |
7823 | ||
cbf71279 | 7824 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
27d6c865 | 7825 | return 1; |
27d6c865 | 7826 | |
cbf71279 RK |
7827 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
7828 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); | |
7829 | return kvm_skip_emulated_instruction(vcpu); | |
7830 | } | |
7831 | ||
7832 | if (vmptr == vmx->nested.vmxon_ptr) { | |
7833 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); | |
7834 | return kvm_skip_emulated_instruction(vcpu); | |
7835 | } | |
7836 | ||
9a2a05b9 | 7837 | if (vmptr == vmx->nested.current_vmptr) |
e7953d7f | 7838 | nested_release_vmcs12(vmx); |
27d6c865 | 7839 | |
587d7e72 JM |
7840 | kvm_vcpu_write_guest(vcpu, |
7841 | vmptr + offsetof(struct vmcs12, launch_state), | |
7842 | &zero, sizeof(zero)); | |
27d6c865 | 7843 | |
27d6c865 | 7844 | nested_vmx_succeed(vcpu); |
6affcbed | 7845 | return kvm_skip_emulated_instruction(vcpu); |
27d6c865 NHE |
7846 | } |
7847 | ||
cd232ad0 NHE |
7848 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
7849 | ||
7850 | /* Emulate the VMLAUNCH instruction */ | |
7851 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) | |
7852 | { | |
7853 | return nested_vmx_run(vcpu, true); | |
7854 | } | |
7855 | ||
7856 | /* Emulate the VMRESUME instruction */ | |
7857 | static int handle_vmresume(struct kvm_vcpu *vcpu) | |
7858 | { | |
7859 | ||
7860 | return nested_vmx_run(vcpu, false); | |
7861 | } | |
7862 | ||
49f705c5 NHE |
7863 | /* |
7864 | * Read a vmcs12 field. Since these can have varying lengths and we return | |
7865 | * one type, we chose the biggest type (u64) and zero-extend the return value | |
7866 | * to that size. Note that the caller, handle_vmread, might need to use only | |
7867 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of | |
7868 | * 64-bit fields are to be returned). | |
7869 | */ | |
a2ae9df7 PB |
7870 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
7871 | unsigned long field, u64 *ret) | |
49f705c5 NHE |
7872 | { |
7873 | short offset = vmcs_field_to_offset(field); | |
7874 | char *p; | |
7875 | ||
7876 | if (offset < 0) | |
a2ae9df7 | 7877 | return offset; |
49f705c5 NHE |
7878 | |
7879 | p = ((char *)(get_vmcs12(vcpu))) + offset; | |
7880 | ||
d37f4267 JM |
7881 | switch (vmcs_field_width(field)) { |
7882 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: | |
49f705c5 | 7883 | *ret = *((natural_width *)p); |
a2ae9df7 | 7884 | return 0; |
d37f4267 | 7885 | case VMCS_FIELD_WIDTH_U16: |
49f705c5 | 7886 | *ret = *((u16 *)p); |
a2ae9df7 | 7887 | return 0; |
d37f4267 | 7888 | case VMCS_FIELD_WIDTH_U32: |
49f705c5 | 7889 | *ret = *((u32 *)p); |
a2ae9df7 | 7890 | return 0; |
d37f4267 | 7891 | case VMCS_FIELD_WIDTH_U64: |
49f705c5 | 7892 | *ret = *((u64 *)p); |
a2ae9df7 | 7893 | return 0; |
49f705c5 | 7894 | default: |
a2ae9df7 PB |
7895 | WARN_ON(1); |
7896 | return -ENOENT; | |
49f705c5 NHE |
7897 | } |
7898 | } | |
7899 | ||
20b97fea | 7900 | |
a2ae9df7 PB |
7901 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
7902 | unsigned long field, u64 field_value){ | |
20b97fea AG |
7903 | short offset = vmcs_field_to_offset(field); |
7904 | char *p = ((char *) get_vmcs12(vcpu)) + offset; | |
7905 | if (offset < 0) | |
a2ae9df7 | 7906 | return offset; |
20b97fea | 7907 | |
d37f4267 JM |
7908 | switch (vmcs_field_width(field)) { |
7909 | case VMCS_FIELD_WIDTH_U16: | |
20b97fea | 7910 | *(u16 *)p = field_value; |
a2ae9df7 | 7911 | return 0; |
d37f4267 | 7912 | case VMCS_FIELD_WIDTH_U32: |
20b97fea | 7913 | *(u32 *)p = field_value; |
a2ae9df7 | 7914 | return 0; |
d37f4267 | 7915 | case VMCS_FIELD_WIDTH_U64: |
20b97fea | 7916 | *(u64 *)p = field_value; |
a2ae9df7 | 7917 | return 0; |
d37f4267 | 7918 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
20b97fea | 7919 | *(natural_width *)p = field_value; |
a2ae9df7 | 7920 | return 0; |
20b97fea | 7921 | default: |
a2ae9df7 PB |
7922 | WARN_ON(1); |
7923 | return -ENOENT; | |
20b97fea AG |
7924 | } |
7925 | ||
7926 | } | |
7927 | ||
16f5b903 AG |
7928 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
7929 | { | |
7930 | int i; | |
7931 | unsigned long field; | |
7932 | u64 field_value; | |
355f4fb1 | 7933 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
44900ba6 | 7934 | const u16 *fields = shadow_read_write_fields; |
c2bae893 | 7935 | const int num_fields = max_shadow_read_write_fields; |
16f5b903 | 7936 | |
282da870 JK |
7937 | preempt_disable(); |
7938 | ||
16f5b903 AG |
7939 | vmcs_load(shadow_vmcs); |
7940 | ||
7941 | for (i = 0; i < num_fields; i++) { | |
7942 | field = fields[i]; | |
44900ba6 | 7943 | field_value = __vmcs_readl(field); |
16f5b903 AG |
7944 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
7945 | } | |
7946 | ||
7947 | vmcs_clear(shadow_vmcs); | |
7948 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
282da870 JK |
7949 | |
7950 | preempt_enable(); | |
16f5b903 AG |
7951 | } |
7952 | ||
c3114420 AG |
7953 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
7954 | { | |
44900ba6 | 7955 | const u16 *fields[] = { |
c2bae893 MK |
7956 | shadow_read_write_fields, |
7957 | shadow_read_only_fields | |
c3114420 | 7958 | }; |
c2bae893 | 7959 | const int max_fields[] = { |
c3114420 AG |
7960 | max_shadow_read_write_fields, |
7961 | max_shadow_read_only_fields | |
7962 | }; | |
7963 | int i, q; | |
7964 | unsigned long field; | |
7965 | u64 field_value = 0; | |
355f4fb1 | 7966 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
c3114420 AG |
7967 | |
7968 | vmcs_load(shadow_vmcs); | |
7969 | ||
c2bae893 | 7970 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
c3114420 AG |
7971 | for (i = 0; i < max_fields[q]; i++) { |
7972 | field = fields[q][i]; | |
7973 | vmcs12_read_any(&vmx->vcpu, field, &field_value); | |
44900ba6 | 7974 | __vmcs_writel(field, field_value); |
c3114420 AG |
7975 | } |
7976 | } | |
7977 | ||
7978 | vmcs_clear(shadow_vmcs); | |
7979 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
7980 | } | |
7981 | ||
49f705c5 NHE |
7982 | /* |
7983 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was | |
7984 | * used before) all generate the same failure when it is missing. | |
7985 | */ | |
7986 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) | |
7987 | { | |
7988 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7989 | if (vmx->nested.current_vmptr == -1ull) { | |
7990 | nested_vmx_failInvalid(vcpu); | |
49f705c5 NHE |
7991 | return 0; |
7992 | } | |
7993 | return 1; | |
7994 | } | |
7995 | ||
7996 | static int handle_vmread(struct kvm_vcpu *vcpu) | |
7997 | { | |
7998 | unsigned long field; | |
7999 | u64 field_value; | |
8000 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8001 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
8002 | gva_t gva = 0; | |
8003 | ||
eb277562 | 8004 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
8005 | return 1; |
8006 | ||
6affcbed KH |
8007 | if (!nested_vmx_check_vmcs12(vcpu)) |
8008 | return kvm_skip_emulated_instruction(vcpu); | |
49f705c5 NHE |
8009 | |
8010 | /* Decode instruction info and find the field to read */ | |
27e6fb5d | 8011 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 | 8012 | /* Read the field, zero-extended to a u64 field_value */ |
a2ae9df7 | 8013 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
49f705c5 | 8014 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 8015 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8016 | } |
8017 | /* | |
8018 | * Now copy part of this value to register or memory, as requested. | |
8019 | * Note that the number of bits actually copied is 32 or 64 depending | |
8020 | * on the guest's mode (32 or 64 bit), not on the given field's length. | |
8021 | */ | |
8022 | if (vmx_instruction_info & (1u << 10)) { | |
27e6fb5d | 8023 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
49f705c5 NHE |
8024 | field_value); |
8025 | } else { | |
8026 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 8027 | vmx_instruction_info, true, &gva)) |
49f705c5 | 8028 | return 1; |
70f3aac9 | 8029 | /* _system ok, as hardware has verified cpl=0 */ |
49f705c5 NHE |
8030 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
8031 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); | |
8032 | } | |
8033 | ||
8034 | nested_vmx_succeed(vcpu); | |
6affcbed | 8035 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8036 | } |
8037 | ||
8038 | ||
8039 | static int handle_vmwrite(struct kvm_vcpu *vcpu) | |
8040 | { | |
8041 | unsigned long field; | |
8042 | gva_t gva; | |
74a497fa | 8043 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
49f705c5 NHE |
8044 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
8045 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
74a497fa | 8046 | |
49f705c5 NHE |
8047 | /* The value to write might be 32 or 64 bits, depending on L1's long |
8048 | * mode, and eventually we need to write that into a field of several | |
8049 | * possible lengths. The code below first zero-extends the value to 64 | |
6a6256f9 | 8050 | * bit (field_value), and then copies only the appropriate number of |
49f705c5 NHE |
8051 | * bits into the vmcs12 field. |
8052 | */ | |
8053 | u64 field_value = 0; | |
8054 | struct x86_exception e; | |
8055 | ||
eb277562 | 8056 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
8057 | return 1; |
8058 | ||
6affcbed KH |
8059 | if (!nested_vmx_check_vmcs12(vcpu)) |
8060 | return kvm_skip_emulated_instruction(vcpu); | |
eb277562 | 8061 | |
49f705c5 | 8062 | if (vmx_instruction_info & (1u << 10)) |
27e6fb5d | 8063 | field_value = kvm_register_readl(vcpu, |
49f705c5 NHE |
8064 | (((vmx_instruction_info) >> 3) & 0xf)); |
8065 | else { | |
8066 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 8067 | vmx_instruction_info, false, &gva)) |
49f705c5 NHE |
8068 | return 1; |
8069 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, | |
27e6fb5d | 8070 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
49f705c5 NHE |
8071 | kvm_inject_page_fault(vcpu, &e); |
8072 | return 1; | |
8073 | } | |
8074 | } | |
8075 | ||
8076 | ||
27e6fb5d | 8077 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 NHE |
8078 | if (vmcs_field_readonly(field)) { |
8079 | nested_vmx_failValid(vcpu, | |
8080 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); | |
6affcbed | 8081 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8082 | } |
8083 | ||
a2ae9df7 | 8084 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
49f705c5 | 8085 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 8086 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8087 | } |
8088 | ||
74a497fa PB |
8089 | switch (field) { |
8090 | #define SHADOW_FIELD_RW(x) case x: | |
8091 | #include "vmx_shadow_fields.h" | |
8092 | /* | |
8093 | * The fields that can be updated by L1 without a vmexit are | |
8094 | * always updated in the vmcs02, the others go down the slow | |
8095 | * path of prepare_vmcs02. | |
8096 | */ | |
8097 | break; | |
8098 | default: | |
8099 | vmx->nested.dirty_vmcs12 = true; | |
8100 | break; | |
8101 | } | |
8102 | ||
49f705c5 | 8103 | nested_vmx_succeed(vcpu); |
6affcbed | 8104 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8105 | } |
8106 | ||
a8bc284e JM |
8107 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
8108 | { | |
8109 | vmx->nested.current_vmptr = vmptr; | |
8110 | if (enable_shadow_vmcs) { | |
8111 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
8112 | SECONDARY_EXEC_SHADOW_VMCS); | |
8113 | vmcs_write64(VMCS_LINK_POINTER, | |
8114 | __pa(vmx->vmcs01.shadow_vmcs)); | |
8115 | vmx->nested.sync_shadow_vmcs = true; | |
8116 | } | |
74a497fa | 8117 | vmx->nested.dirty_vmcs12 = true; |
a8bc284e JM |
8118 | } |
8119 | ||
63846663 NHE |
8120 | /* Emulate the VMPTRLD instruction */ |
8121 | static int handle_vmptrld(struct kvm_vcpu *vcpu) | |
8122 | { | |
8123 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
63846663 | 8124 | gpa_t vmptr; |
63846663 NHE |
8125 | |
8126 | if (!nested_vmx_check_permission(vcpu)) | |
8127 | return 1; | |
8128 | ||
cbf71279 | 8129 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
63846663 | 8130 | return 1; |
63846663 | 8131 | |
cbf71279 RK |
8132 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
8133 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); | |
8134 | return kvm_skip_emulated_instruction(vcpu); | |
8135 | } | |
8136 | ||
8137 | if (vmptr == vmx->nested.vmxon_ptr) { | |
8138 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); | |
8139 | return kvm_skip_emulated_instruction(vcpu); | |
8140 | } | |
8141 | ||
63846663 NHE |
8142 | if (vmx->nested.current_vmptr != vmptr) { |
8143 | struct vmcs12 *new_vmcs12; | |
8144 | struct page *page; | |
5e2f30b7 DH |
8145 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
8146 | if (is_error_page(page)) { | |
63846663 | 8147 | nested_vmx_failInvalid(vcpu); |
6affcbed | 8148 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
8149 | } |
8150 | new_vmcs12 = kmap(page); | |
8151 | if (new_vmcs12->revision_id != VMCS12_REVISION) { | |
8152 | kunmap(page); | |
53a70daf | 8153 | kvm_release_page_clean(page); |
63846663 NHE |
8154 | nested_vmx_failValid(vcpu, |
8155 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); | |
6affcbed | 8156 | return kvm_skip_emulated_instruction(vcpu); |
63846663 | 8157 | } |
63846663 | 8158 | |
9a2a05b9 | 8159 | nested_release_vmcs12(vmx); |
4f2777bc DM |
8160 | /* |
8161 | * Load VMCS12 from guest memory since it is not already | |
8162 | * cached. | |
8163 | */ | |
9f744c59 PB |
8164 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
8165 | kunmap(page); | |
53a70daf | 8166 | kvm_release_page_clean(page); |
9f744c59 | 8167 | |
a8bc284e | 8168 | set_current_vmptr(vmx, vmptr); |
63846663 NHE |
8169 | } |
8170 | ||
8171 | nested_vmx_succeed(vcpu); | |
6affcbed | 8172 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
8173 | } |
8174 | ||
6a4d7550 NHE |
8175 | /* Emulate the VMPTRST instruction */ |
8176 | static int handle_vmptrst(struct kvm_vcpu *vcpu) | |
8177 | { | |
8178 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8179 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
8180 | gva_t vmcs_gva; | |
8181 | struct x86_exception e; | |
8182 | ||
8183 | if (!nested_vmx_check_permission(vcpu)) | |
8184 | return 1; | |
8185 | ||
8186 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 8187 | vmx_instruction_info, true, &vmcs_gva)) |
6a4d7550 | 8188 | return 1; |
70f3aac9 | 8189 | /* ok to use *_system, as hardware has verified cpl=0 */ |
6a4d7550 NHE |
8190 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
8191 | (void *)&to_vmx(vcpu)->nested.current_vmptr, | |
8192 | sizeof(u64), &e)) { | |
8193 | kvm_inject_page_fault(vcpu, &e); | |
8194 | return 1; | |
8195 | } | |
8196 | nested_vmx_succeed(vcpu); | |
6affcbed | 8197 | return kvm_skip_emulated_instruction(vcpu); |
6a4d7550 NHE |
8198 | } |
8199 | ||
bfd0a56b NHE |
8200 | /* Emulate the INVEPT instruction */ |
8201 | static int handle_invept(struct kvm_vcpu *vcpu) | |
8202 | { | |
b9c237bb | 8203 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
bfd0a56b NHE |
8204 | u32 vmx_instruction_info, types; |
8205 | unsigned long type; | |
8206 | gva_t gva; | |
8207 | struct x86_exception e; | |
8208 | struct { | |
8209 | u64 eptp, gpa; | |
8210 | } operand; | |
bfd0a56b | 8211 | |
6677f3da | 8212 | if (!(vmx->nested.msrs.secondary_ctls_high & |
b9c237bb | 8213 | SECONDARY_EXEC_ENABLE_EPT) || |
6677f3da | 8214 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
bfd0a56b NHE |
8215 | kvm_queue_exception(vcpu, UD_VECTOR); |
8216 | return 1; | |
8217 | } | |
8218 | ||
8219 | if (!nested_vmx_check_permission(vcpu)) | |
8220 | return 1; | |
8221 | ||
bfd0a56b | 8222 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
27e6fb5d | 8223 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
bfd0a56b | 8224 | |
6677f3da | 8225 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
bfd0a56b | 8226 | |
85c856b3 | 8227 | if (type >= 32 || !(types & (1 << type))) { |
bfd0a56b NHE |
8228 | nested_vmx_failValid(vcpu, |
8229 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 8230 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
8231 | } |
8232 | ||
8233 | /* According to the Intel VMX instruction reference, the memory | |
8234 | * operand is read even if it isn't needed (e.g., for type==global) | |
8235 | */ | |
8236 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 8237 | vmx_instruction_info, false, &gva)) |
bfd0a56b NHE |
8238 | return 1; |
8239 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, | |
8240 | sizeof(operand), &e)) { | |
8241 | kvm_inject_page_fault(vcpu, &e); | |
8242 | return 1; | |
8243 | } | |
8244 | ||
8245 | switch (type) { | |
bfd0a56b | 8246 | case VMX_EPT_EXTENT_GLOBAL: |
45e11817 BD |
8247 | /* |
8248 | * TODO: track mappings and invalidate | |
8249 | * single context requests appropriately | |
8250 | */ | |
8251 | case VMX_EPT_EXTENT_CONTEXT: | |
bfd0a56b | 8252 | kvm_mmu_sync_roots(vcpu); |
77c3913b | 8253 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
bfd0a56b NHE |
8254 | nested_vmx_succeed(vcpu); |
8255 | break; | |
8256 | default: | |
8257 | BUG_ON(1); | |
8258 | break; | |
8259 | } | |
8260 | ||
6affcbed | 8261 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
8262 | } |
8263 | ||
a642fc30 PM |
8264 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
8265 | { | |
99b83ac8 WL |
8266 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
8267 | u32 vmx_instruction_info; | |
8268 | unsigned long type, types; | |
8269 | gva_t gva; | |
8270 | struct x86_exception e; | |
40352605 JM |
8271 | struct { |
8272 | u64 vpid; | |
8273 | u64 gla; | |
8274 | } operand; | |
99b83ac8 | 8275 | |
6677f3da | 8276 | if (!(vmx->nested.msrs.secondary_ctls_high & |
99b83ac8 | 8277 | SECONDARY_EXEC_ENABLE_VPID) || |
6677f3da | 8278 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
99b83ac8 WL |
8279 | kvm_queue_exception(vcpu, UD_VECTOR); |
8280 | return 1; | |
8281 | } | |
8282 | ||
8283 | if (!nested_vmx_check_permission(vcpu)) | |
8284 | return 1; | |
8285 | ||
8286 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
8287 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); | |
8288 | ||
6677f3da | 8289 | types = (vmx->nested.msrs.vpid_caps & |
bcdde302 | 8290 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
99b83ac8 | 8291 | |
85c856b3 | 8292 | if (type >= 32 || !(types & (1 << type))) { |
99b83ac8 WL |
8293 | nested_vmx_failValid(vcpu, |
8294 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 8295 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
8296 | } |
8297 | ||
8298 | /* according to the intel vmx instruction reference, the memory | |
8299 | * operand is read even if it isn't needed (e.g., for type==global) | |
8300 | */ | |
8301 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
8302 | vmx_instruction_info, false, &gva)) | |
8303 | return 1; | |
40352605 JM |
8304 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
8305 | sizeof(operand), &e)) { | |
99b83ac8 WL |
8306 | kvm_inject_page_fault(vcpu, &e); |
8307 | return 1; | |
8308 | } | |
40352605 JM |
8309 | if (operand.vpid >> 16) { |
8310 | nested_vmx_failValid(vcpu, | |
8311 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
8312 | return kvm_skip_emulated_instruction(vcpu); | |
8313 | } | |
99b83ac8 WL |
8314 | |
8315 | switch (type) { | |
bcdde302 | 8316 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
fd8cb433 | 8317 | if (is_noncanonical_address(operand.gla, vcpu)) { |
40352605 JM |
8318 | nested_vmx_failValid(vcpu, |
8319 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
8320 | return kvm_skip_emulated_instruction(vcpu); | |
8321 | } | |
8322 | /* fall through */ | |
ef697a71 | 8323 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
bcdde302 | 8324 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
40352605 | 8325 | if (!operand.vpid) { |
bcdde302 JD |
8326 | nested_vmx_failValid(vcpu, |
8327 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 8328 | return kvm_skip_emulated_instruction(vcpu); |
bcdde302 JD |
8329 | } |
8330 | break; | |
99b83ac8 | 8331 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
99b83ac8 WL |
8332 | break; |
8333 | default: | |
bcdde302 | 8334 | WARN_ON_ONCE(1); |
6affcbed | 8335 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
8336 | } |
8337 | ||
c2ba05cc | 8338 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
bcdde302 JD |
8339 | nested_vmx_succeed(vcpu); |
8340 | ||
6affcbed | 8341 | return kvm_skip_emulated_instruction(vcpu); |
a642fc30 PM |
8342 | } |
8343 | ||
843e4330 KH |
8344 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
8345 | { | |
8346 | unsigned long exit_qualification; | |
8347 | ||
8348 | trace_kvm_pml_full(vcpu->vcpu_id); | |
8349 | ||
8350 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8351 | ||
8352 | /* | |
8353 | * PML buffer FULL happened while executing iret from NMI, | |
8354 | * "blocked by NMI" bit has to be set before next VM entry. | |
8355 | */ | |
8356 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && | |
d02fcf50 | 8357 | enable_vnmi && |
843e4330 KH |
8358 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
8359 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
8360 | GUEST_INTR_STATE_NMI); | |
8361 | ||
8362 | /* | |
8363 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do | |
8364 | * here.., and there's no userspace involvement needed for PML. | |
8365 | */ | |
8366 | return 1; | |
8367 | } | |
8368 | ||
64672c95 YJ |
8369 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
8370 | { | |
8371 | kvm_lapic_expired_hv_timer(vcpu); | |
8372 | return 1; | |
8373 | } | |
8374 | ||
41ab9372 BD |
8375 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
8376 | { | |
8377 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
41ab9372 BD |
8378 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
8379 | ||
8380 | /* Check for memory type validity */ | |
bb97a016 DH |
8381 | switch (address & VMX_EPTP_MT_MASK) { |
8382 | case VMX_EPTP_MT_UC: | |
6677f3da | 8383 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)) |
41ab9372 BD |
8384 | return false; |
8385 | break; | |
bb97a016 | 8386 | case VMX_EPTP_MT_WB: |
6677f3da | 8387 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)) |
41ab9372 BD |
8388 | return false; |
8389 | break; | |
8390 | default: | |
8391 | return false; | |
8392 | } | |
8393 | ||
bb97a016 DH |
8394 | /* only 4 levels page-walk length are valid */ |
8395 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) | |
41ab9372 BD |
8396 | return false; |
8397 | ||
8398 | /* Reserved bits should not be set */ | |
8399 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) | |
8400 | return false; | |
8401 | ||
8402 | /* AD, if set, should be supported */ | |
bb97a016 | 8403 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
6677f3da | 8404 | if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)) |
41ab9372 BD |
8405 | return false; |
8406 | } | |
8407 | ||
8408 | return true; | |
8409 | } | |
8410 | ||
8411 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, | |
8412 | struct vmcs12 *vmcs12) | |
8413 | { | |
8414 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; | |
8415 | u64 address; | |
8416 | bool accessed_dirty; | |
8417 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; | |
8418 | ||
8419 | if (!nested_cpu_has_eptp_switching(vmcs12) || | |
8420 | !nested_cpu_has_ept(vmcs12)) | |
8421 | return 1; | |
8422 | ||
8423 | if (index >= VMFUNC_EPTP_ENTRIES) | |
8424 | return 1; | |
8425 | ||
8426 | ||
8427 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, | |
8428 | &address, index * 8, 8)) | |
8429 | return 1; | |
8430 | ||
bb97a016 | 8431 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
41ab9372 BD |
8432 | |
8433 | /* | |
8434 | * If the (L2) guest does a vmfunc to the currently | |
8435 | * active ept pointer, we don't have to do anything else | |
8436 | */ | |
8437 | if (vmcs12->ept_pointer != address) { | |
8438 | if (!valid_ept_address(vcpu, address)) | |
8439 | return 1; | |
8440 | ||
8441 | kvm_mmu_unload(vcpu); | |
8442 | mmu->ept_ad = accessed_dirty; | |
8443 | mmu->base_role.ad_disabled = !accessed_dirty; | |
8444 | vmcs12->ept_pointer = address; | |
8445 | /* | |
8446 | * TODO: Check what's the correct approach in case | |
8447 | * mmu reload fails. Currently, we just let the next | |
8448 | * reload potentially fail | |
8449 | */ | |
8450 | kvm_mmu_reload(vcpu); | |
8451 | } | |
8452 | ||
8453 | return 0; | |
8454 | } | |
8455 | ||
2a499e49 BD |
8456 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
8457 | { | |
27c42a1b BD |
8458 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
8459 | struct vmcs12 *vmcs12; | |
8460 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; | |
8461 | ||
8462 | /* | |
8463 | * VMFUNC is only supported for nested guests, but we always enable the | |
8464 | * secondary control for simplicity; for non-nested mode, fake that we | |
8465 | * didn't by injecting #UD. | |
8466 | */ | |
8467 | if (!is_guest_mode(vcpu)) { | |
8468 | kvm_queue_exception(vcpu, UD_VECTOR); | |
8469 | return 1; | |
8470 | } | |
8471 | ||
8472 | vmcs12 = get_vmcs12(vcpu); | |
8473 | if ((vmcs12->vm_function_control & (1 << function)) == 0) | |
8474 | goto fail; | |
41ab9372 BD |
8475 | |
8476 | switch (function) { | |
8477 | case 0: | |
8478 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) | |
8479 | goto fail; | |
8480 | break; | |
8481 | default: | |
8482 | goto fail; | |
8483 | } | |
8484 | return kvm_skip_emulated_instruction(vcpu); | |
27c42a1b BD |
8485 | |
8486 | fail: | |
8487 | nested_vmx_vmexit(vcpu, vmx->exit_reason, | |
8488 | vmcs_read32(VM_EXIT_INTR_INFO), | |
8489 | vmcs_readl(EXIT_QUALIFICATION)); | |
2a499e49 BD |
8490 | return 1; |
8491 | } | |
8492 | ||
6aa8b732 AK |
8493 | /* |
8494 | * The exit handlers return 1 if the exit was handled fully and guest execution | |
8495 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs | |
8496 | * to be done to userspace and return 0. | |
8497 | */ | |
772e0318 | 8498 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
6aa8b732 AK |
8499 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
8500 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, | |
988ad74f | 8501 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
f08864b4 | 8502 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
6aa8b732 | 8503 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
6aa8b732 AK |
8504 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
8505 | [EXIT_REASON_DR_ACCESS] = handle_dr, | |
8506 | [EXIT_REASON_CPUID] = handle_cpuid, | |
8507 | [EXIT_REASON_MSR_READ] = handle_rdmsr, | |
8508 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, | |
8509 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, | |
8510 | [EXIT_REASON_HLT] = handle_halt, | |
ec25d5e6 | 8511 | [EXIT_REASON_INVD] = handle_invd, |
a7052897 | 8512 | [EXIT_REASON_INVLPG] = handle_invlpg, |
fee84b07 | 8513 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
c21415e8 | 8514 | [EXIT_REASON_VMCALL] = handle_vmcall, |
27d6c865 | 8515 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
cd232ad0 | 8516 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
63846663 | 8517 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
6a4d7550 | 8518 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
49f705c5 | 8519 | [EXIT_REASON_VMREAD] = handle_vmread, |
cd232ad0 | 8520 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
49f705c5 | 8521 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
ec378aee NHE |
8522 | [EXIT_REASON_VMOFF] = handle_vmoff, |
8523 | [EXIT_REASON_VMON] = handle_vmon, | |
f78e0e2e SY |
8524 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
8525 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, | |
83d4c286 | 8526 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
c7c9c56c | 8527 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
e5edaa01 | 8528 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
2acf923e | 8529 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
37817f29 | 8530 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
a0861c02 | 8531 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
0367f205 PB |
8532 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
8533 | [EXIT_REASON_LDTR_TR] = handle_desc, | |
68f89400 MT |
8534 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
8535 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, | |
4b8d54f9 | 8536 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
87c00572 | 8537 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
5f3d45e7 | 8538 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
87c00572 | 8539 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
bfd0a56b | 8540 | [EXIT_REASON_INVEPT] = handle_invept, |
a642fc30 | 8541 | [EXIT_REASON_INVVPID] = handle_invvpid, |
45ec368c | 8542 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
75f4fc8d | 8543 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
f53cd63c WL |
8544 | [EXIT_REASON_XSAVES] = handle_xsaves, |
8545 | [EXIT_REASON_XRSTORS] = handle_xrstors, | |
843e4330 | 8546 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
2a499e49 | 8547 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
64672c95 | 8548 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
6aa8b732 AK |
8549 | }; |
8550 | ||
8551 | static const int kvm_vmx_max_exit_handlers = | |
50a3485c | 8552 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
6aa8b732 | 8553 | |
908a7bdd JK |
8554 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
8555 | struct vmcs12 *vmcs12) | |
8556 | { | |
8557 | unsigned long exit_qualification; | |
8558 | gpa_t bitmap, last_bitmap; | |
8559 | unsigned int port; | |
8560 | int size; | |
8561 | u8 b; | |
8562 | ||
908a7bdd | 8563 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
2f0a6397 | 8564 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
908a7bdd JK |
8565 | |
8566 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8567 | ||
8568 | port = exit_qualification >> 16; | |
8569 | size = (exit_qualification & 7) + 1; | |
8570 | ||
8571 | last_bitmap = (gpa_t)-1; | |
8572 | b = -1; | |
8573 | ||
8574 | while (size > 0) { | |
8575 | if (port < 0x8000) | |
8576 | bitmap = vmcs12->io_bitmap_a; | |
8577 | else if (port < 0x10000) | |
8578 | bitmap = vmcs12->io_bitmap_b; | |
8579 | else | |
1d804d07 | 8580 | return true; |
908a7bdd JK |
8581 | bitmap += (port & 0x7fff) / 8; |
8582 | ||
8583 | if (last_bitmap != bitmap) | |
54bf36aa | 8584 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
1d804d07 | 8585 | return true; |
908a7bdd | 8586 | if (b & (1 << (port & 7))) |
1d804d07 | 8587 | return true; |
908a7bdd JK |
8588 | |
8589 | port++; | |
8590 | size--; | |
8591 | last_bitmap = bitmap; | |
8592 | } | |
8593 | ||
1d804d07 | 8594 | return false; |
908a7bdd JK |
8595 | } |
8596 | ||
644d711a NHE |
8597 | /* |
8598 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, | |
8599 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed | |
8600 | * disinterest in the current event (read or write a specific MSR) by using an | |
8601 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. | |
8602 | */ | |
8603 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, | |
8604 | struct vmcs12 *vmcs12, u32 exit_reason) | |
8605 | { | |
8606 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; | |
8607 | gpa_t bitmap; | |
8608 | ||
cbd29cb6 | 8609 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
1d804d07 | 8610 | return true; |
644d711a NHE |
8611 | |
8612 | /* | |
8613 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, | |
8614 | * for the four combinations of read/write and low/high MSR numbers. | |
8615 | * First we need to figure out which of the four to use: | |
8616 | */ | |
8617 | bitmap = vmcs12->msr_bitmap; | |
8618 | if (exit_reason == EXIT_REASON_MSR_WRITE) | |
8619 | bitmap += 2048; | |
8620 | if (msr_index >= 0xc0000000) { | |
8621 | msr_index -= 0xc0000000; | |
8622 | bitmap += 1024; | |
8623 | } | |
8624 | ||
8625 | /* Then read the msr_index'th bit from this bitmap: */ | |
8626 | if (msr_index < 1024*8) { | |
8627 | unsigned char b; | |
54bf36aa | 8628 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
1d804d07 | 8629 | return true; |
644d711a NHE |
8630 | return 1 & (b >> (msr_index & 7)); |
8631 | } else | |
1d804d07 | 8632 | return true; /* let L1 handle the wrong parameter */ |
644d711a NHE |
8633 | } |
8634 | ||
8635 | /* | |
8636 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, | |
8637 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to | |
8638 | * intercept (via guest_host_mask etc.) the current event. | |
8639 | */ | |
8640 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, | |
8641 | struct vmcs12 *vmcs12) | |
8642 | { | |
8643 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8644 | int cr = exit_qualification & 15; | |
e1d39b17 JS |
8645 | int reg; |
8646 | unsigned long val; | |
644d711a NHE |
8647 | |
8648 | switch ((exit_qualification >> 4) & 3) { | |
8649 | case 0: /* mov to cr */ | |
e1d39b17 JS |
8650 | reg = (exit_qualification >> 8) & 15; |
8651 | val = kvm_register_readl(vcpu, reg); | |
644d711a NHE |
8652 | switch (cr) { |
8653 | case 0: | |
8654 | if (vmcs12->cr0_guest_host_mask & | |
8655 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 8656 | return true; |
644d711a NHE |
8657 | break; |
8658 | case 3: | |
8659 | if ((vmcs12->cr3_target_count >= 1 && | |
8660 | vmcs12->cr3_target_value0 == val) || | |
8661 | (vmcs12->cr3_target_count >= 2 && | |
8662 | vmcs12->cr3_target_value1 == val) || | |
8663 | (vmcs12->cr3_target_count >= 3 && | |
8664 | vmcs12->cr3_target_value2 == val) || | |
8665 | (vmcs12->cr3_target_count >= 4 && | |
8666 | vmcs12->cr3_target_value3 == val)) | |
1d804d07 | 8667 | return false; |
644d711a | 8668 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
1d804d07 | 8669 | return true; |
644d711a NHE |
8670 | break; |
8671 | case 4: | |
8672 | if (vmcs12->cr4_guest_host_mask & | |
8673 | (vmcs12->cr4_read_shadow ^ val)) | |
1d804d07 | 8674 | return true; |
644d711a NHE |
8675 | break; |
8676 | case 8: | |
8677 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) | |
1d804d07 | 8678 | return true; |
644d711a NHE |
8679 | break; |
8680 | } | |
8681 | break; | |
8682 | case 2: /* clts */ | |
8683 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && | |
8684 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) | |
1d804d07 | 8685 | return true; |
644d711a NHE |
8686 | break; |
8687 | case 1: /* mov from cr */ | |
8688 | switch (cr) { | |
8689 | case 3: | |
8690 | if (vmcs12->cpu_based_vm_exec_control & | |
8691 | CPU_BASED_CR3_STORE_EXITING) | |
1d804d07 | 8692 | return true; |
644d711a NHE |
8693 | break; |
8694 | case 8: | |
8695 | if (vmcs12->cpu_based_vm_exec_control & | |
8696 | CPU_BASED_CR8_STORE_EXITING) | |
1d804d07 | 8697 | return true; |
644d711a NHE |
8698 | break; |
8699 | } | |
8700 | break; | |
8701 | case 3: /* lmsw */ | |
8702 | /* | |
8703 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of | |
8704 | * cr0. Other attempted changes are ignored, with no exit. | |
8705 | */ | |
e1d39b17 | 8706 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
644d711a NHE |
8707 | if (vmcs12->cr0_guest_host_mask & 0xe & |
8708 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 8709 | return true; |
644d711a NHE |
8710 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
8711 | !(vmcs12->cr0_read_shadow & 0x1) && | |
8712 | (val & 0x1)) | |
1d804d07 | 8713 | return true; |
644d711a NHE |
8714 | break; |
8715 | } | |
1d804d07 | 8716 | return false; |
644d711a NHE |
8717 | } |
8718 | ||
8719 | /* | |
8720 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we | |
8721 | * should handle it ourselves in L0 (and then continue L2). Only call this | |
8722 | * when in is_guest_mode (L2). | |
8723 | */ | |
7313c698 | 8724 | static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason) |
644d711a | 8725 | { |
644d711a NHE |
8726 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
8727 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8728 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
8729 | ||
4f350c6d JM |
8730 | if (vmx->nested.nested_run_pending) |
8731 | return false; | |
8732 | ||
8733 | if (unlikely(vmx->fail)) { | |
8734 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, | |
8735 | vmcs_read32(VM_INSTRUCTION_ERROR)); | |
8736 | return true; | |
8737 | } | |
542060ea | 8738 | |
c9f04407 DM |
8739 | /* |
8740 | * The host physical addresses of some pages of guest memory | |
de3a0021 JM |
8741 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
8742 | * Page). The CPU may write to these pages via their host | |
8743 | * physical address while L2 is running, bypassing any | |
8744 | * address-translation-based dirty tracking (e.g. EPT write | |
8745 | * protection). | |
c9f04407 DM |
8746 | * |
8747 | * Mark them dirty on every exit from L2 to prevent them from | |
8748 | * getting out of sync with dirty tracking. | |
8749 | */ | |
8750 | nested_mark_vmcs12_pages_dirty(vcpu); | |
8751 | ||
4f350c6d JM |
8752 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
8753 | vmcs_readl(EXIT_QUALIFICATION), | |
8754 | vmx->idt_vectoring_info, | |
8755 | intr_info, | |
8756 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
8757 | KVM_ISA_VMX); | |
644d711a NHE |
8758 | |
8759 | switch (exit_reason) { | |
8760 | case EXIT_REASON_EXCEPTION_NMI: | |
ef85b673 | 8761 | if (is_nmi(intr_info)) |
1d804d07 | 8762 | return false; |
644d711a | 8763 | else if (is_page_fault(intr_info)) |
52a5c155 | 8764 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
e504c909 | 8765 | else if (is_no_device(intr_info) && |
ccf9844e | 8766 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
1d804d07 | 8767 | return false; |
6f05485d JK |
8768 | else if (is_debug(intr_info) && |
8769 | vcpu->guest_debug & | |
8770 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
8771 | return false; | |
8772 | else if (is_breakpoint(intr_info) && | |
8773 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) | |
8774 | return false; | |
644d711a NHE |
8775 | return vmcs12->exception_bitmap & |
8776 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); | |
8777 | case EXIT_REASON_EXTERNAL_INTERRUPT: | |
1d804d07 | 8778 | return false; |
644d711a | 8779 | case EXIT_REASON_TRIPLE_FAULT: |
1d804d07 | 8780 | return true; |
644d711a | 8781 | case EXIT_REASON_PENDING_INTERRUPT: |
3b656cf7 | 8782 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
644d711a | 8783 | case EXIT_REASON_NMI_WINDOW: |
3b656cf7 | 8784 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
644d711a | 8785 | case EXIT_REASON_TASK_SWITCH: |
1d804d07 | 8786 | return true; |
644d711a | 8787 | case EXIT_REASON_CPUID: |
1d804d07 | 8788 | return true; |
644d711a NHE |
8789 | case EXIT_REASON_HLT: |
8790 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); | |
8791 | case EXIT_REASON_INVD: | |
1d804d07 | 8792 | return true; |
644d711a NHE |
8793 | case EXIT_REASON_INVLPG: |
8794 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); | |
8795 | case EXIT_REASON_RDPMC: | |
8796 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); | |
a5f46457 | 8797 | case EXIT_REASON_RDRAND: |
736fdf72 | 8798 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
a5f46457 | 8799 | case EXIT_REASON_RDSEED: |
736fdf72 | 8800 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
b3a2a907 | 8801 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
644d711a NHE |
8802 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
8803 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: | |
8804 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: | |
8805 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: | |
8806 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: | |
8807 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: | |
a642fc30 | 8808 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
644d711a NHE |
8809 | /* |
8810 | * VMX instructions trap unconditionally. This allows L1 to | |
8811 | * emulate them for its L2 guest, i.e., allows 3-level nesting! | |
8812 | */ | |
1d804d07 | 8813 | return true; |
644d711a NHE |
8814 | case EXIT_REASON_CR_ACCESS: |
8815 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); | |
8816 | case EXIT_REASON_DR_ACCESS: | |
8817 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); | |
8818 | case EXIT_REASON_IO_INSTRUCTION: | |
908a7bdd | 8819 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
1b07304c PB |
8820 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
8821 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); | |
644d711a NHE |
8822 | case EXIT_REASON_MSR_READ: |
8823 | case EXIT_REASON_MSR_WRITE: | |
8824 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); | |
8825 | case EXIT_REASON_INVALID_STATE: | |
1d804d07 | 8826 | return true; |
644d711a NHE |
8827 | case EXIT_REASON_MWAIT_INSTRUCTION: |
8828 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); | |
5f3d45e7 MD |
8829 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
8830 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); | |
644d711a NHE |
8831 | case EXIT_REASON_MONITOR_INSTRUCTION: |
8832 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); | |
8833 | case EXIT_REASON_PAUSE_INSTRUCTION: | |
8834 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || | |
8835 | nested_cpu_has2(vmcs12, | |
8836 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); | |
8837 | case EXIT_REASON_MCE_DURING_VMENTRY: | |
1d804d07 | 8838 | return false; |
644d711a | 8839 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
a7c0b07d | 8840 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
644d711a NHE |
8841 | case EXIT_REASON_APIC_ACCESS: |
8842 | return nested_cpu_has2(vmcs12, | |
8843 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
82f0dd4b | 8844 | case EXIT_REASON_APIC_WRITE: |
608406e2 WV |
8845 | case EXIT_REASON_EOI_INDUCED: |
8846 | /* apic_write and eoi_induced should exit unconditionally. */ | |
1d804d07 | 8847 | return true; |
644d711a | 8848 | case EXIT_REASON_EPT_VIOLATION: |
2b1be677 NHE |
8849 | /* |
8850 | * L0 always deals with the EPT violation. If nested EPT is | |
8851 | * used, and the nested mmu code discovers that the address is | |
8852 | * missing in the guest EPT table (EPT12), the EPT violation | |
8853 | * will be injected with nested_ept_inject_page_fault() | |
8854 | */ | |
1d804d07 | 8855 | return false; |
644d711a | 8856 | case EXIT_REASON_EPT_MISCONFIG: |
2b1be677 NHE |
8857 | /* |
8858 | * L2 never uses directly L1's EPT, but rather L0's own EPT | |
8859 | * table (shadow on EPT) or a merged EPT table that L0 built | |
8860 | * (EPT on EPT). So any problems with the structure of the | |
8861 | * table is L0's fault. | |
8862 | */ | |
1d804d07 | 8863 | return false; |
90a2db6d PB |
8864 | case EXIT_REASON_INVPCID: |
8865 | return | |
8866 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && | |
8867 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); | |
644d711a NHE |
8868 | case EXIT_REASON_WBINVD: |
8869 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); | |
8870 | case EXIT_REASON_XSETBV: | |
1d804d07 | 8871 | return true; |
81dc01f7 WL |
8872 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
8873 | /* | |
8874 | * This should never happen, since it is not possible to | |
8875 | * set XSS to a non-zero value---neither in L1 nor in L2. | |
8876 | * If if it were, XSS would have to be checked against | |
8877 | * the XSS exit bitmap in vmcs12. | |
8878 | */ | |
8879 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); | |
55123e3c WL |
8880 | case EXIT_REASON_PREEMPTION_TIMER: |
8881 | return false; | |
ab007cc9 | 8882 | case EXIT_REASON_PML_FULL: |
03efce6f | 8883 | /* We emulate PML support to L1. */ |
ab007cc9 | 8884 | return false; |
2a499e49 BD |
8885 | case EXIT_REASON_VMFUNC: |
8886 | /* VM functions are emulated through L2->L0 vmexits. */ | |
8887 | return false; | |
644d711a | 8888 | default: |
1d804d07 | 8889 | return true; |
644d711a NHE |
8890 | } |
8891 | } | |
8892 | ||
7313c698 PB |
8893 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
8894 | { | |
8895 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
8896 | ||
8897 | /* | |
8898 | * At this point, the exit interruption info in exit_intr_info | |
8899 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT | |
8900 | * we need to query the in-kernel LAPIC. | |
8901 | */ | |
8902 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); | |
8903 | if ((exit_intr_info & | |
8904 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == | |
8905 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { | |
8906 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
8907 | vmcs12->vm_exit_intr_error_code = | |
8908 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); | |
8909 | } | |
8910 | ||
8911 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, | |
8912 | vmcs_readl(EXIT_QUALIFICATION)); | |
8913 | return 1; | |
8914 | } | |
8915 | ||
586f9607 AK |
8916 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
8917 | { | |
8918 | *info1 = vmcs_readl(EXIT_QUALIFICATION); | |
8919 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); | |
8920 | } | |
8921 | ||
a3eaa864 | 8922 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
843e4330 | 8923 | { |
a3eaa864 KH |
8924 | if (vmx->pml_pg) { |
8925 | __free_page(vmx->pml_pg); | |
8926 | vmx->pml_pg = NULL; | |
8927 | } | |
843e4330 KH |
8928 | } |
8929 | ||
54bf36aa | 8930 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
843e4330 | 8931 | { |
54bf36aa | 8932 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
843e4330 KH |
8933 | u64 *pml_buf; |
8934 | u16 pml_idx; | |
8935 | ||
8936 | pml_idx = vmcs_read16(GUEST_PML_INDEX); | |
8937 | ||
8938 | /* Do nothing if PML buffer is empty */ | |
8939 | if (pml_idx == (PML_ENTITY_NUM - 1)) | |
8940 | return; | |
8941 | ||
8942 | /* PML index always points to next available PML buffer entity */ | |
8943 | if (pml_idx >= PML_ENTITY_NUM) | |
8944 | pml_idx = 0; | |
8945 | else | |
8946 | pml_idx++; | |
8947 | ||
8948 | pml_buf = page_address(vmx->pml_pg); | |
8949 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { | |
8950 | u64 gpa; | |
8951 | ||
8952 | gpa = pml_buf[pml_idx]; | |
8953 | WARN_ON(gpa & (PAGE_SIZE - 1)); | |
54bf36aa | 8954 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
843e4330 KH |
8955 | } |
8956 | ||
8957 | /* reset PML index */ | |
8958 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
8959 | } | |
8960 | ||
8961 | /* | |
8962 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. | |
8963 | * Called before reporting dirty_bitmap to userspace. | |
8964 | */ | |
8965 | static void kvm_flush_pml_buffers(struct kvm *kvm) | |
8966 | { | |
8967 | int i; | |
8968 | struct kvm_vcpu *vcpu; | |
8969 | /* | |
8970 | * We only need to kick vcpu out of guest mode here, as PML buffer | |
8971 | * is flushed at beginning of all VMEXITs, and it's obvious that only | |
8972 | * vcpus running in guest are possible to have unflushed GPAs in PML | |
8973 | * buffer. | |
8974 | */ | |
8975 | kvm_for_each_vcpu(i, vcpu, kvm) | |
8976 | kvm_vcpu_kick(vcpu); | |
8977 | } | |
8978 | ||
4eb64dce PB |
8979 | static void vmx_dump_sel(char *name, uint32_t sel) |
8980 | { | |
8981 | pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", | |
96794e4e | 8982 | name, vmcs_read16(sel), |
4eb64dce PB |
8983 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
8984 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), | |
8985 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); | |
8986 | } | |
8987 | ||
8988 | static void vmx_dump_dtsel(char *name, uint32_t limit) | |
8989 | { | |
8990 | pr_err("%s limit=0x%08x, base=0x%016lx\n", | |
8991 | name, vmcs_read32(limit), | |
8992 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); | |
8993 | } | |
8994 | ||
8995 | static void dump_vmcs(void) | |
8996 | { | |
8997 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); | |
8998 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); | |
8999 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); | |
9000 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); | |
9001 | u32 secondary_exec_control = 0; | |
9002 | unsigned long cr4 = vmcs_readl(GUEST_CR4); | |
f3531054 | 9003 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
4eb64dce PB |
9004 | int i, n; |
9005 | ||
9006 | if (cpu_has_secondary_exec_ctrls()) | |
9007 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
9008 | ||
9009 | pr_err("*** Guest State ***\n"); | |
9010 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
9011 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), | |
9012 | vmcs_readl(CR0_GUEST_HOST_MASK)); | |
9013 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
9014 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); | |
9015 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); | |
9016 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && | |
9017 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) | |
9018 | { | |
845c5b40 PB |
9019 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
9020 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); | |
9021 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", | |
9022 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); | |
4eb64dce PB |
9023 | } |
9024 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", | |
9025 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); | |
9026 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", | |
9027 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); | |
9028 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
9029 | vmcs_readl(GUEST_SYSENTER_ESP), | |
9030 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); | |
9031 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); | |
9032 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); | |
9033 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); | |
9034 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); | |
9035 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); | |
9036 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); | |
9037 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); | |
9038 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); | |
9039 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); | |
9040 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); | |
9041 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || | |
9042 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) | |
845c5b40 PB |
9043 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
9044 | efer, vmcs_read64(GUEST_IA32_PAT)); | |
9045 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", | |
9046 | vmcs_read64(GUEST_IA32_DEBUGCTL), | |
4eb64dce | 9047 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
773e8a04 VK |
9048 | if (cpu_has_load_perf_global_ctrl && |
9049 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
845c5b40 PB |
9050 | pr_err("PerfGlobCtl = 0x%016llx\n", |
9051 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce | 9052 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
845c5b40 | 9053 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
4eb64dce PB |
9054 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
9055 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), | |
9056 | vmcs_read32(GUEST_ACTIVITY_STATE)); | |
9057 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) | |
9058 | pr_err("InterruptStatus = %04x\n", | |
9059 | vmcs_read16(GUEST_INTR_STATUS)); | |
9060 | ||
9061 | pr_err("*** Host State ***\n"); | |
9062 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", | |
9063 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); | |
9064 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", | |
9065 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), | |
9066 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), | |
9067 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), | |
9068 | vmcs_read16(HOST_TR_SELECTOR)); | |
9069 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", | |
9070 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), | |
9071 | vmcs_readl(HOST_TR_BASE)); | |
9072 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", | |
9073 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); | |
9074 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", | |
9075 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), | |
9076 | vmcs_readl(HOST_CR4)); | |
9077 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
9078 | vmcs_readl(HOST_IA32_SYSENTER_ESP), | |
9079 | vmcs_read32(HOST_IA32_SYSENTER_CS), | |
9080 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); | |
9081 | if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER)) | |
845c5b40 PB |
9082 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
9083 | vmcs_read64(HOST_IA32_EFER), | |
9084 | vmcs_read64(HOST_IA32_PAT)); | |
773e8a04 VK |
9085 | if (cpu_has_load_perf_global_ctrl && |
9086 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) | |
845c5b40 PB |
9087 | pr_err("PerfGlobCtl = 0x%016llx\n", |
9088 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce PB |
9089 | |
9090 | pr_err("*** Control State ***\n"); | |
9091 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", | |
9092 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); | |
9093 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); | |
9094 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", | |
9095 | vmcs_read32(EXCEPTION_BITMAP), | |
9096 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), | |
9097 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); | |
9098 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", | |
9099 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), | |
9100 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), | |
9101 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); | |
9102 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", | |
9103 | vmcs_read32(VM_EXIT_INTR_INFO), | |
9104 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
9105 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); | |
9106 | pr_err(" reason=%08x qualification=%016lx\n", | |
9107 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); | |
9108 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", | |
9109 | vmcs_read32(IDT_VECTORING_INFO_FIELD), | |
9110 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); | |
845c5b40 | 9111 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
8cfe9866 | 9112 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
845c5b40 PB |
9113 | pr_err("TSC Multiplier = 0x%016llx\n", |
9114 | vmcs_read64(TSC_MULTIPLIER)); | |
4eb64dce PB |
9115 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
9116 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); | |
9117 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) | |
9118 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); | |
9119 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) | |
845c5b40 | 9120 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
4eb64dce PB |
9121 | n = vmcs_read32(CR3_TARGET_COUNT); |
9122 | for (i = 0; i + 1 < n; i += 4) | |
9123 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", | |
9124 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), | |
9125 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); | |
9126 | if (i < n) | |
9127 | pr_err("CR3 target%u=%016lx\n", | |
9128 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); | |
9129 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) | |
9130 | pr_err("PLE Gap=%08x Window=%08x\n", | |
9131 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); | |
9132 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) | |
9133 | pr_err("Virtual processor ID = 0x%04x\n", | |
9134 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); | |
9135 | } | |
9136 | ||
6aa8b732 AK |
9137 | /* |
9138 | * The guest has exited. See if we can fix it or if we need userspace | |
9139 | * assistance. | |
9140 | */ | |
851ba692 | 9141 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
6aa8b732 | 9142 | { |
29bd8a78 | 9143 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
a0861c02 | 9144 | u32 exit_reason = vmx->exit_reason; |
1155f76a | 9145 | u32 vectoring_info = vmx->idt_vectoring_info; |
29bd8a78 | 9146 | |
8b89fe1f PB |
9147 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
9148 | ||
843e4330 KH |
9149 | /* |
9150 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more | |
9151 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before | |
9152 | * querying dirty_bitmap, we only need to kick all vcpus out of guest | |
9153 | * mode as if vcpus is in root mode, the PML buffer must has been | |
9154 | * flushed already. | |
9155 | */ | |
9156 | if (enable_pml) | |
54bf36aa | 9157 | vmx_flush_pml_buffer(vcpu); |
843e4330 | 9158 | |
80ced186 | 9159 | /* If guest state is invalid, start emulating */ |
14168786 | 9160 | if (vmx->emulation_required) |
80ced186 | 9161 | return handle_invalid_guest_state(vcpu); |
1d5a4d9b | 9162 | |
7313c698 PB |
9163 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
9164 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); | |
644d711a | 9165 | |
5120702e | 9166 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
4eb64dce | 9167 | dump_vmcs(); |
5120702e MG |
9168 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
9169 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
9170 | = exit_reason; | |
9171 | return 0; | |
9172 | } | |
9173 | ||
29bd8a78 | 9174 | if (unlikely(vmx->fail)) { |
851ba692 AK |
9175 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
9176 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
29bd8a78 AK |
9177 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
9178 | return 0; | |
9179 | } | |
6aa8b732 | 9180 | |
b9bf6882 XG |
9181 | /* |
9182 | * Note: | |
9183 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by | |
9184 | * delivery event since it indicates guest is accessing MMIO. | |
9185 | * The vm-exit can be triggered again after return to guest that | |
9186 | * will cause infinite loop. | |
9187 | */ | |
d77c26fc | 9188 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
1439442c | 9189 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
60637aac | 9190 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
b244c9fc | 9191 | exit_reason != EXIT_REASON_PML_FULL && |
b9bf6882 XG |
9192 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
9193 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
9194 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; | |
70bcd708 | 9195 | vcpu->run->internal.ndata = 3; |
b9bf6882 XG |
9196 | vcpu->run->internal.data[0] = vectoring_info; |
9197 | vcpu->run->internal.data[1] = exit_reason; | |
70bcd708 PB |
9198 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
9199 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { | |
9200 | vcpu->run->internal.ndata++; | |
9201 | vcpu->run->internal.data[3] = | |
9202 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); | |
9203 | } | |
b9bf6882 XG |
9204 | return 0; |
9205 | } | |
3b86cd99 | 9206 | |
d02fcf50 | 9207 | if (unlikely(!enable_vnmi && |
8a1b4392 PB |
9208 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
9209 | if (vmx_interrupt_allowed(vcpu)) { | |
9210 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
9211 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && | |
9212 | vcpu->arch.nmi_pending) { | |
9213 | /* | |
9214 | * This CPU don't support us in finding the end of an | |
9215 | * NMI-blocked window if the guest runs with IRQs | |
9216 | * disabled. So we pull the trigger after 1 s of | |
9217 | * futile waiting, but inform the user about this. | |
9218 | */ | |
9219 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " | |
9220 | "state on VCPU %d after 1 s timeout\n", | |
9221 | __func__, vcpu->vcpu_id); | |
9222 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
9223 | } | |
9224 | } | |
9225 | ||
6aa8b732 AK |
9226 | if (exit_reason < kvm_vmx_max_exit_handlers |
9227 | && kvm_vmx_exit_handlers[exit_reason]) | |
851ba692 | 9228 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
6aa8b732 | 9229 | else { |
6c6c5e03 RK |
9230 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
9231 | exit_reason); | |
2bc19dc3 MT |
9232 | kvm_queue_exception(vcpu, UD_VECTOR); |
9233 | return 1; | |
6aa8b732 | 9234 | } |
6aa8b732 AK |
9235 | } |
9236 | ||
95ba8273 | 9237 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
6e5d865c | 9238 | { |
a7c0b07d WL |
9239 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
9240 | ||
9241 | if (is_guest_mode(vcpu) && | |
9242 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
9243 | return; | |
9244 | ||
95ba8273 | 9245 | if (irr == -1 || tpr < irr) { |
6e5d865c YS |
9246 | vmcs_write32(TPR_THRESHOLD, 0); |
9247 | return; | |
9248 | } | |
9249 | ||
95ba8273 | 9250 | vmcs_write32(TPR_THRESHOLD, irr); |
6e5d865c YS |
9251 | } |
9252 | ||
8d14695f YZ |
9253 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
9254 | { | |
9255 | u32 sec_exec_control; | |
9256 | ||
dccbfcf5 RK |
9257 | /* Postpone execution until vmcs01 is the current VMCS. */ |
9258 | if (is_guest_mode(vcpu)) { | |
9259 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; | |
9260 | return; | |
9261 | } | |
9262 | ||
f6e90f9e | 9263 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
8d14695f YZ |
9264 | return; |
9265 | ||
35754c98 | 9266 | if (!cpu_need_tpr_shadow(vcpu)) |
8d14695f YZ |
9267 | return; |
9268 | ||
9269 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
9270 | ||
9271 | if (set) { | |
9272 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
9273 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
9274 | } else { | |
9275 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
9276 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
a468f2db | 9277 | vmx_flush_tlb(vcpu, true); |
8d14695f YZ |
9278 | } |
9279 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); | |
9280 | ||
904e14fb | 9281 | vmx_update_msr_bitmap(vcpu); |
8d14695f YZ |
9282 | } |
9283 | ||
38b99173 TC |
9284 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
9285 | { | |
9286 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9287 | ||
9288 | /* | |
9289 | * Currently we do not handle the nested case where L2 has an | |
9290 | * APIC access page of its own; that page is still pinned. | |
9291 | * Hence, we skip the case where the VCPU is in guest mode _and_ | |
9292 | * L1 prepared an APIC access page for L2. | |
9293 | * | |
9294 | * For the case where L1 and L2 share the same APIC access page | |
9295 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear | |
9296 | * in the vmcs12), this function will only update either the vmcs01 | |
9297 | * or the vmcs02. If the former, the vmcs02 will be updated by | |
9298 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in | |
9299 | * the next L2->L1 exit. | |
9300 | */ | |
9301 | if (!is_guest_mode(vcpu) || | |
4f2777bc | 9302 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
fb6c8198 | 9303 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
38b99173 | 9304 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
a468f2db | 9305 | vmx_flush_tlb(vcpu, true); |
fb6c8198 | 9306 | } |
38b99173 TC |
9307 | } |
9308 | ||
67c9dddc | 9309 | static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr) |
c7c9c56c YZ |
9310 | { |
9311 | u16 status; | |
9312 | u8 old; | |
9313 | ||
67c9dddc PB |
9314 | if (max_isr == -1) |
9315 | max_isr = 0; | |
c7c9c56c YZ |
9316 | |
9317 | status = vmcs_read16(GUEST_INTR_STATUS); | |
9318 | old = status >> 8; | |
67c9dddc | 9319 | if (max_isr != old) { |
c7c9c56c | 9320 | status &= 0xff; |
67c9dddc | 9321 | status |= max_isr << 8; |
c7c9c56c YZ |
9322 | vmcs_write16(GUEST_INTR_STATUS, status); |
9323 | } | |
9324 | } | |
9325 | ||
9326 | static void vmx_set_rvi(int vector) | |
9327 | { | |
9328 | u16 status; | |
9329 | u8 old; | |
9330 | ||
4114c27d WW |
9331 | if (vector == -1) |
9332 | vector = 0; | |
9333 | ||
c7c9c56c YZ |
9334 | status = vmcs_read16(GUEST_INTR_STATUS); |
9335 | old = (u8)status & 0xff; | |
9336 | if ((u8)vector != old) { | |
9337 | status &= ~0xff; | |
9338 | status |= (u8)vector; | |
9339 | vmcs_write16(GUEST_INTR_STATUS, status); | |
9340 | } | |
9341 | } | |
9342 | ||
9343 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) | |
9344 | { | |
963fee16 | 9345 | /* |
851c1a18 LA |
9346 | * When running L2, updating RVI is only relevant when |
9347 | * vmcs12 virtual-interrupt-delivery enabled. | |
9348 | * However, it can be enabled only when L1 also | |
9349 | * intercepts external-interrupts and in that case | |
9350 | * we should not update vmcs02 RVI but instead intercept | |
9351 | * interrupt. Therefore, do nothing when running L2. | |
963fee16 | 9352 | */ |
851c1a18 LA |
9353 | if (!is_guest_mode(vcpu)) |
9354 | vmx_set_rvi(max_irr); | |
c7c9c56c YZ |
9355 | } |
9356 | ||
76dfafd5 | 9357 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
810e6def PB |
9358 | { |
9359 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
76dfafd5 | 9360 | int max_irr; |
f27a85c4 | 9361 | bool max_irr_updated; |
810e6def | 9362 | |
76dfafd5 PB |
9363 | WARN_ON(!vcpu->arch.apicv_active); |
9364 | if (pi_test_on(&vmx->pi_desc)) { | |
9365 | pi_clear_on(&vmx->pi_desc); | |
9366 | /* | |
9367 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. | |
9368 | * But on x86 this is just a compiler barrier anyway. | |
9369 | */ | |
9370 | smp_mb__after_atomic(); | |
f27a85c4 LA |
9371 | max_irr_updated = |
9372 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); | |
9373 | ||
9374 | /* | |
9375 | * If we are running L2 and L1 has a new pending interrupt | |
9376 | * which can be injected, we should re-evaluate | |
9377 | * what should be done with this new L1 interrupt. | |
851c1a18 LA |
9378 | * If L1 intercepts external-interrupts, we should |
9379 | * exit from L2 to L1. Otherwise, interrupt should be | |
9380 | * delivered directly to L2. | |
f27a85c4 | 9381 | */ |
851c1a18 LA |
9382 | if (is_guest_mode(vcpu) && max_irr_updated) { |
9383 | if (nested_exit_on_intr(vcpu)) | |
9384 | kvm_vcpu_exiting_guest_mode(vcpu); | |
9385 | else | |
9386 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
9387 | } | |
76dfafd5 PB |
9388 | } else { |
9389 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
9390 | } | |
9391 | vmx_hwapic_irr_update(vcpu, max_irr); | |
9392 | return max_irr; | |
810e6def PB |
9393 | } |
9394 | ||
6308630b | 9395 | static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) |
c7c9c56c | 9396 | { |
d62caabb | 9397 | if (!kvm_vcpu_apicv_active(vcpu)) |
3d81bc7e YZ |
9398 | return; |
9399 | ||
c7c9c56c YZ |
9400 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
9401 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); | |
9402 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); | |
9403 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); | |
9404 | } | |
9405 | ||
967235d3 PB |
9406 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
9407 | { | |
9408 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9409 | ||
9410 | pi_clear_on(&vmx->pi_desc); | |
9411 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); | |
9412 | } | |
9413 | ||
51aa01d1 | 9414 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
cf393f75 | 9415 | { |
48ae0fb4 JM |
9416 | u32 exit_intr_info = 0; |
9417 | u16 basic_exit_reason = (u16)vmx->exit_reason; | |
00eba012 | 9418 | |
48ae0fb4 JM |
9419 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
9420 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) | |
00eba012 AK |
9421 | return; |
9422 | ||
48ae0fb4 JM |
9423 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
9424 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
9425 | vmx->exit_intr_info = exit_intr_info; | |
a0861c02 | 9426 | |
1261bfa3 WL |
9427 | /* if exit due to PF check for async PF */ |
9428 | if (is_page_fault(exit_intr_info)) | |
9429 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); | |
9430 | ||
a0861c02 | 9431 | /* Handle machine checks before interrupts are enabled */ |
48ae0fb4 JM |
9432 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
9433 | is_machine_check(exit_intr_info)) | |
a0861c02 AK |
9434 | kvm_machine_check(); |
9435 | ||
20f65983 | 9436 | /* We need to handle NMIs before interrupts are enabled */ |
ef85b673 | 9437 | if (is_nmi(exit_intr_info)) { |
dd60d217 | 9438 | kvm_before_interrupt(&vmx->vcpu); |
20f65983 | 9439 | asm("int $2"); |
dd60d217 | 9440 | kvm_after_interrupt(&vmx->vcpu); |
ff9d07a0 | 9441 | } |
51aa01d1 | 9442 | } |
20f65983 | 9443 | |
a547c6db YZ |
9444 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
9445 | { | |
9446 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
9447 | ||
a547c6db YZ |
9448 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
9449 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { | |
9450 | unsigned int vector; | |
9451 | unsigned long entry; | |
9452 | gate_desc *desc; | |
9453 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9454 | #ifdef CONFIG_X86_64 | |
9455 | unsigned long tmp; | |
9456 | #endif | |
9457 | ||
9458 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
9459 | desc = (gate_desc *)vmx->host_idt_base + vector; | |
64b163fa | 9460 | entry = gate_offset(desc); |
a547c6db YZ |
9461 | asm volatile( |
9462 | #ifdef CONFIG_X86_64 | |
9463 | "mov %%" _ASM_SP ", %[sp]\n\t" | |
9464 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" | |
9465 | "push $%c[ss]\n\t" | |
9466 | "push %[sp]\n\t" | |
9467 | #endif | |
9468 | "pushf\n\t" | |
a547c6db | 9469 | __ASM_SIZE(push) " $%c[cs]\n\t" |
c940a3fb | 9470 | CALL_NOSPEC |
a547c6db YZ |
9471 | : |
9472 | #ifdef CONFIG_X86_64 | |
3f62de5f | 9473 | [sp]"=&r"(tmp), |
a547c6db | 9474 | #endif |
f5caf621 | 9475 | ASM_CALL_CONSTRAINT |
a547c6db | 9476 | : |
c940a3fb | 9477 | THUNK_TARGET(entry), |
a547c6db YZ |
9478 | [ss]"i"(__KERNEL_DS), |
9479 | [cs]"i"(__KERNEL_CS) | |
9480 | ); | |
f2485b3e | 9481 | } |
a547c6db | 9482 | } |
c207aee4 | 9483 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
a547c6db | 9484 | |
6d396b55 PB |
9485 | static bool vmx_has_high_real_mode_segbase(void) |
9486 | { | |
9487 | return enable_unrestricted_guest || emulate_invalid_guest_state; | |
9488 | } | |
9489 | ||
da8999d3 LJ |
9490 | static bool vmx_mpx_supported(void) |
9491 | { | |
9492 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && | |
9493 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); | |
9494 | } | |
9495 | ||
55412b2e WL |
9496 | static bool vmx_xsaves_supported(void) |
9497 | { | |
9498 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
9499 | SECONDARY_EXEC_XSAVES; | |
9500 | } | |
9501 | ||
66336cab PB |
9502 | static bool vmx_umip_emulated(void) |
9503 | { | |
0367f205 PB |
9504 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
9505 | SECONDARY_EXEC_DESC; | |
66336cab PB |
9506 | } |
9507 | ||
51aa01d1 AK |
9508 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
9509 | { | |
c5ca8e57 | 9510 | u32 exit_intr_info; |
51aa01d1 AK |
9511 | bool unblock_nmi; |
9512 | u8 vector; | |
9513 | bool idtv_info_valid; | |
9514 | ||
9515 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
20f65983 | 9516 | |
d02fcf50 | 9517 | if (enable_vnmi) { |
8a1b4392 PB |
9518 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
9519 | return; | |
9520 | /* | |
9521 | * Can't use vmx->exit_intr_info since we're not sure what | |
9522 | * the exit reason is. | |
9523 | */ | |
9524 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
9525 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; | |
9526 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
9527 | /* | |
9528 | * SDM 3: 27.7.1.2 (September 2008) | |
9529 | * Re-set bit "block by NMI" before VM entry if vmexit caused by | |
9530 | * a guest IRET fault. | |
9531 | * SDM 3: 23.2.2 (September 2008) | |
9532 | * Bit 12 is undefined in any of the following cases: | |
9533 | * If the VM exit sets the valid bit in the IDT-vectoring | |
9534 | * information field. | |
9535 | * If the VM exit is due to a double fault. | |
9536 | */ | |
9537 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && | |
9538 | vector != DF_VECTOR && !idtv_info_valid) | |
9539 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
9540 | GUEST_INTR_STATE_NMI); | |
9541 | else | |
9542 | vmx->loaded_vmcs->nmi_known_unmasked = | |
9543 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) | |
9544 | & GUEST_INTR_STATE_NMI); | |
9545 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) | |
9546 | vmx->loaded_vmcs->vnmi_blocked_time += | |
9547 | ktime_to_ns(ktime_sub(ktime_get(), | |
9548 | vmx->loaded_vmcs->entry_time)); | |
51aa01d1 AK |
9549 | } |
9550 | ||
3ab66e8a | 9551 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
83422e17 AK |
9552 | u32 idt_vectoring_info, |
9553 | int instr_len_field, | |
9554 | int error_code_field) | |
51aa01d1 | 9555 | { |
51aa01d1 AK |
9556 | u8 vector; |
9557 | int type; | |
9558 | bool idtv_info_valid; | |
9559 | ||
9560 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
668f612f | 9561 | |
3ab66e8a JK |
9562 | vcpu->arch.nmi_injected = false; |
9563 | kvm_clear_exception_queue(vcpu); | |
9564 | kvm_clear_interrupt_queue(vcpu); | |
37b96e98 GN |
9565 | |
9566 | if (!idtv_info_valid) | |
9567 | return; | |
9568 | ||
3ab66e8a | 9569 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
3842d135 | 9570 | |
668f612f AK |
9571 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
9572 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; | |
37b96e98 | 9573 | |
64a7ec06 | 9574 | switch (type) { |
37b96e98 | 9575 | case INTR_TYPE_NMI_INTR: |
3ab66e8a | 9576 | vcpu->arch.nmi_injected = true; |
668f612f | 9577 | /* |
7b4a25cb | 9578 | * SDM 3: 27.7.1.2 (September 2008) |
37b96e98 GN |
9579 | * Clear bit "block by NMI" before VM entry if a NMI |
9580 | * delivery faulted. | |
668f612f | 9581 | */ |
3ab66e8a | 9582 | vmx_set_nmi_mask(vcpu, false); |
37b96e98 | 9583 | break; |
37b96e98 | 9584 | case INTR_TYPE_SOFT_EXCEPTION: |
3ab66e8a | 9585 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f GN |
9586 | /* fall through */ |
9587 | case INTR_TYPE_HARD_EXCEPTION: | |
35920a35 | 9588 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
83422e17 | 9589 | u32 err = vmcs_read32(error_code_field); |
851eb667 | 9590 | kvm_requeue_exception_e(vcpu, vector, err); |
35920a35 | 9591 | } else |
851eb667 | 9592 | kvm_requeue_exception(vcpu, vector); |
37b96e98 | 9593 | break; |
66fd3f7f | 9594 | case INTR_TYPE_SOFT_INTR: |
3ab66e8a | 9595 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f | 9596 | /* fall through */ |
37b96e98 | 9597 | case INTR_TYPE_EXT_INTR: |
3ab66e8a | 9598 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
37b96e98 GN |
9599 | break; |
9600 | default: | |
9601 | break; | |
f7d9238f | 9602 | } |
cf393f75 AK |
9603 | } |
9604 | ||
83422e17 AK |
9605 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
9606 | { | |
3ab66e8a | 9607 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
83422e17 AK |
9608 | VM_EXIT_INSTRUCTION_LEN, |
9609 | IDT_VECTORING_ERROR_CODE); | |
9610 | } | |
9611 | ||
b463a6f7 AK |
9612 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
9613 | { | |
3ab66e8a | 9614 | __vmx_complete_interrupts(vcpu, |
b463a6f7 AK |
9615 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
9616 | VM_ENTRY_INSTRUCTION_LEN, | |
9617 | VM_ENTRY_EXCEPTION_ERROR_CODE); | |
9618 | ||
9619 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
9620 | } | |
9621 | ||
d7cd9796 GN |
9622 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
9623 | { | |
9624 | int i, nr_msrs; | |
9625 | struct perf_guest_switch_msr *msrs; | |
9626 | ||
9627 | msrs = perf_guest_get_msrs(&nr_msrs); | |
9628 | ||
9629 | if (!msrs) | |
9630 | return; | |
9631 | ||
9632 | for (i = 0; i < nr_msrs; i++) | |
9633 | if (msrs[i].host == msrs[i].guest) | |
9634 | clear_atomic_switch_msr(vmx, msrs[i].msr); | |
9635 | else | |
9636 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, | |
9637 | msrs[i].host); | |
9638 | } | |
9639 | ||
33365e7a | 9640 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
64672c95 YJ |
9641 | { |
9642 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9643 | u64 tscl; | |
9644 | u32 delta_tsc; | |
9645 | ||
9646 | if (vmx->hv_deadline_tsc == -1) | |
9647 | return; | |
9648 | ||
9649 | tscl = rdtsc(); | |
9650 | if (vmx->hv_deadline_tsc > tscl) | |
9651 | /* sure to be 32 bit only because checked on set_hv_timer */ | |
9652 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> | |
9653 | cpu_preemption_timer_multi); | |
9654 | else | |
9655 | delta_tsc = 0; | |
9656 | ||
9657 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); | |
9658 | } | |
9659 | ||
a3b5ba49 | 9660 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
6aa8b732 | 9661 | { |
a2fa3e9f | 9662 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
773e8a04 | 9663 | unsigned long cr3, cr4, evmcs_rsp; |
104f226b | 9664 | |
8a1b4392 | 9665 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
d02fcf50 | 9666 | if (unlikely(!enable_vnmi && |
8a1b4392 PB |
9667 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
9668 | vmx->loaded_vmcs->entry_time = ktime_get(); | |
9669 | ||
104f226b AK |
9670 | /* Don't enter VMX if guest state is invalid, let the exit handler |
9671 | start emulation until we arrive back to a valid state */ | |
14168786 | 9672 | if (vmx->emulation_required) |
104f226b AK |
9673 | return; |
9674 | ||
a7653ecd RK |
9675 | if (vmx->ple_window_dirty) { |
9676 | vmx->ple_window_dirty = false; | |
9677 | vmcs_write32(PLE_WINDOW, vmx->ple_window); | |
9678 | } | |
9679 | ||
012f83cb AG |
9680 | if (vmx->nested.sync_shadow_vmcs) { |
9681 | copy_vmcs12_to_shadow(vmx); | |
9682 | vmx->nested.sync_shadow_vmcs = false; | |
9683 | } | |
9684 | ||
104f226b AK |
9685 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
9686 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); | |
9687 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) | |
9688 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); | |
9689 | ||
d6e41f11 | 9690 | cr3 = __get_current_cr3_fast(); |
44889942 | 9691 | if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) { |
d6e41f11 | 9692 | vmcs_writel(HOST_CR3, cr3); |
44889942 | 9693 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
d6e41f11 AL |
9694 | } |
9695 | ||
1e02ce4c | 9696 | cr4 = cr4_read_shadow(); |
44889942 | 9697 | if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) { |
d974baa3 | 9698 | vmcs_writel(HOST_CR4, cr4); |
44889942 | 9699 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
d974baa3 AL |
9700 | } |
9701 | ||
104f226b AK |
9702 | /* When single-stepping over STI and MOV SS, we must clear the |
9703 | * corresponding interruptibility bits in the guest state. Otherwise | |
9704 | * vmentry fails as it then expects bit 14 (BS) in pending debug | |
9705 | * exceptions being set, but that's not correct for the guest debugging | |
9706 | * case. */ | |
9707 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) | |
9708 | vmx_set_interrupt_shadow(vcpu, 0); | |
9709 | ||
b9dd21e1 PB |
9710 | if (static_cpu_has(X86_FEATURE_PKU) && |
9711 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && | |
9712 | vcpu->arch.pkru != vmx->host_pkru) | |
9713 | __write_pkru(vcpu->arch.pkru); | |
1be0e61c | 9714 | |
d7cd9796 GN |
9715 | atomic_switch_perf_msrs(vmx); |
9716 | ||
64672c95 YJ |
9717 | vmx_arm_hv_timer(vcpu); |
9718 | ||
d28b387f KA |
9719 | /* |
9720 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if | |
9721 | * it's non-zero. Since vmentry is serialising on affected CPUs, there | |
9722 | * is no need to worry about the conditional branch over the wrmsr | |
9723 | * being speculatively taken. | |
9724 | */ | |
5cf68754 | 9725 | x86_spec_ctrl_set_guest(vmx->spec_ctrl); |
d28b387f | 9726 | |
d462b819 | 9727 | vmx->__launched = vmx->loaded_vmcs->launched; |
773e8a04 VK |
9728 | |
9729 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? | |
9730 | (unsigned long)¤t_evmcs->host_rsp : 0; | |
9731 | ||
104f226b | 9732 | asm( |
6aa8b732 | 9733 | /* Store host registers */ |
b188c81f AK |
9734 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
9735 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ | |
9736 | "push %%" _ASM_CX " \n\t" | |
9737 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" | |
313dbd49 | 9738 | "je 1f \n\t" |
b188c81f | 9739 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
773e8a04 VK |
9740 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
9741 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" | |
9742 | "jz 2f \n\t" | |
9743 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" | |
9744 | "jmp 1f \n\t" | |
9745 | "2: \n\t" | |
4ecac3fd | 9746 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
313dbd49 | 9747 | "1: \n\t" |
d3edefc0 | 9748 | /* Reload cr2 if changed */ |
b188c81f AK |
9749 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
9750 | "mov %%cr2, %%" _ASM_DX " \n\t" | |
9751 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" | |
773e8a04 | 9752 | "je 3f \n\t" |
b188c81f | 9753 | "mov %%" _ASM_AX", %%cr2 \n\t" |
773e8a04 | 9754 | "3: \n\t" |
6aa8b732 | 9755 | /* Check if vmlaunch of vmresume is needed */ |
e08aa78a | 9756 | "cmpl $0, %c[launched](%0) \n\t" |
6aa8b732 | 9757 | /* Load guest registers. Don't clobber flags. */ |
b188c81f AK |
9758 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
9759 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" | |
9760 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" | |
9761 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" | |
9762 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" | |
9763 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" | |
05b3e0c2 | 9764 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
9765 | "mov %c[r8](%0), %%r8 \n\t" |
9766 | "mov %c[r9](%0), %%r9 \n\t" | |
9767 | "mov %c[r10](%0), %%r10 \n\t" | |
9768 | "mov %c[r11](%0), %%r11 \n\t" | |
9769 | "mov %c[r12](%0), %%r12 \n\t" | |
9770 | "mov %c[r13](%0), %%r13 \n\t" | |
9771 | "mov %c[r14](%0), %%r14 \n\t" | |
9772 | "mov %c[r15](%0), %%r15 \n\t" | |
6aa8b732 | 9773 | #endif |
b188c81f | 9774 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
c801949d | 9775 | |
6aa8b732 | 9776 | /* Enter guest mode */ |
83287ea4 | 9777 | "jne 1f \n\t" |
4ecac3fd | 9778 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
83287ea4 AK |
9779 | "jmp 2f \n\t" |
9780 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" | |
9781 | "2: " | |
6aa8b732 | 9782 | /* Save guest registers, load host registers, keep flags */ |
b188c81f | 9783 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
40712fae | 9784 | "pop %0 \n\t" |
0cb5b306 | 9785 | "setbe %c[fail](%0)\n\t" |
b188c81f AK |
9786 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
9787 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" | |
9788 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" | |
9789 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" | |
9790 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" | |
9791 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" | |
9792 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" | |
05b3e0c2 | 9793 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
9794 | "mov %%r8, %c[r8](%0) \n\t" |
9795 | "mov %%r9, %c[r9](%0) \n\t" | |
9796 | "mov %%r10, %c[r10](%0) \n\t" | |
9797 | "mov %%r11, %c[r11](%0) \n\t" | |
9798 | "mov %%r12, %c[r12](%0) \n\t" | |
9799 | "mov %%r13, %c[r13](%0) \n\t" | |
9800 | "mov %%r14, %c[r14](%0) \n\t" | |
9801 | "mov %%r15, %c[r15](%0) \n\t" | |
0cb5b306 JM |
9802 | "xor %%r8d, %%r8d \n\t" |
9803 | "xor %%r9d, %%r9d \n\t" | |
9804 | "xor %%r10d, %%r10d \n\t" | |
9805 | "xor %%r11d, %%r11d \n\t" | |
9806 | "xor %%r12d, %%r12d \n\t" | |
9807 | "xor %%r13d, %%r13d \n\t" | |
9808 | "xor %%r14d, %%r14d \n\t" | |
9809 | "xor %%r15d, %%r15d \n\t" | |
6aa8b732 | 9810 | #endif |
b188c81f AK |
9811 | "mov %%cr2, %%" _ASM_AX " \n\t" |
9812 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" | |
c801949d | 9813 | |
0cb5b306 JM |
9814 | "xor %%eax, %%eax \n\t" |
9815 | "xor %%ebx, %%ebx \n\t" | |
9816 | "xor %%esi, %%esi \n\t" | |
9817 | "xor %%edi, %%edi \n\t" | |
b188c81f | 9818 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
83287ea4 AK |
9819 | ".pushsection .rodata \n\t" |
9820 | ".global vmx_return \n\t" | |
9821 | "vmx_return: " _ASM_PTR " 2b \n\t" | |
9822 | ".popsection" | |
773e8a04 | 9823 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
d462b819 | 9824 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
e08aa78a | 9825 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
313dbd49 | 9826 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
ad312c7c ZX |
9827 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
9828 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), | |
9829 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), | |
9830 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), | |
9831 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), | |
9832 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), | |
9833 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), | |
05b3e0c2 | 9834 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
9835 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
9836 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), | |
9837 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), | |
9838 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), | |
9839 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), | |
9840 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), | |
9841 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), | |
9842 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), | |
6aa8b732 | 9843 | #endif |
40712fae AK |
9844 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
9845 | [wordsize]"i"(sizeof(ulong)) | |
c2036300 LV |
9846 | : "cc", "memory" |
9847 | #ifdef CONFIG_X86_64 | |
773e8a04 | 9848 | , "rax", "rbx", "rdi" |
c2036300 | 9849 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
b188c81f | 9850 | #else |
773e8a04 | 9851 | , "eax", "ebx", "edi" |
c2036300 LV |
9852 | #endif |
9853 | ); | |
6aa8b732 | 9854 | |
d28b387f KA |
9855 | /* |
9856 | * We do not use IBRS in the kernel. If this vCPU has used the | |
9857 | * SPEC_CTRL MSR it may have left it on; save the value and | |
9858 | * turn it off. This is much more efficient than blindly adding | |
9859 | * it to the atomic save/restore list. Especially as the former | |
9860 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. | |
9861 | * | |
9862 | * For non-nested case: | |
9863 | * If the L01 MSR bitmap does not intercept the MSR, then we need to | |
9864 | * save it. | |
9865 | * | |
9866 | * For nested case: | |
9867 | * If the L02 MSR bitmap does not intercept the MSR, then we need to | |
9868 | * save it. | |
9869 | */ | |
946fbbc1 | 9870 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
ecb586bd | 9871 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
d28b387f | 9872 | |
5cf68754 | 9873 | x86_spec_ctrl_restore_host(vmx->spec_ctrl); |
d28b387f | 9874 | |
117cc7a9 DW |
9875 | /* Eliminate branch target predictions from guest mode */ |
9876 | vmexit_fill_RSB(); | |
9877 | ||
773e8a04 VK |
9878 | /* All fields are clean at this point */ |
9879 | if (static_branch_unlikely(&enable_evmcs)) | |
9880 | current_evmcs->hv_clean_fields |= | |
9881 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; | |
9882 | ||
2a7921b7 | 9883 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
74c55931 WL |
9884 | if (vmx->host_debugctlmsr) |
9885 | update_debugctlmsr(vmx->host_debugctlmsr); | |
2a7921b7 | 9886 | |
aa67f609 AK |
9887 | #ifndef CONFIG_X86_64 |
9888 | /* | |
9889 | * The sysexit path does not restore ds/es, so we must set them to | |
9890 | * a reasonable value ourselves. | |
9891 | * | |
9892 | * We can't defer this to vmx_load_host_state() since that function | |
9893 | * may be executed in interrupt context, which saves and restore segments | |
9894 | * around it, nullifying its effect. | |
9895 | */ | |
9896 | loadsegment(ds, __USER_DS); | |
9897 | loadsegment(es, __USER_DS); | |
9898 | #endif | |
9899 | ||
6de4f3ad | 9900 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
6de12732 | 9901 | | (1 << VCPU_EXREG_RFLAGS) |
aff48baa | 9902 | | (1 << VCPU_EXREG_PDPTR) |
2fb92db1 | 9903 | | (1 << VCPU_EXREG_SEGMENTS) |
aff48baa | 9904 | | (1 << VCPU_EXREG_CR3)); |
5fdbf976 MT |
9905 | vcpu->arch.regs_dirty = 0; |
9906 | ||
1be0e61c XG |
9907 | /* |
9908 | * eager fpu is enabled if PKEY is supported and CR4 is switched | |
9909 | * back on host, so it is safe to read guest PKRU from current | |
9910 | * XSAVE. | |
9911 | */ | |
b9dd21e1 PB |
9912 | if (static_cpu_has(X86_FEATURE_PKU) && |
9913 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { | |
9914 | vcpu->arch.pkru = __read_pkru(); | |
9915 | if (vcpu->arch.pkru != vmx->host_pkru) | |
1be0e61c | 9916 | __write_pkru(vmx->host_pkru); |
1be0e61c XG |
9917 | } |
9918 | ||
e0b890d3 | 9919 | vmx->nested.nested_run_pending = 0; |
b060ca3b JM |
9920 | vmx->idt_vectoring_info = 0; |
9921 | ||
9922 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); | |
9923 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) | |
9924 | return; | |
9925 | ||
9926 | vmx->loaded_vmcs->launched = 1; | |
9927 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); | |
e0b890d3 | 9928 | |
51aa01d1 AK |
9929 | vmx_complete_atomic_exit(vmx); |
9930 | vmx_recover_nmi_blocking(vmx); | |
cf393f75 | 9931 | vmx_complete_interrupts(vmx); |
6aa8b732 | 9932 | } |
c207aee4 | 9933 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
6aa8b732 | 9934 | |
434a1e94 SC |
9935 | static struct kvm *vmx_vm_alloc(void) |
9936 | { | |
40bbb9d0 SC |
9937 | struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL); |
9938 | return &kvm_vmx->kvm; | |
434a1e94 SC |
9939 | } |
9940 | ||
9941 | static void vmx_vm_free(struct kvm *kvm) | |
9942 | { | |
40bbb9d0 | 9943 | kfree(to_kvm_vmx(kvm)); |
434a1e94 SC |
9944 | } |
9945 | ||
1279a6b1 | 9946 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
4fa7734c PB |
9947 | { |
9948 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9949 | int cpu; | |
9950 | ||
1279a6b1 | 9951 | if (vmx->loaded_vmcs == vmcs) |
4fa7734c PB |
9952 | return; |
9953 | ||
9954 | cpu = get_cpu(); | |
1279a6b1 | 9955 | vmx->loaded_vmcs = vmcs; |
4fa7734c PB |
9956 | vmx_vcpu_put(vcpu); |
9957 | vmx_vcpu_load(vcpu, cpu); | |
4fa7734c PB |
9958 | put_cpu(); |
9959 | } | |
9960 | ||
2f1fe811 JM |
9961 | /* |
9962 | * Ensure that the current vmcs of the logical processor is the | |
9963 | * vmcs01 of the vcpu before calling free_nested(). | |
9964 | */ | |
9965 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) | |
9966 | { | |
9967 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
2f1fe811 | 9968 | |
ec7660cc | 9969 | vcpu_load(vcpu); |
1279a6b1 | 9970 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
2f1fe811 JM |
9971 | free_nested(vmx); |
9972 | vcpu_put(vcpu); | |
9973 | } | |
9974 | ||
6aa8b732 AK |
9975 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
9976 | { | |
fb3f0f51 RR |
9977 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
9978 | ||
843e4330 | 9979 | if (enable_pml) |
a3eaa864 | 9980 | vmx_destroy_pml_buffer(vmx); |
991e7a0e | 9981 | free_vpid(vmx->vpid); |
4fa7734c | 9982 | leave_guest_mode(vcpu); |
2f1fe811 | 9983 | vmx_free_vcpu_nested(vcpu); |
4fa7734c | 9984 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 RR |
9985 | kfree(vmx->guest_msrs); |
9986 | kvm_vcpu_uninit(vcpu); | |
a4770347 | 9987 | kmem_cache_free(kvm_vcpu_cache, vmx); |
6aa8b732 AK |
9988 | } |
9989 | ||
fb3f0f51 | 9990 | static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) |
6aa8b732 | 9991 | { |
fb3f0f51 | 9992 | int err; |
c16f862d | 9993 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
904e14fb | 9994 | unsigned long *msr_bitmap; |
15ad7146 | 9995 | int cpu; |
6aa8b732 | 9996 | |
a2fa3e9f | 9997 | if (!vmx) |
fb3f0f51 RR |
9998 | return ERR_PTR(-ENOMEM); |
9999 | ||
991e7a0e | 10000 | vmx->vpid = allocate_vpid(); |
2384d2b3 | 10001 | |
fb3f0f51 RR |
10002 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
10003 | if (err) | |
10004 | goto free_vcpu; | |
965b58a5 | 10005 | |
4e59516a PF |
10006 | err = -ENOMEM; |
10007 | ||
10008 | /* | |
10009 | * If PML is turned on, failure on enabling PML just results in failure | |
10010 | * of creating the vcpu, therefore we can simplify PML logic (by | |
10011 | * avoiding dealing with cases, such as enabling PML partially on vcpus | |
10012 | * for the guest, etc. | |
10013 | */ | |
10014 | if (enable_pml) { | |
10015 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
10016 | if (!vmx->pml_pg) | |
10017 | goto uninit_vcpu; | |
10018 | } | |
10019 | ||
a2fa3e9f | 10020 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
03916db9 PB |
10021 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
10022 | > PAGE_SIZE); | |
0123be42 | 10023 | |
4e59516a PF |
10024 | if (!vmx->guest_msrs) |
10025 | goto free_pml; | |
965b58a5 | 10026 | |
f21f165e PB |
10027 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
10028 | if (err < 0) | |
fb3f0f51 | 10029 | goto free_msrs; |
a2fa3e9f | 10030 | |
904e14fb PB |
10031 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
10032 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); | |
10033 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); | |
10034 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); | |
10035 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); | |
10036 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); | |
10037 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); | |
10038 | vmx->msr_bitmap_mode = 0; | |
10039 | ||
f21f165e | 10040 | vmx->loaded_vmcs = &vmx->vmcs01; |
15ad7146 AK |
10041 | cpu = get_cpu(); |
10042 | vmx_vcpu_load(&vmx->vcpu, cpu); | |
e48672fa | 10043 | vmx->vcpu.cpu = cpu; |
12d79917 | 10044 | vmx_vcpu_setup(vmx); |
fb3f0f51 | 10045 | vmx_vcpu_put(&vmx->vcpu); |
15ad7146 | 10046 | put_cpu(); |
35754c98 | 10047 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
be6d05cf JK |
10048 | err = alloc_apic_access_page(kvm); |
10049 | if (err) | |
5e4a0b3c | 10050 | goto free_vmcs; |
a63cb560 | 10051 | } |
fb3f0f51 | 10052 | |
e90008df | 10053 | if (enable_ept && !enable_unrestricted_guest) { |
f51770ed TC |
10054 | err = init_rmode_identity_map(kvm); |
10055 | if (err) | |
93ea5388 | 10056 | goto free_vmcs; |
b927a3ce | 10057 | } |
b7ebfb05 | 10058 | |
5c614b35 | 10059 | if (nested) { |
6677f3da PB |
10060 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
10061 | kvm_vcpu_apicv_active(&vmx->vcpu)); | |
5c614b35 WL |
10062 | vmx->nested.vpid02 = allocate_vpid(); |
10063 | } | |
b9c237bb | 10064 | |
705699a1 | 10065 | vmx->nested.posted_intr_nv = -1; |
a9d30f33 | 10066 | vmx->nested.current_vmptr = -1ull; |
a9d30f33 | 10067 | |
37e4c997 HZ |
10068 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
10069 | ||
31afb2ea PB |
10070 | /* |
10071 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR | |
10072 | * or POSTED_INTR_WAKEUP_VECTOR. | |
10073 | */ | |
10074 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; | |
10075 | vmx->pi_desc.sn = 1; | |
10076 | ||
fb3f0f51 RR |
10077 | return &vmx->vcpu; |
10078 | ||
10079 | free_vmcs: | |
5c614b35 | 10080 | free_vpid(vmx->nested.vpid02); |
5f3fbc34 | 10081 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 | 10082 | free_msrs: |
fb3f0f51 | 10083 | kfree(vmx->guest_msrs); |
4e59516a PF |
10084 | free_pml: |
10085 | vmx_destroy_pml_buffer(vmx); | |
fb3f0f51 RR |
10086 | uninit_vcpu: |
10087 | kvm_vcpu_uninit(&vmx->vcpu); | |
10088 | free_vcpu: | |
991e7a0e | 10089 | free_vpid(vmx->vpid); |
a4770347 | 10090 | kmem_cache_free(kvm_vcpu_cache, vmx); |
fb3f0f51 | 10091 | return ERR_PTR(err); |
6aa8b732 AK |
10092 | } |
10093 | ||
b31c114b WL |
10094 | static int vmx_vm_init(struct kvm *kvm) |
10095 | { | |
10096 | if (!ple_gap) | |
10097 | kvm->arch.pause_in_guest = true; | |
10098 | return 0; | |
10099 | } | |
10100 | ||
002c7f7c YS |
10101 | static void __init vmx_check_processor_compat(void *rtn) |
10102 | { | |
10103 | struct vmcs_config vmcs_conf; | |
10104 | ||
10105 | *(int *)rtn = 0; | |
10106 | if (setup_vmcs_config(&vmcs_conf) < 0) | |
10107 | *(int *)rtn = -EIO; | |
1389309c | 10108 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv); |
002c7f7c YS |
10109 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
10110 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", | |
10111 | smp_processor_id()); | |
10112 | *(int *)rtn = -EIO; | |
10113 | } | |
10114 | } | |
10115 | ||
4b12f0de | 10116 | static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
64d4d521 | 10117 | { |
b18d5431 XG |
10118 | u8 cache; |
10119 | u64 ipat = 0; | |
4b12f0de | 10120 | |
522c68c4 | 10121 | /* For VT-d and EPT combination |
606decd6 | 10122 | * 1. MMIO: always map as UC |
522c68c4 SY |
10123 | * 2. EPT with VT-d: |
10124 | * a. VT-d without snooping control feature: can't guarantee the | |
606decd6 | 10125 | * result, try to trust guest. |
522c68c4 SY |
10126 | * b. VT-d with snooping control feature: snooping control feature of |
10127 | * VT-d engine can guarantee the cache correctness. Just set it | |
10128 | * to WB to keep consistent with host. So the same as item 3. | |
a19a6d11 | 10129 | * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep |
522c68c4 SY |
10130 | * consistent with host MTRR |
10131 | */ | |
606decd6 PB |
10132 | if (is_mmio) { |
10133 | cache = MTRR_TYPE_UNCACHABLE; | |
10134 | goto exit; | |
10135 | } | |
10136 | ||
10137 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { | |
b18d5431 XG |
10138 | ipat = VMX_EPT_IPAT_BIT; |
10139 | cache = MTRR_TYPE_WRBACK; | |
10140 | goto exit; | |
10141 | } | |
10142 | ||
10143 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { | |
10144 | ipat = VMX_EPT_IPAT_BIT; | |
0da029ed | 10145 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
fb279950 XG |
10146 | cache = MTRR_TYPE_WRBACK; |
10147 | else | |
10148 | cache = MTRR_TYPE_UNCACHABLE; | |
b18d5431 XG |
10149 | goto exit; |
10150 | } | |
10151 | ||
ff53604b | 10152 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
b18d5431 XG |
10153 | |
10154 | exit: | |
10155 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; | |
64d4d521 SY |
10156 | } |
10157 | ||
17cc3935 | 10158 | static int vmx_get_lpage_level(void) |
344f414f | 10159 | { |
878403b7 SY |
10160 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
10161 | return PT_DIRECTORY_LEVEL; | |
10162 | else | |
10163 | /* For shadow and EPT supported 1GB page */ | |
10164 | return PT_PDPE_LEVEL; | |
344f414f JR |
10165 | } |
10166 | ||
feda805f XG |
10167 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
10168 | { | |
10169 | /* | |
10170 | * These bits in the secondary execution controls field | |
10171 | * are dynamic, the others are mostly based on the hypervisor | |
10172 | * architecture and the guest's CPUID. Do not touch the | |
10173 | * dynamic bits. | |
10174 | */ | |
10175 | u32 mask = | |
10176 | SECONDARY_EXEC_SHADOW_VMCS | | |
10177 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | | |
0367f205 PB |
10178 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
10179 | SECONDARY_EXEC_DESC; | |
feda805f XG |
10180 | |
10181 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
10182 | ||
10183 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, | |
10184 | (new_ctl & ~mask) | (cur_ctl & mask)); | |
10185 | } | |
10186 | ||
8322ebbb DM |
10187 | /* |
10188 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits | |
10189 | * (indicating "allowed-1") if they are supported in the guest's CPUID. | |
10190 | */ | |
10191 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) | |
10192 | { | |
10193 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10194 | struct kvm_cpuid_entry2 *entry; | |
10195 | ||
6677f3da PB |
10196 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
10197 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; | |
8322ebbb DM |
10198 | |
10199 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ | |
10200 | if (entry && (entry->_reg & (_cpuid_mask))) \ | |
6677f3da | 10201 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
8322ebbb DM |
10202 | } while (0) |
10203 | ||
10204 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); | |
10205 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); | |
10206 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); | |
10207 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); | |
10208 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); | |
10209 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); | |
10210 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); | |
10211 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); | |
10212 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); | |
10213 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); | |
10214 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); | |
10215 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); | |
10216 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); | |
10217 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); | |
10218 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); | |
10219 | ||
10220 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); | |
10221 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); | |
10222 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); | |
10223 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); | |
10224 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); | |
c4ad77e0 | 10225 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
8322ebbb DM |
10226 | |
10227 | #undef cr4_fixed1_update | |
10228 | } | |
10229 | ||
0e851880 SY |
10230 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
10231 | { | |
4e47c7a6 | 10232 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
4e47c7a6 | 10233 | |
80154d77 PB |
10234 | if (cpu_has_secondary_exec_ctrls()) { |
10235 | vmx_compute_secondary_exec_control(vmx); | |
10236 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); | |
ad756a16 | 10237 | } |
8b3e34e4 | 10238 | |
37e4c997 HZ |
10239 | if (nested_vmx_allowed(vcpu)) |
10240 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
10241 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
10242 | else | |
10243 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
10244 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
8322ebbb DM |
10245 | |
10246 | if (nested_vmx_allowed(vcpu)) | |
10247 | nested_vmx_cr_fixed1_bits_update(vcpu); | |
0e851880 SY |
10248 | } |
10249 | ||
d4330ef2 JR |
10250 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
10251 | { | |
7b8050f5 NHE |
10252 | if (func == 1 && nested) |
10253 | entry->ecx |= bit(X86_FEATURE_VMX); | |
d4330ef2 JR |
10254 | } |
10255 | ||
25d92081 YZ |
10256 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
10257 | struct x86_exception *fault) | |
10258 | { | |
533558bc | 10259 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
c5f983f6 | 10260 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
533558bc | 10261 | u32 exit_reason; |
c5f983f6 | 10262 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
25d92081 | 10263 | |
c5f983f6 BD |
10264 | if (vmx->nested.pml_full) { |
10265 | exit_reason = EXIT_REASON_PML_FULL; | |
10266 | vmx->nested.pml_full = false; | |
10267 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; | |
10268 | } else if (fault->error_code & PFERR_RSVD_MASK) | |
533558bc | 10269 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
25d92081 | 10270 | else |
533558bc | 10271 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
c5f983f6 BD |
10272 | |
10273 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); | |
25d92081 YZ |
10274 | vmcs12->guest_physical_address = fault->address; |
10275 | } | |
10276 | ||
995f00a6 PF |
10277 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
10278 | { | |
bb97a016 | 10279 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
995f00a6 PF |
10280 | } |
10281 | ||
155a97a3 NHE |
10282 | /* Callbacks for nested_ept_init_mmu_context: */ |
10283 | ||
10284 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) | |
10285 | { | |
10286 | /* return the page table to be shadowed - in our case, EPT12 */ | |
10287 | return get_vmcs12(vcpu)->ept_pointer; | |
10288 | } | |
10289 | ||
ae1e2d10 | 10290 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
155a97a3 | 10291 | { |
ad896af0 | 10292 | WARN_ON(mmu_is_nested(vcpu)); |
a057e0e2 | 10293 | if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu))) |
ae1e2d10 PB |
10294 | return 1; |
10295 | ||
10296 | kvm_mmu_unload(vcpu); | |
ad896af0 | 10297 | kvm_init_shadow_ept_mmu(vcpu, |
6677f3da | 10298 | to_vmx(vcpu)->nested.msrs.ept_caps & |
ae1e2d10 | 10299 | VMX_EPT_EXECUTE_ONLY_BIT, |
a057e0e2 | 10300 | nested_ept_ad_enabled(vcpu)); |
155a97a3 NHE |
10301 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
10302 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; | |
10303 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; | |
10304 | ||
10305 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; | |
ae1e2d10 | 10306 | return 0; |
155a97a3 NHE |
10307 | } |
10308 | ||
10309 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) | |
10310 | { | |
10311 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; | |
10312 | } | |
10313 | ||
19d5f10b EK |
10314 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
10315 | u16 error_code) | |
10316 | { | |
10317 | bool inequality, bit; | |
10318 | ||
10319 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; | |
10320 | inequality = | |
10321 | (error_code & vmcs12->page_fault_error_code_mask) != | |
10322 | vmcs12->page_fault_error_code_match; | |
10323 | return inequality ^ bit; | |
10324 | } | |
10325 | ||
feaf0c7d GN |
10326 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
10327 | struct x86_exception *fault) | |
10328 | { | |
10329 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
10330 | ||
10331 | WARN_ON(!is_guest_mode(vcpu)); | |
10332 | ||
305d0ab4 WL |
10333 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
10334 | !to_vmx(vcpu)->nested.nested_run_pending) { | |
b96fb439 PB |
10335 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
10336 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, | |
10337 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | | |
10338 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, | |
10339 | fault->address); | |
7313c698 | 10340 | } else { |
feaf0c7d | 10341 | kvm_inject_page_fault(vcpu, fault); |
7313c698 | 10342 | } |
feaf0c7d GN |
10343 | } |
10344 | ||
c992384b PB |
10345 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
10346 | struct vmcs12 *vmcs12); | |
6beb7bd5 JM |
10347 | |
10348 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, | |
a2bcba50 WL |
10349 | struct vmcs12 *vmcs12) |
10350 | { | |
10351 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5e2f30b7 | 10352 | struct page *page; |
6beb7bd5 | 10353 | u64 hpa; |
a2bcba50 WL |
10354 | |
10355 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
a2bcba50 WL |
10356 | /* |
10357 | * Translate L1 physical address to host physical | |
10358 | * address for vmcs02. Keep the page pinned, so this | |
10359 | * physical address remains valid. We keep a reference | |
10360 | * to it so we can release it later. | |
10361 | */ | |
5e2f30b7 | 10362 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
53a70daf | 10363 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
5e2f30b7 DH |
10364 | vmx->nested.apic_access_page = NULL; |
10365 | } | |
10366 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); | |
6beb7bd5 JM |
10367 | /* |
10368 | * If translation failed, no matter: This feature asks | |
10369 | * to exit when accessing the given address, and if it | |
10370 | * can never be accessed, this feature won't do | |
10371 | * anything anyway. | |
10372 | */ | |
5e2f30b7 DH |
10373 | if (!is_error_page(page)) { |
10374 | vmx->nested.apic_access_page = page; | |
6beb7bd5 JM |
10375 | hpa = page_to_phys(vmx->nested.apic_access_page); |
10376 | vmcs_write64(APIC_ACCESS_ADDR, hpa); | |
10377 | } else { | |
10378 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
10379 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
10380 | } | |
10381 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && | |
10382 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { | |
10383 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
10384 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
10385 | kvm_vcpu_reload_apic_access_page(vcpu); | |
a2bcba50 | 10386 | } |
a7c0b07d WL |
10387 | |
10388 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { | |
5e2f30b7 | 10389 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
53a70daf | 10390 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
5e2f30b7 DH |
10391 | vmx->nested.virtual_apic_page = NULL; |
10392 | } | |
10393 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); | |
a7c0b07d WL |
10394 | |
10395 | /* | |
6beb7bd5 JM |
10396 | * If translation failed, VM entry will fail because |
10397 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. | |
10398 | * Failing the vm entry is _not_ what the processor | |
10399 | * does but it's basically the only possibility we | |
10400 | * have. We could still enter the guest if CR8 load | |
10401 | * exits are enabled, CR8 store exits are enabled, and | |
10402 | * virtualize APIC access is disabled; in this case | |
10403 | * the processor would never use the TPR shadow and we | |
10404 | * could simply clear the bit from the execution | |
10405 | * control. But such a configuration is useless, so | |
10406 | * let's keep the code simple. | |
a7c0b07d | 10407 | */ |
5e2f30b7 DH |
10408 | if (!is_error_page(page)) { |
10409 | vmx->nested.virtual_apic_page = page; | |
6beb7bd5 JM |
10410 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
10411 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); | |
10412 | } | |
a7c0b07d WL |
10413 | } |
10414 | ||
705699a1 | 10415 | if (nested_cpu_has_posted_intr(vmcs12)) { |
705699a1 WV |
10416 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
10417 | kunmap(vmx->nested.pi_desc_page); | |
53a70daf | 10418 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
5e2f30b7 | 10419 | vmx->nested.pi_desc_page = NULL; |
705699a1 | 10420 | } |
5e2f30b7 DH |
10421 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
10422 | if (is_error_page(page)) | |
6beb7bd5 | 10423 | return; |
5e2f30b7 DH |
10424 | vmx->nested.pi_desc_page = page; |
10425 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); | |
705699a1 WV |
10426 | vmx->nested.pi_desc = |
10427 | (struct pi_desc *)((void *)vmx->nested.pi_desc + | |
10428 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
10429 | (PAGE_SIZE - 1))); | |
6beb7bd5 JM |
10430 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
10431 | page_to_phys(vmx->nested.pi_desc_page) + | |
10432 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
10433 | (PAGE_SIZE - 1))); | |
705699a1 | 10434 | } |
d4667ca1 | 10435 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
3712caeb KA |
10436 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
10437 | CPU_BASED_USE_MSR_BITMAPS); | |
6beb7bd5 JM |
10438 | else |
10439 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, | |
10440 | CPU_BASED_USE_MSR_BITMAPS); | |
a2bcba50 WL |
10441 | } |
10442 | ||
f4124500 JK |
10443 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
10444 | { | |
10445 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; | |
10446 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10447 | ||
10448 | if (vcpu->arch.virtual_tsc_khz == 0) | |
10449 | return; | |
10450 | ||
10451 | /* Make sure short timeouts reliably trigger an immediate vmexit. | |
10452 | * hrtimer_start does not guarantee this. */ | |
10453 | if (preemption_timeout <= 1) { | |
10454 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); | |
10455 | return; | |
10456 | } | |
10457 | ||
10458 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
10459 | preemption_timeout *= 1000000; | |
10460 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); | |
10461 | hrtimer_start(&vmx->nested.preemption_timer, | |
10462 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); | |
10463 | } | |
10464 | ||
56a20510 JM |
10465 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
10466 | struct vmcs12 *vmcs12) | |
10467 | { | |
10468 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) | |
10469 | return 0; | |
10470 | ||
10471 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || | |
10472 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) | |
10473 | return -EINVAL; | |
10474 | ||
10475 | return 0; | |
10476 | } | |
10477 | ||
3af18d9c WV |
10478 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
10479 | struct vmcs12 *vmcs12) | |
10480 | { | |
3af18d9c WV |
10481 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
10482 | return 0; | |
10483 | ||
5fa99cbe | 10484 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
3af18d9c WV |
10485 | return -EINVAL; |
10486 | ||
10487 | return 0; | |
10488 | } | |
10489 | ||
712b12d7 JM |
10490 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
10491 | struct vmcs12 *vmcs12) | |
10492 | { | |
10493 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
10494 | return 0; | |
10495 | ||
10496 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) | |
10497 | return -EINVAL; | |
10498 | ||
10499 | return 0; | |
10500 | } | |
10501 | ||
3af18d9c WV |
10502 | /* |
10503 | * Merge L0's and L1's MSR bitmap, return false to indicate that | |
10504 | * we do not use the hardware. | |
10505 | */ | |
c992384b PB |
10506 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
10507 | struct vmcs12 *vmcs12) | |
3af18d9c | 10508 | { |
82f0dd4b | 10509 | int msr; |
f2b93280 | 10510 | struct page *page; |
d048c098 | 10511 | unsigned long *msr_bitmap_l1; |
904e14fb | 10512 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
15d45071 | 10513 | /* |
d28b387f | 10514 | * pred_cmd & spec_ctrl are trying to verify two things: |
15d45071 AR |
10515 | * |
10516 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This | |
10517 | * ensures that we do not accidentally generate an L02 MSR bitmap | |
10518 | * from the L12 MSR bitmap that is too permissive. | |
10519 | * 2. That L1 or L2s have actually used the MSR. This avoids | |
10520 | * unnecessarily merging of the bitmap if the MSR is unused. This | |
10521 | * works properly because we only update the L01 MSR bitmap lazily. | |
10522 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only | |
10523 | * updated to reflect this when L1 (or its L2s) actually write to | |
10524 | * the MSR. | |
10525 | */ | |
206587a9 KA |
10526 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
10527 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); | |
f2b93280 | 10528 | |
c992384b PB |
10529 | /* Nothing to do if the MSR bitmap is not in use. */ |
10530 | if (!cpu_has_vmx_msr_bitmap() || | |
10531 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) | |
10532 | return false; | |
10533 | ||
15d45071 | 10534 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
d28b387f | 10535 | !pred_cmd && !spec_ctrl) |
f2b93280 WV |
10536 | return false; |
10537 | ||
5e2f30b7 DH |
10538 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
10539 | if (is_error_page(page)) | |
f2b93280 | 10540 | return false; |
f2b93280 | 10541 | |
c992384b PB |
10542 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
10543 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { | |
10544 | /* | |
10545 | * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it | |
10546 | * just lets the processor take the value from the virtual-APIC page; | |
10547 | * take those 256 bits directly from the L1 bitmap. | |
10548 | */ | |
10549 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { | |
10550 | unsigned word = msr / BITS_PER_LONG; | |
10551 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; | |
10552 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; | |
10553 | } | |
10554 | } else { | |
10555 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { | |
10556 | unsigned word = msr / BITS_PER_LONG; | |
10557 | msr_bitmap_l0[word] = ~0; | |
10558 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; | |
10559 | } | |
10560 | } | |
d048c098 | 10561 | |
c992384b PB |
10562 | nested_vmx_disable_intercept_for_msr( |
10563 | msr_bitmap_l1, msr_bitmap_l0, | |
d7231e75 | 10564 | X2APIC_MSR(APIC_TASKPRI), |
c992384b | 10565 | MSR_TYPE_W); |
d048c098 | 10566 | |
c992384b | 10567 | if (nested_cpu_has_vid(vmcs12)) { |
d048c098 | 10568 | nested_vmx_disable_intercept_for_msr( |
c992384b | 10569 | msr_bitmap_l1, msr_bitmap_l0, |
d7231e75 | 10570 | X2APIC_MSR(APIC_EOI), |
c992384b PB |
10571 | MSR_TYPE_W); |
10572 | nested_vmx_disable_intercept_for_msr( | |
10573 | msr_bitmap_l1, msr_bitmap_l0, | |
d7231e75 | 10574 | X2APIC_MSR(APIC_SELF_IPI), |
c992384b | 10575 | MSR_TYPE_W); |
82f0dd4b | 10576 | } |
15d45071 | 10577 | |
d28b387f KA |
10578 | if (spec_ctrl) |
10579 | nested_vmx_disable_intercept_for_msr( | |
10580 | msr_bitmap_l1, msr_bitmap_l0, | |
10581 | MSR_IA32_SPEC_CTRL, | |
10582 | MSR_TYPE_R | MSR_TYPE_W); | |
10583 | ||
15d45071 AR |
10584 | if (pred_cmd) |
10585 | nested_vmx_disable_intercept_for_msr( | |
10586 | msr_bitmap_l1, msr_bitmap_l0, | |
10587 | MSR_IA32_PRED_CMD, | |
10588 | MSR_TYPE_W); | |
10589 | ||
f2b93280 | 10590 | kunmap(page); |
53a70daf | 10591 | kvm_release_page_clean(page); |
f2b93280 WV |
10592 | |
10593 | return true; | |
10594 | } | |
10595 | ||
f0f4cf5b KS |
10596 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
10597 | struct vmcs12 *vmcs12) | |
10598 | { | |
10599 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && | |
10600 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) | |
10601 | return -EINVAL; | |
10602 | else | |
10603 | return 0; | |
10604 | } | |
10605 | ||
f2b93280 WV |
10606 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
10607 | struct vmcs12 *vmcs12) | |
10608 | { | |
82f0dd4b | 10609 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
608406e2 | 10610 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
705699a1 WV |
10611 | !nested_cpu_has_vid(vmcs12) && |
10612 | !nested_cpu_has_posted_intr(vmcs12)) | |
f2b93280 WV |
10613 | return 0; |
10614 | ||
10615 | /* | |
10616 | * If virtualize x2apic mode is enabled, | |
10617 | * virtualize apic access must be disabled. | |
10618 | */ | |
82f0dd4b WV |
10619 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
10620 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
f2b93280 WV |
10621 | return -EINVAL; |
10622 | ||
608406e2 WV |
10623 | /* |
10624 | * If virtual interrupt delivery is enabled, | |
10625 | * we must exit on external interrupts. | |
10626 | */ | |
10627 | if (nested_cpu_has_vid(vmcs12) && | |
10628 | !nested_exit_on_intr(vcpu)) | |
10629 | return -EINVAL; | |
10630 | ||
705699a1 WV |
10631 | /* |
10632 | * bits 15:8 should be zero in posted_intr_nv, | |
10633 | * the descriptor address has been already checked | |
10634 | * in nested_get_vmcs12_pages. | |
10635 | */ | |
10636 | if (nested_cpu_has_posted_intr(vmcs12) && | |
10637 | (!nested_cpu_has_vid(vmcs12) || | |
10638 | !nested_exit_intr_ack_set(vcpu) || | |
10639 | vmcs12->posted_intr_nv & 0xff00)) | |
10640 | return -EINVAL; | |
10641 | ||
f2b93280 WV |
10642 | /* tpr shadow is needed by all apicv features. */ |
10643 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
10644 | return -EINVAL; | |
10645 | ||
10646 | return 0; | |
3af18d9c WV |
10647 | } |
10648 | ||
e9ac033e EK |
10649 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
10650 | unsigned long count_field, | |
92d71bc6 | 10651 | unsigned long addr_field) |
ff651cb6 | 10652 | { |
92d71bc6 | 10653 | int maxphyaddr; |
e9ac033e EK |
10654 | u64 count, addr; |
10655 | ||
10656 | if (vmcs12_read_any(vcpu, count_field, &count) || | |
10657 | vmcs12_read_any(vcpu, addr_field, &addr)) { | |
10658 | WARN_ON(1); | |
10659 | return -EINVAL; | |
10660 | } | |
10661 | if (count == 0) | |
10662 | return 0; | |
92d71bc6 | 10663 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
e9ac033e EK |
10664 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
10665 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { | |
bbe41b95 | 10666 | pr_debug_ratelimited( |
e9ac033e EK |
10667 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
10668 | addr_field, maxphyaddr, count, addr); | |
10669 | return -EINVAL; | |
10670 | } | |
10671 | return 0; | |
10672 | } | |
10673 | ||
10674 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, | |
10675 | struct vmcs12 *vmcs12) | |
10676 | { | |
e9ac033e EK |
10677 | if (vmcs12->vm_exit_msr_load_count == 0 && |
10678 | vmcs12->vm_exit_msr_store_count == 0 && | |
10679 | vmcs12->vm_entry_msr_load_count == 0) | |
10680 | return 0; /* Fast path */ | |
e9ac033e | 10681 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
92d71bc6 | 10682 | VM_EXIT_MSR_LOAD_ADDR) || |
e9ac033e | 10683 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
92d71bc6 | 10684 | VM_EXIT_MSR_STORE_ADDR) || |
e9ac033e | 10685 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
92d71bc6 | 10686 | VM_ENTRY_MSR_LOAD_ADDR)) |
e9ac033e EK |
10687 | return -EINVAL; |
10688 | return 0; | |
10689 | } | |
10690 | ||
c5f983f6 BD |
10691 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
10692 | struct vmcs12 *vmcs12) | |
10693 | { | |
10694 | u64 address = vmcs12->pml_address; | |
10695 | int maxphyaddr = cpuid_maxphyaddr(vcpu); | |
10696 | ||
10697 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { | |
10698 | if (!nested_cpu_has_ept(vmcs12) || | |
10699 | !IS_ALIGNED(address, 4096) || | |
10700 | address >> maxphyaddr) | |
10701 | return -EINVAL; | |
10702 | } | |
10703 | ||
10704 | return 0; | |
10705 | } | |
10706 | ||
e9ac033e EK |
10707 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
10708 | struct vmx_msr_entry *e) | |
10709 | { | |
10710 | /* x2APIC MSR accesses are not allowed */ | |
8a9781f7 | 10711 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
e9ac033e EK |
10712 | return -EINVAL; |
10713 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ | |
10714 | e->index == MSR_IA32_UCODE_REV) | |
10715 | return -EINVAL; | |
10716 | if (e->reserved != 0) | |
ff651cb6 WV |
10717 | return -EINVAL; |
10718 | return 0; | |
10719 | } | |
10720 | ||
e9ac033e EK |
10721 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
10722 | struct vmx_msr_entry *e) | |
ff651cb6 WV |
10723 | { |
10724 | if (e->index == MSR_FS_BASE || | |
10725 | e->index == MSR_GS_BASE || | |
e9ac033e EK |
10726 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
10727 | nested_vmx_msr_check_common(vcpu, e)) | |
10728 | return -EINVAL; | |
10729 | return 0; | |
10730 | } | |
10731 | ||
10732 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, | |
10733 | struct vmx_msr_entry *e) | |
10734 | { | |
10735 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ | |
10736 | nested_vmx_msr_check_common(vcpu, e)) | |
ff651cb6 WV |
10737 | return -EINVAL; |
10738 | return 0; | |
10739 | } | |
10740 | ||
10741 | /* | |
10742 | * Load guest's/host's msr at nested entry/exit. | |
10743 | * return 0 for success, entry index for failure. | |
10744 | */ | |
10745 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
10746 | { | |
10747 | u32 i; | |
10748 | struct vmx_msr_entry e; | |
10749 | struct msr_data msr; | |
10750 | ||
10751 | msr.host_initiated = false; | |
10752 | for (i = 0; i < count; i++) { | |
54bf36aa PB |
10753 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
10754 | &e, sizeof(e))) { | |
bbe41b95 | 10755 | pr_debug_ratelimited( |
e9ac033e EK |
10756 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
10757 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 10758 | goto fail; |
e9ac033e EK |
10759 | } |
10760 | if (nested_vmx_load_msr_check(vcpu, &e)) { | |
bbe41b95 | 10761 | pr_debug_ratelimited( |
e9ac033e EK |
10762 | "%s check failed (%u, 0x%x, 0x%x)\n", |
10763 | __func__, i, e.index, e.reserved); | |
10764 | goto fail; | |
10765 | } | |
ff651cb6 WV |
10766 | msr.index = e.index; |
10767 | msr.data = e.value; | |
e9ac033e | 10768 | if (kvm_set_msr(vcpu, &msr)) { |
bbe41b95 | 10769 | pr_debug_ratelimited( |
e9ac033e EK |
10770 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
10771 | __func__, i, e.index, e.value); | |
ff651cb6 | 10772 | goto fail; |
e9ac033e | 10773 | } |
ff651cb6 WV |
10774 | } |
10775 | return 0; | |
10776 | fail: | |
10777 | return i + 1; | |
10778 | } | |
10779 | ||
10780 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
10781 | { | |
10782 | u32 i; | |
10783 | struct vmx_msr_entry e; | |
10784 | ||
10785 | for (i = 0; i < count; i++) { | |
609e36d3 | 10786 | struct msr_data msr_info; |
54bf36aa PB |
10787 | if (kvm_vcpu_read_guest(vcpu, |
10788 | gpa + i * sizeof(e), | |
10789 | &e, 2 * sizeof(u32))) { | |
bbe41b95 | 10790 | pr_debug_ratelimited( |
e9ac033e EK |
10791 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
10792 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 10793 | return -EINVAL; |
e9ac033e EK |
10794 | } |
10795 | if (nested_vmx_store_msr_check(vcpu, &e)) { | |
bbe41b95 | 10796 | pr_debug_ratelimited( |
e9ac033e EK |
10797 | "%s check failed (%u, 0x%x, 0x%x)\n", |
10798 | __func__, i, e.index, e.reserved); | |
ff651cb6 | 10799 | return -EINVAL; |
e9ac033e | 10800 | } |
609e36d3 PB |
10801 | msr_info.host_initiated = false; |
10802 | msr_info.index = e.index; | |
10803 | if (kvm_get_msr(vcpu, &msr_info)) { | |
bbe41b95 | 10804 | pr_debug_ratelimited( |
e9ac033e EK |
10805 | "%s cannot read MSR (%u, 0x%x)\n", |
10806 | __func__, i, e.index); | |
10807 | return -EINVAL; | |
10808 | } | |
54bf36aa PB |
10809 | if (kvm_vcpu_write_guest(vcpu, |
10810 | gpa + i * sizeof(e) + | |
10811 | offsetof(struct vmx_msr_entry, value), | |
10812 | &msr_info.data, sizeof(msr_info.data))) { | |
bbe41b95 | 10813 | pr_debug_ratelimited( |
e9ac033e | 10814 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
609e36d3 | 10815 | __func__, i, e.index, msr_info.data); |
e9ac033e EK |
10816 | return -EINVAL; |
10817 | } | |
ff651cb6 WV |
10818 | } |
10819 | return 0; | |
10820 | } | |
10821 | ||
1dc35dac LP |
10822 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
10823 | { | |
10824 | unsigned long invalid_mask; | |
10825 | ||
10826 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); | |
10827 | return (val & invalid_mask) == 0; | |
10828 | } | |
10829 | ||
9ed38ffa LP |
10830 | /* |
10831 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are | |
10832 | * emulating VM entry into a guest with EPT enabled. | |
10833 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code | |
10834 | * is assigned to entry_failure_code on failure. | |
10835 | */ | |
10836 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept, | |
ca0bde28 | 10837 | u32 *entry_failure_code) |
9ed38ffa | 10838 | { |
9ed38ffa | 10839 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
1dc35dac | 10840 | if (!nested_cr3_valid(vcpu, cr3)) { |
9ed38ffa LP |
10841 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
10842 | return 1; | |
10843 | } | |
10844 | ||
10845 | /* | |
10846 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and | |
10847 | * must not be dereferenced. | |
10848 | */ | |
10849 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && | |
10850 | !nested_ept) { | |
10851 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { | |
10852 | *entry_failure_code = ENTRY_FAIL_PDPTE; | |
10853 | return 1; | |
10854 | } | |
10855 | } | |
10856 | ||
10857 | vcpu->arch.cr3 = cr3; | |
10858 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
10859 | } | |
10860 | ||
10861 | kvm_mmu_reset_context(vcpu); | |
10862 | return 0; | |
10863 | } | |
10864 | ||
74a497fa PB |
10865 | static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
10866 | bool from_vmentry) | |
fe3ef05c NHE |
10867 | { |
10868 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
fe3ef05c NHE |
10869 | |
10870 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); | |
fe3ef05c NHE |
10871 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
10872 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); | |
10873 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); | |
10874 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); | |
10875 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); | |
10876 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); | |
10877 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); | |
fe3ef05c NHE |
10878 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
10879 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); | |
10880 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); | |
10881 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); | |
10882 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); | |
10883 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); | |
10884 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); | |
10885 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); | |
10886 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); | |
fe3ef05c NHE |
10887 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
10888 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); | |
10889 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); | |
10890 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); | |
10891 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); | |
10892 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); | |
fe3ef05c NHE |
10893 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
10894 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); | |
10895 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); | |
10896 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); | |
10897 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); | |
10898 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); | |
10899 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); | |
10900 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); | |
10901 | ||
25a2e4fe PB |
10902 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
10903 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, | |
10904 | vmcs12->guest_pending_dbg_exceptions); | |
10905 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); | |
10906 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); | |
10907 | ||
10908 | if (nested_cpu_has_xsaves(vmcs12)) | |
10909 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); | |
10910 | vmcs_write64(VMCS_LINK_POINTER, -1ull); | |
10911 | ||
10912 | if (cpu_has_vmx_posted_intr()) | |
10913 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); | |
10914 | ||
10915 | /* | |
10916 | * Whether page-faults are trapped is determined by a combination of | |
10917 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. | |
10918 | * If enable_ept, L0 doesn't care about page faults and we should | |
10919 | * set all of these to L1's desires. However, if !enable_ept, L0 does | |
10920 | * care about (at least some) page faults, and because it is not easy | |
10921 | * (if at all possible?) to merge L0 and L1's desires, we simply ask | |
10922 | * to exit on each and every L2 page fault. This is done by setting | |
10923 | * MASK=MATCH=0 and (see below) EB.PF=1. | |
10924 | * Note that below we don't need special code to set EB.PF beyond the | |
10925 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, | |
10926 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when | |
10927 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. | |
10928 | */ | |
10929 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, | |
10930 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); | |
10931 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, | |
10932 | enable_ept ? vmcs12->page_fault_error_code_match : 0); | |
10933 | ||
10934 | /* All VMFUNCs are currently emulated through L0 vmexits. */ | |
10935 | if (cpu_has_vmx_vmfunc()) | |
10936 | vmcs_write64(VM_FUNCTION_CONTROL, 0); | |
10937 | ||
10938 | if (cpu_has_vmx_apicv()) { | |
10939 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); | |
10940 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); | |
10941 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); | |
10942 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); | |
10943 | } | |
10944 | ||
10945 | /* | |
10946 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) | |
10947 | * Some constant fields are set here by vmx_set_constant_host_state(). | |
10948 | * Other fields are different per CPU, and will be set later when | |
10949 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. | |
10950 | */ | |
10951 | vmx_set_constant_host_state(vmx); | |
10952 | ||
10953 | /* | |
10954 | * Set the MSR load/store lists to match L0's settings. | |
10955 | */ | |
10956 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); | |
10957 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
10958 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); | |
10959 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
10960 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); | |
10961 | ||
10962 | set_cr4_guest_host_mask(vmx); | |
10963 | ||
10964 | if (vmx_mpx_supported()) | |
10965 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); | |
10966 | ||
10967 | if (enable_vpid) { | |
10968 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) | |
10969 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); | |
10970 | else | |
10971 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
10972 | } | |
10973 | ||
10974 | /* | |
10975 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 | |
10976 | */ | |
10977 | if (enable_ept) { | |
10978 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); | |
10979 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); | |
10980 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); | |
10981 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); | |
10982 | } | |
80132f4c RK |
10983 | |
10984 | if (cpu_has_vmx_msr_bitmap()) | |
10985 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); | |
8665c3f9 PB |
10986 | } |
10987 | ||
10988 | /* | |
10989 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested | |
10990 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it | |
10991 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 | |
10992 | * guest in a way that will both be appropriate to L1's requests, and our | |
10993 | * needs. In addition to modifying the active vmcs (which is vmcs02), this | |
10994 | * function also has additional necessary side-effects, like setting various | |
10995 | * vcpu->arch fields. | |
10996 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code | |
10997 | * is assigned to entry_failure_code on failure. | |
10998 | */ | |
10999 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
11000 | bool from_vmentry, u32 *entry_failure_code) | |
11001 | { | |
11002 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
11003 | u32 exec_control, vmcs12_exec_ctrl; | |
11004 | ||
9d1887ef SC |
11005 | if (vmx->nested.dirty_vmcs12) { |
11006 | prepare_vmcs02_full(vcpu, vmcs12, from_vmentry); | |
11007 | vmx->nested.dirty_vmcs12 = false; | |
11008 | } | |
11009 | ||
8665c3f9 PB |
11010 | /* |
11011 | * First, the fields that are shadowed. This must be kept in sync | |
11012 | * with vmx_shadow_fields.h. | |
11013 | */ | |
11014 | ||
11015 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); | |
11016 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); | |
11017 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); | |
11018 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); | |
11019 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); | |
11020 | ||
11021 | /* | |
11022 | * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR, | |
11023 | * HOST_FS_BASE, HOST_GS_BASE. | |
11024 | */ | |
fe3ef05c | 11025 | |
cf8b84f4 JM |
11026 | if (from_vmentry && |
11027 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { | |
2996fca0 JK |
11028 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
11029 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); | |
11030 | } else { | |
11031 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); | |
11032 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); | |
11033 | } | |
cf8b84f4 JM |
11034 | if (from_vmentry) { |
11035 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, | |
11036 | vmcs12->vm_entry_intr_info_field); | |
11037 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, | |
11038 | vmcs12->vm_entry_exception_error_code); | |
11039 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
11040 | vmcs12->vm_entry_instruction_len); | |
11041 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, | |
11042 | vmcs12->guest_interruptibility_info); | |
2d6144e3 WL |
11043 | vmx->loaded_vmcs->nmi_known_unmasked = |
11044 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); | |
cf8b84f4 JM |
11045 | } else { |
11046 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
11047 | } | |
63fbf59f | 11048 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
fe3ef05c | 11049 | |
f4124500 | 11050 | exec_control = vmcs12->pin_based_vm_exec_control; |
9314006d PB |
11051 | |
11052 | /* Preemption timer setting is only taken from vmcs01. */ | |
705699a1 | 11053 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
9314006d PB |
11054 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
11055 | if (vmx->hv_deadline_tsc == -1) | |
11056 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
705699a1 | 11057 | |
9314006d | 11058 | /* Posted interrupts setting is only taken from vmcs12. */ |
705699a1 | 11059 | if (nested_cpu_has_posted_intr(vmcs12)) { |
705699a1 WV |
11060 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
11061 | vmx->nested.pi_pending = false; | |
6beb7bd5 | 11062 | } else { |
705699a1 | 11063 | exec_control &= ~PIN_BASED_POSTED_INTR; |
6beb7bd5 | 11064 | } |
705699a1 | 11065 | |
f4124500 | 11066 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
fe3ef05c | 11067 | |
f4124500 JK |
11068 | vmx->nested.preemption_timer_expired = false; |
11069 | if (nested_cpu_has_preemption_timer(vmcs12)) | |
11070 | vmx_start_preemption_timer(vcpu); | |
0238ea91 | 11071 | |
fe3ef05c | 11072 | if (cpu_has_secondary_exec_ctrls()) { |
80154d77 | 11073 | exec_control = vmx->secondary_exec_control; |
e2821620 | 11074 | |
fe3ef05c | 11075 | /* Take the following fields only from vmcs12 */ |
696dfd95 | 11076 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
90a2db6d | 11077 | SECONDARY_EXEC_ENABLE_INVPCID | |
b3a2a907 | 11078 | SECONDARY_EXEC_RDTSCP | |
3db13480 | 11079 | SECONDARY_EXEC_XSAVES | |
696dfd95 | 11080 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
27c42a1b BD |
11081 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
11082 | SECONDARY_EXEC_ENABLE_VMFUNC); | |
fe3ef05c | 11083 | if (nested_cpu_has(vmcs12, |
03efce6f BD |
11084 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
11085 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & | |
11086 | ~SECONDARY_EXEC_ENABLE_PML; | |
11087 | exec_control |= vmcs12_exec_ctrl; | |
11088 | } | |
fe3ef05c | 11089 | |
25a2e4fe | 11090 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
608406e2 WV |
11091 | vmcs_write16(GUEST_INTR_STATUS, |
11092 | vmcs12->guest_intr_status); | |
608406e2 | 11093 | |
6beb7bd5 JM |
11094 | /* |
11095 | * Write an illegal value to APIC_ACCESS_ADDR. Later, | |
11096 | * nested_get_vmcs12_pages will either fix it up or | |
11097 | * remove the VM execution control. | |
11098 | */ | |
11099 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) | |
11100 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); | |
11101 | ||
fe3ef05c NHE |
11102 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
11103 | } | |
11104 | ||
fe3ef05c NHE |
11105 | /* |
11106 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before | |
11107 | * entry, but only if the current (host) sp changed from the value | |
11108 | * we wrote last (vmx->host_rsp). This cache is no longer relevant | |
11109 | * if we switch vmcs, and rather than hold a separate cache per vmcs, | |
11110 | * here we just force the write to happen on entry. | |
11111 | */ | |
11112 | vmx->host_rsp = 0; | |
11113 | ||
11114 | exec_control = vmx_exec_control(vmx); /* L0's desires */ | |
11115 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; | |
11116 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; | |
11117 | exec_control &= ~CPU_BASED_TPR_SHADOW; | |
11118 | exec_control |= vmcs12->cpu_based_vm_exec_control; | |
a7c0b07d | 11119 | |
6beb7bd5 JM |
11120 | /* |
11121 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if | |
11122 | * nested_get_vmcs12_pages can't fix it up, the illegal value | |
11123 | * will result in a VM entry failure. | |
11124 | */ | |
a7c0b07d | 11125 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
6beb7bd5 | 11126 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
a7c0b07d | 11127 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
51aa68e7 JM |
11128 | } else { |
11129 | #ifdef CONFIG_X86_64 | |
11130 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | | |
11131 | CPU_BASED_CR8_STORE_EXITING; | |
11132 | #endif | |
a7c0b07d WL |
11133 | } |
11134 | ||
fe3ef05c | 11135 | /* |
8eb73e2d QX |
11136 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
11137 | * for I/O port accesses. | |
fe3ef05c | 11138 | */ |
fe3ef05c NHE |
11139 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
11140 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; | |
11141 | ||
11142 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); | |
11143 | ||
11144 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the | |
11145 | * bitwise-or of what L1 wants to trap for L2, and what we want to | |
11146 | * trap. Note that CR0.TS also needs updating - we do this later. | |
11147 | */ | |
11148 | update_exception_bitmap(vcpu); | |
11149 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; | |
11150 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); | |
11151 | ||
8049d651 NHE |
11152 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
11153 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER | |
11154 | * bits are further modified by vmx_set_efer() below. | |
11155 | */ | |
f4124500 | 11156 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
8049d651 NHE |
11157 | |
11158 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are | |
11159 | * emulated by vmx_set_efer(), below. | |
11160 | */ | |
2961e876 | 11161 | vm_entry_controls_init(vmx, |
8049d651 NHE |
11162 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
11163 | ~VM_ENTRY_IA32E_MODE) | | |
fe3ef05c NHE |
11164 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
11165 | ||
cf8b84f4 JM |
11166 | if (from_vmentry && |
11167 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { | |
fe3ef05c | 11168 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
44811c02 | 11169 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
cf8b84f4 | 11170 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
fe3ef05c | 11171 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
cf8b84f4 | 11172 | } |
fe3ef05c | 11173 | |
e79f245d KA |
11174 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
11175 | ||
c95ba92a PF |
11176 | if (kvm_has_tsc_control) |
11177 | decache_tsc_multiplier(vmx); | |
fe3ef05c NHE |
11178 | |
11179 | if (enable_vpid) { | |
11180 | /* | |
5c614b35 WL |
11181 | * There is no direct mapping between vpid02 and vpid12, the |
11182 | * vpid02 is per-vCPU for L0 and reused while the value of | |
11183 | * vpid12 is changed w/ one invvpid during nested vmentry. | |
11184 | * The vpid12 is allocated by L1 for L2, so it will not | |
11185 | * influence global bitmap(for vpid01 and vpid02 allocation) | |
11186 | * even if spawn a lot of nested vCPUs. | |
fe3ef05c | 11187 | */ |
5c614b35 | 11188 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
5c614b35 WL |
11189 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
11190 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; | |
c2ba05cc | 11191 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true); |
5c614b35 WL |
11192 | } |
11193 | } else { | |
c2ba05cc | 11194 | vmx_flush_tlb(vcpu, true); |
5c614b35 | 11195 | } |
fe3ef05c NHE |
11196 | } |
11197 | ||
1fb883bb LP |
11198 | if (enable_pml) { |
11199 | /* | |
11200 | * Conceptually we want to copy the PML address and index from | |
11201 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, | |
11202 | * since we always flush the log on each vmexit, this happens | |
11203 | * to be equivalent to simply resetting the fields in vmcs02. | |
11204 | */ | |
11205 | ASSERT(vmx->pml_pg); | |
11206 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
11207 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
11208 | } | |
11209 | ||
155a97a3 | 11210 | if (nested_cpu_has_ept(vmcs12)) { |
ae1e2d10 PB |
11211 | if (nested_ept_init_mmu_context(vcpu)) { |
11212 | *entry_failure_code = ENTRY_FAIL_DEFAULT; | |
11213 | return 1; | |
11214 | } | |
fb6c8198 JM |
11215 | } else if (nested_cpu_has2(vmcs12, |
11216 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
a468f2db | 11217 | vmx_flush_tlb(vcpu, true); |
155a97a3 NHE |
11218 | } |
11219 | ||
fe3ef05c | 11220 | /* |
bd7e5b08 PB |
11221 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
11222 | * bits which we consider mandatory enabled. | |
fe3ef05c NHE |
11223 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
11224 | * the specifications by L1; It's not enough to take | |
11225 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we | |
11226 | * have more bits than L1 expected. | |
11227 | */ | |
11228 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); | |
11229 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); | |
11230 | ||
11231 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); | |
11232 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); | |
11233 | ||
cf8b84f4 JM |
11234 | if (from_vmentry && |
11235 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) | |
5a6a9748 DM |
11236 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
11237 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) | |
11238 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); | |
11239 | else | |
11240 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
11241 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ | |
11242 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
11243 | ||
2bb8cafe SC |
11244 | /* |
11245 | * Guest state is invalid and unrestricted guest is disabled, | |
11246 | * which means L1 attempted VMEntry to L2 with invalid state. | |
11247 | * Fail the VMEntry. | |
11248 | */ | |
3184a995 PB |
11249 | if (vmx->emulation_required) { |
11250 | *entry_failure_code = ENTRY_FAIL_DEFAULT; | |
2bb8cafe | 11251 | return 1; |
3184a995 | 11252 | } |
2bb8cafe | 11253 | |
9ed38ffa | 11254 | /* Shadow page tables on either EPT or shadow page tables. */ |
7ad658b6 | 11255 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
9ed38ffa LP |
11256 | entry_failure_code)) |
11257 | return 1; | |
7ca29de2 | 11258 | |
feaf0c7d GN |
11259 | if (!enable_ept) |
11260 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; | |
11261 | ||
fe3ef05c NHE |
11262 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
11263 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); | |
ee146c1c | 11264 | return 0; |
fe3ef05c NHE |
11265 | } |
11266 | ||
0c7f650e KS |
11267 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
11268 | { | |
11269 | if (!nested_cpu_has_nmi_exiting(vmcs12) && | |
11270 | nested_cpu_has_virtual_nmis(vmcs12)) | |
11271 | return -EINVAL; | |
11272 | ||
11273 | if (!nested_cpu_has_virtual_nmis(vmcs12) && | |
11274 | nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)) | |
11275 | return -EINVAL; | |
11276 | ||
11277 | return 0; | |
11278 | } | |
11279 | ||
ca0bde28 | 11280 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
cd232ad0 | 11281 | { |
cd232ad0 | 11282 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
7c177938 | 11283 | |
6dfacadd | 11284 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
ca0bde28 JM |
11285 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
11286 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
26539bd0 | 11287 | |
56a20510 JM |
11288 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
11289 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11290 | ||
ca0bde28 JM |
11291 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
11292 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
7c177938 | 11293 | |
f0f4cf5b KS |
11294 | if (nested_vmx_check_apic_access_controls(vcpu, vmcs12)) |
11295 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11296 | ||
712b12d7 JM |
11297 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
11298 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11299 | ||
ca0bde28 JM |
11300 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
11301 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
f2b93280 | 11302 | |
ca0bde28 JM |
11303 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
11304 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
e9ac033e | 11305 | |
c5f983f6 BD |
11306 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
11307 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11308 | ||
7c177938 | 11309 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
6677f3da PB |
11310 | vmx->nested.msrs.procbased_ctls_low, |
11311 | vmx->nested.msrs.procbased_ctls_high) || | |
2e5b0bd9 JM |
11312 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
11313 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, | |
6677f3da PB |
11314 | vmx->nested.msrs.secondary_ctls_low, |
11315 | vmx->nested.msrs.secondary_ctls_high)) || | |
7c177938 | 11316 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
6677f3da PB |
11317 | vmx->nested.msrs.pinbased_ctls_low, |
11318 | vmx->nested.msrs.pinbased_ctls_high) || | |
7c177938 | 11319 | !vmx_control_verify(vmcs12->vm_exit_controls, |
6677f3da PB |
11320 | vmx->nested.msrs.exit_ctls_low, |
11321 | vmx->nested.msrs.exit_ctls_high) || | |
7c177938 | 11322 | !vmx_control_verify(vmcs12->vm_entry_controls, |
6677f3da PB |
11323 | vmx->nested.msrs.entry_ctls_low, |
11324 | vmx->nested.msrs.entry_ctls_high)) | |
ca0bde28 | 11325 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
7c177938 | 11326 | |
0c7f650e | 11327 | if (nested_vmx_check_nmi_controls(vmcs12)) |
ca0bde28 | 11328 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
7c177938 | 11329 | |
41ab9372 BD |
11330 | if (nested_cpu_has_vmfunc(vmcs12)) { |
11331 | if (vmcs12->vm_function_control & | |
6677f3da | 11332 | ~vmx->nested.msrs.vmfunc_controls) |
41ab9372 BD |
11333 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
11334 | ||
11335 | if (nested_cpu_has_eptp_switching(vmcs12)) { | |
11336 | if (!nested_cpu_has_ept(vmcs12) || | |
11337 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) | |
11338 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11339 | } | |
11340 | } | |
27c42a1b | 11341 | |
c7c2c709 JM |
11342 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
11343 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11344 | ||
3899152c | 11345 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
1dc35dac | 11346 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
ca0bde28 JM |
11347 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
11348 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; | |
11349 | ||
11350 | return 0; | |
11351 | } | |
11352 | ||
11353 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
11354 | u32 *exit_qual) | |
11355 | { | |
11356 | bool ia32e; | |
11357 | ||
11358 | *exit_qual = ENTRY_FAIL_DEFAULT; | |
7c177938 | 11359 | |
3899152c | 11360 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
ca0bde28 | 11361 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
b428018a | 11362 | return 1; |
ca0bde28 JM |
11363 | |
11364 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) && | |
11365 | vmcs12->vmcs_link_pointer != -1ull) { | |
11366 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; | |
b428018a | 11367 | return 1; |
7c177938 NHE |
11368 | } |
11369 | ||
384bb783 | 11370 | /* |
cb0c8cda | 11371 | * If the load IA32_EFER VM-entry control is 1, the following checks |
384bb783 JK |
11372 | * are performed on the field for the IA32_EFER MSR: |
11373 | * - Bits reserved in the IA32_EFER MSR must be 0. | |
11374 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of | |
11375 | * the IA-32e mode guest VM-exit control. It must also be identical | |
11376 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to | |
11377 | * CR0.PG) is 1. | |
11378 | */ | |
ca0bde28 JM |
11379 | if (to_vmx(vcpu)->nested.nested_run_pending && |
11380 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { | |
384bb783 JK |
11381 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
11382 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || | |
11383 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || | |
11384 | ((vmcs12->guest_cr0 & X86_CR0_PG) && | |
ca0bde28 | 11385 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
b428018a | 11386 | return 1; |
384bb783 JK |
11387 | } |
11388 | ||
11389 | /* | |
11390 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the | |
11391 | * IA32_EFER MSR must be 0 in the field for that register. In addition, | |
11392 | * the values of the LMA and LME bits in the field must each be that of | |
11393 | * the host address-space size VM-exit control. | |
11394 | */ | |
11395 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { | |
11396 | ia32e = (vmcs12->vm_exit_controls & | |
11397 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; | |
11398 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || | |
11399 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || | |
ca0bde28 | 11400 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
b428018a | 11401 | return 1; |
ca0bde28 JM |
11402 | } |
11403 | ||
f1b026a3 WL |
11404 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
11405 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || | |
11406 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) | |
11407 | return 1; | |
11408 | ||
ca0bde28 JM |
11409 | return 0; |
11410 | } | |
11411 | ||
858e25c0 JM |
11412 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry) |
11413 | { | |
11414 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
11415 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
858e25c0 JM |
11416 | u32 msr_entry_idx; |
11417 | u32 exit_qual; | |
e79f245d | 11418 | int r; |
858e25c0 | 11419 | |
858e25c0 JM |
11420 | enter_guest_mode(vcpu); |
11421 | ||
11422 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) | |
11423 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
11424 | ||
de3a0021 | 11425 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
858e25c0 JM |
11426 | vmx_segment_cache_clear(vmx); |
11427 | ||
e79f245d KA |
11428 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
11429 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; | |
11430 | ||
11431 | r = EXIT_REASON_INVALID_STATE; | |
11432 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) | |
11433 | goto fail; | |
858e25c0 JM |
11434 | |
11435 | nested_get_vmcs12_pages(vcpu, vmcs12); | |
11436 | ||
e79f245d | 11437 | r = EXIT_REASON_MSR_LOAD_FAIL; |
858e25c0 JM |
11438 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
11439 | vmcs12->vm_entry_msr_load_addr, | |
11440 | vmcs12->vm_entry_msr_load_count); | |
e79f245d KA |
11441 | if (msr_entry_idx) |
11442 | goto fail; | |
858e25c0 | 11443 | |
858e25c0 JM |
11444 | /* |
11445 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point | |
11446 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet | |
11447 | * returned as far as L1 is concerned. It will only return (and set | |
11448 | * the success flag) when L2 exits (see nested_vmx_vmexit()). | |
11449 | */ | |
11450 | return 0; | |
e79f245d KA |
11451 | |
11452 | fail: | |
11453 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) | |
11454 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; | |
11455 | leave_guest_mode(vcpu); | |
11456 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); | |
11457 | nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual); | |
11458 | return 1; | |
858e25c0 JM |
11459 | } |
11460 | ||
ca0bde28 JM |
11461 | /* |
11462 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 | |
11463 | * for running an L2 nested guest. | |
11464 | */ | |
11465 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) | |
11466 | { | |
11467 | struct vmcs12 *vmcs12; | |
11468 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
b3f1dfb6 | 11469 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
ca0bde28 JM |
11470 | u32 exit_qual; |
11471 | int ret; | |
11472 | ||
11473 | if (!nested_vmx_check_permission(vcpu)) | |
11474 | return 1; | |
11475 | ||
11476 | if (!nested_vmx_check_vmcs12(vcpu)) | |
11477 | goto out; | |
11478 | ||
11479 | vmcs12 = get_vmcs12(vcpu); | |
11480 | ||
11481 | if (enable_shadow_vmcs) | |
11482 | copy_shadow_to_vmcs12(vmx); | |
11483 | ||
11484 | /* | |
11485 | * The nested entry process starts with enforcing various prerequisites | |
11486 | * on vmcs12 as required by the Intel SDM, and act appropriately when | |
11487 | * they fail: As the SDM explains, some conditions should cause the | |
11488 | * instruction to fail, while others will cause the instruction to seem | |
11489 | * to succeed, but return an EXIT_REASON_INVALID_STATE. | |
11490 | * To speed up the normal (success) code path, we should avoid checking | |
11491 | * for misconfigurations which will anyway be caught by the processor | |
11492 | * when using the merged vmcs02. | |
11493 | */ | |
b3f1dfb6 JM |
11494 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
11495 | nested_vmx_failValid(vcpu, | |
11496 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); | |
11497 | goto out; | |
11498 | } | |
11499 | ||
ca0bde28 JM |
11500 | if (vmcs12->launch_state == launch) { |
11501 | nested_vmx_failValid(vcpu, | |
11502 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS | |
11503 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); | |
11504 | goto out; | |
11505 | } | |
11506 | ||
11507 | ret = check_vmentry_prereqs(vcpu, vmcs12); | |
11508 | if (ret) { | |
11509 | nested_vmx_failValid(vcpu, ret); | |
11510 | goto out; | |
11511 | } | |
11512 | ||
11513 | /* | |
11514 | * After this point, the trap flag no longer triggers a singlestep trap | |
11515 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. | |
11516 | * This is not 100% correct; for performance reasons, we delegate most | |
11517 | * of the checks on host state to the processor. If those fail, | |
11518 | * the singlestep trap is missed. | |
11519 | */ | |
11520 | skip_emulated_instruction(vcpu); | |
11521 | ||
11522 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); | |
11523 | if (ret) { | |
11524 | nested_vmx_entry_failure(vcpu, vmcs12, | |
11525 | EXIT_REASON_INVALID_STATE, exit_qual); | |
11526 | return 1; | |
384bb783 JK |
11527 | } |
11528 | ||
7c177938 NHE |
11529 | /* |
11530 | * We're finally done with prerequisite checking, and can start with | |
11531 | * the nested entry. | |
11532 | */ | |
11533 | ||
858e25c0 JM |
11534 | ret = enter_vmx_non_root_mode(vcpu, true); |
11535 | if (ret) | |
11536 | return ret; | |
ff651cb6 | 11537 | |
135a06c3 CG |
11538 | /* |
11539 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken | |
11540 | * by event injection, halt vcpu. | |
11541 | */ | |
11542 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && | |
11543 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) | |
5cb56059 | 11544 | return kvm_vcpu_halt(vcpu); |
6dfacadd | 11545 | |
7af40ad3 JK |
11546 | vmx->nested.nested_run_pending = 1; |
11547 | ||
cd232ad0 | 11548 | return 1; |
eb277562 KH |
11549 | |
11550 | out: | |
6affcbed | 11551 | return kvm_skip_emulated_instruction(vcpu); |
cd232ad0 NHE |
11552 | } |
11553 | ||
4704d0be NHE |
11554 | /* |
11555 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date | |
11556 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). | |
11557 | * This function returns the new value we should put in vmcs12.guest_cr0. | |
11558 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, | |
11559 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now | |
11560 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 | |
11561 | * didn't trap the bit, because if L1 did, so would L0). | |
11562 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have | |
11563 | * been modified by L2, and L1 knows it. So just leave the old value of | |
11564 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 | |
11565 | * isn't relevant, because if L0 traps this bit it can set it to anything. | |
11566 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have | |
11567 | * changed these bits, and therefore they need to be updated, but L0 | |
11568 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather | |
11569 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. | |
11570 | */ | |
11571 | static inline unsigned long | |
11572 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
11573 | { | |
11574 | return | |
11575 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | | |
11576 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | | |
11577 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | | |
11578 | vcpu->arch.cr0_guest_owned_bits)); | |
11579 | } | |
11580 | ||
11581 | static inline unsigned long | |
11582 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
11583 | { | |
11584 | return | |
11585 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | | |
11586 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | | |
11587 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | | |
11588 | vcpu->arch.cr4_guest_owned_bits)); | |
11589 | } | |
11590 | ||
5f3d5799 JK |
11591 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
11592 | struct vmcs12 *vmcs12) | |
11593 | { | |
11594 | u32 idt_vectoring; | |
11595 | unsigned int nr; | |
11596 | ||
664f8e26 | 11597 | if (vcpu->arch.exception.injected) { |
5f3d5799 JK |
11598 | nr = vcpu->arch.exception.nr; |
11599 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
11600 | ||
11601 | if (kvm_exception_is_soft(nr)) { | |
11602 | vmcs12->vm_exit_instruction_len = | |
11603 | vcpu->arch.event_exit_inst_len; | |
11604 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; | |
11605 | } else | |
11606 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; | |
11607 | ||
11608 | if (vcpu->arch.exception.has_error_code) { | |
11609 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; | |
11610 | vmcs12->idt_vectoring_error_code = | |
11611 | vcpu->arch.exception.error_code; | |
11612 | } | |
11613 | ||
11614 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
cd2633c5 | 11615 | } else if (vcpu->arch.nmi_injected) { |
5f3d5799 JK |
11616 | vmcs12->idt_vectoring_info_field = |
11617 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; | |
04140b41 | 11618 | } else if (vcpu->arch.interrupt.injected) { |
5f3d5799 JK |
11619 | nr = vcpu->arch.interrupt.nr; |
11620 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
11621 | ||
11622 | if (vcpu->arch.interrupt.soft) { | |
11623 | idt_vectoring |= INTR_TYPE_SOFT_INTR; | |
11624 | vmcs12->vm_entry_instruction_len = | |
11625 | vcpu->arch.event_exit_inst_len; | |
11626 | } else | |
11627 | idt_vectoring |= INTR_TYPE_EXT_INTR; | |
11628 | ||
11629 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
11630 | } | |
11631 | } | |
11632 | ||
b6b8a145 JK |
11633 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
11634 | { | |
11635 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
bfcf83b1 | 11636 | unsigned long exit_qual; |
917dc606 LA |
11637 | bool block_nested_events = |
11638 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); | |
acc9ab60 | 11639 | |
bfcf83b1 WL |
11640 | if (vcpu->arch.exception.pending && |
11641 | nested_vmx_check_exception(vcpu, &exit_qual)) { | |
917dc606 | 11642 | if (block_nested_events) |
bfcf83b1 WL |
11643 | return -EBUSY; |
11644 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); | |
bfcf83b1 WL |
11645 | return 0; |
11646 | } | |
11647 | ||
f4124500 JK |
11648 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
11649 | vmx->nested.preemption_timer_expired) { | |
917dc606 | 11650 | if (block_nested_events) |
f4124500 JK |
11651 | return -EBUSY; |
11652 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); | |
11653 | return 0; | |
11654 | } | |
11655 | ||
b6b8a145 | 11656 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
917dc606 | 11657 | if (block_nested_events) |
b6b8a145 JK |
11658 | return -EBUSY; |
11659 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, | |
11660 | NMI_VECTOR | INTR_TYPE_NMI_INTR | | |
11661 | INTR_INFO_VALID_MASK, 0); | |
11662 | /* | |
11663 | * The NMI-triggered VM exit counts as injection: | |
11664 | * clear this one and block further NMIs. | |
11665 | */ | |
11666 | vcpu->arch.nmi_pending = 0; | |
11667 | vmx_set_nmi_mask(vcpu, true); | |
11668 | return 0; | |
11669 | } | |
11670 | ||
11671 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && | |
11672 | nested_exit_on_intr(vcpu)) { | |
917dc606 | 11673 | if (block_nested_events) |
b6b8a145 JK |
11674 | return -EBUSY; |
11675 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); | |
705699a1 | 11676 | return 0; |
b6b8a145 JK |
11677 | } |
11678 | ||
6342c50a DH |
11679 | vmx_complete_nested_posted_interrupt(vcpu); |
11680 | return 0; | |
b6b8a145 JK |
11681 | } |
11682 | ||
f4124500 JK |
11683 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
11684 | { | |
11685 | ktime_t remaining = | |
11686 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); | |
11687 | u64 value; | |
11688 | ||
11689 | if (ktime_to_ns(remaining) <= 0) | |
11690 | return 0; | |
11691 | ||
11692 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; | |
11693 | do_div(value, 1000000); | |
11694 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
11695 | } | |
11696 | ||
4704d0be | 11697 | /* |
cf8b84f4 JM |
11698 | * Update the guest state fields of vmcs12 to reflect changes that |
11699 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the | |
11700 | * VM-entry controls is also updated, since this is really a guest | |
11701 | * state bit.) | |
4704d0be | 11702 | */ |
cf8b84f4 | 11703 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
4704d0be | 11704 | { |
4704d0be NHE |
11705 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
11706 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); | |
11707 | ||
4704d0be NHE |
11708 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
11709 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); | |
11710 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); | |
11711 | ||
11712 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); | |
11713 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); | |
11714 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); | |
11715 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); | |
11716 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); | |
11717 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); | |
11718 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); | |
11719 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); | |
11720 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); | |
11721 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); | |
11722 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); | |
11723 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); | |
11724 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); | |
11725 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); | |
11726 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); | |
11727 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); | |
11728 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); | |
11729 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); | |
11730 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); | |
11731 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); | |
11732 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); | |
11733 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); | |
11734 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); | |
11735 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); | |
11736 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); | |
11737 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); | |
11738 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); | |
11739 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); | |
11740 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); | |
11741 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); | |
11742 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); | |
11743 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); | |
11744 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); | |
11745 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); | |
11746 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); | |
11747 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); | |
11748 | ||
4704d0be NHE |
11749 | vmcs12->guest_interruptibility_info = |
11750 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
11751 | vmcs12->guest_pending_dbg_exceptions = | |
11752 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); | |
3edf1e69 JK |
11753 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
11754 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; | |
11755 | else | |
11756 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; | |
4704d0be | 11757 | |
f4124500 JK |
11758 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
11759 | if (vmcs12->vm_exit_controls & | |
11760 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) | |
11761 | vmcs12->vmx_preemption_timer_value = | |
11762 | vmx_get_preemption_timer_value(vcpu); | |
11763 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); | |
11764 | } | |
7854cbca | 11765 | |
3633cfc3 NHE |
11766 | /* |
11767 | * In some cases (usually, nested EPT), L2 is allowed to change its | |
11768 | * own CR3 without exiting. If it has changed it, we must keep it. | |
11769 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined | |
11770 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. | |
11771 | * | |
11772 | * Additionally, restore L2's PDPTR to vmcs12. | |
11773 | */ | |
11774 | if (enable_ept) { | |
f3531054 | 11775 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
3633cfc3 NHE |
11776 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
11777 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); | |
11778 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); | |
11779 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); | |
11780 | } | |
11781 | ||
d281e13b | 11782 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
119a9c01 | 11783 | |
608406e2 WV |
11784 | if (nested_cpu_has_vid(vmcs12)) |
11785 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); | |
11786 | ||
c18911a2 JK |
11787 | vmcs12->vm_entry_controls = |
11788 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | | |
2961e876 | 11789 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
c18911a2 | 11790 | |
2996fca0 JK |
11791 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
11792 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); | |
11793 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
11794 | } | |
11795 | ||
4704d0be NHE |
11796 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
11797 | * the relevant bit asks not to trap the change */ | |
b8c07d55 | 11798 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
4704d0be | 11799 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
10ba54a5 JK |
11800 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
11801 | vmcs12->guest_ia32_efer = vcpu->arch.efer; | |
4704d0be NHE |
11802 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
11803 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); | |
11804 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); | |
a87036ad | 11805 | if (kvm_mpx_supported()) |
36be0b9d | 11806 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
cf8b84f4 JM |
11807 | } |
11808 | ||
11809 | /* | |
11810 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits | |
11811 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), | |
11812 | * and this function updates it to reflect the changes to the guest state while | |
11813 | * L2 was running (and perhaps made some exits which were handled directly by L0 | |
11814 | * without going back to L1), and to reflect the exit reason. | |
11815 | * Note that we do not have to copy here all VMCS fields, just those that | |
11816 | * could have changed by the L2 guest or the exit - i.e., the guest-state and | |
11817 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, | |
11818 | * which already writes to vmcs12 directly. | |
11819 | */ | |
11820 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
11821 | u32 exit_reason, u32 exit_intr_info, | |
11822 | unsigned long exit_qualification) | |
11823 | { | |
11824 | /* update guest state fields: */ | |
11825 | sync_vmcs12(vcpu, vmcs12); | |
4704d0be NHE |
11826 | |
11827 | /* update exit information fields: */ | |
11828 | ||
533558bc JK |
11829 | vmcs12->vm_exit_reason = exit_reason; |
11830 | vmcs12->exit_qualification = exit_qualification; | |
533558bc | 11831 | vmcs12->vm_exit_intr_info = exit_intr_info; |
7313c698 | 11832 | |
5f3d5799 | 11833 | vmcs12->idt_vectoring_info_field = 0; |
4704d0be NHE |
11834 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
11835 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
11836 | ||
5f3d5799 | 11837 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
7cdc2d62 JM |
11838 | vmcs12->launch_state = 1; |
11839 | ||
5f3d5799 JK |
11840 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
11841 | * instead of reading the real value. */ | |
4704d0be | 11842 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
5f3d5799 JK |
11843 | |
11844 | /* | |
11845 | * Transfer the event that L0 or L1 may wanted to inject into | |
11846 | * L2 to IDT_VECTORING_INFO_FIELD. | |
11847 | */ | |
11848 | vmcs12_save_pending_event(vcpu, vmcs12); | |
11849 | } | |
11850 | ||
11851 | /* | |
11852 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is | |
11853 | * preserved above and would only end up incorrectly in L1. | |
11854 | */ | |
11855 | vcpu->arch.nmi_injected = false; | |
11856 | kvm_clear_exception_queue(vcpu); | |
11857 | kvm_clear_interrupt_queue(vcpu); | |
4704d0be NHE |
11858 | } |
11859 | ||
5af41573 WL |
11860 | static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu, |
11861 | struct vmcs12 *vmcs12) | |
11862 | { | |
11863 | u32 entry_failure_code; | |
11864 | ||
11865 | nested_ept_uninit_mmu_context(vcpu); | |
11866 | ||
11867 | /* | |
11868 | * Only PDPTE load can fail as the value of cr3 was checked on entry and | |
11869 | * couldn't have changed. | |
11870 | */ | |
11871 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) | |
11872 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); | |
11873 | ||
11874 | if (!enable_ept) | |
11875 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; | |
11876 | } | |
11877 | ||
4704d0be NHE |
11878 | /* |
11879 | * A part of what we need to when the nested L2 guest exits and we want to | |
11880 | * run its L1 parent, is to reset L1's guest state to the host state specified | |
11881 | * in vmcs12. | |
11882 | * This function is to be called not only on normal nested exit, but also on | |
11883 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry | |
11884 | * Failures During or After Loading Guest State"). | |
11885 | * This function should be called when the active VMCS is L1's (vmcs01). | |
11886 | */ | |
733568f9 JK |
11887 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
11888 | struct vmcs12 *vmcs12) | |
4704d0be | 11889 | { |
21feb4eb ACL |
11890 | struct kvm_segment seg; |
11891 | ||
4704d0be NHE |
11892 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
11893 | vcpu->arch.efer = vmcs12->host_ia32_efer; | |
d1fa0352 | 11894 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
4704d0be NHE |
11895 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
11896 | else | |
11897 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
11898 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
11899 | ||
11900 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); | |
11901 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); | |
1adfa76a | 11902 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
4704d0be NHE |
11903 | /* |
11904 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't | |
bd7e5b08 PB |
11905 | * actually changed, because vmx_set_cr0 refers to efer set above. |
11906 | * | |
11907 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 | |
11908 | * (KVM doesn't change it); | |
4704d0be | 11909 | */ |
bd7e5b08 | 11910 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
9e3e4dbf | 11911 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
4704d0be | 11912 | |
bd7e5b08 | 11913 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
4704d0be | 11914 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
8eb3f87d | 11915 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
4704d0be | 11916 | |
5af41573 | 11917 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
feaf0c7d | 11918 | |
4704d0be NHE |
11919 | if (enable_vpid) { |
11920 | /* | |
11921 | * Trivially support vpid by letting L2s share their parent | |
11922 | * L1's vpid. TODO: move to a more elaborate solution, giving | |
11923 | * each L2 its own vpid and exposing the vpid feature to L1. | |
11924 | */ | |
c2ba05cc | 11925 | vmx_flush_tlb(vcpu, true); |
4704d0be | 11926 | } |
4704d0be NHE |
11927 | |
11928 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); | |
11929 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); | |
11930 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); | |
11931 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); | |
11932 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); | |
21f2d551 LP |
11933 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
11934 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); | |
4704d0be | 11935 | |
36be0b9d PB |
11936 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
11937 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) | |
11938 | vmcs_write64(GUEST_BNDCFGS, 0); | |
11939 | ||
44811c02 | 11940 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
4704d0be | 11941 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
44811c02 JK |
11942 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
11943 | } | |
4704d0be NHE |
11944 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
11945 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, | |
11946 | vmcs12->host_ia32_perf_global_ctrl); | |
503cd0c5 | 11947 | |
21feb4eb ACL |
11948 | /* Set L1 segment info according to Intel SDM |
11949 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ | |
11950 | seg = (struct kvm_segment) { | |
11951 | .base = 0, | |
11952 | .limit = 0xFFFFFFFF, | |
11953 | .selector = vmcs12->host_cs_selector, | |
11954 | .type = 11, | |
11955 | .present = 1, | |
11956 | .s = 1, | |
11957 | .g = 1 | |
11958 | }; | |
11959 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) | |
11960 | seg.l = 1; | |
11961 | else | |
11962 | seg.db = 1; | |
11963 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); | |
11964 | seg = (struct kvm_segment) { | |
11965 | .base = 0, | |
11966 | .limit = 0xFFFFFFFF, | |
11967 | .type = 3, | |
11968 | .present = 1, | |
11969 | .s = 1, | |
11970 | .db = 1, | |
11971 | .g = 1 | |
11972 | }; | |
11973 | seg.selector = vmcs12->host_ds_selector; | |
11974 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); | |
11975 | seg.selector = vmcs12->host_es_selector; | |
11976 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); | |
11977 | seg.selector = vmcs12->host_ss_selector; | |
11978 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); | |
11979 | seg.selector = vmcs12->host_fs_selector; | |
11980 | seg.base = vmcs12->host_fs_base; | |
11981 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); | |
11982 | seg.selector = vmcs12->host_gs_selector; | |
11983 | seg.base = vmcs12->host_gs_base; | |
11984 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); | |
11985 | seg = (struct kvm_segment) { | |
205befd9 | 11986 | .base = vmcs12->host_tr_base, |
21feb4eb ACL |
11987 | .limit = 0x67, |
11988 | .selector = vmcs12->host_tr_selector, | |
11989 | .type = 11, | |
11990 | .present = 1 | |
11991 | }; | |
11992 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); | |
11993 | ||
503cd0c5 JK |
11994 | kvm_set_dr(vcpu, 7, 0x400); |
11995 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
ff651cb6 | 11996 | |
3af18d9c | 11997 | if (cpu_has_vmx_msr_bitmap()) |
904e14fb | 11998 | vmx_update_msr_bitmap(vcpu); |
3af18d9c | 11999 | |
ff651cb6 WV |
12000 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
12001 | vmcs12->vm_exit_msr_load_count)) | |
12002 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); | |
4704d0be NHE |
12003 | } |
12004 | ||
12005 | /* | |
12006 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 | |
12007 | * and modify vmcs12 to make it see what it would expect to see there if | |
12008 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) | |
12009 | */ | |
533558bc JK |
12010 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
12011 | u32 exit_intr_info, | |
12012 | unsigned long exit_qualification) | |
4704d0be NHE |
12013 | { |
12014 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
4704d0be NHE |
12015 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
12016 | ||
5f3d5799 JK |
12017 | /* trying to cancel vmlaunch/vmresume is a bug */ |
12018 | WARN_ON_ONCE(vmx->nested.nested_run_pending); | |
12019 | ||
4f350c6d JM |
12020 | /* |
12021 | * The only expected VM-instruction error is "VM entry with | |
12022 | * invalid control field(s)." Anything else indicates a | |
12023 | * problem with L0. | |
12024 | */ | |
12025 | WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) != | |
12026 | VMXERR_ENTRY_INVALID_CONTROL_FIELD)); | |
12027 | ||
4704d0be | 12028 | leave_guest_mode(vcpu); |
4704d0be | 12029 | |
e79f245d KA |
12030 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
12031 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; | |
12032 | ||
4f350c6d | 12033 | if (likely(!vmx->fail)) { |
72e9cbdb LP |
12034 | if (exit_reason == -1) |
12035 | sync_vmcs12(vcpu, vmcs12); | |
12036 | else | |
12037 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, | |
12038 | exit_qualification); | |
ff651cb6 | 12039 | |
4f350c6d JM |
12040 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
12041 | vmcs12->vm_exit_msr_store_count)) | |
12042 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); | |
12043 | } | |
cf3215d9 | 12044 | |
1279a6b1 | 12045 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
8391ce44 PB |
12046 | vm_entry_controls_reset_shadow(vmx); |
12047 | vm_exit_controls_reset_shadow(vmx); | |
36c3cc42 JK |
12048 | vmx_segment_cache_clear(vmx); |
12049 | ||
9314006d | 12050 | /* Update any VMCS fields that might have changed while L2 ran */ |
83bafef1 JM |
12051 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
12052 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
ea26e4ec | 12053 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
9314006d PB |
12054 | if (vmx->hv_deadline_tsc == -1) |
12055 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
12056 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
12057 | else | |
12058 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
12059 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c95ba92a PF |
12060 | if (kvm_has_tsc_control) |
12061 | decache_tsc_multiplier(vmx); | |
4704d0be | 12062 | |
dccbfcf5 RK |
12063 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
12064 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; | |
12065 | vmx_set_virtual_x2apic_mode(vcpu, | |
12066 | vcpu->arch.apic_base & X2APIC_ENABLE); | |
fb6c8198 JM |
12067 | } else if (!nested_cpu_has_ept(vmcs12) && |
12068 | nested_cpu_has2(vmcs12, | |
12069 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
a468f2db | 12070 | vmx_flush_tlb(vcpu, true); |
dccbfcf5 | 12071 | } |
4704d0be NHE |
12072 | |
12073 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ | |
12074 | vmx->host_rsp = 0; | |
12075 | ||
12076 | /* Unpin physical memory we referred to in vmcs02 */ | |
12077 | if (vmx->nested.apic_access_page) { | |
53a70daf | 12078 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
48d89b92 | 12079 | vmx->nested.apic_access_page = NULL; |
4704d0be | 12080 | } |
a7c0b07d | 12081 | if (vmx->nested.virtual_apic_page) { |
53a70daf | 12082 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
48d89b92 | 12083 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 12084 | } |
705699a1 WV |
12085 | if (vmx->nested.pi_desc_page) { |
12086 | kunmap(vmx->nested.pi_desc_page); | |
53a70daf | 12087 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
705699a1 WV |
12088 | vmx->nested.pi_desc_page = NULL; |
12089 | vmx->nested.pi_desc = NULL; | |
12090 | } | |
4704d0be | 12091 | |
38b99173 TC |
12092 | /* |
12093 | * We are now running in L2, mmu_notifier will force to reload the | |
12094 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. | |
12095 | */ | |
c83b6d15 | 12096 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
38b99173 | 12097 | |
72e9cbdb | 12098 | if (enable_shadow_vmcs && exit_reason != -1) |
012f83cb | 12099 | vmx->nested.sync_shadow_vmcs = true; |
b6b8a145 JK |
12100 | |
12101 | /* in case we halted in L2 */ | |
12102 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
4f350c6d JM |
12103 | |
12104 | if (likely(!vmx->fail)) { | |
12105 | /* | |
12106 | * TODO: SDM says that with acknowledge interrupt on | |
12107 | * exit, bit 31 of the VM-exit interrupt information | |
12108 | * (valid interrupt) is always set to 1 on | |
12109 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't | |
12110 | * need kvm_cpu_has_interrupt(). See the commit | |
12111 | * message for details. | |
12112 | */ | |
12113 | if (nested_exit_intr_ack_set(vcpu) && | |
12114 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && | |
12115 | kvm_cpu_has_interrupt(vcpu)) { | |
12116 | int irq = kvm_cpu_get_interrupt(vcpu); | |
12117 | WARN_ON(irq < 0); | |
12118 | vmcs12->vm_exit_intr_info = irq | | |
12119 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; | |
12120 | } | |
12121 | ||
72e9cbdb LP |
12122 | if (exit_reason != -1) |
12123 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, | |
12124 | vmcs12->exit_qualification, | |
12125 | vmcs12->idt_vectoring_info_field, | |
12126 | vmcs12->vm_exit_intr_info, | |
12127 | vmcs12->vm_exit_intr_error_code, | |
12128 | KVM_ISA_VMX); | |
4f350c6d JM |
12129 | |
12130 | load_vmcs12_host_state(vcpu, vmcs12); | |
12131 | ||
12132 | return; | |
12133 | } | |
12134 | ||
12135 | /* | |
12136 | * After an early L2 VM-entry failure, we're now back | |
12137 | * in L1 which thinks it just finished a VMLAUNCH or | |
12138 | * VMRESUME instruction, so we need to set the failure | |
12139 | * flag and the VM-instruction error field of the VMCS | |
12140 | * accordingly. | |
12141 | */ | |
12142 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); | |
5af41573 WL |
12143 | |
12144 | load_vmcs12_mmu_host_state(vcpu, vmcs12); | |
12145 | ||
4f350c6d JM |
12146 | /* |
12147 | * The emulated instruction was already skipped in | |
12148 | * nested_vmx_run, but the updated RIP was never | |
12149 | * written back to the vmcs01. | |
12150 | */ | |
12151 | skip_emulated_instruction(vcpu); | |
12152 | vmx->fail = 0; | |
4704d0be NHE |
12153 | } |
12154 | ||
42124925 JK |
12155 | /* |
12156 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. | |
12157 | */ | |
12158 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) | |
12159 | { | |
2f707d97 WL |
12160 | if (is_guest_mode(vcpu)) { |
12161 | to_vmx(vcpu)->nested.nested_run_pending = 0; | |
533558bc | 12162 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
2f707d97 | 12163 | } |
42124925 JK |
12164 | free_nested(to_vmx(vcpu)); |
12165 | } | |
12166 | ||
7c177938 NHE |
12167 | /* |
12168 | * L1's failure to enter L2 is a subset of a normal exit, as explained in | |
12169 | * 23.7 "VM-entry failures during or after loading guest state" (this also | |
12170 | * lists the acceptable exit-reason and exit-qualification parameters). | |
12171 | * It should only be called before L2 actually succeeded to run, and when | |
12172 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). | |
12173 | */ | |
12174 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, | |
12175 | struct vmcs12 *vmcs12, | |
12176 | u32 reason, unsigned long qualification) | |
12177 | { | |
12178 | load_vmcs12_host_state(vcpu, vmcs12); | |
12179 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; | |
12180 | vmcs12->exit_qualification = qualification; | |
12181 | nested_vmx_succeed(vcpu); | |
012f83cb AG |
12182 | if (enable_shadow_vmcs) |
12183 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; | |
7c177938 NHE |
12184 | } |
12185 | ||
8a76d7f2 JR |
12186 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
12187 | struct x86_instruction_info *info, | |
12188 | enum x86_intercept_stage stage) | |
12189 | { | |
fb6d4d34 PB |
12190 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
12191 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; | |
12192 | ||
12193 | /* | |
12194 | * RDPID causes #UD if disabled through secondary execution controls. | |
12195 | * Because it is marked as EmulateOnUD, we need to intercept it here. | |
12196 | */ | |
12197 | if (info->intercept == x86_intercept_rdtscp && | |
12198 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { | |
12199 | ctxt->exception.vector = UD_VECTOR; | |
12200 | ctxt->exception.error_code_valid = false; | |
12201 | return X86EMUL_PROPAGATE_FAULT; | |
12202 | } | |
12203 | ||
12204 | /* TODO: check more intercepts... */ | |
8a76d7f2 JR |
12205 | return X86EMUL_CONTINUE; |
12206 | } | |
12207 | ||
64672c95 YJ |
12208 | #ifdef CONFIG_X86_64 |
12209 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ | |
12210 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, | |
12211 | u64 divisor, u64 *result) | |
12212 | { | |
12213 | u64 low = a << shift, high = a >> (64 - shift); | |
12214 | ||
12215 | /* To avoid the overflow on divq */ | |
12216 | if (high >= divisor) | |
12217 | return 1; | |
12218 | ||
12219 | /* Low hold the result, high hold rem which is discarded */ | |
12220 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : | |
12221 | "rm" (divisor), "0" (low), "1" (high)); | |
12222 | *result = low; | |
12223 | ||
12224 | return 0; | |
12225 | } | |
12226 | ||
12227 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) | |
12228 | { | |
386c6ddb KA |
12229 | struct vcpu_vmx *vmx; |
12230 | u64 tscl, guest_tscl, delta_tsc; | |
12231 | ||
12232 | if (kvm_mwait_in_guest(vcpu->kvm)) | |
12233 | return -EOPNOTSUPP; | |
12234 | ||
12235 | vmx = to_vmx(vcpu); | |
12236 | tscl = rdtsc(); | |
12237 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); | |
12238 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; | |
64672c95 YJ |
12239 | |
12240 | /* Convert to host delta tsc if tsc scaling is enabled */ | |
12241 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && | |
12242 | u64_shl_div_u64(delta_tsc, | |
12243 | kvm_tsc_scaling_ratio_frac_bits, | |
12244 | vcpu->arch.tsc_scaling_ratio, | |
12245 | &delta_tsc)) | |
12246 | return -ERANGE; | |
12247 | ||
12248 | /* | |
12249 | * If the delta tsc can't fit in the 32 bit after the multi shift, | |
12250 | * we can't use the preemption timer. | |
12251 | * It's possible that it fits on later vmentries, but checking | |
12252 | * on every vmentry is costly so we just use an hrtimer. | |
12253 | */ | |
12254 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) | |
12255 | return -ERANGE; | |
12256 | ||
12257 | vmx->hv_deadline_tsc = tscl + delta_tsc; | |
12258 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
12259 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c8533544 WL |
12260 | |
12261 | return delta_tsc == 0; | |
64672c95 YJ |
12262 | } |
12263 | ||
12264 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) | |
12265 | { | |
12266 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
12267 | vmx->hv_deadline_tsc = -1; | |
12268 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
12269 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
12270 | } | |
12271 | #endif | |
12272 | ||
48d89b92 | 12273 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
ae97a3b8 | 12274 | { |
b31c114b | 12275 | if (!kvm_pause_in_guest(vcpu->kvm)) |
b4a2d31d | 12276 | shrink_ple_window(vcpu); |
ae97a3b8 RK |
12277 | } |
12278 | ||
843e4330 KH |
12279 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
12280 | struct kvm_memory_slot *slot) | |
12281 | { | |
12282 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); | |
12283 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); | |
12284 | } | |
12285 | ||
12286 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, | |
12287 | struct kvm_memory_slot *slot) | |
12288 | { | |
12289 | kvm_mmu_slot_set_dirty(kvm, slot); | |
12290 | } | |
12291 | ||
12292 | static void vmx_flush_log_dirty(struct kvm *kvm) | |
12293 | { | |
12294 | kvm_flush_pml_buffers(kvm); | |
12295 | } | |
12296 | ||
c5f983f6 BD |
12297 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
12298 | { | |
12299 | struct vmcs12 *vmcs12; | |
12300 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
12301 | gpa_t gpa; | |
12302 | struct page *page = NULL; | |
12303 | u64 *pml_address; | |
12304 | ||
12305 | if (is_guest_mode(vcpu)) { | |
12306 | WARN_ON_ONCE(vmx->nested.pml_full); | |
12307 | ||
12308 | /* | |
12309 | * Check if PML is enabled for the nested guest. | |
12310 | * Whether eptp bit 6 is set is already checked | |
12311 | * as part of A/D emulation. | |
12312 | */ | |
12313 | vmcs12 = get_vmcs12(vcpu); | |
12314 | if (!nested_cpu_has_pml(vmcs12)) | |
12315 | return 0; | |
12316 | ||
4769886b | 12317 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
c5f983f6 BD |
12318 | vmx->nested.pml_full = true; |
12319 | return 1; | |
12320 | } | |
12321 | ||
12322 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; | |
12323 | ||
5e2f30b7 DH |
12324 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
12325 | if (is_error_page(page)) | |
c5f983f6 BD |
12326 | return 0; |
12327 | ||
12328 | pml_address = kmap(page); | |
12329 | pml_address[vmcs12->guest_pml_index--] = gpa; | |
12330 | kunmap(page); | |
53a70daf | 12331 | kvm_release_page_clean(page); |
c5f983f6 BD |
12332 | } |
12333 | ||
12334 | return 0; | |
12335 | } | |
12336 | ||
843e4330 KH |
12337 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
12338 | struct kvm_memory_slot *memslot, | |
12339 | gfn_t offset, unsigned long mask) | |
12340 | { | |
12341 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); | |
12342 | } | |
12343 | ||
cd39e117 PB |
12344 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
12345 | { | |
12346 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
12347 | struct pi_desc old, new; | |
12348 | unsigned int dest; | |
cd39e117 PB |
12349 | |
12350 | do { | |
12351 | old.control = new.control = pi_desc->control; | |
8b306e2f PB |
12352 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
12353 | "Wakeup handler not enabled while the VCPU is blocked\n"); | |
cd39e117 PB |
12354 | |
12355 | dest = cpu_physical_id(vcpu->cpu); | |
12356 | ||
12357 | if (x2apic_enabled()) | |
12358 | new.ndst = dest; | |
12359 | else | |
12360 | new.ndst = (dest << 8) & 0xFF00; | |
12361 | ||
cd39e117 PB |
12362 | /* set 'NV' to 'notification vector' */ |
12363 | new.nv = POSTED_INTR_VECTOR; | |
c0a1666b PB |
12364 | } while (cmpxchg64(&pi_desc->control, old.control, |
12365 | new.control) != old.control); | |
cd39e117 | 12366 | |
8b306e2f PB |
12367 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
12368 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
cd39e117 | 12369 | list_del(&vcpu->blocked_vcpu_list); |
8b306e2f | 12370 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
cd39e117 PB |
12371 | vcpu->pre_pcpu = -1; |
12372 | } | |
12373 | } | |
12374 | ||
bf9f6ac8 FW |
12375 | /* |
12376 | * This routine does the following things for vCPU which is going | |
12377 | * to be blocked if VT-d PI is enabled. | |
12378 | * - Store the vCPU to the wakeup list, so when interrupts happen | |
12379 | * we can find the right vCPU to wake up. | |
12380 | * - Change the Posted-interrupt descriptor as below: | |
12381 | * 'NDST' <-- vcpu->pre_pcpu | |
12382 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR | |
12383 | * - If 'ON' is set during this process, which means at least one | |
12384 | * interrupt is posted for this vCPU, we cannot block it, in | |
12385 | * this case, return 1, otherwise, return 0. | |
12386 | * | |
12387 | */ | |
bc22512b | 12388 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
bf9f6ac8 | 12389 | { |
bf9f6ac8 FW |
12390 | unsigned int dest; |
12391 | struct pi_desc old, new; | |
12392 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
12393 | ||
12394 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
12395 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
12396 | !kvm_vcpu_apicv_active(vcpu)) | |
bf9f6ac8 FW |
12397 | return 0; |
12398 | ||
8b306e2f PB |
12399 | WARN_ON(irqs_disabled()); |
12400 | local_irq_disable(); | |
12401 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { | |
12402 | vcpu->pre_pcpu = vcpu->cpu; | |
12403 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
12404 | list_add_tail(&vcpu->blocked_vcpu_list, | |
12405 | &per_cpu(blocked_vcpu_on_cpu, | |
12406 | vcpu->pre_pcpu)); | |
12407 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
12408 | } | |
bf9f6ac8 FW |
12409 | |
12410 | do { | |
12411 | old.control = new.control = pi_desc->control; | |
12412 | ||
bf9f6ac8 FW |
12413 | WARN((pi_desc->sn == 1), |
12414 | "Warning: SN field of posted-interrupts " | |
12415 | "is set before blocking\n"); | |
12416 | ||
12417 | /* | |
12418 | * Since vCPU can be preempted during this process, | |
12419 | * vcpu->cpu could be different with pre_pcpu, we | |
12420 | * need to set pre_pcpu as the destination of wakeup | |
12421 | * notification event, then we can find the right vCPU | |
12422 | * to wakeup in wakeup handler if interrupts happen | |
12423 | * when the vCPU is in blocked state. | |
12424 | */ | |
12425 | dest = cpu_physical_id(vcpu->pre_pcpu); | |
12426 | ||
12427 | if (x2apic_enabled()) | |
12428 | new.ndst = dest; | |
12429 | else | |
12430 | new.ndst = (dest << 8) & 0xFF00; | |
12431 | ||
12432 | /* set 'NV' to 'wakeup vector' */ | |
12433 | new.nv = POSTED_INTR_WAKEUP_VECTOR; | |
c0a1666b PB |
12434 | } while (cmpxchg64(&pi_desc->control, old.control, |
12435 | new.control) != old.control); | |
bf9f6ac8 | 12436 | |
8b306e2f PB |
12437 | /* We should not block the vCPU if an interrupt is posted for it. */ |
12438 | if (pi_test_on(pi_desc) == 1) | |
12439 | __pi_post_block(vcpu); | |
12440 | ||
12441 | local_irq_enable(); | |
12442 | return (vcpu->pre_pcpu == -1); | |
bf9f6ac8 FW |
12443 | } |
12444 | ||
bc22512b YJ |
12445 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
12446 | { | |
12447 | if (pi_pre_block(vcpu)) | |
12448 | return 1; | |
12449 | ||
64672c95 YJ |
12450 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
12451 | kvm_lapic_switch_to_sw_timer(vcpu); | |
12452 | ||
bc22512b YJ |
12453 | return 0; |
12454 | } | |
12455 | ||
12456 | static void pi_post_block(struct kvm_vcpu *vcpu) | |
bf9f6ac8 | 12457 | { |
8b306e2f | 12458 | if (vcpu->pre_pcpu == -1) |
bf9f6ac8 FW |
12459 | return; |
12460 | ||
8b306e2f PB |
12461 | WARN_ON(irqs_disabled()); |
12462 | local_irq_disable(); | |
cd39e117 | 12463 | __pi_post_block(vcpu); |
8b306e2f | 12464 | local_irq_enable(); |
bf9f6ac8 FW |
12465 | } |
12466 | ||
bc22512b YJ |
12467 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
12468 | { | |
64672c95 YJ |
12469 | if (kvm_x86_ops->set_hv_timer) |
12470 | kvm_lapic_switch_to_hv_timer(vcpu); | |
12471 | ||
bc22512b YJ |
12472 | pi_post_block(vcpu); |
12473 | } | |
12474 | ||
efc64404 FW |
12475 | /* |
12476 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts | |
12477 | * | |
12478 | * @kvm: kvm | |
12479 | * @host_irq: host irq of the interrupt | |
12480 | * @guest_irq: gsi of the interrupt | |
12481 | * @set: set or unset PI | |
12482 | * returns 0 on success, < 0 on failure | |
12483 | */ | |
12484 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, | |
12485 | uint32_t guest_irq, bool set) | |
12486 | { | |
12487 | struct kvm_kernel_irq_routing_entry *e; | |
12488 | struct kvm_irq_routing_table *irq_rt; | |
12489 | struct kvm_lapic_irq irq; | |
12490 | struct kvm_vcpu *vcpu; | |
12491 | struct vcpu_data vcpu_info; | |
3a8b0677 | 12492 | int idx, ret = 0; |
efc64404 FW |
12493 | |
12494 | if (!kvm_arch_has_assigned_device(kvm) || | |
a0052191 YZ |
12495 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
12496 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) | |
efc64404 FW |
12497 | return 0; |
12498 | ||
12499 | idx = srcu_read_lock(&kvm->irq_srcu); | |
12500 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); | |
3a8b0677 JS |
12501 | if (guest_irq >= irq_rt->nr_rt_entries || |
12502 | hlist_empty(&irq_rt->map[guest_irq])) { | |
12503 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", | |
12504 | guest_irq, irq_rt->nr_rt_entries); | |
12505 | goto out; | |
12506 | } | |
efc64404 FW |
12507 | |
12508 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { | |
12509 | if (e->type != KVM_IRQ_ROUTING_MSI) | |
12510 | continue; | |
12511 | /* | |
12512 | * VT-d PI cannot support posting multicast/broadcast | |
12513 | * interrupts to a vCPU, we still use interrupt remapping | |
12514 | * for these kind of interrupts. | |
12515 | * | |
12516 | * For lowest-priority interrupts, we only support | |
12517 | * those with single CPU as the destination, e.g. user | |
12518 | * configures the interrupts via /proc/irq or uses | |
12519 | * irqbalance to make the interrupts single-CPU. | |
12520 | * | |
12521 | * We will support full lowest-priority interrupt later. | |
12522 | */ | |
12523 | ||
37131313 | 12524 | kvm_set_msi_irq(kvm, e, &irq); |
23a1c257 FW |
12525 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
12526 | /* | |
12527 | * Make sure the IRTE is in remapped mode if | |
12528 | * we don't handle it in posted mode. | |
12529 | */ | |
12530 | ret = irq_set_vcpu_affinity(host_irq, NULL); | |
12531 | if (ret < 0) { | |
12532 | printk(KERN_INFO | |
12533 | "failed to back to remapped mode, irq: %u\n", | |
12534 | host_irq); | |
12535 | goto out; | |
12536 | } | |
12537 | ||
efc64404 | 12538 | continue; |
23a1c257 | 12539 | } |
efc64404 FW |
12540 | |
12541 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); | |
12542 | vcpu_info.vector = irq.vector; | |
12543 | ||
2698d82e | 12544 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
efc64404 FW |
12545 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
12546 | ||
12547 | if (set) | |
12548 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); | |
dc91f2eb | 12549 | else |
efc64404 | 12550 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
efc64404 FW |
12551 | |
12552 | if (ret < 0) { | |
12553 | printk(KERN_INFO "%s: failed to update PI IRTE\n", | |
12554 | __func__); | |
12555 | goto out; | |
12556 | } | |
12557 | } | |
12558 | ||
12559 | ret = 0; | |
12560 | out: | |
12561 | srcu_read_unlock(&kvm->irq_srcu, idx); | |
12562 | return ret; | |
12563 | } | |
12564 | ||
c45dcc71 AR |
12565 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
12566 | { | |
12567 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) | |
12568 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
12569 | FEATURE_CONTROL_LMCE; | |
12570 | else | |
12571 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
12572 | ~FEATURE_CONTROL_LMCE; | |
12573 | } | |
12574 | ||
72d7b374 LP |
12575 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
12576 | { | |
72e9cbdb LP |
12577 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
12578 | if (to_vmx(vcpu)->nested.nested_run_pending) | |
12579 | return 0; | |
72d7b374 LP |
12580 | return 1; |
12581 | } | |
12582 | ||
0234bf88 LP |
12583 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
12584 | { | |
72e9cbdb LP |
12585 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
12586 | ||
12587 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); | |
12588 | if (vmx->nested.smm.guest_mode) | |
12589 | nested_vmx_vmexit(vcpu, -1, 0, 0); | |
12590 | ||
12591 | vmx->nested.smm.vmxon = vmx->nested.vmxon; | |
12592 | vmx->nested.vmxon = false; | |
caa057a2 | 12593 | vmx_clear_hlt(vcpu); |
0234bf88 LP |
12594 | return 0; |
12595 | } | |
12596 | ||
12597 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) | |
12598 | { | |
72e9cbdb LP |
12599 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
12600 | int ret; | |
12601 | ||
12602 | if (vmx->nested.smm.vmxon) { | |
12603 | vmx->nested.vmxon = true; | |
12604 | vmx->nested.smm.vmxon = false; | |
12605 | } | |
12606 | ||
12607 | if (vmx->nested.smm.guest_mode) { | |
12608 | vcpu->arch.hflags &= ~HF_SMM_MASK; | |
12609 | ret = enter_vmx_non_root_mode(vcpu, false); | |
12610 | vcpu->arch.hflags |= HF_SMM_MASK; | |
12611 | if (ret) | |
12612 | return ret; | |
12613 | ||
12614 | vmx->nested.smm.guest_mode = false; | |
12615 | } | |
0234bf88 LP |
12616 | return 0; |
12617 | } | |
12618 | ||
cc3d967f LP |
12619 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
12620 | { | |
12621 | return 0; | |
12622 | } | |
12623 | ||
404f6aac | 12624 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
6aa8b732 AK |
12625 | .cpu_has_kvm_support = cpu_has_kvm_support, |
12626 | .disabled_by_bios = vmx_disabled_by_bios, | |
12627 | .hardware_setup = hardware_setup, | |
12628 | .hardware_unsetup = hardware_unsetup, | |
002c7f7c | 12629 | .check_processor_compatibility = vmx_check_processor_compat, |
6aa8b732 AK |
12630 | .hardware_enable = hardware_enable, |
12631 | .hardware_disable = hardware_disable, | |
04547156 | 12632 | .cpu_has_accelerated_tpr = report_flexpriority, |
6d396b55 | 12633 | .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, |
6aa8b732 | 12634 | |
b31c114b | 12635 | .vm_init = vmx_vm_init, |
434a1e94 SC |
12636 | .vm_alloc = vmx_vm_alloc, |
12637 | .vm_free = vmx_vm_free, | |
b31c114b | 12638 | |
6aa8b732 AK |
12639 | .vcpu_create = vmx_create_vcpu, |
12640 | .vcpu_free = vmx_free_vcpu, | |
04d2cc77 | 12641 | .vcpu_reset = vmx_vcpu_reset, |
6aa8b732 | 12642 | |
04d2cc77 | 12643 | .prepare_guest_switch = vmx_save_host_state, |
6aa8b732 AK |
12644 | .vcpu_load = vmx_vcpu_load, |
12645 | .vcpu_put = vmx_vcpu_put, | |
12646 | ||
a96036b8 | 12647 | .update_bp_intercept = update_exception_bitmap, |
801e459a | 12648 | .get_msr_feature = vmx_get_msr_feature, |
6aa8b732 AK |
12649 | .get_msr = vmx_get_msr, |
12650 | .set_msr = vmx_set_msr, | |
12651 | .get_segment_base = vmx_get_segment_base, | |
12652 | .get_segment = vmx_get_segment, | |
12653 | .set_segment = vmx_set_segment, | |
2e4d2653 | 12654 | .get_cpl = vmx_get_cpl, |
6aa8b732 | 12655 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
e8467fda | 12656 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
aff48baa | 12657 | .decache_cr3 = vmx_decache_cr3, |
25c4c276 | 12658 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
6aa8b732 | 12659 | .set_cr0 = vmx_set_cr0, |
6aa8b732 AK |
12660 | .set_cr3 = vmx_set_cr3, |
12661 | .set_cr4 = vmx_set_cr4, | |
6aa8b732 | 12662 | .set_efer = vmx_set_efer, |
6aa8b732 AK |
12663 | .get_idt = vmx_get_idt, |
12664 | .set_idt = vmx_set_idt, | |
12665 | .get_gdt = vmx_get_gdt, | |
12666 | .set_gdt = vmx_set_gdt, | |
73aaf249 JK |
12667 | .get_dr6 = vmx_get_dr6, |
12668 | .set_dr6 = vmx_set_dr6, | |
020df079 | 12669 | .set_dr7 = vmx_set_dr7, |
81908bf4 | 12670 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
5fdbf976 | 12671 | .cache_reg = vmx_cache_reg, |
6aa8b732 AK |
12672 | .get_rflags = vmx_get_rflags, |
12673 | .set_rflags = vmx_set_rflags, | |
be94f6b7 | 12674 | |
6aa8b732 | 12675 | .tlb_flush = vmx_flush_tlb, |
6aa8b732 | 12676 | |
6aa8b732 | 12677 | .run = vmx_vcpu_run, |
6062d012 | 12678 | .handle_exit = vmx_handle_exit, |
6aa8b732 | 12679 | .skip_emulated_instruction = skip_emulated_instruction, |
2809f5d2 GC |
12680 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
12681 | .get_interrupt_shadow = vmx_get_interrupt_shadow, | |
102d8325 | 12682 | .patch_hypercall = vmx_patch_hypercall, |
2a8067f1 | 12683 | .set_irq = vmx_inject_irq, |
95ba8273 | 12684 | .set_nmi = vmx_inject_nmi, |
298101da | 12685 | .queue_exception = vmx_queue_exception, |
b463a6f7 | 12686 | .cancel_injection = vmx_cancel_injection, |
78646121 | 12687 | .interrupt_allowed = vmx_interrupt_allowed, |
95ba8273 | 12688 | .nmi_allowed = vmx_nmi_allowed, |
3cfc3092 JK |
12689 | .get_nmi_mask = vmx_get_nmi_mask, |
12690 | .set_nmi_mask = vmx_set_nmi_mask, | |
95ba8273 GN |
12691 | .enable_nmi_window = enable_nmi_window, |
12692 | .enable_irq_window = enable_irq_window, | |
12693 | .update_cr8_intercept = update_cr8_intercept, | |
8d14695f | 12694 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
38b99173 | 12695 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
d62caabb AS |
12696 | .get_enable_apicv = vmx_get_enable_apicv, |
12697 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, | |
c7c9c56c | 12698 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
967235d3 | 12699 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
c7c9c56c YZ |
12700 | .hwapic_irr_update = vmx_hwapic_irr_update, |
12701 | .hwapic_isr_update = vmx_hwapic_isr_update, | |
a20ed54d YZ |
12702 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
12703 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, | |
95ba8273 | 12704 | |
cbc94022 | 12705 | .set_tss_addr = vmx_set_tss_addr, |
2ac52ab8 | 12706 | .set_identity_map_addr = vmx_set_identity_map_addr, |
67253af5 | 12707 | .get_tdp_level = get_ept_level, |
4b12f0de | 12708 | .get_mt_mask = vmx_get_mt_mask, |
229456fc | 12709 | |
586f9607 | 12710 | .get_exit_info = vmx_get_exit_info, |
586f9607 | 12711 | |
17cc3935 | 12712 | .get_lpage_level = vmx_get_lpage_level, |
0e851880 SY |
12713 | |
12714 | .cpuid_update = vmx_cpuid_update, | |
4e47c7a6 SY |
12715 | |
12716 | .rdtscp_supported = vmx_rdtscp_supported, | |
ad756a16 | 12717 | .invpcid_supported = vmx_invpcid_supported, |
d4330ef2 JR |
12718 | |
12719 | .set_supported_cpuid = vmx_set_supported_cpuid, | |
f5f48ee1 SY |
12720 | |
12721 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, | |
99e3e30a | 12722 | |
e79f245d | 12723 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
99e3e30a | 12724 | .write_tsc_offset = vmx_write_tsc_offset, |
1c97f0a0 JR |
12725 | |
12726 | .set_tdp_cr3 = vmx_set_cr3, | |
8a76d7f2 JR |
12727 | |
12728 | .check_intercept = vmx_check_intercept, | |
a547c6db | 12729 | .handle_external_intr = vmx_handle_external_intr, |
da8999d3 | 12730 | .mpx_supported = vmx_mpx_supported, |
55412b2e | 12731 | .xsaves_supported = vmx_xsaves_supported, |
66336cab | 12732 | .umip_emulated = vmx_umip_emulated, |
b6b8a145 JK |
12733 | |
12734 | .check_nested_events = vmx_check_nested_events, | |
ae97a3b8 RK |
12735 | |
12736 | .sched_in = vmx_sched_in, | |
843e4330 KH |
12737 | |
12738 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, | |
12739 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, | |
12740 | .flush_log_dirty = vmx_flush_log_dirty, | |
12741 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, | |
c5f983f6 | 12742 | .write_log_dirty = vmx_write_pml_buffer, |
25462f7f | 12743 | |
bf9f6ac8 FW |
12744 | .pre_block = vmx_pre_block, |
12745 | .post_block = vmx_post_block, | |
12746 | ||
25462f7f | 12747 | .pmu_ops = &intel_pmu_ops, |
efc64404 FW |
12748 | |
12749 | .update_pi_irte = vmx_update_pi_irte, | |
64672c95 YJ |
12750 | |
12751 | #ifdef CONFIG_X86_64 | |
12752 | .set_hv_timer = vmx_set_hv_timer, | |
12753 | .cancel_hv_timer = vmx_cancel_hv_timer, | |
12754 | #endif | |
c45dcc71 AR |
12755 | |
12756 | .setup_mce = vmx_setup_mce, | |
0234bf88 | 12757 | |
72d7b374 | 12758 | .smi_allowed = vmx_smi_allowed, |
0234bf88 LP |
12759 | .pre_enter_smm = vmx_pre_enter_smm, |
12760 | .pre_leave_smm = vmx_pre_leave_smm, | |
cc3d967f | 12761 | .enable_smi_window = enable_smi_window, |
6aa8b732 AK |
12762 | }; |
12763 | ||
12764 | static int __init vmx_init(void) | |
12765 | { | |
773e8a04 VK |
12766 | int r; |
12767 | ||
12768 | #if IS_ENABLED(CONFIG_HYPERV) | |
12769 | /* | |
12770 | * Enlightened VMCS usage should be recommended and the host needs | |
12771 | * to support eVMCS v1 or above. We can also disable eVMCS support | |
12772 | * with module parameter. | |
12773 | */ | |
12774 | if (enlightened_vmcs && | |
12775 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && | |
12776 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= | |
12777 | KVM_EVMCS_VERSION) { | |
12778 | int cpu; | |
12779 | ||
12780 | /* Check that we have assist pages on all online CPUs */ | |
12781 | for_each_online_cpu(cpu) { | |
12782 | if (!hv_get_vp_assist_page(cpu)) { | |
12783 | enlightened_vmcs = false; | |
12784 | break; | |
12785 | } | |
12786 | } | |
12787 | ||
12788 | if (enlightened_vmcs) { | |
12789 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); | |
12790 | static_branch_enable(&enable_evmcs); | |
12791 | } | |
12792 | } else { | |
12793 | enlightened_vmcs = false; | |
12794 | } | |
12795 | #endif | |
12796 | ||
12797 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), | |
34a1cd60 | 12798 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
fdef3ad1 | 12799 | if (r) |
34a1cd60 | 12800 | return r; |
25c5f225 | 12801 | |
2965faa5 | 12802 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
12803 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
12804 | crash_vmclear_local_loaded_vmcss); | |
12805 | #endif | |
12806 | ||
fdef3ad1 | 12807 | return 0; |
6aa8b732 AK |
12808 | } |
12809 | ||
12810 | static void __exit vmx_exit(void) | |
12811 | { | |
2965faa5 | 12812 | #ifdef CONFIG_KEXEC_CORE |
3b63a43f | 12813 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
8f536b76 ZY |
12814 | synchronize_rcu(); |
12815 | #endif | |
12816 | ||
cb498ea2 | 12817 | kvm_exit(); |
773e8a04 VK |
12818 | |
12819 | #if IS_ENABLED(CONFIG_HYPERV) | |
12820 | if (static_branch_unlikely(&enable_evmcs)) { | |
12821 | int cpu; | |
12822 | struct hv_vp_assist_page *vp_ap; | |
12823 | /* | |
12824 | * Reset everything to support using non-enlightened VMCS | |
12825 | * access later (e.g. when we reload the module with | |
12826 | * enlightened_vmcs=0) | |
12827 | */ | |
12828 | for_each_online_cpu(cpu) { | |
12829 | vp_ap = hv_get_vp_assist_page(cpu); | |
12830 | ||
12831 | if (!vp_ap) | |
12832 | continue; | |
12833 | ||
12834 | vp_ap->current_nested_vmcs = 0; | |
12835 | vp_ap->enlighten_vmentry = 0; | |
12836 | } | |
12837 | ||
12838 | static_branch_disable(&enable_evmcs); | |
12839 | } | |
12840 | #endif | |
6aa8b732 AK |
12841 | } |
12842 | ||
12843 | module_init(vmx_init) | |
12844 | module_exit(vmx_exit) |