]>
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> |
5fdbf976 | 36 | #include "kvm_cache_regs.h" |
35920a35 | 37 | #include "x86.h" |
e495606d | 38 | |
28b835d6 | 39 | #include <asm/cpu.h> |
6aa8b732 | 40 | #include <asm/io.h> |
3b3be0d1 | 41 | #include <asm/desc.h> |
13673a90 | 42 | #include <asm/vmx.h> |
6210e37b | 43 | #include <asm/virtext.h> |
a0861c02 | 44 | #include <asm/mce.h> |
952f07ec | 45 | #include <asm/fpu/internal.h> |
d7cd9796 | 46 | #include <asm/perf_event.h> |
81908bf4 | 47 | #include <asm/debugreg.h> |
8f536b76 | 48 | #include <asm/kexec.h> |
dab2087d | 49 | #include <asm/apic.h> |
efc64404 | 50 | #include <asm/irq_remapping.h> |
6aa8b732 | 51 | |
229456fc | 52 | #include "trace.h" |
25462f7f | 53 | #include "pmu.h" |
229456fc | 54 | |
4ecac3fd | 55 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
5e520e62 AK |
56 | #define __ex_clear(x, reg) \ |
57 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) | |
4ecac3fd | 58 | |
6aa8b732 AK |
59 | MODULE_AUTHOR("Qumranet"); |
60 | MODULE_LICENSE("GPL"); | |
61 | ||
e9bda3b3 JT |
62 | static const struct x86_cpu_id vmx_cpu_id[] = { |
63 | X86_FEATURE_MATCH(X86_FEATURE_VMX), | |
64 | {} | |
65 | }; | |
66 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); | |
67 | ||
476bc001 | 68 | static bool __read_mostly enable_vpid = 1; |
736caefe | 69 | module_param_named(vpid, enable_vpid, bool, 0444); |
2384d2b3 | 70 | |
476bc001 | 71 | static bool __read_mostly flexpriority_enabled = 1; |
736caefe | 72 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
4c9fc8ef | 73 | |
476bc001 | 74 | static bool __read_mostly enable_ept = 1; |
736caefe | 75 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
d56f546d | 76 | |
476bc001 | 77 | static bool __read_mostly enable_unrestricted_guest = 1; |
3a624e29 NK |
78 | module_param_named(unrestricted_guest, |
79 | enable_unrestricted_guest, bool, S_IRUGO); | |
80 | ||
83c3a331 XH |
81 | static bool __read_mostly enable_ept_ad_bits = 1; |
82 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); | |
83 | ||
a27685c3 | 84 | static bool __read_mostly emulate_invalid_guest_state = true; |
c1f8bc04 | 85 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
04fa4d32 | 86 | |
476bc001 | 87 | static bool __read_mostly fasteoi = 1; |
58fbbf26 KT |
88 | module_param(fasteoi, bool, S_IRUGO); |
89 | ||
5a71785d | 90 | static bool __read_mostly enable_apicv = 1; |
01e439be | 91 | module_param(enable_apicv, bool, S_IRUGO); |
83d4c286 | 92 | |
abc4fc58 AG |
93 | static bool __read_mostly enable_shadow_vmcs = 1; |
94 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); | |
801d3424 NHE |
95 | /* |
96 | * If nested=1, nested virtualization is supported, i.e., guests may use | |
97 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not | |
98 | * use VMX instructions. | |
99 | */ | |
476bc001 | 100 | static bool __read_mostly nested = 0; |
801d3424 NHE |
101 | module_param(nested, bool, S_IRUGO); |
102 | ||
20300099 WL |
103 | static u64 __read_mostly host_xss; |
104 | ||
843e4330 KH |
105 | static bool __read_mostly enable_pml = 1; |
106 | module_param_named(pml, enable_pml, bool, S_IRUGO); | |
107 | ||
64903d61 HZ |
108 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
109 | ||
64672c95 YJ |
110 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
111 | static int __read_mostly cpu_preemption_timer_multi; | |
112 | static bool __read_mostly enable_preemption_timer = 1; | |
113 | #ifdef CONFIG_X86_64 | |
114 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); | |
115 | #endif | |
116 | ||
5037878e GN |
117 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
118 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE) | |
cdc0e244 AK |
119 | #define KVM_VM_CR0_ALWAYS_ON \ |
120 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) | |
4c38609a AK |
121 | #define KVM_CR4_GUEST_OWNED_BITS \ |
122 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ | |
52ce3c21 | 123 | | X86_CR4_OSXMMEXCPT | X86_CR4_TSD) |
4c38609a | 124 | |
cdc0e244 AK |
125 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
126 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) | |
127 | ||
78ac8b47 AK |
128 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
129 | ||
f4124500 JK |
130 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
131 | ||
16c2aec6 JD |
132 | /* |
133 | * Hyper-V requires all of these, so mark them as supported even though | |
134 | * they are just treated the same as all-context. | |
135 | */ | |
136 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ | |
137 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ | |
138 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ | |
139 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ | |
140 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) | |
141 | ||
4b8d54f9 ZE |
142 | /* |
143 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: | |
144 | * ple_gap: upper bound on the amount of time between two successive | |
145 | * executions of PAUSE in a loop. Also indicate if ple enabled. | |
00c25bce | 146 | * According to test, this time is usually smaller than 128 cycles. |
4b8d54f9 ZE |
147 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
148 | * in a PAUSE loop. Tests indicate that most spinlocks are held for | |
149 | * less than 2^12 cycles | |
150 | * Time is measured based on a counter that runs at the same rate as the TSC, | |
151 | * refer SDM volume 3b section 21.6.13 & 22.1.3. | |
152 | */ | |
b4a2d31d RK |
153 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
154 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 | |
155 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 | |
156 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 | |
157 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ | |
158 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW | |
159 | ||
4b8d54f9 ZE |
160 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
161 | module_param(ple_gap, int, S_IRUGO); | |
162 | ||
163 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; | |
164 | module_param(ple_window, int, S_IRUGO); | |
165 | ||
b4a2d31d RK |
166 | /* Default doubles per-vcpu window every exit. */ |
167 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; | |
168 | module_param(ple_window_grow, int, S_IRUGO); | |
169 | ||
170 | /* Default resets per-vcpu window every exit to ple_window. */ | |
171 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; | |
172 | module_param(ple_window_shrink, int, S_IRUGO); | |
173 | ||
174 | /* Default is to compute the maximum so we can never overflow. */ | |
175 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; | |
176 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; | |
177 | module_param(ple_window_max, int, S_IRUGO); | |
178 | ||
83287ea4 AK |
179 | extern const ulong vmx_return; |
180 | ||
8bf00a52 | 181 | #define NR_AUTOLOAD_MSRS 8 |
ff2f6fe9 | 182 | #define VMCS02_POOL_SIZE 1 |
61d2ef2c | 183 | |
a2fa3e9f GH |
184 | struct vmcs { |
185 | u32 revision_id; | |
186 | u32 abort; | |
187 | char data[0]; | |
188 | }; | |
189 | ||
d462b819 NHE |
190 | /* |
191 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also | |
192 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs | |
193 | * loaded on this CPU (so we can clear them if the CPU goes down). | |
194 | */ | |
195 | struct loaded_vmcs { | |
196 | struct vmcs *vmcs; | |
355f4fb1 | 197 | struct vmcs *shadow_vmcs; |
d462b819 NHE |
198 | int cpu; |
199 | int launched; | |
200 | struct list_head loaded_vmcss_on_cpu_link; | |
201 | }; | |
202 | ||
26bb0981 AK |
203 | struct shared_msr_entry { |
204 | unsigned index; | |
205 | u64 data; | |
d5696725 | 206 | u64 mask; |
26bb0981 AK |
207 | }; |
208 | ||
a9d30f33 NHE |
209 | /* |
210 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a | |
211 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has | |
212 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is | |
213 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, | |
214 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. | |
215 | * More than one of these structures may exist, if L1 runs multiple L2 guests. | |
216 | * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the | |
217 | * underlying hardware which will be used to run L2. | |
218 | * This structure is packed to ensure that its layout is identical across | |
219 | * machines (necessary for live migration). | |
220 | * If there are changes in this struct, VMCS12_REVISION must be changed. | |
221 | */ | |
22bd0358 | 222 | typedef u64 natural_width; |
a9d30f33 NHE |
223 | struct __packed vmcs12 { |
224 | /* According to the Intel spec, a VMCS region must start with the | |
225 | * following two fields. Then follow implementation-specific data. | |
226 | */ | |
227 | u32 revision_id; | |
228 | u32 abort; | |
22bd0358 | 229 | |
27d6c865 NHE |
230 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
231 | u32 padding[7]; /* room for future expansion */ | |
232 | ||
22bd0358 NHE |
233 | u64 io_bitmap_a; |
234 | u64 io_bitmap_b; | |
235 | u64 msr_bitmap; | |
236 | u64 vm_exit_msr_store_addr; | |
237 | u64 vm_exit_msr_load_addr; | |
238 | u64 vm_entry_msr_load_addr; | |
239 | u64 tsc_offset; | |
240 | u64 virtual_apic_page_addr; | |
241 | u64 apic_access_addr; | |
705699a1 | 242 | u64 posted_intr_desc_addr; |
22bd0358 | 243 | u64 ept_pointer; |
608406e2 WV |
244 | u64 eoi_exit_bitmap0; |
245 | u64 eoi_exit_bitmap1; | |
246 | u64 eoi_exit_bitmap2; | |
247 | u64 eoi_exit_bitmap3; | |
81dc01f7 | 248 | u64 xss_exit_bitmap; |
22bd0358 NHE |
249 | u64 guest_physical_address; |
250 | u64 vmcs_link_pointer; | |
c5f983f6 | 251 | u64 pml_address; |
22bd0358 NHE |
252 | u64 guest_ia32_debugctl; |
253 | u64 guest_ia32_pat; | |
254 | u64 guest_ia32_efer; | |
255 | u64 guest_ia32_perf_global_ctrl; | |
256 | u64 guest_pdptr0; | |
257 | u64 guest_pdptr1; | |
258 | u64 guest_pdptr2; | |
259 | u64 guest_pdptr3; | |
36be0b9d | 260 | u64 guest_bndcfgs; |
22bd0358 NHE |
261 | u64 host_ia32_pat; |
262 | u64 host_ia32_efer; | |
263 | u64 host_ia32_perf_global_ctrl; | |
264 | u64 padding64[8]; /* room for future expansion */ | |
265 | /* | |
266 | * To allow migration of L1 (complete with its L2 guests) between | |
267 | * machines of different natural widths (32 or 64 bit), we cannot have | |
268 | * unsigned long fields with no explict size. We use u64 (aliased | |
269 | * natural_width) instead. Luckily, x86 is little-endian. | |
270 | */ | |
271 | natural_width cr0_guest_host_mask; | |
272 | natural_width cr4_guest_host_mask; | |
273 | natural_width cr0_read_shadow; | |
274 | natural_width cr4_read_shadow; | |
275 | natural_width cr3_target_value0; | |
276 | natural_width cr3_target_value1; | |
277 | natural_width cr3_target_value2; | |
278 | natural_width cr3_target_value3; | |
279 | natural_width exit_qualification; | |
280 | natural_width guest_linear_address; | |
281 | natural_width guest_cr0; | |
282 | natural_width guest_cr3; | |
283 | natural_width guest_cr4; | |
284 | natural_width guest_es_base; | |
285 | natural_width guest_cs_base; | |
286 | natural_width guest_ss_base; | |
287 | natural_width guest_ds_base; | |
288 | natural_width guest_fs_base; | |
289 | natural_width guest_gs_base; | |
290 | natural_width guest_ldtr_base; | |
291 | natural_width guest_tr_base; | |
292 | natural_width guest_gdtr_base; | |
293 | natural_width guest_idtr_base; | |
294 | natural_width guest_dr7; | |
295 | natural_width guest_rsp; | |
296 | natural_width guest_rip; | |
297 | natural_width guest_rflags; | |
298 | natural_width guest_pending_dbg_exceptions; | |
299 | natural_width guest_sysenter_esp; | |
300 | natural_width guest_sysenter_eip; | |
301 | natural_width host_cr0; | |
302 | natural_width host_cr3; | |
303 | natural_width host_cr4; | |
304 | natural_width host_fs_base; | |
305 | natural_width host_gs_base; | |
306 | natural_width host_tr_base; | |
307 | natural_width host_gdtr_base; | |
308 | natural_width host_idtr_base; | |
309 | natural_width host_ia32_sysenter_esp; | |
310 | natural_width host_ia32_sysenter_eip; | |
311 | natural_width host_rsp; | |
312 | natural_width host_rip; | |
313 | natural_width paddingl[8]; /* room for future expansion */ | |
314 | u32 pin_based_vm_exec_control; | |
315 | u32 cpu_based_vm_exec_control; | |
316 | u32 exception_bitmap; | |
317 | u32 page_fault_error_code_mask; | |
318 | u32 page_fault_error_code_match; | |
319 | u32 cr3_target_count; | |
320 | u32 vm_exit_controls; | |
321 | u32 vm_exit_msr_store_count; | |
322 | u32 vm_exit_msr_load_count; | |
323 | u32 vm_entry_controls; | |
324 | u32 vm_entry_msr_load_count; | |
325 | u32 vm_entry_intr_info_field; | |
326 | u32 vm_entry_exception_error_code; | |
327 | u32 vm_entry_instruction_len; | |
328 | u32 tpr_threshold; | |
329 | u32 secondary_vm_exec_control; | |
330 | u32 vm_instruction_error; | |
331 | u32 vm_exit_reason; | |
332 | u32 vm_exit_intr_info; | |
333 | u32 vm_exit_intr_error_code; | |
334 | u32 idt_vectoring_info_field; | |
335 | u32 idt_vectoring_error_code; | |
336 | u32 vm_exit_instruction_len; | |
337 | u32 vmx_instruction_info; | |
338 | u32 guest_es_limit; | |
339 | u32 guest_cs_limit; | |
340 | u32 guest_ss_limit; | |
341 | u32 guest_ds_limit; | |
342 | u32 guest_fs_limit; | |
343 | u32 guest_gs_limit; | |
344 | u32 guest_ldtr_limit; | |
345 | u32 guest_tr_limit; | |
346 | u32 guest_gdtr_limit; | |
347 | u32 guest_idtr_limit; | |
348 | u32 guest_es_ar_bytes; | |
349 | u32 guest_cs_ar_bytes; | |
350 | u32 guest_ss_ar_bytes; | |
351 | u32 guest_ds_ar_bytes; | |
352 | u32 guest_fs_ar_bytes; | |
353 | u32 guest_gs_ar_bytes; | |
354 | u32 guest_ldtr_ar_bytes; | |
355 | u32 guest_tr_ar_bytes; | |
356 | u32 guest_interruptibility_info; | |
357 | u32 guest_activity_state; | |
358 | u32 guest_sysenter_cs; | |
359 | u32 host_ia32_sysenter_cs; | |
0238ea91 JK |
360 | u32 vmx_preemption_timer_value; |
361 | u32 padding32[7]; /* room for future expansion */ | |
22bd0358 | 362 | u16 virtual_processor_id; |
705699a1 | 363 | u16 posted_intr_nv; |
22bd0358 NHE |
364 | u16 guest_es_selector; |
365 | u16 guest_cs_selector; | |
366 | u16 guest_ss_selector; | |
367 | u16 guest_ds_selector; | |
368 | u16 guest_fs_selector; | |
369 | u16 guest_gs_selector; | |
370 | u16 guest_ldtr_selector; | |
371 | u16 guest_tr_selector; | |
608406e2 | 372 | u16 guest_intr_status; |
c5f983f6 | 373 | u16 guest_pml_index; |
22bd0358 NHE |
374 | u16 host_es_selector; |
375 | u16 host_cs_selector; | |
376 | u16 host_ss_selector; | |
377 | u16 host_ds_selector; | |
378 | u16 host_fs_selector; | |
379 | u16 host_gs_selector; | |
380 | u16 host_tr_selector; | |
a9d30f33 NHE |
381 | }; |
382 | ||
383 | /* | |
384 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or | |
385 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and | |
386 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. | |
387 | */ | |
388 | #define VMCS12_REVISION 0x11e57ed0 | |
389 | ||
390 | /* | |
391 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region | |
392 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the | |
393 | * current implementation, 4K are reserved to avoid future complications. | |
394 | */ | |
395 | #define VMCS12_SIZE 0x1000 | |
396 | ||
ff2f6fe9 NHE |
397 | /* Used to remember the last vmcs02 used for some recently used vmcs12s */ |
398 | struct vmcs02_list { | |
399 | struct list_head list; | |
400 | gpa_t vmptr; | |
401 | struct loaded_vmcs vmcs02; | |
402 | }; | |
403 | ||
ec378aee NHE |
404 | /* |
405 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need | |
406 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. | |
407 | */ | |
408 | struct nested_vmx { | |
409 | /* Has the level1 guest done vmxon? */ | |
410 | bool vmxon; | |
3573e22c | 411 | gpa_t vmxon_ptr; |
c5f983f6 | 412 | bool pml_full; |
a9d30f33 NHE |
413 | |
414 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ | |
415 | gpa_t current_vmptr; | |
416 | /* The host-usable pointer to the above */ | |
417 | struct page *current_vmcs12_page; | |
418 | struct vmcs12 *current_vmcs12; | |
4f2777bc DM |
419 | /* |
420 | * Cache of the guest's VMCS, existing outside of guest memory. | |
421 | * Loaded from guest memory during VMPTRLD. Flushed to guest | |
422 | * memory during VMXOFF, VMCLEAR, VMPTRLD. | |
423 | */ | |
424 | struct vmcs12 *cached_vmcs12; | |
012f83cb AG |
425 | /* |
426 | * Indicates if the shadow vmcs must be updated with the | |
427 | * data hold by vmcs12 | |
428 | */ | |
429 | bool sync_shadow_vmcs; | |
ff2f6fe9 NHE |
430 | |
431 | /* vmcs02_list cache of VMCSs recently used to run L2 guests */ | |
432 | struct list_head vmcs02_pool; | |
433 | int vmcs02_num; | |
dccbfcf5 | 434 | bool change_vmcs01_virtual_x2apic_mode; |
644d711a NHE |
435 | /* L2 must run next, and mustn't decide to exit to L1. */ |
436 | bool nested_run_pending; | |
fe3ef05c NHE |
437 | /* |
438 | * Guest pages referred to in vmcs02 with host-physical pointers, so | |
439 | * we must keep them pinned while L2 runs. | |
440 | */ | |
441 | struct page *apic_access_page; | |
a7c0b07d | 442 | struct page *virtual_apic_page; |
705699a1 WV |
443 | struct page *pi_desc_page; |
444 | struct pi_desc *pi_desc; | |
445 | bool pi_pending; | |
446 | u16 posted_intr_nv; | |
f4124500 | 447 | |
d048c098 RK |
448 | unsigned long *msr_bitmap; |
449 | ||
f4124500 JK |
450 | struct hrtimer preemption_timer; |
451 | bool preemption_timer_expired; | |
2996fca0 JK |
452 | |
453 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ | |
454 | u64 vmcs01_debugctl; | |
b9c237bb | 455 | |
5c614b35 WL |
456 | u16 vpid02; |
457 | u16 last_vpid; | |
458 | ||
0115f9cb DM |
459 | /* |
460 | * We only store the "true" versions of the VMX capability MSRs. We | |
461 | * generate the "non-true" versions by setting the must-be-1 bits | |
462 | * according to the SDM. | |
463 | */ | |
b9c237bb WV |
464 | u32 nested_vmx_procbased_ctls_low; |
465 | u32 nested_vmx_procbased_ctls_high; | |
b9c237bb WV |
466 | u32 nested_vmx_secondary_ctls_low; |
467 | u32 nested_vmx_secondary_ctls_high; | |
468 | u32 nested_vmx_pinbased_ctls_low; | |
469 | u32 nested_vmx_pinbased_ctls_high; | |
470 | u32 nested_vmx_exit_ctls_low; | |
471 | u32 nested_vmx_exit_ctls_high; | |
b9c237bb WV |
472 | u32 nested_vmx_entry_ctls_low; |
473 | u32 nested_vmx_entry_ctls_high; | |
b9c237bb WV |
474 | u32 nested_vmx_misc_low; |
475 | u32 nested_vmx_misc_high; | |
476 | u32 nested_vmx_ept_caps; | |
99b83ac8 | 477 | u32 nested_vmx_vpid_caps; |
62cc6b9d DM |
478 | u64 nested_vmx_basic; |
479 | u64 nested_vmx_cr0_fixed0; | |
480 | u64 nested_vmx_cr0_fixed1; | |
481 | u64 nested_vmx_cr4_fixed0; | |
482 | u64 nested_vmx_cr4_fixed1; | |
483 | u64 nested_vmx_vmcs_enum; | |
ec378aee NHE |
484 | }; |
485 | ||
01e439be | 486 | #define POSTED_INTR_ON 0 |
ebbfc765 FW |
487 | #define POSTED_INTR_SN 1 |
488 | ||
01e439be YZ |
489 | /* Posted-Interrupt Descriptor */ |
490 | struct pi_desc { | |
491 | u32 pir[8]; /* Posted interrupt requested */ | |
6ef1522f FW |
492 | union { |
493 | struct { | |
494 | /* bit 256 - Outstanding Notification */ | |
495 | u16 on : 1, | |
496 | /* bit 257 - Suppress Notification */ | |
497 | sn : 1, | |
498 | /* bit 271:258 - Reserved */ | |
499 | rsvd_1 : 14; | |
500 | /* bit 279:272 - Notification Vector */ | |
501 | u8 nv; | |
502 | /* bit 287:280 - Reserved */ | |
503 | u8 rsvd_2; | |
504 | /* bit 319:288 - Notification Destination */ | |
505 | u32 ndst; | |
506 | }; | |
507 | u64 control; | |
508 | }; | |
509 | u32 rsvd[6]; | |
01e439be YZ |
510 | } __aligned(64); |
511 | ||
a20ed54d YZ |
512 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
513 | { | |
514 | return test_and_set_bit(POSTED_INTR_ON, | |
515 | (unsigned long *)&pi_desc->control); | |
516 | } | |
517 | ||
518 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) | |
519 | { | |
520 | return test_and_clear_bit(POSTED_INTR_ON, | |
521 | (unsigned long *)&pi_desc->control); | |
522 | } | |
523 | ||
524 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) | |
525 | { | |
526 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); | |
527 | } | |
528 | ||
ebbfc765 FW |
529 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
530 | { | |
531 | return clear_bit(POSTED_INTR_SN, | |
532 | (unsigned long *)&pi_desc->control); | |
533 | } | |
534 | ||
535 | static inline void pi_set_sn(struct pi_desc *pi_desc) | |
536 | { | |
537 | return set_bit(POSTED_INTR_SN, | |
538 | (unsigned long *)&pi_desc->control); | |
539 | } | |
540 | ||
ad361091 PB |
541 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
542 | { | |
543 | clear_bit(POSTED_INTR_ON, | |
544 | (unsigned long *)&pi_desc->control); | |
545 | } | |
546 | ||
ebbfc765 FW |
547 | static inline int pi_test_on(struct pi_desc *pi_desc) |
548 | { | |
549 | return test_bit(POSTED_INTR_ON, | |
550 | (unsigned long *)&pi_desc->control); | |
551 | } | |
552 | ||
553 | static inline int pi_test_sn(struct pi_desc *pi_desc) | |
554 | { | |
555 | return test_bit(POSTED_INTR_SN, | |
556 | (unsigned long *)&pi_desc->control); | |
557 | } | |
558 | ||
a2fa3e9f | 559 | struct vcpu_vmx { |
fb3f0f51 | 560 | struct kvm_vcpu vcpu; |
313dbd49 | 561 | unsigned long host_rsp; |
29bd8a78 | 562 | u8 fail; |
9d58b931 | 563 | bool nmi_known_unmasked; |
51aa01d1 | 564 | u32 exit_intr_info; |
1155f76a | 565 | u32 idt_vectoring_info; |
6de12732 | 566 | ulong rflags; |
26bb0981 | 567 | struct shared_msr_entry *guest_msrs; |
a2fa3e9f GH |
568 | int nmsrs; |
569 | int save_nmsrs; | |
a547c6db | 570 | unsigned long host_idt_base; |
a2fa3e9f | 571 | #ifdef CONFIG_X86_64 |
44ea2b17 AK |
572 | u64 msr_host_kernel_gs_base; |
573 | u64 msr_guest_kernel_gs_base; | |
a2fa3e9f | 574 | #endif |
2961e876 GN |
575 | u32 vm_entry_controls_shadow; |
576 | u32 vm_exit_controls_shadow; | |
d462b819 NHE |
577 | /* |
578 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a | |
579 | * non-nested (L1) guest, it always points to vmcs01. For a nested | |
580 | * guest (L2), it points to a different VMCS. | |
581 | */ | |
582 | struct loaded_vmcs vmcs01; | |
583 | struct loaded_vmcs *loaded_vmcs; | |
584 | bool __launched; /* temporary, used in vmx_vcpu_run */ | |
61d2ef2c AK |
585 | struct msr_autoload { |
586 | unsigned nr; | |
587 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; | |
588 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; | |
589 | } msr_autoload; | |
a2fa3e9f GH |
590 | struct { |
591 | int loaded; | |
592 | u16 fs_sel, gs_sel, ldt_sel; | |
b2da15ac AK |
593 | #ifdef CONFIG_X86_64 |
594 | u16 ds_sel, es_sel; | |
595 | #endif | |
152d3f2f LV |
596 | int gs_ldt_reload_needed; |
597 | int fs_reload_needed; | |
da8999d3 | 598 | u64 msr_host_bndcfgs; |
d974baa3 | 599 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
d77c26fc | 600 | } host_state; |
9c8cba37 | 601 | struct { |
7ffd92c5 | 602 | int vm86_active; |
78ac8b47 | 603 | ulong save_rflags; |
f5f7b2fe AK |
604 | struct kvm_segment segs[8]; |
605 | } rmode; | |
606 | struct { | |
607 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ | |
7ffd92c5 AK |
608 | struct kvm_save_segment { |
609 | u16 selector; | |
610 | unsigned long base; | |
611 | u32 limit; | |
612 | u32 ar; | |
f5f7b2fe | 613 | } seg[8]; |
2fb92db1 | 614 | } segment_cache; |
2384d2b3 | 615 | int vpid; |
04fa4d32 | 616 | bool emulation_required; |
3b86cd99 | 617 | |
a0861c02 | 618 | u32 exit_reason; |
4e47c7a6 | 619 | |
01e439be YZ |
620 | /* Posted interrupt descriptor */ |
621 | struct pi_desc pi_desc; | |
622 | ||
ec378aee NHE |
623 | /* Support for a guest hypervisor (nested VMX) */ |
624 | struct nested_vmx nested; | |
a7653ecd RK |
625 | |
626 | /* Dynamic PLE window. */ | |
627 | int ple_window; | |
628 | bool ple_window_dirty; | |
843e4330 KH |
629 | |
630 | /* Support for PML */ | |
631 | #define PML_ENTITY_NUM 512 | |
632 | struct page *pml_pg; | |
2680d6da | 633 | |
64672c95 YJ |
634 | /* apic deadline value in host tsc */ |
635 | u64 hv_deadline_tsc; | |
636 | ||
2680d6da | 637 | u64 current_tsc_ratio; |
1be0e61c XG |
638 | |
639 | bool guest_pkru_valid; | |
640 | u32 guest_pkru; | |
641 | u32 host_pkru; | |
3b84080b | 642 | |
37e4c997 HZ |
643 | /* |
644 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in | |
645 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included | |
646 | * in msr_ia32_feature_control_valid_bits. | |
647 | */ | |
3b84080b | 648 | u64 msr_ia32_feature_control; |
37e4c997 | 649 | u64 msr_ia32_feature_control_valid_bits; |
a2fa3e9f GH |
650 | }; |
651 | ||
2fb92db1 AK |
652 | enum segment_cache_field { |
653 | SEG_FIELD_SEL = 0, | |
654 | SEG_FIELD_BASE = 1, | |
655 | SEG_FIELD_LIMIT = 2, | |
656 | SEG_FIELD_AR = 3, | |
657 | ||
658 | SEG_FIELD_NR = 4 | |
659 | }; | |
660 | ||
a2fa3e9f GH |
661 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
662 | { | |
fb3f0f51 | 663 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
a2fa3e9f GH |
664 | } |
665 | ||
efc64404 FW |
666 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
667 | { | |
668 | return &(to_vmx(vcpu)->pi_desc); | |
669 | } | |
670 | ||
22bd0358 NHE |
671 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
672 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) | |
673 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ | |
674 | [number##_HIGH] = VMCS12_OFFSET(name)+4 | |
675 | ||
4607c2d7 | 676 | |
fe2b201b | 677 | static unsigned long shadow_read_only_fields[] = { |
4607c2d7 AG |
678 | /* |
679 | * We do NOT shadow fields that are modified when L0 | |
680 | * traps and emulates any vmx instruction (e.g. VMPTRLD, | |
681 | * VMXON...) executed by L1. | |
682 | * For example, VM_INSTRUCTION_ERROR is read | |
683 | * by L1 if a vmx instruction fails (part of the error path). | |
684 | * Note the code assumes this logic. If for some reason | |
685 | * we start shadowing these fields then we need to | |
686 | * force a shadow sync when L0 emulates vmx instructions | |
687 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified | |
688 | * by nested_vmx_failValid) | |
689 | */ | |
690 | VM_EXIT_REASON, | |
691 | VM_EXIT_INTR_INFO, | |
692 | VM_EXIT_INSTRUCTION_LEN, | |
693 | IDT_VECTORING_INFO_FIELD, | |
694 | IDT_VECTORING_ERROR_CODE, | |
695 | VM_EXIT_INTR_ERROR_CODE, | |
696 | EXIT_QUALIFICATION, | |
697 | GUEST_LINEAR_ADDRESS, | |
698 | GUEST_PHYSICAL_ADDRESS | |
699 | }; | |
fe2b201b | 700 | static int max_shadow_read_only_fields = |
4607c2d7 AG |
701 | ARRAY_SIZE(shadow_read_only_fields); |
702 | ||
fe2b201b | 703 | static unsigned long shadow_read_write_fields[] = { |
a7c0b07d | 704 | TPR_THRESHOLD, |
4607c2d7 AG |
705 | GUEST_RIP, |
706 | GUEST_RSP, | |
707 | GUEST_CR0, | |
708 | GUEST_CR3, | |
709 | GUEST_CR4, | |
710 | GUEST_INTERRUPTIBILITY_INFO, | |
711 | GUEST_RFLAGS, | |
712 | GUEST_CS_SELECTOR, | |
713 | GUEST_CS_AR_BYTES, | |
714 | GUEST_CS_LIMIT, | |
715 | GUEST_CS_BASE, | |
716 | GUEST_ES_BASE, | |
36be0b9d | 717 | GUEST_BNDCFGS, |
4607c2d7 AG |
718 | CR0_GUEST_HOST_MASK, |
719 | CR0_READ_SHADOW, | |
720 | CR4_READ_SHADOW, | |
721 | TSC_OFFSET, | |
722 | EXCEPTION_BITMAP, | |
723 | CPU_BASED_VM_EXEC_CONTROL, | |
724 | VM_ENTRY_EXCEPTION_ERROR_CODE, | |
725 | VM_ENTRY_INTR_INFO_FIELD, | |
726 | VM_ENTRY_INSTRUCTION_LEN, | |
727 | VM_ENTRY_EXCEPTION_ERROR_CODE, | |
728 | HOST_FS_BASE, | |
729 | HOST_GS_BASE, | |
730 | HOST_FS_SELECTOR, | |
731 | HOST_GS_SELECTOR | |
732 | }; | |
fe2b201b | 733 | static int max_shadow_read_write_fields = |
4607c2d7 AG |
734 | ARRAY_SIZE(shadow_read_write_fields); |
735 | ||
772e0318 | 736 | static const unsigned short vmcs_field_to_offset_table[] = { |
22bd0358 | 737 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
705699a1 | 738 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
22bd0358 NHE |
739 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
740 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), | |
741 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), | |
742 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), | |
743 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), | |
744 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), | |
745 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), | |
746 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), | |
608406e2 | 747 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
c5f983f6 | 748 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
22bd0358 NHE |
749 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
750 | FIELD(HOST_CS_SELECTOR, host_cs_selector), | |
751 | FIELD(HOST_SS_SELECTOR, host_ss_selector), | |
752 | FIELD(HOST_DS_SELECTOR, host_ds_selector), | |
753 | FIELD(HOST_FS_SELECTOR, host_fs_selector), | |
754 | FIELD(HOST_GS_SELECTOR, host_gs_selector), | |
755 | FIELD(HOST_TR_SELECTOR, host_tr_selector), | |
756 | FIELD64(IO_BITMAP_A, io_bitmap_a), | |
757 | FIELD64(IO_BITMAP_B, io_bitmap_b), | |
758 | FIELD64(MSR_BITMAP, msr_bitmap), | |
759 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), | |
760 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), | |
761 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), | |
762 | FIELD64(TSC_OFFSET, tsc_offset), | |
763 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), | |
764 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), | |
705699a1 | 765 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
22bd0358 | 766 | FIELD64(EPT_POINTER, ept_pointer), |
608406e2 WV |
767 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
768 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), | |
769 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), | |
770 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), | |
81dc01f7 | 771 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
22bd0358 NHE |
772 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
773 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), | |
c5f983f6 | 774 | FIELD64(PML_ADDRESS, pml_address), |
22bd0358 NHE |
775 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
776 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), | |
777 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), | |
778 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), | |
779 | FIELD64(GUEST_PDPTR0, guest_pdptr0), | |
780 | FIELD64(GUEST_PDPTR1, guest_pdptr1), | |
781 | FIELD64(GUEST_PDPTR2, guest_pdptr2), | |
782 | FIELD64(GUEST_PDPTR3, guest_pdptr3), | |
36be0b9d | 783 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
22bd0358 NHE |
784 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
785 | FIELD64(HOST_IA32_EFER, host_ia32_efer), | |
786 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), | |
787 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), | |
788 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), | |
789 | FIELD(EXCEPTION_BITMAP, exception_bitmap), | |
790 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), | |
791 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), | |
792 | FIELD(CR3_TARGET_COUNT, cr3_target_count), | |
793 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), | |
794 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), | |
795 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), | |
796 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), | |
797 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), | |
798 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), | |
799 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), | |
800 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), | |
801 | FIELD(TPR_THRESHOLD, tpr_threshold), | |
802 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), | |
803 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), | |
804 | FIELD(VM_EXIT_REASON, vm_exit_reason), | |
805 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), | |
806 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), | |
807 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), | |
808 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), | |
809 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), | |
810 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), | |
811 | FIELD(GUEST_ES_LIMIT, guest_es_limit), | |
812 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), | |
813 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), | |
814 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), | |
815 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), | |
816 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), | |
817 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), | |
818 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), | |
819 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), | |
820 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), | |
821 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), | |
822 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), | |
823 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), | |
824 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), | |
825 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), | |
826 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), | |
827 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), | |
828 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), | |
829 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), | |
830 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), | |
831 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), | |
832 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), | |
0238ea91 | 833 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
22bd0358 NHE |
834 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
835 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), | |
836 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), | |
837 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), | |
838 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), | |
839 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), | |
840 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), | |
841 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), | |
842 | FIELD(EXIT_QUALIFICATION, exit_qualification), | |
843 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), | |
844 | FIELD(GUEST_CR0, guest_cr0), | |
845 | FIELD(GUEST_CR3, guest_cr3), | |
846 | FIELD(GUEST_CR4, guest_cr4), | |
847 | FIELD(GUEST_ES_BASE, guest_es_base), | |
848 | FIELD(GUEST_CS_BASE, guest_cs_base), | |
849 | FIELD(GUEST_SS_BASE, guest_ss_base), | |
850 | FIELD(GUEST_DS_BASE, guest_ds_base), | |
851 | FIELD(GUEST_FS_BASE, guest_fs_base), | |
852 | FIELD(GUEST_GS_BASE, guest_gs_base), | |
853 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), | |
854 | FIELD(GUEST_TR_BASE, guest_tr_base), | |
855 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), | |
856 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), | |
857 | FIELD(GUEST_DR7, guest_dr7), | |
858 | FIELD(GUEST_RSP, guest_rsp), | |
859 | FIELD(GUEST_RIP, guest_rip), | |
860 | FIELD(GUEST_RFLAGS, guest_rflags), | |
861 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), | |
862 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), | |
863 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), | |
864 | FIELD(HOST_CR0, host_cr0), | |
865 | FIELD(HOST_CR3, host_cr3), | |
866 | FIELD(HOST_CR4, host_cr4), | |
867 | FIELD(HOST_FS_BASE, host_fs_base), | |
868 | FIELD(HOST_GS_BASE, host_gs_base), | |
869 | FIELD(HOST_TR_BASE, host_tr_base), | |
870 | FIELD(HOST_GDTR_BASE, host_gdtr_base), | |
871 | FIELD(HOST_IDTR_BASE, host_idtr_base), | |
872 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), | |
873 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), | |
874 | FIELD(HOST_RSP, host_rsp), | |
875 | FIELD(HOST_RIP, host_rip), | |
876 | }; | |
22bd0358 NHE |
877 | |
878 | static inline short vmcs_field_to_offset(unsigned long field) | |
879 | { | |
a2ae9df7 PB |
880 | BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX); |
881 | ||
882 | if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) || | |
883 | vmcs_field_to_offset_table[field] == 0) | |
884 | return -ENOENT; | |
885 | ||
22bd0358 NHE |
886 | return vmcs_field_to_offset_table[field]; |
887 | } | |
888 | ||
a9d30f33 NHE |
889 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
890 | { | |
4f2777bc | 891 | return to_vmx(vcpu)->nested.cached_vmcs12; |
a9d30f33 NHE |
892 | } |
893 | ||
894 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) | |
895 | { | |
54bf36aa | 896 | struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT); |
32cad84f | 897 | if (is_error_page(page)) |
a9d30f33 | 898 | return NULL; |
32cad84f | 899 | |
a9d30f33 NHE |
900 | return page; |
901 | } | |
902 | ||
903 | static void nested_release_page(struct page *page) | |
904 | { | |
905 | kvm_release_page_dirty(page); | |
906 | } | |
907 | ||
908 | static void nested_release_page_clean(struct page *page) | |
909 | { | |
910 | kvm_release_page_clean(page); | |
911 | } | |
912 | ||
995f00a6 | 913 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
bfd0a56b | 914 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
995f00a6 | 915 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
f53cd63c | 916 | static bool vmx_xsaves_supported(void); |
776e58ea | 917 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
b246dd5d OW |
918 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
919 | struct kvm_segment *var, int seg); | |
920 | static void vmx_get_segment(struct kvm_vcpu *vcpu, | |
921 | struct kvm_segment *var, int seg); | |
d99e4152 GN |
922 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
923 | static u32 vmx_segment_access_rights(struct kvm_segment *var); | |
c3114420 | 924 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx); |
16f5b903 | 925 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
a255d479 | 926 | static int alloc_identity_pagetable(struct kvm *kvm); |
75880a01 | 927 | |
6aa8b732 AK |
928 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
929 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); | |
d462b819 NHE |
930 | /* |
931 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed | |
932 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. | |
933 | */ | |
934 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); | |
6aa8b732 | 935 | |
bf9f6ac8 FW |
936 | /* |
937 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we | |
938 | * can find which vCPU should be waken up. | |
939 | */ | |
940 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); | |
941 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); | |
942 | ||
23611332 RK |
943 | enum { |
944 | VMX_IO_BITMAP_A, | |
945 | VMX_IO_BITMAP_B, | |
946 | VMX_MSR_BITMAP_LEGACY, | |
947 | VMX_MSR_BITMAP_LONGMODE, | |
948 | VMX_MSR_BITMAP_LEGACY_X2APIC_APICV, | |
949 | VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV, | |
950 | VMX_MSR_BITMAP_LEGACY_X2APIC, | |
951 | VMX_MSR_BITMAP_LONGMODE_X2APIC, | |
952 | VMX_VMREAD_BITMAP, | |
953 | VMX_VMWRITE_BITMAP, | |
954 | VMX_BITMAP_NR | |
955 | }; | |
956 | ||
957 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; | |
958 | ||
959 | #define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A]) | |
960 | #define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B]) | |
961 | #define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY]) | |
962 | #define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE]) | |
963 | #define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV]) | |
964 | #define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV]) | |
965 | #define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC]) | |
966 | #define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC]) | |
967 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) | |
968 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) | |
fdef3ad1 | 969 | |
110312c8 | 970 | static bool cpu_has_load_ia32_efer; |
8bf00a52 | 971 | static bool cpu_has_load_perf_global_ctrl; |
110312c8 | 972 | |
2384d2b3 SY |
973 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
974 | static DEFINE_SPINLOCK(vmx_vpid_lock); | |
975 | ||
1c3d14fe | 976 | static struct vmcs_config { |
6aa8b732 AK |
977 | int size; |
978 | int order; | |
9ac7e3e8 | 979 | u32 basic_cap; |
6aa8b732 | 980 | u32 revision_id; |
1c3d14fe YS |
981 | u32 pin_based_exec_ctrl; |
982 | u32 cpu_based_exec_ctrl; | |
f78e0e2e | 983 | u32 cpu_based_2nd_exec_ctrl; |
1c3d14fe YS |
984 | u32 vmexit_ctrl; |
985 | u32 vmentry_ctrl; | |
986 | } vmcs_config; | |
6aa8b732 | 987 | |
efff9e53 | 988 | static struct vmx_capability { |
d56f546d SY |
989 | u32 ept; |
990 | u32 vpid; | |
991 | } vmx_capability; | |
992 | ||
6aa8b732 AK |
993 | #define VMX_SEGMENT_FIELD(seg) \ |
994 | [VCPU_SREG_##seg] = { \ | |
995 | .selector = GUEST_##seg##_SELECTOR, \ | |
996 | .base = GUEST_##seg##_BASE, \ | |
997 | .limit = GUEST_##seg##_LIMIT, \ | |
998 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ | |
999 | } | |
1000 | ||
772e0318 | 1001 | static const struct kvm_vmx_segment_field { |
6aa8b732 AK |
1002 | unsigned selector; |
1003 | unsigned base; | |
1004 | unsigned limit; | |
1005 | unsigned ar_bytes; | |
1006 | } kvm_vmx_segment_fields[] = { | |
1007 | VMX_SEGMENT_FIELD(CS), | |
1008 | VMX_SEGMENT_FIELD(DS), | |
1009 | VMX_SEGMENT_FIELD(ES), | |
1010 | VMX_SEGMENT_FIELD(FS), | |
1011 | VMX_SEGMENT_FIELD(GS), | |
1012 | VMX_SEGMENT_FIELD(SS), | |
1013 | VMX_SEGMENT_FIELD(TR), | |
1014 | VMX_SEGMENT_FIELD(LDTR), | |
1015 | }; | |
1016 | ||
26bb0981 AK |
1017 | static u64 host_efer; |
1018 | ||
6de4f3ad AK |
1019 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
1020 | ||
4d56c8a7 | 1021 | /* |
8c06585d | 1022 | * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it |
4d56c8a7 AK |
1023 | * away by decrementing the array size. |
1024 | */ | |
6aa8b732 | 1025 | static const u32 vmx_msr_index[] = { |
05b3e0c2 | 1026 | #ifdef CONFIG_X86_64 |
44ea2b17 | 1027 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
6aa8b732 | 1028 | #endif |
8c06585d | 1029 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
6aa8b732 | 1030 | }; |
6aa8b732 | 1031 | |
5bb16016 | 1032 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
6aa8b732 AK |
1033 | { |
1034 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1035 | INTR_INFO_VALID_MASK)) == | |
5bb16016 JK |
1036 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
1037 | } | |
1038 | ||
6f05485d JK |
1039 | static inline bool is_debug(u32 intr_info) |
1040 | { | |
1041 | return is_exception_n(intr_info, DB_VECTOR); | |
1042 | } | |
1043 | ||
1044 | static inline bool is_breakpoint(u32 intr_info) | |
1045 | { | |
1046 | return is_exception_n(intr_info, BP_VECTOR); | |
1047 | } | |
1048 | ||
5bb16016 JK |
1049 | static inline bool is_page_fault(u32 intr_info) |
1050 | { | |
1051 | return is_exception_n(intr_info, PF_VECTOR); | |
6aa8b732 AK |
1052 | } |
1053 | ||
31299944 | 1054 | static inline bool is_no_device(u32 intr_info) |
2ab455cc | 1055 | { |
5bb16016 | 1056 | return is_exception_n(intr_info, NM_VECTOR); |
2ab455cc AL |
1057 | } |
1058 | ||
31299944 | 1059 | static inline bool is_invalid_opcode(u32 intr_info) |
7aa81cc0 | 1060 | { |
5bb16016 | 1061 | return is_exception_n(intr_info, UD_VECTOR); |
7aa81cc0 AL |
1062 | } |
1063 | ||
31299944 | 1064 | static inline bool is_external_interrupt(u32 intr_info) |
6aa8b732 AK |
1065 | { |
1066 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
1067 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); | |
1068 | } | |
1069 | ||
31299944 | 1070 | static inline bool is_machine_check(u32 intr_info) |
a0861c02 AK |
1071 | { |
1072 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1073 | INTR_INFO_VALID_MASK)) == | |
1074 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); | |
1075 | } | |
1076 | ||
31299944 | 1077 | static inline bool cpu_has_vmx_msr_bitmap(void) |
25c5f225 | 1078 | { |
04547156 | 1079 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
25c5f225 SY |
1080 | } |
1081 | ||
31299944 | 1082 | static inline bool cpu_has_vmx_tpr_shadow(void) |
6e5d865c | 1083 | { |
04547156 | 1084 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
6e5d865c YS |
1085 | } |
1086 | ||
35754c98 | 1087 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
6e5d865c | 1088 | { |
35754c98 | 1089 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
6e5d865c YS |
1090 | } |
1091 | ||
31299944 | 1092 | static inline bool cpu_has_secondary_exec_ctrls(void) |
f78e0e2e | 1093 | { |
04547156 SY |
1094 | return vmcs_config.cpu_based_exec_ctrl & |
1095 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
f78e0e2e SY |
1096 | } |
1097 | ||
774ead3a | 1098 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
f78e0e2e | 1099 | { |
04547156 SY |
1100 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1101 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
1102 | } | |
1103 | ||
8d14695f YZ |
1104 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
1105 | { | |
1106 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1107 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
1108 | } | |
1109 | ||
83d4c286 YZ |
1110 | static inline bool cpu_has_vmx_apic_register_virt(void) |
1111 | { | |
1112 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1113 | SECONDARY_EXEC_APIC_REGISTER_VIRT; | |
1114 | } | |
1115 | ||
c7c9c56c YZ |
1116 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
1117 | { | |
1118 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1119 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; | |
1120 | } | |
1121 | ||
64672c95 YJ |
1122 | /* |
1123 | * Comment's format: document - errata name - stepping - processor name. | |
1124 | * Refer from | |
1125 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp | |
1126 | */ | |
1127 | static u32 vmx_preemption_cpu_tfms[] = { | |
1128 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ | |
1129 | 0x000206E6, | |
1130 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ | |
1131 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ | |
1132 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1133 | 0x00020652, | |
1134 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1135 | 0x00020655, | |
1136 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ | |
1137 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ | |
1138 | /* | |
1139 | * 320767.pdf - AAP86 - B1 - | |
1140 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile | |
1141 | */ | |
1142 | 0x000106E5, | |
1143 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ | |
1144 | 0x000106A0, | |
1145 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ | |
1146 | 0x000106A1, | |
1147 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ | |
1148 | 0x000106A4, | |
1149 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ | |
1150 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ | |
1151 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ | |
1152 | 0x000106A5, | |
1153 | }; | |
1154 | ||
1155 | static inline bool cpu_has_broken_vmx_preemption_timer(void) | |
1156 | { | |
1157 | u32 eax = cpuid_eax(0x00000001), i; | |
1158 | ||
1159 | /* Clear the reserved bits */ | |
1160 | eax &= ~(0x3U << 14 | 0xfU << 28); | |
03f6a22a | 1161 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
64672c95 YJ |
1162 | if (eax == vmx_preemption_cpu_tfms[i]) |
1163 | return true; | |
1164 | ||
1165 | return false; | |
1166 | } | |
1167 | ||
1168 | static inline bool cpu_has_vmx_preemption_timer(void) | |
1169 | { | |
64672c95 YJ |
1170 | return vmcs_config.pin_based_exec_ctrl & |
1171 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1172 | } | |
1173 | ||
01e439be YZ |
1174 | static inline bool cpu_has_vmx_posted_intr(void) |
1175 | { | |
d6a858d1 PB |
1176 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
1177 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; | |
01e439be YZ |
1178 | } |
1179 | ||
1180 | static inline bool cpu_has_vmx_apicv(void) | |
1181 | { | |
1182 | return cpu_has_vmx_apic_register_virt() && | |
1183 | cpu_has_vmx_virtual_intr_delivery() && | |
1184 | cpu_has_vmx_posted_intr(); | |
1185 | } | |
1186 | ||
04547156 SY |
1187 | static inline bool cpu_has_vmx_flexpriority(void) |
1188 | { | |
1189 | return cpu_has_vmx_tpr_shadow() && | |
1190 | cpu_has_vmx_virtualize_apic_accesses(); | |
f78e0e2e SY |
1191 | } |
1192 | ||
e799794e MT |
1193 | static inline bool cpu_has_vmx_ept_execute_only(void) |
1194 | { | |
31299944 | 1195 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
e799794e MT |
1196 | } |
1197 | ||
e799794e MT |
1198 | static inline bool cpu_has_vmx_ept_2m_page(void) |
1199 | { | |
31299944 | 1200 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
e799794e MT |
1201 | } |
1202 | ||
878403b7 SY |
1203 | static inline bool cpu_has_vmx_ept_1g_page(void) |
1204 | { | |
31299944 | 1205 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
878403b7 SY |
1206 | } |
1207 | ||
4bc9b982 SY |
1208 | static inline bool cpu_has_vmx_ept_4levels(void) |
1209 | { | |
1210 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; | |
1211 | } | |
1212 | ||
83c3a331 XH |
1213 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
1214 | { | |
1215 | return vmx_capability.ept & VMX_EPT_AD_BIT; | |
1216 | } | |
1217 | ||
31299944 | 1218 | static inline bool cpu_has_vmx_invept_context(void) |
d56f546d | 1219 | { |
31299944 | 1220 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
d56f546d SY |
1221 | } |
1222 | ||
31299944 | 1223 | static inline bool cpu_has_vmx_invept_global(void) |
d56f546d | 1224 | { |
31299944 | 1225 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
d56f546d SY |
1226 | } |
1227 | ||
518c8aee GJ |
1228 | static inline bool cpu_has_vmx_invvpid_single(void) |
1229 | { | |
1230 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; | |
1231 | } | |
1232 | ||
b9d762fa GJ |
1233 | static inline bool cpu_has_vmx_invvpid_global(void) |
1234 | { | |
1235 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; | |
1236 | } | |
1237 | ||
08d839c4 WL |
1238 | static inline bool cpu_has_vmx_invvpid(void) |
1239 | { | |
1240 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; | |
1241 | } | |
1242 | ||
31299944 | 1243 | static inline bool cpu_has_vmx_ept(void) |
d56f546d | 1244 | { |
04547156 SY |
1245 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1246 | SECONDARY_EXEC_ENABLE_EPT; | |
d56f546d SY |
1247 | } |
1248 | ||
31299944 | 1249 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
3a624e29 NK |
1250 | { |
1251 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1252 | SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
1253 | } | |
1254 | ||
31299944 | 1255 | static inline bool cpu_has_vmx_ple(void) |
4b8d54f9 ZE |
1256 | { |
1257 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1258 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
1259 | } | |
1260 | ||
9ac7e3e8 JD |
1261 | static inline bool cpu_has_vmx_basic_inout(void) |
1262 | { | |
1263 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); | |
1264 | } | |
1265 | ||
35754c98 | 1266 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
f78e0e2e | 1267 | { |
35754c98 | 1268 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
f78e0e2e SY |
1269 | } |
1270 | ||
31299944 | 1271 | static inline bool cpu_has_vmx_vpid(void) |
2384d2b3 | 1272 | { |
04547156 SY |
1273 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1274 | SECONDARY_EXEC_ENABLE_VPID; | |
2384d2b3 SY |
1275 | } |
1276 | ||
31299944 | 1277 | static inline bool cpu_has_vmx_rdtscp(void) |
4e47c7a6 SY |
1278 | { |
1279 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1280 | SECONDARY_EXEC_RDTSCP; | |
1281 | } | |
1282 | ||
ad756a16 MJ |
1283 | static inline bool cpu_has_vmx_invpcid(void) |
1284 | { | |
1285 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1286 | SECONDARY_EXEC_ENABLE_INVPCID; | |
1287 | } | |
1288 | ||
f5f48ee1 SY |
1289 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
1290 | { | |
1291 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1292 | SECONDARY_EXEC_WBINVD_EXITING; | |
1293 | } | |
1294 | ||
abc4fc58 AG |
1295 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
1296 | { | |
1297 | u64 vmx_msr; | |
1298 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
1299 | /* check if the cpu supports writing r/o exit information fields */ | |
1300 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) | |
1301 | return false; | |
1302 | ||
1303 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1304 | SECONDARY_EXEC_SHADOW_VMCS; | |
1305 | } | |
1306 | ||
843e4330 KH |
1307 | static inline bool cpu_has_vmx_pml(void) |
1308 | { | |
1309 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; | |
1310 | } | |
1311 | ||
64903d61 HZ |
1312 | static inline bool cpu_has_vmx_tsc_scaling(void) |
1313 | { | |
1314 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1315 | SECONDARY_EXEC_TSC_SCALING; | |
1316 | } | |
1317 | ||
04547156 SY |
1318 | static inline bool report_flexpriority(void) |
1319 | { | |
1320 | return flexpriority_enabled; | |
1321 | } | |
1322 | ||
c7c2c709 JM |
1323 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
1324 | { | |
1325 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low); | |
1326 | } | |
1327 | ||
fe3ef05c NHE |
1328 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
1329 | { | |
1330 | return vmcs12->cpu_based_vm_exec_control & bit; | |
1331 | } | |
1332 | ||
1333 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) | |
1334 | { | |
1335 | return (vmcs12->cpu_based_vm_exec_control & | |
1336 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && | |
1337 | (vmcs12->secondary_vm_exec_control & bit); | |
1338 | } | |
1339 | ||
f5c4368f | 1340 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
644d711a NHE |
1341 | { |
1342 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; | |
1343 | } | |
1344 | ||
f4124500 JK |
1345 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
1346 | { | |
1347 | return vmcs12->pin_based_vm_exec_control & | |
1348 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1349 | } | |
1350 | ||
155a97a3 NHE |
1351 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
1352 | { | |
1353 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); | |
1354 | } | |
1355 | ||
81dc01f7 WL |
1356 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
1357 | { | |
1358 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) && | |
1359 | vmx_xsaves_supported(); | |
1360 | } | |
1361 | ||
c5f983f6 BD |
1362 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
1363 | { | |
1364 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); | |
1365 | } | |
1366 | ||
f2b93280 WV |
1367 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
1368 | { | |
1369 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); | |
1370 | } | |
1371 | ||
5c614b35 WL |
1372 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
1373 | { | |
1374 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); | |
1375 | } | |
1376 | ||
82f0dd4b WV |
1377 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
1378 | { | |
1379 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); | |
1380 | } | |
1381 | ||
608406e2 WV |
1382 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
1383 | { | |
1384 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
1385 | } | |
1386 | ||
705699a1 WV |
1387 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
1388 | { | |
1389 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; | |
1390 | } | |
1391 | ||
ef85b673 | 1392 | static inline bool is_nmi(u32 intr_info) |
644d711a NHE |
1393 | { |
1394 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
ef85b673 | 1395 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
644d711a NHE |
1396 | } |
1397 | ||
533558bc JK |
1398 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
1399 | u32 exit_intr_info, | |
1400 | unsigned long exit_qualification); | |
7c177938 NHE |
1401 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
1402 | struct vmcs12 *vmcs12, | |
1403 | u32 reason, unsigned long qualification); | |
1404 | ||
8b9cf98c | 1405 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
7725f0ba AK |
1406 | { |
1407 | int i; | |
1408 | ||
a2fa3e9f | 1409 | for (i = 0; i < vmx->nmsrs; ++i) |
26bb0981 | 1410 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
a75beee6 ED |
1411 | return i; |
1412 | return -1; | |
1413 | } | |
1414 | ||
2384d2b3 SY |
1415 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
1416 | { | |
1417 | struct { | |
1418 | u64 vpid : 16; | |
1419 | u64 rsvd : 48; | |
1420 | u64 gva; | |
1421 | } operand = { vpid, 0, gva }; | |
1422 | ||
4ecac3fd | 1423 | asm volatile (__ex(ASM_VMX_INVVPID) |
2384d2b3 SY |
1424 | /* CF==1 or ZF==1 --> rc = -1 */ |
1425 | "; ja 1f ; ud2 ; 1:" | |
1426 | : : "a"(&operand), "c"(ext) : "cc", "memory"); | |
1427 | } | |
1428 | ||
1439442c SY |
1429 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
1430 | { | |
1431 | struct { | |
1432 | u64 eptp, gpa; | |
1433 | } operand = {eptp, gpa}; | |
1434 | ||
4ecac3fd | 1435 | asm volatile (__ex(ASM_VMX_INVEPT) |
1439442c SY |
1436 | /* CF==1 or ZF==1 --> rc = -1 */ |
1437 | "; ja 1f ; ud2 ; 1:\n" | |
1438 | : : "a" (&operand), "c" (ext) : "cc", "memory"); | |
1439 | } | |
1440 | ||
26bb0981 | 1441 | static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
a75beee6 ED |
1442 | { |
1443 | int i; | |
1444 | ||
8b9cf98c | 1445 | i = __find_msr_index(vmx, msr); |
a75beee6 | 1446 | if (i >= 0) |
a2fa3e9f | 1447 | return &vmx->guest_msrs[i]; |
8b6d44c7 | 1448 | return NULL; |
7725f0ba AK |
1449 | } |
1450 | ||
6aa8b732 AK |
1451 | static void vmcs_clear(struct vmcs *vmcs) |
1452 | { | |
1453 | u64 phys_addr = __pa(vmcs); | |
1454 | u8 error; | |
1455 | ||
4ecac3fd | 1456 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
16d8f72f | 1457 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
6aa8b732 AK |
1458 | : "cc", "memory"); |
1459 | if (error) | |
1460 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", | |
1461 | vmcs, phys_addr); | |
1462 | } | |
1463 | ||
d462b819 NHE |
1464 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
1465 | { | |
1466 | vmcs_clear(loaded_vmcs->vmcs); | |
355f4fb1 JM |
1467 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
1468 | vmcs_clear(loaded_vmcs->shadow_vmcs); | |
d462b819 NHE |
1469 | loaded_vmcs->cpu = -1; |
1470 | loaded_vmcs->launched = 0; | |
1471 | } | |
1472 | ||
7725b894 DX |
1473 | static void vmcs_load(struct vmcs *vmcs) |
1474 | { | |
1475 | u64 phys_addr = __pa(vmcs); | |
1476 | u8 error; | |
1477 | ||
1478 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" | |
16d8f72f | 1479 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
7725b894 DX |
1480 | : "cc", "memory"); |
1481 | if (error) | |
2844d849 | 1482 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
7725b894 DX |
1483 | vmcs, phys_addr); |
1484 | } | |
1485 | ||
2965faa5 | 1486 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
1487 | /* |
1488 | * This bitmap is used to indicate whether the vmclear | |
1489 | * operation is enabled on all cpus. All disabled by | |
1490 | * default. | |
1491 | */ | |
1492 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; | |
1493 | ||
1494 | static inline void crash_enable_local_vmclear(int cpu) | |
1495 | { | |
1496 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1497 | } | |
1498 | ||
1499 | static inline void crash_disable_local_vmclear(int cpu) | |
1500 | { | |
1501 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1502 | } | |
1503 | ||
1504 | static inline int crash_local_vmclear_enabled(int cpu) | |
1505 | { | |
1506 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1507 | } | |
1508 | ||
1509 | static void crash_vmclear_local_loaded_vmcss(void) | |
1510 | { | |
1511 | int cpu = raw_smp_processor_id(); | |
1512 | struct loaded_vmcs *v; | |
1513 | ||
1514 | if (!crash_local_vmclear_enabled(cpu)) | |
1515 | return; | |
1516 | ||
1517 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), | |
1518 | loaded_vmcss_on_cpu_link) | |
1519 | vmcs_clear(v->vmcs); | |
1520 | } | |
1521 | #else | |
1522 | static inline void crash_enable_local_vmclear(int cpu) { } | |
1523 | static inline void crash_disable_local_vmclear(int cpu) { } | |
2965faa5 | 1524 | #endif /* CONFIG_KEXEC_CORE */ |
8f536b76 | 1525 | |
d462b819 | 1526 | static void __loaded_vmcs_clear(void *arg) |
6aa8b732 | 1527 | { |
d462b819 | 1528 | struct loaded_vmcs *loaded_vmcs = arg; |
d3b2c338 | 1529 | int cpu = raw_smp_processor_id(); |
6aa8b732 | 1530 | |
d462b819 NHE |
1531 | if (loaded_vmcs->cpu != cpu) |
1532 | return; /* vcpu migration can race with cpu offline */ | |
1533 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) | |
6aa8b732 | 1534 | per_cpu(current_vmcs, cpu) = NULL; |
8f536b76 | 1535 | crash_disable_local_vmclear(cpu); |
d462b819 | 1536 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
5a560f8b XG |
1537 | |
1538 | /* | |
1539 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link | |
1540 | * is before setting loaded_vmcs->vcpu to -1 which is done in | |
1541 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist | |
1542 | * then adds the vmcs into percpu list before it is deleted. | |
1543 | */ | |
1544 | smp_wmb(); | |
1545 | ||
d462b819 | 1546 | loaded_vmcs_init(loaded_vmcs); |
8f536b76 | 1547 | crash_enable_local_vmclear(cpu); |
6aa8b732 AK |
1548 | } |
1549 | ||
d462b819 | 1550 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
8d0be2b3 | 1551 | { |
e6c7d321 XG |
1552 | int cpu = loaded_vmcs->cpu; |
1553 | ||
1554 | if (cpu != -1) | |
1555 | smp_call_function_single(cpu, | |
1556 | __loaded_vmcs_clear, loaded_vmcs, 1); | |
8d0be2b3 AK |
1557 | } |
1558 | ||
dd5f5341 | 1559 | static inline void vpid_sync_vcpu_single(int vpid) |
2384d2b3 | 1560 | { |
dd5f5341 | 1561 | if (vpid == 0) |
2384d2b3 SY |
1562 | return; |
1563 | ||
518c8aee | 1564 | if (cpu_has_vmx_invvpid_single()) |
dd5f5341 | 1565 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
2384d2b3 SY |
1566 | } |
1567 | ||
b9d762fa GJ |
1568 | static inline void vpid_sync_vcpu_global(void) |
1569 | { | |
1570 | if (cpu_has_vmx_invvpid_global()) | |
1571 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); | |
1572 | } | |
1573 | ||
dd5f5341 | 1574 | static inline void vpid_sync_context(int vpid) |
b9d762fa GJ |
1575 | { |
1576 | if (cpu_has_vmx_invvpid_single()) | |
dd5f5341 | 1577 | vpid_sync_vcpu_single(vpid); |
b9d762fa GJ |
1578 | else |
1579 | vpid_sync_vcpu_global(); | |
1580 | } | |
1581 | ||
1439442c SY |
1582 | static inline void ept_sync_global(void) |
1583 | { | |
1584 | if (cpu_has_vmx_invept_global()) | |
1585 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); | |
1586 | } | |
1587 | ||
1588 | static inline void ept_sync_context(u64 eptp) | |
1589 | { | |
089d034e | 1590 | if (enable_ept) { |
1439442c SY |
1591 | if (cpu_has_vmx_invept_context()) |
1592 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); | |
1593 | else | |
1594 | ept_sync_global(); | |
1595 | } | |
1596 | } | |
1597 | ||
8a86aea9 PB |
1598 | static __always_inline void vmcs_check16(unsigned long field) |
1599 | { | |
1600 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1601 | "16-bit accessor invalid for 64-bit field"); | |
1602 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1603 | "16-bit accessor invalid for 64-bit high field"); | |
1604 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1605 | "16-bit accessor invalid for 32-bit high field"); | |
1606 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1607 | "16-bit accessor invalid for natural width field"); | |
1608 | } | |
1609 | ||
1610 | static __always_inline void vmcs_check32(unsigned long field) | |
1611 | { | |
1612 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1613 | "32-bit accessor invalid for 16-bit field"); | |
1614 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1615 | "32-bit accessor invalid for natural width field"); | |
1616 | } | |
1617 | ||
1618 | static __always_inline void vmcs_check64(unsigned long field) | |
1619 | { | |
1620 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1621 | "64-bit accessor invalid for 16-bit field"); | |
1622 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1623 | "64-bit accessor invalid for 64-bit high field"); | |
1624 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1625 | "64-bit accessor invalid for 32-bit field"); | |
1626 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1627 | "64-bit accessor invalid for natural width field"); | |
1628 | } | |
1629 | ||
1630 | static __always_inline void vmcs_checkl(unsigned long field) | |
1631 | { | |
1632 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1633 | "Natural width accessor invalid for 16-bit field"); | |
1634 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1635 | "Natural width accessor invalid for 64-bit field"); | |
1636 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1637 | "Natural width accessor invalid for 64-bit high field"); | |
1638 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1639 | "Natural width accessor invalid for 32-bit field"); | |
1640 | } | |
1641 | ||
1642 | static __always_inline unsigned long __vmcs_readl(unsigned long field) | |
6aa8b732 | 1643 | { |
5e520e62 | 1644 | unsigned long value; |
6aa8b732 | 1645 | |
5e520e62 AK |
1646 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
1647 | : "=a"(value) : "d"(field) : "cc"); | |
6aa8b732 AK |
1648 | return value; |
1649 | } | |
1650 | ||
96304217 | 1651 | static __always_inline u16 vmcs_read16(unsigned long field) |
6aa8b732 | 1652 | { |
8a86aea9 PB |
1653 | vmcs_check16(field); |
1654 | return __vmcs_readl(field); | |
6aa8b732 AK |
1655 | } |
1656 | ||
96304217 | 1657 | static __always_inline u32 vmcs_read32(unsigned long field) |
6aa8b732 | 1658 | { |
8a86aea9 PB |
1659 | vmcs_check32(field); |
1660 | return __vmcs_readl(field); | |
6aa8b732 AK |
1661 | } |
1662 | ||
96304217 | 1663 | static __always_inline u64 vmcs_read64(unsigned long field) |
6aa8b732 | 1664 | { |
8a86aea9 | 1665 | vmcs_check64(field); |
05b3e0c2 | 1666 | #ifdef CONFIG_X86_64 |
8a86aea9 | 1667 | return __vmcs_readl(field); |
6aa8b732 | 1668 | #else |
8a86aea9 | 1669 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
6aa8b732 AK |
1670 | #endif |
1671 | } | |
1672 | ||
8a86aea9 PB |
1673 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
1674 | { | |
1675 | vmcs_checkl(field); | |
1676 | return __vmcs_readl(field); | |
1677 | } | |
1678 | ||
e52de1b8 AK |
1679 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
1680 | { | |
1681 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", | |
1682 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); | |
1683 | dump_stack(); | |
1684 | } | |
1685 | ||
8a86aea9 | 1686 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
6aa8b732 AK |
1687 | { |
1688 | u8 error; | |
1689 | ||
4ecac3fd | 1690 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
d77c26fc | 1691 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
e52de1b8 AK |
1692 | if (unlikely(error)) |
1693 | vmwrite_error(field, value); | |
6aa8b732 AK |
1694 | } |
1695 | ||
8a86aea9 | 1696 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
6aa8b732 | 1697 | { |
8a86aea9 PB |
1698 | vmcs_check16(field); |
1699 | __vmcs_writel(field, value); | |
6aa8b732 AK |
1700 | } |
1701 | ||
8a86aea9 | 1702 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
6aa8b732 | 1703 | { |
8a86aea9 PB |
1704 | vmcs_check32(field); |
1705 | __vmcs_writel(field, value); | |
6aa8b732 AK |
1706 | } |
1707 | ||
8a86aea9 | 1708 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
6aa8b732 | 1709 | { |
8a86aea9 PB |
1710 | vmcs_check64(field); |
1711 | __vmcs_writel(field, value); | |
7682f2d0 | 1712 | #ifndef CONFIG_X86_64 |
6aa8b732 | 1713 | asm volatile (""); |
8a86aea9 | 1714 | __vmcs_writel(field+1, value >> 32); |
6aa8b732 AK |
1715 | #endif |
1716 | } | |
1717 | ||
8a86aea9 | 1718 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
2ab455cc | 1719 | { |
8a86aea9 PB |
1720 | vmcs_checkl(field); |
1721 | __vmcs_writel(field, value); | |
2ab455cc AL |
1722 | } |
1723 | ||
8a86aea9 | 1724 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
2ab455cc | 1725 | { |
8a86aea9 PB |
1726 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1727 | "vmcs_clear_bits does not support 64-bit fields"); | |
1728 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); | |
2ab455cc AL |
1729 | } |
1730 | ||
8a86aea9 | 1731 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
2ab455cc | 1732 | { |
8a86aea9 PB |
1733 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1734 | "vmcs_set_bits does not support 64-bit fields"); | |
1735 | __vmcs_writel(field, __vmcs_readl(field) | mask); | |
2ab455cc AL |
1736 | } |
1737 | ||
8391ce44 PB |
1738 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
1739 | { | |
1740 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); | |
1741 | } | |
1742 | ||
2961e876 GN |
1743 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
1744 | { | |
1745 | vmcs_write32(VM_ENTRY_CONTROLS, val); | |
1746 | vmx->vm_entry_controls_shadow = val; | |
1747 | } | |
1748 | ||
1749 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) | |
1750 | { | |
1751 | if (vmx->vm_entry_controls_shadow != val) | |
1752 | vm_entry_controls_init(vmx, val); | |
1753 | } | |
1754 | ||
1755 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) | |
1756 | { | |
1757 | return vmx->vm_entry_controls_shadow; | |
1758 | } | |
1759 | ||
1760 | ||
1761 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
1762 | { | |
1763 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); | |
1764 | } | |
1765 | ||
1766 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
1767 | { | |
1768 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); | |
1769 | } | |
1770 | ||
8391ce44 PB |
1771 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
1772 | { | |
1773 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); | |
1774 | } | |
1775 | ||
2961e876 GN |
1776 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
1777 | { | |
1778 | vmcs_write32(VM_EXIT_CONTROLS, val); | |
1779 | vmx->vm_exit_controls_shadow = val; | |
1780 | } | |
1781 | ||
1782 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) | |
1783 | { | |
1784 | if (vmx->vm_exit_controls_shadow != val) | |
1785 | vm_exit_controls_init(vmx, val); | |
1786 | } | |
1787 | ||
1788 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) | |
1789 | { | |
1790 | return vmx->vm_exit_controls_shadow; | |
1791 | } | |
1792 | ||
1793 | ||
1794 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
1795 | { | |
1796 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); | |
1797 | } | |
1798 | ||
1799 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
1800 | { | |
1801 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); | |
1802 | } | |
1803 | ||
2fb92db1 AK |
1804 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
1805 | { | |
1806 | vmx->segment_cache.bitmask = 0; | |
1807 | } | |
1808 | ||
1809 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, | |
1810 | unsigned field) | |
1811 | { | |
1812 | bool ret; | |
1813 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); | |
1814 | ||
1815 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { | |
1816 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); | |
1817 | vmx->segment_cache.bitmask = 0; | |
1818 | } | |
1819 | ret = vmx->segment_cache.bitmask & mask; | |
1820 | vmx->segment_cache.bitmask |= mask; | |
1821 | return ret; | |
1822 | } | |
1823 | ||
1824 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) | |
1825 | { | |
1826 | u16 *p = &vmx->segment_cache.seg[seg].selector; | |
1827 | ||
1828 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) | |
1829 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); | |
1830 | return *p; | |
1831 | } | |
1832 | ||
1833 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) | |
1834 | { | |
1835 | ulong *p = &vmx->segment_cache.seg[seg].base; | |
1836 | ||
1837 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) | |
1838 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); | |
1839 | return *p; | |
1840 | } | |
1841 | ||
1842 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) | |
1843 | { | |
1844 | u32 *p = &vmx->segment_cache.seg[seg].limit; | |
1845 | ||
1846 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) | |
1847 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); | |
1848 | return *p; | |
1849 | } | |
1850 | ||
1851 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) | |
1852 | { | |
1853 | u32 *p = &vmx->segment_cache.seg[seg].ar; | |
1854 | ||
1855 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) | |
1856 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); | |
1857 | return *p; | |
1858 | } | |
1859 | ||
abd3f2d6 AK |
1860 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
1861 | { | |
1862 | u32 eb; | |
1863 | ||
fd7373cc | 1864 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
bd7e5b08 | 1865 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
fd7373cc JK |
1866 | if ((vcpu->guest_debug & |
1867 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == | |
1868 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) | |
1869 | eb |= 1u << BP_VECTOR; | |
7ffd92c5 | 1870 | if (to_vmx(vcpu)->rmode.vm86_active) |
abd3f2d6 | 1871 | eb = ~0; |
089d034e | 1872 | if (enable_ept) |
1439442c | 1873 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
36cf24e0 NHE |
1874 | |
1875 | /* When we are running a nested L2 guest and L1 specified for it a | |
1876 | * certain exception bitmap, we must trap the same exceptions and pass | |
1877 | * them to L1. When running L2, we will only handle the exceptions | |
1878 | * specified above if L1 did not want them. | |
1879 | */ | |
1880 | if (is_guest_mode(vcpu)) | |
1881 | eb |= get_vmcs12(vcpu)->exception_bitmap; | |
1882 | ||
abd3f2d6 AK |
1883 | vmcs_write32(EXCEPTION_BITMAP, eb); |
1884 | } | |
1885 | ||
2961e876 GN |
1886 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
1887 | unsigned long entry, unsigned long exit) | |
8bf00a52 | 1888 | { |
2961e876 GN |
1889 | vm_entry_controls_clearbit(vmx, entry); |
1890 | vm_exit_controls_clearbit(vmx, exit); | |
8bf00a52 GN |
1891 | } |
1892 | ||
61d2ef2c AK |
1893 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
1894 | { | |
1895 | unsigned i; | |
1896 | struct msr_autoload *m = &vmx->msr_autoload; | |
1897 | ||
8bf00a52 GN |
1898 | switch (msr) { |
1899 | case MSR_EFER: | |
1900 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
1901 | clear_atomic_switch_msr_special(vmx, |
1902 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
1903 | VM_EXIT_LOAD_IA32_EFER); |
1904 | return; | |
1905 | } | |
1906 | break; | |
1907 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
1908 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 1909 | clear_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
1910 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
1911 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
1912 | return; | |
1913 | } | |
1914 | break; | |
110312c8 AK |
1915 | } |
1916 | ||
61d2ef2c AK |
1917 | for (i = 0; i < m->nr; ++i) |
1918 | if (m->guest[i].index == msr) | |
1919 | break; | |
1920 | ||
1921 | if (i == m->nr) | |
1922 | return; | |
1923 | --m->nr; | |
1924 | m->guest[i] = m->guest[m->nr]; | |
1925 | m->host[i] = m->host[m->nr]; | |
1926 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); | |
1927 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); | |
1928 | } | |
1929 | ||
2961e876 GN |
1930 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
1931 | unsigned long entry, unsigned long exit, | |
1932 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, | |
1933 | u64 guest_val, u64 host_val) | |
8bf00a52 GN |
1934 | { |
1935 | vmcs_write64(guest_val_vmcs, guest_val); | |
1936 | vmcs_write64(host_val_vmcs, host_val); | |
2961e876 GN |
1937 | vm_entry_controls_setbit(vmx, entry); |
1938 | vm_exit_controls_setbit(vmx, exit); | |
8bf00a52 GN |
1939 | } |
1940 | ||
61d2ef2c AK |
1941 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
1942 | u64 guest_val, u64 host_val) | |
1943 | { | |
1944 | unsigned i; | |
1945 | struct msr_autoload *m = &vmx->msr_autoload; | |
1946 | ||
8bf00a52 GN |
1947 | switch (msr) { |
1948 | case MSR_EFER: | |
1949 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
1950 | add_atomic_switch_msr_special(vmx, |
1951 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
1952 | VM_EXIT_LOAD_IA32_EFER, |
1953 | GUEST_IA32_EFER, | |
1954 | HOST_IA32_EFER, | |
1955 | guest_val, host_val); | |
1956 | return; | |
1957 | } | |
1958 | break; | |
1959 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
1960 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 1961 | add_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
1962 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
1963 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, | |
1964 | GUEST_IA32_PERF_GLOBAL_CTRL, | |
1965 | HOST_IA32_PERF_GLOBAL_CTRL, | |
1966 | guest_val, host_val); | |
1967 | return; | |
1968 | } | |
1969 | break; | |
7099e2e1 RK |
1970 | case MSR_IA32_PEBS_ENABLE: |
1971 | /* PEBS needs a quiescent period after being disabled (to write | |
1972 | * a record). Disabling PEBS through VMX MSR swapping doesn't | |
1973 | * provide that period, so a CPU could write host's record into | |
1974 | * guest's memory. | |
1975 | */ | |
1976 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); | |
110312c8 AK |
1977 | } |
1978 | ||
61d2ef2c AK |
1979 | for (i = 0; i < m->nr; ++i) |
1980 | if (m->guest[i].index == msr) | |
1981 | break; | |
1982 | ||
e7fc6f93 | 1983 | if (i == NR_AUTOLOAD_MSRS) { |
60266204 | 1984 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
e7fc6f93 GN |
1985 | "Can't add msr %x\n", msr); |
1986 | return; | |
1987 | } else if (i == m->nr) { | |
61d2ef2c AK |
1988 | ++m->nr; |
1989 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); | |
1990 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); | |
1991 | } | |
1992 | ||
1993 | m->guest[i].index = msr; | |
1994 | m->guest[i].value = guest_val; | |
1995 | m->host[i].index = msr; | |
1996 | m->host[i].value = host_val; | |
1997 | } | |
1998 | ||
92c0d900 | 1999 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
2cc51560 | 2000 | { |
844a5fe2 PB |
2001 | u64 guest_efer = vmx->vcpu.arch.efer; |
2002 | u64 ignore_bits = 0; | |
2003 | ||
2004 | if (!enable_ept) { | |
2005 | /* | |
2006 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing | |
2007 | * host CPUID is more efficient than testing guest CPUID | |
2008 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. | |
2009 | */ | |
2010 | if (boot_cpu_has(X86_FEATURE_SMEP)) | |
2011 | guest_efer |= EFER_NX; | |
2012 | else if (!(guest_efer & EFER_NX)) | |
2013 | ignore_bits |= EFER_NX; | |
2014 | } | |
3a34a881 | 2015 | |
51c6cf66 | 2016 | /* |
844a5fe2 | 2017 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
51c6cf66 | 2018 | */ |
844a5fe2 | 2019 | ignore_bits |= EFER_SCE; |
51c6cf66 AK |
2020 | #ifdef CONFIG_X86_64 |
2021 | ignore_bits |= EFER_LMA | EFER_LME; | |
2022 | /* SCE is meaningful only in long mode on Intel */ | |
2023 | if (guest_efer & EFER_LMA) | |
2024 | ignore_bits &= ~(u64)EFER_SCE; | |
2025 | #endif | |
84ad33ef AK |
2026 | |
2027 | clear_atomic_switch_msr(vmx, MSR_EFER); | |
f6577a5f AL |
2028 | |
2029 | /* | |
2030 | * On EPT, we can't emulate NX, so we must switch EFER atomically. | |
2031 | * On CPUs that support "load IA32_EFER", always switch EFER | |
2032 | * atomically, since it's faster than switching it manually. | |
2033 | */ | |
2034 | if (cpu_has_load_ia32_efer || | |
2035 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { | |
84ad33ef AK |
2036 | if (!(guest_efer & EFER_LMA)) |
2037 | guest_efer &= ~EFER_LME; | |
54b98bff AL |
2038 | if (guest_efer != host_efer) |
2039 | add_atomic_switch_msr(vmx, MSR_EFER, | |
2040 | guest_efer, host_efer); | |
84ad33ef | 2041 | return false; |
844a5fe2 PB |
2042 | } else { |
2043 | guest_efer &= ~ignore_bits; | |
2044 | guest_efer |= host_efer & ignore_bits; | |
2045 | ||
2046 | vmx->guest_msrs[efer_offset].data = guest_efer; | |
2047 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; | |
84ad33ef | 2048 | |
844a5fe2 PB |
2049 | return true; |
2050 | } | |
51c6cf66 AK |
2051 | } |
2052 | ||
e28baead AL |
2053 | #ifdef CONFIG_X86_32 |
2054 | /* | |
2055 | * On 32-bit kernels, VM exits still load the FS and GS bases from the | |
2056 | * VMCS rather than the segment table. KVM uses this helper to figure | |
2057 | * out the current bases to poke them into the VMCS before entry. | |
2058 | */ | |
2d49ec72 GN |
2059 | static unsigned long segment_base(u16 selector) |
2060 | { | |
8c2e41f7 | 2061 | struct desc_struct *table; |
2d49ec72 GN |
2062 | unsigned long v; |
2063 | ||
8c2e41f7 | 2064 | if (!(selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2065 | return 0; |
2066 | ||
45fc8757 | 2067 | table = get_current_gdt_ro(); |
2d49ec72 | 2068 | |
8c2e41f7 | 2069 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
2d49ec72 GN |
2070 | u16 ldt_selector = kvm_read_ldt(); |
2071 | ||
8c2e41f7 | 2072 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2073 | return 0; |
2074 | ||
8c2e41f7 | 2075 | table = (struct desc_struct *)segment_base(ldt_selector); |
2d49ec72 | 2076 | } |
8c2e41f7 | 2077 | v = get_desc_base(&table[selector >> 3]); |
2d49ec72 GN |
2078 | return v; |
2079 | } | |
e28baead | 2080 | #endif |
2d49ec72 | 2081 | |
04d2cc77 | 2082 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
33ed6329 | 2083 | { |
04d2cc77 | 2084 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 2085 | int i; |
04d2cc77 | 2086 | |
a2fa3e9f | 2087 | if (vmx->host_state.loaded) |
33ed6329 AK |
2088 | return; |
2089 | ||
a2fa3e9f | 2090 | vmx->host_state.loaded = 1; |
33ed6329 AK |
2091 | /* |
2092 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not | |
2093 | * allow segment selectors with cpl > 0 or ti == 1. | |
2094 | */ | |
d6e88aec | 2095 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
152d3f2f | 2096 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
9581d442 | 2097 | savesegment(fs, vmx->host_state.fs_sel); |
152d3f2f | 2098 | if (!(vmx->host_state.fs_sel & 7)) { |
a2fa3e9f | 2099 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
152d3f2f LV |
2100 | vmx->host_state.fs_reload_needed = 0; |
2101 | } else { | |
33ed6329 | 2102 | vmcs_write16(HOST_FS_SELECTOR, 0); |
152d3f2f | 2103 | vmx->host_state.fs_reload_needed = 1; |
33ed6329 | 2104 | } |
9581d442 | 2105 | savesegment(gs, vmx->host_state.gs_sel); |
a2fa3e9f GH |
2106 | if (!(vmx->host_state.gs_sel & 7)) |
2107 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); | |
33ed6329 AK |
2108 | else { |
2109 | vmcs_write16(HOST_GS_SELECTOR, 0); | |
152d3f2f | 2110 | vmx->host_state.gs_ldt_reload_needed = 1; |
33ed6329 AK |
2111 | } |
2112 | ||
b2da15ac AK |
2113 | #ifdef CONFIG_X86_64 |
2114 | savesegment(ds, vmx->host_state.ds_sel); | |
2115 | savesegment(es, vmx->host_state.es_sel); | |
2116 | #endif | |
2117 | ||
33ed6329 AK |
2118 | #ifdef CONFIG_X86_64 |
2119 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); | |
2120 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); | |
2121 | #else | |
a2fa3e9f GH |
2122 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
2123 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); | |
33ed6329 | 2124 | #endif |
707c0874 AK |
2125 | |
2126 | #ifdef CONFIG_X86_64 | |
c8770e7b AK |
2127 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
2128 | if (is_long_mode(&vmx->vcpu)) | |
44ea2b17 | 2129 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
707c0874 | 2130 | #endif |
da8999d3 LJ |
2131 | if (boot_cpu_has(X86_FEATURE_MPX)) |
2132 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
26bb0981 AK |
2133 | for (i = 0; i < vmx->save_nmsrs; ++i) |
2134 | kvm_set_shared_msr(vmx->guest_msrs[i].index, | |
d5696725 AK |
2135 | vmx->guest_msrs[i].data, |
2136 | vmx->guest_msrs[i].mask); | |
33ed6329 AK |
2137 | } |
2138 | ||
a9b21b62 | 2139 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
33ed6329 | 2140 | { |
a2fa3e9f | 2141 | if (!vmx->host_state.loaded) |
33ed6329 AK |
2142 | return; |
2143 | ||
e1beb1d3 | 2144 | ++vmx->vcpu.stat.host_state_reload; |
a2fa3e9f | 2145 | vmx->host_state.loaded = 0; |
c8770e7b AK |
2146 | #ifdef CONFIG_X86_64 |
2147 | if (is_long_mode(&vmx->vcpu)) | |
2148 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); | |
2149 | #endif | |
152d3f2f | 2150 | if (vmx->host_state.gs_ldt_reload_needed) { |
d6e88aec | 2151 | kvm_load_ldt(vmx->host_state.ldt_sel); |
33ed6329 | 2152 | #ifdef CONFIG_X86_64 |
9581d442 | 2153 | load_gs_index(vmx->host_state.gs_sel); |
9581d442 AK |
2154 | #else |
2155 | loadsegment(gs, vmx->host_state.gs_sel); | |
33ed6329 | 2156 | #endif |
33ed6329 | 2157 | } |
0a77fe4c AK |
2158 | if (vmx->host_state.fs_reload_needed) |
2159 | loadsegment(fs, vmx->host_state.fs_sel); | |
b2da15ac AK |
2160 | #ifdef CONFIG_X86_64 |
2161 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { | |
2162 | loadsegment(ds, vmx->host_state.ds_sel); | |
2163 | loadsegment(es, vmx->host_state.es_sel); | |
2164 | } | |
b2da15ac | 2165 | #endif |
b7ffc44d | 2166 | invalidate_tss_limit(); |
44ea2b17 | 2167 | #ifdef CONFIG_X86_64 |
c8770e7b | 2168 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
44ea2b17 | 2169 | #endif |
da8999d3 LJ |
2170 | if (vmx->host_state.msr_host_bndcfgs) |
2171 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
45fc8757 | 2172 | load_fixmap_gdt(raw_smp_processor_id()); |
33ed6329 AK |
2173 | } |
2174 | ||
a9b21b62 AK |
2175 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
2176 | { | |
2177 | preempt_disable(); | |
2178 | __vmx_load_host_state(vmx); | |
2179 | preempt_enable(); | |
2180 | } | |
2181 | ||
28b835d6 FW |
2182 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
2183 | { | |
2184 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2185 | struct pi_desc old, new; | |
2186 | unsigned int dest; | |
2187 | ||
2188 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
2189 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
2190 | !kvm_vcpu_apicv_active(vcpu)) | |
28b835d6 FW |
2191 | return; |
2192 | ||
2193 | do { | |
2194 | old.control = new.control = pi_desc->control; | |
2195 | ||
2196 | /* | |
2197 | * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there | |
2198 | * are two possible cases: | |
2199 | * 1. After running 'pre_block', context switch | |
2200 | * happened. For this case, 'sn' was set in | |
2201 | * vmx_vcpu_put(), so we need to clear it here. | |
2202 | * 2. After running 'pre_block', we were blocked, | |
2203 | * and woken up by some other guy. For this case, | |
2204 | * we don't need to do anything, 'pi_post_block' | |
2205 | * will do everything for us. However, we cannot | |
2206 | * check whether it is case #1 or case #2 here | |
2207 | * (maybe, not needed), so we also clear sn here, | |
2208 | * I think it is not a big deal. | |
2209 | */ | |
2210 | if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) { | |
2211 | if (vcpu->cpu != cpu) { | |
2212 | dest = cpu_physical_id(cpu); | |
2213 | ||
2214 | if (x2apic_enabled()) | |
2215 | new.ndst = dest; | |
2216 | else | |
2217 | new.ndst = (dest << 8) & 0xFF00; | |
2218 | } | |
2219 | ||
2220 | /* set 'NV' to 'notification vector' */ | |
2221 | new.nv = POSTED_INTR_VECTOR; | |
2222 | } | |
2223 | ||
2224 | /* Allow posting non-urgent interrupts */ | |
2225 | new.sn = 0; | |
2226 | } while (cmpxchg(&pi_desc->control, old.control, | |
2227 | new.control) != old.control); | |
2228 | } | |
1be0e61c | 2229 | |
c95ba92a PF |
2230 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
2231 | { | |
2232 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; | |
2233 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); | |
2234 | } | |
2235 | ||
6aa8b732 AK |
2236 | /* |
2237 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes | |
2238 | * vcpu mutex is already taken. | |
2239 | */ | |
15ad7146 | 2240 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
6aa8b732 | 2241 | { |
a2fa3e9f | 2242 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b80c76ec | 2243 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
6aa8b732 | 2244 | |
b80c76ec | 2245 | if (!already_loaded) { |
fe0e80be | 2246 | loaded_vmcs_clear(vmx->loaded_vmcs); |
92fe13be | 2247 | local_irq_disable(); |
8f536b76 | 2248 | crash_disable_local_vmclear(cpu); |
5a560f8b XG |
2249 | |
2250 | /* | |
2251 | * Read loaded_vmcs->cpu should be before fetching | |
2252 | * loaded_vmcs->loaded_vmcss_on_cpu_link. | |
2253 | * See the comments in __loaded_vmcs_clear(). | |
2254 | */ | |
2255 | smp_rmb(); | |
2256 | ||
d462b819 NHE |
2257 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
2258 | &per_cpu(loaded_vmcss_on_cpu, cpu)); | |
8f536b76 | 2259 | crash_enable_local_vmclear(cpu); |
92fe13be | 2260 | local_irq_enable(); |
b80c76ec JM |
2261 | } |
2262 | ||
2263 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { | |
2264 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; | |
2265 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
2266 | } | |
2267 | ||
2268 | if (!already_loaded) { | |
59c58ceb | 2269 | void *gdt = get_current_gdt_ro(); |
b80c76ec JM |
2270 | unsigned long sysenter_esp; |
2271 | ||
2272 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); | |
92fe13be | 2273 | |
6aa8b732 AK |
2274 | /* |
2275 | * Linux uses per-cpu TSS and GDT, so set these when switching | |
e0c23063 | 2276 | * processors. See 22.2.4. |
6aa8b732 | 2277 | */ |
e0c23063 AL |
2278 | vmcs_writel(HOST_TR_BASE, |
2279 | (unsigned long)this_cpu_ptr(&cpu_tss)); | |
59c58ceb | 2280 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
6aa8b732 | 2281 | |
b7ffc44d AL |
2282 | /* |
2283 | * VM exits change the host TR limit to 0x67 after a VM | |
2284 | * exit. This is okay, since 0x67 covers everything except | |
2285 | * the IO bitmap and have have code to handle the IO bitmap | |
2286 | * being lost after a VM exit. | |
2287 | */ | |
2288 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); | |
2289 | ||
6aa8b732 AK |
2290 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
2291 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ | |
ff2c3a18 | 2292 | |
d462b819 | 2293 | vmx->loaded_vmcs->cpu = cpu; |
6aa8b732 | 2294 | } |
28b835d6 | 2295 | |
2680d6da OH |
2296 | /* Setup TSC multiplier */ |
2297 | if (kvm_has_tsc_control && | |
c95ba92a PF |
2298 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
2299 | decache_tsc_multiplier(vmx); | |
2680d6da | 2300 | |
28b835d6 | 2301 | vmx_vcpu_pi_load(vcpu, cpu); |
1be0e61c | 2302 | vmx->host_pkru = read_pkru(); |
28b835d6 FW |
2303 | } |
2304 | ||
2305 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) | |
2306 | { | |
2307 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2308 | ||
2309 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
2310 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
2311 | !kvm_vcpu_apicv_active(vcpu)) | |
28b835d6 FW |
2312 | return; |
2313 | ||
2314 | /* Set SN when the vCPU is preempted */ | |
2315 | if (vcpu->preempted) | |
2316 | pi_set_sn(pi_desc); | |
6aa8b732 AK |
2317 | } |
2318 | ||
2319 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) | |
2320 | { | |
28b835d6 FW |
2321 | vmx_vcpu_pi_put(vcpu); |
2322 | ||
a9b21b62 | 2323 | __vmx_load_host_state(to_vmx(vcpu)); |
6aa8b732 AK |
2324 | } |
2325 | ||
edcafe3c AK |
2326 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
2327 | ||
fe3ef05c NHE |
2328 | /* |
2329 | * Return the cr0 value that a nested guest would read. This is a combination | |
2330 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by | |
2331 | * its hypervisor (cr0_read_shadow). | |
2332 | */ | |
2333 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) | |
2334 | { | |
2335 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | | |
2336 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); | |
2337 | } | |
2338 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) | |
2339 | { | |
2340 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | | |
2341 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); | |
2342 | } | |
2343 | ||
6aa8b732 AK |
2344 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
2345 | { | |
78ac8b47 | 2346 | unsigned long rflags, save_rflags; |
345dcaa8 | 2347 | |
6de12732 AK |
2348 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
2349 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); | |
2350 | rflags = vmcs_readl(GUEST_RFLAGS); | |
2351 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
2352 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2353 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; | |
2354 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2355 | } | |
2356 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 | 2357 | } |
6de12732 | 2358 | return to_vmx(vcpu)->rflags; |
6aa8b732 AK |
2359 | } |
2360 | ||
2361 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
2362 | { | |
6de12732 AK |
2363 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
2364 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 AK |
2365 | if (to_vmx(vcpu)->rmode.vm86_active) { |
2366 | to_vmx(vcpu)->rmode.save_rflags = rflags; | |
053de044 | 2367 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
78ac8b47 | 2368 | } |
6aa8b732 AK |
2369 | vmcs_writel(GUEST_RFLAGS, rflags); |
2370 | } | |
2371 | ||
be94f6b7 HH |
2372 | static u32 vmx_get_pkru(struct kvm_vcpu *vcpu) |
2373 | { | |
2374 | return to_vmx(vcpu)->guest_pkru; | |
2375 | } | |
2376 | ||
37ccdcbe | 2377 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
2809f5d2 GC |
2378 | { |
2379 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2380 | int ret = 0; | |
2381 | ||
2382 | if (interruptibility & GUEST_INTR_STATE_STI) | |
48005f64 | 2383 | ret |= KVM_X86_SHADOW_INT_STI; |
2809f5d2 | 2384 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
48005f64 | 2385 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
2809f5d2 | 2386 | |
37ccdcbe | 2387 | return ret; |
2809f5d2 GC |
2388 | } |
2389 | ||
2390 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) | |
2391 | { | |
2392 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2393 | u32 interruptibility = interruptibility_old; | |
2394 | ||
2395 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); | |
2396 | ||
48005f64 | 2397 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
2809f5d2 | 2398 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
48005f64 | 2399 | else if (mask & KVM_X86_SHADOW_INT_STI) |
2809f5d2 GC |
2400 | interruptibility |= GUEST_INTR_STATE_STI; |
2401 | ||
2402 | if ((interruptibility != interruptibility_old)) | |
2403 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); | |
2404 | } | |
2405 | ||
6aa8b732 AK |
2406 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
2407 | { | |
2408 | unsigned long rip; | |
6aa8b732 | 2409 | |
5fdbf976 | 2410 | rip = kvm_rip_read(vcpu); |
6aa8b732 | 2411 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
5fdbf976 | 2412 | kvm_rip_write(vcpu, rip); |
6aa8b732 | 2413 | |
2809f5d2 GC |
2414 | /* skipping an emulated instruction also counts */ |
2415 | vmx_set_interrupt_shadow(vcpu, 0); | |
6aa8b732 AK |
2416 | } |
2417 | ||
0b6ac343 NHE |
2418 | /* |
2419 | * KVM wants to inject page-faults which it got to the guest. This function | |
2420 | * checks whether in a nested guest, we need to inject them to L1 or L2. | |
0b6ac343 | 2421 | */ |
e011c663 | 2422 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr) |
0b6ac343 NHE |
2423 | { |
2424 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
2425 | ||
e011c663 | 2426 | if (!(vmcs12->exception_bitmap & (1u << nr))) |
0b6ac343 NHE |
2427 | return 0; |
2428 | ||
533558bc JK |
2429 | nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason, |
2430 | vmcs_read32(VM_EXIT_INTR_INFO), | |
2431 | vmcs_readl(EXIT_QUALIFICATION)); | |
0b6ac343 NHE |
2432 | return 1; |
2433 | } | |
2434 | ||
298101da | 2435 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
ce7ddec4 JR |
2436 | bool has_error_code, u32 error_code, |
2437 | bool reinject) | |
298101da | 2438 | { |
77ab6db0 | 2439 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
8ab2d2e2 | 2440 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
77ab6db0 | 2441 | |
e011c663 GN |
2442 | if (!reinject && is_guest_mode(vcpu) && |
2443 | nested_vmx_check_exception(vcpu, nr)) | |
0b6ac343 NHE |
2444 | return; |
2445 | ||
8ab2d2e2 | 2446 | if (has_error_code) { |
77ab6db0 | 2447 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
8ab2d2e2 JK |
2448 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
2449 | } | |
77ab6db0 | 2450 | |
7ffd92c5 | 2451 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
2452 | int inc_eip = 0; |
2453 | if (kvm_exception_is_soft(nr)) | |
2454 | inc_eip = vcpu->arch.event_exit_inst_len; | |
2455 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) | |
a92601bb | 2456 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
77ab6db0 JK |
2457 | return; |
2458 | } | |
2459 | ||
66fd3f7f GN |
2460 | if (kvm_exception_is_soft(nr)) { |
2461 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
2462 | vmx->vcpu.arch.event_exit_inst_len); | |
8ab2d2e2 JK |
2463 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
2464 | } else | |
2465 | intr_info |= INTR_TYPE_HARD_EXCEPTION; | |
2466 | ||
2467 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); | |
298101da AK |
2468 | } |
2469 | ||
4e47c7a6 SY |
2470 | static bool vmx_rdtscp_supported(void) |
2471 | { | |
2472 | return cpu_has_vmx_rdtscp(); | |
2473 | } | |
2474 | ||
ad756a16 MJ |
2475 | static bool vmx_invpcid_supported(void) |
2476 | { | |
2477 | return cpu_has_vmx_invpcid() && enable_ept; | |
2478 | } | |
2479 | ||
a75beee6 ED |
2480 | /* |
2481 | * Swap MSR entry in host/guest MSR entry array. | |
2482 | */ | |
8b9cf98c | 2483 | static void move_msr_up(struct vcpu_vmx *vmx, int from, int to) |
a75beee6 | 2484 | { |
26bb0981 | 2485 | struct shared_msr_entry tmp; |
a2fa3e9f GH |
2486 | |
2487 | tmp = vmx->guest_msrs[to]; | |
2488 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; | |
2489 | vmx->guest_msrs[from] = tmp; | |
a75beee6 ED |
2490 | } |
2491 | ||
8d14695f YZ |
2492 | static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu) |
2493 | { | |
2494 | unsigned long *msr_bitmap; | |
2495 | ||
670125bd | 2496 | if (is_guest_mode(vcpu)) |
d048c098 | 2497 | msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap; |
3ce424e4 RK |
2498 | else if (cpu_has_secondary_exec_ctrls() && |
2499 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & | |
2500 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { | |
f6e90f9e WL |
2501 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) { |
2502 | if (is_long_mode(vcpu)) | |
c63e4563 | 2503 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv; |
f6e90f9e | 2504 | else |
c63e4563 | 2505 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv; |
f6e90f9e WL |
2506 | } else { |
2507 | if (is_long_mode(vcpu)) | |
c63e4563 | 2508 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic; |
f6e90f9e | 2509 | else |
c63e4563 | 2510 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic; |
f6e90f9e | 2511 | } |
8d14695f YZ |
2512 | } else { |
2513 | if (is_long_mode(vcpu)) | |
2514 | msr_bitmap = vmx_msr_bitmap_longmode; | |
2515 | else | |
2516 | msr_bitmap = vmx_msr_bitmap_legacy; | |
2517 | } | |
2518 | ||
2519 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); | |
2520 | } | |
2521 | ||
e38aea3e AK |
2522 | /* |
2523 | * Set up the vmcs to automatically save and restore system | |
2524 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy | |
2525 | * mode, as fiddling with msrs is very expensive. | |
2526 | */ | |
8b9cf98c | 2527 | static void setup_msrs(struct vcpu_vmx *vmx) |
e38aea3e | 2528 | { |
26bb0981 | 2529 | int save_nmsrs, index; |
e38aea3e | 2530 | |
a75beee6 ED |
2531 | save_nmsrs = 0; |
2532 | #ifdef CONFIG_X86_64 | |
8b9cf98c | 2533 | if (is_long_mode(&vmx->vcpu)) { |
8b9cf98c | 2534 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
a75beee6 | 2535 | if (index >= 0) |
8b9cf98c RR |
2536 | move_msr_up(vmx, index, save_nmsrs++); |
2537 | index = __find_msr_index(vmx, MSR_LSTAR); | |
a75beee6 | 2538 | if (index >= 0) |
8b9cf98c RR |
2539 | move_msr_up(vmx, index, save_nmsrs++); |
2540 | index = __find_msr_index(vmx, MSR_CSTAR); | |
a75beee6 | 2541 | if (index >= 0) |
8b9cf98c | 2542 | move_msr_up(vmx, index, save_nmsrs++); |
4e47c7a6 | 2543 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
1cea0ce6 | 2544 | if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu)) |
4e47c7a6 | 2545 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 | 2546 | /* |
8c06585d | 2547 | * MSR_STAR is only needed on long mode guests, and only |
a75beee6 ED |
2548 | * if efer.sce is enabled. |
2549 | */ | |
8c06585d | 2550 | index = __find_msr_index(vmx, MSR_STAR); |
f6801dff | 2551 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
8b9cf98c | 2552 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 ED |
2553 | } |
2554 | #endif | |
92c0d900 AK |
2555 | index = __find_msr_index(vmx, MSR_EFER); |
2556 | if (index >= 0 && update_transition_efer(vmx, index)) | |
26bb0981 | 2557 | move_msr_up(vmx, index, save_nmsrs++); |
e38aea3e | 2558 | |
26bb0981 | 2559 | vmx->save_nmsrs = save_nmsrs; |
5897297b | 2560 | |
8d14695f YZ |
2561 | if (cpu_has_vmx_msr_bitmap()) |
2562 | vmx_set_msr_bitmap(&vmx->vcpu); | |
e38aea3e AK |
2563 | } |
2564 | ||
6aa8b732 AK |
2565 | /* |
2566 | * reads and returns guest's timestamp counter "register" | |
be7b263e HZ |
2567 | * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset |
2568 | * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3 | |
6aa8b732 | 2569 | */ |
be7b263e | 2570 | static u64 guest_read_tsc(struct kvm_vcpu *vcpu) |
6aa8b732 AK |
2571 | { |
2572 | u64 host_tsc, tsc_offset; | |
2573 | ||
4ea1636b | 2574 | host_tsc = rdtsc(); |
6aa8b732 | 2575 | tsc_offset = vmcs_read64(TSC_OFFSET); |
be7b263e | 2576 | return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; |
6aa8b732 AK |
2577 | } |
2578 | ||
2579 | /* | |
99e3e30a | 2580 | * writes 'offset' into guest's timestamp counter offset register |
6aa8b732 | 2581 | */ |
99e3e30a | 2582 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
6aa8b732 | 2583 | { |
27fc51b2 | 2584 | if (is_guest_mode(vcpu)) { |
7991825b | 2585 | /* |
27fc51b2 NHE |
2586 | * We're here if L1 chose not to trap WRMSR to TSC. According |
2587 | * to the spec, this should set L1's TSC; The offset that L1 | |
2588 | * set for L2 remains unchanged, and still needs to be added | |
2589 | * to the newly set TSC to get L2's TSC. | |
7991825b | 2590 | */ |
27fc51b2 | 2591 | struct vmcs12 *vmcs12; |
27fc51b2 NHE |
2592 | /* recalculate vmcs02.TSC_OFFSET: */ |
2593 | vmcs12 = get_vmcs12(vcpu); | |
2594 | vmcs_write64(TSC_OFFSET, offset + | |
2595 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? | |
2596 | vmcs12->tsc_offset : 0)); | |
2597 | } else { | |
489223ed YY |
2598 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
2599 | vmcs_read64(TSC_OFFSET), offset); | |
27fc51b2 NHE |
2600 | vmcs_write64(TSC_OFFSET, offset); |
2601 | } | |
6aa8b732 AK |
2602 | } |
2603 | ||
801d3424 NHE |
2604 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
2605 | { | |
2606 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); | |
2607 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); | |
2608 | } | |
2609 | ||
2610 | /* | |
2611 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX | |
2612 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for | |
2613 | * all guests if the "nested" module option is off, and can also be disabled | |
2614 | * for a single guest by disabling its VMX cpuid bit. | |
2615 | */ | |
2616 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) | |
2617 | { | |
2618 | return nested && guest_cpuid_has_vmx(vcpu); | |
2619 | } | |
2620 | ||
b87a51ae NHE |
2621 | /* |
2622 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be | |
2623 | * returned for the various VMX controls MSRs when nested VMX is enabled. | |
2624 | * The same values should also be used to verify that vmcs12 control fields are | |
2625 | * valid during nested entry from L1 to L2. | |
2626 | * Each of these control msrs has a low and high 32-bit half: A low bit is on | |
2627 | * if the corresponding bit in the (32-bit) control field *must* be on, and a | |
2628 | * bit in the high half is on if the corresponding bit in the control field | |
2629 | * may be on. See also vmx_control_verify(). | |
b87a51ae | 2630 | */ |
b9c237bb | 2631 | static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) |
b87a51ae NHE |
2632 | { |
2633 | /* | |
2634 | * Note that as a general rule, the high half of the MSRs (bits in | |
2635 | * the control fields which may be 1) should be initialized by the | |
2636 | * intersection of the underlying hardware's MSR (i.e., features which | |
2637 | * can be supported) and the list of features we want to expose - | |
2638 | * because they are known to be properly supported in our code. | |
2639 | * Also, usually, the low half of the MSRs (bits which must be 1) can | |
2640 | * be set to 0, meaning that L1 may turn off any of these bits. The | |
2641 | * reason is that if one of these bits is necessary, it will appear | |
2642 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control | |
2643 | * fields of vmcs01 and vmcs02, will turn these bits off - and | |
2644 | * nested_vmx_exit_handled() will not pass related exits to L1. | |
2645 | * These rules have exceptions below. | |
2646 | */ | |
2647 | ||
2648 | /* pin-based controls */ | |
eabeaacc | 2649 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
b9c237bb WV |
2650 | vmx->nested.nested_vmx_pinbased_ctls_low, |
2651 | vmx->nested.nested_vmx_pinbased_ctls_high); | |
2652 | vmx->nested.nested_vmx_pinbased_ctls_low |= | |
2653 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
2654 | vmx->nested.nested_vmx_pinbased_ctls_high &= | |
2655 | PIN_BASED_EXT_INTR_MASK | | |
2656 | PIN_BASED_NMI_EXITING | | |
2657 | PIN_BASED_VIRTUAL_NMIS; | |
2658 | vmx->nested.nested_vmx_pinbased_ctls_high |= | |
2659 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | | |
0238ea91 | 2660 | PIN_BASED_VMX_PREEMPTION_TIMER; |
d62caabb | 2661 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) |
705699a1 WV |
2662 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
2663 | PIN_BASED_POSTED_INTR; | |
b87a51ae | 2664 | |
3dbcd8da | 2665 | /* exit controls */ |
c0dfee58 | 2666 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
b9c237bb WV |
2667 | vmx->nested.nested_vmx_exit_ctls_low, |
2668 | vmx->nested.nested_vmx_exit_ctls_high); | |
2669 | vmx->nested.nested_vmx_exit_ctls_low = | |
2670 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; | |
e0ba1a6f | 2671 | |
b9c237bb | 2672 | vmx->nested.nested_vmx_exit_ctls_high &= |
b87a51ae | 2673 | #ifdef CONFIG_X86_64 |
c0dfee58 | 2674 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
b87a51ae | 2675 | #endif |
f4124500 | 2676 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
b9c237bb WV |
2677 | vmx->nested.nested_vmx_exit_ctls_high |= |
2678 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | | |
f4124500 | 2679 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
e0ba1a6f BD |
2680 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
2681 | ||
a87036ad | 2682 | if (kvm_mpx_supported()) |
b9c237bb | 2683 | vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
b87a51ae | 2684 | |
2996fca0 | 2685 | /* We support free control of debug control saving. */ |
0115f9cb | 2686 | vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
2996fca0 | 2687 | |
b87a51ae NHE |
2688 | /* entry controls */ |
2689 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, | |
b9c237bb WV |
2690 | vmx->nested.nested_vmx_entry_ctls_low, |
2691 | vmx->nested.nested_vmx_entry_ctls_high); | |
2692 | vmx->nested.nested_vmx_entry_ctls_low = | |
2693 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; | |
2694 | vmx->nested.nested_vmx_entry_ctls_high &= | |
57435349 JK |
2695 | #ifdef CONFIG_X86_64 |
2696 | VM_ENTRY_IA32E_MODE | | |
2697 | #endif | |
2698 | VM_ENTRY_LOAD_IA32_PAT; | |
b9c237bb WV |
2699 | vmx->nested.nested_vmx_entry_ctls_high |= |
2700 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); | |
a87036ad | 2701 | if (kvm_mpx_supported()) |
b9c237bb | 2702 | vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
57435349 | 2703 | |
2996fca0 | 2704 | /* We support free control of debug control loading. */ |
0115f9cb | 2705 | vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
2996fca0 | 2706 | |
b87a51ae NHE |
2707 | /* cpu-based controls */ |
2708 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, | |
b9c237bb WV |
2709 | vmx->nested.nested_vmx_procbased_ctls_low, |
2710 | vmx->nested.nested_vmx_procbased_ctls_high); | |
2711 | vmx->nested.nested_vmx_procbased_ctls_low = | |
2712 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
2713 | vmx->nested.nested_vmx_procbased_ctls_high &= | |
a294c9bb JK |
2714 | CPU_BASED_VIRTUAL_INTR_PENDING | |
2715 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | | |
b87a51ae NHE |
2716 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
2717 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | | |
2718 | CPU_BASED_CR3_STORE_EXITING | | |
2719 | #ifdef CONFIG_X86_64 | |
2720 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | | |
2721 | #endif | |
2722 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | | |
5f3d45e7 MD |
2723 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
2724 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | | |
2725 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | | |
2726 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
b87a51ae NHE |
2727 | /* |
2728 | * We can allow some features even when not supported by the | |
2729 | * hardware. For example, L1 can specify an MSR bitmap - and we | |
2730 | * can use it to avoid exits to L1 - even when L0 runs L2 | |
2731 | * without MSR bitmaps. | |
2732 | */ | |
b9c237bb WV |
2733 | vmx->nested.nested_vmx_procbased_ctls_high |= |
2734 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | | |
560b7ee1 | 2735 | CPU_BASED_USE_MSR_BITMAPS; |
b87a51ae | 2736 | |
3dcdf3ec | 2737 | /* We support free control of CR3 access interception. */ |
0115f9cb | 2738 | vmx->nested.nested_vmx_procbased_ctls_low &= |
3dcdf3ec JK |
2739 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
2740 | ||
b87a51ae NHE |
2741 | /* secondary cpu-based controls */ |
2742 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, | |
b9c237bb WV |
2743 | vmx->nested.nested_vmx_secondary_ctls_low, |
2744 | vmx->nested.nested_vmx_secondary_ctls_high); | |
2745 | vmx->nested.nested_vmx_secondary_ctls_low = 0; | |
2746 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
a5f46457 | 2747 | SECONDARY_EXEC_RDRAND | SECONDARY_EXEC_RDSEED | |
d6851fbe | 2748 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
b3a2a907 | 2749 | SECONDARY_EXEC_RDTSCP | |
1b07304c | 2750 | SECONDARY_EXEC_DESC | |
f2b93280 | 2751 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
82f0dd4b | 2752 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
608406e2 | 2753 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
81dc01f7 | 2754 | SECONDARY_EXEC_WBINVD_EXITING | |
dfa169bb | 2755 | SECONDARY_EXEC_XSAVES; |
c18911a2 | 2756 | |
afa61f75 NHE |
2757 | if (enable_ept) { |
2758 | /* nested EPT: emulate EPT also to L1 */ | |
b9c237bb | 2759 | vmx->nested.nested_vmx_secondary_ctls_high |= |
0790ec17 | 2760 | SECONDARY_EXEC_ENABLE_EPT; |
b9c237bb | 2761 | vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
7db74265 | 2762 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
02120c45 BD |
2763 | if (cpu_has_vmx_ept_execute_only()) |
2764 | vmx->nested.nested_vmx_ept_caps |= | |
2765 | VMX_EPT_EXECUTE_ONLY_BIT; | |
b9c237bb | 2766 | vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept; |
45e11817 | 2767 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
7db74265 PB |
2768 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
2769 | VMX_EPT_1GB_PAGE_BIT; | |
03efce6f BD |
2770 | if (enable_ept_ad_bits) { |
2771 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2772 | SECONDARY_EXEC_ENABLE_PML; | |
7461fbc4 | 2773 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT; |
03efce6f | 2774 | } |
afa61f75 | 2775 | } else |
b9c237bb | 2776 | vmx->nested.nested_vmx_ept_caps = 0; |
afa61f75 | 2777 | |
ef697a71 PB |
2778 | /* |
2779 | * Old versions of KVM use the single-context version without | |
2780 | * checking for support, so declare that it is supported even | |
2781 | * though it is treated as global context. The alternative is | |
2782 | * not failing the single-context invvpid, and it is worse. | |
2783 | */ | |
63cb6d5f WL |
2784 | if (enable_vpid) { |
2785 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2786 | SECONDARY_EXEC_ENABLE_VPID; | |
089d7b6e | 2787 | vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT | |
bcdde302 | 2788 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
63cb6d5f | 2789 | } else |
089d7b6e | 2790 | vmx->nested.nested_vmx_vpid_caps = 0; |
99b83ac8 | 2791 | |
0790ec17 RK |
2792 | if (enable_unrestricted_guest) |
2793 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
2794 | SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
2795 | ||
c18911a2 | 2796 | /* miscellaneous data */ |
b9c237bb WV |
2797 | rdmsr(MSR_IA32_VMX_MISC, |
2798 | vmx->nested.nested_vmx_misc_low, | |
2799 | vmx->nested.nested_vmx_misc_high); | |
2800 | vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; | |
2801 | vmx->nested.nested_vmx_misc_low |= | |
2802 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | | |
f4124500 | 2803 | VMX_MISC_ACTIVITY_HLT; |
b9c237bb | 2804 | vmx->nested.nested_vmx_misc_high = 0; |
62cc6b9d DM |
2805 | |
2806 | /* | |
2807 | * This MSR reports some information about VMX support. We | |
2808 | * should return information about the VMX we emulate for the | |
2809 | * guest, and the VMCS structure we give it - not about the | |
2810 | * VMX support of the underlying hardware. | |
2811 | */ | |
2812 | vmx->nested.nested_vmx_basic = | |
2813 | VMCS12_REVISION | | |
2814 | VMX_BASIC_TRUE_CTLS | | |
2815 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | | |
2816 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); | |
2817 | ||
2818 | if (cpu_has_vmx_basic_inout()) | |
2819 | vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT; | |
2820 | ||
2821 | /* | |
8322ebbb | 2822 | * These MSRs specify bits which the guest must keep fixed on |
62cc6b9d DM |
2823 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
2824 | * We picked the standard core2 setting. | |
2825 | */ | |
2826 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) | |
2827 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE | |
2828 | vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON; | |
62cc6b9d | 2829 | vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON; |
8322ebbb DM |
2830 | |
2831 | /* These MSRs specify bits which the guest must keep fixed off. */ | |
2832 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1); | |
2833 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1); | |
62cc6b9d DM |
2834 | |
2835 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ | |
2836 | vmx->nested.nested_vmx_vmcs_enum = 0x2e; | |
b87a51ae NHE |
2837 | } |
2838 | ||
3899152c DM |
2839 | /* |
2840 | * if fixed0[i] == 1: val[i] must be 1 | |
2841 | * if fixed1[i] == 0: val[i] must be 0 | |
2842 | */ | |
2843 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) | |
2844 | { | |
2845 | return ((val & fixed1) | fixed0) == val; | |
b87a51ae NHE |
2846 | } |
2847 | ||
2848 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) | |
2849 | { | |
3899152c | 2850 | return fixed_bits_valid(control, low, high); |
b87a51ae NHE |
2851 | } |
2852 | ||
2853 | static inline u64 vmx_control_msr(u32 low, u32 high) | |
2854 | { | |
2855 | return low | ((u64)high << 32); | |
2856 | } | |
2857 | ||
62cc6b9d DM |
2858 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
2859 | { | |
2860 | superset &= mask; | |
2861 | subset &= mask; | |
2862 | ||
2863 | return (superset | subset) == superset; | |
2864 | } | |
2865 | ||
2866 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) | |
2867 | { | |
2868 | const u64 feature_and_reserved = | |
2869 | /* feature (except bit 48; see below) */ | |
2870 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | | |
2871 | /* reserved */ | |
2872 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); | |
2873 | u64 vmx_basic = vmx->nested.nested_vmx_basic; | |
2874 | ||
2875 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) | |
2876 | return -EINVAL; | |
2877 | ||
2878 | /* | |
2879 | * KVM does not emulate a version of VMX that constrains physical | |
2880 | * addresses of VMX structures (e.g. VMCS) to 32-bits. | |
2881 | */ | |
2882 | if (data & BIT_ULL(48)) | |
2883 | return -EINVAL; | |
2884 | ||
2885 | if (vmx_basic_vmcs_revision_id(vmx_basic) != | |
2886 | vmx_basic_vmcs_revision_id(data)) | |
2887 | return -EINVAL; | |
2888 | ||
2889 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) | |
2890 | return -EINVAL; | |
2891 | ||
2892 | vmx->nested.nested_vmx_basic = data; | |
2893 | return 0; | |
2894 | } | |
2895 | ||
2896 | static int | |
2897 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
2898 | { | |
2899 | u64 supported; | |
2900 | u32 *lowp, *highp; | |
2901 | ||
2902 | switch (msr_index) { | |
2903 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
2904 | lowp = &vmx->nested.nested_vmx_pinbased_ctls_low; | |
2905 | highp = &vmx->nested.nested_vmx_pinbased_ctls_high; | |
2906 | break; | |
2907 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
2908 | lowp = &vmx->nested.nested_vmx_procbased_ctls_low; | |
2909 | highp = &vmx->nested.nested_vmx_procbased_ctls_high; | |
2910 | break; | |
2911 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
2912 | lowp = &vmx->nested.nested_vmx_exit_ctls_low; | |
2913 | highp = &vmx->nested.nested_vmx_exit_ctls_high; | |
2914 | break; | |
2915 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
2916 | lowp = &vmx->nested.nested_vmx_entry_ctls_low; | |
2917 | highp = &vmx->nested.nested_vmx_entry_ctls_high; | |
2918 | break; | |
2919 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
2920 | lowp = &vmx->nested.nested_vmx_secondary_ctls_low; | |
2921 | highp = &vmx->nested.nested_vmx_secondary_ctls_high; | |
2922 | break; | |
2923 | default: | |
2924 | BUG(); | |
2925 | } | |
2926 | ||
2927 | supported = vmx_control_msr(*lowp, *highp); | |
2928 | ||
2929 | /* Check must-be-1 bits are still 1. */ | |
2930 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) | |
2931 | return -EINVAL; | |
2932 | ||
2933 | /* Check must-be-0 bits are still 0. */ | |
2934 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) | |
2935 | return -EINVAL; | |
2936 | ||
2937 | *lowp = data; | |
2938 | *highp = data >> 32; | |
2939 | return 0; | |
2940 | } | |
2941 | ||
2942 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) | |
2943 | { | |
2944 | const u64 feature_and_reserved_bits = | |
2945 | /* feature */ | |
2946 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | | |
2947 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | | |
2948 | /* reserved */ | |
2949 | GENMASK_ULL(13, 9) | BIT_ULL(31); | |
2950 | u64 vmx_misc; | |
2951 | ||
2952 | vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low, | |
2953 | vmx->nested.nested_vmx_misc_high); | |
2954 | ||
2955 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) | |
2956 | return -EINVAL; | |
2957 | ||
2958 | if ((vmx->nested.nested_vmx_pinbased_ctls_high & | |
2959 | PIN_BASED_VMX_PREEMPTION_TIMER) && | |
2960 | vmx_misc_preemption_timer_rate(data) != | |
2961 | vmx_misc_preemption_timer_rate(vmx_misc)) | |
2962 | return -EINVAL; | |
2963 | ||
2964 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) | |
2965 | return -EINVAL; | |
2966 | ||
2967 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) | |
2968 | return -EINVAL; | |
2969 | ||
2970 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) | |
2971 | return -EINVAL; | |
2972 | ||
2973 | vmx->nested.nested_vmx_misc_low = data; | |
2974 | vmx->nested.nested_vmx_misc_high = data >> 32; | |
2975 | return 0; | |
2976 | } | |
2977 | ||
2978 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) | |
2979 | { | |
2980 | u64 vmx_ept_vpid_cap; | |
2981 | ||
2982 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps, | |
2983 | vmx->nested.nested_vmx_vpid_caps); | |
2984 | ||
2985 | /* Every bit is either reserved or a feature bit. */ | |
2986 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) | |
2987 | return -EINVAL; | |
2988 | ||
2989 | vmx->nested.nested_vmx_ept_caps = data; | |
2990 | vmx->nested.nested_vmx_vpid_caps = data >> 32; | |
2991 | return 0; | |
2992 | } | |
2993 | ||
2994 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
2995 | { | |
2996 | u64 *msr; | |
2997 | ||
2998 | switch (msr_index) { | |
2999 | case MSR_IA32_VMX_CR0_FIXED0: | |
3000 | msr = &vmx->nested.nested_vmx_cr0_fixed0; | |
3001 | break; | |
3002 | case MSR_IA32_VMX_CR4_FIXED0: | |
3003 | msr = &vmx->nested.nested_vmx_cr4_fixed0; | |
3004 | break; | |
3005 | default: | |
3006 | BUG(); | |
3007 | } | |
3008 | ||
3009 | /* | |
3010 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) | |
3011 | * must be 1 in the restored value. | |
3012 | */ | |
3013 | if (!is_bitwise_subset(data, *msr, -1ULL)) | |
3014 | return -EINVAL; | |
3015 | ||
3016 | *msr = data; | |
3017 | return 0; | |
3018 | } | |
3019 | ||
3020 | /* | |
3021 | * Called when userspace is restoring VMX MSRs. | |
3022 | * | |
3023 | * Returns 0 on success, non-0 otherwise. | |
3024 | */ | |
3025 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
b87a51ae | 3026 | { |
b9c237bb WV |
3027 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3028 | ||
b87a51ae | 3029 | switch (msr_index) { |
b87a51ae | 3030 | case MSR_IA32_VMX_BASIC: |
62cc6b9d DM |
3031 | return vmx_restore_vmx_basic(vmx, data); |
3032 | case MSR_IA32_VMX_PINBASED_CTLS: | |
3033 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
3034 | case MSR_IA32_VMX_EXIT_CTLS: | |
3035 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b87a51ae | 3036 | /* |
62cc6b9d DM |
3037 | * The "non-true" VMX capability MSRs are generated from the |
3038 | * "true" MSRs, so we do not support restoring them directly. | |
3039 | * | |
3040 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace | |
3041 | * should restore the "true" MSRs with the must-be-1 bits | |
3042 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND | |
3043 | * DEFAULT SETTINGS". | |
b87a51ae | 3044 | */ |
62cc6b9d DM |
3045 | return -EINVAL; |
3046 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3047 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3048 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3049 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3050 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
3051 | return vmx_restore_control_msr(vmx, msr_index, data); | |
3052 | case MSR_IA32_VMX_MISC: | |
3053 | return vmx_restore_vmx_misc(vmx, data); | |
3054 | case MSR_IA32_VMX_CR0_FIXED0: | |
3055 | case MSR_IA32_VMX_CR4_FIXED0: | |
3056 | return vmx_restore_fixed0_msr(vmx, msr_index, data); | |
3057 | case MSR_IA32_VMX_CR0_FIXED1: | |
3058 | case MSR_IA32_VMX_CR4_FIXED1: | |
3059 | /* | |
3060 | * These MSRs are generated based on the vCPU's CPUID, so we | |
3061 | * do not support restoring them directly. | |
3062 | */ | |
3063 | return -EINVAL; | |
3064 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
3065 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); | |
3066 | case MSR_IA32_VMX_VMCS_ENUM: | |
3067 | vmx->nested.nested_vmx_vmcs_enum = data; | |
3068 | return 0; | |
3069 | default: | |
b87a51ae | 3070 | /* |
62cc6b9d | 3071 | * The rest of the VMX capability MSRs do not support restore. |
b87a51ae | 3072 | */ |
62cc6b9d DM |
3073 | return -EINVAL; |
3074 | } | |
3075 | } | |
3076 | ||
3077 | /* Returns 0 on success, non-0 otherwise. */ | |
3078 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
3079 | { | |
3080 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
3081 | ||
3082 | switch (msr_index) { | |
3083 | case MSR_IA32_VMX_BASIC: | |
3084 | *pdata = vmx->nested.nested_vmx_basic; | |
b87a51ae NHE |
3085 | break; |
3086 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3087 | case MSR_IA32_VMX_PINBASED_CTLS: | |
b9c237bb WV |
3088 | *pdata = vmx_control_msr( |
3089 | vmx->nested.nested_vmx_pinbased_ctls_low, | |
3090 | vmx->nested.nested_vmx_pinbased_ctls_high); | |
0115f9cb DM |
3091 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
3092 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3093 | break; |
3094 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3095 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
b9c237bb WV |
3096 | *pdata = vmx_control_msr( |
3097 | vmx->nested.nested_vmx_procbased_ctls_low, | |
3098 | vmx->nested.nested_vmx_procbased_ctls_high); | |
0115f9cb DM |
3099 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
3100 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3101 | break; |
3102 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3103 | case MSR_IA32_VMX_EXIT_CTLS: | |
b9c237bb WV |
3104 | *pdata = vmx_control_msr( |
3105 | vmx->nested.nested_vmx_exit_ctls_low, | |
3106 | vmx->nested.nested_vmx_exit_ctls_high); | |
0115f9cb DM |
3107 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
3108 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3109 | break; |
3110 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3111 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b9c237bb WV |
3112 | *pdata = vmx_control_msr( |
3113 | vmx->nested.nested_vmx_entry_ctls_low, | |
3114 | vmx->nested.nested_vmx_entry_ctls_high); | |
0115f9cb DM |
3115 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
3116 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3117 | break; |
3118 | case MSR_IA32_VMX_MISC: | |
b9c237bb WV |
3119 | *pdata = vmx_control_msr( |
3120 | vmx->nested.nested_vmx_misc_low, | |
3121 | vmx->nested.nested_vmx_misc_high); | |
b87a51ae | 3122 | break; |
b87a51ae | 3123 | case MSR_IA32_VMX_CR0_FIXED0: |
62cc6b9d | 3124 | *pdata = vmx->nested.nested_vmx_cr0_fixed0; |
b87a51ae NHE |
3125 | break; |
3126 | case MSR_IA32_VMX_CR0_FIXED1: | |
62cc6b9d | 3127 | *pdata = vmx->nested.nested_vmx_cr0_fixed1; |
b87a51ae NHE |
3128 | break; |
3129 | case MSR_IA32_VMX_CR4_FIXED0: | |
62cc6b9d | 3130 | *pdata = vmx->nested.nested_vmx_cr4_fixed0; |
b87a51ae NHE |
3131 | break; |
3132 | case MSR_IA32_VMX_CR4_FIXED1: | |
62cc6b9d | 3133 | *pdata = vmx->nested.nested_vmx_cr4_fixed1; |
b87a51ae NHE |
3134 | break; |
3135 | case MSR_IA32_VMX_VMCS_ENUM: | |
62cc6b9d | 3136 | *pdata = vmx->nested.nested_vmx_vmcs_enum; |
b87a51ae NHE |
3137 | break; |
3138 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
b9c237bb WV |
3139 | *pdata = vmx_control_msr( |
3140 | vmx->nested.nested_vmx_secondary_ctls_low, | |
3141 | vmx->nested.nested_vmx_secondary_ctls_high); | |
b87a51ae NHE |
3142 | break; |
3143 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
089d7b6e WL |
3144 | *pdata = vmx->nested.nested_vmx_ept_caps | |
3145 | ((u64)vmx->nested.nested_vmx_vpid_caps << 32); | |
b87a51ae NHE |
3146 | break; |
3147 | default: | |
b87a51ae | 3148 | return 1; |
b3897a49 NHE |
3149 | } |
3150 | ||
b87a51ae NHE |
3151 | return 0; |
3152 | } | |
3153 | ||
37e4c997 HZ |
3154 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
3155 | uint64_t val) | |
3156 | { | |
3157 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; | |
3158 | ||
3159 | return !(val & ~valid_bits); | |
3160 | } | |
3161 | ||
6aa8b732 AK |
3162 | /* |
3163 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
3164 | * Returns 0 on success, non-0 otherwise. | |
3165 | * Assumes vcpu_load() was already called. | |
3166 | */ | |
609e36d3 | 3167 | static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3168 | { |
26bb0981 | 3169 | struct shared_msr_entry *msr; |
6aa8b732 | 3170 | |
609e36d3 | 3171 | switch (msr_info->index) { |
05b3e0c2 | 3172 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3173 | case MSR_FS_BASE: |
609e36d3 | 3174 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
6aa8b732 AK |
3175 | break; |
3176 | case MSR_GS_BASE: | |
609e36d3 | 3177 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
6aa8b732 | 3178 | break; |
44ea2b17 AK |
3179 | case MSR_KERNEL_GS_BASE: |
3180 | vmx_load_host_state(to_vmx(vcpu)); | |
609e36d3 | 3181 | msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
44ea2b17 | 3182 | break; |
26bb0981 | 3183 | #endif |
6aa8b732 | 3184 | case MSR_EFER: |
609e36d3 | 3185 | return kvm_get_msr_common(vcpu, msr_info); |
af24a4e4 | 3186 | case MSR_IA32_TSC: |
be7b263e | 3187 | msr_info->data = guest_read_tsc(vcpu); |
6aa8b732 AK |
3188 | break; |
3189 | case MSR_IA32_SYSENTER_CS: | |
609e36d3 | 3190 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
6aa8b732 AK |
3191 | break; |
3192 | case MSR_IA32_SYSENTER_EIP: | |
609e36d3 | 3193 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
6aa8b732 AK |
3194 | break; |
3195 | case MSR_IA32_SYSENTER_ESP: | |
609e36d3 | 3196 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
6aa8b732 | 3197 | break; |
0dd376e7 | 3198 | case MSR_IA32_BNDCFGS: |
4439af9f | 3199 | if (!kvm_mpx_supported() || !guest_cpuid_has_mpx(vcpu)) |
93c4adc7 | 3200 | return 1; |
609e36d3 | 3201 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
0dd376e7 | 3202 | break; |
c45dcc71 AR |
3203 | case MSR_IA32_MCG_EXT_CTL: |
3204 | if (!msr_info->host_initiated && | |
3205 | !(to_vmx(vcpu)->msr_ia32_feature_control & | |
3206 | FEATURE_CONTROL_LMCE)) | |
cae50139 | 3207 | return 1; |
c45dcc71 AR |
3208 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
3209 | break; | |
cae50139 | 3210 | case MSR_IA32_FEATURE_CONTROL: |
3b84080b | 3211 | msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control; |
cae50139 JK |
3212 | break; |
3213 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
3214 | if (!nested_vmx_allowed(vcpu)) | |
3215 | return 1; | |
609e36d3 | 3216 | return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data); |
20300099 WL |
3217 | case MSR_IA32_XSS: |
3218 | if (!vmx_xsaves_supported()) | |
3219 | return 1; | |
609e36d3 | 3220 | msr_info->data = vcpu->arch.ia32_xss; |
20300099 | 3221 | break; |
4e47c7a6 | 3222 | case MSR_TSC_AUX: |
81b1b9ca | 3223 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
4e47c7a6 SY |
3224 | return 1; |
3225 | /* Otherwise falls through */ | |
6aa8b732 | 3226 | default: |
609e36d3 | 3227 | msr = find_msr_entry(to_vmx(vcpu), msr_info->index); |
3bab1f5d | 3228 | if (msr) { |
609e36d3 | 3229 | msr_info->data = msr->data; |
3bab1f5d | 3230 | break; |
6aa8b732 | 3231 | } |
609e36d3 | 3232 | return kvm_get_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3233 | } |
3234 | ||
6aa8b732 AK |
3235 | return 0; |
3236 | } | |
3237 | ||
cae50139 JK |
3238 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
3239 | ||
6aa8b732 AK |
3240 | /* |
3241 | * Writes msr value into into the appropriate "register". | |
3242 | * Returns 0 on success, non-0 otherwise. | |
3243 | * Assumes vcpu_load() was already called. | |
3244 | */ | |
8fe8ab46 | 3245 | static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3246 | { |
a2fa3e9f | 3247 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 3248 | struct shared_msr_entry *msr; |
2cc51560 | 3249 | int ret = 0; |
8fe8ab46 WA |
3250 | u32 msr_index = msr_info->index; |
3251 | u64 data = msr_info->data; | |
2cc51560 | 3252 | |
6aa8b732 | 3253 | switch (msr_index) { |
3bab1f5d | 3254 | case MSR_EFER: |
8fe8ab46 | 3255 | ret = kvm_set_msr_common(vcpu, msr_info); |
2cc51560 | 3256 | break; |
16175a79 | 3257 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3258 | case MSR_FS_BASE: |
2fb92db1 | 3259 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3260 | vmcs_writel(GUEST_FS_BASE, data); |
3261 | break; | |
3262 | case MSR_GS_BASE: | |
2fb92db1 | 3263 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3264 | vmcs_writel(GUEST_GS_BASE, data); |
3265 | break; | |
44ea2b17 AK |
3266 | case MSR_KERNEL_GS_BASE: |
3267 | vmx_load_host_state(vmx); | |
3268 | vmx->msr_guest_kernel_gs_base = data; | |
3269 | break; | |
6aa8b732 AK |
3270 | #endif |
3271 | case MSR_IA32_SYSENTER_CS: | |
3272 | vmcs_write32(GUEST_SYSENTER_CS, data); | |
3273 | break; | |
3274 | case MSR_IA32_SYSENTER_EIP: | |
f5b42c33 | 3275 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
6aa8b732 AK |
3276 | break; |
3277 | case MSR_IA32_SYSENTER_ESP: | |
f5b42c33 | 3278 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
6aa8b732 | 3279 | break; |
0dd376e7 | 3280 | case MSR_IA32_BNDCFGS: |
4439af9f | 3281 | if (!kvm_mpx_supported() || !guest_cpuid_has_mpx(vcpu)) |
93c4adc7 | 3282 | return 1; |
4531662d JM |
3283 | if (is_noncanonical_address(data & PAGE_MASK) || |
3284 | (data & MSR_IA32_BNDCFGS_RSVD)) | |
3285 | return 1; | |
0dd376e7 LJ |
3286 | vmcs_write64(GUEST_BNDCFGS, data); |
3287 | break; | |
af24a4e4 | 3288 | case MSR_IA32_TSC: |
8fe8ab46 | 3289 | kvm_write_tsc(vcpu, msr_info); |
6aa8b732 | 3290 | break; |
468d472f SY |
3291 | case MSR_IA32_CR_PAT: |
3292 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { | |
4566654b NA |
3293 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
3294 | return 1; | |
468d472f SY |
3295 | vmcs_write64(GUEST_IA32_PAT, data); |
3296 | vcpu->arch.pat = data; | |
3297 | break; | |
3298 | } | |
8fe8ab46 | 3299 | ret = kvm_set_msr_common(vcpu, msr_info); |
4e47c7a6 | 3300 | break; |
ba904635 WA |
3301 | case MSR_IA32_TSC_ADJUST: |
3302 | ret = kvm_set_msr_common(vcpu, msr_info); | |
4e47c7a6 | 3303 | break; |
c45dcc71 AR |
3304 | case MSR_IA32_MCG_EXT_CTL: |
3305 | if ((!msr_info->host_initiated && | |
3306 | !(to_vmx(vcpu)->msr_ia32_feature_control & | |
3307 | FEATURE_CONTROL_LMCE)) || | |
3308 | (data & ~MCG_EXT_CTL_LMCE_EN)) | |
3309 | return 1; | |
3310 | vcpu->arch.mcg_ext_ctl = data; | |
3311 | break; | |
cae50139 | 3312 | case MSR_IA32_FEATURE_CONTROL: |
37e4c997 | 3313 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
3b84080b | 3314 | (to_vmx(vcpu)->msr_ia32_feature_control & |
cae50139 JK |
3315 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
3316 | return 1; | |
3b84080b | 3317 | vmx->msr_ia32_feature_control = data; |
cae50139 JK |
3318 | if (msr_info->host_initiated && data == 0) |
3319 | vmx_leave_nested(vcpu); | |
3320 | break; | |
3321 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
62cc6b9d DM |
3322 | if (!msr_info->host_initiated) |
3323 | return 1; /* they are read-only */ | |
3324 | if (!nested_vmx_allowed(vcpu)) | |
3325 | return 1; | |
3326 | return vmx_set_vmx_msr(vcpu, msr_index, data); | |
20300099 WL |
3327 | case MSR_IA32_XSS: |
3328 | if (!vmx_xsaves_supported()) | |
3329 | return 1; | |
3330 | /* | |
3331 | * The only supported bit as of Skylake is bit 8, but | |
3332 | * it is not supported on KVM. | |
3333 | */ | |
3334 | if (data != 0) | |
3335 | return 1; | |
3336 | vcpu->arch.ia32_xss = data; | |
3337 | if (vcpu->arch.ia32_xss != host_xss) | |
3338 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, | |
3339 | vcpu->arch.ia32_xss, host_xss); | |
3340 | else | |
3341 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); | |
3342 | break; | |
4e47c7a6 | 3343 | case MSR_TSC_AUX: |
81b1b9ca | 3344 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
4e47c7a6 SY |
3345 | return 1; |
3346 | /* Check reserved bit, higher 32 bits should be zero */ | |
3347 | if ((data >> 32) != 0) | |
3348 | return 1; | |
3349 | /* Otherwise falls through */ | |
6aa8b732 | 3350 | default: |
8b9cf98c | 3351 | msr = find_msr_entry(vmx, msr_index); |
3bab1f5d | 3352 | if (msr) { |
8b3c3104 | 3353 | u64 old_msr_data = msr->data; |
3bab1f5d | 3354 | msr->data = data; |
2225fd56 AK |
3355 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
3356 | preempt_disable(); | |
8b3c3104 AH |
3357 | ret = kvm_set_shared_msr(msr->index, msr->data, |
3358 | msr->mask); | |
2225fd56 | 3359 | preempt_enable(); |
8b3c3104 AH |
3360 | if (ret) |
3361 | msr->data = old_msr_data; | |
2225fd56 | 3362 | } |
3bab1f5d | 3363 | break; |
6aa8b732 | 3364 | } |
8fe8ab46 | 3365 | ret = kvm_set_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3366 | } |
3367 | ||
2cc51560 | 3368 | return ret; |
6aa8b732 AK |
3369 | } |
3370 | ||
5fdbf976 | 3371 | static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
6aa8b732 | 3372 | { |
5fdbf976 MT |
3373 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
3374 | switch (reg) { | |
3375 | case VCPU_REGS_RSP: | |
3376 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); | |
3377 | break; | |
3378 | case VCPU_REGS_RIP: | |
3379 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); | |
3380 | break; | |
6de4f3ad AK |
3381 | case VCPU_EXREG_PDPTR: |
3382 | if (enable_ept) | |
3383 | ept_save_pdptrs(vcpu); | |
3384 | break; | |
5fdbf976 MT |
3385 | default: |
3386 | break; | |
3387 | } | |
6aa8b732 AK |
3388 | } |
3389 | ||
6aa8b732 AK |
3390 | static __init int cpu_has_kvm_support(void) |
3391 | { | |
6210e37b | 3392 | return cpu_has_vmx(); |
6aa8b732 AK |
3393 | } |
3394 | ||
3395 | static __init int vmx_disabled_by_bios(void) | |
3396 | { | |
3397 | u64 msr; | |
3398 | ||
3399 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); | |
cafd6659 | 3400 | if (msr & FEATURE_CONTROL_LOCKED) { |
23f3e991 | 3401 | /* launched w/ TXT and VMX disabled */ |
cafd6659 SW |
3402 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
3403 | && tboot_enabled()) | |
3404 | return 1; | |
23f3e991 | 3405 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
cafd6659 | 3406 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
23f3e991 | 3407 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
f9335afe SW |
3408 | && !tboot_enabled()) { |
3409 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " | |
23f3e991 | 3410 | "activate TXT before enabling KVM\n"); |
cafd6659 | 3411 | return 1; |
f9335afe | 3412 | } |
23f3e991 JC |
3413 | /* launched w/o TXT and VMX disabled */ |
3414 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) | |
3415 | && !tboot_enabled()) | |
3416 | return 1; | |
cafd6659 SW |
3417 | } |
3418 | ||
3419 | return 0; | |
6aa8b732 AK |
3420 | } |
3421 | ||
7725b894 DX |
3422 | static void kvm_cpu_vmxon(u64 addr) |
3423 | { | |
fe0e80be | 3424 | cr4_set_bits(X86_CR4_VMXE); |
1c5ac21a AS |
3425 | intel_pt_handle_vmx(1); |
3426 | ||
7725b894 DX |
3427 | asm volatile (ASM_VMX_VMXON_RAX |
3428 | : : "a"(&addr), "m"(addr) | |
3429 | : "memory", "cc"); | |
3430 | } | |
3431 | ||
13a34e06 | 3432 | static int hardware_enable(void) |
6aa8b732 AK |
3433 | { |
3434 | int cpu = raw_smp_processor_id(); | |
3435 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); | |
cafd6659 | 3436 | u64 old, test_bits; |
6aa8b732 | 3437 | |
1e02ce4c | 3438 | if (cr4_read_shadow() & X86_CR4_VMXE) |
10474ae8 AG |
3439 | return -EBUSY; |
3440 | ||
d462b819 | 3441 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
bf9f6ac8 FW |
3442 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
3443 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
8f536b76 ZY |
3444 | |
3445 | /* | |
3446 | * Now we can enable the vmclear operation in kdump | |
3447 | * since the loaded_vmcss_on_cpu list on this cpu | |
3448 | * has been initialized. | |
3449 | * | |
3450 | * Though the cpu is not in VMX operation now, there | |
3451 | * is no problem to enable the vmclear operation | |
3452 | * for the loaded_vmcss_on_cpu list is empty! | |
3453 | */ | |
3454 | crash_enable_local_vmclear(cpu); | |
3455 | ||
6aa8b732 | 3456 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
cafd6659 SW |
3457 | |
3458 | test_bits = FEATURE_CONTROL_LOCKED; | |
3459 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
3460 | if (tboot_enabled()) | |
3461 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; | |
3462 | ||
3463 | if ((old & test_bits) != test_bits) { | |
6aa8b732 | 3464 | /* enable and lock */ |
cafd6659 SW |
3465 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
3466 | } | |
fe0e80be DH |
3467 | kvm_cpu_vmxon(phys_addr); |
3468 | ept_sync_global(); | |
10474ae8 AG |
3469 | |
3470 | return 0; | |
6aa8b732 AK |
3471 | } |
3472 | ||
d462b819 | 3473 | static void vmclear_local_loaded_vmcss(void) |
543e4243 AK |
3474 | { |
3475 | int cpu = raw_smp_processor_id(); | |
d462b819 | 3476 | struct loaded_vmcs *v, *n; |
543e4243 | 3477 | |
d462b819 NHE |
3478 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
3479 | loaded_vmcss_on_cpu_link) | |
3480 | __loaded_vmcs_clear(v); | |
543e4243 AK |
3481 | } |
3482 | ||
710ff4a8 EH |
3483 | |
3484 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() | |
3485 | * tricks. | |
3486 | */ | |
3487 | static void kvm_cpu_vmxoff(void) | |
6aa8b732 | 3488 | { |
4ecac3fd | 3489 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
1c5ac21a AS |
3490 | |
3491 | intel_pt_handle_vmx(0); | |
fe0e80be | 3492 | cr4_clear_bits(X86_CR4_VMXE); |
6aa8b732 AK |
3493 | } |
3494 | ||
13a34e06 | 3495 | static void hardware_disable(void) |
710ff4a8 | 3496 | { |
fe0e80be DH |
3497 | vmclear_local_loaded_vmcss(); |
3498 | kvm_cpu_vmxoff(); | |
710ff4a8 EH |
3499 | } |
3500 | ||
1c3d14fe | 3501 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
d77c26fc | 3502 | u32 msr, u32 *result) |
1c3d14fe YS |
3503 | { |
3504 | u32 vmx_msr_low, vmx_msr_high; | |
3505 | u32 ctl = ctl_min | ctl_opt; | |
3506 | ||
3507 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3508 | ||
3509 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ | |
3510 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ | |
3511 | ||
3512 | /* Ensure minimum (required) set of control bits are supported. */ | |
3513 | if (ctl_min & ~ctl) | |
002c7f7c | 3514 | return -EIO; |
1c3d14fe YS |
3515 | |
3516 | *result = ctl; | |
3517 | return 0; | |
3518 | } | |
3519 | ||
110312c8 AK |
3520 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
3521 | { | |
3522 | u32 vmx_msr_low, vmx_msr_high; | |
3523 | ||
3524 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3525 | return vmx_msr_high & ctl; | |
3526 | } | |
3527 | ||
002c7f7c | 3528 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
6aa8b732 AK |
3529 | { |
3530 | u32 vmx_msr_low, vmx_msr_high; | |
d56f546d | 3531 | u32 min, opt, min2, opt2; |
1c3d14fe YS |
3532 | u32 _pin_based_exec_control = 0; |
3533 | u32 _cpu_based_exec_control = 0; | |
f78e0e2e | 3534 | u32 _cpu_based_2nd_exec_control = 0; |
1c3d14fe YS |
3535 | u32 _vmexit_control = 0; |
3536 | u32 _vmentry_control = 0; | |
3537 | ||
10166744 | 3538 | min = CPU_BASED_HLT_EXITING | |
1c3d14fe YS |
3539 | #ifdef CONFIG_X86_64 |
3540 | CPU_BASED_CR8_LOAD_EXITING | | |
3541 | CPU_BASED_CR8_STORE_EXITING | | |
3542 | #endif | |
d56f546d SY |
3543 | CPU_BASED_CR3_LOAD_EXITING | |
3544 | CPU_BASED_CR3_STORE_EXITING | | |
1c3d14fe YS |
3545 | CPU_BASED_USE_IO_BITMAPS | |
3546 | CPU_BASED_MOV_DR_EXITING | | |
a7052897 | 3547 | CPU_BASED_USE_TSC_OFFSETING | |
fee84b07 AK |
3548 | CPU_BASED_INVLPG_EXITING | |
3549 | CPU_BASED_RDPMC_EXITING; | |
443381a8 | 3550 | |
668fffa3 MT |
3551 | if (!kvm_mwait_in_guest()) |
3552 | min |= CPU_BASED_MWAIT_EXITING | | |
3553 | CPU_BASED_MONITOR_EXITING; | |
3554 | ||
f78e0e2e | 3555 | opt = CPU_BASED_TPR_SHADOW | |
25c5f225 | 3556 | CPU_BASED_USE_MSR_BITMAPS | |
f78e0e2e | 3557 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
1c3d14fe YS |
3558 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
3559 | &_cpu_based_exec_control) < 0) | |
002c7f7c | 3560 | return -EIO; |
6e5d865c YS |
3561 | #ifdef CONFIG_X86_64 |
3562 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
3563 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & | |
3564 | ~CPU_BASED_CR8_STORE_EXITING; | |
3565 | #endif | |
f78e0e2e | 3566 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
d56f546d SY |
3567 | min2 = 0; |
3568 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | | |
8d14695f | 3569 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
2384d2b3 | 3570 | SECONDARY_EXEC_WBINVD_EXITING | |
d56f546d | 3571 | SECONDARY_EXEC_ENABLE_VPID | |
3a624e29 | 3572 | SECONDARY_EXEC_ENABLE_EPT | |
4b8d54f9 | 3573 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
4e47c7a6 | 3574 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
ad756a16 | 3575 | SECONDARY_EXEC_RDTSCP | |
83d4c286 | 3576 | SECONDARY_EXEC_ENABLE_INVPCID | |
c7c9c56c | 3577 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
abc4fc58 | 3578 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
20300099 | 3579 | SECONDARY_EXEC_SHADOW_VMCS | |
843e4330 | 3580 | SECONDARY_EXEC_XSAVES | |
8b3e34e4 | 3581 | SECONDARY_EXEC_ENABLE_PML | |
64903d61 | 3582 | SECONDARY_EXEC_TSC_SCALING; |
d56f546d SY |
3583 | if (adjust_vmx_controls(min2, opt2, |
3584 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
f78e0e2e SY |
3585 | &_cpu_based_2nd_exec_control) < 0) |
3586 | return -EIO; | |
3587 | } | |
3588 | #ifndef CONFIG_X86_64 | |
3589 | if (!(_cpu_based_2nd_exec_control & | |
3590 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
3591 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; | |
3592 | #endif | |
83d4c286 YZ |
3593 | |
3594 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
3595 | _cpu_based_2nd_exec_control &= ~( | |
8d14695f | 3596 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
c7c9c56c YZ |
3597 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
3598 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
83d4c286 | 3599 | |
d56f546d | 3600 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
a7052897 MT |
3601 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
3602 | enabled */ | |
5fff7d27 GN |
3603 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
3604 | CPU_BASED_CR3_STORE_EXITING | | |
3605 | CPU_BASED_INVLPG_EXITING); | |
d56f546d SY |
3606 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
3607 | vmx_capability.ept, vmx_capability.vpid); | |
3608 | } | |
1c3d14fe | 3609 | |
91fa0f8e | 3610 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
1c3d14fe YS |
3611 | #ifdef CONFIG_X86_64 |
3612 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; | |
3613 | #endif | |
a547c6db | 3614 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
91fa0f8e | 3615 | VM_EXIT_CLEAR_BNDCFGS; |
1c3d14fe YS |
3616 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
3617 | &_vmexit_control) < 0) | |
002c7f7c | 3618 | return -EIO; |
1c3d14fe | 3619 | |
2c82878b PB |
3620 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING | |
3621 | PIN_BASED_VIRTUAL_NMIS; | |
3622 | opt = PIN_BASED_POSTED_INTR | PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
3623 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
3624 | &_pin_based_exec_control) < 0) | |
3625 | return -EIO; | |
3626 | ||
1c17c3e6 PB |
3627 | if (cpu_has_broken_vmx_preemption_timer()) |
3628 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be | 3629 | if (!(_cpu_based_2nd_exec_control & |
91fa0f8e | 3630 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
01e439be YZ |
3631 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
3632 | ||
c845f9c6 | 3633 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
da8999d3 | 3634 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
1c3d14fe YS |
3635 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
3636 | &_vmentry_control) < 0) | |
002c7f7c | 3637 | return -EIO; |
6aa8b732 | 3638 | |
c68876fd | 3639 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
1c3d14fe YS |
3640 | |
3641 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ | |
3642 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) | |
002c7f7c | 3643 | return -EIO; |
1c3d14fe YS |
3644 | |
3645 | #ifdef CONFIG_X86_64 | |
3646 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ | |
3647 | if (vmx_msr_high & (1u<<16)) | |
002c7f7c | 3648 | return -EIO; |
1c3d14fe YS |
3649 | #endif |
3650 | ||
3651 | /* Require Write-Back (WB) memory type for VMCS accesses. */ | |
3652 | if (((vmx_msr_high >> 18) & 15) != 6) | |
002c7f7c | 3653 | return -EIO; |
1c3d14fe | 3654 | |
002c7f7c | 3655 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
16cb0255 | 3656 | vmcs_conf->order = get_order(vmcs_conf->size); |
9ac7e3e8 | 3657 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
002c7f7c | 3658 | vmcs_conf->revision_id = vmx_msr_low; |
1c3d14fe | 3659 | |
002c7f7c YS |
3660 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
3661 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; | |
f78e0e2e | 3662 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
002c7f7c YS |
3663 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
3664 | vmcs_conf->vmentry_ctrl = _vmentry_control; | |
1c3d14fe | 3665 | |
110312c8 AK |
3666 | cpu_has_load_ia32_efer = |
3667 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
3668 | VM_ENTRY_LOAD_IA32_EFER) | |
3669 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
3670 | VM_EXIT_LOAD_IA32_EFER); | |
3671 | ||
8bf00a52 GN |
3672 | cpu_has_load_perf_global_ctrl = |
3673 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
3674 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
3675 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
3676 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
3677 | ||
3678 | /* | |
3679 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL | |
bb3541f1 | 3680 | * but due to errata below it can't be used. Workaround is to use |
8bf00a52 GN |
3681 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
3682 | * | |
3683 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] | |
3684 | * | |
3685 | * AAK155 (model 26) | |
3686 | * AAP115 (model 30) | |
3687 | * AAT100 (model 37) | |
3688 | * BC86,AAY89,BD102 (model 44) | |
3689 | * BA97 (model 46) | |
3690 | * | |
3691 | */ | |
3692 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { | |
3693 | switch (boot_cpu_data.x86_model) { | |
3694 | case 26: | |
3695 | case 30: | |
3696 | case 37: | |
3697 | case 44: | |
3698 | case 46: | |
3699 | cpu_has_load_perf_global_ctrl = false; | |
3700 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " | |
3701 | "does not work properly. Using workaround\n"); | |
3702 | break; | |
3703 | default: | |
3704 | break; | |
3705 | } | |
3706 | } | |
3707 | ||
782511b0 | 3708 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
20300099 WL |
3709 | rdmsrl(MSR_IA32_XSS, host_xss); |
3710 | ||
1c3d14fe | 3711 | return 0; |
c68876fd | 3712 | } |
6aa8b732 AK |
3713 | |
3714 | static struct vmcs *alloc_vmcs_cpu(int cpu) | |
3715 | { | |
3716 | int node = cpu_to_node(cpu); | |
3717 | struct page *pages; | |
3718 | struct vmcs *vmcs; | |
3719 | ||
96db800f | 3720 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
6aa8b732 AK |
3721 | if (!pages) |
3722 | return NULL; | |
3723 | vmcs = page_address(pages); | |
1c3d14fe YS |
3724 | memset(vmcs, 0, vmcs_config.size); |
3725 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ | |
6aa8b732 AK |
3726 | return vmcs; |
3727 | } | |
3728 | ||
3729 | static struct vmcs *alloc_vmcs(void) | |
3730 | { | |
d3b2c338 | 3731 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
6aa8b732 AK |
3732 | } |
3733 | ||
3734 | static void free_vmcs(struct vmcs *vmcs) | |
3735 | { | |
1c3d14fe | 3736 | free_pages((unsigned long)vmcs, vmcs_config.order); |
6aa8b732 AK |
3737 | } |
3738 | ||
d462b819 NHE |
3739 | /* |
3740 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded | |
3741 | */ | |
3742 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) | |
3743 | { | |
3744 | if (!loaded_vmcs->vmcs) | |
3745 | return; | |
3746 | loaded_vmcs_clear(loaded_vmcs); | |
3747 | free_vmcs(loaded_vmcs->vmcs); | |
3748 | loaded_vmcs->vmcs = NULL; | |
355f4fb1 | 3749 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
d462b819 NHE |
3750 | } |
3751 | ||
39959588 | 3752 | static void free_kvm_area(void) |
6aa8b732 AK |
3753 | { |
3754 | int cpu; | |
3755 | ||
3230bb47 | 3756 | for_each_possible_cpu(cpu) { |
6aa8b732 | 3757 | free_vmcs(per_cpu(vmxarea, cpu)); |
3230bb47 ZA |
3758 | per_cpu(vmxarea, cpu) = NULL; |
3759 | } | |
6aa8b732 AK |
3760 | } |
3761 | ||
fe2b201b BD |
3762 | static void init_vmcs_shadow_fields(void) |
3763 | { | |
3764 | int i, j; | |
3765 | ||
3766 | /* No checks for read only fields yet */ | |
3767 | ||
3768 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { | |
3769 | switch (shadow_read_write_fields[i]) { | |
3770 | case GUEST_BNDCFGS: | |
a87036ad | 3771 | if (!kvm_mpx_supported()) |
fe2b201b BD |
3772 | continue; |
3773 | break; | |
3774 | default: | |
3775 | break; | |
3776 | } | |
3777 | ||
3778 | if (j < i) | |
3779 | shadow_read_write_fields[j] = | |
3780 | shadow_read_write_fields[i]; | |
3781 | j++; | |
3782 | } | |
3783 | max_shadow_read_write_fields = j; | |
3784 | ||
3785 | /* shadowed fields guest access without vmexit */ | |
3786 | for (i = 0; i < max_shadow_read_write_fields; i++) { | |
3787 | clear_bit(shadow_read_write_fields[i], | |
3788 | vmx_vmwrite_bitmap); | |
3789 | clear_bit(shadow_read_write_fields[i], | |
3790 | vmx_vmread_bitmap); | |
3791 | } | |
3792 | for (i = 0; i < max_shadow_read_only_fields; i++) | |
3793 | clear_bit(shadow_read_only_fields[i], | |
3794 | vmx_vmread_bitmap); | |
3795 | } | |
3796 | ||
6aa8b732 AK |
3797 | static __init int alloc_kvm_area(void) |
3798 | { | |
3799 | int cpu; | |
3800 | ||
3230bb47 | 3801 | for_each_possible_cpu(cpu) { |
6aa8b732 AK |
3802 | struct vmcs *vmcs; |
3803 | ||
3804 | vmcs = alloc_vmcs_cpu(cpu); | |
3805 | if (!vmcs) { | |
3806 | free_kvm_area(); | |
3807 | return -ENOMEM; | |
3808 | } | |
3809 | ||
3810 | per_cpu(vmxarea, cpu) = vmcs; | |
3811 | } | |
3812 | return 0; | |
3813 | } | |
3814 | ||
14168786 GN |
3815 | static bool emulation_required(struct kvm_vcpu *vcpu) |
3816 | { | |
3817 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); | |
3818 | } | |
3819 | ||
91b0aa2c | 3820 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
d99e4152 | 3821 | struct kvm_segment *save) |
6aa8b732 | 3822 | { |
d99e4152 GN |
3823 | if (!emulate_invalid_guest_state) { |
3824 | /* | |
3825 | * CS and SS RPL should be equal during guest entry according | |
3826 | * to VMX spec, but in reality it is not always so. Since vcpu | |
3827 | * is in the middle of the transition from real mode to | |
3828 | * protected mode it is safe to assume that RPL 0 is a good | |
3829 | * default value. | |
3830 | */ | |
3831 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) | |
b32a9918 NA |
3832 | save->selector &= ~SEGMENT_RPL_MASK; |
3833 | save->dpl = save->selector & SEGMENT_RPL_MASK; | |
d99e4152 | 3834 | save->s = 1; |
6aa8b732 | 3835 | } |
d99e4152 | 3836 | vmx_set_segment(vcpu, save, seg); |
6aa8b732 AK |
3837 | } |
3838 | ||
3839 | static void enter_pmode(struct kvm_vcpu *vcpu) | |
3840 | { | |
3841 | unsigned long flags; | |
a89a8fb9 | 3842 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 | 3843 | |
d99e4152 GN |
3844 | /* |
3845 | * Update real mode segment cache. It may be not up-to-date if sement | |
3846 | * register was written while vcpu was in a guest mode. | |
3847 | */ | |
3848 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
3849 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
3850 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
3851 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
3852 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); | |
3853 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
3854 | ||
7ffd92c5 | 3855 | vmx->rmode.vm86_active = 0; |
6aa8b732 | 3856 | |
2fb92db1 AK |
3857 | vmx_segment_cache_clear(vmx); |
3858 | ||
f5f7b2fe | 3859 | vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
6aa8b732 AK |
3860 | |
3861 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 AK |
3862 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
3863 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
6aa8b732 AK |
3864 | vmcs_writel(GUEST_RFLAGS, flags); |
3865 | ||
66aee91a RR |
3866 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
3867 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); | |
6aa8b732 AK |
3868 | |
3869 | update_exception_bitmap(vcpu); | |
3870 | ||
91b0aa2c GN |
3871 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
3872 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); | |
3873 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
3874 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
3875 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
3876 | fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
6aa8b732 AK |
3877 | } |
3878 | ||
f5f7b2fe | 3879 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
6aa8b732 | 3880 | { |
772e0318 | 3881 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
d99e4152 GN |
3882 | struct kvm_segment var = *save; |
3883 | ||
3884 | var.dpl = 0x3; | |
3885 | if (seg == VCPU_SREG_CS) | |
3886 | var.type = 0x3; | |
3887 | ||
3888 | if (!emulate_invalid_guest_state) { | |
3889 | var.selector = var.base >> 4; | |
3890 | var.base = var.base & 0xffff0; | |
3891 | var.limit = 0xffff; | |
3892 | var.g = 0; | |
3893 | var.db = 0; | |
3894 | var.present = 1; | |
3895 | var.s = 1; | |
3896 | var.l = 0; | |
3897 | var.unusable = 0; | |
3898 | var.type = 0x3; | |
3899 | var.avl = 0; | |
3900 | if (save->base & 0xf) | |
3901 | printk_once(KERN_WARNING "kvm: segment base is not " | |
3902 | "paragraph aligned when entering " | |
3903 | "protected mode (seg=%d)", seg); | |
3904 | } | |
6aa8b732 | 3905 | |
d99e4152 | 3906 | vmcs_write16(sf->selector, var.selector); |
96794e4e | 3907 | vmcs_writel(sf->base, var.base); |
d99e4152 GN |
3908 | vmcs_write32(sf->limit, var.limit); |
3909 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); | |
6aa8b732 AK |
3910 | } |
3911 | ||
3912 | static void enter_rmode(struct kvm_vcpu *vcpu) | |
3913 | { | |
3914 | unsigned long flags; | |
a89a8fb9 | 3915 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 | 3916 | |
f5f7b2fe AK |
3917 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
3918 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
3919 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
3920 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
3921 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
c6ad1153 GN |
3922 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
3923 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
f5f7b2fe | 3924 | |
7ffd92c5 | 3925 | vmx->rmode.vm86_active = 1; |
6aa8b732 | 3926 | |
776e58ea GN |
3927 | /* |
3928 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering | |
4918c6ca | 3929 | * vcpu. Warn the user that an update is overdue. |
776e58ea | 3930 | */ |
4918c6ca | 3931 | if (!vcpu->kvm->arch.tss_addr) |
776e58ea GN |
3932 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
3933 | "called before entering vcpu\n"); | |
776e58ea | 3934 | |
2fb92db1 AK |
3935 | vmx_segment_cache_clear(vmx); |
3936 | ||
4918c6ca | 3937 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
6aa8b732 | 3938 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
6aa8b732 AK |
3939 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
3940 | ||
3941 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 | 3942 | vmx->rmode.save_rflags = flags; |
6aa8b732 | 3943 | |
053de044 | 3944 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
6aa8b732 AK |
3945 | |
3946 | vmcs_writel(GUEST_RFLAGS, flags); | |
66aee91a | 3947 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
6aa8b732 AK |
3948 | update_exception_bitmap(vcpu); |
3949 | ||
d99e4152 GN |
3950 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
3951 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); | |
3952 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
3953 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
3954 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
3955 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
b246dd5d | 3956 | |
8668a3c4 | 3957 | kvm_mmu_reset_context(vcpu); |
6aa8b732 AK |
3958 | } |
3959 | ||
401d10de AS |
3960 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
3961 | { | |
3962 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
26bb0981 AK |
3963 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
3964 | ||
3965 | if (!msr) | |
3966 | return; | |
401d10de | 3967 | |
44ea2b17 AK |
3968 | /* |
3969 | * Force kernel_gs_base reloading before EFER changes, as control | |
3970 | * of this msr depends on is_long_mode(). | |
3971 | */ | |
3972 | vmx_load_host_state(to_vmx(vcpu)); | |
f6801dff | 3973 | vcpu->arch.efer = efer; |
401d10de | 3974 | if (efer & EFER_LMA) { |
2961e876 | 3975 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
3976 | msr->data = efer; |
3977 | } else { | |
2961e876 | 3978 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
3979 | |
3980 | msr->data = efer & ~EFER_LME; | |
3981 | } | |
3982 | setup_msrs(vmx); | |
3983 | } | |
3984 | ||
05b3e0c2 | 3985 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
3986 | |
3987 | static void enter_lmode(struct kvm_vcpu *vcpu) | |
3988 | { | |
3989 | u32 guest_tr_ar; | |
3990 | ||
2fb92db1 AK |
3991 | vmx_segment_cache_clear(to_vmx(vcpu)); |
3992 | ||
6aa8b732 | 3993 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
4d283ec9 | 3994 | if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) { |
bd80158a JK |
3995 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
3996 | __func__); | |
6aa8b732 | 3997 | vmcs_write32(GUEST_TR_AR_BYTES, |
4d283ec9 AL |
3998 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
3999 | | VMX_AR_TYPE_BUSY_64_TSS); | |
6aa8b732 | 4000 | } |
da38f438 | 4001 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
6aa8b732 AK |
4002 | } |
4003 | ||
4004 | static void exit_lmode(struct kvm_vcpu *vcpu) | |
4005 | { | |
2961e876 | 4006 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
da38f438 | 4007 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
6aa8b732 AK |
4008 | } |
4009 | ||
4010 | #endif | |
4011 | ||
dd5f5341 | 4012 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid) |
2384d2b3 | 4013 | { |
dd180b3e XG |
4014 | if (enable_ept) { |
4015 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) | |
4016 | return; | |
995f00a6 | 4017 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
f0b98c02 JM |
4018 | } else { |
4019 | vpid_sync_context(vpid); | |
dd180b3e | 4020 | } |
2384d2b3 SY |
4021 | } |
4022 | ||
dd5f5341 WL |
4023 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
4024 | { | |
4025 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid); | |
4026 | } | |
4027 | ||
fb6c8198 JM |
4028 | static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu) |
4029 | { | |
4030 | if (enable_ept) | |
4031 | vmx_flush_tlb(vcpu); | |
4032 | } | |
4033 | ||
e8467fda AK |
4034 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
4035 | { | |
4036 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; | |
4037 | ||
4038 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; | |
4039 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; | |
4040 | } | |
4041 | ||
aff48baa AK |
4042 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
4043 | { | |
4044 | if (enable_ept && is_paging(vcpu)) | |
4045 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); | |
4046 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
4047 | } | |
4048 | ||
25c4c276 | 4049 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
399badf3 | 4050 | { |
fc78f519 AK |
4051 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
4052 | ||
4053 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; | |
4054 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; | |
399badf3 AK |
4055 | } |
4056 | ||
1439442c SY |
4057 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
4058 | { | |
d0d538b9 GN |
4059 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4060 | ||
6de4f3ad AK |
4061 | if (!test_bit(VCPU_EXREG_PDPTR, |
4062 | (unsigned long *)&vcpu->arch.regs_dirty)) | |
4063 | return; | |
4064 | ||
1439442c | 4065 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
d0d538b9 GN |
4066 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
4067 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); | |
4068 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); | |
4069 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); | |
1439442c SY |
4070 | } |
4071 | } | |
4072 | ||
8f5d549f AK |
4073 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
4074 | { | |
d0d538b9 GN |
4075 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4076 | ||
8f5d549f | 4077 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
d0d538b9 GN |
4078 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
4079 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); | |
4080 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); | |
4081 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); | |
8f5d549f | 4082 | } |
6de4f3ad AK |
4083 | |
4084 | __set_bit(VCPU_EXREG_PDPTR, | |
4085 | (unsigned long *)&vcpu->arch.regs_avail); | |
4086 | __set_bit(VCPU_EXREG_PDPTR, | |
4087 | (unsigned long *)&vcpu->arch.regs_dirty); | |
8f5d549f AK |
4088 | } |
4089 | ||
3899152c DM |
4090 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
4091 | { | |
4092 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; | |
4093 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; | |
4094 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
4095 | ||
4096 | if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high & | |
4097 | SECONDARY_EXEC_UNRESTRICTED_GUEST && | |
4098 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) | |
4099 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); | |
4100 | ||
4101 | return fixed_bits_valid(val, fixed0, fixed1); | |
4102 | } | |
4103 | ||
4104 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4105 | { | |
4106 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; | |
4107 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; | |
4108 | ||
4109 | return fixed_bits_valid(val, fixed0, fixed1); | |
4110 | } | |
4111 | ||
4112 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4113 | { | |
4114 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0; | |
4115 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1; | |
4116 | ||
4117 | return fixed_bits_valid(val, fixed0, fixed1); | |
4118 | } | |
4119 | ||
4120 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ | |
4121 | #define nested_guest_cr4_valid nested_cr4_valid | |
4122 | #define nested_host_cr4_valid nested_cr4_valid | |
4123 | ||
5e1746d6 | 4124 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
1439442c SY |
4125 | |
4126 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, | |
4127 | unsigned long cr0, | |
4128 | struct kvm_vcpu *vcpu) | |
4129 | { | |
5233dd51 MT |
4130 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
4131 | vmx_decache_cr3(vcpu); | |
1439442c SY |
4132 | if (!(cr0 & X86_CR0_PG)) { |
4133 | /* From paging/starting to nonpaging */ | |
4134 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4135 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
1439442c SY |
4136 | (CPU_BASED_CR3_LOAD_EXITING | |
4137 | CPU_BASED_CR3_STORE_EXITING)); | |
4138 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4139 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c SY |
4140 | } else if (!is_paging(vcpu)) { |
4141 | /* From nonpaging to paging */ | |
4142 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4143 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
1439442c SY |
4144 | ~(CPU_BASED_CR3_LOAD_EXITING | |
4145 | CPU_BASED_CR3_STORE_EXITING)); | |
4146 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4147 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c | 4148 | } |
95eb84a7 SY |
4149 | |
4150 | if (!(cr0 & X86_CR0_WP)) | |
4151 | *hw_cr0 &= ~X86_CR0_WP; | |
1439442c SY |
4152 | } |
4153 | ||
6aa8b732 AK |
4154 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
4155 | { | |
7ffd92c5 | 4156 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3a624e29 NK |
4157 | unsigned long hw_cr0; |
4158 | ||
5037878e | 4159 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
3a624e29 | 4160 | if (enable_unrestricted_guest) |
5037878e | 4161 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
218e763f | 4162 | else { |
5037878e | 4163 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
1439442c | 4164 | |
218e763f GN |
4165 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
4166 | enter_pmode(vcpu); | |
6aa8b732 | 4167 | |
218e763f GN |
4168 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
4169 | enter_rmode(vcpu); | |
4170 | } | |
6aa8b732 | 4171 | |
05b3e0c2 | 4172 | #ifdef CONFIG_X86_64 |
f6801dff | 4173 | if (vcpu->arch.efer & EFER_LME) { |
707d92fa | 4174 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
6aa8b732 | 4175 | enter_lmode(vcpu); |
707d92fa | 4176 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
6aa8b732 AK |
4177 | exit_lmode(vcpu); |
4178 | } | |
4179 | #endif | |
4180 | ||
089d034e | 4181 | if (enable_ept) |
1439442c SY |
4182 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
4183 | ||
6aa8b732 | 4184 | vmcs_writel(CR0_READ_SHADOW, cr0); |
1439442c | 4185 | vmcs_writel(GUEST_CR0, hw_cr0); |
ad312c7c | 4186 | vcpu->arch.cr0 = cr0; |
14168786 GN |
4187 | |
4188 | /* depends on vcpu->arch.cr0 to be set to a new value */ | |
4189 | vmx->emulation_required = emulation_required(vcpu); | |
6aa8b732 AK |
4190 | } |
4191 | ||
995f00a6 | 4192 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
1439442c SY |
4193 | { |
4194 | u64 eptp; | |
4195 | ||
4196 | /* TODO write the value reading from MSR */ | |
4197 | eptp = VMX_EPT_DEFAULT_MT | | |
4198 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; | |
995f00a6 PF |
4199 | if (enable_ept_ad_bits && |
4200 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) | |
b38f9934 | 4201 | eptp |= VMX_EPT_AD_ENABLE_BIT; |
1439442c SY |
4202 | eptp |= (root_hpa & PAGE_MASK); |
4203 | ||
4204 | return eptp; | |
4205 | } | |
4206 | ||
6aa8b732 AK |
4207 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
4208 | { | |
1439442c SY |
4209 | unsigned long guest_cr3; |
4210 | u64 eptp; | |
4211 | ||
4212 | guest_cr3 = cr3; | |
089d034e | 4213 | if (enable_ept) { |
995f00a6 | 4214 | eptp = construct_eptp(vcpu, cr3); |
1439442c | 4215 | vmcs_write64(EPT_POINTER, eptp); |
59ab5a8f JK |
4216 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
4217 | guest_cr3 = kvm_read_cr3(vcpu); | |
4218 | else | |
4219 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; | |
7c93be44 | 4220 | ept_load_pdptrs(vcpu); |
1439442c SY |
4221 | } |
4222 | ||
2384d2b3 | 4223 | vmx_flush_tlb(vcpu); |
1439442c | 4224 | vmcs_writel(GUEST_CR3, guest_cr3); |
6aa8b732 AK |
4225 | } |
4226 | ||
5e1746d6 | 4227 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
6aa8b732 | 4228 | { |
085e68ee BS |
4229 | /* |
4230 | * Pass through host's Machine Check Enable value to hw_cr4, which | |
4231 | * is in force while we are in guest mode. Do not let guests control | |
4232 | * this bit, even if host CR4.MCE == 0. | |
4233 | */ | |
4234 | unsigned long hw_cr4 = | |
4235 | (cr4_read_shadow() & X86_CR4_MCE) | | |
4236 | (cr4 & ~X86_CR4_MCE) | | |
4237 | (to_vmx(vcpu)->rmode.vm86_active ? | |
4238 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); | |
1439442c | 4239 | |
5e1746d6 NHE |
4240 | if (cr4 & X86_CR4_VMXE) { |
4241 | /* | |
4242 | * To use VMXON (and later other VMX instructions), a guest | |
4243 | * must first be able to turn on cr4.VMXE (see handle_vmon()). | |
4244 | * So basically the check on whether to allow nested VMX | |
4245 | * is here. | |
4246 | */ | |
4247 | if (!nested_vmx_allowed(vcpu)) | |
4248 | return 1; | |
1a0d74e6 | 4249 | } |
3899152c DM |
4250 | |
4251 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) | |
5e1746d6 NHE |
4252 | return 1; |
4253 | ||
ad312c7c | 4254 | vcpu->arch.cr4 = cr4; |
bc23008b AK |
4255 | if (enable_ept) { |
4256 | if (!is_paging(vcpu)) { | |
4257 | hw_cr4 &= ~X86_CR4_PAE; | |
4258 | hw_cr4 |= X86_CR4_PSE; | |
4259 | } else if (!(cr4 & X86_CR4_PAE)) { | |
4260 | hw_cr4 &= ~X86_CR4_PAE; | |
4261 | } | |
4262 | } | |
1439442c | 4263 | |
656ec4a4 RK |
4264 | if (!enable_unrestricted_guest && !is_paging(vcpu)) |
4265 | /* | |
ddba2628 HH |
4266 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
4267 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs | |
4268 | * to be manually disabled when guest switches to non-paging | |
4269 | * mode. | |
4270 | * | |
4271 | * If !enable_unrestricted_guest, the CPU is always running | |
4272 | * with CR0.PG=1 and CR4 needs to be modified. | |
4273 | * If enable_unrestricted_guest, the CPU automatically | |
4274 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. | |
656ec4a4 | 4275 | */ |
ddba2628 | 4276 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
656ec4a4 | 4277 | |
1439442c SY |
4278 | vmcs_writel(CR4_READ_SHADOW, cr4); |
4279 | vmcs_writel(GUEST_CR4, hw_cr4); | |
5e1746d6 | 4280 | return 0; |
6aa8b732 AK |
4281 | } |
4282 | ||
6aa8b732 AK |
4283 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
4284 | struct kvm_segment *var, int seg) | |
4285 | { | |
a9179499 | 4286 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 AK |
4287 | u32 ar; |
4288 | ||
c6ad1153 | 4289 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
f5f7b2fe | 4290 | *var = vmx->rmode.segs[seg]; |
a9179499 | 4291 | if (seg == VCPU_SREG_TR |
2fb92db1 | 4292 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
f5f7b2fe | 4293 | return; |
1390a28b AK |
4294 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4295 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4296 | return; | |
a9179499 | 4297 | } |
2fb92db1 AK |
4298 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4299 | var->limit = vmx_read_guest_seg_limit(vmx, seg); | |
4300 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4301 | ar = vmx_read_guest_seg_ar(vmx, seg); | |
03617c18 | 4302 | var->unusable = (ar >> 16) & 1; |
6aa8b732 AK |
4303 | var->type = ar & 15; |
4304 | var->s = (ar >> 4) & 1; | |
4305 | var->dpl = (ar >> 5) & 3; | |
03617c18 GN |
4306 | /* |
4307 | * Some userspaces do not preserve unusable property. Since usable | |
4308 | * segment has to be present according to VMX spec we can use present | |
4309 | * property to amend userspace bug by making unusable segment always | |
4310 | * nonpresent. vmx_segment_access_rights() already marks nonpresent | |
4311 | * segment as unusable. | |
4312 | */ | |
4313 | var->present = !var->unusable; | |
6aa8b732 AK |
4314 | var->avl = (ar >> 12) & 1; |
4315 | var->l = (ar >> 13) & 1; | |
4316 | var->db = (ar >> 14) & 1; | |
4317 | var->g = (ar >> 15) & 1; | |
6aa8b732 AK |
4318 | } |
4319 | ||
a9179499 AK |
4320 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
4321 | { | |
a9179499 AK |
4322 | struct kvm_segment s; |
4323 | ||
4324 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
4325 | vmx_get_segment(vcpu, &s, seg); | |
4326 | return s.base; | |
4327 | } | |
2fb92db1 | 4328 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
a9179499 AK |
4329 | } |
4330 | ||
b09408d0 | 4331 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
2e4d2653 | 4332 | { |
b09408d0 MT |
4333 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
4334 | ||
ae9fedc7 | 4335 | if (unlikely(vmx->rmode.vm86_active)) |
2e4d2653 | 4336 | return 0; |
ae9fedc7 PB |
4337 | else { |
4338 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); | |
4d283ec9 | 4339 | return VMX_AR_DPL(ar); |
69c73028 | 4340 | } |
69c73028 AK |
4341 | } |
4342 | ||
653e3108 | 4343 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
6aa8b732 | 4344 | { |
6aa8b732 AK |
4345 | u32 ar; |
4346 | ||
f0495f9b | 4347 | if (var->unusable || !var->present) |
6aa8b732 AK |
4348 | ar = 1 << 16; |
4349 | else { | |
4350 | ar = var->type & 15; | |
4351 | ar |= (var->s & 1) << 4; | |
4352 | ar |= (var->dpl & 3) << 5; | |
4353 | ar |= (var->present & 1) << 7; | |
4354 | ar |= (var->avl & 1) << 12; | |
4355 | ar |= (var->l & 1) << 13; | |
4356 | ar |= (var->db & 1) << 14; | |
4357 | ar |= (var->g & 1) << 15; | |
4358 | } | |
653e3108 AK |
4359 | |
4360 | return ar; | |
4361 | } | |
4362 | ||
4363 | static void vmx_set_segment(struct kvm_vcpu *vcpu, | |
4364 | struct kvm_segment *var, int seg) | |
4365 | { | |
7ffd92c5 | 4366 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
772e0318 | 4367 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
653e3108 | 4368 | |
2fb92db1 AK |
4369 | vmx_segment_cache_clear(vmx); |
4370 | ||
1ecd50a9 GN |
4371 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
4372 | vmx->rmode.segs[seg] = *var; | |
4373 | if (seg == VCPU_SREG_TR) | |
4374 | vmcs_write16(sf->selector, var->selector); | |
4375 | else if (var->s) | |
4376 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); | |
d99e4152 | 4377 | goto out; |
653e3108 | 4378 | } |
1ecd50a9 | 4379 | |
653e3108 AK |
4380 | vmcs_writel(sf->base, var->base); |
4381 | vmcs_write32(sf->limit, var->limit); | |
4382 | vmcs_write16(sf->selector, var->selector); | |
3a624e29 NK |
4383 | |
4384 | /* | |
4385 | * Fix the "Accessed" bit in AR field of segment registers for older | |
4386 | * qemu binaries. | |
4387 | * IA32 arch specifies that at the time of processor reset the | |
4388 | * "Accessed" bit in the AR field of segment registers is 1. And qemu | |
0fa06071 | 4389 | * is setting it to 0 in the userland code. This causes invalid guest |
3a624e29 NK |
4390 | * state vmexit when "unrestricted guest" mode is turned on. |
4391 | * Fix for this setup issue in cpu_reset is being pushed in the qemu | |
4392 | * tree. Newer qemu binaries with that qemu fix would not need this | |
4393 | * kvm hack. | |
4394 | */ | |
4395 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) | |
f924d66d | 4396 | var->type |= 0x1; /* Accessed */ |
3a624e29 | 4397 | |
f924d66d | 4398 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
d99e4152 GN |
4399 | |
4400 | out: | |
98eb2f8b | 4401 | vmx->emulation_required = emulation_required(vcpu); |
6aa8b732 AK |
4402 | } |
4403 | ||
6aa8b732 AK |
4404 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
4405 | { | |
2fb92db1 | 4406 | u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS); |
6aa8b732 AK |
4407 | |
4408 | *db = (ar >> 14) & 1; | |
4409 | *l = (ar >> 13) & 1; | |
4410 | } | |
4411 | ||
89a27f4d | 4412 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4413 | { |
89a27f4d GN |
4414 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
4415 | dt->address = vmcs_readl(GUEST_IDTR_BASE); | |
6aa8b732 AK |
4416 | } |
4417 | ||
89a27f4d | 4418 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4419 | { |
89a27f4d GN |
4420 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
4421 | vmcs_writel(GUEST_IDTR_BASE, dt->address); | |
6aa8b732 AK |
4422 | } |
4423 | ||
89a27f4d | 4424 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4425 | { |
89a27f4d GN |
4426 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
4427 | dt->address = vmcs_readl(GUEST_GDTR_BASE); | |
6aa8b732 AK |
4428 | } |
4429 | ||
89a27f4d | 4430 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4431 | { |
89a27f4d GN |
4432 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
4433 | vmcs_writel(GUEST_GDTR_BASE, dt->address); | |
6aa8b732 AK |
4434 | } |
4435 | ||
648dfaa7 MG |
4436 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
4437 | { | |
4438 | struct kvm_segment var; | |
4439 | u32 ar; | |
4440 | ||
4441 | vmx_get_segment(vcpu, &var, seg); | |
07f42f5f | 4442 | var.dpl = 0x3; |
0647f4aa GN |
4443 | if (seg == VCPU_SREG_CS) |
4444 | var.type = 0x3; | |
648dfaa7 MG |
4445 | ar = vmx_segment_access_rights(&var); |
4446 | ||
4447 | if (var.base != (var.selector << 4)) | |
4448 | return false; | |
89efbed0 | 4449 | if (var.limit != 0xffff) |
648dfaa7 | 4450 | return false; |
07f42f5f | 4451 | if (ar != 0xf3) |
648dfaa7 MG |
4452 | return false; |
4453 | ||
4454 | return true; | |
4455 | } | |
4456 | ||
4457 | static bool code_segment_valid(struct kvm_vcpu *vcpu) | |
4458 | { | |
4459 | struct kvm_segment cs; | |
4460 | unsigned int cs_rpl; | |
4461 | ||
4462 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
b32a9918 | 4463 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4464 | |
1872a3f4 AK |
4465 | if (cs.unusable) |
4466 | return false; | |
4d283ec9 | 4467 | if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK)) |
648dfaa7 MG |
4468 | return false; |
4469 | if (!cs.s) | |
4470 | return false; | |
4d283ec9 | 4471 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
648dfaa7 MG |
4472 | if (cs.dpl > cs_rpl) |
4473 | return false; | |
1872a3f4 | 4474 | } else { |
648dfaa7 MG |
4475 | if (cs.dpl != cs_rpl) |
4476 | return false; | |
4477 | } | |
4478 | if (!cs.present) | |
4479 | return false; | |
4480 | ||
4481 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ | |
4482 | return true; | |
4483 | } | |
4484 | ||
4485 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) | |
4486 | { | |
4487 | struct kvm_segment ss; | |
4488 | unsigned int ss_rpl; | |
4489 | ||
4490 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
b32a9918 | 4491 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4492 | |
1872a3f4 AK |
4493 | if (ss.unusable) |
4494 | return true; | |
4495 | if (ss.type != 3 && ss.type != 7) | |
648dfaa7 MG |
4496 | return false; |
4497 | if (!ss.s) | |
4498 | return false; | |
4499 | if (ss.dpl != ss_rpl) /* DPL != RPL */ | |
4500 | return false; | |
4501 | if (!ss.present) | |
4502 | return false; | |
4503 | ||
4504 | return true; | |
4505 | } | |
4506 | ||
4507 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) | |
4508 | { | |
4509 | struct kvm_segment var; | |
4510 | unsigned int rpl; | |
4511 | ||
4512 | vmx_get_segment(vcpu, &var, seg); | |
b32a9918 | 4513 | rpl = var.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4514 | |
1872a3f4 AK |
4515 | if (var.unusable) |
4516 | return true; | |
648dfaa7 MG |
4517 | if (!var.s) |
4518 | return false; | |
4519 | if (!var.present) | |
4520 | return false; | |
4d283ec9 | 4521 | if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) { |
648dfaa7 MG |
4522 | if (var.dpl < rpl) /* DPL < RPL */ |
4523 | return false; | |
4524 | } | |
4525 | ||
4526 | /* TODO: Add other members to kvm_segment_field to allow checking for other access | |
4527 | * rights flags | |
4528 | */ | |
4529 | return true; | |
4530 | } | |
4531 | ||
4532 | static bool tr_valid(struct kvm_vcpu *vcpu) | |
4533 | { | |
4534 | struct kvm_segment tr; | |
4535 | ||
4536 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); | |
4537 | ||
1872a3f4 AK |
4538 | if (tr.unusable) |
4539 | return false; | |
b32a9918 | 4540 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 | 4541 | return false; |
1872a3f4 | 4542 | if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ |
648dfaa7 MG |
4543 | return false; |
4544 | if (!tr.present) | |
4545 | return false; | |
4546 | ||
4547 | return true; | |
4548 | } | |
4549 | ||
4550 | static bool ldtr_valid(struct kvm_vcpu *vcpu) | |
4551 | { | |
4552 | struct kvm_segment ldtr; | |
4553 | ||
4554 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); | |
4555 | ||
1872a3f4 AK |
4556 | if (ldtr.unusable) |
4557 | return true; | |
b32a9918 | 4558 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 MG |
4559 | return false; |
4560 | if (ldtr.type != 2) | |
4561 | return false; | |
4562 | if (!ldtr.present) | |
4563 | return false; | |
4564 | ||
4565 | return true; | |
4566 | } | |
4567 | ||
4568 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) | |
4569 | { | |
4570 | struct kvm_segment cs, ss; | |
4571 | ||
4572 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
4573 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
4574 | ||
b32a9918 NA |
4575 | return ((cs.selector & SEGMENT_RPL_MASK) == |
4576 | (ss.selector & SEGMENT_RPL_MASK)); | |
648dfaa7 MG |
4577 | } |
4578 | ||
4579 | /* | |
4580 | * Check if guest state is valid. Returns true if valid, false if | |
4581 | * not. | |
4582 | * We assume that registers are always usable | |
4583 | */ | |
4584 | static bool guest_state_valid(struct kvm_vcpu *vcpu) | |
4585 | { | |
c5e97c80 GN |
4586 | if (enable_unrestricted_guest) |
4587 | return true; | |
4588 | ||
648dfaa7 | 4589 | /* real mode guest state checks */ |
f13882d8 | 4590 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
648dfaa7 MG |
4591 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
4592 | return false; | |
4593 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) | |
4594 | return false; | |
4595 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) | |
4596 | return false; | |
4597 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) | |
4598 | return false; | |
4599 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) | |
4600 | return false; | |
4601 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) | |
4602 | return false; | |
4603 | } else { | |
4604 | /* protected mode guest state checks */ | |
4605 | if (!cs_ss_rpl_check(vcpu)) | |
4606 | return false; | |
4607 | if (!code_segment_valid(vcpu)) | |
4608 | return false; | |
4609 | if (!stack_segment_valid(vcpu)) | |
4610 | return false; | |
4611 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) | |
4612 | return false; | |
4613 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) | |
4614 | return false; | |
4615 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) | |
4616 | return false; | |
4617 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) | |
4618 | return false; | |
4619 | if (!tr_valid(vcpu)) | |
4620 | return false; | |
4621 | if (!ldtr_valid(vcpu)) | |
4622 | return false; | |
4623 | } | |
4624 | /* TODO: | |
4625 | * - Add checks on RIP | |
4626 | * - Add checks on RFLAGS | |
4627 | */ | |
4628 | ||
4629 | return true; | |
4630 | } | |
4631 | ||
d77c26fc | 4632 | static int init_rmode_tss(struct kvm *kvm) |
6aa8b732 | 4633 | { |
40dcaa9f | 4634 | gfn_t fn; |
195aefde | 4635 | u16 data = 0; |
1f755a82 | 4636 | int idx, r; |
6aa8b732 | 4637 | |
40dcaa9f | 4638 | idx = srcu_read_lock(&kvm->srcu); |
4918c6ca | 4639 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
195aefde IE |
4640 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
4641 | if (r < 0) | |
10589a46 | 4642 | goto out; |
195aefde | 4643 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
464d17c8 SY |
4644 | r = kvm_write_guest_page(kvm, fn++, &data, |
4645 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); | |
195aefde | 4646 | if (r < 0) |
10589a46 | 4647 | goto out; |
195aefde IE |
4648 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
4649 | if (r < 0) | |
10589a46 | 4650 | goto out; |
195aefde IE |
4651 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
4652 | if (r < 0) | |
10589a46 | 4653 | goto out; |
195aefde | 4654 | data = ~0; |
10589a46 MT |
4655 | r = kvm_write_guest_page(kvm, fn, &data, |
4656 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, | |
4657 | sizeof(u8)); | |
10589a46 | 4658 | out: |
40dcaa9f | 4659 | srcu_read_unlock(&kvm->srcu, idx); |
1f755a82 | 4660 | return r; |
6aa8b732 AK |
4661 | } |
4662 | ||
b7ebfb05 SY |
4663 | static int init_rmode_identity_map(struct kvm *kvm) |
4664 | { | |
f51770ed | 4665 | int i, idx, r = 0; |
ba049e93 | 4666 | kvm_pfn_t identity_map_pfn; |
b7ebfb05 SY |
4667 | u32 tmp; |
4668 | ||
089d034e | 4669 | if (!enable_ept) |
f51770ed | 4670 | return 0; |
a255d479 TC |
4671 | |
4672 | /* Protect kvm->arch.ept_identity_pagetable_done. */ | |
4673 | mutex_lock(&kvm->slots_lock); | |
4674 | ||
f51770ed | 4675 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
a255d479 | 4676 | goto out2; |
a255d479 | 4677 | |
b927a3ce | 4678 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
a255d479 TC |
4679 | |
4680 | r = alloc_identity_pagetable(kvm); | |
f51770ed | 4681 | if (r < 0) |
a255d479 TC |
4682 | goto out2; |
4683 | ||
40dcaa9f | 4684 | idx = srcu_read_lock(&kvm->srcu); |
b7ebfb05 SY |
4685 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
4686 | if (r < 0) | |
4687 | goto out; | |
4688 | /* Set up identity-mapping pagetable for EPT in real mode */ | |
4689 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { | |
4690 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | | |
4691 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); | |
4692 | r = kvm_write_guest_page(kvm, identity_map_pfn, | |
4693 | &tmp, i * sizeof(tmp), sizeof(tmp)); | |
4694 | if (r < 0) | |
4695 | goto out; | |
4696 | } | |
4697 | kvm->arch.ept_identity_pagetable_done = true; | |
f51770ed | 4698 | |
b7ebfb05 | 4699 | out: |
40dcaa9f | 4700 | srcu_read_unlock(&kvm->srcu, idx); |
a255d479 TC |
4701 | |
4702 | out2: | |
4703 | mutex_unlock(&kvm->slots_lock); | |
f51770ed | 4704 | return r; |
b7ebfb05 SY |
4705 | } |
4706 | ||
6aa8b732 AK |
4707 | static void seg_setup(int seg) |
4708 | { | |
772e0318 | 4709 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
3a624e29 | 4710 | unsigned int ar; |
6aa8b732 AK |
4711 | |
4712 | vmcs_write16(sf->selector, 0); | |
4713 | vmcs_writel(sf->base, 0); | |
4714 | vmcs_write32(sf->limit, 0xffff); | |
d54d07b2 GN |
4715 | ar = 0x93; |
4716 | if (seg == VCPU_SREG_CS) | |
4717 | ar |= 0x08; /* code segment */ | |
3a624e29 NK |
4718 | |
4719 | vmcs_write32(sf->ar_bytes, ar); | |
6aa8b732 AK |
4720 | } |
4721 | ||
f78e0e2e SY |
4722 | static int alloc_apic_access_page(struct kvm *kvm) |
4723 | { | |
4484141a | 4724 | struct page *page; |
f78e0e2e SY |
4725 | int r = 0; |
4726 | ||
79fac95e | 4727 | mutex_lock(&kvm->slots_lock); |
c24ae0dc | 4728 | if (kvm->arch.apic_access_page_done) |
f78e0e2e | 4729 | goto out; |
1d8007bd PB |
4730 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
4731 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); | |
f78e0e2e SY |
4732 | if (r) |
4733 | goto out; | |
72dc67a6 | 4734 | |
73a6d941 | 4735 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
4484141a XG |
4736 | if (is_error_page(page)) { |
4737 | r = -EFAULT; | |
4738 | goto out; | |
4739 | } | |
4740 | ||
c24ae0dc TC |
4741 | /* |
4742 | * Do not pin the page in memory, so that memory hot-unplug | |
4743 | * is able to migrate it. | |
4744 | */ | |
4745 | put_page(page); | |
4746 | kvm->arch.apic_access_page_done = true; | |
f78e0e2e | 4747 | out: |
79fac95e | 4748 | mutex_unlock(&kvm->slots_lock); |
f78e0e2e SY |
4749 | return r; |
4750 | } | |
4751 | ||
b7ebfb05 SY |
4752 | static int alloc_identity_pagetable(struct kvm *kvm) |
4753 | { | |
a255d479 TC |
4754 | /* Called with kvm->slots_lock held. */ |
4755 | ||
b7ebfb05 SY |
4756 | int r = 0; |
4757 | ||
a255d479 TC |
4758 | BUG_ON(kvm->arch.ept_identity_pagetable_done); |
4759 | ||
1d8007bd PB |
4760 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
4761 | kvm->arch.ept_identity_map_addr, PAGE_SIZE); | |
b7ebfb05 | 4762 | |
b7ebfb05 SY |
4763 | return r; |
4764 | } | |
4765 | ||
991e7a0e | 4766 | static int allocate_vpid(void) |
2384d2b3 SY |
4767 | { |
4768 | int vpid; | |
4769 | ||
919818ab | 4770 | if (!enable_vpid) |
991e7a0e | 4771 | return 0; |
2384d2b3 SY |
4772 | spin_lock(&vmx_vpid_lock); |
4773 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); | |
991e7a0e | 4774 | if (vpid < VMX_NR_VPIDS) |
2384d2b3 | 4775 | __set_bit(vpid, vmx_vpid_bitmap); |
991e7a0e WL |
4776 | else |
4777 | vpid = 0; | |
2384d2b3 | 4778 | spin_unlock(&vmx_vpid_lock); |
991e7a0e | 4779 | return vpid; |
2384d2b3 SY |
4780 | } |
4781 | ||
991e7a0e | 4782 | static void free_vpid(int vpid) |
cdbecfc3 | 4783 | { |
991e7a0e | 4784 | if (!enable_vpid || vpid == 0) |
cdbecfc3 LJ |
4785 | return; |
4786 | spin_lock(&vmx_vpid_lock); | |
991e7a0e | 4787 | __clear_bit(vpid, vmx_vpid_bitmap); |
cdbecfc3 LJ |
4788 | spin_unlock(&vmx_vpid_lock); |
4789 | } | |
4790 | ||
8d14695f YZ |
4791 | #define MSR_TYPE_R 1 |
4792 | #define MSR_TYPE_W 2 | |
4793 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, | |
4794 | u32 msr, int type) | |
25c5f225 | 4795 | { |
3e7c73e9 | 4796 | int f = sizeof(unsigned long); |
25c5f225 SY |
4797 | |
4798 | if (!cpu_has_vmx_msr_bitmap()) | |
4799 | return; | |
4800 | ||
4801 | /* | |
4802 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
4803 | * have the write-low and read-high bitmap offsets the wrong way round. | |
4804 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
4805 | */ | |
25c5f225 | 4806 | if (msr <= 0x1fff) { |
8d14695f YZ |
4807 | if (type & MSR_TYPE_R) |
4808 | /* read-low */ | |
4809 | __clear_bit(msr, msr_bitmap + 0x000 / f); | |
4810 | ||
4811 | if (type & MSR_TYPE_W) | |
4812 | /* write-low */ | |
4813 | __clear_bit(msr, msr_bitmap + 0x800 / f); | |
4814 | ||
25c5f225 SY |
4815 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
4816 | msr &= 0x1fff; | |
8d14695f YZ |
4817 | if (type & MSR_TYPE_R) |
4818 | /* read-high */ | |
4819 | __clear_bit(msr, msr_bitmap + 0x400 / f); | |
4820 | ||
4821 | if (type & MSR_TYPE_W) | |
4822 | /* write-high */ | |
4823 | __clear_bit(msr, msr_bitmap + 0xc00 / f); | |
4824 | ||
4825 | } | |
4826 | } | |
4827 | ||
f2b93280 WV |
4828 | /* |
4829 | * If a msr is allowed by L0, we should check whether it is allowed by L1. | |
4830 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. | |
4831 | */ | |
4832 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, | |
4833 | unsigned long *msr_bitmap_nested, | |
4834 | u32 msr, int type) | |
4835 | { | |
4836 | int f = sizeof(unsigned long); | |
4837 | ||
4838 | if (!cpu_has_vmx_msr_bitmap()) { | |
4839 | WARN_ON(1); | |
4840 | return; | |
4841 | } | |
4842 | ||
4843 | /* | |
4844 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
4845 | * have the write-low and read-high bitmap offsets the wrong way round. | |
4846 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
4847 | */ | |
4848 | if (msr <= 0x1fff) { | |
4849 | if (type & MSR_TYPE_R && | |
4850 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) | |
4851 | /* read-low */ | |
4852 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); | |
4853 | ||
4854 | if (type & MSR_TYPE_W && | |
4855 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) | |
4856 | /* write-low */ | |
4857 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); | |
4858 | ||
4859 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
4860 | msr &= 0x1fff; | |
4861 | if (type & MSR_TYPE_R && | |
4862 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) | |
4863 | /* read-high */ | |
4864 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); | |
4865 | ||
4866 | if (type & MSR_TYPE_W && | |
4867 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) | |
4868 | /* write-high */ | |
4869 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); | |
4870 | ||
4871 | } | |
4872 | } | |
4873 | ||
5897297b AK |
4874 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
4875 | { | |
4876 | if (!longmode_only) | |
8d14695f YZ |
4877 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, |
4878 | msr, MSR_TYPE_R | MSR_TYPE_W); | |
4879 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, | |
4880 | msr, MSR_TYPE_R | MSR_TYPE_W); | |
4881 | } | |
4882 | ||
2e69f865 | 4883 | static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active) |
8d14695f | 4884 | { |
f6e90f9e | 4885 | if (apicv_active) { |
c63e4563 | 4886 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv, |
2e69f865 | 4887 | msr, type); |
c63e4563 | 4888 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv, |
2e69f865 | 4889 | msr, type); |
f6e90f9e | 4890 | } else { |
f6e90f9e | 4891 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
2e69f865 | 4892 | msr, type); |
f6e90f9e | 4893 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
2e69f865 | 4894 | msr, type); |
f6e90f9e | 4895 | } |
5897297b AK |
4896 | } |
4897 | ||
d62caabb | 4898 | static bool vmx_get_enable_apicv(void) |
d50ab6c1 | 4899 | { |
d62caabb | 4900 | return enable_apicv; |
d50ab6c1 PB |
4901 | } |
4902 | ||
6342c50a | 4903 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
705699a1 WV |
4904 | { |
4905 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
4906 | int max_irr; | |
4907 | void *vapic_page; | |
4908 | u16 status; | |
4909 | ||
4910 | if (vmx->nested.pi_desc && | |
4911 | vmx->nested.pi_pending) { | |
4912 | vmx->nested.pi_pending = false; | |
4913 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) | |
6342c50a | 4914 | return; |
705699a1 WV |
4915 | |
4916 | max_irr = find_last_bit( | |
4917 | (unsigned long *)vmx->nested.pi_desc->pir, 256); | |
4918 | ||
4919 | if (max_irr == 256) | |
6342c50a | 4920 | return; |
705699a1 WV |
4921 | |
4922 | vapic_page = kmap(vmx->nested.virtual_apic_page); | |
705699a1 WV |
4923 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page); |
4924 | kunmap(vmx->nested.virtual_apic_page); | |
4925 | ||
4926 | status = vmcs_read16(GUEST_INTR_STATUS); | |
4927 | if ((u8)max_irr > ((u8)status & 0xff)) { | |
4928 | status &= ~0xff; | |
4929 | status |= (u8)max_irr; | |
4930 | vmcs_write16(GUEST_INTR_STATUS, status); | |
4931 | } | |
4932 | } | |
705699a1 WV |
4933 | } |
4934 | ||
21bc8dc5 RK |
4935 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu) |
4936 | { | |
4937 | #ifdef CONFIG_SMP | |
4938 | if (vcpu->mode == IN_GUEST_MODE) { | |
28b835d6 FW |
4939 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
4940 | ||
4941 | /* | |
4942 | * Currently, we don't support urgent interrupt, | |
4943 | * all interrupts are recognized as non-urgent | |
4944 | * interrupt, so we cannot post interrupts when | |
4945 | * 'SN' is set. | |
4946 | * | |
4947 | * If the vcpu is in guest mode, it means it is | |
4948 | * running instead of being scheduled out and | |
4949 | * waiting in the run queue, and that's the only | |
4950 | * case when 'SN' is set currently, warning if | |
4951 | * 'SN' is set. | |
4952 | */ | |
4953 | WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc)); | |
4954 | ||
21bc8dc5 RK |
4955 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), |
4956 | POSTED_INTR_VECTOR); | |
4957 | return true; | |
4958 | } | |
4959 | #endif | |
4960 | return false; | |
4961 | } | |
4962 | ||
705699a1 WV |
4963 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
4964 | int vector) | |
4965 | { | |
4966 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
4967 | ||
4968 | if (is_guest_mode(vcpu) && | |
4969 | vector == vmx->nested.posted_intr_nv) { | |
4970 | /* the PIR and ON have been set by L1. */ | |
21bc8dc5 | 4971 | kvm_vcpu_trigger_posted_interrupt(vcpu); |
705699a1 WV |
4972 | /* |
4973 | * If a posted intr is not recognized by hardware, | |
4974 | * we will accomplish it in the next vmentry. | |
4975 | */ | |
4976 | vmx->nested.pi_pending = true; | |
4977 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
4978 | return 0; | |
4979 | } | |
4980 | return -1; | |
4981 | } | |
a20ed54d YZ |
4982 | /* |
4983 | * Send interrupt to vcpu via posted interrupt way. | |
4984 | * 1. If target vcpu is running(non-root mode), send posted interrupt | |
4985 | * notification to vcpu and hardware will sync PIR to vIRR atomically. | |
4986 | * 2. If target vcpu isn't running(root mode), kick it to pick up the | |
4987 | * interrupt from PIR in next vmentry. | |
4988 | */ | |
4989 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) | |
4990 | { | |
4991 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
4992 | int r; | |
4993 | ||
705699a1 WV |
4994 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
4995 | if (!r) | |
4996 | return; | |
4997 | ||
a20ed54d YZ |
4998 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
4999 | return; | |
5000 | ||
b95234c8 PB |
5001 | /* If a previous notification has sent the IPI, nothing to do. */ |
5002 | if (pi_test_and_set_on(&vmx->pi_desc)) | |
5003 | return; | |
5004 | ||
5005 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu)) | |
a20ed54d YZ |
5006 | kvm_vcpu_kick(vcpu); |
5007 | } | |
5008 | ||
a3a8ff8e NHE |
5009 | /* |
5010 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that | |
5011 | * will not change in the lifetime of the guest. | |
5012 | * Note that host-state that does change is set elsewhere. E.g., host-state | |
5013 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. | |
5014 | */ | |
a547c6db | 5015 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
a3a8ff8e NHE |
5016 | { |
5017 | u32 low32, high32; | |
5018 | unsigned long tmpl; | |
5019 | struct desc_ptr dt; | |
04ac88ab | 5020 | unsigned long cr0, cr4; |
a3a8ff8e | 5021 | |
04ac88ab AL |
5022 | cr0 = read_cr0(); |
5023 | WARN_ON(cr0 & X86_CR0_TS); | |
5024 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ | |
a3a8ff8e NHE |
5025 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
5026 | ||
d974baa3 | 5027 | /* Save the most likely value for this task's CR4 in the VMCS. */ |
1e02ce4c | 5028 | cr4 = cr4_read_shadow(); |
d974baa3 AL |
5029 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
5030 | vmx->host_state.vmcs_host_cr4 = cr4; | |
5031 | ||
a3a8ff8e | 5032 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
b2da15ac AK |
5033 | #ifdef CONFIG_X86_64 |
5034 | /* | |
5035 | * Load null selectors, so we can avoid reloading them in | |
5036 | * __vmx_load_host_state(), in case userspace uses the null selectors | |
5037 | * too (the expected case). | |
5038 | */ | |
5039 | vmcs_write16(HOST_DS_SELECTOR, 0); | |
5040 | vmcs_write16(HOST_ES_SELECTOR, 0); | |
5041 | #else | |
a3a8ff8e NHE |
5042 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5043 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ | |
b2da15ac | 5044 | #endif |
a3a8ff8e NHE |
5045 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5046 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ | |
5047 | ||
5048 | native_store_idt(&dt); | |
5049 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ | |
a547c6db | 5050 | vmx->host_idt_base = dt.address; |
a3a8ff8e | 5051 | |
83287ea4 | 5052 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
a3a8ff8e NHE |
5053 | |
5054 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); | |
5055 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); | |
5056 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); | |
5057 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ | |
5058 | ||
5059 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { | |
5060 | rdmsr(MSR_IA32_CR_PAT, low32, high32); | |
5061 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); | |
5062 | } | |
5063 | } | |
5064 | ||
bf8179a0 NHE |
5065 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
5066 | { | |
5067 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; | |
5068 | if (enable_ept) | |
5069 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; | |
fe3ef05c NHE |
5070 | if (is_guest_mode(&vmx->vcpu)) |
5071 | vmx->vcpu.arch.cr4_guest_owned_bits &= | |
5072 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; | |
bf8179a0 NHE |
5073 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
5074 | } | |
5075 | ||
01e439be YZ |
5076 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
5077 | { | |
5078 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; | |
5079 | ||
d62caabb | 5080 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
01e439be | 5081 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
64672c95 YJ |
5082 | /* Enable the preemption timer dynamically */ |
5083 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
5084 | return pin_based_exec_ctrl; |
5085 | } | |
5086 | ||
d62caabb AS |
5087 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
5088 | { | |
5089 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5090 | ||
5091 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); | |
3ce424e4 RK |
5092 | if (cpu_has_secondary_exec_ctrls()) { |
5093 | if (kvm_vcpu_apicv_active(vcpu)) | |
5094 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
5095 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5096 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5097 | else | |
5098 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
5099 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5100 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5101 | } | |
5102 | ||
5103 | if (cpu_has_vmx_msr_bitmap()) | |
5104 | vmx_set_msr_bitmap(vcpu); | |
d62caabb AS |
5105 | } |
5106 | ||
bf8179a0 NHE |
5107 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
5108 | { | |
5109 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; | |
d16c293e PB |
5110 | |
5111 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) | |
5112 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; | |
5113 | ||
35754c98 | 5114 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
bf8179a0 NHE |
5115 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
5116 | #ifdef CONFIG_X86_64 | |
5117 | exec_control |= CPU_BASED_CR8_STORE_EXITING | | |
5118 | CPU_BASED_CR8_LOAD_EXITING; | |
5119 | #endif | |
5120 | } | |
5121 | if (!enable_ept) | |
5122 | exec_control |= CPU_BASED_CR3_STORE_EXITING | | |
5123 | CPU_BASED_CR3_LOAD_EXITING | | |
5124 | CPU_BASED_INVLPG_EXITING; | |
5125 | return exec_control; | |
5126 | } | |
5127 | ||
5128 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) | |
5129 | { | |
5130 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; | |
35754c98 | 5131 | if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu)) |
bf8179a0 NHE |
5132 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
5133 | if (vmx->vpid == 0) | |
5134 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; | |
5135 | if (!enable_ept) { | |
5136 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; | |
5137 | enable_unrestricted_guest = 0; | |
ad756a16 MJ |
5138 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
5139 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; | |
bf8179a0 NHE |
5140 | } |
5141 | if (!enable_unrestricted_guest) | |
5142 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
5143 | if (!ple_gap) | |
5144 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
d62caabb | 5145 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
c7c9c56c YZ |
5146 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
5147 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
8d14695f | 5148 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
abc4fc58 AG |
5149 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
5150 | (handle_vmptrld). | |
5151 | We can NOT enable shadow_vmcs here because we don't have yet | |
5152 | a current VMCS12 | |
5153 | */ | |
5154 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; | |
a3eaa864 KH |
5155 | |
5156 | if (!enable_pml) | |
5157 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; | |
843e4330 | 5158 | |
bf8179a0 NHE |
5159 | return exec_control; |
5160 | } | |
5161 | ||
ce88decf XG |
5162 | static void ept_set_mmio_spte_mask(void) |
5163 | { | |
5164 | /* | |
5165 | * EPT Misconfigurations can be generated if the value of bits 2:0 | |
5166 | * of an EPT paging-structure entry is 110b (write/execute). | |
ce88decf | 5167 | */ |
dcdca5fe PF |
5168 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
5169 | VMX_EPT_MISCONFIG_WX_VALUE); | |
ce88decf XG |
5170 | } |
5171 | ||
f53cd63c | 5172 | #define VMX_XSS_EXIT_BITMAP 0 |
6aa8b732 AK |
5173 | /* |
5174 | * Sets up the vmcs for emulated real mode. | |
5175 | */ | |
8b9cf98c | 5176 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
6aa8b732 | 5177 | { |
2e4ce7f5 | 5178 | #ifdef CONFIG_X86_64 |
6aa8b732 | 5179 | unsigned long a; |
2e4ce7f5 | 5180 | #endif |
6aa8b732 | 5181 | int i; |
6aa8b732 | 5182 | |
6aa8b732 | 5183 | /* I/O */ |
3e7c73e9 AK |
5184 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
5185 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); | |
6aa8b732 | 5186 | |
4607c2d7 AG |
5187 | if (enable_shadow_vmcs) { |
5188 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); | |
5189 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); | |
5190 | } | |
25c5f225 | 5191 | if (cpu_has_vmx_msr_bitmap()) |
5897297b | 5192 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
25c5f225 | 5193 | |
6aa8b732 AK |
5194 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
5195 | ||
6aa8b732 | 5196 | /* Control */ |
01e439be | 5197 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); |
64672c95 | 5198 | vmx->hv_deadline_tsc = -1; |
6e5d865c | 5199 | |
bf8179a0 | 5200 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
6aa8b732 | 5201 | |
dfa169bb | 5202 | if (cpu_has_secondary_exec_ctrls()) { |
bf8179a0 NHE |
5203 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
5204 | vmx_secondary_exec_control(vmx)); | |
dfa169bb | 5205 | } |
f78e0e2e | 5206 | |
d62caabb | 5207 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
c7c9c56c YZ |
5208 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
5209 | vmcs_write64(EOI_EXIT_BITMAP1, 0); | |
5210 | vmcs_write64(EOI_EXIT_BITMAP2, 0); | |
5211 | vmcs_write64(EOI_EXIT_BITMAP3, 0); | |
5212 | ||
5213 | vmcs_write16(GUEST_INTR_STATUS, 0); | |
01e439be | 5214 | |
0bcf261c | 5215 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
01e439be | 5216 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
c7c9c56c YZ |
5217 | } |
5218 | ||
4b8d54f9 ZE |
5219 | if (ple_gap) { |
5220 | vmcs_write32(PLE_GAP, ple_gap); | |
a7653ecd RK |
5221 | vmx->ple_window = ple_window; |
5222 | vmx->ple_window_dirty = true; | |
4b8d54f9 ZE |
5223 | } |
5224 | ||
c3707958 XG |
5225 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
5226 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); | |
6aa8b732 AK |
5227 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
5228 | ||
9581d442 AK |
5229 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
5230 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ | |
a547c6db | 5231 | vmx_set_constant_host_state(vmx); |
05b3e0c2 | 5232 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
5233 | rdmsrl(MSR_FS_BASE, a); |
5234 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ | |
5235 | rdmsrl(MSR_GS_BASE, a); | |
5236 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ | |
5237 | #else | |
5238 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ | |
5239 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ | |
5240 | #endif | |
5241 | ||
2cc51560 ED |
5242 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
5243 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); | |
61d2ef2c | 5244 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
2cc51560 | 5245 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
61d2ef2c | 5246 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
6aa8b732 | 5247 | |
74545705 RK |
5248 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
5249 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); | |
468d472f | 5250 | |
03916db9 | 5251 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
6aa8b732 AK |
5252 | u32 index = vmx_msr_index[i]; |
5253 | u32 data_low, data_high; | |
a2fa3e9f | 5254 | int j = vmx->nmsrs; |
6aa8b732 AK |
5255 | |
5256 | if (rdmsr_safe(index, &data_low, &data_high) < 0) | |
5257 | continue; | |
432bd6cb AK |
5258 | if (wrmsr_safe(index, data_low, data_high) < 0) |
5259 | continue; | |
26bb0981 AK |
5260 | vmx->guest_msrs[j].index = i; |
5261 | vmx->guest_msrs[j].data = 0; | |
d5696725 | 5262 | vmx->guest_msrs[j].mask = -1ull; |
a2fa3e9f | 5263 | ++vmx->nmsrs; |
6aa8b732 | 5264 | } |
6aa8b732 | 5265 | |
2961e876 GN |
5266 | |
5267 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); | |
6aa8b732 AK |
5268 | |
5269 | /* 22.2.1, 20.8.1 */ | |
2961e876 | 5270 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
1c3d14fe | 5271 | |
bd7e5b08 PB |
5272 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
5273 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); | |
5274 | ||
bf8179a0 | 5275 | set_cr4_guest_host_mask(vmx); |
e00c8cf2 | 5276 | |
f53cd63c WL |
5277 | if (vmx_xsaves_supported()) |
5278 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); | |
5279 | ||
4e59516a PF |
5280 | if (enable_pml) { |
5281 | ASSERT(vmx->pml_pg); | |
5282 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
5283 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
5284 | } | |
5285 | ||
e00c8cf2 AK |
5286 | return 0; |
5287 | } | |
5288 | ||
d28bc9dd | 5289 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
e00c8cf2 AK |
5290 | { |
5291 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
58cb628d | 5292 | struct msr_data apic_base_msr; |
d28bc9dd | 5293 | u64 cr0; |
e00c8cf2 | 5294 | |
7ffd92c5 | 5295 | vmx->rmode.vm86_active = 0; |
e00c8cf2 | 5296 | |
ad312c7c | 5297 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
d28bc9dd NA |
5298 | kvm_set_cr8(vcpu, 0); |
5299 | ||
5300 | if (!init_event) { | |
5301 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | | |
5302 | MSR_IA32_APICBASE_ENABLE; | |
5303 | if (kvm_vcpu_is_reset_bsp(vcpu)) | |
5304 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; | |
5305 | apic_base_msr.host_initiated = true; | |
5306 | kvm_set_apic_base(vcpu, &apic_base_msr); | |
5307 | } | |
e00c8cf2 | 5308 | |
2fb92db1 AK |
5309 | vmx_segment_cache_clear(vmx); |
5310 | ||
5706be0d | 5311 | seg_setup(VCPU_SREG_CS); |
66450a21 | 5312 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
f3531054 | 5313 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
e00c8cf2 AK |
5314 | |
5315 | seg_setup(VCPU_SREG_DS); | |
5316 | seg_setup(VCPU_SREG_ES); | |
5317 | seg_setup(VCPU_SREG_FS); | |
5318 | seg_setup(VCPU_SREG_GS); | |
5319 | seg_setup(VCPU_SREG_SS); | |
5320 | ||
5321 | vmcs_write16(GUEST_TR_SELECTOR, 0); | |
5322 | vmcs_writel(GUEST_TR_BASE, 0); | |
5323 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); | |
5324 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); | |
5325 | ||
5326 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); | |
5327 | vmcs_writel(GUEST_LDTR_BASE, 0); | |
5328 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); | |
5329 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); | |
5330 | ||
d28bc9dd NA |
5331 | if (!init_event) { |
5332 | vmcs_write32(GUEST_SYSENTER_CS, 0); | |
5333 | vmcs_writel(GUEST_SYSENTER_ESP, 0); | |
5334 | vmcs_writel(GUEST_SYSENTER_EIP, 0); | |
5335 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
5336 | } | |
e00c8cf2 AK |
5337 | |
5338 | vmcs_writel(GUEST_RFLAGS, 0x02); | |
66450a21 | 5339 | kvm_rip_write(vcpu, 0xfff0); |
e00c8cf2 | 5340 | |
e00c8cf2 AK |
5341 | vmcs_writel(GUEST_GDTR_BASE, 0); |
5342 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); | |
5343 | ||
5344 | vmcs_writel(GUEST_IDTR_BASE, 0); | |
5345 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); | |
5346 | ||
443381a8 | 5347 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
e00c8cf2 | 5348 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
f3531054 | 5349 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
e00c8cf2 | 5350 | |
e00c8cf2 AK |
5351 | setup_msrs(vmx); |
5352 | ||
6aa8b732 AK |
5353 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
5354 | ||
d28bc9dd | 5355 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
f78e0e2e | 5356 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
35754c98 | 5357 | if (cpu_need_tpr_shadow(vcpu)) |
f78e0e2e | 5358 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
d28bc9dd | 5359 | __pa(vcpu->arch.apic->regs)); |
f78e0e2e SY |
5360 | vmcs_write32(TPR_THRESHOLD, 0); |
5361 | } | |
5362 | ||
a73896cb | 5363 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
6aa8b732 | 5364 | |
d62caabb | 5365 | if (kvm_vcpu_apicv_active(vcpu)) |
01e439be YZ |
5366 | memset(&vmx->pi_desc, 0, sizeof(struct pi_desc)); |
5367 | ||
2384d2b3 SY |
5368 | if (vmx->vpid != 0) |
5369 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
5370 | ||
d28bc9dd | 5371 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
d28bc9dd | 5372 | vmx->vcpu.arch.cr0 = cr0; |
f2463247 | 5373 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
d28bc9dd | 5374 | vmx_set_cr4(vcpu, 0); |
5690891b | 5375 | vmx_set_efer(vcpu, 0); |
bd7e5b08 | 5376 | |
d28bc9dd | 5377 | update_exception_bitmap(vcpu); |
6aa8b732 | 5378 | |
dd5f5341 | 5379 | vpid_sync_context(vmx->vpid); |
6aa8b732 AK |
5380 | } |
5381 | ||
b6f1250e NHE |
5382 | /* |
5383 | * In nested virtualization, check if L1 asked to exit on external interrupts. | |
5384 | * For most existing hypervisors, this will always return true. | |
5385 | */ | |
5386 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) | |
5387 | { | |
5388 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & | |
5389 | PIN_BASED_EXT_INTR_MASK; | |
5390 | } | |
5391 | ||
77b0f5d6 BD |
5392 | /* |
5393 | * In nested virtualization, check if L1 has set | |
5394 | * VM_EXIT_ACK_INTR_ON_EXIT | |
5395 | */ | |
5396 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) | |
5397 | { | |
5398 | return get_vmcs12(vcpu)->vm_exit_controls & | |
5399 | VM_EXIT_ACK_INTR_ON_EXIT; | |
5400 | } | |
5401 | ||
ea8ceb83 JK |
5402 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
5403 | { | |
5404 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & | |
5405 | PIN_BASED_NMI_EXITING; | |
5406 | } | |
5407 | ||
c9a7953f | 5408 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 5409 | { |
47c0152e PB |
5410 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
5411 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
3b86cd99 JK |
5412 | } |
5413 | ||
c9a7953f | 5414 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 5415 | { |
2c82878b | 5416 | if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
c9a7953f JK |
5417 | enable_irq_window(vcpu); |
5418 | return; | |
5419 | } | |
3b86cd99 | 5420 | |
47c0152e PB |
5421 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
5422 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
3b86cd99 JK |
5423 | } |
5424 | ||
66fd3f7f | 5425 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
85f455f7 | 5426 | { |
9c8cba37 | 5427 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
66fd3f7f GN |
5428 | uint32_t intr; |
5429 | int irq = vcpu->arch.interrupt.nr; | |
9c8cba37 | 5430 | |
229456fc | 5431 | trace_kvm_inj_virq(irq); |
2714d1d3 | 5432 | |
fa89a817 | 5433 | ++vcpu->stat.irq_injections; |
7ffd92c5 | 5434 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
5435 | int inc_eip = 0; |
5436 | if (vcpu->arch.interrupt.soft) | |
5437 | inc_eip = vcpu->arch.event_exit_inst_len; | |
5438 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) | |
a92601bb | 5439 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
85f455f7 ED |
5440 | return; |
5441 | } | |
66fd3f7f GN |
5442 | intr = irq | INTR_INFO_VALID_MASK; |
5443 | if (vcpu->arch.interrupt.soft) { | |
5444 | intr |= INTR_TYPE_SOFT_INTR; | |
5445 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
5446 | vmx->vcpu.arch.event_exit_inst_len); | |
5447 | } else | |
5448 | intr |= INTR_TYPE_EXT_INTR; | |
5449 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); | |
85f455f7 ED |
5450 | } |
5451 | ||
f08864b4 SY |
5452 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
5453 | { | |
66a5a347 JK |
5454 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
5455 | ||
c5a6d5f7 | 5456 | if (!is_guest_mode(vcpu)) { |
c5a6d5f7 WL |
5457 | ++vcpu->stat.nmi_injections; |
5458 | vmx->nmi_known_unmasked = false; | |
3b86cd99 JK |
5459 | } |
5460 | ||
7ffd92c5 | 5461 | if (vmx->rmode.vm86_active) { |
71f9833b | 5462 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
a92601bb | 5463 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
66a5a347 JK |
5464 | return; |
5465 | } | |
c5a6d5f7 | 5466 | |
f08864b4 SY |
5467 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
5468 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); | |
f08864b4 SY |
5469 | } |
5470 | ||
3cfc3092 JK |
5471 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
5472 | { | |
9d58b931 AK |
5473 | if (to_vmx(vcpu)->nmi_known_unmasked) |
5474 | return false; | |
c332c83a | 5475 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
3cfc3092 JK |
5476 | } |
5477 | ||
5478 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) | |
5479 | { | |
5480 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5481 | ||
2c82878b PB |
5482 | vmx->nmi_known_unmasked = !masked; |
5483 | if (masked) | |
5484 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
5485 | GUEST_INTR_STATE_NMI); | |
5486 | else | |
5487 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, | |
5488 | GUEST_INTR_STATE_NMI); | |
3cfc3092 JK |
5489 | } |
5490 | ||
2505dc9f JK |
5491 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
5492 | { | |
b6b8a145 JK |
5493 | if (to_vmx(vcpu)->nested.nested_run_pending) |
5494 | return 0; | |
ea8ceb83 | 5495 | |
2505dc9f JK |
5496 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
5497 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI | |
5498 | | GUEST_INTR_STATE_NMI)); | |
5499 | } | |
5500 | ||
78646121 GN |
5501 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
5502 | { | |
b6b8a145 JK |
5503 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
5504 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && | |
c4282df9 GN |
5505 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
5506 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); | |
78646121 GN |
5507 | } |
5508 | ||
cbc94022 IE |
5509 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
5510 | { | |
5511 | int ret; | |
cbc94022 | 5512 | |
1d8007bd PB |
5513 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
5514 | PAGE_SIZE * 3); | |
cbc94022 IE |
5515 | if (ret) |
5516 | return ret; | |
bfc6d222 | 5517 | kvm->arch.tss_addr = addr; |
1f755a82 | 5518 | return init_rmode_tss(kvm); |
cbc94022 IE |
5519 | } |
5520 | ||
0ca1b4f4 | 5521 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
6aa8b732 | 5522 | { |
77ab6db0 | 5523 | switch (vec) { |
77ab6db0 | 5524 | case BP_VECTOR: |
c573cd22 JK |
5525 | /* |
5526 | * Update instruction length as we may reinject the exception | |
5527 | * from user space while in guest debugging mode. | |
5528 | */ | |
5529 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = | |
5530 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
d0bfb940 | 5531 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
0ca1b4f4 GN |
5532 | return false; |
5533 | /* fall through */ | |
5534 | case DB_VECTOR: | |
5535 | if (vcpu->guest_debug & | |
5536 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
5537 | return false; | |
d0bfb940 JK |
5538 | /* fall through */ |
5539 | case DE_VECTOR: | |
77ab6db0 JK |
5540 | case OF_VECTOR: |
5541 | case BR_VECTOR: | |
5542 | case UD_VECTOR: | |
5543 | case DF_VECTOR: | |
5544 | case SS_VECTOR: | |
5545 | case GP_VECTOR: | |
5546 | case MF_VECTOR: | |
0ca1b4f4 GN |
5547 | return true; |
5548 | break; | |
77ab6db0 | 5549 | } |
0ca1b4f4 GN |
5550 | return false; |
5551 | } | |
5552 | ||
5553 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, | |
5554 | int vec, u32 err_code) | |
5555 | { | |
5556 | /* | |
5557 | * Instruction with address size override prefix opcode 0x67 | |
5558 | * Cause the #SS fault with 0 error code in VM86 mode. | |
5559 | */ | |
5560 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { | |
5561 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { | |
5562 | if (vcpu->arch.halt_request) { | |
5563 | vcpu->arch.halt_request = 0; | |
5cb56059 | 5564 | return kvm_vcpu_halt(vcpu); |
0ca1b4f4 GN |
5565 | } |
5566 | return 1; | |
5567 | } | |
5568 | return 0; | |
5569 | } | |
5570 | ||
5571 | /* | |
5572 | * Forward all other exceptions that are valid in real mode. | |
5573 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with | |
5574 | * the required debugging infrastructure rework. | |
5575 | */ | |
5576 | kvm_queue_exception(vcpu, vec); | |
5577 | return 1; | |
6aa8b732 AK |
5578 | } |
5579 | ||
a0861c02 AK |
5580 | /* |
5581 | * Trigger machine check on the host. We assume all the MSRs are already set up | |
5582 | * by the CPU and that we still run on the same CPU as the MCE occurred on. | |
5583 | * We pass a fake environment to the machine check handler because we want | |
5584 | * the guest to be always treated like user space, no matter what context | |
5585 | * it used internally. | |
5586 | */ | |
5587 | static void kvm_machine_check(void) | |
5588 | { | |
5589 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) | |
5590 | struct pt_regs regs = { | |
5591 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ | |
5592 | .flags = X86_EFLAGS_IF, | |
5593 | }; | |
5594 | ||
5595 | do_machine_check(®s, 0); | |
5596 | #endif | |
5597 | } | |
5598 | ||
851ba692 | 5599 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
a0861c02 AK |
5600 | { |
5601 | /* already handled by vcpu_run */ | |
5602 | return 1; | |
5603 | } | |
5604 | ||
851ba692 | 5605 | static int handle_exception(struct kvm_vcpu *vcpu) |
6aa8b732 | 5606 | { |
1155f76a | 5607 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
851ba692 | 5608 | struct kvm_run *kvm_run = vcpu->run; |
d0bfb940 | 5609 | u32 intr_info, ex_no, error_code; |
42dbaa5a | 5610 | unsigned long cr2, rip, dr6; |
6aa8b732 AK |
5611 | u32 vect_info; |
5612 | enum emulation_result er; | |
5613 | ||
1155f76a | 5614 | vect_info = vmx->idt_vectoring_info; |
88786475 | 5615 | intr_info = vmx->exit_intr_info; |
6aa8b732 | 5616 | |
a0861c02 | 5617 | if (is_machine_check(intr_info)) |
851ba692 | 5618 | return handle_machine_check(vcpu); |
a0861c02 | 5619 | |
ef85b673 | 5620 | if (is_nmi(intr_info)) |
1b6269db | 5621 | return 1; /* already handled by vmx_vcpu_run() */ |
2ab455cc | 5622 | |
7aa81cc0 | 5623 | if (is_invalid_opcode(intr_info)) { |
ae1f5767 JK |
5624 | if (is_guest_mode(vcpu)) { |
5625 | kvm_queue_exception(vcpu, UD_VECTOR); | |
5626 | return 1; | |
5627 | } | |
51d8b661 | 5628 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
7aa81cc0 | 5629 | if (er != EMULATE_DONE) |
7ee5d940 | 5630 | kvm_queue_exception(vcpu, UD_VECTOR); |
7aa81cc0 AL |
5631 | return 1; |
5632 | } | |
5633 | ||
6aa8b732 | 5634 | error_code = 0; |
2e11384c | 5635 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
6aa8b732 | 5636 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
bf4ca23e XG |
5637 | |
5638 | /* | |
5639 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing | |
5640 | * MMIO, it is better to report an internal error. | |
5641 | * See the comments in vmx_handle_exit. | |
5642 | */ | |
5643 | if ((vect_info & VECTORING_INFO_VALID_MASK) && | |
5644 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { | |
5645 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
5646 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; | |
80f0e95d | 5647 | vcpu->run->internal.ndata = 3; |
bf4ca23e XG |
5648 | vcpu->run->internal.data[0] = vect_info; |
5649 | vcpu->run->internal.data[1] = intr_info; | |
80f0e95d | 5650 | vcpu->run->internal.data[2] = error_code; |
bf4ca23e XG |
5651 | return 0; |
5652 | } | |
5653 | ||
6aa8b732 | 5654 | if (is_page_fault(intr_info)) { |
1439442c | 5655 | /* EPT won't cause page fault directly */ |
cf3ace79 | 5656 | BUG_ON(enable_ept); |
6aa8b732 | 5657 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
229456fc MT |
5658 | trace_kvm_page_fault(cr2, error_code); |
5659 | ||
3298b75c | 5660 | if (kvm_event_needs_reinjection(vcpu)) |
577bdc49 | 5661 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
dc25e89e | 5662 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
6aa8b732 AK |
5663 | } |
5664 | ||
d0bfb940 | 5665 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
0ca1b4f4 GN |
5666 | |
5667 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) | |
5668 | return handle_rmode_exception(vcpu, ex_no, error_code); | |
5669 | ||
42dbaa5a | 5670 | switch (ex_no) { |
54a20552 EN |
5671 | case AC_VECTOR: |
5672 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); | |
5673 | return 1; | |
42dbaa5a JK |
5674 | case DB_VECTOR: |
5675 | dr6 = vmcs_readl(EXIT_QUALIFICATION); | |
5676 | if (!(vcpu->guest_debug & | |
5677 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { | |
8246bf52 | 5678 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 5679 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
fd2a445a HD |
5680 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ |
5681 | skip_emulated_instruction(vcpu); | |
5682 | ||
42dbaa5a JK |
5683 | kvm_queue_exception(vcpu, DB_VECTOR); |
5684 | return 1; | |
5685 | } | |
5686 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; | |
5687 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); | |
5688 | /* fall through */ | |
5689 | case BP_VECTOR: | |
c573cd22 JK |
5690 | /* |
5691 | * Update instruction length as we may reinject #BP from | |
5692 | * user space while in guest debugging mode. Reading it for | |
5693 | * #DB as well causes no harm, it is not used in that case. | |
5694 | */ | |
5695 | vmx->vcpu.arch.event_exit_inst_len = | |
5696 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
6aa8b732 | 5697 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
0a434bb2 | 5698 | rip = kvm_rip_read(vcpu); |
d0bfb940 JK |
5699 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
5700 | kvm_run->debug.arch.exception = ex_no; | |
42dbaa5a JK |
5701 | break; |
5702 | default: | |
d0bfb940 JK |
5703 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
5704 | kvm_run->ex.exception = ex_no; | |
5705 | kvm_run->ex.error_code = error_code; | |
42dbaa5a | 5706 | break; |
6aa8b732 | 5707 | } |
6aa8b732 AK |
5708 | return 0; |
5709 | } | |
5710 | ||
851ba692 | 5711 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
6aa8b732 | 5712 | { |
1165f5fe | 5713 | ++vcpu->stat.irq_exits; |
6aa8b732 AK |
5714 | return 1; |
5715 | } | |
5716 | ||
851ba692 | 5717 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
988ad74f | 5718 | { |
851ba692 | 5719 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
988ad74f AK |
5720 | return 0; |
5721 | } | |
6aa8b732 | 5722 | |
851ba692 | 5723 | static int handle_io(struct kvm_vcpu *vcpu) |
6aa8b732 | 5724 | { |
bfdaab09 | 5725 | unsigned long exit_qualification; |
6affcbed | 5726 | int size, in, string, ret; |
039576c0 | 5727 | unsigned port; |
6aa8b732 | 5728 | |
bfdaab09 | 5729 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
039576c0 | 5730 | string = (exit_qualification & 16) != 0; |
cf8f70bf | 5731 | in = (exit_qualification & 8) != 0; |
e70669ab | 5732 | |
cf8f70bf | 5733 | ++vcpu->stat.io_exits; |
e70669ab | 5734 | |
cf8f70bf | 5735 | if (string || in) |
51d8b661 | 5736 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
e70669ab | 5737 | |
cf8f70bf GN |
5738 | port = exit_qualification >> 16; |
5739 | size = (exit_qualification & 7) + 1; | |
cf8f70bf | 5740 | |
6affcbed KH |
5741 | ret = kvm_skip_emulated_instruction(vcpu); |
5742 | ||
5743 | /* | |
5744 | * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered | |
5745 | * KVM_EXIT_DEBUG here. | |
5746 | */ | |
5747 | return kvm_fast_pio_out(vcpu, size, port) && ret; | |
6aa8b732 AK |
5748 | } |
5749 | ||
102d8325 IM |
5750 | static void |
5751 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) | |
5752 | { | |
5753 | /* | |
5754 | * Patch in the VMCALL instruction: | |
5755 | */ | |
5756 | hypercall[0] = 0x0f; | |
5757 | hypercall[1] = 0x01; | |
5758 | hypercall[2] = 0xc1; | |
102d8325 IM |
5759 | } |
5760 | ||
0fa06071 | 5761 | /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ |
eeadf9e7 NHE |
5762 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
5763 | { | |
eeadf9e7 | 5764 | if (is_guest_mode(vcpu)) { |
1a0d74e6 JK |
5765 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
5766 | unsigned long orig_val = val; | |
5767 | ||
eeadf9e7 NHE |
5768 | /* |
5769 | * We get here when L2 changed cr0 in a way that did not change | |
5770 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), | |
1a0d74e6 JK |
5771 | * but did change L0 shadowed bits. So we first calculate the |
5772 | * effective cr0 value that L1 would like to write into the | |
5773 | * hardware. It consists of the L2-owned bits from the new | |
5774 | * value combined with the L1-owned bits from L1's guest_cr0. | |
eeadf9e7 | 5775 | */ |
1a0d74e6 JK |
5776 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
5777 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); | |
5778 | ||
3899152c | 5779 | if (!nested_guest_cr0_valid(vcpu, val)) |
eeadf9e7 | 5780 | return 1; |
1a0d74e6 JK |
5781 | |
5782 | if (kvm_set_cr0(vcpu, val)) | |
5783 | return 1; | |
5784 | vmcs_writel(CR0_READ_SHADOW, orig_val); | |
eeadf9e7 | 5785 | return 0; |
1a0d74e6 JK |
5786 | } else { |
5787 | if (to_vmx(vcpu)->nested.vmxon && | |
3899152c | 5788 | !nested_host_cr0_valid(vcpu, val)) |
1a0d74e6 | 5789 | return 1; |
3899152c | 5790 | |
eeadf9e7 | 5791 | return kvm_set_cr0(vcpu, val); |
1a0d74e6 | 5792 | } |
eeadf9e7 NHE |
5793 | } |
5794 | ||
5795 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) | |
5796 | { | |
5797 | if (is_guest_mode(vcpu)) { | |
1a0d74e6 JK |
5798 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
5799 | unsigned long orig_val = val; | |
5800 | ||
5801 | /* analogously to handle_set_cr0 */ | |
5802 | val = (val & ~vmcs12->cr4_guest_host_mask) | | |
5803 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); | |
5804 | if (kvm_set_cr4(vcpu, val)) | |
eeadf9e7 | 5805 | return 1; |
1a0d74e6 | 5806 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
eeadf9e7 NHE |
5807 | return 0; |
5808 | } else | |
5809 | return kvm_set_cr4(vcpu, val); | |
5810 | } | |
5811 | ||
851ba692 | 5812 | static int handle_cr(struct kvm_vcpu *vcpu) |
6aa8b732 | 5813 | { |
229456fc | 5814 | unsigned long exit_qualification, val; |
6aa8b732 AK |
5815 | int cr; |
5816 | int reg; | |
49a9b07e | 5817 | int err; |
6affcbed | 5818 | int ret; |
6aa8b732 | 5819 | |
bfdaab09 | 5820 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
6aa8b732 AK |
5821 | cr = exit_qualification & 15; |
5822 | reg = (exit_qualification >> 8) & 15; | |
5823 | switch ((exit_qualification >> 4) & 3) { | |
5824 | case 0: /* mov to cr */ | |
1e32c079 | 5825 | val = kvm_register_readl(vcpu, reg); |
229456fc | 5826 | trace_kvm_cr_write(cr, val); |
6aa8b732 AK |
5827 | switch (cr) { |
5828 | case 0: | |
eeadf9e7 | 5829 | err = handle_set_cr0(vcpu, val); |
6affcbed | 5830 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 5831 | case 3: |
2390218b | 5832 | err = kvm_set_cr3(vcpu, val); |
6affcbed | 5833 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 5834 | case 4: |
eeadf9e7 | 5835 | err = handle_set_cr4(vcpu, val); |
6affcbed | 5836 | return kvm_complete_insn_gp(vcpu, err); |
0a5fff19 GN |
5837 | case 8: { |
5838 | u8 cr8_prev = kvm_get_cr8(vcpu); | |
1e32c079 | 5839 | u8 cr8 = (u8)val; |
eea1cff9 | 5840 | err = kvm_set_cr8(vcpu, cr8); |
6affcbed | 5841 | ret = kvm_complete_insn_gp(vcpu, err); |
35754c98 | 5842 | if (lapic_in_kernel(vcpu)) |
6affcbed | 5843 | return ret; |
0a5fff19 | 5844 | if (cr8_prev <= cr8) |
6affcbed KH |
5845 | return ret; |
5846 | /* | |
5847 | * TODO: we might be squashing a | |
5848 | * KVM_GUESTDBG_SINGLESTEP-triggered | |
5849 | * KVM_EXIT_DEBUG here. | |
5850 | */ | |
851ba692 | 5851 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
0a5fff19 GN |
5852 | return 0; |
5853 | } | |
4b8073e4 | 5854 | } |
6aa8b732 | 5855 | break; |
25c4c276 | 5856 | case 2: /* clts */ |
bd7e5b08 PB |
5857 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
5858 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); | |
4d4ec087 | 5859 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
6affcbed | 5860 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
5861 | case 1: /*mov from cr*/ |
5862 | switch (cr) { | |
5863 | case 3: | |
9f8fe504 AK |
5864 | val = kvm_read_cr3(vcpu); |
5865 | kvm_register_write(vcpu, reg, val); | |
5866 | trace_kvm_cr_read(cr, val); | |
6affcbed | 5867 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 | 5868 | case 8: |
229456fc MT |
5869 | val = kvm_get_cr8(vcpu); |
5870 | kvm_register_write(vcpu, reg, val); | |
5871 | trace_kvm_cr_read(cr, val); | |
6affcbed | 5872 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
5873 | } |
5874 | break; | |
5875 | case 3: /* lmsw */ | |
a1f83a74 | 5876 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
4d4ec087 | 5877 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
a1f83a74 | 5878 | kvm_lmsw(vcpu, val); |
6aa8b732 | 5879 | |
6affcbed | 5880 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
5881 | default: |
5882 | break; | |
5883 | } | |
851ba692 | 5884 | vcpu->run->exit_reason = 0; |
a737f256 | 5885 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
6aa8b732 AK |
5886 | (int)(exit_qualification >> 4) & 3, cr); |
5887 | return 0; | |
5888 | } | |
5889 | ||
851ba692 | 5890 | static int handle_dr(struct kvm_vcpu *vcpu) |
6aa8b732 | 5891 | { |
bfdaab09 | 5892 | unsigned long exit_qualification; |
16f8a6f9 NA |
5893 | int dr, dr7, reg; |
5894 | ||
5895 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
5896 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; | |
5897 | ||
5898 | /* First, if DR does not exist, trigger UD */ | |
5899 | if (!kvm_require_dr(vcpu, dr)) | |
5900 | return 1; | |
6aa8b732 | 5901 | |
f2483415 | 5902 | /* Do not handle if the CPL > 0, will trigger GP on re-entry */ |
0a79b009 AK |
5903 | if (!kvm_require_cpl(vcpu, 0)) |
5904 | return 1; | |
16f8a6f9 NA |
5905 | dr7 = vmcs_readl(GUEST_DR7); |
5906 | if (dr7 & DR7_GD) { | |
42dbaa5a JK |
5907 | /* |
5908 | * As the vm-exit takes precedence over the debug trap, we | |
5909 | * need to emulate the latter, either for the host or the | |
5910 | * guest debugging itself. | |
5911 | */ | |
5912 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { | |
851ba692 | 5913 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
16f8a6f9 | 5914 | vcpu->run->debug.arch.dr7 = dr7; |
82b32774 | 5915 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
851ba692 AK |
5916 | vcpu->run->debug.arch.exception = DB_VECTOR; |
5917 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; | |
42dbaa5a JK |
5918 | return 0; |
5919 | } else { | |
7305eb5d | 5920 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 5921 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
42dbaa5a JK |
5922 | kvm_queue_exception(vcpu, DB_VECTOR); |
5923 | return 1; | |
5924 | } | |
5925 | } | |
5926 | ||
81908bf4 | 5927 | if (vcpu->guest_debug == 0) { |
8f22372f PB |
5928 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
5929 | CPU_BASED_MOV_DR_EXITING); | |
81908bf4 PB |
5930 | |
5931 | /* | |
5932 | * No more DR vmexits; force a reload of the debug registers | |
5933 | * and reenter on this instruction. The next vmexit will | |
5934 | * retrieve the full state of the debug registers. | |
5935 | */ | |
5936 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; | |
5937 | return 1; | |
5938 | } | |
5939 | ||
42dbaa5a JK |
5940 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
5941 | if (exit_qualification & TYPE_MOV_FROM_DR) { | |
020df079 | 5942 | unsigned long val; |
4c4d563b JK |
5943 | |
5944 | if (kvm_get_dr(vcpu, dr, &val)) | |
5945 | return 1; | |
5946 | kvm_register_write(vcpu, reg, val); | |
020df079 | 5947 | } else |
5777392e | 5948 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
4c4d563b JK |
5949 | return 1; |
5950 | ||
6affcbed | 5951 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
5952 | } |
5953 | ||
73aaf249 JK |
5954 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
5955 | { | |
5956 | return vcpu->arch.dr6; | |
5957 | } | |
5958 | ||
5959 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) | |
5960 | { | |
5961 | } | |
5962 | ||
81908bf4 PB |
5963 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
5964 | { | |
81908bf4 PB |
5965 | get_debugreg(vcpu->arch.db[0], 0); |
5966 | get_debugreg(vcpu->arch.db[1], 1); | |
5967 | get_debugreg(vcpu->arch.db[2], 2); | |
5968 | get_debugreg(vcpu->arch.db[3], 3); | |
5969 | get_debugreg(vcpu->arch.dr6, 6); | |
5970 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); | |
5971 | ||
5972 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; | |
8f22372f | 5973 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING); |
81908bf4 PB |
5974 | } |
5975 | ||
020df079 GN |
5976 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
5977 | { | |
5978 | vmcs_writel(GUEST_DR7, val); | |
5979 | } | |
5980 | ||
851ba692 | 5981 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
6aa8b732 | 5982 | { |
6a908b62 | 5983 | return kvm_emulate_cpuid(vcpu); |
6aa8b732 AK |
5984 | } |
5985 | ||
851ba692 | 5986 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 5987 | { |
ad312c7c | 5988 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
609e36d3 | 5989 | struct msr_data msr_info; |
6aa8b732 | 5990 | |
609e36d3 PB |
5991 | msr_info.index = ecx; |
5992 | msr_info.host_initiated = false; | |
5993 | if (vmx_get_msr(vcpu, &msr_info)) { | |
59200273 | 5994 | trace_kvm_msr_read_ex(ecx); |
c1a5d4f9 | 5995 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
5996 | return 1; |
5997 | } | |
5998 | ||
609e36d3 | 5999 | trace_kvm_msr_read(ecx, msr_info.data); |
2714d1d3 | 6000 | |
6aa8b732 | 6001 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
609e36d3 PB |
6002 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
6003 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; | |
6affcbed | 6004 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6005 | } |
6006 | ||
851ba692 | 6007 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6008 | { |
8fe8ab46 | 6009 | struct msr_data msr; |
ad312c7c ZX |
6010 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
6011 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) | |
6012 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); | |
6aa8b732 | 6013 | |
8fe8ab46 WA |
6014 | msr.data = data; |
6015 | msr.index = ecx; | |
6016 | msr.host_initiated = false; | |
854e8bb1 | 6017 | if (kvm_set_msr(vcpu, &msr) != 0) { |
59200273 | 6018 | trace_kvm_msr_write_ex(ecx, data); |
c1a5d4f9 | 6019 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
6020 | return 1; |
6021 | } | |
6022 | ||
59200273 | 6023 | trace_kvm_msr_write(ecx, data); |
6affcbed | 6024 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6025 | } |
6026 | ||
851ba692 | 6027 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
6e5d865c | 6028 | { |
eb90f341 | 6029 | kvm_apic_update_ppr(vcpu); |
6e5d865c YS |
6030 | return 1; |
6031 | } | |
6032 | ||
851ba692 | 6033 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
6aa8b732 | 6034 | { |
47c0152e PB |
6035 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6036 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
2714d1d3 | 6037 | |
3842d135 AK |
6038 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
6039 | ||
a26bf12a | 6040 | ++vcpu->stat.irq_window_exits; |
6aa8b732 AK |
6041 | return 1; |
6042 | } | |
6043 | ||
851ba692 | 6044 | static int handle_halt(struct kvm_vcpu *vcpu) |
6aa8b732 | 6045 | { |
d3bef15f | 6046 | return kvm_emulate_halt(vcpu); |
6aa8b732 AK |
6047 | } |
6048 | ||
851ba692 | 6049 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
c21415e8 | 6050 | { |
0d9c055e | 6051 | return kvm_emulate_hypercall(vcpu); |
c21415e8 IM |
6052 | } |
6053 | ||
ec25d5e6 GN |
6054 | static int handle_invd(struct kvm_vcpu *vcpu) |
6055 | { | |
51d8b661 | 6056 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
ec25d5e6 GN |
6057 | } |
6058 | ||
851ba692 | 6059 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
a7052897 | 6060 | { |
f9c617f6 | 6061 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
a7052897 MT |
6062 | |
6063 | kvm_mmu_invlpg(vcpu, exit_qualification); | |
6affcbed | 6064 | return kvm_skip_emulated_instruction(vcpu); |
a7052897 MT |
6065 | } |
6066 | ||
fee84b07 AK |
6067 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
6068 | { | |
6069 | int err; | |
6070 | ||
6071 | err = kvm_rdpmc(vcpu); | |
6affcbed | 6072 | return kvm_complete_insn_gp(vcpu, err); |
fee84b07 AK |
6073 | } |
6074 | ||
851ba692 | 6075 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
e5edaa01 | 6076 | { |
6affcbed | 6077 | return kvm_emulate_wbinvd(vcpu); |
e5edaa01 ED |
6078 | } |
6079 | ||
2acf923e DC |
6080 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
6081 | { | |
6082 | u64 new_bv = kvm_read_edx_eax(vcpu); | |
6083 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
6084 | ||
6085 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) | |
6affcbed | 6086 | return kvm_skip_emulated_instruction(vcpu); |
2acf923e DC |
6087 | return 1; |
6088 | } | |
6089 | ||
f53cd63c WL |
6090 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
6091 | { | |
6affcbed | 6092 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6093 | WARN(1, "this should never happen\n"); |
6094 | return 1; | |
6095 | } | |
6096 | ||
6097 | static int handle_xrstors(struct kvm_vcpu *vcpu) | |
6098 | { | |
6affcbed | 6099 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6100 | WARN(1, "this should never happen\n"); |
6101 | return 1; | |
6102 | } | |
6103 | ||
851ba692 | 6104 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
f78e0e2e | 6105 | { |
58fbbf26 KT |
6106 | if (likely(fasteoi)) { |
6107 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6108 | int access_type, offset; | |
6109 | ||
6110 | access_type = exit_qualification & APIC_ACCESS_TYPE; | |
6111 | offset = exit_qualification & APIC_ACCESS_OFFSET; | |
6112 | /* | |
6113 | * Sane guest uses MOV to write EOI, with written value | |
6114 | * not cared. So make a short-circuit here by avoiding | |
6115 | * heavy instruction emulation. | |
6116 | */ | |
6117 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && | |
6118 | (offset == APIC_EOI)) { | |
6119 | kvm_lapic_set_eoi(vcpu); | |
6affcbed | 6120 | return kvm_skip_emulated_instruction(vcpu); |
58fbbf26 KT |
6121 | } |
6122 | } | |
51d8b661 | 6123 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
f78e0e2e SY |
6124 | } |
6125 | ||
c7c9c56c YZ |
6126 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
6127 | { | |
6128 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6129 | int vector = exit_qualification & 0xff; | |
6130 | ||
6131 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ | |
6132 | kvm_apic_set_eoi_accelerated(vcpu, vector); | |
6133 | return 1; | |
6134 | } | |
6135 | ||
83d4c286 YZ |
6136 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
6137 | { | |
6138 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6139 | u32 offset = exit_qualification & 0xfff; | |
6140 | ||
6141 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ | |
6142 | kvm_apic_write_nodecode(vcpu, offset); | |
6143 | return 1; | |
6144 | } | |
6145 | ||
851ba692 | 6146 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
37817f29 | 6147 | { |
60637aac | 6148 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
37817f29 | 6149 | unsigned long exit_qualification; |
e269fb21 JK |
6150 | bool has_error_code = false; |
6151 | u32 error_code = 0; | |
37817f29 | 6152 | u16 tss_selector; |
7f3d35fd | 6153 | int reason, type, idt_v, idt_index; |
64a7ec06 GN |
6154 | |
6155 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); | |
7f3d35fd | 6156 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
64a7ec06 | 6157 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
37817f29 IE |
6158 | |
6159 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6160 | ||
6161 | reason = (u32)exit_qualification >> 30; | |
64a7ec06 GN |
6162 | if (reason == TASK_SWITCH_GATE && idt_v) { |
6163 | switch (type) { | |
6164 | case INTR_TYPE_NMI_INTR: | |
6165 | vcpu->arch.nmi_injected = false; | |
654f06fc | 6166 | vmx_set_nmi_mask(vcpu, true); |
64a7ec06 GN |
6167 | break; |
6168 | case INTR_TYPE_EXT_INTR: | |
66fd3f7f | 6169 | case INTR_TYPE_SOFT_INTR: |
64a7ec06 GN |
6170 | kvm_clear_interrupt_queue(vcpu); |
6171 | break; | |
6172 | case INTR_TYPE_HARD_EXCEPTION: | |
e269fb21 JK |
6173 | if (vmx->idt_vectoring_info & |
6174 | VECTORING_INFO_DELIVER_CODE_MASK) { | |
6175 | has_error_code = true; | |
6176 | error_code = | |
6177 | vmcs_read32(IDT_VECTORING_ERROR_CODE); | |
6178 | } | |
6179 | /* fall through */ | |
64a7ec06 GN |
6180 | case INTR_TYPE_SOFT_EXCEPTION: |
6181 | kvm_clear_exception_queue(vcpu); | |
6182 | break; | |
6183 | default: | |
6184 | break; | |
6185 | } | |
60637aac | 6186 | } |
37817f29 IE |
6187 | tss_selector = exit_qualification; |
6188 | ||
64a7ec06 GN |
6189 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
6190 | type != INTR_TYPE_EXT_INTR && | |
6191 | type != INTR_TYPE_NMI_INTR)) | |
6192 | skip_emulated_instruction(vcpu); | |
6193 | ||
7f3d35fd KW |
6194 | if (kvm_task_switch(vcpu, tss_selector, |
6195 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, | |
6196 | has_error_code, error_code) == EMULATE_FAIL) { | |
acb54517 GN |
6197 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
6198 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6199 | vcpu->run->internal.ndata = 0; | |
42dbaa5a | 6200 | return 0; |
acb54517 | 6201 | } |
42dbaa5a | 6202 | |
42dbaa5a JK |
6203 | /* |
6204 | * TODO: What about debug traps on tss switch? | |
6205 | * Are we supposed to inject them and update dr6? | |
6206 | */ | |
6207 | ||
6208 | return 1; | |
37817f29 IE |
6209 | } |
6210 | ||
851ba692 | 6211 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
1439442c | 6212 | { |
f9c617f6 | 6213 | unsigned long exit_qualification; |
1439442c | 6214 | gpa_t gpa; |
4f5982a5 | 6215 | u32 error_code; |
1439442c | 6216 | |
f9c617f6 | 6217 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
1439442c | 6218 | |
0be9c7a8 GN |
6219 | /* |
6220 | * EPT violation happened while executing iret from NMI, | |
6221 | * "blocked by NMI" bit has to be set before next VM entry. | |
6222 | * There are errata that may cause this bit to not be set: | |
6223 | * AAK134, BY25. | |
6224 | */ | |
bcd1c294 | 6225 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
bcd1c294 | 6226 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
0be9c7a8 GN |
6227 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
6228 | ||
1439442c | 6229 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
229456fc | 6230 | trace_kvm_page_fault(gpa, exit_qualification); |
4f5982a5 | 6231 | |
27959a44 | 6232 | /* Is it a read fault? */ |
ab22a473 | 6233 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
27959a44 JS |
6234 | ? PFERR_USER_MASK : 0; |
6235 | /* Is it a write fault? */ | |
ab22a473 | 6236 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
27959a44 JS |
6237 | ? PFERR_WRITE_MASK : 0; |
6238 | /* Is it a fetch fault? */ | |
ab22a473 | 6239 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
27959a44 JS |
6240 | ? PFERR_FETCH_MASK : 0; |
6241 | /* ept page table entry is present? */ | |
6242 | error_code |= (exit_qualification & | |
6243 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | | |
6244 | EPT_VIOLATION_EXECUTABLE)) | |
6245 | ? PFERR_PRESENT_MASK : 0; | |
4f5982a5 | 6246 | |
db1c056c | 6247 | vcpu->arch.gpa_available = true; |
25d92081 YZ |
6248 | vcpu->arch.exit_qualification = exit_qualification; |
6249 | ||
4f5982a5 | 6250 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
1439442c SY |
6251 | } |
6252 | ||
851ba692 | 6253 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
68f89400 | 6254 | { |
f735d4af | 6255 | int ret; |
68f89400 MT |
6256 | gpa_t gpa; |
6257 | ||
6258 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); | |
e32edf4f | 6259 | if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
931c33b1 | 6260 | trace_kvm_fast_mmio(gpa); |
6affcbed | 6261 | return kvm_skip_emulated_instruction(vcpu); |
68c3b4d1 | 6262 | } |
68f89400 | 6263 | |
450869d6 | 6264 | ret = handle_mmio_page_fault(vcpu, gpa, true); |
db1c056c | 6265 | vcpu->arch.gpa_available = true; |
b37fbea6 | 6266 | if (likely(ret == RET_MMIO_PF_EMULATE)) |
ce88decf XG |
6267 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
6268 | EMULATE_DONE; | |
f8f55942 XG |
6269 | |
6270 | if (unlikely(ret == RET_MMIO_PF_INVALID)) | |
6271 | return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0); | |
6272 | ||
b37fbea6 | 6273 | if (unlikely(ret == RET_MMIO_PF_RETRY)) |
ce88decf XG |
6274 | return 1; |
6275 | ||
6276 | /* It is the real ept misconfig */ | |
f735d4af | 6277 | WARN_ON(1); |
68f89400 | 6278 | |
851ba692 AK |
6279 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
6280 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; | |
68f89400 MT |
6281 | |
6282 | return 0; | |
6283 | } | |
6284 | ||
851ba692 | 6285 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
f08864b4 | 6286 | { |
47c0152e PB |
6287 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6288 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
f08864b4 | 6289 | ++vcpu->stat.nmi_window_exits; |
3842d135 | 6290 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
f08864b4 SY |
6291 | |
6292 | return 1; | |
6293 | } | |
6294 | ||
80ced186 | 6295 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
ea953ef0 | 6296 | { |
8b3079a5 AK |
6297 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6298 | enum emulation_result err = EMULATE_DONE; | |
80ced186 | 6299 | int ret = 1; |
49e9d557 AK |
6300 | u32 cpu_exec_ctrl; |
6301 | bool intr_window_requested; | |
b8405c18 | 6302 | unsigned count = 130; |
49e9d557 AK |
6303 | |
6304 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); | |
6305 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; | |
ea953ef0 | 6306 | |
98eb2f8b | 6307 | while (vmx->emulation_required && count-- != 0) { |
bdea48e3 | 6308 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
49e9d557 AK |
6309 | return handle_interrupt_window(&vmx->vcpu); |
6310 | ||
72875d8a | 6311 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
de87dcdd AK |
6312 | return 1; |
6313 | ||
991eebf9 | 6314 | err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE); |
ea953ef0 | 6315 | |
ac0a48c3 | 6316 | if (err == EMULATE_USER_EXIT) { |
94452b9e | 6317 | ++vcpu->stat.mmio_exits; |
80ced186 MG |
6318 | ret = 0; |
6319 | goto out; | |
6320 | } | |
1d5a4d9b | 6321 | |
de5f70e0 AK |
6322 | if (err != EMULATE_DONE) { |
6323 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
6324 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6325 | vcpu->run->internal.ndata = 0; | |
6d77dbfc | 6326 | return 0; |
de5f70e0 | 6327 | } |
ea953ef0 | 6328 | |
8d76c49e GN |
6329 | if (vcpu->arch.halt_request) { |
6330 | vcpu->arch.halt_request = 0; | |
5cb56059 | 6331 | ret = kvm_vcpu_halt(vcpu); |
8d76c49e GN |
6332 | goto out; |
6333 | } | |
6334 | ||
ea953ef0 | 6335 | if (signal_pending(current)) |
80ced186 | 6336 | goto out; |
ea953ef0 MG |
6337 | if (need_resched()) |
6338 | schedule(); | |
6339 | } | |
6340 | ||
80ced186 MG |
6341 | out: |
6342 | return ret; | |
ea953ef0 MG |
6343 | } |
6344 | ||
b4a2d31d RK |
6345 | static int __grow_ple_window(int val) |
6346 | { | |
6347 | if (ple_window_grow < 1) | |
6348 | return ple_window; | |
6349 | ||
6350 | val = min(val, ple_window_actual_max); | |
6351 | ||
6352 | if (ple_window_grow < ple_window) | |
6353 | val *= ple_window_grow; | |
6354 | else | |
6355 | val += ple_window_grow; | |
6356 | ||
6357 | return val; | |
6358 | } | |
6359 | ||
6360 | static int __shrink_ple_window(int val, int modifier, int minimum) | |
6361 | { | |
6362 | if (modifier < 1) | |
6363 | return ple_window; | |
6364 | ||
6365 | if (modifier < ple_window) | |
6366 | val /= modifier; | |
6367 | else | |
6368 | val -= modifier; | |
6369 | ||
6370 | return max(val, minimum); | |
6371 | } | |
6372 | ||
6373 | static void grow_ple_window(struct kvm_vcpu *vcpu) | |
6374 | { | |
6375 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6376 | int old = vmx->ple_window; | |
6377 | ||
6378 | vmx->ple_window = __grow_ple_window(old); | |
6379 | ||
6380 | if (vmx->ple_window != old) | |
6381 | vmx->ple_window_dirty = true; | |
7b46268d RK |
6382 | |
6383 | trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
6384 | } |
6385 | ||
6386 | static void shrink_ple_window(struct kvm_vcpu *vcpu) | |
6387 | { | |
6388 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6389 | int old = vmx->ple_window; | |
6390 | ||
6391 | vmx->ple_window = __shrink_ple_window(old, | |
6392 | ple_window_shrink, ple_window); | |
6393 | ||
6394 | if (vmx->ple_window != old) | |
6395 | vmx->ple_window_dirty = true; | |
7b46268d RK |
6396 | |
6397 | trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
6398 | } |
6399 | ||
6400 | /* | |
6401 | * ple_window_actual_max is computed to be one grow_ple_window() below | |
6402 | * ple_window_max. (See __grow_ple_window for the reason.) | |
6403 | * This prevents overflows, because ple_window_max is int. | |
6404 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in | |
6405 | * this process. | |
6406 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. | |
6407 | */ | |
6408 | static void update_ple_window_actual_max(void) | |
6409 | { | |
6410 | ple_window_actual_max = | |
6411 | __shrink_ple_window(max(ple_window_max, ple_window), | |
6412 | ple_window_grow, INT_MIN); | |
6413 | } | |
6414 | ||
bf9f6ac8 FW |
6415 | /* |
6416 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. | |
6417 | */ | |
6418 | static void wakeup_handler(void) | |
6419 | { | |
6420 | struct kvm_vcpu *vcpu; | |
6421 | int cpu = smp_processor_id(); | |
6422 | ||
6423 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
6424 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), | |
6425 | blocked_vcpu_list) { | |
6426 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
6427 | ||
6428 | if (pi_test_on(pi_desc) == 1) | |
6429 | kvm_vcpu_kick(vcpu); | |
6430 | } | |
6431 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
6432 | } | |
6433 | ||
f160c7b7 JS |
6434 | void vmx_enable_tdp(void) |
6435 | { | |
6436 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, | |
6437 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, | |
6438 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, | |
6439 | 0ull, VMX_EPT_EXECUTABLE_MASK, | |
6440 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, | |
995f00a6 | 6441 | VMX_EPT_RWX_MASK); |
f160c7b7 JS |
6442 | |
6443 | ept_set_mmio_spte_mask(); | |
6444 | kvm_enable_tdp(); | |
6445 | } | |
6446 | ||
f2c7648d TC |
6447 | static __init int hardware_setup(void) |
6448 | { | |
34a1cd60 TC |
6449 | int r = -ENOMEM, i, msr; |
6450 | ||
6451 | rdmsrl_safe(MSR_EFER, &host_efer); | |
6452 | ||
6453 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) | |
6454 | kvm_define_shared_msr(i, vmx_msr_index[i]); | |
6455 | ||
23611332 RK |
6456 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
6457 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); | |
6458 | if (!vmx_bitmap[i]) | |
6459 | goto out; | |
6460 | } | |
34a1cd60 TC |
6461 | |
6462 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); | |
34a1cd60 TC |
6463 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
6464 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); | |
6465 | ||
6466 | /* | |
6467 | * Allow direct access to the PC debug port (it is often used for I/O | |
6468 | * delays, but the vmexits simply slow things down). | |
6469 | */ | |
6470 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); | |
6471 | clear_bit(0x80, vmx_io_bitmap_a); | |
6472 | ||
6473 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); | |
6474 | ||
6475 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); | |
6476 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); | |
6477 | ||
34a1cd60 TC |
6478 | if (setup_vmcs_config(&vmcs_config) < 0) { |
6479 | r = -EIO; | |
23611332 | 6480 | goto out; |
baa03522 | 6481 | } |
f2c7648d TC |
6482 | |
6483 | if (boot_cpu_has(X86_FEATURE_NX)) | |
6484 | kvm_enable_efer_bits(EFER_NX); | |
6485 | ||
08d839c4 WL |
6486 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
6487 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) | |
f2c7648d | 6488 | enable_vpid = 0; |
08d839c4 | 6489 | |
f2c7648d TC |
6490 | if (!cpu_has_vmx_shadow_vmcs()) |
6491 | enable_shadow_vmcs = 0; | |
6492 | if (enable_shadow_vmcs) | |
6493 | init_vmcs_shadow_fields(); | |
6494 | ||
6495 | if (!cpu_has_vmx_ept() || | |
6496 | !cpu_has_vmx_ept_4levels()) { | |
6497 | enable_ept = 0; | |
6498 | enable_unrestricted_guest = 0; | |
6499 | enable_ept_ad_bits = 0; | |
6500 | } | |
6501 | ||
fce6ac4c | 6502 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
f2c7648d TC |
6503 | enable_ept_ad_bits = 0; |
6504 | ||
6505 | if (!cpu_has_vmx_unrestricted_guest()) | |
6506 | enable_unrestricted_guest = 0; | |
6507 | ||
ad15a296 | 6508 | if (!cpu_has_vmx_flexpriority()) |
f2c7648d TC |
6509 | flexpriority_enabled = 0; |
6510 | ||
ad15a296 PB |
6511 | /* |
6512 | * set_apic_access_page_addr() is used to reload apic access | |
6513 | * page upon invalidation. No need to do anything if not | |
6514 | * using the APIC_ACCESS_ADDR VMCS field. | |
6515 | */ | |
6516 | if (!flexpriority_enabled) | |
f2c7648d | 6517 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
f2c7648d TC |
6518 | |
6519 | if (!cpu_has_vmx_tpr_shadow()) | |
6520 | kvm_x86_ops->update_cr8_intercept = NULL; | |
6521 | ||
6522 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) | |
6523 | kvm_disable_largepages(); | |
6524 | ||
6525 | if (!cpu_has_vmx_ple()) | |
6526 | ple_gap = 0; | |
6527 | ||
76dfafd5 | 6528 | if (!cpu_has_vmx_apicv()) { |
f2c7648d | 6529 | enable_apicv = 0; |
76dfafd5 PB |
6530 | kvm_x86_ops->sync_pir_to_irr = NULL; |
6531 | } | |
f2c7648d | 6532 | |
64903d61 HZ |
6533 | if (cpu_has_vmx_tsc_scaling()) { |
6534 | kvm_has_tsc_control = true; | |
6535 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; | |
6536 | kvm_tsc_scaling_ratio_frac_bits = 48; | |
6537 | } | |
6538 | ||
baa03522 TC |
6539 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
6540 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); | |
6541 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); | |
6542 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); | |
6543 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); | |
6544 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); | |
baa03522 | 6545 | |
c63e4563 | 6546 | memcpy(vmx_msr_bitmap_legacy_x2apic_apicv, |
baa03522 | 6547 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
c63e4563 | 6548 | memcpy(vmx_msr_bitmap_longmode_x2apic_apicv, |
baa03522 | 6549 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
c63e4563 | 6550 | memcpy(vmx_msr_bitmap_legacy_x2apic, |
f6e90f9e | 6551 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
c63e4563 | 6552 | memcpy(vmx_msr_bitmap_longmode_x2apic, |
f6e90f9e | 6553 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
baa03522 | 6554 | |
04bb92e4 WL |
6555 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
6556 | ||
40d8338d RK |
6557 | for (msr = 0x800; msr <= 0x8ff; msr++) { |
6558 | if (msr == 0x839 /* TMCCT */) | |
6559 | continue; | |
2e69f865 | 6560 | vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true); |
40d8338d | 6561 | } |
3ce424e4 | 6562 | |
f6e90f9e | 6563 | /* |
2e69f865 RK |
6564 | * TPR reads and writes can be virtualized even if virtual interrupt |
6565 | * delivery is not in use. | |
f6e90f9e | 6566 | */ |
2e69f865 RK |
6567 | vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true); |
6568 | vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false); | |
3ce424e4 | 6569 | |
3ce424e4 | 6570 | /* EOI */ |
2e69f865 | 6571 | vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true); |
3ce424e4 | 6572 | /* SELF-IPI */ |
2e69f865 | 6573 | vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true); |
baa03522 | 6574 | |
f160c7b7 JS |
6575 | if (enable_ept) |
6576 | vmx_enable_tdp(); | |
6577 | else | |
baa03522 TC |
6578 | kvm_disable_tdp(); |
6579 | ||
6580 | update_ple_window_actual_max(); | |
6581 | ||
843e4330 KH |
6582 | /* |
6583 | * Only enable PML when hardware supports PML feature, and both EPT | |
6584 | * and EPT A/D bit features are enabled -- PML depends on them to work. | |
6585 | */ | |
6586 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) | |
6587 | enable_pml = 0; | |
6588 | ||
6589 | if (!enable_pml) { | |
6590 | kvm_x86_ops->slot_enable_log_dirty = NULL; | |
6591 | kvm_x86_ops->slot_disable_log_dirty = NULL; | |
6592 | kvm_x86_ops->flush_log_dirty = NULL; | |
6593 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; | |
6594 | } | |
6595 | ||
64672c95 YJ |
6596 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
6597 | u64 vmx_msr; | |
6598 | ||
6599 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
6600 | cpu_preemption_timer_multi = | |
6601 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; | |
6602 | } else { | |
6603 | kvm_x86_ops->set_hv_timer = NULL; | |
6604 | kvm_x86_ops->cancel_hv_timer = NULL; | |
6605 | } | |
6606 | ||
bf9f6ac8 FW |
6607 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
6608 | ||
c45dcc71 AR |
6609 | kvm_mce_cap_supported |= MCG_LMCE_P; |
6610 | ||
f2c7648d | 6611 | return alloc_kvm_area(); |
34a1cd60 | 6612 | |
34a1cd60 | 6613 | out: |
23611332 RK |
6614 | for (i = 0; i < VMX_BITMAP_NR; i++) |
6615 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 TC |
6616 | |
6617 | return r; | |
f2c7648d TC |
6618 | } |
6619 | ||
6620 | static __exit void hardware_unsetup(void) | |
6621 | { | |
23611332 RK |
6622 | int i; |
6623 | ||
6624 | for (i = 0; i < VMX_BITMAP_NR; i++) | |
6625 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 | 6626 | |
f2c7648d TC |
6627 | free_kvm_area(); |
6628 | } | |
6629 | ||
4b8d54f9 ZE |
6630 | /* |
6631 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE | |
6632 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. | |
6633 | */ | |
9fb41ba8 | 6634 | static int handle_pause(struct kvm_vcpu *vcpu) |
4b8d54f9 | 6635 | { |
b4a2d31d RK |
6636 | if (ple_gap) |
6637 | grow_ple_window(vcpu); | |
6638 | ||
4b8d54f9 | 6639 | kvm_vcpu_on_spin(vcpu); |
6affcbed | 6640 | return kvm_skip_emulated_instruction(vcpu); |
4b8d54f9 ZE |
6641 | } |
6642 | ||
87c00572 | 6643 | static int handle_nop(struct kvm_vcpu *vcpu) |
59708670 | 6644 | { |
6affcbed | 6645 | return kvm_skip_emulated_instruction(vcpu); |
59708670 SY |
6646 | } |
6647 | ||
87c00572 GS |
6648 | static int handle_mwait(struct kvm_vcpu *vcpu) |
6649 | { | |
6650 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); | |
6651 | return handle_nop(vcpu); | |
6652 | } | |
6653 | ||
5f3d45e7 MD |
6654 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
6655 | { | |
6656 | return 1; | |
6657 | } | |
6658 | ||
87c00572 GS |
6659 | static int handle_monitor(struct kvm_vcpu *vcpu) |
6660 | { | |
6661 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); | |
6662 | return handle_nop(vcpu); | |
6663 | } | |
6664 | ||
ff2f6fe9 NHE |
6665 | /* |
6666 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. | |
6667 | * We could reuse a single VMCS for all the L2 guests, but we also want the | |
6668 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this | |
6669 | * allows keeping them loaded on the processor, and in the future will allow | |
6670 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on | |
6671 | * every entry if they never change. | |
6672 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE | |
6673 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. | |
6674 | * | |
6675 | * The following functions allocate and free a vmcs02 in this pool. | |
6676 | */ | |
6677 | ||
6678 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ | |
6679 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) | |
6680 | { | |
6681 | struct vmcs02_list *item; | |
6682 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) | |
6683 | if (item->vmptr == vmx->nested.current_vmptr) { | |
6684 | list_move(&item->list, &vmx->nested.vmcs02_pool); | |
6685 | return &item->vmcs02; | |
6686 | } | |
6687 | ||
6688 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { | |
6689 | /* Recycle the least recently used VMCS. */ | |
d74c0e6b GT |
6690 | item = list_last_entry(&vmx->nested.vmcs02_pool, |
6691 | struct vmcs02_list, list); | |
ff2f6fe9 NHE |
6692 | item->vmptr = vmx->nested.current_vmptr; |
6693 | list_move(&item->list, &vmx->nested.vmcs02_pool); | |
6694 | return &item->vmcs02; | |
6695 | } | |
6696 | ||
6697 | /* Create a new VMCS */ | |
0fa24ce3 | 6698 | item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
ff2f6fe9 NHE |
6699 | if (!item) |
6700 | return NULL; | |
6701 | item->vmcs02.vmcs = alloc_vmcs(); | |
355f4fb1 | 6702 | item->vmcs02.shadow_vmcs = NULL; |
ff2f6fe9 NHE |
6703 | if (!item->vmcs02.vmcs) { |
6704 | kfree(item); | |
6705 | return NULL; | |
6706 | } | |
6707 | loaded_vmcs_init(&item->vmcs02); | |
6708 | item->vmptr = vmx->nested.current_vmptr; | |
6709 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); | |
6710 | vmx->nested.vmcs02_num++; | |
6711 | return &item->vmcs02; | |
6712 | } | |
6713 | ||
6714 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ | |
6715 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) | |
6716 | { | |
6717 | struct vmcs02_list *item; | |
6718 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) | |
6719 | if (item->vmptr == vmptr) { | |
6720 | free_loaded_vmcs(&item->vmcs02); | |
6721 | list_del(&item->list); | |
6722 | kfree(item); | |
6723 | vmx->nested.vmcs02_num--; | |
6724 | return; | |
6725 | } | |
6726 | } | |
6727 | ||
6728 | /* | |
6729 | * Free all VMCSs saved for this vcpu, except the one pointed by | |
4fa7734c PB |
6730 | * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs |
6731 | * must be &vmx->vmcs01. | |
ff2f6fe9 NHE |
6732 | */ |
6733 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) | |
6734 | { | |
6735 | struct vmcs02_list *item, *n; | |
4fa7734c PB |
6736 | |
6737 | WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01); | |
ff2f6fe9 | 6738 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
4fa7734c PB |
6739 | /* |
6740 | * Something will leak if the above WARN triggers. Better than | |
6741 | * a use-after-free. | |
6742 | */ | |
6743 | if (vmx->loaded_vmcs == &item->vmcs02) | |
6744 | continue; | |
6745 | ||
6746 | free_loaded_vmcs(&item->vmcs02); | |
ff2f6fe9 NHE |
6747 | list_del(&item->list); |
6748 | kfree(item); | |
4fa7734c | 6749 | vmx->nested.vmcs02_num--; |
ff2f6fe9 | 6750 | } |
ff2f6fe9 NHE |
6751 | } |
6752 | ||
0658fbaa ACL |
6753 | /* |
6754 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), | |
6755 | * set the success or error code of an emulated VMX instruction, as specified | |
6756 | * by Vol 2B, VMX Instruction Reference, "Conventions". | |
6757 | */ | |
6758 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) | |
6759 | { | |
6760 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) | |
6761 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
6762 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); | |
6763 | } | |
6764 | ||
6765 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) | |
6766 | { | |
6767 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
6768 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | | |
6769 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
6770 | | X86_EFLAGS_CF); | |
6771 | } | |
6772 | ||
145c28dd | 6773 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
0658fbaa ACL |
6774 | u32 vm_instruction_error) |
6775 | { | |
6776 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { | |
6777 | /* | |
6778 | * failValid writes the error number to the current VMCS, which | |
6779 | * can't be done there isn't a current VMCS. | |
6780 | */ | |
6781 | nested_vmx_failInvalid(vcpu); | |
6782 | return; | |
6783 | } | |
6784 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
6785 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
6786 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
6787 | | X86_EFLAGS_ZF); | |
6788 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; | |
6789 | /* | |
6790 | * We don't need to force a shadow sync because | |
6791 | * VM_INSTRUCTION_ERROR is not shadowed | |
6792 | */ | |
6793 | } | |
145c28dd | 6794 | |
ff651cb6 WV |
6795 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
6796 | { | |
6797 | /* TODO: not to reset guest simply here. */ | |
6798 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); | |
bbe41b95 | 6799 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
ff651cb6 WV |
6800 | } |
6801 | ||
f4124500 JK |
6802 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
6803 | { | |
6804 | struct vcpu_vmx *vmx = | |
6805 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); | |
6806 | ||
6807 | vmx->nested.preemption_timer_expired = true; | |
6808 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); | |
6809 | kvm_vcpu_kick(&vmx->vcpu); | |
6810 | ||
6811 | return HRTIMER_NORESTART; | |
6812 | } | |
6813 | ||
19677e32 BD |
6814 | /* |
6815 | * Decode the memory-address operand of a vmx instruction, as recorded on an | |
6816 | * exit caused by such an instruction (run by a guest hypervisor). | |
6817 | * On success, returns 0. When the operand is invalid, returns 1 and throws | |
6818 | * #UD or #GP. | |
6819 | */ | |
6820 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, | |
6821 | unsigned long exit_qualification, | |
f9eb4af6 | 6822 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
19677e32 | 6823 | { |
f9eb4af6 EK |
6824 | gva_t off; |
6825 | bool exn; | |
6826 | struct kvm_segment s; | |
6827 | ||
19677e32 BD |
6828 | /* |
6829 | * According to Vol. 3B, "Information for VM Exits Due to Instruction | |
6830 | * Execution", on an exit, vmx_instruction_info holds most of the | |
6831 | * addressing components of the operand. Only the displacement part | |
6832 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). | |
6833 | * For how an actual address is calculated from all these components, | |
6834 | * refer to Vol. 1, "Operand Addressing". | |
6835 | */ | |
6836 | int scaling = vmx_instruction_info & 3; | |
6837 | int addr_size = (vmx_instruction_info >> 7) & 7; | |
6838 | bool is_reg = vmx_instruction_info & (1u << 10); | |
6839 | int seg_reg = (vmx_instruction_info >> 15) & 7; | |
6840 | int index_reg = (vmx_instruction_info >> 18) & 0xf; | |
6841 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); | |
6842 | int base_reg = (vmx_instruction_info >> 23) & 0xf; | |
6843 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); | |
6844 | ||
6845 | if (is_reg) { | |
6846 | kvm_queue_exception(vcpu, UD_VECTOR); | |
6847 | return 1; | |
6848 | } | |
6849 | ||
6850 | /* Addr = segment_base + offset */ | |
6851 | /* offset = base + [index * scale] + displacement */ | |
f9eb4af6 | 6852 | off = exit_qualification; /* holds the displacement */ |
19677e32 | 6853 | if (base_is_valid) |
f9eb4af6 | 6854 | off += kvm_register_read(vcpu, base_reg); |
19677e32 | 6855 | if (index_is_valid) |
f9eb4af6 EK |
6856 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
6857 | vmx_get_segment(vcpu, &s, seg_reg); | |
6858 | *ret = s.base + off; | |
19677e32 BD |
6859 | |
6860 | if (addr_size == 1) /* 32 bit */ | |
6861 | *ret &= 0xffffffff; | |
6862 | ||
f9eb4af6 EK |
6863 | /* Checks for #GP/#SS exceptions. */ |
6864 | exn = false; | |
ff30ef40 QC |
6865 | if (is_long_mode(vcpu)) { |
6866 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a | |
6867 | * non-canonical form. This is the only check on the memory | |
6868 | * destination for long mode! | |
6869 | */ | |
6870 | exn = is_noncanonical_address(*ret); | |
6871 | } else if (is_protmode(vcpu)) { | |
f9eb4af6 EK |
6872 | /* Protected mode: apply checks for segment validity in the |
6873 | * following order: | |
6874 | * - segment type check (#GP(0) may be thrown) | |
6875 | * - usability check (#GP(0)/#SS(0)) | |
6876 | * - limit check (#GP(0)/#SS(0)) | |
6877 | */ | |
6878 | if (wr) | |
6879 | /* #GP(0) if the destination operand is located in a | |
6880 | * read-only data segment or any code segment. | |
6881 | */ | |
6882 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); | |
6883 | else | |
6884 | /* #GP(0) if the source operand is located in an | |
6885 | * execute-only code segment | |
6886 | */ | |
6887 | exn = ((s.type & 0xa) == 8); | |
ff30ef40 QC |
6888 | if (exn) { |
6889 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
6890 | return 1; | |
6891 | } | |
f9eb4af6 EK |
6892 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
6893 | */ | |
6894 | exn = (s.unusable != 0); | |
6895 | /* Protected mode: #GP(0)/#SS(0) if the memory | |
6896 | * operand is outside the segment limit. | |
6897 | */ | |
6898 | exn = exn || (off + sizeof(u64) > s.limit); | |
6899 | } | |
6900 | if (exn) { | |
6901 | kvm_queue_exception_e(vcpu, | |
6902 | seg_reg == VCPU_SREG_SS ? | |
6903 | SS_VECTOR : GP_VECTOR, | |
6904 | 0); | |
6905 | return 1; | |
6906 | } | |
6907 | ||
19677e32 BD |
6908 | return 0; |
6909 | } | |
6910 | ||
cbf71279 | 6911 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer) |
3573e22c BD |
6912 | { |
6913 | gva_t gva; | |
3573e22c | 6914 | struct x86_exception e; |
3573e22c BD |
6915 | |
6916 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 6917 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
3573e22c BD |
6918 | return 1; |
6919 | ||
cbf71279 RK |
6920 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer, |
6921 | sizeof(*vmpointer), &e)) { | |
3573e22c BD |
6922 | kvm_inject_page_fault(vcpu, &e); |
6923 | return 1; | |
6924 | } | |
6925 | ||
3573e22c BD |
6926 | return 0; |
6927 | } | |
6928 | ||
e29acc55 JM |
6929 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
6930 | { | |
6931 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6932 | struct vmcs *shadow_vmcs; | |
6933 | ||
6934 | if (cpu_has_vmx_msr_bitmap()) { | |
6935 | vmx->nested.msr_bitmap = | |
6936 | (unsigned long *)__get_free_page(GFP_KERNEL); | |
6937 | if (!vmx->nested.msr_bitmap) | |
6938 | goto out_msr_bitmap; | |
6939 | } | |
6940 | ||
6941 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); | |
6942 | if (!vmx->nested.cached_vmcs12) | |
6943 | goto out_cached_vmcs12; | |
6944 | ||
6945 | if (enable_shadow_vmcs) { | |
6946 | shadow_vmcs = alloc_vmcs(); | |
6947 | if (!shadow_vmcs) | |
6948 | goto out_shadow_vmcs; | |
6949 | /* mark vmcs as shadow */ | |
6950 | shadow_vmcs->revision_id |= (1u << 31); | |
6951 | /* init shadow vmcs */ | |
6952 | vmcs_clear(shadow_vmcs); | |
6953 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; | |
6954 | } | |
6955 | ||
6956 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); | |
6957 | vmx->nested.vmcs02_num = 0; | |
6958 | ||
6959 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, | |
6960 | HRTIMER_MODE_REL_PINNED); | |
6961 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; | |
6962 | ||
6963 | vmx->nested.vmxon = true; | |
6964 | return 0; | |
6965 | ||
6966 | out_shadow_vmcs: | |
6967 | kfree(vmx->nested.cached_vmcs12); | |
6968 | ||
6969 | out_cached_vmcs12: | |
6970 | free_page((unsigned long)vmx->nested.msr_bitmap); | |
6971 | ||
6972 | out_msr_bitmap: | |
6973 | return -ENOMEM; | |
6974 | } | |
6975 | ||
ec378aee NHE |
6976 | /* |
6977 | * Emulate the VMXON instruction. | |
6978 | * Currently, we just remember that VMX is active, and do not save or even | |
6979 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we | |
6980 | * do not currently need to store anything in that guest-allocated memory | |
6981 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their | |
6982 | * argument is different from the VMXON pointer (which the spec says they do). | |
6983 | */ | |
6984 | static int handle_vmon(struct kvm_vcpu *vcpu) | |
6985 | { | |
e29acc55 | 6986 | int ret; |
cbf71279 RK |
6987 | gpa_t vmptr; |
6988 | struct page *page; | |
ec378aee | 6989 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b3897a49 NHE |
6990 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
6991 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
ec378aee | 6992 | |
70f3aac9 JM |
6993 | /* |
6994 | * The Intel VMX Instruction Reference lists a bunch of bits that are | |
6995 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to | |
6996 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). | |
6997 | * Otherwise, we should fail with #UD. But most faulting conditions | |
6998 | * have already been checked by hardware, prior to the VM-exit for | |
6999 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has | |
7000 | * that bit set to 1 in non-root mode. | |
ec378aee | 7001 | */ |
70f3aac9 | 7002 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
ec378aee NHE |
7003 | kvm_queue_exception(vcpu, UD_VECTOR); |
7004 | return 1; | |
7005 | } | |
7006 | ||
145c28dd AG |
7007 | if (vmx->nested.vmxon) { |
7008 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); | |
6affcbed | 7009 | return kvm_skip_emulated_instruction(vcpu); |
145c28dd | 7010 | } |
b3897a49 | 7011 | |
3b84080b | 7012 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
b3897a49 NHE |
7013 | != VMXON_NEEDED_FEATURES) { |
7014 | kvm_inject_gp(vcpu, 0); | |
7015 | return 1; | |
7016 | } | |
7017 | ||
cbf71279 | 7018 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
21e7fbe7 | 7019 | return 1; |
cbf71279 RK |
7020 | |
7021 | /* | |
7022 | * SDM 3: 24.11.5 | |
7023 | * The first 4 bytes of VMXON region contain the supported | |
7024 | * VMCS revision identifier | |
7025 | * | |
7026 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; | |
7027 | * which replaces physical address width with 32 | |
7028 | */ | |
7029 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { | |
7030 | nested_vmx_failInvalid(vcpu); | |
7031 | return kvm_skip_emulated_instruction(vcpu); | |
7032 | } | |
7033 | ||
7034 | page = nested_get_page(vcpu, vmptr); | |
7035 | if (page == NULL) { | |
7036 | nested_vmx_failInvalid(vcpu); | |
7037 | return kvm_skip_emulated_instruction(vcpu); | |
7038 | } | |
7039 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { | |
7040 | kunmap(page); | |
7041 | nested_release_page_clean(page); | |
7042 | nested_vmx_failInvalid(vcpu); | |
7043 | return kvm_skip_emulated_instruction(vcpu); | |
7044 | } | |
7045 | kunmap(page); | |
7046 | nested_release_page_clean(page); | |
7047 | ||
7048 | vmx->nested.vmxon_ptr = vmptr; | |
e29acc55 JM |
7049 | ret = enter_vmx_operation(vcpu); |
7050 | if (ret) | |
7051 | return ret; | |
ec378aee | 7052 | |
a25eb114 | 7053 | nested_vmx_succeed(vcpu); |
6affcbed | 7054 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7055 | } |
7056 | ||
7057 | /* | |
7058 | * Intel's VMX Instruction Reference specifies a common set of prerequisites | |
7059 | * for running VMX instructions (except VMXON, whose prerequisites are | |
7060 | * slightly different). It also specifies what exception to inject otherwise. | |
70f3aac9 JM |
7061 | * Note that many of these exceptions have priority over VM exits, so they |
7062 | * don't have to be checked again here. | |
ec378aee NHE |
7063 | */ |
7064 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) | |
7065 | { | |
70f3aac9 | 7066 | if (!to_vmx(vcpu)->nested.vmxon) { |
ec378aee NHE |
7067 | kvm_queue_exception(vcpu, UD_VECTOR); |
7068 | return 0; | |
7069 | } | |
ec378aee NHE |
7070 | return 1; |
7071 | } | |
7072 | ||
e7953d7f AG |
7073 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
7074 | { | |
9a2a05b9 PB |
7075 | if (vmx->nested.current_vmptr == -1ull) |
7076 | return; | |
7077 | ||
7078 | /* current_vmptr and current_vmcs12 are always set/reset together */ | |
7079 | if (WARN_ON(vmx->nested.current_vmcs12 == NULL)) | |
7080 | return; | |
7081 | ||
012f83cb | 7082 | if (enable_shadow_vmcs) { |
9a2a05b9 PB |
7083 | /* copy to memory all shadowed fields in case |
7084 | they were modified */ | |
7085 | copy_shadow_to_vmcs12(vmx); | |
7086 | vmx->nested.sync_shadow_vmcs = false; | |
7ec36296 XG |
7087 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
7088 | SECONDARY_EXEC_SHADOW_VMCS); | |
9a2a05b9 | 7089 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
012f83cb | 7090 | } |
705699a1 | 7091 | vmx->nested.posted_intr_nv = -1; |
4f2777bc DM |
7092 | |
7093 | /* Flush VMCS12 to guest memory */ | |
7094 | memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12, | |
7095 | VMCS12_SIZE); | |
7096 | ||
e7953d7f AG |
7097 | kunmap(vmx->nested.current_vmcs12_page); |
7098 | nested_release_page(vmx->nested.current_vmcs12_page); | |
9a2a05b9 PB |
7099 | vmx->nested.current_vmptr = -1ull; |
7100 | vmx->nested.current_vmcs12 = NULL; | |
e7953d7f AG |
7101 | } |
7102 | ||
ec378aee NHE |
7103 | /* |
7104 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or | |
7105 | * just stops using VMX. | |
7106 | */ | |
7107 | static void free_nested(struct vcpu_vmx *vmx) | |
7108 | { | |
7109 | if (!vmx->nested.vmxon) | |
7110 | return; | |
9a2a05b9 | 7111 | |
ec378aee | 7112 | vmx->nested.vmxon = false; |
5c614b35 | 7113 | free_vpid(vmx->nested.vpid02); |
9a2a05b9 | 7114 | nested_release_vmcs12(vmx); |
d048c098 RK |
7115 | if (vmx->nested.msr_bitmap) { |
7116 | free_page((unsigned long)vmx->nested.msr_bitmap); | |
7117 | vmx->nested.msr_bitmap = NULL; | |
7118 | } | |
355f4fb1 JM |
7119 | if (enable_shadow_vmcs) { |
7120 | vmcs_clear(vmx->vmcs01.shadow_vmcs); | |
7121 | free_vmcs(vmx->vmcs01.shadow_vmcs); | |
7122 | vmx->vmcs01.shadow_vmcs = NULL; | |
7123 | } | |
4f2777bc | 7124 | kfree(vmx->nested.cached_vmcs12); |
fe3ef05c NHE |
7125 | /* Unpin physical memory we referred to in current vmcs02 */ |
7126 | if (vmx->nested.apic_access_page) { | |
7127 | nested_release_page(vmx->nested.apic_access_page); | |
48d89b92 | 7128 | vmx->nested.apic_access_page = NULL; |
fe3ef05c | 7129 | } |
a7c0b07d WL |
7130 | if (vmx->nested.virtual_apic_page) { |
7131 | nested_release_page(vmx->nested.virtual_apic_page); | |
48d89b92 | 7132 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 7133 | } |
705699a1 WV |
7134 | if (vmx->nested.pi_desc_page) { |
7135 | kunmap(vmx->nested.pi_desc_page); | |
7136 | nested_release_page(vmx->nested.pi_desc_page); | |
7137 | vmx->nested.pi_desc_page = NULL; | |
7138 | vmx->nested.pi_desc = NULL; | |
7139 | } | |
ff2f6fe9 NHE |
7140 | |
7141 | nested_free_all_saved_vmcss(vmx); | |
ec378aee NHE |
7142 | } |
7143 | ||
7144 | /* Emulate the VMXOFF instruction */ | |
7145 | static int handle_vmoff(struct kvm_vcpu *vcpu) | |
7146 | { | |
7147 | if (!nested_vmx_check_permission(vcpu)) | |
7148 | return 1; | |
7149 | free_nested(to_vmx(vcpu)); | |
a25eb114 | 7150 | nested_vmx_succeed(vcpu); |
6affcbed | 7151 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7152 | } |
7153 | ||
27d6c865 NHE |
7154 | /* Emulate the VMCLEAR instruction */ |
7155 | static int handle_vmclear(struct kvm_vcpu *vcpu) | |
7156 | { | |
7157 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
587d7e72 | 7158 | u32 zero = 0; |
27d6c865 | 7159 | gpa_t vmptr; |
27d6c865 NHE |
7160 | |
7161 | if (!nested_vmx_check_permission(vcpu)) | |
7162 | return 1; | |
7163 | ||
cbf71279 | 7164 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
27d6c865 | 7165 | return 1; |
27d6c865 | 7166 | |
cbf71279 RK |
7167 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
7168 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); | |
7169 | return kvm_skip_emulated_instruction(vcpu); | |
7170 | } | |
7171 | ||
7172 | if (vmptr == vmx->nested.vmxon_ptr) { | |
7173 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); | |
7174 | return kvm_skip_emulated_instruction(vcpu); | |
7175 | } | |
7176 | ||
9a2a05b9 | 7177 | if (vmptr == vmx->nested.current_vmptr) |
e7953d7f | 7178 | nested_release_vmcs12(vmx); |
27d6c865 | 7179 | |
587d7e72 JM |
7180 | kvm_vcpu_write_guest(vcpu, |
7181 | vmptr + offsetof(struct vmcs12, launch_state), | |
7182 | &zero, sizeof(zero)); | |
27d6c865 NHE |
7183 | |
7184 | nested_free_vmcs02(vmx, vmptr); | |
7185 | ||
27d6c865 | 7186 | nested_vmx_succeed(vcpu); |
6affcbed | 7187 | return kvm_skip_emulated_instruction(vcpu); |
27d6c865 NHE |
7188 | } |
7189 | ||
cd232ad0 NHE |
7190 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
7191 | ||
7192 | /* Emulate the VMLAUNCH instruction */ | |
7193 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) | |
7194 | { | |
7195 | return nested_vmx_run(vcpu, true); | |
7196 | } | |
7197 | ||
7198 | /* Emulate the VMRESUME instruction */ | |
7199 | static int handle_vmresume(struct kvm_vcpu *vcpu) | |
7200 | { | |
7201 | ||
7202 | return nested_vmx_run(vcpu, false); | |
7203 | } | |
7204 | ||
49f705c5 NHE |
7205 | enum vmcs_field_type { |
7206 | VMCS_FIELD_TYPE_U16 = 0, | |
7207 | VMCS_FIELD_TYPE_U64 = 1, | |
7208 | VMCS_FIELD_TYPE_U32 = 2, | |
7209 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 | |
7210 | }; | |
7211 | ||
7212 | static inline int vmcs_field_type(unsigned long field) | |
7213 | { | |
7214 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ | |
7215 | return VMCS_FIELD_TYPE_U32; | |
7216 | return (field >> 13) & 0x3 ; | |
7217 | } | |
7218 | ||
7219 | static inline int vmcs_field_readonly(unsigned long field) | |
7220 | { | |
7221 | return (((field >> 10) & 0x3) == 1); | |
7222 | } | |
7223 | ||
7224 | /* | |
7225 | * Read a vmcs12 field. Since these can have varying lengths and we return | |
7226 | * one type, we chose the biggest type (u64) and zero-extend the return value | |
7227 | * to that size. Note that the caller, handle_vmread, might need to use only | |
7228 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of | |
7229 | * 64-bit fields are to be returned). | |
7230 | */ | |
a2ae9df7 PB |
7231 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
7232 | unsigned long field, u64 *ret) | |
49f705c5 NHE |
7233 | { |
7234 | short offset = vmcs_field_to_offset(field); | |
7235 | char *p; | |
7236 | ||
7237 | if (offset < 0) | |
a2ae9df7 | 7238 | return offset; |
49f705c5 NHE |
7239 | |
7240 | p = ((char *)(get_vmcs12(vcpu))) + offset; | |
7241 | ||
7242 | switch (vmcs_field_type(field)) { | |
7243 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7244 | *ret = *((natural_width *)p); | |
a2ae9df7 | 7245 | return 0; |
49f705c5 NHE |
7246 | case VMCS_FIELD_TYPE_U16: |
7247 | *ret = *((u16 *)p); | |
a2ae9df7 | 7248 | return 0; |
49f705c5 NHE |
7249 | case VMCS_FIELD_TYPE_U32: |
7250 | *ret = *((u32 *)p); | |
a2ae9df7 | 7251 | return 0; |
49f705c5 NHE |
7252 | case VMCS_FIELD_TYPE_U64: |
7253 | *ret = *((u64 *)p); | |
a2ae9df7 | 7254 | return 0; |
49f705c5 | 7255 | default: |
a2ae9df7 PB |
7256 | WARN_ON(1); |
7257 | return -ENOENT; | |
49f705c5 NHE |
7258 | } |
7259 | } | |
7260 | ||
20b97fea | 7261 | |
a2ae9df7 PB |
7262 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
7263 | unsigned long field, u64 field_value){ | |
20b97fea AG |
7264 | short offset = vmcs_field_to_offset(field); |
7265 | char *p = ((char *) get_vmcs12(vcpu)) + offset; | |
7266 | if (offset < 0) | |
a2ae9df7 | 7267 | return offset; |
20b97fea AG |
7268 | |
7269 | switch (vmcs_field_type(field)) { | |
7270 | case VMCS_FIELD_TYPE_U16: | |
7271 | *(u16 *)p = field_value; | |
a2ae9df7 | 7272 | return 0; |
20b97fea AG |
7273 | case VMCS_FIELD_TYPE_U32: |
7274 | *(u32 *)p = field_value; | |
a2ae9df7 | 7275 | return 0; |
20b97fea AG |
7276 | case VMCS_FIELD_TYPE_U64: |
7277 | *(u64 *)p = field_value; | |
a2ae9df7 | 7278 | return 0; |
20b97fea AG |
7279 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
7280 | *(natural_width *)p = field_value; | |
a2ae9df7 | 7281 | return 0; |
20b97fea | 7282 | default: |
a2ae9df7 PB |
7283 | WARN_ON(1); |
7284 | return -ENOENT; | |
20b97fea AG |
7285 | } |
7286 | ||
7287 | } | |
7288 | ||
16f5b903 AG |
7289 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
7290 | { | |
7291 | int i; | |
7292 | unsigned long field; | |
7293 | u64 field_value; | |
355f4fb1 | 7294 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
c2bae893 MK |
7295 | const unsigned long *fields = shadow_read_write_fields; |
7296 | const int num_fields = max_shadow_read_write_fields; | |
16f5b903 | 7297 | |
282da870 JK |
7298 | preempt_disable(); |
7299 | ||
16f5b903 AG |
7300 | vmcs_load(shadow_vmcs); |
7301 | ||
7302 | for (i = 0; i < num_fields; i++) { | |
7303 | field = fields[i]; | |
7304 | switch (vmcs_field_type(field)) { | |
7305 | case VMCS_FIELD_TYPE_U16: | |
7306 | field_value = vmcs_read16(field); | |
7307 | break; | |
7308 | case VMCS_FIELD_TYPE_U32: | |
7309 | field_value = vmcs_read32(field); | |
7310 | break; | |
7311 | case VMCS_FIELD_TYPE_U64: | |
7312 | field_value = vmcs_read64(field); | |
7313 | break; | |
7314 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7315 | field_value = vmcs_readl(field); | |
7316 | break; | |
a2ae9df7 PB |
7317 | default: |
7318 | WARN_ON(1); | |
7319 | continue; | |
16f5b903 AG |
7320 | } |
7321 | vmcs12_write_any(&vmx->vcpu, field, field_value); | |
7322 | } | |
7323 | ||
7324 | vmcs_clear(shadow_vmcs); | |
7325 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
282da870 JK |
7326 | |
7327 | preempt_enable(); | |
16f5b903 AG |
7328 | } |
7329 | ||
c3114420 AG |
7330 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
7331 | { | |
c2bae893 MK |
7332 | const unsigned long *fields[] = { |
7333 | shadow_read_write_fields, | |
7334 | shadow_read_only_fields | |
c3114420 | 7335 | }; |
c2bae893 | 7336 | const int max_fields[] = { |
c3114420 AG |
7337 | max_shadow_read_write_fields, |
7338 | max_shadow_read_only_fields | |
7339 | }; | |
7340 | int i, q; | |
7341 | unsigned long field; | |
7342 | u64 field_value = 0; | |
355f4fb1 | 7343 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
c3114420 AG |
7344 | |
7345 | vmcs_load(shadow_vmcs); | |
7346 | ||
c2bae893 | 7347 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
c3114420 AG |
7348 | for (i = 0; i < max_fields[q]; i++) { |
7349 | field = fields[q][i]; | |
7350 | vmcs12_read_any(&vmx->vcpu, field, &field_value); | |
7351 | ||
7352 | switch (vmcs_field_type(field)) { | |
7353 | case VMCS_FIELD_TYPE_U16: | |
7354 | vmcs_write16(field, (u16)field_value); | |
7355 | break; | |
7356 | case VMCS_FIELD_TYPE_U32: | |
7357 | vmcs_write32(field, (u32)field_value); | |
7358 | break; | |
7359 | case VMCS_FIELD_TYPE_U64: | |
7360 | vmcs_write64(field, (u64)field_value); | |
7361 | break; | |
7362 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7363 | vmcs_writel(field, (long)field_value); | |
7364 | break; | |
a2ae9df7 PB |
7365 | default: |
7366 | WARN_ON(1); | |
7367 | break; | |
c3114420 AG |
7368 | } |
7369 | } | |
7370 | } | |
7371 | ||
7372 | vmcs_clear(shadow_vmcs); | |
7373 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
7374 | } | |
7375 | ||
49f705c5 NHE |
7376 | /* |
7377 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was | |
7378 | * used before) all generate the same failure when it is missing. | |
7379 | */ | |
7380 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) | |
7381 | { | |
7382 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7383 | if (vmx->nested.current_vmptr == -1ull) { | |
7384 | nested_vmx_failInvalid(vcpu); | |
49f705c5 NHE |
7385 | return 0; |
7386 | } | |
7387 | return 1; | |
7388 | } | |
7389 | ||
7390 | static int handle_vmread(struct kvm_vcpu *vcpu) | |
7391 | { | |
7392 | unsigned long field; | |
7393 | u64 field_value; | |
7394 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7395 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7396 | gva_t gva = 0; | |
7397 | ||
eb277562 | 7398 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
7399 | return 1; |
7400 | ||
6affcbed KH |
7401 | if (!nested_vmx_check_vmcs12(vcpu)) |
7402 | return kvm_skip_emulated_instruction(vcpu); | |
49f705c5 NHE |
7403 | |
7404 | /* Decode instruction info and find the field to read */ | |
27e6fb5d | 7405 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 | 7406 | /* Read the field, zero-extended to a u64 field_value */ |
a2ae9df7 | 7407 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
49f705c5 | 7408 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 7409 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7410 | } |
7411 | /* | |
7412 | * Now copy part of this value to register or memory, as requested. | |
7413 | * Note that the number of bits actually copied is 32 or 64 depending | |
7414 | * on the guest's mode (32 or 64 bit), not on the given field's length. | |
7415 | */ | |
7416 | if (vmx_instruction_info & (1u << 10)) { | |
27e6fb5d | 7417 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
49f705c5 NHE |
7418 | field_value); |
7419 | } else { | |
7420 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 7421 | vmx_instruction_info, true, &gva)) |
49f705c5 | 7422 | return 1; |
70f3aac9 | 7423 | /* _system ok, as hardware has verified cpl=0 */ |
49f705c5 NHE |
7424 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
7425 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); | |
7426 | } | |
7427 | ||
7428 | nested_vmx_succeed(vcpu); | |
6affcbed | 7429 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7430 | } |
7431 | ||
7432 | ||
7433 | static int handle_vmwrite(struct kvm_vcpu *vcpu) | |
7434 | { | |
7435 | unsigned long field; | |
7436 | gva_t gva; | |
7437 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7438 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
49f705c5 NHE |
7439 | /* The value to write might be 32 or 64 bits, depending on L1's long |
7440 | * mode, and eventually we need to write that into a field of several | |
7441 | * possible lengths. The code below first zero-extends the value to 64 | |
6a6256f9 | 7442 | * bit (field_value), and then copies only the appropriate number of |
49f705c5 NHE |
7443 | * bits into the vmcs12 field. |
7444 | */ | |
7445 | u64 field_value = 0; | |
7446 | struct x86_exception e; | |
7447 | ||
eb277562 | 7448 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
7449 | return 1; |
7450 | ||
6affcbed KH |
7451 | if (!nested_vmx_check_vmcs12(vcpu)) |
7452 | return kvm_skip_emulated_instruction(vcpu); | |
eb277562 | 7453 | |
49f705c5 | 7454 | if (vmx_instruction_info & (1u << 10)) |
27e6fb5d | 7455 | field_value = kvm_register_readl(vcpu, |
49f705c5 NHE |
7456 | (((vmx_instruction_info) >> 3) & 0xf)); |
7457 | else { | |
7458 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 7459 | vmx_instruction_info, false, &gva)) |
49f705c5 NHE |
7460 | return 1; |
7461 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, | |
27e6fb5d | 7462 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
49f705c5 NHE |
7463 | kvm_inject_page_fault(vcpu, &e); |
7464 | return 1; | |
7465 | } | |
7466 | } | |
7467 | ||
7468 | ||
27e6fb5d | 7469 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 NHE |
7470 | if (vmcs_field_readonly(field)) { |
7471 | nested_vmx_failValid(vcpu, | |
7472 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); | |
6affcbed | 7473 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7474 | } |
7475 | ||
a2ae9df7 | 7476 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
49f705c5 | 7477 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 7478 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7479 | } |
7480 | ||
7481 | nested_vmx_succeed(vcpu); | |
6affcbed | 7482 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
7483 | } |
7484 | ||
a8bc284e JM |
7485 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
7486 | { | |
7487 | vmx->nested.current_vmptr = vmptr; | |
7488 | if (enable_shadow_vmcs) { | |
7489 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
7490 | SECONDARY_EXEC_SHADOW_VMCS); | |
7491 | vmcs_write64(VMCS_LINK_POINTER, | |
7492 | __pa(vmx->vmcs01.shadow_vmcs)); | |
7493 | vmx->nested.sync_shadow_vmcs = true; | |
7494 | } | |
7495 | } | |
7496 | ||
63846663 NHE |
7497 | /* Emulate the VMPTRLD instruction */ |
7498 | static int handle_vmptrld(struct kvm_vcpu *vcpu) | |
7499 | { | |
7500 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
63846663 | 7501 | gpa_t vmptr; |
63846663 NHE |
7502 | |
7503 | if (!nested_vmx_check_permission(vcpu)) | |
7504 | return 1; | |
7505 | ||
cbf71279 | 7506 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
63846663 | 7507 | return 1; |
63846663 | 7508 | |
cbf71279 RK |
7509 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
7510 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); | |
7511 | return kvm_skip_emulated_instruction(vcpu); | |
7512 | } | |
7513 | ||
7514 | if (vmptr == vmx->nested.vmxon_ptr) { | |
7515 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); | |
7516 | return kvm_skip_emulated_instruction(vcpu); | |
7517 | } | |
7518 | ||
63846663 NHE |
7519 | if (vmx->nested.current_vmptr != vmptr) { |
7520 | struct vmcs12 *new_vmcs12; | |
7521 | struct page *page; | |
7522 | page = nested_get_page(vcpu, vmptr); | |
7523 | if (page == NULL) { | |
7524 | nested_vmx_failInvalid(vcpu); | |
6affcbed | 7525 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
7526 | } |
7527 | new_vmcs12 = kmap(page); | |
7528 | if (new_vmcs12->revision_id != VMCS12_REVISION) { | |
7529 | kunmap(page); | |
7530 | nested_release_page_clean(page); | |
7531 | nested_vmx_failValid(vcpu, | |
7532 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); | |
6affcbed | 7533 | return kvm_skip_emulated_instruction(vcpu); |
63846663 | 7534 | } |
63846663 | 7535 | |
9a2a05b9 | 7536 | nested_release_vmcs12(vmx); |
63846663 NHE |
7537 | vmx->nested.current_vmcs12 = new_vmcs12; |
7538 | vmx->nested.current_vmcs12_page = page; | |
4f2777bc DM |
7539 | /* |
7540 | * Load VMCS12 from guest memory since it is not already | |
7541 | * cached. | |
7542 | */ | |
7543 | memcpy(vmx->nested.cached_vmcs12, | |
7544 | vmx->nested.current_vmcs12, VMCS12_SIZE); | |
a8bc284e | 7545 | set_current_vmptr(vmx, vmptr); |
63846663 NHE |
7546 | } |
7547 | ||
7548 | nested_vmx_succeed(vcpu); | |
6affcbed | 7549 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
7550 | } |
7551 | ||
6a4d7550 NHE |
7552 | /* Emulate the VMPTRST instruction */ |
7553 | static int handle_vmptrst(struct kvm_vcpu *vcpu) | |
7554 | { | |
7555 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7556 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7557 | gva_t vmcs_gva; | |
7558 | struct x86_exception e; | |
7559 | ||
7560 | if (!nested_vmx_check_permission(vcpu)) | |
7561 | return 1; | |
7562 | ||
7563 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 7564 | vmx_instruction_info, true, &vmcs_gva)) |
6a4d7550 | 7565 | return 1; |
70f3aac9 | 7566 | /* ok to use *_system, as hardware has verified cpl=0 */ |
6a4d7550 NHE |
7567 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
7568 | (void *)&to_vmx(vcpu)->nested.current_vmptr, | |
7569 | sizeof(u64), &e)) { | |
7570 | kvm_inject_page_fault(vcpu, &e); | |
7571 | return 1; | |
7572 | } | |
7573 | nested_vmx_succeed(vcpu); | |
6affcbed | 7574 | return kvm_skip_emulated_instruction(vcpu); |
6a4d7550 NHE |
7575 | } |
7576 | ||
bfd0a56b NHE |
7577 | /* Emulate the INVEPT instruction */ |
7578 | static int handle_invept(struct kvm_vcpu *vcpu) | |
7579 | { | |
b9c237bb | 7580 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
bfd0a56b NHE |
7581 | u32 vmx_instruction_info, types; |
7582 | unsigned long type; | |
7583 | gva_t gva; | |
7584 | struct x86_exception e; | |
7585 | struct { | |
7586 | u64 eptp, gpa; | |
7587 | } operand; | |
bfd0a56b | 7588 | |
b9c237bb WV |
7589 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
7590 | SECONDARY_EXEC_ENABLE_EPT) || | |
7591 | !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { | |
bfd0a56b NHE |
7592 | kvm_queue_exception(vcpu, UD_VECTOR); |
7593 | return 1; | |
7594 | } | |
7595 | ||
7596 | if (!nested_vmx_check_permission(vcpu)) | |
7597 | return 1; | |
7598 | ||
bfd0a56b | 7599 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
27e6fb5d | 7600 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
bfd0a56b | 7601 | |
b9c237bb | 7602 | types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
bfd0a56b | 7603 | |
85c856b3 | 7604 | if (type >= 32 || !(types & (1 << type))) { |
bfd0a56b NHE |
7605 | nested_vmx_failValid(vcpu, |
7606 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 7607 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
7608 | } |
7609 | ||
7610 | /* According to the Intel VMX instruction reference, the memory | |
7611 | * operand is read even if it isn't needed (e.g., for type==global) | |
7612 | */ | |
7613 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 7614 | vmx_instruction_info, false, &gva)) |
bfd0a56b NHE |
7615 | return 1; |
7616 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, | |
7617 | sizeof(operand), &e)) { | |
7618 | kvm_inject_page_fault(vcpu, &e); | |
7619 | return 1; | |
7620 | } | |
7621 | ||
7622 | switch (type) { | |
bfd0a56b | 7623 | case VMX_EPT_EXTENT_GLOBAL: |
45e11817 BD |
7624 | /* |
7625 | * TODO: track mappings and invalidate | |
7626 | * single context requests appropriately | |
7627 | */ | |
7628 | case VMX_EPT_EXTENT_CONTEXT: | |
bfd0a56b | 7629 | kvm_mmu_sync_roots(vcpu); |
77c3913b | 7630 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
bfd0a56b NHE |
7631 | nested_vmx_succeed(vcpu); |
7632 | break; | |
7633 | default: | |
7634 | BUG_ON(1); | |
7635 | break; | |
7636 | } | |
7637 | ||
6affcbed | 7638 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
7639 | } |
7640 | ||
a642fc30 PM |
7641 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
7642 | { | |
99b83ac8 WL |
7643 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
7644 | u32 vmx_instruction_info; | |
7645 | unsigned long type, types; | |
7646 | gva_t gva; | |
7647 | struct x86_exception e; | |
40352605 JM |
7648 | struct { |
7649 | u64 vpid; | |
7650 | u64 gla; | |
7651 | } operand; | |
99b83ac8 WL |
7652 | |
7653 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & | |
7654 | SECONDARY_EXEC_ENABLE_VPID) || | |
7655 | !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) { | |
7656 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7657 | return 1; | |
7658 | } | |
7659 | ||
7660 | if (!nested_vmx_check_permission(vcpu)) | |
7661 | return 1; | |
7662 | ||
7663 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7664 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); | |
7665 | ||
bcdde302 JD |
7666 | types = (vmx->nested.nested_vmx_vpid_caps & |
7667 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; | |
99b83ac8 | 7668 | |
85c856b3 | 7669 | if (type >= 32 || !(types & (1 << type))) { |
99b83ac8 WL |
7670 | nested_vmx_failValid(vcpu, |
7671 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 7672 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
7673 | } |
7674 | ||
7675 | /* according to the intel vmx instruction reference, the memory | |
7676 | * operand is read even if it isn't needed (e.g., for type==global) | |
7677 | */ | |
7678 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
7679 | vmx_instruction_info, false, &gva)) | |
7680 | return 1; | |
40352605 JM |
7681 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
7682 | sizeof(operand), &e)) { | |
99b83ac8 WL |
7683 | kvm_inject_page_fault(vcpu, &e); |
7684 | return 1; | |
7685 | } | |
40352605 JM |
7686 | if (operand.vpid >> 16) { |
7687 | nested_vmx_failValid(vcpu, | |
7688 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
7689 | return kvm_skip_emulated_instruction(vcpu); | |
7690 | } | |
99b83ac8 WL |
7691 | |
7692 | switch (type) { | |
bcdde302 | 7693 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
40352605 JM |
7694 | if (is_noncanonical_address(operand.gla)) { |
7695 | nested_vmx_failValid(vcpu, | |
7696 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
7697 | return kvm_skip_emulated_instruction(vcpu); | |
7698 | } | |
7699 | /* fall through */ | |
ef697a71 | 7700 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
bcdde302 | 7701 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
40352605 | 7702 | if (!operand.vpid) { |
bcdde302 JD |
7703 | nested_vmx_failValid(vcpu, |
7704 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 7705 | return kvm_skip_emulated_instruction(vcpu); |
bcdde302 JD |
7706 | } |
7707 | break; | |
99b83ac8 | 7708 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
99b83ac8 WL |
7709 | break; |
7710 | default: | |
bcdde302 | 7711 | WARN_ON_ONCE(1); |
6affcbed | 7712 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
7713 | } |
7714 | ||
bcdde302 JD |
7715 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02); |
7716 | nested_vmx_succeed(vcpu); | |
7717 | ||
6affcbed | 7718 | return kvm_skip_emulated_instruction(vcpu); |
a642fc30 PM |
7719 | } |
7720 | ||
843e4330 KH |
7721 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
7722 | { | |
7723 | unsigned long exit_qualification; | |
7724 | ||
7725 | trace_kvm_pml_full(vcpu->vcpu_id); | |
7726 | ||
7727 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7728 | ||
7729 | /* | |
7730 | * PML buffer FULL happened while executing iret from NMI, | |
7731 | * "blocked by NMI" bit has to be set before next VM entry. | |
7732 | */ | |
7733 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && | |
843e4330 KH |
7734 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
7735 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
7736 | GUEST_INTR_STATE_NMI); | |
7737 | ||
7738 | /* | |
7739 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do | |
7740 | * here.., and there's no userspace involvement needed for PML. | |
7741 | */ | |
7742 | return 1; | |
7743 | } | |
7744 | ||
64672c95 YJ |
7745 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
7746 | { | |
7747 | kvm_lapic_expired_hv_timer(vcpu); | |
7748 | return 1; | |
7749 | } | |
7750 | ||
6aa8b732 AK |
7751 | /* |
7752 | * The exit handlers return 1 if the exit was handled fully and guest execution | |
7753 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs | |
7754 | * to be done to userspace and return 0. | |
7755 | */ | |
772e0318 | 7756 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
6aa8b732 AK |
7757 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
7758 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, | |
988ad74f | 7759 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
f08864b4 | 7760 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
6aa8b732 | 7761 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
6aa8b732 AK |
7762 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
7763 | [EXIT_REASON_DR_ACCESS] = handle_dr, | |
7764 | [EXIT_REASON_CPUID] = handle_cpuid, | |
7765 | [EXIT_REASON_MSR_READ] = handle_rdmsr, | |
7766 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, | |
7767 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, | |
7768 | [EXIT_REASON_HLT] = handle_halt, | |
ec25d5e6 | 7769 | [EXIT_REASON_INVD] = handle_invd, |
a7052897 | 7770 | [EXIT_REASON_INVLPG] = handle_invlpg, |
fee84b07 | 7771 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
c21415e8 | 7772 | [EXIT_REASON_VMCALL] = handle_vmcall, |
27d6c865 | 7773 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
cd232ad0 | 7774 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
63846663 | 7775 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
6a4d7550 | 7776 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
49f705c5 | 7777 | [EXIT_REASON_VMREAD] = handle_vmread, |
cd232ad0 | 7778 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
49f705c5 | 7779 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
ec378aee NHE |
7780 | [EXIT_REASON_VMOFF] = handle_vmoff, |
7781 | [EXIT_REASON_VMON] = handle_vmon, | |
f78e0e2e SY |
7782 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
7783 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, | |
83d4c286 | 7784 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
c7c9c56c | 7785 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
e5edaa01 | 7786 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
2acf923e | 7787 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
37817f29 | 7788 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
a0861c02 | 7789 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
68f89400 MT |
7790 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
7791 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, | |
4b8d54f9 | 7792 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
87c00572 | 7793 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
5f3d45e7 | 7794 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
87c00572 | 7795 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
bfd0a56b | 7796 | [EXIT_REASON_INVEPT] = handle_invept, |
a642fc30 | 7797 | [EXIT_REASON_INVVPID] = handle_invvpid, |
f53cd63c WL |
7798 | [EXIT_REASON_XSAVES] = handle_xsaves, |
7799 | [EXIT_REASON_XRSTORS] = handle_xrstors, | |
843e4330 | 7800 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
64672c95 | 7801 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
6aa8b732 AK |
7802 | }; |
7803 | ||
7804 | static const int kvm_vmx_max_exit_handlers = | |
50a3485c | 7805 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
6aa8b732 | 7806 | |
908a7bdd JK |
7807 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
7808 | struct vmcs12 *vmcs12) | |
7809 | { | |
7810 | unsigned long exit_qualification; | |
7811 | gpa_t bitmap, last_bitmap; | |
7812 | unsigned int port; | |
7813 | int size; | |
7814 | u8 b; | |
7815 | ||
908a7bdd | 7816 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
2f0a6397 | 7817 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
908a7bdd JK |
7818 | |
7819 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7820 | ||
7821 | port = exit_qualification >> 16; | |
7822 | size = (exit_qualification & 7) + 1; | |
7823 | ||
7824 | last_bitmap = (gpa_t)-1; | |
7825 | b = -1; | |
7826 | ||
7827 | while (size > 0) { | |
7828 | if (port < 0x8000) | |
7829 | bitmap = vmcs12->io_bitmap_a; | |
7830 | else if (port < 0x10000) | |
7831 | bitmap = vmcs12->io_bitmap_b; | |
7832 | else | |
1d804d07 | 7833 | return true; |
908a7bdd JK |
7834 | bitmap += (port & 0x7fff) / 8; |
7835 | ||
7836 | if (last_bitmap != bitmap) | |
54bf36aa | 7837 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
1d804d07 | 7838 | return true; |
908a7bdd | 7839 | if (b & (1 << (port & 7))) |
1d804d07 | 7840 | return true; |
908a7bdd JK |
7841 | |
7842 | port++; | |
7843 | size--; | |
7844 | last_bitmap = bitmap; | |
7845 | } | |
7846 | ||
1d804d07 | 7847 | return false; |
908a7bdd JK |
7848 | } |
7849 | ||
644d711a NHE |
7850 | /* |
7851 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, | |
7852 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed | |
7853 | * disinterest in the current event (read or write a specific MSR) by using an | |
7854 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. | |
7855 | */ | |
7856 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, | |
7857 | struct vmcs12 *vmcs12, u32 exit_reason) | |
7858 | { | |
7859 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; | |
7860 | gpa_t bitmap; | |
7861 | ||
cbd29cb6 | 7862 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
1d804d07 | 7863 | return true; |
644d711a NHE |
7864 | |
7865 | /* | |
7866 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, | |
7867 | * for the four combinations of read/write and low/high MSR numbers. | |
7868 | * First we need to figure out which of the four to use: | |
7869 | */ | |
7870 | bitmap = vmcs12->msr_bitmap; | |
7871 | if (exit_reason == EXIT_REASON_MSR_WRITE) | |
7872 | bitmap += 2048; | |
7873 | if (msr_index >= 0xc0000000) { | |
7874 | msr_index -= 0xc0000000; | |
7875 | bitmap += 1024; | |
7876 | } | |
7877 | ||
7878 | /* Then read the msr_index'th bit from this bitmap: */ | |
7879 | if (msr_index < 1024*8) { | |
7880 | unsigned char b; | |
54bf36aa | 7881 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
1d804d07 | 7882 | return true; |
644d711a NHE |
7883 | return 1 & (b >> (msr_index & 7)); |
7884 | } else | |
1d804d07 | 7885 | return true; /* let L1 handle the wrong parameter */ |
644d711a NHE |
7886 | } |
7887 | ||
7888 | /* | |
7889 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, | |
7890 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to | |
7891 | * intercept (via guest_host_mask etc.) the current event. | |
7892 | */ | |
7893 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, | |
7894 | struct vmcs12 *vmcs12) | |
7895 | { | |
7896 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7897 | int cr = exit_qualification & 15; | |
e1d39b17 JS |
7898 | int reg; |
7899 | unsigned long val; | |
644d711a NHE |
7900 | |
7901 | switch ((exit_qualification >> 4) & 3) { | |
7902 | case 0: /* mov to cr */ | |
e1d39b17 JS |
7903 | reg = (exit_qualification >> 8) & 15; |
7904 | val = kvm_register_readl(vcpu, reg); | |
644d711a NHE |
7905 | switch (cr) { |
7906 | case 0: | |
7907 | if (vmcs12->cr0_guest_host_mask & | |
7908 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 7909 | return true; |
644d711a NHE |
7910 | break; |
7911 | case 3: | |
7912 | if ((vmcs12->cr3_target_count >= 1 && | |
7913 | vmcs12->cr3_target_value0 == val) || | |
7914 | (vmcs12->cr3_target_count >= 2 && | |
7915 | vmcs12->cr3_target_value1 == val) || | |
7916 | (vmcs12->cr3_target_count >= 3 && | |
7917 | vmcs12->cr3_target_value2 == val) || | |
7918 | (vmcs12->cr3_target_count >= 4 && | |
7919 | vmcs12->cr3_target_value3 == val)) | |
1d804d07 | 7920 | return false; |
644d711a | 7921 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
1d804d07 | 7922 | return true; |
644d711a NHE |
7923 | break; |
7924 | case 4: | |
7925 | if (vmcs12->cr4_guest_host_mask & | |
7926 | (vmcs12->cr4_read_shadow ^ val)) | |
1d804d07 | 7927 | return true; |
644d711a NHE |
7928 | break; |
7929 | case 8: | |
7930 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) | |
1d804d07 | 7931 | return true; |
644d711a NHE |
7932 | break; |
7933 | } | |
7934 | break; | |
7935 | case 2: /* clts */ | |
7936 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && | |
7937 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) | |
1d804d07 | 7938 | return true; |
644d711a NHE |
7939 | break; |
7940 | case 1: /* mov from cr */ | |
7941 | switch (cr) { | |
7942 | case 3: | |
7943 | if (vmcs12->cpu_based_vm_exec_control & | |
7944 | CPU_BASED_CR3_STORE_EXITING) | |
1d804d07 | 7945 | return true; |
644d711a NHE |
7946 | break; |
7947 | case 8: | |
7948 | if (vmcs12->cpu_based_vm_exec_control & | |
7949 | CPU_BASED_CR8_STORE_EXITING) | |
1d804d07 | 7950 | return true; |
644d711a NHE |
7951 | break; |
7952 | } | |
7953 | break; | |
7954 | case 3: /* lmsw */ | |
7955 | /* | |
7956 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of | |
7957 | * cr0. Other attempted changes are ignored, with no exit. | |
7958 | */ | |
e1d39b17 | 7959 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
644d711a NHE |
7960 | if (vmcs12->cr0_guest_host_mask & 0xe & |
7961 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 7962 | return true; |
644d711a NHE |
7963 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
7964 | !(vmcs12->cr0_read_shadow & 0x1) && | |
7965 | (val & 0x1)) | |
1d804d07 | 7966 | return true; |
644d711a NHE |
7967 | break; |
7968 | } | |
1d804d07 | 7969 | return false; |
644d711a NHE |
7970 | } |
7971 | ||
7972 | /* | |
7973 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we | |
7974 | * should handle it ourselves in L0 (and then continue L2). Only call this | |
7975 | * when in is_guest_mode (L2). | |
7976 | */ | |
7977 | static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) | |
7978 | { | |
644d711a NHE |
7979 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
7980 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7981 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
957c897e | 7982 | u32 exit_reason = vmx->exit_reason; |
644d711a | 7983 | |
542060ea JK |
7984 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
7985 | vmcs_readl(EXIT_QUALIFICATION), | |
7986 | vmx->idt_vectoring_info, | |
7987 | intr_info, | |
7988 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
7989 | KVM_ISA_VMX); | |
7990 | ||
644d711a | 7991 | if (vmx->nested.nested_run_pending) |
1d804d07 | 7992 | return false; |
644d711a NHE |
7993 | |
7994 | if (unlikely(vmx->fail)) { | |
bd80158a JK |
7995 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
7996 | vmcs_read32(VM_INSTRUCTION_ERROR)); | |
1d804d07 | 7997 | return true; |
644d711a NHE |
7998 | } |
7999 | ||
8000 | switch (exit_reason) { | |
8001 | case EXIT_REASON_EXCEPTION_NMI: | |
ef85b673 | 8002 | if (is_nmi(intr_info)) |
1d804d07 | 8003 | return false; |
644d711a NHE |
8004 | else if (is_page_fault(intr_info)) |
8005 | return enable_ept; | |
e504c909 | 8006 | else if (is_no_device(intr_info) && |
ccf9844e | 8007 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
1d804d07 | 8008 | return false; |
6f05485d JK |
8009 | else if (is_debug(intr_info) && |
8010 | vcpu->guest_debug & | |
8011 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
8012 | return false; | |
8013 | else if (is_breakpoint(intr_info) && | |
8014 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) | |
8015 | return false; | |
644d711a NHE |
8016 | return vmcs12->exception_bitmap & |
8017 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); | |
8018 | case EXIT_REASON_EXTERNAL_INTERRUPT: | |
1d804d07 | 8019 | return false; |
644d711a | 8020 | case EXIT_REASON_TRIPLE_FAULT: |
1d804d07 | 8021 | return true; |
644d711a | 8022 | case EXIT_REASON_PENDING_INTERRUPT: |
3b656cf7 | 8023 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
644d711a | 8024 | case EXIT_REASON_NMI_WINDOW: |
3b656cf7 | 8025 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
644d711a | 8026 | case EXIT_REASON_TASK_SWITCH: |
1d804d07 | 8027 | return true; |
644d711a | 8028 | case EXIT_REASON_CPUID: |
1d804d07 | 8029 | return true; |
644d711a NHE |
8030 | case EXIT_REASON_HLT: |
8031 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); | |
8032 | case EXIT_REASON_INVD: | |
1d804d07 | 8033 | return true; |
644d711a NHE |
8034 | case EXIT_REASON_INVLPG: |
8035 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); | |
8036 | case EXIT_REASON_RDPMC: | |
8037 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); | |
a5f46457 PB |
8038 | case EXIT_REASON_RDRAND: |
8039 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND); | |
8040 | case EXIT_REASON_RDSEED: | |
8041 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED); | |
b3a2a907 | 8042 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
644d711a NHE |
8043 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
8044 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: | |
8045 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: | |
8046 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: | |
8047 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: | |
8048 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: | |
a642fc30 | 8049 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
644d711a NHE |
8050 | /* |
8051 | * VMX instructions trap unconditionally. This allows L1 to | |
8052 | * emulate them for its L2 guest, i.e., allows 3-level nesting! | |
8053 | */ | |
1d804d07 | 8054 | return true; |
644d711a NHE |
8055 | case EXIT_REASON_CR_ACCESS: |
8056 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); | |
8057 | case EXIT_REASON_DR_ACCESS: | |
8058 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); | |
8059 | case EXIT_REASON_IO_INSTRUCTION: | |
908a7bdd | 8060 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
1b07304c PB |
8061 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
8062 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); | |
644d711a NHE |
8063 | case EXIT_REASON_MSR_READ: |
8064 | case EXIT_REASON_MSR_WRITE: | |
8065 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); | |
8066 | case EXIT_REASON_INVALID_STATE: | |
1d804d07 | 8067 | return true; |
644d711a NHE |
8068 | case EXIT_REASON_MWAIT_INSTRUCTION: |
8069 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); | |
5f3d45e7 MD |
8070 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
8071 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); | |
644d711a NHE |
8072 | case EXIT_REASON_MONITOR_INSTRUCTION: |
8073 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); | |
8074 | case EXIT_REASON_PAUSE_INSTRUCTION: | |
8075 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || | |
8076 | nested_cpu_has2(vmcs12, | |
8077 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); | |
8078 | case EXIT_REASON_MCE_DURING_VMENTRY: | |
1d804d07 | 8079 | return false; |
644d711a | 8080 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
a7c0b07d | 8081 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
644d711a NHE |
8082 | case EXIT_REASON_APIC_ACCESS: |
8083 | return nested_cpu_has2(vmcs12, | |
8084 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
82f0dd4b | 8085 | case EXIT_REASON_APIC_WRITE: |
608406e2 WV |
8086 | case EXIT_REASON_EOI_INDUCED: |
8087 | /* apic_write and eoi_induced should exit unconditionally. */ | |
1d804d07 | 8088 | return true; |
644d711a | 8089 | case EXIT_REASON_EPT_VIOLATION: |
2b1be677 NHE |
8090 | /* |
8091 | * L0 always deals with the EPT violation. If nested EPT is | |
8092 | * used, and the nested mmu code discovers that the address is | |
8093 | * missing in the guest EPT table (EPT12), the EPT violation | |
8094 | * will be injected with nested_ept_inject_page_fault() | |
8095 | */ | |
1d804d07 | 8096 | return false; |
644d711a | 8097 | case EXIT_REASON_EPT_MISCONFIG: |
2b1be677 NHE |
8098 | /* |
8099 | * L2 never uses directly L1's EPT, but rather L0's own EPT | |
8100 | * table (shadow on EPT) or a merged EPT table that L0 built | |
8101 | * (EPT on EPT). So any problems with the structure of the | |
8102 | * table is L0's fault. | |
8103 | */ | |
1d804d07 | 8104 | return false; |
644d711a NHE |
8105 | case EXIT_REASON_WBINVD: |
8106 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); | |
8107 | case EXIT_REASON_XSETBV: | |
1d804d07 | 8108 | return true; |
81dc01f7 WL |
8109 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
8110 | /* | |
8111 | * This should never happen, since it is not possible to | |
8112 | * set XSS to a non-zero value---neither in L1 nor in L2. | |
8113 | * If if it were, XSS would have to be checked against | |
8114 | * the XSS exit bitmap in vmcs12. | |
8115 | */ | |
8116 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); | |
55123e3c WL |
8117 | case EXIT_REASON_PREEMPTION_TIMER: |
8118 | return false; | |
ab007cc9 | 8119 | case EXIT_REASON_PML_FULL: |
03efce6f | 8120 | /* We emulate PML support to L1. */ |
ab007cc9 | 8121 | return false; |
644d711a | 8122 | default: |
1d804d07 | 8123 | return true; |
644d711a NHE |
8124 | } |
8125 | } | |
8126 | ||
586f9607 AK |
8127 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
8128 | { | |
8129 | *info1 = vmcs_readl(EXIT_QUALIFICATION); | |
8130 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); | |
8131 | } | |
8132 | ||
a3eaa864 | 8133 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
843e4330 | 8134 | { |
a3eaa864 KH |
8135 | if (vmx->pml_pg) { |
8136 | __free_page(vmx->pml_pg); | |
8137 | vmx->pml_pg = NULL; | |
8138 | } | |
843e4330 KH |
8139 | } |
8140 | ||
54bf36aa | 8141 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
843e4330 | 8142 | { |
54bf36aa | 8143 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
843e4330 KH |
8144 | u64 *pml_buf; |
8145 | u16 pml_idx; | |
8146 | ||
8147 | pml_idx = vmcs_read16(GUEST_PML_INDEX); | |
8148 | ||
8149 | /* Do nothing if PML buffer is empty */ | |
8150 | if (pml_idx == (PML_ENTITY_NUM - 1)) | |
8151 | return; | |
8152 | ||
8153 | /* PML index always points to next available PML buffer entity */ | |
8154 | if (pml_idx >= PML_ENTITY_NUM) | |
8155 | pml_idx = 0; | |
8156 | else | |
8157 | pml_idx++; | |
8158 | ||
8159 | pml_buf = page_address(vmx->pml_pg); | |
8160 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { | |
8161 | u64 gpa; | |
8162 | ||
8163 | gpa = pml_buf[pml_idx]; | |
8164 | WARN_ON(gpa & (PAGE_SIZE - 1)); | |
54bf36aa | 8165 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
843e4330 KH |
8166 | } |
8167 | ||
8168 | /* reset PML index */ | |
8169 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
8170 | } | |
8171 | ||
8172 | /* | |
8173 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. | |
8174 | * Called before reporting dirty_bitmap to userspace. | |
8175 | */ | |
8176 | static void kvm_flush_pml_buffers(struct kvm *kvm) | |
8177 | { | |
8178 | int i; | |
8179 | struct kvm_vcpu *vcpu; | |
8180 | /* | |
8181 | * We only need to kick vcpu out of guest mode here, as PML buffer | |
8182 | * is flushed at beginning of all VMEXITs, and it's obvious that only | |
8183 | * vcpus running in guest are possible to have unflushed GPAs in PML | |
8184 | * buffer. | |
8185 | */ | |
8186 | kvm_for_each_vcpu(i, vcpu, kvm) | |
8187 | kvm_vcpu_kick(vcpu); | |
8188 | } | |
8189 | ||
4eb64dce PB |
8190 | static void vmx_dump_sel(char *name, uint32_t sel) |
8191 | { | |
8192 | pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", | |
96794e4e | 8193 | name, vmcs_read16(sel), |
4eb64dce PB |
8194 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
8195 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), | |
8196 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); | |
8197 | } | |
8198 | ||
8199 | static void vmx_dump_dtsel(char *name, uint32_t limit) | |
8200 | { | |
8201 | pr_err("%s limit=0x%08x, base=0x%016lx\n", | |
8202 | name, vmcs_read32(limit), | |
8203 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); | |
8204 | } | |
8205 | ||
8206 | static void dump_vmcs(void) | |
8207 | { | |
8208 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); | |
8209 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); | |
8210 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); | |
8211 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); | |
8212 | u32 secondary_exec_control = 0; | |
8213 | unsigned long cr4 = vmcs_readl(GUEST_CR4); | |
f3531054 | 8214 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
4eb64dce PB |
8215 | int i, n; |
8216 | ||
8217 | if (cpu_has_secondary_exec_ctrls()) | |
8218 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
8219 | ||
8220 | pr_err("*** Guest State ***\n"); | |
8221 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
8222 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), | |
8223 | vmcs_readl(CR0_GUEST_HOST_MASK)); | |
8224 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
8225 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); | |
8226 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); | |
8227 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && | |
8228 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) | |
8229 | { | |
845c5b40 PB |
8230 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
8231 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); | |
8232 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", | |
8233 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); | |
4eb64dce PB |
8234 | } |
8235 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", | |
8236 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); | |
8237 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", | |
8238 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); | |
8239 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
8240 | vmcs_readl(GUEST_SYSENTER_ESP), | |
8241 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); | |
8242 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); | |
8243 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); | |
8244 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); | |
8245 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); | |
8246 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); | |
8247 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); | |
8248 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); | |
8249 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); | |
8250 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); | |
8251 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); | |
8252 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || | |
8253 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) | |
845c5b40 PB |
8254 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
8255 | efer, vmcs_read64(GUEST_IA32_PAT)); | |
8256 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", | |
8257 | vmcs_read64(GUEST_IA32_DEBUGCTL), | |
4eb64dce PB |
8258 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
8259 | if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
845c5b40 PB |
8260 | pr_err("PerfGlobCtl = 0x%016llx\n", |
8261 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce | 8262 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
845c5b40 | 8263 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
4eb64dce PB |
8264 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
8265 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), | |
8266 | vmcs_read32(GUEST_ACTIVITY_STATE)); | |
8267 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) | |
8268 | pr_err("InterruptStatus = %04x\n", | |
8269 | vmcs_read16(GUEST_INTR_STATUS)); | |
8270 | ||
8271 | pr_err("*** Host State ***\n"); | |
8272 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", | |
8273 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); | |
8274 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", | |
8275 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), | |
8276 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), | |
8277 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), | |
8278 | vmcs_read16(HOST_TR_SELECTOR)); | |
8279 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", | |
8280 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), | |
8281 | vmcs_readl(HOST_TR_BASE)); | |
8282 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", | |
8283 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); | |
8284 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", | |
8285 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), | |
8286 | vmcs_readl(HOST_CR4)); | |
8287 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
8288 | vmcs_readl(HOST_IA32_SYSENTER_ESP), | |
8289 | vmcs_read32(HOST_IA32_SYSENTER_CS), | |
8290 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); | |
8291 | if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER)) | |
845c5b40 PB |
8292 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
8293 | vmcs_read64(HOST_IA32_EFER), | |
8294 | vmcs_read64(HOST_IA32_PAT)); | |
4eb64dce | 8295 | if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
845c5b40 PB |
8296 | pr_err("PerfGlobCtl = 0x%016llx\n", |
8297 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce PB |
8298 | |
8299 | pr_err("*** Control State ***\n"); | |
8300 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", | |
8301 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); | |
8302 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); | |
8303 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", | |
8304 | vmcs_read32(EXCEPTION_BITMAP), | |
8305 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), | |
8306 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); | |
8307 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", | |
8308 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), | |
8309 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), | |
8310 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); | |
8311 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", | |
8312 | vmcs_read32(VM_EXIT_INTR_INFO), | |
8313 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
8314 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); | |
8315 | pr_err(" reason=%08x qualification=%016lx\n", | |
8316 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); | |
8317 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", | |
8318 | vmcs_read32(IDT_VECTORING_INFO_FIELD), | |
8319 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); | |
845c5b40 | 8320 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
8cfe9866 | 8321 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
845c5b40 PB |
8322 | pr_err("TSC Multiplier = 0x%016llx\n", |
8323 | vmcs_read64(TSC_MULTIPLIER)); | |
4eb64dce PB |
8324 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
8325 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); | |
8326 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) | |
8327 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); | |
8328 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) | |
845c5b40 | 8329 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
4eb64dce PB |
8330 | n = vmcs_read32(CR3_TARGET_COUNT); |
8331 | for (i = 0; i + 1 < n; i += 4) | |
8332 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", | |
8333 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), | |
8334 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); | |
8335 | if (i < n) | |
8336 | pr_err("CR3 target%u=%016lx\n", | |
8337 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); | |
8338 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) | |
8339 | pr_err("PLE Gap=%08x Window=%08x\n", | |
8340 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); | |
8341 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) | |
8342 | pr_err("Virtual processor ID = 0x%04x\n", | |
8343 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); | |
8344 | } | |
8345 | ||
6aa8b732 AK |
8346 | /* |
8347 | * The guest has exited. See if we can fix it or if we need userspace | |
8348 | * assistance. | |
8349 | */ | |
851ba692 | 8350 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
6aa8b732 | 8351 | { |
29bd8a78 | 8352 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
a0861c02 | 8353 | u32 exit_reason = vmx->exit_reason; |
1155f76a | 8354 | u32 vectoring_info = vmx->idt_vectoring_info; |
29bd8a78 | 8355 | |
8b89fe1f | 8356 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
db1c056c | 8357 | vcpu->arch.gpa_available = false; |
8b89fe1f | 8358 | |
843e4330 KH |
8359 | /* |
8360 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more | |
8361 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before | |
8362 | * querying dirty_bitmap, we only need to kick all vcpus out of guest | |
8363 | * mode as if vcpus is in root mode, the PML buffer must has been | |
8364 | * flushed already. | |
8365 | */ | |
8366 | if (enable_pml) | |
54bf36aa | 8367 | vmx_flush_pml_buffer(vcpu); |
843e4330 | 8368 | |
80ced186 | 8369 | /* If guest state is invalid, start emulating */ |
14168786 | 8370 | if (vmx->emulation_required) |
80ced186 | 8371 | return handle_invalid_guest_state(vcpu); |
1d5a4d9b | 8372 | |
644d711a | 8373 | if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) { |
533558bc JK |
8374 | nested_vmx_vmexit(vcpu, exit_reason, |
8375 | vmcs_read32(VM_EXIT_INTR_INFO), | |
8376 | vmcs_readl(EXIT_QUALIFICATION)); | |
644d711a NHE |
8377 | return 1; |
8378 | } | |
8379 | ||
5120702e | 8380 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
4eb64dce | 8381 | dump_vmcs(); |
5120702e MG |
8382 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
8383 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
8384 | = exit_reason; | |
8385 | return 0; | |
8386 | } | |
8387 | ||
29bd8a78 | 8388 | if (unlikely(vmx->fail)) { |
851ba692 AK |
8389 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
8390 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
29bd8a78 AK |
8391 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
8392 | return 0; | |
8393 | } | |
6aa8b732 | 8394 | |
b9bf6882 XG |
8395 | /* |
8396 | * Note: | |
8397 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by | |
8398 | * delivery event since it indicates guest is accessing MMIO. | |
8399 | * The vm-exit can be triggered again after return to guest that | |
8400 | * will cause infinite loop. | |
8401 | */ | |
d77c26fc | 8402 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
1439442c | 8403 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
60637aac | 8404 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
b244c9fc | 8405 | exit_reason != EXIT_REASON_PML_FULL && |
b9bf6882 XG |
8406 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
8407 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
8408 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; | |
8409 | vcpu->run->internal.ndata = 2; | |
8410 | vcpu->run->internal.data[0] = vectoring_info; | |
8411 | vcpu->run->internal.data[1] = exit_reason; | |
8412 | return 0; | |
8413 | } | |
3b86cd99 | 8414 | |
6aa8b732 AK |
8415 | if (exit_reason < kvm_vmx_max_exit_handlers |
8416 | && kvm_vmx_exit_handlers[exit_reason]) | |
851ba692 | 8417 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
6aa8b732 | 8418 | else { |
6c6c5e03 RK |
8419 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
8420 | exit_reason); | |
2bc19dc3 MT |
8421 | kvm_queue_exception(vcpu, UD_VECTOR); |
8422 | return 1; | |
6aa8b732 | 8423 | } |
6aa8b732 AK |
8424 | } |
8425 | ||
95ba8273 | 8426 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
6e5d865c | 8427 | { |
a7c0b07d WL |
8428 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
8429 | ||
8430 | if (is_guest_mode(vcpu) && | |
8431 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
8432 | return; | |
8433 | ||
95ba8273 | 8434 | if (irr == -1 || tpr < irr) { |
6e5d865c YS |
8435 | vmcs_write32(TPR_THRESHOLD, 0); |
8436 | return; | |
8437 | } | |
8438 | ||
95ba8273 | 8439 | vmcs_write32(TPR_THRESHOLD, irr); |
6e5d865c YS |
8440 | } |
8441 | ||
8d14695f YZ |
8442 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
8443 | { | |
8444 | u32 sec_exec_control; | |
8445 | ||
dccbfcf5 RK |
8446 | /* Postpone execution until vmcs01 is the current VMCS. */ |
8447 | if (is_guest_mode(vcpu)) { | |
8448 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; | |
8449 | return; | |
8450 | } | |
8451 | ||
f6e90f9e | 8452 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
8d14695f YZ |
8453 | return; |
8454 | ||
35754c98 | 8455 | if (!cpu_need_tpr_shadow(vcpu)) |
8d14695f YZ |
8456 | return; |
8457 | ||
8458 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
8459 | ||
8460 | if (set) { | |
8461 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
8462 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
8463 | } else { | |
8464 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
8465 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
fb6c8198 | 8466 | vmx_flush_tlb_ept_only(vcpu); |
8d14695f YZ |
8467 | } |
8468 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); | |
8469 | ||
8470 | vmx_set_msr_bitmap(vcpu); | |
8471 | } | |
8472 | ||
38b99173 TC |
8473 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
8474 | { | |
8475 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8476 | ||
8477 | /* | |
8478 | * Currently we do not handle the nested case where L2 has an | |
8479 | * APIC access page of its own; that page is still pinned. | |
8480 | * Hence, we skip the case where the VCPU is in guest mode _and_ | |
8481 | * L1 prepared an APIC access page for L2. | |
8482 | * | |
8483 | * For the case where L1 and L2 share the same APIC access page | |
8484 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear | |
8485 | * in the vmcs12), this function will only update either the vmcs01 | |
8486 | * or the vmcs02. If the former, the vmcs02 will be updated by | |
8487 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in | |
8488 | * the next L2->L1 exit. | |
8489 | */ | |
8490 | if (!is_guest_mode(vcpu) || | |
4f2777bc | 8491 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
fb6c8198 | 8492 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
38b99173 | 8493 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
fb6c8198 JM |
8494 | vmx_flush_tlb_ept_only(vcpu); |
8495 | } | |
38b99173 TC |
8496 | } |
8497 | ||
67c9dddc | 8498 | static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr) |
c7c9c56c YZ |
8499 | { |
8500 | u16 status; | |
8501 | u8 old; | |
8502 | ||
67c9dddc PB |
8503 | if (max_isr == -1) |
8504 | max_isr = 0; | |
c7c9c56c YZ |
8505 | |
8506 | status = vmcs_read16(GUEST_INTR_STATUS); | |
8507 | old = status >> 8; | |
67c9dddc | 8508 | if (max_isr != old) { |
c7c9c56c | 8509 | status &= 0xff; |
67c9dddc | 8510 | status |= max_isr << 8; |
c7c9c56c YZ |
8511 | vmcs_write16(GUEST_INTR_STATUS, status); |
8512 | } | |
8513 | } | |
8514 | ||
8515 | static void vmx_set_rvi(int vector) | |
8516 | { | |
8517 | u16 status; | |
8518 | u8 old; | |
8519 | ||
4114c27d WW |
8520 | if (vector == -1) |
8521 | vector = 0; | |
8522 | ||
c7c9c56c YZ |
8523 | status = vmcs_read16(GUEST_INTR_STATUS); |
8524 | old = (u8)status & 0xff; | |
8525 | if ((u8)vector != old) { | |
8526 | status &= ~0xff; | |
8527 | status |= (u8)vector; | |
8528 | vmcs_write16(GUEST_INTR_STATUS, status); | |
8529 | } | |
8530 | } | |
8531 | ||
8532 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) | |
8533 | { | |
4114c27d WW |
8534 | if (!is_guest_mode(vcpu)) { |
8535 | vmx_set_rvi(max_irr); | |
8536 | return; | |
8537 | } | |
8538 | ||
c7c9c56c YZ |
8539 | if (max_irr == -1) |
8540 | return; | |
8541 | ||
963fee16 | 8542 | /* |
4114c27d WW |
8543 | * In guest mode. If a vmexit is needed, vmx_check_nested_events |
8544 | * handles it. | |
963fee16 | 8545 | */ |
4114c27d | 8546 | if (nested_exit_on_intr(vcpu)) |
963fee16 WL |
8547 | return; |
8548 | ||
963fee16 | 8549 | /* |
4114c27d | 8550 | * Else, fall back to pre-APICv interrupt injection since L2 |
963fee16 WL |
8551 | * is run without virtual interrupt delivery. |
8552 | */ | |
8553 | if (!kvm_event_needs_reinjection(vcpu) && | |
8554 | vmx_interrupt_allowed(vcpu)) { | |
8555 | kvm_queue_interrupt(vcpu, max_irr, false); | |
8556 | vmx_inject_irq(vcpu); | |
8557 | } | |
c7c9c56c YZ |
8558 | } |
8559 | ||
76dfafd5 | 8560 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
810e6def PB |
8561 | { |
8562 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
76dfafd5 | 8563 | int max_irr; |
810e6def | 8564 | |
76dfafd5 PB |
8565 | WARN_ON(!vcpu->arch.apicv_active); |
8566 | if (pi_test_on(&vmx->pi_desc)) { | |
8567 | pi_clear_on(&vmx->pi_desc); | |
8568 | /* | |
8569 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. | |
8570 | * But on x86 this is just a compiler barrier anyway. | |
8571 | */ | |
8572 | smp_mb__after_atomic(); | |
8573 | max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); | |
8574 | } else { | |
8575 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
8576 | } | |
8577 | vmx_hwapic_irr_update(vcpu, max_irr); | |
8578 | return max_irr; | |
810e6def PB |
8579 | } |
8580 | ||
6308630b | 8581 | static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) |
c7c9c56c | 8582 | { |
d62caabb | 8583 | if (!kvm_vcpu_apicv_active(vcpu)) |
3d81bc7e YZ |
8584 | return; |
8585 | ||
c7c9c56c YZ |
8586 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
8587 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); | |
8588 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); | |
8589 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); | |
8590 | } | |
8591 | ||
967235d3 PB |
8592 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
8593 | { | |
8594 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8595 | ||
8596 | pi_clear_on(&vmx->pi_desc); | |
8597 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); | |
8598 | } | |
8599 | ||
51aa01d1 | 8600 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
cf393f75 | 8601 | { |
00eba012 AK |
8602 | u32 exit_intr_info; |
8603 | ||
8604 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY | |
8605 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) | |
8606 | return; | |
8607 | ||
c5ca8e57 | 8608 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
00eba012 | 8609 | exit_intr_info = vmx->exit_intr_info; |
a0861c02 AK |
8610 | |
8611 | /* Handle machine checks before interrupts are enabled */ | |
00eba012 | 8612 | if (is_machine_check(exit_intr_info)) |
a0861c02 AK |
8613 | kvm_machine_check(); |
8614 | ||
20f65983 | 8615 | /* We need to handle NMIs before interrupts are enabled */ |
ef85b673 | 8616 | if (is_nmi(exit_intr_info)) { |
ff9d07a0 | 8617 | kvm_before_handle_nmi(&vmx->vcpu); |
20f65983 | 8618 | asm("int $2"); |
ff9d07a0 ZY |
8619 | kvm_after_handle_nmi(&vmx->vcpu); |
8620 | } | |
51aa01d1 | 8621 | } |
20f65983 | 8622 | |
a547c6db YZ |
8623 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
8624 | { | |
8625 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
3f62de5f | 8626 | register void *__sp asm(_ASM_SP); |
a547c6db | 8627 | |
a547c6db YZ |
8628 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
8629 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { | |
8630 | unsigned int vector; | |
8631 | unsigned long entry; | |
8632 | gate_desc *desc; | |
8633 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8634 | #ifdef CONFIG_X86_64 | |
8635 | unsigned long tmp; | |
8636 | #endif | |
8637 | ||
8638 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
8639 | desc = (gate_desc *)vmx->host_idt_base + vector; | |
8640 | entry = gate_offset(*desc); | |
8641 | asm volatile( | |
8642 | #ifdef CONFIG_X86_64 | |
8643 | "mov %%" _ASM_SP ", %[sp]\n\t" | |
8644 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" | |
8645 | "push $%c[ss]\n\t" | |
8646 | "push %[sp]\n\t" | |
8647 | #endif | |
8648 | "pushf\n\t" | |
a547c6db YZ |
8649 | __ASM_SIZE(push) " $%c[cs]\n\t" |
8650 | "call *%[entry]\n\t" | |
8651 | : | |
8652 | #ifdef CONFIG_X86_64 | |
3f62de5f | 8653 | [sp]"=&r"(tmp), |
a547c6db | 8654 | #endif |
3f62de5f | 8655 | "+r"(__sp) |
a547c6db YZ |
8656 | : |
8657 | [entry]"r"(entry), | |
8658 | [ss]"i"(__KERNEL_DS), | |
8659 | [cs]"i"(__KERNEL_CS) | |
8660 | ); | |
f2485b3e | 8661 | } |
a547c6db YZ |
8662 | } |
8663 | ||
6d396b55 PB |
8664 | static bool vmx_has_high_real_mode_segbase(void) |
8665 | { | |
8666 | return enable_unrestricted_guest || emulate_invalid_guest_state; | |
8667 | } | |
8668 | ||
da8999d3 LJ |
8669 | static bool vmx_mpx_supported(void) |
8670 | { | |
8671 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && | |
8672 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); | |
8673 | } | |
8674 | ||
55412b2e WL |
8675 | static bool vmx_xsaves_supported(void) |
8676 | { | |
8677 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
8678 | SECONDARY_EXEC_XSAVES; | |
8679 | } | |
8680 | ||
51aa01d1 AK |
8681 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
8682 | { | |
c5ca8e57 | 8683 | u32 exit_intr_info; |
51aa01d1 AK |
8684 | bool unblock_nmi; |
8685 | u8 vector; | |
8686 | bool idtv_info_valid; | |
8687 | ||
8688 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
20f65983 | 8689 | |
2c82878b PB |
8690 | if (vmx->nmi_known_unmasked) |
8691 | return; | |
8692 | /* | |
8693 | * Can't use vmx->exit_intr_info since we're not sure what | |
8694 | * the exit reason is. | |
8695 | */ | |
8696 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
8697 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; | |
8698 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
8699 | /* | |
8700 | * SDM 3: 27.7.1.2 (September 2008) | |
8701 | * Re-set bit "block by NMI" before VM entry if vmexit caused by | |
8702 | * a guest IRET fault. | |
8703 | * SDM 3: 23.2.2 (September 2008) | |
8704 | * Bit 12 is undefined in any of the following cases: | |
8705 | * If the VM exit sets the valid bit in the IDT-vectoring | |
8706 | * information field. | |
8707 | * If the VM exit is due to a double fault. | |
8708 | */ | |
8709 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && | |
8710 | vector != DF_VECTOR && !idtv_info_valid) | |
8711 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
8712 | GUEST_INTR_STATE_NMI); | |
8713 | else | |
8714 | vmx->nmi_known_unmasked = | |
8715 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) | |
8716 | & GUEST_INTR_STATE_NMI); | |
51aa01d1 AK |
8717 | } |
8718 | ||
3ab66e8a | 8719 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
83422e17 AK |
8720 | u32 idt_vectoring_info, |
8721 | int instr_len_field, | |
8722 | int error_code_field) | |
51aa01d1 | 8723 | { |
51aa01d1 AK |
8724 | u8 vector; |
8725 | int type; | |
8726 | bool idtv_info_valid; | |
8727 | ||
8728 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
668f612f | 8729 | |
3ab66e8a JK |
8730 | vcpu->arch.nmi_injected = false; |
8731 | kvm_clear_exception_queue(vcpu); | |
8732 | kvm_clear_interrupt_queue(vcpu); | |
37b96e98 GN |
8733 | |
8734 | if (!idtv_info_valid) | |
8735 | return; | |
8736 | ||
3ab66e8a | 8737 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
3842d135 | 8738 | |
668f612f AK |
8739 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
8740 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; | |
37b96e98 | 8741 | |
64a7ec06 | 8742 | switch (type) { |
37b96e98 | 8743 | case INTR_TYPE_NMI_INTR: |
3ab66e8a | 8744 | vcpu->arch.nmi_injected = true; |
668f612f | 8745 | /* |
7b4a25cb | 8746 | * SDM 3: 27.7.1.2 (September 2008) |
37b96e98 GN |
8747 | * Clear bit "block by NMI" before VM entry if a NMI |
8748 | * delivery faulted. | |
668f612f | 8749 | */ |
3ab66e8a | 8750 | vmx_set_nmi_mask(vcpu, false); |
37b96e98 | 8751 | break; |
37b96e98 | 8752 | case INTR_TYPE_SOFT_EXCEPTION: |
3ab66e8a | 8753 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f GN |
8754 | /* fall through */ |
8755 | case INTR_TYPE_HARD_EXCEPTION: | |
35920a35 | 8756 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
83422e17 | 8757 | u32 err = vmcs_read32(error_code_field); |
851eb667 | 8758 | kvm_requeue_exception_e(vcpu, vector, err); |
35920a35 | 8759 | } else |
851eb667 | 8760 | kvm_requeue_exception(vcpu, vector); |
37b96e98 | 8761 | break; |
66fd3f7f | 8762 | case INTR_TYPE_SOFT_INTR: |
3ab66e8a | 8763 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f | 8764 | /* fall through */ |
37b96e98 | 8765 | case INTR_TYPE_EXT_INTR: |
3ab66e8a | 8766 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
37b96e98 GN |
8767 | break; |
8768 | default: | |
8769 | break; | |
f7d9238f | 8770 | } |
cf393f75 AK |
8771 | } |
8772 | ||
83422e17 AK |
8773 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
8774 | { | |
3ab66e8a | 8775 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
83422e17 AK |
8776 | VM_EXIT_INSTRUCTION_LEN, |
8777 | IDT_VECTORING_ERROR_CODE); | |
8778 | } | |
8779 | ||
b463a6f7 AK |
8780 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
8781 | { | |
3ab66e8a | 8782 | __vmx_complete_interrupts(vcpu, |
b463a6f7 AK |
8783 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
8784 | VM_ENTRY_INSTRUCTION_LEN, | |
8785 | VM_ENTRY_EXCEPTION_ERROR_CODE); | |
8786 | ||
8787 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
8788 | } | |
8789 | ||
d7cd9796 GN |
8790 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
8791 | { | |
8792 | int i, nr_msrs; | |
8793 | struct perf_guest_switch_msr *msrs; | |
8794 | ||
8795 | msrs = perf_guest_get_msrs(&nr_msrs); | |
8796 | ||
8797 | if (!msrs) | |
8798 | return; | |
8799 | ||
8800 | for (i = 0; i < nr_msrs; i++) | |
8801 | if (msrs[i].host == msrs[i].guest) | |
8802 | clear_atomic_switch_msr(vmx, msrs[i].msr); | |
8803 | else | |
8804 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, | |
8805 | msrs[i].host); | |
8806 | } | |
8807 | ||
33365e7a | 8808 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
64672c95 YJ |
8809 | { |
8810 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8811 | u64 tscl; | |
8812 | u32 delta_tsc; | |
8813 | ||
8814 | if (vmx->hv_deadline_tsc == -1) | |
8815 | return; | |
8816 | ||
8817 | tscl = rdtsc(); | |
8818 | if (vmx->hv_deadline_tsc > tscl) | |
8819 | /* sure to be 32 bit only because checked on set_hv_timer */ | |
8820 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> | |
8821 | cpu_preemption_timer_multi); | |
8822 | else | |
8823 | delta_tsc = 0; | |
8824 | ||
8825 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); | |
8826 | } | |
8827 | ||
a3b5ba49 | 8828 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
6aa8b732 | 8829 | { |
a2fa3e9f | 8830 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
d974baa3 | 8831 | unsigned long debugctlmsr, cr4; |
104f226b | 8832 | |
104f226b AK |
8833 | /* Don't enter VMX if guest state is invalid, let the exit handler |
8834 | start emulation until we arrive back to a valid state */ | |
14168786 | 8835 | if (vmx->emulation_required) |
104f226b AK |
8836 | return; |
8837 | ||
a7653ecd RK |
8838 | if (vmx->ple_window_dirty) { |
8839 | vmx->ple_window_dirty = false; | |
8840 | vmcs_write32(PLE_WINDOW, vmx->ple_window); | |
8841 | } | |
8842 | ||
012f83cb AG |
8843 | if (vmx->nested.sync_shadow_vmcs) { |
8844 | copy_vmcs12_to_shadow(vmx); | |
8845 | vmx->nested.sync_shadow_vmcs = false; | |
8846 | } | |
8847 | ||
104f226b AK |
8848 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
8849 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); | |
8850 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) | |
8851 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); | |
8852 | ||
1e02ce4c | 8853 | cr4 = cr4_read_shadow(); |
d974baa3 AL |
8854 | if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) { |
8855 | vmcs_writel(HOST_CR4, cr4); | |
8856 | vmx->host_state.vmcs_host_cr4 = cr4; | |
8857 | } | |
8858 | ||
104f226b AK |
8859 | /* When single-stepping over STI and MOV SS, we must clear the |
8860 | * corresponding interruptibility bits in the guest state. Otherwise | |
8861 | * vmentry fails as it then expects bit 14 (BS) in pending debug | |
8862 | * exceptions being set, but that's not correct for the guest debugging | |
8863 | * case. */ | |
8864 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) | |
8865 | vmx_set_interrupt_shadow(vcpu, 0); | |
8866 | ||
1be0e61c XG |
8867 | if (vmx->guest_pkru_valid) |
8868 | __write_pkru(vmx->guest_pkru); | |
8869 | ||
d7cd9796 | 8870 | atomic_switch_perf_msrs(vmx); |
2a7921b7 | 8871 | debugctlmsr = get_debugctlmsr(); |
d7cd9796 | 8872 | |
64672c95 YJ |
8873 | vmx_arm_hv_timer(vcpu); |
8874 | ||
d462b819 | 8875 | vmx->__launched = vmx->loaded_vmcs->launched; |
104f226b | 8876 | asm( |
6aa8b732 | 8877 | /* Store host registers */ |
b188c81f AK |
8878 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
8879 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ | |
8880 | "push %%" _ASM_CX " \n\t" | |
8881 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" | |
313dbd49 | 8882 | "je 1f \n\t" |
b188c81f | 8883 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
4ecac3fd | 8884 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
313dbd49 | 8885 | "1: \n\t" |
d3edefc0 | 8886 | /* Reload cr2 if changed */ |
b188c81f AK |
8887 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
8888 | "mov %%cr2, %%" _ASM_DX " \n\t" | |
8889 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" | |
d3edefc0 | 8890 | "je 2f \n\t" |
b188c81f | 8891 | "mov %%" _ASM_AX", %%cr2 \n\t" |
d3edefc0 | 8892 | "2: \n\t" |
6aa8b732 | 8893 | /* Check if vmlaunch of vmresume is needed */ |
e08aa78a | 8894 | "cmpl $0, %c[launched](%0) \n\t" |
6aa8b732 | 8895 | /* Load guest registers. Don't clobber flags. */ |
b188c81f AK |
8896 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
8897 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" | |
8898 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" | |
8899 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" | |
8900 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" | |
8901 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" | |
05b3e0c2 | 8902 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
8903 | "mov %c[r8](%0), %%r8 \n\t" |
8904 | "mov %c[r9](%0), %%r9 \n\t" | |
8905 | "mov %c[r10](%0), %%r10 \n\t" | |
8906 | "mov %c[r11](%0), %%r11 \n\t" | |
8907 | "mov %c[r12](%0), %%r12 \n\t" | |
8908 | "mov %c[r13](%0), %%r13 \n\t" | |
8909 | "mov %c[r14](%0), %%r14 \n\t" | |
8910 | "mov %c[r15](%0), %%r15 \n\t" | |
6aa8b732 | 8911 | #endif |
b188c81f | 8912 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
c801949d | 8913 | |
6aa8b732 | 8914 | /* Enter guest mode */ |
83287ea4 | 8915 | "jne 1f \n\t" |
4ecac3fd | 8916 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
83287ea4 AK |
8917 | "jmp 2f \n\t" |
8918 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" | |
8919 | "2: " | |
6aa8b732 | 8920 | /* Save guest registers, load host registers, keep flags */ |
b188c81f | 8921 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
40712fae | 8922 | "pop %0 \n\t" |
b188c81f AK |
8923 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
8924 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" | |
8925 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" | |
8926 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" | |
8927 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" | |
8928 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" | |
8929 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" | |
05b3e0c2 | 8930 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
8931 | "mov %%r8, %c[r8](%0) \n\t" |
8932 | "mov %%r9, %c[r9](%0) \n\t" | |
8933 | "mov %%r10, %c[r10](%0) \n\t" | |
8934 | "mov %%r11, %c[r11](%0) \n\t" | |
8935 | "mov %%r12, %c[r12](%0) \n\t" | |
8936 | "mov %%r13, %c[r13](%0) \n\t" | |
8937 | "mov %%r14, %c[r14](%0) \n\t" | |
8938 | "mov %%r15, %c[r15](%0) \n\t" | |
6aa8b732 | 8939 | #endif |
b188c81f AK |
8940 | "mov %%cr2, %%" _ASM_AX " \n\t" |
8941 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" | |
c801949d | 8942 | |
b188c81f | 8943 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
e08aa78a | 8944 | "setbe %c[fail](%0) \n\t" |
83287ea4 AK |
8945 | ".pushsection .rodata \n\t" |
8946 | ".global vmx_return \n\t" | |
8947 | "vmx_return: " _ASM_PTR " 2b \n\t" | |
8948 | ".popsection" | |
e08aa78a | 8949 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
d462b819 | 8950 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
e08aa78a | 8951 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
313dbd49 | 8952 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
ad312c7c ZX |
8953 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
8954 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), | |
8955 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), | |
8956 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), | |
8957 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), | |
8958 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), | |
8959 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), | |
05b3e0c2 | 8960 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
8961 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
8962 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), | |
8963 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), | |
8964 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), | |
8965 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), | |
8966 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), | |
8967 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), | |
8968 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), | |
6aa8b732 | 8969 | #endif |
40712fae AK |
8970 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
8971 | [wordsize]"i"(sizeof(ulong)) | |
c2036300 LV |
8972 | : "cc", "memory" |
8973 | #ifdef CONFIG_X86_64 | |
b188c81f | 8974 | , "rax", "rbx", "rdi", "rsi" |
c2036300 | 8975 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
b188c81f AK |
8976 | #else |
8977 | , "eax", "ebx", "edi", "esi" | |
c2036300 LV |
8978 | #endif |
8979 | ); | |
6aa8b732 | 8980 | |
2a7921b7 GN |
8981 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
8982 | if (debugctlmsr) | |
8983 | update_debugctlmsr(debugctlmsr); | |
8984 | ||
aa67f609 AK |
8985 | #ifndef CONFIG_X86_64 |
8986 | /* | |
8987 | * The sysexit path does not restore ds/es, so we must set them to | |
8988 | * a reasonable value ourselves. | |
8989 | * | |
8990 | * We can't defer this to vmx_load_host_state() since that function | |
8991 | * may be executed in interrupt context, which saves and restore segments | |
8992 | * around it, nullifying its effect. | |
8993 | */ | |
8994 | loadsegment(ds, __USER_DS); | |
8995 | loadsegment(es, __USER_DS); | |
8996 | #endif | |
8997 | ||
6de4f3ad | 8998 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
6de12732 | 8999 | | (1 << VCPU_EXREG_RFLAGS) |
aff48baa | 9000 | | (1 << VCPU_EXREG_PDPTR) |
2fb92db1 | 9001 | | (1 << VCPU_EXREG_SEGMENTS) |
aff48baa | 9002 | | (1 << VCPU_EXREG_CR3)); |
5fdbf976 MT |
9003 | vcpu->arch.regs_dirty = 0; |
9004 | ||
1155f76a AK |
9005 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
9006 | ||
d462b819 | 9007 | vmx->loaded_vmcs->launched = 1; |
1b6269db | 9008 | |
51aa01d1 | 9009 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
51aa01d1 | 9010 | |
1be0e61c XG |
9011 | /* |
9012 | * eager fpu is enabled if PKEY is supported and CR4 is switched | |
9013 | * back on host, so it is safe to read guest PKRU from current | |
9014 | * XSAVE. | |
9015 | */ | |
9016 | if (boot_cpu_has(X86_FEATURE_OSPKE)) { | |
9017 | vmx->guest_pkru = __read_pkru(); | |
9018 | if (vmx->guest_pkru != vmx->host_pkru) { | |
9019 | vmx->guest_pkru_valid = true; | |
9020 | __write_pkru(vmx->host_pkru); | |
9021 | } else | |
9022 | vmx->guest_pkru_valid = false; | |
9023 | } | |
9024 | ||
e0b890d3 GN |
9025 | /* |
9026 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if | |
9027 | * we did not inject a still-pending event to L1 now because of | |
9028 | * nested_run_pending, we need to re-enable this bit. | |
9029 | */ | |
9030 | if (vmx->nested.nested_run_pending) | |
9031 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
9032 | ||
9033 | vmx->nested.nested_run_pending = 0; | |
9034 | ||
51aa01d1 AK |
9035 | vmx_complete_atomic_exit(vmx); |
9036 | vmx_recover_nmi_blocking(vmx); | |
cf393f75 | 9037 | vmx_complete_interrupts(vmx); |
6aa8b732 AK |
9038 | } |
9039 | ||
1279a6b1 | 9040 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
4fa7734c PB |
9041 | { |
9042 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9043 | int cpu; | |
9044 | ||
1279a6b1 | 9045 | if (vmx->loaded_vmcs == vmcs) |
4fa7734c PB |
9046 | return; |
9047 | ||
9048 | cpu = get_cpu(); | |
1279a6b1 | 9049 | vmx->loaded_vmcs = vmcs; |
4fa7734c PB |
9050 | vmx_vcpu_put(vcpu); |
9051 | vmx_vcpu_load(vcpu, cpu); | |
9052 | vcpu->cpu = cpu; | |
9053 | put_cpu(); | |
9054 | } | |
9055 | ||
2f1fe811 JM |
9056 | /* |
9057 | * Ensure that the current vmcs of the logical processor is the | |
9058 | * vmcs01 of the vcpu before calling free_nested(). | |
9059 | */ | |
9060 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) | |
9061 | { | |
9062 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9063 | int r; | |
9064 | ||
9065 | r = vcpu_load(vcpu); | |
9066 | BUG_ON(r); | |
1279a6b1 | 9067 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
2f1fe811 JM |
9068 | free_nested(vmx); |
9069 | vcpu_put(vcpu); | |
9070 | } | |
9071 | ||
6aa8b732 AK |
9072 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
9073 | { | |
fb3f0f51 RR |
9074 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
9075 | ||
843e4330 | 9076 | if (enable_pml) |
a3eaa864 | 9077 | vmx_destroy_pml_buffer(vmx); |
991e7a0e | 9078 | free_vpid(vmx->vpid); |
4fa7734c | 9079 | leave_guest_mode(vcpu); |
2f1fe811 | 9080 | vmx_free_vcpu_nested(vcpu); |
4fa7734c | 9081 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 RR |
9082 | kfree(vmx->guest_msrs); |
9083 | kvm_vcpu_uninit(vcpu); | |
a4770347 | 9084 | kmem_cache_free(kvm_vcpu_cache, vmx); |
6aa8b732 AK |
9085 | } |
9086 | ||
fb3f0f51 | 9087 | static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) |
6aa8b732 | 9088 | { |
fb3f0f51 | 9089 | int err; |
c16f862d | 9090 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
15ad7146 | 9091 | int cpu; |
6aa8b732 | 9092 | |
a2fa3e9f | 9093 | if (!vmx) |
fb3f0f51 RR |
9094 | return ERR_PTR(-ENOMEM); |
9095 | ||
991e7a0e | 9096 | vmx->vpid = allocate_vpid(); |
2384d2b3 | 9097 | |
fb3f0f51 RR |
9098 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
9099 | if (err) | |
9100 | goto free_vcpu; | |
965b58a5 | 9101 | |
4e59516a PF |
9102 | err = -ENOMEM; |
9103 | ||
9104 | /* | |
9105 | * If PML is turned on, failure on enabling PML just results in failure | |
9106 | * of creating the vcpu, therefore we can simplify PML logic (by | |
9107 | * avoiding dealing with cases, such as enabling PML partially on vcpus | |
9108 | * for the guest, etc. | |
9109 | */ | |
9110 | if (enable_pml) { | |
9111 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
9112 | if (!vmx->pml_pg) | |
9113 | goto uninit_vcpu; | |
9114 | } | |
9115 | ||
a2fa3e9f | 9116 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
03916db9 PB |
9117 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
9118 | > PAGE_SIZE); | |
0123be42 | 9119 | |
4e59516a PF |
9120 | if (!vmx->guest_msrs) |
9121 | goto free_pml; | |
965b58a5 | 9122 | |
d462b819 NHE |
9123 | vmx->loaded_vmcs = &vmx->vmcs01; |
9124 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); | |
355f4fb1 | 9125 | vmx->loaded_vmcs->shadow_vmcs = NULL; |
d462b819 | 9126 | if (!vmx->loaded_vmcs->vmcs) |
fb3f0f51 | 9127 | goto free_msrs; |
d462b819 | 9128 | loaded_vmcs_init(vmx->loaded_vmcs); |
a2fa3e9f | 9129 | |
15ad7146 AK |
9130 | cpu = get_cpu(); |
9131 | vmx_vcpu_load(&vmx->vcpu, cpu); | |
e48672fa | 9132 | vmx->vcpu.cpu = cpu; |
8b9cf98c | 9133 | err = vmx_vcpu_setup(vmx); |
fb3f0f51 | 9134 | vmx_vcpu_put(&vmx->vcpu); |
15ad7146 | 9135 | put_cpu(); |
fb3f0f51 RR |
9136 | if (err) |
9137 | goto free_vmcs; | |
35754c98 | 9138 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
be6d05cf JK |
9139 | err = alloc_apic_access_page(kvm); |
9140 | if (err) | |
5e4a0b3c | 9141 | goto free_vmcs; |
a63cb560 | 9142 | } |
fb3f0f51 | 9143 | |
b927a3ce SY |
9144 | if (enable_ept) { |
9145 | if (!kvm->arch.ept_identity_map_addr) | |
9146 | kvm->arch.ept_identity_map_addr = | |
9147 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; | |
f51770ed TC |
9148 | err = init_rmode_identity_map(kvm); |
9149 | if (err) | |
93ea5388 | 9150 | goto free_vmcs; |
b927a3ce | 9151 | } |
b7ebfb05 | 9152 | |
5c614b35 | 9153 | if (nested) { |
b9c237bb | 9154 | nested_vmx_setup_ctls_msrs(vmx); |
5c614b35 WL |
9155 | vmx->nested.vpid02 = allocate_vpid(); |
9156 | } | |
b9c237bb | 9157 | |
705699a1 | 9158 | vmx->nested.posted_intr_nv = -1; |
a9d30f33 NHE |
9159 | vmx->nested.current_vmptr = -1ull; |
9160 | vmx->nested.current_vmcs12 = NULL; | |
9161 | ||
37e4c997 HZ |
9162 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
9163 | ||
fb3f0f51 RR |
9164 | return &vmx->vcpu; |
9165 | ||
9166 | free_vmcs: | |
5c614b35 | 9167 | free_vpid(vmx->nested.vpid02); |
5f3fbc34 | 9168 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 | 9169 | free_msrs: |
fb3f0f51 | 9170 | kfree(vmx->guest_msrs); |
4e59516a PF |
9171 | free_pml: |
9172 | vmx_destroy_pml_buffer(vmx); | |
fb3f0f51 RR |
9173 | uninit_vcpu: |
9174 | kvm_vcpu_uninit(&vmx->vcpu); | |
9175 | free_vcpu: | |
991e7a0e | 9176 | free_vpid(vmx->vpid); |
a4770347 | 9177 | kmem_cache_free(kvm_vcpu_cache, vmx); |
fb3f0f51 | 9178 | return ERR_PTR(err); |
6aa8b732 AK |
9179 | } |
9180 | ||
002c7f7c YS |
9181 | static void __init vmx_check_processor_compat(void *rtn) |
9182 | { | |
9183 | struct vmcs_config vmcs_conf; | |
9184 | ||
9185 | *(int *)rtn = 0; | |
9186 | if (setup_vmcs_config(&vmcs_conf) < 0) | |
9187 | *(int *)rtn = -EIO; | |
9188 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { | |
9189 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", | |
9190 | smp_processor_id()); | |
9191 | *(int *)rtn = -EIO; | |
9192 | } | |
9193 | } | |
9194 | ||
67253af5 SY |
9195 | static int get_ept_level(void) |
9196 | { | |
9197 | return VMX_EPT_DEFAULT_GAW + 1; | |
9198 | } | |
9199 | ||
4b12f0de | 9200 | static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
64d4d521 | 9201 | { |
b18d5431 XG |
9202 | u8 cache; |
9203 | u64 ipat = 0; | |
4b12f0de | 9204 | |
522c68c4 | 9205 | /* For VT-d and EPT combination |
606decd6 | 9206 | * 1. MMIO: always map as UC |
522c68c4 SY |
9207 | * 2. EPT with VT-d: |
9208 | * a. VT-d without snooping control feature: can't guarantee the | |
606decd6 | 9209 | * result, try to trust guest. |
522c68c4 SY |
9210 | * b. VT-d with snooping control feature: snooping control feature of |
9211 | * VT-d engine can guarantee the cache correctness. Just set it | |
9212 | * to WB to keep consistent with host. So the same as item 3. | |
a19a6d11 | 9213 | * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep |
522c68c4 SY |
9214 | * consistent with host MTRR |
9215 | */ | |
606decd6 PB |
9216 | if (is_mmio) { |
9217 | cache = MTRR_TYPE_UNCACHABLE; | |
9218 | goto exit; | |
9219 | } | |
9220 | ||
9221 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { | |
b18d5431 XG |
9222 | ipat = VMX_EPT_IPAT_BIT; |
9223 | cache = MTRR_TYPE_WRBACK; | |
9224 | goto exit; | |
9225 | } | |
9226 | ||
9227 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { | |
9228 | ipat = VMX_EPT_IPAT_BIT; | |
0da029ed | 9229 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
fb279950 XG |
9230 | cache = MTRR_TYPE_WRBACK; |
9231 | else | |
9232 | cache = MTRR_TYPE_UNCACHABLE; | |
b18d5431 XG |
9233 | goto exit; |
9234 | } | |
9235 | ||
ff53604b | 9236 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
b18d5431 XG |
9237 | |
9238 | exit: | |
9239 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; | |
64d4d521 SY |
9240 | } |
9241 | ||
17cc3935 | 9242 | static int vmx_get_lpage_level(void) |
344f414f | 9243 | { |
878403b7 SY |
9244 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
9245 | return PT_DIRECTORY_LEVEL; | |
9246 | else | |
9247 | /* For shadow and EPT supported 1GB page */ | |
9248 | return PT_PDPE_LEVEL; | |
344f414f JR |
9249 | } |
9250 | ||
feda805f XG |
9251 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
9252 | { | |
9253 | /* | |
9254 | * These bits in the secondary execution controls field | |
9255 | * are dynamic, the others are mostly based on the hypervisor | |
9256 | * architecture and the guest's CPUID. Do not touch the | |
9257 | * dynamic bits. | |
9258 | */ | |
9259 | u32 mask = | |
9260 | SECONDARY_EXEC_SHADOW_VMCS | | |
9261 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | | |
9262 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
9263 | ||
9264 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
9265 | ||
9266 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, | |
9267 | (new_ctl & ~mask) | (cur_ctl & mask)); | |
9268 | } | |
9269 | ||
8322ebbb DM |
9270 | /* |
9271 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits | |
9272 | * (indicating "allowed-1") if they are supported in the guest's CPUID. | |
9273 | */ | |
9274 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) | |
9275 | { | |
9276 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9277 | struct kvm_cpuid_entry2 *entry; | |
9278 | ||
9279 | vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff; | |
9280 | vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE; | |
9281 | ||
9282 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ | |
9283 | if (entry && (entry->_reg & (_cpuid_mask))) \ | |
9284 | vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \ | |
9285 | } while (0) | |
9286 | ||
9287 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); | |
9288 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); | |
9289 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); | |
9290 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); | |
9291 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); | |
9292 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); | |
9293 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); | |
9294 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); | |
9295 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); | |
9296 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); | |
9297 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); | |
9298 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); | |
9299 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); | |
9300 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); | |
9301 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); | |
9302 | ||
9303 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); | |
9304 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); | |
9305 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); | |
9306 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); | |
9307 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); | |
9308 | /* TODO: Use X86_CR4_UMIP and X86_FEATURE_UMIP macros */ | |
9309 | cr4_fixed1_update(bit(11), ecx, bit(2)); | |
9310 | ||
9311 | #undef cr4_fixed1_update | |
9312 | } | |
9313 | ||
0e851880 SY |
9314 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
9315 | { | |
4e47c7a6 SY |
9316 | struct kvm_cpuid_entry2 *best; |
9317 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
feda805f | 9318 | u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx); |
4e47c7a6 | 9319 | |
4e47c7a6 | 9320 | if (vmx_rdtscp_supported()) { |
1cea0ce6 XG |
9321 | bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu); |
9322 | if (!rdtscp_enabled) | |
feda805f | 9323 | secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP; |
f36201e5 | 9324 | |
8b97265a | 9325 | if (nested) { |
1cea0ce6 | 9326 | if (rdtscp_enabled) |
8b97265a PB |
9327 | vmx->nested.nested_vmx_secondary_ctls_high |= |
9328 | SECONDARY_EXEC_RDTSCP; | |
9329 | else | |
9330 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
9331 | ~SECONDARY_EXEC_RDTSCP; | |
9332 | } | |
4e47c7a6 | 9333 | } |
ad756a16 | 9334 | |
ad756a16 MJ |
9335 | /* Exposing INVPCID only when PCID is exposed */ |
9336 | best = kvm_find_cpuid_entry(vcpu, 0x7, 0); | |
9337 | if (vmx_invpcid_supported() && | |
29541bb8 XG |
9338 | (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) || |
9339 | !guest_cpuid_has_pcid(vcpu))) { | |
feda805f | 9340 | secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
29541bb8 | 9341 | |
ad756a16 | 9342 | if (best) |
4f977045 | 9343 | best->ebx &= ~bit(X86_FEATURE_INVPCID); |
ad756a16 | 9344 | } |
8b3e34e4 | 9345 | |
45bdbcfd HH |
9346 | if (cpu_has_secondary_exec_ctrls()) |
9347 | vmcs_set_secondary_exec_control(secondary_exec_ctl); | |
feda805f | 9348 | |
37e4c997 HZ |
9349 | if (nested_vmx_allowed(vcpu)) |
9350 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
9351 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
9352 | else | |
9353 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
9354 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
8322ebbb DM |
9355 | |
9356 | if (nested_vmx_allowed(vcpu)) | |
9357 | nested_vmx_cr_fixed1_bits_update(vcpu); | |
0e851880 SY |
9358 | } |
9359 | ||
d4330ef2 JR |
9360 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
9361 | { | |
7b8050f5 NHE |
9362 | if (func == 1 && nested) |
9363 | entry->ecx |= bit(X86_FEATURE_VMX); | |
d4330ef2 JR |
9364 | } |
9365 | ||
25d92081 YZ |
9366 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
9367 | struct x86_exception *fault) | |
9368 | { | |
533558bc | 9369 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
c5f983f6 | 9370 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
533558bc | 9371 | u32 exit_reason; |
c5f983f6 | 9372 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
25d92081 | 9373 | |
c5f983f6 BD |
9374 | if (vmx->nested.pml_full) { |
9375 | exit_reason = EXIT_REASON_PML_FULL; | |
9376 | vmx->nested.pml_full = false; | |
9377 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; | |
9378 | } else if (fault->error_code & PFERR_RSVD_MASK) | |
533558bc | 9379 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
25d92081 | 9380 | else |
533558bc | 9381 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
c5f983f6 BD |
9382 | |
9383 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); | |
25d92081 YZ |
9384 | vmcs12->guest_physical_address = fault->address; |
9385 | } | |
9386 | ||
995f00a6 PF |
9387 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
9388 | { | |
9389 | return nested_ept_get_cr3(vcpu) & VMX_EPT_AD_ENABLE_BIT; | |
9390 | } | |
9391 | ||
155a97a3 NHE |
9392 | /* Callbacks for nested_ept_init_mmu_context: */ |
9393 | ||
9394 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) | |
9395 | { | |
9396 | /* return the page table to be shadowed - in our case, EPT12 */ | |
9397 | return get_vmcs12(vcpu)->ept_pointer; | |
9398 | } | |
9399 | ||
ae1e2d10 | 9400 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
155a97a3 | 9401 | { |
995f00a6 | 9402 | bool wants_ad; |
ae1e2d10 | 9403 | |
ad896af0 | 9404 | WARN_ON(mmu_is_nested(vcpu)); |
995f00a6 PF |
9405 | wants_ad = nested_ept_ad_enabled(vcpu); |
9406 | if (wants_ad && !enable_ept_ad_bits) | |
ae1e2d10 PB |
9407 | return 1; |
9408 | ||
9409 | kvm_mmu_unload(vcpu); | |
ad896af0 | 9410 | kvm_init_shadow_ept_mmu(vcpu, |
b9c237bb | 9411 | to_vmx(vcpu)->nested.nested_vmx_ept_caps & |
ae1e2d10 | 9412 | VMX_EPT_EXECUTE_ONLY_BIT, |
995f00a6 | 9413 | wants_ad); |
155a97a3 NHE |
9414 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
9415 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; | |
9416 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; | |
9417 | ||
9418 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; | |
ae1e2d10 | 9419 | return 0; |
155a97a3 NHE |
9420 | } |
9421 | ||
9422 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) | |
9423 | { | |
9424 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; | |
9425 | } | |
9426 | ||
19d5f10b EK |
9427 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
9428 | u16 error_code) | |
9429 | { | |
9430 | bool inequality, bit; | |
9431 | ||
9432 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; | |
9433 | inequality = | |
9434 | (error_code & vmcs12->page_fault_error_code_mask) != | |
9435 | vmcs12->page_fault_error_code_match; | |
9436 | return inequality ^ bit; | |
9437 | } | |
9438 | ||
feaf0c7d GN |
9439 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
9440 | struct x86_exception *fault) | |
9441 | { | |
9442 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
9443 | ||
9444 | WARN_ON(!is_guest_mode(vcpu)); | |
9445 | ||
19d5f10b | 9446 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) |
533558bc JK |
9447 | nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason, |
9448 | vmcs_read32(VM_EXIT_INTR_INFO), | |
9449 | vmcs_readl(EXIT_QUALIFICATION)); | |
feaf0c7d GN |
9450 | else |
9451 | kvm_inject_page_fault(vcpu, fault); | |
9452 | } | |
9453 | ||
6beb7bd5 JM |
9454 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
9455 | struct vmcs12 *vmcs12); | |
9456 | ||
9457 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, | |
a2bcba50 WL |
9458 | struct vmcs12 *vmcs12) |
9459 | { | |
9460 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6beb7bd5 | 9461 | u64 hpa; |
a2bcba50 WL |
9462 | |
9463 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
a2bcba50 WL |
9464 | /* |
9465 | * Translate L1 physical address to host physical | |
9466 | * address for vmcs02. Keep the page pinned, so this | |
9467 | * physical address remains valid. We keep a reference | |
9468 | * to it so we can release it later. | |
9469 | */ | |
9470 | if (vmx->nested.apic_access_page) /* shouldn't happen */ | |
9471 | nested_release_page(vmx->nested.apic_access_page); | |
9472 | vmx->nested.apic_access_page = | |
9473 | nested_get_page(vcpu, vmcs12->apic_access_addr); | |
6beb7bd5 JM |
9474 | /* |
9475 | * If translation failed, no matter: This feature asks | |
9476 | * to exit when accessing the given address, and if it | |
9477 | * can never be accessed, this feature won't do | |
9478 | * anything anyway. | |
9479 | */ | |
9480 | if (vmx->nested.apic_access_page) { | |
9481 | hpa = page_to_phys(vmx->nested.apic_access_page); | |
9482 | vmcs_write64(APIC_ACCESS_ADDR, hpa); | |
9483 | } else { | |
9484 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
9485 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
9486 | } | |
9487 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && | |
9488 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { | |
9489 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
9490 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
9491 | kvm_vcpu_reload_apic_access_page(vcpu); | |
a2bcba50 | 9492 | } |
a7c0b07d WL |
9493 | |
9494 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { | |
a7c0b07d WL |
9495 | if (vmx->nested.virtual_apic_page) /* shouldn't happen */ |
9496 | nested_release_page(vmx->nested.virtual_apic_page); | |
9497 | vmx->nested.virtual_apic_page = | |
9498 | nested_get_page(vcpu, vmcs12->virtual_apic_page_addr); | |
9499 | ||
9500 | /* | |
6beb7bd5 JM |
9501 | * If translation failed, VM entry will fail because |
9502 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. | |
9503 | * Failing the vm entry is _not_ what the processor | |
9504 | * does but it's basically the only possibility we | |
9505 | * have. We could still enter the guest if CR8 load | |
9506 | * exits are enabled, CR8 store exits are enabled, and | |
9507 | * virtualize APIC access is disabled; in this case | |
9508 | * the processor would never use the TPR shadow and we | |
9509 | * could simply clear the bit from the execution | |
9510 | * control. But such a configuration is useless, so | |
9511 | * let's keep the code simple. | |
a7c0b07d | 9512 | */ |
6beb7bd5 JM |
9513 | if (vmx->nested.virtual_apic_page) { |
9514 | hpa = page_to_phys(vmx->nested.virtual_apic_page); | |
9515 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); | |
9516 | } | |
a7c0b07d WL |
9517 | } |
9518 | ||
705699a1 | 9519 | if (nested_cpu_has_posted_intr(vmcs12)) { |
705699a1 WV |
9520 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
9521 | kunmap(vmx->nested.pi_desc_page); | |
9522 | nested_release_page(vmx->nested.pi_desc_page); | |
9523 | } | |
9524 | vmx->nested.pi_desc_page = | |
9525 | nested_get_page(vcpu, vmcs12->posted_intr_desc_addr); | |
705699a1 WV |
9526 | vmx->nested.pi_desc = |
9527 | (struct pi_desc *)kmap(vmx->nested.pi_desc_page); | |
9528 | if (!vmx->nested.pi_desc) { | |
9529 | nested_release_page_clean(vmx->nested.pi_desc_page); | |
6beb7bd5 | 9530 | return; |
705699a1 WV |
9531 | } |
9532 | vmx->nested.pi_desc = | |
9533 | (struct pi_desc *)((void *)vmx->nested.pi_desc + | |
9534 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
9535 | (PAGE_SIZE - 1))); | |
6beb7bd5 JM |
9536 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
9537 | page_to_phys(vmx->nested.pi_desc_page) + | |
9538 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
9539 | (PAGE_SIZE - 1))); | |
705699a1 | 9540 | } |
6beb7bd5 JM |
9541 | if (cpu_has_vmx_msr_bitmap() && |
9542 | nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) && | |
9543 | nested_vmx_merge_msr_bitmap(vcpu, vmcs12)) | |
9544 | ; | |
9545 | else | |
9546 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, | |
9547 | CPU_BASED_USE_MSR_BITMAPS); | |
a2bcba50 WL |
9548 | } |
9549 | ||
f4124500 JK |
9550 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
9551 | { | |
9552 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; | |
9553 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9554 | ||
9555 | if (vcpu->arch.virtual_tsc_khz == 0) | |
9556 | return; | |
9557 | ||
9558 | /* Make sure short timeouts reliably trigger an immediate vmexit. | |
9559 | * hrtimer_start does not guarantee this. */ | |
9560 | if (preemption_timeout <= 1) { | |
9561 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); | |
9562 | return; | |
9563 | } | |
9564 | ||
9565 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
9566 | preemption_timeout *= 1000000; | |
9567 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); | |
9568 | hrtimer_start(&vmx->nested.preemption_timer, | |
9569 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); | |
9570 | } | |
9571 | ||
3af18d9c WV |
9572 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
9573 | struct vmcs12 *vmcs12) | |
9574 | { | |
9575 | int maxphyaddr; | |
9576 | u64 addr; | |
9577 | ||
9578 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) | |
9579 | return 0; | |
9580 | ||
9581 | if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) { | |
9582 | WARN_ON(1); | |
9583 | return -EINVAL; | |
9584 | } | |
9585 | maxphyaddr = cpuid_maxphyaddr(vcpu); | |
9586 | ||
9587 | if (!PAGE_ALIGNED(vmcs12->msr_bitmap) || | |
9588 | ((addr + PAGE_SIZE) >> maxphyaddr)) | |
9589 | return -EINVAL; | |
9590 | ||
9591 | return 0; | |
9592 | } | |
9593 | ||
9594 | /* | |
9595 | * Merge L0's and L1's MSR bitmap, return false to indicate that | |
9596 | * we do not use the hardware. | |
9597 | */ | |
9598 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, | |
9599 | struct vmcs12 *vmcs12) | |
9600 | { | |
82f0dd4b | 9601 | int msr; |
f2b93280 | 9602 | struct page *page; |
d048c098 RK |
9603 | unsigned long *msr_bitmap_l1; |
9604 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap; | |
f2b93280 | 9605 | |
d048c098 | 9606 | /* This shortcut is ok because we support only x2APIC MSRs so far. */ |
f2b93280 WV |
9607 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12)) |
9608 | return false; | |
9609 | ||
9610 | page = nested_get_page(vcpu, vmcs12->msr_bitmap); | |
05d8d346 | 9611 | if (!page) |
f2b93280 | 9612 | return false; |
d048c098 | 9613 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
f2b93280 | 9614 | |
d048c098 RK |
9615 | memset(msr_bitmap_l0, 0xff, PAGE_SIZE); |
9616 | ||
f2b93280 | 9617 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
82f0dd4b WV |
9618 | if (nested_cpu_has_apic_reg_virt(vmcs12)) |
9619 | for (msr = 0x800; msr <= 0x8ff; msr++) | |
9620 | nested_vmx_disable_intercept_for_msr( | |
d048c098 | 9621 | msr_bitmap_l1, msr_bitmap_l0, |
82f0dd4b | 9622 | msr, MSR_TYPE_R); |
d048c098 RK |
9623 | |
9624 | nested_vmx_disable_intercept_for_msr( | |
9625 | msr_bitmap_l1, msr_bitmap_l0, | |
f2b93280 WV |
9626 | APIC_BASE_MSR + (APIC_TASKPRI >> 4), |
9627 | MSR_TYPE_R | MSR_TYPE_W); | |
d048c098 | 9628 | |
608406e2 | 9629 | if (nested_cpu_has_vid(vmcs12)) { |
608406e2 | 9630 | nested_vmx_disable_intercept_for_msr( |
d048c098 | 9631 | msr_bitmap_l1, msr_bitmap_l0, |
608406e2 WV |
9632 | APIC_BASE_MSR + (APIC_EOI >> 4), |
9633 | MSR_TYPE_W); | |
9634 | nested_vmx_disable_intercept_for_msr( | |
d048c098 | 9635 | msr_bitmap_l1, msr_bitmap_l0, |
608406e2 WV |
9636 | APIC_BASE_MSR + (APIC_SELF_IPI >> 4), |
9637 | MSR_TYPE_W); | |
9638 | } | |
82f0dd4b | 9639 | } |
f2b93280 WV |
9640 | kunmap(page); |
9641 | nested_release_page_clean(page); | |
9642 | ||
9643 | return true; | |
9644 | } | |
9645 | ||
9646 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, | |
9647 | struct vmcs12 *vmcs12) | |
9648 | { | |
82f0dd4b | 9649 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
608406e2 | 9650 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
705699a1 WV |
9651 | !nested_cpu_has_vid(vmcs12) && |
9652 | !nested_cpu_has_posted_intr(vmcs12)) | |
f2b93280 WV |
9653 | return 0; |
9654 | ||
9655 | /* | |
9656 | * If virtualize x2apic mode is enabled, | |
9657 | * virtualize apic access must be disabled. | |
9658 | */ | |
82f0dd4b WV |
9659 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
9660 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
f2b93280 WV |
9661 | return -EINVAL; |
9662 | ||
608406e2 WV |
9663 | /* |
9664 | * If virtual interrupt delivery is enabled, | |
9665 | * we must exit on external interrupts. | |
9666 | */ | |
9667 | if (nested_cpu_has_vid(vmcs12) && | |
9668 | !nested_exit_on_intr(vcpu)) | |
9669 | return -EINVAL; | |
9670 | ||
705699a1 WV |
9671 | /* |
9672 | * bits 15:8 should be zero in posted_intr_nv, | |
9673 | * the descriptor address has been already checked | |
9674 | * in nested_get_vmcs12_pages. | |
9675 | */ | |
9676 | if (nested_cpu_has_posted_intr(vmcs12) && | |
9677 | (!nested_cpu_has_vid(vmcs12) || | |
9678 | !nested_exit_intr_ack_set(vcpu) || | |
9679 | vmcs12->posted_intr_nv & 0xff00)) | |
9680 | return -EINVAL; | |
9681 | ||
f2b93280 WV |
9682 | /* tpr shadow is needed by all apicv features. */ |
9683 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
9684 | return -EINVAL; | |
9685 | ||
9686 | return 0; | |
3af18d9c WV |
9687 | } |
9688 | ||
e9ac033e EK |
9689 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
9690 | unsigned long count_field, | |
92d71bc6 | 9691 | unsigned long addr_field) |
ff651cb6 | 9692 | { |
92d71bc6 | 9693 | int maxphyaddr; |
e9ac033e EK |
9694 | u64 count, addr; |
9695 | ||
9696 | if (vmcs12_read_any(vcpu, count_field, &count) || | |
9697 | vmcs12_read_any(vcpu, addr_field, &addr)) { | |
9698 | WARN_ON(1); | |
9699 | return -EINVAL; | |
9700 | } | |
9701 | if (count == 0) | |
9702 | return 0; | |
92d71bc6 | 9703 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
e9ac033e EK |
9704 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
9705 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { | |
bbe41b95 | 9706 | pr_debug_ratelimited( |
e9ac033e EK |
9707 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
9708 | addr_field, maxphyaddr, count, addr); | |
9709 | return -EINVAL; | |
9710 | } | |
9711 | return 0; | |
9712 | } | |
9713 | ||
9714 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, | |
9715 | struct vmcs12 *vmcs12) | |
9716 | { | |
e9ac033e EK |
9717 | if (vmcs12->vm_exit_msr_load_count == 0 && |
9718 | vmcs12->vm_exit_msr_store_count == 0 && | |
9719 | vmcs12->vm_entry_msr_load_count == 0) | |
9720 | return 0; /* Fast path */ | |
e9ac033e | 9721 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
92d71bc6 | 9722 | VM_EXIT_MSR_LOAD_ADDR) || |
e9ac033e | 9723 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
92d71bc6 | 9724 | VM_EXIT_MSR_STORE_ADDR) || |
e9ac033e | 9725 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
92d71bc6 | 9726 | VM_ENTRY_MSR_LOAD_ADDR)) |
e9ac033e EK |
9727 | return -EINVAL; |
9728 | return 0; | |
9729 | } | |
9730 | ||
c5f983f6 BD |
9731 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
9732 | struct vmcs12 *vmcs12) | |
9733 | { | |
9734 | u64 address = vmcs12->pml_address; | |
9735 | int maxphyaddr = cpuid_maxphyaddr(vcpu); | |
9736 | ||
9737 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { | |
9738 | if (!nested_cpu_has_ept(vmcs12) || | |
9739 | !IS_ALIGNED(address, 4096) || | |
9740 | address >> maxphyaddr) | |
9741 | return -EINVAL; | |
9742 | } | |
9743 | ||
9744 | return 0; | |
9745 | } | |
9746 | ||
e9ac033e EK |
9747 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
9748 | struct vmx_msr_entry *e) | |
9749 | { | |
9750 | /* x2APIC MSR accesses are not allowed */ | |
8a9781f7 | 9751 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
e9ac033e EK |
9752 | return -EINVAL; |
9753 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ | |
9754 | e->index == MSR_IA32_UCODE_REV) | |
9755 | return -EINVAL; | |
9756 | if (e->reserved != 0) | |
ff651cb6 WV |
9757 | return -EINVAL; |
9758 | return 0; | |
9759 | } | |
9760 | ||
e9ac033e EK |
9761 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
9762 | struct vmx_msr_entry *e) | |
ff651cb6 WV |
9763 | { |
9764 | if (e->index == MSR_FS_BASE || | |
9765 | e->index == MSR_GS_BASE || | |
e9ac033e EK |
9766 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
9767 | nested_vmx_msr_check_common(vcpu, e)) | |
9768 | return -EINVAL; | |
9769 | return 0; | |
9770 | } | |
9771 | ||
9772 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, | |
9773 | struct vmx_msr_entry *e) | |
9774 | { | |
9775 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ | |
9776 | nested_vmx_msr_check_common(vcpu, e)) | |
ff651cb6 WV |
9777 | return -EINVAL; |
9778 | return 0; | |
9779 | } | |
9780 | ||
9781 | /* | |
9782 | * Load guest's/host's msr at nested entry/exit. | |
9783 | * return 0 for success, entry index for failure. | |
9784 | */ | |
9785 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
9786 | { | |
9787 | u32 i; | |
9788 | struct vmx_msr_entry e; | |
9789 | struct msr_data msr; | |
9790 | ||
9791 | msr.host_initiated = false; | |
9792 | for (i = 0; i < count; i++) { | |
54bf36aa PB |
9793 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
9794 | &e, sizeof(e))) { | |
bbe41b95 | 9795 | pr_debug_ratelimited( |
e9ac033e EK |
9796 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
9797 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 9798 | goto fail; |
e9ac033e EK |
9799 | } |
9800 | if (nested_vmx_load_msr_check(vcpu, &e)) { | |
bbe41b95 | 9801 | pr_debug_ratelimited( |
e9ac033e EK |
9802 | "%s check failed (%u, 0x%x, 0x%x)\n", |
9803 | __func__, i, e.index, e.reserved); | |
9804 | goto fail; | |
9805 | } | |
ff651cb6 WV |
9806 | msr.index = e.index; |
9807 | msr.data = e.value; | |
e9ac033e | 9808 | if (kvm_set_msr(vcpu, &msr)) { |
bbe41b95 | 9809 | pr_debug_ratelimited( |
e9ac033e EK |
9810 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
9811 | __func__, i, e.index, e.value); | |
ff651cb6 | 9812 | goto fail; |
e9ac033e | 9813 | } |
ff651cb6 WV |
9814 | } |
9815 | return 0; | |
9816 | fail: | |
9817 | return i + 1; | |
9818 | } | |
9819 | ||
9820 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
9821 | { | |
9822 | u32 i; | |
9823 | struct vmx_msr_entry e; | |
9824 | ||
9825 | for (i = 0; i < count; i++) { | |
609e36d3 | 9826 | struct msr_data msr_info; |
54bf36aa PB |
9827 | if (kvm_vcpu_read_guest(vcpu, |
9828 | gpa + i * sizeof(e), | |
9829 | &e, 2 * sizeof(u32))) { | |
bbe41b95 | 9830 | pr_debug_ratelimited( |
e9ac033e EK |
9831 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
9832 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 9833 | return -EINVAL; |
e9ac033e EK |
9834 | } |
9835 | if (nested_vmx_store_msr_check(vcpu, &e)) { | |
bbe41b95 | 9836 | pr_debug_ratelimited( |
e9ac033e EK |
9837 | "%s check failed (%u, 0x%x, 0x%x)\n", |
9838 | __func__, i, e.index, e.reserved); | |
ff651cb6 | 9839 | return -EINVAL; |
e9ac033e | 9840 | } |
609e36d3 PB |
9841 | msr_info.host_initiated = false; |
9842 | msr_info.index = e.index; | |
9843 | if (kvm_get_msr(vcpu, &msr_info)) { | |
bbe41b95 | 9844 | pr_debug_ratelimited( |
e9ac033e EK |
9845 | "%s cannot read MSR (%u, 0x%x)\n", |
9846 | __func__, i, e.index); | |
9847 | return -EINVAL; | |
9848 | } | |
54bf36aa PB |
9849 | if (kvm_vcpu_write_guest(vcpu, |
9850 | gpa + i * sizeof(e) + | |
9851 | offsetof(struct vmx_msr_entry, value), | |
9852 | &msr_info.data, sizeof(msr_info.data))) { | |
bbe41b95 | 9853 | pr_debug_ratelimited( |
e9ac033e | 9854 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
609e36d3 | 9855 | __func__, i, e.index, msr_info.data); |
e9ac033e EK |
9856 | return -EINVAL; |
9857 | } | |
ff651cb6 WV |
9858 | } |
9859 | return 0; | |
9860 | } | |
9861 | ||
1dc35dac LP |
9862 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
9863 | { | |
9864 | unsigned long invalid_mask; | |
9865 | ||
9866 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); | |
9867 | return (val & invalid_mask) == 0; | |
9868 | } | |
9869 | ||
9ed38ffa LP |
9870 | /* |
9871 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are | |
9872 | * emulating VM entry into a guest with EPT enabled. | |
9873 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code | |
9874 | * is assigned to entry_failure_code on failure. | |
9875 | */ | |
9876 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept, | |
ca0bde28 | 9877 | u32 *entry_failure_code) |
9ed38ffa | 9878 | { |
9ed38ffa | 9879 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
1dc35dac | 9880 | if (!nested_cr3_valid(vcpu, cr3)) { |
9ed38ffa LP |
9881 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
9882 | return 1; | |
9883 | } | |
9884 | ||
9885 | /* | |
9886 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and | |
9887 | * must not be dereferenced. | |
9888 | */ | |
9889 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && | |
9890 | !nested_ept) { | |
9891 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { | |
9892 | *entry_failure_code = ENTRY_FAIL_PDPTE; | |
9893 | return 1; | |
9894 | } | |
9895 | } | |
9896 | ||
9897 | vcpu->arch.cr3 = cr3; | |
9898 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
9899 | } | |
9900 | ||
9901 | kvm_mmu_reset_context(vcpu); | |
9902 | return 0; | |
9903 | } | |
9904 | ||
fe3ef05c NHE |
9905 | /* |
9906 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested | |
9907 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it | |
b4619660 | 9908 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
fe3ef05c NHE |
9909 | * guest in a way that will both be appropriate to L1's requests, and our |
9910 | * needs. In addition to modifying the active vmcs (which is vmcs02), this | |
9911 | * function also has additional necessary side-effects, like setting various | |
9912 | * vcpu->arch fields. | |
ee146c1c LP |
9913 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
9914 | * is assigned to entry_failure_code on failure. | |
fe3ef05c | 9915 | */ |
ee146c1c | 9916 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
ca0bde28 | 9917 | bool from_vmentry, u32 *entry_failure_code) |
fe3ef05c NHE |
9918 | { |
9919 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
03efce6f | 9920 | u32 exec_control, vmcs12_exec_ctrl; |
fe3ef05c NHE |
9921 | |
9922 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); | |
9923 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); | |
9924 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); | |
9925 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); | |
9926 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); | |
9927 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); | |
9928 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); | |
9929 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); | |
9930 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); | |
9931 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); | |
9932 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); | |
9933 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); | |
9934 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); | |
9935 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); | |
9936 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); | |
9937 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); | |
9938 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); | |
9939 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); | |
9940 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); | |
9941 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); | |
9942 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); | |
9943 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); | |
9944 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); | |
9945 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); | |
9946 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); | |
9947 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); | |
9948 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); | |
9949 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); | |
9950 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); | |
9951 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); | |
9952 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); | |
9953 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); | |
9954 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); | |
9955 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); | |
9956 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); | |
9957 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); | |
9958 | ||
cf8b84f4 JM |
9959 | if (from_vmentry && |
9960 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { | |
2996fca0 JK |
9961 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
9962 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); | |
9963 | } else { | |
9964 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); | |
9965 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); | |
9966 | } | |
cf8b84f4 JM |
9967 | if (from_vmentry) { |
9968 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, | |
9969 | vmcs12->vm_entry_intr_info_field); | |
9970 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, | |
9971 | vmcs12->vm_entry_exception_error_code); | |
9972 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
9973 | vmcs12->vm_entry_instruction_len); | |
9974 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, | |
9975 | vmcs12->guest_interruptibility_info); | |
9976 | } else { | |
9977 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
9978 | } | |
fe3ef05c | 9979 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
63fbf59f | 9980 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
fe3ef05c NHE |
9981 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
9982 | vmcs12->guest_pending_dbg_exceptions); | |
9983 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); | |
9984 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); | |
9985 | ||
81dc01f7 WL |
9986 | if (nested_cpu_has_xsaves(vmcs12)) |
9987 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); | |
fe3ef05c NHE |
9988 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
9989 | ||
f4124500 | 9990 | exec_control = vmcs12->pin_based_vm_exec_control; |
9314006d PB |
9991 | |
9992 | /* Preemption timer setting is only taken from vmcs01. */ | |
705699a1 | 9993 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
9314006d PB |
9994 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
9995 | if (vmx->hv_deadline_tsc == -1) | |
9996 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
705699a1 | 9997 | |
9314006d | 9998 | /* Posted interrupts setting is only taken from vmcs12. */ |
705699a1 WV |
9999 | if (nested_cpu_has_posted_intr(vmcs12)) { |
10000 | /* | |
10001 | * Note that we use L0's vector here and in | |
10002 | * vmx_deliver_nested_posted_interrupt. | |
10003 | */ | |
10004 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; | |
10005 | vmx->nested.pi_pending = false; | |
0bcf261c | 10006 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
6beb7bd5 | 10007 | } else { |
705699a1 | 10008 | exec_control &= ~PIN_BASED_POSTED_INTR; |
6beb7bd5 | 10009 | } |
705699a1 | 10010 | |
f4124500 | 10011 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
fe3ef05c | 10012 | |
f4124500 JK |
10013 | vmx->nested.preemption_timer_expired = false; |
10014 | if (nested_cpu_has_preemption_timer(vmcs12)) | |
10015 | vmx_start_preemption_timer(vcpu); | |
0238ea91 | 10016 | |
fe3ef05c NHE |
10017 | /* |
10018 | * Whether page-faults are trapped is determined by a combination of | |
10019 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. | |
10020 | * If enable_ept, L0 doesn't care about page faults and we should | |
10021 | * set all of these to L1's desires. However, if !enable_ept, L0 does | |
10022 | * care about (at least some) page faults, and because it is not easy | |
10023 | * (if at all possible?) to merge L0 and L1's desires, we simply ask | |
10024 | * to exit on each and every L2 page fault. This is done by setting | |
10025 | * MASK=MATCH=0 and (see below) EB.PF=1. | |
10026 | * Note that below we don't need special code to set EB.PF beyond the | |
10027 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, | |
10028 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when | |
10029 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. | |
10030 | * | |
10031 | * A problem with this approach (when !enable_ept) is that L1 may be | |
10032 | * injected with more page faults than it asked for. This could have | |
10033 | * caused problems, but in practice existing hypervisors don't care. | |
10034 | * To fix this, we will need to emulate the PFEC checking (on the L1 | |
10035 | * page tables), using walk_addr(), when injecting PFs to L1. | |
10036 | */ | |
10037 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, | |
10038 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); | |
10039 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, | |
10040 | enable_ept ? vmcs12->page_fault_error_code_match : 0); | |
10041 | ||
10042 | if (cpu_has_secondary_exec_ctrls()) { | |
f4124500 | 10043 | exec_control = vmx_secondary_exec_control(vmx); |
e2821620 | 10044 | |
fe3ef05c | 10045 | /* Take the following fields only from vmcs12 */ |
696dfd95 | 10046 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
b3a2a907 | 10047 | SECONDARY_EXEC_RDTSCP | |
696dfd95 | 10048 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
dfa169bb | 10049 | SECONDARY_EXEC_APIC_REGISTER_VIRT); |
fe3ef05c | 10050 | if (nested_cpu_has(vmcs12, |
03efce6f BD |
10051 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
10052 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & | |
10053 | ~SECONDARY_EXEC_ENABLE_PML; | |
10054 | exec_control |= vmcs12_exec_ctrl; | |
10055 | } | |
fe3ef05c | 10056 | |
608406e2 WV |
10057 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { |
10058 | vmcs_write64(EOI_EXIT_BITMAP0, | |
10059 | vmcs12->eoi_exit_bitmap0); | |
10060 | vmcs_write64(EOI_EXIT_BITMAP1, | |
10061 | vmcs12->eoi_exit_bitmap1); | |
10062 | vmcs_write64(EOI_EXIT_BITMAP2, | |
10063 | vmcs12->eoi_exit_bitmap2); | |
10064 | vmcs_write64(EOI_EXIT_BITMAP3, | |
10065 | vmcs12->eoi_exit_bitmap3); | |
10066 | vmcs_write16(GUEST_INTR_STATUS, | |
10067 | vmcs12->guest_intr_status); | |
10068 | } | |
10069 | ||
6beb7bd5 JM |
10070 | /* |
10071 | * Write an illegal value to APIC_ACCESS_ADDR. Later, | |
10072 | * nested_get_vmcs12_pages will either fix it up or | |
10073 | * remove the VM execution control. | |
10074 | */ | |
10075 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) | |
10076 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); | |
10077 | ||
fe3ef05c NHE |
10078 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
10079 | } | |
10080 | ||
10081 | ||
10082 | /* | |
10083 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) | |
10084 | * Some constant fields are set here by vmx_set_constant_host_state(). | |
10085 | * Other fields are different per CPU, and will be set later when | |
10086 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. | |
10087 | */ | |
a547c6db | 10088 | vmx_set_constant_host_state(vmx); |
fe3ef05c | 10089 | |
83bafef1 JM |
10090 | /* |
10091 | * Set the MSR load/store lists to match L0's settings. | |
10092 | */ | |
10093 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); | |
10094 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
10095 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); | |
10096 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
10097 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); | |
10098 | ||
fe3ef05c NHE |
10099 | /* |
10100 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before | |
10101 | * entry, but only if the current (host) sp changed from the value | |
10102 | * we wrote last (vmx->host_rsp). This cache is no longer relevant | |
10103 | * if we switch vmcs, and rather than hold a separate cache per vmcs, | |
10104 | * here we just force the write to happen on entry. | |
10105 | */ | |
10106 | vmx->host_rsp = 0; | |
10107 | ||
10108 | exec_control = vmx_exec_control(vmx); /* L0's desires */ | |
10109 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; | |
10110 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; | |
10111 | exec_control &= ~CPU_BASED_TPR_SHADOW; | |
10112 | exec_control |= vmcs12->cpu_based_vm_exec_control; | |
a7c0b07d | 10113 | |
6beb7bd5 JM |
10114 | /* |
10115 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if | |
10116 | * nested_get_vmcs12_pages can't fix it up, the illegal value | |
10117 | * will result in a VM entry failure. | |
10118 | */ | |
a7c0b07d | 10119 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
6beb7bd5 | 10120 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
a7c0b07d WL |
10121 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
10122 | } | |
10123 | ||
fe3ef05c | 10124 | /* |
3af18d9c | 10125 | * Merging of IO bitmap not currently supported. |
fe3ef05c NHE |
10126 | * Rather, exit every time. |
10127 | */ | |
fe3ef05c NHE |
10128 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
10129 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; | |
10130 | ||
10131 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); | |
10132 | ||
10133 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the | |
10134 | * bitwise-or of what L1 wants to trap for L2, and what we want to | |
10135 | * trap. Note that CR0.TS also needs updating - we do this later. | |
10136 | */ | |
10137 | update_exception_bitmap(vcpu); | |
10138 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; | |
10139 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); | |
10140 | ||
8049d651 NHE |
10141 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
10142 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER | |
10143 | * bits are further modified by vmx_set_efer() below. | |
10144 | */ | |
f4124500 | 10145 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
8049d651 NHE |
10146 | |
10147 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are | |
10148 | * emulated by vmx_set_efer(), below. | |
10149 | */ | |
2961e876 | 10150 | vm_entry_controls_init(vmx, |
8049d651 NHE |
10151 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
10152 | ~VM_ENTRY_IA32E_MODE) | | |
fe3ef05c NHE |
10153 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
10154 | ||
cf8b84f4 JM |
10155 | if (from_vmentry && |
10156 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { | |
fe3ef05c | 10157 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
44811c02 | 10158 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
cf8b84f4 | 10159 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
fe3ef05c | 10160 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
cf8b84f4 | 10161 | } |
fe3ef05c NHE |
10162 | |
10163 | set_cr4_guest_host_mask(vmx); | |
10164 | ||
cf8b84f4 JM |
10165 | if (from_vmentry && |
10166 | vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) | |
36be0b9d PB |
10167 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
10168 | ||
27fc51b2 NHE |
10169 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
10170 | vmcs_write64(TSC_OFFSET, | |
ea26e4ec | 10171 | vcpu->arch.tsc_offset + vmcs12->tsc_offset); |
27fc51b2 | 10172 | else |
ea26e4ec | 10173 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
c95ba92a PF |
10174 | if (kvm_has_tsc_control) |
10175 | decache_tsc_multiplier(vmx); | |
fe3ef05c NHE |
10176 | |
10177 | if (enable_vpid) { | |
10178 | /* | |
5c614b35 WL |
10179 | * There is no direct mapping between vpid02 and vpid12, the |
10180 | * vpid02 is per-vCPU for L0 and reused while the value of | |
10181 | * vpid12 is changed w/ one invvpid during nested vmentry. | |
10182 | * The vpid12 is allocated by L1 for L2, so it will not | |
10183 | * influence global bitmap(for vpid01 and vpid02 allocation) | |
10184 | * even if spawn a lot of nested vCPUs. | |
fe3ef05c | 10185 | */ |
5c614b35 WL |
10186 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
10187 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); | |
10188 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { | |
10189 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; | |
10190 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); | |
10191 | } | |
10192 | } else { | |
10193 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
10194 | vmx_flush_tlb(vcpu); | |
10195 | } | |
10196 | ||
fe3ef05c NHE |
10197 | } |
10198 | ||
1fb883bb LP |
10199 | if (enable_pml) { |
10200 | /* | |
10201 | * Conceptually we want to copy the PML address and index from | |
10202 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, | |
10203 | * since we always flush the log on each vmexit, this happens | |
10204 | * to be equivalent to simply resetting the fields in vmcs02. | |
10205 | */ | |
10206 | ASSERT(vmx->pml_pg); | |
10207 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
10208 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
10209 | } | |
10210 | ||
155a97a3 | 10211 | if (nested_cpu_has_ept(vmcs12)) { |
ae1e2d10 PB |
10212 | if (nested_ept_init_mmu_context(vcpu)) { |
10213 | *entry_failure_code = ENTRY_FAIL_DEFAULT; | |
10214 | return 1; | |
10215 | } | |
fb6c8198 JM |
10216 | } else if (nested_cpu_has2(vmcs12, |
10217 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
10218 | vmx_flush_tlb_ept_only(vcpu); | |
155a97a3 NHE |
10219 | } |
10220 | ||
fe3ef05c | 10221 | /* |
bd7e5b08 PB |
10222 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
10223 | * bits which we consider mandatory enabled. | |
fe3ef05c NHE |
10224 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
10225 | * the specifications by L1; It's not enough to take | |
10226 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we | |
10227 | * have more bits than L1 expected. | |
10228 | */ | |
10229 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); | |
10230 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); | |
10231 | ||
10232 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); | |
10233 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); | |
10234 | ||
cf8b84f4 JM |
10235 | if (from_vmentry && |
10236 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) | |
5a6a9748 DM |
10237 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
10238 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) | |
10239 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); | |
10240 | else | |
10241 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
10242 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ | |
10243 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
10244 | ||
9ed38ffa | 10245 | /* Shadow page tables on either EPT or shadow page tables. */ |
7ad658b6 | 10246 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
9ed38ffa LP |
10247 | entry_failure_code)) |
10248 | return 1; | |
7ca29de2 | 10249 | |
feaf0c7d GN |
10250 | if (!enable_ept) |
10251 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; | |
10252 | ||
3633cfc3 NHE |
10253 | /* |
10254 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 | |
10255 | */ | |
10256 | if (enable_ept) { | |
10257 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); | |
10258 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); | |
10259 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); | |
10260 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); | |
10261 | } | |
10262 | ||
fe3ef05c NHE |
10263 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
10264 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); | |
ee146c1c | 10265 | return 0; |
fe3ef05c NHE |
10266 | } |
10267 | ||
ca0bde28 | 10268 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
cd232ad0 | 10269 | { |
cd232ad0 | 10270 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
7c177938 | 10271 | |
6dfacadd | 10272 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
ca0bde28 JM |
10273 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
10274 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
26539bd0 | 10275 | |
ca0bde28 JM |
10276 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
10277 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
7c177938 | 10278 | |
ca0bde28 JM |
10279 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
10280 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
f2b93280 | 10281 | |
ca0bde28 JM |
10282 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
10283 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
e9ac033e | 10284 | |
c5f983f6 BD |
10285 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
10286 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10287 | ||
7c177938 | 10288 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
0115f9cb | 10289 | vmx->nested.nested_vmx_procbased_ctls_low, |
b9c237bb | 10290 | vmx->nested.nested_vmx_procbased_ctls_high) || |
2e5b0bd9 JM |
10291 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
10292 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, | |
10293 | vmx->nested.nested_vmx_secondary_ctls_low, | |
10294 | vmx->nested.nested_vmx_secondary_ctls_high)) || | |
7c177938 | 10295 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
b9c237bb WV |
10296 | vmx->nested.nested_vmx_pinbased_ctls_low, |
10297 | vmx->nested.nested_vmx_pinbased_ctls_high) || | |
7c177938 | 10298 | !vmx_control_verify(vmcs12->vm_exit_controls, |
0115f9cb | 10299 | vmx->nested.nested_vmx_exit_ctls_low, |
b9c237bb | 10300 | vmx->nested.nested_vmx_exit_ctls_high) || |
7c177938 | 10301 | !vmx_control_verify(vmcs12->vm_entry_controls, |
0115f9cb | 10302 | vmx->nested.nested_vmx_entry_ctls_low, |
b9c237bb | 10303 | vmx->nested.nested_vmx_entry_ctls_high)) |
ca0bde28 | 10304 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
7c177938 | 10305 | |
c7c2c709 JM |
10306 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
10307 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
10308 | ||
3899152c | 10309 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
1dc35dac | 10310 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
ca0bde28 JM |
10311 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
10312 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; | |
10313 | ||
10314 | return 0; | |
10315 | } | |
10316 | ||
10317 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
10318 | u32 *exit_qual) | |
10319 | { | |
10320 | bool ia32e; | |
10321 | ||
10322 | *exit_qual = ENTRY_FAIL_DEFAULT; | |
7c177938 | 10323 | |
3899152c | 10324 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
ca0bde28 | 10325 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
b428018a | 10326 | return 1; |
ca0bde28 JM |
10327 | |
10328 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) && | |
10329 | vmcs12->vmcs_link_pointer != -1ull) { | |
10330 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; | |
b428018a | 10331 | return 1; |
7c177938 NHE |
10332 | } |
10333 | ||
384bb783 | 10334 | /* |
cb0c8cda | 10335 | * If the load IA32_EFER VM-entry control is 1, the following checks |
384bb783 JK |
10336 | * are performed on the field for the IA32_EFER MSR: |
10337 | * - Bits reserved in the IA32_EFER MSR must be 0. | |
10338 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of | |
10339 | * the IA-32e mode guest VM-exit control. It must also be identical | |
10340 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to | |
10341 | * CR0.PG) is 1. | |
10342 | */ | |
ca0bde28 JM |
10343 | if (to_vmx(vcpu)->nested.nested_run_pending && |
10344 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { | |
384bb783 JK |
10345 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
10346 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || | |
10347 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || | |
10348 | ((vmcs12->guest_cr0 & X86_CR0_PG) && | |
ca0bde28 | 10349 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
b428018a | 10350 | return 1; |
384bb783 JK |
10351 | } |
10352 | ||
10353 | /* | |
10354 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the | |
10355 | * IA32_EFER MSR must be 0 in the field for that register. In addition, | |
10356 | * the values of the LMA and LME bits in the field must each be that of | |
10357 | * the host address-space size VM-exit control. | |
10358 | */ | |
10359 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { | |
10360 | ia32e = (vmcs12->vm_exit_controls & | |
10361 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; | |
10362 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || | |
10363 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || | |
ca0bde28 | 10364 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
b428018a | 10365 | return 1; |
ca0bde28 JM |
10366 | } |
10367 | ||
10368 | return 0; | |
10369 | } | |
10370 | ||
858e25c0 JM |
10371 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry) |
10372 | { | |
10373 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10374 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
10375 | struct loaded_vmcs *vmcs02; | |
858e25c0 JM |
10376 | u32 msr_entry_idx; |
10377 | u32 exit_qual; | |
10378 | ||
10379 | vmcs02 = nested_get_current_vmcs02(vmx); | |
10380 | if (!vmcs02) | |
10381 | return -ENOMEM; | |
10382 | ||
10383 | enter_guest_mode(vcpu); | |
10384 | ||
10385 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) | |
10386 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
10387 | ||
1279a6b1 | 10388 | vmx_switch_vmcs(vcpu, vmcs02); |
858e25c0 JM |
10389 | vmx_segment_cache_clear(vmx); |
10390 | ||
10391 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) { | |
10392 | leave_guest_mode(vcpu); | |
1279a6b1 | 10393 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
858e25c0 JM |
10394 | nested_vmx_entry_failure(vcpu, vmcs12, |
10395 | EXIT_REASON_INVALID_STATE, exit_qual); | |
10396 | return 1; | |
10397 | } | |
10398 | ||
10399 | nested_get_vmcs12_pages(vcpu, vmcs12); | |
10400 | ||
10401 | msr_entry_idx = nested_vmx_load_msr(vcpu, | |
10402 | vmcs12->vm_entry_msr_load_addr, | |
10403 | vmcs12->vm_entry_msr_load_count); | |
10404 | if (msr_entry_idx) { | |
10405 | leave_guest_mode(vcpu); | |
1279a6b1 | 10406 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
858e25c0 JM |
10407 | nested_vmx_entry_failure(vcpu, vmcs12, |
10408 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); | |
10409 | return 1; | |
10410 | } | |
10411 | ||
10412 | vmcs12->launch_state = 1; | |
10413 | ||
10414 | /* | |
10415 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point | |
10416 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet | |
10417 | * returned as far as L1 is concerned. It will only return (and set | |
10418 | * the success flag) when L2 exits (see nested_vmx_vmexit()). | |
10419 | */ | |
10420 | return 0; | |
10421 | } | |
10422 | ||
ca0bde28 JM |
10423 | /* |
10424 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 | |
10425 | * for running an L2 nested guest. | |
10426 | */ | |
10427 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) | |
10428 | { | |
10429 | struct vmcs12 *vmcs12; | |
10430 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
ca0bde28 JM |
10431 | u32 exit_qual; |
10432 | int ret; | |
10433 | ||
10434 | if (!nested_vmx_check_permission(vcpu)) | |
10435 | return 1; | |
10436 | ||
10437 | if (!nested_vmx_check_vmcs12(vcpu)) | |
10438 | goto out; | |
10439 | ||
10440 | vmcs12 = get_vmcs12(vcpu); | |
10441 | ||
10442 | if (enable_shadow_vmcs) | |
10443 | copy_shadow_to_vmcs12(vmx); | |
10444 | ||
10445 | /* | |
10446 | * The nested entry process starts with enforcing various prerequisites | |
10447 | * on vmcs12 as required by the Intel SDM, and act appropriately when | |
10448 | * they fail: As the SDM explains, some conditions should cause the | |
10449 | * instruction to fail, while others will cause the instruction to seem | |
10450 | * to succeed, but return an EXIT_REASON_INVALID_STATE. | |
10451 | * To speed up the normal (success) code path, we should avoid checking | |
10452 | * for misconfigurations which will anyway be caught by the processor | |
10453 | * when using the merged vmcs02. | |
10454 | */ | |
10455 | if (vmcs12->launch_state == launch) { | |
10456 | nested_vmx_failValid(vcpu, | |
10457 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS | |
10458 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); | |
10459 | goto out; | |
10460 | } | |
10461 | ||
10462 | ret = check_vmentry_prereqs(vcpu, vmcs12); | |
10463 | if (ret) { | |
10464 | nested_vmx_failValid(vcpu, ret); | |
10465 | goto out; | |
10466 | } | |
10467 | ||
10468 | /* | |
10469 | * After this point, the trap flag no longer triggers a singlestep trap | |
10470 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. | |
10471 | * This is not 100% correct; for performance reasons, we delegate most | |
10472 | * of the checks on host state to the processor. If those fail, | |
10473 | * the singlestep trap is missed. | |
10474 | */ | |
10475 | skip_emulated_instruction(vcpu); | |
10476 | ||
10477 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); | |
10478 | if (ret) { | |
10479 | nested_vmx_entry_failure(vcpu, vmcs12, | |
10480 | EXIT_REASON_INVALID_STATE, exit_qual); | |
10481 | return 1; | |
384bb783 JK |
10482 | } |
10483 | ||
7c177938 NHE |
10484 | /* |
10485 | * We're finally done with prerequisite checking, and can start with | |
10486 | * the nested entry. | |
10487 | */ | |
10488 | ||
858e25c0 JM |
10489 | ret = enter_vmx_non_root_mode(vcpu, true); |
10490 | if (ret) | |
10491 | return ret; | |
ff651cb6 | 10492 | |
6dfacadd | 10493 | if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) |
5cb56059 | 10494 | return kvm_vcpu_halt(vcpu); |
6dfacadd | 10495 | |
7af40ad3 JK |
10496 | vmx->nested.nested_run_pending = 1; |
10497 | ||
cd232ad0 | 10498 | return 1; |
eb277562 KH |
10499 | |
10500 | out: | |
6affcbed | 10501 | return kvm_skip_emulated_instruction(vcpu); |
cd232ad0 NHE |
10502 | } |
10503 | ||
4704d0be NHE |
10504 | /* |
10505 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date | |
10506 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). | |
10507 | * This function returns the new value we should put in vmcs12.guest_cr0. | |
10508 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, | |
10509 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now | |
10510 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 | |
10511 | * didn't trap the bit, because if L1 did, so would L0). | |
10512 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have | |
10513 | * been modified by L2, and L1 knows it. So just leave the old value of | |
10514 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 | |
10515 | * isn't relevant, because if L0 traps this bit it can set it to anything. | |
10516 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have | |
10517 | * changed these bits, and therefore they need to be updated, but L0 | |
10518 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather | |
10519 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. | |
10520 | */ | |
10521 | static inline unsigned long | |
10522 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
10523 | { | |
10524 | return | |
10525 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | | |
10526 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | | |
10527 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | | |
10528 | vcpu->arch.cr0_guest_owned_bits)); | |
10529 | } | |
10530 | ||
10531 | static inline unsigned long | |
10532 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
10533 | { | |
10534 | return | |
10535 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | | |
10536 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | | |
10537 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | | |
10538 | vcpu->arch.cr4_guest_owned_bits)); | |
10539 | } | |
10540 | ||
5f3d5799 JK |
10541 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
10542 | struct vmcs12 *vmcs12) | |
10543 | { | |
10544 | u32 idt_vectoring; | |
10545 | unsigned int nr; | |
10546 | ||
851eb667 | 10547 | if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) { |
5f3d5799 JK |
10548 | nr = vcpu->arch.exception.nr; |
10549 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
10550 | ||
10551 | if (kvm_exception_is_soft(nr)) { | |
10552 | vmcs12->vm_exit_instruction_len = | |
10553 | vcpu->arch.event_exit_inst_len; | |
10554 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; | |
10555 | } else | |
10556 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; | |
10557 | ||
10558 | if (vcpu->arch.exception.has_error_code) { | |
10559 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; | |
10560 | vmcs12->idt_vectoring_error_code = | |
10561 | vcpu->arch.exception.error_code; | |
10562 | } | |
10563 | ||
10564 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
cd2633c5 | 10565 | } else if (vcpu->arch.nmi_injected) { |
5f3d5799 JK |
10566 | vmcs12->idt_vectoring_info_field = |
10567 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; | |
10568 | } else if (vcpu->arch.interrupt.pending) { | |
10569 | nr = vcpu->arch.interrupt.nr; | |
10570 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
10571 | ||
10572 | if (vcpu->arch.interrupt.soft) { | |
10573 | idt_vectoring |= INTR_TYPE_SOFT_INTR; | |
10574 | vmcs12->vm_entry_instruction_len = | |
10575 | vcpu->arch.event_exit_inst_len; | |
10576 | } else | |
10577 | idt_vectoring |= INTR_TYPE_EXT_INTR; | |
10578 | ||
10579 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
10580 | } | |
10581 | } | |
10582 | ||
b6b8a145 JK |
10583 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
10584 | { | |
10585 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10586 | ||
acc9ab60 WL |
10587 | if (vcpu->arch.exception.pending || |
10588 | vcpu->arch.nmi_injected || | |
10589 | vcpu->arch.interrupt.pending) | |
10590 | return -EBUSY; | |
10591 | ||
f4124500 JK |
10592 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
10593 | vmx->nested.preemption_timer_expired) { | |
10594 | if (vmx->nested.nested_run_pending) | |
10595 | return -EBUSY; | |
10596 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); | |
10597 | return 0; | |
10598 | } | |
10599 | ||
b6b8a145 | 10600 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
acc9ab60 | 10601 | if (vmx->nested.nested_run_pending) |
b6b8a145 JK |
10602 | return -EBUSY; |
10603 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, | |
10604 | NMI_VECTOR | INTR_TYPE_NMI_INTR | | |
10605 | INTR_INFO_VALID_MASK, 0); | |
10606 | /* | |
10607 | * The NMI-triggered VM exit counts as injection: | |
10608 | * clear this one and block further NMIs. | |
10609 | */ | |
10610 | vcpu->arch.nmi_pending = 0; | |
10611 | vmx_set_nmi_mask(vcpu, true); | |
10612 | return 0; | |
10613 | } | |
10614 | ||
10615 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && | |
10616 | nested_exit_on_intr(vcpu)) { | |
10617 | if (vmx->nested.nested_run_pending) | |
10618 | return -EBUSY; | |
10619 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); | |
705699a1 | 10620 | return 0; |
b6b8a145 JK |
10621 | } |
10622 | ||
6342c50a DH |
10623 | vmx_complete_nested_posted_interrupt(vcpu); |
10624 | return 0; | |
b6b8a145 JK |
10625 | } |
10626 | ||
f4124500 JK |
10627 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
10628 | { | |
10629 | ktime_t remaining = | |
10630 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); | |
10631 | u64 value; | |
10632 | ||
10633 | if (ktime_to_ns(remaining) <= 0) | |
10634 | return 0; | |
10635 | ||
10636 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; | |
10637 | do_div(value, 1000000); | |
10638 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
10639 | } | |
10640 | ||
4704d0be | 10641 | /* |
cf8b84f4 JM |
10642 | * Update the guest state fields of vmcs12 to reflect changes that |
10643 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the | |
10644 | * VM-entry controls is also updated, since this is really a guest | |
10645 | * state bit.) | |
4704d0be | 10646 | */ |
cf8b84f4 | 10647 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
4704d0be | 10648 | { |
4704d0be NHE |
10649 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
10650 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); | |
10651 | ||
4704d0be NHE |
10652 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
10653 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); | |
10654 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); | |
10655 | ||
10656 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); | |
10657 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); | |
10658 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); | |
10659 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); | |
10660 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); | |
10661 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); | |
10662 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); | |
10663 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); | |
10664 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); | |
10665 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); | |
10666 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); | |
10667 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); | |
10668 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); | |
10669 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); | |
10670 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); | |
10671 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); | |
10672 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); | |
10673 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); | |
10674 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); | |
10675 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); | |
10676 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); | |
10677 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); | |
10678 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); | |
10679 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); | |
10680 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); | |
10681 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); | |
10682 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); | |
10683 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); | |
10684 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); | |
10685 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); | |
10686 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); | |
10687 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); | |
10688 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); | |
10689 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); | |
10690 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); | |
10691 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); | |
10692 | ||
4704d0be NHE |
10693 | vmcs12->guest_interruptibility_info = |
10694 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
10695 | vmcs12->guest_pending_dbg_exceptions = | |
10696 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); | |
3edf1e69 JK |
10697 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
10698 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; | |
10699 | else | |
10700 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; | |
4704d0be | 10701 | |
f4124500 JK |
10702 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
10703 | if (vmcs12->vm_exit_controls & | |
10704 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) | |
10705 | vmcs12->vmx_preemption_timer_value = | |
10706 | vmx_get_preemption_timer_value(vcpu); | |
10707 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); | |
10708 | } | |
7854cbca | 10709 | |
3633cfc3 NHE |
10710 | /* |
10711 | * In some cases (usually, nested EPT), L2 is allowed to change its | |
10712 | * own CR3 without exiting. If it has changed it, we must keep it. | |
10713 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined | |
10714 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. | |
10715 | * | |
10716 | * Additionally, restore L2's PDPTR to vmcs12. | |
10717 | */ | |
10718 | if (enable_ept) { | |
f3531054 | 10719 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
3633cfc3 NHE |
10720 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
10721 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); | |
10722 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); | |
10723 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); | |
10724 | } | |
10725 | ||
d281e13b | 10726 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
119a9c01 | 10727 | |
608406e2 WV |
10728 | if (nested_cpu_has_vid(vmcs12)) |
10729 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); | |
10730 | ||
c18911a2 JK |
10731 | vmcs12->vm_entry_controls = |
10732 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | | |
2961e876 | 10733 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
c18911a2 | 10734 | |
2996fca0 JK |
10735 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
10736 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); | |
10737 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
10738 | } | |
10739 | ||
4704d0be NHE |
10740 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
10741 | * the relevant bit asks not to trap the change */ | |
b8c07d55 | 10742 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
4704d0be | 10743 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
10ba54a5 JK |
10744 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
10745 | vmcs12->guest_ia32_efer = vcpu->arch.efer; | |
4704d0be NHE |
10746 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
10747 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); | |
10748 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); | |
a87036ad | 10749 | if (kvm_mpx_supported()) |
36be0b9d | 10750 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
cf8b84f4 JM |
10751 | } |
10752 | ||
10753 | /* | |
10754 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits | |
10755 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), | |
10756 | * and this function updates it to reflect the changes to the guest state while | |
10757 | * L2 was running (and perhaps made some exits which were handled directly by L0 | |
10758 | * without going back to L1), and to reflect the exit reason. | |
10759 | * Note that we do not have to copy here all VMCS fields, just those that | |
10760 | * could have changed by the L2 guest or the exit - i.e., the guest-state and | |
10761 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, | |
10762 | * which already writes to vmcs12 directly. | |
10763 | */ | |
10764 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
10765 | u32 exit_reason, u32 exit_intr_info, | |
10766 | unsigned long exit_qualification) | |
10767 | { | |
10768 | /* update guest state fields: */ | |
10769 | sync_vmcs12(vcpu, vmcs12); | |
4704d0be NHE |
10770 | |
10771 | /* update exit information fields: */ | |
10772 | ||
533558bc JK |
10773 | vmcs12->vm_exit_reason = exit_reason; |
10774 | vmcs12->exit_qualification = exit_qualification; | |
4704d0be | 10775 | |
533558bc | 10776 | vmcs12->vm_exit_intr_info = exit_intr_info; |
c0d1c770 JK |
10777 | if ((vmcs12->vm_exit_intr_info & |
10778 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == | |
10779 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) | |
10780 | vmcs12->vm_exit_intr_error_code = | |
10781 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); | |
5f3d5799 | 10782 | vmcs12->idt_vectoring_info_field = 0; |
4704d0be NHE |
10783 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
10784 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
10785 | ||
5f3d5799 JK |
10786 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
10787 | /* vm_entry_intr_info_field is cleared on exit. Emulate this | |
10788 | * instead of reading the real value. */ | |
4704d0be | 10789 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
5f3d5799 JK |
10790 | |
10791 | /* | |
10792 | * Transfer the event that L0 or L1 may wanted to inject into | |
10793 | * L2 to IDT_VECTORING_INFO_FIELD. | |
10794 | */ | |
10795 | vmcs12_save_pending_event(vcpu, vmcs12); | |
10796 | } | |
10797 | ||
10798 | /* | |
10799 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is | |
10800 | * preserved above and would only end up incorrectly in L1. | |
10801 | */ | |
10802 | vcpu->arch.nmi_injected = false; | |
10803 | kvm_clear_exception_queue(vcpu); | |
10804 | kvm_clear_interrupt_queue(vcpu); | |
4704d0be NHE |
10805 | } |
10806 | ||
10807 | /* | |
10808 | * A part of what we need to when the nested L2 guest exits and we want to | |
10809 | * run its L1 parent, is to reset L1's guest state to the host state specified | |
10810 | * in vmcs12. | |
10811 | * This function is to be called not only on normal nested exit, but also on | |
10812 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry | |
10813 | * Failures During or After Loading Guest State"). | |
10814 | * This function should be called when the active VMCS is L1's (vmcs01). | |
10815 | */ | |
733568f9 JK |
10816 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
10817 | struct vmcs12 *vmcs12) | |
4704d0be | 10818 | { |
21feb4eb | 10819 | struct kvm_segment seg; |
ca0bde28 | 10820 | u32 entry_failure_code; |
21feb4eb | 10821 | |
4704d0be NHE |
10822 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
10823 | vcpu->arch.efer = vmcs12->host_ia32_efer; | |
d1fa0352 | 10824 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
4704d0be NHE |
10825 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
10826 | else | |
10827 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
10828 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
10829 | ||
10830 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); | |
10831 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); | |
1adfa76a | 10832 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
4704d0be NHE |
10833 | /* |
10834 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't | |
bd7e5b08 PB |
10835 | * actually changed, because vmx_set_cr0 refers to efer set above. |
10836 | * | |
10837 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 | |
10838 | * (KVM doesn't change it); | |
4704d0be | 10839 | */ |
bd7e5b08 | 10840 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
9e3e4dbf | 10841 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
4704d0be | 10842 | |
bd7e5b08 | 10843 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
4704d0be NHE |
10844 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
10845 | kvm_set_cr4(vcpu, vmcs12->host_cr4); | |
10846 | ||
29bf08f1 | 10847 | nested_ept_uninit_mmu_context(vcpu); |
155a97a3 | 10848 | |
1dc35dac LP |
10849 | /* |
10850 | * Only PDPTE load can fail as the value of cr3 was checked on entry and | |
10851 | * couldn't have changed. | |
10852 | */ | |
10853 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) | |
10854 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); | |
4704d0be | 10855 | |
feaf0c7d GN |
10856 | if (!enable_ept) |
10857 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; | |
10858 | ||
4704d0be NHE |
10859 | if (enable_vpid) { |
10860 | /* | |
10861 | * Trivially support vpid by letting L2s share their parent | |
10862 | * L1's vpid. TODO: move to a more elaborate solution, giving | |
10863 | * each L2 its own vpid and exposing the vpid feature to L1. | |
10864 | */ | |
10865 | vmx_flush_tlb(vcpu); | |
10866 | } | |
10867 | ||
10868 | ||
10869 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); | |
10870 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); | |
10871 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); | |
10872 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); | |
10873 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); | |
4704d0be | 10874 | |
36be0b9d PB |
10875 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
10876 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) | |
10877 | vmcs_write64(GUEST_BNDCFGS, 0); | |
10878 | ||
44811c02 | 10879 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
4704d0be | 10880 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
44811c02 JK |
10881 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
10882 | } | |
4704d0be NHE |
10883 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
10884 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, | |
10885 | vmcs12->host_ia32_perf_global_ctrl); | |
503cd0c5 | 10886 | |
21feb4eb ACL |
10887 | /* Set L1 segment info according to Intel SDM |
10888 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ | |
10889 | seg = (struct kvm_segment) { | |
10890 | .base = 0, | |
10891 | .limit = 0xFFFFFFFF, | |
10892 | .selector = vmcs12->host_cs_selector, | |
10893 | .type = 11, | |
10894 | .present = 1, | |
10895 | .s = 1, | |
10896 | .g = 1 | |
10897 | }; | |
10898 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) | |
10899 | seg.l = 1; | |
10900 | else | |
10901 | seg.db = 1; | |
10902 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); | |
10903 | seg = (struct kvm_segment) { | |
10904 | .base = 0, | |
10905 | .limit = 0xFFFFFFFF, | |
10906 | .type = 3, | |
10907 | .present = 1, | |
10908 | .s = 1, | |
10909 | .db = 1, | |
10910 | .g = 1 | |
10911 | }; | |
10912 | seg.selector = vmcs12->host_ds_selector; | |
10913 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); | |
10914 | seg.selector = vmcs12->host_es_selector; | |
10915 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); | |
10916 | seg.selector = vmcs12->host_ss_selector; | |
10917 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); | |
10918 | seg.selector = vmcs12->host_fs_selector; | |
10919 | seg.base = vmcs12->host_fs_base; | |
10920 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); | |
10921 | seg.selector = vmcs12->host_gs_selector; | |
10922 | seg.base = vmcs12->host_gs_base; | |
10923 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); | |
10924 | seg = (struct kvm_segment) { | |
205befd9 | 10925 | .base = vmcs12->host_tr_base, |
21feb4eb ACL |
10926 | .limit = 0x67, |
10927 | .selector = vmcs12->host_tr_selector, | |
10928 | .type = 11, | |
10929 | .present = 1 | |
10930 | }; | |
10931 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); | |
10932 | ||
503cd0c5 JK |
10933 | kvm_set_dr(vcpu, 7, 0x400); |
10934 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
ff651cb6 | 10935 | |
3af18d9c WV |
10936 | if (cpu_has_vmx_msr_bitmap()) |
10937 | vmx_set_msr_bitmap(vcpu); | |
10938 | ||
ff651cb6 WV |
10939 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
10940 | vmcs12->vm_exit_msr_load_count)) | |
10941 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); | |
4704d0be NHE |
10942 | } |
10943 | ||
10944 | /* | |
10945 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 | |
10946 | * and modify vmcs12 to make it see what it would expect to see there if | |
10947 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) | |
10948 | */ | |
533558bc JK |
10949 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
10950 | u32 exit_intr_info, | |
10951 | unsigned long exit_qualification) | |
4704d0be NHE |
10952 | { |
10953 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
4704d0be | 10954 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
cf3215d9 | 10955 | u32 vm_inst_error = 0; |
4704d0be | 10956 | |
5f3d5799 JK |
10957 | /* trying to cancel vmlaunch/vmresume is a bug */ |
10958 | WARN_ON_ONCE(vmx->nested.nested_run_pending); | |
10959 | ||
4704d0be | 10960 | leave_guest_mode(vcpu); |
533558bc JK |
10961 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
10962 | exit_qualification); | |
4704d0be | 10963 | |
ff651cb6 WV |
10964 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
10965 | vmcs12->vm_exit_msr_store_count)) | |
10966 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); | |
10967 | ||
cf3215d9 JM |
10968 | if (unlikely(vmx->fail)) |
10969 | vm_inst_error = vmcs_read32(VM_INSTRUCTION_ERROR); | |
10970 | ||
1279a6b1 | 10971 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
f3380ca5 | 10972 | |
77b0f5d6 BD |
10973 | if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) |
10974 | && nested_exit_intr_ack_set(vcpu)) { | |
10975 | int irq = kvm_cpu_get_interrupt(vcpu); | |
10976 | WARN_ON(irq < 0); | |
10977 | vmcs12->vm_exit_intr_info = irq | | |
10978 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; | |
10979 | } | |
10980 | ||
542060ea JK |
10981 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
10982 | vmcs12->exit_qualification, | |
10983 | vmcs12->idt_vectoring_info_field, | |
10984 | vmcs12->vm_exit_intr_info, | |
10985 | vmcs12->vm_exit_intr_error_code, | |
10986 | KVM_ISA_VMX); | |
4704d0be | 10987 | |
8391ce44 PB |
10988 | vm_entry_controls_reset_shadow(vmx); |
10989 | vm_exit_controls_reset_shadow(vmx); | |
36c3cc42 JK |
10990 | vmx_segment_cache_clear(vmx); |
10991 | ||
4704d0be NHE |
10992 | /* if no vmcs02 cache requested, remove the one we used */ |
10993 | if (VMCS02_POOL_SIZE == 0) | |
10994 | nested_free_vmcs02(vmx, vmx->nested.current_vmptr); | |
10995 | ||
10996 | load_vmcs12_host_state(vcpu, vmcs12); | |
10997 | ||
9314006d | 10998 | /* Update any VMCS fields that might have changed while L2 ran */ |
83bafef1 JM |
10999 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
11000 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); | |
ea26e4ec | 11001 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
9314006d PB |
11002 | if (vmx->hv_deadline_tsc == -1) |
11003 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11004 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
11005 | else | |
11006 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11007 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c95ba92a PF |
11008 | if (kvm_has_tsc_control) |
11009 | decache_tsc_multiplier(vmx); | |
4704d0be | 11010 | |
dccbfcf5 RK |
11011 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
11012 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; | |
11013 | vmx_set_virtual_x2apic_mode(vcpu, | |
11014 | vcpu->arch.apic_base & X2APIC_ENABLE); | |
fb6c8198 JM |
11015 | } else if (!nested_cpu_has_ept(vmcs12) && |
11016 | nested_cpu_has2(vmcs12, | |
11017 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
11018 | vmx_flush_tlb_ept_only(vcpu); | |
dccbfcf5 | 11019 | } |
4704d0be NHE |
11020 | |
11021 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ | |
11022 | vmx->host_rsp = 0; | |
11023 | ||
11024 | /* Unpin physical memory we referred to in vmcs02 */ | |
11025 | if (vmx->nested.apic_access_page) { | |
11026 | nested_release_page(vmx->nested.apic_access_page); | |
48d89b92 | 11027 | vmx->nested.apic_access_page = NULL; |
4704d0be | 11028 | } |
a7c0b07d WL |
11029 | if (vmx->nested.virtual_apic_page) { |
11030 | nested_release_page(vmx->nested.virtual_apic_page); | |
48d89b92 | 11031 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 11032 | } |
705699a1 WV |
11033 | if (vmx->nested.pi_desc_page) { |
11034 | kunmap(vmx->nested.pi_desc_page); | |
11035 | nested_release_page(vmx->nested.pi_desc_page); | |
11036 | vmx->nested.pi_desc_page = NULL; | |
11037 | vmx->nested.pi_desc = NULL; | |
11038 | } | |
4704d0be | 11039 | |
38b99173 TC |
11040 | /* |
11041 | * We are now running in L2, mmu_notifier will force to reload the | |
11042 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. | |
11043 | */ | |
c83b6d15 | 11044 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
38b99173 | 11045 | |
4704d0be NHE |
11046 | /* |
11047 | * Exiting from L2 to L1, we're now back to L1 which thinks it just | |
11048 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the | |
11049 | * success or failure flag accordingly. | |
11050 | */ | |
11051 | if (unlikely(vmx->fail)) { | |
11052 | vmx->fail = 0; | |
cf3215d9 | 11053 | nested_vmx_failValid(vcpu, vm_inst_error); |
4704d0be NHE |
11054 | } else |
11055 | nested_vmx_succeed(vcpu); | |
012f83cb AG |
11056 | if (enable_shadow_vmcs) |
11057 | vmx->nested.sync_shadow_vmcs = true; | |
b6b8a145 JK |
11058 | |
11059 | /* in case we halted in L2 */ | |
11060 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
4704d0be NHE |
11061 | } |
11062 | ||
42124925 JK |
11063 | /* |
11064 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. | |
11065 | */ | |
11066 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) | |
11067 | { | |
2f707d97 WL |
11068 | if (is_guest_mode(vcpu)) { |
11069 | to_vmx(vcpu)->nested.nested_run_pending = 0; | |
533558bc | 11070 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
2f707d97 | 11071 | } |
42124925 JK |
11072 | free_nested(to_vmx(vcpu)); |
11073 | } | |
11074 | ||
7c177938 NHE |
11075 | /* |
11076 | * L1's failure to enter L2 is a subset of a normal exit, as explained in | |
11077 | * 23.7 "VM-entry failures during or after loading guest state" (this also | |
11078 | * lists the acceptable exit-reason and exit-qualification parameters). | |
11079 | * It should only be called before L2 actually succeeded to run, and when | |
11080 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). | |
11081 | */ | |
11082 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, | |
11083 | struct vmcs12 *vmcs12, | |
11084 | u32 reason, unsigned long qualification) | |
11085 | { | |
11086 | load_vmcs12_host_state(vcpu, vmcs12); | |
11087 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; | |
11088 | vmcs12->exit_qualification = qualification; | |
11089 | nested_vmx_succeed(vcpu); | |
012f83cb AG |
11090 | if (enable_shadow_vmcs) |
11091 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; | |
7c177938 NHE |
11092 | } |
11093 | ||
8a76d7f2 JR |
11094 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
11095 | struct x86_instruction_info *info, | |
11096 | enum x86_intercept_stage stage) | |
11097 | { | |
11098 | return X86EMUL_CONTINUE; | |
11099 | } | |
11100 | ||
64672c95 YJ |
11101 | #ifdef CONFIG_X86_64 |
11102 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ | |
11103 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, | |
11104 | u64 divisor, u64 *result) | |
11105 | { | |
11106 | u64 low = a << shift, high = a >> (64 - shift); | |
11107 | ||
11108 | /* To avoid the overflow on divq */ | |
11109 | if (high >= divisor) | |
11110 | return 1; | |
11111 | ||
11112 | /* Low hold the result, high hold rem which is discarded */ | |
11113 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : | |
11114 | "rm" (divisor), "0" (low), "1" (high)); | |
11115 | *result = low; | |
11116 | ||
11117 | return 0; | |
11118 | } | |
11119 | ||
11120 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) | |
11121 | { | |
11122 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9175d2e9 PB |
11123 | u64 tscl = rdtsc(); |
11124 | u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); | |
11125 | u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; | |
64672c95 YJ |
11126 | |
11127 | /* Convert to host delta tsc if tsc scaling is enabled */ | |
11128 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && | |
11129 | u64_shl_div_u64(delta_tsc, | |
11130 | kvm_tsc_scaling_ratio_frac_bits, | |
11131 | vcpu->arch.tsc_scaling_ratio, | |
11132 | &delta_tsc)) | |
11133 | return -ERANGE; | |
11134 | ||
11135 | /* | |
11136 | * If the delta tsc can't fit in the 32 bit after the multi shift, | |
11137 | * we can't use the preemption timer. | |
11138 | * It's possible that it fits on later vmentries, but checking | |
11139 | * on every vmentry is costly so we just use an hrtimer. | |
11140 | */ | |
11141 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) | |
11142 | return -ERANGE; | |
11143 | ||
11144 | vmx->hv_deadline_tsc = tscl + delta_tsc; | |
11145 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11146 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c8533544 WL |
11147 | |
11148 | return delta_tsc == 0; | |
64672c95 YJ |
11149 | } |
11150 | ||
11151 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) | |
11152 | { | |
11153 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
11154 | vmx->hv_deadline_tsc = -1; | |
11155 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
11156 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
11157 | } | |
11158 | #endif | |
11159 | ||
48d89b92 | 11160 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
ae97a3b8 | 11161 | { |
b4a2d31d RK |
11162 | if (ple_gap) |
11163 | shrink_ple_window(vcpu); | |
ae97a3b8 RK |
11164 | } |
11165 | ||
843e4330 KH |
11166 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
11167 | struct kvm_memory_slot *slot) | |
11168 | { | |
11169 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); | |
11170 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); | |
11171 | } | |
11172 | ||
11173 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, | |
11174 | struct kvm_memory_slot *slot) | |
11175 | { | |
11176 | kvm_mmu_slot_set_dirty(kvm, slot); | |
11177 | } | |
11178 | ||
11179 | static void vmx_flush_log_dirty(struct kvm *kvm) | |
11180 | { | |
11181 | kvm_flush_pml_buffers(kvm); | |
11182 | } | |
11183 | ||
c5f983f6 BD |
11184 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
11185 | { | |
11186 | struct vmcs12 *vmcs12; | |
11187 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
11188 | gpa_t gpa; | |
11189 | struct page *page = NULL; | |
11190 | u64 *pml_address; | |
11191 | ||
11192 | if (is_guest_mode(vcpu)) { | |
11193 | WARN_ON_ONCE(vmx->nested.pml_full); | |
11194 | ||
11195 | /* | |
11196 | * Check if PML is enabled for the nested guest. | |
11197 | * Whether eptp bit 6 is set is already checked | |
11198 | * as part of A/D emulation. | |
11199 | */ | |
11200 | vmcs12 = get_vmcs12(vcpu); | |
11201 | if (!nested_cpu_has_pml(vmcs12)) | |
11202 | return 0; | |
11203 | ||
4769886b | 11204 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
c5f983f6 BD |
11205 | vmx->nested.pml_full = true; |
11206 | return 1; | |
11207 | } | |
11208 | ||
11209 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; | |
11210 | ||
11211 | page = nested_get_page(vcpu, vmcs12->pml_address); | |
11212 | if (!page) | |
11213 | return 0; | |
11214 | ||
11215 | pml_address = kmap(page); | |
11216 | pml_address[vmcs12->guest_pml_index--] = gpa; | |
11217 | kunmap(page); | |
11218 | nested_release_page_clean(page); | |
11219 | } | |
11220 | ||
11221 | return 0; | |
11222 | } | |
11223 | ||
843e4330 KH |
11224 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
11225 | struct kvm_memory_slot *memslot, | |
11226 | gfn_t offset, unsigned long mask) | |
11227 | { | |
11228 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); | |
11229 | } | |
11230 | ||
bf9f6ac8 FW |
11231 | /* |
11232 | * This routine does the following things for vCPU which is going | |
11233 | * to be blocked if VT-d PI is enabled. | |
11234 | * - Store the vCPU to the wakeup list, so when interrupts happen | |
11235 | * we can find the right vCPU to wake up. | |
11236 | * - Change the Posted-interrupt descriptor as below: | |
11237 | * 'NDST' <-- vcpu->pre_pcpu | |
11238 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR | |
11239 | * - If 'ON' is set during this process, which means at least one | |
11240 | * interrupt is posted for this vCPU, we cannot block it, in | |
11241 | * this case, return 1, otherwise, return 0. | |
11242 | * | |
11243 | */ | |
bc22512b | 11244 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
bf9f6ac8 FW |
11245 | { |
11246 | unsigned long flags; | |
11247 | unsigned int dest; | |
11248 | struct pi_desc old, new; | |
11249 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
11250 | ||
11251 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
11252 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
11253 | !kvm_vcpu_apicv_active(vcpu)) | |
bf9f6ac8 FW |
11254 | return 0; |
11255 | ||
11256 | vcpu->pre_pcpu = vcpu->cpu; | |
11257 | spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock, | |
11258 | vcpu->pre_pcpu), flags); | |
11259 | list_add_tail(&vcpu->blocked_vcpu_list, | |
11260 | &per_cpu(blocked_vcpu_on_cpu, | |
11261 | vcpu->pre_pcpu)); | |
11262 | spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock, | |
11263 | vcpu->pre_pcpu), flags); | |
11264 | ||
11265 | do { | |
11266 | old.control = new.control = pi_desc->control; | |
11267 | ||
11268 | /* | |
11269 | * We should not block the vCPU if | |
11270 | * an interrupt is posted for it. | |
11271 | */ | |
11272 | if (pi_test_on(pi_desc) == 1) { | |
11273 | spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock, | |
11274 | vcpu->pre_pcpu), flags); | |
11275 | list_del(&vcpu->blocked_vcpu_list); | |
11276 | spin_unlock_irqrestore( | |
11277 | &per_cpu(blocked_vcpu_on_cpu_lock, | |
11278 | vcpu->pre_pcpu), flags); | |
11279 | vcpu->pre_pcpu = -1; | |
11280 | ||
11281 | return 1; | |
11282 | } | |
11283 | ||
11284 | WARN((pi_desc->sn == 1), | |
11285 | "Warning: SN field of posted-interrupts " | |
11286 | "is set before blocking\n"); | |
11287 | ||
11288 | /* | |
11289 | * Since vCPU can be preempted during this process, | |
11290 | * vcpu->cpu could be different with pre_pcpu, we | |
11291 | * need to set pre_pcpu as the destination of wakeup | |
11292 | * notification event, then we can find the right vCPU | |
11293 | * to wakeup in wakeup handler if interrupts happen | |
11294 | * when the vCPU is in blocked state. | |
11295 | */ | |
11296 | dest = cpu_physical_id(vcpu->pre_pcpu); | |
11297 | ||
11298 | if (x2apic_enabled()) | |
11299 | new.ndst = dest; | |
11300 | else | |
11301 | new.ndst = (dest << 8) & 0xFF00; | |
11302 | ||
11303 | /* set 'NV' to 'wakeup vector' */ | |
11304 | new.nv = POSTED_INTR_WAKEUP_VECTOR; | |
11305 | } while (cmpxchg(&pi_desc->control, old.control, | |
11306 | new.control) != old.control); | |
11307 | ||
11308 | return 0; | |
11309 | } | |
11310 | ||
bc22512b YJ |
11311 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
11312 | { | |
11313 | if (pi_pre_block(vcpu)) | |
11314 | return 1; | |
11315 | ||
64672c95 YJ |
11316 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
11317 | kvm_lapic_switch_to_sw_timer(vcpu); | |
11318 | ||
bc22512b YJ |
11319 | return 0; |
11320 | } | |
11321 | ||
11322 | static void pi_post_block(struct kvm_vcpu *vcpu) | |
bf9f6ac8 FW |
11323 | { |
11324 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
11325 | struct pi_desc old, new; | |
11326 | unsigned int dest; | |
11327 | unsigned long flags; | |
11328 | ||
11329 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
11330 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
11331 | !kvm_vcpu_apicv_active(vcpu)) | |
bf9f6ac8 FW |
11332 | return; |
11333 | ||
11334 | do { | |
11335 | old.control = new.control = pi_desc->control; | |
11336 | ||
11337 | dest = cpu_physical_id(vcpu->cpu); | |
11338 | ||
11339 | if (x2apic_enabled()) | |
11340 | new.ndst = dest; | |
11341 | else | |
11342 | new.ndst = (dest << 8) & 0xFF00; | |
11343 | ||
11344 | /* Allow posting non-urgent interrupts */ | |
11345 | new.sn = 0; | |
11346 | ||
11347 | /* set 'NV' to 'notification vector' */ | |
11348 | new.nv = POSTED_INTR_VECTOR; | |
11349 | } while (cmpxchg(&pi_desc->control, old.control, | |
11350 | new.control) != old.control); | |
11351 | ||
11352 | if(vcpu->pre_pcpu != -1) { | |
11353 | spin_lock_irqsave( | |
11354 | &per_cpu(blocked_vcpu_on_cpu_lock, | |
11355 | vcpu->pre_pcpu), flags); | |
11356 | list_del(&vcpu->blocked_vcpu_list); | |
11357 | spin_unlock_irqrestore( | |
11358 | &per_cpu(blocked_vcpu_on_cpu_lock, | |
11359 | vcpu->pre_pcpu), flags); | |
11360 | vcpu->pre_pcpu = -1; | |
11361 | } | |
11362 | } | |
11363 | ||
bc22512b YJ |
11364 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
11365 | { | |
64672c95 YJ |
11366 | if (kvm_x86_ops->set_hv_timer) |
11367 | kvm_lapic_switch_to_hv_timer(vcpu); | |
11368 | ||
bc22512b YJ |
11369 | pi_post_block(vcpu); |
11370 | } | |
11371 | ||
efc64404 FW |
11372 | /* |
11373 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts | |
11374 | * | |
11375 | * @kvm: kvm | |
11376 | * @host_irq: host irq of the interrupt | |
11377 | * @guest_irq: gsi of the interrupt | |
11378 | * @set: set or unset PI | |
11379 | * returns 0 on success, < 0 on failure | |
11380 | */ | |
11381 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, | |
11382 | uint32_t guest_irq, bool set) | |
11383 | { | |
11384 | struct kvm_kernel_irq_routing_entry *e; | |
11385 | struct kvm_irq_routing_table *irq_rt; | |
11386 | struct kvm_lapic_irq irq; | |
11387 | struct kvm_vcpu *vcpu; | |
11388 | struct vcpu_data vcpu_info; | |
11389 | int idx, ret = -EINVAL; | |
11390 | ||
11391 | if (!kvm_arch_has_assigned_device(kvm) || | |
a0052191 YZ |
11392 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
11393 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) | |
efc64404 FW |
11394 | return 0; |
11395 | ||
11396 | idx = srcu_read_lock(&kvm->irq_srcu); | |
11397 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); | |
11398 | BUG_ON(guest_irq >= irq_rt->nr_rt_entries); | |
11399 | ||
11400 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { | |
11401 | if (e->type != KVM_IRQ_ROUTING_MSI) | |
11402 | continue; | |
11403 | /* | |
11404 | * VT-d PI cannot support posting multicast/broadcast | |
11405 | * interrupts to a vCPU, we still use interrupt remapping | |
11406 | * for these kind of interrupts. | |
11407 | * | |
11408 | * For lowest-priority interrupts, we only support | |
11409 | * those with single CPU as the destination, e.g. user | |
11410 | * configures the interrupts via /proc/irq or uses | |
11411 | * irqbalance to make the interrupts single-CPU. | |
11412 | * | |
11413 | * We will support full lowest-priority interrupt later. | |
11414 | */ | |
11415 | ||
37131313 | 11416 | kvm_set_msi_irq(kvm, e, &irq); |
23a1c257 FW |
11417 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
11418 | /* | |
11419 | * Make sure the IRTE is in remapped mode if | |
11420 | * we don't handle it in posted mode. | |
11421 | */ | |
11422 | ret = irq_set_vcpu_affinity(host_irq, NULL); | |
11423 | if (ret < 0) { | |
11424 | printk(KERN_INFO | |
11425 | "failed to back to remapped mode, irq: %u\n", | |
11426 | host_irq); | |
11427 | goto out; | |
11428 | } | |
11429 | ||
efc64404 | 11430 | continue; |
23a1c257 | 11431 | } |
efc64404 FW |
11432 | |
11433 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); | |
11434 | vcpu_info.vector = irq.vector; | |
11435 | ||
b6ce9780 | 11436 | trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi, |
efc64404 FW |
11437 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
11438 | ||
11439 | if (set) | |
11440 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); | |
11441 | else { | |
11442 | /* suppress notification event before unposting */ | |
11443 | pi_set_sn(vcpu_to_pi_desc(vcpu)); | |
11444 | ret = irq_set_vcpu_affinity(host_irq, NULL); | |
11445 | pi_clear_sn(vcpu_to_pi_desc(vcpu)); | |
11446 | } | |
11447 | ||
11448 | if (ret < 0) { | |
11449 | printk(KERN_INFO "%s: failed to update PI IRTE\n", | |
11450 | __func__); | |
11451 | goto out; | |
11452 | } | |
11453 | } | |
11454 | ||
11455 | ret = 0; | |
11456 | out: | |
11457 | srcu_read_unlock(&kvm->irq_srcu, idx); | |
11458 | return ret; | |
11459 | } | |
11460 | ||
c45dcc71 AR |
11461 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
11462 | { | |
11463 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) | |
11464 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
11465 | FEATURE_CONTROL_LMCE; | |
11466 | else | |
11467 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
11468 | ~FEATURE_CONTROL_LMCE; | |
11469 | } | |
11470 | ||
404f6aac | 11471 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
6aa8b732 AK |
11472 | .cpu_has_kvm_support = cpu_has_kvm_support, |
11473 | .disabled_by_bios = vmx_disabled_by_bios, | |
11474 | .hardware_setup = hardware_setup, | |
11475 | .hardware_unsetup = hardware_unsetup, | |
002c7f7c | 11476 | .check_processor_compatibility = vmx_check_processor_compat, |
6aa8b732 AK |
11477 | .hardware_enable = hardware_enable, |
11478 | .hardware_disable = hardware_disable, | |
04547156 | 11479 | .cpu_has_accelerated_tpr = report_flexpriority, |
6d396b55 | 11480 | .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, |
6aa8b732 AK |
11481 | |
11482 | .vcpu_create = vmx_create_vcpu, | |
11483 | .vcpu_free = vmx_free_vcpu, | |
04d2cc77 | 11484 | .vcpu_reset = vmx_vcpu_reset, |
6aa8b732 | 11485 | |
04d2cc77 | 11486 | .prepare_guest_switch = vmx_save_host_state, |
6aa8b732 AK |
11487 | .vcpu_load = vmx_vcpu_load, |
11488 | .vcpu_put = vmx_vcpu_put, | |
11489 | ||
a96036b8 | 11490 | .update_bp_intercept = update_exception_bitmap, |
6aa8b732 AK |
11491 | .get_msr = vmx_get_msr, |
11492 | .set_msr = vmx_set_msr, | |
11493 | .get_segment_base = vmx_get_segment_base, | |
11494 | .get_segment = vmx_get_segment, | |
11495 | .set_segment = vmx_set_segment, | |
2e4d2653 | 11496 | .get_cpl = vmx_get_cpl, |
6aa8b732 | 11497 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
e8467fda | 11498 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
aff48baa | 11499 | .decache_cr3 = vmx_decache_cr3, |
25c4c276 | 11500 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
6aa8b732 | 11501 | .set_cr0 = vmx_set_cr0, |
6aa8b732 AK |
11502 | .set_cr3 = vmx_set_cr3, |
11503 | .set_cr4 = vmx_set_cr4, | |
6aa8b732 | 11504 | .set_efer = vmx_set_efer, |
6aa8b732 AK |
11505 | .get_idt = vmx_get_idt, |
11506 | .set_idt = vmx_set_idt, | |
11507 | .get_gdt = vmx_get_gdt, | |
11508 | .set_gdt = vmx_set_gdt, | |
73aaf249 JK |
11509 | .get_dr6 = vmx_get_dr6, |
11510 | .set_dr6 = vmx_set_dr6, | |
020df079 | 11511 | .set_dr7 = vmx_set_dr7, |
81908bf4 | 11512 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
5fdbf976 | 11513 | .cache_reg = vmx_cache_reg, |
6aa8b732 AK |
11514 | .get_rflags = vmx_get_rflags, |
11515 | .set_rflags = vmx_set_rflags, | |
be94f6b7 HH |
11516 | |
11517 | .get_pkru = vmx_get_pkru, | |
11518 | ||
6aa8b732 | 11519 | .tlb_flush = vmx_flush_tlb, |
6aa8b732 | 11520 | |
6aa8b732 | 11521 | .run = vmx_vcpu_run, |
6062d012 | 11522 | .handle_exit = vmx_handle_exit, |
6aa8b732 | 11523 | .skip_emulated_instruction = skip_emulated_instruction, |
2809f5d2 GC |
11524 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
11525 | .get_interrupt_shadow = vmx_get_interrupt_shadow, | |
102d8325 | 11526 | .patch_hypercall = vmx_patch_hypercall, |
2a8067f1 | 11527 | .set_irq = vmx_inject_irq, |
95ba8273 | 11528 | .set_nmi = vmx_inject_nmi, |
298101da | 11529 | .queue_exception = vmx_queue_exception, |
b463a6f7 | 11530 | .cancel_injection = vmx_cancel_injection, |
78646121 | 11531 | .interrupt_allowed = vmx_interrupt_allowed, |
95ba8273 | 11532 | .nmi_allowed = vmx_nmi_allowed, |
3cfc3092 JK |
11533 | .get_nmi_mask = vmx_get_nmi_mask, |
11534 | .set_nmi_mask = vmx_set_nmi_mask, | |
95ba8273 GN |
11535 | .enable_nmi_window = enable_nmi_window, |
11536 | .enable_irq_window = enable_irq_window, | |
11537 | .update_cr8_intercept = update_cr8_intercept, | |
8d14695f | 11538 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
38b99173 | 11539 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
d62caabb AS |
11540 | .get_enable_apicv = vmx_get_enable_apicv, |
11541 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, | |
c7c9c56c | 11542 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
967235d3 | 11543 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
c7c9c56c YZ |
11544 | .hwapic_irr_update = vmx_hwapic_irr_update, |
11545 | .hwapic_isr_update = vmx_hwapic_isr_update, | |
a20ed54d YZ |
11546 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
11547 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, | |
95ba8273 | 11548 | |
cbc94022 | 11549 | .set_tss_addr = vmx_set_tss_addr, |
67253af5 | 11550 | .get_tdp_level = get_ept_level, |
4b12f0de | 11551 | .get_mt_mask = vmx_get_mt_mask, |
229456fc | 11552 | |
586f9607 | 11553 | .get_exit_info = vmx_get_exit_info, |
586f9607 | 11554 | |
17cc3935 | 11555 | .get_lpage_level = vmx_get_lpage_level, |
0e851880 SY |
11556 | |
11557 | .cpuid_update = vmx_cpuid_update, | |
4e47c7a6 SY |
11558 | |
11559 | .rdtscp_supported = vmx_rdtscp_supported, | |
ad756a16 | 11560 | .invpcid_supported = vmx_invpcid_supported, |
d4330ef2 JR |
11561 | |
11562 | .set_supported_cpuid = vmx_set_supported_cpuid, | |
f5f48ee1 SY |
11563 | |
11564 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, | |
99e3e30a ZA |
11565 | |
11566 | .write_tsc_offset = vmx_write_tsc_offset, | |
1c97f0a0 JR |
11567 | |
11568 | .set_tdp_cr3 = vmx_set_cr3, | |
8a76d7f2 JR |
11569 | |
11570 | .check_intercept = vmx_check_intercept, | |
a547c6db | 11571 | .handle_external_intr = vmx_handle_external_intr, |
da8999d3 | 11572 | .mpx_supported = vmx_mpx_supported, |
55412b2e | 11573 | .xsaves_supported = vmx_xsaves_supported, |
b6b8a145 JK |
11574 | |
11575 | .check_nested_events = vmx_check_nested_events, | |
ae97a3b8 RK |
11576 | |
11577 | .sched_in = vmx_sched_in, | |
843e4330 KH |
11578 | |
11579 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, | |
11580 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, | |
11581 | .flush_log_dirty = vmx_flush_log_dirty, | |
11582 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, | |
c5f983f6 | 11583 | .write_log_dirty = vmx_write_pml_buffer, |
25462f7f | 11584 | |
bf9f6ac8 FW |
11585 | .pre_block = vmx_pre_block, |
11586 | .post_block = vmx_post_block, | |
11587 | ||
25462f7f | 11588 | .pmu_ops = &intel_pmu_ops, |
efc64404 FW |
11589 | |
11590 | .update_pi_irte = vmx_update_pi_irte, | |
64672c95 YJ |
11591 | |
11592 | #ifdef CONFIG_X86_64 | |
11593 | .set_hv_timer = vmx_set_hv_timer, | |
11594 | .cancel_hv_timer = vmx_cancel_hv_timer, | |
11595 | #endif | |
c45dcc71 AR |
11596 | |
11597 | .setup_mce = vmx_setup_mce, | |
6aa8b732 AK |
11598 | }; |
11599 | ||
11600 | static int __init vmx_init(void) | |
11601 | { | |
34a1cd60 TC |
11602 | int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
11603 | __alignof__(struct vcpu_vmx), THIS_MODULE); | |
fdef3ad1 | 11604 | if (r) |
34a1cd60 | 11605 | return r; |
25c5f225 | 11606 | |
2965faa5 | 11607 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
11608 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
11609 | crash_vmclear_local_loaded_vmcss); | |
11610 | #endif | |
11611 | ||
fdef3ad1 | 11612 | return 0; |
6aa8b732 AK |
11613 | } |
11614 | ||
11615 | static void __exit vmx_exit(void) | |
11616 | { | |
2965faa5 | 11617 | #ifdef CONFIG_KEXEC_CORE |
3b63a43f | 11618 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
8f536b76 ZY |
11619 | synchronize_rcu(); |
11620 | #endif | |
11621 | ||
cb498ea2 | 11622 | kvm_exit(); |
6aa8b732 AK |
11623 | } |
11624 | ||
11625 | module_init(vmx_init) | |
11626 | module_exit(vmx_exit) |