Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * | |
4 | * This module enables machines with Intel VT-x extensions to run virtual | |
5 | * machines without emulation or binary translation. | |
6 | * | |
7 | * Copyright (C) 2006 Qumranet, Inc. | |
9611c187 | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
6aa8b732 AK |
9 | * |
10 | * Authors: | |
11 | * Avi Kivity <avi@qumranet.com> | |
12 | * Yaniv Kamay <yaniv@qumranet.com> | |
13 | * | |
14 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
15 | * the COPYING file in the top-level directory. | |
16 | * | |
17 | */ | |
18 | ||
85f455f7 | 19 | #include "irq.h" |
1d737c8a | 20 | #include "mmu.h" |
00b27a3e | 21 | #include "cpuid.h" |
d62caabb | 22 | #include "lapic.h" |
e495606d | 23 | |
edf88417 | 24 | #include <linux/kvm_host.h> |
6aa8b732 | 25 | #include <linux/module.h> |
9d8f549d | 26 | #include <linux/kernel.h> |
6aa8b732 AK |
27 | #include <linux/mm.h> |
28 | #include <linux/highmem.h> | |
e8edc6e0 | 29 | #include <linux/sched.h> |
c7addb90 | 30 | #include <linux/moduleparam.h> |
e9bda3b3 | 31 | #include <linux/mod_devicetable.h> |
af658dca | 32 | #include <linux/trace_events.h> |
5a0e3ad6 | 33 | #include <linux/slab.h> |
cafd6659 | 34 | #include <linux/tboot.h> |
f4124500 | 35 | #include <linux/hrtimer.h> |
c207aee4 | 36 | #include <linux/frame.h> |
5fdbf976 | 37 | #include "kvm_cache_regs.h" |
35920a35 | 38 | #include "x86.h" |
e495606d | 39 | |
28b835d6 | 40 | #include <asm/cpu.h> |
6aa8b732 | 41 | #include <asm/io.h> |
3b3be0d1 | 42 | #include <asm/desc.h> |
13673a90 | 43 | #include <asm/vmx.h> |
6210e37b | 44 | #include <asm/virtext.h> |
a0861c02 | 45 | #include <asm/mce.h> |
952f07ec | 46 | #include <asm/fpu/internal.h> |
d7cd9796 | 47 | #include <asm/perf_event.h> |
81908bf4 | 48 | #include <asm/debugreg.h> |
8f536b76 | 49 | #include <asm/kexec.h> |
dab2087d | 50 | #include <asm/apic.h> |
efc64404 | 51 | #include <asm/irq_remapping.h> |
d6e41f11 | 52 | #include <asm/mmu_context.h> |
6aa8b732 | 53 | |
229456fc | 54 | #include "trace.h" |
25462f7f | 55 | #include "pmu.h" |
229456fc | 56 | |
4ecac3fd | 57 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
5e520e62 AK |
58 | #define __ex_clear(x, reg) \ |
59 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) | |
4ecac3fd | 60 | |
6aa8b732 AK |
61 | MODULE_AUTHOR("Qumranet"); |
62 | MODULE_LICENSE("GPL"); | |
63 | ||
e9bda3b3 JT |
64 | static const struct x86_cpu_id vmx_cpu_id[] = { |
65 | X86_FEATURE_MATCH(X86_FEATURE_VMX), | |
66 | {} | |
67 | }; | |
68 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); | |
69 | ||
476bc001 | 70 | static bool __read_mostly enable_vpid = 1; |
736caefe | 71 | module_param_named(vpid, enable_vpid, bool, 0444); |
2384d2b3 | 72 | |
d02fcf50 PB |
73 | static bool __read_mostly enable_vnmi = 1; |
74 | module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); | |
75 | ||
476bc001 | 76 | static bool __read_mostly flexpriority_enabled = 1; |
736caefe | 77 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
4c9fc8ef | 78 | |
476bc001 | 79 | static bool __read_mostly enable_ept = 1; |
736caefe | 80 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
d56f546d | 81 | |
476bc001 | 82 | static bool __read_mostly enable_unrestricted_guest = 1; |
3a624e29 NK |
83 | module_param_named(unrestricted_guest, |
84 | enable_unrestricted_guest, bool, S_IRUGO); | |
85 | ||
83c3a331 XH |
86 | static bool __read_mostly enable_ept_ad_bits = 1; |
87 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); | |
88 | ||
a27685c3 | 89 | static bool __read_mostly emulate_invalid_guest_state = true; |
c1f8bc04 | 90 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
04fa4d32 | 91 | |
476bc001 | 92 | static bool __read_mostly fasteoi = 1; |
58fbbf26 KT |
93 | module_param(fasteoi, bool, S_IRUGO); |
94 | ||
5a71785d | 95 | static bool __read_mostly enable_apicv = 1; |
01e439be | 96 | module_param(enable_apicv, bool, S_IRUGO); |
83d4c286 | 97 | |
abc4fc58 AG |
98 | static bool __read_mostly enable_shadow_vmcs = 1; |
99 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); | |
801d3424 NHE |
100 | /* |
101 | * If nested=1, nested virtualization is supported, i.e., guests may use | |
102 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not | |
103 | * use VMX instructions. | |
104 | */ | |
476bc001 | 105 | static bool __read_mostly nested = 0; |
801d3424 NHE |
106 | module_param(nested, bool, S_IRUGO); |
107 | ||
20300099 WL |
108 | static u64 __read_mostly host_xss; |
109 | ||
843e4330 KH |
110 | static bool __read_mostly enable_pml = 1; |
111 | module_param_named(pml, enable_pml, bool, S_IRUGO); | |
112 | ||
64903d61 HZ |
113 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
114 | ||
64672c95 YJ |
115 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
116 | static int __read_mostly cpu_preemption_timer_multi; | |
117 | static bool __read_mostly enable_preemption_timer = 1; | |
118 | #ifdef CONFIG_X86_64 | |
119 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); | |
120 | #endif | |
121 | ||
5037878e GN |
122 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
123 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE) | |
cdc0e244 AK |
124 | #define KVM_VM_CR0_ALWAYS_ON \ |
125 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) | |
4c38609a AK |
126 | #define KVM_CR4_GUEST_OWNED_BITS \ |
127 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ | |
fd8cb433 | 128 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
4c38609a | 129 | |
cdc0e244 AK |
130 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
131 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) | |
132 | ||
78ac8b47 AK |
133 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
134 | ||
f4124500 JK |
135 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
136 | ||
16c2aec6 JD |
137 | /* |
138 | * Hyper-V requires all of these, so mark them as supported even though | |
139 | * they are just treated the same as all-context. | |
140 | */ | |
141 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ | |
142 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ | |
143 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ | |
144 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ | |
145 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) | |
146 | ||
4b8d54f9 ZE |
147 | /* |
148 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: | |
149 | * ple_gap: upper bound on the amount of time between two successive | |
150 | * executions of PAUSE in a loop. Also indicate if ple enabled. | |
00c25bce | 151 | * According to test, this time is usually smaller than 128 cycles. |
4b8d54f9 ZE |
152 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
153 | * in a PAUSE loop. Tests indicate that most spinlocks are held for | |
154 | * less than 2^12 cycles | |
155 | * Time is measured based on a counter that runs at the same rate as the TSC, | |
156 | * refer SDM volume 3b section 21.6.13 & 22.1.3. | |
157 | */ | |
b4a2d31d RK |
158 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
159 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 | |
160 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 | |
161 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 | |
162 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ | |
163 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW | |
164 | ||
4b8d54f9 ZE |
165 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
166 | module_param(ple_gap, int, S_IRUGO); | |
167 | ||
168 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; | |
169 | module_param(ple_window, int, S_IRUGO); | |
170 | ||
b4a2d31d RK |
171 | /* Default doubles per-vcpu window every exit. */ |
172 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; | |
173 | module_param(ple_window_grow, int, S_IRUGO); | |
174 | ||
175 | /* Default resets per-vcpu window every exit to ple_window. */ | |
176 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; | |
177 | module_param(ple_window_shrink, int, S_IRUGO); | |
178 | ||
179 | /* Default is to compute the maximum so we can never overflow. */ | |
180 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; | |
181 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; | |
182 | module_param(ple_window_max, int, S_IRUGO); | |
183 | ||
83287ea4 AK |
184 | extern const ulong vmx_return; |
185 | ||
8bf00a52 | 186 | #define NR_AUTOLOAD_MSRS 8 |
61d2ef2c | 187 | |
a2fa3e9f GH |
188 | struct vmcs { |
189 | u32 revision_id; | |
190 | u32 abort; | |
191 | char data[0]; | |
192 | }; | |
193 | ||
d462b819 NHE |
194 | /* |
195 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also | |
196 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs | |
197 | * loaded on this CPU (so we can clear them if the CPU goes down). | |
198 | */ | |
199 | struct loaded_vmcs { | |
200 | struct vmcs *vmcs; | |
355f4fb1 | 201 | struct vmcs *shadow_vmcs; |
d462b819 | 202 | int cpu; |
4c4a6f79 PB |
203 | bool launched; |
204 | bool nmi_known_unmasked; | |
44889942 LP |
205 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
206 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ | |
8a1b4392 PB |
207 | /* Support for vnmi-less CPUs */ |
208 | int soft_vnmi_blocked; | |
209 | ktime_t entry_time; | |
210 | s64 vnmi_blocked_time; | |
d462b819 NHE |
211 | struct list_head loaded_vmcss_on_cpu_link; |
212 | }; | |
213 | ||
26bb0981 AK |
214 | struct shared_msr_entry { |
215 | unsigned index; | |
216 | u64 data; | |
d5696725 | 217 | u64 mask; |
26bb0981 AK |
218 | }; |
219 | ||
a9d30f33 NHE |
220 | /* |
221 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a | |
222 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has | |
223 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is | |
224 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, | |
225 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. | |
226 | * More than one of these structures may exist, if L1 runs multiple L2 guests. | |
00647b44 | 227 | * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the |
a9d30f33 NHE |
228 | * underlying hardware which will be used to run L2. |
229 | * This structure is packed to ensure that its layout is identical across | |
230 | * machines (necessary for live migration). | |
231 | * If there are changes in this struct, VMCS12_REVISION must be changed. | |
232 | */ | |
22bd0358 | 233 | typedef u64 natural_width; |
a9d30f33 NHE |
234 | struct __packed vmcs12 { |
235 | /* According to the Intel spec, a VMCS region must start with the | |
236 | * following two fields. Then follow implementation-specific data. | |
237 | */ | |
238 | u32 revision_id; | |
239 | u32 abort; | |
22bd0358 | 240 | |
27d6c865 NHE |
241 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
242 | u32 padding[7]; /* room for future expansion */ | |
243 | ||
22bd0358 NHE |
244 | u64 io_bitmap_a; |
245 | u64 io_bitmap_b; | |
246 | u64 msr_bitmap; | |
247 | u64 vm_exit_msr_store_addr; | |
248 | u64 vm_exit_msr_load_addr; | |
249 | u64 vm_entry_msr_load_addr; | |
250 | u64 tsc_offset; | |
251 | u64 virtual_apic_page_addr; | |
252 | u64 apic_access_addr; | |
705699a1 | 253 | u64 posted_intr_desc_addr; |
27c42a1b | 254 | u64 vm_function_control; |
22bd0358 | 255 | u64 ept_pointer; |
608406e2 WV |
256 | u64 eoi_exit_bitmap0; |
257 | u64 eoi_exit_bitmap1; | |
258 | u64 eoi_exit_bitmap2; | |
259 | u64 eoi_exit_bitmap3; | |
41ab9372 | 260 | u64 eptp_list_address; |
81dc01f7 | 261 | u64 xss_exit_bitmap; |
22bd0358 NHE |
262 | u64 guest_physical_address; |
263 | u64 vmcs_link_pointer; | |
c5f983f6 | 264 | u64 pml_address; |
22bd0358 NHE |
265 | u64 guest_ia32_debugctl; |
266 | u64 guest_ia32_pat; | |
267 | u64 guest_ia32_efer; | |
268 | u64 guest_ia32_perf_global_ctrl; | |
269 | u64 guest_pdptr0; | |
270 | u64 guest_pdptr1; | |
271 | u64 guest_pdptr2; | |
272 | u64 guest_pdptr3; | |
36be0b9d | 273 | u64 guest_bndcfgs; |
22bd0358 NHE |
274 | u64 host_ia32_pat; |
275 | u64 host_ia32_efer; | |
276 | u64 host_ia32_perf_global_ctrl; | |
277 | u64 padding64[8]; /* room for future expansion */ | |
278 | /* | |
279 | * To allow migration of L1 (complete with its L2 guests) between | |
280 | * machines of different natural widths (32 or 64 bit), we cannot have | |
281 | * unsigned long fields with no explict size. We use u64 (aliased | |
282 | * natural_width) instead. Luckily, x86 is little-endian. | |
283 | */ | |
284 | natural_width cr0_guest_host_mask; | |
285 | natural_width cr4_guest_host_mask; | |
286 | natural_width cr0_read_shadow; | |
287 | natural_width cr4_read_shadow; | |
288 | natural_width cr3_target_value0; | |
289 | natural_width cr3_target_value1; | |
290 | natural_width cr3_target_value2; | |
291 | natural_width cr3_target_value3; | |
292 | natural_width exit_qualification; | |
293 | natural_width guest_linear_address; | |
294 | natural_width guest_cr0; | |
295 | natural_width guest_cr3; | |
296 | natural_width guest_cr4; | |
297 | natural_width guest_es_base; | |
298 | natural_width guest_cs_base; | |
299 | natural_width guest_ss_base; | |
300 | natural_width guest_ds_base; | |
301 | natural_width guest_fs_base; | |
302 | natural_width guest_gs_base; | |
303 | natural_width guest_ldtr_base; | |
304 | natural_width guest_tr_base; | |
305 | natural_width guest_gdtr_base; | |
306 | natural_width guest_idtr_base; | |
307 | natural_width guest_dr7; | |
308 | natural_width guest_rsp; | |
309 | natural_width guest_rip; | |
310 | natural_width guest_rflags; | |
311 | natural_width guest_pending_dbg_exceptions; | |
312 | natural_width guest_sysenter_esp; | |
313 | natural_width guest_sysenter_eip; | |
314 | natural_width host_cr0; | |
315 | natural_width host_cr3; | |
316 | natural_width host_cr4; | |
317 | natural_width host_fs_base; | |
318 | natural_width host_gs_base; | |
319 | natural_width host_tr_base; | |
320 | natural_width host_gdtr_base; | |
321 | natural_width host_idtr_base; | |
322 | natural_width host_ia32_sysenter_esp; | |
323 | natural_width host_ia32_sysenter_eip; | |
324 | natural_width host_rsp; | |
325 | natural_width host_rip; | |
326 | natural_width paddingl[8]; /* room for future expansion */ | |
327 | u32 pin_based_vm_exec_control; | |
328 | u32 cpu_based_vm_exec_control; | |
329 | u32 exception_bitmap; | |
330 | u32 page_fault_error_code_mask; | |
331 | u32 page_fault_error_code_match; | |
332 | u32 cr3_target_count; | |
333 | u32 vm_exit_controls; | |
334 | u32 vm_exit_msr_store_count; | |
335 | u32 vm_exit_msr_load_count; | |
336 | u32 vm_entry_controls; | |
337 | u32 vm_entry_msr_load_count; | |
338 | u32 vm_entry_intr_info_field; | |
339 | u32 vm_entry_exception_error_code; | |
340 | u32 vm_entry_instruction_len; | |
341 | u32 tpr_threshold; | |
342 | u32 secondary_vm_exec_control; | |
343 | u32 vm_instruction_error; | |
344 | u32 vm_exit_reason; | |
345 | u32 vm_exit_intr_info; | |
346 | u32 vm_exit_intr_error_code; | |
347 | u32 idt_vectoring_info_field; | |
348 | u32 idt_vectoring_error_code; | |
349 | u32 vm_exit_instruction_len; | |
350 | u32 vmx_instruction_info; | |
351 | u32 guest_es_limit; | |
352 | u32 guest_cs_limit; | |
353 | u32 guest_ss_limit; | |
354 | u32 guest_ds_limit; | |
355 | u32 guest_fs_limit; | |
356 | u32 guest_gs_limit; | |
357 | u32 guest_ldtr_limit; | |
358 | u32 guest_tr_limit; | |
359 | u32 guest_gdtr_limit; | |
360 | u32 guest_idtr_limit; | |
361 | u32 guest_es_ar_bytes; | |
362 | u32 guest_cs_ar_bytes; | |
363 | u32 guest_ss_ar_bytes; | |
364 | u32 guest_ds_ar_bytes; | |
365 | u32 guest_fs_ar_bytes; | |
366 | u32 guest_gs_ar_bytes; | |
367 | u32 guest_ldtr_ar_bytes; | |
368 | u32 guest_tr_ar_bytes; | |
369 | u32 guest_interruptibility_info; | |
370 | u32 guest_activity_state; | |
371 | u32 guest_sysenter_cs; | |
372 | u32 host_ia32_sysenter_cs; | |
0238ea91 JK |
373 | u32 vmx_preemption_timer_value; |
374 | u32 padding32[7]; /* room for future expansion */ | |
22bd0358 | 375 | u16 virtual_processor_id; |
705699a1 | 376 | u16 posted_intr_nv; |
22bd0358 NHE |
377 | u16 guest_es_selector; |
378 | u16 guest_cs_selector; | |
379 | u16 guest_ss_selector; | |
380 | u16 guest_ds_selector; | |
381 | u16 guest_fs_selector; | |
382 | u16 guest_gs_selector; | |
383 | u16 guest_ldtr_selector; | |
384 | u16 guest_tr_selector; | |
608406e2 | 385 | u16 guest_intr_status; |
c5f983f6 | 386 | u16 guest_pml_index; |
22bd0358 NHE |
387 | u16 host_es_selector; |
388 | u16 host_cs_selector; | |
389 | u16 host_ss_selector; | |
390 | u16 host_ds_selector; | |
391 | u16 host_fs_selector; | |
392 | u16 host_gs_selector; | |
393 | u16 host_tr_selector; | |
a9d30f33 NHE |
394 | }; |
395 | ||
396 | /* | |
397 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or | |
398 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and | |
399 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. | |
400 | */ | |
401 | #define VMCS12_REVISION 0x11e57ed0 | |
402 | ||
403 | /* | |
404 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region | |
405 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the | |
406 | * current implementation, 4K are reserved to avoid future complications. | |
407 | */ | |
408 | #define VMCS12_SIZE 0x1000 | |
409 | ||
5b15706d JM |
410 | /* |
411 | * VMCS12_MAX_FIELD_INDEX is the highest index value used in any | |
412 | * supported VMCS12 field encoding. | |
413 | */ | |
414 | #define VMCS12_MAX_FIELD_INDEX 0x17 | |
415 | ||
ec378aee NHE |
416 | /* |
417 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need | |
418 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. | |
419 | */ | |
420 | struct nested_vmx { | |
421 | /* Has the level1 guest done vmxon? */ | |
422 | bool vmxon; | |
3573e22c | 423 | gpa_t vmxon_ptr; |
c5f983f6 | 424 | bool pml_full; |
a9d30f33 NHE |
425 | |
426 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ | |
427 | gpa_t current_vmptr; | |
4f2777bc DM |
428 | /* |
429 | * Cache of the guest's VMCS, existing outside of guest memory. | |
430 | * Loaded from guest memory during VMPTRLD. Flushed to guest | |
8ca44e88 | 431 | * memory during VMCLEAR and VMPTRLD. |
4f2777bc DM |
432 | */ |
433 | struct vmcs12 *cached_vmcs12; | |
012f83cb AG |
434 | /* |
435 | * Indicates if the shadow vmcs must be updated with the | |
436 | * data hold by vmcs12 | |
437 | */ | |
438 | bool sync_shadow_vmcs; | |
ff2f6fe9 | 439 | |
dccbfcf5 | 440 | bool change_vmcs01_virtual_x2apic_mode; |
644d711a NHE |
441 | /* L2 must run next, and mustn't decide to exit to L1. */ |
442 | bool nested_run_pending; | |
00647b44 JM |
443 | |
444 | struct loaded_vmcs vmcs02; | |
445 | ||
fe3ef05c | 446 | /* |
00647b44 JM |
447 | * Guest pages referred to in the vmcs02 with host-physical |
448 | * pointers, so we must keep them pinned while L2 runs. | |
fe3ef05c NHE |
449 | */ |
450 | struct page *apic_access_page; | |
a7c0b07d | 451 | struct page *virtual_apic_page; |
705699a1 WV |
452 | struct page *pi_desc_page; |
453 | struct pi_desc *pi_desc; | |
454 | bool pi_pending; | |
455 | u16 posted_intr_nv; | |
f4124500 | 456 | |
d048c098 RK |
457 | unsigned long *msr_bitmap; |
458 | ||
f4124500 JK |
459 | struct hrtimer preemption_timer; |
460 | bool preemption_timer_expired; | |
2996fca0 JK |
461 | |
462 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ | |
463 | u64 vmcs01_debugctl; | |
b9c237bb | 464 | |
5c614b35 WL |
465 | u16 vpid02; |
466 | u16 last_vpid; | |
467 | ||
0115f9cb DM |
468 | /* |
469 | * We only store the "true" versions of the VMX capability MSRs. We | |
470 | * generate the "non-true" versions by setting the must-be-1 bits | |
471 | * according to the SDM. | |
472 | */ | |
b9c237bb WV |
473 | u32 nested_vmx_procbased_ctls_low; |
474 | u32 nested_vmx_procbased_ctls_high; | |
b9c237bb WV |
475 | u32 nested_vmx_secondary_ctls_low; |
476 | u32 nested_vmx_secondary_ctls_high; | |
477 | u32 nested_vmx_pinbased_ctls_low; | |
478 | u32 nested_vmx_pinbased_ctls_high; | |
479 | u32 nested_vmx_exit_ctls_low; | |
480 | u32 nested_vmx_exit_ctls_high; | |
b9c237bb WV |
481 | u32 nested_vmx_entry_ctls_low; |
482 | u32 nested_vmx_entry_ctls_high; | |
b9c237bb WV |
483 | u32 nested_vmx_misc_low; |
484 | u32 nested_vmx_misc_high; | |
485 | u32 nested_vmx_ept_caps; | |
99b83ac8 | 486 | u32 nested_vmx_vpid_caps; |
62cc6b9d DM |
487 | u64 nested_vmx_basic; |
488 | u64 nested_vmx_cr0_fixed0; | |
489 | u64 nested_vmx_cr0_fixed1; | |
490 | u64 nested_vmx_cr4_fixed0; | |
491 | u64 nested_vmx_cr4_fixed1; | |
492 | u64 nested_vmx_vmcs_enum; | |
27c42a1b | 493 | u64 nested_vmx_vmfunc_controls; |
72e9cbdb LP |
494 | |
495 | /* SMM related state */ | |
496 | struct { | |
497 | /* in VMX operation on SMM entry? */ | |
498 | bool vmxon; | |
499 | /* in guest mode on SMM entry? */ | |
500 | bool guest_mode; | |
501 | } smm; | |
ec378aee NHE |
502 | }; |
503 | ||
01e439be | 504 | #define POSTED_INTR_ON 0 |
ebbfc765 FW |
505 | #define POSTED_INTR_SN 1 |
506 | ||
01e439be YZ |
507 | /* Posted-Interrupt Descriptor */ |
508 | struct pi_desc { | |
509 | u32 pir[8]; /* Posted interrupt requested */ | |
6ef1522f FW |
510 | union { |
511 | struct { | |
512 | /* bit 256 - Outstanding Notification */ | |
513 | u16 on : 1, | |
514 | /* bit 257 - Suppress Notification */ | |
515 | sn : 1, | |
516 | /* bit 271:258 - Reserved */ | |
517 | rsvd_1 : 14; | |
518 | /* bit 279:272 - Notification Vector */ | |
519 | u8 nv; | |
520 | /* bit 287:280 - Reserved */ | |
521 | u8 rsvd_2; | |
522 | /* bit 319:288 - Notification Destination */ | |
523 | u32 ndst; | |
524 | }; | |
525 | u64 control; | |
526 | }; | |
527 | u32 rsvd[6]; | |
01e439be YZ |
528 | } __aligned(64); |
529 | ||
a20ed54d YZ |
530 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
531 | { | |
532 | return test_and_set_bit(POSTED_INTR_ON, | |
533 | (unsigned long *)&pi_desc->control); | |
534 | } | |
535 | ||
536 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) | |
537 | { | |
538 | return test_and_clear_bit(POSTED_INTR_ON, | |
539 | (unsigned long *)&pi_desc->control); | |
540 | } | |
541 | ||
542 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) | |
543 | { | |
544 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); | |
545 | } | |
546 | ||
ebbfc765 FW |
547 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
548 | { | |
549 | return clear_bit(POSTED_INTR_SN, | |
550 | (unsigned long *)&pi_desc->control); | |
551 | } | |
552 | ||
553 | static inline void pi_set_sn(struct pi_desc *pi_desc) | |
554 | { | |
555 | return set_bit(POSTED_INTR_SN, | |
556 | (unsigned long *)&pi_desc->control); | |
557 | } | |
558 | ||
ad361091 PB |
559 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
560 | { | |
561 | clear_bit(POSTED_INTR_ON, | |
562 | (unsigned long *)&pi_desc->control); | |
563 | } | |
564 | ||
ebbfc765 FW |
565 | static inline int pi_test_on(struct pi_desc *pi_desc) |
566 | { | |
567 | return test_bit(POSTED_INTR_ON, | |
568 | (unsigned long *)&pi_desc->control); | |
569 | } | |
570 | ||
571 | static inline int pi_test_sn(struct pi_desc *pi_desc) | |
572 | { | |
573 | return test_bit(POSTED_INTR_SN, | |
574 | (unsigned long *)&pi_desc->control); | |
575 | } | |
576 | ||
a2fa3e9f | 577 | struct vcpu_vmx { |
fb3f0f51 | 578 | struct kvm_vcpu vcpu; |
313dbd49 | 579 | unsigned long host_rsp; |
29bd8a78 | 580 | u8 fail; |
51aa01d1 | 581 | u32 exit_intr_info; |
1155f76a | 582 | u32 idt_vectoring_info; |
6de12732 | 583 | ulong rflags; |
26bb0981 | 584 | struct shared_msr_entry *guest_msrs; |
a2fa3e9f GH |
585 | int nmsrs; |
586 | int save_nmsrs; | |
a547c6db | 587 | unsigned long host_idt_base; |
a2fa3e9f | 588 | #ifdef CONFIG_X86_64 |
44ea2b17 AK |
589 | u64 msr_host_kernel_gs_base; |
590 | u64 msr_guest_kernel_gs_base; | |
a2fa3e9f | 591 | #endif |
2961e876 GN |
592 | u32 vm_entry_controls_shadow; |
593 | u32 vm_exit_controls_shadow; | |
80154d77 PB |
594 | u32 secondary_exec_control; |
595 | ||
d462b819 NHE |
596 | /* |
597 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a | |
598 | * non-nested (L1) guest, it always points to vmcs01. For a nested | |
599 | * guest (L2), it points to a different VMCS. | |
600 | */ | |
601 | struct loaded_vmcs vmcs01; | |
602 | struct loaded_vmcs *loaded_vmcs; | |
603 | bool __launched; /* temporary, used in vmx_vcpu_run */ | |
61d2ef2c AK |
604 | struct msr_autoload { |
605 | unsigned nr; | |
606 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; | |
607 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; | |
608 | } msr_autoload; | |
a2fa3e9f GH |
609 | struct { |
610 | int loaded; | |
611 | u16 fs_sel, gs_sel, ldt_sel; | |
b2da15ac AK |
612 | #ifdef CONFIG_X86_64 |
613 | u16 ds_sel, es_sel; | |
614 | #endif | |
152d3f2f LV |
615 | int gs_ldt_reload_needed; |
616 | int fs_reload_needed; | |
da8999d3 | 617 | u64 msr_host_bndcfgs; |
d77c26fc | 618 | } host_state; |
9c8cba37 | 619 | struct { |
7ffd92c5 | 620 | int vm86_active; |
78ac8b47 | 621 | ulong save_rflags; |
f5f7b2fe AK |
622 | struct kvm_segment segs[8]; |
623 | } rmode; | |
624 | struct { | |
625 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ | |
7ffd92c5 AK |
626 | struct kvm_save_segment { |
627 | u16 selector; | |
628 | unsigned long base; | |
629 | u32 limit; | |
630 | u32 ar; | |
f5f7b2fe | 631 | } seg[8]; |
2fb92db1 | 632 | } segment_cache; |
2384d2b3 | 633 | int vpid; |
04fa4d32 | 634 | bool emulation_required; |
3b86cd99 | 635 | |
a0861c02 | 636 | u32 exit_reason; |
4e47c7a6 | 637 | |
01e439be YZ |
638 | /* Posted interrupt descriptor */ |
639 | struct pi_desc pi_desc; | |
640 | ||
ec378aee NHE |
641 | /* Support for a guest hypervisor (nested VMX) */ |
642 | struct nested_vmx nested; | |
a7653ecd RK |
643 | |
644 | /* Dynamic PLE window. */ | |
645 | int ple_window; | |
646 | bool ple_window_dirty; | |
843e4330 KH |
647 | |
648 | /* Support for PML */ | |
649 | #define PML_ENTITY_NUM 512 | |
650 | struct page *pml_pg; | |
2680d6da | 651 | |
64672c95 YJ |
652 | /* apic deadline value in host tsc */ |
653 | u64 hv_deadline_tsc; | |
654 | ||
2680d6da | 655 | u64 current_tsc_ratio; |
1be0e61c | 656 | |
1be0e61c | 657 | u32 host_pkru; |
3b84080b | 658 | |
74c55931 WL |
659 | unsigned long host_debugctlmsr; |
660 | ||
37e4c997 HZ |
661 | /* |
662 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in | |
663 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included | |
664 | * in msr_ia32_feature_control_valid_bits. | |
665 | */ | |
3b84080b | 666 | u64 msr_ia32_feature_control; |
37e4c997 | 667 | u64 msr_ia32_feature_control_valid_bits; |
a2fa3e9f GH |
668 | }; |
669 | ||
2fb92db1 AK |
670 | enum segment_cache_field { |
671 | SEG_FIELD_SEL = 0, | |
672 | SEG_FIELD_BASE = 1, | |
673 | SEG_FIELD_LIMIT = 2, | |
674 | SEG_FIELD_AR = 3, | |
675 | ||
676 | SEG_FIELD_NR = 4 | |
677 | }; | |
678 | ||
a2fa3e9f GH |
679 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
680 | { | |
fb3f0f51 | 681 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
a2fa3e9f GH |
682 | } |
683 | ||
efc64404 FW |
684 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
685 | { | |
686 | return &(to_vmx(vcpu)->pi_desc); | |
687 | } | |
688 | ||
22bd0358 NHE |
689 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
690 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) | |
691 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ | |
692 | [number##_HIGH] = VMCS12_OFFSET(name)+4 | |
693 | ||
4607c2d7 | 694 | |
44900ba6 | 695 | static u16 shadow_read_only_fields[] = { |
4607c2d7 AG |
696 | /* |
697 | * We do NOT shadow fields that are modified when L0 | |
698 | * traps and emulates any vmx instruction (e.g. VMPTRLD, | |
699 | * VMXON...) executed by L1. | |
700 | * For example, VM_INSTRUCTION_ERROR is read | |
701 | * by L1 if a vmx instruction fails (part of the error path). | |
702 | * Note the code assumes this logic. If for some reason | |
703 | * we start shadowing these fields then we need to | |
704 | * force a shadow sync when L0 emulates vmx instructions | |
705 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified | |
706 | * by nested_vmx_failValid) | |
707 | */ | |
44900ba6 | 708 | /* 32-bits */ |
4607c2d7 AG |
709 | VM_EXIT_REASON, |
710 | VM_EXIT_INTR_INFO, | |
711 | VM_EXIT_INSTRUCTION_LEN, | |
712 | IDT_VECTORING_INFO_FIELD, | |
713 | IDT_VECTORING_ERROR_CODE, | |
714 | VM_EXIT_INTR_ERROR_CODE, | |
44900ba6 PB |
715 | |
716 | /* Natural width */ | |
4607c2d7 AG |
717 | EXIT_QUALIFICATION, |
718 | GUEST_LINEAR_ADDRESS, | |
44900ba6 PB |
719 | |
720 | /* 64-bit */ | |
721 | GUEST_PHYSICAL_ADDRESS, | |
722 | GUEST_PHYSICAL_ADDRESS_HIGH, | |
4607c2d7 | 723 | }; |
fe2b201b | 724 | static int max_shadow_read_only_fields = |
4607c2d7 AG |
725 | ARRAY_SIZE(shadow_read_only_fields); |
726 | ||
44900ba6 PB |
727 | static u16 shadow_read_write_fields[] = { |
728 | /* 16-bits */ | |
729 | GUEST_CS_SELECTOR, | |
730 | GUEST_INTR_STATUS, | |
731 | GUEST_PML_INDEX, | |
732 | HOST_FS_SELECTOR, | |
733 | HOST_GS_SELECTOR, | |
734 | ||
735 | /* 32-bits */ | |
736 | CPU_BASED_VM_EXEC_CONTROL, | |
737 | EXCEPTION_BITMAP, | |
738 | VM_ENTRY_EXCEPTION_ERROR_CODE, | |
739 | VM_ENTRY_INTR_INFO_FIELD, | |
740 | VM_ENTRY_INSTRUCTION_LEN, | |
a7c0b07d | 741 | TPR_THRESHOLD, |
44900ba6 PB |
742 | GUEST_CS_LIMIT, |
743 | GUEST_CS_AR_BYTES, | |
744 | GUEST_INTERRUPTIBILITY_INFO, | |
745 | VMX_PREEMPTION_TIMER_VALUE, | |
746 | ||
747 | /* Natural width */ | |
4607c2d7 AG |
748 | GUEST_RIP, |
749 | GUEST_RSP, | |
750 | GUEST_CR0, | |
751 | GUEST_CR3, | |
752 | GUEST_CR4, | |
4607c2d7 | 753 | GUEST_RFLAGS, |
4607c2d7 AG |
754 | GUEST_CS_BASE, |
755 | GUEST_ES_BASE, | |
756 | CR0_GUEST_HOST_MASK, | |
757 | CR0_READ_SHADOW, | |
758 | CR4_READ_SHADOW, | |
4607c2d7 AG |
759 | HOST_FS_BASE, |
760 | HOST_GS_BASE, | |
4607c2d7 | 761 | }; |
fe2b201b | 762 | static int max_shadow_read_write_fields = |
4607c2d7 AG |
763 | ARRAY_SIZE(shadow_read_write_fields); |
764 | ||
772e0318 | 765 | static const unsigned short vmcs_field_to_offset_table[] = { |
22bd0358 | 766 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
705699a1 | 767 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
22bd0358 NHE |
768 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
769 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), | |
770 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), | |
771 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), | |
772 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), | |
773 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), | |
774 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), | |
775 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), | |
608406e2 | 776 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
c5f983f6 | 777 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
22bd0358 NHE |
778 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
779 | FIELD(HOST_CS_SELECTOR, host_cs_selector), | |
780 | FIELD(HOST_SS_SELECTOR, host_ss_selector), | |
781 | FIELD(HOST_DS_SELECTOR, host_ds_selector), | |
782 | FIELD(HOST_FS_SELECTOR, host_fs_selector), | |
783 | FIELD(HOST_GS_SELECTOR, host_gs_selector), | |
784 | FIELD(HOST_TR_SELECTOR, host_tr_selector), | |
785 | FIELD64(IO_BITMAP_A, io_bitmap_a), | |
786 | FIELD64(IO_BITMAP_B, io_bitmap_b), | |
787 | FIELD64(MSR_BITMAP, msr_bitmap), | |
788 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), | |
789 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), | |
790 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), | |
791 | FIELD64(TSC_OFFSET, tsc_offset), | |
792 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), | |
793 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), | |
705699a1 | 794 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
27c42a1b | 795 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
22bd0358 | 796 | FIELD64(EPT_POINTER, ept_pointer), |
608406e2 WV |
797 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
798 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), | |
799 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), | |
800 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), | |
41ab9372 | 801 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
81dc01f7 | 802 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
22bd0358 NHE |
803 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
804 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), | |
c5f983f6 | 805 | FIELD64(PML_ADDRESS, pml_address), |
22bd0358 NHE |
806 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
807 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), | |
808 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), | |
809 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), | |
810 | FIELD64(GUEST_PDPTR0, guest_pdptr0), | |
811 | FIELD64(GUEST_PDPTR1, guest_pdptr1), | |
812 | FIELD64(GUEST_PDPTR2, guest_pdptr2), | |
813 | FIELD64(GUEST_PDPTR3, guest_pdptr3), | |
36be0b9d | 814 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
22bd0358 NHE |
815 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
816 | FIELD64(HOST_IA32_EFER, host_ia32_efer), | |
817 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), | |
818 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), | |
819 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), | |
820 | FIELD(EXCEPTION_BITMAP, exception_bitmap), | |
821 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), | |
822 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), | |
823 | FIELD(CR3_TARGET_COUNT, cr3_target_count), | |
824 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), | |
825 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), | |
826 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), | |
827 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), | |
828 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), | |
829 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), | |
830 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), | |
831 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), | |
832 | FIELD(TPR_THRESHOLD, tpr_threshold), | |
833 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), | |
834 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), | |
835 | FIELD(VM_EXIT_REASON, vm_exit_reason), | |
836 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), | |
837 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), | |
838 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), | |
839 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), | |
840 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), | |
841 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), | |
842 | FIELD(GUEST_ES_LIMIT, guest_es_limit), | |
843 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), | |
844 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), | |
845 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), | |
846 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), | |
847 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), | |
848 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), | |
849 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), | |
850 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), | |
851 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), | |
852 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), | |
853 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), | |
854 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), | |
855 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), | |
856 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), | |
857 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), | |
858 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), | |
859 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), | |
860 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), | |
861 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), | |
862 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), | |
863 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), | |
0238ea91 | 864 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
22bd0358 NHE |
865 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
866 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), | |
867 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), | |
868 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), | |
869 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), | |
870 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), | |
871 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), | |
872 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), | |
873 | FIELD(EXIT_QUALIFICATION, exit_qualification), | |
874 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), | |
875 | FIELD(GUEST_CR0, guest_cr0), | |
876 | FIELD(GUEST_CR3, guest_cr3), | |
877 | FIELD(GUEST_CR4, guest_cr4), | |
878 | FIELD(GUEST_ES_BASE, guest_es_base), | |
879 | FIELD(GUEST_CS_BASE, guest_cs_base), | |
880 | FIELD(GUEST_SS_BASE, guest_ss_base), | |
881 | FIELD(GUEST_DS_BASE, guest_ds_base), | |
882 | FIELD(GUEST_FS_BASE, guest_fs_base), | |
883 | FIELD(GUEST_GS_BASE, guest_gs_base), | |
884 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), | |
885 | FIELD(GUEST_TR_BASE, guest_tr_base), | |
886 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), | |
887 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), | |
888 | FIELD(GUEST_DR7, guest_dr7), | |
889 | FIELD(GUEST_RSP, guest_rsp), | |
890 | FIELD(GUEST_RIP, guest_rip), | |
891 | FIELD(GUEST_RFLAGS, guest_rflags), | |
892 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), | |
893 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), | |
894 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), | |
895 | FIELD(HOST_CR0, host_cr0), | |
896 | FIELD(HOST_CR3, host_cr3), | |
897 | FIELD(HOST_CR4, host_cr4), | |
898 | FIELD(HOST_FS_BASE, host_fs_base), | |
899 | FIELD(HOST_GS_BASE, host_gs_base), | |
900 | FIELD(HOST_TR_BASE, host_tr_base), | |
901 | FIELD(HOST_GDTR_BASE, host_gdtr_base), | |
902 | FIELD(HOST_IDTR_BASE, host_idtr_base), | |
903 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), | |
904 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), | |
905 | FIELD(HOST_RSP, host_rsp), | |
906 | FIELD(HOST_RIP, host_rip), | |
907 | }; | |
22bd0358 NHE |
908 | |
909 | static inline short vmcs_field_to_offset(unsigned long field) | |
910 | { | |
a2ae9df7 PB |
911 | BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX); |
912 | ||
75f139aa AH |
913 | if (field >= ARRAY_SIZE(vmcs_field_to_offset_table)) |
914 | return -ENOENT; | |
915 | ||
916 | /* | |
917 | * FIXME: Mitigation for CVE-2017-5753. To be replaced with a | |
918 | * generic mechanism. | |
919 | */ | |
920 | asm("lfence"); | |
921 | ||
922 | if (vmcs_field_to_offset_table[field] == 0) | |
a2ae9df7 PB |
923 | return -ENOENT; |
924 | ||
22bd0358 NHE |
925 | return vmcs_field_to_offset_table[field]; |
926 | } | |
927 | ||
a9d30f33 NHE |
928 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
929 | { | |
4f2777bc | 930 | return to_vmx(vcpu)->nested.cached_vmcs12; |
a9d30f33 NHE |
931 | } |
932 | ||
995f00a6 | 933 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
bfd0a56b | 934 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
995f00a6 | 935 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
f53cd63c | 936 | static bool vmx_xsaves_supported(void); |
b246dd5d OW |
937 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
938 | struct kvm_segment *var, int seg); | |
939 | static void vmx_get_segment(struct kvm_vcpu *vcpu, | |
940 | struct kvm_segment *var, int seg); | |
d99e4152 GN |
941 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
942 | static u32 vmx_segment_access_rights(struct kvm_segment *var); | |
16f5b903 | 943 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
b96fb439 PB |
944 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
945 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); | |
946 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, | |
947 | u16 error_code); | |
75880a01 | 948 | |
6aa8b732 AK |
949 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
950 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); | |
d462b819 NHE |
951 | /* |
952 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed | |
953 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. | |
954 | */ | |
955 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); | |
6aa8b732 | 956 | |
bf9f6ac8 FW |
957 | /* |
958 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we | |
959 | * can find which vCPU should be waken up. | |
960 | */ | |
961 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); | |
962 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); | |
963 | ||
23611332 | 964 | enum { |
23611332 RK |
965 | VMX_MSR_BITMAP_LEGACY, |
966 | VMX_MSR_BITMAP_LONGMODE, | |
967 | VMX_MSR_BITMAP_LEGACY_X2APIC_APICV, | |
968 | VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV, | |
969 | VMX_MSR_BITMAP_LEGACY_X2APIC, | |
970 | VMX_MSR_BITMAP_LONGMODE_X2APIC, | |
971 | VMX_VMREAD_BITMAP, | |
972 | VMX_VMWRITE_BITMAP, | |
973 | VMX_BITMAP_NR | |
974 | }; | |
975 | ||
976 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; | |
977 | ||
23611332 RK |
978 | #define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY]) |
979 | #define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE]) | |
980 | #define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV]) | |
981 | #define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV]) | |
982 | #define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC]) | |
983 | #define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC]) | |
984 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) | |
985 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) | |
fdef3ad1 | 986 | |
110312c8 | 987 | static bool cpu_has_load_ia32_efer; |
8bf00a52 | 988 | static bool cpu_has_load_perf_global_ctrl; |
110312c8 | 989 | |
2384d2b3 SY |
990 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
991 | static DEFINE_SPINLOCK(vmx_vpid_lock); | |
992 | ||
1c3d14fe | 993 | static struct vmcs_config { |
6aa8b732 AK |
994 | int size; |
995 | int order; | |
9ac7e3e8 | 996 | u32 basic_cap; |
6aa8b732 | 997 | u32 revision_id; |
1c3d14fe YS |
998 | u32 pin_based_exec_ctrl; |
999 | u32 cpu_based_exec_ctrl; | |
f78e0e2e | 1000 | u32 cpu_based_2nd_exec_ctrl; |
1c3d14fe YS |
1001 | u32 vmexit_ctrl; |
1002 | u32 vmentry_ctrl; | |
1003 | } vmcs_config; | |
6aa8b732 | 1004 | |
efff9e53 | 1005 | static struct vmx_capability { |
d56f546d SY |
1006 | u32 ept; |
1007 | u32 vpid; | |
1008 | } vmx_capability; | |
1009 | ||
6aa8b732 AK |
1010 | #define VMX_SEGMENT_FIELD(seg) \ |
1011 | [VCPU_SREG_##seg] = { \ | |
1012 | .selector = GUEST_##seg##_SELECTOR, \ | |
1013 | .base = GUEST_##seg##_BASE, \ | |
1014 | .limit = GUEST_##seg##_LIMIT, \ | |
1015 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ | |
1016 | } | |
1017 | ||
772e0318 | 1018 | static const struct kvm_vmx_segment_field { |
6aa8b732 AK |
1019 | unsigned selector; |
1020 | unsigned base; | |
1021 | unsigned limit; | |
1022 | unsigned ar_bytes; | |
1023 | } kvm_vmx_segment_fields[] = { | |
1024 | VMX_SEGMENT_FIELD(CS), | |
1025 | VMX_SEGMENT_FIELD(DS), | |
1026 | VMX_SEGMENT_FIELD(ES), | |
1027 | VMX_SEGMENT_FIELD(FS), | |
1028 | VMX_SEGMENT_FIELD(GS), | |
1029 | VMX_SEGMENT_FIELD(SS), | |
1030 | VMX_SEGMENT_FIELD(TR), | |
1031 | VMX_SEGMENT_FIELD(LDTR), | |
1032 | }; | |
1033 | ||
26bb0981 AK |
1034 | static u64 host_efer; |
1035 | ||
6de4f3ad AK |
1036 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
1037 | ||
4d56c8a7 | 1038 | /* |
8c06585d | 1039 | * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it |
4d56c8a7 AK |
1040 | * away by decrementing the array size. |
1041 | */ | |
6aa8b732 | 1042 | static const u32 vmx_msr_index[] = { |
05b3e0c2 | 1043 | #ifdef CONFIG_X86_64 |
44ea2b17 | 1044 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
6aa8b732 | 1045 | #endif |
8c06585d | 1046 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
6aa8b732 | 1047 | }; |
6aa8b732 | 1048 | |
5bb16016 | 1049 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
6aa8b732 AK |
1050 | { |
1051 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1052 | INTR_INFO_VALID_MASK)) == | |
5bb16016 JK |
1053 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
1054 | } | |
1055 | ||
6f05485d JK |
1056 | static inline bool is_debug(u32 intr_info) |
1057 | { | |
1058 | return is_exception_n(intr_info, DB_VECTOR); | |
1059 | } | |
1060 | ||
1061 | static inline bool is_breakpoint(u32 intr_info) | |
1062 | { | |
1063 | return is_exception_n(intr_info, BP_VECTOR); | |
1064 | } | |
1065 | ||
5bb16016 JK |
1066 | static inline bool is_page_fault(u32 intr_info) |
1067 | { | |
1068 | return is_exception_n(intr_info, PF_VECTOR); | |
6aa8b732 AK |
1069 | } |
1070 | ||
31299944 | 1071 | static inline bool is_no_device(u32 intr_info) |
2ab455cc | 1072 | { |
5bb16016 | 1073 | return is_exception_n(intr_info, NM_VECTOR); |
2ab455cc AL |
1074 | } |
1075 | ||
31299944 | 1076 | static inline bool is_invalid_opcode(u32 intr_info) |
7aa81cc0 | 1077 | { |
5bb16016 | 1078 | return is_exception_n(intr_info, UD_VECTOR); |
7aa81cc0 AL |
1079 | } |
1080 | ||
31299944 | 1081 | static inline bool is_external_interrupt(u32 intr_info) |
6aa8b732 AK |
1082 | { |
1083 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
1084 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); | |
1085 | } | |
1086 | ||
31299944 | 1087 | static inline bool is_machine_check(u32 intr_info) |
a0861c02 AK |
1088 | { |
1089 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1090 | INTR_INFO_VALID_MASK)) == | |
1091 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); | |
1092 | } | |
1093 | ||
31299944 | 1094 | static inline bool cpu_has_vmx_msr_bitmap(void) |
25c5f225 | 1095 | { |
04547156 | 1096 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
25c5f225 SY |
1097 | } |
1098 | ||
31299944 | 1099 | static inline bool cpu_has_vmx_tpr_shadow(void) |
6e5d865c | 1100 | { |
04547156 | 1101 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
6e5d865c YS |
1102 | } |
1103 | ||
35754c98 | 1104 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
6e5d865c | 1105 | { |
35754c98 | 1106 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
6e5d865c YS |
1107 | } |
1108 | ||
31299944 | 1109 | static inline bool cpu_has_secondary_exec_ctrls(void) |
f78e0e2e | 1110 | { |
04547156 SY |
1111 | return vmcs_config.cpu_based_exec_ctrl & |
1112 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
f78e0e2e SY |
1113 | } |
1114 | ||
774ead3a | 1115 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
f78e0e2e | 1116 | { |
04547156 SY |
1117 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1118 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
1119 | } | |
1120 | ||
8d14695f YZ |
1121 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
1122 | { | |
1123 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1124 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
1125 | } | |
1126 | ||
83d4c286 YZ |
1127 | static inline bool cpu_has_vmx_apic_register_virt(void) |
1128 | { | |
1129 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1130 | SECONDARY_EXEC_APIC_REGISTER_VIRT; | |
1131 | } | |
1132 | ||
c7c9c56c YZ |
1133 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
1134 | { | |
1135 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1136 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; | |
1137 | } | |
1138 | ||
64672c95 YJ |
1139 | /* |
1140 | * Comment's format: document - errata name - stepping - processor name. | |
1141 | * Refer from | |
1142 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp | |
1143 | */ | |
1144 | static u32 vmx_preemption_cpu_tfms[] = { | |
1145 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ | |
1146 | 0x000206E6, | |
1147 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ | |
1148 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ | |
1149 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1150 | 0x00020652, | |
1151 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1152 | 0x00020655, | |
1153 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ | |
1154 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ | |
1155 | /* | |
1156 | * 320767.pdf - AAP86 - B1 - | |
1157 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile | |
1158 | */ | |
1159 | 0x000106E5, | |
1160 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ | |
1161 | 0x000106A0, | |
1162 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ | |
1163 | 0x000106A1, | |
1164 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ | |
1165 | 0x000106A4, | |
1166 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ | |
1167 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ | |
1168 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ | |
1169 | 0x000106A5, | |
1170 | }; | |
1171 | ||
1172 | static inline bool cpu_has_broken_vmx_preemption_timer(void) | |
1173 | { | |
1174 | u32 eax = cpuid_eax(0x00000001), i; | |
1175 | ||
1176 | /* Clear the reserved bits */ | |
1177 | eax &= ~(0x3U << 14 | 0xfU << 28); | |
03f6a22a | 1178 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
64672c95 YJ |
1179 | if (eax == vmx_preemption_cpu_tfms[i]) |
1180 | return true; | |
1181 | ||
1182 | return false; | |
1183 | } | |
1184 | ||
1185 | static inline bool cpu_has_vmx_preemption_timer(void) | |
1186 | { | |
64672c95 YJ |
1187 | return vmcs_config.pin_based_exec_ctrl & |
1188 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1189 | } | |
1190 | ||
01e439be YZ |
1191 | static inline bool cpu_has_vmx_posted_intr(void) |
1192 | { | |
d6a858d1 PB |
1193 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
1194 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; | |
01e439be YZ |
1195 | } |
1196 | ||
1197 | static inline bool cpu_has_vmx_apicv(void) | |
1198 | { | |
1199 | return cpu_has_vmx_apic_register_virt() && | |
1200 | cpu_has_vmx_virtual_intr_delivery() && | |
1201 | cpu_has_vmx_posted_intr(); | |
1202 | } | |
1203 | ||
04547156 SY |
1204 | static inline bool cpu_has_vmx_flexpriority(void) |
1205 | { | |
1206 | return cpu_has_vmx_tpr_shadow() && | |
1207 | cpu_has_vmx_virtualize_apic_accesses(); | |
f78e0e2e SY |
1208 | } |
1209 | ||
e799794e MT |
1210 | static inline bool cpu_has_vmx_ept_execute_only(void) |
1211 | { | |
31299944 | 1212 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
e799794e MT |
1213 | } |
1214 | ||
e799794e MT |
1215 | static inline bool cpu_has_vmx_ept_2m_page(void) |
1216 | { | |
31299944 | 1217 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
e799794e MT |
1218 | } |
1219 | ||
878403b7 SY |
1220 | static inline bool cpu_has_vmx_ept_1g_page(void) |
1221 | { | |
31299944 | 1222 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
878403b7 SY |
1223 | } |
1224 | ||
4bc9b982 SY |
1225 | static inline bool cpu_has_vmx_ept_4levels(void) |
1226 | { | |
1227 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; | |
1228 | } | |
1229 | ||
42aa53b4 DH |
1230 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
1231 | { | |
1232 | return vmx_capability.ept & VMX_EPTP_WB_BIT; | |
1233 | } | |
1234 | ||
855feb67 YZ |
1235 | static inline bool cpu_has_vmx_ept_5levels(void) |
1236 | { | |
1237 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; | |
1238 | } | |
1239 | ||
83c3a331 XH |
1240 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
1241 | { | |
1242 | return vmx_capability.ept & VMX_EPT_AD_BIT; | |
1243 | } | |
1244 | ||
31299944 | 1245 | static inline bool cpu_has_vmx_invept_context(void) |
d56f546d | 1246 | { |
31299944 | 1247 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
d56f546d SY |
1248 | } |
1249 | ||
31299944 | 1250 | static inline bool cpu_has_vmx_invept_global(void) |
d56f546d | 1251 | { |
31299944 | 1252 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
d56f546d SY |
1253 | } |
1254 | ||
518c8aee GJ |
1255 | static inline bool cpu_has_vmx_invvpid_single(void) |
1256 | { | |
1257 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; | |
1258 | } | |
1259 | ||
b9d762fa GJ |
1260 | static inline bool cpu_has_vmx_invvpid_global(void) |
1261 | { | |
1262 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; | |
1263 | } | |
1264 | ||
08d839c4 WL |
1265 | static inline bool cpu_has_vmx_invvpid(void) |
1266 | { | |
1267 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; | |
1268 | } | |
1269 | ||
31299944 | 1270 | static inline bool cpu_has_vmx_ept(void) |
d56f546d | 1271 | { |
04547156 SY |
1272 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1273 | SECONDARY_EXEC_ENABLE_EPT; | |
d56f546d SY |
1274 | } |
1275 | ||
31299944 | 1276 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
3a624e29 NK |
1277 | { |
1278 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1279 | SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
1280 | } | |
1281 | ||
31299944 | 1282 | static inline bool cpu_has_vmx_ple(void) |
4b8d54f9 ZE |
1283 | { |
1284 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1285 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
1286 | } | |
1287 | ||
9ac7e3e8 JD |
1288 | static inline bool cpu_has_vmx_basic_inout(void) |
1289 | { | |
1290 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); | |
1291 | } | |
1292 | ||
35754c98 | 1293 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
f78e0e2e | 1294 | { |
35754c98 | 1295 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
f78e0e2e SY |
1296 | } |
1297 | ||
31299944 | 1298 | static inline bool cpu_has_vmx_vpid(void) |
2384d2b3 | 1299 | { |
04547156 SY |
1300 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1301 | SECONDARY_EXEC_ENABLE_VPID; | |
2384d2b3 SY |
1302 | } |
1303 | ||
31299944 | 1304 | static inline bool cpu_has_vmx_rdtscp(void) |
4e47c7a6 SY |
1305 | { |
1306 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1307 | SECONDARY_EXEC_RDTSCP; | |
1308 | } | |
1309 | ||
ad756a16 MJ |
1310 | static inline bool cpu_has_vmx_invpcid(void) |
1311 | { | |
1312 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1313 | SECONDARY_EXEC_ENABLE_INVPCID; | |
1314 | } | |
1315 | ||
8a1b4392 PB |
1316 | static inline bool cpu_has_virtual_nmis(void) |
1317 | { | |
1318 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; | |
1319 | } | |
1320 | ||
f5f48ee1 SY |
1321 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
1322 | { | |
1323 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1324 | SECONDARY_EXEC_WBINVD_EXITING; | |
1325 | } | |
1326 | ||
abc4fc58 AG |
1327 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
1328 | { | |
1329 | u64 vmx_msr; | |
1330 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
1331 | /* check if the cpu supports writing r/o exit information fields */ | |
1332 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) | |
1333 | return false; | |
1334 | ||
1335 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1336 | SECONDARY_EXEC_SHADOW_VMCS; | |
1337 | } | |
1338 | ||
843e4330 KH |
1339 | static inline bool cpu_has_vmx_pml(void) |
1340 | { | |
1341 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; | |
1342 | } | |
1343 | ||
64903d61 HZ |
1344 | static inline bool cpu_has_vmx_tsc_scaling(void) |
1345 | { | |
1346 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1347 | SECONDARY_EXEC_TSC_SCALING; | |
1348 | } | |
1349 | ||
2a499e49 BD |
1350 | static inline bool cpu_has_vmx_vmfunc(void) |
1351 | { | |
1352 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1353 | SECONDARY_EXEC_ENABLE_VMFUNC; | |
1354 | } | |
1355 | ||
04547156 SY |
1356 | static inline bool report_flexpriority(void) |
1357 | { | |
1358 | return flexpriority_enabled; | |
1359 | } | |
1360 | ||
c7c2c709 JM |
1361 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
1362 | { | |
1363 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low); | |
1364 | } | |
1365 | ||
fe3ef05c NHE |
1366 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
1367 | { | |
1368 | return vmcs12->cpu_based_vm_exec_control & bit; | |
1369 | } | |
1370 | ||
1371 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) | |
1372 | { | |
1373 | return (vmcs12->cpu_based_vm_exec_control & | |
1374 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && | |
1375 | (vmcs12->secondary_vm_exec_control & bit); | |
1376 | } | |
1377 | ||
f4124500 JK |
1378 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
1379 | { | |
1380 | return vmcs12->pin_based_vm_exec_control & | |
1381 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1382 | } | |
1383 | ||
155a97a3 NHE |
1384 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
1385 | { | |
1386 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); | |
1387 | } | |
1388 | ||
81dc01f7 WL |
1389 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
1390 | { | |
3db13480 | 1391 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
81dc01f7 WL |
1392 | } |
1393 | ||
c5f983f6 BD |
1394 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
1395 | { | |
1396 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); | |
1397 | } | |
1398 | ||
f2b93280 WV |
1399 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
1400 | { | |
1401 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); | |
1402 | } | |
1403 | ||
5c614b35 WL |
1404 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
1405 | { | |
1406 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); | |
1407 | } | |
1408 | ||
82f0dd4b WV |
1409 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
1410 | { | |
1411 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); | |
1412 | } | |
1413 | ||
608406e2 WV |
1414 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
1415 | { | |
1416 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
1417 | } | |
1418 | ||
705699a1 WV |
1419 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
1420 | { | |
1421 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; | |
1422 | } | |
1423 | ||
27c42a1b BD |
1424 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
1425 | { | |
1426 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); | |
1427 | } | |
1428 | ||
41ab9372 BD |
1429 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
1430 | { | |
1431 | return nested_cpu_has_vmfunc(vmcs12) && | |
1432 | (vmcs12->vm_function_control & | |
1433 | VMX_VMFUNC_EPTP_SWITCHING); | |
1434 | } | |
1435 | ||
ef85b673 | 1436 | static inline bool is_nmi(u32 intr_info) |
644d711a NHE |
1437 | { |
1438 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
ef85b673 | 1439 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
644d711a NHE |
1440 | } |
1441 | ||
533558bc JK |
1442 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
1443 | u32 exit_intr_info, | |
1444 | unsigned long exit_qualification); | |
7c177938 NHE |
1445 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
1446 | struct vmcs12 *vmcs12, | |
1447 | u32 reason, unsigned long qualification); | |
1448 | ||
8b9cf98c | 1449 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
7725f0ba AK |
1450 | { |
1451 | int i; | |
1452 | ||
a2fa3e9f | 1453 | for (i = 0; i < vmx->nmsrs; ++i) |
26bb0981 | 1454 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
a75beee6 ED |
1455 | return i; |
1456 | return -1; | |
1457 | } | |
1458 | ||
2384d2b3 SY |
1459 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
1460 | { | |
1461 | struct { | |
1462 | u64 vpid : 16; | |
1463 | u64 rsvd : 48; | |
1464 | u64 gva; | |
1465 | } operand = { vpid, 0, gva }; | |
1466 | ||
4ecac3fd | 1467 | asm volatile (__ex(ASM_VMX_INVVPID) |
2384d2b3 SY |
1468 | /* CF==1 or ZF==1 --> rc = -1 */ |
1469 | "; ja 1f ; ud2 ; 1:" | |
1470 | : : "a"(&operand), "c"(ext) : "cc", "memory"); | |
1471 | } | |
1472 | ||
1439442c SY |
1473 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
1474 | { | |
1475 | struct { | |
1476 | u64 eptp, gpa; | |
1477 | } operand = {eptp, gpa}; | |
1478 | ||
4ecac3fd | 1479 | asm volatile (__ex(ASM_VMX_INVEPT) |
1439442c SY |
1480 | /* CF==1 or ZF==1 --> rc = -1 */ |
1481 | "; ja 1f ; ud2 ; 1:\n" | |
1482 | : : "a" (&operand), "c" (ext) : "cc", "memory"); | |
1483 | } | |
1484 | ||
26bb0981 | 1485 | static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
a75beee6 ED |
1486 | { |
1487 | int i; | |
1488 | ||
8b9cf98c | 1489 | i = __find_msr_index(vmx, msr); |
a75beee6 | 1490 | if (i >= 0) |
a2fa3e9f | 1491 | return &vmx->guest_msrs[i]; |
8b6d44c7 | 1492 | return NULL; |
7725f0ba AK |
1493 | } |
1494 | ||
6aa8b732 AK |
1495 | static void vmcs_clear(struct vmcs *vmcs) |
1496 | { | |
1497 | u64 phys_addr = __pa(vmcs); | |
1498 | u8 error; | |
1499 | ||
4ecac3fd | 1500 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
16d8f72f | 1501 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
6aa8b732 AK |
1502 | : "cc", "memory"); |
1503 | if (error) | |
1504 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", | |
1505 | vmcs, phys_addr); | |
1506 | } | |
1507 | ||
d462b819 NHE |
1508 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
1509 | { | |
1510 | vmcs_clear(loaded_vmcs->vmcs); | |
355f4fb1 JM |
1511 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
1512 | vmcs_clear(loaded_vmcs->shadow_vmcs); | |
d462b819 NHE |
1513 | loaded_vmcs->cpu = -1; |
1514 | loaded_vmcs->launched = 0; | |
1515 | } | |
1516 | ||
7725b894 DX |
1517 | static void vmcs_load(struct vmcs *vmcs) |
1518 | { | |
1519 | u64 phys_addr = __pa(vmcs); | |
1520 | u8 error; | |
1521 | ||
1522 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" | |
16d8f72f | 1523 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
7725b894 DX |
1524 | : "cc", "memory"); |
1525 | if (error) | |
2844d849 | 1526 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
7725b894 DX |
1527 | vmcs, phys_addr); |
1528 | } | |
1529 | ||
2965faa5 | 1530 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
1531 | /* |
1532 | * This bitmap is used to indicate whether the vmclear | |
1533 | * operation is enabled on all cpus. All disabled by | |
1534 | * default. | |
1535 | */ | |
1536 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; | |
1537 | ||
1538 | static inline void crash_enable_local_vmclear(int cpu) | |
1539 | { | |
1540 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1541 | } | |
1542 | ||
1543 | static inline void crash_disable_local_vmclear(int cpu) | |
1544 | { | |
1545 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1546 | } | |
1547 | ||
1548 | static inline int crash_local_vmclear_enabled(int cpu) | |
1549 | { | |
1550 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1551 | } | |
1552 | ||
1553 | static void crash_vmclear_local_loaded_vmcss(void) | |
1554 | { | |
1555 | int cpu = raw_smp_processor_id(); | |
1556 | struct loaded_vmcs *v; | |
1557 | ||
1558 | if (!crash_local_vmclear_enabled(cpu)) | |
1559 | return; | |
1560 | ||
1561 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), | |
1562 | loaded_vmcss_on_cpu_link) | |
1563 | vmcs_clear(v->vmcs); | |
1564 | } | |
1565 | #else | |
1566 | static inline void crash_enable_local_vmclear(int cpu) { } | |
1567 | static inline void crash_disable_local_vmclear(int cpu) { } | |
2965faa5 | 1568 | #endif /* CONFIG_KEXEC_CORE */ |
8f536b76 | 1569 | |
d462b819 | 1570 | static void __loaded_vmcs_clear(void *arg) |
6aa8b732 | 1571 | { |
d462b819 | 1572 | struct loaded_vmcs *loaded_vmcs = arg; |
d3b2c338 | 1573 | int cpu = raw_smp_processor_id(); |
6aa8b732 | 1574 | |
d462b819 NHE |
1575 | if (loaded_vmcs->cpu != cpu) |
1576 | return; /* vcpu migration can race with cpu offline */ | |
1577 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) | |
6aa8b732 | 1578 | per_cpu(current_vmcs, cpu) = NULL; |
8f536b76 | 1579 | crash_disable_local_vmclear(cpu); |
d462b819 | 1580 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
5a560f8b XG |
1581 | |
1582 | /* | |
1583 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link | |
1584 | * is before setting loaded_vmcs->vcpu to -1 which is done in | |
1585 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist | |
1586 | * then adds the vmcs into percpu list before it is deleted. | |
1587 | */ | |
1588 | smp_wmb(); | |
1589 | ||
d462b819 | 1590 | loaded_vmcs_init(loaded_vmcs); |
8f536b76 | 1591 | crash_enable_local_vmclear(cpu); |
6aa8b732 AK |
1592 | } |
1593 | ||
d462b819 | 1594 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
8d0be2b3 | 1595 | { |
e6c7d321 XG |
1596 | int cpu = loaded_vmcs->cpu; |
1597 | ||
1598 | if (cpu != -1) | |
1599 | smp_call_function_single(cpu, | |
1600 | __loaded_vmcs_clear, loaded_vmcs, 1); | |
8d0be2b3 AK |
1601 | } |
1602 | ||
dd5f5341 | 1603 | static inline void vpid_sync_vcpu_single(int vpid) |
2384d2b3 | 1604 | { |
dd5f5341 | 1605 | if (vpid == 0) |
2384d2b3 SY |
1606 | return; |
1607 | ||
518c8aee | 1608 | if (cpu_has_vmx_invvpid_single()) |
dd5f5341 | 1609 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
2384d2b3 SY |
1610 | } |
1611 | ||
b9d762fa GJ |
1612 | static inline void vpid_sync_vcpu_global(void) |
1613 | { | |
1614 | if (cpu_has_vmx_invvpid_global()) | |
1615 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); | |
1616 | } | |
1617 | ||
dd5f5341 | 1618 | static inline void vpid_sync_context(int vpid) |
b9d762fa GJ |
1619 | { |
1620 | if (cpu_has_vmx_invvpid_single()) | |
dd5f5341 | 1621 | vpid_sync_vcpu_single(vpid); |
b9d762fa GJ |
1622 | else |
1623 | vpid_sync_vcpu_global(); | |
1624 | } | |
1625 | ||
1439442c SY |
1626 | static inline void ept_sync_global(void) |
1627 | { | |
f5f51586 | 1628 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
1439442c SY |
1629 | } |
1630 | ||
1631 | static inline void ept_sync_context(u64 eptp) | |
1632 | { | |
0e1252dc DH |
1633 | if (cpu_has_vmx_invept_context()) |
1634 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); | |
1635 | else | |
1636 | ept_sync_global(); | |
1439442c SY |
1637 | } |
1638 | ||
8a86aea9 PB |
1639 | static __always_inline void vmcs_check16(unsigned long field) |
1640 | { | |
1641 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1642 | "16-bit accessor invalid for 64-bit field"); | |
1643 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1644 | "16-bit accessor invalid for 64-bit high field"); | |
1645 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1646 | "16-bit accessor invalid for 32-bit high field"); | |
1647 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1648 | "16-bit accessor invalid for natural width field"); | |
1649 | } | |
1650 | ||
1651 | static __always_inline void vmcs_check32(unsigned long field) | |
1652 | { | |
1653 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1654 | "32-bit accessor invalid for 16-bit field"); | |
1655 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1656 | "32-bit accessor invalid for natural width field"); | |
1657 | } | |
1658 | ||
1659 | static __always_inline void vmcs_check64(unsigned long field) | |
1660 | { | |
1661 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1662 | "64-bit accessor invalid for 16-bit field"); | |
1663 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1664 | "64-bit accessor invalid for 64-bit high field"); | |
1665 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1666 | "64-bit accessor invalid for 32-bit field"); | |
1667 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1668 | "64-bit accessor invalid for natural width field"); | |
1669 | } | |
1670 | ||
1671 | static __always_inline void vmcs_checkl(unsigned long field) | |
1672 | { | |
1673 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1674 | "Natural width accessor invalid for 16-bit field"); | |
1675 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1676 | "Natural width accessor invalid for 64-bit field"); | |
1677 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1678 | "Natural width accessor invalid for 64-bit high field"); | |
1679 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1680 | "Natural width accessor invalid for 32-bit field"); | |
1681 | } | |
1682 | ||
1683 | static __always_inline unsigned long __vmcs_readl(unsigned long field) | |
6aa8b732 | 1684 | { |
5e520e62 | 1685 | unsigned long value; |
6aa8b732 | 1686 | |
5e520e62 AK |
1687 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
1688 | : "=a"(value) : "d"(field) : "cc"); | |
6aa8b732 AK |
1689 | return value; |
1690 | } | |
1691 | ||
96304217 | 1692 | static __always_inline u16 vmcs_read16(unsigned long field) |
6aa8b732 | 1693 | { |
8a86aea9 PB |
1694 | vmcs_check16(field); |
1695 | return __vmcs_readl(field); | |
6aa8b732 AK |
1696 | } |
1697 | ||
96304217 | 1698 | static __always_inline u32 vmcs_read32(unsigned long field) |
6aa8b732 | 1699 | { |
8a86aea9 PB |
1700 | vmcs_check32(field); |
1701 | return __vmcs_readl(field); | |
6aa8b732 AK |
1702 | } |
1703 | ||
96304217 | 1704 | static __always_inline u64 vmcs_read64(unsigned long field) |
6aa8b732 | 1705 | { |
8a86aea9 | 1706 | vmcs_check64(field); |
05b3e0c2 | 1707 | #ifdef CONFIG_X86_64 |
8a86aea9 | 1708 | return __vmcs_readl(field); |
6aa8b732 | 1709 | #else |
8a86aea9 | 1710 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
6aa8b732 AK |
1711 | #endif |
1712 | } | |
1713 | ||
8a86aea9 PB |
1714 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
1715 | { | |
1716 | vmcs_checkl(field); | |
1717 | return __vmcs_readl(field); | |
1718 | } | |
1719 | ||
e52de1b8 AK |
1720 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
1721 | { | |
1722 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", | |
1723 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); | |
1724 | dump_stack(); | |
1725 | } | |
1726 | ||
8a86aea9 | 1727 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
6aa8b732 AK |
1728 | { |
1729 | u8 error; | |
1730 | ||
4ecac3fd | 1731 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
d77c26fc | 1732 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
e52de1b8 AK |
1733 | if (unlikely(error)) |
1734 | vmwrite_error(field, value); | |
6aa8b732 AK |
1735 | } |
1736 | ||
8a86aea9 | 1737 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
6aa8b732 | 1738 | { |
8a86aea9 PB |
1739 | vmcs_check16(field); |
1740 | __vmcs_writel(field, value); | |
6aa8b732 AK |
1741 | } |
1742 | ||
8a86aea9 | 1743 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
6aa8b732 | 1744 | { |
8a86aea9 PB |
1745 | vmcs_check32(field); |
1746 | __vmcs_writel(field, value); | |
6aa8b732 AK |
1747 | } |
1748 | ||
8a86aea9 | 1749 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
6aa8b732 | 1750 | { |
8a86aea9 PB |
1751 | vmcs_check64(field); |
1752 | __vmcs_writel(field, value); | |
7682f2d0 | 1753 | #ifndef CONFIG_X86_64 |
6aa8b732 | 1754 | asm volatile (""); |
8a86aea9 | 1755 | __vmcs_writel(field+1, value >> 32); |
6aa8b732 AK |
1756 | #endif |
1757 | } | |
1758 | ||
8a86aea9 | 1759 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
2ab455cc | 1760 | { |
8a86aea9 PB |
1761 | vmcs_checkl(field); |
1762 | __vmcs_writel(field, value); | |
2ab455cc AL |
1763 | } |
1764 | ||
8a86aea9 | 1765 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
2ab455cc | 1766 | { |
8a86aea9 PB |
1767 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1768 | "vmcs_clear_bits does not support 64-bit fields"); | |
1769 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); | |
2ab455cc AL |
1770 | } |
1771 | ||
8a86aea9 | 1772 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
2ab455cc | 1773 | { |
8a86aea9 PB |
1774 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1775 | "vmcs_set_bits does not support 64-bit fields"); | |
1776 | __vmcs_writel(field, __vmcs_readl(field) | mask); | |
2ab455cc AL |
1777 | } |
1778 | ||
8391ce44 PB |
1779 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
1780 | { | |
1781 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); | |
1782 | } | |
1783 | ||
2961e876 GN |
1784 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
1785 | { | |
1786 | vmcs_write32(VM_ENTRY_CONTROLS, val); | |
1787 | vmx->vm_entry_controls_shadow = val; | |
1788 | } | |
1789 | ||
1790 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) | |
1791 | { | |
1792 | if (vmx->vm_entry_controls_shadow != val) | |
1793 | vm_entry_controls_init(vmx, val); | |
1794 | } | |
1795 | ||
1796 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) | |
1797 | { | |
1798 | return vmx->vm_entry_controls_shadow; | |
1799 | } | |
1800 | ||
1801 | ||
1802 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
1803 | { | |
1804 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); | |
1805 | } | |
1806 | ||
1807 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
1808 | { | |
1809 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); | |
1810 | } | |
1811 | ||
8391ce44 PB |
1812 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
1813 | { | |
1814 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); | |
1815 | } | |
1816 | ||
2961e876 GN |
1817 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
1818 | { | |
1819 | vmcs_write32(VM_EXIT_CONTROLS, val); | |
1820 | vmx->vm_exit_controls_shadow = val; | |
1821 | } | |
1822 | ||
1823 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) | |
1824 | { | |
1825 | if (vmx->vm_exit_controls_shadow != val) | |
1826 | vm_exit_controls_init(vmx, val); | |
1827 | } | |
1828 | ||
1829 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) | |
1830 | { | |
1831 | return vmx->vm_exit_controls_shadow; | |
1832 | } | |
1833 | ||
1834 | ||
1835 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
1836 | { | |
1837 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); | |
1838 | } | |
1839 | ||
1840 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
1841 | { | |
1842 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); | |
1843 | } | |
1844 | ||
2fb92db1 AK |
1845 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
1846 | { | |
1847 | vmx->segment_cache.bitmask = 0; | |
1848 | } | |
1849 | ||
1850 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, | |
1851 | unsigned field) | |
1852 | { | |
1853 | bool ret; | |
1854 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); | |
1855 | ||
1856 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { | |
1857 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); | |
1858 | vmx->segment_cache.bitmask = 0; | |
1859 | } | |
1860 | ret = vmx->segment_cache.bitmask & mask; | |
1861 | vmx->segment_cache.bitmask |= mask; | |
1862 | return ret; | |
1863 | } | |
1864 | ||
1865 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) | |
1866 | { | |
1867 | u16 *p = &vmx->segment_cache.seg[seg].selector; | |
1868 | ||
1869 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) | |
1870 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); | |
1871 | return *p; | |
1872 | } | |
1873 | ||
1874 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) | |
1875 | { | |
1876 | ulong *p = &vmx->segment_cache.seg[seg].base; | |
1877 | ||
1878 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) | |
1879 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); | |
1880 | return *p; | |
1881 | } | |
1882 | ||
1883 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) | |
1884 | { | |
1885 | u32 *p = &vmx->segment_cache.seg[seg].limit; | |
1886 | ||
1887 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) | |
1888 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); | |
1889 | return *p; | |
1890 | } | |
1891 | ||
1892 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) | |
1893 | { | |
1894 | u32 *p = &vmx->segment_cache.seg[seg].ar; | |
1895 | ||
1896 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) | |
1897 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); | |
1898 | return *p; | |
1899 | } | |
1900 | ||
abd3f2d6 AK |
1901 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
1902 | { | |
1903 | u32 eb; | |
1904 | ||
ac9b305c | 1905 | eb = (1u << PF_VECTOR) | (1u << MC_VECTOR) | |
bd7e5b08 | 1906 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
fd7373cc JK |
1907 | if ((vcpu->guest_debug & |
1908 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == | |
1909 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) | |
1910 | eb |= 1u << BP_VECTOR; | |
7ffd92c5 | 1911 | if (to_vmx(vcpu)->rmode.vm86_active) |
abd3f2d6 | 1912 | eb = ~0; |
089d034e | 1913 | if (enable_ept) |
1439442c | 1914 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
36cf24e0 NHE |
1915 | |
1916 | /* When we are running a nested L2 guest and L1 specified for it a | |
1917 | * certain exception bitmap, we must trap the same exceptions and pass | |
1918 | * them to L1. When running L2, we will only handle the exceptions | |
1919 | * specified above if L1 did not want them. | |
1920 | */ | |
1921 | if (is_guest_mode(vcpu)) | |
1922 | eb |= get_vmcs12(vcpu)->exception_bitmap; | |
ac9b305c LA |
1923 | else |
1924 | eb |= 1u << UD_VECTOR; | |
36cf24e0 | 1925 | |
abd3f2d6 AK |
1926 | vmcs_write32(EXCEPTION_BITMAP, eb); |
1927 | } | |
1928 | ||
2961e876 GN |
1929 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
1930 | unsigned long entry, unsigned long exit) | |
8bf00a52 | 1931 | { |
2961e876 GN |
1932 | vm_entry_controls_clearbit(vmx, entry); |
1933 | vm_exit_controls_clearbit(vmx, exit); | |
8bf00a52 GN |
1934 | } |
1935 | ||
61d2ef2c AK |
1936 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
1937 | { | |
1938 | unsigned i; | |
1939 | struct msr_autoload *m = &vmx->msr_autoload; | |
1940 | ||
8bf00a52 GN |
1941 | switch (msr) { |
1942 | case MSR_EFER: | |
1943 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
1944 | clear_atomic_switch_msr_special(vmx, |
1945 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
1946 | VM_EXIT_LOAD_IA32_EFER); |
1947 | return; | |
1948 | } | |
1949 | break; | |
1950 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
1951 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 1952 | clear_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
1953 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
1954 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
1955 | return; | |
1956 | } | |
1957 | break; | |
110312c8 AK |
1958 | } |
1959 | ||
61d2ef2c AK |
1960 | for (i = 0; i < m->nr; ++i) |
1961 | if (m->guest[i].index == msr) | |
1962 | break; | |
1963 | ||
1964 | if (i == m->nr) | |
1965 | return; | |
1966 | --m->nr; | |
1967 | m->guest[i] = m->guest[m->nr]; | |
1968 | m->host[i] = m->host[m->nr]; | |
1969 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); | |
1970 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); | |
1971 | } | |
1972 | ||
2961e876 GN |
1973 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
1974 | unsigned long entry, unsigned long exit, | |
1975 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, | |
1976 | u64 guest_val, u64 host_val) | |
8bf00a52 GN |
1977 | { |
1978 | vmcs_write64(guest_val_vmcs, guest_val); | |
1979 | vmcs_write64(host_val_vmcs, host_val); | |
2961e876 GN |
1980 | vm_entry_controls_setbit(vmx, entry); |
1981 | vm_exit_controls_setbit(vmx, exit); | |
8bf00a52 GN |
1982 | } |
1983 | ||
61d2ef2c AK |
1984 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
1985 | u64 guest_val, u64 host_val) | |
1986 | { | |
1987 | unsigned i; | |
1988 | struct msr_autoload *m = &vmx->msr_autoload; | |
1989 | ||
8bf00a52 GN |
1990 | switch (msr) { |
1991 | case MSR_EFER: | |
1992 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
1993 | add_atomic_switch_msr_special(vmx, |
1994 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
1995 | VM_EXIT_LOAD_IA32_EFER, |
1996 | GUEST_IA32_EFER, | |
1997 | HOST_IA32_EFER, | |
1998 | guest_val, host_val); | |
1999 | return; | |
2000 | } | |
2001 | break; | |
2002 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
2003 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 2004 | add_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
2005 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
2006 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, | |
2007 | GUEST_IA32_PERF_GLOBAL_CTRL, | |
2008 | HOST_IA32_PERF_GLOBAL_CTRL, | |
2009 | guest_val, host_val); | |
2010 | return; | |
2011 | } | |
2012 | break; | |
7099e2e1 RK |
2013 | case MSR_IA32_PEBS_ENABLE: |
2014 | /* PEBS needs a quiescent period after being disabled (to write | |
2015 | * a record). Disabling PEBS through VMX MSR swapping doesn't | |
2016 | * provide that period, so a CPU could write host's record into | |
2017 | * guest's memory. | |
2018 | */ | |
2019 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); | |
110312c8 AK |
2020 | } |
2021 | ||
61d2ef2c AK |
2022 | for (i = 0; i < m->nr; ++i) |
2023 | if (m->guest[i].index == msr) | |
2024 | break; | |
2025 | ||
e7fc6f93 | 2026 | if (i == NR_AUTOLOAD_MSRS) { |
60266204 | 2027 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
e7fc6f93 GN |
2028 | "Can't add msr %x\n", msr); |
2029 | return; | |
2030 | } else if (i == m->nr) { | |
61d2ef2c AK |
2031 | ++m->nr; |
2032 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); | |
2033 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); | |
2034 | } | |
2035 | ||
2036 | m->guest[i].index = msr; | |
2037 | m->guest[i].value = guest_val; | |
2038 | m->host[i].index = msr; | |
2039 | m->host[i].value = host_val; | |
2040 | } | |
2041 | ||
92c0d900 | 2042 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
2cc51560 | 2043 | { |
844a5fe2 PB |
2044 | u64 guest_efer = vmx->vcpu.arch.efer; |
2045 | u64 ignore_bits = 0; | |
2046 | ||
2047 | if (!enable_ept) { | |
2048 | /* | |
2049 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing | |
2050 | * host CPUID is more efficient than testing guest CPUID | |
2051 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. | |
2052 | */ | |
2053 | if (boot_cpu_has(X86_FEATURE_SMEP)) | |
2054 | guest_efer |= EFER_NX; | |
2055 | else if (!(guest_efer & EFER_NX)) | |
2056 | ignore_bits |= EFER_NX; | |
2057 | } | |
3a34a881 | 2058 | |
51c6cf66 | 2059 | /* |
844a5fe2 | 2060 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
51c6cf66 | 2061 | */ |
844a5fe2 | 2062 | ignore_bits |= EFER_SCE; |
51c6cf66 AK |
2063 | #ifdef CONFIG_X86_64 |
2064 | ignore_bits |= EFER_LMA | EFER_LME; | |
2065 | /* SCE is meaningful only in long mode on Intel */ | |
2066 | if (guest_efer & EFER_LMA) | |
2067 | ignore_bits &= ~(u64)EFER_SCE; | |
2068 | #endif | |
84ad33ef AK |
2069 | |
2070 | clear_atomic_switch_msr(vmx, MSR_EFER); | |
f6577a5f AL |
2071 | |
2072 | /* | |
2073 | * On EPT, we can't emulate NX, so we must switch EFER atomically. | |
2074 | * On CPUs that support "load IA32_EFER", always switch EFER | |
2075 | * atomically, since it's faster than switching it manually. | |
2076 | */ | |
2077 | if (cpu_has_load_ia32_efer || | |
2078 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { | |
84ad33ef AK |
2079 | if (!(guest_efer & EFER_LMA)) |
2080 | guest_efer &= ~EFER_LME; | |
54b98bff AL |
2081 | if (guest_efer != host_efer) |
2082 | add_atomic_switch_msr(vmx, MSR_EFER, | |
2083 | guest_efer, host_efer); | |
84ad33ef | 2084 | return false; |
844a5fe2 PB |
2085 | } else { |
2086 | guest_efer &= ~ignore_bits; | |
2087 | guest_efer |= host_efer & ignore_bits; | |
2088 | ||
2089 | vmx->guest_msrs[efer_offset].data = guest_efer; | |
2090 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; | |
84ad33ef | 2091 | |
844a5fe2 PB |
2092 | return true; |
2093 | } | |
51c6cf66 AK |
2094 | } |
2095 | ||
e28baead AL |
2096 | #ifdef CONFIG_X86_32 |
2097 | /* | |
2098 | * On 32-bit kernels, VM exits still load the FS and GS bases from the | |
2099 | * VMCS rather than the segment table. KVM uses this helper to figure | |
2100 | * out the current bases to poke them into the VMCS before entry. | |
2101 | */ | |
2d49ec72 GN |
2102 | static unsigned long segment_base(u16 selector) |
2103 | { | |
8c2e41f7 | 2104 | struct desc_struct *table; |
2d49ec72 GN |
2105 | unsigned long v; |
2106 | ||
8c2e41f7 | 2107 | if (!(selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2108 | return 0; |
2109 | ||
45fc8757 | 2110 | table = get_current_gdt_ro(); |
2d49ec72 | 2111 | |
8c2e41f7 | 2112 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
2d49ec72 GN |
2113 | u16 ldt_selector = kvm_read_ldt(); |
2114 | ||
8c2e41f7 | 2115 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2116 | return 0; |
2117 | ||
8c2e41f7 | 2118 | table = (struct desc_struct *)segment_base(ldt_selector); |
2d49ec72 | 2119 | } |
8c2e41f7 | 2120 | v = get_desc_base(&table[selector >> 3]); |
2d49ec72 GN |
2121 | return v; |
2122 | } | |
e28baead | 2123 | #endif |
2d49ec72 | 2124 | |
04d2cc77 | 2125 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
33ed6329 | 2126 | { |
04d2cc77 | 2127 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 2128 | int i; |
04d2cc77 | 2129 | |
a2fa3e9f | 2130 | if (vmx->host_state.loaded) |
33ed6329 AK |
2131 | return; |
2132 | ||
a2fa3e9f | 2133 | vmx->host_state.loaded = 1; |
33ed6329 AK |
2134 | /* |
2135 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not | |
2136 | * allow segment selectors with cpl > 0 or ti == 1. | |
2137 | */ | |
d6e88aec | 2138 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
152d3f2f | 2139 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
9581d442 | 2140 | savesegment(fs, vmx->host_state.fs_sel); |
152d3f2f | 2141 | if (!(vmx->host_state.fs_sel & 7)) { |
a2fa3e9f | 2142 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
152d3f2f LV |
2143 | vmx->host_state.fs_reload_needed = 0; |
2144 | } else { | |
33ed6329 | 2145 | vmcs_write16(HOST_FS_SELECTOR, 0); |
152d3f2f | 2146 | vmx->host_state.fs_reload_needed = 1; |
33ed6329 | 2147 | } |
9581d442 | 2148 | savesegment(gs, vmx->host_state.gs_sel); |
a2fa3e9f GH |
2149 | if (!(vmx->host_state.gs_sel & 7)) |
2150 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); | |
33ed6329 AK |
2151 | else { |
2152 | vmcs_write16(HOST_GS_SELECTOR, 0); | |
152d3f2f | 2153 | vmx->host_state.gs_ldt_reload_needed = 1; |
33ed6329 AK |
2154 | } |
2155 | ||
b2da15ac AK |
2156 | #ifdef CONFIG_X86_64 |
2157 | savesegment(ds, vmx->host_state.ds_sel); | |
2158 | savesegment(es, vmx->host_state.es_sel); | |
2159 | #endif | |
2160 | ||
33ed6329 AK |
2161 | #ifdef CONFIG_X86_64 |
2162 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); | |
2163 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); | |
2164 | #else | |
a2fa3e9f GH |
2165 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
2166 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); | |
33ed6329 | 2167 | #endif |
707c0874 AK |
2168 | |
2169 | #ifdef CONFIG_X86_64 | |
c8770e7b AK |
2170 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
2171 | if (is_long_mode(&vmx->vcpu)) | |
44ea2b17 | 2172 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
707c0874 | 2173 | #endif |
da8999d3 LJ |
2174 | if (boot_cpu_has(X86_FEATURE_MPX)) |
2175 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
26bb0981 AK |
2176 | for (i = 0; i < vmx->save_nmsrs; ++i) |
2177 | kvm_set_shared_msr(vmx->guest_msrs[i].index, | |
d5696725 AK |
2178 | vmx->guest_msrs[i].data, |
2179 | vmx->guest_msrs[i].mask); | |
33ed6329 AK |
2180 | } |
2181 | ||
a9b21b62 | 2182 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
33ed6329 | 2183 | { |
a2fa3e9f | 2184 | if (!vmx->host_state.loaded) |
33ed6329 AK |
2185 | return; |
2186 | ||
e1beb1d3 | 2187 | ++vmx->vcpu.stat.host_state_reload; |
a2fa3e9f | 2188 | vmx->host_state.loaded = 0; |
c8770e7b AK |
2189 | #ifdef CONFIG_X86_64 |
2190 | if (is_long_mode(&vmx->vcpu)) | |
2191 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); | |
2192 | #endif | |
152d3f2f | 2193 | if (vmx->host_state.gs_ldt_reload_needed) { |
d6e88aec | 2194 | kvm_load_ldt(vmx->host_state.ldt_sel); |
33ed6329 | 2195 | #ifdef CONFIG_X86_64 |
9581d442 | 2196 | load_gs_index(vmx->host_state.gs_sel); |
9581d442 AK |
2197 | #else |
2198 | loadsegment(gs, vmx->host_state.gs_sel); | |
33ed6329 | 2199 | #endif |
33ed6329 | 2200 | } |
0a77fe4c AK |
2201 | if (vmx->host_state.fs_reload_needed) |
2202 | loadsegment(fs, vmx->host_state.fs_sel); | |
b2da15ac AK |
2203 | #ifdef CONFIG_X86_64 |
2204 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { | |
2205 | loadsegment(ds, vmx->host_state.ds_sel); | |
2206 | loadsegment(es, vmx->host_state.es_sel); | |
2207 | } | |
b2da15ac | 2208 | #endif |
b7ffc44d | 2209 | invalidate_tss_limit(); |
44ea2b17 | 2210 | #ifdef CONFIG_X86_64 |
c8770e7b | 2211 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
44ea2b17 | 2212 | #endif |
da8999d3 LJ |
2213 | if (vmx->host_state.msr_host_bndcfgs) |
2214 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
45fc8757 | 2215 | load_fixmap_gdt(raw_smp_processor_id()); |
33ed6329 AK |
2216 | } |
2217 | ||
a9b21b62 AK |
2218 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
2219 | { | |
2220 | preempt_disable(); | |
2221 | __vmx_load_host_state(vmx); | |
2222 | preempt_enable(); | |
2223 | } | |
2224 | ||
28b835d6 FW |
2225 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
2226 | { | |
2227 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2228 | struct pi_desc old, new; | |
2229 | unsigned int dest; | |
2230 | ||
31afb2ea PB |
2231 | /* |
2232 | * In case of hot-plug or hot-unplug, we may have to undo | |
2233 | * vmx_vcpu_pi_put even if there is no assigned device. And we | |
2234 | * always keep PI.NDST up to date for simplicity: it makes the | |
2235 | * code easier, and CPU migration is not a fast path. | |
2236 | */ | |
2237 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) | |
28b835d6 FW |
2238 | return; |
2239 | ||
31afb2ea PB |
2240 | /* |
2241 | * First handle the simple case where no cmpxchg is necessary; just | |
2242 | * allow posting non-urgent interrupts. | |
2243 | * | |
2244 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change | |
2245 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler | |
2246 | * expects the VCPU to be on the blocked_vcpu_list that matches | |
2247 | * PI.NDST. | |
2248 | */ | |
2249 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || | |
2250 | vcpu->cpu == cpu) { | |
2251 | pi_clear_sn(pi_desc); | |
28b835d6 | 2252 | return; |
31afb2ea | 2253 | } |
28b835d6 | 2254 | |
31afb2ea | 2255 | /* The full case. */ |
28b835d6 FW |
2256 | do { |
2257 | old.control = new.control = pi_desc->control; | |
2258 | ||
31afb2ea | 2259 | dest = cpu_physical_id(cpu); |
28b835d6 | 2260 | |
31afb2ea PB |
2261 | if (x2apic_enabled()) |
2262 | new.ndst = dest; | |
2263 | else | |
2264 | new.ndst = (dest << 8) & 0xFF00; | |
28b835d6 | 2265 | |
28b835d6 | 2266 | new.sn = 0; |
c0a1666b PB |
2267 | } while (cmpxchg64(&pi_desc->control, old.control, |
2268 | new.control) != old.control); | |
28b835d6 | 2269 | } |
1be0e61c | 2270 | |
c95ba92a PF |
2271 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
2272 | { | |
2273 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; | |
2274 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); | |
2275 | } | |
2276 | ||
6aa8b732 AK |
2277 | /* |
2278 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes | |
2279 | * vcpu mutex is already taken. | |
2280 | */ | |
15ad7146 | 2281 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
6aa8b732 | 2282 | { |
a2fa3e9f | 2283 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b80c76ec | 2284 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
6aa8b732 | 2285 | |
b80c76ec | 2286 | if (!already_loaded) { |
fe0e80be | 2287 | loaded_vmcs_clear(vmx->loaded_vmcs); |
92fe13be | 2288 | local_irq_disable(); |
8f536b76 | 2289 | crash_disable_local_vmclear(cpu); |
5a560f8b XG |
2290 | |
2291 | /* | |
2292 | * Read loaded_vmcs->cpu should be before fetching | |
2293 | * loaded_vmcs->loaded_vmcss_on_cpu_link. | |
2294 | * See the comments in __loaded_vmcs_clear(). | |
2295 | */ | |
2296 | smp_rmb(); | |
2297 | ||
d462b819 NHE |
2298 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
2299 | &per_cpu(loaded_vmcss_on_cpu, cpu)); | |
8f536b76 | 2300 | crash_enable_local_vmclear(cpu); |
92fe13be | 2301 | local_irq_enable(); |
b80c76ec JM |
2302 | } |
2303 | ||
2304 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { | |
2305 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; | |
2306 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
2307 | } | |
2308 | ||
2309 | if (!already_loaded) { | |
59c58ceb | 2310 | void *gdt = get_current_gdt_ro(); |
b80c76ec JM |
2311 | unsigned long sysenter_esp; |
2312 | ||
2313 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); | |
92fe13be | 2314 | |
6aa8b732 AK |
2315 | /* |
2316 | * Linux uses per-cpu TSS and GDT, so set these when switching | |
e0c23063 | 2317 | * processors. See 22.2.4. |
6aa8b732 | 2318 | */ |
e0c23063 AL |
2319 | vmcs_writel(HOST_TR_BASE, |
2320 | (unsigned long)this_cpu_ptr(&cpu_tss)); | |
59c58ceb | 2321 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
6aa8b732 | 2322 | |
b7ffc44d AL |
2323 | /* |
2324 | * VM exits change the host TR limit to 0x67 after a VM | |
2325 | * exit. This is okay, since 0x67 covers everything except | |
2326 | * the IO bitmap and have have code to handle the IO bitmap | |
2327 | * being lost after a VM exit. | |
2328 | */ | |
2329 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); | |
2330 | ||
6aa8b732 AK |
2331 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
2332 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ | |
ff2c3a18 | 2333 | |
d462b819 | 2334 | vmx->loaded_vmcs->cpu = cpu; |
6aa8b732 | 2335 | } |
28b835d6 | 2336 | |
2680d6da OH |
2337 | /* Setup TSC multiplier */ |
2338 | if (kvm_has_tsc_control && | |
c95ba92a PF |
2339 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
2340 | decache_tsc_multiplier(vmx); | |
2680d6da | 2341 | |
28b835d6 | 2342 | vmx_vcpu_pi_load(vcpu, cpu); |
1be0e61c | 2343 | vmx->host_pkru = read_pkru(); |
74c55931 | 2344 | vmx->host_debugctlmsr = get_debugctlmsr(); |
28b835d6 FW |
2345 | } |
2346 | ||
2347 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) | |
2348 | { | |
2349 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2350 | ||
2351 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
2352 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
2353 | !kvm_vcpu_apicv_active(vcpu)) | |
28b835d6 FW |
2354 | return; |
2355 | ||
2356 | /* Set SN when the vCPU is preempted */ | |
2357 | if (vcpu->preempted) | |
2358 | pi_set_sn(pi_desc); | |
6aa8b732 AK |
2359 | } |
2360 | ||
2361 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) | |
2362 | { | |
28b835d6 FW |
2363 | vmx_vcpu_pi_put(vcpu); |
2364 | ||
a9b21b62 | 2365 | __vmx_load_host_state(to_vmx(vcpu)); |
6aa8b732 AK |
2366 | } |
2367 | ||
f244deed WL |
2368 | static bool emulation_required(struct kvm_vcpu *vcpu) |
2369 | { | |
2370 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); | |
2371 | } | |
2372 | ||
edcafe3c AK |
2373 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
2374 | ||
fe3ef05c NHE |
2375 | /* |
2376 | * Return the cr0 value that a nested guest would read. This is a combination | |
2377 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by | |
2378 | * its hypervisor (cr0_read_shadow). | |
2379 | */ | |
2380 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) | |
2381 | { | |
2382 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | | |
2383 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); | |
2384 | } | |
2385 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) | |
2386 | { | |
2387 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | | |
2388 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); | |
2389 | } | |
2390 | ||
6aa8b732 AK |
2391 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
2392 | { | |
78ac8b47 | 2393 | unsigned long rflags, save_rflags; |
345dcaa8 | 2394 | |
6de12732 AK |
2395 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
2396 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); | |
2397 | rflags = vmcs_readl(GUEST_RFLAGS); | |
2398 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
2399 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2400 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; | |
2401 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2402 | } | |
2403 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 | 2404 | } |
6de12732 | 2405 | return to_vmx(vcpu)->rflags; |
6aa8b732 AK |
2406 | } |
2407 | ||
2408 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
2409 | { | |
f244deed WL |
2410 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
2411 | ||
6de12732 AK |
2412 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
2413 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 AK |
2414 | if (to_vmx(vcpu)->rmode.vm86_active) { |
2415 | to_vmx(vcpu)->rmode.save_rflags = rflags; | |
053de044 | 2416 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
78ac8b47 | 2417 | } |
6aa8b732 | 2418 | vmcs_writel(GUEST_RFLAGS, rflags); |
f244deed WL |
2419 | |
2420 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) | |
2421 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); | |
6aa8b732 AK |
2422 | } |
2423 | ||
37ccdcbe | 2424 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
2809f5d2 GC |
2425 | { |
2426 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2427 | int ret = 0; | |
2428 | ||
2429 | if (interruptibility & GUEST_INTR_STATE_STI) | |
48005f64 | 2430 | ret |= KVM_X86_SHADOW_INT_STI; |
2809f5d2 | 2431 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
48005f64 | 2432 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
2809f5d2 | 2433 | |
37ccdcbe | 2434 | return ret; |
2809f5d2 GC |
2435 | } |
2436 | ||
2437 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) | |
2438 | { | |
2439 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2440 | u32 interruptibility = interruptibility_old; | |
2441 | ||
2442 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); | |
2443 | ||
48005f64 | 2444 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
2809f5d2 | 2445 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
48005f64 | 2446 | else if (mask & KVM_X86_SHADOW_INT_STI) |
2809f5d2 GC |
2447 | interruptibility |= GUEST_INTR_STATE_STI; |
2448 | ||
2449 | if ((interruptibility != interruptibility_old)) | |
2450 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); | |
2451 | } | |
2452 | ||
6aa8b732 AK |
2453 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
2454 | { | |
2455 | unsigned long rip; | |
6aa8b732 | 2456 | |
5fdbf976 | 2457 | rip = kvm_rip_read(vcpu); |
6aa8b732 | 2458 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
5fdbf976 | 2459 | kvm_rip_write(vcpu, rip); |
6aa8b732 | 2460 | |
2809f5d2 GC |
2461 | /* skipping an emulated instruction also counts */ |
2462 | vmx_set_interrupt_shadow(vcpu, 0); | |
6aa8b732 AK |
2463 | } |
2464 | ||
b96fb439 PB |
2465 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
2466 | unsigned long exit_qual) | |
2467 | { | |
2468 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
2469 | unsigned int nr = vcpu->arch.exception.nr; | |
2470 | u32 intr_info = nr | INTR_INFO_VALID_MASK; | |
2471 | ||
2472 | if (vcpu->arch.exception.has_error_code) { | |
2473 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; | |
2474 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; | |
2475 | } | |
2476 | ||
2477 | if (kvm_exception_is_soft(nr)) | |
2478 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; | |
2479 | else | |
2480 | intr_info |= INTR_TYPE_HARD_EXCEPTION; | |
2481 | ||
2482 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && | |
2483 | vmx_get_nmi_mask(vcpu)) | |
2484 | intr_info |= INTR_INFO_UNBLOCK_NMI; | |
2485 | ||
2486 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); | |
2487 | } | |
2488 | ||
0b6ac343 NHE |
2489 | /* |
2490 | * KVM wants to inject page-faults which it got to the guest. This function | |
2491 | * checks whether in a nested guest, we need to inject them to L1 or L2. | |
0b6ac343 | 2492 | */ |
bfcf83b1 | 2493 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
0b6ac343 NHE |
2494 | { |
2495 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
adfe20fb | 2496 | unsigned int nr = vcpu->arch.exception.nr; |
0b6ac343 | 2497 | |
b96fb439 PB |
2498 | if (nr == PF_VECTOR) { |
2499 | if (vcpu->arch.exception.nested_apf) { | |
bfcf83b1 | 2500 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
b96fb439 PB |
2501 | return 1; |
2502 | } | |
2503 | /* | |
2504 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. | |
2505 | * The fix is to add the ancillary datum (CR2 or DR6) to structs | |
2506 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 | |
2507 | * can be written only when inject_pending_event runs. This should be | |
2508 | * conditional on a new capability---if the capability is disabled, | |
2509 | * kvm_multiple_exception would write the ancillary information to | |
2510 | * CR2 or DR6, for backwards ABI-compatibility. | |
2511 | */ | |
2512 | if (nested_vmx_is_page_fault_vmexit(vmcs12, | |
2513 | vcpu->arch.exception.error_code)) { | |
bfcf83b1 | 2514 | *exit_qual = vcpu->arch.cr2; |
b96fb439 PB |
2515 | return 1; |
2516 | } | |
2517 | } else { | |
b96fb439 | 2518 | if (vmcs12->exception_bitmap & (1u << nr)) { |
bfcf83b1 WL |
2519 | if (nr == DB_VECTOR) |
2520 | *exit_qual = vcpu->arch.dr6; | |
2521 | else | |
2522 | *exit_qual = 0; | |
b96fb439 PB |
2523 | return 1; |
2524 | } | |
adfe20fb WL |
2525 | } |
2526 | ||
b96fb439 | 2527 | return 0; |
0b6ac343 NHE |
2528 | } |
2529 | ||
cfcd20e5 | 2530 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
298101da | 2531 | { |
77ab6db0 | 2532 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
cfcd20e5 WL |
2533 | unsigned nr = vcpu->arch.exception.nr; |
2534 | bool has_error_code = vcpu->arch.exception.has_error_code; | |
cfcd20e5 | 2535 | u32 error_code = vcpu->arch.exception.error_code; |
8ab2d2e2 | 2536 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
77ab6db0 | 2537 | |
8ab2d2e2 | 2538 | if (has_error_code) { |
77ab6db0 | 2539 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
8ab2d2e2 JK |
2540 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
2541 | } | |
77ab6db0 | 2542 | |
7ffd92c5 | 2543 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
2544 | int inc_eip = 0; |
2545 | if (kvm_exception_is_soft(nr)) | |
2546 | inc_eip = vcpu->arch.event_exit_inst_len; | |
2547 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) | |
a92601bb | 2548 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
77ab6db0 JK |
2549 | return; |
2550 | } | |
2551 | ||
66fd3f7f GN |
2552 | if (kvm_exception_is_soft(nr)) { |
2553 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
2554 | vmx->vcpu.arch.event_exit_inst_len); | |
8ab2d2e2 JK |
2555 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
2556 | } else | |
2557 | intr_info |= INTR_TYPE_HARD_EXCEPTION; | |
2558 | ||
2559 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); | |
298101da AK |
2560 | } |
2561 | ||
4e47c7a6 SY |
2562 | static bool vmx_rdtscp_supported(void) |
2563 | { | |
2564 | return cpu_has_vmx_rdtscp(); | |
2565 | } | |
2566 | ||
ad756a16 MJ |
2567 | static bool vmx_invpcid_supported(void) |
2568 | { | |
2569 | return cpu_has_vmx_invpcid() && enable_ept; | |
2570 | } | |
2571 | ||
a75beee6 ED |
2572 | /* |
2573 | * Swap MSR entry in host/guest MSR entry array. | |
2574 | */ | |
8b9cf98c | 2575 | static void move_msr_up(struct vcpu_vmx *vmx, int from, int to) |
a75beee6 | 2576 | { |
26bb0981 | 2577 | struct shared_msr_entry tmp; |
a2fa3e9f GH |
2578 | |
2579 | tmp = vmx->guest_msrs[to]; | |
2580 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; | |
2581 | vmx->guest_msrs[from] = tmp; | |
a75beee6 ED |
2582 | } |
2583 | ||
8d14695f YZ |
2584 | static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu) |
2585 | { | |
2586 | unsigned long *msr_bitmap; | |
2587 | ||
670125bd | 2588 | if (is_guest_mode(vcpu)) |
d048c098 | 2589 | msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap; |
3ce424e4 RK |
2590 | else if (cpu_has_secondary_exec_ctrls() && |
2591 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & | |
2592 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { | |
f6e90f9e WL |
2593 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) { |
2594 | if (is_long_mode(vcpu)) | |
c63e4563 | 2595 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv; |
f6e90f9e | 2596 | else |
c63e4563 | 2597 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv; |
f6e90f9e WL |
2598 | } else { |
2599 | if (is_long_mode(vcpu)) | |
c63e4563 | 2600 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic; |
f6e90f9e | 2601 | else |
c63e4563 | 2602 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic; |
f6e90f9e | 2603 | } |
8d14695f YZ |
2604 | } else { |
2605 | if (is_long_mode(vcpu)) | |
2606 | msr_bitmap = vmx_msr_bitmap_longmode; | |
2607 | else | |
2608 | msr_bitmap = vmx_msr_bitmap_legacy; | |
2609 | } | |
2610 | ||
2611 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); | |
2612 | } | |
2613 | ||
e38aea3e AK |
2614 | /* |
2615 | * Set up the vmcs to automatically save and restore system | |
2616 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy | |
2617 | * mode, as fiddling with msrs is very expensive. | |
2618 | */ | |
8b9cf98c | 2619 | static void setup_msrs(struct vcpu_vmx *vmx) |
e38aea3e | 2620 | { |
26bb0981 | 2621 | int save_nmsrs, index; |
e38aea3e | 2622 | |
a75beee6 ED |
2623 | save_nmsrs = 0; |
2624 | #ifdef CONFIG_X86_64 | |
8b9cf98c | 2625 | if (is_long_mode(&vmx->vcpu)) { |
8b9cf98c | 2626 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
a75beee6 | 2627 | if (index >= 0) |
8b9cf98c RR |
2628 | move_msr_up(vmx, index, save_nmsrs++); |
2629 | index = __find_msr_index(vmx, MSR_LSTAR); | |
a75beee6 | 2630 | if (index >= 0) |
8b9cf98c RR |
2631 | move_msr_up(vmx, index, save_nmsrs++); |
2632 | index = __find_msr_index(vmx, MSR_CSTAR); | |
a75beee6 | 2633 | if (index >= 0) |
8b9cf98c | 2634 | move_msr_up(vmx, index, save_nmsrs++); |
4e47c7a6 | 2635 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
d6321d49 | 2636 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
4e47c7a6 | 2637 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 | 2638 | /* |
8c06585d | 2639 | * MSR_STAR is only needed on long mode guests, and only |
a75beee6 ED |
2640 | * if efer.sce is enabled. |
2641 | */ | |
8c06585d | 2642 | index = __find_msr_index(vmx, MSR_STAR); |
f6801dff | 2643 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
8b9cf98c | 2644 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 ED |
2645 | } |
2646 | #endif | |
92c0d900 AK |
2647 | index = __find_msr_index(vmx, MSR_EFER); |
2648 | if (index >= 0 && update_transition_efer(vmx, index)) | |
26bb0981 | 2649 | move_msr_up(vmx, index, save_nmsrs++); |
e38aea3e | 2650 | |
26bb0981 | 2651 | vmx->save_nmsrs = save_nmsrs; |
5897297b | 2652 | |
8d14695f YZ |
2653 | if (cpu_has_vmx_msr_bitmap()) |
2654 | vmx_set_msr_bitmap(&vmx->vcpu); | |
e38aea3e AK |
2655 | } |
2656 | ||
6aa8b732 AK |
2657 | /* |
2658 | * reads and returns guest's timestamp counter "register" | |
be7b263e HZ |
2659 | * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset |
2660 | * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3 | |
6aa8b732 | 2661 | */ |
be7b263e | 2662 | static u64 guest_read_tsc(struct kvm_vcpu *vcpu) |
6aa8b732 AK |
2663 | { |
2664 | u64 host_tsc, tsc_offset; | |
2665 | ||
4ea1636b | 2666 | host_tsc = rdtsc(); |
6aa8b732 | 2667 | tsc_offset = vmcs_read64(TSC_OFFSET); |
be7b263e | 2668 | return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; |
6aa8b732 AK |
2669 | } |
2670 | ||
2671 | /* | |
99e3e30a | 2672 | * writes 'offset' into guest's timestamp counter offset register |
6aa8b732 | 2673 | */ |
99e3e30a | 2674 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
6aa8b732 | 2675 | { |
27fc51b2 | 2676 | if (is_guest_mode(vcpu)) { |
7991825b | 2677 | /* |
27fc51b2 NHE |
2678 | * We're here if L1 chose not to trap WRMSR to TSC. According |
2679 | * to the spec, this should set L1's TSC; The offset that L1 | |
2680 | * set for L2 remains unchanged, and still needs to be added | |
2681 | * to the newly set TSC to get L2's TSC. | |
7991825b | 2682 | */ |
27fc51b2 | 2683 | struct vmcs12 *vmcs12; |
27fc51b2 NHE |
2684 | /* recalculate vmcs02.TSC_OFFSET: */ |
2685 | vmcs12 = get_vmcs12(vcpu); | |
2686 | vmcs_write64(TSC_OFFSET, offset + | |
2687 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? | |
2688 | vmcs12->tsc_offset : 0)); | |
2689 | } else { | |
489223ed YY |
2690 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
2691 | vmcs_read64(TSC_OFFSET), offset); | |
27fc51b2 NHE |
2692 | vmcs_write64(TSC_OFFSET, offset); |
2693 | } | |
6aa8b732 AK |
2694 | } |
2695 | ||
801d3424 NHE |
2696 | /* |
2697 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX | |
2698 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for | |
2699 | * all guests if the "nested" module option is off, and can also be disabled | |
2700 | * for a single guest by disabling its VMX cpuid bit. | |
2701 | */ | |
2702 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) | |
2703 | { | |
d6321d49 | 2704 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
801d3424 NHE |
2705 | } |
2706 | ||
b87a51ae NHE |
2707 | /* |
2708 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be | |
2709 | * returned for the various VMX controls MSRs when nested VMX is enabled. | |
2710 | * The same values should also be used to verify that vmcs12 control fields are | |
2711 | * valid during nested entry from L1 to L2. | |
2712 | * Each of these control msrs has a low and high 32-bit half: A low bit is on | |
2713 | * if the corresponding bit in the (32-bit) control field *must* be on, and a | |
2714 | * bit in the high half is on if the corresponding bit in the control field | |
2715 | * may be on. See also vmx_control_verify(). | |
b87a51ae | 2716 | */ |
b9c237bb | 2717 | static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) |
b87a51ae NHE |
2718 | { |
2719 | /* | |
2720 | * Note that as a general rule, the high half of the MSRs (bits in | |
2721 | * the control fields which may be 1) should be initialized by the | |
2722 | * intersection of the underlying hardware's MSR (i.e., features which | |
2723 | * can be supported) and the list of features we want to expose - | |
2724 | * because they are known to be properly supported in our code. | |
2725 | * Also, usually, the low half of the MSRs (bits which must be 1) can | |
2726 | * be set to 0, meaning that L1 may turn off any of these bits. The | |
2727 | * reason is that if one of these bits is necessary, it will appear | |
2728 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control | |
2729 | * fields of vmcs01 and vmcs02, will turn these bits off - and | |
7313c698 | 2730 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
b87a51ae NHE |
2731 | * These rules have exceptions below. |
2732 | */ | |
2733 | ||
2734 | /* pin-based controls */ | |
eabeaacc | 2735 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
b9c237bb WV |
2736 | vmx->nested.nested_vmx_pinbased_ctls_low, |
2737 | vmx->nested.nested_vmx_pinbased_ctls_high); | |
2738 | vmx->nested.nested_vmx_pinbased_ctls_low |= | |
2739 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
2740 | vmx->nested.nested_vmx_pinbased_ctls_high &= | |
2741 | PIN_BASED_EXT_INTR_MASK | | |
2742 | PIN_BASED_NMI_EXITING | | |
2743 | PIN_BASED_VIRTUAL_NMIS; | |
2744 | vmx->nested.nested_vmx_pinbased_ctls_high |= | |
2745 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | | |
0238ea91 | 2746 | PIN_BASED_VMX_PREEMPTION_TIMER; |
d62caabb | 2747 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) |
705699a1 WV |
2748 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
2749 | PIN_BASED_POSTED_INTR; | |
b87a51ae | 2750 | |
3dbcd8da | 2751 | /* exit controls */ |
c0dfee58 | 2752 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
b9c237bb WV |
2753 | vmx->nested.nested_vmx_exit_ctls_low, |
2754 | vmx->nested.nested_vmx_exit_ctls_high); | |
2755 | vmx->nested.nested_vmx_exit_ctls_low = | |
2756 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; | |
e0ba1a6f | 2757 | |
b9c237bb | 2758 | vmx->nested.nested_vmx_exit_ctls_high &= |
b87a51ae | 2759 | #ifdef CONFIG_X86_64 |
c0dfee58 | 2760 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
b87a51ae | 2761 | #endif |
f4124500 | 2762 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
b9c237bb WV |
2763 | vmx->nested.nested_vmx_exit_ctls_high |= |
2764 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | | |
f4124500 | 2765 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
e0ba1a6f BD |
2766 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
2767 | ||
a87036ad | 2768 | if (kvm_mpx_supported()) |
b9c237bb | 2769 | vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
b87a51ae | 2770 | |
2996fca0 | 2771 | /* We support free control of debug control saving. */ |
0115f9cb | 2772 | vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
2996fca0 | 2773 | |
b87a51ae NHE |
2774 | /* entry controls */ |
2775 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, | |
b9c237bb WV |
2776 | vmx->nested.nested_vmx_entry_ctls_low, |
2777 | vmx->nested.nested_vmx_entry_ctls_high); | |
2778 | vmx->nested.nested_vmx_entry_ctls_low = | |
2779 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; | |
2780 | vmx->nested.nested_vmx_entry_ctls_high &= | |
57435349 JK |
2781 | #ifdef CONFIG_X86_64 |
2782 | VM_ENTRY_IA32E_MODE | | |
2783 | #endif | |
2784 | VM_ENTRY_LOAD_IA32_PAT; | |
b9c237bb WV |
2785 | vmx->nested.nested_vmx_entry_ctls_high |= |
2786 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); | |
a87036ad | 2787 | if (kvm_mpx_supported()) |
b9c237bb | 2788 | vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
57435349 | 2789 | |
2996fca0 | 2790 | /* We support free control of debug control loading. */ |
0115f9cb | 2791 | vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
2996fca0 | 2792 | |
b87a51ae NHE |
2793 | /* cpu-based controls */ |
2794 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, | |
b9c237bb WV |
2795 | vmx->nested.nested_vmx_procbased_ctls_low, |
2796 | vmx->nested.nested_vmx_procbased_ctls_high); | |
2797 | vmx->nested.nested_vmx_procbased_ctls_low = | |
2798 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
2799 | vmx->nested.nested_vmx_procbased_ctls_high &= | |
a294c9bb JK |
2800 | CPU_BASED_VIRTUAL_INTR_PENDING | |
2801 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | | |
b87a51ae NHE |
2802 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
2803 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | | |
2804 | CPU_BASED_CR3_STORE_EXITING | | |
2805 | #ifdef CONFIG_X86_64 | |
2806 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | | |
2807 | #endif | |
2808 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | | |
5f3d45e7 MD |
2809 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
2810 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | | |
2811 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | | |
2812 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
b87a51ae NHE |
2813 | /* |
2814 | * We can allow some features even when not supported by the | |
2815 | * hardware. For example, L1 can specify an MSR bitmap - and we | |
2816 | * can use it to avoid exits to L1 - even when L0 runs L2 | |
2817 | * without MSR bitmaps. | |
2818 | */ | |
b9c237bb WV |
2819 | vmx->nested.nested_vmx_procbased_ctls_high |= |
2820 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | | |
560b7ee1 | 2821 | CPU_BASED_USE_MSR_BITMAPS; |
b87a51ae | 2822 | |
3dcdf3ec | 2823 | /* We support free control of CR3 access interception. */ |
0115f9cb | 2824 | vmx->nested.nested_vmx_procbased_ctls_low &= |
3dcdf3ec JK |
2825 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
2826 | ||
80154d77 PB |
2827 | /* |
2828 | * secondary cpu-based controls. Do not include those that | |
2829 | * depend on CPUID bits, they are added later by vmx_cpuid_update. | |
2830 | */ | |
b87a51ae | 2831 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
b9c237bb WV |
2832 | vmx->nested.nested_vmx_secondary_ctls_low, |
2833 | vmx->nested.nested_vmx_secondary_ctls_high); | |
2834 | vmx->nested.nested_vmx_secondary_ctls_low = 0; | |
2835 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
d6851fbe | 2836 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
1b07304c | 2837 | SECONDARY_EXEC_DESC | |
f2b93280 | 2838 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
82f0dd4b | 2839 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
608406e2 | 2840 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
3db13480 | 2841 | SECONDARY_EXEC_WBINVD_EXITING; |
c18911a2 | 2842 | |
afa61f75 NHE |
2843 | if (enable_ept) { |
2844 | /* nested EPT: emulate EPT also to L1 */ | |
b9c237bb | 2845 | vmx->nested.nested_vmx_secondary_ctls_high |= |
0790ec17 | 2846 | SECONDARY_EXEC_ENABLE_EPT; |
b9c237bb | 2847 | vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
7db74265 | 2848 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
02120c45 BD |
2849 | if (cpu_has_vmx_ept_execute_only()) |
2850 | vmx->nested.nested_vmx_ept_caps |= | |
2851 | VMX_EPT_EXECUTE_ONLY_BIT; | |
b9c237bb | 2852 | vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept; |
45e11817 | 2853 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
7db74265 PB |
2854 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
2855 | VMX_EPT_1GB_PAGE_BIT; | |
03efce6f BD |
2856 | if (enable_ept_ad_bits) { |
2857 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2858 | SECONDARY_EXEC_ENABLE_PML; | |
7461fbc4 | 2859 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT; |
03efce6f | 2860 | } |
1c13bffd | 2861 | } |
afa61f75 | 2862 | |
27c42a1b BD |
2863 | if (cpu_has_vmx_vmfunc()) { |
2864 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2865 | SECONDARY_EXEC_ENABLE_VMFUNC; | |
41ab9372 BD |
2866 | /* |
2867 | * Advertise EPTP switching unconditionally | |
2868 | * since we emulate it | |
2869 | */ | |
575b3a2c WL |
2870 | if (enable_ept) |
2871 | vmx->nested.nested_vmx_vmfunc_controls = | |
2872 | VMX_VMFUNC_EPTP_SWITCHING; | |
27c42a1b BD |
2873 | } |
2874 | ||
ef697a71 PB |
2875 | /* |
2876 | * Old versions of KVM use the single-context version without | |
2877 | * checking for support, so declare that it is supported even | |
2878 | * though it is treated as global context. The alternative is | |
2879 | * not failing the single-context invvpid, and it is worse. | |
2880 | */ | |
63cb6d5f WL |
2881 | if (enable_vpid) { |
2882 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2883 | SECONDARY_EXEC_ENABLE_VPID; | |
089d7b6e | 2884 | vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT | |
bcdde302 | 2885 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
1c13bffd | 2886 | } |
99b83ac8 | 2887 | |
0790ec17 RK |
2888 | if (enable_unrestricted_guest) |
2889 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2890 | SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
2891 | ||
c18911a2 | 2892 | /* miscellaneous data */ |
b9c237bb WV |
2893 | rdmsr(MSR_IA32_VMX_MISC, |
2894 | vmx->nested.nested_vmx_misc_low, | |
2895 | vmx->nested.nested_vmx_misc_high); | |
2896 | vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; | |
2897 | vmx->nested.nested_vmx_misc_low |= | |
2898 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | | |
f4124500 | 2899 | VMX_MISC_ACTIVITY_HLT; |
b9c237bb | 2900 | vmx->nested.nested_vmx_misc_high = 0; |
62cc6b9d DM |
2901 | |
2902 | /* | |
2903 | * This MSR reports some information about VMX support. We | |
2904 | * should return information about the VMX we emulate for the | |
2905 | * guest, and the VMCS structure we give it - not about the | |
2906 | * VMX support of the underlying hardware. | |
2907 | */ | |
2908 | vmx->nested.nested_vmx_basic = | |
2909 | VMCS12_REVISION | | |
2910 | VMX_BASIC_TRUE_CTLS | | |
2911 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | | |
2912 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); | |
2913 | ||
2914 | if (cpu_has_vmx_basic_inout()) | |
2915 | vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT; | |
2916 | ||
2917 | /* | |
8322ebbb | 2918 | * These MSRs specify bits which the guest must keep fixed on |
62cc6b9d DM |
2919 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
2920 | * We picked the standard core2 setting. | |
2921 | */ | |
2922 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) | |
2923 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE | |
2924 | vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON; | |
62cc6b9d | 2925 | vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON; |
8322ebbb DM |
2926 | |
2927 | /* These MSRs specify bits which the guest must keep fixed off. */ | |
2928 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1); | |
2929 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1); | |
62cc6b9d DM |
2930 | |
2931 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ | |
5b15706d | 2932 | vmx->nested.nested_vmx_vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
b87a51ae NHE |
2933 | } |
2934 | ||
3899152c DM |
2935 | /* |
2936 | * if fixed0[i] == 1: val[i] must be 1 | |
2937 | * if fixed1[i] == 0: val[i] must be 0 | |
2938 | */ | |
2939 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) | |
2940 | { | |
2941 | return ((val & fixed1) | fixed0) == val; | |
b87a51ae NHE |
2942 | } |
2943 | ||
2944 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) | |
2945 | { | |
3899152c | 2946 | return fixed_bits_valid(control, low, high); |
b87a51ae NHE |
2947 | } |
2948 | ||
2949 | static inline u64 vmx_control_msr(u32 low, u32 high) | |
2950 | { | |
2951 | return low | ((u64)high << 32); | |
2952 | } | |
2953 | ||
62cc6b9d DM |
2954 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
2955 | { | |
2956 | superset &= mask; | |
2957 | subset &= mask; | |
2958 | ||
2959 | return (superset | subset) == superset; | |
2960 | } | |
2961 | ||
2962 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) | |
2963 | { | |
2964 | const u64 feature_and_reserved = | |
2965 | /* feature (except bit 48; see below) */ | |
2966 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | | |
2967 | /* reserved */ | |
2968 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); | |
2969 | u64 vmx_basic = vmx->nested.nested_vmx_basic; | |
2970 | ||
2971 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) | |
2972 | return -EINVAL; | |
2973 | ||
2974 | /* | |
2975 | * KVM does not emulate a version of VMX that constrains physical | |
2976 | * addresses of VMX structures (e.g. VMCS) to 32-bits. | |
2977 | */ | |
2978 | if (data & BIT_ULL(48)) | |
2979 | return -EINVAL; | |
2980 | ||
2981 | if (vmx_basic_vmcs_revision_id(vmx_basic) != | |
2982 | vmx_basic_vmcs_revision_id(data)) | |
2983 | return -EINVAL; | |
2984 | ||
2985 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) | |
2986 | return -EINVAL; | |
2987 | ||
2988 | vmx->nested.nested_vmx_basic = data; | |
2989 | return 0; | |
2990 | } | |
2991 | ||
2992 | static int | |
2993 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
2994 | { | |
2995 | u64 supported; | |
2996 | u32 *lowp, *highp; | |
2997 | ||
2998 | switch (msr_index) { | |
2999 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3000 | lowp = &vmx->nested.nested_vmx_pinbased_ctls_low; | |
3001 | highp = &vmx->nested.nested_vmx_pinbased_ctls_high; | |
3002 | break; | |
3003 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3004 | lowp = &vmx->nested.nested_vmx_procbased_ctls_low; | |
3005 | highp = &vmx->nested.nested_vmx_procbased_ctls_high; | |
3006 | break; | |
3007 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3008 | lowp = &vmx->nested.nested_vmx_exit_ctls_low; | |
3009 | highp = &vmx->nested.nested_vmx_exit_ctls_high; | |
3010 | break; | |
3011 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3012 | lowp = &vmx->nested.nested_vmx_entry_ctls_low; | |
3013 | highp = &vmx->nested.nested_vmx_entry_ctls_high; | |
3014 | break; | |
3015 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
3016 | lowp = &vmx->nested.nested_vmx_secondary_ctls_low; | |
3017 | highp = &vmx->nested.nested_vmx_secondary_ctls_high; | |
3018 | break; | |
3019 | default: | |
3020 | BUG(); | |
3021 | } | |
3022 | ||
3023 | supported = vmx_control_msr(*lowp, *highp); | |
3024 | ||
3025 | /* Check must-be-1 bits are still 1. */ | |
3026 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) | |
3027 | return -EINVAL; | |
3028 | ||
3029 | /* Check must-be-0 bits are still 0. */ | |
3030 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) | |
3031 | return -EINVAL; | |
3032 | ||
3033 | *lowp = data; | |
3034 | *highp = data >> 32; | |
3035 | return 0; | |
3036 | } | |
3037 | ||
3038 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) | |
3039 | { | |
3040 | const u64 feature_and_reserved_bits = | |
3041 | /* feature */ | |
3042 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | | |
3043 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | | |
3044 | /* reserved */ | |
3045 | GENMASK_ULL(13, 9) | BIT_ULL(31); | |
3046 | u64 vmx_misc; | |
3047 | ||
3048 | vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low, | |
3049 | vmx->nested.nested_vmx_misc_high); | |
3050 | ||
3051 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) | |
3052 | return -EINVAL; | |
3053 | ||
3054 | if ((vmx->nested.nested_vmx_pinbased_ctls_high & | |
3055 | PIN_BASED_VMX_PREEMPTION_TIMER) && | |
3056 | vmx_misc_preemption_timer_rate(data) != | |
3057 | vmx_misc_preemption_timer_rate(vmx_misc)) | |
3058 | return -EINVAL; | |
3059 | ||
3060 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) | |
3061 | return -EINVAL; | |
3062 | ||
3063 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) | |
3064 | return -EINVAL; | |
3065 | ||
3066 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) | |
3067 | return -EINVAL; | |
3068 | ||
3069 | vmx->nested.nested_vmx_misc_low = data; | |
3070 | vmx->nested.nested_vmx_misc_high = data >> 32; | |
3071 | return 0; | |
3072 | } | |
3073 | ||
3074 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) | |
3075 | { | |
3076 | u64 vmx_ept_vpid_cap; | |
3077 | ||
3078 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps, | |
3079 | vmx->nested.nested_vmx_vpid_caps); | |
3080 | ||
3081 | /* Every bit is either reserved or a feature bit. */ | |
3082 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) | |
3083 | return -EINVAL; | |
3084 | ||
3085 | vmx->nested.nested_vmx_ept_caps = data; | |
3086 | vmx->nested.nested_vmx_vpid_caps = data >> 32; | |
3087 | return 0; | |
3088 | } | |
3089 | ||
3090 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
3091 | { | |
3092 | u64 *msr; | |
3093 | ||
3094 | switch (msr_index) { | |
3095 | case MSR_IA32_VMX_CR0_FIXED0: | |
3096 | msr = &vmx->nested.nested_vmx_cr0_fixed0; | |
3097 | break; | |
3098 | case MSR_IA32_VMX_CR4_FIXED0: | |
3099 | msr = &vmx->nested.nested_vmx_cr4_fixed0; | |
3100 | break; | |
3101 | default: | |
3102 | BUG(); | |
3103 | } | |
3104 | ||
3105 | /* | |
3106 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) | |
3107 | * must be 1 in the restored value. | |
3108 | */ | |
3109 | if (!is_bitwise_subset(data, *msr, -1ULL)) | |
3110 | return -EINVAL; | |
3111 | ||
3112 | *msr = data; | |
3113 | return 0; | |
3114 | } | |
3115 | ||
3116 | /* | |
3117 | * Called when userspace is restoring VMX MSRs. | |
3118 | * | |
3119 | * Returns 0 on success, non-0 otherwise. | |
3120 | */ | |
3121 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
b87a51ae | 3122 | { |
b9c237bb WV |
3123 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3124 | ||
b87a51ae | 3125 | switch (msr_index) { |
b87a51ae | 3126 | case MSR_IA32_VMX_BASIC: |
62cc6b9d DM |
3127 | return vmx_restore_vmx_basic(vmx, data); |
3128 | case MSR_IA32_VMX_PINBASED_CTLS: | |
3129 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
3130 | case MSR_IA32_VMX_EXIT_CTLS: | |
3131 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b87a51ae | 3132 | /* |
62cc6b9d DM |
3133 | * The "non-true" VMX capability MSRs are generated from the |
3134 | * "true" MSRs, so we do not support restoring them directly. | |
3135 | * | |
3136 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace | |
3137 | * should restore the "true" MSRs with the must-be-1 bits | |
3138 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND | |
3139 | * DEFAULT SETTINGS". | |
b87a51ae | 3140 | */ |
62cc6b9d DM |
3141 | return -EINVAL; |
3142 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3143 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3144 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3145 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3146 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
3147 | return vmx_restore_control_msr(vmx, msr_index, data); | |
3148 | case MSR_IA32_VMX_MISC: | |
3149 | return vmx_restore_vmx_misc(vmx, data); | |
3150 | case MSR_IA32_VMX_CR0_FIXED0: | |
3151 | case MSR_IA32_VMX_CR4_FIXED0: | |
3152 | return vmx_restore_fixed0_msr(vmx, msr_index, data); | |
3153 | case MSR_IA32_VMX_CR0_FIXED1: | |
3154 | case MSR_IA32_VMX_CR4_FIXED1: | |
3155 | /* | |
3156 | * These MSRs are generated based on the vCPU's CPUID, so we | |
3157 | * do not support restoring them directly. | |
3158 | */ | |
3159 | return -EINVAL; | |
3160 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
3161 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); | |
3162 | case MSR_IA32_VMX_VMCS_ENUM: | |
3163 | vmx->nested.nested_vmx_vmcs_enum = data; | |
3164 | return 0; | |
3165 | default: | |
b87a51ae | 3166 | /* |
62cc6b9d | 3167 | * The rest of the VMX capability MSRs do not support restore. |
b87a51ae | 3168 | */ |
62cc6b9d DM |
3169 | return -EINVAL; |
3170 | } | |
3171 | } | |
3172 | ||
3173 | /* Returns 0 on success, non-0 otherwise. */ | |
3174 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
3175 | { | |
3176 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
3177 | ||
3178 | switch (msr_index) { | |
3179 | case MSR_IA32_VMX_BASIC: | |
3180 | *pdata = vmx->nested.nested_vmx_basic; | |
b87a51ae NHE |
3181 | break; |
3182 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3183 | case MSR_IA32_VMX_PINBASED_CTLS: | |
b9c237bb WV |
3184 | *pdata = vmx_control_msr( |
3185 | vmx->nested.nested_vmx_pinbased_ctls_low, | |
3186 | vmx->nested.nested_vmx_pinbased_ctls_high); | |
0115f9cb DM |
3187 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
3188 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3189 | break; |
3190 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3191 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
b9c237bb WV |
3192 | *pdata = vmx_control_msr( |
3193 | vmx->nested.nested_vmx_procbased_ctls_low, | |
3194 | vmx->nested.nested_vmx_procbased_ctls_high); | |
0115f9cb DM |
3195 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
3196 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3197 | break; |
3198 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3199 | case MSR_IA32_VMX_EXIT_CTLS: | |
b9c237bb WV |
3200 | *pdata = vmx_control_msr( |
3201 | vmx->nested.nested_vmx_exit_ctls_low, | |
3202 | vmx->nested.nested_vmx_exit_ctls_high); | |
0115f9cb DM |
3203 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
3204 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3205 | break; |
3206 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3207 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b9c237bb WV |
3208 | *pdata = vmx_control_msr( |
3209 | vmx->nested.nested_vmx_entry_ctls_low, | |
3210 | vmx->nested.nested_vmx_entry_ctls_high); | |
0115f9cb DM |
3211 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
3212 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3213 | break; |
3214 | case MSR_IA32_VMX_MISC: | |
b9c237bb WV |
3215 | *pdata = vmx_control_msr( |
3216 | vmx->nested.nested_vmx_misc_low, | |
3217 | vmx->nested.nested_vmx_misc_high); | |
b87a51ae | 3218 | break; |
b87a51ae | 3219 | case MSR_IA32_VMX_CR0_FIXED0: |
62cc6b9d | 3220 | *pdata = vmx->nested.nested_vmx_cr0_fixed0; |
b87a51ae NHE |
3221 | break; |
3222 | case MSR_IA32_VMX_CR0_FIXED1: | |
62cc6b9d | 3223 | *pdata = vmx->nested.nested_vmx_cr0_fixed1; |
b87a51ae NHE |
3224 | break; |
3225 | case MSR_IA32_VMX_CR4_FIXED0: | |
62cc6b9d | 3226 | *pdata = vmx->nested.nested_vmx_cr4_fixed0; |
b87a51ae NHE |
3227 | break; |
3228 | case MSR_IA32_VMX_CR4_FIXED1: | |
62cc6b9d | 3229 | *pdata = vmx->nested.nested_vmx_cr4_fixed1; |
b87a51ae NHE |
3230 | break; |
3231 | case MSR_IA32_VMX_VMCS_ENUM: | |
62cc6b9d | 3232 | *pdata = vmx->nested.nested_vmx_vmcs_enum; |
b87a51ae NHE |
3233 | break; |
3234 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
b9c237bb WV |
3235 | *pdata = vmx_control_msr( |
3236 | vmx->nested.nested_vmx_secondary_ctls_low, | |
3237 | vmx->nested.nested_vmx_secondary_ctls_high); | |
b87a51ae NHE |
3238 | break; |
3239 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
089d7b6e WL |
3240 | *pdata = vmx->nested.nested_vmx_ept_caps | |
3241 | ((u64)vmx->nested.nested_vmx_vpid_caps << 32); | |
b87a51ae | 3242 | break; |
27c42a1b BD |
3243 | case MSR_IA32_VMX_VMFUNC: |
3244 | *pdata = vmx->nested.nested_vmx_vmfunc_controls; | |
3245 | break; | |
b87a51ae | 3246 | default: |
b87a51ae | 3247 | return 1; |
b3897a49 NHE |
3248 | } |
3249 | ||
b87a51ae NHE |
3250 | return 0; |
3251 | } | |
3252 | ||
37e4c997 HZ |
3253 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
3254 | uint64_t val) | |
3255 | { | |
3256 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; | |
3257 | ||
3258 | return !(val & ~valid_bits); | |
3259 | } | |
3260 | ||
6aa8b732 AK |
3261 | /* |
3262 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
3263 | * Returns 0 on success, non-0 otherwise. | |
3264 | * Assumes vcpu_load() was already called. | |
3265 | */ | |
609e36d3 | 3266 | static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3267 | { |
a6cb099a | 3268 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 3269 | struct shared_msr_entry *msr; |
6aa8b732 | 3270 | |
609e36d3 | 3271 | switch (msr_info->index) { |
05b3e0c2 | 3272 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3273 | case MSR_FS_BASE: |
609e36d3 | 3274 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
6aa8b732 AK |
3275 | break; |
3276 | case MSR_GS_BASE: | |
609e36d3 | 3277 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
6aa8b732 | 3278 | break; |
44ea2b17 | 3279 | case MSR_KERNEL_GS_BASE: |
a6cb099a BP |
3280 | vmx_load_host_state(vmx); |
3281 | msr_info->data = vmx->msr_guest_kernel_gs_base; | |
44ea2b17 | 3282 | break; |
26bb0981 | 3283 | #endif |
6aa8b732 | 3284 | case MSR_EFER: |
609e36d3 | 3285 | return kvm_get_msr_common(vcpu, msr_info); |
af24a4e4 | 3286 | case MSR_IA32_TSC: |
be7b263e | 3287 | msr_info->data = guest_read_tsc(vcpu); |
6aa8b732 AK |
3288 | break; |
3289 | case MSR_IA32_SYSENTER_CS: | |
609e36d3 | 3290 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
6aa8b732 AK |
3291 | break; |
3292 | case MSR_IA32_SYSENTER_EIP: | |
609e36d3 | 3293 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
6aa8b732 AK |
3294 | break; |
3295 | case MSR_IA32_SYSENTER_ESP: | |
609e36d3 | 3296 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
6aa8b732 | 3297 | break; |
0dd376e7 | 3298 | case MSR_IA32_BNDCFGS: |
691bd434 | 3299 | if (!kvm_mpx_supported() || |
d6321d49 RK |
3300 | (!msr_info->host_initiated && |
3301 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) | |
93c4adc7 | 3302 | return 1; |
609e36d3 | 3303 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
0dd376e7 | 3304 | break; |
c45dcc71 AR |
3305 | case MSR_IA32_MCG_EXT_CTL: |
3306 | if (!msr_info->host_initiated && | |
a6cb099a | 3307 | !(vmx->msr_ia32_feature_control & |
c45dcc71 | 3308 | FEATURE_CONTROL_LMCE)) |
cae50139 | 3309 | return 1; |
c45dcc71 AR |
3310 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
3311 | break; | |
cae50139 | 3312 | case MSR_IA32_FEATURE_CONTROL: |
a6cb099a | 3313 | msr_info->data = vmx->msr_ia32_feature_control; |
cae50139 JK |
3314 | break; |
3315 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
3316 | if (!nested_vmx_allowed(vcpu)) | |
3317 | return 1; | |
609e36d3 | 3318 | return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data); |
20300099 WL |
3319 | case MSR_IA32_XSS: |
3320 | if (!vmx_xsaves_supported()) | |
3321 | return 1; | |
609e36d3 | 3322 | msr_info->data = vcpu->arch.ia32_xss; |
20300099 | 3323 | break; |
4e47c7a6 | 3324 | case MSR_TSC_AUX: |
d6321d49 RK |
3325 | if (!msr_info->host_initiated && |
3326 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) | |
4e47c7a6 SY |
3327 | return 1; |
3328 | /* Otherwise falls through */ | |
6aa8b732 | 3329 | default: |
a6cb099a | 3330 | msr = find_msr_entry(vmx, msr_info->index); |
3bab1f5d | 3331 | if (msr) { |
609e36d3 | 3332 | msr_info->data = msr->data; |
3bab1f5d | 3333 | break; |
6aa8b732 | 3334 | } |
609e36d3 | 3335 | return kvm_get_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3336 | } |
3337 | ||
6aa8b732 AK |
3338 | return 0; |
3339 | } | |
3340 | ||
cae50139 JK |
3341 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
3342 | ||
6aa8b732 AK |
3343 | /* |
3344 | * Writes msr value into into the appropriate "register". | |
3345 | * Returns 0 on success, non-0 otherwise. | |
3346 | * Assumes vcpu_load() was already called. | |
3347 | */ | |
8fe8ab46 | 3348 | static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3349 | { |
a2fa3e9f | 3350 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 3351 | struct shared_msr_entry *msr; |
2cc51560 | 3352 | int ret = 0; |
8fe8ab46 WA |
3353 | u32 msr_index = msr_info->index; |
3354 | u64 data = msr_info->data; | |
2cc51560 | 3355 | |
6aa8b732 | 3356 | switch (msr_index) { |
3bab1f5d | 3357 | case MSR_EFER: |
8fe8ab46 | 3358 | ret = kvm_set_msr_common(vcpu, msr_info); |
2cc51560 | 3359 | break; |
16175a79 | 3360 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3361 | case MSR_FS_BASE: |
2fb92db1 | 3362 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3363 | vmcs_writel(GUEST_FS_BASE, data); |
3364 | break; | |
3365 | case MSR_GS_BASE: | |
2fb92db1 | 3366 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3367 | vmcs_writel(GUEST_GS_BASE, data); |
3368 | break; | |
44ea2b17 AK |
3369 | case MSR_KERNEL_GS_BASE: |
3370 | vmx_load_host_state(vmx); | |
3371 | vmx->msr_guest_kernel_gs_base = data; | |
3372 | break; | |
6aa8b732 AK |
3373 | #endif |
3374 | case MSR_IA32_SYSENTER_CS: | |
3375 | vmcs_write32(GUEST_SYSENTER_CS, data); | |
3376 | break; | |
3377 | case MSR_IA32_SYSENTER_EIP: | |
f5b42c33 | 3378 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
6aa8b732 AK |
3379 | break; |
3380 | case MSR_IA32_SYSENTER_ESP: | |
f5b42c33 | 3381 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
6aa8b732 | 3382 | break; |
0dd376e7 | 3383 | case MSR_IA32_BNDCFGS: |
691bd434 | 3384 | if (!kvm_mpx_supported() || |
d6321d49 RK |
3385 | (!msr_info->host_initiated && |
3386 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) | |
93c4adc7 | 3387 | return 1; |
fd8cb433 | 3388 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
4531662d | 3389 | (data & MSR_IA32_BNDCFGS_RSVD)) |
93c4adc7 | 3390 | return 1; |
0dd376e7 LJ |
3391 | vmcs_write64(GUEST_BNDCFGS, data); |
3392 | break; | |
af24a4e4 | 3393 | case MSR_IA32_TSC: |
8fe8ab46 | 3394 | kvm_write_tsc(vcpu, msr_info); |
6aa8b732 | 3395 | break; |
468d472f SY |
3396 | case MSR_IA32_CR_PAT: |
3397 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { | |
4566654b NA |
3398 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
3399 | return 1; | |
468d472f SY |
3400 | vmcs_write64(GUEST_IA32_PAT, data); |
3401 | vcpu->arch.pat = data; | |
3402 | break; | |
3403 | } | |
8fe8ab46 | 3404 | ret = kvm_set_msr_common(vcpu, msr_info); |
4e47c7a6 | 3405 | break; |
ba904635 WA |
3406 | case MSR_IA32_TSC_ADJUST: |
3407 | ret = kvm_set_msr_common(vcpu, msr_info); | |
4e47c7a6 | 3408 | break; |
c45dcc71 AR |
3409 | case MSR_IA32_MCG_EXT_CTL: |
3410 | if ((!msr_info->host_initiated && | |
3411 | !(to_vmx(vcpu)->msr_ia32_feature_control & | |
3412 | FEATURE_CONTROL_LMCE)) || | |
3413 | (data & ~MCG_EXT_CTL_LMCE_EN)) | |
3414 | return 1; | |
3415 | vcpu->arch.mcg_ext_ctl = data; | |
3416 | break; | |
cae50139 | 3417 | case MSR_IA32_FEATURE_CONTROL: |
37e4c997 | 3418 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
3b84080b | 3419 | (to_vmx(vcpu)->msr_ia32_feature_control & |
cae50139 JK |
3420 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
3421 | return 1; | |
3b84080b | 3422 | vmx->msr_ia32_feature_control = data; |
cae50139 JK |
3423 | if (msr_info->host_initiated && data == 0) |
3424 | vmx_leave_nested(vcpu); | |
3425 | break; | |
3426 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
62cc6b9d DM |
3427 | if (!msr_info->host_initiated) |
3428 | return 1; /* they are read-only */ | |
3429 | if (!nested_vmx_allowed(vcpu)) | |
3430 | return 1; | |
3431 | return vmx_set_vmx_msr(vcpu, msr_index, data); | |
20300099 WL |
3432 | case MSR_IA32_XSS: |
3433 | if (!vmx_xsaves_supported()) | |
3434 | return 1; | |
3435 | /* | |
3436 | * The only supported bit as of Skylake is bit 8, but | |
3437 | * it is not supported on KVM. | |
3438 | */ | |
3439 | if (data != 0) | |
3440 | return 1; | |
3441 | vcpu->arch.ia32_xss = data; | |
3442 | if (vcpu->arch.ia32_xss != host_xss) | |
3443 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, | |
3444 | vcpu->arch.ia32_xss, host_xss); | |
3445 | else | |
3446 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); | |
3447 | break; | |
4e47c7a6 | 3448 | case MSR_TSC_AUX: |
d6321d49 RK |
3449 | if (!msr_info->host_initiated && |
3450 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) | |
4e47c7a6 SY |
3451 | return 1; |
3452 | /* Check reserved bit, higher 32 bits should be zero */ | |
3453 | if ((data >> 32) != 0) | |
3454 | return 1; | |
3455 | /* Otherwise falls through */ | |
6aa8b732 | 3456 | default: |
8b9cf98c | 3457 | msr = find_msr_entry(vmx, msr_index); |
3bab1f5d | 3458 | if (msr) { |
8b3c3104 | 3459 | u64 old_msr_data = msr->data; |
3bab1f5d | 3460 | msr->data = data; |
2225fd56 AK |
3461 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
3462 | preempt_disable(); | |
8b3c3104 AH |
3463 | ret = kvm_set_shared_msr(msr->index, msr->data, |
3464 | msr->mask); | |
2225fd56 | 3465 | preempt_enable(); |
8b3c3104 AH |
3466 | if (ret) |
3467 | msr->data = old_msr_data; | |
2225fd56 | 3468 | } |
3bab1f5d | 3469 | break; |
6aa8b732 | 3470 | } |
8fe8ab46 | 3471 | ret = kvm_set_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3472 | } |
3473 | ||
2cc51560 | 3474 | return ret; |
6aa8b732 AK |
3475 | } |
3476 | ||
5fdbf976 | 3477 | static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
6aa8b732 | 3478 | { |
5fdbf976 MT |
3479 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
3480 | switch (reg) { | |
3481 | case VCPU_REGS_RSP: | |
3482 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); | |
3483 | break; | |
3484 | case VCPU_REGS_RIP: | |
3485 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); | |
3486 | break; | |
6de4f3ad AK |
3487 | case VCPU_EXREG_PDPTR: |
3488 | if (enable_ept) | |
3489 | ept_save_pdptrs(vcpu); | |
3490 | break; | |
5fdbf976 MT |
3491 | default: |
3492 | break; | |
3493 | } | |
6aa8b732 AK |
3494 | } |
3495 | ||
6aa8b732 AK |
3496 | static __init int cpu_has_kvm_support(void) |
3497 | { | |
6210e37b | 3498 | return cpu_has_vmx(); |
6aa8b732 AK |
3499 | } |
3500 | ||
3501 | static __init int vmx_disabled_by_bios(void) | |
3502 | { | |
3503 | u64 msr; | |
3504 | ||
3505 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); | |
cafd6659 | 3506 | if (msr & FEATURE_CONTROL_LOCKED) { |
23f3e991 | 3507 | /* launched w/ TXT and VMX disabled */ |
cafd6659 SW |
3508 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
3509 | && tboot_enabled()) | |
3510 | return 1; | |
23f3e991 | 3511 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
cafd6659 | 3512 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
23f3e991 | 3513 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
f9335afe SW |
3514 | && !tboot_enabled()) { |
3515 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " | |
23f3e991 | 3516 | "activate TXT before enabling KVM\n"); |
cafd6659 | 3517 | return 1; |
f9335afe | 3518 | } |
23f3e991 JC |
3519 | /* launched w/o TXT and VMX disabled */ |
3520 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) | |
3521 | && !tboot_enabled()) | |
3522 | return 1; | |
cafd6659 SW |
3523 | } |
3524 | ||
3525 | return 0; | |
6aa8b732 AK |
3526 | } |
3527 | ||
7725b894 DX |
3528 | static void kvm_cpu_vmxon(u64 addr) |
3529 | { | |
fe0e80be | 3530 | cr4_set_bits(X86_CR4_VMXE); |
1c5ac21a AS |
3531 | intel_pt_handle_vmx(1); |
3532 | ||
7725b894 DX |
3533 | asm volatile (ASM_VMX_VMXON_RAX |
3534 | : : "a"(&addr), "m"(addr) | |
3535 | : "memory", "cc"); | |
3536 | } | |
3537 | ||
13a34e06 | 3538 | static int hardware_enable(void) |
6aa8b732 AK |
3539 | { |
3540 | int cpu = raw_smp_processor_id(); | |
3541 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); | |
cafd6659 | 3542 | u64 old, test_bits; |
6aa8b732 | 3543 | |
1e02ce4c | 3544 | if (cr4_read_shadow() & X86_CR4_VMXE) |
10474ae8 AG |
3545 | return -EBUSY; |
3546 | ||
d462b819 | 3547 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
bf9f6ac8 FW |
3548 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
3549 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
8f536b76 ZY |
3550 | |
3551 | /* | |
3552 | * Now we can enable the vmclear operation in kdump | |
3553 | * since the loaded_vmcss_on_cpu list on this cpu | |
3554 | * has been initialized. | |
3555 | * | |
3556 | * Though the cpu is not in VMX operation now, there | |
3557 | * is no problem to enable the vmclear operation | |
3558 | * for the loaded_vmcss_on_cpu list is empty! | |
3559 | */ | |
3560 | crash_enable_local_vmclear(cpu); | |
3561 | ||
6aa8b732 | 3562 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
cafd6659 SW |
3563 | |
3564 | test_bits = FEATURE_CONTROL_LOCKED; | |
3565 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
3566 | if (tboot_enabled()) | |
3567 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; | |
3568 | ||
3569 | if ((old & test_bits) != test_bits) { | |
6aa8b732 | 3570 | /* enable and lock */ |
cafd6659 SW |
3571 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
3572 | } | |
fe0e80be | 3573 | kvm_cpu_vmxon(phys_addr); |
fdf288bf DH |
3574 | if (enable_ept) |
3575 | ept_sync_global(); | |
10474ae8 AG |
3576 | |
3577 | return 0; | |
6aa8b732 AK |
3578 | } |
3579 | ||
d462b819 | 3580 | static void vmclear_local_loaded_vmcss(void) |
543e4243 AK |
3581 | { |
3582 | int cpu = raw_smp_processor_id(); | |
d462b819 | 3583 | struct loaded_vmcs *v, *n; |
543e4243 | 3584 | |
d462b819 NHE |
3585 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
3586 | loaded_vmcss_on_cpu_link) | |
3587 | __loaded_vmcs_clear(v); | |
543e4243 AK |
3588 | } |
3589 | ||
710ff4a8 EH |
3590 | |
3591 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() | |
3592 | * tricks. | |
3593 | */ | |
3594 | static void kvm_cpu_vmxoff(void) | |
6aa8b732 | 3595 | { |
4ecac3fd | 3596 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
1c5ac21a AS |
3597 | |
3598 | intel_pt_handle_vmx(0); | |
fe0e80be | 3599 | cr4_clear_bits(X86_CR4_VMXE); |
6aa8b732 AK |
3600 | } |
3601 | ||
13a34e06 | 3602 | static void hardware_disable(void) |
710ff4a8 | 3603 | { |
fe0e80be DH |
3604 | vmclear_local_loaded_vmcss(); |
3605 | kvm_cpu_vmxoff(); | |
710ff4a8 EH |
3606 | } |
3607 | ||
1c3d14fe | 3608 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
d77c26fc | 3609 | u32 msr, u32 *result) |
1c3d14fe YS |
3610 | { |
3611 | u32 vmx_msr_low, vmx_msr_high; | |
3612 | u32 ctl = ctl_min | ctl_opt; | |
3613 | ||
3614 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3615 | ||
3616 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ | |
3617 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ | |
3618 | ||
3619 | /* Ensure minimum (required) set of control bits are supported. */ | |
3620 | if (ctl_min & ~ctl) | |
002c7f7c | 3621 | return -EIO; |
1c3d14fe YS |
3622 | |
3623 | *result = ctl; | |
3624 | return 0; | |
3625 | } | |
3626 | ||
110312c8 AK |
3627 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
3628 | { | |
3629 | u32 vmx_msr_low, vmx_msr_high; | |
3630 | ||
3631 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3632 | return vmx_msr_high & ctl; | |
3633 | } | |
3634 | ||
002c7f7c | 3635 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
6aa8b732 AK |
3636 | { |
3637 | u32 vmx_msr_low, vmx_msr_high; | |
d56f546d | 3638 | u32 min, opt, min2, opt2; |
1c3d14fe YS |
3639 | u32 _pin_based_exec_control = 0; |
3640 | u32 _cpu_based_exec_control = 0; | |
f78e0e2e | 3641 | u32 _cpu_based_2nd_exec_control = 0; |
1c3d14fe YS |
3642 | u32 _vmexit_control = 0; |
3643 | u32 _vmentry_control = 0; | |
3644 | ||
10166744 | 3645 | min = CPU_BASED_HLT_EXITING | |
1c3d14fe YS |
3646 | #ifdef CONFIG_X86_64 |
3647 | CPU_BASED_CR8_LOAD_EXITING | | |
3648 | CPU_BASED_CR8_STORE_EXITING | | |
3649 | #endif | |
d56f546d SY |
3650 | CPU_BASED_CR3_LOAD_EXITING | |
3651 | CPU_BASED_CR3_STORE_EXITING | | |
8eb73e2d | 3652 | CPU_BASED_UNCOND_IO_EXITING | |
1c3d14fe | 3653 | CPU_BASED_MOV_DR_EXITING | |
a7052897 | 3654 | CPU_BASED_USE_TSC_OFFSETING | |
fee84b07 AK |
3655 | CPU_BASED_INVLPG_EXITING | |
3656 | CPU_BASED_RDPMC_EXITING; | |
443381a8 | 3657 | |
668fffa3 MT |
3658 | if (!kvm_mwait_in_guest()) |
3659 | min |= CPU_BASED_MWAIT_EXITING | | |
3660 | CPU_BASED_MONITOR_EXITING; | |
3661 | ||
f78e0e2e | 3662 | opt = CPU_BASED_TPR_SHADOW | |
25c5f225 | 3663 | CPU_BASED_USE_MSR_BITMAPS | |
f78e0e2e | 3664 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
1c3d14fe YS |
3665 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
3666 | &_cpu_based_exec_control) < 0) | |
002c7f7c | 3667 | return -EIO; |
6e5d865c YS |
3668 | #ifdef CONFIG_X86_64 |
3669 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
3670 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & | |
3671 | ~CPU_BASED_CR8_STORE_EXITING; | |
3672 | #endif | |
f78e0e2e | 3673 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
d56f546d SY |
3674 | min2 = 0; |
3675 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | | |
8d14695f | 3676 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
2384d2b3 | 3677 | SECONDARY_EXEC_WBINVD_EXITING | |
d56f546d | 3678 | SECONDARY_EXEC_ENABLE_VPID | |
3a624e29 | 3679 | SECONDARY_EXEC_ENABLE_EPT | |
4b8d54f9 | 3680 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
4e47c7a6 | 3681 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
0367f205 | 3682 | SECONDARY_EXEC_DESC | |
ad756a16 | 3683 | SECONDARY_EXEC_RDTSCP | |
83d4c286 | 3684 | SECONDARY_EXEC_ENABLE_INVPCID | |
c7c9c56c | 3685 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
abc4fc58 | 3686 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
20300099 | 3687 | SECONDARY_EXEC_SHADOW_VMCS | |
843e4330 | 3688 | SECONDARY_EXEC_XSAVES | |
736fdf72 DH |
3689 | SECONDARY_EXEC_RDSEED_EXITING | |
3690 | SECONDARY_EXEC_RDRAND_EXITING | | |
8b3e34e4 | 3691 | SECONDARY_EXEC_ENABLE_PML | |
2a499e49 BD |
3692 | SECONDARY_EXEC_TSC_SCALING | |
3693 | SECONDARY_EXEC_ENABLE_VMFUNC; | |
d56f546d SY |
3694 | if (adjust_vmx_controls(min2, opt2, |
3695 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
f78e0e2e SY |
3696 | &_cpu_based_2nd_exec_control) < 0) |
3697 | return -EIO; | |
3698 | } | |
3699 | #ifndef CONFIG_X86_64 | |
3700 | if (!(_cpu_based_2nd_exec_control & | |
3701 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
3702 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; | |
3703 | #endif | |
83d4c286 YZ |
3704 | |
3705 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
3706 | _cpu_based_2nd_exec_control &= ~( | |
8d14695f | 3707 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
c7c9c56c YZ |
3708 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
3709 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
83d4c286 | 3710 | |
61f1dd90 WL |
3711 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
3712 | &vmx_capability.ept, &vmx_capability.vpid); | |
3713 | ||
d56f546d | 3714 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
a7052897 MT |
3715 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
3716 | enabled */ | |
5fff7d27 GN |
3717 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
3718 | CPU_BASED_CR3_STORE_EXITING | | |
3719 | CPU_BASED_INVLPG_EXITING); | |
61f1dd90 WL |
3720 | } else if (vmx_capability.ept) { |
3721 | vmx_capability.ept = 0; | |
3722 | pr_warn_once("EPT CAP should not exist if not support " | |
3723 | "1-setting enable EPT VM-execution control\n"); | |
3724 | } | |
3725 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && | |
3726 | vmx_capability.vpid) { | |
3727 | vmx_capability.vpid = 0; | |
3728 | pr_warn_once("VPID CAP should not exist if not support " | |
3729 | "1-setting enable VPID VM-execution control\n"); | |
d56f546d | 3730 | } |
1c3d14fe | 3731 | |
91fa0f8e | 3732 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
1c3d14fe YS |
3733 | #ifdef CONFIG_X86_64 |
3734 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; | |
3735 | #endif | |
a547c6db | 3736 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
91fa0f8e | 3737 | VM_EXIT_CLEAR_BNDCFGS; |
1c3d14fe YS |
3738 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
3739 | &_vmexit_control) < 0) | |
002c7f7c | 3740 | return -EIO; |
1c3d14fe | 3741 | |
8a1b4392 PB |
3742 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
3743 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | | |
3744 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
3745 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
3746 | &_pin_based_exec_control) < 0) | |
3747 | return -EIO; | |
3748 | ||
1c17c3e6 PB |
3749 | if (cpu_has_broken_vmx_preemption_timer()) |
3750 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be | 3751 | if (!(_cpu_based_2nd_exec_control & |
91fa0f8e | 3752 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
01e439be YZ |
3753 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
3754 | ||
c845f9c6 | 3755 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
da8999d3 | 3756 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
1c3d14fe YS |
3757 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
3758 | &_vmentry_control) < 0) | |
002c7f7c | 3759 | return -EIO; |
6aa8b732 | 3760 | |
c68876fd | 3761 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
1c3d14fe YS |
3762 | |
3763 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ | |
3764 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) | |
002c7f7c | 3765 | return -EIO; |
1c3d14fe YS |
3766 | |
3767 | #ifdef CONFIG_X86_64 | |
3768 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ | |
3769 | if (vmx_msr_high & (1u<<16)) | |
002c7f7c | 3770 | return -EIO; |
1c3d14fe YS |
3771 | #endif |
3772 | ||
3773 | /* Require Write-Back (WB) memory type for VMCS accesses. */ | |
3774 | if (((vmx_msr_high >> 18) & 15) != 6) | |
002c7f7c | 3775 | return -EIO; |
1c3d14fe | 3776 | |
002c7f7c | 3777 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
16cb0255 | 3778 | vmcs_conf->order = get_order(vmcs_conf->size); |
9ac7e3e8 | 3779 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
002c7f7c | 3780 | vmcs_conf->revision_id = vmx_msr_low; |
1c3d14fe | 3781 | |
002c7f7c YS |
3782 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
3783 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; | |
f78e0e2e | 3784 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
002c7f7c YS |
3785 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
3786 | vmcs_conf->vmentry_ctrl = _vmentry_control; | |
1c3d14fe | 3787 | |
110312c8 AK |
3788 | cpu_has_load_ia32_efer = |
3789 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
3790 | VM_ENTRY_LOAD_IA32_EFER) | |
3791 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
3792 | VM_EXIT_LOAD_IA32_EFER); | |
3793 | ||
8bf00a52 GN |
3794 | cpu_has_load_perf_global_ctrl = |
3795 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
3796 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
3797 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
3798 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
3799 | ||
3800 | /* | |
3801 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL | |
bb3541f1 | 3802 | * but due to errata below it can't be used. Workaround is to use |
8bf00a52 GN |
3803 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
3804 | * | |
3805 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] | |
3806 | * | |
3807 | * AAK155 (model 26) | |
3808 | * AAP115 (model 30) | |
3809 | * AAT100 (model 37) | |
3810 | * BC86,AAY89,BD102 (model 44) | |
3811 | * BA97 (model 46) | |
3812 | * | |
3813 | */ | |
3814 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { | |
3815 | switch (boot_cpu_data.x86_model) { | |
3816 | case 26: | |
3817 | case 30: | |
3818 | case 37: | |
3819 | case 44: | |
3820 | case 46: | |
3821 | cpu_has_load_perf_global_ctrl = false; | |
3822 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " | |
3823 | "does not work properly. Using workaround\n"); | |
3824 | break; | |
3825 | default: | |
3826 | break; | |
3827 | } | |
3828 | } | |
3829 | ||
782511b0 | 3830 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
20300099 WL |
3831 | rdmsrl(MSR_IA32_XSS, host_xss); |
3832 | ||
1c3d14fe | 3833 | return 0; |
c68876fd | 3834 | } |
6aa8b732 AK |
3835 | |
3836 | static struct vmcs *alloc_vmcs_cpu(int cpu) | |
3837 | { | |
3838 | int node = cpu_to_node(cpu); | |
3839 | struct page *pages; | |
3840 | struct vmcs *vmcs; | |
3841 | ||
96db800f | 3842 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
6aa8b732 AK |
3843 | if (!pages) |
3844 | return NULL; | |
3845 | vmcs = page_address(pages); | |
1c3d14fe YS |
3846 | memset(vmcs, 0, vmcs_config.size); |
3847 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ | |
6aa8b732 AK |
3848 | return vmcs; |
3849 | } | |
3850 | ||
3851 | static struct vmcs *alloc_vmcs(void) | |
3852 | { | |
d3b2c338 | 3853 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
6aa8b732 AK |
3854 | } |
3855 | ||
3856 | static void free_vmcs(struct vmcs *vmcs) | |
3857 | { | |
1c3d14fe | 3858 | free_pages((unsigned long)vmcs, vmcs_config.order); |
6aa8b732 AK |
3859 | } |
3860 | ||
d462b819 NHE |
3861 | /* |
3862 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded | |
3863 | */ | |
3864 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) | |
3865 | { | |
3866 | if (!loaded_vmcs->vmcs) | |
3867 | return; | |
3868 | loaded_vmcs_clear(loaded_vmcs); | |
3869 | free_vmcs(loaded_vmcs->vmcs); | |
3870 | loaded_vmcs->vmcs = NULL; | |
355f4fb1 | 3871 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
d462b819 NHE |
3872 | } |
3873 | ||
276c796c MK |
3874 | static void vmx_nested_free_vmcs02(struct vcpu_vmx *vmx) |
3875 | { | |
3876 | struct loaded_vmcs *loaded_vmcs = &vmx->nested.vmcs02; | |
3877 | ||
3878 | /* | |
3879 | * Just leak the VMCS02 if the WARN triggers. Better than | |
3880 | * a use-after-free. | |
3881 | */ | |
3882 | if (WARN_ON(vmx->loaded_vmcs == loaded_vmcs)) | |
3883 | return; | |
3884 | free_loaded_vmcs(loaded_vmcs); | |
3885 | } | |
3886 | ||
39959588 | 3887 | static void free_kvm_area(void) |
6aa8b732 AK |
3888 | { |
3889 | int cpu; | |
3890 | ||
3230bb47 | 3891 | for_each_possible_cpu(cpu) { |
6aa8b732 | 3892 | free_vmcs(per_cpu(vmxarea, cpu)); |
3230bb47 ZA |
3893 | per_cpu(vmxarea, cpu) = NULL; |
3894 | } | |
6aa8b732 AK |
3895 | } |
3896 | ||
85fd514e JM |
3897 | enum vmcs_field_type { |
3898 | VMCS_FIELD_TYPE_U16 = 0, | |
3899 | VMCS_FIELD_TYPE_U64 = 1, | |
3900 | VMCS_FIELD_TYPE_U32 = 2, | |
3901 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 | |
3902 | }; | |
3903 | ||
3904 | static inline int vmcs_field_type(unsigned long field) | |
3905 | { | |
3906 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ | |
3907 | return VMCS_FIELD_TYPE_U32; | |
3908 | return (field >> 13) & 0x3 ; | |
3909 | } | |
3910 | ||
3911 | static inline int vmcs_field_readonly(unsigned long field) | |
3912 | { | |
3913 | return (((field >> 10) & 0x3) == 1); | |
3914 | } | |
3915 | ||
fe2b201b BD |
3916 | static void init_vmcs_shadow_fields(void) |
3917 | { | |
3918 | int i, j; | |
3919 | ||
44900ba6 PB |
3920 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
3921 | u16 field = shadow_read_only_fields[i]; | |
3922 | if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64 && | |
3923 | (i + 1 == max_shadow_read_only_fields || | |
3924 | shadow_read_only_fields[i + 1] != field + 1)) | |
3925 | pr_err("Missing field from shadow_read_only_field %x\n", | |
3926 | field + 1); | |
3927 | ||
3928 | clear_bit(field, vmx_vmread_bitmap); | |
3929 | #ifdef CONFIG_X86_64 | |
3930 | if (field & 1) | |
3931 | continue; | |
3932 | #endif | |
3933 | if (j < i) | |
3934 | shadow_read_only_fields[j] = field; | |
3935 | j++; | |
3936 | } | |
3937 | max_shadow_read_only_fields = j; | |
fe2b201b BD |
3938 | |
3939 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { | |
44900ba6 PB |
3940 | u16 field = shadow_read_write_fields[i]; |
3941 | if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64 && | |
3942 | (i + 1 == max_shadow_read_write_fields || | |
3943 | shadow_read_write_fields[i + 1] != field + 1)) | |
3944 | pr_err("Missing field from shadow_read_write_field %x\n", | |
3945 | field + 1); | |
3946 | ||
c5d167b2 PB |
3947 | /* |
3948 | * PML and the preemption timer can be emulated, but the | |
3949 | * processor cannot vmwrite to fields that don't exist | |
3950 | * on bare metal. | |
3951 | */ | |
44900ba6 | 3952 | switch (field) { |
c5d167b2 PB |
3953 | case GUEST_PML_INDEX: |
3954 | if (!cpu_has_vmx_pml()) | |
3955 | continue; | |
3956 | break; | |
3957 | case VMX_PREEMPTION_TIMER_VALUE: | |
3958 | if (!cpu_has_vmx_preemption_timer()) | |
3959 | continue; | |
3960 | break; | |
3961 | case GUEST_INTR_STATUS: | |
3962 | if (!cpu_has_vmx_apicv()) | |
fe2b201b BD |
3963 | continue; |
3964 | break; | |
3965 | default: | |
3966 | break; | |
3967 | } | |
3968 | ||
44900ba6 PB |
3969 | clear_bit(field, vmx_vmwrite_bitmap); |
3970 | clear_bit(field, vmx_vmread_bitmap); | |
3971 | #ifdef CONFIG_X86_64 | |
3972 | if (field & 1) | |
3973 | continue; | |
3974 | #endif | |
fe2b201b | 3975 | if (j < i) |
44900ba6 | 3976 | shadow_read_write_fields[j] = field; |
fe2b201b BD |
3977 | j++; |
3978 | } | |
3979 | max_shadow_read_write_fields = j; | |
fe2b201b BD |
3980 | } |
3981 | ||
6aa8b732 AK |
3982 | static __init int alloc_kvm_area(void) |
3983 | { | |
3984 | int cpu; | |
3985 | ||
3230bb47 | 3986 | for_each_possible_cpu(cpu) { |
6aa8b732 AK |
3987 | struct vmcs *vmcs; |
3988 | ||
3989 | vmcs = alloc_vmcs_cpu(cpu); | |
3990 | if (!vmcs) { | |
3991 | free_kvm_area(); | |
3992 | return -ENOMEM; | |
3993 | } | |
3994 | ||
3995 | per_cpu(vmxarea, cpu) = vmcs; | |
3996 | } | |
3997 | return 0; | |
3998 | } | |
3999 | ||
91b0aa2c | 4000 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
d99e4152 | 4001 | struct kvm_segment *save) |
6aa8b732 | 4002 | { |
d99e4152 GN |
4003 | if (!emulate_invalid_guest_state) { |
4004 | /* | |
4005 | * CS and SS RPL should be equal during guest entry according | |
4006 | * to VMX spec, but in reality it is not always so. Since vcpu | |
4007 | * is in the middle of the transition from real mode to | |
4008 | * protected mode it is safe to assume that RPL 0 is a good | |
4009 | * default value. | |
4010 | */ | |
4011 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) | |
b32a9918 NA |
4012 | save->selector &= ~SEGMENT_RPL_MASK; |
4013 | save->dpl = save->selector & SEGMENT_RPL_MASK; | |
d99e4152 | 4014 | save->s = 1; |
6aa8b732 | 4015 | } |
d99e4152 | 4016 | vmx_set_segment(vcpu, save, seg); |
6aa8b732 AK |
4017 | } |
4018 | ||
4019 | static void enter_pmode(struct kvm_vcpu *vcpu) | |
4020 | { | |
4021 | unsigned long flags; | |
a89a8fb9 | 4022 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 | 4023 | |
d99e4152 GN |
4024 | /* |
4025 | * Update real mode segment cache. It may be not up-to-date if sement | |
4026 | * register was written while vcpu was in a guest mode. | |
4027 | */ | |
4028 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
4029 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
4030 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
4031 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
4032 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); | |
4033 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
4034 | ||
7ffd92c5 | 4035 | vmx->rmode.vm86_active = 0; |
6aa8b732 | 4036 | |
2fb92db1 AK |
4037 | vmx_segment_cache_clear(vmx); |
4038 | ||
f5f7b2fe | 4039 | vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
6aa8b732 AK |
4040 | |
4041 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 AK |
4042 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
4043 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
6aa8b732 AK |
4044 | vmcs_writel(GUEST_RFLAGS, flags); |
4045 | ||
66aee91a RR |
4046 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
4047 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); | |
6aa8b732 AK |
4048 | |
4049 | update_exception_bitmap(vcpu); | |
4050 | ||
91b0aa2c GN |
4051 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
4052 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); | |
4053 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
4054 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
4055 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
4056 | fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
6aa8b732 AK |
4057 | } |
4058 | ||
f5f7b2fe | 4059 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
6aa8b732 | 4060 | { |
772e0318 | 4061 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
d99e4152 GN |
4062 | struct kvm_segment var = *save; |
4063 | ||
4064 | var.dpl = 0x3; | |
4065 | if (seg == VCPU_SREG_CS) | |
4066 | var.type = 0x3; | |
4067 | ||
4068 | if (!emulate_invalid_guest_state) { | |
4069 | var.selector = var.base >> 4; | |
4070 | var.base = var.base & 0xffff0; | |
4071 | var.limit = 0xffff; | |
4072 | var.g = 0; | |
4073 | var.db = 0; | |
4074 | var.present = 1; | |
4075 | var.s = 1; | |
4076 | var.l = 0; | |
4077 | var.unusable = 0; | |
4078 | var.type = 0x3; | |
4079 | var.avl = 0; | |
4080 | if (save->base & 0xf) | |
4081 | printk_once(KERN_WARNING "kvm: segment base is not " | |
4082 | "paragraph aligned when entering " | |
4083 | "protected mode (seg=%d)", seg); | |
4084 | } | |
6aa8b732 | 4085 | |
d99e4152 | 4086 | vmcs_write16(sf->selector, var.selector); |
96794e4e | 4087 | vmcs_writel(sf->base, var.base); |
d99e4152 GN |
4088 | vmcs_write32(sf->limit, var.limit); |
4089 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); | |
6aa8b732 AK |
4090 | } |
4091 | ||
4092 | static void enter_rmode(struct kvm_vcpu *vcpu) | |
4093 | { | |
4094 | unsigned long flags; | |
a89a8fb9 | 4095 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 | 4096 | |
f5f7b2fe AK |
4097 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
4098 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
4099 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
4100 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
4101 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
c6ad1153 GN |
4102 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
4103 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
f5f7b2fe | 4104 | |
7ffd92c5 | 4105 | vmx->rmode.vm86_active = 1; |
6aa8b732 | 4106 | |
776e58ea GN |
4107 | /* |
4108 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering | |
4918c6ca | 4109 | * vcpu. Warn the user that an update is overdue. |
776e58ea | 4110 | */ |
4918c6ca | 4111 | if (!vcpu->kvm->arch.tss_addr) |
776e58ea GN |
4112 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
4113 | "called before entering vcpu\n"); | |
776e58ea | 4114 | |
2fb92db1 AK |
4115 | vmx_segment_cache_clear(vmx); |
4116 | ||
4918c6ca | 4117 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
6aa8b732 | 4118 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
6aa8b732 AK |
4119 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
4120 | ||
4121 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 | 4122 | vmx->rmode.save_rflags = flags; |
6aa8b732 | 4123 | |
053de044 | 4124 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
6aa8b732 AK |
4125 | |
4126 | vmcs_writel(GUEST_RFLAGS, flags); | |
66aee91a | 4127 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
6aa8b732 AK |
4128 | update_exception_bitmap(vcpu); |
4129 | ||
d99e4152 GN |
4130 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
4131 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); | |
4132 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
4133 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
4134 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
4135 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
b246dd5d | 4136 | |
8668a3c4 | 4137 | kvm_mmu_reset_context(vcpu); |
6aa8b732 AK |
4138 | } |
4139 | ||
401d10de AS |
4140 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
4141 | { | |
4142 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
26bb0981 AK |
4143 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
4144 | ||
4145 | if (!msr) | |
4146 | return; | |
401d10de | 4147 | |
44ea2b17 AK |
4148 | /* |
4149 | * Force kernel_gs_base reloading before EFER changes, as control | |
4150 | * of this msr depends on is_long_mode(). | |
4151 | */ | |
4152 | vmx_load_host_state(to_vmx(vcpu)); | |
f6801dff | 4153 | vcpu->arch.efer = efer; |
401d10de | 4154 | if (efer & EFER_LMA) { |
2961e876 | 4155 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
4156 | msr->data = efer; |
4157 | } else { | |
2961e876 | 4158 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
4159 | |
4160 | msr->data = efer & ~EFER_LME; | |
4161 | } | |
4162 | setup_msrs(vmx); | |
4163 | } | |
4164 | ||
05b3e0c2 | 4165 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
4166 | |
4167 | static void enter_lmode(struct kvm_vcpu *vcpu) | |
4168 | { | |
4169 | u32 guest_tr_ar; | |
4170 | ||
2fb92db1 AK |
4171 | vmx_segment_cache_clear(to_vmx(vcpu)); |
4172 | ||
6aa8b732 | 4173 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
4d283ec9 | 4174 | if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) { |
bd80158a JK |
4175 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
4176 | __func__); | |
6aa8b732 | 4177 | vmcs_write32(GUEST_TR_AR_BYTES, |
4d283ec9 AL |
4178 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
4179 | | VMX_AR_TYPE_BUSY_64_TSS); | |
6aa8b732 | 4180 | } |
da38f438 | 4181 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
6aa8b732 AK |
4182 | } |
4183 | ||
4184 | static void exit_lmode(struct kvm_vcpu *vcpu) | |
4185 | { | |
2961e876 | 4186 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
da38f438 | 4187 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
6aa8b732 AK |
4188 | } |
4189 | ||
4190 | #endif | |
4191 | ||
c2ba05cc WL |
4192 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
4193 | bool invalidate_gpa) | |
2384d2b3 | 4194 | { |
c2ba05cc | 4195 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
dd180b3e XG |
4196 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
4197 | return; | |
995f00a6 | 4198 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
f0b98c02 JM |
4199 | } else { |
4200 | vpid_sync_context(vpid); | |
dd180b3e | 4201 | } |
2384d2b3 SY |
4202 | } |
4203 | ||
c2ba05cc | 4204 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
dd5f5341 | 4205 | { |
c2ba05cc | 4206 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
dd5f5341 WL |
4207 | } |
4208 | ||
fb6c8198 JM |
4209 | static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu) |
4210 | { | |
4211 | if (enable_ept) | |
c2ba05cc | 4212 | vmx_flush_tlb(vcpu, true); |
fb6c8198 JM |
4213 | } |
4214 | ||
e8467fda AK |
4215 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
4216 | { | |
4217 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; | |
4218 | ||
4219 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; | |
4220 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; | |
4221 | } | |
4222 | ||
aff48baa AK |
4223 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
4224 | { | |
4225 | if (enable_ept && is_paging(vcpu)) | |
4226 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); | |
4227 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
4228 | } | |
4229 | ||
25c4c276 | 4230 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
399badf3 | 4231 | { |
fc78f519 AK |
4232 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
4233 | ||
4234 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; | |
4235 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; | |
399badf3 AK |
4236 | } |
4237 | ||
1439442c SY |
4238 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
4239 | { | |
d0d538b9 GN |
4240 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4241 | ||
6de4f3ad AK |
4242 | if (!test_bit(VCPU_EXREG_PDPTR, |
4243 | (unsigned long *)&vcpu->arch.regs_dirty)) | |
4244 | return; | |
4245 | ||
1439442c | 4246 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
d0d538b9 GN |
4247 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
4248 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); | |
4249 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); | |
4250 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); | |
1439442c SY |
4251 | } |
4252 | } | |
4253 | ||
8f5d549f AK |
4254 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
4255 | { | |
d0d538b9 GN |
4256 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4257 | ||
8f5d549f | 4258 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
d0d538b9 GN |
4259 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
4260 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); | |
4261 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); | |
4262 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); | |
8f5d549f | 4263 | } |
6de4f3ad AK |
4264 | |
4265 | __set_bit(VCPU_EXREG_PDPTR, | |
4266 | (unsigned long *)&vcpu->arch.regs_avail); | |
4267 | __set_bit(VCPU_EXREG_PDPTR, | |
4268 | (unsigned long *)&vcpu->arch.regs_dirty); | |
8f5d549f AK |
4269 | } |
4270 | ||
3899152c DM |
4271 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
4272 | { | |
4273 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; | |
4274 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; | |
4275 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
4276 | ||
4277 | if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high & | |
4278 | SECONDARY_EXEC_UNRESTRICTED_GUEST && | |
4279 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) | |
4280 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); | |
4281 | ||
4282 | return fixed_bits_valid(val, fixed0, fixed1); | |
4283 | } | |
4284 | ||
4285 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4286 | { | |
4287 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; | |
4288 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; | |
4289 | ||
4290 | return fixed_bits_valid(val, fixed0, fixed1); | |
4291 | } | |
4292 | ||
4293 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4294 | { | |
4295 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0; | |
4296 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1; | |
4297 | ||
4298 | return fixed_bits_valid(val, fixed0, fixed1); | |
4299 | } | |
4300 | ||
4301 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ | |
4302 | #define nested_guest_cr4_valid nested_cr4_valid | |
4303 | #define nested_host_cr4_valid nested_cr4_valid | |
4304 | ||
5e1746d6 | 4305 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
1439442c SY |
4306 | |
4307 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, | |
4308 | unsigned long cr0, | |
4309 | struct kvm_vcpu *vcpu) | |
4310 | { | |
5233dd51 MT |
4311 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
4312 | vmx_decache_cr3(vcpu); | |
1439442c SY |
4313 | if (!(cr0 & X86_CR0_PG)) { |
4314 | /* From paging/starting to nonpaging */ | |
4315 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4316 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
1439442c SY |
4317 | (CPU_BASED_CR3_LOAD_EXITING | |
4318 | CPU_BASED_CR3_STORE_EXITING)); | |
4319 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4320 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c SY |
4321 | } else if (!is_paging(vcpu)) { |
4322 | /* From nonpaging to paging */ | |
4323 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4324 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
1439442c SY |
4325 | ~(CPU_BASED_CR3_LOAD_EXITING | |
4326 | CPU_BASED_CR3_STORE_EXITING)); | |
4327 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4328 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c | 4329 | } |
95eb84a7 SY |
4330 | |
4331 | if (!(cr0 & X86_CR0_WP)) | |
4332 | *hw_cr0 &= ~X86_CR0_WP; | |
1439442c SY |
4333 | } |
4334 | ||
6aa8b732 AK |
4335 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
4336 | { | |
7ffd92c5 | 4337 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3a624e29 NK |
4338 | unsigned long hw_cr0; |
4339 | ||
5037878e | 4340 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
3a624e29 | 4341 | if (enable_unrestricted_guest) |
5037878e | 4342 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
218e763f | 4343 | else { |
5037878e | 4344 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
1439442c | 4345 | |
218e763f GN |
4346 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
4347 | enter_pmode(vcpu); | |
6aa8b732 | 4348 | |
218e763f GN |
4349 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
4350 | enter_rmode(vcpu); | |
4351 | } | |
6aa8b732 | 4352 | |
05b3e0c2 | 4353 | #ifdef CONFIG_X86_64 |
f6801dff | 4354 | if (vcpu->arch.efer & EFER_LME) { |
707d92fa | 4355 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
6aa8b732 | 4356 | enter_lmode(vcpu); |
707d92fa | 4357 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
6aa8b732 AK |
4358 | exit_lmode(vcpu); |
4359 | } | |
4360 | #endif | |
4361 | ||
089d034e | 4362 | if (enable_ept) |
1439442c SY |
4363 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
4364 | ||
6aa8b732 | 4365 | vmcs_writel(CR0_READ_SHADOW, cr0); |
1439442c | 4366 | vmcs_writel(GUEST_CR0, hw_cr0); |
ad312c7c | 4367 | vcpu->arch.cr0 = cr0; |
14168786 GN |
4368 | |
4369 | /* depends on vcpu->arch.cr0 to be set to a new value */ | |
4370 | vmx->emulation_required = emulation_required(vcpu); | |
6aa8b732 AK |
4371 | } |
4372 | ||
855feb67 YZ |
4373 | static int get_ept_level(struct kvm_vcpu *vcpu) |
4374 | { | |
4375 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) | |
4376 | return 5; | |
4377 | return 4; | |
4378 | } | |
4379 | ||
995f00a6 | 4380 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
1439442c | 4381 | { |
855feb67 YZ |
4382 | u64 eptp = VMX_EPTP_MT_WB; |
4383 | ||
4384 | eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4; | |
1439442c | 4385 | |
995f00a6 PF |
4386 | if (enable_ept_ad_bits && |
4387 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) | |
bb97a016 | 4388 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
1439442c SY |
4389 | eptp |= (root_hpa & PAGE_MASK); |
4390 | ||
4391 | return eptp; | |
4392 | } | |
4393 | ||
6aa8b732 AK |
4394 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
4395 | { | |
1439442c SY |
4396 | unsigned long guest_cr3; |
4397 | u64 eptp; | |
4398 | ||
4399 | guest_cr3 = cr3; | |
089d034e | 4400 | if (enable_ept) { |
995f00a6 | 4401 | eptp = construct_eptp(vcpu, cr3); |
1439442c | 4402 | vmcs_write64(EPT_POINTER, eptp); |
59ab5a8f JK |
4403 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
4404 | guest_cr3 = kvm_read_cr3(vcpu); | |
4405 | else | |
4406 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; | |
7c93be44 | 4407 | ept_load_pdptrs(vcpu); |
1439442c SY |
4408 | } |
4409 | ||
c2ba05cc | 4410 | vmx_flush_tlb(vcpu, true); |
1439442c | 4411 | vmcs_writel(GUEST_CR3, guest_cr3); |
6aa8b732 AK |
4412 | } |
4413 | ||
5e1746d6 | 4414 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
6aa8b732 | 4415 | { |
085e68ee BS |
4416 | /* |
4417 | * Pass through host's Machine Check Enable value to hw_cr4, which | |
4418 | * is in force while we are in guest mode. Do not let guests control | |
4419 | * this bit, even if host CR4.MCE == 0. | |
4420 | */ | |
4421 | unsigned long hw_cr4 = | |
4422 | (cr4_read_shadow() & X86_CR4_MCE) | | |
4423 | (cr4 & ~X86_CR4_MCE) | | |
4424 | (to_vmx(vcpu)->rmode.vm86_active ? | |
4425 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); | |
1439442c | 4426 | |
0367f205 PB |
4427 | if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) { |
4428 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
4429 | SECONDARY_EXEC_DESC); | |
4430 | hw_cr4 &= ~X86_CR4_UMIP; | |
4431 | } else | |
4432 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
4433 | SECONDARY_EXEC_DESC); | |
4434 | ||
5e1746d6 NHE |
4435 | if (cr4 & X86_CR4_VMXE) { |
4436 | /* | |
4437 | * To use VMXON (and later other VMX instructions), a guest | |
4438 | * must first be able to turn on cr4.VMXE (see handle_vmon()). | |
4439 | * So basically the check on whether to allow nested VMX | |
4440 | * is here. | |
4441 | */ | |
4442 | if (!nested_vmx_allowed(vcpu)) | |
4443 | return 1; | |
1a0d74e6 | 4444 | } |
3899152c DM |
4445 | |
4446 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) | |
5e1746d6 NHE |
4447 | return 1; |
4448 | ||
ad312c7c | 4449 | vcpu->arch.cr4 = cr4; |
bc23008b AK |
4450 | if (enable_ept) { |
4451 | if (!is_paging(vcpu)) { | |
4452 | hw_cr4 &= ~X86_CR4_PAE; | |
4453 | hw_cr4 |= X86_CR4_PSE; | |
4454 | } else if (!(cr4 & X86_CR4_PAE)) { | |
4455 | hw_cr4 &= ~X86_CR4_PAE; | |
4456 | } | |
4457 | } | |
1439442c | 4458 | |
656ec4a4 RK |
4459 | if (!enable_unrestricted_guest && !is_paging(vcpu)) |
4460 | /* | |
ddba2628 HH |
4461 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
4462 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs | |
4463 | * to be manually disabled when guest switches to non-paging | |
4464 | * mode. | |
4465 | * | |
4466 | * If !enable_unrestricted_guest, the CPU is always running | |
4467 | * with CR0.PG=1 and CR4 needs to be modified. | |
4468 | * If enable_unrestricted_guest, the CPU automatically | |
4469 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. | |
656ec4a4 | 4470 | */ |
ddba2628 | 4471 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
656ec4a4 | 4472 | |
1439442c SY |
4473 | vmcs_writel(CR4_READ_SHADOW, cr4); |
4474 | vmcs_writel(GUEST_CR4, hw_cr4); | |
5e1746d6 | 4475 | return 0; |
6aa8b732 AK |
4476 | } |
4477 | ||
6aa8b732 AK |
4478 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
4479 | struct kvm_segment *var, int seg) | |
4480 | { | |
a9179499 | 4481 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 AK |
4482 | u32 ar; |
4483 | ||
c6ad1153 | 4484 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
f5f7b2fe | 4485 | *var = vmx->rmode.segs[seg]; |
a9179499 | 4486 | if (seg == VCPU_SREG_TR |
2fb92db1 | 4487 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
f5f7b2fe | 4488 | return; |
1390a28b AK |
4489 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4490 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4491 | return; | |
a9179499 | 4492 | } |
2fb92db1 AK |
4493 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4494 | var->limit = vmx_read_guest_seg_limit(vmx, seg); | |
4495 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4496 | ar = vmx_read_guest_seg_ar(vmx, seg); | |
03617c18 | 4497 | var->unusable = (ar >> 16) & 1; |
6aa8b732 AK |
4498 | var->type = ar & 15; |
4499 | var->s = (ar >> 4) & 1; | |
4500 | var->dpl = (ar >> 5) & 3; | |
03617c18 GN |
4501 | /* |
4502 | * Some userspaces do not preserve unusable property. Since usable | |
4503 | * segment has to be present according to VMX spec we can use present | |
4504 | * property to amend userspace bug by making unusable segment always | |
4505 | * nonpresent. vmx_segment_access_rights() already marks nonpresent | |
4506 | * segment as unusable. | |
4507 | */ | |
4508 | var->present = !var->unusable; | |
6aa8b732 AK |
4509 | var->avl = (ar >> 12) & 1; |
4510 | var->l = (ar >> 13) & 1; | |
4511 | var->db = (ar >> 14) & 1; | |
4512 | var->g = (ar >> 15) & 1; | |
6aa8b732 AK |
4513 | } |
4514 | ||
a9179499 AK |
4515 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
4516 | { | |
a9179499 AK |
4517 | struct kvm_segment s; |
4518 | ||
4519 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
4520 | vmx_get_segment(vcpu, &s, seg); | |
4521 | return s.base; | |
4522 | } | |
2fb92db1 | 4523 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
a9179499 AK |
4524 | } |
4525 | ||
b09408d0 | 4526 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
2e4d2653 | 4527 | { |
b09408d0 MT |
4528 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
4529 | ||
ae9fedc7 | 4530 | if (unlikely(vmx->rmode.vm86_active)) |
2e4d2653 | 4531 | return 0; |
ae9fedc7 PB |
4532 | else { |
4533 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); | |
4d283ec9 | 4534 | return VMX_AR_DPL(ar); |
69c73028 | 4535 | } |
69c73028 AK |
4536 | } |
4537 | ||
653e3108 | 4538 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
6aa8b732 | 4539 | { |
6aa8b732 AK |
4540 | u32 ar; |
4541 | ||
f0495f9b | 4542 | if (var->unusable || !var->present) |
6aa8b732 AK |
4543 | ar = 1 << 16; |
4544 | else { | |
4545 | ar = var->type & 15; | |
4546 | ar |= (var->s & 1) << 4; | |
4547 | ar |= (var->dpl & 3) << 5; | |
4548 | ar |= (var->present & 1) << 7; | |
4549 | ar |= (var->avl & 1) << 12; | |
4550 | ar |= (var->l & 1) << 13; | |
4551 | ar |= (var->db & 1) << 14; | |
4552 | ar |= (var->g & 1) << 15; | |
4553 | } | |
653e3108 AK |
4554 | |
4555 | return ar; | |
4556 | } | |
4557 | ||
4558 | static void vmx_set_segment(struct kvm_vcpu *vcpu, | |
4559 | struct kvm_segment *var, int seg) | |
4560 | { | |
7ffd92c5 | 4561 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
772e0318 | 4562 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
653e3108 | 4563 | |
2fb92db1 AK |
4564 | vmx_segment_cache_clear(vmx); |
4565 | ||
1ecd50a9 GN |
4566 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
4567 | vmx->rmode.segs[seg] = *var; | |
4568 | if (seg == VCPU_SREG_TR) | |
4569 | vmcs_write16(sf->selector, var->selector); | |
4570 | else if (var->s) | |
4571 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); | |
d99e4152 | 4572 | goto out; |
653e3108 | 4573 | } |
1ecd50a9 | 4574 | |
653e3108 AK |
4575 | vmcs_writel(sf->base, var->base); |
4576 | vmcs_write32(sf->limit, var->limit); | |
4577 | vmcs_write16(sf->selector, var->selector); | |
3a624e29 NK |
4578 | |
4579 | /* | |
4580 | * Fix the "Accessed" bit in AR field of segment registers for older | |
4581 | * qemu binaries. | |
4582 | * IA32 arch specifies that at the time of processor reset the | |
4583 | * "Accessed" bit in the AR field of segment registers is 1. And qemu | |
0fa06071 | 4584 | * is setting it to 0 in the userland code. This causes invalid guest |
3a624e29 NK |
4585 | * state vmexit when "unrestricted guest" mode is turned on. |
4586 | * Fix for this setup issue in cpu_reset is being pushed in the qemu | |
4587 | * tree. Newer qemu binaries with that qemu fix would not need this | |
4588 | * kvm hack. | |
4589 | */ | |
4590 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) | |
f924d66d | 4591 | var->type |= 0x1; /* Accessed */ |
3a624e29 | 4592 | |
f924d66d | 4593 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
d99e4152 GN |
4594 | |
4595 | out: | |
98eb2f8b | 4596 | vmx->emulation_required = emulation_required(vcpu); |
6aa8b732 AK |
4597 | } |
4598 | ||
6aa8b732 AK |
4599 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
4600 | { | |
2fb92db1 | 4601 | u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS); |
6aa8b732 AK |
4602 | |
4603 | *db = (ar >> 14) & 1; | |
4604 | *l = (ar >> 13) & 1; | |
4605 | } | |
4606 | ||
89a27f4d | 4607 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4608 | { |
89a27f4d GN |
4609 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
4610 | dt->address = vmcs_readl(GUEST_IDTR_BASE); | |
6aa8b732 AK |
4611 | } |
4612 | ||
89a27f4d | 4613 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4614 | { |
89a27f4d GN |
4615 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
4616 | vmcs_writel(GUEST_IDTR_BASE, dt->address); | |
6aa8b732 AK |
4617 | } |
4618 | ||
89a27f4d | 4619 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4620 | { |
89a27f4d GN |
4621 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
4622 | dt->address = vmcs_readl(GUEST_GDTR_BASE); | |
6aa8b732 AK |
4623 | } |
4624 | ||
89a27f4d | 4625 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4626 | { |
89a27f4d GN |
4627 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
4628 | vmcs_writel(GUEST_GDTR_BASE, dt->address); | |
6aa8b732 AK |
4629 | } |
4630 | ||
648dfaa7 MG |
4631 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
4632 | { | |
4633 | struct kvm_segment var; | |
4634 | u32 ar; | |
4635 | ||
4636 | vmx_get_segment(vcpu, &var, seg); | |
07f42f5f | 4637 | var.dpl = 0x3; |
0647f4aa GN |
4638 | if (seg == VCPU_SREG_CS) |
4639 | var.type = 0x3; | |
648dfaa7 MG |
4640 | ar = vmx_segment_access_rights(&var); |
4641 | ||
4642 | if (var.base != (var.selector << 4)) | |
4643 | return false; | |
89efbed0 | 4644 | if (var.limit != 0xffff) |
648dfaa7 | 4645 | return false; |
07f42f5f | 4646 | if (ar != 0xf3) |
648dfaa7 MG |
4647 | return false; |
4648 | ||
4649 | return true; | |
4650 | } | |
4651 | ||
4652 | static bool code_segment_valid(struct kvm_vcpu *vcpu) | |
4653 | { | |
4654 | struct kvm_segment cs; | |
4655 | unsigned int cs_rpl; | |
4656 | ||
4657 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
b32a9918 | 4658 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4659 | |
1872a3f4 AK |
4660 | if (cs.unusable) |
4661 | return false; | |
4d283ec9 | 4662 | if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK)) |
648dfaa7 MG |
4663 | return false; |
4664 | if (!cs.s) | |
4665 | return false; | |
4d283ec9 | 4666 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
648dfaa7 MG |
4667 | if (cs.dpl > cs_rpl) |
4668 | return false; | |
1872a3f4 | 4669 | } else { |
648dfaa7 MG |
4670 | if (cs.dpl != cs_rpl) |
4671 | return false; | |
4672 | } | |
4673 | if (!cs.present) | |
4674 | return false; | |
4675 | ||
4676 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ | |
4677 | return true; | |
4678 | } | |
4679 | ||
4680 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) | |
4681 | { | |
4682 | struct kvm_segment ss; | |
4683 | unsigned int ss_rpl; | |
4684 | ||
4685 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
b32a9918 | 4686 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4687 | |
1872a3f4 AK |
4688 | if (ss.unusable) |
4689 | return true; | |
4690 | if (ss.type != 3 && ss.type != 7) | |
648dfaa7 MG |
4691 | return false; |
4692 | if (!ss.s) | |
4693 | return false; | |
4694 | if (ss.dpl != ss_rpl) /* DPL != RPL */ | |
4695 | return false; | |
4696 | if (!ss.present) | |
4697 | return false; | |
4698 | ||
4699 | return true; | |
4700 | } | |
4701 | ||
4702 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) | |
4703 | { | |
4704 | struct kvm_segment var; | |
4705 | unsigned int rpl; | |
4706 | ||
4707 | vmx_get_segment(vcpu, &var, seg); | |
b32a9918 | 4708 | rpl = var.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4709 | |
1872a3f4 AK |
4710 | if (var.unusable) |
4711 | return true; | |
648dfaa7 MG |
4712 | if (!var.s) |
4713 | return false; | |
4714 | if (!var.present) | |
4715 | return false; | |
4d283ec9 | 4716 | if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) { |
648dfaa7 MG |
4717 | if (var.dpl < rpl) /* DPL < RPL */ |
4718 | return false; | |
4719 | } | |
4720 | ||
4721 | /* TODO: Add other members to kvm_segment_field to allow checking for other access | |
4722 | * rights flags | |
4723 | */ | |
4724 | return true; | |
4725 | } | |
4726 | ||
4727 | static bool tr_valid(struct kvm_vcpu *vcpu) | |
4728 | { | |
4729 | struct kvm_segment tr; | |
4730 | ||
4731 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); | |
4732 | ||
1872a3f4 AK |
4733 | if (tr.unusable) |
4734 | return false; | |
b32a9918 | 4735 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 | 4736 | return false; |
1872a3f4 | 4737 | if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ |
648dfaa7 MG |
4738 | return false; |
4739 | if (!tr.present) | |
4740 | return false; | |
4741 | ||
4742 | return true; | |
4743 | } | |
4744 | ||
4745 | static bool ldtr_valid(struct kvm_vcpu *vcpu) | |
4746 | { | |
4747 | struct kvm_segment ldtr; | |
4748 | ||
4749 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); | |
4750 | ||
1872a3f4 AK |
4751 | if (ldtr.unusable) |
4752 | return true; | |
b32a9918 | 4753 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 MG |
4754 | return false; |
4755 | if (ldtr.type != 2) | |
4756 | return false; | |
4757 | if (!ldtr.present) | |
4758 | return false; | |
4759 | ||
4760 | return true; | |
4761 | } | |
4762 | ||
4763 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) | |
4764 | { | |
4765 | struct kvm_segment cs, ss; | |
4766 | ||
4767 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
4768 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
4769 | ||
b32a9918 NA |
4770 | return ((cs.selector & SEGMENT_RPL_MASK) == |
4771 | (ss.selector & SEGMENT_RPL_MASK)); | |
648dfaa7 MG |
4772 | } |
4773 | ||
4774 | /* | |
4775 | * Check if guest state is valid. Returns true if valid, false if | |
4776 | * not. | |
4777 | * We assume that registers are always usable | |
4778 | */ | |
4779 | static bool guest_state_valid(struct kvm_vcpu *vcpu) | |
4780 | { | |
c5e97c80 GN |
4781 | if (enable_unrestricted_guest) |
4782 | return true; | |
4783 | ||
648dfaa7 | 4784 | /* real mode guest state checks */ |
f13882d8 | 4785 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
648dfaa7 MG |
4786 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
4787 | return false; | |
4788 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) | |
4789 | return false; | |
4790 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) | |
4791 | return false; | |
4792 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) | |
4793 | return false; | |
4794 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) | |
4795 | return false; | |
4796 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) | |
4797 | return false; | |
4798 | } else { | |
4799 | /* protected mode guest state checks */ | |
4800 | if (!cs_ss_rpl_check(vcpu)) | |
4801 | return false; | |
4802 | if (!code_segment_valid(vcpu)) | |
4803 | return false; | |
4804 | if (!stack_segment_valid(vcpu)) | |
4805 | return false; | |
4806 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) | |
4807 | return false; | |
4808 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) | |
4809 | return false; | |
4810 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) | |
4811 | return false; | |
4812 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) | |
4813 | return false; | |
4814 | if (!tr_valid(vcpu)) | |
4815 | return false; | |
4816 | if (!ldtr_valid(vcpu)) | |
4817 | return false; | |
4818 | } | |
4819 | /* TODO: | |
4820 | * - Add checks on RIP | |
4821 | * - Add checks on RFLAGS | |
4822 | */ | |
4823 | ||
4824 | return true; | |
4825 | } | |
4826 | ||
5fa99cbe JM |
4827 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
4828 | { | |
4829 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); | |
4830 | } | |
4831 | ||
d77c26fc | 4832 | static int init_rmode_tss(struct kvm *kvm) |
6aa8b732 | 4833 | { |
40dcaa9f | 4834 | gfn_t fn; |
195aefde | 4835 | u16 data = 0; |
1f755a82 | 4836 | int idx, r; |
6aa8b732 | 4837 | |
40dcaa9f | 4838 | idx = srcu_read_lock(&kvm->srcu); |
4918c6ca | 4839 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
195aefde IE |
4840 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
4841 | if (r < 0) | |
10589a46 | 4842 | goto out; |
195aefde | 4843 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
464d17c8 SY |
4844 | r = kvm_write_guest_page(kvm, fn++, &data, |
4845 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); | |
195aefde | 4846 | if (r < 0) |
10589a46 | 4847 | goto out; |
195aefde IE |
4848 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
4849 | if (r < 0) | |
10589a46 | 4850 | goto out; |
195aefde IE |
4851 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
4852 | if (r < 0) | |
10589a46 | 4853 | goto out; |
195aefde | 4854 | data = ~0; |
10589a46 MT |
4855 | r = kvm_write_guest_page(kvm, fn, &data, |
4856 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, | |
4857 | sizeof(u8)); | |
10589a46 | 4858 | out: |
40dcaa9f | 4859 | srcu_read_unlock(&kvm->srcu, idx); |
1f755a82 | 4860 | return r; |
6aa8b732 AK |
4861 | } |
4862 | ||
b7ebfb05 SY |
4863 | static int init_rmode_identity_map(struct kvm *kvm) |
4864 | { | |
f51770ed | 4865 | int i, idx, r = 0; |
ba049e93 | 4866 | kvm_pfn_t identity_map_pfn; |
b7ebfb05 SY |
4867 | u32 tmp; |
4868 | ||
a255d479 TC |
4869 | /* Protect kvm->arch.ept_identity_pagetable_done. */ |
4870 | mutex_lock(&kvm->slots_lock); | |
4871 | ||
f51770ed | 4872 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
a255d479 | 4873 | goto out2; |
a255d479 | 4874 | |
d8a6e365 DH |
4875 | if (!kvm->arch.ept_identity_map_addr) |
4876 | kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; | |
b927a3ce | 4877 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
a255d479 | 4878 | |
d8a6e365 DH |
4879 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
4880 | kvm->arch.ept_identity_map_addr, PAGE_SIZE); | |
f51770ed | 4881 | if (r < 0) |
a255d479 TC |
4882 | goto out2; |
4883 | ||
40dcaa9f | 4884 | idx = srcu_read_lock(&kvm->srcu); |
b7ebfb05 SY |
4885 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
4886 | if (r < 0) | |
4887 | goto out; | |
4888 | /* Set up identity-mapping pagetable for EPT in real mode */ | |
4889 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { | |
4890 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | | |
4891 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); | |
4892 | r = kvm_write_guest_page(kvm, identity_map_pfn, | |
4893 | &tmp, i * sizeof(tmp), sizeof(tmp)); | |
4894 | if (r < 0) | |
4895 | goto out; | |
4896 | } | |
4897 | kvm->arch.ept_identity_pagetable_done = true; | |
f51770ed | 4898 | |
b7ebfb05 | 4899 | out: |
40dcaa9f | 4900 | srcu_read_unlock(&kvm->srcu, idx); |
a255d479 TC |
4901 | |
4902 | out2: | |
4903 | mutex_unlock(&kvm->slots_lock); | |
f51770ed | 4904 | return r; |
b7ebfb05 SY |
4905 | } |
4906 | ||
6aa8b732 AK |
4907 | static void seg_setup(int seg) |
4908 | { | |
772e0318 | 4909 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
3a624e29 | 4910 | unsigned int ar; |
6aa8b732 AK |
4911 | |
4912 | vmcs_write16(sf->selector, 0); | |
4913 | vmcs_writel(sf->base, 0); | |
4914 | vmcs_write32(sf->limit, 0xffff); | |
d54d07b2 GN |
4915 | ar = 0x93; |
4916 | if (seg == VCPU_SREG_CS) | |
4917 | ar |= 0x08; /* code segment */ | |
3a624e29 NK |
4918 | |
4919 | vmcs_write32(sf->ar_bytes, ar); | |
6aa8b732 AK |
4920 | } |
4921 | ||
f78e0e2e SY |
4922 | static int alloc_apic_access_page(struct kvm *kvm) |
4923 | { | |
4484141a | 4924 | struct page *page; |
f78e0e2e SY |
4925 | int r = 0; |
4926 | ||
79fac95e | 4927 | mutex_lock(&kvm->slots_lock); |
c24ae0dc | 4928 | if (kvm->arch.apic_access_page_done) |
f78e0e2e | 4929 | goto out; |
1d8007bd PB |
4930 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
4931 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); | |
f78e0e2e SY |
4932 | if (r) |
4933 | goto out; | |
72dc67a6 | 4934 | |
73a6d941 | 4935 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
4484141a XG |
4936 | if (is_error_page(page)) { |
4937 | r = -EFAULT; | |
4938 | goto out; | |
4939 | } | |
4940 | ||
c24ae0dc TC |
4941 | /* |
4942 | * Do not pin the page in memory, so that memory hot-unplug | |
4943 | * is able to migrate it. | |
4944 | */ | |
4945 | put_page(page); | |
4946 | kvm->arch.apic_access_page_done = true; | |
f78e0e2e | 4947 | out: |
79fac95e | 4948 | mutex_unlock(&kvm->slots_lock); |
f78e0e2e SY |
4949 | return r; |
4950 | } | |
4951 | ||
991e7a0e | 4952 | static int allocate_vpid(void) |
2384d2b3 SY |
4953 | { |
4954 | int vpid; | |
4955 | ||
919818ab | 4956 | if (!enable_vpid) |
991e7a0e | 4957 | return 0; |
2384d2b3 SY |
4958 | spin_lock(&vmx_vpid_lock); |
4959 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); | |
991e7a0e | 4960 | if (vpid < VMX_NR_VPIDS) |
2384d2b3 | 4961 | __set_bit(vpid, vmx_vpid_bitmap); |
991e7a0e WL |
4962 | else |
4963 | vpid = 0; | |
2384d2b3 | 4964 | spin_unlock(&vmx_vpid_lock); |
991e7a0e | 4965 | return vpid; |
2384d2b3 SY |
4966 | } |
4967 | ||
991e7a0e | 4968 | static void free_vpid(int vpid) |
cdbecfc3 | 4969 | { |
991e7a0e | 4970 | if (!enable_vpid || vpid == 0) |
cdbecfc3 LJ |
4971 | return; |
4972 | spin_lock(&vmx_vpid_lock); | |
991e7a0e | 4973 | __clear_bit(vpid, vmx_vpid_bitmap); |
cdbecfc3 LJ |
4974 | spin_unlock(&vmx_vpid_lock); |
4975 | } | |
4976 | ||
8d14695f YZ |
4977 | #define MSR_TYPE_R 1 |
4978 | #define MSR_TYPE_W 2 | |
4979 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, | |
4980 | u32 msr, int type) | |
25c5f225 | 4981 | { |
3e7c73e9 | 4982 | int f = sizeof(unsigned long); |
25c5f225 SY |
4983 | |
4984 | if (!cpu_has_vmx_msr_bitmap()) | |
4985 | return; | |
4986 | ||
4987 | /* | |
4988 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
4989 | * have the write-low and read-high bitmap offsets the wrong way round. | |
4990 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
4991 | */ | |
25c5f225 | 4992 | if (msr <= 0x1fff) { |
8d14695f YZ |
4993 | if (type & MSR_TYPE_R) |
4994 | /* read-low */ | |
4995 | __clear_bit(msr, msr_bitmap + 0x000 / f); | |
4996 | ||
4997 | if (type & MSR_TYPE_W) | |
4998 | /* write-low */ | |
4999 | __clear_bit(msr, msr_bitmap + 0x800 / f); | |
5000 | ||
25c5f225 SY |
5001 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
5002 | msr &= 0x1fff; | |
8d14695f YZ |
5003 | if (type & MSR_TYPE_R) |
5004 | /* read-high */ | |
5005 | __clear_bit(msr, msr_bitmap + 0x400 / f); | |
5006 | ||
5007 | if (type & MSR_TYPE_W) | |
5008 | /* write-high */ | |
5009 | __clear_bit(msr, msr_bitmap + 0xc00 / f); | |
5010 | ||
5011 | } | |
5012 | } | |
5013 | ||
f2b93280 WV |
5014 | /* |
5015 | * If a msr is allowed by L0, we should check whether it is allowed by L1. | |
5016 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. | |
5017 | */ | |
5018 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, | |
5019 | unsigned long *msr_bitmap_nested, | |
5020 | u32 msr, int type) | |
5021 | { | |
5022 | int f = sizeof(unsigned long); | |
5023 | ||
5024 | if (!cpu_has_vmx_msr_bitmap()) { | |
5025 | WARN_ON(1); | |
5026 | return; | |
5027 | } | |
5028 | ||
5029 | /* | |
5030 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
5031 | * have the write-low and read-high bitmap offsets the wrong way round. | |
5032 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
5033 | */ | |
5034 | if (msr <= 0x1fff) { | |
5035 | if (type & MSR_TYPE_R && | |
5036 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) | |
5037 | /* read-low */ | |
5038 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); | |
5039 | ||
5040 | if (type & MSR_TYPE_W && | |
5041 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) | |
5042 | /* write-low */ | |
5043 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); | |
5044 | ||
5045 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
5046 | msr &= 0x1fff; | |
5047 | if (type & MSR_TYPE_R && | |
5048 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) | |
5049 | /* read-high */ | |
5050 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); | |
5051 | ||
5052 | if (type & MSR_TYPE_W && | |
5053 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) | |
5054 | /* write-high */ | |
5055 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); | |
5056 | ||
5057 | } | |
5058 | } | |
5059 | ||
5897297b AK |
5060 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
5061 | { | |
5062 | if (!longmode_only) | |
8d14695f YZ |
5063 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, |
5064 | msr, MSR_TYPE_R | MSR_TYPE_W); | |
5065 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, | |
5066 | msr, MSR_TYPE_R | MSR_TYPE_W); | |
5067 | } | |
5068 | ||
2e69f865 | 5069 | static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active) |
8d14695f | 5070 | { |
f6e90f9e | 5071 | if (apicv_active) { |
c63e4563 | 5072 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv, |
2e69f865 | 5073 | msr, type); |
c63e4563 | 5074 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv, |
2e69f865 | 5075 | msr, type); |
f6e90f9e | 5076 | } else { |
f6e90f9e | 5077 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
2e69f865 | 5078 | msr, type); |
f6e90f9e | 5079 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
2e69f865 | 5080 | msr, type); |
f6e90f9e | 5081 | } |
5897297b AK |
5082 | } |
5083 | ||
b2a05fef | 5084 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
d50ab6c1 | 5085 | { |
d62caabb | 5086 | return enable_apicv; |
d50ab6c1 PB |
5087 | } |
5088 | ||
c9f04407 DM |
5089 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
5090 | { | |
5091 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
5092 | gfn_t gfn; | |
5093 | ||
5094 | /* | |
5095 | * Don't need to mark the APIC access page dirty; it is never | |
5096 | * written to by the CPU during APIC virtualization. | |
5097 | */ | |
5098 | ||
5099 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { | |
5100 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; | |
5101 | kvm_vcpu_mark_page_dirty(vcpu, gfn); | |
5102 | } | |
5103 | ||
5104 | if (nested_cpu_has_posted_intr(vmcs12)) { | |
5105 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; | |
5106 | kvm_vcpu_mark_page_dirty(vcpu, gfn); | |
5107 | } | |
5108 | } | |
5109 | ||
5110 | ||
6342c50a | 5111 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
705699a1 WV |
5112 | { |
5113 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5114 | int max_irr; | |
5115 | void *vapic_page; | |
5116 | u16 status; | |
5117 | ||
c9f04407 DM |
5118 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
5119 | return; | |
705699a1 | 5120 | |
c9f04407 DM |
5121 | vmx->nested.pi_pending = false; |
5122 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) | |
5123 | return; | |
705699a1 | 5124 | |
c9f04407 DM |
5125 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
5126 | if (max_irr != 256) { | |
705699a1 | 5127 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
e7387b0e LA |
5128 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
5129 | vapic_page, &max_irr); | |
705699a1 WV |
5130 | kunmap(vmx->nested.virtual_apic_page); |
5131 | ||
5132 | status = vmcs_read16(GUEST_INTR_STATUS); | |
5133 | if ((u8)max_irr > ((u8)status & 0xff)) { | |
5134 | status &= ~0xff; | |
5135 | status |= (u8)max_irr; | |
5136 | vmcs_write16(GUEST_INTR_STATUS, status); | |
5137 | } | |
5138 | } | |
c9f04407 DM |
5139 | |
5140 | nested_mark_vmcs12_pages_dirty(vcpu); | |
705699a1 WV |
5141 | } |
5142 | ||
06a5524f WV |
5143 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
5144 | bool nested) | |
21bc8dc5 RK |
5145 | { |
5146 | #ifdef CONFIG_SMP | |
06a5524f WV |
5147 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
5148 | ||
21bc8dc5 | 5149 | if (vcpu->mode == IN_GUEST_MODE) { |
28b835d6 | 5150 | /* |
5753743f HZ |
5151 | * The vector of interrupt to be delivered to vcpu had |
5152 | * been set in PIR before this function. | |
5153 | * | |
5154 | * Following cases will be reached in this block, and | |
5155 | * we always send a notification event in all cases as | |
5156 | * explained below. | |
5157 | * | |
5158 | * Case 1: vcpu keeps in non-root mode. Sending a | |
5159 | * notification event posts the interrupt to vcpu. | |
5160 | * | |
5161 | * Case 2: vcpu exits to root mode and is still | |
5162 | * runnable. PIR will be synced to vIRR before the | |
5163 | * next vcpu entry. Sending a notification event in | |
5164 | * this case has no effect, as vcpu is not in root | |
5165 | * mode. | |
28b835d6 | 5166 | * |
5753743f HZ |
5167 | * Case 3: vcpu exits to root mode and is blocked. |
5168 | * vcpu_block() has already synced PIR to vIRR and | |
5169 | * never blocks vcpu if vIRR is not cleared. Therefore, | |
5170 | * a blocked vcpu here does not wait for any requested | |
5171 | * interrupts in PIR, and sending a notification event | |
5172 | * which has no effect is safe here. | |
28b835d6 | 5173 | */ |
28b835d6 | 5174 | |
06a5524f | 5175 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
21bc8dc5 RK |
5176 | return true; |
5177 | } | |
5178 | #endif | |
5179 | return false; | |
5180 | } | |
5181 | ||
705699a1 WV |
5182 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
5183 | int vector) | |
5184 | { | |
5185 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5186 | ||
5187 | if (is_guest_mode(vcpu) && | |
5188 | vector == vmx->nested.posted_intr_nv) { | |
705699a1 WV |
5189 | /* |
5190 | * If a posted intr is not recognized by hardware, | |
5191 | * we will accomplish it in the next vmentry. | |
5192 | */ | |
5193 | vmx->nested.pi_pending = true; | |
5194 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
6b697711 LA |
5195 | /* the PIR and ON have been set by L1. */ |
5196 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) | |
5197 | kvm_vcpu_kick(vcpu); | |
705699a1 WV |
5198 | return 0; |
5199 | } | |
5200 | return -1; | |
5201 | } | |
a20ed54d YZ |
5202 | /* |
5203 | * Send interrupt to vcpu via posted interrupt way. | |
5204 | * 1. If target vcpu is running(non-root mode), send posted interrupt | |
5205 | * notification to vcpu and hardware will sync PIR to vIRR atomically. | |
5206 | * 2. If target vcpu isn't running(root mode), kick it to pick up the | |
5207 | * interrupt from PIR in next vmentry. | |
5208 | */ | |
5209 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) | |
5210 | { | |
5211 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5212 | int r; | |
5213 | ||
705699a1 WV |
5214 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
5215 | if (!r) | |
5216 | return; | |
5217 | ||
a20ed54d YZ |
5218 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
5219 | return; | |
5220 | ||
b95234c8 PB |
5221 | /* If a previous notification has sent the IPI, nothing to do. */ |
5222 | if (pi_test_and_set_on(&vmx->pi_desc)) | |
5223 | return; | |
5224 | ||
06a5524f | 5225 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
a20ed54d YZ |
5226 | kvm_vcpu_kick(vcpu); |
5227 | } | |
5228 | ||
a3a8ff8e NHE |
5229 | /* |
5230 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that | |
5231 | * will not change in the lifetime of the guest. | |
5232 | * Note that host-state that does change is set elsewhere. E.g., host-state | |
5233 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. | |
5234 | */ | |
a547c6db | 5235 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
a3a8ff8e NHE |
5236 | { |
5237 | u32 low32, high32; | |
5238 | unsigned long tmpl; | |
5239 | struct desc_ptr dt; | |
d6e41f11 | 5240 | unsigned long cr0, cr3, cr4; |
a3a8ff8e | 5241 | |
04ac88ab AL |
5242 | cr0 = read_cr0(); |
5243 | WARN_ON(cr0 & X86_CR0_TS); | |
5244 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ | |
d6e41f11 AL |
5245 | |
5246 | /* | |
5247 | * Save the most likely value for this task's CR3 in the VMCS. | |
5248 | * We can't use __get_current_cr3_fast() because we're not atomic. | |
5249 | */ | |
6c690ee1 | 5250 | cr3 = __read_cr3(); |
d6e41f11 | 5251 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
44889942 | 5252 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
a3a8ff8e | 5253 | |
d974baa3 | 5254 | /* Save the most likely value for this task's CR4 in the VMCS. */ |
1e02ce4c | 5255 | cr4 = cr4_read_shadow(); |
d974baa3 | 5256 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
44889942 | 5257 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
d974baa3 | 5258 | |
a3a8ff8e | 5259 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
b2da15ac AK |
5260 | #ifdef CONFIG_X86_64 |
5261 | /* | |
5262 | * Load null selectors, so we can avoid reloading them in | |
5263 | * __vmx_load_host_state(), in case userspace uses the null selectors | |
5264 | * too (the expected case). | |
5265 | */ | |
5266 | vmcs_write16(HOST_DS_SELECTOR, 0); | |
5267 | vmcs_write16(HOST_ES_SELECTOR, 0); | |
5268 | #else | |
a3a8ff8e NHE |
5269 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5270 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ | |
b2da15ac | 5271 | #endif |
a3a8ff8e NHE |
5272 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5273 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ | |
5274 | ||
87930019 | 5275 | store_idt(&dt); |
a3a8ff8e | 5276 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
a547c6db | 5277 | vmx->host_idt_base = dt.address; |
a3a8ff8e | 5278 | |
83287ea4 | 5279 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
a3a8ff8e NHE |
5280 | |
5281 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); | |
5282 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); | |
5283 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); | |
5284 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ | |
5285 | ||
5286 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { | |
5287 | rdmsr(MSR_IA32_CR_PAT, low32, high32); | |
5288 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); | |
5289 | } | |
5290 | } | |
5291 | ||
bf8179a0 NHE |
5292 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
5293 | { | |
5294 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; | |
5295 | if (enable_ept) | |
5296 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; | |
fe3ef05c NHE |
5297 | if (is_guest_mode(&vmx->vcpu)) |
5298 | vmx->vcpu.arch.cr4_guest_owned_bits &= | |
5299 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; | |
bf8179a0 NHE |
5300 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
5301 | } | |
5302 | ||
01e439be YZ |
5303 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
5304 | { | |
5305 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; | |
5306 | ||
d62caabb | 5307 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
01e439be | 5308 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
d02fcf50 PB |
5309 | |
5310 | if (!enable_vnmi) | |
5311 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; | |
5312 | ||
64672c95 YJ |
5313 | /* Enable the preemption timer dynamically */ |
5314 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
5315 | return pin_based_exec_ctrl; |
5316 | } | |
5317 | ||
d62caabb AS |
5318 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
5319 | { | |
5320 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5321 | ||
5322 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); | |
3ce424e4 RK |
5323 | if (cpu_has_secondary_exec_ctrls()) { |
5324 | if (kvm_vcpu_apicv_active(vcpu)) | |
5325 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
5326 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5327 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5328 | else | |
5329 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
5330 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5331 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5332 | } | |
5333 | ||
5334 | if (cpu_has_vmx_msr_bitmap()) | |
5335 | vmx_set_msr_bitmap(vcpu); | |
d62caabb AS |
5336 | } |
5337 | ||
bf8179a0 NHE |
5338 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
5339 | { | |
5340 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; | |
d16c293e PB |
5341 | |
5342 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) | |
5343 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; | |
5344 | ||
35754c98 | 5345 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
bf8179a0 NHE |
5346 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
5347 | #ifdef CONFIG_X86_64 | |
5348 | exec_control |= CPU_BASED_CR8_STORE_EXITING | | |
5349 | CPU_BASED_CR8_LOAD_EXITING; | |
5350 | #endif | |
5351 | } | |
5352 | if (!enable_ept) | |
5353 | exec_control |= CPU_BASED_CR3_STORE_EXITING | | |
5354 | CPU_BASED_CR3_LOAD_EXITING | | |
5355 | CPU_BASED_INVLPG_EXITING; | |
5356 | return exec_control; | |
5357 | } | |
5358 | ||
45ec368c | 5359 | static bool vmx_rdrand_supported(void) |
bf8179a0 | 5360 | { |
45ec368c | 5361 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
736fdf72 | 5362 | SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5363 | } |
5364 | ||
75f4fc8d JM |
5365 | static bool vmx_rdseed_supported(void) |
5366 | { | |
5367 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
736fdf72 | 5368 | SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5369 | } |
5370 | ||
80154d77 | 5371 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
bf8179a0 | 5372 | { |
80154d77 PB |
5373 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
5374 | ||
bf8179a0 | 5375 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
0367f205 | 5376 | |
80154d77 | 5377 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
bf8179a0 NHE |
5378 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
5379 | if (vmx->vpid == 0) | |
5380 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; | |
5381 | if (!enable_ept) { | |
5382 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; | |
5383 | enable_unrestricted_guest = 0; | |
ad756a16 MJ |
5384 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
5385 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; | |
bf8179a0 NHE |
5386 | } |
5387 | if (!enable_unrestricted_guest) | |
5388 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
5389 | if (!ple_gap) | |
5390 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
80154d77 | 5391 | if (!kvm_vcpu_apicv_active(vcpu)) |
c7c9c56c YZ |
5392 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
5393 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
8d14695f | 5394 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
0367f205 PB |
5395 | |
5396 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, | |
5397 | * in vmx_set_cr4. */ | |
5398 | exec_control &= ~SECONDARY_EXEC_DESC; | |
5399 | ||
abc4fc58 AG |
5400 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
5401 | (handle_vmptrld). | |
5402 | We can NOT enable shadow_vmcs here because we don't have yet | |
5403 | a current VMCS12 | |
5404 | */ | |
5405 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; | |
a3eaa864 KH |
5406 | |
5407 | if (!enable_pml) | |
5408 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; | |
843e4330 | 5409 | |
3db13480 PB |
5410 | if (vmx_xsaves_supported()) { |
5411 | /* Exposing XSAVES only when XSAVE is exposed */ | |
5412 | bool xsaves_enabled = | |
5413 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && | |
5414 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); | |
5415 | ||
5416 | if (!xsaves_enabled) | |
5417 | exec_control &= ~SECONDARY_EXEC_XSAVES; | |
5418 | ||
5419 | if (nested) { | |
5420 | if (xsaves_enabled) | |
5421 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
5422 | SECONDARY_EXEC_XSAVES; | |
5423 | else | |
5424 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
5425 | ~SECONDARY_EXEC_XSAVES; | |
5426 | } | |
5427 | } | |
5428 | ||
80154d77 PB |
5429 | if (vmx_rdtscp_supported()) { |
5430 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); | |
5431 | if (!rdtscp_enabled) | |
5432 | exec_control &= ~SECONDARY_EXEC_RDTSCP; | |
5433 | ||
5434 | if (nested) { | |
5435 | if (rdtscp_enabled) | |
5436 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
5437 | SECONDARY_EXEC_RDTSCP; | |
5438 | else | |
5439 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
5440 | ~SECONDARY_EXEC_RDTSCP; | |
5441 | } | |
5442 | } | |
5443 | ||
5444 | if (vmx_invpcid_supported()) { | |
5445 | /* Exposing INVPCID only when PCID is exposed */ | |
5446 | bool invpcid_enabled = | |
5447 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && | |
5448 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); | |
5449 | ||
5450 | if (!invpcid_enabled) { | |
5451 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; | |
5452 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); | |
5453 | } | |
5454 | ||
5455 | if (nested) { | |
5456 | if (invpcid_enabled) | |
5457 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
5458 | SECONDARY_EXEC_ENABLE_INVPCID; | |
5459 | else | |
5460 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
5461 | ~SECONDARY_EXEC_ENABLE_INVPCID; | |
5462 | } | |
5463 | } | |
5464 | ||
45ec368c JM |
5465 | if (vmx_rdrand_supported()) { |
5466 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); | |
5467 | if (rdrand_enabled) | |
736fdf72 | 5468 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5469 | |
5470 | if (nested) { | |
5471 | if (rdrand_enabled) | |
5472 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
736fdf72 | 5473 | SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5474 | else |
5475 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
736fdf72 | 5476 | ~SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5477 | } |
5478 | } | |
5479 | ||
75f4fc8d JM |
5480 | if (vmx_rdseed_supported()) { |
5481 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); | |
5482 | if (rdseed_enabled) | |
736fdf72 | 5483 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5484 | |
5485 | if (nested) { | |
5486 | if (rdseed_enabled) | |
5487 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
736fdf72 | 5488 | SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5489 | else |
5490 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
736fdf72 | 5491 | ~SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5492 | } |
5493 | } | |
5494 | ||
80154d77 | 5495 | vmx->secondary_exec_control = exec_control; |
bf8179a0 NHE |
5496 | } |
5497 | ||
ce88decf XG |
5498 | static void ept_set_mmio_spte_mask(void) |
5499 | { | |
5500 | /* | |
5501 | * EPT Misconfigurations can be generated if the value of bits 2:0 | |
5502 | * of an EPT paging-structure entry is 110b (write/execute). | |
ce88decf | 5503 | */ |
dcdca5fe PF |
5504 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
5505 | VMX_EPT_MISCONFIG_WX_VALUE); | |
ce88decf XG |
5506 | } |
5507 | ||
f53cd63c | 5508 | #define VMX_XSS_EXIT_BITMAP 0 |
6aa8b732 AK |
5509 | /* |
5510 | * Sets up the vmcs for emulated real mode. | |
5511 | */ | |
12d79917 | 5512 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
6aa8b732 | 5513 | { |
2e4ce7f5 | 5514 | #ifdef CONFIG_X86_64 |
6aa8b732 | 5515 | unsigned long a; |
2e4ce7f5 | 5516 | #endif |
6aa8b732 | 5517 | int i; |
6aa8b732 | 5518 | |
4607c2d7 AG |
5519 | if (enable_shadow_vmcs) { |
5520 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); | |
5521 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); | |
5522 | } | |
25c5f225 | 5523 | if (cpu_has_vmx_msr_bitmap()) |
5897297b | 5524 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
25c5f225 | 5525 | |
6aa8b732 AK |
5526 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
5527 | ||
6aa8b732 | 5528 | /* Control */ |
01e439be | 5529 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); |
64672c95 | 5530 | vmx->hv_deadline_tsc = -1; |
6e5d865c | 5531 | |
bf8179a0 | 5532 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
6aa8b732 | 5533 | |
dfa169bb | 5534 | if (cpu_has_secondary_exec_ctrls()) { |
80154d77 | 5535 | vmx_compute_secondary_exec_control(vmx); |
bf8179a0 | 5536 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
80154d77 | 5537 | vmx->secondary_exec_control); |
dfa169bb | 5538 | } |
f78e0e2e | 5539 | |
d62caabb | 5540 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
c7c9c56c YZ |
5541 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
5542 | vmcs_write64(EOI_EXIT_BITMAP1, 0); | |
5543 | vmcs_write64(EOI_EXIT_BITMAP2, 0); | |
5544 | vmcs_write64(EOI_EXIT_BITMAP3, 0); | |
5545 | ||
5546 | vmcs_write16(GUEST_INTR_STATUS, 0); | |
01e439be | 5547 | |
0bcf261c | 5548 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
01e439be | 5549 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
c7c9c56c YZ |
5550 | } |
5551 | ||
4b8d54f9 ZE |
5552 | if (ple_gap) { |
5553 | vmcs_write32(PLE_GAP, ple_gap); | |
a7653ecd RK |
5554 | vmx->ple_window = ple_window; |
5555 | vmx->ple_window_dirty = true; | |
4b8d54f9 ZE |
5556 | } |
5557 | ||
c3707958 XG |
5558 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
5559 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); | |
6aa8b732 AK |
5560 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
5561 | ||
9581d442 AK |
5562 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
5563 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ | |
a547c6db | 5564 | vmx_set_constant_host_state(vmx); |
05b3e0c2 | 5565 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
5566 | rdmsrl(MSR_FS_BASE, a); |
5567 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ | |
5568 | rdmsrl(MSR_GS_BASE, a); | |
5569 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ | |
5570 | #else | |
5571 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ | |
5572 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ | |
5573 | #endif | |
5574 | ||
2a499e49 BD |
5575 | if (cpu_has_vmx_vmfunc()) |
5576 | vmcs_write64(VM_FUNCTION_CONTROL, 0); | |
5577 | ||
2cc51560 ED |
5578 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
5579 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); | |
61d2ef2c | 5580 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
2cc51560 | 5581 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
61d2ef2c | 5582 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
6aa8b732 | 5583 | |
74545705 RK |
5584 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
5585 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); | |
468d472f | 5586 | |
03916db9 | 5587 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
6aa8b732 AK |
5588 | u32 index = vmx_msr_index[i]; |
5589 | u32 data_low, data_high; | |
a2fa3e9f | 5590 | int j = vmx->nmsrs; |
6aa8b732 AK |
5591 | |
5592 | if (rdmsr_safe(index, &data_low, &data_high) < 0) | |
5593 | continue; | |
432bd6cb AK |
5594 | if (wrmsr_safe(index, data_low, data_high) < 0) |
5595 | continue; | |
26bb0981 AK |
5596 | vmx->guest_msrs[j].index = i; |
5597 | vmx->guest_msrs[j].data = 0; | |
d5696725 | 5598 | vmx->guest_msrs[j].mask = -1ull; |
a2fa3e9f | 5599 | ++vmx->nmsrs; |
6aa8b732 | 5600 | } |
6aa8b732 | 5601 | |
2961e876 GN |
5602 | |
5603 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); | |
6aa8b732 AK |
5604 | |
5605 | /* 22.2.1, 20.8.1 */ | |
2961e876 | 5606 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
1c3d14fe | 5607 | |
bd7e5b08 PB |
5608 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
5609 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); | |
5610 | ||
bf8179a0 | 5611 | set_cr4_guest_host_mask(vmx); |
e00c8cf2 | 5612 | |
f53cd63c WL |
5613 | if (vmx_xsaves_supported()) |
5614 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); | |
5615 | ||
4e59516a PF |
5616 | if (enable_pml) { |
5617 | ASSERT(vmx->pml_pg); | |
5618 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
5619 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
5620 | } | |
e00c8cf2 AK |
5621 | } |
5622 | ||
d28bc9dd | 5623 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
e00c8cf2 AK |
5624 | { |
5625 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
58cb628d | 5626 | struct msr_data apic_base_msr; |
d28bc9dd | 5627 | u64 cr0; |
e00c8cf2 | 5628 | |
7ffd92c5 | 5629 | vmx->rmode.vm86_active = 0; |
e00c8cf2 | 5630 | |
ad312c7c | 5631 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
d28bc9dd NA |
5632 | kvm_set_cr8(vcpu, 0); |
5633 | ||
5634 | if (!init_event) { | |
5635 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | | |
5636 | MSR_IA32_APICBASE_ENABLE; | |
5637 | if (kvm_vcpu_is_reset_bsp(vcpu)) | |
5638 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; | |
5639 | apic_base_msr.host_initiated = true; | |
5640 | kvm_set_apic_base(vcpu, &apic_base_msr); | |
5641 | } | |
e00c8cf2 | 5642 | |
2fb92db1 AK |
5643 | vmx_segment_cache_clear(vmx); |
5644 | ||
5706be0d | 5645 | seg_setup(VCPU_SREG_CS); |
66450a21 | 5646 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
f3531054 | 5647 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
e00c8cf2 AK |
5648 | |
5649 | seg_setup(VCPU_SREG_DS); | |
5650 | seg_setup(VCPU_SREG_ES); | |
5651 | seg_setup(VCPU_SREG_FS); | |
5652 | seg_setup(VCPU_SREG_GS); | |
5653 | seg_setup(VCPU_SREG_SS); | |
5654 | ||
5655 | vmcs_write16(GUEST_TR_SELECTOR, 0); | |
5656 | vmcs_writel(GUEST_TR_BASE, 0); | |
5657 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); | |
5658 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); | |
5659 | ||
5660 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); | |
5661 | vmcs_writel(GUEST_LDTR_BASE, 0); | |
5662 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); | |
5663 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); | |
5664 | ||
d28bc9dd NA |
5665 | if (!init_event) { |
5666 | vmcs_write32(GUEST_SYSENTER_CS, 0); | |
5667 | vmcs_writel(GUEST_SYSENTER_ESP, 0); | |
5668 | vmcs_writel(GUEST_SYSENTER_EIP, 0); | |
5669 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
5670 | } | |
e00c8cf2 | 5671 | |
c37c2873 | 5672 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
66450a21 | 5673 | kvm_rip_write(vcpu, 0xfff0); |
e00c8cf2 | 5674 | |
e00c8cf2 AK |
5675 | vmcs_writel(GUEST_GDTR_BASE, 0); |
5676 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); | |
5677 | ||
5678 | vmcs_writel(GUEST_IDTR_BASE, 0); | |
5679 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); | |
5680 | ||
443381a8 | 5681 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
e00c8cf2 | 5682 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
f3531054 | 5683 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
a554d207 WL |
5684 | if (kvm_mpx_supported()) |
5685 | vmcs_write64(GUEST_BNDCFGS, 0); | |
e00c8cf2 | 5686 | |
e00c8cf2 AK |
5687 | setup_msrs(vmx); |
5688 | ||
6aa8b732 AK |
5689 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
5690 | ||
d28bc9dd | 5691 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
f78e0e2e | 5692 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
35754c98 | 5693 | if (cpu_need_tpr_shadow(vcpu)) |
f78e0e2e | 5694 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
d28bc9dd | 5695 | __pa(vcpu->arch.apic->regs)); |
f78e0e2e SY |
5696 | vmcs_write32(TPR_THRESHOLD, 0); |
5697 | } | |
5698 | ||
a73896cb | 5699 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
6aa8b732 | 5700 | |
2384d2b3 SY |
5701 | if (vmx->vpid != 0) |
5702 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
5703 | ||
d28bc9dd | 5704 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
d28bc9dd | 5705 | vmx->vcpu.arch.cr0 = cr0; |
f2463247 | 5706 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
d28bc9dd | 5707 | vmx_set_cr4(vcpu, 0); |
5690891b | 5708 | vmx_set_efer(vcpu, 0); |
bd7e5b08 | 5709 | |
d28bc9dd | 5710 | update_exception_bitmap(vcpu); |
6aa8b732 | 5711 | |
dd5f5341 | 5712 | vpid_sync_context(vmx->vpid); |
6aa8b732 AK |
5713 | } |
5714 | ||
b6f1250e NHE |
5715 | /* |
5716 | * In nested virtualization, check if L1 asked to exit on external interrupts. | |
5717 | * For most existing hypervisors, this will always return true. | |
5718 | */ | |
5719 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) | |
5720 | { | |
5721 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & | |
5722 | PIN_BASED_EXT_INTR_MASK; | |
5723 | } | |
5724 | ||
77b0f5d6 BD |
5725 | /* |
5726 | * In nested virtualization, check if L1 has set | |
5727 | * VM_EXIT_ACK_INTR_ON_EXIT | |
5728 | */ | |
5729 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) | |
5730 | { | |
5731 | return get_vmcs12(vcpu)->vm_exit_controls & | |
5732 | VM_EXIT_ACK_INTR_ON_EXIT; | |
5733 | } | |
5734 | ||
ea8ceb83 JK |
5735 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
5736 | { | |
5737 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & | |
5738 | PIN_BASED_NMI_EXITING; | |
5739 | } | |
5740 | ||
c9a7953f | 5741 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 5742 | { |
47c0152e PB |
5743 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
5744 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
3b86cd99 JK |
5745 | } |
5746 | ||
c9a7953f | 5747 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 5748 | { |
d02fcf50 | 5749 | if (!enable_vnmi || |
8a1b4392 | 5750 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
c9a7953f JK |
5751 | enable_irq_window(vcpu); |
5752 | return; | |
5753 | } | |
3b86cd99 | 5754 | |
47c0152e PB |
5755 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
5756 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
3b86cd99 JK |
5757 | } |
5758 | ||
66fd3f7f | 5759 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
85f455f7 | 5760 | { |
9c8cba37 | 5761 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
66fd3f7f GN |
5762 | uint32_t intr; |
5763 | int irq = vcpu->arch.interrupt.nr; | |
9c8cba37 | 5764 | |
229456fc | 5765 | trace_kvm_inj_virq(irq); |
2714d1d3 | 5766 | |
fa89a817 | 5767 | ++vcpu->stat.irq_injections; |
7ffd92c5 | 5768 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
5769 | int inc_eip = 0; |
5770 | if (vcpu->arch.interrupt.soft) | |
5771 | inc_eip = vcpu->arch.event_exit_inst_len; | |
5772 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) | |
a92601bb | 5773 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
85f455f7 ED |
5774 | return; |
5775 | } | |
66fd3f7f GN |
5776 | intr = irq | INTR_INFO_VALID_MASK; |
5777 | if (vcpu->arch.interrupt.soft) { | |
5778 | intr |= INTR_TYPE_SOFT_INTR; | |
5779 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
5780 | vmx->vcpu.arch.event_exit_inst_len); | |
5781 | } else | |
5782 | intr |= INTR_TYPE_EXT_INTR; | |
5783 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); | |
85f455f7 ED |
5784 | } |
5785 | ||
f08864b4 SY |
5786 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
5787 | { | |
66a5a347 JK |
5788 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
5789 | ||
d02fcf50 | 5790 | if (!enable_vnmi) { |
8a1b4392 PB |
5791 | /* |
5792 | * Tracking the NMI-blocked state in software is built upon | |
5793 | * finding the next open IRQ window. This, in turn, depends on | |
5794 | * well-behaving guests: They have to keep IRQs disabled at | |
5795 | * least as long as the NMI handler runs. Otherwise we may | |
5796 | * cause NMI nesting, maybe breaking the guest. But as this is | |
5797 | * highly unlikely, we can live with the residual risk. | |
5798 | */ | |
5799 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; | |
5800 | vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
5801 | } | |
5802 | ||
4c4a6f79 PB |
5803 | ++vcpu->stat.nmi_injections; |
5804 | vmx->loaded_vmcs->nmi_known_unmasked = false; | |
3b86cd99 | 5805 | |
7ffd92c5 | 5806 | if (vmx->rmode.vm86_active) { |
71f9833b | 5807 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
a92601bb | 5808 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
66a5a347 JK |
5809 | return; |
5810 | } | |
c5a6d5f7 | 5811 | |
f08864b4 SY |
5812 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
5813 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); | |
f08864b4 SY |
5814 | } |
5815 | ||
3cfc3092 JK |
5816 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
5817 | { | |
4c4a6f79 PB |
5818 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
5819 | bool masked; | |
5820 | ||
d02fcf50 | 5821 | if (!enable_vnmi) |
8a1b4392 | 5822 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
4c4a6f79 | 5823 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
9d58b931 | 5824 | return false; |
4c4a6f79 PB |
5825 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
5826 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
5827 | return masked; | |
3cfc3092 JK |
5828 | } |
5829 | ||
5830 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) | |
5831 | { | |
5832 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5833 | ||
d02fcf50 | 5834 | if (!enable_vnmi) { |
8a1b4392 PB |
5835 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
5836 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; | |
5837 | vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
5838 | } | |
5839 | } else { | |
5840 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
5841 | if (masked) | |
5842 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
5843 | GUEST_INTR_STATE_NMI); | |
5844 | else | |
5845 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, | |
5846 | GUEST_INTR_STATE_NMI); | |
5847 | } | |
3cfc3092 JK |
5848 | } |
5849 | ||
2505dc9f JK |
5850 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
5851 | { | |
b6b8a145 JK |
5852 | if (to_vmx(vcpu)->nested.nested_run_pending) |
5853 | return 0; | |
ea8ceb83 | 5854 | |
d02fcf50 | 5855 | if (!enable_vnmi && |
8a1b4392 PB |
5856 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
5857 | return 0; | |
5858 | ||
2505dc9f JK |
5859 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
5860 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI | |
5861 | | GUEST_INTR_STATE_NMI)); | |
5862 | } | |
5863 | ||
78646121 GN |
5864 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
5865 | { | |
b6b8a145 JK |
5866 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
5867 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && | |
c4282df9 GN |
5868 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
5869 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); | |
78646121 GN |
5870 | } |
5871 | ||
cbc94022 IE |
5872 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
5873 | { | |
5874 | int ret; | |
cbc94022 | 5875 | |
1d8007bd PB |
5876 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
5877 | PAGE_SIZE * 3); | |
cbc94022 IE |
5878 | if (ret) |
5879 | return ret; | |
bfc6d222 | 5880 | kvm->arch.tss_addr = addr; |
1f755a82 | 5881 | return init_rmode_tss(kvm); |
cbc94022 IE |
5882 | } |
5883 | ||
0ca1b4f4 | 5884 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
6aa8b732 | 5885 | { |
77ab6db0 | 5886 | switch (vec) { |
77ab6db0 | 5887 | case BP_VECTOR: |
c573cd22 JK |
5888 | /* |
5889 | * Update instruction length as we may reinject the exception | |
5890 | * from user space while in guest debugging mode. | |
5891 | */ | |
5892 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = | |
5893 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
d0bfb940 | 5894 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
0ca1b4f4 GN |
5895 | return false; |
5896 | /* fall through */ | |
5897 | case DB_VECTOR: | |
5898 | if (vcpu->guest_debug & | |
5899 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
5900 | return false; | |
d0bfb940 JK |
5901 | /* fall through */ |
5902 | case DE_VECTOR: | |
77ab6db0 JK |
5903 | case OF_VECTOR: |
5904 | case BR_VECTOR: | |
5905 | case UD_VECTOR: | |
5906 | case DF_VECTOR: | |
5907 | case SS_VECTOR: | |
5908 | case GP_VECTOR: | |
5909 | case MF_VECTOR: | |
0ca1b4f4 GN |
5910 | return true; |
5911 | break; | |
77ab6db0 | 5912 | } |
0ca1b4f4 GN |
5913 | return false; |
5914 | } | |
5915 | ||
5916 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, | |
5917 | int vec, u32 err_code) | |
5918 | { | |
5919 | /* | |
5920 | * Instruction with address size override prefix opcode 0x67 | |
5921 | * Cause the #SS fault with 0 error code in VM86 mode. | |
5922 | */ | |
5923 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { | |
5924 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { | |
5925 | if (vcpu->arch.halt_request) { | |
5926 | vcpu->arch.halt_request = 0; | |
5cb56059 | 5927 | return kvm_vcpu_halt(vcpu); |
0ca1b4f4 GN |
5928 | } |
5929 | return 1; | |
5930 | } | |
5931 | return 0; | |
5932 | } | |
5933 | ||
5934 | /* | |
5935 | * Forward all other exceptions that are valid in real mode. | |
5936 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with | |
5937 | * the required debugging infrastructure rework. | |
5938 | */ | |
5939 | kvm_queue_exception(vcpu, vec); | |
5940 | return 1; | |
6aa8b732 AK |
5941 | } |
5942 | ||
a0861c02 AK |
5943 | /* |
5944 | * Trigger machine check on the host. We assume all the MSRs are already set up | |
5945 | * by the CPU and that we still run on the same CPU as the MCE occurred on. | |
5946 | * We pass a fake environment to the machine check handler because we want | |
5947 | * the guest to be always treated like user space, no matter what context | |
5948 | * it used internally. | |
5949 | */ | |
5950 | static void kvm_machine_check(void) | |
5951 | { | |
5952 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) | |
5953 | struct pt_regs regs = { | |
5954 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ | |
5955 | .flags = X86_EFLAGS_IF, | |
5956 | }; | |
5957 | ||
5958 | do_machine_check(®s, 0); | |
5959 | #endif | |
5960 | } | |
5961 | ||
851ba692 | 5962 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
a0861c02 AK |
5963 | { |
5964 | /* already handled by vcpu_run */ | |
5965 | return 1; | |
5966 | } | |
5967 | ||
851ba692 | 5968 | static int handle_exception(struct kvm_vcpu *vcpu) |
6aa8b732 | 5969 | { |
1155f76a | 5970 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
851ba692 | 5971 | struct kvm_run *kvm_run = vcpu->run; |
d0bfb940 | 5972 | u32 intr_info, ex_no, error_code; |
42dbaa5a | 5973 | unsigned long cr2, rip, dr6; |
6aa8b732 AK |
5974 | u32 vect_info; |
5975 | enum emulation_result er; | |
5976 | ||
1155f76a | 5977 | vect_info = vmx->idt_vectoring_info; |
88786475 | 5978 | intr_info = vmx->exit_intr_info; |
6aa8b732 | 5979 | |
a0861c02 | 5980 | if (is_machine_check(intr_info)) |
851ba692 | 5981 | return handle_machine_check(vcpu); |
a0861c02 | 5982 | |
ef85b673 | 5983 | if (is_nmi(intr_info)) |
1b6269db | 5984 | return 1; /* already handled by vmx_vcpu_run() */ |
2ab455cc | 5985 | |
7aa81cc0 | 5986 | if (is_invalid_opcode(intr_info)) { |
ac9b305c | 5987 | WARN_ON_ONCE(is_guest_mode(vcpu)); |
51d8b661 | 5988 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
61cb57c9 LA |
5989 | if (er == EMULATE_USER_EXIT) |
5990 | return 0; | |
7aa81cc0 | 5991 | if (er != EMULATE_DONE) |
7ee5d940 | 5992 | kvm_queue_exception(vcpu, UD_VECTOR); |
7aa81cc0 AL |
5993 | return 1; |
5994 | } | |
5995 | ||
6aa8b732 | 5996 | error_code = 0; |
2e11384c | 5997 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
6aa8b732 | 5998 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
bf4ca23e XG |
5999 | |
6000 | /* | |
6001 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing | |
6002 | * MMIO, it is better to report an internal error. | |
6003 | * See the comments in vmx_handle_exit. | |
6004 | */ | |
6005 | if ((vect_info & VECTORING_INFO_VALID_MASK) && | |
6006 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { | |
6007 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
6008 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; | |
80f0e95d | 6009 | vcpu->run->internal.ndata = 3; |
bf4ca23e XG |
6010 | vcpu->run->internal.data[0] = vect_info; |
6011 | vcpu->run->internal.data[1] = intr_info; | |
80f0e95d | 6012 | vcpu->run->internal.data[2] = error_code; |
bf4ca23e XG |
6013 | return 0; |
6014 | } | |
6015 | ||
6aa8b732 AK |
6016 | if (is_page_fault(intr_info)) { |
6017 | cr2 = vmcs_readl(EXIT_QUALIFICATION); | |
1261bfa3 WL |
6018 | /* EPT won't cause page fault directly */ |
6019 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); | |
d0006530 | 6020 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
6aa8b732 AK |
6021 | } |
6022 | ||
d0bfb940 | 6023 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
0ca1b4f4 GN |
6024 | |
6025 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) | |
6026 | return handle_rmode_exception(vcpu, ex_no, error_code); | |
6027 | ||
42dbaa5a | 6028 | switch (ex_no) { |
54a20552 EN |
6029 | case AC_VECTOR: |
6030 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); | |
6031 | return 1; | |
42dbaa5a JK |
6032 | case DB_VECTOR: |
6033 | dr6 = vmcs_readl(EXIT_QUALIFICATION); | |
6034 | if (!(vcpu->guest_debug & | |
6035 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { | |
8246bf52 | 6036 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 6037 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
fd2a445a HD |
6038 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ |
6039 | skip_emulated_instruction(vcpu); | |
6040 | ||
42dbaa5a JK |
6041 | kvm_queue_exception(vcpu, DB_VECTOR); |
6042 | return 1; | |
6043 | } | |
6044 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; | |
6045 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); | |
6046 | /* fall through */ | |
6047 | case BP_VECTOR: | |
c573cd22 JK |
6048 | /* |
6049 | * Update instruction length as we may reinject #BP from | |
6050 | * user space while in guest debugging mode. Reading it for | |
6051 | * #DB as well causes no harm, it is not used in that case. | |
6052 | */ | |
6053 | vmx->vcpu.arch.event_exit_inst_len = | |
6054 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
6aa8b732 | 6055 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
0a434bb2 | 6056 | rip = kvm_rip_read(vcpu); |
d0bfb940 JK |
6057 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
6058 | kvm_run->debug.arch.exception = ex_no; | |
42dbaa5a JK |
6059 | break; |
6060 | default: | |
d0bfb940 JK |
6061 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
6062 | kvm_run->ex.exception = ex_no; | |
6063 | kvm_run->ex.error_code = error_code; | |
42dbaa5a | 6064 | break; |
6aa8b732 | 6065 | } |
6aa8b732 AK |
6066 | return 0; |
6067 | } | |
6068 | ||
851ba692 | 6069 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
6aa8b732 | 6070 | { |
1165f5fe | 6071 | ++vcpu->stat.irq_exits; |
6aa8b732 AK |
6072 | return 1; |
6073 | } | |
6074 | ||
851ba692 | 6075 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
988ad74f | 6076 | { |
851ba692 | 6077 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
bbeac283 | 6078 | vcpu->mmio_needed = 0; |
988ad74f AK |
6079 | return 0; |
6080 | } | |
6aa8b732 | 6081 | |
851ba692 | 6082 | static int handle_io(struct kvm_vcpu *vcpu) |
6aa8b732 | 6083 | { |
bfdaab09 | 6084 | unsigned long exit_qualification; |
6affcbed | 6085 | int size, in, string, ret; |
039576c0 | 6086 | unsigned port; |
6aa8b732 | 6087 | |
bfdaab09 | 6088 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
039576c0 | 6089 | string = (exit_qualification & 16) != 0; |
cf8f70bf | 6090 | in = (exit_qualification & 8) != 0; |
e70669ab | 6091 | |
cf8f70bf | 6092 | ++vcpu->stat.io_exits; |
e70669ab | 6093 | |
cf8f70bf | 6094 | if (string || in) |
51d8b661 | 6095 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
e70669ab | 6096 | |
cf8f70bf GN |
6097 | port = exit_qualification >> 16; |
6098 | size = (exit_qualification & 7) + 1; | |
cf8f70bf | 6099 | |
6affcbed KH |
6100 | ret = kvm_skip_emulated_instruction(vcpu); |
6101 | ||
6102 | /* | |
6103 | * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered | |
6104 | * KVM_EXIT_DEBUG here. | |
6105 | */ | |
6106 | return kvm_fast_pio_out(vcpu, size, port) && ret; | |
6aa8b732 AK |
6107 | } |
6108 | ||
102d8325 IM |
6109 | static void |
6110 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) | |
6111 | { | |
6112 | /* | |
6113 | * Patch in the VMCALL instruction: | |
6114 | */ | |
6115 | hypercall[0] = 0x0f; | |
6116 | hypercall[1] = 0x01; | |
6117 | hypercall[2] = 0xc1; | |
102d8325 IM |
6118 | } |
6119 | ||
0fa06071 | 6120 | /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ |
eeadf9e7 NHE |
6121 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
6122 | { | |
eeadf9e7 | 6123 | if (is_guest_mode(vcpu)) { |
1a0d74e6 JK |
6124 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
6125 | unsigned long orig_val = val; | |
6126 | ||
eeadf9e7 NHE |
6127 | /* |
6128 | * We get here when L2 changed cr0 in a way that did not change | |
6129 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), | |
1a0d74e6 JK |
6130 | * but did change L0 shadowed bits. So we first calculate the |
6131 | * effective cr0 value that L1 would like to write into the | |
6132 | * hardware. It consists of the L2-owned bits from the new | |
6133 | * value combined with the L1-owned bits from L1's guest_cr0. | |
eeadf9e7 | 6134 | */ |
1a0d74e6 JK |
6135 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
6136 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); | |
6137 | ||
3899152c | 6138 | if (!nested_guest_cr0_valid(vcpu, val)) |
eeadf9e7 | 6139 | return 1; |
1a0d74e6 JK |
6140 | |
6141 | if (kvm_set_cr0(vcpu, val)) | |
6142 | return 1; | |
6143 | vmcs_writel(CR0_READ_SHADOW, orig_val); | |
eeadf9e7 | 6144 | return 0; |
1a0d74e6 JK |
6145 | } else { |
6146 | if (to_vmx(vcpu)->nested.vmxon && | |
3899152c | 6147 | !nested_host_cr0_valid(vcpu, val)) |
1a0d74e6 | 6148 | return 1; |
3899152c | 6149 | |
eeadf9e7 | 6150 | return kvm_set_cr0(vcpu, val); |
1a0d74e6 | 6151 | } |
eeadf9e7 NHE |
6152 | } |
6153 | ||
6154 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) | |
6155 | { | |
6156 | if (is_guest_mode(vcpu)) { | |
1a0d74e6 JK |
6157 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
6158 | unsigned long orig_val = val; | |
6159 | ||
6160 | /* analogously to handle_set_cr0 */ | |
6161 | val = (val & ~vmcs12->cr4_guest_host_mask) | | |
6162 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); | |
6163 | if (kvm_set_cr4(vcpu, val)) | |
eeadf9e7 | 6164 | return 1; |
1a0d74e6 | 6165 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
eeadf9e7 NHE |
6166 | return 0; |
6167 | } else | |
6168 | return kvm_set_cr4(vcpu, val); | |
6169 | } | |
6170 | ||
0367f205 PB |
6171 | static int handle_desc(struct kvm_vcpu *vcpu) |
6172 | { | |
6173 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); | |
6174 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; | |
6175 | } | |
6176 | ||
851ba692 | 6177 | static int handle_cr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6178 | { |
229456fc | 6179 | unsigned long exit_qualification, val; |
6aa8b732 AK |
6180 | int cr; |
6181 | int reg; | |
49a9b07e | 6182 | int err; |
6affcbed | 6183 | int ret; |
6aa8b732 | 6184 | |
bfdaab09 | 6185 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
6aa8b732 AK |
6186 | cr = exit_qualification & 15; |
6187 | reg = (exit_qualification >> 8) & 15; | |
6188 | switch ((exit_qualification >> 4) & 3) { | |
6189 | case 0: /* mov to cr */ | |
1e32c079 | 6190 | val = kvm_register_readl(vcpu, reg); |
229456fc | 6191 | trace_kvm_cr_write(cr, val); |
6aa8b732 AK |
6192 | switch (cr) { |
6193 | case 0: | |
eeadf9e7 | 6194 | err = handle_set_cr0(vcpu, val); |
6affcbed | 6195 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 6196 | case 3: |
2390218b | 6197 | err = kvm_set_cr3(vcpu, val); |
6affcbed | 6198 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 6199 | case 4: |
eeadf9e7 | 6200 | err = handle_set_cr4(vcpu, val); |
6affcbed | 6201 | return kvm_complete_insn_gp(vcpu, err); |
0a5fff19 GN |
6202 | case 8: { |
6203 | u8 cr8_prev = kvm_get_cr8(vcpu); | |
1e32c079 | 6204 | u8 cr8 = (u8)val; |
eea1cff9 | 6205 | err = kvm_set_cr8(vcpu, cr8); |
6affcbed | 6206 | ret = kvm_complete_insn_gp(vcpu, err); |
35754c98 | 6207 | if (lapic_in_kernel(vcpu)) |
6affcbed | 6208 | return ret; |
0a5fff19 | 6209 | if (cr8_prev <= cr8) |
6affcbed KH |
6210 | return ret; |
6211 | /* | |
6212 | * TODO: we might be squashing a | |
6213 | * KVM_GUESTDBG_SINGLESTEP-triggered | |
6214 | * KVM_EXIT_DEBUG here. | |
6215 | */ | |
851ba692 | 6216 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
0a5fff19 GN |
6217 | return 0; |
6218 | } | |
4b8073e4 | 6219 | } |
6aa8b732 | 6220 | break; |
25c4c276 | 6221 | case 2: /* clts */ |
bd7e5b08 PB |
6222 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
6223 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); | |
4d4ec087 | 6224 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
6affcbed | 6225 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6226 | case 1: /*mov from cr*/ |
6227 | switch (cr) { | |
6228 | case 3: | |
9f8fe504 AK |
6229 | val = kvm_read_cr3(vcpu); |
6230 | kvm_register_write(vcpu, reg, val); | |
6231 | trace_kvm_cr_read(cr, val); | |
6affcbed | 6232 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 | 6233 | case 8: |
229456fc MT |
6234 | val = kvm_get_cr8(vcpu); |
6235 | kvm_register_write(vcpu, reg, val); | |
6236 | trace_kvm_cr_read(cr, val); | |
6affcbed | 6237 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6238 | } |
6239 | break; | |
6240 | case 3: /* lmsw */ | |
a1f83a74 | 6241 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
4d4ec087 | 6242 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
a1f83a74 | 6243 | kvm_lmsw(vcpu, val); |
6aa8b732 | 6244 | |
6affcbed | 6245 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6246 | default: |
6247 | break; | |
6248 | } | |
851ba692 | 6249 | vcpu->run->exit_reason = 0; |
a737f256 | 6250 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
6aa8b732 AK |
6251 | (int)(exit_qualification >> 4) & 3, cr); |
6252 | return 0; | |
6253 | } | |
6254 | ||
851ba692 | 6255 | static int handle_dr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6256 | { |
bfdaab09 | 6257 | unsigned long exit_qualification; |
16f8a6f9 NA |
6258 | int dr, dr7, reg; |
6259 | ||
6260 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6261 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; | |
6262 | ||
6263 | /* First, if DR does not exist, trigger UD */ | |
6264 | if (!kvm_require_dr(vcpu, dr)) | |
6265 | return 1; | |
6aa8b732 | 6266 | |
f2483415 | 6267 | /* Do not handle if the CPL > 0, will trigger GP on re-entry */ |
0a79b009 AK |
6268 | if (!kvm_require_cpl(vcpu, 0)) |
6269 | return 1; | |
16f8a6f9 NA |
6270 | dr7 = vmcs_readl(GUEST_DR7); |
6271 | if (dr7 & DR7_GD) { | |
42dbaa5a JK |
6272 | /* |
6273 | * As the vm-exit takes precedence over the debug trap, we | |
6274 | * need to emulate the latter, either for the host or the | |
6275 | * guest debugging itself. | |
6276 | */ | |
6277 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { | |
851ba692 | 6278 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
16f8a6f9 | 6279 | vcpu->run->debug.arch.dr7 = dr7; |
82b32774 | 6280 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
851ba692 AK |
6281 | vcpu->run->debug.arch.exception = DB_VECTOR; |
6282 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; | |
42dbaa5a JK |
6283 | return 0; |
6284 | } else { | |
7305eb5d | 6285 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 6286 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
42dbaa5a JK |
6287 | kvm_queue_exception(vcpu, DB_VECTOR); |
6288 | return 1; | |
6289 | } | |
6290 | } | |
6291 | ||
81908bf4 | 6292 | if (vcpu->guest_debug == 0) { |
8f22372f PB |
6293 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6294 | CPU_BASED_MOV_DR_EXITING); | |
81908bf4 PB |
6295 | |
6296 | /* | |
6297 | * No more DR vmexits; force a reload of the debug registers | |
6298 | * and reenter on this instruction. The next vmexit will | |
6299 | * retrieve the full state of the debug registers. | |
6300 | */ | |
6301 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; | |
6302 | return 1; | |
6303 | } | |
6304 | ||
42dbaa5a JK |
6305 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
6306 | if (exit_qualification & TYPE_MOV_FROM_DR) { | |
020df079 | 6307 | unsigned long val; |
4c4d563b JK |
6308 | |
6309 | if (kvm_get_dr(vcpu, dr, &val)) | |
6310 | return 1; | |
6311 | kvm_register_write(vcpu, reg, val); | |
020df079 | 6312 | } else |
5777392e | 6313 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
4c4d563b JK |
6314 | return 1; |
6315 | ||
6affcbed | 6316 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6317 | } |
6318 | ||
73aaf249 JK |
6319 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
6320 | { | |
6321 | return vcpu->arch.dr6; | |
6322 | } | |
6323 | ||
6324 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) | |
6325 | { | |
6326 | } | |
6327 | ||
81908bf4 PB |
6328 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
6329 | { | |
81908bf4 PB |
6330 | get_debugreg(vcpu->arch.db[0], 0); |
6331 | get_debugreg(vcpu->arch.db[1], 1); | |
6332 | get_debugreg(vcpu->arch.db[2], 2); | |
6333 | get_debugreg(vcpu->arch.db[3], 3); | |
6334 | get_debugreg(vcpu->arch.dr6, 6); | |
6335 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); | |
6336 | ||
6337 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; | |
8f22372f | 6338 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING); |
81908bf4 PB |
6339 | } |
6340 | ||
020df079 GN |
6341 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
6342 | { | |
6343 | vmcs_writel(GUEST_DR7, val); | |
6344 | } | |
6345 | ||
851ba692 | 6346 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
6aa8b732 | 6347 | { |
6a908b62 | 6348 | return kvm_emulate_cpuid(vcpu); |
6aa8b732 AK |
6349 | } |
6350 | ||
851ba692 | 6351 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6352 | { |
ad312c7c | 6353 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
609e36d3 | 6354 | struct msr_data msr_info; |
6aa8b732 | 6355 | |
609e36d3 PB |
6356 | msr_info.index = ecx; |
6357 | msr_info.host_initiated = false; | |
6358 | if (vmx_get_msr(vcpu, &msr_info)) { | |
59200273 | 6359 | trace_kvm_msr_read_ex(ecx); |
c1a5d4f9 | 6360 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
6361 | return 1; |
6362 | } | |
6363 | ||
609e36d3 | 6364 | trace_kvm_msr_read(ecx, msr_info.data); |
2714d1d3 | 6365 | |
6aa8b732 | 6366 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
609e36d3 PB |
6367 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
6368 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; | |
6affcbed | 6369 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6370 | } |
6371 | ||
851ba692 | 6372 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6373 | { |
8fe8ab46 | 6374 | struct msr_data msr; |
ad312c7c ZX |
6375 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
6376 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) | |
6377 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); | |
6aa8b732 | 6378 | |
8fe8ab46 WA |
6379 | msr.data = data; |
6380 | msr.index = ecx; | |
6381 | msr.host_initiated = false; | |
854e8bb1 | 6382 | if (kvm_set_msr(vcpu, &msr) != 0) { |
59200273 | 6383 | trace_kvm_msr_write_ex(ecx, data); |
c1a5d4f9 | 6384 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
6385 | return 1; |
6386 | } | |
6387 | ||
59200273 | 6388 | trace_kvm_msr_write(ecx, data); |
6affcbed | 6389 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6390 | } |
6391 | ||
851ba692 | 6392 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
6e5d865c | 6393 | { |
eb90f341 | 6394 | kvm_apic_update_ppr(vcpu); |
6e5d865c YS |
6395 | return 1; |
6396 | } | |
6397 | ||
851ba692 | 6398 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
6aa8b732 | 6399 | { |
47c0152e PB |
6400 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6401 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
2714d1d3 | 6402 | |
3842d135 AK |
6403 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
6404 | ||
a26bf12a | 6405 | ++vcpu->stat.irq_window_exits; |
6aa8b732 AK |
6406 | return 1; |
6407 | } | |
6408 | ||
851ba692 | 6409 | static int handle_halt(struct kvm_vcpu *vcpu) |
6aa8b732 | 6410 | { |
d3bef15f | 6411 | return kvm_emulate_halt(vcpu); |
6aa8b732 AK |
6412 | } |
6413 | ||
851ba692 | 6414 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
c21415e8 | 6415 | { |
0d9c055e | 6416 | return kvm_emulate_hypercall(vcpu); |
c21415e8 IM |
6417 | } |
6418 | ||
ec25d5e6 GN |
6419 | static int handle_invd(struct kvm_vcpu *vcpu) |
6420 | { | |
51d8b661 | 6421 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
ec25d5e6 GN |
6422 | } |
6423 | ||
851ba692 | 6424 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
a7052897 | 6425 | { |
f9c617f6 | 6426 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
a7052897 MT |
6427 | |
6428 | kvm_mmu_invlpg(vcpu, exit_qualification); | |
6affcbed | 6429 | return kvm_skip_emulated_instruction(vcpu); |
a7052897 MT |
6430 | } |
6431 | ||
fee84b07 AK |
6432 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
6433 | { | |
6434 | int err; | |
6435 | ||
6436 | err = kvm_rdpmc(vcpu); | |
6affcbed | 6437 | return kvm_complete_insn_gp(vcpu, err); |
fee84b07 AK |
6438 | } |
6439 | ||
851ba692 | 6440 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
e5edaa01 | 6441 | { |
6affcbed | 6442 | return kvm_emulate_wbinvd(vcpu); |
e5edaa01 ED |
6443 | } |
6444 | ||
2acf923e DC |
6445 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
6446 | { | |
6447 | u64 new_bv = kvm_read_edx_eax(vcpu); | |
6448 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
6449 | ||
6450 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) | |
6affcbed | 6451 | return kvm_skip_emulated_instruction(vcpu); |
2acf923e DC |
6452 | return 1; |
6453 | } | |
6454 | ||
f53cd63c WL |
6455 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
6456 | { | |
6affcbed | 6457 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6458 | WARN(1, "this should never happen\n"); |
6459 | return 1; | |
6460 | } | |
6461 | ||
6462 | static int handle_xrstors(struct kvm_vcpu *vcpu) | |
6463 | { | |
6affcbed | 6464 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6465 | WARN(1, "this should never happen\n"); |
6466 | return 1; | |
6467 | } | |
6468 | ||
851ba692 | 6469 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
f78e0e2e | 6470 | { |
58fbbf26 KT |
6471 | if (likely(fasteoi)) { |
6472 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6473 | int access_type, offset; | |
6474 | ||
6475 | access_type = exit_qualification & APIC_ACCESS_TYPE; | |
6476 | offset = exit_qualification & APIC_ACCESS_OFFSET; | |
6477 | /* | |
6478 | * Sane guest uses MOV to write EOI, with written value | |
6479 | * not cared. So make a short-circuit here by avoiding | |
6480 | * heavy instruction emulation. | |
6481 | */ | |
6482 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && | |
6483 | (offset == APIC_EOI)) { | |
6484 | kvm_lapic_set_eoi(vcpu); | |
6affcbed | 6485 | return kvm_skip_emulated_instruction(vcpu); |
58fbbf26 KT |
6486 | } |
6487 | } | |
51d8b661 | 6488 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
f78e0e2e SY |
6489 | } |
6490 | ||
c7c9c56c YZ |
6491 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
6492 | { | |
6493 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6494 | int vector = exit_qualification & 0xff; | |
6495 | ||
6496 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ | |
6497 | kvm_apic_set_eoi_accelerated(vcpu, vector); | |
6498 | return 1; | |
6499 | } | |
6500 | ||
83d4c286 YZ |
6501 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
6502 | { | |
6503 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6504 | u32 offset = exit_qualification & 0xfff; | |
6505 | ||
6506 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ | |
6507 | kvm_apic_write_nodecode(vcpu, offset); | |
6508 | return 1; | |
6509 | } | |
6510 | ||
851ba692 | 6511 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
37817f29 | 6512 | { |
60637aac | 6513 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
37817f29 | 6514 | unsigned long exit_qualification; |
e269fb21 JK |
6515 | bool has_error_code = false; |
6516 | u32 error_code = 0; | |
37817f29 | 6517 | u16 tss_selector; |
7f3d35fd | 6518 | int reason, type, idt_v, idt_index; |
64a7ec06 GN |
6519 | |
6520 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); | |
7f3d35fd | 6521 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
64a7ec06 | 6522 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
37817f29 IE |
6523 | |
6524 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6525 | ||
6526 | reason = (u32)exit_qualification >> 30; | |
64a7ec06 GN |
6527 | if (reason == TASK_SWITCH_GATE && idt_v) { |
6528 | switch (type) { | |
6529 | case INTR_TYPE_NMI_INTR: | |
6530 | vcpu->arch.nmi_injected = false; | |
654f06fc | 6531 | vmx_set_nmi_mask(vcpu, true); |
64a7ec06 GN |
6532 | break; |
6533 | case INTR_TYPE_EXT_INTR: | |
66fd3f7f | 6534 | case INTR_TYPE_SOFT_INTR: |
64a7ec06 GN |
6535 | kvm_clear_interrupt_queue(vcpu); |
6536 | break; | |
6537 | case INTR_TYPE_HARD_EXCEPTION: | |
e269fb21 JK |
6538 | if (vmx->idt_vectoring_info & |
6539 | VECTORING_INFO_DELIVER_CODE_MASK) { | |
6540 | has_error_code = true; | |
6541 | error_code = | |
6542 | vmcs_read32(IDT_VECTORING_ERROR_CODE); | |
6543 | } | |
6544 | /* fall through */ | |
64a7ec06 GN |
6545 | case INTR_TYPE_SOFT_EXCEPTION: |
6546 | kvm_clear_exception_queue(vcpu); | |
6547 | break; | |
6548 | default: | |
6549 | break; | |
6550 | } | |
60637aac | 6551 | } |
37817f29 IE |
6552 | tss_selector = exit_qualification; |
6553 | ||
64a7ec06 GN |
6554 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
6555 | type != INTR_TYPE_EXT_INTR && | |
6556 | type != INTR_TYPE_NMI_INTR)) | |
6557 | skip_emulated_instruction(vcpu); | |
6558 | ||
7f3d35fd KW |
6559 | if (kvm_task_switch(vcpu, tss_selector, |
6560 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, | |
6561 | has_error_code, error_code) == EMULATE_FAIL) { | |
acb54517 GN |
6562 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
6563 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6564 | vcpu->run->internal.ndata = 0; | |
42dbaa5a | 6565 | return 0; |
acb54517 | 6566 | } |
42dbaa5a | 6567 | |
42dbaa5a JK |
6568 | /* |
6569 | * TODO: What about debug traps on tss switch? | |
6570 | * Are we supposed to inject them and update dr6? | |
6571 | */ | |
6572 | ||
6573 | return 1; | |
37817f29 IE |
6574 | } |
6575 | ||
851ba692 | 6576 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
1439442c | 6577 | { |
f9c617f6 | 6578 | unsigned long exit_qualification; |
1439442c | 6579 | gpa_t gpa; |
eebed243 | 6580 | u64 error_code; |
1439442c | 6581 | |
f9c617f6 | 6582 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
1439442c | 6583 | |
0be9c7a8 GN |
6584 | /* |
6585 | * EPT violation happened while executing iret from NMI, | |
6586 | * "blocked by NMI" bit has to be set before next VM entry. | |
6587 | * There are errata that may cause this bit to not be set: | |
6588 | * AAK134, BY25. | |
6589 | */ | |
bcd1c294 | 6590 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
d02fcf50 | 6591 | enable_vnmi && |
bcd1c294 | 6592 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
0be9c7a8 GN |
6593 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
6594 | ||
1439442c | 6595 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
229456fc | 6596 | trace_kvm_page_fault(gpa, exit_qualification); |
4f5982a5 | 6597 | |
27959a44 | 6598 | /* Is it a read fault? */ |
ab22a473 | 6599 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
27959a44 JS |
6600 | ? PFERR_USER_MASK : 0; |
6601 | /* Is it a write fault? */ | |
ab22a473 | 6602 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
27959a44 JS |
6603 | ? PFERR_WRITE_MASK : 0; |
6604 | /* Is it a fetch fault? */ | |
ab22a473 | 6605 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
27959a44 JS |
6606 | ? PFERR_FETCH_MASK : 0; |
6607 | /* ept page table entry is present? */ | |
6608 | error_code |= (exit_qualification & | |
6609 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | | |
6610 | EPT_VIOLATION_EXECUTABLE)) | |
6611 | ? PFERR_PRESENT_MASK : 0; | |
4f5982a5 | 6612 | |
eebed243 PB |
6613 | error_code |= (exit_qualification & 0x100) != 0 ? |
6614 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; | |
25d92081 | 6615 | |
25d92081 | 6616 | vcpu->arch.exit_qualification = exit_qualification; |
4f5982a5 | 6617 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
1439442c SY |
6618 | } |
6619 | ||
851ba692 | 6620 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
68f89400 | 6621 | { |
f735d4af | 6622 | int ret; |
68f89400 MT |
6623 | gpa_t gpa; |
6624 | ||
9034e6e8 PB |
6625 | /* |
6626 | * A nested guest cannot optimize MMIO vmexits, because we have an | |
6627 | * nGPA here instead of the required GPA. | |
6628 | */ | |
68f89400 | 6629 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
9034e6e8 PB |
6630 | if (!is_guest_mode(vcpu) && |
6631 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { | |
931c33b1 | 6632 | trace_kvm_fast_mmio(gpa); |
6affcbed | 6633 | return kvm_skip_emulated_instruction(vcpu); |
68c3b4d1 | 6634 | } |
68f89400 | 6635 | |
e08d26f0 PB |
6636 | ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
6637 | if (ret >= 0) | |
6638 | return ret; | |
ce88decf XG |
6639 | |
6640 | /* It is the real ept misconfig */ | |
f735d4af | 6641 | WARN_ON(1); |
68f89400 | 6642 | |
851ba692 AK |
6643 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
6644 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; | |
68f89400 MT |
6645 | |
6646 | return 0; | |
6647 | } | |
6648 | ||
851ba692 | 6649 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
f08864b4 | 6650 | { |
d02fcf50 | 6651 | WARN_ON_ONCE(!enable_vnmi); |
47c0152e PB |
6652 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6653 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
f08864b4 | 6654 | ++vcpu->stat.nmi_window_exits; |
3842d135 | 6655 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
f08864b4 SY |
6656 | |
6657 | return 1; | |
6658 | } | |
6659 | ||
80ced186 | 6660 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
ea953ef0 | 6661 | { |
8b3079a5 AK |
6662 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6663 | enum emulation_result err = EMULATE_DONE; | |
80ced186 | 6664 | int ret = 1; |
49e9d557 AK |
6665 | u32 cpu_exec_ctrl; |
6666 | bool intr_window_requested; | |
b8405c18 | 6667 | unsigned count = 130; |
49e9d557 AK |
6668 | |
6669 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); | |
6670 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; | |
ea953ef0 | 6671 | |
98eb2f8b | 6672 | while (vmx->emulation_required && count-- != 0) { |
bdea48e3 | 6673 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
49e9d557 AK |
6674 | return handle_interrupt_window(&vmx->vcpu); |
6675 | ||
72875d8a | 6676 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
de87dcdd AK |
6677 | return 1; |
6678 | ||
9b8ae637 | 6679 | err = emulate_instruction(vcpu, 0); |
ea953ef0 | 6680 | |
ac0a48c3 | 6681 | if (err == EMULATE_USER_EXIT) { |
94452b9e | 6682 | ++vcpu->stat.mmio_exits; |
80ced186 MG |
6683 | ret = 0; |
6684 | goto out; | |
6685 | } | |
1d5a4d9b | 6686 | |
de5f70e0 AK |
6687 | if (err != EMULATE_DONE) { |
6688 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
6689 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6690 | vcpu->run->internal.ndata = 0; | |
6d77dbfc | 6691 | return 0; |
de5f70e0 | 6692 | } |
ea953ef0 | 6693 | |
8d76c49e GN |
6694 | if (vcpu->arch.halt_request) { |
6695 | vcpu->arch.halt_request = 0; | |
5cb56059 | 6696 | ret = kvm_vcpu_halt(vcpu); |
8d76c49e GN |
6697 | goto out; |
6698 | } | |
6699 | ||
ea953ef0 | 6700 | if (signal_pending(current)) |
80ced186 | 6701 | goto out; |
ea953ef0 MG |
6702 | if (need_resched()) |
6703 | schedule(); | |
6704 | } | |
6705 | ||
80ced186 MG |
6706 | out: |
6707 | return ret; | |
ea953ef0 MG |
6708 | } |
6709 | ||
b4a2d31d RK |
6710 | static int __grow_ple_window(int val) |
6711 | { | |
6712 | if (ple_window_grow < 1) | |
6713 | return ple_window; | |
6714 | ||
6715 | val = min(val, ple_window_actual_max); | |
6716 | ||
6717 | if (ple_window_grow < ple_window) | |
6718 | val *= ple_window_grow; | |
6719 | else | |
6720 | val += ple_window_grow; | |
6721 | ||
6722 | return val; | |
6723 | } | |
6724 | ||
6725 | static int __shrink_ple_window(int val, int modifier, int minimum) | |
6726 | { | |
6727 | if (modifier < 1) | |
6728 | return ple_window; | |
6729 | ||
6730 | if (modifier < ple_window) | |
6731 | val /= modifier; | |
6732 | else | |
6733 | val -= modifier; | |
6734 | ||
6735 | return max(val, minimum); | |
6736 | } | |
6737 | ||
6738 | static void grow_ple_window(struct kvm_vcpu *vcpu) | |
6739 | { | |
6740 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6741 | int old = vmx->ple_window; | |
6742 | ||
6743 | vmx->ple_window = __grow_ple_window(old); | |
6744 | ||
6745 | if (vmx->ple_window != old) | |
6746 | vmx->ple_window_dirty = true; | |
7b46268d RK |
6747 | |
6748 | trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
6749 | } |
6750 | ||
6751 | static void shrink_ple_window(struct kvm_vcpu *vcpu) | |
6752 | { | |
6753 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6754 | int old = vmx->ple_window; | |
6755 | ||
6756 | vmx->ple_window = __shrink_ple_window(old, | |
6757 | ple_window_shrink, ple_window); | |
6758 | ||
6759 | if (vmx->ple_window != old) | |
6760 | vmx->ple_window_dirty = true; | |
7b46268d RK |
6761 | |
6762 | trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
6763 | } |
6764 | ||
6765 | /* | |
6766 | * ple_window_actual_max is computed to be one grow_ple_window() below | |
6767 | * ple_window_max. (See __grow_ple_window for the reason.) | |
6768 | * This prevents overflows, because ple_window_max is int. | |
6769 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in | |
6770 | * this process. | |
6771 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. | |
6772 | */ | |
6773 | static void update_ple_window_actual_max(void) | |
6774 | { | |
6775 | ple_window_actual_max = | |
6776 | __shrink_ple_window(max(ple_window_max, ple_window), | |
6777 | ple_window_grow, INT_MIN); | |
6778 | } | |
6779 | ||
bf9f6ac8 FW |
6780 | /* |
6781 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. | |
6782 | */ | |
6783 | static void wakeup_handler(void) | |
6784 | { | |
6785 | struct kvm_vcpu *vcpu; | |
6786 | int cpu = smp_processor_id(); | |
6787 | ||
6788 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
6789 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), | |
6790 | blocked_vcpu_list) { | |
6791 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
6792 | ||
6793 | if (pi_test_on(pi_desc) == 1) | |
6794 | kvm_vcpu_kick(vcpu); | |
6795 | } | |
6796 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
6797 | } | |
6798 | ||
f160c7b7 JS |
6799 | void vmx_enable_tdp(void) |
6800 | { | |
6801 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, | |
6802 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, | |
6803 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, | |
6804 | 0ull, VMX_EPT_EXECUTABLE_MASK, | |
6805 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, | |
d0ec49d4 | 6806 | VMX_EPT_RWX_MASK, 0ull); |
f160c7b7 JS |
6807 | |
6808 | ept_set_mmio_spte_mask(); | |
6809 | kvm_enable_tdp(); | |
6810 | } | |
6811 | ||
f2c7648d TC |
6812 | static __init int hardware_setup(void) |
6813 | { | |
34a1cd60 TC |
6814 | int r = -ENOMEM, i, msr; |
6815 | ||
6816 | rdmsrl_safe(MSR_EFER, &host_efer); | |
6817 | ||
6818 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) | |
6819 | kvm_define_shared_msr(i, vmx_msr_index[i]); | |
6820 | ||
23611332 RK |
6821 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
6822 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); | |
6823 | if (!vmx_bitmap[i]) | |
6824 | goto out; | |
6825 | } | |
34a1cd60 | 6826 | |
34a1cd60 TC |
6827 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
6828 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); | |
6829 | ||
34a1cd60 TC |
6830 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
6831 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); | |
6832 | ||
34a1cd60 TC |
6833 | if (setup_vmcs_config(&vmcs_config) < 0) { |
6834 | r = -EIO; | |
23611332 | 6835 | goto out; |
baa03522 | 6836 | } |
f2c7648d TC |
6837 | |
6838 | if (boot_cpu_has(X86_FEATURE_NX)) | |
6839 | kvm_enable_efer_bits(EFER_NX); | |
6840 | ||
08d839c4 WL |
6841 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
6842 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) | |
f2c7648d | 6843 | enable_vpid = 0; |
08d839c4 | 6844 | |
f2c7648d | 6845 | if (!cpu_has_vmx_ept() || |
42aa53b4 | 6846 | !cpu_has_vmx_ept_4levels() || |
f5f51586 | 6847 | !cpu_has_vmx_ept_mt_wb() || |
8ad8182e | 6848 | !cpu_has_vmx_invept_global()) |
f2c7648d | 6849 | enable_ept = 0; |
f2c7648d | 6850 | |
fce6ac4c | 6851 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
f2c7648d TC |
6852 | enable_ept_ad_bits = 0; |
6853 | ||
8ad8182e | 6854 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
f2c7648d TC |
6855 | enable_unrestricted_guest = 0; |
6856 | ||
ad15a296 | 6857 | if (!cpu_has_vmx_flexpriority()) |
f2c7648d TC |
6858 | flexpriority_enabled = 0; |
6859 | ||
d02fcf50 PB |
6860 | if (!cpu_has_virtual_nmis()) |
6861 | enable_vnmi = 0; | |
6862 | ||
ad15a296 PB |
6863 | /* |
6864 | * set_apic_access_page_addr() is used to reload apic access | |
6865 | * page upon invalidation. No need to do anything if not | |
6866 | * using the APIC_ACCESS_ADDR VMCS field. | |
6867 | */ | |
6868 | if (!flexpriority_enabled) | |
f2c7648d | 6869 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
f2c7648d TC |
6870 | |
6871 | if (!cpu_has_vmx_tpr_shadow()) | |
6872 | kvm_x86_ops->update_cr8_intercept = NULL; | |
6873 | ||
6874 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) | |
6875 | kvm_disable_largepages(); | |
6876 | ||
0f107682 | 6877 | if (!cpu_has_vmx_ple()) { |
f2c7648d | 6878 | ple_gap = 0; |
0f107682 WL |
6879 | ple_window = 0; |
6880 | ple_window_grow = 0; | |
6881 | ple_window_max = 0; | |
6882 | ple_window_shrink = 0; | |
6883 | } | |
f2c7648d | 6884 | |
76dfafd5 | 6885 | if (!cpu_has_vmx_apicv()) { |
f2c7648d | 6886 | enable_apicv = 0; |
76dfafd5 PB |
6887 | kvm_x86_ops->sync_pir_to_irr = NULL; |
6888 | } | |
f2c7648d | 6889 | |
64903d61 HZ |
6890 | if (cpu_has_vmx_tsc_scaling()) { |
6891 | kvm_has_tsc_control = true; | |
6892 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; | |
6893 | kvm_tsc_scaling_ratio_frac_bits = 48; | |
6894 | } | |
6895 | ||
baa03522 TC |
6896 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
6897 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); | |
6898 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); | |
6899 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); | |
6900 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); | |
6901 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); | |
baa03522 | 6902 | |
c63e4563 | 6903 | memcpy(vmx_msr_bitmap_legacy_x2apic_apicv, |
baa03522 | 6904 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
c63e4563 | 6905 | memcpy(vmx_msr_bitmap_longmode_x2apic_apicv, |
baa03522 | 6906 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
c63e4563 | 6907 | memcpy(vmx_msr_bitmap_legacy_x2apic, |
f6e90f9e | 6908 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
c63e4563 | 6909 | memcpy(vmx_msr_bitmap_longmode_x2apic, |
f6e90f9e | 6910 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
baa03522 | 6911 | |
04bb92e4 WL |
6912 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
6913 | ||
40d8338d RK |
6914 | for (msr = 0x800; msr <= 0x8ff; msr++) { |
6915 | if (msr == 0x839 /* TMCCT */) | |
6916 | continue; | |
2e69f865 | 6917 | vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true); |
40d8338d | 6918 | } |
3ce424e4 | 6919 | |
f6e90f9e | 6920 | /* |
2e69f865 RK |
6921 | * TPR reads and writes can be virtualized even if virtual interrupt |
6922 | * delivery is not in use. | |
f6e90f9e | 6923 | */ |
2e69f865 RK |
6924 | vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true); |
6925 | vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false); | |
3ce424e4 | 6926 | |
3ce424e4 | 6927 | /* EOI */ |
2e69f865 | 6928 | vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true); |
3ce424e4 | 6929 | /* SELF-IPI */ |
2e69f865 | 6930 | vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true); |
baa03522 | 6931 | |
f160c7b7 JS |
6932 | if (enable_ept) |
6933 | vmx_enable_tdp(); | |
6934 | else | |
baa03522 TC |
6935 | kvm_disable_tdp(); |
6936 | ||
6937 | update_ple_window_actual_max(); | |
6938 | ||
843e4330 KH |
6939 | /* |
6940 | * Only enable PML when hardware supports PML feature, and both EPT | |
6941 | * and EPT A/D bit features are enabled -- PML depends on them to work. | |
6942 | */ | |
6943 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) | |
6944 | enable_pml = 0; | |
6945 | ||
6946 | if (!enable_pml) { | |
6947 | kvm_x86_ops->slot_enable_log_dirty = NULL; | |
6948 | kvm_x86_ops->slot_disable_log_dirty = NULL; | |
6949 | kvm_x86_ops->flush_log_dirty = NULL; | |
6950 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; | |
6951 | } | |
6952 | ||
64672c95 YJ |
6953 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
6954 | u64 vmx_msr; | |
6955 | ||
6956 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
6957 | cpu_preemption_timer_multi = | |
6958 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; | |
6959 | } else { | |
6960 | kvm_x86_ops->set_hv_timer = NULL; | |
6961 | kvm_x86_ops->cancel_hv_timer = NULL; | |
6962 | } | |
6963 | ||
c5d167b2 PB |
6964 | if (!cpu_has_vmx_shadow_vmcs()) |
6965 | enable_shadow_vmcs = 0; | |
6966 | if (enable_shadow_vmcs) | |
6967 | init_vmcs_shadow_fields(); | |
6968 | ||
bf9f6ac8 FW |
6969 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
6970 | ||
c45dcc71 AR |
6971 | kvm_mce_cap_supported |= MCG_LMCE_P; |
6972 | ||
f2c7648d | 6973 | return alloc_kvm_area(); |
34a1cd60 | 6974 | |
34a1cd60 | 6975 | out: |
23611332 RK |
6976 | for (i = 0; i < VMX_BITMAP_NR; i++) |
6977 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 TC |
6978 | |
6979 | return r; | |
f2c7648d TC |
6980 | } |
6981 | ||
6982 | static __exit void hardware_unsetup(void) | |
6983 | { | |
23611332 RK |
6984 | int i; |
6985 | ||
6986 | for (i = 0; i < VMX_BITMAP_NR; i++) | |
6987 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 | 6988 | |
f2c7648d TC |
6989 | free_kvm_area(); |
6990 | } | |
6991 | ||
4b8d54f9 ZE |
6992 | /* |
6993 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE | |
6994 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. | |
6995 | */ | |
9fb41ba8 | 6996 | static int handle_pause(struct kvm_vcpu *vcpu) |
4b8d54f9 | 6997 | { |
b4a2d31d RK |
6998 | if (ple_gap) |
6999 | grow_ple_window(vcpu); | |
7000 | ||
de63ad4c LM |
7001 | /* |
7002 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" | |
7003 | * VM-execution control is ignored if CPL > 0. OTOH, KVM | |
7004 | * never set PAUSE_EXITING and just set PLE if supported, | |
7005 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. | |
7006 | */ | |
7007 | kvm_vcpu_on_spin(vcpu, true); | |
6affcbed | 7008 | return kvm_skip_emulated_instruction(vcpu); |
4b8d54f9 ZE |
7009 | } |
7010 | ||
87c00572 | 7011 | static int handle_nop(struct kvm_vcpu *vcpu) |
59708670 | 7012 | { |
6affcbed | 7013 | return kvm_skip_emulated_instruction(vcpu); |
59708670 SY |
7014 | } |
7015 | ||
87c00572 GS |
7016 | static int handle_mwait(struct kvm_vcpu *vcpu) |
7017 | { | |
7018 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); | |
7019 | return handle_nop(vcpu); | |
7020 | } | |
7021 | ||
45ec368c JM |
7022 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
7023 | { | |
7024 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7025 | return 1; | |
7026 | } | |
7027 | ||
5f3d45e7 MD |
7028 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
7029 | { | |
7030 | return 1; | |
7031 | } | |
7032 | ||
87c00572 GS |
7033 | static int handle_monitor(struct kvm_vcpu *vcpu) |
7034 | { | |
7035 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); | |
7036 | return handle_nop(vcpu); | |
7037 | } | |
7038 | ||
0658fbaa ACL |
7039 | /* |
7040 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), | |
7041 | * set the success or error code of an emulated VMX instruction, as specified | |
7042 | * by Vol 2B, VMX Instruction Reference, "Conventions". | |
7043 | */ | |
7044 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) | |
7045 | { | |
7046 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) | |
7047 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
7048 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); | |
7049 | } | |
7050 | ||
7051 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) | |
7052 | { | |
7053 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
7054 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | | |
7055 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
7056 | | X86_EFLAGS_CF); | |
7057 | } | |
7058 | ||
145c28dd | 7059 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
0658fbaa ACL |
7060 | u32 vm_instruction_error) |
7061 | { | |
7062 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { | |
7063 | /* | |
7064 | * failValid writes the error number to the current VMCS, which | |
7065 | * can't be done there isn't a current VMCS. | |
7066 | */ | |
7067 | nested_vmx_failInvalid(vcpu); | |
7068 | return; | |
7069 | } | |
7070 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
7071 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
7072 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
7073 | | X86_EFLAGS_ZF); | |
7074 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; | |
7075 | /* | |
7076 | * We don't need to force a shadow sync because | |
7077 | * VM_INSTRUCTION_ERROR is not shadowed | |
7078 | */ | |
7079 | } | |
145c28dd | 7080 | |
ff651cb6 WV |
7081 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
7082 | { | |
7083 | /* TODO: not to reset guest simply here. */ | |
7084 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); | |
bbe41b95 | 7085 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
ff651cb6 WV |
7086 | } |
7087 | ||
f4124500 JK |
7088 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
7089 | { | |
7090 | struct vcpu_vmx *vmx = | |
7091 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); | |
7092 | ||
7093 | vmx->nested.preemption_timer_expired = true; | |
7094 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); | |
7095 | kvm_vcpu_kick(&vmx->vcpu); | |
7096 | ||
7097 | return HRTIMER_NORESTART; | |
7098 | } | |
7099 | ||
19677e32 BD |
7100 | /* |
7101 | * Decode the memory-address operand of a vmx instruction, as recorded on an | |
7102 | * exit caused by such an instruction (run by a guest hypervisor). | |
7103 | * On success, returns 0. When the operand is invalid, returns 1 and throws | |
7104 | * #UD or #GP. | |
7105 | */ | |
7106 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, | |
7107 | unsigned long exit_qualification, | |
f9eb4af6 | 7108 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
19677e32 | 7109 | { |
f9eb4af6 EK |
7110 | gva_t off; |
7111 | bool exn; | |
7112 | struct kvm_segment s; | |
7113 | ||
19677e32 BD |
7114 | /* |
7115 | * According to Vol. 3B, "Information for VM Exits Due to Instruction | |
7116 | * Execution", on an exit, vmx_instruction_info holds most of the | |
7117 | * addressing components of the operand. Only the displacement part | |
7118 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). | |
7119 | * For how an actual address is calculated from all these components, | |
7120 | * refer to Vol. 1, "Operand Addressing". | |
7121 | */ | |
7122 | int scaling = vmx_instruction_info & 3; | |
7123 | int addr_size = (vmx_instruction_info >> 7) & 7; | |
7124 | bool is_reg = vmx_instruction_info & (1u << 10); | |
7125 | int seg_reg = (vmx_instruction_info >> 15) & 7; | |
7126 | int index_reg = (vmx_instruction_info >> 18) & 0xf; | |
7127 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); | |
7128 | int base_reg = (vmx_instruction_info >> 23) & 0xf; | |
7129 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); | |
7130 | ||
7131 | if (is_reg) { | |
7132 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7133 | return 1; | |
7134 | } | |
7135 | ||
7136 | /* Addr = segment_base + offset */ | |
7137 | /* offset = base + [index * scale] + displacement */ | |
f9eb4af6 | 7138 | off = exit_qualification; /* holds the displacement */ |
19677e32 | 7139 | if (base_is_valid) |
f9eb4af6 | 7140 | off += kvm_register_read(vcpu, base_reg); |
19677e32 | 7141 | if (index_is_valid) |
f9eb4af6 EK |
7142 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
7143 | vmx_get_segment(vcpu, &s, seg_reg); | |
7144 | *ret = s.base + off; | |
19677e32 BD |
7145 | |
7146 | if (addr_size == 1) /* 32 bit */ | |
7147 | *ret &= 0xffffffff; | |
7148 | ||
f9eb4af6 EK |
7149 | /* Checks for #GP/#SS exceptions. */ |
7150 | exn = false; | |
ff30ef40 QC |
7151 | if (is_long_mode(vcpu)) { |
7152 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a | |
7153 | * non-canonical form. This is the only check on the memory | |
7154 | * destination for long mode! | |
7155 | */ | |
fd8cb433 | 7156 | exn = is_noncanonical_address(*ret, vcpu); |
ff30ef40 | 7157 | } else if (is_protmode(vcpu)) { |
f9eb4af6 EK |
7158 | /* Protected mode: apply checks for segment validity in the |
7159 | * following order: | |
7160 | * - segment type check (#GP(0) may be thrown) | |
7161 | * - usability check (#GP(0)/#SS(0)) | |
7162 | * - limit check (#GP(0)/#SS(0)) | |
7163 | */ | |
7164 | if (wr) | |
7165 | /* #GP(0) if the destination operand is located in a | |
7166 | * read-only data segment or any code segment. | |
7167 | */ | |
7168 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); | |
7169 | else | |
7170 | /* #GP(0) if the source operand is located in an | |
7171 | * execute-only code segment | |
7172 | */ | |
7173 | exn = ((s.type & 0xa) == 8); | |
ff30ef40 QC |
7174 | if (exn) { |
7175 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
7176 | return 1; | |
7177 | } | |
f9eb4af6 EK |
7178 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
7179 | */ | |
7180 | exn = (s.unusable != 0); | |
7181 | /* Protected mode: #GP(0)/#SS(0) if the memory | |
7182 | * operand is outside the segment limit. | |
7183 | */ | |
7184 | exn = exn || (off + sizeof(u64) > s.limit); | |
7185 | } | |
7186 | if (exn) { | |
7187 | kvm_queue_exception_e(vcpu, | |
7188 | seg_reg == VCPU_SREG_SS ? | |
7189 | SS_VECTOR : GP_VECTOR, | |
7190 | 0); | |
7191 | return 1; | |
7192 | } | |
7193 | ||
19677e32 BD |
7194 | return 0; |
7195 | } | |
7196 | ||
cbf71279 | 7197 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer) |
3573e22c BD |
7198 | { |
7199 | gva_t gva; | |
3573e22c | 7200 | struct x86_exception e; |
3573e22c BD |
7201 | |
7202 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 7203 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
3573e22c BD |
7204 | return 1; |
7205 | ||
cbf71279 RK |
7206 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer, |
7207 | sizeof(*vmpointer), &e)) { | |
3573e22c BD |
7208 | kvm_inject_page_fault(vcpu, &e); |
7209 | return 1; | |
7210 | } | |
7211 | ||
3573e22c BD |
7212 | return 0; |
7213 | } | |
7214 | ||
e29acc55 JM |
7215 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
7216 | { | |
7217 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7218 | struct vmcs *shadow_vmcs; | |
7219 | ||
00647b44 JM |
7220 | vmx->nested.vmcs02.vmcs = alloc_vmcs(); |
7221 | vmx->nested.vmcs02.shadow_vmcs = NULL; | |
7222 | if (!vmx->nested.vmcs02.vmcs) | |
7223 | goto out_vmcs02; | |
7224 | loaded_vmcs_init(&vmx->nested.vmcs02); | |
7225 | ||
e29acc55 JM |
7226 | if (cpu_has_vmx_msr_bitmap()) { |
7227 | vmx->nested.msr_bitmap = | |
7228 | (unsigned long *)__get_free_page(GFP_KERNEL); | |
7229 | if (!vmx->nested.msr_bitmap) | |
7230 | goto out_msr_bitmap; | |
7231 | } | |
7232 | ||
7233 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); | |
7234 | if (!vmx->nested.cached_vmcs12) | |
7235 | goto out_cached_vmcs12; | |
7236 | ||
7237 | if (enable_shadow_vmcs) { | |
7238 | shadow_vmcs = alloc_vmcs(); | |
7239 | if (!shadow_vmcs) | |
7240 | goto out_shadow_vmcs; | |
7241 | /* mark vmcs as shadow */ | |
7242 | shadow_vmcs->revision_id |= (1u << 31); | |
7243 | /* init shadow vmcs */ | |
7244 | vmcs_clear(shadow_vmcs); | |
7245 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; | |
7246 | } | |
7247 | ||
e29acc55 JM |
7248 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
7249 | HRTIMER_MODE_REL_PINNED); | |
7250 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; | |
7251 | ||
7252 | vmx->nested.vmxon = true; | |
7253 | return 0; | |
7254 | ||
7255 | out_shadow_vmcs: | |
7256 | kfree(vmx->nested.cached_vmcs12); | |
7257 | ||
7258 | out_cached_vmcs12: | |
7259 | free_page((unsigned long)vmx->nested.msr_bitmap); | |
7260 | ||
7261 | out_msr_bitmap: | |
276c796c | 7262 | vmx_nested_free_vmcs02(vmx); |
00647b44 JM |
7263 | |
7264 | out_vmcs02: | |
e29acc55 JM |
7265 | return -ENOMEM; |
7266 | } | |
7267 | ||
ec378aee NHE |
7268 | /* |
7269 | * Emulate the VMXON instruction. | |
7270 | * Currently, we just remember that VMX is active, and do not save or even | |
7271 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we | |
7272 | * do not currently need to store anything in that guest-allocated memory | |
7273 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their | |
7274 | * argument is different from the VMXON pointer (which the spec says they do). | |
7275 | */ | |
7276 | static int handle_vmon(struct kvm_vcpu *vcpu) | |
7277 | { | |
e29acc55 | 7278 | int ret; |
cbf71279 RK |
7279 | gpa_t vmptr; |
7280 | struct page *page; | |
ec378aee | 7281 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b3897a49 NHE |
7282 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
7283 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
ec378aee | 7284 | |
70f3aac9 JM |
7285 | /* |
7286 | * The Intel VMX Instruction Reference lists a bunch of bits that are | |
7287 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to | |
7288 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). | |
7289 | * Otherwise, we should fail with #UD. But most faulting conditions | |
7290 | * have already been checked by hardware, prior to the VM-exit for | |
7291 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has | |
7292 | * that bit set to 1 in non-root mode. | |
ec378aee | 7293 | */ |
70f3aac9 | 7294 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
ec378aee NHE |
7295 | kvm_queue_exception(vcpu, UD_VECTOR); |
7296 | return 1; | |
7297 | } | |
7298 | ||
145c28dd AG |
7299 | if (vmx->nested.vmxon) { |
7300 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); | |
6affcbed | 7301 | return kvm_skip_emulated_instruction(vcpu); |
145c28dd | 7302 | } |
b3897a49 | 7303 | |
3b84080b | 7304 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
b3897a49 NHE |
7305 | != VMXON_NEEDED_FEATURES) { |
7306 | kvm_inject_gp(vcpu, 0); | |
7307 | return 1; | |
7308 | } | |
7309 | ||
cbf71279 | 7310 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
21e7fbe7 | 7311 | return 1; |
cbf71279 RK |
7312 | |
7313 | /* | |
7314 | * SDM 3: 24.11.5 | |
7315 | * The first 4 bytes of VMXON region contain the supported | |
7316 | * VMCS revision identifier | |
7317 | * | |
7318 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; | |
7319 | * which replaces physical address width with 32 | |
7320 | */ | |
7321 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { | |
7322 | nested_vmx_failInvalid(vcpu); | |
7323 | return kvm_skip_emulated_instruction(vcpu); | |
7324 | } | |
7325 | ||
5e2f30b7 DH |
7326 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
7327 | if (is_error_page(page)) { | |
cbf71279 RK |
7328 | nested_vmx_failInvalid(vcpu); |
7329 | return kvm_skip_emulated_instruction(vcpu); | |
7330 | } | |
7331 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { | |
7332 | kunmap(page); | |
53a70daf | 7333 | kvm_release_page_clean(page); |
cbf71279 RK |
7334 | nested_vmx_failInvalid(vcpu); |
7335 | return kvm_skip_emulated_instruction(vcpu); | |
7336 | } | |
7337 | kunmap(page); | |
53a70daf | 7338 | kvm_release_page_clean(page); |
cbf71279 RK |
7339 | |
7340 | vmx->nested.vmxon_ptr = vmptr; | |
e29acc55 JM |
7341 | ret = enter_vmx_operation(vcpu); |
7342 | if (ret) | |
7343 | return ret; | |
ec378aee | 7344 | |
a25eb114 | 7345 | nested_vmx_succeed(vcpu); |
6affcbed | 7346 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7347 | } |
7348 | ||
7349 | /* | |
7350 | * Intel's VMX Instruction Reference specifies a common set of prerequisites | |
7351 | * for running VMX instructions (except VMXON, whose prerequisites are | |
7352 | * slightly different). It also specifies what exception to inject otherwise. | |
70f3aac9 JM |
7353 | * Note that many of these exceptions have priority over VM exits, so they |
7354 | * don't have to be checked again here. | |
ec378aee NHE |
7355 | */ |
7356 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) | |
7357 | { | |
70f3aac9 | 7358 | if (!to_vmx(vcpu)->nested.vmxon) { |
ec378aee NHE |
7359 | kvm_queue_exception(vcpu, UD_VECTOR); |
7360 | return 0; | |
7361 | } | |
ec378aee NHE |
7362 | return 1; |
7363 | } | |
7364 | ||
8ca44e88 DM |
7365 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
7366 | { | |
7367 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); | |
7368 | vmcs_write64(VMCS_LINK_POINTER, -1ull); | |
7369 | } | |
7370 | ||
e7953d7f AG |
7371 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
7372 | { | |
9a2a05b9 PB |
7373 | if (vmx->nested.current_vmptr == -1ull) |
7374 | return; | |
7375 | ||
012f83cb | 7376 | if (enable_shadow_vmcs) { |
9a2a05b9 PB |
7377 | /* copy to memory all shadowed fields in case |
7378 | they were modified */ | |
7379 | copy_shadow_to_vmcs12(vmx); | |
7380 | vmx->nested.sync_shadow_vmcs = false; | |
8ca44e88 | 7381 | vmx_disable_shadow_vmcs(vmx); |
012f83cb | 7382 | } |
705699a1 | 7383 | vmx->nested.posted_intr_nv = -1; |
4f2777bc DM |
7384 | |
7385 | /* Flush VMCS12 to guest memory */ | |
9f744c59 PB |
7386 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
7387 | vmx->nested.current_vmptr >> PAGE_SHIFT, | |
7388 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); | |
4f2777bc | 7389 | |
9a2a05b9 | 7390 | vmx->nested.current_vmptr = -1ull; |
e7953d7f AG |
7391 | } |
7392 | ||
ec378aee NHE |
7393 | /* |
7394 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or | |
7395 | * just stops using VMX. | |
7396 | */ | |
7397 | static void free_nested(struct vcpu_vmx *vmx) | |
7398 | { | |
b7455825 | 7399 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
ec378aee | 7400 | return; |
9a2a05b9 | 7401 | |
ec378aee | 7402 | vmx->nested.vmxon = false; |
b7455825 | 7403 | vmx->nested.smm.vmxon = false; |
5c614b35 | 7404 | free_vpid(vmx->nested.vpid02); |
8ca44e88 DM |
7405 | vmx->nested.posted_intr_nv = -1; |
7406 | vmx->nested.current_vmptr = -1ull; | |
d048c098 RK |
7407 | if (vmx->nested.msr_bitmap) { |
7408 | free_page((unsigned long)vmx->nested.msr_bitmap); | |
7409 | vmx->nested.msr_bitmap = NULL; | |
7410 | } | |
355f4fb1 | 7411 | if (enable_shadow_vmcs) { |
8ca44e88 | 7412 | vmx_disable_shadow_vmcs(vmx); |
355f4fb1 JM |
7413 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
7414 | free_vmcs(vmx->vmcs01.shadow_vmcs); | |
7415 | vmx->vmcs01.shadow_vmcs = NULL; | |
7416 | } | |
4f2777bc | 7417 | kfree(vmx->nested.cached_vmcs12); |
00647b44 | 7418 | /* Unpin physical memory we referred to in the vmcs02 */ |
fe3ef05c | 7419 | if (vmx->nested.apic_access_page) { |
53a70daf | 7420 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
48d89b92 | 7421 | vmx->nested.apic_access_page = NULL; |
fe3ef05c | 7422 | } |
a7c0b07d | 7423 | if (vmx->nested.virtual_apic_page) { |
53a70daf | 7424 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
48d89b92 | 7425 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 7426 | } |
705699a1 WV |
7427 | if (vmx->nested.pi_desc_page) { |
7428 | kunmap(vmx->nested.pi_desc_page); | |
53a70daf | 7429 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
705699a1 WV |
7430 | vmx->nested.pi_desc_page = NULL; |
7431 | vmx->nested.pi_desc = NULL; | |
7432 | } | |
ff2f6fe9 | 7433 | |
276c796c | 7434 | vmx_nested_free_vmcs02(vmx); |
ec378aee NHE |
7435 | } |
7436 | ||
7437 | /* Emulate the VMXOFF instruction */ | |
7438 | static int handle_vmoff(struct kvm_vcpu *vcpu) | |
7439 | { | |
7440 | if (!nested_vmx_check_permission(vcpu)) | |
7441 | return 1; | |
7442 | free_nested(to_vmx(vcpu)); | |
a25eb114 | 7443 | nested_vmx_succeed(vcpu); |
6affcbed | 7444 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7445 | } |
7446 | ||
27d6c865 NHE |
7447 | /* Emulate the VMCLEAR instruction */ |
7448 | static int handle_vmclear(struct kvm_vcpu *vcpu) | |
7449 | { | |
7450 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
587d7e72 | 7451 | u32 zero = 0; |
27d6c865 | 7452 | gpa_t vmptr; |
27d6c865 NHE |
7453 | |
7454 | if (!nested_vmx_check_permission(vcpu)) | |
7455 | return 1; | |
7456 | ||
cbf71279 | 7457 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
27d6c865 | 7458 | return 1; |
27d6c865 | 7459 | |
cbf71279 RK |
7460 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
7461 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); | |
7462 | return kvm_skip_emulated_instruction(vcpu); | |
7463 | } | |
7464 | ||
7465 | if (vmptr == vmx->nested.vmxon_ptr) { | |
7466 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); | |
7467 | return kvm_skip_emulated_instruction(vcpu); | |
7468 | } | |
7469 | ||
9a2a05b9 | 7470 | if (vmptr == vmx->nested.current_vmptr) |
e7953d7f | 7471 | nested_release_vmcs12(vmx); |
27d6c865 | 7472 | |
587d7e72 JM |
7473 | kvm_vcpu_write_guest(vcpu, |
7474 | vmptr + offsetof(struct vmcs12, launch_state), | |
7475 | &zero, sizeof(zero)); | |
27d6c865 | 7476 | |
27d6c865 | 7477 | nested_vmx_succeed(vcpu); |
6affcbed | 7478 | return kvm_skip_emulated_instruction(vcpu); |
27d6c865 NHE |
7479 | } |
7480 | ||
cd232ad0 NHE |
7481 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
7482 | ||
7483 | /* Emulate the VMLAUNCH instruction */ | |
7484 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) | |
7485 | { | |
7486 | return nested_vmx_run(vcpu, true); | |
7487 | } | |
7488 | ||
7489 | /* Emulate the VMRESUME instruction */ | |
7490 | static int handle_vmresume(struct kvm_vcpu *vcpu) | |
7491 | { | |
7492 | ||
7493 | return nested_vmx_run(vcpu, false); | |
7494 | } | |
7495 | ||
49f705c5 NHE |
7496 | /* |
7497 | * Read a vmcs12 field. Since these can have varying lengths and we return | |
7498 | * one type, we chose the biggest type (u64) and zero-extend the return value | |
7499 | * to that size. Note that the caller, handle_vmread, might need to use only | |
7500 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of | |
7501 | * 64-bit fields are to be returned). | |
7502 | */ | |
a2ae9df7 PB |
7503 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
7504 | unsigned long field, u64 *ret) | |
49f705c5 NHE |
7505 | { |
7506 | short offset = vmcs_field_to_offset(field); | |
7507 | char *p; | |
7508 | ||
7509 | if (offset < 0) | |
a2ae9df7 | 7510 | return offset; |
49f705c5 NHE |
7511 | |
7512 | p = ((char *)(get_vmcs12(vcpu))) + offset; | |
7513 | ||
7514 | switch (vmcs_field_type(field)) { | |
7515 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7516 | *ret = *((natural_width *)p); | |
a2ae9df7 | 7517 | return 0; |
49f705c5 NHE |
7518 | case VMCS_FIELD_TYPE_U16: |
7519 | *ret = *((u16 *)p); | |
a2ae9df7 | 7520 | return 0; |
49f705c5 NHE |
7521 | case VMCS_FIELD_TYPE_U32: |
7522 | *ret = *((u32 *)p); | |
a2ae9df7 | 7523 | return 0; |
49f705c5 NHE |
7524 | case VMCS_FIELD_TYPE_U64: |
7525 | *ret = *((u64 *)p); | |
a2ae9df7 | 7526 | return 0; |
49f705c5 | 7527 | default: |
a2ae9df7 PB |
7528 | WARN_ON(1); |
7529 | return -ENOENT; | |
49f705c5 NHE |
7530 | } |
7531 | } | |
7532 | ||
20b97fea | 7533 | |
a2ae9df7 PB |
7534 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
7535 | unsigned long field, u64 field_value){ | |
20b97fea AG |
7536 | short offset = vmcs_field_to_offset(field); |
7537 | char *p = ((char *) get_vmcs12(vcpu)) + offset; | |
7538 | if (offset < 0) | |
a2ae9df7 | 7539 | return offset; |
20b97fea AG |
7540 | |
7541 | switch (vmcs_field_type(field)) { | |
7542 | case VMCS_FIELD_TYPE_U16: | |
7543 | *(u16 *)p = field_value; | |
a2ae9df7 | 7544 | return 0; |
20b97fea AG |
7545 | case VMCS_FIELD_TYPE_U32: |
7546 | *(u32 *)p = field_value; | |
a2ae9df7 | 7547 | return 0; |
20b97fea AG |
7548 | case VMCS_FIELD_TYPE_U64: |
7549 | *(u64 *)p = field_value; | |
a2ae9df7 | 7550 | return 0; |
20b97fea AG |
7551 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
7552 | *(natural_width *)p = field_value; | |
a2ae9df7 | 7553 | return 0; |
20b97fea | 7554 | default: |
a2ae9df7 PB |
7555 | WARN_ON(1); |
7556 | return -ENOENT; | |
20b97fea AG |
7557 | } |
7558 | ||
7559 | } | |
7560 | ||
16f5b903 AG |
7561 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
7562 | { | |
7563 | int i; | |
7564 | unsigned long field; | |
7565 | u64 field_value; | |
355f4fb1 | 7566 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
44900ba6 | 7567 | const u16 *fields = shadow_read_write_fields; |
c2bae893 | 7568 | const int num_fields = max_shadow_read_write_fields; |
16f5b903 | 7569 | |
282da870 JK |
7570 | preempt_disable(); |
7571 | ||
16f5b903 AG |
7572 | vmcs_load(shadow_vmcs); |
7573 | ||
7574 | for (i = 0; i < num_fields; i++) { | |
7575 | field = fields[i]; | |
44900ba6 | 7576 | field_value = __vmcs_readl(field); |
16f5b903 AG |
7577 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
7578 | } | |
7579 | ||
7580 | vmcs_clear(shadow_vmcs); | |
7581 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
282da870 JK |
7582 | |
7583 | preempt_enable(); | |
16f5b903 AG |
7584 | } |
7585 | ||
c3114420 AG |
7586 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
7587 | { | |
44900ba6 | 7588 | const u16 *fields[] = { |
c2bae893 MK |
7589 | shadow_read_write_fields, |
7590 | shadow_read_only_fields | |
c3114420 | 7591 | }; |
c2bae893 | 7592 | const int max_fields[] = { |
c3114420 AG |
7593 | max_shadow_read_write_fields, |
7594 | max_shadow_read_only_fields | |
7595 | }; | |
7596 | int i, q; | |
7597 | unsigned long field; | |
7598 | u64 field_value = 0; | |
355f4fb1 | 7599 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
c3114420 AG |
7600 | |
7601 | vmcs_load(shadow_vmcs); | |
7602 | ||
c2bae893 | 7603 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
c3114420 AG |
7604 | for (i = 0; i < max_fields[q]; i++) { |
7605 | field = fields[q][i]; | |
7606 | vmcs12_read_any(&vmx->vcpu, field, &field_value); | |
44900ba6 | 7607 | __vmcs_writel(field, field_value); |
c3114420 AG |
7608 | } |
7609 | } | |
7610 | ||
7611 | vmcs_clear(shadow_vmcs); | |
7612 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
7613 | } | |
7614 | ||
49f705c5 NHE |
7615 | /* |
7616 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was | |
7617 | * used before) all generate the same failure when it is missing. | |
7618 | */ | |
7619 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) | |
7620 | { | |
7621 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7622 | if (vmx->nested.current_vmptr == -1ull) { | |
7623 | nested_vmx_failInvalid(vcpu); | |
49f705c5 NHE |
7624 | return 0; |
7625 | } | |
7626 | return 1; | |
7627 | } | |
7628 | ||
7629 | static int handle_vmread(struct kvm_vcpu *vcpu) | |
7630 | { | |
7631 | unsigned long field; | |
7632 | u64 field_value; | |
7633 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7634 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7635 | gva_t gva = 0; | |
7636 | ||
eb277562 | 7637 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
7638 | return 1; |
7639 | ||
6affcbed KH |
7640 | if (!nested_vmx_check_vmcs12(vcpu)) |
7641 | return kvm_skip_emulated_instruction(vcpu); | |
49f705c5 NHE |
7642 | |
7643 | /* Decode instruction info and find the field to read */ | |
27e6fb5d | 7644 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 | 7645 | /* Read the field, zero-extended to a u64 field_value */ |
a2ae9df7 | 7646 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
49f705c5 | 7647 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 7648 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7649 | } |
7650 | /* | |
7651 | * Now copy part of this value to register or memory, as requested. | |
7652 | * Note that the number of bits actually copied is 32 or 64 depending | |
7653 | * on the guest's mode (32 or 64 bit), not on the given field's length. | |
7654 | */ | |
7655 | if (vmx_instruction_info & (1u << 10)) { | |
27e6fb5d | 7656 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
49f705c5 NHE |
7657 | field_value); |
7658 | } else { | |
7659 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 7660 | vmx_instruction_info, true, &gva)) |
49f705c5 | 7661 | return 1; |
70f3aac9 | 7662 | /* _system ok, as hardware has verified cpl=0 */ |
49f705c5 NHE |
7663 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
7664 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); | |
7665 | } | |
7666 | ||
7667 | nested_vmx_succeed(vcpu); | |
6affcbed | 7668 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7669 | } |
7670 | ||
7671 | ||
7672 | static int handle_vmwrite(struct kvm_vcpu *vcpu) | |
7673 | { | |
7674 | unsigned long field; | |
7675 | gva_t gva; | |
7676 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7677 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
49f705c5 NHE |
7678 | /* The value to write might be 32 or 64 bits, depending on L1's long |
7679 | * mode, and eventually we need to write that into a field of several | |
7680 | * possible lengths. The code below first zero-extends the value to 64 | |
6a6256f9 | 7681 | * bit (field_value), and then copies only the appropriate number of |
49f705c5 NHE |
7682 | * bits into the vmcs12 field. |
7683 | */ | |
7684 | u64 field_value = 0; | |
7685 | struct x86_exception e; | |
7686 | ||
eb277562 | 7687 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
7688 | return 1; |
7689 | ||
6affcbed KH |
7690 | if (!nested_vmx_check_vmcs12(vcpu)) |
7691 | return kvm_skip_emulated_instruction(vcpu); | |
eb277562 | 7692 | |
49f705c5 | 7693 | if (vmx_instruction_info & (1u << 10)) |
27e6fb5d | 7694 | field_value = kvm_register_readl(vcpu, |
49f705c5 NHE |
7695 | (((vmx_instruction_info) >> 3) & 0xf)); |
7696 | else { | |
7697 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 7698 | vmx_instruction_info, false, &gva)) |
49f705c5 NHE |
7699 | return 1; |
7700 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, | |
27e6fb5d | 7701 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
49f705c5 NHE |
7702 | kvm_inject_page_fault(vcpu, &e); |
7703 | return 1; | |
7704 | } | |
7705 | } | |
7706 | ||
7707 | ||
27e6fb5d | 7708 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 NHE |
7709 | if (vmcs_field_readonly(field)) { |
7710 | nested_vmx_failValid(vcpu, | |
7711 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); | |
6affcbed | 7712 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7713 | } |
7714 | ||
a2ae9df7 | 7715 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
49f705c5 | 7716 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 7717 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7718 | } |
7719 | ||
7720 | nested_vmx_succeed(vcpu); | |
6affcbed | 7721 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7722 | } |
7723 | ||
a8bc284e JM |
7724 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
7725 | { | |
7726 | vmx->nested.current_vmptr = vmptr; | |
7727 | if (enable_shadow_vmcs) { | |
7728 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
7729 | SECONDARY_EXEC_SHADOW_VMCS); | |
7730 | vmcs_write64(VMCS_LINK_POINTER, | |
7731 | __pa(vmx->vmcs01.shadow_vmcs)); | |
7732 | vmx->nested.sync_shadow_vmcs = true; | |
7733 | } | |
7734 | } | |
7735 | ||
63846663 NHE |
7736 | /* Emulate the VMPTRLD instruction */ |
7737 | static int handle_vmptrld(struct kvm_vcpu *vcpu) | |
7738 | { | |
7739 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
63846663 | 7740 | gpa_t vmptr; |
63846663 NHE |
7741 | |
7742 | if (!nested_vmx_check_permission(vcpu)) | |
7743 | return 1; | |
7744 | ||
cbf71279 | 7745 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
63846663 | 7746 | return 1; |
63846663 | 7747 | |
cbf71279 RK |
7748 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
7749 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); | |
7750 | return kvm_skip_emulated_instruction(vcpu); | |
7751 | } | |
7752 | ||
7753 | if (vmptr == vmx->nested.vmxon_ptr) { | |
7754 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); | |
7755 | return kvm_skip_emulated_instruction(vcpu); | |
7756 | } | |
7757 | ||
63846663 NHE |
7758 | if (vmx->nested.current_vmptr != vmptr) { |
7759 | struct vmcs12 *new_vmcs12; | |
7760 | struct page *page; | |
5e2f30b7 DH |
7761 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
7762 | if (is_error_page(page)) { | |
63846663 | 7763 | nested_vmx_failInvalid(vcpu); |
6affcbed | 7764 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
7765 | } |
7766 | new_vmcs12 = kmap(page); | |
7767 | if (new_vmcs12->revision_id != VMCS12_REVISION) { | |
7768 | kunmap(page); | |
53a70daf | 7769 | kvm_release_page_clean(page); |
63846663 NHE |
7770 | nested_vmx_failValid(vcpu, |
7771 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); | |
6affcbed | 7772 | return kvm_skip_emulated_instruction(vcpu); |
63846663 | 7773 | } |
63846663 | 7774 | |
9a2a05b9 | 7775 | nested_release_vmcs12(vmx); |
4f2777bc DM |
7776 | /* |
7777 | * Load VMCS12 from guest memory since it is not already | |
7778 | * cached. | |
7779 | */ | |
9f744c59 PB |
7780 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
7781 | kunmap(page); | |
53a70daf | 7782 | kvm_release_page_clean(page); |
9f744c59 | 7783 | |
a8bc284e | 7784 | set_current_vmptr(vmx, vmptr); |
63846663 NHE |
7785 | } |
7786 | ||
7787 | nested_vmx_succeed(vcpu); | |
6affcbed | 7788 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
7789 | } |
7790 | ||
6a4d7550 NHE |
7791 | /* Emulate the VMPTRST instruction */ |
7792 | static int handle_vmptrst(struct kvm_vcpu *vcpu) | |
7793 | { | |
7794 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7795 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7796 | gva_t vmcs_gva; | |
7797 | struct x86_exception e; | |
7798 | ||
7799 | if (!nested_vmx_check_permission(vcpu)) | |
7800 | return 1; | |
7801 | ||
7802 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 7803 | vmx_instruction_info, true, &vmcs_gva)) |
6a4d7550 | 7804 | return 1; |
70f3aac9 | 7805 | /* ok to use *_system, as hardware has verified cpl=0 */ |
6a4d7550 NHE |
7806 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
7807 | (void *)&to_vmx(vcpu)->nested.current_vmptr, | |
7808 | sizeof(u64), &e)) { | |
7809 | kvm_inject_page_fault(vcpu, &e); | |
7810 | return 1; | |
7811 | } | |
7812 | nested_vmx_succeed(vcpu); | |
6affcbed | 7813 | return kvm_skip_emulated_instruction(vcpu); |
6a4d7550 NHE |
7814 | } |
7815 | ||
bfd0a56b NHE |
7816 | /* Emulate the INVEPT instruction */ |
7817 | static int handle_invept(struct kvm_vcpu *vcpu) | |
7818 | { | |
b9c237bb | 7819 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
bfd0a56b NHE |
7820 | u32 vmx_instruction_info, types; |
7821 | unsigned long type; | |
7822 | gva_t gva; | |
7823 | struct x86_exception e; | |
7824 | struct { | |
7825 | u64 eptp, gpa; | |
7826 | } operand; | |
bfd0a56b | 7827 | |
b9c237bb WV |
7828 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
7829 | SECONDARY_EXEC_ENABLE_EPT) || | |
7830 | !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { | |
bfd0a56b NHE |
7831 | kvm_queue_exception(vcpu, UD_VECTOR); |
7832 | return 1; | |
7833 | } | |
7834 | ||
7835 | if (!nested_vmx_check_permission(vcpu)) | |
7836 | return 1; | |
7837 | ||
bfd0a56b | 7838 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
27e6fb5d | 7839 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
bfd0a56b | 7840 | |
b9c237bb | 7841 | types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
bfd0a56b | 7842 | |
85c856b3 | 7843 | if (type >= 32 || !(types & (1 << type))) { |
bfd0a56b NHE |
7844 | nested_vmx_failValid(vcpu, |
7845 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 7846 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
7847 | } |
7848 | ||
7849 | /* According to the Intel VMX instruction reference, the memory | |
7850 | * operand is read even if it isn't needed (e.g., for type==global) | |
7851 | */ | |
7852 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 7853 | vmx_instruction_info, false, &gva)) |
bfd0a56b NHE |
7854 | return 1; |
7855 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, | |
7856 | sizeof(operand), &e)) { | |
7857 | kvm_inject_page_fault(vcpu, &e); | |
7858 | return 1; | |
7859 | } | |
7860 | ||
7861 | switch (type) { | |
bfd0a56b | 7862 | case VMX_EPT_EXTENT_GLOBAL: |
45e11817 BD |
7863 | /* |
7864 | * TODO: track mappings and invalidate | |
7865 | * single context requests appropriately | |
7866 | */ | |
7867 | case VMX_EPT_EXTENT_CONTEXT: | |
bfd0a56b | 7868 | kvm_mmu_sync_roots(vcpu); |
77c3913b | 7869 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
bfd0a56b NHE |
7870 | nested_vmx_succeed(vcpu); |
7871 | break; | |
7872 | default: | |
7873 | BUG_ON(1); | |
7874 | break; | |
7875 | } | |
7876 | ||
6affcbed | 7877 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
7878 | } |
7879 | ||
a642fc30 PM |
7880 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
7881 | { | |
99b83ac8 WL |
7882 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
7883 | u32 vmx_instruction_info; | |
7884 | unsigned long type, types; | |
7885 | gva_t gva; | |
7886 | struct x86_exception e; | |
40352605 JM |
7887 | struct { |
7888 | u64 vpid; | |
7889 | u64 gla; | |
7890 | } operand; | |
99b83ac8 WL |
7891 | |
7892 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & | |
7893 | SECONDARY_EXEC_ENABLE_VPID) || | |
7894 | !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) { | |
7895 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7896 | return 1; | |
7897 | } | |
7898 | ||
7899 | if (!nested_vmx_check_permission(vcpu)) | |
7900 | return 1; | |
7901 | ||
7902 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7903 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); | |
7904 | ||
bcdde302 JD |
7905 | types = (vmx->nested.nested_vmx_vpid_caps & |
7906 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; | |
99b83ac8 | 7907 | |
85c856b3 | 7908 | if (type >= 32 || !(types & (1 << type))) { |
99b83ac8 WL |
7909 | nested_vmx_failValid(vcpu, |
7910 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 7911 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
7912 | } |
7913 | ||
7914 | /* according to the intel vmx instruction reference, the memory | |
7915 | * operand is read even if it isn't needed (e.g., for type==global) | |
7916 | */ | |
7917 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
7918 | vmx_instruction_info, false, &gva)) | |
7919 | return 1; | |
40352605 JM |
7920 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
7921 | sizeof(operand), &e)) { | |
99b83ac8 WL |
7922 | kvm_inject_page_fault(vcpu, &e); |
7923 | return 1; | |
7924 | } | |
40352605 JM |
7925 | if (operand.vpid >> 16) { |
7926 | nested_vmx_failValid(vcpu, | |
7927 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
7928 | return kvm_skip_emulated_instruction(vcpu); | |
7929 | } | |
99b83ac8 WL |
7930 | |
7931 | switch (type) { | |
bcdde302 | 7932 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
fd8cb433 | 7933 | if (is_noncanonical_address(operand.gla, vcpu)) { |
40352605 JM |
7934 | nested_vmx_failValid(vcpu, |
7935 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
7936 | return kvm_skip_emulated_instruction(vcpu); | |
7937 | } | |
7938 | /* fall through */ | |
ef697a71 | 7939 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
bcdde302 | 7940 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
40352605 | 7941 | if (!operand.vpid) { |
bcdde302 JD |
7942 | nested_vmx_failValid(vcpu, |
7943 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 7944 | return kvm_skip_emulated_instruction(vcpu); |
bcdde302 JD |
7945 | } |
7946 | break; | |
99b83ac8 | 7947 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
99b83ac8 WL |
7948 | break; |
7949 | default: | |
bcdde302 | 7950 | WARN_ON_ONCE(1); |
6affcbed | 7951 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
7952 | } |
7953 | ||
c2ba05cc | 7954 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
bcdde302 JD |
7955 | nested_vmx_succeed(vcpu); |
7956 | ||
6affcbed | 7957 | return kvm_skip_emulated_instruction(vcpu); |
a642fc30 PM |
7958 | } |
7959 | ||
843e4330 KH |
7960 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
7961 | { | |
7962 | unsigned long exit_qualification; | |
7963 | ||
7964 | trace_kvm_pml_full(vcpu->vcpu_id); | |
7965 | ||
7966 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7967 | ||
7968 | /* | |
7969 | * PML buffer FULL happened while executing iret from NMI, | |
7970 | * "blocked by NMI" bit has to be set before next VM entry. | |
7971 | */ | |
7972 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && | |
d02fcf50 | 7973 | enable_vnmi && |
843e4330 KH |
7974 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
7975 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
7976 | GUEST_INTR_STATE_NMI); | |
7977 | ||
7978 | /* | |
7979 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do | |
7980 | * here.., and there's no userspace involvement needed for PML. | |
7981 | */ | |
7982 | return 1; | |
7983 | } | |
7984 | ||
64672c95 YJ |
7985 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
7986 | { | |
7987 | kvm_lapic_expired_hv_timer(vcpu); | |
7988 | return 1; | |
7989 | } | |
7990 | ||
41ab9372 BD |
7991 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
7992 | { | |
7993 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
41ab9372 BD |
7994 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
7995 | ||
7996 | /* Check for memory type validity */ | |
bb97a016 DH |
7997 | switch (address & VMX_EPTP_MT_MASK) { |
7998 | case VMX_EPTP_MT_UC: | |
41ab9372 BD |
7999 | if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT)) |
8000 | return false; | |
8001 | break; | |
bb97a016 | 8002 | case VMX_EPTP_MT_WB: |
41ab9372 BD |
8003 | if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT)) |
8004 | return false; | |
8005 | break; | |
8006 | default: | |
8007 | return false; | |
8008 | } | |
8009 | ||
bb97a016 DH |
8010 | /* only 4 levels page-walk length are valid */ |
8011 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) | |
41ab9372 BD |
8012 | return false; |
8013 | ||
8014 | /* Reserved bits should not be set */ | |
8015 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) | |
8016 | return false; | |
8017 | ||
8018 | /* AD, if set, should be supported */ | |
bb97a016 | 8019 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
41ab9372 BD |
8020 | if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT)) |
8021 | return false; | |
8022 | } | |
8023 | ||
8024 | return true; | |
8025 | } | |
8026 | ||
8027 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, | |
8028 | struct vmcs12 *vmcs12) | |
8029 | { | |
8030 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; | |
8031 | u64 address; | |
8032 | bool accessed_dirty; | |
8033 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; | |
8034 | ||
8035 | if (!nested_cpu_has_eptp_switching(vmcs12) || | |
8036 | !nested_cpu_has_ept(vmcs12)) | |
8037 | return 1; | |
8038 | ||
8039 | if (index >= VMFUNC_EPTP_ENTRIES) | |
8040 | return 1; | |
8041 | ||
8042 | ||
8043 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, | |
8044 | &address, index * 8, 8)) | |
8045 | return 1; | |
8046 | ||
bb97a016 | 8047 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
41ab9372 BD |
8048 | |
8049 | /* | |
8050 | * If the (L2) guest does a vmfunc to the currently | |
8051 | * active ept pointer, we don't have to do anything else | |
8052 | */ | |
8053 | if (vmcs12->ept_pointer != address) { | |
8054 | if (!valid_ept_address(vcpu, address)) | |
8055 | return 1; | |
8056 | ||
8057 | kvm_mmu_unload(vcpu); | |
8058 | mmu->ept_ad = accessed_dirty; | |
8059 | mmu->base_role.ad_disabled = !accessed_dirty; | |
8060 | vmcs12->ept_pointer = address; | |
8061 | /* | |
8062 | * TODO: Check what's the correct approach in case | |
8063 | * mmu reload fails. Currently, we just let the next | |
8064 | * reload potentially fail | |
8065 | */ | |
8066 | kvm_mmu_reload(vcpu); | |
8067 | } | |
8068 | ||
8069 | return 0; | |
8070 | } | |
8071 | ||
2a499e49 BD |
8072 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
8073 | { | |
27c42a1b BD |
8074 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
8075 | struct vmcs12 *vmcs12; | |
8076 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; | |
8077 | ||
8078 | /* | |
8079 | * VMFUNC is only supported for nested guests, but we always enable the | |
8080 | * secondary control for simplicity; for non-nested mode, fake that we | |
8081 | * didn't by injecting #UD. | |
8082 | */ | |
8083 | if (!is_guest_mode(vcpu)) { | |
8084 | kvm_queue_exception(vcpu, UD_VECTOR); | |
8085 | return 1; | |
8086 | } | |
8087 | ||
8088 | vmcs12 = get_vmcs12(vcpu); | |
8089 | if ((vmcs12->vm_function_control & (1 << function)) == 0) | |
8090 | goto fail; | |
41ab9372 BD |
8091 | |
8092 | switch (function) { | |
8093 | case 0: | |
8094 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) | |
8095 | goto fail; | |
8096 | break; | |
8097 | default: | |
8098 | goto fail; | |
8099 | } | |
8100 | return kvm_skip_emulated_instruction(vcpu); | |
27c42a1b BD |
8101 | |
8102 | fail: | |
8103 | nested_vmx_vmexit(vcpu, vmx->exit_reason, | |
8104 | vmcs_read32(VM_EXIT_INTR_INFO), | |
8105 | vmcs_readl(EXIT_QUALIFICATION)); | |
2a499e49 BD |
8106 | return 1; |
8107 | } | |
8108 | ||
6aa8b732 AK |
8109 | /* |
8110 | * The exit handlers return 1 if the exit was handled fully and guest execution | |
8111 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs | |
8112 | * to be done to userspace and return 0. | |
8113 | */ | |
772e0318 | 8114 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
6aa8b732 AK |
8115 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
8116 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, | |
988ad74f | 8117 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
f08864b4 | 8118 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
6aa8b732 | 8119 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
6aa8b732 AK |
8120 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
8121 | [EXIT_REASON_DR_ACCESS] = handle_dr, | |
8122 | [EXIT_REASON_CPUID] = handle_cpuid, | |
8123 | [EXIT_REASON_MSR_READ] = handle_rdmsr, | |
8124 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, | |
8125 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, | |
8126 | [EXIT_REASON_HLT] = handle_halt, | |
ec25d5e6 | 8127 | [EXIT_REASON_INVD] = handle_invd, |
a7052897 | 8128 | [EXIT_REASON_INVLPG] = handle_invlpg, |
fee84b07 | 8129 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
c21415e8 | 8130 | [EXIT_REASON_VMCALL] = handle_vmcall, |
27d6c865 | 8131 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
cd232ad0 | 8132 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
63846663 | 8133 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
6a4d7550 | 8134 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
49f705c5 | 8135 | [EXIT_REASON_VMREAD] = handle_vmread, |
cd232ad0 | 8136 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
49f705c5 | 8137 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
ec378aee NHE |
8138 | [EXIT_REASON_VMOFF] = handle_vmoff, |
8139 | [EXIT_REASON_VMON] = handle_vmon, | |
f78e0e2e SY |
8140 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
8141 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, | |
83d4c286 | 8142 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
c7c9c56c | 8143 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
e5edaa01 | 8144 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
2acf923e | 8145 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
37817f29 | 8146 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
a0861c02 | 8147 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
0367f205 PB |
8148 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
8149 | [EXIT_REASON_LDTR_TR] = handle_desc, | |
68f89400 MT |
8150 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
8151 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, | |
4b8d54f9 | 8152 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
87c00572 | 8153 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
5f3d45e7 | 8154 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
87c00572 | 8155 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
bfd0a56b | 8156 | [EXIT_REASON_INVEPT] = handle_invept, |
a642fc30 | 8157 | [EXIT_REASON_INVVPID] = handle_invvpid, |
45ec368c | 8158 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
75f4fc8d | 8159 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
f53cd63c WL |
8160 | [EXIT_REASON_XSAVES] = handle_xsaves, |
8161 | [EXIT_REASON_XRSTORS] = handle_xrstors, | |
843e4330 | 8162 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
2a499e49 | 8163 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
64672c95 | 8164 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
6aa8b732 AK |
8165 | }; |
8166 | ||
8167 | static const int kvm_vmx_max_exit_handlers = | |
50a3485c | 8168 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
6aa8b732 | 8169 | |
908a7bdd JK |
8170 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
8171 | struct vmcs12 *vmcs12) | |
8172 | { | |
8173 | unsigned long exit_qualification; | |
8174 | gpa_t bitmap, last_bitmap; | |
8175 | unsigned int port; | |
8176 | int size; | |
8177 | u8 b; | |
8178 | ||
908a7bdd | 8179 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
2f0a6397 | 8180 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
908a7bdd JK |
8181 | |
8182 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8183 | ||
8184 | port = exit_qualification >> 16; | |
8185 | size = (exit_qualification & 7) + 1; | |
8186 | ||
8187 | last_bitmap = (gpa_t)-1; | |
8188 | b = -1; | |
8189 | ||
8190 | while (size > 0) { | |
8191 | if (port < 0x8000) | |
8192 | bitmap = vmcs12->io_bitmap_a; | |
8193 | else if (port < 0x10000) | |
8194 | bitmap = vmcs12->io_bitmap_b; | |
8195 | else | |
1d804d07 | 8196 | return true; |
908a7bdd JK |
8197 | bitmap += (port & 0x7fff) / 8; |
8198 | ||
8199 | if (last_bitmap != bitmap) | |
54bf36aa | 8200 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
1d804d07 | 8201 | return true; |
908a7bdd | 8202 | if (b & (1 << (port & 7))) |
1d804d07 | 8203 | return true; |
908a7bdd JK |
8204 | |
8205 | port++; | |
8206 | size--; | |
8207 | last_bitmap = bitmap; | |
8208 | } | |
8209 | ||
1d804d07 | 8210 | return false; |
908a7bdd JK |
8211 | } |
8212 | ||
644d711a NHE |
8213 | /* |
8214 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, | |
8215 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed | |
8216 | * disinterest in the current event (read or write a specific MSR) by using an | |
8217 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. | |
8218 | */ | |
8219 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, | |
8220 | struct vmcs12 *vmcs12, u32 exit_reason) | |
8221 | { | |
8222 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; | |
8223 | gpa_t bitmap; | |
8224 | ||
cbd29cb6 | 8225 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
1d804d07 | 8226 | return true; |
644d711a NHE |
8227 | |
8228 | /* | |
8229 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, | |
8230 | * for the four combinations of read/write and low/high MSR numbers. | |
8231 | * First we need to figure out which of the four to use: | |
8232 | */ | |
8233 | bitmap = vmcs12->msr_bitmap; | |
8234 | if (exit_reason == EXIT_REASON_MSR_WRITE) | |
8235 | bitmap += 2048; | |
8236 | if (msr_index >= 0xc0000000) { | |
8237 | msr_index -= 0xc0000000; | |
8238 | bitmap += 1024; | |
8239 | } | |
8240 | ||
8241 | /* Then read the msr_index'th bit from this bitmap: */ | |
8242 | if (msr_index < 1024*8) { | |
8243 | unsigned char b; | |
54bf36aa | 8244 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
1d804d07 | 8245 | return true; |
644d711a NHE |
8246 | return 1 & (b >> (msr_index & 7)); |
8247 | } else | |
1d804d07 | 8248 | return true; /* let L1 handle the wrong parameter */ |
644d711a NHE |
8249 | } |
8250 | ||
8251 | /* | |
8252 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, | |
8253 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to | |
8254 | * intercept (via guest_host_mask etc.) the current event. | |
8255 | */ | |
8256 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, | |
8257 | struct vmcs12 *vmcs12) | |
8258 | { | |
8259 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8260 | int cr = exit_qualification & 15; | |
e1d39b17 JS |
8261 | int reg; |
8262 | unsigned long val; | |
644d711a NHE |
8263 | |
8264 | switch ((exit_qualification >> 4) & 3) { | |
8265 | case 0: /* mov to cr */ | |
e1d39b17 JS |
8266 | reg = (exit_qualification >> 8) & 15; |
8267 | val = kvm_register_readl(vcpu, reg); | |
644d711a NHE |
8268 | switch (cr) { |
8269 | case 0: | |
8270 | if (vmcs12->cr0_guest_host_mask & | |
8271 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 8272 | return true; |
644d711a NHE |
8273 | break; |
8274 | case 3: | |
8275 | if ((vmcs12->cr3_target_count >= 1 && | |
8276 | vmcs12->cr3_target_value0 == val) || | |
8277 | (vmcs12->cr3_target_count >= 2 && | |
8278 | vmcs12->cr3_target_value1 == val) || | |
8279 | (vmcs12->cr3_target_count >= 3 && | |
8280 | vmcs12->cr3_target_value2 == val) || | |
8281 | (vmcs12->cr3_target_count >= 4 && | |
8282 | vmcs12->cr3_target_value3 == val)) | |
1d804d07 | 8283 | return false; |
644d711a | 8284 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
1d804d07 | 8285 | return true; |
644d711a NHE |
8286 | break; |
8287 | case 4: | |
8288 | if (vmcs12->cr4_guest_host_mask & | |
8289 | (vmcs12->cr4_read_shadow ^ val)) | |
1d804d07 | 8290 | return true; |
644d711a NHE |
8291 | break; |
8292 | case 8: | |
8293 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) | |
1d804d07 | 8294 | return true; |
644d711a NHE |
8295 | break; |
8296 | } | |
8297 | break; | |
8298 | case 2: /* clts */ | |
8299 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && | |
8300 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) | |
1d804d07 | 8301 | return true; |
644d711a NHE |
8302 | break; |
8303 | case 1: /* mov from cr */ | |
8304 | switch (cr) { | |
8305 | case 3: | |
8306 | if (vmcs12->cpu_based_vm_exec_control & | |
8307 | CPU_BASED_CR3_STORE_EXITING) | |
1d804d07 | 8308 | return true; |
644d711a NHE |
8309 | break; |
8310 | case 8: | |
8311 | if (vmcs12->cpu_based_vm_exec_control & | |
8312 | CPU_BASED_CR8_STORE_EXITING) | |
1d804d07 | 8313 | return true; |
644d711a NHE |
8314 | break; |
8315 | } | |
8316 | break; | |
8317 | case 3: /* lmsw */ | |
8318 | /* | |
8319 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of | |
8320 | * cr0. Other attempted changes are ignored, with no exit. | |
8321 | */ | |
e1d39b17 | 8322 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
644d711a NHE |
8323 | if (vmcs12->cr0_guest_host_mask & 0xe & |
8324 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 8325 | return true; |
644d711a NHE |
8326 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
8327 | !(vmcs12->cr0_read_shadow & 0x1) && | |
8328 | (val & 0x1)) | |
1d804d07 | 8329 | return true; |
644d711a NHE |
8330 | break; |
8331 | } | |
1d804d07 | 8332 | return false; |
644d711a NHE |
8333 | } |
8334 | ||
8335 | /* | |
8336 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we | |
8337 | * should handle it ourselves in L0 (and then continue L2). Only call this | |
8338 | * when in is_guest_mode (L2). | |
8339 | */ | |
7313c698 | 8340 | static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason) |
644d711a | 8341 | { |
644d711a NHE |
8342 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
8343 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8344 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
8345 | ||
4f350c6d JM |
8346 | if (vmx->nested.nested_run_pending) |
8347 | return false; | |
8348 | ||
8349 | if (unlikely(vmx->fail)) { | |
8350 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, | |
8351 | vmcs_read32(VM_INSTRUCTION_ERROR)); | |
8352 | return true; | |
8353 | } | |
542060ea | 8354 | |
c9f04407 DM |
8355 | /* |
8356 | * The host physical addresses of some pages of guest memory | |
00647b44 JM |
8357 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
8358 | * Page). The CPU may write to these pages via their host | |
8359 | * physical address while L2 is running, bypassing any | |
8360 | * address-translation-based dirty tracking (e.g. EPT write | |
8361 | * protection). | |
c9f04407 DM |
8362 | * |
8363 | * Mark them dirty on every exit from L2 to prevent them from | |
8364 | * getting out of sync with dirty tracking. | |
8365 | */ | |
8366 | nested_mark_vmcs12_pages_dirty(vcpu); | |
8367 | ||
4f350c6d JM |
8368 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
8369 | vmcs_readl(EXIT_QUALIFICATION), | |
8370 | vmx->idt_vectoring_info, | |
8371 | intr_info, | |
8372 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
8373 | KVM_ISA_VMX); | |
644d711a NHE |
8374 | |
8375 | switch (exit_reason) { | |
8376 | case EXIT_REASON_EXCEPTION_NMI: | |
ef85b673 | 8377 | if (is_nmi(intr_info)) |
1d804d07 | 8378 | return false; |
644d711a | 8379 | else if (is_page_fault(intr_info)) |
52a5c155 | 8380 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
e504c909 | 8381 | else if (is_no_device(intr_info) && |
ccf9844e | 8382 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
1d804d07 | 8383 | return false; |
6f05485d JK |
8384 | else if (is_debug(intr_info) && |
8385 | vcpu->guest_debug & | |
8386 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
8387 | return false; | |
8388 | else if (is_breakpoint(intr_info) && | |
8389 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) | |
8390 | return false; | |
644d711a NHE |
8391 | return vmcs12->exception_bitmap & |
8392 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); | |
8393 | case EXIT_REASON_EXTERNAL_INTERRUPT: | |
1d804d07 | 8394 | return false; |
644d711a | 8395 | case EXIT_REASON_TRIPLE_FAULT: |
1d804d07 | 8396 | return true; |
644d711a | 8397 | case EXIT_REASON_PENDING_INTERRUPT: |
3b656cf7 | 8398 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
644d711a | 8399 | case EXIT_REASON_NMI_WINDOW: |
3b656cf7 | 8400 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
644d711a | 8401 | case EXIT_REASON_TASK_SWITCH: |
1d804d07 | 8402 | return true; |
644d711a | 8403 | case EXIT_REASON_CPUID: |
1d804d07 | 8404 | return true; |
644d711a NHE |
8405 | case EXIT_REASON_HLT: |
8406 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); | |
8407 | case EXIT_REASON_INVD: | |
1d804d07 | 8408 | return true; |
644d711a NHE |
8409 | case EXIT_REASON_INVLPG: |
8410 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); | |
8411 | case EXIT_REASON_RDPMC: | |
8412 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); | |
a5f46457 | 8413 | case EXIT_REASON_RDRAND: |
736fdf72 | 8414 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
a5f46457 | 8415 | case EXIT_REASON_RDSEED: |
736fdf72 | 8416 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
b3a2a907 | 8417 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
644d711a NHE |
8418 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
8419 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: | |
8420 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: | |
8421 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: | |
8422 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: | |
8423 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: | |
a642fc30 | 8424 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
644d711a NHE |
8425 | /* |
8426 | * VMX instructions trap unconditionally. This allows L1 to | |
8427 | * emulate them for its L2 guest, i.e., allows 3-level nesting! | |
8428 | */ | |
1d804d07 | 8429 | return true; |
644d711a NHE |
8430 | case EXIT_REASON_CR_ACCESS: |
8431 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); | |
8432 | case EXIT_REASON_DR_ACCESS: | |
8433 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); | |
8434 | case EXIT_REASON_IO_INSTRUCTION: | |
908a7bdd | 8435 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
1b07304c PB |
8436 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
8437 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); | |
644d711a NHE |
8438 | case EXIT_REASON_MSR_READ: |
8439 | case EXIT_REASON_MSR_WRITE: | |
8440 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); | |
8441 | case EXIT_REASON_INVALID_STATE: | |
1d804d07 | 8442 | return true; |
644d711a NHE |
8443 | case EXIT_REASON_MWAIT_INSTRUCTION: |
8444 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); | |
5f3d45e7 MD |
8445 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
8446 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); | |
644d711a NHE |
8447 | case EXIT_REASON_MONITOR_INSTRUCTION: |
8448 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); | |
8449 | case EXIT_REASON_PAUSE_INSTRUCTION: | |
8450 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || | |
8451 | nested_cpu_has2(vmcs12, | |
8452 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); | |
8453 | case EXIT_REASON_MCE_DURING_VMENTRY: | |
1d804d07 | 8454 | return false; |
644d711a | 8455 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
a7c0b07d | 8456 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
644d711a NHE |
8457 | case EXIT_REASON_APIC_ACCESS: |
8458 | return nested_cpu_has2(vmcs12, | |
8459 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
82f0dd4b | 8460 | case EXIT_REASON_APIC_WRITE: |
608406e2 WV |
8461 | case EXIT_REASON_EOI_INDUCED: |
8462 | /* apic_write and eoi_induced should exit unconditionally. */ | |
1d804d07 | 8463 | return true; |
644d711a | 8464 | case EXIT_REASON_EPT_VIOLATION: |
2b1be677 NHE |
8465 | /* |
8466 | * L0 always deals with the EPT violation. If nested EPT is | |
8467 | * used, and the nested mmu code discovers that the address is | |
8468 | * missing in the guest EPT table (EPT12), the EPT violation | |
8469 | * will be injected with nested_ept_inject_page_fault() | |
8470 | */ | |
1d804d07 | 8471 | return false; |
644d711a | 8472 | case EXIT_REASON_EPT_MISCONFIG: |
2b1be677 NHE |
8473 | /* |
8474 | * L2 never uses directly L1's EPT, but rather L0's own EPT | |
8475 | * table (shadow on EPT) or a merged EPT table that L0 built | |
8476 | * (EPT on EPT). So any problems with the structure of the | |
8477 | * table is L0's fault. | |
8478 | */ | |
1d804d07 | 8479 | return false; |
90a2db6d PB |
8480 | case EXIT_REASON_INVPCID: |
8481 | return | |
8482 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && | |
8483 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); | |
644d711a NHE |
8484 | case EXIT_REASON_WBINVD: |
8485 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); | |
8486 | case EXIT_REASON_XSETBV: | |
1d804d07 | 8487 | return true; |
81dc01f7 WL |
8488 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
8489 | /* | |
8490 | * This should never happen, since it is not possible to | |
8491 | * set XSS to a non-zero value---neither in L1 nor in L2. | |
8492 | * If if it were, XSS would have to be checked against | |
8493 | * the XSS exit bitmap in vmcs12. | |
8494 | */ | |
8495 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); | |
55123e3c WL |
8496 | case EXIT_REASON_PREEMPTION_TIMER: |
8497 | return false; | |
ab007cc9 | 8498 | case EXIT_REASON_PML_FULL: |
03efce6f | 8499 | /* We emulate PML support to L1. */ |
ab007cc9 | 8500 | return false; |
2a499e49 BD |
8501 | case EXIT_REASON_VMFUNC: |
8502 | /* VM functions are emulated through L2->L0 vmexits. */ | |
8503 | return false; | |
644d711a | 8504 | default: |
1d804d07 | 8505 | return true; |
644d711a NHE |
8506 | } |
8507 | } | |
8508 | ||
7313c698 PB |
8509 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
8510 | { | |
8511 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
8512 | ||
8513 | /* | |
8514 | * At this point, the exit interruption info in exit_intr_info | |
8515 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT | |
8516 | * we need to query the in-kernel LAPIC. | |
8517 | */ | |
8518 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); | |
8519 | if ((exit_intr_info & | |
8520 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == | |
8521 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { | |
8522 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
8523 | vmcs12->vm_exit_intr_error_code = | |
8524 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); | |
8525 | } | |
8526 | ||
8527 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, | |
8528 | vmcs_readl(EXIT_QUALIFICATION)); | |
8529 | return 1; | |
8530 | } | |
8531 | ||
586f9607 AK |
8532 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
8533 | { | |
8534 | *info1 = vmcs_readl(EXIT_QUALIFICATION); | |
8535 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); | |
8536 | } | |
8537 | ||
a3eaa864 | 8538 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
843e4330 | 8539 | { |
a3eaa864 KH |
8540 | if (vmx->pml_pg) { |
8541 | __free_page(vmx->pml_pg); | |
8542 | vmx->pml_pg = NULL; | |
8543 | } | |
843e4330 KH |
8544 | } |
8545 | ||
54bf36aa | 8546 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
843e4330 | 8547 | { |
54bf36aa | 8548 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
843e4330 KH |
8549 | u64 *pml_buf; |
8550 | u16 pml_idx; | |
8551 | ||
8552 | pml_idx = vmcs_read16(GUEST_PML_INDEX); | |
8553 | ||
8554 | /* Do nothing if PML buffer is empty */ | |
8555 | if (pml_idx == (PML_ENTITY_NUM - 1)) | |
8556 | return; | |
8557 | ||
8558 | /* PML index always points to next available PML buffer entity */ | |
8559 | if (pml_idx >= PML_ENTITY_NUM) | |
8560 | pml_idx = 0; | |
8561 | else | |
8562 | pml_idx++; | |
8563 | ||
8564 | pml_buf = page_address(vmx->pml_pg); | |
8565 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { | |
8566 | u64 gpa; | |
8567 | ||
8568 | gpa = pml_buf[pml_idx]; | |
8569 | WARN_ON(gpa & (PAGE_SIZE - 1)); | |
54bf36aa | 8570 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
843e4330 KH |
8571 | } |
8572 | ||
8573 | /* reset PML index */ | |
8574 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
8575 | } | |
8576 | ||
8577 | /* | |
8578 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. | |
8579 | * Called before reporting dirty_bitmap to userspace. | |
8580 | */ | |
8581 | static void kvm_flush_pml_buffers(struct kvm *kvm) | |
8582 | { | |
8583 | int i; | |
8584 | struct kvm_vcpu *vcpu; | |
8585 | /* | |
8586 | * We only need to kick vcpu out of guest mode here, as PML buffer | |
8587 | * is flushed at beginning of all VMEXITs, and it's obvious that only | |
8588 | * vcpus running in guest are possible to have unflushed GPAs in PML | |
8589 | * buffer. | |
8590 | */ | |
8591 | kvm_for_each_vcpu(i, vcpu, kvm) | |
8592 | kvm_vcpu_kick(vcpu); | |
8593 | } | |
8594 | ||
4eb64dce PB |
8595 | static void vmx_dump_sel(char *name, uint32_t sel) |
8596 | { | |
8597 | pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", | |
96794e4e | 8598 | name, vmcs_read16(sel), |
4eb64dce PB |
8599 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
8600 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), | |
8601 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); | |
8602 | } | |
8603 | ||
8604 | static void vmx_dump_dtsel(char *name, uint32_t limit) | |
8605 | { | |
8606 | pr_err("%s limit=0x%08x, base=0x%016lx\n", | |
8607 | name, vmcs_read32(limit), | |
8608 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); | |
8609 | } | |
8610 | ||
8611 | static void dump_vmcs(void) | |
8612 | { | |
8613 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); | |
8614 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); | |
8615 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); | |
8616 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); | |
8617 | u32 secondary_exec_control = 0; | |
8618 | unsigned long cr4 = vmcs_readl(GUEST_CR4); | |
f3531054 | 8619 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
4eb64dce PB |
8620 | int i, n; |
8621 | ||
8622 | if (cpu_has_secondary_exec_ctrls()) | |
8623 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
8624 | ||
8625 | pr_err("*** Guest State ***\n"); | |
8626 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
8627 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), | |
8628 | vmcs_readl(CR0_GUEST_HOST_MASK)); | |
8629 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
8630 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); | |
8631 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); | |
8632 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && | |
8633 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) | |
8634 | { | |
845c5b40 PB |
8635 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
8636 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); | |
8637 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", | |
8638 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); | |
4eb64dce PB |
8639 | } |
8640 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", | |
8641 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); | |
8642 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", | |
8643 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); | |
8644 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
8645 | vmcs_readl(GUEST_SYSENTER_ESP), | |
8646 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); | |
8647 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); | |
8648 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); | |
8649 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); | |
8650 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); | |
8651 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); | |
8652 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); | |
8653 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); | |
8654 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); | |
8655 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); | |
8656 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); | |
8657 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || | |
8658 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) | |
845c5b40 PB |
8659 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
8660 | efer, vmcs_read64(GUEST_IA32_PAT)); | |
8661 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", | |
8662 | vmcs_read64(GUEST_IA32_DEBUGCTL), | |
4eb64dce PB |
8663 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
8664 | if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
845c5b40 PB |
8665 | pr_err("PerfGlobCtl = 0x%016llx\n", |
8666 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce | 8667 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
845c5b40 | 8668 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
4eb64dce PB |
8669 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
8670 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), | |
8671 | vmcs_read32(GUEST_ACTIVITY_STATE)); | |
8672 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) | |
8673 | pr_err("InterruptStatus = %04x\n", | |
8674 | vmcs_read16(GUEST_INTR_STATUS)); | |
8675 | ||
8676 | pr_err("*** Host State ***\n"); | |
8677 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", | |
8678 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); | |
8679 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", | |
8680 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), | |
8681 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), | |
8682 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), | |
8683 | vmcs_read16(HOST_TR_SELECTOR)); | |
8684 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", | |
8685 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), | |
8686 | vmcs_readl(HOST_TR_BASE)); | |
8687 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", | |
8688 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); | |
8689 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", | |
8690 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), | |
8691 | vmcs_readl(HOST_CR4)); | |
8692 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
8693 | vmcs_readl(HOST_IA32_SYSENTER_ESP), | |
8694 | vmcs_read32(HOST_IA32_SYSENTER_CS), | |
8695 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); | |
8696 | if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER)) | |
845c5b40 PB |
8697 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
8698 | vmcs_read64(HOST_IA32_EFER), | |
8699 | vmcs_read64(HOST_IA32_PAT)); | |
4eb64dce | 8700 | if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
845c5b40 PB |
8701 | pr_err("PerfGlobCtl = 0x%016llx\n", |
8702 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce PB |
8703 | |
8704 | pr_err("*** Control State ***\n"); | |
8705 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", | |
8706 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); | |
8707 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); | |
8708 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", | |
8709 | vmcs_read32(EXCEPTION_BITMAP), | |
8710 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), | |
8711 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); | |
8712 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", | |
8713 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), | |
8714 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), | |
8715 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); | |
8716 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", | |
8717 | vmcs_read32(VM_EXIT_INTR_INFO), | |
8718 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
8719 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); | |
8720 | pr_err(" reason=%08x qualification=%016lx\n", | |
8721 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); | |
8722 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", | |
8723 | vmcs_read32(IDT_VECTORING_INFO_FIELD), | |
8724 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); | |
845c5b40 | 8725 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
8cfe9866 | 8726 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
845c5b40 PB |
8727 | pr_err("TSC Multiplier = 0x%016llx\n", |
8728 | vmcs_read64(TSC_MULTIPLIER)); | |
4eb64dce PB |
8729 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
8730 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); | |
8731 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) | |
8732 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); | |
8733 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) | |
845c5b40 | 8734 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
4eb64dce PB |
8735 | n = vmcs_read32(CR3_TARGET_COUNT); |
8736 | for (i = 0; i + 1 < n; i += 4) | |
8737 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", | |
8738 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), | |
8739 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); | |
8740 | if (i < n) | |
8741 | pr_err("CR3 target%u=%016lx\n", | |
8742 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); | |
8743 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) | |
8744 | pr_err("PLE Gap=%08x Window=%08x\n", | |
8745 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); | |
8746 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) | |
8747 | pr_err("Virtual processor ID = 0x%04x\n", | |
8748 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); | |
8749 | } | |
8750 | ||
6aa8b732 AK |
8751 | /* |
8752 | * The guest has exited. See if we can fix it or if we need userspace | |
8753 | * assistance. | |
8754 | */ | |
851ba692 | 8755 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
6aa8b732 | 8756 | { |
29bd8a78 | 8757 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
a0861c02 | 8758 | u32 exit_reason = vmx->exit_reason; |
1155f76a | 8759 | u32 vectoring_info = vmx->idt_vectoring_info; |
29bd8a78 | 8760 | |
8b89fe1f PB |
8761 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
8762 | ||
843e4330 KH |
8763 | /* |
8764 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more | |
8765 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before | |
8766 | * querying dirty_bitmap, we only need to kick all vcpus out of guest | |
8767 | * mode as if vcpus is in root mode, the PML buffer must has been | |
8768 | * flushed already. | |
8769 | */ | |
8770 | if (enable_pml) | |
54bf36aa | 8771 | vmx_flush_pml_buffer(vcpu); |
843e4330 | 8772 | |
80ced186 | 8773 | /* If guest state is invalid, start emulating */ |
14168786 | 8774 | if (vmx->emulation_required) |
80ced186 | 8775 | return handle_invalid_guest_state(vcpu); |
1d5a4d9b | 8776 | |
7313c698 PB |
8777 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
8778 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); | |
644d711a | 8779 | |
5120702e | 8780 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
4eb64dce | 8781 | dump_vmcs(); |
5120702e MG |
8782 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
8783 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
8784 | = exit_reason; | |
8785 | return 0; | |
8786 | } | |
8787 | ||
29bd8a78 | 8788 | if (unlikely(vmx->fail)) { |
851ba692 AK |
8789 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
8790 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
29bd8a78 AK |
8791 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
8792 | return 0; | |
8793 | } | |
6aa8b732 | 8794 | |
b9bf6882 XG |
8795 | /* |
8796 | * Note: | |
8797 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by | |
8798 | * delivery event since it indicates guest is accessing MMIO. | |
8799 | * The vm-exit can be triggered again after return to guest that | |
8800 | * will cause infinite loop. | |
8801 | */ | |
d77c26fc | 8802 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
1439442c | 8803 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
60637aac | 8804 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
b244c9fc | 8805 | exit_reason != EXIT_REASON_PML_FULL && |
b9bf6882 XG |
8806 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
8807 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
8808 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; | |
70bcd708 | 8809 | vcpu->run->internal.ndata = 3; |
b9bf6882 XG |
8810 | vcpu->run->internal.data[0] = vectoring_info; |
8811 | vcpu->run->internal.data[1] = exit_reason; | |
70bcd708 PB |
8812 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
8813 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { | |
8814 | vcpu->run->internal.ndata++; | |
8815 | vcpu->run->internal.data[3] = | |
8816 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); | |
8817 | } | |
b9bf6882 XG |
8818 | return 0; |
8819 | } | |
3b86cd99 | 8820 | |
d02fcf50 | 8821 | if (unlikely(!enable_vnmi && |
8a1b4392 PB |
8822 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
8823 | if (vmx_interrupt_allowed(vcpu)) { | |
8824 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
8825 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && | |
8826 | vcpu->arch.nmi_pending) { | |
8827 | /* | |
8828 | * This CPU don't support us in finding the end of an | |
8829 | * NMI-blocked window if the guest runs with IRQs | |
8830 | * disabled. So we pull the trigger after 1 s of | |
8831 | * futile waiting, but inform the user about this. | |
8832 | */ | |
8833 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " | |
8834 | "state on VCPU %d after 1 s timeout\n", | |
8835 | __func__, vcpu->vcpu_id); | |
8836 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
8837 | } | |
8838 | } | |
8839 | ||
6aa8b732 AK |
8840 | if (exit_reason < kvm_vmx_max_exit_handlers |
8841 | && kvm_vmx_exit_handlers[exit_reason]) | |
851ba692 | 8842 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
6aa8b732 | 8843 | else { |
6c6c5e03 RK |
8844 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
8845 | exit_reason); | |
2bc19dc3 MT |
8846 | kvm_queue_exception(vcpu, UD_VECTOR); |
8847 | return 1; | |
6aa8b732 | 8848 | } |
6aa8b732 AK |
8849 | } |
8850 | ||
95ba8273 | 8851 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
6e5d865c | 8852 | { |
a7c0b07d WL |
8853 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
8854 | ||
8855 | if (is_guest_mode(vcpu) && | |
8856 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
8857 | return; | |
8858 | ||
95ba8273 | 8859 | if (irr == -1 || tpr < irr) { |
6e5d865c YS |
8860 | vmcs_write32(TPR_THRESHOLD, 0); |
8861 | return; | |
8862 | } | |
8863 | ||
95ba8273 | 8864 | vmcs_write32(TPR_THRESHOLD, irr); |
6e5d865c YS |
8865 | } |
8866 | ||
8d14695f YZ |
8867 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
8868 | { | |
8869 | u32 sec_exec_control; | |
8870 | ||
dccbfcf5 RK |
8871 | /* Postpone execution until vmcs01 is the current VMCS. */ |
8872 | if (is_guest_mode(vcpu)) { | |
8873 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; | |
8874 | return; | |
8875 | } | |
8876 | ||
f6e90f9e | 8877 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
8d14695f YZ |
8878 | return; |
8879 | ||
35754c98 | 8880 | if (!cpu_need_tpr_shadow(vcpu)) |
8d14695f YZ |
8881 | return; |
8882 | ||
8883 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
8884 | ||
8885 | if (set) { | |
8886 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
8887 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
8888 | } else { | |
8889 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
8890 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
fb6c8198 | 8891 | vmx_flush_tlb_ept_only(vcpu); |
8d14695f YZ |
8892 | } |
8893 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); | |
8894 | ||
8895 | vmx_set_msr_bitmap(vcpu); | |
8896 | } | |
8897 | ||
38b99173 TC |
8898 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
8899 | { | |
8900 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8901 | ||
8902 | /* | |
8903 | * Currently we do not handle the nested case where L2 has an | |
8904 | * APIC access page of its own; that page is still pinned. | |
8905 | * Hence, we skip the case where the VCPU is in guest mode _and_ | |
8906 | * L1 prepared an APIC access page for L2. | |
8907 | * | |
8908 | * For the case where L1 and L2 share the same APIC access page | |
8909 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear | |
8910 | * in the vmcs12), this function will only update either the vmcs01 | |
8911 | * or the vmcs02. If the former, the vmcs02 will be updated by | |
8912 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in | |
8913 | * the next L2->L1 exit. | |
8914 | */ | |
8915 | if (!is_guest_mode(vcpu) || | |
4f2777bc | 8916 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
fb6c8198 | 8917 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
38b99173 | 8918 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
fb6c8198 JM |
8919 | vmx_flush_tlb_ept_only(vcpu); |
8920 | } | |
38b99173 TC |
8921 | } |
8922 | ||
67c9dddc | 8923 | static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr) |
c7c9c56c YZ |
8924 | { |
8925 | u16 status; | |
8926 | u8 old; | |
8927 | ||
67c9dddc PB |
8928 | if (max_isr == -1) |
8929 | max_isr = 0; | |
c7c9c56c YZ |
8930 | |
8931 | status = vmcs_read16(GUEST_INTR_STATUS); | |
8932 | old = status >> 8; | |
67c9dddc | 8933 | if (max_isr != old) { |
c7c9c56c | 8934 | status &= 0xff; |
67c9dddc | 8935 | status |= max_isr << 8; |
c7c9c56c YZ |
8936 | vmcs_write16(GUEST_INTR_STATUS, status); |
8937 | } | |
8938 | } | |
8939 | ||
8940 | static void vmx_set_rvi(int vector) | |
8941 | { | |
8942 | u16 status; | |
8943 | u8 old; | |
8944 | ||
4114c27d WW |
8945 | if (vector == -1) |
8946 | vector = 0; | |
8947 | ||
c7c9c56c YZ |
8948 | status = vmcs_read16(GUEST_INTR_STATUS); |
8949 | old = (u8)status & 0xff; | |
8950 | if ((u8)vector != old) { | |
8951 | status &= ~0xff; | |
8952 | status |= (u8)vector; | |
8953 | vmcs_write16(GUEST_INTR_STATUS, status); | |
8954 | } | |
8955 | } | |
8956 | ||
8957 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) | |
8958 | { | |
963fee16 | 8959 | /* |
851c1a18 LA |
8960 | * When running L2, updating RVI is only relevant when |
8961 | * vmcs12 virtual-interrupt-delivery enabled. | |
8962 | * However, it can be enabled only when L1 also | |
8963 | * intercepts external-interrupts and in that case | |
8964 | * we should not update vmcs02 RVI but instead intercept | |
8965 | * interrupt. Therefore, do nothing when running L2. | |
963fee16 | 8966 | */ |
851c1a18 LA |
8967 | if (!is_guest_mode(vcpu)) |
8968 | vmx_set_rvi(max_irr); | |
c7c9c56c YZ |
8969 | } |
8970 | ||
76dfafd5 | 8971 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
810e6def PB |
8972 | { |
8973 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
76dfafd5 | 8974 | int max_irr; |
f27a85c4 | 8975 | bool max_irr_updated; |
810e6def | 8976 | |
76dfafd5 PB |
8977 | WARN_ON(!vcpu->arch.apicv_active); |
8978 | if (pi_test_on(&vmx->pi_desc)) { | |
8979 | pi_clear_on(&vmx->pi_desc); | |
8980 | /* | |
8981 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. | |
8982 | * But on x86 this is just a compiler barrier anyway. | |
8983 | */ | |
8984 | smp_mb__after_atomic(); | |
f27a85c4 LA |
8985 | max_irr_updated = |
8986 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); | |
8987 | ||
8988 | /* | |
8989 | * If we are running L2 and L1 has a new pending interrupt | |
8990 | * which can be injected, we should re-evaluate | |
8991 | * what should be done with this new L1 interrupt. | |
851c1a18 LA |
8992 | * If L1 intercepts external-interrupts, we should |
8993 | * exit from L2 to L1. Otherwise, interrupt should be | |
8994 | * delivered directly to L2. | |
f27a85c4 | 8995 | */ |
851c1a18 LA |
8996 | if (is_guest_mode(vcpu) && max_irr_updated) { |
8997 | if (nested_exit_on_intr(vcpu)) | |
8998 | kvm_vcpu_exiting_guest_mode(vcpu); | |
8999 | else | |
9000 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
9001 | } | |
76dfafd5 PB |
9002 | } else { |
9003 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
9004 | } | |
9005 | vmx_hwapic_irr_update(vcpu, max_irr); | |
9006 | return max_irr; | |
810e6def PB |
9007 | } |
9008 | ||
6308630b | 9009 | static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) |
c7c9c56c | 9010 | { |
d62caabb | 9011 | if (!kvm_vcpu_apicv_active(vcpu)) |
3d81bc7e YZ |
9012 | return; |
9013 | ||
c7c9c56c YZ |
9014 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
9015 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); | |
9016 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); | |
9017 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); | |
9018 | } | |
9019 | ||
967235d3 PB |
9020 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
9021 | { | |
9022 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9023 | ||
9024 | pi_clear_on(&vmx->pi_desc); | |
9025 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); | |
9026 | } | |
9027 | ||
51aa01d1 | 9028 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
cf393f75 | 9029 | { |
48ae0fb4 JM |
9030 | u32 exit_intr_info = 0; |
9031 | u16 basic_exit_reason = (u16)vmx->exit_reason; | |
00eba012 | 9032 | |
48ae0fb4 JM |
9033 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
9034 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) | |
00eba012 AK |
9035 | return; |
9036 | ||
48ae0fb4 JM |
9037 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
9038 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
9039 | vmx->exit_intr_info = exit_intr_info; | |
a0861c02 | 9040 | |
1261bfa3 WL |
9041 | /* if exit due to PF check for async PF */ |
9042 | if (is_page_fault(exit_intr_info)) | |
9043 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); | |
9044 | ||
a0861c02 | 9045 | /* Handle machine checks before interrupts are enabled */ |
48ae0fb4 JM |
9046 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
9047 | is_machine_check(exit_intr_info)) | |
a0861c02 AK |
9048 | kvm_machine_check(); |
9049 | ||
20f65983 | 9050 | /* We need to handle NMIs before interrupts are enabled */ |
ef85b673 | 9051 | if (is_nmi(exit_intr_info)) { |
ff9d07a0 | 9052 | kvm_before_handle_nmi(&vmx->vcpu); |
20f65983 | 9053 | asm("int $2"); |
ff9d07a0 ZY |
9054 | kvm_after_handle_nmi(&vmx->vcpu); |
9055 | } | |
51aa01d1 | 9056 | } |
20f65983 | 9057 | |
a547c6db YZ |
9058 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
9059 | { | |
9060 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
9061 | ||
a547c6db YZ |
9062 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
9063 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { | |
9064 | unsigned int vector; | |
9065 | unsigned long entry; | |
9066 | gate_desc *desc; | |
9067 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9068 | #ifdef CONFIG_X86_64 | |
9069 | unsigned long tmp; | |
9070 | #endif | |
9071 | ||
9072 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
9073 | desc = (gate_desc *)vmx->host_idt_base + vector; | |
64b163fa | 9074 | entry = gate_offset(desc); |
a547c6db YZ |
9075 | asm volatile( |
9076 | #ifdef CONFIG_X86_64 | |
9077 | "mov %%" _ASM_SP ", %[sp]\n\t" | |
9078 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" | |
9079 | "push $%c[ss]\n\t" | |
9080 | "push %[sp]\n\t" | |
9081 | #endif | |
9082 | "pushf\n\t" | |
a547c6db YZ |
9083 | __ASM_SIZE(push) " $%c[cs]\n\t" |
9084 | "call *%[entry]\n\t" | |
9085 | : | |
9086 | #ifdef CONFIG_X86_64 | |
3f62de5f | 9087 | [sp]"=&r"(tmp), |
a547c6db | 9088 | #endif |
f5caf621 | 9089 | ASM_CALL_CONSTRAINT |
a547c6db YZ |
9090 | : |
9091 | [entry]"r"(entry), | |
9092 | [ss]"i"(__KERNEL_DS), | |
9093 | [cs]"i"(__KERNEL_CS) | |
9094 | ); | |
f2485b3e | 9095 | } |
a547c6db | 9096 | } |
c207aee4 | 9097 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
a547c6db | 9098 | |
6d396b55 PB |
9099 | static bool vmx_has_high_real_mode_segbase(void) |
9100 | { | |
9101 | return enable_unrestricted_guest || emulate_invalid_guest_state; | |
9102 | } | |
9103 | ||
da8999d3 LJ |
9104 | static bool vmx_mpx_supported(void) |
9105 | { | |
9106 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && | |
9107 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); | |
9108 | } | |
9109 | ||
55412b2e WL |
9110 | static bool vmx_xsaves_supported(void) |
9111 | { | |
9112 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
9113 | SECONDARY_EXEC_XSAVES; | |
9114 | } | |
9115 | ||
66336cab PB |
9116 | static bool vmx_umip_emulated(void) |
9117 | { | |
0367f205 PB |
9118 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
9119 | SECONDARY_EXEC_DESC; | |
66336cab PB |
9120 | } |
9121 | ||
51aa01d1 AK |
9122 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
9123 | { | |
c5ca8e57 | 9124 | u32 exit_intr_info; |
51aa01d1 AK |
9125 | bool unblock_nmi; |
9126 | u8 vector; | |
9127 | bool idtv_info_valid; | |
9128 | ||
9129 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
20f65983 | 9130 | |
d02fcf50 | 9131 | if (enable_vnmi) { |
8a1b4392 PB |
9132 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
9133 | return; | |
9134 | /* | |
9135 | * Can't use vmx->exit_intr_info since we're not sure what | |
9136 | * the exit reason is. | |
9137 | */ | |
9138 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
9139 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; | |
9140 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
9141 | /* | |
9142 | * SDM 3: 27.7.1.2 (September 2008) | |
9143 | * Re-set bit "block by NMI" before VM entry if vmexit caused by | |
9144 | * a guest IRET fault. | |
9145 | * SDM 3: 23.2.2 (September 2008) | |
9146 | * Bit 12 is undefined in any of the following cases: | |
9147 | * If the VM exit sets the valid bit in the IDT-vectoring | |
9148 | * information field. | |
9149 | * If the VM exit is due to a double fault. | |
9150 | */ | |
9151 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && | |
9152 | vector != DF_VECTOR && !idtv_info_valid) | |
9153 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
9154 | GUEST_INTR_STATE_NMI); | |
9155 | else | |
9156 | vmx->loaded_vmcs->nmi_known_unmasked = | |
9157 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) | |
9158 | & GUEST_INTR_STATE_NMI); | |
9159 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) | |
9160 | vmx->loaded_vmcs->vnmi_blocked_time += | |
9161 | ktime_to_ns(ktime_sub(ktime_get(), | |
9162 | vmx->loaded_vmcs->entry_time)); | |
51aa01d1 AK |
9163 | } |
9164 | ||
3ab66e8a | 9165 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
83422e17 AK |
9166 | u32 idt_vectoring_info, |
9167 | int instr_len_field, | |
9168 | int error_code_field) | |
51aa01d1 | 9169 | { |
51aa01d1 AK |
9170 | u8 vector; |
9171 | int type; | |
9172 | bool idtv_info_valid; | |
9173 | ||
9174 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
668f612f | 9175 | |
3ab66e8a JK |
9176 | vcpu->arch.nmi_injected = false; |
9177 | kvm_clear_exception_queue(vcpu); | |
9178 | kvm_clear_interrupt_queue(vcpu); | |
37b96e98 GN |
9179 | |
9180 | if (!idtv_info_valid) | |
9181 | return; | |
9182 | ||
3ab66e8a | 9183 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
3842d135 | 9184 | |
668f612f AK |
9185 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
9186 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; | |
37b96e98 | 9187 | |
64a7ec06 | 9188 | switch (type) { |
37b96e98 | 9189 | case INTR_TYPE_NMI_INTR: |
3ab66e8a | 9190 | vcpu->arch.nmi_injected = true; |
668f612f | 9191 | /* |
7b4a25cb | 9192 | * SDM 3: 27.7.1.2 (September 2008) |
37b96e98 GN |
9193 | * Clear bit "block by NMI" before VM entry if a NMI |
9194 | * delivery faulted. | |
668f612f | 9195 | */ |
3ab66e8a | 9196 | vmx_set_nmi_mask(vcpu, false); |
37b96e98 | 9197 | break; |
37b96e98 | 9198 | case INTR_TYPE_SOFT_EXCEPTION: |
3ab66e8a | 9199 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f GN |
9200 | /* fall through */ |
9201 | case INTR_TYPE_HARD_EXCEPTION: | |
35920a35 | 9202 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
83422e17 | 9203 | u32 err = vmcs_read32(error_code_field); |
851eb667 | 9204 | kvm_requeue_exception_e(vcpu, vector, err); |
35920a35 | 9205 | } else |
851eb667 | 9206 | kvm_requeue_exception(vcpu, vector); |
37b96e98 | 9207 | break; |
66fd3f7f | 9208 | case INTR_TYPE_SOFT_INTR: |
3ab66e8a | 9209 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f | 9210 | /* fall through */ |
37b96e98 | 9211 | case INTR_TYPE_EXT_INTR: |
3ab66e8a | 9212 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
37b96e98 GN |
9213 | break; |
9214 | default: | |
9215 | break; | |
f7d9238f | 9216 | } |
cf393f75 AK |
9217 | } |
9218 | ||
83422e17 AK |
9219 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
9220 | { | |
3ab66e8a | 9221 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
83422e17 AK |
9222 | VM_EXIT_INSTRUCTION_LEN, |
9223 | IDT_VECTORING_ERROR_CODE); | |
9224 | } | |
9225 | ||
b463a6f7 AK |
9226 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
9227 | { | |
3ab66e8a | 9228 | __vmx_complete_interrupts(vcpu, |
b463a6f7 AK |
9229 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
9230 | VM_ENTRY_INSTRUCTION_LEN, | |
9231 | VM_ENTRY_EXCEPTION_ERROR_CODE); | |
9232 | ||
9233 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
9234 | } | |
9235 | ||
d7cd9796 GN |
9236 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
9237 | { | |
9238 | int i, nr_msrs; | |
9239 | struct perf_guest_switch_msr *msrs; | |
9240 | ||
9241 | msrs = perf_guest_get_msrs(&nr_msrs); | |
9242 | ||
9243 | if (!msrs) | |
9244 | return; | |
9245 | ||
9246 | for (i = 0; i < nr_msrs; i++) | |
9247 | if (msrs[i].host == msrs[i].guest) | |
9248 | clear_atomic_switch_msr(vmx, msrs[i].msr); | |
9249 | else | |
9250 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, | |
9251 | msrs[i].host); | |
9252 | } | |
9253 | ||
33365e7a | 9254 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
64672c95 YJ |
9255 | { |
9256 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9257 | u64 tscl; | |
9258 | u32 delta_tsc; | |
9259 | ||
9260 | if (vmx->hv_deadline_tsc == -1) | |
9261 | return; | |
9262 | ||
9263 | tscl = rdtsc(); | |
9264 | if (vmx->hv_deadline_tsc > tscl) | |
9265 | /* sure to be 32 bit only because checked on set_hv_timer */ | |
9266 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> | |
9267 | cpu_preemption_timer_multi); | |
9268 | else | |
9269 | delta_tsc = 0; | |
9270 | ||
9271 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); | |
9272 | } | |
9273 | ||
a3b5ba49 | 9274 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
6aa8b732 | 9275 | { |
a2fa3e9f | 9276 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
74c55931 | 9277 | unsigned long cr3, cr4; |
104f226b | 9278 | |
8a1b4392 | 9279 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
d02fcf50 | 9280 | if (unlikely(!enable_vnmi && |
8a1b4392 PB |
9281 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
9282 | vmx->loaded_vmcs->entry_time = ktime_get(); | |
9283 | ||
104f226b AK |
9284 | /* Don't enter VMX if guest state is invalid, let the exit handler |
9285 | start emulation until we arrive back to a valid state */ | |
14168786 | 9286 | if (vmx->emulation_required) |
104f226b AK |
9287 | return; |
9288 | ||
a7653ecd RK |
9289 | if (vmx->ple_window_dirty) { |
9290 | vmx->ple_window_dirty = false; | |
9291 | vmcs_write32(PLE_WINDOW, vmx->ple_window); | |
9292 | } | |
9293 | ||
012f83cb AG |
9294 | if (vmx->nested.sync_shadow_vmcs) { |
9295 | copy_vmcs12_to_shadow(vmx); | |
9296 | vmx->nested.sync_shadow_vmcs = false; | |
9297 | } | |
9298 | ||
104f226b AK |
9299 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
9300 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); | |
9301 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) | |
9302 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); | |
9303 | ||
d6e41f11 | 9304 | cr3 = __get_current_cr3_fast(); |
44889942 | 9305 | if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) { |
d6e41f11 | 9306 | vmcs_writel(HOST_CR3, cr3); |
44889942 | 9307 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
d6e41f11 AL |
9308 | } |
9309 | ||
1e02ce4c | 9310 | cr4 = cr4_read_shadow(); |
44889942 | 9311 | if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) { |
d974baa3 | 9312 | vmcs_writel(HOST_CR4, cr4); |
44889942 | 9313 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
d974baa3 AL |
9314 | } |
9315 | ||
104f226b AK |
9316 | /* When single-stepping over STI and MOV SS, we must clear the |
9317 | * corresponding interruptibility bits in the guest state. Otherwise | |
9318 | * vmentry fails as it then expects bit 14 (BS) in pending debug | |
9319 | * exceptions being set, but that's not correct for the guest debugging | |
9320 | * case. */ | |
9321 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) | |
9322 | vmx_set_interrupt_shadow(vcpu, 0); | |
9323 | ||
b9dd21e1 PB |
9324 | if (static_cpu_has(X86_FEATURE_PKU) && |
9325 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && | |
9326 | vcpu->arch.pkru != vmx->host_pkru) | |
9327 | __write_pkru(vcpu->arch.pkru); | |
1be0e61c | 9328 | |
d7cd9796 GN |
9329 | atomic_switch_perf_msrs(vmx); |
9330 | ||
64672c95 YJ |
9331 | vmx_arm_hv_timer(vcpu); |
9332 | ||
d462b819 | 9333 | vmx->__launched = vmx->loaded_vmcs->launched; |
104f226b | 9334 | asm( |
6aa8b732 | 9335 | /* Store host registers */ |
b188c81f AK |
9336 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
9337 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ | |
9338 | "push %%" _ASM_CX " \n\t" | |
9339 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" | |
313dbd49 | 9340 | "je 1f \n\t" |
b188c81f | 9341 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
4ecac3fd | 9342 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
313dbd49 | 9343 | "1: \n\t" |
d3edefc0 | 9344 | /* Reload cr2 if changed */ |
b188c81f AK |
9345 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
9346 | "mov %%cr2, %%" _ASM_DX " \n\t" | |
9347 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" | |
d3edefc0 | 9348 | "je 2f \n\t" |
b188c81f | 9349 | "mov %%" _ASM_AX", %%cr2 \n\t" |
d3edefc0 | 9350 | "2: \n\t" |
6aa8b732 | 9351 | /* Check if vmlaunch of vmresume is needed */ |
e08aa78a | 9352 | "cmpl $0, %c[launched](%0) \n\t" |
6aa8b732 | 9353 | /* Load guest registers. Don't clobber flags. */ |
b188c81f AK |
9354 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
9355 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" | |
9356 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" | |
9357 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" | |
9358 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" | |
9359 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" | |
05b3e0c2 | 9360 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
9361 | "mov %c[r8](%0), %%r8 \n\t" |
9362 | "mov %c[r9](%0), %%r9 \n\t" | |
9363 | "mov %c[r10](%0), %%r10 \n\t" | |
9364 | "mov %c[r11](%0), %%r11 \n\t" | |
9365 | "mov %c[r12](%0), %%r12 \n\t" | |
9366 | "mov %c[r13](%0), %%r13 \n\t" | |
9367 | "mov %c[r14](%0), %%r14 \n\t" | |
9368 | "mov %c[r15](%0), %%r15 \n\t" | |
6aa8b732 | 9369 | #endif |
b188c81f | 9370 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
c801949d | 9371 | |
6aa8b732 | 9372 | /* Enter guest mode */ |
83287ea4 | 9373 | "jne 1f \n\t" |
4ecac3fd | 9374 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
83287ea4 AK |
9375 | "jmp 2f \n\t" |
9376 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" | |
9377 | "2: " | |
6aa8b732 | 9378 | /* Save guest registers, load host registers, keep flags */ |
b188c81f | 9379 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
40712fae | 9380 | "pop %0 \n\t" |
b188c81f AK |
9381 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
9382 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" | |
9383 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" | |
9384 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" | |
9385 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" | |
9386 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" | |
9387 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" | |
05b3e0c2 | 9388 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
9389 | "mov %%r8, %c[r8](%0) \n\t" |
9390 | "mov %%r9, %c[r9](%0) \n\t" | |
9391 | "mov %%r10, %c[r10](%0) \n\t" | |
9392 | "mov %%r11, %c[r11](%0) \n\t" | |
9393 | "mov %%r12, %c[r12](%0) \n\t" | |
9394 | "mov %%r13, %c[r13](%0) \n\t" | |
9395 | "mov %%r14, %c[r14](%0) \n\t" | |
9396 | "mov %%r15, %c[r15](%0) \n\t" | |
6aa8b732 | 9397 | #endif |
b188c81f AK |
9398 | "mov %%cr2, %%" _ASM_AX " \n\t" |
9399 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" | |
c801949d | 9400 | |
b188c81f | 9401 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
e08aa78a | 9402 | "setbe %c[fail](%0) \n\t" |
83287ea4 AK |
9403 | ".pushsection .rodata \n\t" |
9404 | ".global vmx_return \n\t" | |
9405 | "vmx_return: " _ASM_PTR " 2b \n\t" | |
9406 | ".popsection" | |
e08aa78a | 9407 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
d462b819 | 9408 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
e08aa78a | 9409 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
313dbd49 | 9410 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
ad312c7c ZX |
9411 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
9412 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), | |
9413 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), | |
9414 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), | |
9415 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), | |
9416 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), | |
9417 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), | |
05b3e0c2 | 9418 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
9419 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
9420 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), | |
9421 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), | |
9422 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), | |
9423 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), | |
9424 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), | |
9425 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), | |
9426 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), | |
6aa8b732 | 9427 | #endif |
40712fae AK |
9428 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
9429 | [wordsize]"i"(sizeof(ulong)) | |
c2036300 LV |
9430 | : "cc", "memory" |
9431 | #ifdef CONFIG_X86_64 | |
b188c81f | 9432 | , "rax", "rbx", "rdi", "rsi" |
c2036300 | 9433 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
b188c81f AK |
9434 | #else |
9435 | , "eax", "ebx", "edi", "esi" | |
c2036300 LV |
9436 | #endif |
9437 | ); | |
6aa8b732 | 9438 | |
2a7921b7 | 9439 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
74c55931 WL |
9440 | if (vmx->host_debugctlmsr) |
9441 | update_debugctlmsr(vmx->host_debugctlmsr); | |
2a7921b7 | 9442 | |
aa67f609 AK |
9443 | #ifndef CONFIG_X86_64 |
9444 | /* | |
9445 | * The sysexit path does not restore ds/es, so we must set them to | |
9446 | * a reasonable value ourselves. | |
9447 | * | |
9448 | * We can't defer this to vmx_load_host_state() since that function | |
9449 | * may be executed in interrupt context, which saves and restore segments | |
9450 | * around it, nullifying its effect. | |
9451 | */ | |
9452 | loadsegment(ds, __USER_DS); | |
9453 | loadsegment(es, __USER_DS); | |
9454 | #endif | |
9455 | ||
6de4f3ad | 9456 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
6de12732 | 9457 | | (1 << VCPU_EXREG_RFLAGS) |
aff48baa | 9458 | | (1 << VCPU_EXREG_PDPTR) |
2fb92db1 | 9459 | | (1 << VCPU_EXREG_SEGMENTS) |
aff48baa | 9460 | | (1 << VCPU_EXREG_CR3)); |
5fdbf976 MT |
9461 | vcpu->arch.regs_dirty = 0; |
9462 | ||
1be0e61c XG |
9463 | /* |
9464 | * eager fpu is enabled if PKEY is supported and CR4 is switched | |
9465 | * back on host, so it is safe to read guest PKRU from current | |
9466 | * XSAVE. | |
9467 | */ | |
b9dd21e1 PB |
9468 | if (static_cpu_has(X86_FEATURE_PKU) && |
9469 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { | |
9470 | vcpu->arch.pkru = __read_pkru(); | |
9471 | if (vcpu->arch.pkru != vmx->host_pkru) | |
1be0e61c | 9472 | __write_pkru(vmx->host_pkru); |
1be0e61c XG |
9473 | } |
9474 | ||
e0b890d3 GN |
9475 | /* |
9476 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if | |
9477 | * we did not inject a still-pending event to L1 now because of | |
9478 | * nested_run_pending, we need to re-enable this bit. | |
9479 | */ | |
9480 | if (vmx->nested.nested_run_pending) | |
9481 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
9482 | ||
9483 | vmx->nested.nested_run_pending = 0; | |
b060ca3b JM |
9484 | vmx->idt_vectoring_info = 0; |
9485 | ||
9486 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); | |
9487 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) | |
9488 | return; | |
9489 | ||
9490 | vmx->loaded_vmcs->launched = 1; | |
9491 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); | |
e0b890d3 | 9492 | |
51aa01d1 AK |
9493 | vmx_complete_atomic_exit(vmx); |
9494 | vmx_recover_nmi_blocking(vmx); | |
cf393f75 | 9495 | vmx_complete_interrupts(vmx); |
6aa8b732 | 9496 | } |
c207aee4 | 9497 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
6aa8b732 | 9498 | |
1279a6b1 | 9499 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
4fa7734c PB |
9500 | { |
9501 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9502 | int cpu; | |
9503 | ||
1279a6b1 | 9504 | if (vmx->loaded_vmcs == vmcs) |
4fa7734c PB |
9505 | return; |
9506 | ||
9507 | cpu = get_cpu(); | |
1279a6b1 | 9508 | vmx->loaded_vmcs = vmcs; |
4fa7734c PB |
9509 | vmx_vcpu_put(vcpu); |
9510 | vmx_vcpu_load(vcpu, cpu); | |
4fa7734c PB |
9511 | put_cpu(); |
9512 | } | |
9513 | ||
2f1fe811 JM |
9514 | /* |
9515 | * Ensure that the current vmcs of the logical processor is the | |
9516 | * vmcs01 of the vcpu before calling free_nested(). | |
9517 | */ | |
9518 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) | |
9519 | { | |
9520 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
2f1fe811 | 9521 | |
ec7660cc | 9522 | vcpu_load(vcpu); |
1279a6b1 | 9523 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
2f1fe811 JM |
9524 | free_nested(vmx); |
9525 | vcpu_put(vcpu); | |
9526 | } | |
9527 | ||
6aa8b732 AK |
9528 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
9529 | { | |
fb3f0f51 RR |
9530 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
9531 | ||
843e4330 | 9532 | if (enable_pml) |
a3eaa864 | 9533 | vmx_destroy_pml_buffer(vmx); |
991e7a0e | 9534 | free_vpid(vmx->vpid); |
4fa7734c | 9535 | leave_guest_mode(vcpu); |
2f1fe811 | 9536 | vmx_free_vcpu_nested(vcpu); |
4fa7734c | 9537 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 RR |
9538 | kfree(vmx->guest_msrs); |
9539 | kvm_vcpu_uninit(vcpu); | |
a4770347 | 9540 | kmem_cache_free(kvm_vcpu_cache, vmx); |
6aa8b732 AK |
9541 | } |
9542 | ||
fb3f0f51 | 9543 | static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) |
6aa8b732 | 9544 | { |
fb3f0f51 | 9545 | int err; |
c16f862d | 9546 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
15ad7146 | 9547 | int cpu; |
6aa8b732 | 9548 | |
a2fa3e9f | 9549 | if (!vmx) |
fb3f0f51 RR |
9550 | return ERR_PTR(-ENOMEM); |
9551 | ||
991e7a0e | 9552 | vmx->vpid = allocate_vpid(); |
2384d2b3 | 9553 | |
fb3f0f51 RR |
9554 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
9555 | if (err) | |
9556 | goto free_vcpu; | |
965b58a5 | 9557 | |
4e59516a PF |
9558 | err = -ENOMEM; |
9559 | ||
9560 | /* | |
9561 | * If PML is turned on, failure on enabling PML just results in failure | |
9562 | * of creating the vcpu, therefore we can simplify PML logic (by | |
9563 | * avoiding dealing with cases, such as enabling PML partially on vcpus | |
9564 | * for the guest, etc. | |
9565 | */ | |
9566 | if (enable_pml) { | |
9567 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
9568 | if (!vmx->pml_pg) | |
9569 | goto uninit_vcpu; | |
9570 | } | |
9571 | ||
a2fa3e9f | 9572 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
03916db9 PB |
9573 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
9574 | > PAGE_SIZE); | |
0123be42 | 9575 | |
4e59516a PF |
9576 | if (!vmx->guest_msrs) |
9577 | goto free_pml; | |
965b58a5 | 9578 | |
d462b819 NHE |
9579 | vmx->loaded_vmcs = &vmx->vmcs01; |
9580 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); | |
355f4fb1 | 9581 | vmx->loaded_vmcs->shadow_vmcs = NULL; |
d462b819 | 9582 | if (!vmx->loaded_vmcs->vmcs) |
fb3f0f51 | 9583 | goto free_msrs; |
d462b819 | 9584 | loaded_vmcs_init(vmx->loaded_vmcs); |
a2fa3e9f | 9585 | |
15ad7146 AK |
9586 | cpu = get_cpu(); |
9587 | vmx_vcpu_load(&vmx->vcpu, cpu); | |
e48672fa | 9588 | vmx->vcpu.cpu = cpu; |
12d79917 | 9589 | vmx_vcpu_setup(vmx); |
fb3f0f51 | 9590 | vmx_vcpu_put(&vmx->vcpu); |
15ad7146 | 9591 | put_cpu(); |
35754c98 | 9592 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
be6d05cf JK |
9593 | err = alloc_apic_access_page(kvm); |
9594 | if (err) | |
5e4a0b3c | 9595 | goto free_vmcs; |
a63cb560 | 9596 | } |
fb3f0f51 | 9597 | |
b927a3ce | 9598 | if (enable_ept) { |
f51770ed TC |
9599 | err = init_rmode_identity_map(kvm); |
9600 | if (err) | |
93ea5388 | 9601 | goto free_vmcs; |
b927a3ce | 9602 | } |
b7ebfb05 | 9603 | |
5c614b35 | 9604 | if (nested) { |
b9c237bb | 9605 | nested_vmx_setup_ctls_msrs(vmx); |
5c614b35 WL |
9606 | vmx->nested.vpid02 = allocate_vpid(); |
9607 | } | |
b9c237bb | 9608 | |
705699a1 | 9609 | vmx->nested.posted_intr_nv = -1; |
a9d30f33 | 9610 | vmx->nested.current_vmptr = -1ull; |
a9d30f33 | 9611 | |
37e4c997 HZ |
9612 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
9613 | ||
31afb2ea PB |
9614 | /* |
9615 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR | |
9616 | * or POSTED_INTR_WAKEUP_VECTOR. | |
9617 | */ | |
9618 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; | |
9619 | vmx->pi_desc.sn = 1; | |
9620 | ||
fb3f0f51 RR |
9621 | return &vmx->vcpu; |
9622 | ||
9623 | free_vmcs: | |
5c614b35 | 9624 | free_vpid(vmx->nested.vpid02); |
5f3fbc34 | 9625 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 | 9626 | free_msrs: |
fb3f0f51 | 9627 | kfree(vmx->guest_msrs); |
4e59516a PF |
9628 | free_pml: |
9629 | vmx_destroy_pml_buffer(vmx); | |
fb3f0f51 RR |
9630 | uninit_vcpu: |
9631 | kvm_vcpu_uninit(&vmx->vcpu); | |
9632 | free_vcpu: | |
991e7a0e | 9633 | free_vpid(vmx->vpid); |
a4770347 | 9634 | kmem_cache_free(kvm_vcpu_cache, vmx); |
fb3f0f51 | 9635 | return ERR_PTR(err); |
6aa8b732 AK |
9636 | } |
9637 | ||
002c7f7c YS |
9638 | static void __init vmx_check_processor_compat(void *rtn) |
9639 | { | |
9640 | struct vmcs_config vmcs_conf; | |
9641 | ||
9642 | *(int *)rtn = 0; | |
9643 | if (setup_vmcs_config(&vmcs_conf) < 0) | |
9644 | *(int *)rtn = -EIO; | |
9645 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { | |
9646 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", | |
9647 | smp_processor_id()); | |
9648 | *(int *)rtn = -EIO; | |
9649 | } | |
9650 | } | |
9651 | ||
4b12f0de | 9652 | static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
64d4d521 | 9653 | { |
b18d5431 XG |
9654 | u8 cache; |
9655 | u64 ipat = 0; | |
4b12f0de | 9656 | |
522c68c4 | 9657 | /* For VT-d and EPT combination |
606decd6 | 9658 | * 1. MMIO: always map as UC |
522c68c4 SY |
9659 | * 2. EPT with VT-d: |
9660 | * a. VT-d without snooping control feature: can't guarantee the | |
606decd6 | 9661 | * result, try to trust guest. |
522c68c4 SY |
9662 | * b. VT-d with snooping control feature: snooping control feature of |
9663 | * VT-d engine can guarantee the cache correctness. Just set it | |
9664 | * to WB to keep consistent with host. So the same as item 3. | |
a19a6d11 | 9665 | * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep |
522c68c4 SY |
9666 | * consistent with host MTRR |
9667 | */ | |
606decd6 PB |
9668 | if (is_mmio) { |
9669 | cache = MTRR_TYPE_UNCACHABLE; | |
9670 | goto exit; | |
9671 | } | |
9672 | ||
9673 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { | |
b18d5431 XG |
9674 | ipat = VMX_EPT_IPAT_BIT; |
9675 | cache = MTRR_TYPE_WRBACK; | |
9676 | goto exit; | |
9677 | } | |
9678 | ||
9679 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { | |
9680 | ipat = VMX_EPT_IPAT_BIT; | |
0da029ed | 9681 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
fb279950 XG |
9682 | cache = MTRR_TYPE_WRBACK; |
9683 | else | |
9684 | cache = MTRR_TYPE_UNCACHABLE; | |
b18d5431 XG |
9685 | goto exit; |
9686 | } | |
9687 | ||
ff53604b | 9688 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
b18d5431 XG |
9689 | |
9690 | exit: | |
9691 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; | |
64d4d521 SY |
9692 | } |
9693 | ||
17cc3935 | 9694 | static int vmx_get_lpage_level(void) |
344f414f | 9695 | { |
878403b7 SY |
9696 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
9697 | return PT_DIRECTORY_LEVEL; | |
9698 | else | |
9699 | /* For shadow and EPT supported 1GB page */ | |
9700 | return PT_PDPE_LEVEL; | |
344f414f JR |
9701 | } |
9702 | ||
feda805f XG |
9703 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
9704 | { | |
9705 | /* | |
9706 | * These bits in the secondary execution controls field | |
9707 | * are dynamic, the others are mostly based on the hypervisor | |
9708 | * architecture and the guest's CPUID. Do not touch the | |
9709 | * dynamic bits. | |
9710 | */ | |
9711 | u32 mask = | |
9712 | SECONDARY_EXEC_SHADOW_VMCS | | |
9713 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | | |
0367f205 PB |
9714 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
9715 | SECONDARY_EXEC_DESC; | |
feda805f XG |
9716 | |
9717 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
9718 | ||
9719 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, | |
9720 | (new_ctl & ~mask) | (cur_ctl & mask)); | |
9721 | } | |
9722 | ||
8322ebbb DM |
9723 | /* |
9724 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits | |
9725 | * (indicating "allowed-1") if they are supported in the guest's CPUID. | |
9726 | */ | |
9727 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) | |
9728 | { | |
9729 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9730 | struct kvm_cpuid_entry2 *entry; | |
9731 | ||
9732 | vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff; | |
9733 | vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE; | |
9734 | ||
9735 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ | |
9736 | if (entry && (entry->_reg & (_cpuid_mask))) \ | |
9737 | vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \ | |
9738 | } while (0) | |
9739 | ||
9740 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); | |
9741 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); | |
9742 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); | |
9743 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); | |
9744 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); | |
9745 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); | |
9746 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); | |
9747 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); | |
9748 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); | |
9749 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); | |
9750 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); | |
9751 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); | |
9752 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); | |
9753 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); | |
9754 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); | |
9755 | ||
9756 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); | |
9757 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); | |
9758 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); | |
9759 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); | |
9760 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); | |
c4ad77e0 | 9761 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
8322ebbb DM |
9762 | |
9763 | #undef cr4_fixed1_update | |
9764 | } | |
9765 | ||
0e851880 SY |
9766 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
9767 | { | |
4e47c7a6 | 9768 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
4e47c7a6 | 9769 | |
80154d77 PB |
9770 | if (cpu_has_secondary_exec_ctrls()) { |
9771 | vmx_compute_secondary_exec_control(vmx); | |
9772 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); | |
ad756a16 | 9773 | } |
8b3e34e4 | 9774 | |
37e4c997 HZ |
9775 | if (nested_vmx_allowed(vcpu)) |
9776 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
9777 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
9778 | else | |
9779 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
9780 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
8322ebbb DM |
9781 | |
9782 | if (nested_vmx_allowed(vcpu)) | |
9783 | nested_vmx_cr_fixed1_bits_update(vcpu); | |
0e851880 SY |
9784 | } |
9785 | ||
d4330ef2 JR |
9786 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
9787 | { | |
7b8050f5 NHE |
9788 | if (func == 1 && nested) |
9789 | entry->ecx |= bit(X86_FEATURE_VMX); | |
d4330ef2 JR |
9790 | } |
9791 | ||
25d92081 YZ |
9792 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
9793 | struct x86_exception *fault) | |
9794 | { | |
533558bc | 9795 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
c5f983f6 | 9796 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
533558bc | 9797 | u32 exit_reason; |
c5f983f6 | 9798 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
25d92081 | 9799 | |
c5f983f6 BD |
9800 | if (vmx->nested.pml_full) { |
9801 | exit_reason = EXIT_REASON_PML_FULL; | |
9802 | vmx->nested.pml_full = false; | |
9803 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; | |
9804 | } else if (fault->error_code & PFERR_RSVD_MASK) | |
533558bc | 9805 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
25d92081 | 9806 | else |
533558bc | 9807 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
c5f983f6 BD |
9808 | |
9809 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); | |
25d92081 YZ |
9810 | vmcs12->guest_physical_address = fault->address; |
9811 | } | |
9812 | ||
995f00a6 PF |
9813 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
9814 | { | |
bb97a016 | 9815 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
995f00a6 PF |
9816 | } |
9817 | ||
155a97a3 NHE |
9818 | /* Callbacks for nested_ept_init_mmu_context: */ |
9819 | ||
9820 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) | |
9821 | { | |
9822 | /* return the page table to be shadowed - in our case, EPT12 */ | |
9823 | return get_vmcs12(vcpu)->ept_pointer; | |
9824 | } | |
9825 | ||
ae1e2d10 | 9826 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
155a97a3 | 9827 | { |
ad896af0 | 9828 | WARN_ON(mmu_is_nested(vcpu)); |
a057e0e2 | 9829 | if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu))) |
ae1e2d10 PB |
9830 | return 1; |
9831 | ||
9832 | kvm_mmu_unload(vcpu); | |
ad896af0 | 9833 | kvm_init_shadow_ept_mmu(vcpu, |
b9c237bb | 9834 | to_vmx(vcpu)->nested.nested_vmx_ept_caps & |
ae1e2d10 | 9835 | VMX_EPT_EXECUTE_ONLY_BIT, |
a057e0e2 | 9836 | nested_ept_ad_enabled(vcpu)); |
155a97a3 NHE |
9837 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
9838 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; | |
9839 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; | |
9840 | ||
9841 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; | |
ae1e2d10 | 9842 | return 0; |
155a97a3 NHE |
9843 | } |
9844 | ||
9845 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) | |
9846 | { | |
9847 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; | |
9848 | } | |
9849 | ||
19d5f10b EK |
9850 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
9851 | u16 error_code) | |
9852 | { | |
9853 | bool inequality, bit; | |
9854 | ||
9855 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; | |
9856 | inequality = | |
9857 | (error_code & vmcs12->page_fault_error_code_mask) != | |
9858 | vmcs12->page_fault_error_code_match; | |
9859 | return inequality ^ bit; | |
9860 | } | |
9861 | ||
feaf0c7d GN |
9862 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
9863 | struct x86_exception *fault) | |
9864 | { | |
9865 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
9866 | ||
9867 | WARN_ON(!is_guest_mode(vcpu)); | |
9868 | ||
305d0ab4 WL |
9869 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
9870 | !to_vmx(vcpu)->nested.nested_run_pending) { | |
b96fb439 PB |
9871 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
9872 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, | |
9873 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | | |
9874 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, | |
9875 | fault->address); | |
7313c698 | 9876 | } else { |
feaf0c7d | 9877 | kvm_inject_page_fault(vcpu, fault); |
7313c698 | 9878 | } |
feaf0c7d GN |
9879 | } |
9880 | ||
6beb7bd5 JM |
9881 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
9882 | struct vmcs12 *vmcs12); | |
9883 | ||
9884 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, | |
a2bcba50 WL |
9885 | struct vmcs12 *vmcs12) |
9886 | { | |
9887 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5e2f30b7 | 9888 | struct page *page; |
6beb7bd5 | 9889 | u64 hpa; |
a2bcba50 WL |
9890 | |
9891 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
a2bcba50 WL |
9892 | /* |
9893 | * Translate L1 physical address to host physical | |
9894 | * address for vmcs02. Keep the page pinned, so this | |
9895 | * physical address remains valid. We keep a reference | |
9896 | * to it so we can release it later. | |
9897 | */ | |
5e2f30b7 | 9898 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
53a70daf | 9899 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
5e2f30b7 DH |
9900 | vmx->nested.apic_access_page = NULL; |
9901 | } | |
9902 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); | |
6beb7bd5 JM |
9903 | /* |
9904 | * If translation failed, no matter: This feature asks | |
9905 | * to exit when accessing the given address, and if it | |
9906 | * can never be accessed, this feature won't do | |
9907 | * anything anyway. | |
9908 | */ | |
5e2f30b7 DH |
9909 | if (!is_error_page(page)) { |
9910 | vmx->nested.apic_access_page = page; | |
6beb7bd5 JM |
9911 | hpa = page_to_phys(vmx->nested.apic_access_page); |
9912 | vmcs_write64(APIC_ACCESS_ADDR, hpa); | |
9913 | } else { | |
9914 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
9915 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
9916 | } | |
9917 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && | |
9918 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { | |
9919 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
9920 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
9921 | kvm_vcpu_reload_apic_access_page(vcpu); | |
a2bcba50 | 9922 | } |
a7c0b07d WL |
9923 | |
9924 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { | |
5e2f30b7 | 9925 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
53a70daf | 9926 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
5e2f30b7 DH |
9927 | vmx->nested.virtual_apic_page = NULL; |
9928 | } | |
9929 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); | |
a7c0b07d WL |
9930 | |
9931 | /* | |
6beb7bd5 JM |
9932 | * If translation failed, VM entry will fail because |
9933 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. | |
9934 | * Failing the vm entry is _not_ what the processor | |
9935 | * does but it's basically the only possibility we | |
9936 | * have. We could still enter the guest if CR8 load | |
9937 | * exits are enabled, CR8 store exits are enabled, and | |
9938 | * virtualize APIC access is disabled; in this case | |
9939 | * the processor would never use the TPR shadow and we | |
9940 | * could simply clear the bit from the execution | |
9941 | * control. But such a configuration is useless, so | |
9942 | * let's keep the code simple. | |
a7c0b07d | 9943 | */ |
5e2f30b7 DH |
9944 | if (!is_error_page(page)) { |
9945 | vmx->nested.virtual_apic_page = page; | |
6beb7bd5 JM |
9946 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
9947 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); | |
9948 | } | |
a7c0b07d WL |
9949 | } |
9950 | ||
705699a1 | 9951 | if (nested_cpu_has_posted_intr(vmcs12)) { |
705699a1 WV |
9952 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
9953 | kunmap(vmx->nested.pi_desc_page); | |
53a70daf | 9954 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
5e2f30b7 | 9955 | vmx->nested.pi_desc_page = NULL; |
705699a1 | 9956 | } |
5e2f30b7 DH |
9957 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
9958 | if (is_error_page(page)) | |
6beb7bd5 | 9959 | return; |
5e2f30b7 DH |
9960 | vmx->nested.pi_desc_page = page; |
9961 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); | |
705699a1 WV |
9962 | vmx->nested.pi_desc = |
9963 | (struct pi_desc *)((void *)vmx->nested.pi_desc + | |
9964 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
9965 | (PAGE_SIZE - 1))); | |
6beb7bd5 JM |
9966 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
9967 | page_to_phys(vmx->nested.pi_desc_page) + | |
9968 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
9969 | (PAGE_SIZE - 1))); | |
705699a1 | 9970 | } |
6beb7bd5 JM |
9971 | if (cpu_has_vmx_msr_bitmap() && |
9972 | nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) && | |
9973 | nested_vmx_merge_msr_bitmap(vcpu, vmcs12)) | |
9974 | ; | |
9975 | else | |
9976 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, | |
9977 | CPU_BASED_USE_MSR_BITMAPS); | |
a2bcba50 WL |
9978 | } |
9979 | ||
f4124500 JK |
9980 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
9981 | { | |
9982 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; | |
9983 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9984 | ||
9985 | if (vcpu->arch.virtual_tsc_khz == 0) | |
9986 | return; | |
9987 | ||
9988 | /* Make sure short timeouts reliably trigger an immediate vmexit. | |
9989 | * hrtimer_start does not guarantee this. */ | |
9990 | if (preemption_timeout <= 1) { | |
9991 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); | |
9992 | return; | |
9993 | } | |
9994 | ||
9995 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
9996 | preemption_timeout *= 1000000; | |
9997 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); | |
9998 | hrtimer_start(&vmx->nested.preemption_timer, | |
9999 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); | |
10000 | } | |
10001 | ||
56a20510 JM |
10002 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
10003 | struct vmcs12 *vmcs12) | |
10004 | { | |
10005 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) | |
10006 | return 0; | |
10007 | ||
10008 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || | |
10009 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) | |
10010 | return -EINVAL; | |
10011 | ||
10012 | return 0; | |
10013 | } | |
10014 | ||
3af18d9c WV |
10015 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
10016 | struct vmcs12 *vmcs12) | |
10017 | { | |
3af18d9c WV |
10018 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
10019 | return 0; | |
10020 | ||
5fa99cbe | 10021 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
3af18d9c WV |
10022 | return -EINVAL; |
10023 | ||
10024 | return 0; | |
10025 | } | |
10026 | ||
712b12d7 JM |
10027 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
10028 | struct vmcs12 *vmcs12) | |
10029 | { | |
10030 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
10031 | return 0; | |
10032 | ||
10033 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) | |
10034 | return -EINVAL; | |
10035 | ||
10036 | return 0; | |
10037 | } | |
10038 | ||
3af18d9c WV |
10039 | /* |
10040 | * Merge L0's and L1's MSR bitmap, return false to indicate that | |
10041 | * we do not use the hardware. | |
10042 | */ | |
10043 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, | |
10044 | struct vmcs12 *vmcs12) | |
10045 | { | |
82f0dd4b | 10046 | int msr; |
f2b93280 | 10047 | struct page *page; |
d048c098 RK |
10048 | unsigned long *msr_bitmap_l1; |
10049 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap; | |
f2b93280 | 10050 | |
d048c098 | 10051 | /* This shortcut is ok because we support only x2APIC MSRs so far. */ |
f2b93280 WV |
10052 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12)) |
10053 | return false; | |
10054 | ||
5e2f30b7 DH |
10055 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
10056 | if (is_error_page(page)) | |
f2b93280 | 10057 | return false; |
d048c098 | 10058 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
f2b93280 | 10059 | |
d048c098 RK |
10060 | memset(msr_bitmap_l0, 0xff, PAGE_SIZE); |
10061 | ||
f2b93280 | 10062 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
82f0dd4b WV |
10063 | if (nested_cpu_has_apic_reg_virt(vmcs12)) |
10064 | for (msr = 0x800; msr <= 0x8ff; msr++) | |
10065 | nested_vmx_disable_intercept_for_msr( | |
d048c098 | 10066 | msr_bitmap_l1, msr_bitmap_l0, |
82f0dd4b | 10067 | msr, MSR_TYPE_R); |
d048c098 RK |
10068 | |
10069 | nested_vmx_disable_intercept_for_msr( | |
10070 | msr_bitmap_l1, msr_bitmap_l0, | |
f2b93280 WV |
10071 | APIC_BASE_MSR + (APIC_TASKPRI >> 4), |
10072 | MSR_TYPE_R | MSR_TYPE_W); | |
d048c098 | 10073 | |
608406e2 | 10074 | if (nested_cpu_has_vid(vmcs12)) { |
608406e2 | 10075 | nested_vmx_disable_intercept_for_msr( |
d048c098 | 10076 | msr_bitmap_l1, msr_bitmap_l0, |
608406e2 WV |
10077 | APIC_BASE_MSR + (APIC_EOI >> 4), |
10078 | MSR_TYPE_W); | |
10079 | nested_vmx_disable_intercept_for_msr( | |
d048c098 | 10080 | msr_bitmap_l1, msr_bitmap_l0, |
608406e2 WV |
10081 | APIC_BASE_MSR + (APIC_SELF_IPI >> 4), |
10082 | MSR_TYPE_W); | |
10083 | } | |
82f0dd4b | 10084 | } |
f2b93280 | 10085 | kunmap(page); |
53a70daf | 10086 | kvm_release_page_clean(page); |
f2b93280 WV |
10087 | |
10088 | return true; | |
10089 | } | |
10090 | ||
10091 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, | |
10092 | struct vmcs12 *vmcs12) | |
10093 | { | |
82f0dd4b | 10094 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
608406e2 | 10095 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
705699a1 WV |
10096 | !nested_cpu_has_vid(vmcs12) && |
10097 | !nested_cpu_has_posted_intr(vmcs12)) | |
f2b93280 WV |
10098 | return 0; |
10099 | ||
10100 | /* | |
10101 | * If virtualize x2apic mode is enabled, | |
10102 | * virtualize apic access must be disabled. | |
10103 | */ | |
82f0dd4b WV |
10104 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
10105 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
f2b93280 WV |
10106 | return -EINVAL; |
10107 | ||
608406e2 WV |
10108 | /* |
10109 | * If virtual interrupt delivery is enabled, | |
10110 | * we must exit on external interrupts. | |
10111 | */ | |
10112 | if (nested_cpu_has_vid(vmcs12) && | |
10113 | !nested_exit_on_intr(vcpu)) | |
10114 | return -EINVAL; | |
10115 | ||
705699a1 WV |
10116 | /* |
10117 | * bits 15:8 should be zero in posted_intr_nv, | |
10118 | * the descriptor address has been already checked | |
10119 | * in nested_get_vmcs12_pages. | |
10120 | */ | |
10121 | if (nested_cpu_has_posted_intr(vmcs12) && | |
10122 | (!nested_cpu_has_vid(vmcs12) || | |
10123 | !nested_exit_intr_ack_set(vcpu) || | |
10124 | vmcs12->posted_intr_nv & 0xff00)) | |
10125 | return -EINVAL; | |
10126 | ||
f2b93280 WV |
10127 | /* tpr shadow is needed by all apicv features. */ |
10128 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
10129 | return -EINVAL; | |
10130 | ||
10131 | return 0; | |
3af18d9c WV |
10132 | } |
10133 | ||
e9ac033e EK |
10134 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
10135 | unsigned long count_field, | |
92d71bc6 | 10136 | unsigned long addr_field) |
ff651cb6 | 10137 | { |
92d71bc6 | 10138 | int maxphyaddr; |
e9ac033e EK |
10139 | u64 count, addr; |
10140 | ||
10141 | if (vmcs12_read_any(vcpu, count_field, &count) || | |
10142 | vmcs12_read_any(vcpu, addr_field, &addr)) { | |
10143 | WARN_ON(1); | |
10144 | return -EINVAL; | |
10145 | } | |
10146 | if (count == 0) | |
10147 | return 0; | |
92d71bc6 | 10148 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
e9ac033e EK |
10149 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
10150 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { | |
bbe41b95 | 10151 | pr_debug_ratelimited( |
e9ac033e EK |
10152 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
10153 | addr_field, maxphyaddr, count, addr); | |
10154 | return -EINVAL; | |
10155 | } | |
10156 | return 0; | |
10157 | } | |
10158 | ||
10159 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, | |
10160 | struct vmcs12 *vmcs12) | |
10161 | { | |
e9ac033e EK |
10162 | if (vmcs12->vm_exit_msr_load_count == 0 && |
10163 | vmcs12->vm_exit_msr_store_count == 0 && | |
10164 | vmcs12->vm_entry_msr_load_count == 0) | |
10165 | return 0; /* Fast path */ | |
e9ac033e | 10166 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
92d71bc6 | 10167 | VM_EXIT_MSR_LOAD_ADDR) || |
e9ac033e | 10168 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
92d71bc6 | 10169 | VM_EXIT_MSR_STORE_ADDR) || |
e9ac033e | 10170 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
92d71bc6 | 10171 | VM_ENTRY_MSR_LOAD_ADDR)) |
e9ac033e EK |
10172 | return -EINVAL; |
10173 | return 0; | |
10174 | } | |
10175 | ||
c5f983f6 BD |
10176 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
10177 | struct vmcs12 *vmcs12) | |
10178 | { | |
10179 | u64 address = vmcs12->pml_address; | |
10180 | int maxphyaddr = cpuid_maxphyaddr(vcpu); | |
10181 | ||
10182 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { | |
10183 | if (!nested_cpu_has_ept(vmcs12) || | |
10184 | !IS_ALIGNED(address, 4096) || | |
10185 | address >> maxphyaddr) | |
10186 | return -EINVAL; | |
10187 | } | |
10188 | ||
10189 | return 0; | |
10190 | } | |
10191 | ||
e9ac033e EK |
10192 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
10193 | struct vmx_msr_entry *e) | |
10194 | { | |
10195 | /* x2APIC MSR accesses are not allowed */ | |
8a9781f7 | 10196 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
e9ac033e EK |
10197 | return -EINVAL; |
10198 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ | |
10199 | e->index == MSR_IA32_UCODE_REV) | |
10200 | return -EINVAL; | |
10201 | if (e->reserved != 0) | |
ff651cb6 WV |
10202 | return -EINVAL; |
10203 | return 0; | |
10204 | } | |
10205 | ||
e9ac033e EK |
10206 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
10207 | struct vmx_msr_entry *e) | |
ff651cb6 WV |
10208 | { |
10209 | if (e->index == MSR_FS_BASE || | |
10210 | e->index == MSR_GS_BASE || | |
e9ac033e EK |
10211 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
10212 | nested_vmx_msr_check_common(vcpu, e)) | |
10213 | return -EINVAL; | |
10214 | return 0; | |
10215 | } | |
10216 | ||
10217 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, | |
10218 | struct vmx_msr_entry *e) | |
10219 | { | |
10220 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ | |
10221 | nested_vmx_msr_check_common(vcpu, e)) | |
ff651cb6 WV |
10222 | return -EINVAL; |
10223 | return 0; | |
10224 | } | |
10225 | ||
10226 | /* | |
10227 | * Load guest's/host's msr at nested entry/exit. | |
10228 | * return 0 for success, entry index for failure. | |
10229 | */ | |
10230 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
10231 | { | |
10232 | u32 i; | |
10233 | struct vmx_msr_entry e; | |
10234 | struct msr_data msr; | |
10235 | ||
10236 | msr.host_initiated = false; | |
10237 | for (i = 0; i < count; i++) { | |
54bf36aa PB |
10238 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
10239 | &e, sizeof(e))) { | |
bbe41b95 | 10240 | pr_debug_ratelimited( |
e9ac033e EK |
10241 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
10242 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 10243 | goto fail; |
e9ac033e EK |
10244 | } |
10245 | if (nested_vmx_load_msr_check(vcpu, &e)) { | |
bbe41b95 | 10246 | pr_debug_ratelimited( |
e9ac033e EK |
10247 | "%s check failed (%u, 0x%x, 0x%x)\n", |
10248 | __func__, i, e.index, e.reserved); | |
10249 | goto fail; | |
10250 | } | |
ff651cb6 WV |
10251 | msr.index = e.index; |
10252 | msr.data = e.value; | |
e9ac033e | 10253 | if (kvm_set_msr(vcpu, &msr)) { |
bbe41b95 | 10254 | pr_debug_ratelimited( |
e9ac033e EK |
10255 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
10256 | __func__, i, e.index, e.value); | |
ff651cb6 | 10257 | goto fail; |
e9ac033e | 10258 | } |
ff651cb6 WV |
10259 | } |
10260 | return 0; | |
10261 | fail: | |
10262 | return i + 1; | |
10263 | } | |
10264 | ||
10265 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
10266 | { | |
10267 | u32 i; | |
10268 | struct vmx_msr_entry e; | |
10269 | ||
10270 | for (i = 0; i < count; i++) { | |
609e36d3 | 10271 | struct msr_data msr_info; |
54bf36aa PB |
10272 | if (kvm_vcpu_read_guest(vcpu, |
10273 | gpa + i * sizeof(e), | |
10274 | &e, 2 * sizeof(u32))) { | |
bbe41b95 | 10275 | pr_debug_ratelimited( |
e9ac033e EK |
10276 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
10277 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 10278 | return -EINVAL; |
e9ac033e EK |
10279 | } |
10280 | if (nested_vmx_store_msr_check(vcpu, &e)) { | |
bbe41b95 | 10281 | pr_debug_ratelimited( |
e9ac033e EK |
10282 | "%s check failed (%u, 0x%x, 0x%x)\n", |
10283 | __func__, i, e.index, e.reserved); | |
ff651cb6 | 10284 | return -EINVAL; |
e9ac033e | 10285 | } |
609e36d3 PB |
10286 | msr_info.host_initiated = false; |
10287 | msr_info.index = e.index; | |
10288 | if (kvm_get_msr(vcpu, &msr_info)) { | |
bbe41b95 | 10289 | pr_debug_ratelimited( |
e9ac033e EK |
10290 | "%s cannot read MSR (%u, 0x%x)\n", |
10291 | __func__, i, e.index); | |
10292 | return -EINVAL; | |
10293 | } | |
54bf36aa PB |
10294 | if (kvm_vcpu_write_guest(vcpu, |
10295 | gpa + i * sizeof(e) + | |
10296 | offsetof(struct vmx_msr_entry, value), | |
10297 | &msr_info.data, sizeof(msr_info.data))) { | |
bbe41b95 | 10298 | pr_debug_ratelimited( |
e9ac033e | 10299 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
609e36d3 | 10300 | __func__, i, e.index, msr_info.data); |
e9ac033e EK |
10301 | return -EINVAL; |
10302 | } | |
ff651cb6 WV |
10303 | } |
10304 | return 0; | |
10305 | } | |
10306 | ||
1dc35dac LP |
10307 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
10308 | { | |
10309 | unsigned long invalid_mask; | |
10310 | ||
10311 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); | |
10312 | return (val & invalid_mask) == 0; | |
10313 | } | |
10314 | ||
9ed38ffa LP |
10315 | /* |
10316 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are | |
10317 | * emulating VM entry into a guest with EPT enabled. | |
10318 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code | |
10319 | * is assigned to entry_failure_code on failure. | |
10320 | */ | |
10321 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept, | |
ca0bde28 | 10322 | u32 *entry_failure_code) |
9ed38ffa | 10323 | { |
9ed38ffa | 10324 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
1dc35dac | 10325 | if (!nested_cr3_valid(vcpu, cr3)) { |
9ed38ffa LP |
10326 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
10327 | return 1; | |
10328 | } | |
10329 | ||
10330 | /* | |
10331 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and | |
10332 | * must not be dereferenced. | |
10333 | */ | |
10334 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && | |
10335 | !nested_ept) { | |
10336 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { | |
10337 | *entry_failure_code = ENTRY_FAIL_PDPTE; | |
10338 | return 1; | |
10339 | } | |
10340 | } | |
10341 | ||
10342 | vcpu->arch.cr3 = cr3; | |
10343 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
10344 | } | |
10345 | ||
10346 | kvm_mmu_reset_context(vcpu); | |
10347 | return 0; | |
10348 | } | |
10349 | ||
fe3ef05c NHE |
10350 | /* |
10351 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested | |
10352 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it | |
b4619660 | 10353 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
fe3ef05c NHE |
10354 | * guest in a way that will both be appropriate to L1's requests, and our |
10355 | * needs. In addition to modifying the active vmcs (which is vmcs02), this | |
10356 | * function also has additional necessary side-effects, like setting various | |
10357 | * vcpu->arch fields. | |
ee146c1c LP |
10358 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
10359 | * is assigned to entry_failure_code on failure. | |
fe3ef05c | 10360 | */ |
ee146c1c | 10361 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
ca0bde28 | 10362 | bool from_vmentry, u32 *entry_failure_code) |
fe3ef05c NHE |
10363 | { |
10364 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
03efce6f | 10365 | u32 exec_control, vmcs12_exec_ctrl; |
fe3ef05c NHE |
10366 | |
10367 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); | |
10368 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); | |
10369 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); | |
10370 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); | |
10371 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); | |
10372 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); | |
10373 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); | |
10374 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); | |
10375 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); | |
10376 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); | |
10377 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); | |
10378 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); | |
10379 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); | |
10380 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); | |
10381 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); | |
10382 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); | |
10383 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); | |
10384 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); | |
10385 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); | |
10386 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); | |
10387 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); | |
10388 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); | |
10389 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); | |
10390 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); | |
10391 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); | |
10392 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); | |
10393 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); | |
10394 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); | |
10395 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); | |
10396 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); | |
10397 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); | |
10398 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); | |
10399 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); | |
10400 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); | |
10401 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); | |
10402 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); | |
10403 | ||
cf8b84f4 JM |
10404 | if (from_vmentry && |
10405 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { | |
2996fca0 JK |
10406 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
10407 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); | |
10408 | } else { | |
10409 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); | |
10410 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); | |
10411 | } | |
cf8b84f4 JM |
10412 | if (from_vmentry) { |
10413 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, | |
10414 | vmcs12->vm_entry_intr_info_field); | |
10415 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, | |
10416 | vmcs12->vm_entry_exception_error_code); | |
10417 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
10418 | vmcs12->vm_entry_instruction_len); | |
10419 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, | |
10420 | vmcs12->guest_interruptibility_info); | |
2d6144e3 WL |
10421 | vmx->loaded_vmcs->nmi_known_unmasked = |
10422 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); | |
cf8b84f4 JM |
10423 | } else { |
10424 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
10425 | } | |
fe3ef05c | 10426 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
63fbf59f | 10427 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
fe3ef05c NHE |
10428 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
10429 | vmcs12->guest_pending_dbg_exceptions); | |
10430 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); | |
10431 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); | |
10432 | ||
81dc01f7 WL |
10433 | if (nested_cpu_has_xsaves(vmcs12)) |
10434 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); | |
fe3ef05c NHE |
10435 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
10436 | ||
f4124500 | 10437 | exec_control = vmcs12->pin_based_vm_exec_control; |
9314006d PB |
10438 | |
10439 | /* Preemption timer setting is only taken from vmcs01. */ | |
705699a1 | 10440 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
9314006d PB |
10441 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
10442 | if (vmx->hv_deadline_tsc == -1) | |
10443 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
705699a1 | 10444 | |
9314006d | 10445 | /* Posted interrupts setting is only taken from vmcs12. */ |
705699a1 | 10446 | if (nested_cpu_has_posted_intr(vmcs12)) { |
705699a1 WV |
10447 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
10448 | vmx->nested.pi_pending = false; | |
06a5524f | 10449 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
6beb7bd5 | 10450 | } else { |
705699a1 | 10451 | exec_control &= ~PIN_BASED_POSTED_INTR; |
6beb7bd5 | 10452 | } |
705699a1 | 10453 | |
f4124500 | 10454 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
fe3ef05c | 10455 | |
f4124500 JK |
10456 | vmx->nested.preemption_timer_expired = false; |
10457 | if (nested_cpu_has_preemption_timer(vmcs12)) | |
10458 | vmx_start_preemption_timer(vcpu); | |
0238ea91 | 10459 | |
fe3ef05c NHE |
10460 | /* |
10461 | * Whether page-faults are trapped is determined by a combination of | |
10462 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. | |
10463 | * If enable_ept, L0 doesn't care about page faults and we should | |
10464 | * set all of these to L1's desires. However, if !enable_ept, L0 does | |
10465 | * care about (at least some) page faults, and because it is not easy | |
10466 | * (if at all possible?) to merge L0 and L1's desires, we simply ask | |
10467 | * to exit on each and every L2 page fault. This is done by setting | |
10468 | * MASK=MATCH=0 and (see below) EB.PF=1. | |
10469 | * Note that below we don't need special code to set EB.PF beyond the | |
10470 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, | |
10471 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when | |
10472 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. | |
fe3ef05c NHE |
10473 | */ |
10474 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, | |
10475 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); | |
10476 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, | |
10477 | enable_ept ? vmcs12->page_fault_error_code_match : 0); | |
10478 | ||
10479 | if (cpu_has_secondary_exec_ctrls()) { | |
80154d77 | 10480 | exec_control = vmx->secondary_exec_control; |
e2821620 | 10481 | |
fe3ef05c | 10482 | /* Take the following fields only from vmcs12 */ |
696dfd95 | 10483 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
90a2db6d | 10484 | SECONDARY_EXEC_ENABLE_INVPCID | |
b3a2a907 | 10485 | SECONDARY_EXEC_RDTSCP | |
3db13480 | 10486 | SECONDARY_EXEC_XSAVES | |
696dfd95 | 10487 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
27c42a1b BD |
10488 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
10489 | SECONDARY_EXEC_ENABLE_VMFUNC); | |
fe3ef05c | 10490 | if (nested_cpu_has(vmcs12, |
03efce6f BD |
10491 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
10492 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & | |
10493 | ~SECONDARY_EXEC_ENABLE_PML; | |
10494 | exec_control |= vmcs12_exec_ctrl; | |
10495 | } | |
fe3ef05c | 10496 | |
27c42a1b BD |
10497 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
10498 | if (exec_control & SECONDARY_EXEC_ENABLE_VMFUNC) | |
10499 | vmcs_write64(VM_FUNCTION_CONTROL, 0); | |
10500 | ||
608406e2 WV |
10501 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { |
10502 | vmcs_write64(EOI_EXIT_BITMAP0, | |
10503 | vmcs12->eoi_exit_bitmap0); | |
10504 | vmcs_write64(EOI_EXIT_BITMAP1, | |
10505 | vmcs12->eoi_exit_bitmap1); | |
10506 | vmcs_write64(EOI_EXIT_BITMAP2, | |
10507 | vmcs12->eoi_exit_bitmap2); | |
10508 | vmcs_write64(EOI_EXIT_BITMAP3, | |
10509 | vmcs12->eoi_exit_bitmap3); | |
10510 | vmcs_write16(GUEST_INTR_STATUS, | |
10511 | vmcs12->guest_intr_status); | |
10512 | } | |
10513 | ||
6beb7bd5 JM |
10514 | /* |
10515 | * Write an illegal value to APIC_ACCESS_ADDR. Later, | |
10516 | * nested_get_vmcs12_pages will either fix it up or | |
10517 | * remove the VM execution control. | |
10518 | */ | |
10519 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) | |
10520 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); | |
10521 | ||
fe3ef05c NHE |
10522 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
10523 | } | |
10524 | ||
10525 | ||
10526 | /* | |
10527 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) | |
10528 | * Some constant fields are set here by vmx_set_constant_host_state(). | |
10529 | * Other fields are different per CPU, and will be set later when | |
10530 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. | |
10531 | */ | |
a547c6db | 10532 | vmx_set_constant_host_state(vmx); |
fe3ef05c | 10533 | |
83bafef1 JM |
10534 | /* |
10535 | * Set the MSR load/store lists to match L0's settings. | |
10536 | */ | |
10537 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); | |
10538 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
10539 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); | |
10540 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
10541 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); | |
10542 | ||
fe3ef05c NHE |
10543 | /* |
10544 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before | |
10545 | * entry, but only if the current (host) sp changed from the value | |
10546 | * we wrote last (vmx->host_rsp). This cache is no longer relevant | |
10547 | * if we switch vmcs, and rather than hold a separate cache per vmcs, | |
10548 | * here we just force the write to happen on entry. | |
10549 | */ | |
10550 | vmx->host_rsp = 0; | |
10551 | ||
10552 | exec_control = vmx_exec_control(vmx); /* L0's desires */ | |
10553 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; | |
10554 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; | |
10555 | exec_control &= ~CPU_BASED_TPR_SHADOW; | |
10556 | exec_control |= vmcs12->cpu_based_vm_exec_control; | |
a7c0b07d | 10557 | |
6beb7bd5 JM |
10558 | /* |
10559 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if | |
10560 | * nested_get_vmcs12_pages can't fix it up, the illegal value | |
10561 | * will result in a VM entry failure. | |
10562 | */ | |
a7c0b07d | 10563 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
6beb7bd5 | 10564 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
a7c0b07d | 10565 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
51aa68e7 JM |
10566 | } else { |
10567 | #ifdef CONFIG_X86_64 | |
10568 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | | |
10569 | CPU_BASED_CR8_STORE_EXITING; | |
10570 | #endif | |
a7c0b07d WL |
10571 | } |
10572 | ||
fe3ef05c | 10573 | /* |
8eb73e2d QX |
10574 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
10575 | * for I/O port accesses. | |
fe3ef05c | 10576 | */ |
fe3ef05c NHE |
10577 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
10578 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; | |
10579 | ||
10580 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); | |
10581 | ||
10582 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the | |
10583 | * bitwise-or of what L1 wants to trap for L2, and what we want to | |
10584 | * trap. Note that CR0.TS also needs updating - we do this later. | |
10585 | */ | |
10586 | update_exception_bitmap(vcpu); | |
10587 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; | |
10588 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); | |
10589 | ||
8049d651 NHE |
10590 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
10591 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER | |
10592 | * bits are further modified by vmx_set_efer() below. | |
10593 | */ | |
f4124500 | 10594 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
8049d651 NHE |
10595 | |
10596 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are | |
10597 | * emulated by vmx_set_efer(), below. | |
10598 | */ | |
2961e876 | 10599 | vm_entry_controls_init(vmx, |
8049d651 NHE |
10600 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
10601 | ~VM_ENTRY_IA32E_MODE) | | |
fe3ef05c NHE |
10602 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
10603 | ||
cf8b84f4 JM |
10604 | if (from_vmentry && |
10605 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { | |
fe3ef05c | 10606 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
44811c02 | 10607 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
cf8b84f4 | 10608 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
fe3ef05c | 10609 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
cf8b84f4 | 10610 | } |
fe3ef05c NHE |
10611 | |
10612 | set_cr4_guest_host_mask(vmx); | |
10613 | ||
cf8b84f4 JM |
10614 | if (from_vmentry && |
10615 | vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) | |
36be0b9d PB |
10616 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
10617 | ||
27fc51b2 NHE |
10618 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
10619 | vmcs_write64(TSC_OFFSET, | |
ea26e4ec | 10620 | vcpu->arch.tsc_offset + vmcs12->tsc_offset); |
27fc51b2 | 10621 | else |
ea26e4ec | 10622 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
c95ba92a PF |
10623 | if (kvm_has_tsc_control) |
10624 | decache_tsc_multiplier(vmx); | |
fe3ef05c NHE |
10625 | |
10626 | if (enable_vpid) { | |
10627 | /* | |
5c614b35 WL |
10628 | * There is no direct mapping between vpid02 and vpid12, the |
10629 | * vpid02 is per-vCPU for L0 and reused while the value of | |
10630 | * vpid12 is changed w/ one invvpid during nested vmentry. | |
10631 | * The vpid12 is allocated by L1 for L2, so it will not | |
10632 | * influence global bitmap(for vpid01 and vpid02 allocation) | |
10633 | * even if spawn a lot of nested vCPUs. | |
fe3ef05c | 10634 | */ |
5c614b35 WL |
10635 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
10636 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); | |
10637 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { | |
10638 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; | |
c2ba05cc | 10639 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true); |
5c614b35 WL |
10640 | } |
10641 | } else { | |
10642 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
c2ba05cc | 10643 | vmx_flush_tlb(vcpu, true); |
5c614b35 WL |
10644 | } |
10645 | ||
fe3ef05c NHE |
10646 | } |
10647 | ||
1fb883bb LP |
10648 | if (enable_pml) { |
10649 | /* | |
10650 | * Conceptually we want to copy the PML address and index from | |
10651 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, | |
10652 | * since we always flush the log on each vmexit, this happens | |
10653 | * to be equivalent to simply resetting the fields in vmcs02. | |
10654 | */ | |
10655 | ASSERT(vmx->pml_pg); | |
10656 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
10657 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
10658 | } | |
10659 | ||
155a97a3 | 10660 | if (nested_cpu_has_ept(vmcs12)) { |
ae1e2d10 PB |
10661 | if (nested_ept_init_mmu_context(vcpu)) { |
10662 | *entry_failure_code = ENTRY_FAIL_DEFAULT; | |
10663 | return 1; | |
10664 | } | |
fb6c8198 JM |
10665 | } else if (nested_cpu_has2(vmcs12, |
10666 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
10667 | vmx_flush_tlb_ept_only(vcpu); | |
155a97a3 NHE |
10668 | } |
10669 | ||
fe3ef05c | 10670 | /* |
bd7e5b08 PB |
10671 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
10672 | * bits which we consider mandatory enabled. | |
fe3ef05c NHE |
10673 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
10674 | * the specifications by L1; It's not enough to take | |
10675 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we | |
10676 | * have more bits than L1 expected. | |
10677 | */ | |
10678 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); | |
10679 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); | |
10680 | ||
10681 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); | |
10682 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); | |
10683 | ||
cf8b84f4 JM |
10684 | if (from_vmentry && |
10685 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) | |
5a6a9748 DM |
10686 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
10687 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) | |
10688 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); | |
10689 | else | |
10690 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
10691 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ | |
10692 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
10693 | ||
9ed38ffa | 10694 | /* Shadow page tables on either EPT or shadow page tables. */ |
7ad658b6 | 10695 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
9ed38ffa LP |
10696 | entry_failure_code)) |
10697 | return 1; | |
7ca29de2 | 10698 | |
feaf0c7d GN |
10699 | if (!enable_ept) |
10700 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; | |
10701 | ||
3633cfc3 NHE |
10702 | /* |
10703 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 | |
10704 | */ | |
10705 | if (enable_ept) { | |
10706 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); | |
10707 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); | |
10708 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); | |
10709 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); | |
10710 | } | |
10711 | ||
fe3ef05c NHE |
10712 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
10713 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); | |
ee146c1c | 10714 | return 0; |
fe3ef05c NHE |
10715 | } |
10716 | ||
ca0bde28 | 10717 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
cd232ad0 | 10718 | { |
cd232ad0 | 10719 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
7c177938 | 10720 | |
6dfacadd | 10721 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
ca0bde28 JM |
10722 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
10723 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
26539bd0 | 10724 | |
56a20510 JM |
10725 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
10726 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10727 | ||
ca0bde28 JM |
10728 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
10729 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
7c177938 | 10730 | |
712b12d7 JM |
10731 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
10732 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10733 | ||
ca0bde28 JM |
10734 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
10735 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
f2b93280 | 10736 | |
ca0bde28 JM |
10737 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
10738 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
e9ac033e | 10739 | |
c5f983f6 BD |
10740 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
10741 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10742 | ||
7c177938 | 10743 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
0115f9cb | 10744 | vmx->nested.nested_vmx_procbased_ctls_low, |
b9c237bb | 10745 | vmx->nested.nested_vmx_procbased_ctls_high) || |
2e5b0bd9 JM |
10746 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
10747 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, | |
10748 | vmx->nested.nested_vmx_secondary_ctls_low, | |
10749 | vmx->nested.nested_vmx_secondary_ctls_high)) || | |
7c177938 | 10750 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
b9c237bb WV |
10751 | vmx->nested.nested_vmx_pinbased_ctls_low, |
10752 | vmx->nested.nested_vmx_pinbased_ctls_high) || | |
7c177938 | 10753 | !vmx_control_verify(vmcs12->vm_exit_controls, |
0115f9cb | 10754 | vmx->nested.nested_vmx_exit_ctls_low, |
b9c237bb | 10755 | vmx->nested.nested_vmx_exit_ctls_high) || |
7c177938 | 10756 | !vmx_control_verify(vmcs12->vm_entry_controls, |
0115f9cb | 10757 | vmx->nested.nested_vmx_entry_ctls_low, |
b9c237bb | 10758 | vmx->nested.nested_vmx_entry_ctls_high)) |
ca0bde28 | 10759 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
7c177938 | 10760 | |
41ab9372 BD |
10761 | if (nested_cpu_has_vmfunc(vmcs12)) { |
10762 | if (vmcs12->vm_function_control & | |
10763 | ~vmx->nested.nested_vmx_vmfunc_controls) | |
10764 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10765 | ||
10766 | if (nested_cpu_has_eptp_switching(vmcs12)) { | |
10767 | if (!nested_cpu_has_ept(vmcs12) || | |
10768 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) | |
10769 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10770 | } | |
10771 | } | |
27c42a1b | 10772 | |
c7c2c709 JM |
10773 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
10774 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10775 | ||
3899152c | 10776 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
1dc35dac | 10777 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
ca0bde28 JM |
10778 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
10779 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; | |
10780 | ||
10781 | return 0; | |
10782 | } | |
10783 | ||
10784 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
10785 | u32 *exit_qual) | |
10786 | { | |
10787 | bool ia32e; | |
10788 | ||
10789 | *exit_qual = ENTRY_FAIL_DEFAULT; | |
7c177938 | 10790 | |
3899152c | 10791 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
ca0bde28 | 10792 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
b428018a | 10793 | return 1; |
ca0bde28 JM |
10794 | |
10795 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) && | |
10796 | vmcs12->vmcs_link_pointer != -1ull) { | |
10797 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; | |
b428018a | 10798 | return 1; |
7c177938 NHE |
10799 | } |
10800 | ||
384bb783 | 10801 | /* |
cb0c8cda | 10802 | * If the load IA32_EFER VM-entry control is 1, the following checks |
384bb783 JK |
10803 | * are performed on the field for the IA32_EFER MSR: |
10804 | * - Bits reserved in the IA32_EFER MSR must be 0. | |
10805 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of | |
10806 | * the IA-32e mode guest VM-exit control. It must also be identical | |
10807 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to | |
10808 | * CR0.PG) is 1. | |
10809 | */ | |
ca0bde28 JM |
10810 | if (to_vmx(vcpu)->nested.nested_run_pending && |
10811 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { | |
384bb783 JK |
10812 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
10813 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || | |
10814 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || | |
10815 | ((vmcs12->guest_cr0 & X86_CR0_PG) && | |
ca0bde28 | 10816 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
b428018a | 10817 | return 1; |
384bb783 JK |
10818 | } |
10819 | ||
10820 | /* | |
10821 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the | |
10822 | * IA32_EFER MSR must be 0 in the field for that register. In addition, | |
10823 | * the values of the LMA and LME bits in the field must each be that of | |
10824 | * the host address-space size VM-exit control. | |
10825 | */ | |
10826 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { | |
10827 | ia32e = (vmcs12->vm_exit_controls & | |
10828 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; | |
10829 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || | |
10830 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || | |
ca0bde28 | 10831 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
b428018a | 10832 | return 1; |
ca0bde28 JM |
10833 | } |
10834 | ||
f1b026a3 WL |
10835 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
10836 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || | |
10837 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) | |
10838 | return 1; | |
10839 | ||
ca0bde28 JM |
10840 | return 0; |
10841 | } | |
10842 | ||
858e25c0 JM |
10843 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry) |
10844 | { | |
10845 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10846 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
858e25c0 JM |
10847 | u32 msr_entry_idx; |
10848 | u32 exit_qual; | |
10849 | ||
858e25c0 JM |
10850 | enter_guest_mode(vcpu); |
10851 | ||
10852 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) | |
10853 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
10854 | ||
00647b44 | 10855 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
858e25c0 JM |
10856 | vmx_segment_cache_clear(vmx); |
10857 | ||
10858 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) { | |
10859 | leave_guest_mode(vcpu); | |
1279a6b1 | 10860 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
858e25c0 JM |
10861 | nested_vmx_entry_failure(vcpu, vmcs12, |
10862 | EXIT_REASON_INVALID_STATE, exit_qual); | |
10863 | return 1; | |
10864 | } | |
10865 | ||
10866 | nested_get_vmcs12_pages(vcpu, vmcs12); | |
10867 | ||
10868 | msr_entry_idx = nested_vmx_load_msr(vcpu, | |
10869 | vmcs12->vm_entry_msr_load_addr, | |
10870 | vmcs12->vm_entry_msr_load_count); | |
10871 | if (msr_entry_idx) { | |
10872 | leave_guest_mode(vcpu); | |
1279a6b1 | 10873 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
858e25c0 JM |
10874 | nested_vmx_entry_failure(vcpu, vmcs12, |
10875 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); | |
10876 | return 1; | |
10877 | } | |
10878 | ||
858e25c0 JM |
10879 | /* |
10880 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point | |
10881 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet | |
10882 | * returned as far as L1 is concerned. It will only return (and set | |
10883 | * the success flag) when L2 exits (see nested_vmx_vmexit()). | |
10884 | */ | |
10885 | return 0; | |
10886 | } | |
10887 | ||
ca0bde28 JM |
10888 | /* |
10889 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 | |
10890 | * for running an L2 nested guest. | |
10891 | */ | |
10892 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) | |
10893 | { | |
10894 | struct vmcs12 *vmcs12; | |
10895 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
b3f1dfb6 | 10896 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
ca0bde28 JM |
10897 | u32 exit_qual; |
10898 | int ret; | |
10899 | ||
10900 | if (!nested_vmx_check_permission(vcpu)) | |
10901 | return 1; | |
10902 | ||
10903 | if (!nested_vmx_check_vmcs12(vcpu)) | |
10904 | goto out; | |
10905 | ||
10906 | vmcs12 = get_vmcs12(vcpu); | |
10907 | ||
10908 | if (enable_shadow_vmcs) | |
10909 | copy_shadow_to_vmcs12(vmx); | |
10910 | ||
10911 | /* | |
10912 | * The nested entry process starts with enforcing various prerequisites | |
10913 | * on vmcs12 as required by the Intel SDM, and act appropriately when | |
10914 | * they fail: As the SDM explains, some conditions should cause the | |
10915 | * instruction to fail, while others will cause the instruction to seem | |
10916 | * to succeed, but return an EXIT_REASON_INVALID_STATE. | |
10917 | * To speed up the normal (success) code path, we should avoid checking | |
10918 | * for misconfigurations which will anyway be caught by the processor | |
10919 | * when using the merged vmcs02. | |
10920 | */ | |
b3f1dfb6 JM |
10921 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
10922 | nested_vmx_failValid(vcpu, | |
10923 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); | |
10924 | goto out; | |
10925 | } | |
10926 | ||
ca0bde28 JM |
10927 | if (vmcs12->launch_state == launch) { |
10928 | nested_vmx_failValid(vcpu, | |
10929 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS | |
10930 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); | |
10931 | goto out; | |
10932 | } | |
10933 | ||
10934 | ret = check_vmentry_prereqs(vcpu, vmcs12); | |
10935 | if (ret) { | |
10936 | nested_vmx_failValid(vcpu, ret); | |
10937 | goto out; | |
10938 | } | |
10939 | ||
10940 | /* | |
10941 | * After this point, the trap flag no longer triggers a singlestep trap | |
10942 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. | |
10943 | * This is not 100% correct; for performance reasons, we delegate most | |
10944 | * of the checks on host state to the processor. If those fail, | |
10945 | * the singlestep trap is missed. | |
10946 | */ | |
10947 | skip_emulated_instruction(vcpu); | |
10948 | ||
10949 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); | |
10950 | if (ret) { | |
10951 | nested_vmx_entry_failure(vcpu, vmcs12, | |
10952 | EXIT_REASON_INVALID_STATE, exit_qual); | |
10953 | return 1; | |
384bb783 JK |
10954 | } |
10955 | ||
7c177938 NHE |
10956 | /* |
10957 | * We're finally done with prerequisite checking, and can start with | |
10958 | * the nested entry. | |
10959 | */ | |
10960 | ||
858e25c0 JM |
10961 | ret = enter_vmx_non_root_mode(vcpu, true); |
10962 | if (ret) | |
10963 | return ret; | |
ff651cb6 | 10964 | |
6dfacadd | 10965 | if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) |
5cb56059 | 10966 | return kvm_vcpu_halt(vcpu); |
6dfacadd | 10967 | |
7af40ad3 JK |
10968 | vmx->nested.nested_run_pending = 1; |
10969 | ||
cd232ad0 | 10970 | return 1; |
eb277562 KH |
10971 | |
10972 | out: | |
6affcbed | 10973 | return kvm_skip_emulated_instruction(vcpu); |
cd232ad0 NHE |
10974 | } |
10975 | ||
4704d0be NHE |
10976 | /* |
10977 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date | |
10978 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). | |
10979 | * This function returns the new value we should put in vmcs12.guest_cr0. | |
10980 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, | |
10981 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now | |
10982 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 | |
10983 | * didn't trap the bit, because if L1 did, so would L0). | |
10984 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have | |
10985 | * been modified by L2, and L1 knows it. So just leave the old value of | |
10986 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 | |
10987 | * isn't relevant, because if L0 traps this bit it can set it to anything. | |
10988 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have | |
10989 | * changed these bits, and therefore they need to be updated, but L0 | |
10990 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather | |
10991 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. | |
10992 | */ | |
10993 | static inline unsigned long | |
10994 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
10995 | { | |
10996 | return | |
10997 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | | |
10998 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | | |
10999 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | | |
11000 | vcpu->arch.cr0_guest_owned_bits)); | |
11001 | } | |
11002 | ||
11003 | static inline unsigned long | |
11004 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
11005 | { | |
11006 | return | |
11007 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | | |
11008 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | | |
11009 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | | |
11010 | vcpu->arch.cr4_guest_owned_bits)); | |
11011 | } | |
11012 | ||
5f3d5799 JK |
11013 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
11014 | struct vmcs12 *vmcs12) | |
11015 | { | |
11016 | u32 idt_vectoring; | |
11017 | unsigned int nr; | |
11018 | ||
664f8e26 | 11019 | if (vcpu->arch.exception.injected) { |
5f3d5799 JK |
11020 | nr = vcpu->arch.exception.nr; |
11021 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
11022 | ||
11023 | if (kvm_exception_is_soft(nr)) { | |
11024 | vmcs12->vm_exit_instruction_len = | |
11025 | vcpu->arch.event_exit_inst_len; | |
11026 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; | |
11027 | } else | |
11028 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; | |
11029 | ||
11030 | if (vcpu->arch.exception.has_error_code) { | |
11031 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; | |
11032 | vmcs12->idt_vectoring_error_code = | |
11033 | vcpu->arch.exception.error_code; | |
11034 | } | |
11035 | ||
11036 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
cd2633c5 | 11037 | } else if (vcpu->arch.nmi_injected) { |
5f3d5799 JK |
11038 | vmcs12->idt_vectoring_info_field = |
11039 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; | |
11040 | } else if (vcpu->arch.interrupt.pending) { | |
11041 | nr = vcpu->arch.interrupt.nr; | |
11042 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
11043 | ||
11044 | if (vcpu->arch.interrupt.soft) { | |
11045 | idt_vectoring |= INTR_TYPE_SOFT_INTR; | |
11046 | vmcs12->vm_entry_instruction_len = | |
11047 | vcpu->arch.event_exit_inst_len; | |
11048 | } else | |
11049 | idt_vectoring |= INTR_TYPE_EXT_INTR; | |
11050 | ||
11051 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
11052 | } | |
11053 | } | |
11054 | ||
b6b8a145 JK |
11055 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
11056 | { | |
11057 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
bfcf83b1 | 11058 | unsigned long exit_qual; |
917dc606 LA |
11059 | bool block_nested_events = |
11060 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); | |
acc9ab60 | 11061 | |
bfcf83b1 WL |
11062 | if (vcpu->arch.exception.pending && |
11063 | nested_vmx_check_exception(vcpu, &exit_qual)) { | |
917dc606 | 11064 | if (block_nested_events) |
bfcf83b1 WL |
11065 | return -EBUSY; |
11066 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); | |
bfcf83b1 WL |
11067 | return 0; |
11068 | } | |
11069 | ||
f4124500 JK |
11070 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
11071 | vmx->nested.preemption_timer_expired) { | |
917dc606 | 11072 | if (block_nested_events) |
f4124500 JK |
11073 | return -EBUSY; |
11074 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); | |
11075 | return 0; | |
11076 | } | |
11077 | ||
b6b8a145 | 11078 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
917dc606 | 11079 | if (block_nested_events) |
b6b8a145 JK |
11080 | return -EBUSY; |
11081 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, | |
11082 | NMI_VECTOR | INTR_TYPE_NMI_INTR | | |
11083 | INTR_INFO_VALID_MASK, 0); | |
11084 | /* | |
11085 | * The NMI-triggered VM exit counts as injection: | |
11086 | * clear this one and block further NMIs. | |
11087 | */ | |
11088 | vcpu->arch.nmi_pending = 0; | |
11089 | vmx_set_nmi_mask(vcpu, true); | |
11090 | return 0; | |
11091 | } | |
11092 | ||
11093 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && | |
11094 | nested_exit_on_intr(vcpu)) { | |
917dc606 | 11095 | if (block_nested_events) |
b6b8a145 JK |
11096 | return -EBUSY; |
11097 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); | |
705699a1 | 11098 | return 0; |
b6b8a145 JK |
11099 | } |
11100 | ||
6342c50a DH |
11101 | vmx_complete_nested_posted_interrupt(vcpu); |
11102 | return 0; | |
b6b8a145 JK |
11103 | } |
11104 | ||
f4124500 JK |
11105 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
11106 | { | |
11107 | ktime_t remaining = | |
11108 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); | |
11109 | u64 value; | |
11110 | ||
11111 | if (ktime_to_ns(remaining) <= 0) | |
11112 | return 0; | |
11113 | ||
11114 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; | |
11115 | do_div(value, 1000000); | |
11116 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
11117 | } | |
11118 | ||
4704d0be | 11119 | /* |
cf8b84f4 JM |
11120 | * Update the guest state fields of vmcs12 to reflect changes that |
11121 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the | |
11122 | * VM-entry controls is also updated, since this is really a guest | |
11123 | * state bit.) | |
4704d0be | 11124 | */ |
cf8b84f4 | 11125 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
4704d0be | 11126 | { |
4704d0be NHE |
11127 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
11128 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); | |
11129 | ||
4704d0be NHE |
11130 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
11131 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); | |
11132 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); | |
11133 | ||
11134 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); | |
11135 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); | |
11136 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); | |
11137 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); | |
11138 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); | |
11139 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); | |
11140 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); | |
11141 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); | |
11142 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); | |
11143 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); | |
11144 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); | |
11145 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); | |
11146 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); | |
11147 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); | |
11148 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); | |
11149 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); | |
11150 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); | |
11151 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); | |
11152 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); | |
11153 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); | |
11154 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); | |
11155 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); | |
11156 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); | |
11157 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); | |
11158 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); | |
11159 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); | |
11160 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); | |
11161 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); | |
11162 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); | |
11163 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); | |
11164 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); | |
11165 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); | |
11166 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); | |
11167 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); | |
11168 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); | |
11169 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); | |
11170 | ||
4704d0be NHE |
11171 | vmcs12->guest_interruptibility_info = |
11172 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
11173 | vmcs12->guest_pending_dbg_exceptions = | |
11174 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); | |
3edf1e69 JK |
11175 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
11176 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; | |
11177 | else | |
11178 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; | |
4704d0be | 11179 | |
f4124500 JK |
11180 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
11181 | if (vmcs12->vm_exit_controls & | |
11182 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) | |
11183 | vmcs12->vmx_preemption_timer_value = | |
11184 | vmx_get_preemption_timer_value(vcpu); | |
11185 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); | |
11186 | } | |
7854cbca | 11187 | |
3633cfc3 NHE |
11188 | /* |
11189 | * In some cases (usually, nested EPT), L2 is allowed to change its | |
11190 | * own CR3 without exiting. If it has changed it, we must keep it. | |
11191 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined | |
11192 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. | |
11193 | * | |
11194 | * Additionally, restore L2's PDPTR to vmcs12. | |
11195 | */ | |
11196 | if (enable_ept) { | |
f3531054 | 11197 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
3633cfc3 NHE |
11198 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
11199 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); | |
11200 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); | |
11201 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); | |
11202 | } | |
11203 | ||
d281e13b | 11204 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
119a9c01 | 11205 | |
608406e2 WV |
11206 | if (nested_cpu_has_vid(vmcs12)) |
11207 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); | |
11208 | ||
c18911a2 JK |
11209 | vmcs12->vm_entry_controls = |
11210 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | | |
2961e876 | 11211 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
c18911a2 | 11212 | |
2996fca0 JK |
11213 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
11214 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); | |
11215 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
11216 | } | |
11217 | ||
4704d0be NHE |
11218 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
11219 | * the relevant bit asks not to trap the change */ | |
b8c07d55 | 11220 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
4704d0be | 11221 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
10ba54a5 JK |
11222 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
11223 | vmcs12->guest_ia32_efer = vcpu->arch.efer; | |
4704d0be NHE |
11224 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
11225 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); | |
11226 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); | |
a87036ad | 11227 | if (kvm_mpx_supported()) |
36be0b9d | 11228 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
cf8b84f4 JM |
11229 | } |
11230 | ||
11231 | /* | |
11232 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits | |
11233 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), | |
11234 | * and this function updates it to reflect the changes to the guest state while | |
11235 | * L2 was running (and perhaps made some exits which were handled directly by L0 | |
11236 | * without going back to L1), and to reflect the exit reason. | |
11237 | * Note that we do not have to copy here all VMCS fields, just those that | |
11238 | * could have changed by the L2 guest or the exit - i.e., the guest-state and | |
11239 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, | |
11240 | * which already writes to vmcs12 directly. | |
11241 | */ | |
11242 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
11243 | u32 exit_reason, u32 exit_intr_info, | |
11244 | unsigned long exit_qualification) | |
11245 | { | |
11246 | /* update guest state fields: */ | |
11247 | sync_vmcs12(vcpu, vmcs12); | |
4704d0be NHE |
11248 | |
11249 | /* update exit information fields: */ | |
11250 | ||
533558bc JK |
11251 | vmcs12->vm_exit_reason = exit_reason; |
11252 | vmcs12->exit_qualification = exit_qualification; | |
533558bc | 11253 | vmcs12->vm_exit_intr_info = exit_intr_info; |
7313c698 | 11254 | |
5f3d5799 | 11255 | vmcs12->idt_vectoring_info_field = 0; |
4704d0be NHE |
11256 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
11257 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
11258 | ||
5f3d5799 | 11259 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
7cdc2d62 JM |
11260 | vmcs12->launch_state = 1; |
11261 | ||
5f3d5799 JK |
11262 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
11263 | * instead of reading the real value. */ | |
4704d0be | 11264 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
5f3d5799 JK |
11265 | |
11266 | /* | |
11267 | * Transfer the event that L0 or L1 may wanted to inject into | |
11268 | * L2 to IDT_VECTORING_INFO_FIELD. | |
11269 | */ | |
11270 | vmcs12_save_pending_event(vcpu, vmcs12); | |
11271 | } | |
11272 | ||
11273 | /* | |
11274 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is | |
11275 | * preserved above and would only end up incorrectly in L1. | |
11276 | */ | |
11277 | vcpu->arch.nmi_injected = false; | |
11278 | kvm_clear_exception_queue(vcpu); | |
11279 | kvm_clear_interrupt_queue(vcpu); | |
4704d0be NHE |
11280 | } |
11281 | ||
5af41573 WL |
11282 | static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu, |
11283 | struct vmcs12 *vmcs12) | |
11284 | { | |
11285 | u32 entry_failure_code; | |
11286 | ||
11287 | nested_ept_uninit_mmu_context(vcpu); | |
11288 | ||
11289 | /* | |
11290 | * Only PDPTE load can fail as the value of cr3 was checked on entry and | |
11291 | * couldn't have changed. | |
11292 | */ | |
11293 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) | |
11294 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); | |
11295 | ||
11296 | if (!enable_ept) | |
11297 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; | |
11298 | } | |
11299 | ||
4704d0be NHE |
11300 | /* |
11301 | * A part of what we need to when the nested L2 guest exits and we want to | |
11302 | * run its L1 parent, is to reset L1's guest state to the host state specified | |
11303 | * in vmcs12. | |
11304 | * This function is to be called not only on normal nested exit, but also on | |
11305 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry | |
11306 | * Failures During or After Loading Guest State"). | |
11307 | * This function should be called when the active VMCS is L1's (vmcs01). | |
11308 | */ | |
733568f9 JK |
11309 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
11310 | struct vmcs12 *vmcs12) | |
4704d0be | 11311 | { |
21feb4eb ACL |
11312 | struct kvm_segment seg; |
11313 | ||
4704d0be NHE |
11314 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
11315 | vcpu->arch.efer = vmcs12->host_ia32_efer; | |
d1fa0352 | 11316 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
4704d0be NHE |
11317 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
11318 | else | |
11319 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
11320 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
11321 | ||
11322 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); | |
11323 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); | |
1adfa76a | 11324 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
4704d0be NHE |
11325 | /* |
11326 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't | |
bd7e5b08 PB |
11327 | * actually changed, because vmx_set_cr0 refers to efer set above. |
11328 | * | |
11329 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 | |
11330 | * (KVM doesn't change it); | |
4704d0be | 11331 | */ |
bd7e5b08 | 11332 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
9e3e4dbf | 11333 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
4704d0be | 11334 | |
bd7e5b08 | 11335 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
4704d0be | 11336 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
8eb3f87d | 11337 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
4704d0be | 11338 | |
5af41573 | 11339 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
feaf0c7d | 11340 | |
4704d0be NHE |
11341 | if (enable_vpid) { |
11342 | /* | |
11343 | * Trivially support vpid by letting L2s share their parent | |
11344 | * L1's vpid. TODO: move to a more elaborate solution, giving | |
11345 | * each L2 its own vpid and exposing the vpid feature to L1. | |
11346 | */ | |
c2ba05cc | 11347 | vmx_flush_tlb(vcpu, true); |
4704d0be | 11348 | } |
06a5524f WV |
11349 | /* Restore posted intr vector. */ |
11350 | if (nested_cpu_has_posted_intr(vmcs12)) | |
11351 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); | |
4704d0be NHE |
11352 | |
11353 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); | |
11354 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); | |
11355 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); | |
11356 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); | |
11357 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); | |
21f2d551 LP |
11358 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
11359 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); | |
4704d0be | 11360 | |
36be0b9d PB |
11361 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
11362 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) | |
11363 | vmcs_write64(GUEST_BNDCFGS, 0); | |
11364 | ||
44811c02 | 11365 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
4704d0be | 11366 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
44811c02 JK |
11367 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
11368 | } | |
4704d0be NHE |
11369 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
11370 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, | |
11371 | vmcs12->host_ia32_perf_global_ctrl); | |
503cd0c5 | 11372 | |
21feb4eb ACL |
11373 | /* Set L1 segment info according to Intel SDM |
11374 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ | |
11375 | seg = (struct kvm_segment) { | |
11376 | .base = 0, | |
11377 | .limit = 0xFFFFFFFF, | |
11378 | .selector = vmcs12->host_cs_selector, | |
11379 | .type = 11, | |
11380 | .present = 1, | |
11381 | .s = 1, | |
11382 | .g = 1 | |
11383 | }; | |
11384 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) | |
11385 | seg.l = 1; | |
11386 | else | |
11387 | seg.db = 1; | |
11388 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); | |
11389 | seg = (struct kvm_segment) { | |
11390 | .base = 0, | |
11391 | .limit = 0xFFFFFFFF, | |
11392 | .type = 3, | |
11393 | .present = 1, | |
11394 | .s = 1, | |
11395 | .db = 1, | |
11396 | .g = 1 | |
11397 | }; | |
11398 | seg.selector = vmcs12->host_ds_selector; | |
11399 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); | |
11400 | seg.selector = vmcs12->host_es_selector; | |
11401 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); | |
11402 | seg.selector = vmcs12->host_ss_selector; | |
11403 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); | |
11404 | seg.selector = vmcs12->host_fs_selector; | |
11405 | seg.base = vmcs12->host_fs_base; | |
11406 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); | |
11407 | seg.selector = vmcs12->host_gs_selector; | |
11408 | seg.base = vmcs12->host_gs_base; | |
11409 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); | |
11410 | seg = (struct kvm_segment) { | |
205befd9 | 11411 | .base = vmcs12->host_tr_base, |
21feb4eb ACL |
11412 | .limit = 0x67, |
11413 | .selector = vmcs12->host_tr_selector, | |
11414 | .type = 11, | |
11415 | .present = 1 | |
11416 | }; | |
11417 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); | |
11418 | ||
503cd0c5 JK |
11419 | kvm_set_dr(vcpu, 7, 0x400); |
11420 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
ff651cb6 | 11421 | |
3af18d9c WV |
11422 | if (cpu_has_vmx_msr_bitmap()) |
11423 | vmx_set_msr_bitmap(vcpu); | |
11424 | ||
ff651cb6 WV |
11425 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
11426 | vmcs12->vm_exit_msr_load_count)) | |
11427 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); | |
4704d0be NHE |
11428 | } |
11429 | ||
11430 | /* | |
11431 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 | |
11432 | * and modify vmcs12 to make it see what it would expect to see there if | |
11433 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) | |
11434 | */ | |
533558bc JK |
11435 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
11436 | u32 exit_intr_info, | |
11437 | unsigned long exit_qualification) | |
4704d0be NHE |
11438 | { |
11439 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
4704d0be NHE |
11440 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
11441 | ||
5f3d5799 JK |
11442 | /* trying to cancel vmlaunch/vmresume is a bug */ |
11443 | WARN_ON_ONCE(vmx->nested.nested_run_pending); | |
11444 | ||
4f350c6d JM |
11445 | /* |
11446 | * The only expected VM-instruction error is "VM entry with | |
11447 | * invalid control field(s)." Anything else indicates a | |
11448 | * problem with L0. | |
11449 | */ | |
11450 | WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) != | |
11451 | VMXERR_ENTRY_INVALID_CONTROL_FIELD)); | |
11452 | ||
4704d0be | 11453 | leave_guest_mode(vcpu); |
4704d0be | 11454 | |
4f350c6d | 11455 | if (likely(!vmx->fail)) { |
72e9cbdb LP |
11456 | if (exit_reason == -1) |
11457 | sync_vmcs12(vcpu, vmcs12); | |
11458 | else | |
11459 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, | |
11460 | exit_qualification); | |
ff651cb6 | 11461 | |
4f350c6d JM |
11462 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
11463 | vmcs12->vm_exit_msr_store_count)) | |
11464 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); | |
11465 | } | |
cf3215d9 | 11466 | |
1279a6b1 | 11467 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
8391ce44 PB |
11468 | vm_entry_controls_reset_shadow(vmx); |
11469 | vm_exit_controls_reset_shadow(vmx); | |
36c3cc42 JK |
11470 | vmx_segment_cache_clear(vmx); |
11471 | ||
9314006d | 11472 | /* Update any VMCS fields that might have changed while L2 ran */ |
83bafef1 JM |
11473 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
11474 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
ea26e4ec | 11475 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
9314006d PB |
11476 | if (vmx->hv_deadline_tsc == -1) |
11477 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11478 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
11479 | else | |
11480 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11481 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c95ba92a PF |
11482 | if (kvm_has_tsc_control) |
11483 | decache_tsc_multiplier(vmx); | |
4704d0be | 11484 | |
dccbfcf5 RK |
11485 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
11486 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; | |
11487 | vmx_set_virtual_x2apic_mode(vcpu, | |
11488 | vcpu->arch.apic_base & X2APIC_ENABLE); | |
fb6c8198 JM |
11489 | } else if (!nested_cpu_has_ept(vmcs12) && |
11490 | nested_cpu_has2(vmcs12, | |
11491 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
11492 | vmx_flush_tlb_ept_only(vcpu); | |
dccbfcf5 | 11493 | } |
4704d0be NHE |
11494 | |
11495 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ | |
11496 | vmx->host_rsp = 0; | |
11497 | ||
11498 | /* Unpin physical memory we referred to in vmcs02 */ | |
11499 | if (vmx->nested.apic_access_page) { | |
53a70daf | 11500 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
48d89b92 | 11501 | vmx->nested.apic_access_page = NULL; |
4704d0be | 11502 | } |
a7c0b07d | 11503 | if (vmx->nested.virtual_apic_page) { |
53a70daf | 11504 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
48d89b92 | 11505 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 11506 | } |
705699a1 WV |
11507 | if (vmx->nested.pi_desc_page) { |
11508 | kunmap(vmx->nested.pi_desc_page); | |
53a70daf | 11509 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
705699a1 WV |
11510 | vmx->nested.pi_desc_page = NULL; |
11511 | vmx->nested.pi_desc = NULL; | |
11512 | } | |
4704d0be | 11513 | |
38b99173 TC |
11514 | /* |
11515 | * We are now running in L2, mmu_notifier will force to reload the | |
11516 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. | |
11517 | */ | |
c83b6d15 | 11518 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
38b99173 | 11519 | |
72e9cbdb | 11520 | if (enable_shadow_vmcs && exit_reason != -1) |
012f83cb | 11521 | vmx->nested.sync_shadow_vmcs = true; |
b6b8a145 JK |
11522 | |
11523 | /* in case we halted in L2 */ | |
11524 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
4f350c6d JM |
11525 | |
11526 | if (likely(!vmx->fail)) { | |
11527 | /* | |
11528 | * TODO: SDM says that with acknowledge interrupt on | |
11529 | * exit, bit 31 of the VM-exit interrupt information | |
11530 | * (valid interrupt) is always set to 1 on | |
11531 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't | |
11532 | * need kvm_cpu_has_interrupt(). See the commit | |
11533 | * message for details. | |
11534 | */ | |
11535 | if (nested_exit_intr_ack_set(vcpu) && | |
11536 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && | |
11537 | kvm_cpu_has_interrupt(vcpu)) { | |
11538 | int irq = kvm_cpu_get_interrupt(vcpu); | |
11539 | WARN_ON(irq < 0); | |
11540 | vmcs12->vm_exit_intr_info = irq | | |
11541 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; | |
11542 | } | |
11543 | ||
72e9cbdb LP |
11544 | if (exit_reason != -1) |
11545 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, | |
11546 | vmcs12->exit_qualification, | |
11547 | vmcs12->idt_vectoring_info_field, | |
11548 | vmcs12->vm_exit_intr_info, | |
11549 | vmcs12->vm_exit_intr_error_code, | |
11550 | KVM_ISA_VMX); | |
4f350c6d JM |
11551 | |
11552 | load_vmcs12_host_state(vcpu, vmcs12); | |
11553 | ||
11554 | return; | |
11555 | } | |
11556 | ||
11557 | /* | |
11558 | * After an early L2 VM-entry failure, we're now back | |
11559 | * in L1 which thinks it just finished a VMLAUNCH or | |
11560 | * VMRESUME instruction, so we need to set the failure | |
11561 | * flag and the VM-instruction error field of the VMCS | |
11562 | * accordingly. | |
11563 | */ | |
11564 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); | |
5af41573 WL |
11565 | |
11566 | load_vmcs12_mmu_host_state(vcpu, vmcs12); | |
11567 | ||
4f350c6d JM |
11568 | /* |
11569 | * The emulated instruction was already skipped in | |
11570 | * nested_vmx_run, but the updated RIP was never | |
11571 | * written back to the vmcs01. | |
11572 | */ | |
11573 | skip_emulated_instruction(vcpu); | |
11574 | vmx->fail = 0; | |
4704d0be NHE |
11575 | } |
11576 | ||
42124925 JK |
11577 | /* |
11578 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. | |
11579 | */ | |
11580 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) | |
11581 | { | |
2f707d97 WL |
11582 | if (is_guest_mode(vcpu)) { |
11583 | to_vmx(vcpu)->nested.nested_run_pending = 0; | |
533558bc | 11584 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
2f707d97 | 11585 | } |
42124925 JK |
11586 | free_nested(to_vmx(vcpu)); |
11587 | } | |
11588 | ||
7c177938 NHE |
11589 | /* |
11590 | * L1's failure to enter L2 is a subset of a normal exit, as explained in | |
11591 | * 23.7 "VM-entry failures during or after loading guest state" (this also | |
11592 | * lists the acceptable exit-reason and exit-qualification parameters). | |
11593 | * It should only be called before L2 actually succeeded to run, and when | |
11594 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). | |
11595 | */ | |
11596 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, | |
11597 | struct vmcs12 *vmcs12, | |
11598 | u32 reason, unsigned long qualification) | |
11599 | { | |
11600 | load_vmcs12_host_state(vcpu, vmcs12); | |
11601 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; | |
11602 | vmcs12->exit_qualification = qualification; | |
11603 | nested_vmx_succeed(vcpu); | |
012f83cb AG |
11604 | if (enable_shadow_vmcs) |
11605 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; | |
7c177938 NHE |
11606 | } |
11607 | ||
8a76d7f2 JR |
11608 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
11609 | struct x86_instruction_info *info, | |
11610 | enum x86_intercept_stage stage) | |
11611 | { | |
fb6d4d34 PB |
11612 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
11613 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; | |
11614 | ||
11615 | /* | |
11616 | * RDPID causes #UD if disabled through secondary execution controls. | |
11617 | * Because it is marked as EmulateOnUD, we need to intercept it here. | |
11618 | */ | |
11619 | if (info->intercept == x86_intercept_rdtscp && | |
11620 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { | |
11621 | ctxt->exception.vector = UD_VECTOR; | |
11622 | ctxt->exception.error_code_valid = false; | |
11623 | return X86EMUL_PROPAGATE_FAULT; | |
11624 | } | |
11625 | ||
11626 | /* TODO: check more intercepts... */ | |
8a76d7f2 JR |
11627 | return X86EMUL_CONTINUE; |
11628 | } | |
11629 | ||
64672c95 YJ |
11630 | #ifdef CONFIG_X86_64 |
11631 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ | |
11632 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, | |
11633 | u64 divisor, u64 *result) | |
11634 | { | |
11635 | u64 low = a << shift, high = a >> (64 - shift); | |
11636 | ||
11637 | /* To avoid the overflow on divq */ | |
11638 | if (high >= divisor) | |
11639 | return 1; | |
11640 | ||
11641 | /* Low hold the result, high hold rem which is discarded */ | |
11642 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : | |
11643 | "rm" (divisor), "0" (low), "1" (high)); | |
11644 | *result = low; | |
11645 | ||
11646 | return 0; | |
11647 | } | |
11648 | ||
11649 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) | |
11650 | { | |
11651 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9175d2e9 PB |
11652 | u64 tscl = rdtsc(); |
11653 | u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); | |
11654 | u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; | |
64672c95 YJ |
11655 | |
11656 | /* Convert to host delta tsc if tsc scaling is enabled */ | |
11657 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && | |
11658 | u64_shl_div_u64(delta_tsc, | |
11659 | kvm_tsc_scaling_ratio_frac_bits, | |
11660 | vcpu->arch.tsc_scaling_ratio, | |
11661 | &delta_tsc)) | |
11662 | return -ERANGE; | |
11663 | ||
11664 | /* | |
11665 | * If the delta tsc can't fit in the 32 bit after the multi shift, | |
11666 | * we can't use the preemption timer. | |
11667 | * It's possible that it fits on later vmentries, but checking | |
11668 | * on every vmentry is costly so we just use an hrtimer. | |
11669 | */ | |
11670 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) | |
11671 | return -ERANGE; | |
11672 | ||
11673 | vmx->hv_deadline_tsc = tscl + delta_tsc; | |
11674 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11675 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c8533544 WL |
11676 | |
11677 | return delta_tsc == 0; | |
64672c95 YJ |
11678 | } |
11679 | ||
11680 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) | |
11681 | { | |
11682 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
11683 | vmx->hv_deadline_tsc = -1; | |
11684 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11685 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
11686 | } | |
11687 | #endif | |
11688 | ||
48d89b92 | 11689 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
ae97a3b8 | 11690 | { |
b4a2d31d RK |
11691 | if (ple_gap) |
11692 | shrink_ple_window(vcpu); | |
ae97a3b8 RK |
11693 | } |
11694 | ||
843e4330 KH |
11695 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
11696 | struct kvm_memory_slot *slot) | |
11697 | { | |
11698 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); | |
11699 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); | |
11700 | } | |
11701 | ||
11702 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, | |
11703 | struct kvm_memory_slot *slot) | |
11704 | { | |
11705 | kvm_mmu_slot_set_dirty(kvm, slot); | |
11706 | } | |
11707 | ||
11708 | static void vmx_flush_log_dirty(struct kvm *kvm) | |
11709 | { | |
11710 | kvm_flush_pml_buffers(kvm); | |
11711 | } | |
11712 | ||
c5f983f6 BD |
11713 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
11714 | { | |
11715 | struct vmcs12 *vmcs12; | |
11716 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
11717 | gpa_t gpa; | |
11718 | struct page *page = NULL; | |
11719 | u64 *pml_address; | |
11720 | ||
11721 | if (is_guest_mode(vcpu)) { | |
11722 | WARN_ON_ONCE(vmx->nested.pml_full); | |
11723 | ||
11724 | /* | |
11725 | * Check if PML is enabled for the nested guest. | |
11726 | * Whether eptp bit 6 is set is already checked | |
11727 | * as part of A/D emulation. | |
11728 | */ | |
11729 | vmcs12 = get_vmcs12(vcpu); | |
11730 | if (!nested_cpu_has_pml(vmcs12)) | |
11731 | return 0; | |
11732 | ||
4769886b | 11733 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
c5f983f6 BD |
11734 | vmx->nested.pml_full = true; |
11735 | return 1; | |
11736 | } | |
11737 | ||
11738 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; | |
11739 | ||
5e2f30b7 DH |
11740 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
11741 | if (is_error_page(page)) | |
c5f983f6 BD |
11742 | return 0; |
11743 | ||
11744 | pml_address = kmap(page); | |
11745 | pml_address[vmcs12->guest_pml_index--] = gpa; | |
11746 | kunmap(page); | |
53a70daf | 11747 | kvm_release_page_clean(page); |
c5f983f6 BD |
11748 | } |
11749 | ||
11750 | return 0; | |
11751 | } | |
11752 | ||
843e4330 KH |
11753 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
11754 | struct kvm_memory_slot *memslot, | |
11755 | gfn_t offset, unsigned long mask) | |
11756 | { | |
11757 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); | |
11758 | } | |
11759 | ||
cd39e117 PB |
11760 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
11761 | { | |
11762 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
11763 | struct pi_desc old, new; | |
11764 | unsigned int dest; | |
cd39e117 PB |
11765 | |
11766 | do { | |
11767 | old.control = new.control = pi_desc->control; | |
8b306e2f PB |
11768 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
11769 | "Wakeup handler not enabled while the VCPU is blocked\n"); | |
cd39e117 PB |
11770 | |
11771 | dest = cpu_physical_id(vcpu->cpu); | |
11772 | ||
11773 | if (x2apic_enabled()) | |
11774 | new.ndst = dest; | |
11775 | else | |
11776 | new.ndst = (dest << 8) & 0xFF00; | |
11777 | ||
cd39e117 PB |
11778 | /* set 'NV' to 'notification vector' */ |
11779 | new.nv = POSTED_INTR_VECTOR; | |
c0a1666b PB |
11780 | } while (cmpxchg64(&pi_desc->control, old.control, |
11781 | new.control) != old.control); | |
cd39e117 | 11782 | |
8b306e2f PB |
11783 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
11784 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
cd39e117 | 11785 | list_del(&vcpu->blocked_vcpu_list); |
8b306e2f | 11786 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
cd39e117 PB |
11787 | vcpu->pre_pcpu = -1; |
11788 | } | |
11789 | } | |
11790 | ||
bf9f6ac8 FW |
11791 | /* |
11792 | * This routine does the following things for vCPU which is going | |
11793 | * to be blocked if VT-d PI is enabled. | |
11794 | * - Store the vCPU to the wakeup list, so when interrupts happen | |
11795 | * we can find the right vCPU to wake up. | |
11796 | * - Change the Posted-interrupt descriptor as below: | |
11797 | * 'NDST' <-- vcpu->pre_pcpu | |
11798 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR | |
11799 | * - If 'ON' is set during this process, which means at least one | |
11800 | * interrupt is posted for this vCPU, we cannot block it, in | |
11801 | * this case, return 1, otherwise, return 0. | |
11802 | * | |
11803 | */ | |
bc22512b | 11804 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
bf9f6ac8 | 11805 | { |
bf9f6ac8 FW |
11806 | unsigned int dest; |
11807 | struct pi_desc old, new; | |
11808 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
11809 | ||
11810 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
11811 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
11812 | !kvm_vcpu_apicv_active(vcpu)) | |
bf9f6ac8 FW |
11813 | return 0; |
11814 | ||
8b306e2f PB |
11815 | WARN_ON(irqs_disabled()); |
11816 | local_irq_disable(); | |
11817 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { | |
11818 | vcpu->pre_pcpu = vcpu->cpu; | |
11819 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
11820 | list_add_tail(&vcpu->blocked_vcpu_list, | |
11821 | &per_cpu(blocked_vcpu_on_cpu, | |
11822 | vcpu->pre_pcpu)); | |
11823 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
11824 | } | |
bf9f6ac8 FW |
11825 | |
11826 | do { | |
11827 | old.control = new.control = pi_desc->control; | |
11828 | ||
bf9f6ac8 FW |
11829 | WARN((pi_desc->sn == 1), |
11830 | "Warning: SN field of posted-interrupts " | |
11831 | "is set before blocking\n"); | |
11832 | ||
11833 | /* | |
11834 | * Since vCPU can be preempted during this process, | |
11835 | * vcpu->cpu could be different with pre_pcpu, we | |
11836 | * need to set pre_pcpu as the destination of wakeup | |
11837 | * notification event, then we can find the right vCPU | |
11838 | * to wakeup in wakeup handler if interrupts happen | |
11839 | * when the vCPU is in blocked state. | |
11840 | */ | |
11841 | dest = cpu_physical_id(vcpu->pre_pcpu); | |
11842 | ||
11843 | if (x2apic_enabled()) | |
11844 | new.ndst = dest; | |
11845 | else | |
11846 | new.ndst = (dest << 8) & 0xFF00; | |
11847 | ||
11848 | /* set 'NV' to 'wakeup vector' */ | |
11849 | new.nv = POSTED_INTR_WAKEUP_VECTOR; | |
c0a1666b PB |
11850 | } while (cmpxchg64(&pi_desc->control, old.control, |
11851 | new.control) != old.control); | |
bf9f6ac8 | 11852 | |
8b306e2f PB |
11853 | /* We should not block the vCPU if an interrupt is posted for it. */ |
11854 | if (pi_test_on(pi_desc) == 1) | |
11855 | __pi_post_block(vcpu); | |
11856 | ||
11857 | local_irq_enable(); | |
11858 | return (vcpu->pre_pcpu == -1); | |
bf9f6ac8 FW |
11859 | } |
11860 | ||
bc22512b YJ |
11861 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
11862 | { | |
11863 | if (pi_pre_block(vcpu)) | |
11864 | return 1; | |
11865 | ||
64672c95 YJ |
11866 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
11867 | kvm_lapic_switch_to_sw_timer(vcpu); | |
11868 | ||
bc22512b YJ |
11869 | return 0; |
11870 | } | |
11871 | ||
11872 | static void pi_post_block(struct kvm_vcpu *vcpu) | |
bf9f6ac8 | 11873 | { |
8b306e2f | 11874 | if (vcpu->pre_pcpu == -1) |
bf9f6ac8 FW |
11875 | return; |
11876 | ||
8b306e2f PB |
11877 | WARN_ON(irqs_disabled()); |
11878 | local_irq_disable(); | |
cd39e117 | 11879 | __pi_post_block(vcpu); |
8b306e2f | 11880 | local_irq_enable(); |
bf9f6ac8 FW |
11881 | } |
11882 | ||
bc22512b YJ |
11883 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
11884 | { | |
64672c95 YJ |
11885 | if (kvm_x86_ops->set_hv_timer) |
11886 | kvm_lapic_switch_to_hv_timer(vcpu); | |
11887 | ||
bc22512b YJ |
11888 | pi_post_block(vcpu); |
11889 | } | |
11890 | ||
efc64404 FW |
11891 | /* |
11892 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts | |
11893 | * | |
11894 | * @kvm: kvm | |
11895 | * @host_irq: host irq of the interrupt | |
11896 | * @guest_irq: gsi of the interrupt | |
11897 | * @set: set or unset PI | |
11898 | * returns 0 on success, < 0 on failure | |
11899 | */ | |
11900 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, | |
11901 | uint32_t guest_irq, bool set) | |
11902 | { | |
11903 | struct kvm_kernel_irq_routing_entry *e; | |
11904 | struct kvm_irq_routing_table *irq_rt; | |
11905 | struct kvm_lapic_irq irq; | |
11906 | struct kvm_vcpu *vcpu; | |
11907 | struct vcpu_data vcpu_info; | |
3a8b0677 | 11908 | int idx, ret = 0; |
efc64404 FW |
11909 | |
11910 | if (!kvm_arch_has_assigned_device(kvm) || | |
a0052191 YZ |
11911 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
11912 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) | |
efc64404 FW |
11913 | return 0; |
11914 | ||
11915 | idx = srcu_read_lock(&kvm->irq_srcu); | |
11916 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); | |
3a8b0677 JS |
11917 | if (guest_irq >= irq_rt->nr_rt_entries || |
11918 | hlist_empty(&irq_rt->map[guest_irq])) { | |
11919 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", | |
11920 | guest_irq, irq_rt->nr_rt_entries); | |
11921 | goto out; | |
11922 | } | |
efc64404 FW |
11923 | |
11924 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { | |
11925 | if (e->type != KVM_IRQ_ROUTING_MSI) | |
11926 | continue; | |
11927 | /* | |
11928 | * VT-d PI cannot support posting multicast/broadcast | |
11929 | * interrupts to a vCPU, we still use interrupt remapping | |
11930 | * for these kind of interrupts. | |
11931 | * | |
11932 | * For lowest-priority interrupts, we only support | |
11933 | * those with single CPU as the destination, e.g. user | |
11934 | * configures the interrupts via /proc/irq or uses | |
11935 | * irqbalance to make the interrupts single-CPU. | |
11936 | * | |
11937 | * We will support full lowest-priority interrupt later. | |
11938 | */ | |
11939 | ||
37131313 | 11940 | kvm_set_msi_irq(kvm, e, &irq); |
23a1c257 FW |
11941 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
11942 | /* | |
11943 | * Make sure the IRTE is in remapped mode if | |
11944 | * we don't handle it in posted mode. | |
11945 | */ | |
11946 | ret = irq_set_vcpu_affinity(host_irq, NULL); | |
11947 | if (ret < 0) { | |
11948 | printk(KERN_INFO | |
11949 | "failed to back to remapped mode, irq: %u\n", | |
11950 | host_irq); | |
11951 | goto out; | |
11952 | } | |
11953 | ||
efc64404 | 11954 | continue; |
23a1c257 | 11955 | } |
efc64404 FW |
11956 | |
11957 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); | |
11958 | vcpu_info.vector = irq.vector; | |
11959 | ||
b6ce9780 | 11960 | trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi, |
efc64404 FW |
11961 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
11962 | ||
11963 | if (set) | |
11964 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); | |
dc91f2eb | 11965 | else |
efc64404 | 11966 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
efc64404 FW |
11967 | |
11968 | if (ret < 0) { | |
11969 | printk(KERN_INFO "%s: failed to update PI IRTE\n", | |
11970 | __func__); | |
11971 | goto out; | |
11972 | } | |
11973 | } | |
11974 | ||
11975 | ret = 0; | |
11976 | out: | |
11977 | srcu_read_unlock(&kvm->irq_srcu, idx); | |
11978 | return ret; | |
11979 | } | |
11980 | ||
c45dcc71 AR |
11981 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
11982 | { | |
11983 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) | |
11984 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
11985 | FEATURE_CONTROL_LMCE; | |
11986 | else | |
11987 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
11988 | ~FEATURE_CONTROL_LMCE; | |
11989 | } | |
11990 | ||
72d7b374 LP |
11991 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
11992 | { | |
72e9cbdb LP |
11993 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
11994 | if (to_vmx(vcpu)->nested.nested_run_pending) | |
11995 | return 0; | |
72d7b374 LP |
11996 | return 1; |
11997 | } | |
11998 | ||
0234bf88 LP |
11999 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
12000 | { | |
72e9cbdb LP |
12001 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
12002 | ||
12003 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); | |
12004 | if (vmx->nested.smm.guest_mode) | |
12005 | nested_vmx_vmexit(vcpu, -1, 0, 0); | |
12006 | ||
12007 | vmx->nested.smm.vmxon = vmx->nested.vmxon; | |
12008 | vmx->nested.vmxon = false; | |
0234bf88 LP |
12009 | return 0; |
12010 | } | |
12011 | ||
12012 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) | |
12013 | { | |
72e9cbdb LP |
12014 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
12015 | int ret; | |
12016 | ||
12017 | if (vmx->nested.smm.vmxon) { | |
12018 | vmx->nested.vmxon = true; | |
12019 | vmx->nested.smm.vmxon = false; | |
12020 | } | |
12021 | ||
12022 | if (vmx->nested.smm.guest_mode) { | |
12023 | vcpu->arch.hflags &= ~HF_SMM_MASK; | |
12024 | ret = enter_vmx_non_root_mode(vcpu, false); | |
12025 | vcpu->arch.hflags |= HF_SMM_MASK; | |
12026 | if (ret) | |
12027 | return ret; | |
12028 | ||
12029 | vmx->nested.smm.guest_mode = false; | |
12030 | } | |
0234bf88 LP |
12031 | return 0; |
12032 | } | |
12033 | ||
cc3d967f LP |
12034 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
12035 | { | |
12036 | return 0; | |
12037 | } | |
12038 | ||
404f6aac | 12039 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
6aa8b732 AK |
12040 | .cpu_has_kvm_support = cpu_has_kvm_support, |
12041 | .disabled_by_bios = vmx_disabled_by_bios, | |
12042 | .hardware_setup = hardware_setup, | |
12043 | .hardware_unsetup = hardware_unsetup, | |
002c7f7c | 12044 | .check_processor_compatibility = vmx_check_processor_compat, |
6aa8b732 AK |
12045 | .hardware_enable = hardware_enable, |
12046 | .hardware_disable = hardware_disable, | |
04547156 | 12047 | .cpu_has_accelerated_tpr = report_flexpriority, |
6d396b55 | 12048 | .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, |
6aa8b732 AK |
12049 | |
12050 | .vcpu_create = vmx_create_vcpu, | |
12051 | .vcpu_free = vmx_free_vcpu, | |
04d2cc77 | 12052 | .vcpu_reset = vmx_vcpu_reset, |
6aa8b732 | 12053 | |
04d2cc77 | 12054 | .prepare_guest_switch = vmx_save_host_state, |
6aa8b732 AK |
12055 | .vcpu_load = vmx_vcpu_load, |
12056 | .vcpu_put = vmx_vcpu_put, | |
12057 | ||
a96036b8 | 12058 | .update_bp_intercept = update_exception_bitmap, |
6aa8b732 AK |
12059 | .get_msr = vmx_get_msr, |
12060 | .set_msr = vmx_set_msr, | |
12061 | .get_segment_base = vmx_get_segment_base, | |
12062 | .get_segment = vmx_get_segment, | |
12063 | .set_segment = vmx_set_segment, | |
2e4d2653 | 12064 | .get_cpl = vmx_get_cpl, |
6aa8b732 | 12065 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
e8467fda | 12066 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
aff48baa | 12067 | .decache_cr3 = vmx_decache_cr3, |
25c4c276 | 12068 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
6aa8b732 | 12069 | .set_cr0 = vmx_set_cr0, |
6aa8b732 AK |
12070 | .set_cr3 = vmx_set_cr3, |
12071 | .set_cr4 = vmx_set_cr4, | |
6aa8b732 | 12072 | .set_efer = vmx_set_efer, |
6aa8b732 AK |
12073 | .get_idt = vmx_get_idt, |
12074 | .set_idt = vmx_set_idt, | |
12075 | .get_gdt = vmx_get_gdt, | |
12076 | .set_gdt = vmx_set_gdt, | |
73aaf249 JK |
12077 | .get_dr6 = vmx_get_dr6, |
12078 | .set_dr6 = vmx_set_dr6, | |
020df079 | 12079 | .set_dr7 = vmx_set_dr7, |
81908bf4 | 12080 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
5fdbf976 | 12081 | .cache_reg = vmx_cache_reg, |
6aa8b732 AK |
12082 | .get_rflags = vmx_get_rflags, |
12083 | .set_rflags = vmx_set_rflags, | |
be94f6b7 | 12084 | |
6aa8b732 | 12085 | .tlb_flush = vmx_flush_tlb, |
6aa8b732 | 12086 | |
6aa8b732 | 12087 | .run = vmx_vcpu_run, |
6062d012 | 12088 | .handle_exit = vmx_handle_exit, |
6aa8b732 | 12089 | .skip_emulated_instruction = skip_emulated_instruction, |
2809f5d2 GC |
12090 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
12091 | .get_interrupt_shadow = vmx_get_interrupt_shadow, | |
102d8325 | 12092 | .patch_hypercall = vmx_patch_hypercall, |
2a8067f1 | 12093 | .set_irq = vmx_inject_irq, |
95ba8273 | 12094 | .set_nmi = vmx_inject_nmi, |
298101da | 12095 | .queue_exception = vmx_queue_exception, |
b463a6f7 | 12096 | .cancel_injection = vmx_cancel_injection, |
78646121 | 12097 | .interrupt_allowed = vmx_interrupt_allowed, |
95ba8273 | 12098 | .nmi_allowed = vmx_nmi_allowed, |
3cfc3092 JK |
12099 | .get_nmi_mask = vmx_get_nmi_mask, |
12100 | .set_nmi_mask = vmx_set_nmi_mask, | |
95ba8273 GN |
12101 | .enable_nmi_window = enable_nmi_window, |
12102 | .enable_irq_window = enable_irq_window, | |
12103 | .update_cr8_intercept = update_cr8_intercept, | |
8d14695f | 12104 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
38b99173 | 12105 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
d62caabb AS |
12106 | .get_enable_apicv = vmx_get_enable_apicv, |
12107 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, | |
c7c9c56c | 12108 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
967235d3 | 12109 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
c7c9c56c YZ |
12110 | .hwapic_irr_update = vmx_hwapic_irr_update, |
12111 | .hwapic_isr_update = vmx_hwapic_isr_update, | |
a20ed54d YZ |
12112 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
12113 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, | |
95ba8273 | 12114 | |
cbc94022 | 12115 | .set_tss_addr = vmx_set_tss_addr, |
67253af5 | 12116 | .get_tdp_level = get_ept_level, |
4b12f0de | 12117 | .get_mt_mask = vmx_get_mt_mask, |
229456fc | 12118 | |
586f9607 | 12119 | .get_exit_info = vmx_get_exit_info, |
586f9607 | 12120 | |
17cc3935 | 12121 | .get_lpage_level = vmx_get_lpage_level, |
0e851880 SY |
12122 | |
12123 | .cpuid_update = vmx_cpuid_update, | |
4e47c7a6 SY |
12124 | |
12125 | .rdtscp_supported = vmx_rdtscp_supported, | |
ad756a16 | 12126 | .invpcid_supported = vmx_invpcid_supported, |
d4330ef2 JR |
12127 | |
12128 | .set_supported_cpuid = vmx_set_supported_cpuid, | |
f5f48ee1 SY |
12129 | |
12130 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, | |
99e3e30a ZA |
12131 | |
12132 | .write_tsc_offset = vmx_write_tsc_offset, | |
1c97f0a0 JR |
12133 | |
12134 | .set_tdp_cr3 = vmx_set_cr3, | |
8a76d7f2 JR |
12135 | |
12136 | .check_intercept = vmx_check_intercept, | |
a547c6db | 12137 | .handle_external_intr = vmx_handle_external_intr, |
da8999d3 | 12138 | .mpx_supported = vmx_mpx_supported, |
55412b2e | 12139 | .xsaves_supported = vmx_xsaves_supported, |
66336cab | 12140 | .umip_emulated = vmx_umip_emulated, |
b6b8a145 JK |
12141 | |
12142 | .check_nested_events = vmx_check_nested_events, | |
ae97a3b8 RK |
12143 | |
12144 | .sched_in = vmx_sched_in, | |
843e4330 KH |
12145 | |
12146 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, | |
12147 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, | |
12148 | .flush_log_dirty = vmx_flush_log_dirty, | |
12149 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, | |
c5f983f6 | 12150 | .write_log_dirty = vmx_write_pml_buffer, |
25462f7f | 12151 | |
bf9f6ac8 FW |
12152 | .pre_block = vmx_pre_block, |
12153 | .post_block = vmx_post_block, | |
12154 | ||
25462f7f | 12155 | .pmu_ops = &intel_pmu_ops, |
efc64404 FW |
12156 | |
12157 | .update_pi_irte = vmx_update_pi_irte, | |
64672c95 YJ |
12158 | |
12159 | #ifdef CONFIG_X86_64 | |
12160 | .set_hv_timer = vmx_set_hv_timer, | |
12161 | .cancel_hv_timer = vmx_cancel_hv_timer, | |
12162 | #endif | |
c45dcc71 AR |
12163 | |
12164 | .setup_mce = vmx_setup_mce, | |
0234bf88 | 12165 | |
72d7b374 | 12166 | .smi_allowed = vmx_smi_allowed, |
0234bf88 LP |
12167 | .pre_enter_smm = vmx_pre_enter_smm, |
12168 | .pre_leave_smm = vmx_pre_leave_smm, | |
cc3d967f | 12169 | .enable_smi_window = enable_smi_window, |
6aa8b732 AK |
12170 | }; |
12171 | ||
12172 | static int __init vmx_init(void) | |
12173 | { | |
34a1cd60 TC |
12174 | int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
12175 | __alignof__(struct vcpu_vmx), THIS_MODULE); | |
fdef3ad1 | 12176 | if (r) |
34a1cd60 | 12177 | return r; |
25c5f225 | 12178 | |
2965faa5 | 12179 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
12180 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
12181 | crash_vmclear_local_loaded_vmcss); | |
12182 | #endif | |
12183 | ||
fdef3ad1 | 12184 | return 0; |
6aa8b732 AK |
12185 | } |
12186 | ||
12187 | static void __exit vmx_exit(void) | |
12188 | { | |
2965faa5 | 12189 | #ifdef CONFIG_KEXEC_CORE |
3b63a43f | 12190 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
8f536b76 ZY |
12191 | synchronize_rcu(); |
12192 | #endif | |
12193 | ||
cb498ea2 | 12194 | kvm_exit(); |
6aa8b732 AK |
12195 | } |
12196 | ||
12197 | module_init(vmx_init) | |
12198 | module_exit(vmx_exit) |