]>
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> |
67cd6fac | 30 | #include <linux/sched/smt.h> |
c7addb90 | 31 | #include <linux/moduleparam.h> |
e9bda3b3 | 32 | #include <linux/mod_devicetable.h> |
af658dca | 33 | #include <linux/trace_events.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
cafd6659 | 35 | #include <linux/tboot.h> |
f4124500 | 36 | #include <linux/hrtimer.h> |
c207aee4 | 37 | #include <linux/frame.h> |
bcaf287c | 38 | #include <linux/nospec.h> |
5fdbf976 | 39 | #include "kvm_cache_regs.h" |
35920a35 | 40 | #include "x86.h" |
e495606d | 41 | |
28b835d6 | 42 | #include <asm/cpu.h> |
6aa8b732 | 43 | #include <asm/io.h> |
3b3be0d1 | 44 | #include <asm/desc.h> |
13673a90 | 45 | #include <asm/vmx.h> |
6210e37b | 46 | #include <asm/virtext.h> |
a0861c02 | 47 | #include <asm/mce.h> |
952f07ec | 48 | #include <asm/fpu/internal.h> |
d7cd9796 | 49 | #include <asm/perf_event.h> |
81908bf4 | 50 | #include <asm/debugreg.h> |
8f536b76 | 51 | #include <asm/kexec.h> |
dab2087d | 52 | #include <asm/apic.h> |
efc64404 | 53 | #include <asm/irq_remapping.h> |
d6e41f11 | 54 | #include <asm/mmu_context.h> |
f471d71b | 55 | #include <asm/microcode.h> |
d7a6a163 | 56 | #include <asm/spec-ctrl.h> |
6aa8b732 | 57 | |
229456fc | 58 | #include "trace.h" |
25462f7f | 59 | #include "pmu.h" |
229456fc | 60 | |
4ecac3fd | 61 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
5e520e62 AK |
62 | #define __ex_clear(x, reg) \ |
63 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) | |
4ecac3fd | 64 | |
6aa8b732 AK |
65 | MODULE_AUTHOR("Qumranet"); |
66 | MODULE_LICENSE("GPL"); | |
67 | ||
e9bda3b3 JT |
68 | static const struct x86_cpu_id vmx_cpu_id[] = { |
69 | X86_FEATURE_MATCH(X86_FEATURE_VMX), | |
70 | {} | |
71 | }; | |
72 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); | |
73 | ||
476bc001 | 74 | static bool __read_mostly enable_vpid = 1; |
736caefe | 75 | module_param_named(vpid, enable_vpid, bool, 0444); |
2384d2b3 | 76 | |
d02fcf50 PB |
77 | static bool __read_mostly enable_vnmi = 1; |
78 | module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); | |
79 | ||
476bc001 | 80 | static bool __read_mostly flexpriority_enabled = 1; |
736caefe | 81 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
4c9fc8ef | 82 | |
476bc001 | 83 | static bool __read_mostly enable_ept = 1; |
736caefe | 84 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
d56f546d | 85 | |
476bc001 | 86 | static bool __read_mostly enable_unrestricted_guest = 1; |
3a624e29 NK |
87 | module_param_named(unrestricted_guest, |
88 | enable_unrestricted_guest, bool, S_IRUGO); | |
89 | ||
83c3a331 XH |
90 | static bool __read_mostly enable_ept_ad_bits = 1; |
91 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); | |
92 | ||
a27685c3 | 93 | static bool __read_mostly emulate_invalid_guest_state = true; |
c1f8bc04 | 94 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
04fa4d32 | 95 | |
476bc001 | 96 | static bool __read_mostly fasteoi = 1; |
58fbbf26 KT |
97 | module_param(fasteoi, bool, S_IRUGO); |
98 | ||
5a71785d | 99 | static bool __read_mostly enable_apicv = 1; |
01e439be | 100 | module_param(enable_apicv, bool, S_IRUGO); |
83d4c286 | 101 | |
abc4fc58 AG |
102 | static bool __read_mostly enable_shadow_vmcs = 1; |
103 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); | |
801d3424 NHE |
104 | /* |
105 | * If nested=1, nested virtualization is supported, i.e., guests may use | |
106 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not | |
107 | * use VMX instructions. | |
108 | */ | |
476bc001 | 109 | static bool __read_mostly nested = 0; |
801d3424 NHE |
110 | module_param(nested, bool, S_IRUGO); |
111 | ||
20300099 WL |
112 | static u64 __read_mostly host_xss; |
113 | ||
843e4330 KH |
114 | static bool __read_mostly enable_pml = 1; |
115 | module_param_named(pml, enable_pml, bool, S_IRUGO); | |
116 | ||
4b0be90f PB |
117 | #define MSR_TYPE_R 1 |
118 | #define MSR_TYPE_W 2 | |
119 | #define MSR_TYPE_RW 3 | |
120 | ||
121 | #define MSR_BITMAP_MODE_X2APIC 1 | |
122 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 | |
123 | #define MSR_BITMAP_MODE_LM 4 | |
124 | ||
64903d61 HZ |
125 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
126 | ||
64672c95 YJ |
127 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
128 | static int __read_mostly cpu_preemption_timer_multi; | |
129 | static bool __read_mostly enable_preemption_timer = 1; | |
130 | #ifdef CONFIG_X86_64 | |
131 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); | |
132 | #endif | |
133 | ||
5037878e GN |
134 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
135 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE) | |
cdc0e244 AK |
136 | #define KVM_VM_CR0_ALWAYS_ON \ |
137 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) | |
4c38609a AK |
138 | #define KVM_CR4_GUEST_OWNED_BITS \ |
139 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ | |
fd8cb433 | 140 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
4c38609a | 141 | |
cdc0e244 AK |
142 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
143 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) | |
144 | ||
78ac8b47 AK |
145 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
146 | ||
f4124500 JK |
147 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
148 | ||
16c2aec6 JD |
149 | /* |
150 | * Hyper-V requires all of these, so mark them as supported even though | |
151 | * they are just treated the same as all-context. | |
152 | */ | |
153 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ | |
154 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ | |
155 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ | |
156 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ | |
157 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) | |
158 | ||
4b8d54f9 ZE |
159 | /* |
160 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: | |
161 | * ple_gap: upper bound on the amount of time between two successive | |
162 | * executions of PAUSE in a loop. Also indicate if ple enabled. | |
00c25bce | 163 | * According to test, this time is usually smaller than 128 cycles. |
4b8d54f9 ZE |
164 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
165 | * in a PAUSE loop. Tests indicate that most spinlocks are held for | |
166 | * less than 2^12 cycles | |
167 | * Time is measured based on a counter that runs at the same rate as the TSC, | |
168 | * refer SDM volume 3b section 21.6.13 & 22.1.3. | |
169 | */ | |
b4a2d31d RK |
170 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
171 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 | |
172 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 | |
173 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 | |
174 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ | |
175 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW | |
176 | ||
4b8d54f9 ZE |
177 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
178 | module_param(ple_gap, int, S_IRUGO); | |
179 | ||
180 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; | |
181 | module_param(ple_window, int, S_IRUGO); | |
182 | ||
b4a2d31d RK |
183 | /* Default doubles per-vcpu window every exit. */ |
184 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; | |
185 | module_param(ple_window_grow, int, S_IRUGO); | |
186 | ||
187 | /* Default resets per-vcpu window every exit to ple_window. */ | |
188 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; | |
189 | module_param(ple_window_shrink, int, S_IRUGO); | |
190 | ||
191 | /* Default is to compute the maximum so we can never overflow. */ | |
192 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; | |
193 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; | |
194 | module_param(ple_window_max, int, S_IRUGO); | |
195 | ||
83287ea4 AK |
196 | extern const ulong vmx_return; |
197 | ||
1749555e | 198 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush); |
12960b11 | 199 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond); |
eeec2ec1 | 200 | static DEFINE_MUTEX(vmx_l1d_flush_mutex); |
1749555e | 201 | |
2bcd5b01 TG |
202 | /* Storage for pre module init parameter parsing */ |
203 | static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO; | |
1749555e KRW |
204 | |
205 | static const struct { | |
206 | const char *option; | |
1dd6f98d | 207 | bool for_parse; |
1749555e | 208 | } vmentry_l1d_param[] = { |
1dd6f98d PB |
209 | [VMENTER_L1D_FLUSH_AUTO] = {"auto", true}, |
210 | [VMENTER_L1D_FLUSH_NEVER] = {"never", true}, | |
211 | [VMENTER_L1D_FLUSH_COND] = {"cond", true}, | |
212 | [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true}, | |
213 | [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false}, | |
214 | [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false}, | |
1749555e KRW |
215 | }; |
216 | ||
2bcd5b01 TG |
217 | #define L1D_CACHE_ORDER 4 |
218 | static void *vmx_l1d_flush_pages; | |
219 | ||
220 | static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf) | |
1749555e | 221 | { |
2bcd5b01 | 222 | struct page *page; |
55bd6950 | 223 | unsigned int i; |
1749555e | 224 | |
2bcd5b01 TG |
225 | if (!enable_ept) { |
226 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED; | |
227 | return 0; | |
228 | } | |
229 | ||
917d427f JH |
230 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) { |
231 | u64 msr; | |
232 | ||
233 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr); | |
234 | if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) { | |
235 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED; | |
236 | return 0; | |
237 | } | |
238 | } | |
364a4311 | 239 | |
24fcb53c JK |
240 | /* If set to auto use the default l1tf mitigation method */ |
241 | if (l1tf == VMENTER_L1D_FLUSH_AUTO) { | |
242 | switch (l1tf_mitigation) { | |
243 | case L1TF_MITIGATION_OFF: | |
244 | l1tf = VMENTER_L1D_FLUSH_NEVER; | |
245 | break; | |
246 | case L1TF_MITIGATION_FLUSH_NOWARN: | |
247 | case L1TF_MITIGATION_FLUSH: | |
248 | case L1TF_MITIGATION_FLUSH_NOSMT: | |
249 | l1tf = VMENTER_L1D_FLUSH_COND; | |
250 | break; | |
251 | case L1TF_MITIGATION_FULL: | |
252 | case L1TF_MITIGATION_FULL_FORCE: | |
253 | l1tf = VMENTER_L1D_FLUSH_ALWAYS; | |
254 | break; | |
255 | } | |
256 | } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) { | |
257 | l1tf = VMENTER_L1D_FLUSH_ALWAYS; | |
258 | } | |
259 | ||
2bcd5b01 TG |
260 | if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages && |
261 | !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) { | |
262 | page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER); | |
263 | if (!page) | |
264 | return -ENOMEM; | |
265 | vmx_l1d_flush_pages = page_address(page); | |
55bd6950 NS |
266 | |
267 | /* | |
268 | * Initialize each page with a different pattern in | |
269 | * order to protect against KSM in the nested | |
270 | * virtualization case. | |
271 | */ | |
272 | for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) { | |
273 | memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1, | |
274 | PAGE_SIZE); | |
275 | } | |
1749555e KRW |
276 | } |
277 | ||
2bcd5b01 TG |
278 | l1tf_vmx_mitigation = l1tf; |
279 | ||
522c7bed TG |
280 | if (l1tf != VMENTER_L1D_FLUSH_NEVER) |
281 | static_branch_enable(&vmx_l1d_should_flush); | |
282 | else | |
283 | static_branch_disable(&vmx_l1d_should_flush); | |
ae021965 | 284 | |
12960b11 NS |
285 | if (l1tf == VMENTER_L1D_FLUSH_COND) |
286 | static_branch_enable(&vmx_l1d_flush_cond); | |
522c7bed | 287 | else |
12960b11 | 288 | static_branch_disable(&vmx_l1d_flush_cond); |
2bcd5b01 TG |
289 | return 0; |
290 | } | |
291 | ||
292 | static int vmentry_l1d_flush_parse(const char *s) | |
293 | { | |
294 | unsigned int i; | |
295 | ||
296 | if (s) { | |
297 | for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) { | |
1dd6f98d PB |
298 | if (vmentry_l1d_param[i].for_parse && |
299 | sysfs_streq(s, vmentry_l1d_param[i].option)) | |
300 | return i; | |
2bcd5b01 TG |
301 | } |
302 | } | |
1749555e KRW |
303 | return -EINVAL; |
304 | } | |
305 | ||
2bcd5b01 TG |
306 | static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp) |
307 | { | |
eeec2ec1 | 308 | int l1tf, ret; |
2bcd5b01 | 309 | |
2bcd5b01 TG |
310 | l1tf = vmentry_l1d_flush_parse(s); |
311 | if (l1tf < 0) | |
312 | return l1tf; | |
313 | ||
1dd6f98d PB |
314 | if (!boot_cpu_has(X86_BUG_L1TF)) |
315 | return 0; | |
316 | ||
2bcd5b01 TG |
317 | /* |
318 | * Has vmx_init() run already? If not then this is the pre init | |
319 | * parameter parsing. In that case just store the value and let | |
320 | * vmx_init() do the proper setup after enable_ept has been | |
321 | * established. | |
322 | */ | |
323 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) { | |
324 | vmentry_l1d_flush_param = l1tf; | |
325 | return 0; | |
326 | } | |
327 | ||
eeec2ec1 TG |
328 | mutex_lock(&vmx_l1d_flush_mutex); |
329 | ret = vmx_setup_l1d_flush(l1tf); | |
330 | mutex_unlock(&vmx_l1d_flush_mutex); | |
331 | return ret; | |
2bcd5b01 TG |
332 | } |
333 | ||
1749555e KRW |
334 | static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp) |
335 | { | |
1dd6f98d PB |
336 | if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param))) |
337 | return sprintf(s, "???\n"); | |
338 | ||
2bcd5b01 | 339 | return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option); |
1749555e KRW |
340 | } |
341 | ||
342 | static const struct kernel_param_ops vmentry_l1d_flush_ops = { | |
343 | .set = vmentry_l1d_flush_set, | |
344 | .get = vmentry_l1d_flush_get, | |
345 | }; | |
522c7bed | 346 | module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644); |
1749555e | 347 | |
8bf00a52 | 348 | #define NR_AUTOLOAD_MSRS 8 |
61d2ef2c | 349 | |
a2fa3e9f GH |
350 | struct vmcs { |
351 | u32 revision_id; | |
352 | u32 abort; | |
353 | char data[0]; | |
354 | }; | |
355 | ||
d462b819 NHE |
356 | /* |
357 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also | |
358 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs | |
359 | * loaded on this CPU (so we can clear them if the CPU goes down). | |
360 | */ | |
361 | struct loaded_vmcs { | |
362 | struct vmcs *vmcs; | |
355f4fb1 | 363 | struct vmcs *shadow_vmcs; |
d462b819 | 364 | int cpu; |
4c4a6f79 PB |
365 | bool launched; |
366 | bool nmi_known_unmasked; | |
44889942 LP |
367 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
368 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ | |
8a1b4392 PB |
369 | /* Support for vnmi-less CPUs */ |
370 | int soft_vnmi_blocked; | |
371 | ktime_t entry_time; | |
372 | s64 vnmi_blocked_time; | |
4b0be90f | 373 | unsigned long *msr_bitmap; |
d462b819 NHE |
374 | struct list_head loaded_vmcss_on_cpu_link; |
375 | }; | |
376 | ||
26bb0981 AK |
377 | struct shared_msr_entry { |
378 | unsigned index; | |
379 | u64 data; | |
d5696725 | 380 | u64 mask; |
26bb0981 AK |
381 | }; |
382 | ||
a9d30f33 NHE |
383 | /* |
384 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a | |
385 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has | |
386 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is | |
387 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, | |
388 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. | |
389 | * More than one of these structures may exist, if L1 runs multiple L2 guests. | |
8819227c | 390 | * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the |
a9d30f33 NHE |
391 | * underlying hardware which will be used to run L2. |
392 | * This structure is packed to ensure that its layout is identical across | |
393 | * machines (necessary for live migration). | |
394 | * If there are changes in this struct, VMCS12_REVISION must be changed. | |
395 | */ | |
22bd0358 | 396 | typedef u64 natural_width; |
a9d30f33 NHE |
397 | struct __packed vmcs12 { |
398 | /* According to the Intel spec, a VMCS region must start with the | |
399 | * following two fields. Then follow implementation-specific data. | |
400 | */ | |
401 | u32 revision_id; | |
402 | u32 abort; | |
22bd0358 | 403 | |
27d6c865 NHE |
404 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
405 | u32 padding[7]; /* room for future expansion */ | |
406 | ||
22bd0358 NHE |
407 | u64 io_bitmap_a; |
408 | u64 io_bitmap_b; | |
409 | u64 msr_bitmap; | |
410 | u64 vm_exit_msr_store_addr; | |
411 | u64 vm_exit_msr_load_addr; | |
412 | u64 vm_entry_msr_load_addr; | |
413 | u64 tsc_offset; | |
414 | u64 virtual_apic_page_addr; | |
415 | u64 apic_access_addr; | |
705699a1 | 416 | u64 posted_intr_desc_addr; |
27c42a1b | 417 | u64 vm_function_control; |
22bd0358 | 418 | u64 ept_pointer; |
608406e2 WV |
419 | u64 eoi_exit_bitmap0; |
420 | u64 eoi_exit_bitmap1; | |
421 | u64 eoi_exit_bitmap2; | |
422 | u64 eoi_exit_bitmap3; | |
41ab9372 | 423 | u64 eptp_list_address; |
81dc01f7 | 424 | u64 xss_exit_bitmap; |
22bd0358 NHE |
425 | u64 guest_physical_address; |
426 | u64 vmcs_link_pointer; | |
c5f983f6 | 427 | u64 pml_address; |
22bd0358 NHE |
428 | u64 guest_ia32_debugctl; |
429 | u64 guest_ia32_pat; | |
430 | u64 guest_ia32_efer; | |
431 | u64 guest_ia32_perf_global_ctrl; | |
432 | u64 guest_pdptr0; | |
433 | u64 guest_pdptr1; | |
434 | u64 guest_pdptr2; | |
435 | u64 guest_pdptr3; | |
36be0b9d | 436 | u64 guest_bndcfgs; |
22bd0358 NHE |
437 | u64 host_ia32_pat; |
438 | u64 host_ia32_efer; | |
439 | u64 host_ia32_perf_global_ctrl; | |
440 | u64 padding64[8]; /* room for future expansion */ | |
441 | /* | |
442 | * To allow migration of L1 (complete with its L2 guests) between | |
443 | * machines of different natural widths (32 or 64 bit), we cannot have | |
444 | * unsigned long fields with no explict size. We use u64 (aliased | |
445 | * natural_width) instead. Luckily, x86 is little-endian. | |
446 | */ | |
447 | natural_width cr0_guest_host_mask; | |
448 | natural_width cr4_guest_host_mask; | |
449 | natural_width cr0_read_shadow; | |
450 | natural_width cr4_read_shadow; | |
451 | natural_width cr3_target_value0; | |
452 | natural_width cr3_target_value1; | |
453 | natural_width cr3_target_value2; | |
454 | natural_width cr3_target_value3; | |
455 | natural_width exit_qualification; | |
456 | natural_width guest_linear_address; | |
457 | natural_width guest_cr0; | |
458 | natural_width guest_cr3; | |
459 | natural_width guest_cr4; | |
460 | natural_width guest_es_base; | |
461 | natural_width guest_cs_base; | |
462 | natural_width guest_ss_base; | |
463 | natural_width guest_ds_base; | |
464 | natural_width guest_fs_base; | |
465 | natural_width guest_gs_base; | |
466 | natural_width guest_ldtr_base; | |
467 | natural_width guest_tr_base; | |
468 | natural_width guest_gdtr_base; | |
469 | natural_width guest_idtr_base; | |
470 | natural_width guest_dr7; | |
471 | natural_width guest_rsp; | |
472 | natural_width guest_rip; | |
473 | natural_width guest_rflags; | |
474 | natural_width guest_pending_dbg_exceptions; | |
475 | natural_width guest_sysenter_esp; | |
476 | natural_width guest_sysenter_eip; | |
477 | natural_width host_cr0; | |
478 | natural_width host_cr3; | |
479 | natural_width host_cr4; | |
480 | natural_width host_fs_base; | |
481 | natural_width host_gs_base; | |
482 | natural_width host_tr_base; | |
483 | natural_width host_gdtr_base; | |
484 | natural_width host_idtr_base; | |
485 | natural_width host_ia32_sysenter_esp; | |
486 | natural_width host_ia32_sysenter_eip; | |
487 | natural_width host_rsp; | |
488 | natural_width host_rip; | |
489 | natural_width paddingl[8]; /* room for future expansion */ | |
490 | u32 pin_based_vm_exec_control; | |
491 | u32 cpu_based_vm_exec_control; | |
492 | u32 exception_bitmap; | |
493 | u32 page_fault_error_code_mask; | |
494 | u32 page_fault_error_code_match; | |
495 | u32 cr3_target_count; | |
496 | u32 vm_exit_controls; | |
497 | u32 vm_exit_msr_store_count; | |
498 | u32 vm_exit_msr_load_count; | |
499 | u32 vm_entry_controls; | |
500 | u32 vm_entry_msr_load_count; | |
501 | u32 vm_entry_intr_info_field; | |
502 | u32 vm_entry_exception_error_code; | |
503 | u32 vm_entry_instruction_len; | |
504 | u32 tpr_threshold; | |
505 | u32 secondary_vm_exec_control; | |
506 | u32 vm_instruction_error; | |
507 | u32 vm_exit_reason; | |
508 | u32 vm_exit_intr_info; | |
509 | u32 vm_exit_intr_error_code; | |
510 | u32 idt_vectoring_info_field; | |
511 | u32 idt_vectoring_error_code; | |
512 | u32 vm_exit_instruction_len; | |
513 | u32 vmx_instruction_info; | |
514 | u32 guest_es_limit; | |
515 | u32 guest_cs_limit; | |
516 | u32 guest_ss_limit; | |
517 | u32 guest_ds_limit; | |
518 | u32 guest_fs_limit; | |
519 | u32 guest_gs_limit; | |
520 | u32 guest_ldtr_limit; | |
521 | u32 guest_tr_limit; | |
522 | u32 guest_gdtr_limit; | |
523 | u32 guest_idtr_limit; | |
524 | u32 guest_es_ar_bytes; | |
525 | u32 guest_cs_ar_bytes; | |
526 | u32 guest_ss_ar_bytes; | |
527 | u32 guest_ds_ar_bytes; | |
528 | u32 guest_fs_ar_bytes; | |
529 | u32 guest_gs_ar_bytes; | |
530 | u32 guest_ldtr_ar_bytes; | |
531 | u32 guest_tr_ar_bytes; | |
532 | u32 guest_interruptibility_info; | |
533 | u32 guest_activity_state; | |
534 | u32 guest_sysenter_cs; | |
535 | u32 host_ia32_sysenter_cs; | |
0238ea91 JK |
536 | u32 vmx_preemption_timer_value; |
537 | u32 padding32[7]; /* room for future expansion */ | |
22bd0358 | 538 | u16 virtual_processor_id; |
705699a1 | 539 | u16 posted_intr_nv; |
22bd0358 NHE |
540 | u16 guest_es_selector; |
541 | u16 guest_cs_selector; | |
542 | u16 guest_ss_selector; | |
543 | u16 guest_ds_selector; | |
544 | u16 guest_fs_selector; | |
545 | u16 guest_gs_selector; | |
546 | u16 guest_ldtr_selector; | |
547 | u16 guest_tr_selector; | |
608406e2 | 548 | u16 guest_intr_status; |
c5f983f6 | 549 | u16 guest_pml_index; |
22bd0358 NHE |
550 | u16 host_es_selector; |
551 | u16 host_cs_selector; | |
552 | u16 host_ss_selector; | |
553 | u16 host_ds_selector; | |
554 | u16 host_fs_selector; | |
555 | u16 host_gs_selector; | |
556 | u16 host_tr_selector; | |
a9d30f33 NHE |
557 | }; |
558 | ||
559 | /* | |
560 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or | |
561 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and | |
562 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. | |
563 | */ | |
564 | #define VMCS12_REVISION 0x11e57ed0 | |
565 | ||
566 | /* | |
567 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region | |
568 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the | |
569 | * current implementation, 4K are reserved to avoid future complications. | |
570 | */ | |
571 | #define VMCS12_SIZE 0x1000 | |
572 | ||
ec378aee NHE |
573 | /* |
574 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need | |
575 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. | |
576 | */ | |
577 | struct nested_vmx { | |
578 | /* Has the level1 guest done vmxon? */ | |
579 | bool vmxon; | |
3573e22c | 580 | gpa_t vmxon_ptr; |
c5f983f6 | 581 | bool pml_full; |
a9d30f33 NHE |
582 | |
583 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ | |
584 | gpa_t current_vmptr; | |
4f2777bc DM |
585 | /* |
586 | * Cache of the guest's VMCS, existing outside of guest memory. | |
587 | * Loaded from guest memory during VMPTRLD. Flushed to guest | |
8ca44e88 | 588 | * memory during VMCLEAR and VMPTRLD. |
4f2777bc DM |
589 | */ |
590 | struct vmcs12 *cached_vmcs12; | |
012f83cb AG |
591 | /* |
592 | * Indicates if the shadow vmcs must be updated with the | |
593 | * data hold by vmcs12 | |
594 | */ | |
595 | bool sync_shadow_vmcs; | |
ff2f6fe9 | 596 | |
d3f4c0a5 JM |
597 | bool change_vmcs01_virtual_apic_mode; |
598 | ||
644d711a NHE |
599 | /* L2 must run next, and mustn't decide to exit to L1. */ |
600 | bool nested_run_pending; | |
8819227c JM |
601 | |
602 | struct loaded_vmcs vmcs02; | |
603 | ||
fe3ef05c | 604 | /* |
8819227c JM |
605 | * Guest pages referred to in the vmcs02 with host-physical |
606 | * pointers, so we must keep them pinned while L2 runs. | |
fe3ef05c NHE |
607 | */ |
608 | struct page *apic_access_page; | |
a7c0b07d | 609 | struct page *virtual_apic_page; |
705699a1 WV |
610 | struct page *pi_desc_page; |
611 | struct pi_desc *pi_desc; | |
612 | bool pi_pending; | |
613 | u16 posted_intr_nv; | |
f4124500 JK |
614 | |
615 | struct hrtimer preemption_timer; | |
616 | bool preemption_timer_expired; | |
2996fca0 JK |
617 | |
618 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ | |
619 | u64 vmcs01_debugctl; | |
b9c237bb | 620 | |
5c614b35 WL |
621 | u16 vpid02; |
622 | u16 last_vpid; | |
623 | ||
0115f9cb DM |
624 | /* |
625 | * We only store the "true" versions of the VMX capability MSRs. We | |
626 | * generate the "non-true" versions by setting the must-be-1 bits | |
627 | * according to the SDM. | |
628 | */ | |
b9c237bb WV |
629 | u32 nested_vmx_procbased_ctls_low; |
630 | u32 nested_vmx_procbased_ctls_high; | |
b9c237bb WV |
631 | u32 nested_vmx_secondary_ctls_low; |
632 | u32 nested_vmx_secondary_ctls_high; | |
633 | u32 nested_vmx_pinbased_ctls_low; | |
634 | u32 nested_vmx_pinbased_ctls_high; | |
635 | u32 nested_vmx_exit_ctls_low; | |
636 | u32 nested_vmx_exit_ctls_high; | |
b9c237bb WV |
637 | u32 nested_vmx_entry_ctls_low; |
638 | u32 nested_vmx_entry_ctls_high; | |
b9c237bb WV |
639 | u32 nested_vmx_misc_low; |
640 | u32 nested_vmx_misc_high; | |
641 | u32 nested_vmx_ept_caps; | |
99b83ac8 | 642 | u32 nested_vmx_vpid_caps; |
62cc6b9d DM |
643 | u64 nested_vmx_basic; |
644 | u64 nested_vmx_cr0_fixed0; | |
645 | u64 nested_vmx_cr0_fixed1; | |
646 | u64 nested_vmx_cr4_fixed0; | |
647 | u64 nested_vmx_cr4_fixed1; | |
648 | u64 nested_vmx_vmcs_enum; | |
27c42a1b | 649 | u64 nested_vmx_vmfunc_controls; |
72e9cbdb LP |
650 | |
651 | /* SMM related state */ | |
652 | struct { | |
653 | /* in VMX operation on SMM entry? */ | |
654 | bool vmxon; | |
655 | /* in guest mode on SMM entry? */ | |
656 | bool guest_mode; | |
657 | } smm; | |
ec378aee NHE |
658 | }; |
659 | ||
01e439be | 660 | #define POSTED_INTR_ON 0 |
ebbfc765 FW |
661 | #define POSTED_INTR_SN 1 |
662 | ||
01e439be YZ |
663 | /* Posted-Interrupt Descriptor */ |
664 | struct pi_desc { | |
665 | u32 pir[8]; /* Posted interrupt requested */ | |
6ef1522f FW |
666 | union { |
667 | struct { | |
668 | /* bit 256 - Outstanding Notification */ | |
669 | u16 on : 1, | |
670 | /* bit 257 - Suppress Notification */ | |
671 | sn : 1, | |
672 | /* bit 271:258 - Reserved */ | |
673 | rsvd_1 : 14; | |
674 | /* bit 279:272 - Notification Vector */ | |
675 | u8 nv; | |
676 | /* bit 287:280 - Reserved */ | |
677 | u8 rsvd_2; | |
678 | /* bit 319:288 - Notification Destination */ | |
679 | u32 ndst; | |
680 | }; | |
681 | u64 control; | |
682 | }; | |
683 | u32 rsvd[6]; | |
01e439be YZ |
684 | } __aligned(64); |
685 | ||
a20ed54d YZ |
686 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
687 | { | |
688 | return test_and_set_bit(POSTED_INTR_ON, | |
689 | (unsigned long *)&pi_desc->control); | |
690 | } | |
691 | ||
692 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) | |
693 | { | |
694 | return test_and_clear_bit(POSTED_INTR_ON, | |
695 | (unsigned long *)&pi_desc->control); | |
696 | } | |
697 | ||
698 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) | |
699 | { | |
700 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); | |
701 | } | |
702 | ||
ebbfc765 FW |
703 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
704 | { | |
705 | return clear_bit(POSTED_INTR_SN, | |
706 | (unsigned long *)&pi_desc->control); | |
707 | } | |
708 | ||
709 | static inline void pi_set_sn(struct pi_desc *pi_desc) | |
710 | { | |
711 | return set_bit(POSTED_INTR_SN, | |
712 | (unsigned long *)&pi_desc->control); | |
713 | } | |
714 | ||
ad361091 PB |
715 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
716 | { | |
717 | clear_bit(POSTED_INTR_ON, | |
718 | (unsigned long *)&pi_desc->control); | |
719 | } | |
720 | ||
ebbfc765 FW |
721 | static inline int pi_test_on(struct pi_desc *pi_desc) |
722 | { | |
723 | return test_bit(POSTED_INTR_ON, | |
724 | (unsigned long *)&pi_desc->control); | |
725 | } | |
726 | ||
727 | static inline int pi_test_sn(struct pi_desc *pi_desc) | |
728 | { | |
729 | return test_bit(POSTED_INTR_SN, | |
730 | (unsigned long *)&pi_desc->control); | |
731 | } | |
732 | ||
6e3dedb6 KRW |
733 | struct vmx_msrs { |
734 | unsigned int nr; | |
735 | struct vmx_msr_entry val[NR_AUTOLOAD_MSRS]; | |
736 | }; | |
737 | ||
a2fa3e9f | 738 | struct vcpu_vmx { |
fb3f0f51 | 739 | struct kvm_vcpu vcpu; |
313dbd49 | 740 | unsigned long host_rsp; |
29bd8a78 | 741 | u8 fail; |
4b0be90f | 742 | u8 msr_bitmap_mode; |
51aa01d1 | 743 | u32 exit_intr_info; |
1155f76a | 744 | u32 idt_vectoring_info; |
6de12732 | 745 | ulong rflags; |
26bb0981 | 746 | struct shared_msr_entry *guest_msrs; |
a2fa3e9f GH |
747 | int nmsrs; |
748 | int save_nmsrs; | |
a547c6db | 749 | unsigned long host_idt_base; |
a2fa3e9f | 750 | #ifdef CONFIG_X86_64 |
44ea2b17 AK |
751 | u64 msr_host_kernel_gs_base; |
752 | u64 msr_guest_kernel_gs_base; | |
a2fa3e9f | 753 | #endif |
33241bfe | 754 | |
74469996 | 755 | u64 spec_ctrl; |
a6005a79 | 756 | |
2961e876 GN |
757 | u32 vm_entry_controls_shadow; |
758 | u32 vm_exit_controls_shadow; | |
80154d77 PB |
759 | u32 secondary_exec_control; |
760 | ||
d462b819 NHE |
761 | /* |
762 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a | |
763 | * non-nested (L1) guest, it always points to vmcs01. For a nested | |
8a12167a SC |
764 | * guest (L2), it points to a different VMCS. loaded_cpu_state points |
765 | * to the VMCS whose state is loaded into the CPU registers that only | |
766 | * need to be switched when transitioning to/from the kernel; a NULL | |
767 | * value indicates that host state is loaded. | |
d462b819 NHE |
768 | */ |
769 | struct loaded_vmcs vmcs01; | |
770 | struct loaded_vmcs *loaded_vmcs; | |
8a12167a | 771 | struct loaded_vmcs *loaded_cpu_state; |
d462b819 | 772 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
61d2ef2c | 773 | struct msr_autoload { |
6e3dedb6 KRW |
774 | struct vmx_msrs guest; |
775 | struct vmx_msrs host; | |
61d2ef2c | 776 | } msr_autoload; |
8a12167a | 777 | |
a2fa3e9f | 778 | struct { |
a2fa3e9f | 779 | u16 fs_sel, gs_sel, ldt_sel; |
b2da15ac AK |
780 | #ifdef CONFIG_X86_64 |
781 | u16 ds_sel, es_sel; | |
782 | #endif | |
152d3f2f LV |
783 | int gs_ldt_reload_needed; |
784 | int fs_reload_needed; | |
da8999d3 | 785 | u64 msr_host_bndcfgs; |
d77c26fc | 786 | } host_state; |
9c8cba37 | 787 | struct { |
7ffd92c5 | 788 | int vm86_active; |
78ac8b47 | 789 | ulong save_rflags; |
f5f7b2fe AK |
790 | struct kvm_segment segs[8]; |
791 | } rmode; | |
792 | struct { | |
793 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ | |
7ffd92c5 AK |
794 | struct kvm_save_segment { |
795 | u16 selector; | |
796 | unsigned long base; | |
797 | u32 limit; | |
798 | u32 ar; | |
f5f7b2fe | 799 | } seg[8]; |
2fb92db1 | 800 | } segment_cache; |
2384d2b3 | 801 | int vpid; |
04fa4d32 | 802 | bool emulation_required; |
3b86cd99 | 803 | |
a0861c02 | 804 | u32 exit_reason; |
4e47c7a6 | 805 | |
01e439be YZ |
806 | /* Posted interrupt descriptor */ |
807 | struct pi_desc pi_desc; | |
808 | ||
ec378aee NHE |
809 | /* Support for a guest hypervisor (nested VMX) */ |
810 | struct nested_vmx nested; | |
a7653ecd RK |
811 | |
812 | /* Dynamic PLE window. */ | |
813 | int ple_window; | |
814 | bool ple_window_dirty; | |
843e4330 KH |
815 | |
816 | /* Support for PML */ | |
817 | #define PML_ENTITY_NUM 512 | |
818 | struct page *pml_pg; | |
2680d6da | 819 | |
64672c95 YJ |
820 | /* apic deadline value in host tsc */ |
821 | u64 hv_deadline_tsc; | |
822 | ||
2680d6da | 823 | u64 current_tsc_ratio; |
1be0e61c | 824 | |
1be0e61c | 825 | u32 host_pkru; |
3b84080b | 826 | |
37e4c997 HZ |
827 | /* |
828 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in | |
829 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included | |
830 | * in msr_ia32_feature_control_valid_bits. | |
831 | */ | |
3b84080b | 832 | u64 msr_ia32_feature_control; |
37e4c997 | 833 | u64 msr_ia32_feature_control_valid_bits; |
a2fa3e9f GH |
834 | }; |
835 | ||
2fb92db1 AK |
836 | enum segment_cache_field { |
837 | SEG_FIELD_SEL = 0, | |
838 | SEG_FIELD_BASE = 1, | |
839 | SEG_FIELD_LIMIT = 2, | |
840 | SEG_FIELD_AR = 3, | |
841 | ||
842 | SEG_FIELD_NR = 4 | |
843 | }; | |
844 | ||
a2fa3e9f GH |
845 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
846 | { | |
fb3f0f51 | 847 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
a2fa3e9f GH |
848 | } |
849 | ||
efc64404 FW |
850 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
851 | { | |
852 | return &(to_vmx(vcpu)->pi_desc); | |
853 | } | |
854 | ||
22bd0358 NHE |
855 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
856 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) | |
857 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ | |
858 | [number##_HIGH] = VMCS12_OFFSET(name)+4 | |
859 | ||
4607c2d7 | 860 | |
fe2b201b | 861 | static unsigned long shadow_read_only_fields[] = { |
4607c2d7 AG |
862 | /* |
863 | * We do NOT shadow fields that are modified when L0 | |
864 | * traps and emulates any vmx instruction (e.g. VMPTRLD, | |
865 | * VMXON...) executed by L1. | |
866 | * For example, VM_INSTRUCTION_ERROR is read | |
867 | * by L1 if a vmx instruction fails (part of the error path). | |
868 | * Note the code assumes this logic. If for some reason | |
869 | * we start shadowing these fields then we need to | |
870 | * force a shadow sync when L0 emulates vmx instructions | |
871 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified | |
872 | * by nested_vmx_failValid) | |
873 | */ | |
874 | VM_EXIT_REASON, | |
875 | VM_EXIT_INTR_INFO, | |
876 | VM_EXIT_INSTRUCTION_LEN, | |
877 | IDT_VECTORING_INFO_FIELD, | |
878 | IDT_VECTORING_ERROR_CODE, | |
879 | VM_EXIT_INTR_ERROR_CODE, | |
880 | EXIT_QUALIFICATION, | |
881 | GUEST_LINEAR_ADDRESS, | |
882 | GUEST_PHYSICAL_ADDRESS | |
883 | }; | |
fe2b201b | 884 | static int max_shadow_read_only_fields = |
4607c2d7 AG |
885 | ARRAY_SIZE(shadow_read_only_fields); |
886 | ||
fe2b201b | 887 | static unsigned long shadow_read_write_fields[] = { |
a7c0b07d | 888 | TPR_THRESHOLD, |
4607c2d7 AG |
889 | GUEST_RIP, |
890 | GUEST_RSP, | |
891 | GUEST_CR0, | |
892 | GUEST_CR3, | |
893 | GUEST_CR4, | |
894 | GUEST_INTERRUPTIBILITY_INFO, | |
895 | GUEST_RFLAGS, | |
896 | GUEST_CS_SELECTOR, | |
897 | GUEST_CS_AR_BYTES, | |
898 | GUEST_CS_LIMIT, | |
899 | GUEST_CS_BASE, | |
900 | GUEST_ES_BASE, | |
36be0b9d | 901 | GUEST_BNDCFGS, |
4607c2d7 AG |
902 | CR0_GUEST_HOST_MASK, |
903 | CR0_READ_SHADOW, | |
904 | CR4_READ_SHADOW, | |
905 | TSC_OFFSET, | |
906 | EXCEPTION_BITMAP, | |
907 | CPU_BASED_VM_EXEC_CONTROL, | |
908 | VM_ENTRY_EXCEPTION_ERROR_CODE, | |
909 | VM_ENTRY_INTR_INFO_FIELD, | |
910 | VM_ENTRY_INSTRUCTION_LEN, | |
911 | VM_ENTRY_EXCEPTION_ERROR_CODE, | |
912 | HOST_FS_BASE, | |
913 | HOST_GS_BASE, | |
914 | HOST_FS_SELECTOR, | |
915 | HOST_GS_SELECTOR | |
916 | }; | |
fe2b201b | 917 | static int max_shadow_read_write_fields = |
4607c2d7 AG |
918 | ARRAY_SIZE(shadow_read_write_fields); |
919 | ||
772e0318 | 920 | static const unsigned short vmcs_field_to_offset_table[] = { |
22bd0358 | 921 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
705699a1 | 922 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
22bd0358 NHE |
923 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
924 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), | |
925 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), | |
926 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), | |
927 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), | |
928 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), | |
929 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), | |
930 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), | |
608406e2 | 931 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
c5f983f6 | 932 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
22bd0358 NHE |
933 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
934 | FIELD(HOST_CS_SELECTOR, host_cs_selector), | |
935 | FIELD(HOST_SS_SELECTOR, host_ss_selector), | |
936 | FIELD(HOST_DS_SELECTOR, host_ds_selector), | |
937 | FIELD(HOST_FS_SELECTOR, host_fs_selector), | |
938 | FIELD(HOST_GS_SELECTOR, host_gs_selector), | |
939 | FIELD(HOST_TR_SELECTOR, host_tr_selector), | |
940 | FIELD64(IO_BITMAP_A, io_bitmap_a), | |
941 | FIELD64(IO_BITMAP_B, io_bitmap_b), | |
942 | FIELD64(MSR_BITMAP, msr_bitmap), | |
943 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), | |
944 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), | |
945 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), | |
946 | FIELD64(TSC_OFFSET, tsc_offset), | |
947 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), | |
948 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), | |
705699a1 | 949 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
27c42a1b | 950 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
22bd0358 | 951 | FIELD64(EPT_POINTER, ept_pointer), |
608406e2 WV |
952 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
953 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), | |
954 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), | |
955 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), | |
41ab9372 | 956 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
81dc01f7 | 957 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
22bd0358 NHE |
958 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
959 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), | |
c5f983f6 | 960 | FIELD64(PML_ADDRESS, pml_address), |
22bd0358 NHE |
961 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
962 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), | |
963 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), | |
964 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), | |
965 | FIELD64(GUEST_PDPTR0, guest_pdptr0), | |
966 | FIELD64(GUEST_PDPTR1, guest_pdptr1), | |
967 | FIELD64(GUEST_PDPTR2, guest_pdptr2), | |
968 | FIELD64(GUEST_PDPTR3, guest_pdptr3), | |
36be0b9d | 969 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
22bd0358 NHE |
970 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
971 | FIELD64(HOST_IA32_EFER, host_ia32_efer), | |
972 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), | |
973 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), | |
974 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), | |
975 | FIELD(EXCEPTION_BITMAP, exception_bitmap), | |
976 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), | |
977 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), | |
978 | FIELD(CR3_TARGET_COUNT, cr3_target_count), | |
979 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), | |
980 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), | |
981 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), | |
982 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), | |
983 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), | |
984 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), | |
985 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), | |
986 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), | |
987 | FIELD(TPR_THRESHOLD, tpr_threshold), | |
988 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), | |
989 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), | |
990 | FIELD(VM_EXIT_REASON, vm_exit_reason), | |
991 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), | |
992 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), | |
993 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), | |
994 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), | |
995 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), | |
996 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), | |
997 | FIELD(GUEST_ES_LIMIT, guest_es_limit), | |
998 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), | |
999 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), | |
1000 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), | |
1001 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), | |
1002 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), | |
1003 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), | |
1004 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), | |
1005 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), | |
1006 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), | |
1007 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), | |
1008 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), | |
1009 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), | |
1010 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), | |
1011 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), | |
1012 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), | |
1013 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), | |
1014 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), | |
1015 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), | |
1016 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), | |
1017 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), | |
1018 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), | |
0238ea91 | 1019 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
22bd0358 NHE |
1020 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
1021 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), | |
1022 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), | |
1023 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), | |
1024 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), | |
1025 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), | |
1026 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), | |
1027 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), | |
1028 | FIELD(EXIT_QUALIFICATION, exit_qualification), | |
1029 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), | |
1030 | FIELD(GUEST_CR0, guest_cr0), | |
1031 | FIELD(GUEST_CR3, guest_cr3), | |
1032 | FIELD(GUEST_CR4, guest_cr4), | |
1033 | FIELD(GUEST_ES_BASE, guest_es_base), | |
1034 | FIELD(GUEST_CS_BASE, guest_cs_base), | |
1035 | FIELD(GUEST_SS_BASE, guest_ss_base), | |
1036 | FIELD(GUEST_DS_BASE, guest_ds_base), | |
1037 | FIELD(GUEST_FS_BASE, guest_fs_base), | |
1038 | FIELD(GUEST_GS_BASE, guest_gs_base), | |
1039 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), | |
1040 | FIELD(GUEST_TR_BASE, guest_tr_base), | |
1041 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), | |
1042 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), | |
1043 | FIELD(GUEST_DR7, guest_dr7), | |
1044 | FIELD(GUEST_RSP, guest_rsp), | |
1045 | FIELD(GUEST_RIP, guest_rip), | |
1046 | FIELD(GUEST_RFLAGS, guest_rflags), | |
1047 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), | |
1048 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), | |
1049 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), | |
1050 | FIELD(HOST_CR0, host_cr0), | |
1051 | FIELD(HOST_CR3, host_cr3), | |
1052 | FIELD(HOST_CR4, host_cr4), | |
1053 | FIELD(HOST_FS_BASE, host_fs_base), | |
1054 | FIELD(HOST_GS_BASE, host_gs_base), | |
1055 | FIELD(HOST_TR_BASE, host_tr_base), | |
1056 | FIELD(HOST_GDTR_BASE, host_gdtr_base), | |
1057 | FIELD(HOST_IDTR_BASE, host_idtr_base), | |
1058 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), | |
1059 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), | |
1060 | FIELD(HOST_RSP, host_rsp), | |
1061 | FIELD(HOST_RIP, host_rip), | |
1062 | }; | |
22bd0358 NHE |
1063 | |
1064 | static inline short vmcs_field_to_offset(unsigned long field) | |
1065 | { | |
bcaf287c DW |
1066 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
1067 | unsigned short offset; | |
a2ae9df7 | 1068 | |
bcaf287c DW |
1069 | BUILD_BUG_ON(size > SHRT_MAX); |
1070 | if (field >= size) | |
75f139aa AH |
1071 | return -ENOENT; |
1072 | ||
bcaf287c DW |
1073 | field = array_index_nospec(field, size); |
1074 | offset = vmcs_field_to_offset_table[field]; | |
1075 | if (offset == 0) | |
a2ae9df7 | 1076 | return -ENOENT; |
bcaf287c | 1077 | return offset; |
22bd0358 NHE |
1078 | } |
1079 | ||
a9d30f33 NHE |
1080 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
1081 | { | |
4f2777bc | 1082 | return to_vmx(vcpu)->nested.cached_vmcs12; |
a9d30f33 NHE |
1083 | } |
1084 | ||
995f00a6 | 1085 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
bfd0a56b | 1086 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
995f00a6 | 1087 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
f53cd63c | 1088 | static bool vmx_xsaves_supported(void); |
b246dd5d OW |
1089 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
1090 | struct kvm_segment *var, int seg); | |
1091 | static void vmx_get_segment(struct kvm_vcpu *vcpu, | |
1092 | struct kvm_segment *var, int seg); | |
d99e4152 GN |
1093 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
1094 | static u32 vmx_segment_access_rights(struct kvm_segment *var); | |
16f5b903 | 1095 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
b96fb439 PB |
1096 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
1097 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); | |
1098 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, | |
1099 | u16 error_code); | |
4b0be90f | 1100 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
3861351c | 1101 | static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
33241bfe | 1102 | u32 msr, int type); |
75880a01 | 1103 | |
6aa8b732 AK |
1104 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
1105 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); | |
d462b819 NHE |
1106 | /* |
1107 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed | |
1108 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. | |
1109 | */ | |
1110 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); | |
6aa8b732 | 1111 | |
bf9f6ac8 FW |
1112 | /* |
1113 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we | |
1114 | * can find which vCPU should be waken up. | |
1115 | */ | |
1116 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); | |
1117 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); | |
1118 | ||
23611332 RK |
1119 | enum { |
1120 | VMX_IO_BITMAP_A, | |
1121 | VMX_IO_BITMAP_B, | |
23611332 RK |
1122 | VMX_VMREAD_BITMAP, |
1123 | VMX_VMWRITE_BITMAP, | |
1124 | VMX_BITMAP_NR | |
1125 | }; | |
1126 | ||
1127 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; | |
1128 | ||
1129 | #define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A]) | |
1130 | #define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B]) | |
23611332 RK |
1131 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
1132 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) | |
fdef3ad1 | 1133 | |
110312c8 | 1134 | static bool cpu_has_load_ia32_efer; |
8bf00a52 | 1135 | static bool cpu_has_load_perf_global_ctrl; |
110312c8 | 1136 | |
2384d2b3 SY |
1137 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
1138 | static DEFINE_SPINLOCK(vmx_vpid_lock); | |
1139 | ||
1c3d14fe | 1140 | static struct vmcs_config { |
6aa8b732 AK |
1141 | int size; |
1142 | int order; | |
9ac7e3e8 | 1143 | u32 basic_cap; |
6aa8b732 | 1144 | u32 revision_id; |
1c3d14fe YS |
1145 | u32 pin_based_exec_ctrl; |
1146 | u32 cpu_based_exec_ctrl; | |
f78e0e2e | 1147 | u32 cpu_based_2nd_exec_ctrl; |
1c3d14fe YS |
1148 | u32 vmexit_ctrl; |
1149 | u32 vmentry_ctrl; | |
1150 | } vmcs_config; | |
6aa8b732 | 1151 | |
efff9e53 | 1152 | static struct vmx_capability { |
d56f546d SY |
1153 | u32 ept; |
1154 | u32 vpid; | |
1155 | } vmx_capability; | |
1156 | ||
6aa8b732 AK |
1157 | #define VMX_SEGMENT_FIELD(seg) \ |
1158 | [VCPU_SREG_##seg] = { \ | |
1159 | .selector = GUEST_##seg##_SELECTOR, \ | |
1160 | .base = GUEST_##seg##_BASE, \ | |
1161 | .limit = GUEST_##seg##_LIMIT, \ | |
1162 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ | |
1163 | } | |
1164 | ||
772e0318 | 1165 | static const struct kvm_vmx_segment_field { |
6aa8b732 AK |
1166 | unsigned selector; |
1167 | unsigned base; | |
1168 | unsigned limit; | |
1169 | unsigned ar_bytes; | |
1170 | } kvm_vmx_segment_fields[] = { | |
1171 | VMX_SEGMENT_FIELD(CS), | |
1172 | VMX_SEGMENT_FIELD(DS), | |
1173 | VMX_SEGMENT_FIELD(ES), | |
1174 | VMX_SEGMENT_FIELD(FS), | |
1175 | VMX_SEGMENT_FIELD(GS), | |
1176 | VMX_SEGMENT_FIELD(SS), | |
1177 | VMX_SEGMENT_FIELD(TR), | |
1178 | VMX_SEGMENT_FIELD(LDTR), | |
1179 | }; | |
1180 | ||
26bb0981 AK |
1181 | static u64 host_efer; |
1182 | ||
6de4f3ad AK |
1183 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
1184 | ||
4d56c8a7 | 1185 | /* |
8c06585d | 1186 | * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it |
4d56c8a7 AK |
1187 | * away by decrementing the array size. |
1188 | */ | |
6aa8b732 | 1189 | static const u32 vmx_msr_index[] = { |
05b3e0c2 | 1190 | #ifdef CONFIG_X86_64 |
44ea2b17 | 1191 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
6aa8b732 | 1192 | #endif |
8c06585d | 1193 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
6aa8b732 | 1194 | }; |
6aa8b732 | 1195 | |
5bb16016 | 1196 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
6aa8b732 AK |
1197 | { |
1198 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1199 | INTR_INFO_VALID_MASK)) == | |
5bb16016 JK |
1200 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
1201 | } | |
1202 | ||
6f05485d JK |
1203 | static inline bool is_debug(u32 intr_info) |
1204 | { | |
1205 | return is_exception_n(intr_info, DB_VECTOR); | |
1206 | } | |
1207 | ||
1208 | static inline bool is_breakpoint(u32 intr_info) | |
1209 | { | |
1210 | return is_exception_n(intr_info, BP_VECTOR); | |
1211 | } | |
1212 | ||
5bb16016 JK |
1213 | static inline bool is_page_fault(u32 intr_info) |
1214 | { | |
1215 | return is_exception_n(intr_info, PF_VECTOR); | |
6aa8b732 AK |
1216 | } |
1217 | ||
31299944 | 1218 | static inline bool is_no_device(u32 intr_info) |
2ab455cc | 1219 | { |
5bb16016 | 1220 | return is_exception_n(intr_info, NM_VECTOR); |
2ab455cc AL |
1221 | } |
1222 | ||
31299944 | 1223 | static inline bool is_invalid_opcode(u32 intr_info) |
7aa81cc0 | 1224 | { |
5bb16016 | 1225 | return is_exception_n(intr_info, UD_VECTOR); |
7aa81cc0 AL |
1226 | } |
1227 | ||
31299944 | 1228 | static inline bool is_external_interrupt(u32 intr_info) |
6aa8b732 AK |
1229 | { |
1230 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
1231 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); | |
1232 | } | |
1233 | ||
31299944 | 1234 | static inline bool is_machine_check(u32 intr_info) |
a0861c02 AK |
1235 | { |
1236 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
1237 | INTR_INFO_VALID_MASK)) == | |
1238 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); | |
1239 | } | |
1240 | ||
3252850d LT |
1241 | /* Undocumented: icebp/int1 */ |
1242 | static inline bool is_icebp(u32 intr_info) | |
1243 | { | |
1244 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
1245 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); | |
1246 | } | |
1247 | ||
31299944 | 1248 | static inline bool cpu_has_vmx_msr_bitmap(void) |
25c5f225 | 1249 | { |
04547156 | 1250 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
25c5f225 SY |
1251 | } |
1252 | ||
31299944 | 1253 | static inline bool cpu_has_vmx_tpr_shadow(void) |
6e5d865c | 1254 | { |
04547156 | 1255 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
6e5d865c YS |
1256 | } |
1257 | ||
35754c98 | 1258 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
6e5d865c | 1259 | { |
35754c98 | 1260 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
6e5d865c YS |
1261 | } |
1262 | ||
31299944 | 1263 | static inline bool cpu_has_secondary_exec_ctrls(void) |
f78e0e2e | 1264 | { |
04547156 SY |
1265 | return vmcs_config.cpu_based_exec_ctrl & |
1266 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
f78e0e2e SY |
1267 | } |
1268 | ||
774ead3a | 1269 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
f78e0e2e | 1270 | { |
04547156 SY |
1271 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1272 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
1273 | } | |
1274 | ||
8d14695f YZ |
1275 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
1276 | { | |
1277 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1278 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
1279 | } | |
1280 | ||
83d4c286 YZ |
1281 | static inline bool cpu_has_vmx_apic_register_virt(void) |
1282 | { | |
1283 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1284 | SECONDARY_EXEC_APIC_REGISTER_VIRT; | |
1285 | } | |
1286 | ||
c7c9c56c YZ |
1287 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
1288 | { | |
1289 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1290 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; | |
1291 | } | |
1292 | ||
64672c95 YJ |
1293 | /* |
1294 | * Comment's format: document - errata name - stepping - processor name. | |
1295 | * Refer from | |
1296 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp | |
1297 | */ | |
1298 | static u32 vmx_preemption_cpu_tfms[] = { | |
1299 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ | |
1300 | 0x000206E6, | |
1301 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ | |
1302 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ | |
1303 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1304 | 0x00020652, | |
1305 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ | |
1306 | 0x00020655, | |
1307 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ | |
1308 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ | |
1309 | /* | |
1310 | * 320767.pdf - AAP86 - B1 - | |
1311 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile | |
1312 | */ | |
1313 | 0x000106E5, | |
1314 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ | |
1315 | 0x000106A0, | |
1316 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ | |
1317 | 0x000106A1, | |
1318 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ | |
1319 | 0x000106A4, | |
1320 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ | |
1321 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ | |
1322 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ | |
1323 | 0x000106A5, | |
1324 | }; | |
1325 | ||
1326 | static inline bool cpu_has_broken_vmx_preemption_timer(void) | |
1327 | { | |
1328 | u32 eax = cpuid_eax(0x00000001), i; | |
1329 | ||
1330 | /* Clear the reserved bits */ | |
1331 | eax &= ~(0x3U << 14 | 0xfU << 28); | |
03f6a22a | 1332 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
64672c95 YJ |
1333 | if (eax == vmx_preemption_cpu_tfms[i]) |
1334 | return true; | |
1335 | ||
1336 | return false; | |
1337 | } | |
1338 | ||
1339 | static inline bool cpu_has_vmx_preemption_timer(void) | |
1340 | { | |
64672c95 YJ |
1341 | return vmcs_config.pin_based_exec_ctrl & |
1342 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1343 | } | |
1344 | ||
01e439be YZ |
1345 | static inline bool cpu_has_vmx_posted_intr(void) |
1346 | { | |
d6a858d1 PB |
1347 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
1348 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; | |
01e439be YZ |
1349 | } |
1350 | ||
1351 | static inline bool cpu_has_vmx_apicv(void) | |
1352 | { | |
1353 | return cpu_has_vmx_apic_register_virt() && | |
1354 | cpu_has_vmx_virtual_intr_delivery() && | |
1355 | cpu_has_vmx_posted_intr(); | |
1356 | } | |
1357 | ||
04547156 SY |
1358 | static inline bool cpu_has_vmx_flexpriority(void) |
1359 | { | |
1360 | return cpu_has_vmx_tpr_shadow() && | |
1361 | cpu_has_vmx_virtualize_apic_accesses(); | |
f78e0e2e SY |
1362 | } |
1363 | ||
e799794e MT |
1364 | static inline bool cpu_has_vmx_ept_execute_only(void) |
1365 | { | |
31299944 | 1366 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
e799794e MT |
1367 | } |
1368 | ||
e799794e MT |
1369 | static inline bool cpu_has_vmx_ept_2m_page(void) |
1370 | { | |
31299944 | 1371 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
e799794e MT |
1372 | } |
1373 | ||
878403b7 SY |
1374 | static inline bool cpu_has_vmx_ept_1g_page(void) |
1375 | { | |
31299944 | 1376 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
878403b7 SY |
1377 | } |
1378 | ||
4bc9b982 SY |
1379 | static inline bool cpu_has_vmx_ept_4levels(void) |
1380 | { | |
1381 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; | |
1382 | } | |
1383 | ||
42aa53b4 DH |
1384 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
1385 | { | |
1386 | return vmx_capability.ept & VMX_EPTP_WB_BIT; | |
1387 | } | |
1388 | ||
855feb67 YZ |
1389 | static inline bool cpu_has_vmx_ept_5levels(void) |
1390 | { | |
1391 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; | |
1392 | } | |
1393 | ||
83c3a331 XH |
1394 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
1395 | { | |
1396 | return vmx_capability.ept & VMX_EPT_AD_BIT; | |
1397 | } | |
1398 | ||
31299944 | 1399 | static inline bool cpu_has_vmx_invept_context(void) |
d56f546d | 1400 | { |
31299944 | 1401 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
d56f546d SY |
1402 | } |
1403 | ||
31299944 | 1404 | static inline bool cpu_has_vmx_invept_global(void) |
d56f546d | 1405 | { |
31299944 | 1406 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
d56f546d SY |
1407 | } |
1408 | ||
518c8aee GJ |
1409 | static inline bool cpu_has_vmx_invvpid_single(void) |
1410 | { | |
1411 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; | |
1412 | } | |
1413 | ||
b9d762fa GJ |
1414 | static inline bool cpu_has_vmx_invvpid_global(void) |
1415 | { | |
1416 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; | |
1417 | } | |
1418 | ||
08d839c4 WL |
1419 | static inline bool cpu_has_vmx_invvpid(void) |
1420 | { | |
1421 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; | |
1422 | } | |
1423 | ||
31299944 | 1424 | static inline bool cpu_has_vmx_ept(void) |
d56f546d | 1425 | { |
04547156 SY |
1426 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1427 | SECONDARY_EXEC_ENABLE_EPT; | |
d56f546d SY |
1428 | } |
1429 | ||
31299944 | 1430 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
3a624e29 NK |
1431 | { |
1432 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1433 | SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
1434 | } | |
1435 | ||
31299944 | 1436 | static inline bool cpu_has_vmx_ple(void) |
4b8d54f9 ZE |
1437 | { |
1438 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1439 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
1440 | } | |
1441 | ||
9ac7e3e8 JD |
1442 | static inline bool cpu_has_vmx_basic_inout(void) |
1443 | { | |
1444 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); | |
1445 | } | |
1446 | ||
35754c98 | 1447 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
f78e0e2e | 1448 | { |
35754c98 | 1449 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
f78e0e2e SY |
1450 | } |
1451 | ||
31299944 | 1452 | static inline bool cpu_has_vmx_vpid(void) |
2384d2b3 | 1453 | { |
04547156 SY |
1454 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
1455 | SECONDARY_EXEC_ENABLE_VPID; | |
2384d2b3 SY |
1456 | } |
1457 | ||
31299944 | 1458 | static inline bool cpu_has_vmx_rdtscp(void) |
4e47c7a6 SY |
1459 | { |
1460 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1461 | SECONDARY_EXEC_RDTSCP; | |
1462 | } | |
1463 | ||
ad756a16 MJ |
1464 | static inline bool cpu_has_vmx_invpcid(void) |
1465 | { | |
1466 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1467 | SECONDARY_EXEC_ENABLE_INVPCID; | |
1468 | } | |
1469 | ||
8a1b4392 PB |
1470 | static inline bool cpu_has_virtual_nmis(void) |
1471 | { | |
1472 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; | |
1473 | } | |
1474 | ||
f5f48ee1 SY |
1475 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
1476 | { | |
1477 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1478 | SECONDARY_EXEC_WBINVD_EXITING; | |
1479 | } | |
1480 | ||
abc4fc58 AG |
1481 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
1482 | { | |
1483 | u64 vmx_msr; | |
1484 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
1485 | /* check if the cpu supports writing r/o exit information fields */ | |
1486 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) | |
1487 | return false; | |
1488 | ||
1489 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1490 | SECONDARY_EXEC_SHADOW_VMCS; | |
1491 | } | |
1492 | ||
843e4330 KH |
1493 | static inline bool cpu_has_vmx_pml(void) |
1494 | { | |
1495 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; | |
1496 | } | |
1497 | ||
64903d61 HZ |
1498 | static inline bool cpu_has_vmx_tsc_scaling(void) |
1499 | { | |
1500 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1501 | SECONDARY_EXEC_TSC_SCALING; | |
1502 | } | |
1503 | ||
2a499e49 BD |
1504 | static inline bool cpu_has_vmx_vmfunc(void) |
1505 | { | |
1506 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
1507 | SECONDARY_EXEC_ENABLE_VMFUNC; | |
1508 | } | |
1509 | ||
04547156 SY |
1510 | static inline bool report_flexpriority(void) |
1511 | { | |
1512 | return flexpriority_enabled; | |
1513 | } | |
1514 | ||
c7c2c709 JM |
1515 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
1516 | { | |
1517 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low); | |
1518 | } | |
1519 | ||
fe3ef05c NHE |
1520 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
1521 | { | |
1522 | return vmcs12->cpu_based_vm_exec_control & bit; | |
1523 | } | |
1524 | ||
1525 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) | |
1526 | { | |
1527 | return (vmcs12->cpu_based_vm_exec_control & | |
1528 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && | |
1529 | (vmcs12->secondary_vm_exec_control & bit); | |
1530 | } | |
1531 | ||
f4124500 JK |
1532 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
1533 | { | |
1534 | return vmcs12->pin_based_vm_exec_control & | |
1535 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
1536 | } | |
1537 | ||
155a97a3 NHE |
1538 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
1539 | { | |
1540 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); | |
1541 | } | |
1542 | ||
81dc01f7 WL |
1543 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
1544 | { | |
3db13480 | 1545 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
81dc01f7 WL |
1546 | } |
1547 | ||
c5f983f6 BD |
1548 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
1549 | { | |
1550 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); | |
1551 | } | |
1552 | ||
f2b93280 WV |
1553 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
1554 | { | |
1555 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); | |
1556 | } | |
1557 | ||
5c614b35 WL |
1558 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
1559 | { | |
1560 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); | |
1561 | } | |
1562 | ||
82f0dd4b WV |
1563 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
1564 | { | |
1565 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); | |
1566 | } | |
1567 | ||
608406e2 WV |
1568 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
1569 | { | |
1570 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
1571 | } | |
1572 | ||
705699a1 WV |
1573 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
1574 | { | |
1575 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; | |
1576 | } | |
1577 | ||
27c42a1b BD |
1578 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
1579 | { | |
1580 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); | |
1581 | } | |
1582 | ||
41ab9372 BD |
1583 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
1584 | { | |
1585 | return nested_cpu_has_vmfunc(vmcs12) && | |
1586 | (vmcs12->vm_function_control & | |
1587 | VMX_VMFUNC_EPTP_SWITCHING); | |
1588 | } | |
1589 | ||
ef85b673 | 1590 | static inline bool is_nmi(u32 intr_info) |
644d711a NHE |
1591 | { |
1592 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
ef85b673 | 1593 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
644d711a NHE |
1594 | } |
1595 | ||
533558bc JK |
1596 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
1597 | u32 exit_intr_info, | |
1598 | unsigned long exit_qualification); | |
7c177938 NHE |
1599 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
1600 | struct vmcs12 *vmcs12, | |
1601 | u32 reason, unsigned long qualification); | |
1602 | ||
8b9cf98c | 1603 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
7725f0ba AK |
1604 | { |
1605 | int i; | |
1606 | ||
a2fa3e9f | 1607 | for (i = 0; i < vmx->nmsrs; ++i) |
26bb0981 | 1608 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
a75beee6 ED |
1609 | return i; |
1610 | return -1; | |
1611 | } | |
1612 | ||
b9d2fecb | 1613 | static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva) |
2384d2b3 SY |
1614 | { |
1615 | struct { | |
1616 | u64 vpid : 16; | |
1617 | u64 rsvd : 48; | |
1618 | u64 gva; | |
1619 | } operand = { vpid, 0, gva }; | |
1620 | ||
4ecac3fd | 1621 | asm volatile (__ex(ASM_VMX_INVVPID) |
2384d2b3 SY |
1622 | /* CF==1 or ZF==1 --> rc = -1 */ |
1623 | "; ja 1f ; ud2 ; 1:" | |
1624 | : : "a"(&operand), "c"(ext) : "cc", "memory"); | |
1625 | } | |
1626 | ||
b9d2fecb | 1627 | static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa) |
1439442c SY |
1628 | { |
1629 | struct { | |
1630 | u64 eptp, gpa; | |
1631 | } operand = {eptp, gpa}; | |
1632 | ||
4ecac3fd | 1633 | asm volatile (__ex(ASM_VMX_INVEPT) |
1439442c SY |
1634 | /* CF==1 or ZF==1 --> rc = -1 */ |
1635 | "; ja 1f ; ud2 ; 1:\n" | |
1636 | : : "a" (&operand), "c" (ext) : "cc", "memory"); | |
1637 | } | |
1638 | ||
26bb0981 | 1639 | static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
a75beee6 ED |
1640 | { |
1641 | int i; | |
1642 | ||
8b9cf98c | 1643 | i = __find_msr_index(vmx, msr); |
a75beee6 | 1644 | if (i >= 0) |
a2fa3e9f | 1645 | return &vmx->guest_msrs[i]; |
8b6d44c7 | 1646 | return NULL; |
7725f0ba AK |
1647 | } |
1648 | ||
6aa8b732 AK |
1649 | static void vmcs_clear(struct vmcs *vmcs) |
1650 | { | |
1651 | u64 phys_addr = __pa(vmcs); | |
1652 | u8 error; | |
1653 | ||
4ecac3fd | 1654 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
16d8f72f | 1655 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
6aa8b732 AK |
1656 | : "cc", "memory"); |
1657 | if (error) | |
1658 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", | |
1659 | vmcs, phys_addr); | |
1660 | } | |
1661 | ||
d462b819 NHE |
1662 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
1663 | { | |
1664 | vmcs_clear(loaded_vmcs->vmcs); | |
355f4fb1 JM |
1665 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
1666 | vmcs_clear(loaded_vmcs->shadow_vmcs); | |
d462b819 NHE |
1667 | loaded_vmcs->cpu = -1; |
1668 | loaded_vmcs->launched = 0; | |
1669 | } | |
1670 | ||
7725b894 DX |
1671 | static void vmcs_load(struct vmcs *vmcs) |
1672 | { | |
1673 | u64 phys_addr = __pa(vmcs); | |
1674 | u8 error; | |
1675 | ||
1676 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" | |
16d8f72f | 1677 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
7725b894 DX |
1678 | : "cc", "memory"); |
1679 | if (error) | |
2844d849 | 1680 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
7725b894 DX |
1681 | vmcs, phys_addr); |
1682 | } | |
1683 | ||
2965faa5 | 1684 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
1685 | /* |
1686 | * This bitmap is used to indicate whether the vmclear | |
1687 | * operation is enabled on all cpus. All disabled by | |
1688 | * default. | |
1689 | */ | |
1690 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; | |
1691 | ||
1692 | static inline void crash_enable_local_vmclear(int cpu) | |
1693 | { | |
1694 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1695 | } | |
1696 | ||
1697 | static inline void crash_disable_local_vmclear(int cpu) | |
1698 | { | |
1699 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1700 | } | |
1701 | ||
1702 | static inline int crash_local_vmclear_enabled(int cpu) | |
1703 | { | |
1704 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); | |
1705 | } | |
1706 | ||
1707 | static void crash_vmclear_local_loaded_vmcss(void) | |
1708 | { | |
1709 | int cpu = raw_smp_processor_id(); | |
1710 | struct loaded_vmcs *v; | |
1711 | ||
1712 | if (!crash_local_vmclear_enabled(cpu)) | |
1713 | return; | |
1714 | ||
1715 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), | |
1716 | loaded_vmcss_on_cpu_link) | |
1717 | vmcs_clear(v->vmcs); | |
1718 | } | |
1719 | #else | |
1720 | static inline void crash_enable_local_vmclear(int cpu) { } | |
1721 | static inline void crash_disable_local_vmclear(int cpu) { } | |
2965faa5 | 1722 | #endif /* CONFIG_KEXEC_CORE */ |
8f536b76 | 1723 | |
d462b819 | 1724 | static void __loaded_vmcs_clear(void *arg) |
6aa8b732 | 1725 | { |
d462b819 | 1726 | struct loaded_vmcs *loaded_vmcs = arg; |
d3b2c338 | 1727 | int cpu = raw_smp_processor_id(); |
6aa8b732 | 1728 | |
d462b819 NHE |
1729 | if (loaded_vmcs->cpu != cpu) |
1730 | return; /* vcpu migration can race with cpu offline */ | |
1731 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) | |
6aa8b732 | 1732 | per_cpu(current_vmcs, cpu) = NULL; |
8f536b76 | 1733 | crash_disable_local_vmclear(cpu); |
d462b819 | 1734 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
5a560f8b XG |
1735 | |
1736 | /* | |
1737 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link | |
1738 | * is before setting loaded_vmcs->vcpu to -1 which is done in | |
1739 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist | |
1740 | * then adds the vmcs into percpu list before it is deleted. | |
1741 | */ | |
1742 | smp_wmb(); | |
1743 | ||
d462b819 | 1744 | loaded_vmcs_init(loaded_vmcs); |
8f536b76 | 1745 | crash_enable_local_vmclear(cpu); |
6aa8b732 AK |
1746 | } |
1747 | ||
d462b819 | 1748 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
8d0be2b3 | 1749 | { |
e6c7d321 XG |
1750 | int cpu = loaded_vmcs->cpu; |
1751 | ||
1752 | if (cpu != -1) | |
1753 | smp_call_function_single(cpu, | |
1754 | __loaded_vmcs_clear, loaded_vmcs, 1); | |
8d0be2b3 AK |
1755 | } |
1756 | ||
dd5f5341 | 1757 | static inline void vpid_sync_vcpu_single(int vpid) |
2384d2b3 | 1758 | { |
dd5f5341 | 1759 | if (vpid == 0) |
2384d2b3 SY |
1760 | return; |
1761 | ||
518c8aee | 1762 | if (cpu_has_vmx_invvpid_single()) |
dd5f5341 | 1763 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
2384d2b3 SY |
1764 | } |
1765 | ||
b9d762fa GJ |
1766 | static inline void vpid_sync_vcpu_global(void) |
1767 | { | |
1768 | if (cpu_has_vmx_invvpid_global()) | |
1769 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); | |
1770 | } | |
1771 | ||
dd5f5341 | 1772 | static inline void vpid_sync_context(int vpid) |
b9d762fa GJ |
1773 | { |
1774 | if (cpu_has_vmx_invvpid_single()) | |
dd5f5341 | 1775 | vpid_sync_vcpu_single(vpid); |
b9d762fa GJ |
1776 | else |
1777 | vpid_sync_vcpu_global(); | |
1778 | } | |
1779 | ||
1439442c SY |
1780 | static inline void ept_sync_global(void) |
1781 | { | |
f5f51586 | 1782 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
1439442c SY |
1783 | } |
1784 | ||
1785 | static inline void ept_sync_context(u64 eptp) | |
1786 | { | |
0e1252dc DH |
1787 | if (cpu_has_vmx_invept_context()) |
1788 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); | |
1789 | else | |
1790 | ept_sync_global(); | |
1439442c SY |
1791 | } |
1792 | ||
8a86aea9 PB |
1793 | static __always_inline void vmcs_check16(unsigned long field) |
1794 | { | |
1795 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1796 | "16-bit accessor invalid for 64-bit field"); | |
1797 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1798 | "16-bit accessor invalid for 64-bit high field"); | |
1799 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1800 | "16-bit accessor invalid for 32-bit high field"); | |
1801 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1802 | "16-bit accessor invalid for natural width field"); | |
1803 | } | |
1804 | ||
1805 | static __always_inline void vmcs_check32(unsigned long field) | |
1806 | { | |
1807 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1808 | "32-bit accessor invalid for 16-bit field"); | |
1809 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1810 | "32-bit accessor invalid for natural width field"); | |
1811 | } | |
1812 | ||
1813 | static __always_inline void vmcs_check64(unsigned long field) | |
1814 | { | |
1815 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1816 | "64-bit accessor invalid for 16-bit field"); | |
1817 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1818 | "64-bit accessor invalid for 64-bit high field"); | |
1819 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1820 | "64-bit accessor invalid for 32-bit field"); | |
1821 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, | |
1822 | "64-bit accessor invalid for natural width field"); | |
1823 | } | |
1824 | ||
1825 | static __always_inline void vmcs_checkl(unsigned long field) | |
1826 | { | |
1827 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, | |
1828 | "Natural width accessor invalid for 16-bit field"); | |
1829 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, | |
1830 | "Natural width accessor invalid for 64-bit field"); | |
1831 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, | |
1832 | "Natural width accessor invalid for 64-bit high field"); | |
1833 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, | |
1834 | "Natural width accessor invalid for 32-bit field"); | |
1835 | } | |
1836 | ||
1837 | static __always_inline unsigned long __vmcs_readl(unsigned long field) | |
6aa8b732 | 1838 | { |
5e520e62 | 1839 | unsigned long value; |
6aa8b732 | 1840 | |
5e520e62 AK |
1841 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
1842 | : "=a"(value) : "d"(field) : "cc"); | |
6aa8b732 AK |
1843 | return value; |
1844 | } | |
1845 | ||
96304217 | 1846 | static __always_inline u16 vmcs_read16(unsigned long field) |
6aa8b732 | 1847 | { |
8a86aea9 PB |
1848 | vmcs_check16(field); |
1849 | return __vmcs_readl(field); | |
6aa8b732 AK |
1850 | } |
1851 | ||
96304217 | 1852 | static __always_inline u32 vmcs_read32(unsigned long field) |
6aa8b732 | 1853 | { |
8a86aea9 PB |
1854 | vmcs_check32(field); |
1855 | return __vmcs_readl(field); | |
6aa8b732 AK |
1856 | } |
1857 | ||
96304217 | 1858 | static __always_inline u64 vmcs_read64(unsigned long field) |
6aa8b732 | 1859 | { |
8a86aea9 | 1860 | vmcs_check64(field); |
05b3e0c2 | 1861 | #ifdef CONFIG_X86_64 |
8a86aea9 | 1862 | return __vmcs_readl(field); |
6aa8b732 | 1863 | #else |
8a86aea9 | 1864 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
6aa8b732 AK |
1865 | #endif |
1866 | } | |
1867 | ||
8a86aea9 PB |
1868 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
1869 | { | |
1870 | vmcs_checkl(field); | |
1871 | return __vmcs_readl(field); | |
1872 | } | |
1873 | ||
e52de1b8 AK |
1874 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
1875 | { | |
1876 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", | |
1877 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); | |
1878 | dump_stack(); | |
1879 | } | |
1880 | ||
8a86aea9 | 1881 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
6aa8b732 AK |
1882 | { |
1883 | u8 error; | |
1884 | ||
4ecac3fd | 1885 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
d77c26fc | 1886 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
e52de1b8 AK |
1887 | if (unlikely(error)) |
1888 | vmwrite_error(field, value); | |
6aa8b732 AK |
1889 | } |
1890 | ||
8a86aea9 | 1891 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
6aa8b732 | 1892 | { |
8a86aea9 PB |
1893 | vmcs_check16(field); |
1894 | __vmcs_writel(field, value); | |
6aa8b732 AK |
1895 | } |
1896 | ||
8a86aea9 | 1897 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
6aa8b732 | 1898 | { |
8a86aea9 PB |
1899 | vmcs_check32(field); |
1900 | __vmcs_writel(field, value); | |
6aa8b732 AK |
1901 | } |
1902 | ||
8a86aea9 | 1903 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
6aa8b732 | 1904 | { |
8a86aea9 PB |
1905 | vmcs_check64(field); |
1906 | __vmcs_writel(field, value); | |
7682f2d0 | 1907 | #ifndef CONFIG_X86_64 |
6aa8b732 | 1908 | asm volatile (""); |
8a86aea9 | 1909 | __vmcs_writel(field+1, value >> 32); |
6aa8b732 AK |
1910 | #endif |
1911 | } | |
1912 | ||
8a86aea9 | 1913 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
2ab455cc | 1914 | { |
8a86aea9 PB |
1915 | vmcs_checkl(field); |
1916 | __vmcs_writel(field, value); | |
2ab455cc AL |
1917 | } |
1918 | ||
8a86aea9 | 1919 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
2ab455cc | 1920 | { |
8a86aea9 PB |
1921 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1922 | "vmcs_clear_bits does not support 64-bit fields"); | |
1923 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); | |
2ab455cc AL |
1924 | } |
1925 | ||
8a86aea9 | 1926 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
2ab455cc | 1927 | { |
8a86aea9 PB |
1928 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
1929 | "vmcs_set_bits does not support 64-bit fields"); | |
1930 | __vmcs_writel(field, __vmcs_readl(field) | mask); | |
2ab455cc AL |
1931 | } |
1932 | ||
8391ce44 PB |
1933 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
1934 | { | |
1935 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); | |
1936 | } | |
1937 | ||
2961e876 GN |
1938 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
1939 | { | |
1940 | vmcs_write32(VM_ENTRY_CONTROLS, val); | |
1941 | vmx->vm_entry_controls_shadow = val; | |
1942 | } | |
1943 | ||
1944 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) | |
1945 | { | |
1946 | if (vmx->vm_entry_controls_shadow != val) | |
1947 | vm_entry_controls_init(vmx, val); | |
1948 | } | |
1949 | ||
1950 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) | |
1951 | { | |
1952 | return vmx->vm_entry_controls_shadow; | |
1953 | } | |
1954 | ||
1955 | ||
1956 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
1957 | { | |
1958 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); | |
1959 | } | |
1960 | ||
1961 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
1962 | { | |
1963 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); | |
1964 | } | |
1965 | ||
8391ce44 PB |
1966 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
1967 | { | |
1968 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); | |
1969 | } | |
1970 | ||
2961e876 GN |
1971 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
1972 | { | |
1973 | vmcs_write32(VM_EXIT_CONTROLS, val); | |
1974 | vmx->vm_exit_controls_shadow = val; | |
1975 | } | |
1976 | ||
1977 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) | |
1978 | { | |
1979 | if (vmx->vm_exit_controls_shadow != val) | |
1980 | vm_exit_controls_init(vmx, val); | |
1981 | } | |
1982 | ||
1983 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) | |
1984 | { | |
1985 | return vmx->vm_exit_controls_shadow; | |
1986 | } | |
1987 | ||
1988 | ||
1989 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) | |
1990 | { | |
1991 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); | |
1992 | } | |
1993 | ||
1994 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) | |
1995 | { | |
1996 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); | |
1997 | } | |
1998 | ||
2fb92db1 AK |
1999 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
2000 | { | |
2001 | vmx->segment_cache.bitmask = 0; | |
2002 | } | |
2003 | ||
2004 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, | |
2005 | unsigned field) | |
2006 | { | |
2007 | bool ret; | |
2008 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); | |
2009 | ||
2010 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { | |
2011 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); | |
2012 | vmx->segment_cache.bitmask = 0; | |
2013 | } | |
2014 | ret = vmx->segment_cache.bitmask & mask; | |
2015 | vmx->segment_cache.bitmask |= mask; | |
2016 | return ret; | |
2017 | } | |
2018 | ||
2019 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) | |
2020 | { | |
2021 | u16 *p = &vmx->segment_cache.seg[seg].selector; | |
2022 | ||
2023 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) | |
2024 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); | |
2025 | return *p; | |
2026 | } | |
2027 | ||
2028 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) | |
2029 | { | |
2030 | ulong *p = &vmx->segment_cache.seg[seg].base; | |
2031 | ||
2032 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) | |
2033 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); | |
2034 | return *p; | |
2035 | } | |
2036 | ||
2037 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) | |
2038 | { | |
2039 | u32 *p = &vmx->segment_cache.seg[seg].limit; | |
2040 | ||
2041 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) | |
2042 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); | |
2043 | return *p; | |
2044 | } | |
2045 | ||
2046 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) | |
2047 | { | |
2048 | u32 *p = &vmx->segment_cache.seg[seg].ar; | |
2049 | ||
2050 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) | |
2051 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); | |
2052 | return *p; | |
2053 | } | |
2054 | ||
abd3f2d6 AK |
2055 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
2056 | { | |
2057 | u32 eb; | |
2058 | ||
bd89525a | 2059 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
bd7e5b08 | 2060 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
fd7373cc JK |
2061 | if ((vcpu->guest_debug & |
2062 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == | |
2063 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) | |
2064 | eb |= 1u << BP_VECTOR; | |
7ffd92c5 | 2065 | if (to_vmx(vcpu)->rmode.vm86_active) |
abd3f2d6 | 2066 | eb = ~0; |
089d034e | 2067 | if (enable_ept) |
1439442c | 2068 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
36cf24e0 NHE |
2069 | |
2070 | /* When we are running a nested L2 guest and L1 specified for it a | |
2071 | * certain exception bitmap, we must trap the same exceptions and pass | |
2072 | * them to L1. When running L2, we will only handle the exceptions | |
2073 | * specified above if L1 did not want them. | |
2074 | */ | |
2075 | if (is_guest_mode(vcpu)) | |
2076 | eb |= get_vmcs12(vcpu)->exception_bitmap; | |
2077 | ||
abd3f2d6 AK |
2078 | vmcs_write32(EXCEPTION_BITMAP, eb); |
2079 | } | |
2080 | ||
74469996 KA |
2081 | /* |
2082 | * Check if MSR is intercepted for currently loaded MSR bitmap. | |
2083 | */ | |
2084 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) | |
2085 | { | |
2086 | unsigned long *msr_bitmap; | |
2087 | int f = sizeof(unsigned long); | |
2088 | ||
2089 | if (!cpu_has_vmx_msr_bitmap()) | |
2090 | return true; | |
2091 | ||
2092 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; | |
2093 | ||
2094 | if (msr <= 0x1fff) { | |
2095 | return !!test_bit(msr, msr_bitmap + 0x800 / f); | |
2096 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
2097 | msr &= 0x1fff; | |
2098 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); | |
2099 | } | |
2100 | ||
2101 | return true; | |
2102 | } | |
2103 | ||
33241bfe AR |
2104 | /* |
2105 | * Check if MSR is intercepted for L01 MSR bitmap. | |
2106 | */ | |
2107 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) | |
2108 | { | |
2109 | unsigned long *msr_bitmap; | |
2110 | int f = sizeof(unsigned long); | |
2111 | ||
2112 | if (!cpu_has_vmx_msr_bitmap()) | |
2113 | return true; | |
2114 | ||
2115 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; | |
2116 | ||
2117 | if (msr <= 0x1fff) { | |
2118 | return !!test_bit(msr, msr_bitmap + 0x800 / f); | |
2119 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
2120 | msr &= 0x1fff; | |
2121 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); | |
2122 | } | |
2123 | ||
2124 | return true; | |
2125 | } | |
2126 | ||
2961e876 GN |
2127 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
2128 | unsigned long entry, unsigned long exit) | |
8bf00a52 | 2129 | { |
2961e876 GN |
2130 | vm_entry_controls_clearbit(vmx, entry); |
2131 | vm_exit_controls_clearbit(vmx, exit); | |
8bf00a52 GN |
2132 | } |
2133 | ||
d8066b74 KRW |
2134 | static int find_msr(struct vmx_msrs *m, unsigned int msr) |
2135 | { | |
2136 | unsigned int i; | |
2137 | ||
2138 | for (i = 0; i < m->nr; ++i) { | |
2139 | if (m->val[i].index == msr) | |
2140 | return i; | |
2141 | } | |
2142 | return -ENOENT; | |
2143 | } | |
2144 | ||
61d2ef2c AK |
2145 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
2146 | { | |
d8066b74 | 2147 | int i; |
61d2ef2c AK |
2148 | struct msr_autoload *m = &vmx->msr_autoload; |
2149 | ||
8bf00a52 GN |
2150 | switch (msr) { |
2151 | case MSR_EFER: | |
2152 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
2153 | clear_atomic_switch_msr_special(vmx, |
2154 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
2155 | VM_EXIT_LOAD_IA32_EFER); |
2156 | return; | |
2157 | } | |
2158 | break; | |
2159 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
2160 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 2161 | clear_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
2162 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
2163 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
2164 | return; | |
2165 | } | |
2166 | break; | |
110312c8 | 2167 | } |
d8066b74 KRW |
2168 | i = find_msr(&m->guest, msr); |
2169 | if (i < 0) | |
0666648b | 2170 | goto skip_guest; |
6e3dedb6 | 2171 | --m->guest.nr; |
6e3dedb6 | 2172 | m->guest.val[i] = m->guest.val[m->guest.nr]; |
6e3dedb6 | 2173 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
0666648b KRW |
2174 | |
2175 | skip_guest: | |
2176 | i = find_msr(&m->host, msr); | |
2177 | if (i < 0) | |
2178 | return; | |
2179 | ||
2180 | --m->host.nr; | |
2181 | m->host.val[i] = m->host.val[m->host.nr]; | |
6e3dedb6 | 2182 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
61d2ef2c AK |
2183 | } |
2184 | ||
2961e876 GN |
2185 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
2186 | unsigned long entry, unsigned long exit, | |
2187 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, | |
2188 | u64 guest_val, u64 host_val) | |
8bf00a52 GN |
2189 | { |
2190 | vmcs_write64(guest_val_vmcs, guest_val); | |
2191 | vmcs_write64(host_val_vmcs, host_val); | |
2961e876 GN |
2192 | vm_entry_controls_setbit(vmx, entry); |
2193 | vm_exit_controls_setbit(vmx, exit); | |
8bf00a52 GN |
2194 | } |
2195 | ||
61d2ef2c | 2196 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
129ce7ac | 2197 | u64 guest_val, u64 host_val, bool entry_only) |
61d2ef2c | 2198 | { |
129ce7ac | 2199 | int i, j = 0; |
61d2ef2c AK |
2200 | struct msr_autoload *m = &vmx->msr_autoload; |
2201 | ||
8bf00a52 GN |
2202 | switch (msr) { |
2203 | case MSR_EFER: | |
2204 | if (cpu_has_load_ia32_efer) { | |
2961e876 GN |
2205 | add_atomic_switch_msr_special(vmx, |
2206 | VM_ENTRY_LOAD_IA32_EFER, | |
8bf00a52 GN |
2207 | VM_EXIT_LOAD_IA32_EFER, |
2208 | GUEST_IA32_EFER, | |
2209 | HOST_IA32_EFER, | |
2210 | guest_val, host_val); | |
2211 | return; | |
2212 | } | |
2213 | break; | |
2214 | case MSR_CORE_PERF_GLOBAL_CTRL: | |
2215 | if (cpu_has_load_perf_global_ctrl) { | |
2961e876 | 2216 | add_atomic_switch_msr_special(vmx, |
8bf00a52 GN |
2217 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
2218 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, | |
2219 | GUEST_IA32_PERF_GLOBAL_CTRL, | |
2220 | HOST_IA32_PERF_GLOBAL_CTRL, | |
2221 | guest_val, host_val); | |
2222 | return; | |
2223 | } | |
2224 | break; | |
7099e2e1 RK |
2225 | case MSR_IA32_PEBS_ENABLE: |
2226 | /* PEBS needs a quiescent period after being disabled (to write | |
2227 | * a record). Disabling PEBS through VMX MSR swapping doesn't | |
2228 | * provide that period, so a CPU could write host's record into | |
2229 | * guest's memory. | |
2230 | */ | |
2231 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); | |
110312c8 AK |
2232 | } |
2233 | ||
d8066b74 | 2234 | i = find_msr(&m->guest, msr); |
129ce7ac KRW |
2235 | if (!entry_only) |
2236 | j = find_msr(&m->host, msr); | |
2237 | ||
2034b9ef XL |
2238 | if ((i < 0 && m->guest.nr == NR_AUTOLOAD_MSRS) || |
2239 | (j < 0 && m->host.nr == NR_AUTOLOAD_MSRS)) { | |
60266204 | 2240 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
e7fc6f93 GN |
2241 | "Can't add msr %x\n", msr); |
2242 | return; | |
0666648b KRW |
2243 | } |
2244 | if (i < 0) { | |
d8066b74 | 2245 | i = m->guest.nr++; |
6e3dedb6 | 2246 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
0666648b | 2247 | } |
129ce7ac KRW |
2248 | m->guest.val[i].index = msr; |
2249 | m->guest.val[i].value = guest_val; | |
2250 | ||
2251 | if (entry_only) | |
2252 | return; | |
2253 | ||
0666648b KRW |
2254 | if (j < 0) { |
2255 | j = m->host.nr++; | |
6e3dedb6 | 2256 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
61d2ef2c | 2257 | } |
0666648b KRW |
2258 | m->host.val[j].index = msr; |
2259 | m->host.val[j].value = host_val; | |
61d2ef2c AK |
2260 | } |
2261 | ||
92c0d900 | 2262 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
2cc51560 | 2263 | { |
844a5fe2 PB |
2264 | u64 guest_efer = vmx->vcpu.arch.efer; |
2265 | u64 ignore_bits = 0; | |
2266 | ||
6bea1213 PB |
2267 | /* Shadow paging assumes NX to be available. */ |
2268 | if (!enable_ept) | |
2269 | guest_efer |= EFER_NX; | |
3a34a881 | 2270 | |
51c6cf66 | 2271 | /* |
844a5fe2 | 2272 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
51c6cf66 | 2273 | */ |
844a5fe2 | 2274 | ignore_bits |= EFER_SCE; |
51c6cf66 AK |
2275 | #ifdef CONFIG_X86_64 |
2276 | ignore_bits |= EFER_LMA | EFER_LME; | |
2277 | /* SCE is meaningful only in long mode on Intel */ | |
2278 | if (guest_efer & EFER_LMA) | |
2279 | ignore_bits &= ~(u64)EFER_SCE; | |
2280 | #endif | |
84ad33ef AK |
2281 | |
2282 | clear_atomic_switch_msr(vmx, MSR_EFER); | |
f6577a5f AL |
2283 | |
2284 | /* | |
2285 | * On EPT, we can't emulate NX, so we must switch EFER atomically. | |
2286 | * On CPUs that support "load IA32_EFER", always switch EFER | |
2287 | * atomically, since it's faster than switching it manually. | |
2288 | */ | |
2289 | if (cpu_has_load_ia32_efer || | |
2290 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { | |
84ad33ef AK |
2291 | if (!(guest_efer & EFER_LMA)) |
2292 | guest_efer &= ~EFER_LME; | |
54b98bff AL |
2293 | if (guest_efer != host_efer) |
2294 | add_atomic_switch_msr(vmx, MSR_EFER, | |
129ce7ac | 2295 | guest_efer, host_efer, false); |
84ad33ef | 2296 | return false; |
844a5fe2 PB |
2297 | } else { |
2298 | guest_efer &= ~ignore_bits; | |
2299 | guest_efer |= host_efer & ignore_bits; | |
2300 | ||
2301 | vmx->guest_msrs[efer_offset].data = guest_efer; | |
2302 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; | |
84ad33ef | 2303 | |
844a5fe2 PB |
2304 | return true; |
2305 | } | |
51c6cf66 AK |
2306 | } |
2307 | ||
e28baead AL |
2308 | #ifdef CONFIG_X86_32 |
2309 | /* | |
2310 | * On 32-bit kernels, VM exits still load the FS and GS bases from the | |
2311 | * VMCS rather than the segment table. KVM uses this helper to figure | |
2312 | * out the current bases to poke them into the VMCS before entry. | |
2313 | */ | |
2d49ec72 GN |
2314 | static unsigned long segment_base(u16 selector) |
2315 | { | |
8c2e41f7 | 2316 | struct desc_struct *table; |
2d49ec72 GN |
2317 | unsigned long v; |
2318 | ||
8c2e41f7 | 2319 | if (!(selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2320 | return 0; |
2321 | ||
45fc8757 | 2322 | table = get_current_gdt_ro(); |
2d49ec72 | 2323 | |
8c2e41f7 | 2324 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
2d49ec72 GN |
2325 | u16 ldt_selector = kvm_read_ldt(); |
2326 | ||
8c2e41f7 | 2327 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
2d49ec72 GN |
2328 | return 0; |
2329 | ||
8c2e41f7 | 2330 | table = (struct desc_struct *)segment_base(ldt_selector); |
2d49ec72 | 2331 | } |
8c2e41f7 | 2332 | v = get_desc_base(&table[selector >> 3]); |
2d49ec72 GN |
2333 | return v; |
2334 | } | |
e28baead | 2335 | #endif |
2d49ec72 | 2336 | |
04d2cc77 | 2337 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
33ed6329 | 2338 | { |
04d2cc77 | 2339 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 2340 | int i; |
04d2cc77 | 2341 | |
8a12167a | 2342 | if (vmx->loaded_cpu_state) |
33ed6329 AK |
2343 | return; |
2344 | ||
8a12167a SC |
2345 | vmx->loaded_cpu_state = vmx->loaded_vmcs; |
2346 | ||
33ed6329 AK |
2347 | /* |
2348 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not | |
2349 | * allow segment selectors with cpl > 0 or ti == 1. | |
2350 | */ | |
d6e88aec | 2351 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
152d3f2f | 2352 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
9581d442 | 2353 | savesegment(fs, vmx->host_state.fs_sel); |
152d3f2f | 2354 | if (!(vmx->host_state.fs_sel & 7)) { |
a2fa3e9f | 2355 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
152d3f2f LV |
2356 | vmx->host_state.fs_reload_needed = 0; |
2357 | } else { | |
33ed6329 | 2358 | vmcs_write16(HOST_FS_SELECTOR, 0); |
152d3f2f | 2359 | vmx->host_state.fs_reload_needed = 1; |
33ed6329 | 2360 | } |
9581d442 | 2361 | savesegment(gs, vmx->host_state.gs_sel); |
a2fa3e9f GH |
2362 | if (!(vmx->host_state.gs_sel & 7)) |
2363 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); | |
33ed6329 AK |
2364 | else { |
2365 | vmcs_write16(HOST_GS_SELECTOR, 0); | |
152d3f2f | 2366 | vmx->host_state.gs_ldt_reload_needed = 1; |
33ed6329 AK |
2367 | } |
2368 | ||
b2da15ac AK |
2369 | #ifdef CONFIG_X86_64 |
2370 | savesegment(ds, vmx->host_state.ds_sel); | |
2371 | savesegment(es, vmx->host_state.es_sel); | |
2372 | #endif | |
2373 | ||
33ed6329 AK |
2374 | #ifdef CONFIG_X86_64 |
2375 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); | |
2376 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); | |
2377 | #else | |
a2fa3e9f GH |
2378 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
2379 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); | |
33ed6329 | 2380 | #endif |
707c0874 AK |
2381 | |
2382 | #ifdef CONFIG_X86_64 | |
c8770e7b AK |
2383 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
2384 | if (is_long_mode(&vmx->vcpu)) | |
44ea2b17 | 2385 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
707c0874 | 2386 | #endif |
da8999d3 LJ |
2387 | if (boot_cpu_has(X86_FEATURE_MPX)) |
2388 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
26bb0981 AK |
2389 | for (i = 0; i < vmx->save_nmsrs; ++i) |
2390 | kvm_set_shared_msr(vmx->guest_msrs[i].index, | |
d5696725 AK |
2391 | vmx->guest_msrs[i].data, |
2392 | vmx->guest_msrs[i].mask); | |
33ed6329 AK |
2393 | } |
2394 | ||
a9b21b62 | 2395 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
33ed6329 | 2396 | { |
8a12167a | 2397 | if (!vmx->loaded_cpu_state) |
33ed6329 AK |
2398 | return; |
2399 | ||
8a12167a SC |
2400 | WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs); |
2401 | ||
e1beb1d3 | 2402 | ++vmx->vcpu.stat.host_state_reload; |
8a12167a SC |
2403 | vmx->loaded_cpu_state = NULL; |
2404 | ||
c8770e7b AK |
2405 | #ifdef CONFIG_X86_64 |
2406 | if (is_long_mode(&vmx->vcpu)) | |
2407 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); | |
2408 | #endif | |
152d3f2f | 2409 | if (vmx->host_state.gs_ldt_reload_needed) { |
d6e88aec | 2410 | kvm_load_ldt(vmx->host_state.ldt_sel); |
33ed6329 | 2411 | #ifdef CONFIG_X86_64 |
9581d442 | 2412 | load_gs_index(vmx->host_state.gs_sel); |
9581d442 AK |
2413 | #else |
2414 | loadsegment(gs, vmx->host_state.gs_sel); | |
33ed6329 | 2415 | #endif |
33ed6329 | 2416 | } |
0a77fe4c AK |
2417 | if (vmx->host_state.fs_reload_needed) |
2418 | loadsegment(fs, vmx->host_state.fs_sel); | |
b2da15ac AK |
2419 | #ifdef CONFIG_X86_64 |
2420 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { | |
2421 | loadsegment(ds, vmx->host_state.ds_sel); | |
2422 | loadsegment(es, vmx->host_state.es_sel); | |
2423 | } | |
b2da15ac | 2424 | #endif |
b7ffc44d | 2425 | invalidate_tss_limit(); |
44ea2b17 | 2426 | #ifdef CONFIG_X86_64 |
c8770e7b | 2427 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
44ea2b17 | 2428 | #endif |
da8999d3 LJ |
2429 | if (vmx->host_state.msr_host_bndcfgs) |
2430 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); | |
45fc8757 | 2431 | load_fixmap_gdt(raw_smp_processor_id()); |
33ed6329 AK |
2432 | } |
2433 | ||
a9b21b62 AK |
2434 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
2435 | { | |
2436 | preempt_disable(); | |
2437 | __vmx_load_host_state(vmx); | |
2438 | preempt_enable(); | |
2439 | } | |
2440 | ||
28b835d6 FW |
2441 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
2442 | { | |
2443 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2444 | struct pi_desc old, new; | |
2445 | unsigned int dest; | |
2446 | ||
31afb2ea PB |
2447 | /* |
2448 | * In case of hot-plug or hot-unplug, we may have to undo | |
2449 | * vmx_vcpu_pi_put even if there is no assigned device. And we | |
2450 | * always keep PI.NDST up to date for simplicity: it makes the | |
2451 | * code easier, and CPU migration is not a fast path. | |
2452 | */ | |
2453 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) | |
28b835d6 FW |
2454 | return; |
2455 | ||
31afb2ea PB |
2456 | /* |
2457 | * First handle the simple case where no cmpxchg is necessary; just | |
2458 | * allow posting non-urgent interrupts. | |
2459 | * | |
2460 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change | |
2461 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler | |
2462 | * expects the VCPU to be on the blocked_vcpu_list that matches | |
2463 | * PI.NDST. | |
2464 | */ | |
2465 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || | |
2466 | vcpu->cpu == cpu) { | |
2467 | pi_clear_sn(pi_desc); | |
28b835d6 | 2468 | return; |
31afb2ea | 2469 | } |
28b835d6 | 2470 | |
31afb2ea | 2471 | /* The full case. */ |
28b835d6 FW |
2472 | do { |
2473 | old.control = new.control = pi_desc->control; | |
2474 | ||
31afb2ea | 2475 | dest = cpu_physical_id(cpu); |
28b835d6 | 2476 | |
31afb2ea PB |
2477 | if (x2apic_enabled()) |
2478 | new.ndst = dest; | |
2479 | else | |
2480 | new.ndst = (dest << 8) & 0xFF00; | |
28b835d6 | 2481 | |
28b835d6 | 2482 | new.sn = 0; |
c0a1666b PB |
2483 | } while (cmpxchg64(&pi_desc->control, old.control, |
2484 | new.control) != old.control); | |
28b835d6 | 2485 | } |
1be0e61c | 2486 | |
c95ba92a PF |
2487 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
2488 | { | |
2489 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; | |
2490 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); | |
2491 | } | |
2492 | ||
6aa8b732 AK |
2493 | /* |
2494 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes | |
2495 | * vcpu mutex is already taken. | |
2496 | */ | |
15ad7146 | 2497 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
6aa8b732 | 2498 | { |
a2fa3e9f | 2499 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b80c76ec | 2500 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
6aa8b732 | 2501 | |
b80c76ec | 2502 | if (!already_loaded) { |
fe0e80be | 2503 | loaded_vmcs_clear(vmx->loaded_vmcs); |
92fe13be | 2504 | local_irq_disable(); |
8f536b76 | 2505 | crash_disable_local_vmclear(cpu); |
5a560f8b XG |
2506 | |
2507 | /* | |
2508 | * Read loaded_vmcs->cpu should be before fetching | |
2509 | * loaded_vmcs->loaded_vmcss_on_cpu_link. | |
2510 | * See the comments in __loaded_vmcs_clear(). | |
2511 | */ | |
2512 | smp_rmb(); | |
2513 | ||
d462b819 NHE |
2514 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
2515 | &per_cpu(loaded_vmcss_on_cpu, cpu)); | |
8f536b76 | 2516 | crash_enable_local_vmclear(cpu); |
92fe13be | 2517 | local_irq_enable(); |
b80c76ec JM |
2518 | } |
2519 | ||
2520 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { | |
2521 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; | |
2522 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
33241bfe | 2523 | indirect_branch_prediction_barrier(); |
b80c76ec JM |
2524 | } |
2525 | ||
2526 | if (!already_loaded) { | |
59c58ceb | 2527 | void *gdt = get_current_gdt_ro(); |
b80c76ec JM |
2528 | unsigned long sysenter_esp; |
2529 | ||
2530 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); | |
92fe13be | 2531 | |
6aa8b732 AK |
2532 | /* |
2533 | * Linux uses per-cpu TSS and GDT, so set these when switching | |
e0c23063 | 2534 | * processors. See 22.2.4. |
6aa8b732 | 2535 | */ |
e0c23063 | 2536 | vmcs_writel(HOST_TR_BASE, |
72f5e08d | 2537 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
59c58ceb | 2538 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
6aa8b732 | 2539 | |
b7ffc44d AL |
2540 | /* |
2541 | * VM exits change the host TR limit to 0x67 after a VM | |
2542 | * exit. This is okay, since 0x67 covers everything except | |
2543 | * the IO bitmap and have have code to handle the IO bitmap | |
2544 | * being lost after a VM exit. | |
2545 | */ | |
2546 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); | |
2547 | ||
6aa8b732 AK |
2548 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
2549 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ | |
ff2c3a18 | 2550 | |
d462b819 | 2551 | vmx->loaded_vmcs->cpu = cpu; |
6aa8b732 | 2552 | } |
28b835d6 | 2553 | |
2680d6da OH |
2554 | /* Setup TSC multiplier */ |
2555 | if (kvm_has_tsc_control && | |
c95ba92a PF |
2556 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
2557 | decache_tsc_multiplier(vmx); | |
2680d6da | 2558 | |
28b835d6 | 2559 | vmx_vcpu_pi_load(vcpu, cpu); |
1be0e61c | 2560 | vmx->host_pkru = read_pkru(); |
28b835d6 FW |
2561 | } |
2562 | ||
2563 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) | |
2564 | { | |
2565 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
2566 | ||
2567 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
2568 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
2569 | !kvm_vcpu_apicv_active(vcpu)) | |
28b835d6 FW |
2570 | return; |
2571 | ||
2572 | /* Set SN when the vCPU is preempted */ | |
2573 | if (vcpu->preempted) | |
2574 | pi_set_sn(pi_desc); | |
6aa8b732 AK |
2575 | } |
2576 | ||
2577 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) | |
2578 | { | |
28b835d6 FW |
2579 | vmx_vcpu_pi_put(vcpu); |
2580 | ||
a9b21b62 | 2581 | __vmx_load_host_state(to_vmx(vcpu)); |
6aa8b732 AK |
2582 | } |
2583 | ||
f244deed WL |
2584 | static bool emulation_required(struct kvm_vcpu *vcpu) |
2585 | { | |
2586 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); | |
2587 | } | |
2588 | ||
edcafe3c AK |
2589 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
2590 | ||
fe3ef05c NHE |
2591 | /* |
2592 | * Return the cr0 value that a nested guest would read. This is a combination | |
2593 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by | |
2594 | * its hypervisor (cr0_read_shadow). | |
2595 | */ | |
2596 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) | |
2597 | { | |
2598 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | | |
2599 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); | |
2600 | } | |
2601 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) | |
2602 | { | |
2603 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | | |
2604 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); | |
2605 | } | |
2606 | ||
6aa8b732 AK |
2607 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
2608 | { | |
78ac8b47 | 2609 | unsigned long rflags, save_rflags; |
345dcaa8 | 2610 | |
6de12732 AK |
2611 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
2612 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); | |
2613 | rflags = vmcs_readl(GUEST_RFLAGS); | |
2614 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
2615 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2616 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; | |
2617 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
2618 | } | |
2619 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 | 2620 | } |
6de12732 | 2621 | return to_vmx(vcpu)->rflags; |
6aa8b732 AK |
2622 | } |
2623 | ||
2624 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
2625 | { | |
f244deed WL |
2626 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
2627 | ||
6de12732 AK |
2628 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
2629 | to_vmx(vcpu)->rflags = rflags; | |
78ac8b47 AK |
2630 | if (to_vmx(vcpu)->rmode.vm86_active) { |
2631 | to_vmx(vcpu)->rmode.save_rflags = rflags; | |
053de044 | 2632 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
78ac8b47 | 2633 | } |
6aa8b732 | 2634 | vmcs_writel(GUEST_RFLAGS, rflags); |
f244deed WL |
2635 | |
2636 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) | |
2637 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); | |
6aa8b732 AK |
2638 | } |
2639 | ||
37ccdcbe | 2640 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
2809f5d2 GC |
2641 | { |
2642 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2643 | int ret = 0; | |
2644 | ||
2645 | if (interruptibility & GUEST_INTR_STATE_STI) | |
48005f64 | 2646 | ret |= KVM_X86_SHADOW_INT_STI; |
2809f5d2 | 2647 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
48005f64 | 2648 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
2809f5d2 | 2649 | |
37ccdcbe | 2650 | return ret; |
2809f5d2 GC |
2651 | } |
2652 | ||
2653 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) | |
2654 | { | |
2655 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
2656 | u32 interruptibility = interruptibility_old; | |
2657 | ||
2658 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); | |
2659 | ||
48005f64 | 2660 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
2809f5d2 | 2661 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
48005f64 | 2662 | else if (mask & KVM_X86_SHADOW_INT_STI) |
2809f5d2 GC |
2663 | interruptibility |= GUEST_INTR_STATE_STI; |
2664 | ||
2665 | if ((interruptibility != interruptibility_old)) | |
2666 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); | |
2667 | } | |
2668 | ||
6aa8b732 AK |
2669 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
2670 | { | |
2671 | unsigned long rip; | |
6aa8b732 | 2672 | |
5fdbf976 | 2673 | rip = kvm_rip_read(vcpu); |
6aa8b732 | 2674 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
5fdbf976 | 2675 | kvm_rip_write(vcpu, rip); |
6aa8b732 | 2676 | |
2809f5d2 GC |
2677 | /* skipping an emulated instruction also counts */ |
2678 | vmx_set_interrupt_shadow(vcpu, 0); | |
6aa8b732 AK |
2679 | } |
2680 | ||
b96fb439 PB |
2681 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
2682 | unsigned long exit_qual) | |
2683 | { | |
2684 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
2685 | unsigned int nr = vcpu->arch.exception.nr; | |
2686 | u32 intr_info = nr | INTR_INFO_VALID_MASK; | |
2687 | ||
2688 | if (vcpu->arch.exception.has_error_code) { | |
2689 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; | |
2690 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; | |
2691 | } | |
2692 | ||
2693 | if (kvm_exception_is_soft(nr)) | |
2694 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; | |
2695 | else | |
2696 | intr_info |= INTR_TYPE_HARD_EXCEPTION; | |
2697 | ||
2698 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && | |
2699 | vmx_get_nmi_mask(vcpu)) | |
2700 | intr_info |= INTR_INFO_UNBLOCK_NMI; | |
2701 | ||
2702 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); | |
2703 | } | |
2704 | ||
0b6ac343 NHE |
2705 | /* |
2706 | * KVM wants to inject page-faults which it got to the guest. This function | |
2707 | * checks whether in a nested guest, we need to inject them to L1 or L2. | |
0b6ac343 | 2708 | */ |
bfcf83b1 | 2709 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
0b6ac343 NHE |
2710 | { |
2711 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
adfe20fb | 2712 | unsigned int nr = vcpu->arch.exception.nr; |
0b6ac343 | 2713 | |
b96fb439 PB |
2714 | if (nr == PF_VECTOR) { |
2715 | if (vcpu->arch.exception.nested_apf) { | |
bfcf83b1 | 2716 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
b96fb439 PB |
2717 | return 1; |
2718 | } | |
2719 | /* | |
2720 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. | |
2721 | * The fix is to add the ancillary datum (CR2 or DR6) to structs | |
2722 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 | |
2723 | * can be written only when inject_pending_event runs. This should be | |
2724 | * conditional on a new capability---if the capability is disabled, | |
2725 | * kvm_multiple_exception would write the ancillary information to | |
2726 | * CR2 or DR6, for backwards ABI-compatibility. | |
2727 | */ | |
2728 | if (nested_vmx_is_page_fault_vmexit(vmcs12, | |
2729 | vcpu->arch.exception.error_code)) { | |
bfcf83b1 | 2730 | *exit_qual = vcpu->arch.cr2; |
b96fb439 PB |
2731 | return 1; |
2732 | } | |
2733 | } else { | |
b96fb439 | 2734 | if (vmcs12->exception_bitmap & (1u << nr)) { |
a2812bb8 | 2735 | if (nr == DB_VECTOR) { |
bfcf83b1 | 2736 | *exit_qual = vcpu->arch.dr6; |
a2812bb8 JM |
2737 | *exit_qual &= ~(DR6_FIXED_1 | DR6_BT); |
2738 | *exit_qual ^= DR6_RTM; | |
2739 | } else { | |
bfcf83b1 | 2740 | *exit_qual = 0; |
a2812bb8 | 2741 | } |
b96fb439 PB |
2742 | return 1; |
2743 | } | |
adfe20fb WL |
2744 | } |
2745 | ||
b96fb439 | 2746 | return 0; |
0b6ac343 NHE |
2747 | } |
2748 | ||
cfcd20e5 | 2749 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
298101da | 2750 | { |
77ab6db0 | 2751 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
cfcd20e5 WL |
2752 | unsigned nr = vcpu->arch.exception.nr; |
2753 | bool has_error_code = vcpu->arch.exception.has_error_code; | |
cfcd20e5 | 2754 | u32 error_code = vcpu->arch.exception.error_code; |
8ab2d2e2 | 2755 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
77ab6db0 | 2756 | |
8ab2d2e2 | 2757 | if (has_error_code) { |
77ab6db0 | 2758 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
8ab2d2e2 JK |
2759 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
2760 | } | |
77ab6db0 | 2761 | |
7ffd92c5 | 2762 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
2763 | int inc_eip = 0; |
2764 | if (kvm_exception_is_soft(nr)) | |
2765 | inc_eip = vcpu->arch.event_exit_inst_len; | |
2766 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) | |
a92601bb | 2767 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
77ab6db0 JK |
2768 | return; |
2769 | } | |
2770 | ||
299f0328 SC |
2771 | WARN_ON_ONCE(vmx->emulation_required); |
2772 | ||
66fd3f7f GN |
2773 | if (kvm_exception_is_soft(nr)) { |
2774 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
2775 | vmx->vcpu.arch.event_exit_inst_len); | |
8ab2d2e2 JK |
2776 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
2777 | } else | |
2778 | intr_info |= INTR_TYPE_HARD_EXCEPTION; | |
2779 | ||
2780 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); | |
298101da AK |
2781 | } |
2782 | ||
4e47c7a6 SY |
2783 | static bool vmx_rdtscp_supported(void) |
2784 | { | |
2785 | return cpu_has_vmx_rdtscp(); | |
2786 | } | |
2787 | ||
ad756a16 MJ |
2788 | static bool vmx_invpcid_supported(void) |
2789 | { | |
2790 | return cpu_has_vmx_invpcid() && enable_ept; | |
2791 | } | |
2792 | ||
a75beee6 ED |
2793 | /* |
2794 | * Swap MSR entry in host/guest MSR entry array. | |
2795 | */ | |
8b9cf98c | 2796 | static void move_msr_up(struct vcpu_vmx *vmx, int from, int to) |
a75beee6 | 2797 | { |
26bb0981 | 2798 | struct shared_msr_entry tmp; |
a2fa3e9f GH |
2799 | |
2800 | tmp = vmx->guest_msrs[to]; | |
2801 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; | |
2802 | vmx->guest_msrs[from] = tmp; | |
a75beee6 ED |
2803 | } |
2804 | ||
e38aea3e AK |
2805 | /* |
2806 | * Set up the vmcs to automatically save and restore system | |
2807 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy | |
2808 | * mode, as fiddling with msrs is very expensive. | |
2809 | */ | |
8b9cf98c | 2810 | static void setup_msrs(struct vcpu_vmx *vmx) |
e38aea3e | 2811 | { |
26bb0981 | 2812 | int save_nmsrs, index; |
e38aea3e | 2813 | |
a75beee6 ED |
2814 | save_nmsrs = 0; |
2815 | #ifdef CONFIG_X86_64 | |
8b9cf98c | 2816 | if (is_long_mode(&vmx->vcpu)) { |
8b9cf98c | 2817 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
a75beee6 | 2818 | if (index >= 0) |
8b9cf98c RR |
2819 | move_msr_up(vmx, index, save_nmsrs++); |
2820 | index = __find_msr_index(vmx, MSR_LSTAR); | |
a75beee6 | 2821 | if (index >= 0) |
8b9cf98c RR |
2822 | move_msr_up(vmx, index, save_nmsrs++); |
2823 | index = __find_msr_index(vmx, MSR_CSTAR); | |
a75beee6 | 2824 | if (index >= 0) |
8b9cf98c | 2825 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 | 2826 | /* |
8c06585d | 2827 | * MSR_STAR is only needed on long mode guests, and only |
a75beee6 ED |
2828 | * if efer.sce is enabled. |
2829 | */ | |
8c06585d | 2830 | index = __find_msr_index(vmx, MSR_STAR); |
f6801dff | 2831 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
8b9cf98c | 2832 | move_msr_up(vmx, index, save_nmsrs++); |
a75beee6 ED |
2833 | } |
2834 | #endif | |
92c0d900 AK |
2835 | index = __find_msr_index(vmx, MSR_EFER); |
2836 | if (index >= 0 && update_transition_efer(vmx, index)) | |
26bb0981 | 2837 | move_msr_up(vmx, index, save_nmsrs++); |
bb4c5f0a JM |
2838 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
2839 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) | |
2840 | move_msr_up(vmx, index, save_nmsrs++); | |
e38aea3e | 2841 | |
26bb0981 | 2842 | vmx->save_nmsrs = save_nmsrs; |
5897297b | 2843 | |
8d14695f | 2844 | if (cpu_has_vmx_msr_bitmap()) |
4b0be90f | 2845 | vmx_update_msr_bitmap(&vmx->vcpu); |
e38aea3e AK |
2846 | } |
2847 | ||
f7f5542f KA |
2848 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
2849 | { | |
2850 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
2851 | ||
2852 | if (is_guest_mode(vcpu) && | |
2853 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) | |
2854 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; | |
2855 | ||
2856 | return vcpu->arch.tsc_offset; | |
2857 | } | |
2858 | ||
7cb0f5cc | 2859 | static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
6aa8b732 | 2860 | { |
7cb0f5cc | 2861 | u64 active_offset = offset; |
27fc51b2 | 2862 | if (is_guest_mode(vcpu)) { |
7991825b | 2863 | /* |
27fc51b2 NHE |
2864 | * We're here if L1 chose not to trap WRMSR to TSC. According |
2865 | * to the spec, this should set L1's TSC; The offset that L1 | |
2866 | * set for L2 remains unchanged, and still needs to be added | |
2867 | * to the newly set TSC to get L2's TSC. | |
7991825b | 2868 | */ |
7cb0f5cc LS |
2869 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
2870 | if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING)) | |
2871 | active_offset += vmcs12->tsc_offset; | |
27fc51b2 | 2872 | } else { |
489223ed YY |
2873 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
2874 | vmcs_read64(TSC_OFFSET), offset); | |
27fc51b2 | 2875 | } |
7cb0f5cc LS |
2876 | |
2877 | vmcs_write64(TSC_OFFSET, active_offset); | |
2878 | return active_offset; | |
6aa8b732 AK |
2879 | } |
2880 | ||
801d3424 NHE |
2881 | /* |
2882 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX | |
2883 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for | |
2884 | * all guests if the "nested" module option is off, and can also be disabled | |
2885 | * for a single guest by disabling its VMX cpuid bit. | |
2886 | */ | |
2887 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) | |
2888 | { | |
d6321d49 | 2889 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
801d3424 NHE |
2890 | } |
2891 | ||
b87a51ae NHE |
2892 | /* |
2893 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be | |
2894 | * returned for the various VMX controls MSRs when nested VMX is enabled. | |
2895 | * The same values should also be used to verify that vmcs12 control fields are | |
2896 | * valid during nested entry from L1 to L2. | |
2897 | * Each of these control msrs has a low and high 32-bit half: A low bit is on | |
2898 | * if the corresponding bit in the (32-bit) control field *must* be on, and a | |
2899 | * bit in the high half is on if the corresponding bit in the control field | |
2900 | * may be on. See also vmx_control_verify(). | |
b87a51ae | 2901 | */ |
b9c237bb | 2902 | static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) |
b87a51ae NHE |
2903 | { |
2904 | /* | |
2905 | * Note that as a general rule, the high half of the MSRs (bits in | |
2906 | * the control fields which may be 1) should be initialized by the | |
2907 | * intersection of the underlying hardware's MSR (i.e., features which | |
2908 | * can be supported) and the list of features we want to expose - | |
2909 | * because they are known to be properly supported in our code. | |
2910 | * Also, usually, the low half of the MSRs (bits which must be 1) can | |
2911 | * be set to 0, meaning that L1 may turn off any of these bits. The | |
2912 | * reason is that if one of these bits is necessary, it will appear | |
2913 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control | |
2914 | * fields of vmcs01 and vmcs02, will turn these bits off - and | |
7313c698 | 2915 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
b87a51ae NHE |
2916 | * These rules have exceptions below. |
2917 | */ | |
2918 | ||
2919 | /* pin-based controls */ | |
eabeaacc | 2920 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
b9c237bb WV |
2921 | vmx->nested.nested_vmx_pinbased_ctls_low, |
2922 | vmx->nested.nested_vmx_pinbased_ctls_high); | |
2923 | vmx->nested.nested_vmx_pinbased_ctls_low |= | |
2924 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
2925 | vmx->nested.nested_vmx_pinbased_ctls_high &= | |
2926 | PIN_BASED_EXT_INTR_MASK | | |
2927 | PIN_BASED_NMI_EXITING | | |
2928 | PIN_BASED_VIRTUAL_NMIS; | |
2929 | vmx->nested.nested_vmx_pinbased_ctls_high |= | |
2930 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | | |
0238ea91 | 2931 | PIN_BASED_VMX_PREEMPTION_TIMER; |
d62caabb | 2932 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) |
705699a1 WV |
2933 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
2934 | PIN_BASED_POSTED_INTR; | |
b87a51ae | 2935 | |
3dbcd8da | 2936 | /* exit controls */ |
c0dfee58 | 2937 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
b9c237bb WV |
2938 | vmx->nested.nested_vmx_exit_ctls_low, |
2939 | vmx->nested.nested_vmx_exit_ctls_high); | |
2940 | vmx->nested.nested_vmx_exit_ctls_low = | |
2941 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; | |
e0ba1a6f | 2942 | |
b9c237bb | 2943 | vmx->nested.nested_vmx_exit_ctls_high &= |
b87a51ae | 2944 | #ifdef CONFIG_X86_64 |
c0dfee58 | 2945 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
b87a51ae | 2946 | #endif |
f4124500 | 2947 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
b9c237bb WV |
2948 | vmx->nested.nested_vmx_exit_ctls_high |= |
2949 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | | |
f4124500 | 2950 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
e0ba1a6f BD |
2951 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
2952 | ||
a87036ad | 2953 | if (kvm_mpx_supported()) |
b9c237bb | 2954 | vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
b87a51ae | 2955 | |
2996fca0 | 2956 | /* We support free control of debug control saving. */ |
0115f9cb | 2957 | vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
2996fca0 | 2958 | |
b87a51ae NHE |
2959 | /* entry controls */ |
2960 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, | |
b9c237bb WV |
2961 | vmx->nested.nested_vmx_entry_ctls_low, |
2962 | vmx->nested.nested_vmx_entry_ctls_high); | |
2963 | vmx->nested.nested_vmx_entry_ctls_low = | |
2964 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; | |
2965 | vmx->nested.nested_vmx_entry_ctls_high &= | |
57435349 JK |
2966 | #ifdef CONFIG_X86_64 |
2967 | VM_ENTRY_IA32E_MODE | | |
2968 | #endif | |
2969 | VM_ENTRY_LOAD_IA32_PAT; | |
b9c237bb WV |
2970 | vmx->nested.nested_vmx_entry_ctls_high |= |
2971 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); | |
a87036ad | 2972 | if (kvm_mpx_supported()) |
b9c237bb | 2973 | vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
57435349 | 2974 | |
2996fca0 | 2975 | /* We support free control of debug control loading. */ |
0115f9cb | 2976 | vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
2996fca0 | 2977 | |
b87a51ae NHE |
2978 | /* cpu-based controls */ |
2979 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, | |
b9c237bb WV |
2980 | vmx->nested.nested_vmx_procbased_ctls_low, |
2981 | vmx->nested.nested_vmx_procbased_ctls_high); | |
2982 | vmx->nested.nested_vmx_procbased_ctls_low = | |
2983 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
2984 | vmx->nested.nested_vmx_procbased_ctls_high &= | |
a294c9bb JK |
2985 | CPU_BASED_VIRTUAL_INTR_PENDING | |
2986 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | | |
b87a51ae NHE |
2987 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
2988 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | | |
2989 | CPU_BASED_CR3_STORE_EXITING | | |
2990 | #ifdef CONFIG_X86_64 | |
2991 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | | |
2992 | #endif | |
2993 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | | |
5f3d45e7 MD |
2994 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
2995 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | | |
2996 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | | |
2997 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; | |
b87a51ae NHE |
2998 | /* |
2999 | * We can allow some features even when not supported by the | |
3000 | * hardware. For example, L1 can specify an MSR bitmap - and we | |
3001 | * can use it to avoid exits to L1 - even when L0 runs L2 | |
3002 | * without MSR bitmaps. | |
3003 | */ | |
b9c237bb WV |
3004 | vmx->nested.nested_vmx_procbased_ctls_high |= |
3005 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | | |
560b7ee1 | 3006 | CPU_BASED_USE_MSR_BITMAPS; |
b87a51ae | 3007 | |
3dcdf3ec | 3008 | /* We support free control of CR3 access interception. */ |
0115f9cb | 3009 | vmx->nested.nested_vmx_procbased_ctls_low &= |
3dcdf3ec JK |
3010 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
3011 | ||
80154d77 PB |
3012 | /* |
3013 | * secondary cpu-based controls. Do not include those that | |
3014 | * depend on CPUID bits, they are added later by vmx_cpuid_update. | |
3015 | */ | |
b87a51ae | 3016 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
b9c237bb WV |
3017 | vmx->nested.nested_vmx_secondary_ctls_low, |
3018 | vmx->nested.nested_vmx_secondary_ctls_high); | |
3019 | vmx->nested.nested_vmx_secondary_ctls_low = 0; | |
3020 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
d6851fbe | 3021 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
1b07304c | 3022 | SECONDARY_EXEC_DESC | |
f2b93280 | 3023 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
82f0dd4b | 3024 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
608406e2 | 3025 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
3db13480 | 3026 | SECONDARY_EXEC_WBINVD_EXITING; |
c18911a2 | 3027 | |
afa61f75 NHE |
3028 | if (enable_ept) { |
3029 | /* nested EPT: emulate EPT also to L1 */ | |
b9c237bb | 3030 | vmx->nested.nested_vmx_secondary_ctls_high |= |
0790ec17 | 3031 | SECONDARY_EXEC_ENABLE_EPT; |
b9c237bb | 3032 | vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
7db74265 | 3033 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
02120c45 BD |
3034 | if (cpu_has_vmx_ept_execute_only()) |
3035 | vmx->nested.nested_vmx_ept_caps |= | |
3036 | VMX_EPT_EXECUTE_ONLY_BIT; | |
b9c237bb | 3037 | vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept; |
45e11817 | 3038 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
7db74265 PB |
3039 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
3040 | VMX_EPT_1GB_PAGE_BIT; | |
03efce6f BD |
3041 | if (enable_ept_ad_bits) { |
3042 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
3043 | SECONDARY_EXEC_ENABLE_PML; | |
7461fbc4 | 3044 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT; |
03efce6f | 3045 | } |
1c13bffd | 3046 | } |
afa61f75 | 3047 | |
27c42a1b BD |
3048 | if (cpu_has_vmx_vmfunc()) { |
3049 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
3050 | SECONDARY_EXEC_ENABLE_VMFUNC; | |
41ab9372 BD |
3051 | /* |
3052 | * Advertise EPTP switching unconditionally | |
3053 | * since we emulate it | |
3054 | */ | |
575b3a2c WL |
3055 | if (enable_ept) |
3056 | vmx->nested.nested_vmx_vmfunc_controls = | |
3057 | VMX_VMFUNC_EPTP_SWITCHING; | |
27c42a1b BD |
3058 | } |
3059 | ||
ef697a71 PB |
3060 | /* |
3061 | * Old versions of KVM use the single-context version without | |
3062 | * checking for support, so declare that it is supported even | |
3063 | * though it is treated as global context. The alternative is | |
3064 | * not failing the single-context invvpid, and it is worse. | |
3065 | */ | |
63cb6d5f WL |
3066 | if (enable_vpid) { |
3067 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
3068 | SECONDARY_EXEC_ENABLE_VPID; | |
089d7b6e | 3069 | vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT | |
bcdde302 | 3070 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
1c13bffd | 3071 | } |
99b83ac8 | 3072 | |
0790ec17 RK |
3073 | if (enable_unrestricted_guest) |
3074 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
3075 | SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
3076 | ||
c18911a2 | 3077 | /* miscellaneous data */ |
b9c237bb WV |
3078 | rdmsr(MSR_IA32_VMX_MISC, |
3079 | vmx->nested.nested_vmx_misc_low, | |
3080 | vmx->nested.nested_vmx_misc_high); | |
3081 | vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; | |
3082 | vmx->nested.nested_vmx_misc_low |= | |
3083 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | | |
f4124500 | 3084 | VMX_MISC_ACTIVITY_HLT; |
b9c237bb | 3085 | vmx->nested.nested_vmx_misc_high = 0; |
62cc6b9d DM |
3086 | |
3087 | /* | |
3088 | * This MSR reports some information about VMX support. We | |
3089 | * should return information about the VMX we emulate for the | |
3090 | * guest, and the VMCS structure we give it - not about the | |
3091 | * VMX support of the underlying hardware. | |
3092 | */ | |
3093 | vmx->nested.nested_vmx_basic = | |
3094 | VMCS12_REVISION | | |
3095 | VMX_BASIC_TRUE_CTLS | | |
3096 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | | |
3097 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); | |
3098 | ||
3099 | if (cpu_has_vmx_basic_inout()) | |
3100 | vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT; | |
3101 | ||
3102 | /* | |
8322ebbb | 3103 | * These MSRs specify bits which the guest must keep fixed on |
62cc6b9d DM |
3104 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
3105 | * We picked the standard core2 setting. | |
3106 | */ | |
3107 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) | |
3108 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE | |
3109 | vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON; | |
62cc6b9d | 3110 | vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON; |
8322ebbb DM |
3111 | |
3112 | /* These MSRs specify bits which the guest must keep fixed off. */ | |
3113 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1); | |
3114 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1); | |
62cc6b9d DM |
3115 | |
3116 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ | |
3117 | vmx->nested.nested_vmx_vmcs_enum = 0x2e; | |
b87a51ae NHE |
3118 | } |
3119 | ||
3899152c DM |
3120 | /* |
3121 | * if fixed0[i] == 1: val[i] must be 1 | |
3122 | * if fixed1[i] == 0: val[i] must be 0 | |
3123 | */ | |
3124 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) | |
3125 | { | |
3126 | return ((val & fixed1) | fixed0) == val; | |
b87a51ae NHE |
3127 | } |
3128 | ||
3129 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) | |
3130 | { | |
3899152c | 3131 | return fixed_bits_valid(control, low, high); |
b87a51ae NHE |
3132 | } |
3133 | ||
3134 | static inline u64 vmx_control_msr(u32 low, u32 high) | |
3135 | { | |
3136 | return low | ((u64)high << 32); | |
3137 | } | |
3138 | ||
62cc6b9d DM |
3139 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
3140 | { | |
3141 | superset &= mask; | |
3142 | subset &= mask; | |
3143 | ||
3144 | return (superset | subset) == superset; | |
3145 | } | |
3146 | ||
3147 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) | |
3148 | { | |
3149 | const u64 feature_and_reserved = | |
3150 | /* feature (except bit 48; see below) */ | |
3151 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | | |
3152 | /* reserved */ | |
3153 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); | |
3154 | u64 vmx_basic = vmx->nested.nested_vmx_basic; | |
3155 | ||
3156 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) | |
3157 | return -EINVAL; | |
3158 | ||
3159 | /* | |
3160 | * KVM does not emulate a version of VMX that constrains physical | |
3161 | * addresses of VMX structures (e.g. VMCS) to 32-bits. | |
3162 | */ | |
3163 | if (data & BIT_ULL(48)) | |
3164 | return -EINVAL; | |
3165 | ||
3166 | if (vmx_basic_vmcs_revision_id(vmx_basic) != | |
3167 | vmx_basic_vmcs_revision_id(data)) | |
3168 | return -EINVAL; | |
3169 | ||
3170 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) | |
3171 | return -EINVAL; | |
3172 | ||
3173 | vmx->nested.nested_vmx_basic = data; | |
3174 | return 0; | |
3175 | } | |
3176 | ||
3177 | static int | |
3178 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
3179 | { | |
3180 | u64 supported; | |
3181 | u32 *lowp, *highp; | |
3182 | ||
3183 | switch (msr_index) { | |
3184 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3185 | lowp = &vmx->nested.nested_vmx_pinbased_ctls_low; | |
3186 | highp = &vmx->nested.nested_vmx_pinbased_ctls_high; | |
3187 | break; | |
3188 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3189 | lowp = &vmx->nested.nested_vmx_procbased_ctls_low; | |
3190 | highp = &vmx->nested.nested_vmx_procbased_ctls_high; | |
3191 | break; | |
3192 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3193 | lowp = &vmx->nested.nested_vmx_exit_ctls_low; | |
3194 | highp = &vmx->nested.nested_vmx_exit_ctls_high; | |
3195 | break; | |
3196 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3197 | lowp = &vmx->nested.nested_vmx_entry_ctls_low; | |
3198 | highp = &vmx->nested.nested_vmx_entry_ctls_high; | |
3199 | break; | |
3200 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
3201 | lowp = &vmx->nested.nested_vmx_secondary_ctls_low; | |
3202 | highp = &vmx->nested.nested_vmx_secondary_ctls_high; | |
3203 | break; | |
3204 | default: | |
3205 | BUG(); | |
3206 | } | |
3207 | ||
3208 | supported = vmx_control_msr(*lowp, *highp); | |
3209 | ||
3210 | /* Check must-be-1 bits are still 1. */ | |
3211 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) | |
3212 | return -EINVAL; | |
3213 | ||
3214 | /* Check must-be-0 bits are still 0. */ | |
3215 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) | |
3216 | return -EINVAL; | |
3217 | ||
3218 | *lowp = data; | |
3219 | *highp = data >> 32; | |
3220 | return 0; | |
3221 | } | |
3222 | ||
3223 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) | |
3224 | { | |
3225 | const u64 feature_and_reserved_bits = | |
3226 | /* feature */ | |
3227 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | | |
3228 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | | |
3229 | /* reserved */ | |
3230 | GENMASK_ULL(13, 9) | BIT_ULL(31); | |
3231 | u64 vmx_misc; | |
3232 | ||
3233 | vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low, | |
3234 | vmx->nested.nested_vmx_misc_high); | |
3235 | ||
3236 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) | |
3237 | return -EINVAL; | |
3238 | ||
3239 | if ((vmx->nested.nested_vmx_pinbased_ctls_high & | |
3240 | PIN_BASED_VMX_PREEMPTION_TIMER) && | |
3241 | vmx_misc_preemption_timer_rate(data) != | |
3242 | vmx_misc_preemption_timer_rate(vmx_misc)) | |
3243 | return -EINVAL; | |
3244 | ||
3245 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) | |
3246 | return -EINVAL; | |
3247 | ||
3248 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) | |
3249 | return -EINVAL; | |
3250 | ||
3251 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) | |
3252 | return -EINVAL; | |
3253 | ||
3254 | vmx->nested.nested_vmx_misc_low = data; | |
3255 | vmx->nested.nested_vmx_misc_high = data >> 32; | |
3256 | return 0; | |
3257 | } | |
3258 | ||
3259 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) | |
3260 | { | |
3261 | u64 vmx_ept_vpid_cap; | |
3262 | ||
3263 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps, | |
3264 | vmx->nested.nested_vmx_vpid_caps); | |
3265 | ||
3266 | /* Every bit is either reserved or a feature bit. */ | |
3267 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) | |
3268 | return -EINVAL; | |
3269 | ||
3270 | vmx->nested.nested_vmx_ept_caps = data; | |
3271 | vmx->nested.nested_vmx_vpid_caps = data >> 32; | |
3272 | return 0; | |
3273 | } | |
3274 | ||
3275 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) | |
3276 | { | |
3277 | u64 *msr; | |
3278 | ||
3279 | switch (msr_index) { | |
3280 | case MSR_IA32_VMX_CR0_FIXED0: | |
3281 | msr = &vmx->nested.nested_vmx_cr0_fixed0; | |
3282 | break; | |
3283 | case MSR_IA32_VMX_CR4_FIXED0: | |
3284 | msr = &vmx->nested.nested_vmx_cr4_fixed0; | |
3285 | break; | |
3286 | default: | |
3287 | BUG(); | |
3288 | } | |
3289 | ||
3290 | /* | |
3291 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) | |
3292 | * must be 1 in the restored value. | |
3293 | */ | |
3294 | if (!is_bitwise_subset(data, *msr, -1ULL)) | |
3295 | return -EINVAL; | |
3296 | ||
3297 | *msr = data; | |
3298 | return 0; | |
3299 | } | |
3300 | ||
3301 | /* | |
3302 | * Called when userspace is restoring VMX MSRs. | |
3303 | * | |
3304 | * Returns 0 on success, non-0 otherwise. | |
3305 | */ | |
3306 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
b87a51ae | 3307 | { |
b9c237bb WV |
3308 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3309 | ||
b87a51ae | 3310 | switch (msr_index) { |
b87a51ae | 3311 | case MSR_IA32_VMX_BASIC: |
62cc6b9d DM |
3312 | return vmx_restore_vmx_basic(vmx, data); |
3313 | case MSR_IA32_VMX_PINBASED_CTLS: | |
3314 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
3315 | case MSR_IA32_VMX_EXIT_CTLS: | |
3316 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b87a51ae | 3317 | /* |
62cc6b9d DM |
3318 | * The "non-true" VMX capability MSRs are generated from the |
3319 | * "true" MSRs, so we do not support restoring them directly. | |
3320 | * | |
3321 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace | |
3322 | * should restore the "true" MSRs with the must-be-1 bits | |
3323 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND | |
3324 | * DEFAULT SETTINGS". | |
b87a51ae | 3325 | */ |
62cc6b9d DM |
3326 | return -EINVAL; |
3327 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3328 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3329 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3330 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3331 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
3332 | return vmx_restore_control_msr(vmx, msr_index, data); | |
3333 | case MSR_IA32_VMX_MISC: | |
3334 | return vmx_restore_vmx_misc(vmx, data); | |
3335 | case MSR_IA32_VMX_CR0_FIXED0: | |
3336 | case MSR_IA32_VMX_CR4_FIXED0: | |
3337 | return vmx_restore_fixed0_msr(vmx, msr_index, data); | |
3338 | case MSR_IA32_VMX_CR0_FIXED1: | |
3339 | case MSR_IA32_VMX_CR4_FIXED1: | |
3340 | /* | |
3341 | * These MSRs are generated based on the vCPU's CPUID, so we | |
3342 | * do not support restoring them directly. | |
3343 | */ | |
3344 | return -EINVAL; | |
3345 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
3346 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); | |
3347 | case MSR_IA32_VMX_VMCS_ENUM: | |
3348 | vmx->nested.nested_vmx_vmcs_enum = data; | |
3349 | return 0; | |
3350 | default: | |
b87a51ae | 3351 | /* |
62cc6b9d | 3352 | * The rest of the VMX capability MSRs do not support restore. |
b87a51ae | 3353 | */ |
62cc6b9d DM |
3354 | return -EINVAL; |
3355 | } | |
3356 | } | |
3357 | ||
3358 | /* Returns 0 on success, non-0 otherwise. */ | |
3359 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
3360 | { | |
3361 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
3362 | ||
3363 | switch (msr_index) { | |
3364 | case MSR_IA32_VMX_BASIC: | |
3365 | *pdata = vmx->nested.nested_vmx_basic; | |
b87a51ae NHE |
3366 | break; |
3367 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: | |
3368 | case MSR_IA32_VMX_PINBASED_CTLS: | |
b9c237bb WV |
3369 | *pdata = vmx_control_msr( |
3370 | vmx->nested.nested_vmx_pinbased_ctls_low, | |
3371 | vmx->nested.nested_vmx_pinbased_ctls_high); | |
0115f9cb DM |
3372 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
3373 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3374 | break; |
3375 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: | |
3376 | case MSR_IA32_VMX_PROCBASED_CTLS: | |
b9c237bb WV |
3377 | *pdata = vmx_control_msr( |
3378 | vmx->nested.nested_vmx_procbased_ctls_low, | |
3379 | vmx->nested.nested_vmx_procbased_ctls_high); | |
0115f9cb DM |
3380 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
3381 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3382 | break; |
3383 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: | |
3384 | case MSR_IA32_VMX_EXIT_CTLS: | |
b9c237bb WV |
3385 | *pdata = vmx_control_msr( |
3386 | vmx->nested.nested_vmx_exit_ctls_low, | |
3387 | vmx->nested.nested_vmx_exit_ctls_high); | |
0115f9cb DM |
3388 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
3389 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3390 | break; |
3391 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: | |
3392 | case MSR_IA32_VMX_ENTRY_CTLS: | |
b9c237bb WV |
3393 | *pdata = vmx_control_msr( |
3394 | vmx->nested.nested_vmx_entry_ctls_low, | |
3395 | vmx->nested.nested_vmx_entry_ctls_high); | |
0115f9cb DM |
3396 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
3397 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; | |
b87a51ae NHE |
3398 | break; |
3399 | case MSR_IA32_VMX_MISC: | |
b9c237bb WV |
3400 | *pdata = vmx_control_msr( |
3401 | vmx->nested.nested_vmx_misc_low, | |
3402 | vmx->nested.nested_vmx_misc_high); | |
b87a51ae | 3403 | break; |
b87a51ae | 3404 | case MSR_IA32_VMX_CR0_FIXED0: |
62cc6b9d | 3405 | *pdata = vmx->nested.nested_vmx_cr0_fixed0; |
b87a51ae NHE |
3406 | break; |
3407 | case MSR_IA32_VMX_CR0_FIXED1: | |
62cc6b9d | 3408 | *pdata = vmx->nested.nested_vmx_cr0_fixed1; |
b87a51ae NHE |
3409 | break; |
3410 | case MSR_IA32_VMX_CR4_FIXED0: | |
62cc6b9d | 3411 | *pdata = vmx->nested.nested_vmx_cr4_fixed0; |
b87a51ae NHE |
3412 | break; |
3413 | case MSR_IA32_VMX_CR4_FIXED1: | |
62cc6b9d | 3414 | *pdata = vmx->nested.nested_vmx_cr4_fixed1; |
b87a51ae NHE |
3415 | break; |
3416 | case MSR_IA32_VMX_VMCS_ENUM: | |
62cc6b9d | 3417 | *pdata = vmx->nested.nested_vmx_vmcs_enum; |
b87a51ae NHE |
3418 | break; |
3419 | case MSR_IA32_VMX_PROCBASED_CTLS2: | |
b9c237bb WV |
3420 | *pdata = vmx_control_msr( |
3421 | vmx->nested.nested_vmx_secondary_ctls_low, | |
3422 | vmx->nested.nested_vmx_secondary_ctls_high); | |
b87a51ae NHE |
3423 | break; |
3424 | case MSR_IA32_VMX_EPT_VPID_CAP: | |
089d7b6e WL |
3425 | *pdata = vmx->nested.nested_vmx_ept_caps | |
3426 | ((u64)vmx->nested.nested_vmx_vpid_caps << 32); | |
b87a51ae | 3427 | break; |
27c42a1b BD |
3428 | case MSR_IA32_VMX_VMFUNC: |
3429 | *pdata = vmx->nested.nested_vmx_vmfunc_controls; | |
3430 | break; | |
b87a51ae | 3431 | default: |
b87a51ae | 3432 | return 1; |
b3897a49 NHE |
3433 | } |
3434 | ||
b87a51ae NHE |
3435 | return 0; |
3436 | } | |
3437 | ||
37e4c997 HZ |
3438 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
3439 | uint64_t val) | |
3440 | { | |
3441 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; | |
3442 | ||
3443 | return !(val & ~valid_bits); | |
3444 | } | |
3445 | ||
ab1bebf8 TL |
3446 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
3447 | { | |
3448 | return 1; | |
3449 | } | |
3450 | ||
6aa8b732 AK |
3451 | /* |
3452 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
3453 | * Returns 0 on success, non-0 otherwise. | |
3454 | * Assumes vcpu_load() was already called. | |
3455 | */ | |
609e36d3 | 3456 | static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3457 | { |
26bb0981 | 3458 | struct shared_msr_entry *msr; |
6aa8b732 | 3459 | |
609e36d3 | 3460 | switch (msr_info->index) { |
05b3e0c2 | 3461 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3462 | case MSR_FS_BASE: |
609e36d3 | 3463 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
6aa8b732 AK |
3464 | break; |
3465 | case MSR_GS_BASE: | |
609e36d3 | 3466 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
6aa8b732 | 3467 | break; |
44ea2b17 AK |
3468 | case MSR_KERNEL_GS_BASE: |
3469 | vmx_load_host_state(to_vmx(vcpu)); | |
609e36d3 | 3470 | msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
44ea2b17 | 3471 | break; |
26bb0981 | 3472 | #endif |
6aa8b732 | 3473 | case MSR_EFER: |
609e36d3 | 3474 | return kvm_get_msr_common(vcpu, msr_info); |
74469996 KA |
3475 | case MSR_IA32_SPEC_CTRL: |
3476 | if (!msr_info->host_initiated && | |
5856293c | 3477 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
74469996 KA |
3478 | return 1; |
3479 | ||
3480 | msr_info->data = to_vmx(vcpu)->spec_ctrl; | |
3481 | break; | |
6aa8b732 | 3482 | case MSR_IA32_SYSENTER_CS: |
609e36d3 | 3483 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
6aa8b732 AK |
3484 | break; |
3485 | case MSR_IA32_SYSENTER_EIP: | |
609e36d3 | 3486 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
6aa8b732 AK |
3487 | break; |
3488 | case MSR_IA32_SYSENTER_ESP: | |
609e36d3 | 3489 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
6aa8b732 | 3490 | break; |
0dd376e7 | 3491 | case MSR_IA32_BNDCFGS: |
691bd434 | 3492 | if (!kvm_mpx_supported() || |
d6321d49 RK |
3493 | (!msr_info->host_initiated && |
3494 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) | |
93c4adc7 | 3495 | return 1; |
609e36d3 | 3496 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
0dd376e7 | 3497 | break; |
c45dcc71 AR |
3498 | case MSR_IA32_MCG_EXT_CTL: |
3499 | if (!msr_info->host_initiated && | |
3500 | !(to_vmx(vcpu)->msr_ia32_feature_control & | |
3501 | FEATURE_CONTROL_LMCE)) | |
cae50139 | 3502 | return 1; |
c45dcc71 AR |
3503 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
3504 | break; | |
cae50139 | 3505 | case MSR_IA32_FEATURE_CONTROL: |
3b84080b | 3506 | msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control; |
cae50139 JK |
3507 | break; |
3508 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
3509 | if (!nested_vmx_allowed(vcpu)) | |
3510 | return 1; | |
609e36d3 | 3511 | return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data); |
20300099 | 3512 | case MSR_IA32_XSS: |
47ee4fb1 WL |
3513 | if (!vmx_xsaves_supported() || |
3514 | (!msr_info->host_initiated && | |
3515 | !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && | |
3516 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)))) | |
20300099 | 3517 | return 1; |
609e36d3 | 3518 | msr_info->data = vcpu->arch.ia32_xss; |
20300099 | 3519 | break; |
4e47c7a6 | 3520 | case MSR_TSC_AUX: |
d6321d49 RK |
3521 | if (!msr_info->host_initiated && |
3522 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) | |
4e47c7a6 SY |
3523 | return 1; |
3524 | /* Otherwise falls through */ | |
6aa8b732 | 3525 | default: |
609e36d3 | 3526 | msr = find_msr_entry(to_vmx(vcpu), msr_info->index); |
3bab1f5d | 3527 | if (msr) { |
609e36d3 | 3528 | msr_info->data = msr->data; |
3bab1f5d | 3529 | break; |
6aa8b732 | 3530 | } |
609e36d3 | 3531 | return kvm_get_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3532 | } |
3533 | ||
6aa8b732 AK |
3534 | return 0; |
3535 | } | |
3536 | ||
cae50139 JK |
3537 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
3538 | ||
6aa8b732 AK |
3539 | /* |
3540 | * Writes msr value into into the appropriate "register". | |
3541 | * Returns 0 on success, non-0 otherwise. | |
3542 | * Assumes vcpu_load() was already called. | |
3543 | */ | |
8fe8ab46 | 3544 | static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
6aa8b732 | 3545 | { |
a2fa3e9f | 3546 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
26bb0981 | 3547 | struct shared_msr_entry *msr; |
2cc51560 | 3548 | int ret = 0; |
8fe8ab46 WA |
3549 | u32 msr_index = msr_info->index; |
3550 | u64 data = msr_info->data; | |
2cc51560 | 3551 | |
6aa8b732 | 3552 | switch (msr_index) { |
3bab1f5d | 3553 | case MSR_EFER: |
8fe8ab46 | 3554 | ret = kvm_set_msr_common(vcpu, msr_info); |
2cc51560 | 3555 | break; |
16175a79 | 3556 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3557 | case MSR_FS_BASE: |
2fb92db1 | 3558 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3559 | vmcs_writel(GUEST_FS_BASE, data); |
3560 | break; | |
3561 | case MSR_GS_BASE: | |
2fb92db1 | 3562 | vmx_segment_cache_clear(vmx); |
6aa8b732 AK |
3563 | vmcs_writel(GUEST_GS_BASE, data); |
3564 | break; | |
44ea2b17 AK |
3565 | case MSR_KERNEL_GS_BASE: |
3566 | vmx_load_host_state(vmx); | |
3567 | vmx->msr_guest_kernel_gs_base = data; | |
3568 | break; | |
6aa8b732 AK |
3569 | #endif |
3570 | case MSR_IA32_SYSENTER_CS: | |
3571 | vmcs_write32(GUEST_SYSENTER_CS, data); | |
3572 | break; | |
3573 | case MSR_IA32_SYSENTER_EIP: | |
f5b42c33 | 3574 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
6aa8b732 AK |
3575 | break; |
3576 | case MSR_IA32_SYSENTER_ESP: | |
f5b42c33 | 3577 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
6aa8b732 | 3578 | break; |
0dd376e7 | 3579 | case MSR_IA32_BNDCFGS: |
691bd434 | 3580 | if (!kvm_mpx_supported() || |
d6321d49 RK |
3581 | (!msr_info->host_initiated && |
3582 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) | |
93c4adc7 | 3583 | return 1; |
fd8cb433 | 3584 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
4531662d | 3585 | (data & MSR_IA32_BNDCFGS_RSVD)) |
93c4adc7 | 3586 | return 1; |
0dd376e7 LJ |
3587 | vmcs_write64(GUEST_BNDCFGS, data); |
3588 | break; | |
74469996 KA |
3589 | case MSR_IA32_SPEC_CTRL: |
3590 | if (!msr_info->host_initiated && | |
5856293c | 3591 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
74469996 KA |
3592 | return 1; |
3593 | ||
3594 | /* The STIBP bit doesn't fault even if it's not advertised */ | |
8fe36c9d | 3595 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
74469996 KA |
3596 | return 1; |
3597 | ||
3598 | vmx->spec_ctrl = data; | |
3599 | ||
3600 | if (!data) | |
3601 | break; | |
3602 | ||
3603 | /* | |
3604 | * For non-nested: | |
3605 | * When it's written (to non-zero) for the first time, pass | |
3606 | * it through. | |
3607 | * | |
3608 | * For nested: | |
3609 | * The handling of the MSR bitmap for L2 guests is done in | |
3610 | * nested_vmx_merge_msr_bitmap. We should not touch the | |
3611 | * vmcs02.msr_bitmap here since it gets completely overwritten | |
3612 | * in the merging. We update the vmcs01 here for L1 as well | |
3613 | * since it will end up touching the MSR anyway now. | |
3614 | */ | |
3615 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, | |
3616 | MSR_IA32_SPEC_CTRL, | |
3617 | MSR_TYPE_RW); | |
3618 | break; | |
33241bfe AR |
3619 | case MSR_IA32_PRED_CMD: |
3620 | if (!msr_info->host_initiated && | |
33241bfe AR |
3621 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
3622 | return 1; | |
3623 | ||
3624 | if (data & ~PRED_CMD_IBPB) | |
3625 | return 1; | |
3626 | ||
3627 | if (!data) | |
3628 | break; | |
3629 | ||
3630 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); | |
3631 | ||
3632 | /* | |
3633 | * For non-nested: | |
3634 | * When it's written (to non-zero) for the first time, pass | |
3635 | * it through. | |
3636 | * | |
3637 | * For nested: | |
3638 | * The handling of the MSR bitmap for L2 guests is done in | |
3639 | * nested_vmx_merge_msr_bitmap. We should not touch the | |
3640 | * vmcs02.msr_bitmap here since it gets completely overwritten | |
3641 | * in the merging. | |
3642 | */ | |
3643 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, | |
3644 | MSR_TYPE_W); | |
3645 | break; | |
468d472f | 3646 | case MSR_IA32_CR_PAT: |
9a5c34a2 SC |
3647 | if (!kvm_pat_valid(data)) |
3648 | return 1; | |
3649 | ||
468d472f SY |
3650 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
3651 | vmcs_write64(GUEST_IA32_PAT, data); | |
3652 | vcpu->arch.pat = data; | |
3653 | break; | |
3654 | } | |
8fe8ab46 | 3655 | ret = kvm_set_msr_common(vcpu, msr_info); |
4e47c7a6 | 3656 | break; |
ba904635 WA |
3657 | case MSR_IA32_TSC_ADJUST: |
3658 | ret = kvm_set_msr_common(vcpu, msr_info); | |
4e47c7a6 | 3659 | break; |
c45dcc71 AR |
3660 | case MSR_IA32_MCG_EXT_CTL: |
3661 | if ((!msr_info->host_initiated && | |
3662 | !(to_vmx(vcpu)->msr_ia32_feature_control & | |
3663 | FEATURE_CONTROL_LMCE)) || | |
3664 | (data & ~MCG_EXT_CTL_LMCE_EN)) | |
3665 | return 1; | |
3666 | vcpu->arch.mcg_ext_ctl = data; | |
3667 | break; | |
cae50139 | 3668 | case MSR_IA32_FEATURE_CONTROL: |
37e4c997 | 3669 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
3b84080b | 3670 | (to_vmx(vcpu)->msr_ia32_feature_control & |
cae50139 JK |
3671 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
3672 | return 1; | |
3b84080b | 3673 | vmx->msr_ia32_feature_control = data; |
cae50139 JK |
3674 | if (msr_info->host_initiated && data == 0) |
3675 | vmx_leave_nested(vcpu); | |
3676 | break; | |
3677 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: | |
62cc6b9d DM |
3678 | if (!msr_info->host_initiated) |
3679 | return 1; /* they are read-only */ | |
3680 | if (!nested_vmx_allowed(vcpu)) | |
3681 | return 1; | |
3682 | return vmx_set_vmx_msr(vcpu, msr_index, data); | |
20300099 | 3683 | case MSR_IA32_XSS: |
47ee4fb1 WL |
3684 | if (!vmx_xsaves_supported() || |
3685 | (!msr_info->host_initiated && | |
3686 | !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && | |
3687 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)))) | |
20300099 WL |
3688 | return 1; |
3689 | /* | |
3690 | * The only supported bit as of Skylake is bit 8, but | |
3691 | * it is not supported on KVM. | |
3692 | */ | |
3693 | if (data != 0) | |
3694 | return 1; | |
3695 | vcpu->arch.ia32_xss = data; | |
3696 | if (vcpu->arch.ia32_xss != host_xss) | |
3697 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, | |
129ce7ac | 3698 | vcpu->arch.ia32_xss, host_xss, false); |
20300099 WL |
3699 | else |
3700 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); | |
3701 | break; | |
4e47c7a6 | 3702 | case MSR_TSC_AUX: |
d6321d49 RK |
3703 | if (!msr_info->host_initiated && |
3704 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) | |
4e47c7a6 SY |
3705 | return 1; |
3706 | /* Check reserved bit, higher 32 bits should be zero */ | |
3707 | if ((data >> 32) != 0) | |
3708 | return 1; | |
3709 | /* Otherwise falls through */ | |
6aa8b732 | 3710 | default: |
8b9cf98c | 3711 | msr = find_msr_entry(vmx, msr_index); |
3bab1f5d | 3712 | if (msr) { |
8b3c3104 | 3713 | u64 old_msr_data = msr->data; |
3bab1f5d | 3714 | msr->data = data; |
2225fd56 AK |
3715 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
3716 | preempt_disable(); | |
8b3c3104 AH |
3717 | ret = kvm_set_shared_msr(msr->index, msr->data, |
3718 | msr->mask); | |
2225fd56 | 3719 | preempt_enable(); |
8b3c3104 AH |
3720 | if (ret) |
3721 | msr->data = old_msr_data; | |
2225fd56 | 3722 | } |
3bab1f5d | 3723 | break; |
6aa8b732 | 3724 | } |
8fe8ab46 | 3725 | ret = kvm_set_msr_common(vcpu, msr_info); |
6aa8b732 AK |
3726 | } |
3727 | ||
2cc51560 | 3728 | return ret; |
6aa8b732 AK |
3729 | } |
3730 | ||
5fdbf976 | 3731 | static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
6aa8b732 | 3732 | { |
5fdbf976 MT |
3733 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
3734 | switch (reg) { | |
3735 | case VCPU_REGS_RSP: | |
3736 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); | |
3737 | break; | |
3738 | case VCPU_REGS_RIP: | |
3739 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); | |
3740 | break; | |
6de4f3ad AK |
3741 | case VCPU_EXREG_PDPTR: |
3742 | if (enable_ept) | |
3743 | ept_save_pdptrs(vcpu); | |
3744 | break; | |
5fdbf976 MT |
3745 | default: |
3746 | break; | |
3747 | } | |
6aa8b732 AK |
3748 | } |
3749 | ||
6aa8b732 AK |
3750 | static __init int cpu_has_kvm_support(void) |
3751 | { | |
6210e37b | 3752 | return cpu_has_vmx(); |
6aa8b732 AK |
3753 | } |
3754 | ||
3755 | static __init int vmx_disabled_by_bios(void) | |
3756 | { | |
3757 | u64 msr; | |
3758 | ||
3759 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); | |
cafd6659 | 3760 | if (msr & FEATURE_CONTROL_LOCKED) { |
23f3e991 | 3761 | /* launched w/ TXT and VMX disabled */ |
cafd6659 SW |
3762 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
3763 | && tboot_enabled()) | |
3764 | return 1; | |
23f3e991 | 3765 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
cafd6659 | 3766 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
23f3e991 | 3767 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
f9335afe SW |
3768 | && !tboot_enabled()) { |
3769 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " | |
23f3e991 | 3770 | "activate TXT before enabling KVM\n"); |
cafd6659 | 3771 | return 1; |
f9335afe | 3772 | } |
23f3e991 JC |
3773 | /* launched w/o TXT and VMX disabled */ |
3774 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) | |
3775 | && !tboot_enabled()) | |
3776 | return 1; | |
cafd6659 SW |
3777 | } |
3778 | ||
3779 | return 0; | |
6aa8b732 AK |
3780 | } |
3781 | ||
7725b894 DX |
3782 | static void kvm_cpu_vmxon(u64 addr) |
3783 | { | |
fe0e80be | 3784 | cr4_set_bits(X86_CR4_VMXE); |
1c5ac21a AS |
3785 | intel_pt_handle_vmx(1); |
3786 | ||
7725b894 DX |
3787 | asm volatile (ASM_VMX_VMXON_RAX |
3788 | : : "a"(&addr), "m"(addr) | |
3789 | : "memory", "cc"); | |
3790 | } | |
3791 | ||
13a34e06 | 3792 | static int hardware_enable(void) |
6aa8b732 AK |
3793 | { |
3794 | int cpu = raw_smp_processor_id(); | |
3795 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); | |
cafd6659 | 3796 | u64 old, test_bits; |
6aa8b732 | 3797 | |
1e02ce4c | 3798 | if (cr4_read_shadow() & X86_CR4_VMXE) |
10474ae8 AG |
3799 | return -EBUSY; |
3800 | ||
d462b819 | 3801 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
bf9f6ac8 FW |
3802 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
3803 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
8f536b76 ZY |
3804 | |
3805 | /* | |
3806 | * Now we can enable the vmclear operation in kdump | |
3807 | * since the loaded_vmcss_on_cpu list on this cpu | |
3808 | * has been initialized. | |
3809 | * | |
3810 | * Though the cpu is not in VMX operation now, there | |
3811 | * is no problem to enable the vmclear operation | |
3812 | * for the loaded_vmcss_on_cpu list is empty! | |
3813 | */ | |
3814 | crash_enable_local_vmclear(cpu); | |
3815 | ||
6aa8b732 | 3816 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
cafd6659 SW |
3817 | |
3818 | test_bits = FEATURE_CONTROL_LOCKED; | |
3819 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
3820 | if (tboot_enabled()) | |
3821 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; | |
3822 | ||
3823 | if ((old & test_bits) != test_bits) { | |
6aa8b732 | 3824 | /* enable and lock */ |
cafd6659 SW |
3825 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
3826 | } | |
fe0e80be | 3827 | kvm_cpu_vmxon(phys_addr); |
fdf288bf DH |
3828 | if (enable_ept) |
3829 | ept_sync_global(); | |
10474ae8 AG |
3830 | |
3831 | return 0; | |
6aa8b732 AK |
3832 | } |
3833 | ||
d462b819 | 3834 | static void vmclear_local_loaded_vmcss(void) |
543e4243 AK |
3835 | { |
3836 | int cpu = raw_smp_processor_id(); | |
d462b819 | 3837 | struct loaded_vmcs *v, *n; |
543e4243 | 3838 | |
d462b819 NHE |
3839 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
3840 | loaded_vmcss_on_cpu_link) | |
3841 | __loaded_vmcs_clear(v); | |
543e4243 AK |
3842 | } |
3843 | ||
710ff4a8 EH |
3844 | |
3845 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() | |
3846 | * tricks. | |
3847 | */ | |
3848 | static void kvm_cpu_vmxoff(void) | |
6aa8b732 | 3849 | { |
4ecac3fd | 3850 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
1c5ac21a AS |
3851 | |
3852 | intel_pt_handle_vmx(0); | |
fe0e80be | 3853 | cr4_clear_bits(X86_CR4_VMXE); |
6aa8b732 AK |
3854 | } |
3855 | ||
13a34e06 | 3856 | static void hardware_disable(void) |
710ff4a8 | 3857 | { |
fe0e80be DH |
3858 | vmclear_local_loaded_vmcss(); |
3859 | kvm_cpu_vmxoff(); | |
710ff4a8 EH |
3860 | } |
3861 | ||
1c3d14fe | 3862 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
d77c26fc | 3863 | u32 msr, u32 *result) |
1c3d14fe YS |
3864 | { |
3865 | u32 vmx_msr_low, vmx_msr_high; | |
3866 | u32 ctl = ctl_min | ctl_opt; | |
3867 | ||
3868 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3869 | ||
3870 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ | |
3871 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ | |
3872 | ||
3873 | /* Ensure minimum (required) set of control bits are supported. */ | |
3874 | if (ctl_min & ~ctl) | |
002c7f7c | 3875 | return -EIO; |
1c3d14fe YS |
3876 | |
3877 | *result = ctl; | |
3878 | return 0; | |
3879 | } | |
3880 | ||
110312c8 AK |
3881 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
3882 | { | |
3883 | u32 vmx_msr_low, vmx_msr_high; | |
3884 | ||
3885 | rdmsr(msr, vmx_msr_low, vmx_msr_high); | |
3886 | return vmx_msr_high & ctl; | |
3887 | } | |
3888 | ||
002c7f7c | 3889 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
6aa8b732 AK |
3890 | { |
3891 | u32 vmx_msr_low, vmx_msr_high; | |
d56f546d | 3892 | u32 min, opt, min2, opt2; |
1c3d14fe YS |
3893 | u32 _pin_based_exec_control = 0; |
3894 | u32 _cpu_based_exec_control = 0; | |
f78e0e2e | 3895 | u32 _cpu_based_2nd_exec_control = 0; |
1c3d14fe YS |
3896 | u32 _vmexit_control = 0; |
3897 | u32 _vmentry_control = 0; | |
3898 | ||
10166744 | 3899 | min = CPU_BASED_HLT_EXITING | |
1c3d14fe YS |
3900 | #ifdef CONFIG_X86_64 |
3901 | CPU_BASED_CR8_LOAD_EXITING | | |
3902 | CPU_BASED_CR8_STORE_EXITING | | |
3903 | #endif | |
d56f546d SY |
3904 | CPU_BASED_CR3_LOAD_EXITING | |
3905 | CPU_BASED_CR3_STORE_EXITING | | |
1c3d14fe YS |
3906 | CPU_BASED_USE_IO_BITMAPS | |
3907 | CPU_BASED_MOV_DR_EXITING | | |
a7052897 | 3908 | CPU_BASED_USE_TSC_OFFSETING | |
fee84b07 AK |
3909 | CPU_BASED_INVLPG_EXITING | |
3910 | CPU_BASED_RDPMC_EXITING; | |
443381a8 | 3911 | |
668fffa3 MT |
3912 | if (!kvm_mwait_in_guest()) |
3913 | min |= CPU_BASED_MWAIT_EXITING | | |
3914 | CPU_BASED_MONITOR_EXITING; | |
3915 | ||
f78e0e2e | 3916 | opt = CPU_BASED_TPR_SHADOW | |
25c5f225 | 3917 | CPU_BASED_USE_MSR_BITMAPS | |
f78e0e2e | 3918 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
1c3d14fe YS |
3919 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
3920 | &_cpu_based_exec_control) < 0) | |
002c7f7c | 3921 | return -EIO; |
6e5d865c YS |
3922 | #ifdef CONFIG_X86_64 |
3923 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
3924 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & | |
3925 | ~CPU_BASED_CR8_STORE_EXITING; | |
3926 | #endif | |
f78e0e2e | 3927 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
d56f546d SY |
3928 | min2 = 0; |
3929 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | | |
8d14695f | 3930 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
2384d2b3 | 3931 | SECONDARY_EXEC_WBINVD_EXITING | |
d56f546d | 3932 | SECONDARY_EXEC_ENABLE_VPID | |
3a624e29 | 3933 | SECONDARY_EXEC_ENABLE_EPT | |
4b8d54f9 | 3934 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
4e47c7a6 | 3935 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
ad756a16 | 3936 | SECONDARY_EXEC_RDTSCP | |
83d4c286 | 3937 | SECONDARY_EXEC_ENABLE_INVPCID | |
c7c9c56c | 3938 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
abc4fc58 | 3939 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
20300099 | 3940 | SECONDARY_EXEC_SHADOW_VMCS | |
843e4330 | 3941 | SECONDARY_EXEC_XSAVES | |
736fdf72 DH |
3942 | SECONDARY_EXEC_RDSEED_EXITING | |
3943 | SECONDARY_EXEC_RDRAND_EXITING | | |
8b3e34e4 | 3944 | SECONDARY_EXEC_ENABLE_PML | |
2a499e49 BD |
3945 | SECONDARY_EXEC_TSC_SCALING | |
3946 | SECONDARY_EXEC_ENABLE_VMFUNC; | |
d56f546d SY |
3947 | if (adjust_vmx_controls(min2, opt2, |
3948 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
f78e0e2e SY |
3949 | &_cpu_based_2nd_exec_control) < 0) |
3950 | return -EIO; | |
3951 | } | |
3952 | #ifndef CONFIG_X86_64 | |
3953 | if (!(_cpu_based_2nd_exec_control & | |
3954 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
3955 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; | |
3956 | #endif | |
83d4c286 YZ |
3957 | |
3958 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) | |
3959 | _cpu_based_2nd_exec_control &= ~( | |
8d14695f | 3960 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
c7c9c56c YZ |
3961 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
3962 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
83d4c286 | 3963 | |
61f1dd90 WL |
3964 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
3965 | &vmx_capability.ept, &vmx_capability.vpid); | |
3966 | ||
d56f546d | 3967 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
a7052897 MT |
3968 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
3969 | enabled */ | |
5fff7d27 GN |
3970 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
3971 | CPU_BASED_CR3_STORE_EXITING | | |
3972 | CPU_BASED_INVLPG_EXITING); | |
61f1dd90 WL |
3973 | } else if (vmx_capability.ept) { |
3974 | vmx_capability.ept = 0; | |
3975 | pr_warn_once("EPT CAP should not exist if not support " | |
3976 | "1-setting enable EPT VM-execution control\n"); | |
3977 | } | |
3978 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && | |
3979 | vmx_capability.vpid) { | |
3980 | vmx_capability.vpid = 0; | |
3981 | pr_warn_once("VPID CAP should not exist if not support " | |
3982 | "1-setting enable VPID VM-execution control\n"); | |
d56f546d | 3983 | } |
1c3d14fe | 3984 | |
91fa0f8e | 3985 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
1c3d14fe YS |
3986 | #ifdef CONFIG_X86_64 |
3987 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; | |
3988 | #endif | |
a547c6db | 3989 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
91fa0f8e | 3990 | VM_EXIT_CLEAR_BNDCFGS; |
1c3d14fe YS |
3991 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
3992 | &_vmexit_control) < 0) | |
002c7f7c | 3993 | return -EIO; |
1c3d14fe | 3994 | |
8a1b4392 PB |
3995 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
3996 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | | |
3997 | PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
3998 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
3999 | &_pin_based_exec_control) < 0) | |
4000 | return -EIO; | |
4001 | ||
1c17c3e6 PB |
4002 | if (cpu_has_broken_vmx_preemption_timer()) |
4003 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be | 4004 | if (!(_cpu_based_2nd_exec_control & |
91fa0f8e | 4005 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
01e439be YZ |
4006 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
4007 | ||
c845f9c6 | 4008 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
da8999d3 | 4009 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
1c3d14fe YS |
4010 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
4011 | &_vmentry_control) < 0) | |
002c7f7c | 4012 | return -EIO; |
6aa8b732 | 4013 | |
c68876fd | 4014 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
1c3d14fe YS |
4015 | |
4016 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ | |
4017 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) | |
002c7f7c | 4018 | return -EIO; |
1c3d14fe YS |
4019 | |
4020 | #ifdef CONFIG_X86_64 | |
4021 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ | |
4022 | if (vmx_msr_high & (1u<<16)) | |
002c7f7c | 4023 | return -EIO; |
1c3d14fe YS |
4024 | #endif |
4025 | ||
4026 | /* Require Write-Back (WB) memory type for VMCS accesses. */ | |
4027 | if (((vmx_msr_high >> 18) & 15) != 6) | |
002c7f7c | 4028 | return -EIO; |
1c3d14fe | 4029 | |
002c7f7c | 4030 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
16cb0255 | 4031 | vmcs_conf->order = get_order(vmcs_conf->size); |
9ac7e3e8 | 4032 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
002c7f7c | 4033 | vmcs_conf->revision_id = vmx_msr_low; |
1c3d14fe | 4034 | |
002c7f7c YS |
4035 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
4036 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; | |
f78e0e2e | 4037 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
002c7f7c YS |
4038 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
4039 | vmcs_conf->vmentry_ctrl = _vmentry_control; | |
1c3d14fe | 4040 | |
110312c8 AK |
4041 | cpu_has_load_ia32_efer = |
4042 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
4043 | VM_ENTRY_LOAD_IA32_EFER) | |
4044 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
4045 | VM_EXIT_LOAD_IA32_EFER); | |
4046 | ||
8bf00a52 GN |
4047 | cpu_has_load_perf_global_ctrl = |
4048 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, | |
4049 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
4050 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, | |
4051 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); | |
4052 | ||
4053 | /* | |
4054 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL | |
bb3541f1 | 4055 | * but due to errata below it can't be used. Workaround is to use |
8bf00a52 GN |
4056 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
4057 | * | |
4058 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] | |
4059 | * | |
4060 | * AAK155 (model 26) | |
4061 | * AAP115 (model 30) | |
4062 | * AAT100 (model 37) | |
4063 | * BC86,AAY89,BD102 (model 44) | |
4064 | * BA97 (model 46) | |
4065 | * | |
4066 | */ | |
4067 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { | |
4068 | switch (boot_cpu_data.x86_model) { | |
4069 | case 26: | |
4070 | case 30: | |
4071 | case 37: | |
4072 | case 44: | |
4073 | case 46: | |
4074 | cpu_has_load_perf_global_ctrl = false; | |
4075 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " | |
4076 | "does not work properly. Using workaround\n"); | |
4077 | break; | |
4078 | default: | |
4079 | break; | |
4080 | } | |
4081 | } | |
4082 | ||
782511b0 | 4083 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
20300099 WL |
4084 | rdmsrl(MSR_IA32_XSS, host_xss); |
4085 | ||
1c3d14fe | 4086 | return 0; |
c68876fd | 4087 | } |
6aa8b732 AK |
4088 | |
4089 | static struct vmcs *alloc_vmcs_cpu(int cpu) | |
4090 | { | |
4091 | int node = cpu_to_node(cpu); | |
4092 | struct page *pages; | |
4093 | struct vmcs *vmcs; | |
4094 | ||
96db800f | 4095 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
6aa8b732 AK |
4096 | if (!pages) |
4097 | return NULL; | |
4098 | vmcs = page_address(pages); | |
1c3d14fe YS |
4099 | memset(vmcs, 0, vmcs_config.size); |
4100 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ | |
6aa8b732 AK |
4101 | return vmcs; |
4102 | } | |
4103 | ||
6aa8b732 AK |
4104 | static void free_vmcs(struct vmcs *vmcs) |
4105 | { | |
1c3d14fe | 4106 | free_pages((unsigned long)vmcs, vmcs_config.order); |
6aa8b732 AK |
4107 | } |
4108 | ||
d462b819 NHE |
4109 | /* |
4110 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded | |
4111 | */ | |
4112 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) | |
4113 | { | |
4114 | if (!loaded_vmcs->vmcs) | |
4115 | return; | |
4116 | loaded_vmcs_clear(loaded_vmcs); | |
4117 | free_vmcs(loaded_vmcs->vmcs); | |
4118 | loaded_vmcs->vmcs = NULL; | |
4b0be90f PB |
4119 | if (loaded_vmcs->msr_bitmap) |
4120 | free_page((unsigned long)loaded_vmcs->msr_bitmap); | |
355f4fb1 | 4121 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
d462b819 NHE |
4122 | } |
4123 | ||
b6d7026d PB |
4124 | static struct vmcs *alloc_vmcs(void) |
4125 | { | |
4126 | return alloc_vmcs_cpu(raw_smp_processor_id()); | |
4127 | } | |
4128 | ||
4129 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) | |
4130 | { | |
4131 | loaded_vmcs->vmcs = alloc_vmcs(); | |
4132 | if (!loaded_vmcs->vmcs) | |
4133 | return -ENOMEM; | |
4134 | ||
4135 | loaded_vmcs->shadow_vmcs = NULL; | |
4136 | loaded_vmcs_init(loaded_vmcs); | |
4b0be90f PB |
4137 | |
4138 | if (cpu_has_vmx_msr_bitmap()) { | |
4139 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); | |
4140 | if (!loaded_vmcs->msr_bitmap) | |
4141 | goto out_vmcs; | |
4142 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); | |
4143 | } | |
b6d7026d | 4144 | return 0; |
4b0be90f PB |
4145 | |
4146 | out_vmcs: | |
4147 | free_loaded_vmcs(loaded_vmcs); | |
4148 | return -ENOMEM; | |
b6d7026d PB |
4149 | } |
4150 | ||
39959588 | 4151 | static void free_kvm_area(void) |
6aa8b732 AK |
4152 | { |
4153 | int cpu; | |
4154 | ||
3230bb47 | 4155 | for_each_possible_cpu(cpu) { |
6aa8b732 | 4156 | free_vmcs(per_cpu(vmxarea, cpu)); |
3230bb47 ZA |
4157 | per_cpu(vmxarea, cpu) = NULL; |
4158 | } | |
6aa8b732 AK |
4159 | } |
4160 | ||
85fd514e JM |
4161 | enum vmcs_field_type { |
4162 | VMCS_FIELD_TYPE_U16 = 0, | |
4163 | VMCS_FIELD_TYPE_U64 = 1, | |
4164 | VMCS_FIELD_TYPE_U32 = 2, | |
4165 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 | |
4166 | }; | |
4167 | ||
4168 | static inline int vmcs_field_type(unsigned long field) | |
4169 | { | |
4170 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ | |
4171 | return VMCS_FIELD_TYPE_U32; | |
4172 | return (field >> 13) & 0x3 ; | |
4173 | } | |
4174 | ||
4175 | static inline int vmcs_field_readonly(unsigned long field) | |
4176 | { | |
4177 | return (((field >> 10) & 0x3) == 1); | |
4178 | } | |
4179 | ||
fe2b201b BD |
4180 | static void init_vmcs_shadow_fields(void) |
4181 | { | |
4182 | int i, j; | |
4183 | ||
4184 | /* No checks for read only fields yet */ | |
4185 | ||
4186 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { | |
4187 | switch (shadow_read_write_fields[i]) { | |
4188 | case GUEST_BNDCFGS: | |
a87036ad | 4189 | if (!kvm_mpx_supported()) |
fe2b201b BD |
4190 | continue; |
4191 | break; | |
4192 | default: | |
4193 | break; | |
4194 | } | |
4195 | ||
4196 | if (j < i) | |
4197 | shadow_read_write_fields[j] = | |
4198 | shadow_read_write_fields[i]; | |
4199 | j++; | |
4200 | } | |
4201 | max_shadow_read_write_fields = j; | |
4202 | ||
4203 | /* shadowed fields guest access without vmexit */ | |
4204 | for (i = 0; i < max_shadow_read_write_fields; i++) { | |
85fd514e JM |
4205 | unsigned long field = shadow_read_write_fields[i]; |
4206 | ||
4207 | clear_bit(field, vmx_vmwrite_bitmap); | |
4208 | clear_bit(field, vmx_vmread_bitmap); | |
4209 | if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) { | |
4210 | clear_bit(field + 1, vmx_vmwrite_bitmap); | |
4211 | clear_bit(field + 1, vmx_vmread_bitmap); | |
4212 | } | |
4213 | } | |
4214 | for (i = 0; i < max_shadow_read_only_fields; i++) { | |
4215 | unsigned long field = shadow_read_only_fields[i]; | |
4216 | ||
4217 | clear_bit(field, vmx_vmread_bitmap); | |
4218 | if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) | |
4219 | clear_bit(field + 1, vmx_vmread_bitmap); | |
fe2b201b | 4220 | } |
fe2b201b BD |
4221 | } |
4222 | ||
6aa8b732 AK |
4223 | static __init int alloc_kvm_area(void) |
4224 | { | |
4225 | int cpu; | |
4226 | ||
3230bb47 | 4227 | for_each_possible_cpu(cpu) { |
6aa8b732 AK |
4228 | struct vmcs *vmcs; |
4229 | ||
4230 | vmcs = alloc_vmcs_cpu(cpu); | |
4231 | if (!vmcs) { | |
4232 | free_kvm_area(); | |
4233 | return -ENOMEM; | |
4234 | } | |
4235 | ||
4236 | per_cpu(vmxarea, cpu) = vmcs; | |
4237 | } | |
4238 | return 0; | |
4239 | } | |
4240 | ||
91b0aa2c | 4241 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
d99e4152 | 4242 | struct kvm_segment *save) |
6aa8b732 | 4243 | { |
d99e4152 GN |
4244 | if (!emulate_invalid_guest_state) { |
4245 | /* | |
4246 | * CS and SS RPL should be equal during guest entry according | |
4247 | * to VMX spec, but in reality it is not always so. Since vcpu | |
4248 | * is in the middle of the transition from real mode to | |
4249 | * protected mode it is safe to assume that RPL 0 is a good | |
4250 | * default value. | |
4251 | */ | |
4252 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) | |
b32a9918 NA |
4253 | save->selector &= ~SEGMENT_RPL_MASK; |
4254 | save->dpl = save->selector & SEGMENT_RPL_MASK; | |
d99e4152 | 4255 | save->s = 1; |
6aa8b732 | 4256 | } |
d99e4152 | 4257 | vmx_set_segment(vcpu, save, seg); |
6aa8b732 AK |
4258 | } |
4259 | ||
4260 | static void enter_pmode(struct kvm_vcpu *vcpu) | |
4261 | { | |
4262 | unsigned long flags; | |
a89a8fb9 | 4263 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 | 4264 | |
d99e4152 GN |
4265 | /* |
4266 | * Update real mode segment cache. It may be not up-to-date if sement | |
4267 | * register was written while vcpu was in a guest mode. | |
4268 | */ | |
4269 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
4270 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
4271 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
4272 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
4273 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); | |
4274 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
4275 | ||
7ffd92c5 | 4276 | vmx->rmode.vm86_active = 0; |
6aa8b732 | 4277 | |
2fb92db1 AK |
4278 | vmx_segment_cache_clear(vmx); |
4279 | ||
f5f7b2fe | 4280 | vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
6aa8b732 AK |
4281 | |
4282 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 AK |
4283 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
4284 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; | |
6aa8b732 AK |
4285 | vmcs_writel(GUEST_RFLAGS, flags); |
4286 | ||
66aee91a RR |
4287 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
4288 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); | |
6aa8b732 AK |
4289 | |
4290 | update_exception_bitmap(vcpu); | |
4291 | ||
91b0aa2c GN |
4292 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
4293 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); | |
4294 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
4295 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
4296 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
4297 | fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
6aa8b732 AK |
4298 | } |
4299 | ||
f5f7b2fe | 4300 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
6aa8b732 | 4301 | { |
772e0318 | 4302 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
d99e4152 GN |
4303 | struct kvm_segment var = *save; |
4304 | ||
4305 | var.dpl = 0x3; | |
4306 | if (seg == VCPU_SREG_CS) | |
4307 | var.type = 0x3; | |
4308 | ||
4309 | if (!emulate_invalid_guest_state) { | |
4310 | var.selector = var.base >> 4; | |
4311 | var.base = var.base & 0xffff0; | |
4312 | var.limit = 0xffff; | |
4313 | var.g = 0; | |
4314 | var.db = 0; | |
4315 | var.present = 1; | |
4316 | var.s = 1; | |
4317 | var.l = 0; | |
4318 | var.unusable = 0; | |
4319 | var.type = 0x3; | |
4320 | var.avl = 0; | |
4321 | if (save->base & 0xf) | |
4322 | printk_once(KERN_WARNING "kvm: segment base is not " | |
4323 | "paragraph aligned when entering " | |
4324 | "protected mode (seg=%d)", seg); | |
4325 | } | |
6aa8b732 | 4326 | |
d99e4152 | 4327 | vmcs_write16(sf->selector, var.selector); |
96794e4e | 4328 | vmcs_writel(sf->base, var.base); |
d99e4152 GN |
4329 | vmcs_write32(sf->limit, var.limit); |
4330 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); | |
6aa8b732 AK |
4331 | } |
4332 | ||
4333 | static void enter_rmode(struct kvm_vcpu *vcpu) | |
4334 | { | |
4335 | unsigned long flags; | |
a89a8fb9 | 4336 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 | 4337 | |
f5f7b2fe AK |
4338 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
4339 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); | |
4340 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); | |
4341 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); | |
4342 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); | |
c6ad1153 GN |
4343 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
4344 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); | |
f5f7b2fe | 4345 | |
7ffd92c5 | 4346 | vmx->rmode.vm86_active = 1; |
6aa8b732 | 4347 | |
776e58ea GN |
4348 | /* |
4349 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering | |
4918c6ca | 4350 | * vcpu. Warn the user that an update is overdue. |
776e58ea | 4351 | */ |
4918c6ca | 4352 | if (!vcpu->kvm->arch.tss_addr) |
776e58ea GN |
4353 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
4354 | "called before entering vcpu\n"); | |
776e58ea | 4355 | |
2fb92db1 AK |
4356 | vmx_segment_cache_clear(vmx); |
4357 | ||
4918c6ca | 4358 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
6aa8b732 | 4359 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
6aa8b732 AK |
4360 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
4361 | ||
4362 | flags = vmcs_readl(GUEST_RFLAGS); | |
78ac8b47 | 4363 | vmx->rmode.save_rflags = flags; |
6aa8b732 | 4364 | |
053de044 | 4365 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
6aa8b732 AK |
4366 | |
4367 | vmcs_writel(GUEST_RFLAGS, flags); | |
66aee91a | 4368 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
6aa8b732 AK |
4369 | update_exception_bitmap(vcpu); |
4370 | ||
d99e4152 GN |
4371 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
4372 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); | |
4373 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); | |
4374 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); | |
4375 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); | |
4376 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); | |
b246dd5d | 4377 | |
8668a3c4 | 4378 | kvm_mmu_reset_context(vcpu); |
6aa8b732 AK |
4379 | } |
4380 | ||
401d10de AS |
4381 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
4382 | { | |
4383 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
26bb0981 AK |
4384 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
4385 | ||
4386 | if (!msr) | |
4387 | return; | |
401d10de | 4388 | |
44ea2b17 AK |
4389 | /* |
4390 | * Force kernel_gs_base reloading before EFER changes, as control | |
4391 | * of this msr depends on is_long_mode(). | |
4392 | */ | |
4393 | vmx_load_host_state(to_vmx(vcpu)); | |
f6801dff | 4394 | vcpu->arch.efer = efer; |
401d10de | 4395 | if (efer & EFER_LMA) { |
2961e876 | 4396 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
4397 | msr->data = efer; |
4398 | } else { | |
2961e876 | 4399 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
401d10de AS |
4400 | |
4401 | msr->data = efer & ~EFER_LME; | |
4402 | } | |
4403 | setup_msrs(vmx); | |
4404 | } | |
4405 | ||
05b3e0c2 | 4406 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
4407 | |
4408 | static void enter_lmode(struct kvm_vcpu *vcpu) | |
4409 | { | |
4410 | u32 guest_tr_ar; | |
4411 | ||
2fb92db1 AK |
4412 | vmx_segment_cache_clear(to_vmx(vcpu)); |
4413 | ||
6aa8b732 | 4414 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
4d283ec9 | 4415 | if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) { |
bd80158a JK |
4416 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
4417 | __func__); | |
6aa8b732 | 4418 | vmcs_write32(GUEST_TR_AR_BYTES, |
4d283ec9 AL |
4419 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
4420 | | VMX_AR_TYPE_BUSY_64_TSS); | |
6aa8b732 | 4421 | } |
da38f438 | 4422 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
6aa8b732 AK |
4423 | } |
4424 | ||
4425 | static void exit_lmode(struct kvm_vcpu *vcpu) | |
4426 | { | |
2961e876 | 4427 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
da38f438 | 4428 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
6aa8b732 AK |
4429 | } |
4430 | ||
4431 | #endif | |
4432 | ||
6bf41e55 WL |
4433 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
4434 | bool invalidate_gpa) | |
2384d2b3 | 4435 | { |
6bf41e55 | 4436 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
dd180b3e XG |
4437 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
4438 | return; | |
995f00a6 | 4439 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
f0b98c02 JM |
4440 | } else { |
4441 | vpid_sync_context(vpid); | |
dd180b3e | 4442 | } |
2384d2b3 SY |
4443 | } |
4444 | ||
6bf41e55 | 4445 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
dd5f5341 | 4446 | { |
6bf41e55 | 4447 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
dd5f5341 WL |
4448 | } |
4449 | ||
e8467fda AK |
4450 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
4451 | { | |
4452 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; | |
4453 | ||
4454 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; | |
4455 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; | |
4456 | } | |
4457 | ||
aff48baa AK |
4458 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
4459 | { | |
4460 | if (enable_ept && is_paging(vcpu)) | |
4461 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); | |
4462 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
4463 | } | |
4464 | ||
25c4c276 | 4465 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
399badf3 | 4466 | { |
fc78f519 AK |
4467 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
4468 | ||
4469 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; | |
4470 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; | |
399badf3 AK |
4471 | } |
4472 | ||
1439442c SY |
4473 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
4474 | { | |
d0d538b9 GN |
4475 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4476 | ||
6de4f3ad AK |
4477 | if (!test_bit(VCPU_EXREG_PDPTR, |
4478 | (unsigned long *)&vcpu->arch.regs_dirty)) | |
4479 | return; | |
4480 | ||
f68adeae | 4481 | if (is_pae_paging(vcpu)) { |
d0d538b9 GN |
4482 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
4483 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); | |
4484 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); | |
4485 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); | |
1439442c SY |
4486 | } |
4487 | } | |
4488 | ||
8f5d549f AK |
4489 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
4490 | { | |
d0d538b9 GN |
4491 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
4492 | ||
f68adeae | 4493 | if (is_pae_paging(vcpu)) { |
d0d538b9 GN |
4494 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
4495 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); | |
4496 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); | |
4497 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); | |
8f5d549f | 4498 | } |
6de4f3ad AK |
4499 | |
4500 | __set_bit(VCPU_EXREG_PDPTR, | |
4501 | (unsigned long *)&vcpu->arch.regs_avail); | |
4502 | __set_bit(VCPU_EXREG_PDPTR, | |
4503 | (unsigned long *)&vcpu->arch.regs_dirty); | |
8f5d549f AK |
4504 | } |
4505 | ||
3899152c DM |
4506 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
4507 | { | |
4508 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; | |
4509 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; | |
4510 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
4511 | ||
4512 | if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high & | |
4513 | SECONDARY_EXEC_UNRESTRICTED_GUEST && | |
4514 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) | |
4515 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); | |
4516 | ||
4517 | return fixed_bits_valid(val, fixed0, fixed1); | |
4518 | } | |
4519 | ||
4520 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4521 | { | |
4522 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; | |
4523 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; | |
4524 | ||
4525 | return fixed_bits_valid(val, fixed0, fixed1); | |
4526 | } | |
4527 | ||
4528 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) | |
4529 | { | |
4530 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0; | |
4531 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1; | |
4532 | ||
4533 | return fixed_bits_valid(val, fixed0, fixed1); | |
4534 | } | |
4535 | ||
4536 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ | |
4537 | #define nested_guest_cr4_valid nested_cr4_valid | |
4538 | #define nested_host_cr4_valid nested_cr4_valid | |
4539 | ||
5e1746d6 | 4540 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
1439442c SY |
4541 | |
4542 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, | |
4543 | unsigned long cr0, | |
4544 | struct kvm_vcpu *vcpu) | |
4545 | { | |
5233dd51 MT |
4546 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
4547 | vmx_decache_cr3(vcpu); | |
1439442c SY |
4548 | if (!(cr0 & X86_CR0_PG)) { |
4549 | /* From paging/starting to nonpaging */ | |
4550 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4551 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
1439442c SY |
4552 | (CPU_BASED_CR3_LOAD_EXITING | |
4553 | CPU_BASED_CR3_STORE_EXITING)); | |
4554 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4555 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c SY |
4556 | } else if (!is_paging(vcpu)) { |
4557 | /* From nonpaging to paging */ | |
4558 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
65267ea1 | 4559 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
1439442c SY |
4560 | ~(CPU_BASED_CR3_LOAD_EXITING | |
4561 | CPU_BASED_CR3_STORE_EXITING)); | |
4562 | vcpu->arch.cr0 = cr0; | |
fc78f519 | 4563 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
1439442c | 4564 | } |
95eb84a7 SY |
4565 | |
4566 | if (!(cr0 & X86_CR0_WP)) | |
4567 | *hw_cr0 &= ~X86_CR0_WP; | |
1439442c SY |
4568 | } |
4569 | ||
6aa8b732 AK |
4570 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
4571 | { | |
7ffd92c5 | 4572 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
3a624e29 NK |
4573 | unsigned long hw_cr0; |
4574 | ||
5037878e | 4575 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
3a624e29 | 4576 | if (enable_unrestricted_guest) |
5037878e | 4577 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
218e763f | 4578 | else { |
5037878e | 4579 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
1439442c | 4580 | |
218e763f GN |
4581 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
4582 | enter_pmode(vcpu); | |
6aa8b732 | 4583 | |
218e763f GN |
4584 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
4585 | enter_rmode(vcpu); | |
4586 | } | |
6aa8b732 | 4587 | |
05b3e0c2 | 4588 | #ifdef CONFIG_X86_64 |
f6801dff | 4589 | if (vcpu->arch.efer & EFER_LME) { |
707d92fa | 4590 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
6aa8b732 | 4591 | enter_lmode(vcpu); |
707d92fa | 4592 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
6aa8b732 AK |
4593 | exit_lmode(vcpu); |
4594 | } | |
4595 | #endif | |
4596 | ||
089d034e | 4597 | if (enable_ept) |
1439442c SY |
4598 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
4599 | ||
6aa8b732 | 4600 | vmcs_writel(CR0_READ_SHADOW, cr0); |
1439442c | 4601 | vmcs_writel(GUEST_CR0, hw_cr0); |
ad312c7c | 4602 | vcpu->arch.cr0 = cr0; |
14168786 GN |
4603 | |
4604 | /* depends on vcpu->arch.cr0 to be set to a new value */ | |
4605 | vmx->emulation_required = emulation_required(vcpu); | |
6aa8b732 AK |
4606 | } |
4607 | ||
855feb67 YZ |
4608 | static int get_ept_level(struct kvm_vcpu *vcpu) |
4609 | { | |
4610 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) | |
4611 | return 5; | |
4612 | return 4; | |
4613 | } | |
4614 | ||
995f00a6 | 4615 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
1439442c | 4616 | { |
855feb67 YZ |
4617 | u64 eptp = VMX_EPTP_MT_WB; |
4618 | ||
4619 | eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4; | |
1439442c | 4620 | |
995f00a6 PF |
4621 | if (enable_ept_ad_bits && |
4622 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) | |
bb97a016 | 4623 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
1439442c SY |
4624 | eptp |= (root_hpa & PAGE_MASK); |
4625 | ||
4626 | return eptp; | |
4627 | } | |
4628 | ||
6aa8b732 AK |
4629 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
4630 | { | |
1439442c SY |
4631 | unsigned long guest_cr3; |
4632 | u64 eptp; | |
4633 | ||
4634 | guest_cr3 = cr3; | |
089d034e | 4635 | if (enable_ept) { |
995f00a6 | 4636 | eptp = construct_eptp(vcpu, cr3); |
1439442c | 4637 | vmcs_write64(EPT_POINTER, eptp); |
59ab5a8f JK |
4638 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
4639 | guest_cr3 = kvm_read_cr3(vcpu); | |
4640 | else | |
4641 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; | |
7c93be44 | 4642 | ept_load_pdptrs(vcpu); |
1439442c SY |
4643 | } |
4644 | ||
6bf41e55 | 4645 | vmx_flush_tlb(vcpu, true); |
1439442c | 4646 | vmcs_writel(GUEST_CR3, guest_cr3); |
6aa8b732 AK |
4647 | } |
4648 | ||
5e1746d6 | 4649 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
6aa8b732 | 4650 | { |
085e68ee BS |
4651 | /* |
4652 | * Pass through host's Machine Check Enable value to hw_cr4, which | |
4653 | * is in force while we are in guest mode. Do not let guests control | |
4654 | * this bit, even if host CR4.MCE == 0. | |
4655 | */ | |
4656 | unsigned long hw_cr4 = | |
4657 | (cr4_read_shadow() & X86_CR4_MCE) | | |
4658 | (cr4 & ~X86_CR4_MCE) | | |
4659 | (to_vmx(vcpu)->rmode.vm86_active ? | |
4660 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); | |
1439442c | 4661 | |
5e1746d6 NHE |
4662 | if (cr4 & X86_CR4_VMXE) { |
4663 | /* | |
4664 | * To use VMXON (and later other VMX instructions), a guest | |
4665 | * must first be able to turn on cr4.VMXE (see handle_vmon()). | |
4666 | * So basically the check on whether to allow nested VMX | |
4667 | * is here. | |
4668 | */ | |
4669 | if (!nested_vmx_allowed(vcpu)) | |
4670 | return 1; | |
1a0d74e6 | 4671 | } |
3899152c DM |
4672 | |
4673 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) | |
5e1746d6 NHE |
4674 | return 1; |
4675 | ||
ad312c7c | 4676 | vcpu->arch.cr4 = cr4; |
bc23008b AK |
4677 | if (enable_ept) { |
4678 | if (!is_paging(vcpu)) { | |
4679 | hw_cr4 &= ~X86_CR4_PAE; | |
4680 | hw_cr4 |= X86_CR4_PSE; | |
4681 | } else if (!(cr4 & X86_CR4_PAE)) { | |
4682 | hw_cr4 &= ~X86_CR4_PAE; | |
4683 | } | |
4684 | } | |
1439442c | 4685 | |
656ec4a4 RK |
4686 | if (!enable_unrestricted_guest && !is_paging(vcpu)) |
4687 | /* | |
ddba2628 HH |
4688 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
4689 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs | |
4690 | * to be manually disabled when guest switches to non-paging | |
4691 | * mode. | |
4692 | * | |
4693 | * If !enable_unrestricted_guest, the CPU is always running | |
4694 | * with CR0.PG=1 and CR4 needs to be modified. | |
4695 | * If enable_unrestricted_guest, the CPU automatically | |
4696 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. | |
656ec4a4 | 4697 | */ |
ddba2628 | 4698 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
656ec4a4 | 4699 | |
1439442c SY |
4700 | vmcs_writel(CR4_READ_SHADOW, cr4); |
4701 | vmcs_writel(GUEST_CR4, hw_cr4); | |
5e1746d6 | 4702 | return 0; |
6aa8b732 AK |
4703 | } |
4704 | ||
6aa8b732 AK |
4705 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
4706 | struct kvm_segment *var, int seg) | |
4707 | { | |
a9179499 | 4708 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6aa8b732 AK |
4709 | u32 ar; |
4710 | ||
c6ad1153 | 4711 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
f5f7b2fe | 4712 | *var = vmx->rmode.segs[seg]; |
a9179499 | 4713 | if (seg == VCPU_SREG_TR |
2fb92db1 | 4714 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
f5f7b2fe | 4715 | return; |
1390a28b AK |
4716 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4717 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4718 | return; | |
a9179499 | 4719 | } |
2fb92db1 AK |
4720 | var->base = vmx_read_guest_seg_base(vmx, seg); |
4721 | var->limit = vmx_read_guest_seg_limit(vmx, seg); | |
4722 | var->selector = vmx_read_guest_seg_selector(vmx, seg); | |
4723 | ar = vmx_read_guest_seg_ar(vmx, seg); | |
03617c18 | 4724 | var->unusable = (ar >> 16) & 1; |
6aa8b732 AK |
4725 | var->type = ar & 15; |
4726 | var->s = (ar >> 4) & 1; | |
4727 | var->dpl = (ar >> 5) & 3; | |
03617c18 GN |
4728 | /* |
4729 | * Some userspaces do not preserve unusable property. Since usable | |
4730 | * segment has to be present according to VMX spec we can use present | |
4731 | * property to amend userspace bug by making unusable segment always | |
4732 | * nonpresent. vmx_segment_access_rights() already marks nonpresent | |
4733 | * segment as unusable. | |
4734 | */ | |
4735 | var->present = !var->unusable; | |
6aa8b732 AK |
4736 | var->avl = (ar >> 12) & 1; |
4737 | var->l = (ar >> 13) & 1; | |
4738 | var->db = (ar >> 14) & 1; | |
4739 | var->g = (ar >> 15) & 1; | |
6aa8b732 AK |
4740 | } |
4741 | ||
a9179499 AK |
4742 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
4743 | { | |
a9179499 AK |
4744 | struct kvm_segment s; |
4745 | ||
4746 | if (to_vmx(vcpu)->rmode.vm86_active) { | |
4747 | vmx_get_segment(vcpu, &s, seg); | |
4748 | return s.base; | |
4749 | } | |
2fb92db1 | 4750 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
a9179499 AK |
4751 | } |
4752 | ||
b09408d0 | 4753 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
2e4d2653 | 4754 | { |
b09408d0 MT |
4755 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
4756 | ||
ae9fedc7 | 4757 | if (unlikely(vmx->rmode.vm86_active)) |
2e4d2653 | 4758 | return 0; |
ae9fedc7 PB |
4759 | else { |
4760 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); | |
4d283ec9 | 4761 | return VMX_AR_DPL(ar); |
69c73028 | 4762 | } |
69c73028 AK |
4763 | } |
4764 | ||
653e3108 | 4765 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
6aa8b732 | 4766 | { |
6aa8b732 AK |
4767 | u32 ar; |
4768 | ||
f0495f9b | 4769 | if (var->unusable || !var->present) |
6aa8b732 AK |
4770 | ar = 1 << 16; |
4771 | else { | |
4772 | ar = var->type & 15; | |
4773 | ar |= (var->s & 1) << 4; | |
4774 | ar |= (var->dpl & 3) << 5; | |
4775 | ar |= (var->present & 1) << 7; | |
4776 | ar |= (var->avl & 1) << 12; | |
4777 | ar |= (var->l & 1) << 13; | |
4778 | ar |= (var->db & 1) << 14; | |
4779 | ar |= (var->g & 1) << 15; | |
4780 | } | |
653e3108 AK |
4781 | |
4782 | return ar; | |
4783 | } | |
4784 | ||
4785 | static void vmx_set_segment(struct kvm_vcpu *vcpu, | |
4786 | struct kvm_segment *var, int seg) | |
4787 | { | |
7ffd92c5 | 4788 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
772e0318 | 4789 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
653e3108 | 4790 | |
2fb92db1 AK |
4791 | vmx_segment_cache_clear(vmx); |
4792 | ||
1ecd50a9 GN |
4793 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
4794 | vmx->rmode.segs[seg] = *var; | |
4795 | if (seg == VCPU_SREG_TR) | |
4796 | vmcs_write16(sf->selector, var->selector); | |
4797 | else if (var->s) | |
4798 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); | |
d99e4152 | 4799 | goto out; |
653e3108 | 4800 | } |
1ecd50a9 | 4801 | |
653e3108 AK |
4802 | vmcs_writel(sf->base, var->base); |
4803 | vmcs_write32(sf->limit, var->limit); | |
4804 | vmcs_write16(sf->selector, var->selector); | |
3a624e29 NK |
4805 | |
4806 | /* | |
4807 | * Fix the "Accessed" bit in AR field of segment registers for older | |
4808 | * qemu binaries. | |
4809 | * IA32 arch specifies that at the time of processor reset the | |
4810 | * "Accessed" bit in the AR field of segment registers is 1. And qemu | |
0fa06071 | 4811 | * is setting it to 0 in the userland code. This causes invalid guest |
3a624e29 NK |
4812 | * state vmexit when "unrestricted guest" mode is turned on. |
4813 | * Fix for this setup issue in cpu_reset is being pushed in the qemu | |
4814 | * tree. Newer qemu binaries with that qemu fix would not need this | |
4815 | * kvm hack. | |
4816 | */ | |
4817 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) | |
f924d66d | 4818 | var->type |= 0x1; /* Accessed */ |
3a624e29 | 4819 | |
f924d66d | 4820 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
d99e4152 GN |
4821 | |
4822 | out: | |
98eb2f8b | 4823 | vmx->emulation_required = emulation_required(vcpu); |
6aa8b732 AK |
4824 | } |
4825 | ||
6aa8b732 AK |
4826 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
4827 | { | |
2fb92db1 | 4828 | u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS); |
6aa8b732 AK |
4829 | |
4830 | *db = (ar >> 14) & 1; | |
4831 | *l = (ar >> 13) & 1; | |
4832 | } | |
4833 | ||
89a27f4d | 4834 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4835 | { |
89a27f4d GN |
4836 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
4837 | dt->address = vmcs_readl(GUEST_IDTR_BASE); | |
6aa8b732 AK |
4838 | } |
4839 | ||
89a27f4d | 4840 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4841 | { |
89a27f4d GN |
4842 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
4843 | vmcs_writel(GUEST_IDTR_BASE, dt->address); | |
6aa8b732 AK |
4844 | } |
4845 | ||
89a27f4d | 4846 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4847 | { |
89a27f4d GN |
4848 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
4849 | dt->address = vmcs_readl(GUEST_GDTR_BASE); | |
6aa8b732 AK |
4850 | } |
4851 | ||
89a27f4d | 4852 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 4853 | { |
89a27f4d GN |
4854 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
4855 | vmcs_writel(GUEST_GDTR_BASE, dt->address); | |
6aa8b732 AK |
4856 | } |
4857 | ||
648dfaa7 MG |
4858 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
4859 | { | |
4860 | struct kvm_segment var; | |
4861 | u32 ar; | |
4862 | ||
4863 | vmx_get_segment(vcpu, &var, seg); | |
07f42f5f | 4864 | var.dpl = 0x3; |
0647f4aa GN |
4865 | if (seg == VCPU_SREG_CS) |
4866 | var.type = 0x3; | |
648dfaa7 MG |
4867 | ar = vmx_segment_access_rights(&var); |
4868 | ||
4869 | if (var.base != (var.selector << 4)) | |
4870 | return false; | |
89efbed0 | 4871 | if (var.limit != 0xffff) |
648dfaa7 | 4872 | return false; |
07f42f5f | 4873 | if (ar != 0xf3) |
648dfaa7 MG |
4874 | return false; |
4875 | ||
4876 | return true; | |
4877 | } | |
4878 | ||
4879 | static bool code_segment_valid(struct kvm_vcpu *vcpu) | |
4880 | { | |
4881 | struct kvm_segment cs; | |
4882 | unsigned int cs_rpl; | |
4883 | ||
4884 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
b32a9918 | 4885 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4886 | |
1872a3f4 AK |
4887 | if (cs.unusable) |
4888 | return false; | |
4d283ec9 | 4889 | if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK)) |
648dfaa7 MG |
4890 | return false; |
4891 | if (!cs.s) | |
4892 | return false; | |
4d283ec9 | 4893 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
648dfaa7 MG |
4894 | if (cs.dpl > cs_rpl) |
4895 | return false; | |
1872a3f4 | 4896 | } else { |
648dfaa7 MG |
4897 | if (cs.dpl != cs_rpl) |
4898 | return false; | |
4899 | } | |
4900 | if (!cs.present) | |
4901 | return false; | |
4902 | ||
4903 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ | |
4904 | return true; | |
4905 | } | |
4906 | ||
4907 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) | |
4908 | { | |
4909 | struct kvm_segment ss; | |
4910 | unsigned int ss_rpl; | |
4911 | ||
4912 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
b32a9918 | 4913 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4914 | |
1872a3f4 AK |
4915 | if (ss.unusable) |
4916 | return true; | |
4917 | if (ss.type != 3 && ss.type != 7) | |
648dfaa7 MG |
4918 | return false; |
4919 | if (!ss.s) | |
4920 | return false; | |
4921 | if (ss.dpl != ss_rpl) /* DPL != RPL */ | |
4922 | return false; | |
4923 | if (!ss.present) | |
4924 | return false; | |
4925 | ||
4926 | return true; | |
4927 | } | |
4928 | ||
4929 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) | |
4930 | { | |
4931 | struct kvm_segment var; | |
4932 | unsigned int rpl; | |
4933 | ||
4934 | vmx_get_segment(vcpu, &var, seg); | |
b32a9918 | 4935 | rpl = var.selector & SEGMENT_RPL_MASK; |
648dfaa7 | 4936 | |
1872a3f4 AK |
4937 | if (var.unusable) |
4938 | return true; | |
648dfaa7 MG |
4939 | if (!var.s) |
4940 | return false; | |
4941 | if (!var.present) | |
4942 | return false; | |
4d283ec9 | 4943 | if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) { |
648dfaa7 MG |
4944 | if (var.dpl < rpl) /* DPL < RPL */ |
4945 | return false; | |
4946 | } | |
4947 | ||
4948 | /* TODO: Add other members to kvm_segment_field to allow checking for other access | |
4949 | * rights flags | |
4950 | */ | |
4951 | return true; | |
4952 | } | |
4953 | ||
4954 | static bool tr_valid(struct kvm_vcpu *vcpu) | |
4955 | { | |
4956 | struct kvm_segment tr; | |
4957 | ||
4958 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); | |
4959 | ||
1872a3f4 AK |
4960 | if (tr.unusable) |
4961 | return false; | |
b32a9918 | 4962 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 | 4963 | return false; |
1872a3f4 | 4964 | if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ |
648dfaa7 MG |
4965 | return false; |
4966 | if (!tr.present) | |
4967 | return false; | |
4968 | ||
4969 | return true; | |
4970 | } | |
4971 | ||
4972 | static bool ldtr_valid(struct kvm_vcpu *vcpu) | |
4973 | { | |
4974 | struct kvm_segment ldtr; | |
4975 | ||
4976 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); | |
4977 | ||
1872a3f4 AK |
4978 | if (ldtr.unusable) |
4979 | return true; | |
b32a9918 | 4980 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
648dfaa7 MG |
4981 | return false; |
4982 | if (ldtr.type != 2) | |
4983 | return false; | |
4984 | if (!ldtr.present) | |
4985 | return false; | |
4986 | ||
4987 | return true; | |
4988 | } | |
4989 | ||
4990 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) | |
4991 | { | |
4992 | struct kvm_segment cs, ss; | |
4993 | ||
4994 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
4995 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); | |
4996 | ||
b32a9918 NA |
4997 | return ((cs.selector & SEGMENT_RPL_MASK) == |
4998 | (ss.selector & SEGMENT_RPL_MASK)); | |
648dfaa7 MG |
4999 | } |
5000 | ||
5001 | /* | |
5002 | * Check if guest state is valid. Returns true if valid, false if | |
5003 | * not. | |
5004 | * We assume that registers are always usable | |
5005 | */ | |
5006 | static bool guest_state_valid(struct kvm_vcpu *vcpu) | |
5007 | { | |
c5e97c80 GN |
5008 | if (enable_unrestricted_guest) |
5009 | return true; | |
5010 | ||
648dfaa7 | 5011 | /* real mode guest state checks */ |
f13882d8 | 5012 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
648dfaa7 MG |
5013 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
5014 | return false; | |
5015 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) | |
5016 | return false; | |
5017 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) | |
5018 | return false; | |
5019 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) | |
5020 | return false; | |
5021 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) | |
5022 | return false; | |
5023 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) | |
5024 | return false; | |
5025 | } else { | |
5026 | /* protected mode guest state checks */ | |
5027 | if (!cs_ss_rpl_check(vcpu)) | |
5028 | return false; | |
5029 | if (!code_segment_valid(vcpu)) | |
5030 | return false; | |
5031 | if (!stack_segment_valid(vcpu)) | |
5032 | return false; | |
5033 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) | |
5034 | return false; | |
5035 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) | |
5036 | return false; | |
5037 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) | |
5038 | return false; | |
5039 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) | |
5040 | return false; | |
5041 | if (!tr_valid(vcpu)) | |
5042 | return false; | |
5043 | if (!ldtr_valid(vcpu)) | |
5044 | return false; | |
5045 | } | |
5046 | /* TODO: | |
5047 | * - Add checks on RIP | |
5048 | * - Add checks on RFLAGS | |
5049 | */ | |
5050 | ||
5051 | return true; | |
5052 | } | |
5053 | ||
5fa99cbe JM |
5054 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
5055 | { | |
5056 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); | |
5057 | } | |
5058 | ||
d77c26fc | 5059 | static int init_rmode_tss(struct kvm *kvm) |
6aa8b732 | 5060 | { |
40dcaa9f | 5061 | gfn_t fn; |
195aefde | 5062 | u16 data = 0; |
1f755a82 | 5063 | int idx, r; |
6aa8b732 | 5064 | |
40dcaa9f | 5065 | idx = srcu_read_lock(&kvm->srcu); |
4918c6ca | 5066 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
195aefde IE |
5067 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
5068 | if (r < 0) | |
10589a46 | 5069 | goto out; |
195aefde | 5070 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
464d17c8 SY |
5071 | r = kvm_write_guest_page(kvm, fn++, &data, |
5072 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); | |
195aefde | 5073 | if (r < 0) |
10589a46 | 5074 | goto out; |
195aefde IE |
5075 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
5076 | if (r < 0) | |
10589a46 | 5077 | goto out; |
195aefde IE |
5078 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
5079 | if (r < 0) | |
10589a46 | 5080 | goto out; |
195aefde | 5081 | data = ~0; |
10589a46 MT |
5082 | r = kvm_write_guest_page(kvm, fn, &data, |
5083 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, | |
5084 | sizeof(u8)); | |
10589a46 | 5085 | out: |
40dcaa9f | 5086 | srcu_read_unlock(&kvm->srcu, idx); |
1f755a82 | 5087 | return r; |
6aa8b732 AK |
5088 | } |
5089 | ||
b7ebfb05 SY |
5090 | static int init_rmode_identity_map(struct kvm *kvm) |
5091 | { | |
f51770ed | 5092 | int i, idx, r = 0; |
ba049e93 | 5093 | kvm_pfn_t identity_map_pfn; |
b7ebfb05 SY |
5094 | u32 tmp; |
5095 | ||
a255d479 TC |
5096 | /* Protect kvm->arch.ept_identity_pagetable_done. */ |
5097 | mutex_lock(&kvm->slots_lock); | |
5098 | ||
f51770ed | 5099 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
a255d479 | 5100 | goto out2; |
a255d479 | 5101 | |
d8a6e365 DH |
5102 | if (!kvm->arch.ept_identity_map_addr) |
5103 | kvm->arch.ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; | |
b927a3ce | 5104 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
a255d479 | 5105 | |
d8a6e365 DH |
5106 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
5107 | kvm->arch.ept_identity_map_addr, PAGE_SIZE); | |
f51770ed | 5108 | if (r < 0) |
a255d479 TC |
5109 | goto out2; |
5110 | ||
40dcaa9f | 5111 | idx = srcu_read_lock(&kvm->srcu); |
b7ebfb05 SY |
5112 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
5113 | if (r < 0) | |
5114 | goto out; | |
5115 | /* Set up identity-mapping pagetable for EPT in real mode */ | |
5116 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { | |
5117 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | | |
5118 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); | |
5119 | r = kvm_write_guest_page(kvm, identity_map_pfn, | |
5120 | &tmp, i * sizeof(tmp), sizeof(tmp)); | |
5121 | if (r < 0) | |
5122 | goto out; | |
5123 | } | |
5124 | kvm->arch.ept_identity_pagetable_done = true; | |
f51770ed | 5125 | |
b7ebfb05 | 5126 | out: |
40dcaa9f | 5127 | srcu_read_unlock(&kvm->srcu, idx); |
a255d479 TC |
5128 | |
5129 | out2: | |
5130 | mutex_unlock(&kvm->slots_lock); | |
f51770ed | 5131 | return r; |
b7ebfb05 SY |
5132 | } |
5133 | ||
6aa8b732 AK |
5134 | static void seg_setup(int seg) |
5135 | { | |
772e0318 | 5136 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
3a624e29 | 5137 | unsigned int ar; |
6aa8b732 AK |
5138 | |
5139 | vmcs_write16(sf->selector, 0); | |
5140 | vmcs_writel(sf->base, 0); | |
5141 | vmcs_write32(sf->limit, 0xffff); | |
d54d07b2 GN |
5142 | ar = 0x93; |
5143 | if (seg == VCPU_SREG_CS) | |
5144 | ar |= 0x08; /* code segment */ | |
3a624e29 NK |
5145 | |
5146 | vmcs_write32(sf->ar_bytes, ar); | |
6aa8b732 AK |
5147 | } |
5148 | ||
f78e0e2e SY |
5149 | static int alloc_apic_access_page(struct kvm *kvm) |
5150 | { | |
4484141a | 5151 | struct page *page; |
f78e0e2e SY |
5152 | int r = 0; |
5153 | ||
79fac95e | 5154 | mutex_lock(&kvm->slots_lock); |
c24ae0dc | 5155 | if (kvm->arch.apic_access_page_done) |
f78e0e2e | 5156 | goto out; |
1d8007bd PB |
5157 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
5158 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); | |
f78e0e2e SY |
5159 | if (r) |
5160 | goto out; | |
72dc67a6 | 5161 | |
73a6d941 | 5162 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
4484141a XG |
5163 | if (is_error_page(page)) { |
5164 | r = -EFAULT; | |
5165 | goto out; | |
5166 | } | |
5167 | ||
c24ae0dc TC |
5168 | /* |
5169 | * Do not pin the page in memory, so that memory hot-unplug | |
5170 | * is able to migrate it. | |
5171 | */ | |
5172 | put_page(page); | |
5173 | kvm->arch.apic_access_page_done = true; | |
f78e0e2e | 5174 | out: |
79fac95e | 5175 | mutex_unlock(&kvm->slots_lock); |
f78e0e2e SY |
5176 | return r; |
5177 | } | |
5178 | ||
991e7a0e | 5179 | static int allocate_vpid(void) |
2384d2b3 SY |
5180 | { |
5181 | int vpid; | |
5182 | ||
919818ab | 5183 | if (!enable_vpid) |
991e7a0e | 5184 | return 0; |
2384d2b3 SY |
5185 | spin_lock(&vmx_vpid_lock); |
5186 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); | |
991e7a0e | 5187 | if (vpid < VMX_NR_VPIDS) |
2384d2b3 | 5188 | __set_bit(vpid, vmx_vpid_bitmap); |
991e7a0e WL |
5189 | else |
5190 | vpid = 0; | |
2384d2b3 | 5191 | spin_unlock(&vmx_vpid_lock); |
991e7a0e | 5192 | return vpid; |
2384d2b3 SY |
5193 | } |
5194 | ||
991e7a0e | 5195 | static void free_vpid(int vpid) |
cdbecfc3 | 5196 | { |
991e7a0e | 5197 | if (!enable_vpid || vpid == 0) |
cdbecfc3 LJ |
5198 | return; |
5199 | spin_lock(&vmx_vpid_lock); | |
991e7a0e | 5200 | __clear_bit(vpid, vmx_vpid_bitmap); |
cdbecfc3 LJ |
5201 | spin_unlock(&vmx_vpid_lock); |
5202 | } | |
5203 | ||
3861351c | 5204 | static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
4b0be90f | 5205 | u32 msr, int type) |
25c5f225 | 5206 | { |
3e7c73e9 | 5207 | int f = sizeof(unsigned long); |
25c5f225 SY |
5208 | |
5209 | if (!cpu_has_vmx_msr_bitmap()) | |
5210 | return; | |
5211 | ||
5212 | /* | |
5213 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
5214 | * have the write-low and read-high bitmap offsets the wrong way round. | |
5215 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
5216 | */ | |
25c5f225 | 5217 | if (msr <= 0x1fff) { |
8d14695f YZ |
5218 | if (type & MSR_TYPE_R) |
5219 | /* read-low */ | |
5220 | __clear_bit(msr, msr_bitmap + 0x000 / f); | |
5221 | ||
5222 | if (type & MSR_TYPE_W) | |
5223 | /* write-low */ | |
5224 | __clear_bit(msr, msr_bitmap + 0x800 / f); | |
5225 | ||
25c5f225 SY |
5226 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
5227 | msr &= 0x1fff; | |
8d14695f YZ |
5228 | if (type & MSR_TYPE_R) |
5229 | /* read-high */ | |
5230 | __clear_bit(msr, msr_bitmap + 0x400 / f); | |
5231 | ||
5232 | if (type & MSR_TYPE_W) | |
5233 | /* write-high */ | |
5234 | __clear_bit(msr, msr_bitmap + 0xc00 / f); | |
5235 | ||
5236 | } | |
5237 | } | |
5238 | ||
3861351c | 5239 | static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
4b0be90f PB |
5240 | u32 msr, int type) |
5241 | { | |
5242 | int f = sizeof(unsigned long); | |
5243 | ||
5244 | if (!cpu_has_vmx_msr_bitmap()) | |
5245 | return; | |
5246 | ||
5247 | /* | |
5248 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
5249 | * have the write-low and read-high bitmap offsets the wrong way round. | |
5250 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
5251 | */ | |
5252 | if (msr <= 0x1fff) { | |
5253 | if (type & MSR_TYPE_R) | |
5254 | /* read-low */ | |
5255 | __set_bit(msr, msr_bitmap + 0x000 / f); | |
5256 | ||
5257 | if (type & MSR_TYPE_W) | |
5258 | /* write-low */ | |
5259 | __set_bit(msr, msr_bitmap + 0x800 / f); | |
5260 | ||
5261 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
5262 | msr &= 0x1fff; | |
5263 | if (type & MSR_TYPE_R) | |
5264 | /* read-high */ | |
5265 | __set_bit(msr, msr_bitmap + 0x400 / f); | |
5266 | ||
5267 | if (type & MSR_TYPE_W) | |
5268 | /* write-high */ | |
5269 | __set_bit(msr, msr_bitmap + 0xc00 / f); | |
5270 | ||
5271 | } | |
5272 | } | |
5273 | ||
3861351c | 5274 | static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
4b0be90f PB |
5275 | u32 msr, int type, bool value) |
5276 | { | |
5277 | if (value) | |
5278 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); | |
5279 | else | |
5280 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); | |
5281 | } | |
5282 | ||
f2b93280 WV |
5283 | /* |
5284 | * If a msr is allowed by L0, we should check whether it is allowed by L1. | |
5285 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. | |
5286 | */ | |
5287 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, | |
5288 | unsigned long *msr_bitmap_nested, | |
5289 | u32 msr, int type) | |
5290 | { | |
5291 | int f = sizeof(unsigned long); | |
5292 | ||
5293 | if (!cpu_has_vmx_msr_bitmap()) { | |
5294 | WARN_ON(1); | |
5295 | return; | |
5296 | } | |
5297 | ||
5298 | /* | |
5299 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals | |
5300 | * have the write-low and read-high bitmap offsets the wrong way round. | |
5301 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. | |
5302 | */ | |
5303 | if (msr <= 0x1fff) { | |
5304 | if (type & MSR_TYPE_R && | |
5305 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) | |
5306 | /* read-low */ | |
5307 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); | |
5308 | ||
5309 | if (type & MSR_TYPE_W && | |
5310 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) | |
5311 | /* write-low */ | |
5312 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); | |
5313 | ||
5314 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { | |
5315 | msr &= 0x1fff; | |
5316 | if (type & MSR_TYPE_R && | |
5317 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) | |
5318 | /* read-high */ | |
5319 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); | |
5320 | ||
5321 | if (type & MSR_TYPE_W && | |
5322 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) | |
5323 | /* write-high */ | |
5324 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); | |
5325 | ||
5326 | } | |
5327 | } | |
5328 | ||
4b0be90f | 5329 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
5897297b | 5330 | { |
4b0be90f PB |
5331 | u8 mode = 0; |
5332 | ||
5333 | if (cpu_has_secondary_exec_ctrls() && | |
5334 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & | |
5335 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { | |
5336 | mode |= MSR_BITMAP_MODE_X2APIC; | |
5337 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) | |
5338 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; | |
5339 | } | |
5340 | ||
5341 | if (is_long_mode(vcpu)) | |
5342 | mode |= MSR_BITMAP_MODE_LM; | |
5343 | ||
5344 | return mode; | |
8d14695f YZ |
5345 | } |
5346 | ||
4b0be90f PB |
5347 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
5348 | ||
5349 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, | |
5350 | u8 mode) | |
8d14695f | 5351 | { |
4b0be90f PB |
5352 | int msr; |
5353 | ||
5354 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { | |
5355 | unsigned word = msr / BITS_PER_LONG; | |
5356 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; | |
5357 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; | |
5358 | } | |
5359 | ||
5360 | if (mode & MSR_BITMAP_MODE_X2APIC) { | |
5361 | /* | |
5362 | * TPR reads and writes can be virtualized even if virtual interrupt | |
5363 | * delivery is not in use. | |
5364 | */ | |
5365 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); | |
5366 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { | |
5367 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); | |
5368 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); | |
5369 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); | |
5370 | } | |
f6e90f9e | 5371 | } |
5897297b AK |
5372 | } |
5373 | ||
4b0be90f PB |
5374 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
5375 | { | |
5376 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5377 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; | |
5378 | u8 mode = vmx_msr_bitmap_mode(vcpu); | |
5379 | u8 changed = mode ^ vmx->msr_bitmap_mode; | |
5380 | ||
5381 | if (!changed) | |
5382 | return; | |
5383 | ||
5384 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, | |
5385 | !(mode & MSR_BITMAP_MODE_LM)); | |
5386 | ||
5387 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) | |
5388 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); | |
5389 | ||
5390 | vmx->msr_bitmap_mode = mode; | |
5391 | } | |
5392 | ||
b2a05fef | 5393 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
d50ab6c1 | 5394 | { |
d62caabb | 5395 | return enable_apicv; |
d50ab6c1 PB |
5396 | } |
5397 | ||
c9f04407 DM |
5398 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
5399 | { | |
5400 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
5401 | gfn_t gfn; | |
5402 | ||
5403 | /* | |
5404 | * Don't need to mark the APIC access page dirty; it is never | |
5405 | * written to by the CPU during APIC virtualization. | |
5406 | */ | |
5407 | ||
5408 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { | |
5409 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; | |
5410 | kvm_vcpu_mark_page_dirty(vcpu, gfn); | |
5411 | } | |
5412 | ||
5413 | if (nested_cpu_has_posted_intr(vmcs12)) { | |
5414 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; | |
5415 | kvm_vcpu_mark_page_dirty(vcpu, gfn); | |
5416 | } | |
5417 | } | |
5418 | ||
5419 | ||
6342c50a | 5420 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
705699a1 WV |
5421 | { |
5422 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5423 | int max_irr; | |
5424 | void *vapic_page; | |
5425 | u16 status; | |
5426 | ||
c9f04407 DM |
5427 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
5428 | return; | |
705699a1 | 5429 | |
c9f04407 DM |
5430 | vmx->nested.pi_pending = false; |
5431 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) | |
5432 | return; | |
705699a1 | 5433 | |
c9f04407 DM |
5434 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
5435 | if (max_irr != 256) { | |
705699a1 | 5436 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
705699a1 WV |
5437 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page); |
5438 | kunmap(vmx->nested.virtual_apic_page); | |
5439 | ||
5440 | status = vmcs_read16(GUEST_INTR_STATUS); | |
5441 | if ((u8)max_irr > ((u8)status & 0xff)) { | |
5442 | status &= ~0xff; | |
5443 | status |= (u8)max_irr; | |
5444 | vmcs_write16(GUEST_INTR_STATUS, status); | |
5445 | } | |
5446 | } | |
c9f04407 DM |
5447 | |
5448 | nested_mark_vmcs12_pages_dirty(vcpu); | |
705699a1 WV |
5449 | } |
5450 | ||
06a5524f WV |
5451 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
5452 | bool nested) | |
21bc8dc5 RK |
5453 | { |
5454 | #ifdef CONFIG_SMP | |
06a5524f WV |
5455 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
5456 | ||
21bc8dc5 | 5457 | if (vcpu->mode == IN_GUEST_MODE) { |
28b835d6 | 5458 | /* |
5753743f HZ |
5459 | * The vector of interrupt to be delivered to vcpu had |
5460 | * been set in PIR before this function. | |
5461 | * | |
5462 | * Following cases will be reached in this block, and | |
5463 | * we always send a notification event in all cases as | |
5464 | * explained below. | |
5465 | * | |
5466 | * Case 1: vcpu keeps in non-root mode. Sending a | |
5467 | * notification event posts the interrupt to vcpu. | |
5468 | * | |
5469 | * Case 2: vcpu exits to root mode and is still | |
5470 | * runnable. PIR will be synced to vIRR before the | |
5471 | * next vcpu entry. Sending a notification event in | |
5472 | * this case has no effect, as vcpu is not in root | |
5473 | * mode. | |
28b835d6 | 5474 | * |
5753743f HZ |
5475 | * Case 3: vcpu exits to root mode and is blocked. |
5476 | * vcpu_block() has already synced PIR to vIRR and | |
5477 | * never blocks vcpu if vIRR is not cleared. Therefore, | |
5478 | * a blocked vcpu here does not wait for any requested | |
5479 | * interrupts in PIR, and sending a notification event | |
5480 | * which has no effect is safe here. | |
28b835d6 | 5481 | */ |
28b835d6 | 5482 | |
06a5524f | 5483 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
21bc8dc5 RK |
5484 | return true; |
5485 | } | |
5486 | #endif | |
5487 | return false; | |
5488 | } | |
5489 | ||
705699a1 WV |
5490 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
5491 | int vector) | |
5492 | { | |
5493 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5494 | ||
5495 | if (is_guest_mode(vcpu) && | |
5496 | vector == vmx->nested.posted_intr_nv) { | |
705699a1 WV |
5497 | /* |
5498 | * If a posted intr is not recognized by hardware, | |
5499 | * we will accomplish it in the next vmentry. | |
5500 | */ | |
5501 | vmx->nested.pi_pending = true; | |
5502 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
7b616667 LA |
5503 | /* the PIR and ON have been set by L1. */ |
5504 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) | |
5505 | kvm_vcpu_kick(vcpu); | |
705699a1 WV |
5506 | return 0; |
5507 | } | |
5508 | return -1; | |
5509 | } | |
a20ed54d YZ |
5510 | /* |
5511 | * Send interrupt to vcpu via posted interrupt way. | |
5512 | * 1. If target vcpu is running(non-root mode), send posted interrupt | |
5513 | * notification to vcpu and hardware will sync PIR to vIRR atomically. | |
5514 | * 2. If target vcpu isn't running(root mode), kick it to pick up the | |
5515 | * interrupt from PIR in next vmentry. | |
5516 | */ | |
5517 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) | |
5518 | { | |
5519 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5520 | int r; | |
5521 | ||
705699a1 WV |
5522 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
5523 | if (!r) | |
5524 | return; | |
5525 | ||
a20ed54d YZ |
5526 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
5527 | return; | |
5528 | ||
b95234c8 PB |
5529 | /* If a previous notification has sent the IPI, nothing to do. */ |
5530 | if (pi_test_and_set_on(&vmx->pi_desc)) | |
5531 | return; | |
5532 | ||
06a5524f | 5533 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
a20ed54d YZ |
5534 | kvm_vcpu_kick(vcpu); |
5535 | } | |
5536 | ||
a3a8ff8e NHE |
5537 | /* |
5538 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that | |
5539 | * will not change in the lifetime of the guest. | |
5540 | * Note that host-state that does change is set elsewhere. E.g., host-state | |
5541 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. | |
5542 | */ | |
a547c6db | 5543 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
a3a8ff8e NHE |
5544 | { |
5545 | u32 low32, high32; | |
5546 | unsigned long tmpl; | |
5547 | struct desc_ptr dt; | |
d6e41f11 | 5548 | unsigned long cr0, cr3, cr4; |
a3a8ff8e | 5549 | |
04ac88ab AL |
5550 | cr0 = read_cr0(); |
5551 | WARN_ON(cr0 & X86_CR0_TS); | |
5552 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ | |
d6e41f11 AL |
5553 | |
5554 | /* | |
5555 | * Save the most likely value for this task's CR3 in the VMCS. | |
5556 | * We can't use __get_current_cr3_fast() because we're not atomic. | |
5557 | */ | |
6c690ee1 | 5558 | cr3 = __read_cr3(); |
d6e41f11 | 5559 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
44889942 | 5560 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
a3a8ff8e | 5561 | |
d974baa3 | 5562 | /* Save the most likely value for this task's CR4 in the VMCS. */ |
1e02ce4c | 5563 | cr4 = cr4_read_shadow(); |
d974baa3 | 5564 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
44889942 | 5565 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
d974baa3 | 5566 | |
a3a8ff8e | 5567 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
b2da15ac AK |
5568 | #ifdef CONFIG_X86_64 |
5569 | /* | |
5570 | * Load null selectors, so we can avoid reloading them in | |
5571 | * __vmx_load_host_state(), in case userspace uses the null selectors | |
5572 | * too (the expected case). | |
5573 | */ | |
5574 | vmcs_write16(HOST_DS_SELECTOR, 0); | |
5575 | vmcs_write16(HOST_ES_SELECTOR, 0); | |
5576 | #else | |
a3a8ff8e NHE |
5577 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5578 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ | |
b2da15ac | 5579 | #endif |
a3a8ff8e NHE |
5580 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
5581 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ | |
5582 | ||
87930019 | 5583 | store_idt(&dt); |
a3a8ff8e | 5584 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
a547c6db | 5585 | vmx->host_idt_base = dt.address; |
a3a8ff8e | 5586 | |
83287ea4 | 5587 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
a3a8ff8e NHE |
5588 | |
5589 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); | |
5590 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); | |
5591 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); | |
5592 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ | |
5593 | ||
5594 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { | |
5595 | rdmsr(MSR_IA32_CR_PAT, low32, high32); | |
5596 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); | |
5597 | } | |
5598 | } | |
5599 | ||
bf8179a0 NHE |
5600 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
5601 | { | |
5602 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; | |
5603 | if (enable_ept) | |
5604 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; | |
fe3ef05c NHE |
5605 | if (is_guest_mode(&vmx->vcpu)) |
5606 | vmx->vcpu.arch.cr4_guest_owned_bits &= | |
5607 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; | |
bf8179a0 NHE |
5608 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
5609 | } | |
5610 | ||
01e439be YZ |
5611 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
5612 | { | |
5613 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; | |
5614 | ||
d62caabb | 5615 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
01e439be | 5616 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
d02fcf50 PB |
5617 | |
5618 | if (!enable_vnmi) | |
5619 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; | |
5620 | ||
64672c95 YJ |
5621 | /* Enable the preemption timer dynamically */ |
5622 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
01e439be YZ |
5623 | return pin_based_exec_ctrl; |
5624 | } | |
5625 | ||
d62caabb AS |
5626 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
5627 | { | |
5628 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5629 | ||
5630 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); | |
3ce424e4 RK |
5631 | if (cpu_has_secondary_exec_ctrls()) { |
5632 | if (kvm_vcpu_apicv_active(vcpu)) | |
5633 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
5634 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5635 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5636 | else | |
5637 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
5638 | SECONDARY_EXEC_APIC_REGISTER_VIRT | | |
5639 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
5640 | } | |
5641 | ||
5642 | if (cpu_has_vmx_msr_bitmap()) | |
4b0be90f | 5643 | vmx_update_msr_bitmap(vcpu); |
d62caabb AS |
5644 | } |
5645 | ||
bf8179a0 NHE |
5646 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
5647 | { | |
5648 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; | |
d16c293e PB |
5649 | |
5650 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) | |
5651 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; | |
5652 | ||
35754c98 | 5653 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
bf8179a0 NHE |
5654 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
5655 | #ifdef CONFIG_X86_64 | |
5656 | exec_control |= CPU_BASED_CR8_STORE_EXITING | | |
5657 | CPU_BASED_CR8_LOAD_EXITING; | |
5658 | #endif | |
5659 | } | |
5660 | if (!enable_ept) | |
5661 | exec_control |= CPU_BASED_CR3_STORE_EXITING | | |
5662 | CPU_BASED_CR3_LOAD_EXITING | | |
5663 | CPU_BASED_INVLPG_EXITING; | |
5664 | return exec_control; | |
5665 | } | |
5666 | ||
45ec368c | 5667 | static bool vmx_rdrand_supported(void) |
bf8179a0 | 5668 | { |
45ec368c | 5669 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
736fdf72 | 5670 | SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5671 | } |
5672 | ||
75f4fc8d JM |
5673 | static bool vmx_rdseed_supported(void) |
5674 | { | |
5675 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
736fdf72 | 5676 | SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5677 | } |
5678 | ||
80154d77 | 5679 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
bf8179a0 | 5680 | { |
80154d77 PB |
5681 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
5682 | ||
bf8179a0 | 5683 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
80154d77 | 5684 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
bf8179a0 NHE |
5685 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
5686 | if (vmx->vpid == 0) | |
5687 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; | |
5688 | if (!enable_ept) { | |
5689 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; | |
5690 | enable_unrestricted_guest = 0; | |
ad756a16 MJ |
5691 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
5692 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; | |
bf8179a0 NHE |
5693 | } |
5694 | if (!enable_unrestricted_guest) | |
5695 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; | |
5696 | if (!ple_gap) | |
5697 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; | |
80154d77 | 5698 | if (!kvm_vcpu_apicv_active(vcpu)) |
c7c9c56c YZ |
5699 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
5700 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); | |
8d14695f | 5701 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
abc4fc58 AG |
5702 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
5703 | (handle_vmptrld). | |
5704 | We can NOT enable shadow_vmcs here because we don't have yet | |
5705 | a current VMCS12 | |
5706 | */ | |
5707 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; | |
a3eaa864 KH |
5708 | |
5709 | if (!enable_pml) | |
5710 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; | |
843e4330 | 5711 | |
3db13480 PB |
5712 | if (vmx_xsaves_supported()) { |
5713 | /* Exposing XSAVES only when XSAVE is exposed */ | |
5714 | bool xsaves_enabled = | |
5715 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && | |
5716 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); | |
5717 | ||
5718 | if (!xsaves_enabled) | |
5719 | exec_control &= ~SECONDARY_EXEC_XSAVES; | |
5720 | ||
5721 | if (nested) { | |
5722 | if (xsaves_enabled) | |
5723 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
5724 | SECONDARY_EXEC_XSAVES; | |
5725 | else | |
5726 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
5727 | ~SECONDARY_EXEC_XSAVES; | |
5728 | } | |
5729 | } | |
5730 | ||
80154d77 PB |
5731 | if (vmx_rdtscp_supported()) { |
5732 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); | |
5733 | if (!rdtscp_enabled) | |
5734 | exec_control &= ~SECONDARY_EXEC_RDTSCP; | |
5735 | ||
5736 | if (nested) { | |
5737 | if (rdtscp_enabled) | |
5738 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
5739 | SECONDARY_EXEC_RDTSCP; | |
5740 | else | |
5741 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
5742 | ~SECONDARY_EXEC_RDTSCP; | |
5743 | } | |
5744 | } | |
5745 | ||
5746 | if (vmx_invpcid_supported()) { | |
5747 | /* Exposing INVPCID only when PCID is exposed */ | |
5748 | bool invpcid_enabled = | |
5749 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && | |
5750 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); | |
5751 | ||
5752 | if (!invpcid_enabled) { | |
5753 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; | |
5754 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); | |
5755 | } | |
5756 | ||
5757 | if (nested) { | |
5758 | if (invpcid_enabled) | |
5759 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
5760 | SECONDARY_EXEC_ENABLE_INVPCID; | |
5761 | else | |
5762 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
5763 | ~SECONDARY_EXEC_ENABLE_INVPCID; | |
5764 | } | |
5765 | } | |
5766 | ||
45ec368c JM |
5767 | if (vmx_rdrand_supported()) { |
5768 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); | |
5769 | if (rdrand_enabled) | |
736fdf72 | 5770 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5771 | |
5772 | if (nested) { | |
5773 | if (rdrand_enabled) | |
5774 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
736fdf72 | 5775 | SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5776 | else |
5777 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
736fdf72 | 5778 | ~SECONDARY_EXEC_RDRAND_EXITING; |
45ec368c JM |
5779 | } |
5780 | } | |
5781 | ||
75f4fc8d JM |
5782 | if (vmx_rdseed_supported()) { |
5783 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); | |
5784 | if (rdseed_enabled) | |
736fdf72 | 5785 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5786 | |
5787 | if (nested) { | |
5788 | if (rdseed_enabled) | |
5789 | vmx->nested.nested_vmx_secondary_ctls_high |= | |
736fdf72 | 5790 | SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5791 | else |
5792 | vmx->nested.nested_vmx_secondary_ctls_high &= | |
736fdf72 | 5793 | ~SECONDARY_EXEC_RDSEED_EXITING; |
75f4fc8d JM |
5794 | } |
5795 | } | |
5796 | ||
80154d77 | 5797 | vmx->secondary_exec_control = exec_control; |
bf8179a0 NHE |
5798 | } |
5799 | ||
ce88decf XG |
5800 | static void ept_set_mmio_spte_mask(void) |
5801 | { | |
5802 | /* | |
5803 | * EPT Misconfigurations can be generated if the value of bits 2:0 | |
5804 | * of an EPT paging-structure entry is 110b (write/execute). | |
ce88decf | 5805 | */ |
dcdca5fe PF |
5806 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
5807 | VMX_EPT_MISCONFIG_WX_VALUE); | |
ce88decf XG |
5808 | } |
5809 | ||
f53cd63c | 5810 | #define VMX_XSS_EXIT_BITMAP 0 |
6aa8b732 AK |
5811 | /* |
5812 | * Sets up the vmcs for emulated real mode. | |
5813 | */ | |
12d79917 | 5814 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
6aa8b732 | 5815 | { |
2e4ce7f5 | 5816 | #ifdef CONFIG_X86_64 |
6aa8b732 | 5817 | unsigned long a; |
2e4ce7f5 | 5818 | #endif |
6aa8b732 | 5819 | int i; |
6aa8b732 | 5820 | |
6aa8b732 | 5821 | /* I/O */ |
3e7c73e9 AK |
5822 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
5823 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); | |
6aa8b732 | 5824 | |
4607c2d7 AG |
5825 | if (enable_shadow_vmcs) { |
5826 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); | |
5827 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); | |
5828 | } | |
25c5f225 | 5829 | if (cpu_has_vmx_msr_bitmap()) |
4b0be90f | 5830 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
25c5f225 | 5831 | |
6aa8b732 AK |
5832 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
5833 | ||
6aa8b732 | 5834 | /* Control */ |
01e439be | 5835 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); |
64672c95 | 5836 | vmx->hv_deadline_tsc = -1; |
6e5d865c | 5837 | |
bf8179a0 | 5838 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
6aa8b732 | 5839 | |
dfa169bb | 5840 | if (cpu_has_secondary_exec_ctrls()) { |
80154d77 | 5841 | vmx_compute_secondary_exec_control(vmx); |
bf8179a0 | 5842 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
80154d77 | 5843 | vmx->secondary_exec_control); |
dfa169bb | 5844 | } |
f78e0e2e | 5845 | |
d62caabb | 5846 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
c7c9c56c YZ |
5847 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
5848 | vmcs_write64(EOI_EXIT_BITMAP1, 0); | |
5849 | vmcs_write64(EOI_EXIT_BITMAP2, 0); | |
5850 | vmcs_write64(EOI_EXIT_BITMAP3, 0); | |
5851 | ||
5852 | vmcs_write16(GUEST_INTR_STATUS, 0); | |
01e439be | 5853 | |
0bcf261c | 5854 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
01e439be | 5855 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
c7c9c56c YZ |
5856 | } |
5857 | ||
4b8d54f9 ZE |
5858 | if (ple_gap) { |
5859 | vmcs_write32(PLE_GAP, ple_gap); | |
a7653ecd RK |
5860 | vmx->ple_window = ple_window; |
5861 | vmx->ple_window_dirty = true; | |
4b8d54f9 ZE |
5862 | } |
5863 | ||
c3707958 XG |
5864 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
5865 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); | |
6aa8b732 AK |
5866 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
5867 | ||
9581d442 AK |
5868 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
5869 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ | |
a547c6db | 5870 | vmx_set_constant_host_state(vmx); |
05b3e0c2 | 5871 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
5872 | rdmsrl(MSR_FS_BASE, a); |
5873 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ | |
5874 | rdmsrl(MSR_GS_BASE, a); | |
5875 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ | |
5876 | #else | |
5877 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ | |
5878 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ | |
5879 | #endif | |
5880 | ||
2a499e49 BD |
5881 | if (cpu_has_vmx_vmfunc()) |
5882 | vmcs_write64(VM_FUNCTION_CONTROL, 0); | |
5883 | ||
2cc51560 ED |
5884 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
5885 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); | |
6e3dedb6 | 5886 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
2cc51560 | 5887 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
6e3dedb6 | 5888 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
6aa8b732 | 5889 | |
74545705 RK |
5890 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
5891 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); | |
468d472f | 5892 | |
03916db9 | 5893 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
6aa8b732 AK |
5894 | u32 index = vmx_msr_index[i]; |
5895 | u32 data_low, data_high; | |
a2fa3e9f | 5896 | int j = vmx->nmsrs; |
6aa8b732 AK |
5897 | |
5898 | if (rdmsr_safe(index, &data_low, &data_high) < 0) | |
5899 | continue; | |
432bd6cb AK |
5900 | if (wrmsr_safe(index, data_low, data_high) < 0) |
5901 | continue; | |
26bb0981 AK |
5902 | vmx->guest_msrs[j].index = i; |
5903 | vmx->guest_msrs[j].data = 0; | |
d5696725 | 5904 | vmx->guest_msrs[j].mask = -1ull; |
a2fa3e9f | 5905 | ++vmx->nmsrs; |
6aa8b732 | 5906 | } |
6aa8b732 | 5907 | |
2961e876 | 5908 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
6aa8b732 AK |
5909 | |
5910 | /* 22.2.1, 20.8.1 */ | |
2961e876 | 5911 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
1c3d14fe | 5912 | |
bd7e5b08 PB |
5913 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
5914 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); | |
5915 | ||
bf8179a0 | 5916 | set_cr4_guest_host_mask(vmx); |
e00c8cf2 | 5917 | |
f53cd63c WL |
5918 | if (vmx_xsaves_supported()) |
5919 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); | |
5920 | ||
4e59516a PF |
5921 | if (enable_pml) { |
5922 | ASSERT(vmx->pml_pg); | |
5923 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
5924 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
5925 | } | |
e00c8cf2 AK |
5926 | } |
5927 | ||
d28bc9dd | 5928 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
e00c8cf2 AK |
5929 | { |
5930 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
58cb628d | 5931 | struct msr_data apic_base_msr; |
d28bc9dd | 5932 | u64 cr0; |
e00c8cf2 | 5933 | |
7ffd92c5 | 5934 | vmx->rmode.vm86_active = 0; |
74469996 | 5935 | vmx->spec_ctrl = 0; |
e00c8cf2 | 5936 | |
2033c674 | 5937 | vcpu->arch.microcode_version = 0x100000000ULL; |
ad312c7c | 5938 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
d28bc9dd NA |
5939 | kvm_set_cr8(vcpu, 0); |
5940 | ||
5941 | if (!init_event) { | |
5942 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | | |
5943 | MSR_IA32_APICBASE_ENABLE; | |
5944 | if (kvm_vcpu_is_reset_bsp(vcpu)) | |
5945 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; | |
5946 | apic_base_msr.host_initiated = true; | |
5947 | kvm_set_apic_base(vcpu, &apic_base_msr); | |
5948 | } | |
e00c8cf2 | 5949 | |
2fb92db1 AK |
5950 | vmx_segment_cache_clear(vmx); |
5951 | ||
5706be0d | 5952 | seg_setup(VCPU_SREG_CS); |
66450a21 | 5953 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
f3531054 | 5954 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
e00c8cf2 AK |
5955 | |
5956 | seg_setup(VCPU_SREG_DS); | |
5957 | seg_setup(VCPU_SREG_ES); | |
5958 | seg_setup(VCPU_SREG_FS); | |
5959 | seg_setup(VCPU_SREG_GS); | |
5960 | seg_setup(VCPU_SREG_SS); | |
5961 | ||
5962 | vmcs_write16(GUEST_TR_SELECTOR, 0); | |
5963 | vmcs_writel(GUEST_TR_BASE, 0); | |
5964 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); | |
5965 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); | |
5966 | ||
5967 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); | |
5968 | vmcs_writel(GUEST_LDTR_BASE, 0); | |
5969 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); | |
5970 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); | |
5971 | ||
d28bc9dd NA |
5972 | if (!init_event) { |
5973 | vmcs_write32(GUEST_SYSENTER_CS, 0); | |
5974 | vmcs_writel(GUEST_SYSENTER_ESP, 0); | |
5975 | vmcs_writel(GUEST_SYSENTER_EIP, 0); | |
5976 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
5977 | } | |
e00c8cf2 | 5978 | |
c37c2873 | 5979 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
66450a21 | 5980 | kvm_rip_write(vcpu, 0xfff0); |
e00c8cf2 | 5981 | |
e00c8cf2 AK |
5982 | vmcs_writel(GUEST_GDTR_BASE, 0); |
5983 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); | |
5984 | ||
5985 | vmcs_writel(GUEST_IDTR_BASE, 0); | |
5986 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); | |
5987 | ||
443381a8 | 5988 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
e00c8cf2 | 5989 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
f3531054 | 5990 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
a554d207 WL |
5991 | if (kvm_mpx_supported()) |
5992 | vmcs_write64(GUEST_BNDCFGS, 0); | |
e00c8cf2 | 5993 | |
e00c8cf2 AK |
5994 | setup_msrs(vmx); |
5995 | ||
6aa8b732 AK |
5996 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
5997 | ||
d28bc9dd | 5998 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
f78e0e2e | 5999 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
35754c98 | 6000 | if (cpu_need_tpr_shadow(vcpu)) |
f78e0e2e | 6001 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
d28bc9dd | 6002 | __pa(vcpu->arch.apic->regs)); |
f78e0e2e SY |
6003 | vmcs_write32(TPR_THRESHOLD, 0); |
6004 | } | |
6005 | ||
a73896cb | 6006 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
6aa8b732 | 6007 | |
2384d2b3 SY |
6008 | if (vmx->vpid != 0) |
6009 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
6010 | ||
d28bc9dd | 6011 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
d28bc9dd | 6012 | vmx->vcpu.arch.cr0 = cr0; |
f2463247 | 6013 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
d28bc9dd | 6014 | vmx_set_cr4(vcpu, 0); |
5690891b | 6015 | vmx_set_efer(vcpu, 0); |
bd7e5b08 | 6016 | |
d28bc9dd | 6017 | update_exception_bitmap(vcpu); |
6aa8b732 | 6018 | |
dd5f5341 | 6019 | vpid_sync_context(vmx->vpid); |
6aa8b732 AK |
6020 | } |
6021 | ||
b6f1250e NHE |
6022 | /* |
6023 | * In nested virtualization, check if L1 asked to exit on external interrupts. | |
6024 | * For most existing hypervisors, this will always return true. | |
6025 | */ | |
6026 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) | |
6027 | { | |
6028 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & | |
6029 | PIN_BASED_EXT_INTR_MASK; | |
6030 | } | |
6031 | ||
77b0f5d6 BD |
6032 | /* |
6033 | * In nested virtualization, check if L1 has set | |
6034 | * VM_EXIT_ACK_INTR_ON_EXIT | |
6035 | */ | |
6036 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) | |
6037 | { | |
6038 | return get_vmcs12(vcpu)->vm_exit_controls & | |
6039 | VM_EXIT_ACK_INTR_ON_EXIT; | |
6040 | } | |
6041 | ||
ea8ceb83 JK |
6042 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
6043 | { | |
6044 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & | |
6045 | PIN_BASED_NMI_EXITING; | |
6046 | } | |
6047 | ||
c9a7953f | 6048 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 6049 | { |
47c0152e PB |
6050 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
6051 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
3b86cd99 JK |
6052 | } |
6053 | ||
c9a7953f | 6054 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
3b86cd99 | 6055 | { |
d02fcf50 | 6056 | if (!enable_vnmi || |
8a1b4392 | 6057 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
c9a7953f JK |
6058 | enable_irq_window(vcpu); |
6059 | return; | |
6060 | } | |
3b86cd99 | 6061 | |
47c0152e PB |
6062 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
6063 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
3b86cd99 JK |
6064 | } |
6065 | ||
66fd3f7f | 6066 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
85f455f7 | 6067 | { |
9c8cba37 | 6068 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
66fd3f7f GN |
6069 | uint32_t intr; |
6070 | int irq = vcpu->arch.interrupt.nr; | |
9c8cba37 | 6071 | |
229456fc | 6072 | trace_kvm_inj_virq(irq); |
2714d1d3 | 6073 | |
fa89a817 | 6074 | ++vcpu->stat.irq_injections; |
7ffd92c5 | 6075 | if (vmx->rmode.vm86_active) { |
71f9833b SH |
6076 | int inc_eip = 0; |
6077 | if (vcpu->arch.interrupt.soft) | |
6078 | inc_eip = vcpu->arch.event_exit_inst_len; | |
6079 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) | |
a92601bb | 6080 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
85f455f7 ED |
6081 | return; |
6082 | } | |
66fd3f7f GN |
6083 | intr = irq | INTR_INFO_VALID_MASK; |
6084 | if (vcpu->arch.interrupt.soft) { | |
6085 | intr |= INTR_TYPE_SOFT_INTR; | |
6086 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
6087 | vmx->vcpu.arch.event_exit_inst_len); | |
6088 | } else | |
6089 | intr |= INTR_TYPE_EXT_INTR; | |
6090 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); | |
85f455f7 ED |
6091 | } |
6092 | ||
f08864b4 SY |
6093 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
6094 | { | |
66a5a347 JK |
6095 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6096 | ||
d02fcf50 | 6097 | if (!enable_vnmi) { |
8a1b4392 PB |
6098 | /* |
6099 | * Tracking the NMI-blocked state in software is built upon | |
6100 | * finding the next open IRQ window. This, in turn, depends on | |
6101 | * well-behaving guests: They have to keep IRQs disabled at | |
6102 | * least as long as the NMI handler runs. Otherwise we may | |
6103 | * cause NMI nesting, maybe breaking the guest. But as this is | |
6104 | * highly unlikely, we can live with the residual risk. | |
6105 | */ | |
6106 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; | |
6107 | vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
6108 | } | |
6109 | ||
4c4a6f79 PB |
6110 | ++vcpu->stat.nmi_injections; |
6111 | vmx->loaded_vmcs->nmi_known_unmasked = false; | |
3b86cd99 | 6112 | |
7ffd92c5 | 6113 | if (vmx->rmode.vm86_active) { |
71f9833b | 6114 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
a92601bb | 6115 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
66a5a347 JK |
6116 | return; |
6117 | } | |
c5a6d5f7 | 6118 | |
f08864b4 SY |
6119 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
6120 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); | |
f08864b4 SY |
6121 | } |
6122 | ||
3cfc3092 JK |
6123 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
6124 | { | |
4c4a6f79 PB |
6125 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6126 | bool masked; | |
6127 | ||
d02fcf50 | 6128 | if (!enable_vnmi) |
8a1b4392 | 6129 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
4c4a6f79 | 6130 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
9d58b931 | 6131 | return false; |
4c4a6f79 PB |
6132 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
6133 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
6134 | return masked; | |
3cfc3092 JK |
6135 | } |
6136 | ||
6137 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) | |
6138 | { | |
6139 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
6140 | ||
d02fcf50 | 6141 | if (!enable_vnmi) { |
8a1b4392 PB |
6142 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
6143 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; | |
6144 | vmx->loaded_vmcs->vnmi_blocked_time = 0; | |
6145 | } | |
6146 | } else { | |
6147 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; | |
6148 | if (masked) | |
6149 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
6150 | GUEST_INTR_STATE_NMI); | |
6151 | else | |
6152 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, | |
6153 | GUEST_INTR_STATE_NMI); | |
6154 | } | |
3cfc3092 JK |
6155 | } |
6156 | ||
2505dc9f JK |
6157 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
6158 | { | |
b6b8a145 JK |
6159 | if (to_vmx(vcpu)->nested.nested_run_pending) |
6160 | return 0; | |
ea8ceb83 | 6161 | |
d02fcf50 | 6162 | if (!enable_vnmi && |
8a1b4392 PB |
6163 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
6164 | return 0; | |
6165 | ||
2505dc9f JK |
6166 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
6167 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI | |
6168 | | GUEST_INTR_STATE_NMI)); | |
6169 | } | |
6170 | ||
78646121 GN |
6171 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
6172 | { | |
b6b8a145 JK |
6173 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
6174 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && | |
c4282df9 GN |
6175 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
6176 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); | |
78646121 GN |
6177 | } |
6178 | ||
cbc94022 IE |
6179 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
6180 | { | |
6181 | int ret; | |
cbc94022 | 6182 | |
1d8007bd PB |
6183 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
6184 | PAGE_SIZE * 3); | |
cbc94022 IE |
6185 | if (ret) |
6186 | return ret; | |
bfc6d222 | 6187 | kvm->arch.tss_addr = addr; |
1f755a82 | 6188 | return init_rmode_tss(kvm); |
cbc94022 IE |
6189 | } |
6190 | ||
0ca1b4f4 | 6191 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
6aa8b732 | 6192 | { |
77ab6db0 | 6193 | switch (vec) { |
77ab6db0 | 6194 | case BP_VECTOR: |
c573cd22 JK |
6195 | /* |
6196 | * Update instruction length as we may reinject the exception | |
6197 | * from user space while in guest debugging mode. | |
6198 | */ | |
6199 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = | |
6200 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
d0bfb940 | 6201 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
0ca1b4f4 GN |
6202 | return false; |
6203 | /* fall through */ | |
6204 | case DB_VECTOR: | |
6205 | if (vcpu->guest_debug & | |
6206 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
6207 | return false; | |
d0bfb940 JK |
6208 | /* fall through */ |
6209 | case DE_VECTOR: | |
77ab6db0 JK |
6210 | case OF_VECTOR: |
6211 | case BR_VECTOR: | |
6212 | case UD_VECTOR: | |
6213 | case DF_VECTOR: | |
6214 | case SS_VECTOR: | |
6215 | case GP_VECTOR: | |
6216 | case MF_VECTOR: | |
0ca1b4f4 GN |
6217 | return true; |
6218 | break; | |
77ab6db0 | 6219 | } |
0ca1b4f4 GN |
6220 | return false; |
6221 | } | |
6222 | ||
6223 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, | |
6224 | int vec, u32 err_code) | |
6225 | { | |
6226 | /* | |
6227 | * Instruction with address size override prefix opcode 0x67 | |
6228 | * Cause the #SS fault with 0 error code in VM86 mode. | |
6229 | */ | |
6230 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { | |
6231 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { | |
6232 | if (vcpu->arch.halt_request) { | |
6233 | vcpu->arch.halt_request = 0; | |
5cb56059 | 6234 | return kvm_vcpu_halt(vcpu); |
0ca1b4f4 GN |
6235 | } |
6236 | return 1; | |
6237 | } | |
6238 | return 0; | |
6239 | } | |
6240 | ||
6241 | /* | |
6242 | * Forward all other exceptions that are valid in real mode. | |
6243 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with | |
6244 | * the required debugging infrastructure rework. | |
6245 | */ | |
6246 | kvm_queue_exception(vcpu, vec); | |
6247 | return 1; | |
6aa8b732 AK |
6248 | } |
6249 | ||
a0861c02 AK |
6250 | /* |
6251 | * Trigger machine check on the host. We assume all the MSRs are already set up | |
6252 | * by the CPU and that we still run on the same CPU as the MCE occurred on. | |
6253 | * We pass a fake environment to the machine check handler because we want | |
6254 | * the guest to be always treated like user space, no matter what context | |
6255 | * it used internally. | |
6256 | */ | |
6257 | static void kvm_machine_check(void) | |
6258 | { | |
6259 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) | |
6260 | struct pt_regs regs = { | |
6261 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ | |
6262 | .flags = X86_EFLAGS_IF, | |
6263 | }; | |
6264 | ||
6265 | do_machine_check(®s, 0); | |
6266 | #endif | |
6267 | } | |
6268 | ||
851ba692 | 6269 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
a0861c02 AK |
6270 | { |
6271 | /* already handled by vcpu_run */ | |
6272 | return 1; | |
6273 | } | |
6274 | ||
851ba692 | 6275 | static int handle_exception(struct kvm_vcpu *vcpu) |
6aa8b732 | 6276 | { |
1155f76a | 6277 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
851ba692 | 6278 | struct kvm_run *kvm_run = vcpu->run; |
d0bfb940 | 6279 | u32 intr_info, ex_no, error_code; |
42dbaa5a | 6280 | unsigned long cr2, rip, dr6; |
6aa8b732 AK |
6281 | u32 vect_info; |
6282 | enum emulation_result er; | |
6283 | ||
1155f76a | 6284 | vect_info = vmx->idt_vectoring_info; |
88786475 | 6285 | intr_info = vmx->exit_intr_info; |
6aa8b732 | 6286 | |
a0861c02 | 6287 | if (is_machine_check(intr_info)) |
851ba692 | 6288 | return handle_machine_check(vcpu); |
a0861c02 | 6289 | |
ef85b673 | 6290 | if (is_nmi(intr_info)) |
1b6269db | 6291 | return 1; /* already handled by vmx_vcpu_run() */ |
2ab455cc | 6292 | |
7aa81cc0 | 6293 | if (is_invalid_opcode(intr_info)) { |
51d8b661 | 6294 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
61cb57c9 LA |
6295 | if (er == EMULATE_USER_EXIT) |
6296 | return 0; | |
7aa81cc0 | 6297 | if (er != EMULATE_DONE) |
7ee5d940 | 6298 | kvm_queue_exception(vcpu, UD_VECTOR); |
7aa81cc0 AL |
6299 | return 1; |
6300 | } | |
6301 | ||
6aa8b732 | 6302 | error_code = 0; |
2e11384c | 6303 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
6aa8b732 | 6304 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
bf4ca23e XG |
6305 | |
6306 | /* | |
6307 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing | |
6308 | * MMIO, it is better to report an internal error. | |
6309 | * See the comments in vmx_handle_exit. | |
6310 | */ | |
6311 | if ((vect_info & VECTORING_INFO_VALID_MASK) && | |
6312 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { | |
6313 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
6314 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; | |
80f0e95d | 6315 | vcpu->run->internal.ndata = 3; |
bf4ca23e XG |
6316 | vcpu->run->internal.data[0] = vect_info; |
6317 | vcpu->run->internal.data[1] = intr_info; | |
80f0e95d | 6318 | vcpu->run->internal.data[2] = error_code; |
bf4ca23e XG |
6319 | return 0; |
6320 | } | |
6321 | ||
6aa8b732 AK |
6322 | if (is_page_fault(intr_info)) { |
6323 | cr2 = vmcs_readl(EXIT_QUALIFICATION); | |
1261bfa3 WL |
6324 | /* EPT won't cause page fault directly */ |
6325 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); | |
d0006530 | 6326 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
6aa8b732 AK |
6327 | } |
6328 | ||
d0bfb940 | 6329 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
0ca1b4f4 GN |
6330 | |
6331 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) | |
6332 | return handle_rmode_exception(vcpu, ex_no, error_code); | |
6333 | ||
42dbaa5a | 6334 | switch (ex_no) { |
54a20552 EN |
6335 | case AC_VECTOR: |
6336 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); | |
6337 | return 1; | |
42dbaa5a JK |
6338 | case DB_VECTOR: |
6339 | dr6 = vmcs_readl(EXIT_QUALIFICATION); | |
6340 | if (!(vcpu->guest_debug & | |
6341 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { | |
8246bf52 | 6342 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 6343 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
3252850d | 6344 | if (is_icebp(intr_info)) |
fd2a445a HD |
6345 | skip_emulated_instruction(vcpu); |
6346 | ||
42dbaa5a JK |
6347 | kvm_queue_exception(vcpu, DB_VECTOR); |
6348 | return 1; | |
6349 | } | |
6350 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; | |
6351 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); | |
6352 | /* fall through */ | |
6353 | case BP_VECTOR: | |
c573cd22 JK |
6354 | /* |
6355 | * Update instruction length as we may reinject #BP from | |
6356 | * user space while in guest debugging mode. Reading it for | |
6357 | * #DB as well causes no harm, it is not used in that case. | |
6358 | */ | |
6359 | vmx->vcpu.arch.event_exit_inst_len = | |
6360 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
6aa8b732 | 6361 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
0a434bb2 | 6362 | rip = kvm_rip_read(vcpu); |
d0bfb940 JK |
6363 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
6364 | kvm_run->debug.arch.exception = ex_no; | |
42dbaa5a JK |
6365 | break; |
6366 | default: | |
d0bfb940 JK |
6367 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
6368 | kvm_run->ex.exception = ex_no; | |
6369 | kvm_run->ex.error_code = error_code; | |
42dbaa5a | 6370 | break; |
6aa8b732 | 6371 | } |
6aa8b732 AK |
6372 | return 0; |
6373 | } | |
6374 | ||
851ba692 | 6375 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
6aa8b732 | 6376 | { |
1165f5fe | 6377 | ++vcpu->stat.irq_exits; |
6aa8b732 AK |
6378 | return 1; |
6379 | } | |
6380 | ||
851ba692 | 6381 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
988ad74f | 6382 | { |
851ba692 | 6383 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
bbeac283 | 6384 | vcpu->mmio_needed = 0; |
988ad74f AK |
6385 | return 0; |
6386 | } | |
6aa8b732 | 6387 | |
851ba692 | 6388 | static int handle_io(struct kvm_vcpu *vcpu) |
6aa8b732 | 6389 | { |
bfdaab09 | 6390 | unsigned long exit_qualification; |
6affcbed | 6391 | int size, in, string, ret; |
039576c0 | 6392 | unsigned port; |
6aa8b732 | 6393 | |
bfdaab09 | 6394 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
039576c0 | 6395 | string = (exit_qualification & 16) != 0; |
cf8f70bf | 6396 | in = (exit_qualification & 8) != 0; |
e70669ab | 6397 | |
cf8f70bf | 6398 | ++vcpu->stat.io_exits; |
e70669ab | 6399 | |
cf8f70bf | 6400 | if (string || in) |
51d8b661 | 6401 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
e70669ab | 6402 | |
cf8f70bf GN |
6403 | port = exit_qualification >> 16; |
6404 | size = (exit_qualification & 7) + 1; | |
cf8f70bf | 6405 | |
6affcbed KH |
6406 | ret = kvm_skip_emulated_instruction(vcpu); |
6407 | ||
6408 | /* | |
6409 | * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered | |
6410 | * KVM_EXIT_DEBUG here. | |
6411 | */ | |
6412 | return kvm_fast_pio_out(vcpu, size, port) && ret; | |
6aa8b732 AK |
6413 | } |
6414 | ||
102d8325 IM |
6415 | static void |
6416 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) | |
6417 | { | |
6418 | /* | |
6419 | * Patch in the VMCALL instruction: | |
6420 | */ | |
6421 | hypercall[0] = 0x0f; | |
6422 | hypercall[1] = 0x01; | |
6423 | hypercall[2] = 0xc1; | |
102d8325 IM |
6424 | } |
6425 | ||
0fa06071 | 6426 | /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ |
eeadf9e7 NHE |
6427 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
6428 | { | |
eeadf9e7 | 6429 | if (is_guest_mode(vcpu)) { |
1a0d74e6 JK |
6430 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
6431 | unsigned long orig_val = val; | |
6432 | ||
eeadf9e7 NHE |
6433 | /* |
6434 | * We get here when L2 changed cr0 in a way that did not change | |
6435 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), | |
1a0d74e6 JK |
6436 | * but did change L0 shadowed bits. So we first calculate the |
6437 | * effective cr0 value that L1 would like to write into the | |
6438 | * hardware. It consists of the L2-owned bits from the new | |
6439 | * value combined with the L1-owned bits from L1's guest_cr0. | |
eeadf9e7 | 6440 | */ |
1a0d74e6 JK |
6441 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
6442 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); | |
6443 | ||
3899152c | 6444 | if (!nested_guest_cr0_valid(vcpu, val)) |
eeadf9e7 | 6445 | return 1; |
1a0d74e6 JK |
6446 | |
6447 | if (kvm_set_cr0(vcpu, val)) | |
6448 | return 1; | |
6449 | vmcs_writel(CR0_READ_SHADOW, orig_val); | |
eeadf9e7 | 6450 | return 0; |
1a0d74e6 JK |
6451 | } else { |
6452 | if (to_vmx(vcpu)->nested.vmxon && | |
3899152c | 6453 | !nested_host_cr0_valid(vcpu, val)) |
1a0d74e6 | 6454 | return 1; |
3899152c | 6455 | |
eeadf9e7 | 6456 | return kvm_set_cr0(vcpu, val); |
1a0d74e6 | 6457 | } |
eeadf9e7 NHE |
6458 | } |
6459 | ||
6460 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) | |
6461 | { | |
6462 | if (is_guest_mode(vcpu)) { | |
1a0d74e6 JK |
6463 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
6464 | unsigned long orig_val = val; | |
6465 | ||
6466 | /* analogously to handle_set_cr0 */ | |
6467 | val = (val & ~vmcs12->cr4_guest_host_mask) | | |
6468 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); | |
6469 | if (kvm_set_cr4(vcpu, val)) | |
eeadf9e7 | 6470 | return 1; |
1a0d74e6 | 6471 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
eeadf9e7 NHE |
6472 | return 0; |
6473 | } else | |
6474 | return kvm_set_cr4(vcpu, val); | |
6475 | } | |
6476 | ||
851ba692 | 6477 | static int handle_cr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6478 | { |
229456fc | 6479 | unsigned long exit_qualification, val; |
6aa8b732 AK |
6480 | int cr; |
6481 | int reg; | |
49a9b07e | 6482 | int err; |
6affcbed | 6483 | int ret; |
6aa8b732 | 6484 | |
bfdaab09 | 6485 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
6aa8b732 AK |
6486 | cr = exit_qualification & 15; |
6487 | reg = (exit_qualification >> 8) & 15; | |
6488 | switch ((exit_qualification >> 4) & 3) { | |
6489 | case 0: /* mov to cr */ | |
1e32c079 | 6490 | val = kvm_register_readl(vcpu, reg); |
229456fc | 6491 | trace_kvm_cr_write(cr, val); |
6aa8b732 AK |
6492 | switch (cr) { |
6493 | case 0: | |
eeadf9e7 | 6494 | err = handle_set_cr0(vcpu, val); |
6affcbed | 6495 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 6496 | case 3: |
2390218b | 6497 | err = kvm_set_cr3(vcpu, val); |
6affcbed | 6498 | return kvm_complete_insn_gp(vcpu, err); |
6aa8b732 | 6499 | case 4: |
eeadf9e7 | 6500 | err = handle_set_cr4(vcpu, val); |
6affcbed | 6501 | return kvm_complete_insn_gp(vcpu, err); |
0a5fff19 GN |
6502 | case 8: { |
6503 | u8 cr8_prev = kvm_get_cr8(vcpu); | |
1e32c079 | 6504 | u8 cr8 = (u8)val; |
eea1cff9 | 6505 | err = kvm_set_cr8(vcpu, cr8); |
6affcbed | 6506 | ret = kvm_complete_insn_gp(vcpu, err); |
35754c98 | 6507 | if (lapic_in_kernel(vcpu)) |
6affcbed | 6508 | return ret; |
0a5fff19 | 6509 | if (cr8_prev <= cr8) |
6affcbed KH |
6510 | return ret; |
6511 | /* | |
6512 | * TODO: we might be squashing a | |
6513 | * KVM_GUESTDBG_SINGLESTEP-triggered | |
6514 | * KVM_EXIT_DEBUG here. | |
6515 | */ | |
851ba692 | 6516 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
0a5fff19 GN |
6517 | return 0; |
6518 | } | |
4b8073e4 | 6519 | } |
6aa8b732 | 6520 | break; |
25c4c276 | 6521 | case 2: /* clts */ |
bd7e5b08 PB |
6522 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
6523 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); | |
4d4ec087 | 6524 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
6affcbed | 6525 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6526 | case 1: /*mov from cr*/ |
6527 | switch (cr) { | |
6528 | case 3: | |
9f8fe504 AK |
6529 | val = kvm_read_cr3(vcpu); |
6530 | kvm_register_write(vcpu, reg, val); | |
6531 | trace_kvm_cr_read(cr, val); | |
6affcbed | 6532 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 | 6533 | case 8: |
229456fc MT |
6534 | val = kvm_get_cr8(vcpu); |
6535 | kvm_register_write(vcpu, reg, val); | |
6536 | trace_kvm_cr_read(cr, val); | |
6affcbed | 6537 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6538 | } |
6539 | break; | |
6540 | case 3: /* lmsw */ | |
a1f83a74 | 6541 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
4d4ec087 | 6542 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
a1f83a74 | 6543 | kvm_lmsw(vcpu, val); |
6aa8b732 | 6544 | |
6affcbed | 6545 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6546 | default: |
6547 | break; | |
6548 | } | |
851ba692 | 6549 | vcpu->run->exit_reason = 0; |
a737f256 | 6550 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
6aa8b732 AK |
6551 | (int)(exit_qualification >> 4) & 3, cr); |
6552 | return 0; | |
6553 | } | |
6554 | ||
851ba692 | 6555 | static int handle_dr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6556 | { |
bfdaab09 | 6557 | unsigned long exit_qualification; |
16f8a6f9 NA |
6558 | int dr, dr7, reg; |
6559 | ||
6560 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6561 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; | |
6562 | ||
6563 | /* First, if DR does not exist, trigger UD */ | |
6564 | if (!kvm_require_dr(vcpu, dr)) | |
6565 | return 1; | |
6aa8b732 | 6566 | |
f2483415 | 6567 | /* Do not handle if the CPL > 0, will trigger GP on re-entry */ |
0a79b009 AK |
6568 | if (!kvm_require_cpl(vcpu, 0)) |
6569 | return 1; | |
16f8a6f9 NA |
6570 | dr7 = vmcs_readl(GUEST_DR7); |
6571 | if (dr7 & DR7_GD) { | |
42dbaa5a JK |
6572 | /* |
6573 | * As the vm-exit takes precedence over the debug trap, we | |
6574 | * need to emulate the latter, either for the host or the | |
6575 | * guest debugging itself. | |
6576 | */ | |
6577 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { | |
851ba692 | 6578 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
16f8a6f9 | 6579 | vcpu->run->debug.arch.dr7 = dr7; |
82b32774 | 6580 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
851ba692 AK |
6581 | vcpu->run->debug.arch.exception = DB_VECTOR; |
6582 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; | |
42dbaa5a JK |
6583 | return 0; |
6584 | } else { | |
7305eb5d | 6585 | vcpu->arch.dr6 &= ~15; |
6f43ed01 | 6586 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
42dbaa5a JK |
6587 | kvm_queue_exception(vcpu, DB_VECTOR); |
6588 | return 1; | |
6589 | } | |
6590 | } | |
6591 | ||
81908bf4 | 6592 | if (vcpu->guest_debug == 0) { |
8f22372f PB |
6593 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6594 | CPU_BASED_MOV_DR_EXITING); | |
81908bf4 PB |
6595 | |
6596 | /* | |
6597 | * No more DR vmexits; force a reload of the debug registers | |
6598 | * and reenter on this instruction. The next vmexit will | |
6599 | * retrieve the full state of the debug registers. | |
6600 | */ | |
6601 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; | |
6602 | return 1; | |
6603 | } | |
6604 | ||
42dbaa5a JK |
6605 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
6606 | if (exit_qualification & TYPE_MOV_FROM_DR) { | |
020df079 | 6607 | unsigned long val; |
4c4d563b JK |
6608 | |
6609 | if (kvm_get_dr(vcpu, dr, &val)) | |
6610 | return 1; | |
6611 | kvm_register_write(vcpu, reg, val); | |
020df079 | 6612 | } else |
5777392e | 6613 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
4c4d563b JK |
6614 | return 1; |
6615 | ||
6affcbed | 6616 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6617 | } |
6618 | ||
73aaf249 JK |
6619 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
6620 | { | |
6621 | return vcpu->arch.dr6; | |
6622 | } | |
6623 | ||
6624 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) | |
6625 | { | |
6626 | } | |
6627 | ||
81908bf4 PB |
6628 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
6629 | { | |
81908bf4 PB |
6630 | get_debugreg(vcpu->arch.db[0], 0); |
6631 | get_debugreg(vcpu->arch.db[1], 1); | |
6632 | get_debugreg(vcpu->arch.db[2], 2); | |
6633 | get_debugreg(vcpu->arch.db[3], 3); | |
6634 | get_debugreg(vcpu->arch.dr6, 6); | |
6635 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); | |
6636 | ||
6637 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; | |
8f22372f | 6638 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING); |
81908bf4 PB |
6639 | } |
6640 | ||
020df079 GN |
6641 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
6642 | { | |
6643 | vmcs_writel(GUEST_DR7, val); | |
6644 | } | |
6645 | ||
851ba692 | 6646 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
6aa8b732 | 6647 | { |
6a908b62 | 6648 | return kvm_emulate_cpuid(vcpu); |
6aa8b732 AK |
6649 | } |
6650 | ||
851ba692 | 6651 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6652 | { |
ad312c7c | 6653 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
609e36d3 | 6654 | struct msr_data msr_info; |
6aa8b732 | 6655 | |
609e36d3 PB |
6656 | msr_info.index = ecx; |
6657 | msr_info.host_initiated = false; | |
6658 | if (vmx_get_msr(vcpu, &msr_info)) { | |
59200273 | 6659 | trace_kvm_msr_read_ex(ecx); |
c1a5d4f9 | 6660 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
6661 | return 1; |
6662 | } | |
6663 | ||
609e36d3 | 6664 | trace_kvm_msr_read(ecx, msr_info.data); |
2714d1d3 | 6665 | |
6aa8b732 | 6666 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
609e36d3 PB |
6667 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
6668 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; | |
6affcbed | 6669 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6670 | } |
6671 | ||
851ba692 | 6672 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
6aa8b732 | 6673 | { |
8fe8ab46 | 6674 | struct msr_data msr; |
ad312c7c ZX |
6675 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
6676 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) | |
6677 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); | |
6aa8b732 | 6678 | |
8fe8ab46 WA |
6679 | msr.data = data; |
6680 | msr.index = ecx; | |
6681 | msr.host_initiated = false; | |
854e8bb1 | 6682 | if (kvm_set_msr(vcpu, &msr) != 0) { |
59200273 | 6683 | trace_kvm_msr_write_ex(ecx, data); |
c1a5d4f9 | 6684 | kvm_inject_gp(vcpu, 0); |
6aa8b732 AK |
6685 | return 1; |
6686 | } | |
6687 | ||
59200273 | 6688 | trace_kvm_msr_write(ecx, data); |
6affcbed | 6689 | return kvm_skip_emulated_instruction(vcpu); |
6aa8b732 AK |
6690 | } |
6691 | ||
851ba692 | 6692 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
6e5d865c | 6693 | { |
eb90f341 | 6694 | kvm_apic_update_ppr(vcpu); |
6e5d865c YS |
6695 | return 1; |
6696 | } | |
6697 | ||
851ba692 | 6698 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
6aa8b732 | 6699 | { |
47c0152e PB |
6700 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6701 | CPU_BASED_VIRTUAL_INTR_PENDING); | |
2714d1d3 | 6702 | |
3842d135 AK |
6703 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
6704 | ||
a26bf12a | 6705 | ++vcpu->stat.irq_window_exits; |
6aa8b732 AK |
6706 | return 1; |
6707 | } | |
6708 | ||
851ba692 | 6709 | static int handle_halt(struct kvm_vcpu *vcpu) |
6aa8b732 | 6710 | { |
d3bef15f | 6711 | return kvm_emulate_halt(vcpu); |
6aa8b732 AK |
6712 | } |
6713 | ||
851ba692 | 6714 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
c21415e8 | 6715 | { |
0d9c055e | 6716 | return kvm_emulate_hypercall(vcpu); |
c21415e8 IM |
6717 | } |
6718 | ||
ec25d5e6 GN |
6719 | static int handle_invd(struct kvm_vcpu *vcpu) |
6720 | { | |
51d8b661 | 6721 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
ec25d5e6 GN |
6722 | } |
6723 | ||
851ba692 | 6724 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
a7052897 | 6725 | { |
f9c617f6 | 6726 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
a7052897 MT |
6727 | |
6728 | kvm_mmu_invlpg(vcpu, exit_qualification); | |
6affcbed | 6729 | return kvm_skip_emulated_instruction(vcpu); |
a7052897 MT |
6730 | } |
6731 | ||
fee84b07 AK |
6732 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
6733 | { | |
6734 | int err; | |
6735 | ||
6736 | err = kvm_rdpmc(vcpu); | |
6affcbed | 6737 | return kvm_complete_insn_gp(vcpu, err); |
fee84b07 AK |
6738 | } |
6739 | ||
851ba692 | 6740 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
e5edaa01 | 6741 | { |
6affcbed | 6742 | return kvm_emulate_wbinvd(vcpu); |
e5edaa01 ED |
6743 | } |
6744 | ||
2acf923e DC |
6745 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
6746 | { | |
6747 | u64 new_bv = kvm_read_edx_eax(vcpu); | |
6748 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); | |
6749 | ||
6750 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) | |
6affcbed | 6751 | return kvm_skip_emulated_instruction(vcpu); |
2acf923e DC |
6752 | return 1; |
6753 | } | |
6754 | ||
f53cd63c WL |
6755 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
6756 | { | |
6affcbed | 6757 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6758 | WARN(1, "this should never happen\n"); |
6759 | return 1; | |
6760 | } | |
6761 | ||
6762 | static int handle_xrstors(struct kvm_vcpu *vcpu) | |
6763 | { | |
6affcbed | 6764 | kvm_skip_emulated_instruction(vcpu); |
f53cd63c WL |
6765 | WARN(1, "this should never happen\n"); |
6766 | return 1; | |
6767 | } | |
6768 | ||
851ba692 | 6769 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
f78e0e2e | 6770 | { |
58fbbf26 KT |
6771 | if (likely(fasteoi)) { |
6772 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6773 | int access_type, offset; | |
6774 | ||
6775 | access_type = exit_qualification & APIC_ACCESS_TYPE; | |
6776 | offset = exit_qualification & APIC_ACCESS_OFFSET; | |
6777 | /* | |
6778 | * Sane guest uses MOV to write EOI, with written value | |
6779 | * not cared. So make a short-circuit here by avoiding | |
6780 | * heavy instruction emulation. | |
6781 | */ | |
6782 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && | |
6783 | (offset == APIC_EOI)) { | |
6784 | kvm_lapic_set_eoi(vcpu); | |
6affcbed | 6785 | return kvm_skip_emulated_instruction(vcpu); |
58fbbf26 KT |
6786 | } |
6787 | } | |
51d8b661 | 6788 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
f78e0e2e SY |
6789 | } |
6790 | ||
c7c9c56c YZ |
6791 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
6792 | { | |
6793 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6794 | int vector = exit_qualification & 0xff; | |
6795 | ||
6796 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ | |
6797 | kvm_apic_set_eoi_accelerated(vcpu, vector); | |
6798 | return 1; | |
6799 | } | |
6800 | ||
83d4c286 YZ |
6801 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
6802 | { | |
6803 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6804 | u32 offset = exit_qualification & 0xfff; | |
6805 | ||
6806 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ | |
6807 | kvm_apic_write_nodecode(vcpu, offset); | |
6808 | return 1; | |
6809 | } | |
6810 | ||
851ba692 | 6811 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
37817f29 | 6812 | { |
60637aac | 6813 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
37817f29 | 6814 | unsigned long exit_qualification; |
e269fb21 JK |
6815 | bool has_error_code = false; |
6816 | u32 error_code = 0; | |
37817f29 | 6817 | u16 tss_selector; |
7f3d35fd | 6818 | int reason, type, idt_v, idt_index; |
64a7ec06 GN |
6819 | |
6820 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); | |
7f3d35fd | 6821 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
64a7ec06 | 6822 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
37817f29 IE |
6823 | |
6824 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
6825 | ||
6826 | reason = (u32)exit_qualification >> 30; | |
64a7ec06 GN |
6827 | if (reason == TASK_SWITCH_GATE && idt_v) { |
6828 | switch (type) { | |
6829 | case INTR_TYPE_NMI_INTR: | |
6830 | vcpu->arch.nmi_injected = false; | |
654f06fc | 6831 | vmx_set_nmi_mask(vcpu, true); |
64a7ec06 GN |
6832 | break; |
6833 | case INTR_TYPE_EXT_INTR: | |
66fd3f7f | 6834 | case INTR_TYPE_SOFT_INTR: |
64a7ec06 GN |
6835 | kvm_clear_interrupt_queue(vcpu); |
6836 | break; | |
6837 | case INTR_TYPE_HARD_EXCEPTION: | |
e269fb21 JK |
6838 | if (vmx->idt_vectoring_info & |
6839 | VECTORING_INFO_DELIVER_CODE_MASK) { | |
6840 | has_error_code = true; | |
6841 | error_code = | |
6842 | vmcs_read32(IDT_VECTORING_ERROR_CODE); | |
6843 | } | |
6844 | /* fall through */ | |
64a7ec06 GN |
6845 | case INTR_TYPE_SOFT_EXCEPTION: |
6846 | kvm_clear_exception_queue(vcpu); | |
6847 | break; | |
6848 | default: | |
6849 | break; | |
6850 | } | |
60637aac | 6851 | } |
37817f29 IE |
6852 | tss_selector = exit_qualification; |
6853 | ||
64a7ec06 GN |
6854 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
6855 | type != INTR_TYPE_EXT_INTR && | |
6856 | type != INTR_TYPE_NMI_INTR)) | |
6857 | skip_emulated_instruction(vcpu); | |
6858 | ||
7f3d35fd KW |
6859 | if (kvm_task_switch(vcpu, tss_selector, |
6860 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, | |
6861 | has_error_code, error_code) == EMULATE_FAIL) { | |
acb54517 GN |
6862 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
6863 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6864 | vcpu->run->internal.ndata = 0; | |
42dbaa5a | 6865 | return 0; |
acb54517 | 6866 | } |
42dbaa5a | 6867 | |
42dbaa5a JK |
6868 | /* |
6869 | * TODO: What about debug traps on tss switch? | |
6870 | * Are we supposed to inject them and update dr6? | |
6871 | */ | |
6872 | ||
6873 | return 1; | |
37817f29 IE |
6874 | } |
6875 | ||
851ba692 | 6876 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
1439442c | 6877 | { |
f9c617f6 | 6878 | unsigned long exit_qualification; |
1439442c | 6879 | gpa_t gpa; |
eebed243 | 6880 | u64 error_code; |
1439442c | 6881 | |
f9c617f6 | 6882 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
1439442c | 6883 | |
0be9c7a8 GN |
6884 | /* |
6885 | * EPT violation happened while executing iret from NMI, | |
6886 | * "blocked by NMI" bit has to be set before next VM entry. | |
6887 | * There are errata that may cause this bit to not be set: | |
6888 | * AAK134, BY25. | |
6889 | */ | |
bcd1c294 | 6890 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
d02fcf50 | 6891 | enable_vnmi && |
bcd1c294 | 6892 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
0be9c7a8 GN |
6893 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
6894 | ||
1439442c | 6895 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
229456fc | 6896 | trace_kvm_page_fault(gpa, exit_qualification); |
4f5982a5 | 6897 | |
27959a44 | 6898 | /* Is it a read fault? */ |
ab22a473 | 6899 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
27959a44 JS |
6900 | ? PFERR_USER_MASK : 0; |
6901 | /* Is it a write fault? */ | |
ab22a473 | 6902 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
27959a44 JS |
6903 | ? PFERR_WRITE_MASK : 0; |
6904 | /* Is it a fetch fault? */ | |
ab22a473 | 6905 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
27959a44 JS |
6906 | ? PFERR_FETCH_MASK : 0; |
6907 | /* ept page table entry is present? */ | |
6908 | error_code |= (exit_qualification & | |
6909 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | | |
6910 | EPT_VIOLATION_EXECUTABLE)) | |
6911 | ? PFERR_PRESENT_MASK : 0; | |
4f5982a5 | 6912 | |
eebed243 PB |
6913 | error_code |= (exit_qualification & 0x100) != 0 ? |
6914 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; | |
25d92081 | 6915 | |
25d92081 | 6916 | vcpu->arch.exit_qualification = exit_qualification; |
4f5982a5 | 6917 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
1439442c SY |
6918 | } |
6919 | ||
851ba692 | 6920 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
68f89400 | 6921 | { |
f735d4af | 6922 | int ret; |
68f89400 MT |
6923 | gpa_t gpa; |
6924 | ||
9034e6e8 PB |
6925 | /* |
6926 | * A nested guest cannot optimize MMIO vmexits, because we have an | |
6927 | * nGPA here instead of the required GPA. | |
6928 | */ | |
68f89400 | 6929 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
9034e6e8 PB |
6930 | if (!is_guest_mode(vcpu) && |
6931 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { | |
931c33b1 | 6932 | trace_kvm_fast_mmio(gpa); |
60165b0a VK |
6933 | /* |
6934 | * Doing kvm_skip_emulated_instruction() depends on undefined | |
6935 | * behavior: Intel's manual doesn't mandate | |
6936 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG | |
6937 | * occurs and while on real hardware it was observed to be set, | |
6938 | * other hypervisors (namely Hyper-V) don't set it, we end up | |
6939 | * advancing IP with some random value. Disable fast mmio when | |
6940 | * running nested and keep it for real hardware in hope that | |
6941 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. | |
6942 | */ | |
6943 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) | |
6944 | return kvm_skip_emulated_instruction(vcpu); | |
6945 | else | |
90a488b3 SC |
6946 | return emulate_instruction(vcpu, EMULTYPE_SKIP) == |
6947 | EMULATE_DONE; | |
68c3b4d1 | 6948 | } |
68f89400 | 6949 | |
e08d26f0 PB |
6950 | ret = kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
6951 | if (ret >= 0) | |
6952 | return ret; | |
ce88decf XG |
6953 | |
6954 | /* It is the real ept misconfig */ | |
f735d4af | 6955 | WARN_ON(1); |
68f89400 | 6956 | |
851ba692 AK |
6957 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
6958 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; | |
68f89400 MT |
6959 | |
6960 | return 0; | |
6961 | } | |
6962 | ||
851ba692 | 6963 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
f08864b4 | 6964 | { |
d02fcf50 | 6965 | WARN_ON_ONCE(!enable_vnmi); |
47c0152e PB |
6966 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
6967 | CPU_BASED_VIRTUAL_NMI_PENDING); | |
f08864b4 | 6968 | ++vcpu->stat.nmi_window_exits; |
3842d135 | 6969 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
f08864b4 SY |
6970 | |
6971 | return 1; | |
6972 | } | |
6973 | ||
80ced186 | 6974 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
ea953ef0 | 6975 | { |
8b3079a5 AK |
6976 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
6977 | enum emulation_result err = EMULATE_DONE; | |
80ced186 | 6978 | int ret = 1; |
49e9d557 AK |
6979 | u32 cpu_exec_ctrl; |
6980 | bool intr_window_requested; | |
b8405c18 | 6981 | unsigned count = 130; |
49e9d557 AK |
6982 | |
6983 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); | |
6984 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; | |
ea953ef0 | 6985 | |
98eb2f8b | 6986 | while (vmx->emulation_required && count-- != 0) { |
bdea48e3 | 6987 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
49e9d557 AK |
6988 | return handle_interrupt_window(&vmx->vcpu); |
6989 | ||
72875d8a | 6990 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
de87dcdd AK |
6991 | return 1; |
6992 | ||
9b8ae637 | 6993 | err = emulate_instruction(vcpu, 0); |
ea953ef0 | 6994 | |
ac0a48c3 | 6995 | if (err == EMULATE_USER_EXIT) { |
94452b9e | 6996 | ++vcpu->stat.mmio_exits; |
80ced186 MG |
6997 | ret = 0; |
6998 | goto out; | |
6999 | } | |
1d5a4d9b | 7000 | |
299f0328 SC |
7001 | if (err != EMULATE_DONE) |
7002 | goto emulation_error; | |
7003 | ||
7004 | if (vmx->emulation_required && !vmx->rmode.vm86_active && | |
7005 | vcpu->arch.exception.pending) | |
7006 | goto emulation_error; | |
ea953ef0 | 7007 | |
8d76c49e GN |
7008 | if (vcpu->arch.halt_request) { |
7009 | vcpu->arch.halt_request = 0; | |
5cb56059 | 7010 | ret = kvm_vcpu_halt(vcpu); |
8d76c49e GN |
7011 | goto out; |
7012 | } | |
7013 | ||
ea953ef0 | 7014 | if (signal_pending(current)) |
80ced186 | 7015 | goto out; |
ea953ef0 MG |
7016 | if (need_resched()) |
7017 | schedule(); | |
7018 | } | |
7019 | ||
80ced186 MG |
7020 | out: |
7021 | return ret; | |
299f0328 SC |
7022 | |
7023 | emulation_error: | |
7024 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
7025 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
7026 | vcpu->run->internal.ndata = 0; | |
7027 | return 0; | |
ea953ef0 MG |
7028 | } |
7029 | ||
b4a2d31d RK |
7030 | static int __grow_ple_window(int val) |
7031 | { | |
7032 | if (ple_window_grow < 1) | |
7033 | return ple_window; | |
7034 | ||
7035 | val = min(val, ple_window_actual_max); | |
7036 | ||
7037 | if (ple_window_grow < ple_window) | |
7038 | val *= ple_window_grow; | |
7039 | else | |
7040 | val += ple_window_grow; | |
7041 | ||
7042 | return val; | |
7043 | } | |
7044 | ||
7045 | static int __shrink_ple_window(int val, int modifier, int minimum) | |
7046 | { | |
7047 | if (modifier < 1) | |
7048 | return ple_window; | |
7049 | ||
7050 | if (modifier < ple_window) | |
7051 | val /= modifier; | |
7052 | else | |
7053 | val -= modifier; | |
7054 | ||
7055 | return max(val, minimum); | |
7056 | } | |
7057 | ||
7058 | static void grow_ple_window(struct kvm_vcpu *vcpu) | |
7059 | { | |
7060 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7061 | int old = vmx->ple_window; | |
7062 | ||
7063 | vmx->ple_window = __grow_ple_window(old); | |
7064 | ||
7065 | if (vmx->ple_window != old) | |
7066 | vmx->ple_window_dirty = true; | |
7b46268d RK |
7067 | |
7068 | trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
7069 | } |
7070 | ||
7071 | static void shrink_ple_window(struct kvm_vcpu *vcpu) | |
7072 | { | |
7073 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7074 | int old = vmx->ple_window; | |
7075 | ||
7076 | vmx->ple_window = __shrink_ple_window(old, | |
7077 | ple_window_shrink, ple_window); | |
7078 | ||
7079 | if (vmx->ple_window != old) | |
7080 | vmx->ple_window_dirty = true; | |
7b46268d RK |
7081 | |
7082 | trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old); | |
b4a2d31d RK |
7083 | } |
7084 | ||
7085 | /* | |
7086 | * ple_window_actual_max is computed to be one grow_ple_window() below | |
7087 | * ple_window_max. (See __grow_ple_window for the reason.) | |
7088 | * This prevents overflows, because ple_window_max is int. | |
7089 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in | |
7090 | * this process. | |
7091 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. | |
7092 | */ | |
7093 | static void update_ple_window_actual_max(void) | |
7094 | { | |
7095 | ple_window_actual_max = | |
7096 | __shrink_ple_window(max(ple_window_max, ple_window), | |
7097 | ple_window_grow, INT_MIN); | |
7098 | } | |
7099 | ||
bf9f6ac8 FW |
7100 | /* |
7101 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. | |
7102 | */ | |
7103 | static void wakeup_handler(void) | |
7104 | { | |
7105 | struct kvm_vcpu *vcpu; | |
7106 | int cpu = smp_processor_id(); | |
7107 | ||
7108 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
7109 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), | |
7110 | blocked_vcpu_list) { | |
7111 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
7112 | ||
7113 | if (pi_test_on(pi_desc) == 1) | |
7114 | kvm_vcpu_kick(vcpu); | |
7115 | } | |
7116 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); | |
7117 | } | |
7118 | ||
f160c7b7 JS |
7119 | void vmx_enable_tdp(void) |
7120 | { | |
7121 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, | |
7122 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, | |
7123 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, | |
7124 | 0ull, VMX_EPT_EXECUTABLE_MASK, | |
7125 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, | |
d0ec49d4 | 7126 | VMX_EPT_RWX_MASK, 0ull); |
f160c7b7 JS |
7127 | |
7128 | ept_set_mmio_spte_mask(); | |
7129 | kvm_enable_tdp(); | |
7130 | } | |
7131 | ||
f2c7648d TC |
7132 | static __init int hardware_setup(void) |
7133 | { | |
4b0be90f | 7134 | int r = -ENOMEM, i; |
34a1cd60 TC |
7135 | |
7136 | rdmsrl_safe(MSR_EFER, &host_efer); | |
7137 | ||
7138 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) | |
7139 | kvm_define_shared_msr(i, vmx_msr_index[i]); | |
7140 | ||
23611332 RK |
7141 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
7142 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); | |
7143 | if (!vmx_bitmap[i]) | |
7144 | goto out; | |
7145 | } | |
34a1cd60 | 7146 | |
34a1cd60 TC |
7147 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
7148 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); | |
7149 | ||
34a1cd60 | 7150 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
34a1cd60 TC |
7151 | |
7152 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); | |
7153 | ||
34a1cd60 TC |
7154 | if (setup_vmcs_config(&vmcs_config) < 0) { |
7155 | r = -EIO; | |
23611332 | 7156 | goto out; |
baa03522 | 7157 | } |
f2c7648d TC |
7158 | |
7159 | if (boot_cpu_has(X86_FEATURE_NX)) | |
7160 | kvm_enable_efer_bits(EFER_NX); | |
7161 | ||
08d839c4 WL |
7162 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
7163 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) | |
f2c7648d | 7164 | enable_vpid = 0; |
08d839c4 | 7165 | |
f2c7648d TC |
7166 | if (!cpu_has_vmx_shadow_vmcs()) |
7167 | enable_shadow_vmcs = 0; | |
7168 | if (enable_shadow_vmcs) | |
7169 | init_vmcs_shadow_fields(); | |
7170 | ||
7171 | if (!cpu_has_vmx_ept() || | |
42aa53b4 | 7172 | !cpu_has_vmx_ept_4levels() || |
f5f51586 | 7173 | !cpu_has_vmx_ept_mt_wb() || |
8ad8182e | 7174 | !cpu_has_vmx_invept_global()) |
f2c7648d | 7175 | enable_ept = 0; |
f2c7648d | 7176 | |
fce6ac4c | 7177 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
f2c7648d TC |
7178 | enable_ept_ad_bits = 0; |
7179 | ||
8ad8182e | 7180 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
f2c7648d TC |
7181 | enable_unrestricted_guest = 0; |
7182 | ||
ad15a296 | 7183 | if (!cpu_has_vmx_flexpriority()) |
f2c7648d TC |
7184 | flexpriority_enabled = 0; |
7185 | ||
d02fcf50 PB |
7186 | if (!cpu_has_virtual_nmis()) |
7187 | enable_vnmi = 0; | |
7188 | ||
ad15a296 PB |
7189 | /* |
7190 | * set_apic_access_page_addr() is used to reload apic access | |
7191 | * page upon invalidation. No need to do anything if not | |
7192 | * using the APIC_ACCESS_ADDR VMCS field. | |
7193 | */ | |
7194 | if (!flexpriority_enabled) | |
f2c7648d | 7195 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
f2c7648d TC |
7196 | |
7197 | if (!cpu_has_vmx_tpr_shadow()) | |
7198 | kvm_x86_ops->update_cr8_intercept = NULL; | |
7199 | ||
7200 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) | |
7201 | kvm_disable_largepages(); | |
7202 | ||
0f107682 | 7203 | if (!cpu_has_vmx_ple()) { |
f2c7648d | 7204 | ple_gap = 0; |
0f107682 WL |
7205 | ple_window = 0; |
7206 | ple_window_grow = 0; | |
7207 | ple_window_max = 0; | |
7208 | ple_window_shrink = 0; | |
7209 | } | |
f2c7648d | 7210 | |
76dfafd5 | 7211 | if (!cpu_has_vmx_apicv()) { |
f2c7648d | 7212 | enable_apicv = 0; |
76dfafd5 PB |
7213 | kvm_x86_ops->sync_pir_to_irr = NULL; |
7214 | } | |
f2c7648d | 7215 | |
64903d61 HZ |
7216 | if (cpu_has_vmx_tsc_scaling()) { |
7217 | kvm_has_tsc_control = true; | |
7218 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; | |
7219 | kvm_tsc_scaling_ratio_frac_bits = 48; | |
7220 | } | |
7221 | ||
04bb92e4 WL |
7222 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
7223 | ||
f160c7b7 JS |
7224 | if (enable_ept) |
7225 | vmx_enable_tdp(); | |
7226 | else | |
baa03522 TC |
7227 | kvm_disable_tdp(); |
7228 | ||
7229 | update_ple_window_actual_max(); | |
7230 | ||
843e4330 KH |
7231 | /* |
7232 | * Only enable PML when hardware supports PML feature, and both EPT | |
7233 | * and EPT A/D bit features are enabled -- PML depends on them to work. | |
7234 | */ | |
7235 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) | |
7236 | enable_pml = 0; | |
7237 | ||
7238 | if (!enable_pml) { | |
7239 | kvm_x86_ops->slot_enable_log_dirty = NULL; | |
7240 | kvm_x86_ops->slot_disable_log_dirty = NULL; | |
7241 | kvm_x86_ops->flush_log_dirty = NULL; | |
7242 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; | |
7243 | } | |
7244 | ||
64672c95 YJ |
7245 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
7246 | u64 vmx_msr; | |
7247 | ||
7248 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); | |
7249 | cpu_preemption_timer_multi = | |
7250 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; | |
7251 | } else { | |
7252 | kvm_x86_ops->set_hv_timer = NULL; | |
7253 | kvm_x86_ops->cancel_hv_timer = NULL; | |
7254 | } | |
7255 | ||
bf9f6ac8 FW |
7256 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
7257 | ||
c45dcc71 AR |
7258 | kvm_mce_cap_supported |= MCG_LMCE_P; |
7259 | ||
2b8d8b33 SC |
7260 | r = alloc_kvm_area(); |
7261 | if (r) | |
7262 | goto out; | |
7263 | return 0; | |
34a1cd60 | 7264 | |
34a1cd60 | 7265 | out: |
23611332 RK |
7266 | for (i = 0; i < VMX_BITMAP_NR; i++) |
7267 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 | 7268 | |
2b8d8b33 | 7269 | return r; |
f2c7648d TC |
7270 | } |
7271 | ||
7272 | static __exit void hardware_unsetup(void) | |
7273 | { | |
23611332 RK |
7274 | int i; |
7275 | ||
7276 | for (i = 0; i < VMX_BITMAP_NR; i++) | |
7277 | free_page((unsigned long)vmx_bitmap[i]); | |
34a1cd60 | 7278 | |
f2c7648d TC |
7279 | free_kvm_area(); |
7280 | } | |
7281 | ||
4b8d54f9 ZE |
7282 | /* |
7283 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE | |
7284 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. | |
7285 | */ | |
9fb41ba8 | 7286 | static int handle_pause(struct kvm_vcpu *vcpu) |
4b8d54f9 | 7287 | { |
b4a2d31d RK |
7288 | if (ple_gap) |
7289 | grow_ple_window(vcpu); | |
7290 | ||
de63ad4c LM |
7291 | /* |
7292 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" | |
7293 | * VM-execution control is ignored if CPL > 0. OTOH, KVM | |
7294 | * never set PAUSE_EXITING and just set PLE if supported, | |
7295 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. | |
7296 | */ | |
7297 | kvm_vcpu_on_spin(vcpu, true); | |
6affcbed | 7298 | return kvm_skip_emulated_instruction(vcpu); |
4b8d54f9 ZE |
7299 | } |
7300 | ||
87c00572 | 7301 | static int handle_nop(struct kvm_vcpu *vcpu) |
59708670 | 7302 | { |
6affcbed | 7303 | return kvm_skip_emulated_instruction(vcpu); |
59708670 SY |
7304 | } |
7305 | ||
87c00572 GS |
7306 | static int handle_mwait(struct kvm_vcpu *vcpu) |
7307 | { | |
7308 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); | |
7309 | return handle_nop(vcpu); | |
7310 | } | |
7311 | ||
45ec368c JM |
7312 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
7313 | { | |
7314 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7315 | return 1; | |
7316 | } | |
7317 | ||
5f3d45e7 MD |
7318 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
7319 | { | |
7320 | return 1; | |
7321 | } | |
7322 | ||
87c00572 GS |
7323 | static int handle_monitor(struct kvm_vcpu *vcpu) |
7324 | { | |
7325 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); | |
7326 | return handle_nop(vcpu); | |
7327 | } | |
7328 | ||
0658fbaa ACL |
7329 | /* |
7330 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), | |
7331 | * set the success or error code of an emulated VMX instruction, as specified | |
7332 | * by Vol 2B, VMX Instruction Reference, "Conventions". | |
7333 | */ | |
7334 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) | |
7335 | { | |
7336 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) | |
7337 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
7338 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); | |
7339 | } | |
7340 | ||
7341 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) | |
7342 | { | |
7343 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
7344 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | | |
7345 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
7346 | | X86_EFLAGS_CF); | |
7347 | } | |
7348 | ||
145c28dd | 7349 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
0658fbaa ACL |
7350 | u32 vm_instruction_error) |
7351 | { | |
7352 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { | |
7353 | /* | |
7354 | * failValid writes the error number to the current VMCS, which | |
7355 | * can't be done there isn't a current VMCS. | |
7356 | */ | |
7357 | nested_vmx_failInvalid(vcpu); | |
7358 | return; | |
7359 | } | |
7360 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) | |
7361 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | | |
7362 | X86_EFLAGS_SF | X86_EFLAGS_OF)) | |
7363 | | X86_EFLAGS_ZF); | |
7364 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; | |
7365 | /* | |
7366 | * We don't need to force a shadow sync because | |
7367 | * VM_INSTRUCTION_ERROR is not shadowed | |
7368 | */ | |
7369 | } | |
145c28dd | 7370 | |
ff651cb6 WV |
7371 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
7372 | { | |
7373 | /* TODO: not to reset guest simply here. */ | |
7374 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); | |
bbe41b95 | 7375 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
ff651cb6 WV |
7376 | } |
7377 | ||
f4124500 JK |
7378 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
7379 | { | |
7380 | struct vcpu_vmx *vmx = | |
7381 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); | |
7382 | ||
7383 | vmx->nested.preemption_timer_expired = true; | |
7384 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); | |
7385 | kvm_vcpu_kick(&vmx->vcpu); | |
7386 | ||
7387 | return HRTIMER_NORESTART; | |
7388 | } | |
7389 | ||
19677e32 BD |
7390 | /* |
7391 | * Decode the memory-address operand of a vmx instruction, as recorded on an | |
7392 | * exit caused by such an instruction (run by a guest hypervisor). | |
7393 | * On success, returns 0. When the operand is invalid, returns 1 and throws | |
7394 | * #UD or #GP. | |
7395 | */ | |
7396 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, | |
7397 | unsigned long exit_qualification, | |
f9eb4af6 | 7398 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
19677e32 | 7399 | { |
f9eb4af6 EK |
7400 | gva_t off; |
7401 | bool exn; | |
7402 | struct kvm_segment s; | |
7403 | ||
19677e32 BD |
7404 | /* |
7405 | * According to Vol. 3B, "Information for VM Exits Due to Instruction | |
7406 | * Execution", on an exit, vmx_instruction_info holds most of the | |
7407 | * addressing components of the operand. Only the displacement part | |
7408 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). | |
7409 | * For how an actual address is calculated from all these components, | |
7410 | * refer to Vol. 1, "Operand Addressing". | |
7411 | */ | |
7412 | int scaling = vmx_instruction_info & 3; | |
7413 | int addr_size = (vmx_instruction_info >> 7) & 7; | |
7414 | bool is_reg = vmx_instruction_info & (1u << 10); | |
7415 | int seg_reg = (vmx_instruction_info >> 15) & 7; | |
7416 | int index_reg = (vmx_instruction_info >> 18) & 0xf; | |
7417 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); | |
7418 | int base_reg = (vmx_instruction_info >> 23) & 0xf; | |
7419 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); | |
7420 | ||
7421 | if (is_reg) { | |
7422 | kvm_queue_exception(vcpu, UD_VECTOR); | |
7423 | return 1; | |
7424 | } | |
7425 | ||
7426 | /* Addr = segment_base + offset */ | |
7427 | /* offset = base + [index * scale] + displacement */ | |
f9eb4af6 | 7428 | off = exit_qualification; /* holds the displacement */ |
4f7d4d74 SC |
7429 | if (addr_size == 1) |
7430 | off = (gva_t)sign_extend64(off, 31); | |
7431 | else if (addr_size == 0) | |
7432 | off = (gva_t)sign_extend64(off, 15); | |
19677e32 | 7433 | if (base_is_valid) |
f9eb4af6 | 7434 | off += kvm_register_read(vcpu, base_reg); |
19677e32 | 7435 | if (index_is_valid) |
f9eb4af6 EK |
7436 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
7437 | vmx_get_segment(vcpu, &s, seg_reg); | |
19677e32 | 7438 | |
fedc84f4 SC |
7439 | /* |
7440 | * The effective address, i.e. @off, of a memory operand is truncated | |
7441 | * based on the address size of the instruction. Note that this is | |
7442 | * the *effective address*, i.e. the address prior to accounting for | |
7443 | * the segment's base. | |
7444 | */ | |
19677e32 | 7445 | if (addr_size == 1) /* 32 bit */ |
fedc84f4 SC |
7446 | off &= 0xffffffff; |
7447 | else if (addr_size == 0) /* 16 bit */ | |
7448 | off &= 0xffff; | |
19677e32 | 7449 | |
f9eb4af6 EK |
7450 | /* Checks for #GP/#SS exceptions. */ |
7451 | exn = false; | |
ff30ef40 | 7452 | if (is_long_mode(vcpu)) { |
fedc84f4 SC |
7453 | /* |
7454 | * The virtual/linear address is never truncated in 64-bit | |
7455 | * mode, e.g. a 32-bit address size can yield a 64-bit virtual | |
7456 | * address when using FS/GS with a non-zero base. | |
7457 | */ | |
7458 | *ret = s.base + off; | |
7459 | ||
ff30ef40 QC |
7460 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
7461 | * non-canonical form. This is the only check on the memory | |
7462 | * destination for long mode! | |
7463 | */ | |
fd8cb433 | 7464 | exn = is_noncanonical_address(*ret, vcpu); |
ff30ef40 | 7465 | } else if (is_protmode(vcpu)) { |
fedc84f4 SC |
7466 | /* |
7467 | * When not in long mode, the virtual/linear address is | |
7468 | * unconditionally truncated to 32 bits regardless of the | |
7469 | * address size. | |
7470 | */ | |
7471 | *ret = (s.base + off) & 0xffffffff; | |
7472 | ||
f9eb4af6 EK |
7473 | /* Protected mode: apply checks for segment validity in the |
7474 | * following order: | |
7475 | * - segment type check (#GP(0) may be thrown) | |
7476 | * - usability check (#GP(0)/#SS(0)) | |
7477 | * - limit check (#GP(0)/#SS(0)) | |
7478 | */ | |
7479 | if (wr) | |
7480 | /* #GP(0) if the destination operand is located in a | |
7481 | * read-only data segment or any code segment. | |
7482 | */ | |
7483 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); | |
7484 | else | |
7485 | /* #GP(0) if the source operand is located in an | |
7486 | * execute-only code segment | |
7487 | */ | |
7488 | exn = ((s.type & 0xa) == 8); | |
ff30ef40 QC |
7489 | if (exn) { |
7490 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
7491 | return 1; | |
7492 | } | |
f9eb4af6 EK |
7493 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
7494 | */ | |
7495 | exn = (s.unusable != 0); | |
05787d1f SC |
7496 | |
7497 | /* | |
7498 | * Protected mode: #GP(0)/#SS(0) if the memory operand is | |
7499 | * outside the segment limit. All CPUs that support VMX ignore | |
7500 | * limit checks for flat segments, i.e. segments with base==0, | |
7501 | * limit==0xffffffff and of type expand-up data or code. | |
f9eb4af6 | 7502 | */ |
05787d1f SC |
7503 | if (!(s.base == 0 && s.limit == 0xffffffff && |
7504 | ((s.type & 8) || !(s.type & 4)))) | |
7505 | exn = exn || (off + sizeof(u64) > s.limit); | |
f9eb4af6 EK |
7506 | } |
7507 | if (exn) { | |
7508 | kvm_queue_exception_e(vcpu, | |
7509 | seg_reg == VCPU_SREG_SS ? | |
7510 | SS_VECTOR : GP_VECTOR, | |
7511 | 0); | |
7512 | return 1; | |
7513 | } | |
7514 | ||
19677e32 BD |
7515 | return 0; |
7516 | } | |
7517 | ||
cbf71279 | 7518 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer) |
3573e22c BD |
7519 | { |
7520 | gva_t gva; | |
3573e22c | 7521 | struct x86_exception e; |
3573e22c BD |
7522 | |
7523 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 7524 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
3573e22c BD |
7525 | return 1; |
7526 | ||
40d2dba3 | 7527 | if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) { |
3573e22c BD |
7528 | kvm_inject_page_fault(vcpu, &e); |
7529 | return 1; | |
7530 | } | |
7531 | ||
3573e22c BD |
7532 | return 0; |
7533 | } | |
7534 | ||
e29acc55 JM |
7535 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
7536 | { | |
7537 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7538 | struct vmcs *shadow_vmcs; | |
b6d7026d | 7539 | int r; |
e29acc55 | 7540 | |
b6d7026d PB |
7541 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
7542 | if (r < 0) | |
8819227c | 7543 | goto out_vmcs02; |
8819227c | 7544 | |
e29acc55 JM |
7545 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
7546 | if (!vmx->nested.cached_vmcs12) | |
7547 | goto out_cached_vmcs12; | |
7548 | ||
7549 | if (enable_shadow_vmcs) { | |
7550 | shadow_vmcs = alloc_vmcs(); | |
7551 | if (!shadow_vmcs) | |
7552 | goto out_shadow_vmcs; | |
7553 | /* mark vmcs as shadow */ | |
7554 | shadow_vmcs->revision_id |= (1u << 31); | |
7555 | /* init shadow vmcs */ | |
7556 | vmcs_clear(shadow_vmcs); | |
7557 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; | |
7558 | } | |
7559 | ||
e29acc55 JM |
7560 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
7561 | HRTIMER_MODE_REL_PINNED); | |
7562 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; | |
7563 | ||
23f2bb27 RK |
7564 | vmx->nested.vpid02 = allocate_vpid(); |
7565 | ||
e29acc55 JM |
7566 | vmx->nested.vmxon = true; |
7567 | return 0; | |
7568 | ||
7569 | out_shadow_vmcs: | |
7570 | kfree(vmx->nested.cached_vmcs12); | |
7571 | ||
7572 | out_cached_vmcs12: | |
8819227c JM |
7573 | free_loaded_vmcs(&vmx->nested.vmcs02); |
7574 | ||
7575 | out_vmcs02: | |
e29acc55 JM |
7576 | return -ENOMEM; |
7577 | } | |
7578 | ||
ec378aee NHE |
7579 | /* |
7580 | * Emulate the VMXON instruction. | |
7581 | * Currently, we just remember that VMX is active, and do not save or even | |
7582 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we | |
7583 | * do not currently need to store anything in that guest-allocated memory | |
7584 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their | |
7585 | * argument is different from the VMXON pointer (which the spec says they do). | |
7586 | */ | |
7587 | static int handle_vmon(struct kvm_vcpu *vcpu) | |
7588 | { | |
e29acc55 | 7589 | int ret; |
cbf71279 RK |
7590 | gpa_t vmptr; |
7591 | struct page *page; | |
ec378aee | 7592 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
b3897a49 NHE |
7593 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
7594 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
ec378aee | 7595 | |
70f3aac9 JM |
7596 | /* |
7597 | * The Intel VMX Instruction Reference lists a bunch of bits that are | |
7598 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to | |
7599 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). | |
7600 | * Otherwise, we should fail with #UD. But most faulting conditions | |
7601 | * have already been checked by hardware, prior to the VM-exit for | |
7602 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has | |
7603 | * that bit set to 1 in non-root mode. | |
ec378aee | 7604 | */ |
70f3aac9 | 7605 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
ec378aee NHE |
7606 | kvm_queue_exception(vcpu, UD_VECTOR); |
7607 | return 1; | |
7608 | } | |
7609 | ||
a325262f FW |
7610 | /* CPL=0 must be checked manually. */ |
7611 | if (vmx_get_cpl(vcpu)) { | |
bc9a5a47 | 7612 | kvm_inject_gp(vcpu, 0); |
a325262f FW |
7613 | return 1; |
7614 | } | |
7615 | ||
145c28dd AG |
7616 | if (vmx->nested.vmxon) { |
7617 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); | |
6affcbed | 7618 | return kvm_skip_emulated_instruction(vcpu); |
145c28dd | 7619 | } |
b3897a49 | 7620 | |
3b84080b | 7621 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
b3897a49 NHE |
7622 | != VMXON_NEEDED_FEATURES) { |
7623 | kvm_inject_gp(vcpu, 0); | |
7624 | return 1; | |
7625 | } | |
7626 | ||
cbf71279 | 7627 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
21e7fbe7 | 7628 | return 1; |
cbf71279 RK |
7629 | |
7630 | /* | |
7631 | * SDM 3: 24.11.5 | |
7632 | * The first 4 bytes of VMXON region contain the supported | |
7633 | * VMCS revision identifier | |
7634 | * | |
7635 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; | |
7636 | * which replaces physical address width with 32 | |
7637 | */ | |
7638 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { | |
7639 | nested_vmx_failInvalid(vcpu); | |
7640 | return kvm_skip_emulated_instruction(vcpu); | |
7641 | } | |
7642 | ||
5e2f30b7 DH |
7643 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
7644 | if (is_error_page(page)) { | |
cbf71279 RK |
7645 | nested_vmx_failInvalid(vcpu); |
7646 | return kvm_skip_emulated_instruction(vcpu); | |
7647 | } | |
7648 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { | |
7649 | kunmap(page); | |
53a70daf | 7650 | kvm_release_page_clean(page); |
cbf71279 RK |
7651 | nested_vmx_failInvalid(vcpu); |
7652 | return kvm_skip_emulated_instruction(vcpu); | |
7653 | } | |
7654 | kunmap(page); | |
53a70daf | 7655 | kvm_release_page_clean(page); |
cbf71279 RK |
7656 | |
7657 | vmx->nested.vmxon_ptr = vmptr; | |
e29acc55 JM |
7658 | ret = enter_vmx_operation(vcpu); |
7659 | if (ret) | |
7660 | return ret; | |
ec378aee | 7661 | |
a25eb114 | 7662 | nested_vmx_succeed(vcpu); |
6affcbed | 7663 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7664 | } |
7665 | ||
7666 | /* | |
7667 | * Intel's VMX Instruction Reference specifies a common set of prerequisites | |
7668 | * for running VMX instructions (except VMXON, whose prerequisites are | |
7669 | * slightly different). It also specifies what exception to inject otherwise. | |
70f3aac9 JM |
7670 | * Note that many of these exceptions have priority over VM exits, so they |
7671 | * don't have to be checked again here. | |
ec378aee NHE |
7672 | */ |
7673 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) | |
7674 | { | |
a325262f | 7675 | if (vmx_get_cpl(vcpu)) { |
bc9a5a47 | 7676 | kvm_inject_gp(vcpu, 0); |
a325262f FW |
7677 | return 0; |
7678 | } | |
7679 | ||
70f3aac9 | 7680 | if (!to_vmx(vcpu)->nested.vmxon) { |
ec378aee NHE |
7681 | kvm_queue_exception(vcpu, UD_VECTOR); |
7682 | return 0; | |
7683 | } | |
ec378aee NHE |
7684 | return 1; |
7685 | } | |
7686 | ||
8ca44e88 DM |
7687 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
7688 | { | |
7689 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); | |
7690 | vmcs_write64(VMCS_LINK_POINTER, -1ull); | |
b2459372 | 7691 | vmx->nested.sync_shadow_vmcs = false; |
8ca44e88 DM |
7692 | } |
7693 | ||
e7953d7f AG |
7694 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
7695 | { | |
9a2a05b9 PB |
7696 | if (vmx->nested.current_vmptr == -1ull) |
7697 | return; | |
7698 | ||
012f83cb | 7699 | if (enable_shadow_vmcs) { |
9a2a05b9 PB |
7700 | /* copy to memory all shadowed fields in case |
7701 | they were modified */ | |
7702 | copy_shadow_to_vmcs12(vmx); | |
8ca44e88 | 7703 | vmx_disable_shadow_vmcs(vmx); |
012f83cb | 7704 | } |
705699a1 | 7705 | vmx->nested.posted_intr_nv = -1; |
4f2777bc DM |
7706 | |
7707 | /* Flush VMCS12 to guest memory */ | |
9f744c59 PB |
7708 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
7709 | vmx->nested.current_vmptr >> PAGE_SHIFT, | |
7710 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); | |
4f2777bc | 7711 | |
9a2a05b9 | 7712 | vmx->nested.current_vmptr = -1ull; |
e7953d7f AG |
7713 | } |
7714 | ||
ec378aee NHE |
7715 | /* |
7716 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or | |
7717 | * just stops using VMX. | |
7718 | */ | |
7719 | static void free_nested(struct vcpu_vmx *vmx) | |
7720 | { | |
b7455825 | 7721 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
ec378aee | 7722 | return; |
9a2a05b9 | 7723 | |
fe192085 | 7724 | hrtimer_cancel(&vmx->nested.preemption_timer); |
ec378aee | 7725 | vmx->nested.vmxon = false; |
b7455825 | 7726 | vmx->nested.smm.vmxon = false; |
5c614b35 | 7727 | free_vpid(vmx->nested.vpid02); |
8ca44e88 DM |
7728 | vmx->nested.posted_intr_nv = -1; |
7729 | vmx->nested.current_vmptr = -1ull; | |
355f4fb1 | 7730 | if (enable_shadow_vmcs) { |
8ca44e88 | 7731 | vmx_disable_shadow_vmcs(vmx); |
355f4fb1 JM |
7732 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
7733 | free_vmcs(vmx->vmcs01.shadow_vmcs); | |
7734 | vmx->vmcs01.shadow_vmcs = NULL; | |
7735 | } | |
4f2777bc | 7736 | kfree(vmx->nested.cached_vmcs12); |
8819227c | 7737 | /* Unpin physical memory we referred to in the vmcs02 */ |
fe3ef05c | 7738 | if (vmx->nested.apic_access_page) { |
53a70daf | 7739 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
48d89b92 | 7740 | vmx->nested.apic_access_page = NULL; |
fe3ef05c | 7741 | } |
a7c0b07d | 7742 | if (vmx->nested.virtual_apic_page) { |
53a70daf | 7743 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
48d89b92 | 7744 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 7745 | } |
705699a1 WV |
7746 | if (vmx->nested.pi_desc_page) { |
7747 | kunmap(vmx->nested.pi_desc_page); | |
53a70daf | 7748 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
705699a1 WV |
7749 | vmx->nested.pi_desc_page = NULL; |
7750 | vmx->nested.pi_desc = NULL; | |
7751 | } | |
ff2f6fe9 | 7752 | |
8819227c | 7753 | free_loaded_vmcs(&vmx->nested.vmcs02); |
ec378aee NHE |
7754 | } |
7755 | ||
7756 | /* Emulate the VMXOFF instruction */ | |
7757 | static int handle_vmoff(struct kvm_vcpu *vcpu) | |
7758 | { | |
7759 | if (!nested_vmx_check_permission(vcpu)) | |
7760 | return 1; | |
7761 | free_nested(to_vmx(vcpu)); | |
a25eb114 | 7762 | nested_vmx_succeed(vcpu); |
6affcbed | 7763 | return kvm_skip_emulated_instruction(vcpu); |
ec378aee NHE |
7764 | } |
7765 | ||
27d6c865 NHE |
7766 | /* Emulate the VMCLEAR instruction */ |
7767 | static int handle_vmclear(struct kvm_vcpu *vcpu) | |
7768 | { | |
7769 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
587d7e72 | 7770 | u32 zero = 0; |
27d6c865 | 7771 | gpa_t vmptr; |
27d6c865 NHE |
7772 | |
7773 | if (!nested_vmx_check_permission(vcpu)) | |
7774 | return 1; | |
7775 | ||
cbf71279 | 7776 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
27d6c865 | 7777 | return 1; |
27d6c865 | 7778 | |
cbf71279 RK |
7779 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
7780 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); | |
7781 | return kvm_skip_emulated_instruction(vcpu); | |
7782 | } | |
7783 | ||
7784 | if (vmptr == vmx->nested.vmxon_ptr) { | |
7785 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); | |
7786 | return kvm_skip_emulated_instruction(vcpu); | |
7787 | } | |
7788 | ||
9a2a05b9 | 7789 | if (vmptr == vmx->nested.current_vmptr) |
e7953d7f | 7790 | nested_release_vmcs12(vmx); |
27d6c865 | 7791 | |
587d7e72 JM |
7792 | kvm_vcpu_write_guest(vcpu, |
7793 | vmptr + offsetof(struct vmcs12, launch_state), | |
7794 | &zero, sizeof(zero)); | |
27d6c865 | 7795 | |
27d6c865 | 7796 | nested_vmx_succeed(vcpu); |
6affcbed | 7797 | return kvm_skip_emulated_instruction(vcpu); |
27d6c865 NHE |
7798 | } |
7799 | ||
cd232ad0 NHE |
7800 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
7801 | ||
7802 | /* Emulate the VMLAUNCH instruction */ | |
7803 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) | |
7804 | { | |
7805 | return nested_vmx_run(vcpu, true); | |
7806 | } | |
7807 | ||
7808 | /* Emulate the VMRESUME instruction */ | |
7809 | static int handle_vmresume(struct kvm_vcpu *vcpu) | |
7810 | { | |
7811 | ||
7812 | return nested_vmx_run(vcpu, false); | |
7813 | } | |
7814 | ||
49f705c5 NHE |
7815 | /* |
7816 | * Read a vmcs12 field. Since these can have varying lengths and we return | |
7817 | * one type, we chose the biggest type (u64) and zero-extend the return value | |
7818 | * to that size. Note that the caller, handle_vmread, might need to use only | |
7819 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of | |
7820 | * 64-bit fields are to be returned). | |
7821 | */ | |
a2ae9df7 PB |
7822 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
7823 | unsigned long field, u64 *ret) | |
49f705c5 NHE |
7824 | { |
7825 | short offset = vmcs_field_to_offset(field); | |
7826 | char *p; | |
7827 | ||
7828 | if (offset < 0) | |
a2ae9df7 | 7829 | return offset; |
49f705c5 NHE |
7830 | |
7831 | p = ((char *)(get_vmcs12(vcpu))) + offset; | |
7832 | ||
7833 | switch (vmcs_field_type(field)) { | |
7834 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7835 | *ret = *((natural_width *)p); | |
a2ae9df7 | 7836 | return 0; |
49f705c5 NHE |
7837 | case VMCS_FIELD_TYPE_U16: |
7838 | *ret = *((u16 *)p); | |
a2ae9df7 | 7839 | return 0; |
49f705c5 NHE |
7840 | case VMCS_FIELD_TYPE_U32: |
7841 | *ret = *((u32 *)p); | |
a2ae9df7 | 7842 | return 0; |
49f705c5 NHE |
7843 | case VMCS_FIELD_TYPE_U64: |
7844 | *ret = *((u64 *)p); | |
a2ae9df7 | 7845 | return 0; |
49f705c5 | 7846 | default: |
a2ae9df7 PB |
7847 | WARN_ON(1); |
7848 | return -ENOENT; | |
49f705c5 NHE |
7849 | } |
7850 | } | |
7851 | ||
20b97fea | 7852 | |
a2ae9df7 PB |
7853 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
7854 | unsigned long field, u64 field_value){ | |
20b97fea AG |
7855 | short offset = vmcs_field_to_offset(field); |
7856 | char *p = ((char *) get_vmcs12(vcpu)) + offset; | |
7857 | if (offset < 0) | |
a2ae9df7 | 7858 | return offset; |
20b97fea AG |
7859 | |
7860 | switch (vmcs_field_type(field)) { | |
7861 | case VMCS_FIELD_TYPE_U16: | |
7862 | *(u16 *)p = field_value; | |
a2ae9df7 | 7863 | return 0; |
20b97fea AG |
7864 | case VMCS_FIELD_TYPE_U32: |
7865 | *(u32 *)p = field_value; | |
a2ae9df7 | 7866 | return 0; |
20b97fea AG |
7867 | case VMCS_FIELD_TYPE_U64: |
7868 | *(u64 *)p = field_value; | |
a2ae9df7 | 7869 | return 0; |
20b97fea AG |
7870 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
7871 | *(natural_width *)p = field_value; | |
a2ae9df7 | 7872 | return 0; |
20b97fea | 7873 | default: |
a2ae9df7 PB |
7874 | WARN_ON(1); |
7875 | return -ENOENT; | |
20b97fea AG |
7876 | } |
7877 | ||
7878 | } | |
7879 | ||
16f5b903 AG |
7880 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
7881 | { | |
7882 | int i; | |
7883 | unsigned long field; | |
7884 | u64 field_value; | |
355f4fb1 | 7885 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
c2bae893 MK |
7886 | const unsigned long *fields = shadow_read_write_fields; |
7887 | const int num_fields = max_shadow_read_write_fields; | |
16f5b903 | 7888 | |
b2459372 PB |
7889 | if (WARN_ON(!shadow_vmcs)) |
7890 | return; | |
7891 | ||
282da870 JK |
7892 | preempt_disable(); |
7893 | ||
16f5b903 AG |
7894 | vmcs_load(shadow_vmcs); |
7895 | ||
7896 | for (i = 0; i < num_fields; i++) { | |
7897 | field = fields[i]; | |
7898 | switch (vmcs_field_type(field)) { | |
7899 | case VMCS_FIELD_TYPE_U16: | |
7900 | field_value = vmcs_read16(field); | |
7901 | break; | |
7902 | case VMCS_FIELD_TYPE_U32: | |
7903 | field_value = vmcs_read32(field); | |
7904 | break; | |
7905 | case VMCS_FIELD_TYPE_U64: | |
7906 | field_value = vmcs_read64(field); | |
7907 | break; | |
7908 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7909 | field_value = vmcs_readl(field); | |
7910 | break; | |
a2ae9df7 PB |
7911 | default: |
7912 | WARN_ON(1); | |
7913 | continue; | |
16f5b903 AG |
7914 | } |
7915 | vmcs12_write_any(&vmx->vcpu, field, field_value); | |
7916 | } | |
7917 | ||
7918 | vmcs_clear(shadow_vmcs); | |
7919 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
282da870 JK |
7920 | |
7921 | preempt_enable(); | |
16f5b903 AG |
7922 | } |
7923 | ||
c3114420 AG |
7924 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
7925 | { | |
c2bae893 MK |
7926 | const unsigned long *fields[] = { |
7927 | shadow_read_write_fields, | |
7928 | shadow_read_only_fields | |
c3114420 | 7929 | }; |
c2bae893 | 7930 | const int max_fields[] = { |
c3114420 AG |
7931 | max_shadow_read_write_fields, |
7932 | max_shadow_read_only_fields | |
7933 | }; | |
7934 | int i, q; | |
7935 | unsigned long field; | |
7936 | u64 field_value = 0; | |
355f4fb1 | 7937 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
c3114420 | 7938 | |
b2459372 PB |
7939 | if (WARN_ON(!shadow_vmcs)) |
7940 | return; | |
7941 | ||
c3114420 AG |
7942 | vmcs_load(shadow_vmcs); |
7943 | ||
c2bae893 | 7944 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
c3114420 AG |
7945 | for (i = 0; i < max_fields[q]; i++) { |
7946 | field = fields[q][i]; | |
7947 | vmcs12_read_any(&vmx->vcpu, field, &field_value); | |
7948 | ||
7949 | switch (vmcs_field_type(field)) { | |
7950 | case VMCS_FIELD_TYPE_U16: | |
7951 | vmcs_write16(field, (u16)field_value); | |
7952 | break; | |
7953 | case VMCS_FIELD_TYPE_U32: | |
7954 | vmcs_write32(field, (u32)field_value); | |
7955 | break; | |
7956 | case VMCS_FIELD_TYPE_U64: | |
7957 | vmcs_write64(field, (u64)field_value); | |
7958 | break; | |
7959 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: | |
7960 | vmcs_writel(field, (long)field_value); | |
7961 | break; | |
a2ae9df7 PB |
7962 | default: |
7963 | WARN_ON(1); | |
7964 | break; | |
c3114420 AG |
7965 | } |
7966 | } | |
7967 | } | |
7968 | ||
7969 | vmcs_clear(shadow_vmcs); | |
7970 | vmcs_load(vmx->loaded_vmcs->vmcs); | |
7971 | } | |
7972 | ||
49f705c5 NHE |
7973 | /* |
7974 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was | |
7975 | * used before) all generate the same failure when it is missing. | |
7976 | */ | |
7977 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) | |
7978 | { | |
7979 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
7980 | if (vmx->nested.current_vmptr == -1ull) { | |
7981 | nested_vmx_failInvalid(vcpu); | |
49f705c5 NHE |
7982 | return 0; |
7983 | } | |
7984 | return 1; | |
7985 | } | |
7986 | ||
7987 | static int handle_vmread(struct kvm_vcpu *vcpu) | |
7988 | { | |
7989 | unsigned long field; | |
7990 | u64 field_value; | |
7991 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
7992 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
7993 | gva_t gva = 0; | |
ff2726ac | 7994 | struct x86_exception e; |
49f705c5 | 7995 | |
eb277562 | 7996 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
7997 | return 1; |
7998 | ||
6affcbed KH |
7999 | if (!nested_vmx_check_vmcs12(vcpu)) |
8000 | return kvm_skip_emulated_instruction(vcpu); | |
49f705c5 NHE |
8001 | |
8002 | /* Decode instruction info and find the field to read */ | |
27e6fb5d | 8003 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 | 8004 | /* Read the field, zero-extended to a u64 field_value */ |
a2ae9df7 | 8005 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
49f705c5 | 8006 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 8007 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8008 | } |
8009 | /* | |
8010 | * Now copy part of this value to register or memory, as requested. | |
8011 | * Note that the number of bits actually copied is 32 or 64 depending | |
8012 | * on the guest's mode (32 or 64 bit), not on the given field's length. | |
8013 | */ | |
8014 | if (vmx_instruction_info & (1u << 10)) { | |
27e6fb5d | 8015 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
49f705c5 NHE |
8016 | field_value); |
8017 | } else { | |
8018 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 8019 | vmx_instruction_info, true, &gva)) |
49f705c5 | 8020 | return 1; |
a325262f | 8021 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
ff2726ac PB |
8022 | if (kvm_write_guest_virt_system(vcpu, gva, &field_value, |
8023 | (is_long_mode(vcpu) ? 8 : 4), | |
aed7b463 | 8024 | &e)) |
ff2726ac | 8025 | kvm_inject_page_fault(vcpu, &e); |
49f705c5 NHE |
8026 | } |
8027 | ||
8028 | nested_vmx_succeed(vcpu); | |
6affcbed | 8029 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8030 | } |
8031 | ||
8032 | ||
8033 | static int handle_vmwrite(struct kvm_vcpu *vcpu) | |
8034 | { | |
8035 | unsigned long field; | |
8036 | gva_t gva; | |
8037 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8038 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
49f705c5 NHE |
8039 | /* The value to write might be 32 or 64 bits, depending on L1's long |
8040 | * mode, and eventually we need to write that into a field of several | |
8041 | * possible lengths. The code below first zero-extends the value to 64 | |
6a6256f9 | 8042 | * bit (field_value), and then copies only the appropriate number of |
49f705c5 NHE |
8043 | * bits into the vmcs12 field. |
8044 | */ | |
8045 | u64 field_value = 0; | |
8046 | struct x86_exception e; | |
8047 | ||
eb277562 | 8048 | if (!nested_vmx_check_permission(vcpu)) |
49f705c5 NHE |
8049 | return 1; |
8050 | ||
6affcbed KH |
8051 | if (!nested_vmx_check_vmcs12(vcpu)) |
8052 | return kvm_skip_emulated_instruction(vcpu); | |
eb277562 | 8053 | |
49f705c5 | 8054 | if (vmx_instruction_info & (1u << 10)) |
27e6fb5d | 8055 | field_value = kvm_register_readl(vcpu, |
49f705c5 NHE |
8056 | (((vmx_instruction_info) >> 3) & 0xf)); |
8057 | else { | |
8058 | if (get_vmx_mem_address(vcpu, exit_qualification, | |
f9eb4af6 | 8059 | vmx_instruction_info, false, &gva)) |
49f705c5 | 8060 | return 1; |
40d2dba3 PB |
8061 | if (kvm_read_guest_virt(vcpu, gva, &field_value, |
8062 | (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { | |
49f705c5 NHE |
8063 | kvm_inject_page_fault(vcpu, &e); |
8064 | return 1; | |
8065 | } | |
8066 | } | |
8067 | ||
8068 | ||
27e6fb5d | 8069 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
49f705c5 NHE |
8070 | if (vmcs_field_readonly(field)) { |
8071 | nested_vmx_failValid(vcpu, | |
8072 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); | |
6affcbed | 8073 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8074 | } |
8075 | ||
a2ae9df7 | 8076 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
49f705c5 | 8077 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
6affcbed | 8078 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8079 | } |
8080 | ||
8081 | nested_vmx_succeed(vcpu); | |
6affcbed | 8082 | return kvm_skip_emulated_instruction(vcpu); |
49f705c5 NHE |
8083 | } |
8084 | ||
a8bc284e JM |
8085 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
8086 | { | |
8087 | vmx->nested.current_vmptr = vmptr; | |
8088 | if (enable_shadow_vmcs) { | |
8089 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
8090 | SECONDARY_EXEC_SHADOW_VMCS); | |
8091 | vmcs_write64(VMCS_LINK_POINTER, | |
8092 | __pa(vmx->vmcs01.shadow_vmcs)); | |
8093 | vmx->nested.sync_shadow_vmcs = true; | |
8094 | } | |
8095 | } | |
8096 | ||
63846663 NHE |
8097 | /* Emulate the VMPTRLD instruction */ |
8098 | static int handle_vmptrld(struct kvm_vcpu *vcpu) | |
8099 | { | |
8100 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
63846663 | 8101 | gpa_t vmptr; |
63846663 NHE |
8102 | |
8103 | if (!nested_vmx_check_permission(vcpu)) | |
8104 | return 1; | |
8105 | ||
cbf71279 | 8106 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
63846663 | 8107 | return 1; |
63846663 | 8108 | |
cbf71279 RK |
8109 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
8110 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); | |
8111 | return kvm_skip_emulated_instruction(vcpu); | |
8112 | } | |
8113 | ||
8114 | if (vmptr == vmx->nested.vmxon_ptr) { | |
8115 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); | |
8116 | return kvm_skip_emulated_instruction(vcpu); | |
8117 | } | |
8118 | ||
63846663 NHE |
8119 | if (vmx->nested.current_vmptr != vmptr) { |
8120 | struct vmcs12 *new_vmcs12; | |
8121 | struct page *page; | |
5e2f30b7 DH |
8122 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
8123 | if (is_error_page(page)) { | |
63846663 | 8124 | nested_vmx_failInvalid(vcpu); |
6affcbed | 8125 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
8126 | } |
8127 | new_vmcs12 = kmap(page); | |
8128 | if (new_vmcs12->revision_id != VMCS12_REVISION) { | |
8129 | kunmap(page); | |
53a70daf | 8130 | kvm_release_page_clean(page); |
63846663 NHE |
8131 | nested_vmx_failValid(vcpu, |
8132 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); | |
6affcbed | 8133 | return kvm_skip_emulated_instruction(vcpu); |
63846663 | 8134 | } |
63846663 | 8135 | |
9a2a05b9 | 8136 | nested_release_vmcs12(vmx); |
4f2777bc DM |
8137 | /* |
8138 | * Load VMCS12 from guest memory since it is not already | |
8139 | * cached. | |
8140 | */ | |
9f744c59 PB |
8141 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
8142 | kunmap(page); | |
53a70daf | 8143 | kvm_release_page_clean(page); |
9f744c59 | 8144 | |
a8bc284e | 8145 | set_current_vmptr(vmx, vmptr); |
63846663 NHE |
8146 | } |
8147 | ||
8148 | nested_vmx_succeed(vcpu); | |
6affcbed | 8149 | return kvm_skip_emulated_instruction(vcpu); |
63846663 NHE |
8150 | } |
8151 | ||
6a4d7550 NHE |
8152 | /* Emulate the VMPTRST instruction */ |
8153 | static int handle_vmptrst(struct kvm_vcpu *vcpu) | |
8154 | { | |
30c549fb SC |
8155 | unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION); |
8156 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
8157 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; | |
6a4d7550 | 8158 | struct x86_exception e; |
30c549fb | 8159 | gva_t gva; |
6a4d7550 NHE |
8160 | |
8161 | if (!nested_vmx_check_permission(vcpu)) | |
8162 | return 1; | |
8163 | ||
30c549fb | 8164 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva)) |
6a4d7550 | 8165 | return 1; |
a325262f | 8166 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
30c549fb SC |
8167 | if (kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
8168 | sizeof(gpa_t), &e)) { | |
6a4d7550 NHE |
8169 | kvm_inject_page_fault(vcpu, &e); |
8170 | return 1; | |
8171 | } | |
8172 | nested_vmx_succeed(vcpu); | |
6affcbed | 8173 | return kvm_skip_emulated_instruction(vcpu); |
6a4d7550 NHE |
8174 | } |
8175 | ||
bfd0a56b NHE |
8176 | /* Emulate the INVEPT instruction */ |
8177 | static int handle_invept(struct kvm_vcpu *vcpu) | |
8178 | { | |
b9c237bb | 8179 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
bfd0a56b NHE |
8180 | u32 vmx_instruction_info, types; |
8181 | unsigned long type; | |
8182 | gva_t gva; | |
8183 | struct x86_exception e; | |
8184 | struct { | |
8185 | u64 eptp, gpa; | |
8186 | } operand; | |
bfd0a56b | 8187 | |
b9c237bb WV |
8188 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
8189 | SECONDARY_EXEC_ENABLE_EPT) || | |
8190 | !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { | |
bfd0a56b NHE |
8191 | kvm_queue_exception(vcpu, UD_VECTOR); |
8192 | return 1; | |
8193 | } | |
8194 | ||
8195 | if (!nested_vmx_check_permission(vcpu)) | |
8196 | return 1; | |
8197 | ||
bfd0a56b | 8198 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
27e6fb5d | 8199 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
bfd0a56b | 8200 | |
b9c237bb | 8201 | types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
bfd0a56b | 8202 | |
85c856b3 | 8203 | if (type >= 32 || !(types & (1 << type))) { |
bfd0a56b NHE |
8204 | nested_vmx_failValid(vcpu, |
8205 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 8206 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
8207 | } |
8208 | ||
8209 | /* According to the Intel VMX instruction reference, the memory | |
8210 | * operand is read even if it isn't needed (e.g., for type==global) | |
8211 | */ | |
8212 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
f9eb4af6 | 8213 | vmx_instruction_info, false, &gva)) |
bfd0a56b | 8214 | return 1; |
40d2dba3 | 8215 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
bfd0a56b NHE |
8216 | kvm_inject_page_fault(vcpu, &e); |
8217 | return 1; | |
8218 | } | |
8219 | ||
8220 | switch (type) { | |
bfd0a56b | 8221 | case VMX_EPT_EXTENT_GLOBAL: |
45e11817 BD |
8222 | /* |
8223 | * TODO: track mappings and invalidate | |
8224 | * single context requests appropriately | |
8225 | */ | |
8226 | case VMX_EPT_EXTENT_CONTEXT: | |
bfd0a56b | 8227 | kvm_mmu_sync_roots(vcpu); |
77c3913b | 8228 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
bfd0a56b NHE |
8229 | nested_vmx_succeed(vcpu); |
8230 | break; | |
8231 | default: | |
8232 | BUG_ON(1); | |
8233 | break; | |
8234 | } | |
8235 | ||
6affcbed | 8236 | return kvm_skip_emulated_instruction(vcpu); |
bfd0a56b NHE |
8237 | } |
8238 | ||
a642fc30 PM |
8239 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
8240 | { | |
99b83ac8 WL |
8241 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
8242 | u32 vmx_instruction_info; | |
8243 | unsigned long type, types; | |
8244 | gva_t gva; | |
8245 | struct x86_exception e; | |
40352605 JM |
8246 | struct { |
8247 | u64 vpid; | |
8248 | u64 gla; | |
8249 | } operand; | |
99b83ac8 WL |
8250 | |
8251 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & | |
8252 | SECONDARY_EXEC_ENABLE_VPID) || | |
8253 | !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) { | |
8254 | kvm_queue_exception(vcpu, UD_VECTOR); | |
8255 | return 1; | |
8256 | } | |
8257 | ||
8258 | if (!nested_vmx_check_permission(vcpu)) | |
8259 | return 1; | |
8260 | ||
8261 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
8262 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); | |
8263 | ||
bcdde302 JD |
8264 | types = (vmx->nested.nested_vmx_vpid_caps & |
8265 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; | |
99b83ac8 | 8266 | |
85c856b3 | 8267 | if (type >= 32 || !(types & (1 << type))) { |
99b83ac8 WL |
8268 | nested_vmx_failValid(vcpu, |
8269 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 8270 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
8271 | } |
8272 | ||
8273 | /* according to the intel vmx instruction reference, the memory | |
8274 | * operand is read even if it isn't needed (e.g., for type==global) | |
8275 | */ | |
8276 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), | |
8277 | vmx_instruction_info, false, &gva)) | |
8278 | return 1; | |
40d2dba3 | 8279 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
99b83ac8 WL |
8280 | kvm_inject_page_fault(vcpu, &e); |
8281 | return 1; | |
8282 | } | |
40352605 JM |
8283 | if (operand.vpid >> 16) { |
8284 | nested_vmx_failValid(vcpu, | |
8285 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
8286 | return kvm_skip_emulated_instruction(vcpu); | |
8287 | } | |
99b83ac8 WL |
8288 | |
8289 | switch (type) { | |
bcdde302 | 8290 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
fd8cb433 | 8291 | if (is_noncanonical_address(operand.gla, vcpu)) { |
40352605 JM |
8292 | nested_vmx_failValid(vcpu, |
8293 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
8294 | return kvm_skip_emulated_instruction(vcpu); | |
8295 | } | |
8296 | /* fall through */ | |
ef697a71 | 8297 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
bcdde302 | 8298 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
40352605 | 8299 | if (!operand.vpid) { |
bcdde302 JD |
8300 | nested_vmx_failValid(vcpu, |
8301 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); | |
6affcbed | 8302 | return kvm_skip_emulated_instruction(vcpu); |
bcdde302 JD |
8303 | } |
8304 | break; | |
99b83ac8 | 8305 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
99b83ac8 WL |
8306 | break; |
8307 | default: | |
bcdde302 | 8308 | WARN_ON_ONCE(1); |
6affcbed | 8309 | return kvm_skip_emulated_instruction(vcpu); |
99b83ac8 WL |
8310 | } |
8311 | ||
6bf41e55 | 8312 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
bcdde302 JD |
8313 | nested_vmx_succeed(vcpu); |
8314 | ||
6affcbed | 8315 | return kvm_skip_emulated_instruction(vcpu); |
a642fc30 PM |
8316 | } |
8317 | ||
843e4330 KH |
8318 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
8319 | { | |
8320 | unsigned long exit_qualification; | |
8321 | ||
8322 | trace_kvm_pml_full(vcpu->vcpu_id); | |
8323 | ||
8324 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8325 | ||
8326 | /* | |
8327 | * PML buffer FULL happened while executing iret from NMI, | |
8328 | * "blocked by NMI" bit has to be set before next VM entry. | |
8329 | */ | |
8330 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && | |
d02fcf50 | 8331 | enable_vnmi && |
843e4330 KH |
8332 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
8333 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
8334 | GUEST_INTR_STATE_NMI); | |
8335 | ||
8336 | /* | |
8337 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do | |
8338 | * here.., and there's no userspace involvement needed for PML. | |
8339 | */ | |
8340 | return 1; | |
8341 | } | |
8342 | ||
64672c95 YJ |
8343 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
8344 | { | |
8345 | kvm_lapic_expired_hv_timer(vcpu); | |
8346 | return 1; | |
8347 | } | |
8348 | ||
41ab9372 BD |
8349 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
8350 | { | |
8351 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
41ab9372 BD |
8352 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
8353 | ||
8354 | /* Check for memory type validity */ | |
bb97a016 DH |
8355 | switch (address & VMX_EPTP_MT_MASK) { |
8356 | case VMX_EPTP_MT_UC: | |
41ab9372 BD |
8357 | if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_UC_BIT)) |
8358 | return false; | |
8359 | break; | |
bb97a016 | 8360 | case VMX_EPTP_MT_WB: |
41ab9372 BD |
8361 | if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPTP_WB_BIT)) |
8362 | return false; | |
8363 | break; | |
8364 | default: | |
8365 | return false; | |
8366 | } | |
8367 | ||
bb97a016 DH |
8368 | /* only 4 levels page-walk length are valid */ |
8369 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) | |
41ab9372 BD |
8370 | return false; |
8371 | ||
8372 | /* Reserved bits should not be set */ | |
8373 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) | |
8374 | return false; | |
8375 | ||
8376 | /* AD, if set, should be supported */ | |
bb97a016 | 8377 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
41ab9372 BD |
8378 | if (!(vmx->nested.nested_vmx_ept_caps & VMX_EPT_AD_BIT)) |
8379 | return false; | |
8380 | } | |
8381 | ||
8382 | return true; | |
8383 | } | |
8384 | ||
8385 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, | |
8386 | struct vmcs12 *vmcs12) | |
8387 | { | |
8388 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; | |
8389 | u64 address; | |
8390 | bool accessed_dirty; | |
8391 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; | |
8392 | ||
8393 | if (!nested_cpu_has_eptp_switching(vmcs12) || | |
8394 | !nested_cpu_has_ept(vmcs12)) | |
8395 | return 1; | |
8396 | ||
8397 | if (index >= VMFUNC_EPTP_ENTRIES) | |
8398 | return 1; | |
8399 | ||
8400 | ||
8401 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, | |
8402 | &address, index * 8, 8)) | |
8403 | return 1; | |
8404 | ||
bb97a016 | 8405 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
41ab9372 BD |
8406 | |
8407 | /* | |
8408 | * If the (L2) guest does a vmfunc to the currently | |
8409 | * active ept pointer, we don't have to do anything else | |
8410 | */ | |
8411 | if (vmcs12->ept_pointer != address) { | |
8412 | if (!valid_ept_address(vcpu, address)) | |
8413 | return 1; | |
8414 | ||
8415 | kvm_mmu_unload(vcpu); | |
8416 | mmu->ept_ad = accessed_dirty; | |
8417 | mmu->base_role.ad_disabled = !accessed_dirty; | |
8418 | vmcs12->ept_pointer = address; | |
8419 | /* | |
8420 | * TODO: Check what's the correct approach in case | |
8421 | * mmu reload fails. Currently, we just let the next | |
8422 | * reload potentially fail | |
8423 | */ | |
8424 | kvm_mmu_reload(vcpu); | |
8425 | } | |
8426 | ||
8427 | return 0; | |
8428 | } | |
8429 | ||
2a499e49 BD |
8430 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
8431 | { | |
27c42a1b BD |
8432 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
8433 | struct vmcs12 *vmcs12; | |
8434 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; | |
8435 | ||
8436 | /* | |
8437 | * VMFUNC is only supported for nested guests, but we always enable the | |
8438 | * secondary control for simplicity; for non-nested mode, fake that we | |
8439 | * didn't by injecting #UD. | |
8440 | */ | |
8441 | if (!is_guest_mode(vcpu)) { | |
8442 | kvm_queue_exception(vcpu, UD_VECTOR); | |
8443 | return 1; | |
8444 | } | |
8445 | ||
8446 | vmcs12 = get_vmcs12(vcpu); | |
8447 | if ((vmcs12->vm_function_control & (1 << function)) == 0) | |
8448 | goto fail; | |
41ab9372 BD |
8449 | |
8450 | switch (function) { | |
8451 | case 0: | |
8452 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) | |
8453 | goto fail; | |
8454 | break; | |
8455 | default: | |
8456 | goto fail; | |
8457 | } | |
8458 | return kvm_skip_emulated_instruction(vcpu); | |
27c42a1b BD |
8459 | |
8460 | fail: | |
8461 | nested_vmx_vmexit(vcpu, vmx->exit_reason, | |
8462 | vmcs_read32(VM_EXIT_INTR_INFO), | |
8463 | vmcs_readl(EXIT_QUALIFICATION)); | |
2a499e49 BD |
8464 | return 1; |
8465 | } | |
8466 | ||
6aa8b732 AK |
8467 | /* |
8468 | * The exit handlers return 1 if the exit was handled fully and guest execution | |
8469 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs | |
8470 | * to be done to userspace and return 0. | |
8471 | */ | |
772e0318 | 8472 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
6aa8b732 AK |
8473 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
8474 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, | |
988ad74f | 8475 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
f08864b4 | 8476 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
6aa8b732 | 8477 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
6aa8b732 AK |
8478 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
8479 | [EXIT_REASON_DR_ACCESS] = handle_dr, | |
8480 | [EXIT_REASON_CPUID] = handle_cpuid, | |
8481 | [EXIT_REASON_MSR_READ] = handle_rdmsr, | |
8482 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, | |
8483 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, | |
8484 | [EXIT_REASON_HLT] = handle_halt, | |
ec25d5e6 | 8485 | [EXIT_REASON_INVD] = handle_invd, |
a7052897 | 8486 | [EXIT_REASON_INVLPG] = handle_invlpg, |
fee84b07 | 8487 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
c21415e8 | 8488 | [EXIT_REASON_VMCALL] = handle_vmcall, |
27d6c865 | 8489 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
cd232ad0 | 8490 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
63846663 | 8491 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
6a4d7550 | 8492 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
49f705c5 | 8493 | [EXIT_REASON_VMREAD] = handle_vmread, |
cd232ad0 | 8494 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
49f705c5 | 8495 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
ec378aee NHE |
8496 | [EXIT_REASON_VMOFF] = handle_vmoff, |
8497 | [EXIT_REASON_VMON] = handle_vmon, | |
f78e0e2e SY |
8498 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
8499 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, | |
83d4c286 | 8500 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
c7c9c56c | 8501 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
e5edaa01 | 8502 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
2acf923e | 8503 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
37817f29 | 8504 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
a0861c02 | 8505 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
68f89400 MT |
8506 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
8507 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, | |
4b8d54f9 | 8508 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
87c00572 | 8509 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
5f3d45e7 | 8510 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
87c00572 | 8511 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
bfd0a56b | 8512 | [EXIT_REASON_INVEPT] = handle_invept, |
a642fc30 | 8513 | [EXIT_REASON_INVVPID] = handle_invvpid, |
45ec368c | 8514 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
75f4fc8d | 8515 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
f53cd63c WL |
8516 | [EXIT_REASON_XSAVES] = handle_xsaves, |
8517 | [EXIT_REASON_XRSTORS] = handle_xrstors, | |
843e4330 | 8518 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
2a499e49 | 8519 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
64672c95 | 8520 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
6aa8b732 AK |
8521 | }; |
8522 | ||
8523 | static const int kvm_vmx_max_exit_handlers = | |
50a3485c | 8524 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
6aa8b732 | 8525 | |
908a7bdd JK |
8526 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
8527 | struct vmcs12 *vmcs12) | |
8528 | { | |
8529 | unsigned long exit_qualification; | |
8530 | gpa_t bitmap, last_bitmap; | |
8531 | unsigned int port; | |
8532 | int size; | |
8533 | u8 b; | |
8534 | ||
908a7bdd | 8535 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
2f0a6397 | 8536 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
908a7bdd JK |
8537 | |
8538 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8539 | ||
8540 | port = exit_qualification >> 16; | |
8541 | size = (exit_qualification & 7) + 1; | |
8542 | ||
8543 | last_bitmap = (gpa_t)-1; | |
8544 | b = -1; | |
8545 | ||
8546 | while (size > 0) { | |
8547 | if (port < 0x8000) | |
8548 | bitmap = vmcs12->io_bitmap_a; | |
8549 | else if (port < 0x10000) | |
8550 | bitmap = vmcs12->io_bitmap_b; | |
8551 | else | |
1d804d07 | 8552 | return true; |
908a7bdd JK |
8553 | bitmap += (port & 0x7fff) / 8; |
8554 | ||
8555 | if (last_bitmap != bitmap) | |
54bf36aa | 8556 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
1d804d07 | 8557 | return true; |
908a7bdd | 8558 | if (b & (1 << (port & 7))) |
1d804d07 | 8559 | return true; |
908a7bdd JK |
8560 | |
8561 | port++; | |
8562 | size--; | |
8563 | last_bitmap = bitmap; | |
8564 | } | |
8565 | ||
1d804d07 | 8566 | return false; |
908a7bdd JK |
8567 | } |
8568 | ||
644d711a NHE |
8569 | /* |
8570 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, | |
8571 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed | |
8572 | * disinterest in the current event (read or write a specific MSR) by using an | |
8573 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. | |
8574 | */ | |
8575 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, | |
8576 | struct vmcs12 *vmcs12, u32 exit_reason) | |
8577 | { | |
8578 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; | |
8579 | gpa_t bitmap; | |
8580 | ||
cbd29cb6 | 8581 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
1d804d07 | 8582 | return true; |
644d711a NHE |
8583 | |
8584 | /* | |
8585 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, | |
8586 | * for the four combinations of read/write and low/high MSR numbers. | |
8587 | * First we need to figure out which of the four to use: | |
8588 | */ | |
8589 | bitmap = vmcs12->msr_bitmap; | |
8590 | if (exit_reason == EXIT_REASON_MSR_WRITE) | |
8591 | bitmap += 2048; | |
8592 | if (msr_index >= 0xc0000000) { | |
8593 | msr_index -= 0xc0000000; | |
8594 | bitmap += 1024; | |
8595 | } | |
8596 | ||
8597 | /* Then read the msr_index'th bit from this bitmap: */ | |
8598 | if (msr_index < 1024*8) { | |
8599 | unsigned char b; | |
54bf36aa | 8600 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
1d804d07 | 8601 | return true; |
644d711a NHE |
8602 | return 1 & (b >> (msr_index & 7)); |
8603 | } else | |
1d804d07 | 8604 | return true; /* let L1 handle the wrong parameter */ |
644d711a NHE |
8605 | } |
8606 | ||
8607 | /* | |
8608 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, | |
8609 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to | |
8610 | * intercept (via guest_host_mask etc.) the current event. | |
8611 | */ | |
8612 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, | |
8613 | struct vmcs12 *vmcs12) | |
8614 | { | |
8615 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); | |
8616 | int cr = exit_qualification & 15; | |
e1d39b17 JS |
8617 | int reg; |
8618 | unsigned long val; | |
644d711a NHE |
8619 | |
8620 | switch ((exit_qualification >> 4) & 3) { | |
8621 | case 0: /* mov to cr */ | |
e1d39b17 JS |
8622 | reg = (exit_qualification >> 8) & 15; |
8623 | val = kvm_register_readl(vcpu, reg); | |
644d711a NHE |
8624 | switch (cr) { |
8625 | case 0: | |
8626 | if (vmcs12->cr0_guest_host_mask & | |
8627 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 8628 | return true; |
644d711a NHE |
8629 | break; |
8630 | case 3: | |
8631 | if ((vmcs12->cr3_target_count >= 1 && | |
8632 | vmcs12->cr3_target_value0 == val) || | |
8633 | (vmcs12->cr3_target_count >= 2 && | |
8634 | vmcs12->cr3_target_value1 == val) || | |
8635 | (vmcs12->cr3_target_count >= 3 && | |
8636 | vmcs12->cr3_target_value2 == val) || | |
8637 | (vmcs12->cr3_target_count >= 4 && | |
8638 | vmcs12->cr3_target_value3 == val)) | |
1d804d07 | 8639 | return false; |
644d711a | 8640 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
1d804d07 | 8641 | return true; |
644d711a NHE |
8642 | break; |
8643 | case 4: | |
8644 | if (vmcs12->cr4_guest_host_mask & | |
8645 | (vmcs12->cr4_read_shadow ^ val)) | |
1d804d07 | 8646 | return true; |
644d711a NHE |
8647 | break; |
8648 | case 8: | |
8649 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) | |
1d804d07 | 8650 | return true; |
644d711a NHE |
8651 | break; |
8652 | } | |
8653 | break; | |
8654 | case 2: /* clts */ | |
8655 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && | |
8656 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) | |
1d804d07 | 8657 | return true; |
644d711a NHE |
8658 | break; |
8659 | case 1: /* mov from cr */ | |
8660 | switch (cr) { | |
8661 | case 3: | |
8662 | if (vmcs12->cpu_based_vm_exec_control & | |
8663 | CPU_BASED_CR3_STORE_EXITING) | |
1d804d07 | 8664 | return true; |
644d711a NHE |
8665 | break; |
8666 | case 8: | |
8667 | if (vmcs12->cpu_based_vm_exec_control & | |
8668 | CPU_BASED_CR8_STORE_EXITING) | |
1d804d07 | 8669 | return true; |
644d711a NHE |
8670 | break; |
8671 | } | |
8672 | break; | |
8673 | case 3: /* lmsw */ | |
8674 | /* | |
8675 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of | |
8676 | * cr0. Other attempted changes are ignored, with no exit. | |
8677 | */ | |
e1d39b17 | 8678 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
644d711a NHE |
8679 | if (vmcs12->cr0_guest_host_mask & 0xe & |
8680 | (val ^ vmcs12->cr0_read_shadow)) | |
1d804d07 | 8681 | return true; |
644d711a NHE |
8682 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
8683 | !(vmcs12->cr0_read_shadow & 0x1) && | |
8684 | (val & 0x1)) | |
1d804d07 | 8685 | return true; |
644d711a NHE |
8686 | break; |
8687 | } | |
1d804d07 | 8688 | return false; |
644d711a NHE |
8689 | } |
8690 | ||
8691 | /* | |
8692 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we | |
8693 | * should handle it ourselves in L0 (and then continue L2). Only call this | |
8694 | * when in is_guest_mode (L2). | |
8695 | */ | |
7313c698 | 8696 | static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason) |
644d711a | 8697 | { |
644d711a NHE |
8698 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
8699 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
8700 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
8701 | ||
4f350c6d JM |
8702 | if (vmx->nested.nested_run_pending) |
8703 | return false; | |
8704 | ||
8705 | if (unlikely(vmx->fail)) { | |
8706 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, | |
8707 | vmcs_read32(VM_INSTRUCTION_ERROR)); | |
8708 | return true; | |
8709 | } | |
542060ea | 8710 | |
c9f04407 DM |
8711 | /* |
8712 | * The host physical addresses of some pages of guest memory | |
8819227c JM |
8713 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
8714 | * Page). The CPU may write to these pages via their host | |
8715 | * physical address while L2 is running, bypassing any | |
8716 | * address-translation-based dirty tracking (e.g. EPT write | |
8717 | * protection). | |
c9f04407 DM |
8718 | * |
8719 | * Mark them dirty on every exit from L2 to prevent them from | |
8720 | * getting out of sync with dirty tracking. | |
8721 | */ | |
8722 | nested_mark_vmcs12_pages_dirty(vcpu); | |
8723 | ||
4f350c6d JM |
8724 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
8725 | vmcs_readl(EXIT_QUALIFICATION), | |
8726 | vmx->idt_vectoring_info, | |
8727 | intr_info, | |
8728 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
8729 | KVM_ISA_VMX); | |
644d711a NHE |
8730 | |
8731 | switch (exit_reason) { | |
8732 | case EXIT_REASON_EXCEPTION_NMI: | |
ef85b673 | 8733 | if (is_nmi(intr_info)) |
1d804d07 | 8734 | return false; |
644d711a | 8735 | else if (is_page_fault(intr_info)) |
52a5c155 | 8736 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
e504c909 | 8737 | else if (is_no_device(intr_info) && |
ccf9844e | 8738 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
1d804d07 | 8739 | return false; |
6f05485d JK |
8740 | else if (is_debug(intr_info) && |
8741 | vcpu->guest_debug & | |
8742 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
8743 | return false; | |
8744 | else if (is_breakpoint(intr_info) && | |
8745 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) | |
8746 | return false; | |
644d711a NHE |
8747 | return vmcs12->exception_bitmap & |
8748 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); | |
8749 | case EXIT_REASON_EXTERNAL_INTERRUPT: | |
1d804d07 | 8750 | return false; |
644d711a | 8751 | case EXIT_REASON_TRIPLE_FAULT: |
1d804d07 | 8752 | return true; |
644d711a | 8753 | case EXIT_REASON_PENDING_INTERRUPT: |
3b656cf7 | 8754 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
644d711a | 8755 | case EXIT_REASON_NMI_WINDOW: |
3b656cf7 | 8756 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
644d711a | 8757 | case EXIT_REASON_TASK_SWITCH: |
1d804d07 | 8758 | return true; |
644d711a | 8759 | case EXIT_REASON_CPUID: |
1d804d07 | 8760 | return true; |
644d711a NHE |
8761 | case EXIT_REASON_HLT: |
8762 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); | |
8763 | case EXIT_REASON_INVD: | |
1d804d07 | 8764 | return true; |
644d711a NHE |
8765 | case EXIT_REASON_INVLPG: |
8766 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); | |
8767 | case EXIT_REASON_RDPMC: | |
8768 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); | |
a5f46457 | 8769 | case EXIT_REASON_RDRAND: |
736fdf72 | 8770 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
a5f46457 | 8771 | case EXIT_REASON_RDSEED: |
736fdf72 | 8772 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
b3a2a907 | 8773 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
644d711a NHE |
8774 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
8775 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: | |
8776 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: | |
8777 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: | |
8778 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: | |
8779 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: | |
a642fc30 | 8780 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
644d711a NHE |
8781 | /* |
8782 | * VMX instructions trap unconditionally. This allows L1 to | |
8783 | * emulate them for its L2 guest, i.e., allows 3-level nesting! | |
8784 | */ | |
1d804d07 | 8785 | return true; |
644d711a NHE |
8786 | case EXIT_REASON_CR_ACCESS: |
8787 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); | |
8788 | case EXIT_REASON_DR_ACCESS: | |
8789 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); | |
8790 | case EXIT_REASON_IO_INSTRUCTION: | |
908a7bdd | 8791 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
1b07304c PB |
8792 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
8793 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); | |
644d711a NHE |
8794 | case EXIT_REASON_MSR_READ: |
8795 | case EXIT_REASON_MSR_WRITE: | |
8796 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); | |
8797 | case EXIT_REASON_INVALID_STATE: | |
1d804d07 | 8798 | return true; |
644d711a NHE |
8799 | case EXIT_REASON_MWAIT_INSTRUCTION: |
8800 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); | |
5f3d45e7 MD |
8801 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
8802 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); | |
644d711a NHE |
8803 | case EXIT_REASON_MONITOR_INSTRUCTION: |
8804 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); | |
8805 | case EXIT_REASON_PAUSE_INSTRUCTION: | |
8806 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || | |
8807 | nested_cpu_has2(vmcs12, | |
8808 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); | |
8809 | case EXIT_REASON_MCE_DURING_VMENTRY: | |
1d804d07 | 8810 | return false; |
644d711a | 8811 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
a7c0b07d | 8812 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
644d711a NHE |
8813 | case EXIT_REASON_APIC_ACCESS: |
8814 | return nested_cpu_has2(vmcs12, | |
8815 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
82f0dd4b | 8816 | case EXIT_REASON_APIC_WRITE: |
608406e2 WV |
8817 | case EXIT_REASON_EOI_INDUCED: |
8818 | /* apic_write and eoi_induced should exit unconditionally. */ | |
1d804d07 | 8819 | return true; |
644d711a | 8820 | case EXIT_REASON_EPT_VIOLATION: |
2b1be677 NHE |
8821 | /* |
8822 | * L0 always deals with the EPT violation. If nested EPT is | |
8823 | * used, and the nested mmu code discovers that the address is | |
8824 | * missing in the guest EPT table (EPT12), the EPT violation | |
8825 | * will be injected with nested_ept_inject_page_fault() | |
8826 | */ | |
1d804d07 | 8827 | return false; |
644d711a | 8828 | case EXIT_REASON_EPT_MISCONFIG: |
2b1be677 NHE |
8829 | /* |
8830 | * L2 never uses directly L1's EPT, but rather L0's own EPT | |
8831 | * table (shadow on EPT) or a merged EPT table that L0 built | |
8832 | * (EPT on EPT). So any problems with the structure of the | |
8833 | * table is L0's fault. | |
8834 | */ | |
1d804d07 | 8835 | return false; |
90a2db6d PB |
8836 | case EXIT_REASON_INVPCID: |
8837 | return | |
8838 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && | |
8839 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); | |
644d711a NHE |
8840 | case EXIT_REASON_WBINVD: |
8841 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); | |
8842 | case EXIT_REASON_XSETBV: | |
1d804d07 | 8843 | return true; |
81dc01f7 WL |
8844 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
8845 | /* | |
8846 | * This should never happen, since it is not possible to | |
8847 | * set XSS to a non-zero value---neither in L1 nor in L2. | |
8848 | * If if it were, XSS would have to be checked against | |
8849 | * the XSS exit bitmap in vmcs12. | |
8850 | */ | |
8851 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); | |
55123e3c WL |
8852 | case EXIT_REASON_PREEMPTION_TIMER: |
8853 | return false; | |
ab007cc9 | 8854 | case EXIT_REASON_PML_FULL: |
03efce6f | 8855 | /* We emulate PML support to L1. */ |
ab007cc9 | 8856 | return false; |
2a499e49 BD |
8857 | case EXIT_REASON_VMFUNC: |
8858 | /* VM functions are emulated through L2->L0 vmexits. */ | |
8859 | return false; | |
644d711a | 8860 | default: |
1d804d07 | 8861 | return true; |
644d711a NHE |
8862 | } |
8863 | } | |
8864 | ||
7313c698 PB |
8865 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
8866 | { | |
8867 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
8868 | ||
8869 | /* | |
8870 | * At this point, the exit interruption info in exit_intr_info | |
8871 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT | |
8872 | * we need to query the in-kernel LAPIC. | |
8873 | */ | |
8874 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); | |
8875 | if ((exit_intr_info & | |
8876 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == | |
8877 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { | |
8878 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
8879 | vmcs12->vm_exit_intr_error_code = | |
8880 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); | |
8881 | } | |
8882 | ||
8883 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, | |
8884 | vmcs_readl(EXIT_QUALIFICATION)); | |
8885 | return 1; | |
8886 | } | |
8887 | ||
586f9607 AK |
8888 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
8889 | { | |
8890 | *info1 = vmcs_readl(EXIT_QUALIFICATION); | |
8891 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); | |
8892 | } | |
8893 | ||
a3eaa864 | 8894 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
843e4330 | 8895 | { |
a3eaa864 KH |
8896 | if (vmx->pml_pg) { |
8897 | __free_page(vmx->pml_pg); | |
8898 | vmx->pml_pg = NULL; | |
8899 | } | |
843e4330 KH |
8900 | } |
8901 | ||
54bf36aa | 8902 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
843e4330 | 8903 | { |
54bf36aa | 8904 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
843e4330 KH |
8905 | u64 *pml_buf; |
8906 | u16 pml_idx; | |
8907 | ||
8908 | pml_idx = vmcs_read16(GUEST_PML_INDEX); | |
8909 | ||
8910 | /* Do nothing if PML buffer is empty */ | |
8911 | if (pml_idx == (PML_ENTITY_NUM - 1)) | |
8912 | return; | |
8913 | ||
8914 | /* PML index always points to next available PML buffer entity */ | |
8915 | if (pml_idx >= PML_ENTITY_NUM) | |
8916 | pml_idx = 0; | |
8917 | else | |
8918 | pml_idx++; | |
8919 | ||
8920 | pml_buf = page_address(vmx->pml_pg); | |
8921 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { | |
8922 | u64 gpa; | |
8923 | ||
8924 | gpa = pml_buf[pml_idx]; | |
8925 | WARN_ON(gpa & (PAGE_SIZE - 1)); | |
54bf36aa | 8926 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
843e4330 KH |
8927 | } |
8928 | ||
8929 | /* reset PML index */ | |
8930 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
8931 | } | |
8932 | ||
8933 | /* | |
8934 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. | |
8935 | * Called before reporting dirty_bitmap to userspace. | |
8936 | */ | |
8937 | static void kvm_flush_pml_buffers(struct kvm *kvm) | |
8938 | { | |
8939 | int i; | |
8940 | struct kvm_vcpu *vcpu; | |
8941 | /* | |
8942 | * We only need to kick vcpu out of guest mode here, as PML buffer | |
8943 | * is flushed at beginning of all VMEXITs, and it's obvious that only | |
8944 | * vcpus running in guest are possible to have unflushed GPAs in PML | |
8945 | * buffer. | |
8946 | */ | |
8947 | kvm_for_each_vcpu(i, vcpu, kvm) | |
8948 | kvm_vcpu_kick(vcpu); | |
8949 | } | |
8950 | ||
4eb64dce PB |
8951 | static void vmx_dump_sel(char *name, uint32_t sel) |
8952 | { | |
8953 | pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", | |
96794e4e | 8954 | name, vmcs_read16(sel), |
4eb64dce PB |
8955 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
8956 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), | |
8957 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); | |
8958 | } | |
8959 | ||
8960 | static void vmx_dump_dtsel(char *name, uint32_t limit) | |
8961 | { | |
8962 | pr_err("%s limit=0x%08x, base=0x%016lx\n", | |
8963 | name, vmcs_read32(limit), | |
8964 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); | |
8965 | } | |
8966 | ||
8967 | static void dump_vmcs(void) | |
8968 | { | |
8969 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); | |
8970 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); | |
8971 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); | |
8972 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); | |
8973 | u32 secondary_exec_control = 0; | |
8974 | unsigned long cr4 = vmcs_readl(GUEST_CR4); | |
f3531054 | 8975 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
4eb64dce PB |
8976 | int i, n; |
8977 | ||
8978 | if (cpu_has_secondary_exec_ctrls()) | |
8979 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
8980 | ||
8981 | pr_err("*** Guest State ***\n"); | |
8982 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
8983 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), | |
8984 | vmcs_readl(CR0_GUEST_HOST_MASK)); | |
8985 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", | |
8986 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); | |
8987 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); | |
8988 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && | |
8989 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) | |
8990 | { | |
845c5b40 PB |
8991 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
8992 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); | |
8993 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", | |
8994 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); | |
4eb64dce PB |
8995 | } |
8996 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", | |
8997 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); | |
8998 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", | |
8999 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); | |
9000 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
9001 | vmcs_readl(GUEST_SYSENTER_ESP), | |
9002 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); | |
9003 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); | |
9004 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); | |
9005 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); | |
9006 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); | |
9007 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); | |
9008 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); | |
9009 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); | |
9010 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); | |
9011 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); | |
9012 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); | |
9013 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || | |
9014 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) | |
845c5b40 PB |
9015 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
9016 | efer, vmcs_read64(GUEST_IA32_PAT)); | |
9017 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", | |
9018 | vmcs_read64(GUEST_IA32_DEBUGCTL), | |
4eb64dce PB |
9019 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
9020 | if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) | |
845c5b40 PB |
9021 | pr_err("PerfGlobCtl = 0x%016llx\n", |
9022 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce | 9023 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
845c5b40 | 9024 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
4eb64dce PB |
9025 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
9026 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), | |
9027 | vmcs_read32(GUEST_ACTIVITY_STATE)); | |
9028 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) | |
9029 | pr_err("InterruptStatus = %04x\n", | |
9030 | vmcs_read16(GUEST_INTR_STATUS)); | |
9031 | ||
9032 | pr_err("*** Host State ***\n"); | |
9033 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", | |
9034 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); | |
9035 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", | |
9036 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), | |
9037 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), | |
9038 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), | |
9039 | vmcs_read16(HOST_TR_SELECTOR)); | |
9040 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", | |
9041 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), | |
9042 | vmcs_readl(HOST_TR_BASE)); | |
9043 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", | |
9044 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); | |
9045 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", | |
9046 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), | |
9047 | vmcs_readl(HOST_CR4)); | |
9048 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", | |
9049 | vmcs_readl(HOST_IA32_SYSENTER_ESP), | |
9050 | vmcs_read32(HOST_IA32_SYSENTER_CS), | |
9051 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); | |
9052 | if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER)) | |
845c5b40 PB |
9053 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
9054 | vmcs_read64(HOST_IA32_EFER), | |
9055 | vmcs_read64(HOST_IA32_PAT)); | |
4eb64dce | 9056 | if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
845c5b40 PB |
9057 | pr_err("PerfGlobCtl = 0x%016llx\n", |
9058 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); | |
4eb64dce PB |
9059 | |
9060 | pr_err("*** Control State ***\n"); | |
9061 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", | |
9062 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); | |
9063 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); | |
9064 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", | |
9065 | vmcs_read32(EXCEPTION_BITMAP), | |
9066 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), | |
9067 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); | |
9068 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", | |
9069 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), | |
9070 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), | |
9071 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); | |
9072 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", | |
9073 | vmcs_read32(VM_EXIT_INTR_INFO), | |
9074 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), | |
9075 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); | |
9076 | pr_err(" reason=%08x qualification=%016lx\n", | |
9077 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); | |
9078 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", | |
9079 | vmcs_read32(IDT_VECTORING_INFO_FIELD), | |
9080 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); | |
845c5b40 | 9081 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
8cfe9866 | 9082 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
845c5b40 PB |
9083 | pr_err("TSC Multiplier = 0x%016llx\n", |
9084 | vmcs_read64(TSC_MULTIPLIER)); | |
4eb64dce PB |
9085 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
9086 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); | |
9087 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) | |
9088 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); | |
9089 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) | |
845c5b40 | 9090 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
4eb64dce PB |
9091 | n = vmcs_read32(CR3_TARGET_COUNT); |
9092 | for (i = 0; i + 1 < n; i += 4) | |
9093 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", | |
9094 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), | |
9095 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); | |
9096 | if (i < n) | |
9097 | pr_err("CR3 target%u=%016lx\n", | |
9098 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); | |
9099 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) | |
9100 | pr_err("PLE Gap=%08x Window=%08x\n", | |
9101 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); | |
9102 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) | |
9103 | pr_err("Virtual processor ID = 0x%04x\n", | |
9104 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); | |
9105 | } | |
9106 | ||
6aa8b732 AK |
9107 | /* |
9108 | * The guest has exited. See if we can fix it or if we need userspace | |
9109 | * assistance. | |
9110 | */ | |
851ba692 | 9111 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
6aa8b732 | 9112 | { |
29bd8a78 | 9113 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
a0861c02 | 9114 | u32 exit_reason = vmx->exit_reason; |
1155f76a | 9115 | u32 vectoring_info = vmx->idt_vectoring_info; |
29bd8a78 | 9116 | |
8b89fe1f PB |
9117 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
9118 | ||
843e4330 KH |
9119 | /* |
9120 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more | |
9121 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before | |
9122 | * querying dirty_bitmap, we only need to kick all vcpus out of guest | |
9123 | * mode as if vcpus is in root mode, the PML buffer must has been | |
9124 | * flushed already. | |
9125 | */ | |
9126 | if (enable_pml) | |
54bf36aa | 9127 | vmx_flush_pml_buffer(vcpu); |
843e4330 | 9128 | |
80ced186 | 9129 | /* If guest state is invalid, start emulating */ |
14168786 | 9130 | if (vmx->emulation_required) |
80ced186 | 9131 | return handle_invalid_guest_state(vcpu); |
1d5a4d9b | 9132 | |
7313c698 PB |
9133 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
9134 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); | |
644d711a | 9135 | |
5120702e | 9136 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
4eb64dce | 9137 | dump_vmcs(); |
5120702e MG |
9138 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
9139 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
9140 | = exit_reason; | |
9141 | return 0; | |
9142 | } | |
9143 | ||
29bd8a78 | 9144 | if (unlikely(vmx->fail)) { |
851ba692 AK |
9145 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
9146 | vcpu->run->fail_entry.hardware_entry_failure_reason | |
29bd8a78 AK |
9147 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
9148 | return 0; | |
9149 | } | |
6aa8b732 | 9150 | |
b9bf6882 XG |
9151 | /* |
9152 | * Note: | |
9153 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by | |
9154 | * delivery event since it indicates guest is accessing MMIO. | |
9155 | * The vm-exit can be triggered again after return to guest that | |
9156 | * will cause infinite loop. | |
9157 | */ | |
d77c26fc | 9158 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
1439442c | 9159 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
60637aac | 9160 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
b244c9fc | 9161 | exit_reason != EXIT_REASON_PML_FULL && |
b9bf6882 XG |
9162 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
9163 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
9164 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; | |
70bcd708 | 9165 | vcpu->run->internal.ndata = 3; |
b9bf6882 XG |
9166 | vcpu->run->internal.data[0] = vectoring_info; |
9167 | vcpu->run->internal.data[1] = exit_reason; | |
70bcd708 PB |
9168 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
9169 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { | |
9170 | vcpu->run->internal.ndata++; | |
9171 | vcpu->run->internal.data[3] = | |
9172 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); | |
9173 | } | |
b9bf6882 XG |
9174 | return 0; |
9175 | } | |
3b86cd99 | 9176 | |
d02fcf50 | 9177 | if (unlikely(!enable_vnmi && |
8a1b4392 PB |
9178 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
9179 | if (vmx_interrupt_allowed(vcpu)) { | |
9180 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
9181 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && | |
9182 | vcpu->arch.nmi_pending) { | |
9183 | /* | |
9184 | * This CPU don't support us in finding the end of an | |
9185 | * NMI-blocked window if the guest runs with IRQs | |
9186 | * disabled. So we pull the trigger after 1 s of | |
9187 | * futile waiting, but inform the user about this. | |
9188 | */ | |
9189 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " | |
9190 | "state on VCPU %d after 1 s timeout\n", | |
9191 | __func__, vcpu->vcpu_id); | |
9192 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; | |
9193 | } | |
9194 | } | |
9195 | ||
6aa8b732 AK |
9196 | if (exit_reason < kvm_vmx_max_exit_handlers |
9197 | && kvm_vmx_exit_handlers[exit_reason]) | |
851ba692 | 9198 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
6aa8b732 | 9199 | else { |
6c6c5e03 RK |
9200 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
9201 | exit_reason); | |
2bc19dc3 MT |
9202 | kvm_queue_exception(vcpu, UD_VECTOR); |
9203 | return 1; | |
6aa8b732 | 9204 | } |
6aa8b732 AK |
9205 | } |
9206 | ||
d665f9fc PB |
9207 | /* |
9208 | * Software based L1D cache flush which is used when microcode providing | |
9209 | * the cache control MSR is not loaded. | |
9210 | * | |
9211 | * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to | |
9212 | * flush it is required to read in 64 KiB because the replacement algorithm | |
9213 | * is not exactly LRU. This could be sized at runtime via topology | |
9214 | * information but as all relevant affected CPUs have 32KiB L1D cache size | |
9215 | * there is no point in doing so. | |
9216 | */ | |
f0ace387 | 9217 | static void vmx_l1d_flush(struct kvm_vcpu *vcpu) |
d665f9fc PB |
9218 | { |
9219 | int size = PAGE_SIZE << L1D_CACHE_ORDER; | |
f0ace387 PB |
9220 | |
9221 | /* | |
1384247a TG |
9222 | * This code is only executed when the the flush mode is 'cond' or |
9223 | * 'always' | |
f0ace387 | 9224 | */ |
12960b11 | 9225 | if (static_branch_likely(&vmx_l1d_flush_cond)) { |
64947f95 | 9226 | bool flush_l1d; |
bcbe4077 | 9227 | |
07edf60a | 9228 | /* |
64947f95 NS |
9229 | * Clear the per-vcpu flush bit, it gets set again |
9230 | * either from vcpu_run() or from one of the unsafe | |
9231 | * VMEXIT handlers. | |
07edf60a | 9232 | */ |
64947f95 | 9233 | flush_l1d = vcpu->arch.l1tf_flush_l1d; |
ae021965 | 9234 | vcpu->arch.l1tf_flush_l1d = false; |
64947f95 NS |
9235 | |
9236 | /* | |
9237 | * Clear the per-cpu flush bit, it gets set again from | |
9238 | * the interrupt handlers. | |
9239 | */ | |
9240 | flush_l1d |= kvm_get_cpu_l1tf_flush_l1d(); | |
9241 | kvm_clear_cpu_l1tf_flush_l1d(); | |
9242 | ||
bcbe4077 NS |
9243 | if (!flush_l1d) |
9244 | return; | |
07edf60a | 9245 | } |
f0ace387 PB |
9246 | |
9247 | vcpu->stat.l1d_flush++; | |
d665f9fc | 9248 | |
8e494dea PB |
9249 | if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
9250 | wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); | |
9251 | return; | |
9252 | } | |
9253 | ||
d665f9fc PB |
9254 | asm volatile( |
9255 | /* First ensure the pages are in the TLB */ | |
9256 | "xorl %%eax, %%eax\n" | |
9257 | ".Lpopulate_tlb:\n\t" | |
55bd6950 | 9258 | "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" |
d665f9fc PB |
9259 | "addl $4096, %%eax\n\t" |
9260 | "cmpl %%eax, %[size]\n\t" | |
9261 | "jne .Lpopulate_tlb\n\t" | |
9262 | "xorl %%eax, %%eax\n\t" | |
9263 | "cpuid\n\t" | |
9264 | /* Now fill the cache */ | |
9265 | "xorl %%eax, %%eax\n" | |
9266 | ".Lfill_cache:\n" | |
55bd6950 | 9267 | "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" |
d665f9fc PB |
9268 | "addl $64, %%eax\n\t" |
9269 | "cmpl %%eax, %[size]\n\t" | |
9270 | "jne .Lfill_cache\n\t" | |
9271 | "lfence\n" | |
55bd6950 | 9272 | :: [flush_pages] "r" (vmx_l1d_flush_pages), |
d665f9fc PB |
9273 | [size] "r" (size) |
9274 | : "eax", "ebx", "ecx", "edx"); | |
9275 | } | |
9276 | ||
95ba8273 | 9277 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
6e5d865c | 9278 | { |
a7c0b07d WL |
9279 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
9280 | ||
9281 | if (is_guest_mode(vcpu) && | |
9282 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
9283 | return; | |
9284 | ||
95ba8273 | 9285 | if (irr == -1 || tpr < irr) { |
6e5d865c YS |
9286 | vmcs_write32(TPR_THRESHOLD, 0); |
9287 | return; | |
9288 | } | |
9289 | ||
95ba8273 | 9290 | vmcs_write32(TPR_THRESHOLD, irr); |
6e5d865c YS |
9291 | } |
9292 | ||
d3f4c0a5 | 9293 | static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
8d14695f YZ |
9294 | { |
9295 | u32 sec_exec_control; | |
9296 | ||
d3f4c0a5 JM |
9297 | if (!lapic_in_kernel(vcpu)) |
9298 | return; | |
9299 | ||
dccbfcf5 RK |
9300 | /* Postpone execution until vmcs01 is the current VMCS. */ |
9301 | if (is_guest_mode(vcpu)) { | |
d3f4c0a5 | 9302 | to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true; |
dccbfcf5 RK |
9303 | return; |
9304 | } | |
9305 | ||
35754c98 | 9306 | if (!cpu_need_tpr_shadow(vcpu)) |
8d14695f YZ |
9307 | return; |
9308 | ||
9309 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
d3f4c0a5 JM |
9310 | sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
9311 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); | |
8d14695f | 9312 | |
d3f4c0a5 JM |
9313 | switch (kvm_get_apic_mode(vcpu)) { |
9314 | case LAPIC_MODE_INVALID: | |
9315 | WARN_ONCE(true, "Invalid local APIC state"); | |
9316 | case LAPIC_MODE_DISABLED: | |
9317 | break; | |
9318 | case LAPIC_MODE_XAPIC: | |
9319 | if (flexpriority_enabled) { | |
9320 | sec_exec_control |= | |
9321 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
9322 | vmx_flush_tlb(vcpu, true); | |
9323 | } | |
9324 | break; | |
9325 | case LAPIC_MODE_X2APIC: | |
9326 | if (cpu_has_vmx_virtualize_x2apic_mode()) | |
9327 | sec_exec_control |= | |
9328 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; | |
9329 | break; | |
8d14695f YZ |
9330 | } |
9331 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); | |
9332 | ||
4b0be90f | 9333 | vmx_update_msr_bitmap(vcpu); |
8d14695f YZ |
9334 | } |
9335 | ||
38b99173 TC |
9336 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
9337 | { | |
9338 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9339 | ||
9340 | /* | |
9341 | * Currently we do not handle the nested case where L2 has an | |
9342 | * APIC access page of its own; that page is still pinned. | |
9343 | * Hence, we skip the case where the VCPU is in guest mode _and_ | |
9344 | * L1 prepared an APIC access page for L2. | |
9345 | * | |
9346 | * For the case where L1 and L2 share the same APIC access page | |
9347 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear | |
9348 | * in the vmcs12), this function will only update either the vmcs01 | |
9349 | * or the vmcs02. If the former, the vmcs02 will be updated by | |
9350 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in | |
9351 | * the next L2->L1 exit. | |
9352 | */ | |
9353 | if (!is_guest_mode(vcpu) || | |
4f2777bc | 9354 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
fb6c8198 | 9355 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
38b99173 | 9356 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
6bf41e55 | 9357 | vmx_flush_tlb(vcpu, true); |
fb6c8198 | 9358 | } |
38b99173 TC |
9359 | } |
9360 | ||
67c9dddc | 9361 | static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr) |
c7c9c56c YZ |
9362 | { |
9363 | u16 status; | |
9364 | u8 old; | |
9365 | ||
67c9dddc PB |
9366 | if (max_isr == -1) |
9367 | max_isr = 0; | |
c7c9c56c YZ |
9368 | |
9369 | status = vmcs_read16(GUEST_INTR_STATUS); | |
9370 | old = status >> 8; | |
67c9dddc | 9371 | if (max_isr != old) { |
c7c9c56c | 9372 | status &= 0xff; |
67c9dddc | 9373 | status |= max_isr << 8; |
c7c9c56c YZ |
9374 | vmcs_write16(GUEST_INTR_STATUS, status); |
9375 | } | |
9376 | } | |
9377 | ||
9378 | static void vmx_set_rvi(int vector) | |
9379 | { | |
9380 | u16 status; | |
9381 | u8 old; | |
9382 | ||
4114c27d WW |
9383 | if (vector == -1) |
9384 | vector = 0; | |
9385 | ||
c7c9c56c YZ |
9386 | status = vmcs_read16(GUEST_INTR_STATUS); |
9387 | old = (u8)status & 0xff; | |
9388 | if ((u8)vector != old) { | |
9389 | status &= ~0xff; | |
9390 | status |= (u8)vector; | |
9391 | vmcs_write16(GUEST_INTR_STATUS, status); | |
9392 | } | |
9393 | } | |
9394 | ||
9395 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) | |
9396 | { | |
4114c27d WW |
9397 | if (!is_guest_mode(vcpu)) { |
9398 | vmx_set_rvi(max_irr); | |
9399 | return; | |
9400 | } | |
9401 | ||
c7c9c56c YZ |
9402 | if (max_irr == -1) |
9403 | return; | |
9404 | ||
963fee16 | 9405 | /* |
4114c27d WW |
9406 | * In guest mode. If a vmexit is needed, vmx_check_nested_events |
9407 | * handles it. | |
963fee16 | 9408 | */ |
4114c27d | 9409 | if (nested_exit_on_intr(vcpu)) |
963fee16 WL |
9410 | return; |
9411 | ||
963fee16 | 9412 | /* |
4114c27d | 9413 | * Else, fall back to pre-APICv interrupt injection since L2 |
963fee16 WL |
9414 | * is run without virtual interrupt delivery. |
9415 | */ | |
9416 | if (!kvm_event_needs_reinjection(vcpu) && | |
9417 | vmx_interrupt_allowed(vcpu)) { | |
9418 | kvm_queue_interrupt(vcpu, max_irr, false); | |
9419 | vmx_inject_irq(vcpu); | |
9420 | } | |
c7c9c56c YZ |
9421 | } |
9422 | ||
76dfafd5 | 9423 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
810e6def PB |
9424 | { |
9425 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
76dfafd5 | 9426 | int max_irr; |
810e6def | 9427 | |
76dfafd5 PB |
9428 | WARN_ON(!vcpu->arch.apicv_active); |
9429 | if (pi_test_on(&vmx->pi_desc)) { | |
9430 | pi_clear_on(&vmx->pi_desc); | |
9431 | /* | |
9432 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. | |
9433 | * But on x86 this is just a compiler barrier anyway. | |
9434 | */ | |
9435 | smp_mb__after_atomic(); | |
9436 | max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); | |
9437 | } else { | |
9438 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
9439 | } | |
9440 | vmx_hwapic_irr_update(vcpu, max_irr); | |
9441 | return max_irr; | |
810e6def PB |
9442 | } |
9443 | ||
050ee5a5 WL |
9444 | static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu) |
9445 | { | |
9446 | return pi_test_on(vcpu_to_pi_desc(vcpu)); | |
9447 | } | |
9448 | ||
6308630b | 9449 | static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) |
c7c9c56c | 9450 | { |
d62caabb | 9451 | if (!kvm_vcpu_apicv_active(vcpu)) |
3d81bc7e YZ |
9452 | return; |
9453 | ||
c7c9c56c YZ |
9454 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
9455 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); | |
9456 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); | |
9457 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); | |
9458 | } | |
9459 | ||
967235d3 PB |
9460 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
9461 | { | |
9462 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9463 | ||
9464 | pi_clear_on(&vmx->pi_desc); | |
9465 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); | |
9466 | } | |
9467 | ||
51aa01d1 | 9468 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
cf393f75 | 9469 | { |
7db8a9c1 | 9470 | if (vmx->exit_reason != EXIT_REASON_EXCEPTION_NMI) |
00eba012 AK |
9471 | return; |
9472 | ||
7db8a9c1 | 9473 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
a0861c02 | 9474 | |
1261bfa3 | 9475 | /* if exit due to PF check for async PF */ |
7db8a9c1 | 9476 | if (is_page_fault(vmx->exit_intr_info)) |
1261bfa3 WL |
9477 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
9478 | ||
a0861c02 | 9479 | /* Handle machine checks before interrupts are enabled */ |
7db8a9c1 | 9480 | if (is_machine_check(vmx->exit_intr_info)) |
a0861c02 AK |
9481 | kvm_machine_check(); |
9482 | ||
20f65983 | 9483 | /* We need to handle NMIs before interrupts are enabled */ |
7db8a9c1 | 9484 | if (is_nmi(vmx->exit_intr_info)) { |
ff9d07a0 | 9485 | kvm_before_handle_nmi(&vmx->vcpu); |
20f65983 | 9486 | asm("int $2"); |
ff9d07a0 ZY |
9487 | kvm_after_handle_nmi(&vmx->vcpu); |
9488 | } | |
51aa01d1 | 9489 | } |
20f65983 | 9490 | |
a547c6db YZ |
9491 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
9492 | { | |
9493 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
9494 | ||
a547c6db YZ |
9495 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
9496 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { | |
9497 | unsigned int vector; | |
9498 | unsigned long entry; | |
9499 | gate_desc *desc; | |
9500 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9501 | #ifdef CONFIG_X86_64 | |
9502 | unsigned long tmp; | |
9503 | #endif | |
9504 | ||
9505 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
9506 | desc = (gate_desc *)vmx->host_idt_base + vector; | |
64b163fa | 9507 | entry = gate_offset(desc); |
a547c6db YZ |
9508 | asm volatile( |
9509 | #ifdef CONFIG_X86_64 | |
9510 | "mov %%" _ASM_SP ", %[sp]\n\t" | |
9511 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" | |
9512 | "push $%c[ss]\n\t" | |
9513 | "push %[sp]\n\t" | |
9514 | #endif | |
9515 | "pushf\n\t" | |
a547c6db | 9516 | __ASM_SIZE(push) " $%c[cs]\n\t" |
405b60d2 | 9517 | CALL_NOSPEC |
a547c6db YZ |
9518 | : |
9519 | #ifdef CONFIG_X86_64 | |
3f62de5f | 9520 | [sp]"=&r"(tmp), |
a547c6db | 9521 | #endif |
f5caf621 | 9522 | ASM_CALL_CONSTRAINT |
a547c6db | 9523 | : |
405b60d2 | 9524 | THUNK_TARGET(entry), |
a547c6db YZ |
9525 | [ss]"i"(__KERNEL_DS), |
9526 | [cs]"i"(__KERNEL_CS) | |
9527 | ); | |
f2485b3e | 9528 | } |
a547c6db | 9529 | } |
c207aee4 | 9530 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
a547c6db | 9531 | |
4d5c8a07 | 9532 | static bool vmx_has_emulated_msr(int index) |
6d396b55 | 9533 | { |
4d5c8a07 TL |
9534 | switch (index) { |
9535 | case MSR_IA32_SMBASE: | |
9536 | /* | |
9537 | * We cannot do SMM unless we can run the guest in big | |
9538 | * real mode. | |
9539 | */ | |
9540 | return enable_unrestricted_guest || emulate_invalid_guest_state; | |
9541 | case MSR_AMD64_VIRT_SPEC_CTRL: | |
9542 | /* This is AMD only. */ | |
9543 | return false; | |
9544 | default: | |
9545 | return true; | |
9546 | } | |
6d396b55 PB |
9547 | } |
9548 | ||
da8999d3 LJ |
9549 | static bool vmx_mpx_supported(void) |
9550 | { | |
9551 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && | |
9552 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); | |
9553 | } | |
9554 | ||
55412b2e WL |
9555 | static bool vmx_xsaves_supported(void) |
9556 | { | |
9557 | return vmcs_config.cpu_based_2nd_exec_ctrl & | |
9558 | SECONDARY_EXEC_XSAVES; | |
9559 | } | |
9560 | ||
51aa01d1 AK |
9561 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
9562 | { | |
c5ca8e57 | 9563 | u32 exit_intr_info; |
51aa01d1 AK |
9564 | bool unblock_nmi; |
9565 | u8 vector; | |
9566 | bool idtv_info_valid; | |
9567 | ||
9568 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
20f65983 | 9569 | |
d02fcf50 | 9570 | if (enable_vnmi) { |
8a1b4392 PB |
9571 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
9572 | return; | |
9573 | /* | |
9574 | * Can't use vmx->exit_intr_info since we're not sure what | |
9575 | * the exit reason is. | |
9576 | */ | |
9577 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
9578 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; | |
9579 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; | |
9580 | /* | |
9581 | * SDM 3: 27.7.1.2 (September 2008) | |
9582 | * Re-set bit "block by NMI" before VM entry if vmexit caused by | |
9583 | * a guest IRET fault. | |
9584 | * SDM 3: 23.2.2 (September 2008) | |
9585 | * Bit 12 is undefined in any of the following cases: | |
9586 | * If the VM exit sets the valid bit in the IDT-vectoring | |
9587 | * information field. | |
9588 | * If the VM exit is due to a double fault. | |
9589 | */ | |
9590 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && | |
9591 | vector != DF_VECTOR && !idtv_info_valid) | |
9592 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, | |
9593 | GUEST_INTR_STATE_NMI); | |
9594 | else | |
9595 | vmx->loaded_vmcs->nmi_known_unmasked = | |
9596 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) | |
9597 | & GUEST_INTR_STATE_NMI); | |
9598 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) | |
9599 | vmx->loaded_vmcs->vnmi_blocked_time += | |
9600 | ktime_to_ns(ktime_sub(ktime_get(), | |
9601 | vmx->loaded_vmcs->entry_time)); | |
51aa01d1 AK |
9602 | } |
9603 | ||
3ab66e8a | 9604 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
83422e17 AK |
9605 | u32 idt_vectoring_info, |
9606 | int instr_len_field, | |
9607 | int error_code_field) | |
51aa01d1 | 9608 | { |
51aa01d1 AK |
9609 | u8 vector; |
9610 | int type; | |
9611 | bool idtv_info_valid; | |
9612 | ||
9613 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; | |
668f612f | 9614 | |
3ab66e8a JK |
9615 | vcpu->arch.nmi_injected = false; |
9616 | kvm_clear_exception_queue(vcpu); | |
9617 | kvm_clear_interrupt_queue(vcpu); | |
37b96e98 GN |
9618 | |
9619 | if (!idtv_info_valid) | |
9620 | return; | |
9621 | ||
3ab66e8a | 9622 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
3842d135 | 9623 | |
668f612f AK |
9624 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
9625 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; | |
37b96e98 | 9626 | |
64a7ec06 | 9627 | switch (type) { |
37b96e98 | 9628 | case INTR_TYPE_NMI_INTR: |
3ab66e8a | 9629 | vcpu->arch.nmi_injected = true; |
668f612f | 9630 | /* |
7b4a25cb | 9631 | * SDM 3: 27.7.1.2 (September 2008) |
37b96e98 GN |
9632 | * Clear bit "block by NMI" before VM entry if a NMI |
9633 | * delivery faulted. | |
668f612f | 9634 | */ |
3ab66e8a | 9635 | vmx_set_nmi_mask(vcpu, false); |
37b96e98 | 9636 | break; |
37b96e98 | 9637 | case INTR_TYPE_SOFT_EXCEPTION: |
3ab66e8a | 9638 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f GN |
9639 | /* fall through */ |
9640 | case INTR_TYPE_HARD_EXCEPTION: | |
35920a35 | 9641 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
83422e17 | 9642 | u32 err = vmcs_read32(error_code_field); |
851eb667 | 9643 | kvm_requeue_exception_e(vcpu, vector, err); |
35920a35 | 9644 | } else |
851eb667 | 9645 | kvm_requeue_exception(vcpu, vector); |
37b96e98 | 9646 | break; |
66fd3f7f | 9647 | case INTR_TYPE_SOFT_INTR: |
3ab66e8a | 9648 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
66fd3f7f | 9649 | /* fall through */ |
37b96e98 | 9650 | case INTR_TYPE_EXT_INTR: |
3ab66e8a | 9651 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
37b96e98 GN |
9652 | break; |
9653 | default: | |
9654 | break; | |
f7d9238f | 9655 | } |
cf393f75 AK |
9656 | } |
9657 | ||
83422e17 AK |
9658 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
9659 | { | |
3ab66e8a | 9660 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
83422e17 AK |
9661 | VM_EXIT_INSTRUCTION_LEN, |
9662 | IDT_VECTORING_ERROR_CODE); | |
9663 | } | |
9664 | ||
b463a6f7 AK |
9665 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
9666 | { | |
3ab66e8a | 9667 | __vmx_complete_interrupts(vcpu, |
b463a6f7 AK |
9668 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
9669 | VM_ENTRY_INSTRUCTION_LEN, | |
9670 | VM_ENTRY_EXCEPTION_ERROR_CODE); | |
9671 | ||
9672 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
9673 | } | |
9674 | ||
d7cd9796 GN |
9675 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
9676 | { | |
9677 | int i, nr_msrs; | |
9678 | struct perf_guest_switch_msr *msrs; | |
9679 | ||
9680 | msrs = perf_guest_get_msrs(&nr_msrs); | |
9681 | ||
9682 | if (!msrs) | |
9683 | return; | |
9684 | ||
9685 | for (i = 0; i < nr_msrs; i++) | |
9686 | if (msrs[i].host == msrs[i].guest) | |
9687 | clear_atomic_switch_msr(vmx, msrs[i].msr); | |
9688 | else | |
9689 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, | |
129ce7ac | 9690 | msrs[i].host, false); |
d7cd9796 GN |
9691 | } |
9692 | ||
33365e7a | 9693 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
64672c95 YJ |
9694 | { |
9695 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9696 | u64 tscl; | |
9697 | u32 delta_tsc; | |
9698 | ||
9699 | if (vmx->hv_deadline_tsc == -1) | |
9700 | return; | |
9701 | ||
9702 | tscl = rdtsc(); | |
9703 | if (vmx->hv_deadline_tsc > tscl) | |
9704 | /* sure to be 32 bit only because checked on set_hv_timer */ | |
9705 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> | |
9706 | cpu_preemption_timer_multi); | |
9707 | else | |
9708 | delta_tsc = 0; | |
9709 | ||
9710 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); | |
9711 | } | |
9712 | ||
a3b5ba49 | 9713 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
6aa8b732 | 9714 | { |
a2fa3e9f | 9715 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
d6e41f11 | 9716 | unsigned long debugctlmsr, cr3, cr4; |
104f226b | 9717 | |
8a1b4392 | 9718 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
d02fcf50 | 9719 | if (unlikely(!enable_vnmi && |
8a1b4392 PB |
9720 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
9721 | vmx->loaded_vmcs->entry_time = ktime_get(); | |
9722 | ||
104f226b AK |
9723 | /* Don't enter VMX if guest state is invalid, let the exit handler |
9724 | start emulation until we arrive back to a valid state */ | |
14168786 | 9725 | if (vmx->emulation_required) |
104f226b AK |
9726 | return; |
9727 | ||
a7653ecd RK |
9728 | if (vmx->ple_window_dirty) { |
9729 | vmx->ple_window_dirty = false; | |
9730 | vmcs_write32(PLE_WINDOW, vmx->ple_window); | |
9731 | } | |
9732 | ||
012f83cb AG |
9733 | if (vmx->nested.sync_shadow_vmcs) { |
9734 | copy_vmcs12_to_shadow(vmx); | |
9735 | vmx->nested.sync_shadow_vmcs = false; | |
9736 | } | |
9737 | ||
104f226b AK |
9738 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
9739 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); | |
9740 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) | |
9741 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); | |
9742 | ||
d6e41f11 | 9743 | cr3 = __get_current_cr3_fast(); |
44889942 | 9744 | if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) { |
d6e41f11 | 9745 | vmcs_writel(HOST_CR3, cr3); |
44889942 | 9746 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
d6e41f11 AL |
9747 | } |
9748 | ||
1e02ce4c | 9749 | cr4 = cr4_read_shadow(); |
44889942 | 9750 | if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) { |
d974baa3 | 9751 | vmcs_writel(HOST_CR4, cr4); |
44889942 | 9752 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
d974baa3 AL |
9753 | } |
9754 | ||
104f226b AK |
9755 | /* When single-stepping over STI and MOV SS, we must clear the |
9756 | * corresponding interruptibility bits in the guest state. Otherwise | |
9757 | * vmentry fails as it then expects bit 14 (BS) in pending debug | |
9758 | * exceptions being set, but that's not correct for the guest debugging | |
9759 | * case. */ | |
9760 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) | |
9761 | vmx_set_interrupt_shadow(vcpu, 0); | |
9762 | ||
54bafc23 WC |
9763 | kvm_load_guest_xcr0(vcpu); |
9764 | ||
b9dd21e1 PB |
9765 | if (static_cpu_has(X86_FEATURE_PKU) && |
9766 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && | |
9767 | vcpu->arch.pkru != vmx->host_pkru) | |
9768 | __write_pkru(vcpu->arch.pkru); | |
1be0e61c | 9769 | |
d7cd9796 | 9770 | atomic_switch_perf_msrs(vmx); |
2a7921b7 | 9771 | debugctlmsr = get_debugctlmsr(); |
d7cd9796 | 9772 | |
64672c95 YJ |
9773 | vmx_arm_hv_timer(vcpu); |
9774 | ||
74469996 KA |
9775 | /* |
9776 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if | |
9777 | * it's non-zero. Since vmentry is serialising on affected CPUs, there | |
9778 | * is no need to worry about the conditional branch over the wrmsr | |
9779 | * being speculatively taken. | |
9780 | */ | |
692b5d07 | 9781 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
74469996 | 9782 | |
d462b819 | 9783 | vmx->__launched = vmx->loaded_vmcs->launched; |
f0ace387 | 9784 | |
c7ed1048 | 9785 | /* L1D Flush includes CPU buffer clear to mitigate MDS */ |
bcbe4077 NS |
9786 | if (static_branch_unlikely(&vmx_l1d_should_flush)) |
9787 | vmx_l1d_flush(vcpu); | |
abf9852b TG |
9788 | else if (static_branch_unlikely(&mds_user_clear)) |
9789 | mds_clear_cpu_buffers(); | |
f0ace387 | 9790 | |
104f226b | 9791 | asm( |
6aa8b732 | 9792 | /* Store host registers */ |
b188c81f AK |
9793 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
9794 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ | |
9795 | "push %%" _ASM_CX " \n\t" | |
9796 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" | |
313dbd49 | 9797 | "je 1f \n\t" |
b188c81f | 9798 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
4ecac3fd | 9799 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
313dbd49 | 9800 | "1: \n\t" |
d3edefc0 | 9801 | /* Reload cr2 if changed */ |
b188c81f AK |
9802 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
9803 | "mov %%cr2, %%" _ASM_DX " \n\t" | |
9804 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" | |
d3edefc0 | 9805 | "je 2f \n\t" |
b188c81f | 9806 | "mov %%" _ASM_AX", %%cr2 \n\t" |
d3edefc0 | 9807 | "2: \n\t" |
6aa8b732 | 9808 | /* Check if vmlaunch of vmresume is needed */ |
61ce5ca0 | 9809 | "cmpb $0, %c[launched](%0) \n\t" |
6aa8b732 | 9810 | /* Load guest registers. Don't clobber flags. */ |
b188c81f AK |
9811 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
9812 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" | |
9813 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" | |
9814 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" | |
9815 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" | |
9816 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" | |
05b3e0c2 | 9817 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
9818 | "mov %c[r8](%0), %%r8 \n\t" |
9819 | "mov %c[r9](%0), %%r9 \n\t" | |
9820 | "mov %c[r10](%0), %%r10 \n\t" | |
9821 | "mov %c[r11](%0), %%r11 \n\t" | |
9822 | "mov %c[r12](%0), %%r12 \n\t" | |
9823 | "mov %c[r13](%0), %%r13 \n\t" | |
9824 | "mov %c[r14](%0), %%r14 \n\t" | |
9825 | "mov %c[r15](%0), %%r15 \n\t" | |
6aa8b732 | 9826 | #endif |
b188c81f | 9827 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
c801949d | 9828 | |
6aa8b732 | 9829 | /* Enter guest mode */ |
83287ea4 | 9830 | "jne 1f \n\t" |
4ecac3fd | 9831 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
83287ea4 AK |
9832 | "jmp 2f \n\t" |
9833 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" | |
9834 | "2: " | |
6aa8b732 | 9835 | /* Save guest registers, load host registers, keep flags */ |
b188c81f | 9836 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
40712fae | 9837 | "pop %0 \n\t" |
0cb5b306 | 9838 | "setbe %c[fail](%0)\n\t" |
b188c81f AK |
9839 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
9840 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" | |
9841 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" | |
9842 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" | |
9843 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" | |
9844 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" | |
9845 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" | |
05b3e0c2 | 9846 | #ifdef CONFIG_X86_64 |
e08aa78a AK |
9847 | "mov %%r8, %c[r8](%0) \n\t" |
9848 | "mov %%r9, %c[r9](%0) \n\t" | |
9849 | "mov %%r10, %c[r10](%0) \n\t" | |
9850 | "mov %%r11, %c[r11](%0) \n\t" | |
9851 | "mov %%r12, %c[r12](%0) \n\t" | |
9852 | "mov %%r13, %c[r13](%0) \n\t" | |
9853 | "mov %%r14, %c[r14](%0) \n\t" | |
9854 | "mov %%r15, %c[r15](%0) \n\t" | |
0cb5b306 JM |
9855 | "xor %%r8d, %%r8d \n\t" |
9856 | "xor %%r9d, %%r9d \n\t" | |
9857 | "xor %%r10d, %%r10d \n\t" | |
9858 | "xor %%r11d, %%r11d \n\t" | |
9859 | "xor %%r12d, %%r12d \n\t" | |
9860 | "xor %%r13d, %%r13d \n\t" | |
9861 | "xor %%r14d, %%r14d \n\t" | |
9862 | "xor %%r15d, %%r15d \n\t" | |
6aa8b732 | 9863 | #endif |
b188c81f AK |
9864 | "mov %%cr2, %%" _ASM_AX " \n\t" |
9865 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" | |
c801949d | 9866 | |
0cb5b306 JM |
9867 | "xor %%eax, %%eax \n\t" |
9868 | "xor %%ebx, %%ebx \n\t" | |
9869 | "xor %%esi, %%esi \n\t" | |
9870 | "xor %%edi, %%edi \n\t" | |
b188c81f | 9871 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
83287ea4 AK |
9872 | ".pushsection .rodata \n\t" |
9873 | ".global vmx_return \n\t" | |
9874 | "vmx_return: " _ASM_PTR " 2b \n\t" | |
9875 | ".popsection" | |
e08aa78a | 9876 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
d462b819 | 9877 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
e08aa78a | 9878 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
313dbd49 | 9879 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
ad312c7c ZX |
9880 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
9881 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), | |
9882 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), | |
9883 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), | |
9884 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), | |
9885 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), | |
9886 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), | |
05b3e0c2 | 9887 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
9888 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
9889 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), | |
9890 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), | |
9891 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), | |
9892 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), | |
9893 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), | |
9894 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), | |
9895 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), | |
6aa8b732 | 9896 | #endif |
40712fae AK |
9897 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
9898 | [wordsize]"i"(sizeof(ulong)) | |
c2036300 LV |
9899 | : "cc", "memory" |
9900 | #ifdef CONFIG_X86_64 | |
b188c81f | 9901 | , "rax", "rbx", "rdi", "rsi" |
c2036300 | 9902 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
b188c81f AK |
9903 | #else |
9904 | , "eax", "ebx", "edi", "esi" | |
c2036300 LV |
9905 | #endif |
9906 | ); | |
6aa8b732 | 9907 | |
74469996 KA |
9908 | /* |
9909 | * We do not use IBRS in the kernel. If this vCPU has used the | |
9910 | * SPEC_CTRL MSR it may have left it on; save the value and | |
9911 | * turn it off. This is much more efficient than blindly adding | |
9912 | * it to the atomic save/restore list. Especially as the former | |
9913 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. | |
9914 | * | |
9915 | * For non-nested case: | |
9916 | * If the L01 MSR bitmap does not intercept the MSR, then we need to | |
9917 | * save it. | |
9918 | * | |
9919 | * For nested case: | |
9920 | * If the L02 MSR bitmap does not intercept the MSR, then we need to | |
9921 | * save it. | |
9922 | */ | |
481ab71e | 9923 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
f471d71b | 9924 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
74469996 | 9925 | |
692b5d07 | 9926 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
74469996 | 9927 | |
117cc7a9 DW |
9928 | /* Eliminate branch target predictions from guest mode */ |
9929 | vmexit_fill_RSB(); | |
9930 | ||
2a7921b7 GN |
9931 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
9932 | if (debugctlmsr) | |
9933 | update_debugctlmsr(debugctlmsr); | |
9934 | ||
aa67f609 AK |
9935 | #ifndef CONFIG_X86_64 |
9936 | /* | |
9937 | * The sysexit path does not restore ds/es, so we must set them to | |
9938 | * a reasonable value ourselves. | |
9939 | * | |
9940 | * We can't defer this to vmx_load_host_state() since that function | |
9941 | * may be executed in interrupt context, which saves and restore segments | |
9942 | * around it, nullifying its effect. | |
9943 | */ | |
9944 | loadsegment(ds, __USER_DS); | |
9945 | loadsegment(es, __USER_DS); | |
9946 | #endif | |
9947 | ||
6de4f3ad | 9948 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
6de12732 | 9949 | | (1 << VCPU_EXREG_RFLAGS) |
aff48baa | 9950 | | (1 << VCPU_EXREG_PDPTR) |
2fb92db1 | 9951 | | (1 << VCPU_EXREG_SEGMENTS) |
aff48baa | 9952 | | (1 << VCPU_EXREG_CR3)); |
5fdbf976 MT |
9953 | vcpu->arch.regs_dirty = 0; |
9954 | ||
1be0e61c XG |
9955 | /* |
9956 | * eager fpu is enabled if PKEY is supported and CR4 is switched | |
9957 | * back on host, so it is safe to read guest PKRU from current | |
9958 | * XSAVE. | |
9959 | */ | |
b9dd21e1 PB |
9960 | if (static_cpu_has(X86_FEATURE_PKU) && |
9961 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { | |
9962 | vcpu->arch.pkru = __read_pkru(); | |
9963 | if (vcpu->arch.pkru != vmx->host_pkru) | |
1be0e61c | 9964 | __write_pkru(vmx->host_pkru); |
1be0e61c XG |
9965 | } |
9966 | ||
e0b890d3 GN |
9967 | /* |
9968 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if | |
9969 | * we did not inject a still-pending event to L1 now because of | |
9970 | * nested_run_pending, we need to re-enable this bit. | |
9971 | */ | |
9972 | if (vmx->nested.nested_run_pending) | |
9973 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
9974 | ||
54bafc23 WC |
9975 | kvm_put_guest_xcr0(vcpu); |
9976 | ||
e0b890d3 | 9977 | vmx->nested.nested_run_pending = 0; |
b060ca3b JM |
9978 | vmx->idt_vectoring_info = 0; |
9979 | ||
9980 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); | |
7db8a9c1 SC |
9981 | if ((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY) |
9982 | kvm_machine_check(); | |
9983 | ||
b060ca3b JM |
9984 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
9985 | return; | |
9986 | ||
9987 | vmx->loaded_vmcs->launched = 1; | |
9988 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); | |
e0b890d3 | 9989 | |
51aa01d1 AK |
9990 | vmx_complete_atomic_exit(vmx); |
9991 | vmx_recover_nmi_blocking(vmx); | |
cf393f75 | 9992 | vmx_complete_interrupts(vmx); |
6aa8b732 | 9993 | } |
c207aee4 | 9994 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
6aa8b732 | 9995 | |
1279a6b1 | 9996 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
4fa7734c PB |
9997 | { |
9998 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9999 | int cpu; | |
10000 | ||
1279a6b1 | 10001 | if (vmx->loaded_vmcs == vmcs) |
4fa7734c PB |
10002 | return; |
10003 | ||
10004 | cpu = get_cpu(); | |
4fa7734c | 10005 | vmx_vcpu_put(vcpu); |
8a12167a | 10006 | vmx->loaded_vmcs = vmcs; |
4fa7734c | 10007 | vmx_vcpu_load(vcpu, cpu); |
4fa7734c PB |
10008 | put_cpu(); |
10009 | } | |
10010 | ||
2f1fe811 JM |
10011 | /* |
10012 | * Ensure that the current vmcs of the logical processor is the | |
10013 | * vmcs01 of the vcpu before calling free_nested(). | |
10014 | */ | |
10015 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) | |
10016 | { | |
10017 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10018 | int r; | |
10019 | ||
10020 | r = vcpu_load(vcpu); | |
10021 | BUG_ON(r); | |
1279a6b1 | 10022 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
2f1fe811 JM |
10023 | free_nested(vmx); |
10024 | vcpu_put(vcpu); | |
10025 | } | |
10026 | ||
6aa8b732 AK |
10027 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
10028 | { | |
fb3f0f51 RR |
10029 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
10030 | ||
843e4330 | 10031 | if (enable_pml) |
a3eaa864 | 10032 | vmx_destroy_pml_buffer(vmx); |
991e7a0e | 10033 | free_vpid(vmx->vpid); |
4fa7734c | 10034 | leave_guest_mode(vcpu); |
2f1fe811 | 10035 | vmx_free_vcpu_nested(vcpu); |
4fa7734c | 10036 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 RR |
10037 | kfree(vmx->guest_msrs); |
10038 | kvm_vcpu_uninit(vcpu); | |
a4770347 | 10039 | kmem_cache_free(kvm_vcpu_cache, vmx); |
6aa8b732 AK |
10040 | } |
10041 | ||
fb3f0f51 | 10042 | static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) |
6aa8b732 | 10043 | { |
fb3f0f51 | 10044 | int err; |
c16f862d | 10045 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
4b0be90f | 10046 | unsigned long *msr_bitmap; |
15ad7146 | 10047 | int cpu; |
6aa8b732 | 10048 | |
a2fa3e9f | 10049 | if (!vmx) |
fb3f0f51 RR |
10050 | return ERR_PTR(-ENOMEM); |
10051 | ||
991e7a0e | 10052 | vmx->vpid = allocate_vpid(); |
2384d2b3 | 10053 | |
fb3f0f51 RR |
10054 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
10055 | if (err) | |
10056 | goto free_vcpu; | |
965b58a5 | 10057 | |
4e59516a PF |
10058 | err = -ENOMEM; |
10059 | ||
10060 | /* | |
10061 | * If PML is turned on, failure on enabling PML just results in failure | |
10062 | * of creating the vcpu, therefore we can simplify PML logic (by | |
10063 | * avoiding dealing with cases, such as enabling PML partially on vcpus | |
10064 | * for the guest, etc. | |
10065 | */ | |
10066 | if (enable_pml) { | |
10067 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
10068 | if (!vmx->pml_pg) | |
10069 | goto uninit_vcpu; | |
10070 | } | |
10071 | ||
a2fa3e9f | 10072 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
03916db9 PB |
10073 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
10074 | > PAGE_SIZE); | |
0123be42 | 10075 | |
4e59516a PF |
10076 | if (!vmx->guest_msrs) |
10077 | goto free_pml; | |
965b58a5 | 10078 | |
b6d7026d PB |
10079 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
10080 | if (err < 0) | |
fb3f0f51 | 10081 | goto free_msrs; |
a2fa3e9f | 10082 | |
4b0be90f PB |
10083 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
10084 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); | |
10085 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); | |
10086 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); | |
10087 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); | |
10088 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); | |
10089 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); | |
10090 | vmx->msr_bitmap_mode = 0; | |
10091 | ||
b6d7026d | 10092 | vmx->loaded_vmcs = &vmx->vmcs01; |
15ad7146 AK |
10093 | cpu = get_cpu(); |
10094 | vmx_vcpu_load(&vmx->vcpu, cpu); | |
e48672fa | 10095 | vmx->vcpu.cpu = cpu; |
12d79917 | 10096 | vmx_vcpu_setup(vmx); |
fb3f0f51 | 10097 | vmx_vcpu_put(&vmx->vcpu); |
15ad7146 | 10098 | put_cpu(); |
35754c98 | 10099 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
be6d05cf JK |
10100 | err = alloc_apic_access_page(kvm); |
10101 | if (err) | |
5e4a0b3c | 10102 | goto free_vmcs; |
a63cb560 | 10103 | } |
fb3f0f51 | 10104 | |
b927a3ce | 10105 | if (enable_ept) { |
f51770ed TC |
10106 | err = init_rmode_identity_map(kvm); |
10107 | if (err) | |
93ea5388 | 10108 | goto free_vmcs; |
b927a3ce | 10109 | } |
b7ebfb05 | 10110 | |
23f2bb27 | 10111 | if (nested) |
b9c237bb WV |
10112 | nested_vmx_setup_ctls_msrs(vmx); |
10113 | ||
705699a1 | 10114 | vmx->nested.posted_intr_nv = -1; |
a9d30f33 | 10115 | vmx->nested.current_vmptr = -1ull; |
a9d30f33 | 10116 | |
37e4c997 HZ |
10117 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
10118 | ||
31afb2ea PB |
10119 | /* |
10120 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR | |
10121 | * or POSTED_INTR_WAKEUP_VECTOR. | |
10122 | */ | |
10123 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; | |
10124 | vmx->pi_desc.sn = 1; | |
10125 | ||
fb3f0f51 RR |
10126 | return &vmx->vcpu; |
10127 | ||
10128 | free_vmcs: | |
5f3fbc34 | 10129 | free_loaded_vmcs(vmx->loaded_vmcs); |
fb3f0f51 | 10130 | free_msrs: |
fb3f0f51 | 10131 | kfree(vmx->guest_msrs); |
4e59516a PF |
10132 | free_pml: |
10133 | vmx_destroy_pml_buffer(vmx); | |
fb3f0f51 RR |
10134 | uninit_vcpu: |
10135 | kvm_vcpu_uninit(&vmx->vcpu); | |
10136 | free_vcpu: | |
991e7a0e | 10137 | free_vpid(vmx->vpid); |
a4770347 | 10138 | kmem_cache_free(kvm_vcpu_cache, vmx); |
fb3f0f51 | 10139 | return ERR_PTR(err); |
6aa8b732 AK |
10140 | } |
10141 | ||
c7ed1048 TH |
10142 | #define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n" |
10143 | #define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n" | |
275b72a9 KRW |
10144 | |
10145 | static int vmx_vm_init(struct kvm *kvm) | |
10146 | { | |
24fcb53c JK |
10147 | if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) { |
10148 | switch (l1tf_mitigation) { | |
10149 | case L1TF_MITIGATION_OFF: | |
10150 | case L1TF_MITIGATION_FLUSH_NOWARN: | |
10151 | /* 'I explicitly don't care' is set */ | |
10152 | break; | |
10153 | case L1TF_MITIGATION_FLUSH: | |
10154 | case L1TF_MITIGATION_FLUSH_NOSMT: | |
10155 | case L1TF_MITIGATION_FULL: | |
10156 | /* | |
10157 | * Warn upon starting the first VM in a potentially | |
10158 | * insecure environment. | |
10159 | */ | |
67cd6fac | 10160 | if (sched_smt_active()) |
24fcb53c JK |
10161 | pr_warn_once(L1TF_MSG_SMT); |
10162 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER) | |
10163 | pr_warn_once(L1TF_MSG_L1D); | |
10164 | break; | |
10165 | case L1TF_MITIGATION_FULL_FORCE: | |
10166 | /* Flush is enforced */ | |
10167 | break; | |
275b72a9 | 10168 | } |
275b72a9 KRW |
10169 | } |
10170 | return 0; | |
10171 | } | |
10172 | ||
002c7f7c YS |
10173 | static void __init vmx_check_processor_compat(void *rtn) |
10174 | { | |
10175 | struct vmcs_config vmcs_conf; | |
10176 | ||
10177 | *(int *)rtn = 0; | |
10178 | if (setup_vmcs_config(&vmcs_conf) < 0) | |
10179 | *(int *)rtn = -EIO; | |
10180 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { | |
10181 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", | |
10182 | smp_processor_id()); | |
10183 | *(int *)rtn = -EIO; | |
10184 | } | |
10185 | } | |
10186 | ||
4b12f0de | 10187 | static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
64d4d521 | 10188 | { |
b18d5431 XG |
10189 | u8 cache; |
10190 | u64 ipat = 0; | |
4b12f0de | 10191 | |
522c68c4 | 10192 | /* For VT-d and EPT combination |
606decd6 | 10193 | * 1. MMIO: always map as UC |
522c68c4 SY |
10194 | * 2. EPT with VT-d: |
10195 | * a. VT-d without snooping control feature: can't guarantee the | |
606decd6 | 10196 | * result, try to trust guest. |
522c68c4 SY |
10197 | * b. VT-d with snooping control feature: snooping control feature of |
10198 | * VT-d engine can guarantee the cache correctness. Just set it | |
10199 | * to WB to keep consistent with host. So the same as item 3. | |
a19a6d11 | 10200 | * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep |
522c68c4 SY |
10201 | * consistent with host MTRR |
10202 | */ | |
606decd6 PB |
10203 | if (is_mmio) { |
10204 | cache = MTRR_TYPE_UNCACHABLE; | |
10205 | goto exit; | |
10206 | } | |
10207 | ||
10208 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { | |
b18d5431 XG |
10209 | ipat = VMX_EPT_IPAT_BIT; |
10210 | cache = MTRR_TYPE_WRBACK; | |
10211 | goto exit; | |
10212 | } | |
10213 | ||
10214 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { | |
10215 | ipat = VMX_EPT_IPAT_BIT; | |
0da029ed | 10216 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
fb279950 XG |
10217 | cache = MTRR_TYPE_WRBACK; |
10218 | else | |
10219 | cache = MTRR_TYPE_UNCACHABLE; | |
b18d5431 XG |
10220 | goto exit; |
10221 | } | |
10222 | ||
ff53604b | 10223 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
b18d5431 XG |
10224 | |
10225 | exit: | |
10226 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; | |
64d4d521 SY |
10227 | } |
10228 | ||
17cc3935 | 10229 | static int vmx_get_lpage_level(void) |
344f414f | 10230 | { |
878403b7 SY |
10231 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
10232 | return PT_DIRECTORY_LEVEL; | |
10233 | else | |
10234 | /* For shadow and EPT supported 1GB page */ | |
10235 | return PT_PDPE_LEVEL; | |
344f414f JR |
10236 | } |
10237 | ||
feda805f XG |
10238 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
10239 | { | |
10240 | /* | |
10241 | * These bits in the secondary execution controls field | |
10242 | * are dynamic, the others are mostly based on the hypervisor | |
10243 | * architecture and the guest's CPUID. Do not touch the | |
10244 | * dynamic bits. | |
10245 | */ | |
10246 | u32 mask = | |
10247 | SECONDARY_EXEC_SHADOW_VMCS | | |
10248 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | | |
10249 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; | |
10250 | ||
10251 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); | |
10252 | ||
10253 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, | |
10254 | (new_ctl & ~mask) | (cur_ctl & mask)); | |
10255 | } | |
10256 | ||
8322ebbb DM |
10257 | /* |
10258 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits | |
10259 | * (indicating "allowed-1") if they are supported in the guest's CPUID. | |
10260 | */ | |
10261 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) | |
10262 | { | |
10263 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10264 | struct kvm_cpuid_entry2 *entry; | |
10265 | ||
10266 | vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff; | |
10267 | vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE; | |
10268 | ||
10269 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ | |
10270 | if (entry && (entry->_reg & (_cpuid_mask))) \ | |
10271 | vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \ | |
10272 | } while (0) | |
10273 | ||
10274 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); | |
10275 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); | |
10276 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); | |
10277 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); | |
10278 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); | |
10279 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); | |
10280 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); | |
10281 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); | |
10282 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); | |
10283 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); | |
10284 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); | |
10285 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); | |
10286 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); | |
10287 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); | |
10288 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); | |
10289 | ||
10290 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); | |
10291 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); | |
10292 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); | |
10293 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); | |
10294 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); | |
c4ad77e0 | 10295 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
8322ebbb DM |
10296 | |
10297 | #undef cr4_fixed1_update | |
10298 | } | |
10299 | ||
0e851880 SY |
10300 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
10301 | { | |
4e47c7a6 | 10302 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
4e47c7a6 | 10303 | |
80154d77 PB |
10304 | if (cpu_has_secondary_exec_ctrls()) { |
10305 | vmx_compute_secondary_exec_control(vmx); | |
10306 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); | |
ad756a16 | 10307 | } |
8b3e34e4 | 10308 | |
37e4c997 HZ |
10309 | if (nested_vmx_allowed(vcpu)) |
10310 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
10311 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
10312 | else | |
10313 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
10314 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; | |
8322ebbb DM |
10315 | |
10316 | if (nested_vmx_allowed(vcpu)) | |
10317 | nested_vmx_cr_fixed1_bits_update(vcpu); | |
0e851880 SY |
10318 | } |
10319 | ||
d4330ef2 JR |
10320 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
10321 | { | |
7b8050f5 NHE |
10322 | if (func == 1 && nested) |
10323 | entry->ecx |= bit(X86_FEATURE_VMX); | |
d4330ef2 JR |
10324 | } |
10325 | ||
25d92081 YZ |
10326 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
10327 | struct x86_exception *fault) | |
10328 | { | |
533558bc | 10329 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
c5f983f6 | 10330 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
533558bc | 10331 | u32 exit_reason; |
c5f983f6 | 10332 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
25d92081 | 10333 | |
c5f983f6 BD |
10334 | if (vmx->nested.pml_full) { |
10335 | exit_reason = EXIT_REASON_PML_FULL; | |
10336 | vmx->nested.pml_full = false; | |
10337 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; | |
10338 | } else if (fault->error_code & PFERR_RSVD_MASK) | |
533558bc | 10339 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
25d92081 | 10340 | else |
533558bc | 10341 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
c5f983f6 BD |
10342 | |
10343 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); | |
25d92081 YZ |
10344 | vmcs12->guest_physical_address = fault->address; |
10345 | } | |
10346 | ||
995f00a6 PF |
10347 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
10348 | { | |
bb97a016 | 10349 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
995f00a6 PF |
10350 | } |
10351 | ||
155a97a3 NHE |
10352 | /* Callbacks for nested_ept_init_mmu_context: */ |
10353 | ||
10354 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) | |
10355 | { | |
10356 | /* return the page table to be shadowed - in our case, EPT12 */ | |
10357 | return get_vmcs12(vcpu)->ept_pointer; | |
10358 | } | |
10359 | ||
ae1e2d10 | 10360 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
155a97a3 | 10361 | { |
ad896af0 | 10362 | WARN_ON(mmu_is_nested(vcpu)); |
a057e0e2 | 10363 | if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu))) |
ae1e2d10 PB |
10364 | return 1; |
10365 | ||
10366 | kvm_mmu_unload(vcpu); | |
ad896af0 | 10367 | kvm_init_shadow_ept_mmu(vcpu, |
b9c237bb | 10368 | to_vmx(vcpu)->nested.nested_vmx_ept_caps & |
ae1e2d10 | 10369 | VMX_EPT_EXECUTE_ONLY_BIT, |
a057e0e2 | 10370 | nested_ept_ad_enabled(vcpu)); |
155a97a3 NHE |
10371 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
10372 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; | |
10373 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; | |
10374 | ||
10375 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; | |
ae1e2d10 | 10376 | return 0; |
155a97a3 NHE |
10377 | } |
10378 | ||
10379 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) | |
10380 | { | |
10381 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; | |
10382 | } | |
10383 | ||
19d5f10b EK |
10384 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
10385 | u16 error_code) | |
10386 | { | |
10387 | bool inequality, bit; | |
10388 | ||
10389 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; | |
10390 | inequality = | |
10391 | (error_code & vmcs12->page_fault_error_code_mask) != | |
10392 | vmcs12->page_fault_error_code_match; | |
10393 | return inequality ^ bit; | |
10394 | } | |
10395 | ||
feaf0c7d GN |
10396 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
10397 | struct x86_exception *fault) | |
10398 | { | |
10399 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
10400 | ||
10401 | WARN_ON(!is_guest_mode(vcpu)); | |
10402 | ||
305d0ab4 WL |
10403 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
10404 | !to_vmx(vcpu)->nested.nested_run_pending) { | |
b96fb439 PB |
10405 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
10406 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, | |
10407 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | | |
10408 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, | |
10409 | fault->address); | |
7313c698 | 10410 | } else { |
feaf0c7d | 10411 | kvm_inject_page_fault(vcpu, fault); |
7313c698 | 10412 | } |
feaf0c7d GN |
10413 | } |
10414 | ||
6beb7bd5 JM |
10415 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
10416 | struct vmcs12 *vmcs12); | |
10417 | ||
10418 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, | |
a2bcba50 WL |
10419 | struct vmcs12 *vmcs12) |
10420 | { | |
10421 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
5e2f30b7 | 10422 | struct page *page; |
6beb7bd5 | 10423 | u64 hpa; |
a2bcba50 WL |
10424 | |
10425 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
a2bcba50 WL |
10426 | /* |
10427 | * Translate L1 physical address to host physical | |
10428 | * address for vmcs02. Keep the page pinned, so this | |
10429 | * physical address remains valid. We keep a reference | |
10430 | * to it so we can release it later. | |
10431 | */ | |
5e2f30b7 | 10432 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
53a70daf | 10433 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
5e2f30b7 DH |
10434 | vmx->nested.apic_access_page = NULL; |
10435 | } | |
10436 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); | |
6beb7bd5 JM |
10437 | /* |
10438 | * If translation failed, no matter: This feature asks | |
10439 | * to exit when accessing the given address, and if it | |
10440 | * can never be accessed, this feature won't do | |
10441 | * anything anyway. | |
10442 | */ | |
5e2f30b7 DH |
10443 | if (!is_error_page(page)) { |
10444 | vmx->nested.apic_access_page = page; | |
6beb7bd5 JM |
10445 | hpa = page_to_phys(vmx->nested.apic_access_page); |
10446 | vmcs_write64(APIC_ACCESS_ADDR, hpa); | |
10447 | } else { | |
10448 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, | |
10449 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
10450 | } | |
10451 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && | |
10452 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { | |
10453 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, | |
10454 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); | |
10455 | kvm_vcpu_reload_apic_access_page(vcpu); | |
a2bcba50 | 10456 | } |
a7c0b07d WL |
10457 | |
10458 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { | |
5e2f30b7 | 10459 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
53a70daf | 10460 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
5e2f30b7 DH |
10461 | vmx->nested.virtual_apic_page = NULL; |
10462 | } | |
10463 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); | |
a7c0b07d WL |
10464 | |
10465 | /* | |
6beb7bd5 JM |
10466 | * If translation failed, VM entry will fail because |
10467 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. | |
10468 | * Failing the vm entry is _not_ what the processor | |
10469 | * does but it's basically the only possibility we | |
10470 | * have. We could still enter the guest if CR8 load | |
10471 | * exits are enabled, CR8 store exits are enabled, and | |
10472 | * virtualize APIC access is disabled; in this case | |
10473 | * the processor would never use the TPR shadow and we | |
10474 | * could simply clear the bit from the execution | |
10475 | * control. But such a configuration is useless, so | |
10476 | * let's keep the code simple. | |
a7c0b07d | 10477 | */ |
5e2f30b7 DH |
10478 | if (!is_error_page(page)) { |
10479 | vmx->nested.virtual_apic_page = page; | |
6beb7bd5 JM |
10480 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
10481 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); | |
10482 | } | |
a7c0b07d WL |
10483 | } |
10484 | ||
705699a1 | 10485 | if (nested_cpu_has_posted_intr(vmcs12)) { |
705699a1 WV |
10486 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
10487 | kunmap(vmx->nested.pi_desc_page); | |
53a70daf | 10488 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
5e2f30b7 | 10489 | vmx->nested.pi_desc_page = NULL; |
3cf01066 CC |
10490 | vmx->nested.pi_desc = NULL; |
10491 | vmcs_write64(POSTED_INTR_DESC_ADDR, -1ull); | |
705699a1 | 10492 | } |
5e2f30b7 DH |
10493 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
10494 | if (is_error_page(page)) | |
6beb7bd5 | 10495 | return; |
5e2f30b7 DH |
10496 | vmx->nested.pi_desc_page = page; |
10497 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); | |
705699a1 WV |
10498 | vmx->nested.pi_desc = |
10499 | (struct pi_desc *)((void *)vmx->nested.pi_desc + | |
10500 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
10501 | (PAGE_SIZE - 1))); | |
6beb7bd5 JM |
10502 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
10503 | page_to_phys(vmx->nested.pi_desc_page) + | |
10504 | (unsigned long)(vmcs12->posted_intr_desc_addr & | |
10505 | (PAGE_SIZE - 1))); | |
705699a1 | 10506 | } |
6beb7bd5 JM |
10507 | if (cpu_has_vmx_msr_bitmap() && |
10508 | nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) && | |
10509 | nested_vmx_merge_msr_bitmap(vcpu, vmcs12)) | |
cf40088f KA |
10510 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
10511 | CPU_BASED_USE_MSR_BITMAPS); | |
6beb7bd5 JM |
10512 | else |
10513 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, | |
10514 | CPU_BASED_USE_MSR_BITMAPS); | |
a2bcba50 WL |
10515 | } |
10516 | ||
f4124500 JK |
10517 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
10518 | { | |
10519 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; | |
10520 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
10521 | ||
10522 | if (vcpu->arch.virtual_tsc_khz == 0) | |
10523 | return; | |
10524 | ||
10525 | /* Make sure short timeouts reliably trigger an immediate vmexit. | |
10526 | * hrtimer_start does not guarantee this. */ | |
10527 | if (preemption_timeout <= 1) { | |
10528 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); | |
10529 | return; | |
10530 | } | |
10531 | ||
10532 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
10533 | preemption_timeout *= 1000000; | |
10534 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); | |
10535 | hrtimer_start(&vmx->nested.preemption_timer, | |
10536 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); | |
10537 | } | |
10538 | ||
56a20510 JM |
10539 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
10540 | struct vmcs12 *vmcs12) | |
10541 | { | |
10542 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) | |
10543 | return 0; | |
10544 | ||
10545 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || | |
10546 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) | |
10547 | return -EINVAL; | |
10548 | ||
10549 | return 0; | |
10550 | } | |
10551 | ||
3af18d9c WV |
10552 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
10553 | struct vmcs12 *vmcs12) | |
10554 | { | |
3af18d9c WV |
10555 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
10556 | return 0; | |
10557 | ||
5fa99cbe | 10558 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
3af18d9c WV |
10559 | return -EINVAL; |
10560 | ||
10561 | return 0; | |
10562 | } | |
10563 | ||
712b12d7 JM |
10564 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
10565 | struct vmcs12 *vmcs12) | |
10566 | { | |
10567 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
10568 | return 0; | |
10569 | ||
10570 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) | |
10571 | return -EINVAL; | |
10572 | ||
10573 | return 0; | |
10574 | } | |
10575 | ||
3af18d9c WV |
10576 | /* |
10577 | * Merge L0's and L1's MSR bitmap, return false to indicate that | |
10578 | * we do not use the hardware. | |
10579 | */ | |
10580 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, | |
10581 | struct vmcs12 *vmcs12) | |
10582 | { | |
82f0dd4b | 10583 | int msr; |
f2b93280 | 10584 | struct page *page; |
d048c098 | 10585 | unsigned long *msr_bitmap_l1; |
4b0be90f | 10586 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
33241bfe | 10587 | /* |
74469996 | 10588 | * pred_cmd & spec_ctrl are trying to verify two things: |
33241bfe AR |
10589 | * |
10590 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This | |
10591 | * ensures that we do not accidentally generate an L02 MSR bitmap | |
10592 | * from the L12 MSR bitmap that is too permissive. | |
10593 | * 2. That L1 or L2s have actually used the MSR. This avoids | |
10594 | * unnecessarily merging of the bitmap if the MSR is unused. This | |
10595 | * works properly because we only update the L01 MSR bitmap lazily. | |
10596 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only | |
10597 | * updated to reflect this when L1 (or its L2s) actually write to | |
10598 | * the MSR. | |
10599 | */ | |
cb9138cc KA |
10600 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
10601 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); | |
f2b93280 | 10602 | |
33241bfe | 10603 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
74469996 | 10604 | !pred_cmd && !spec_ctrl) |
f2b93280 WV |
10605 | return false; |
10606 | ||
5e2f30b7 DH |
10607 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
10608 | if (is_error_page(page)) | |
f2b93280 | 10609 | return false; |
d048c098 | 10610 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
f2b93280 | 10611 | |
d048c098 RK |
10612 | memset(msr_bitmap_l0, 0xff, PAGE_SIZE); |
10613 | ||
f2b93280 | 10614 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
82f0dd4b WV |
10615 | if (nested_cpu_has_apic_reg_virt(vmcs12)) |
10616 | for (msr = 0x800; msr <= 0x8ff; msr++) | |
10617 | nested_vmx_disable_intercept_for_msr( | |
d048c098 | 10618 | msr_bitmap_l1, msr_bitmap_l0, |
82f0dd4b | 10619 | msr, MSR_TYPE_R); |
d048c098 RK |
10620 | |
10621 | nested_vmx_disable_intercept_for_msr( | |
10622 | msr_bitmap_l1, msr_bitmap_l0, | |
f2b93280 WV |
10623 | APIC_BASE_MSR + (APIC_TASKPRI >> 4), |
10624 | MSR_TYPE_R | MSR_TYPE_W); | |
d048c098 | 10625 | |
608406e2 | 10626 | if (nested_cpu_has_vid(vmcs12)) { |
608406e2 | 10627 | nested_vmx_disable_intercept_for_msr( |
d048c098 | 10628 | msr_bitmap_l1, msr_bitmap_l0, |
608406e2 WV |
10629 | APIC_BASE_MSR + (APIC_EOI >> 4), |
10630 | MSR_TYPE_W); | |
10631 | nested_vmx_disable_intercept_for_msr( | |
d048c098 | 10632 | msr_bitmap_l1, msr_bitmap_l0, |
608406e2 WV |
10633 | APIC_BASE_MSR + (APIC_SELF_IPI >> 4), |
10634 | MSR_TYPE_W); | |
10635 | } | |
82f0dd4b | 10636 | } |
33241bfe | 10637 | |
74469996 KA |
10638 | if (spec_ctrl) |
10639 | nested_vmx_disable_intercept_for_msr( | |
10640 | msr_bitmap_l1, msr_bitmap_l0, | |
10641 | MSR_IA32_SPEC_CTRL, | |
10642 | MSR_TYPE_R | MSR_TYPE_W); | |
10643 | ||
33241bfe AR |
10644 | if (pred_cmd) |
10645 | nested_vmx_disable_intercept_for_msr( | |
10646 | msr_bitmap_l1, msr_bitmap_l0, | |
10647 | MSR_IA32_PRED_CMD, | |
10648 | MSR_TYPE_W); | |
10649 | ||
f2b93280 | 10650 | kunmap(page); |
53a70daf | 10651 | kvm_release_page_clean(page); |
f2b93280 WV |
10652 | |
10653 | return true; | |
10654 | } | |
10655 | ||
76a19ac5 KS |
10656 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
10657 | struct vmcs12 *vmcs12) | |
10658 | { | |
10659 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && | |
10660 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) | |
10661 | return -EINVAL; | |
10662 | else | |
10663 | return 0; | |
10664 | } | |
10665 | ||
f2b93280 WV |
10666 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
10667 | struct vmcs12 *vmcs12) | |
10668 | { | |
82f0dd4b | 10669 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
608406e2 | 10670 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
705699a1 WV |
10671 | !nested_cpu_has_vid(vmcs12) && |
10672 | !nested_cpu_has_posted_intr(vmcs12)) | |
f2b93280 WV |
10673 | return 0; |
10674 | ||
10675 | /* | |
10676 | * If virtualize x2apic mode is enabled, | |
10677 | * virtualize apic access must be disabled. | |
10678 | */ | |
82f0dd4b WV |
10679 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
10680 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) | |
f2b93280 WV |
10681 | return -EINVAL; |
10682 | ||
608406e2 WV |
10683 | /* |
10684 | * If virtual interrupt delivery is enabled, | |
10685 | * we must exit on external interrupts. | |
10686 | */ | |
10687 | if (nested_cpu_has_vid(vmcs12) && | |
10688 | !nested_exit_on_intr(vcpu)) | |
10689 | return -EINVAL; | |
10690 | ||
705699a1 WV |
10691 | /* |
10692 | * bits 15:8 should be zero in posted_intr_nv, | |
10693 | * the descriptor address has been already checked | |
10694 | * in nested_get_vmcs12_pages. | |
10695 | */ | |
10696 | if (nested_cpu_has_posted_intr(vmcs12) && | |
10697 | (!nested_cpu_has_vid(vmcs12) || | |
10698 | !nested_exit_intr_ack_set(vcpu) || | |
10699 | vmcs12->posted_intr_nv & 0xff00)) | |
10700 | return -EINVAL; | |
10701 | ||
f2b93280 WV |
10702 | /* tpr shadow is needed by all apicv features. */ |
10703 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) | |
10704 | return -EINVAL; | |
10705 | ||
10706 | return 0; | |
3af18d9c WV |
10707 | } |
10708 | ||
e9ac033e EK |
10709 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
10710 | unsigned long count_field, | |
92d71bc6 | 10711 | unsigned long addr_field) |
ff651cb6 | 10712 | { |
92d71bc6 | 10713 | int maxphyaddr; |
e9ac033e EK |
10714 | u64 count, addr; |
10715 | ||
10716 | if (vmcs12_read_any(vcpu, count_field, &count) || | |
10717 | vmcs12_read_any(vcpu, addr_field, &addr)) { | |
10718 | WARN_ON(1); | |
10719 | return -EINVAL; | |
10720 | } | |
10721 | if (count == 0) | |
10722 | return 0; | |
92d71bc6 | 10723 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
e9ac033e EK |
10724 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
10725 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { | |
bbe41b95 | 10726 | pr_debug_ratelimited( |
e9ac033e EK |
10727 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
10728 | addr_field, maxphyaddr, count, addr); | |
10729 | return -EINVAL; | |
10730 | } | |
10731 | return 0; | |
10732 | } | |
10733 | ||
10734 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, | |
10735 | struct vmcs12 *vmcs12) | |
10736 | { | |
e9ac033e EK |
10737 | if (vmcs12->vm_exit_msr_load_count == 0 && |
10738 | vmcs12->vm_exit_msr_store_count == 0 && | |
10739 | vmcs12->vm_entry_msr_load_count == 0) | |
10740 | return 0; /* Fast path */ | |
e9ac033e | 10741 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
92d71bc6 | 10742 | VM_EXIT_MSR_LOAD_ADDR) || |
e9ac033e | 10743 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
92d71bc6 | 10744 | VM_EXIT_MSR_STORE_ADDR) || |
e9ac033e | 10745 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
92d71bc6 | 10746 | VM_ENTRY_MSR_LOAD_ADDR)) |
e9ac033e EK |
10747 | return -EINVAL; |
10748 | return 0; | |
10749 | } | |
10750 | ||
c5f983f6 BD |
10751 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
10752 | struct vmcs12 *vmcs12) | |
10753 | { | |
10754 | u64 address = vmcs12->pml_address; | |
10755 | int maxphyaddr = cpuid_maxphyaddr(vcpu); | |
10756 | ||
10757 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { | |
10758 | if (!nested_cpu_has_ept(vmcs12) || | |
10759 | !IS_ALIGNED(address, 4096) || | |
10760 | address >> maxphyaddr) | |
10761 | return -EINVAL; | |
10762 | } | |
10763 | ||
10764 | return 0; | |
10765 | } | |
10766 | ||
e9ac033e EK |
10767 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
10768 | struct vmx_msr_entry *e) | |
10769 | { | |
10770 | /* x2APIC MSR accesses are not allowed */ | |
8a9781f7 | 10771 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
e9ac033e EK |
10772 | return -EINVAL; |
10773 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ | |
10774 | e->index == MSR_IA32_UCODE_REV) | |
10775 | return -EINVAL; | |
10776 | if (e->reserved != 0) | |
ff651cb6 WV |
10777 | return -EINVAL; |
10778 | return 0; | |
10779 | } | |
10780 | ||
e9ac033e EK |
10781 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
10782 | struct vmx_msr_entry *e) | |
ff651cb6 WV |
10783 | { |
10784 | if (e->index == MSR_FS_BASE || | |
10785 | e->index == MSR_GS_BASE || | |
e9ac033e EK |
10786 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
10787 | nested_vmx_msr_check_common(vcpu, e)) | |
10788 | return -EINVAL; | |
10789 | return 0; | |
10790 | } | |
10791 | ||
10792 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, | |
10793 | struct vmx_msr_entry *e) | |
10794 | { | |
10795 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ | |
10796 | nested_vmx_msr_check_common(vcpu, e)) | |
ff651cb6 WV |
10797 | return -EINVAL; |
10798 | return 0; | |
10799 | } | |
10800 | ||
10801 | /* | |
10802 | * Load guest's/host's msr at nested entry/exit. | |
10803 | * return 0 for success, entry index for failure. | |
10804 | */ | |
10805 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
10806 | { | |
10807 | u32 i; | |
10808 | struct vmx_msr_entry e; | |
10809 | struct msr_data msr; | |
10810 | ||
10811 | msr.host_initiated = false; | |
10812 | for (i = 0; i < count; i++) { | |
54bf36aa PB |
10813 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
10814 | &e, sizeof(e))) { | |
bbe41b95 | 10815 | pr_debug_ratelimited( |
e9ac033e EK |
10816 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
10817 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 10818 | goto fail; |
e9ac033e EK |
10819 | } |
10820 | if (nested_vmx_load_msr_check(vcpu, &e)) { | |
bbe41b95 | 10821 | pr_debug_ratelimited( |
e9ac033e EK |
10822 | "%s check failed (%u, 0x%x, 0x%x)\n", |
10823 | __func__, i, e.index, e.reserved); | |
10824 | goto fail; | |
10825 | } | |
ff651cb6 WV |
10826 | msr.index = e.index; |
10827 | msr.data = e.value; | |
e9ac033e | 10828 | if (kvm_set_msr(vcpu, &msr)) { |
bbe41b95 | 10829 | pr_debug_ratelimited( |
e9ac033e EK |
10830 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
10831 | __func__, i, e.index, e.value); | |
ff651cb6 | 10832 | goto fail; |
e9ac033e | 10833 | } |
ff651cb6 WV |
10834 | } |
10835 | return 0; | |
10836 | fail: | |
10837 | return i + 1; | |
10838 | } | |
10839 | ||
10840 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) | |
10841 | { | |
10842 | u32 i; | |
10843 | struct vmx_msr_entry e; | |
10844 | ||
10845 | for (i = 0; i < count; i++) { | |
609e36d3 | 10846 | struct msr_data msr_info; |
54bf36aa PB |
10847 | if (kvm_vcpu_read_guest(vcpu, |
10848 | gpa + i * sizeof(e), | |
10849 | &e, 2 * sizeof(u32))) { | |
bbe41b95 | 10850 | pr_debug_ratelimited( |
e9ac033e EK |
10851 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
10852 | __func__, i, gpa + i * sizeof(e)); | |
ff651cb6 | 10853 | return -EINVAL; |
e9ac033e EK |
10854 | } |
10855 | if (nested_vmx_store_msr_check(vcpu, &e)) { | |
bbe41b95 | 10856 | pr_debug_ratelimited( |
e9ac033e EK |
10857 | "%s check failed (%u, 0x%x, 0x%x)\n", |
10858 | __func__, i, e.index, e.reserved); | |
ff651cb6 | 10859 | return -EINVAL; |
e9ac033e | 10860 | } |
609e36d3 PB |
10861 | msr_info.host_initiated = false; |
10862 | msr_info.index = e.index; | |
10863 | if (kvm_get_msr(vcpu, &msr_info)) { | |
bbe41b95 | 10864 | pr_debug_ratelimited( |
e9ac033e EK |
10865 | "%s cannot read MSR (%u, 0x%x)\n", |
10866 | __func__, i, e.index); | |
10867 | return -EINVAL; | |
10868 | } | |
54bf36aa PB |
10869 | if (kvm_vcpu_write_guest(vcpu, |
10870 | gpa + i * sizeof(e) + | |
10871 | offsetof(struct vmx_msr_entry, value), | |
10872 | &msr_info.data, sizeof(msr_info.data))) { | |
bbe41b95 | 10873 | pr_debug_ratelimited( |
e9ac033e | 10874 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
609e36d3 | 10875 | __func__, i, e.index, msr_info.data); |
e9ac033e EK |
10876 | return -EINVAL; |
10877 | } | |
ff651cb6 WV |
10878 | } |
10879 | return 0; | |
10880 | } | |
10881 | ||
1dc35dac LP |
10882 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
10883 | { | |
10884 | unsigned long invalid_mask; | |
10885 | ||
10886 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); | |
10887 | return (val & invalid_mask) == 0; | |
10888 | } | |
10889 | ||
9ed38ffa LP |
10890 | /* |
10891 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are | |
10892 | * emulating VM entry into a guest with EPT enabled. | |
10893 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code | |
10894 | * is assigned to entry_failure_code on failure. | |
10895 | */ | |
10896 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept, | |
ca0bde28 | 10897 | u32 *entry_failure_code) |
9ed38ffa | 10898 | { |
9ed38ffa | 10899 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
1dc35dac | 10900 | if (!nested_cr3_valid(vcpu, cr3)) { |
9ed38ffa LP |
10901 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
10902 | return 1; | |
10903 | } | |
10904 | ||
10905 | /* | |
10906 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and | |
10907 | * must not be dereferenced. | |
10908 | */ | |
f68adeae | 10909 | if (is_pae_paging(vcpu) && !nested_ept) { |
9ed38ffa LP |
10910 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
10911 | *entry_failure_code = ENTRY_FAIL_PDPTE; | |
10912 | return 1; | |
10913 | } | |
10914 | } | |
10915 | ||
10916 | vcpu->arch.cr3 = cr3; | |
10917 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
10918 | } | |
10919 | ||
10920 | kvm_mmu_reset_context(vcpu); | |
10921 | return 0; | |
10922 | } | |
10923 | ||
fe3ef05c NHE |
10924 | /* |
10925 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested | |
10926 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it | |
b4619660 | 10927 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
fe3ef05c NHE |
10928 | * guest in a way that will both be appropriate to L1's requests, and our |
10929 | * needs. In addition to modifying the active vmcs (which is vmcs02), this | |
10930 | * function also has additional necessary side-effects, like setting various | |
10931 | * vcpu->arch fields. | |
ee146c1c LP |
10932 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
10933 | * is assigned to entry_failure_code on failure. | |
fe3ef05c | 10934 | */ |
ee146c1c | 10935 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
ca0bde28 | 10936 | bool from_vmentry, u32 *entry_failure_code) |
fe3ef05c NHE |
10937 | { |
10938 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
03efce6f | 10939 | u32 exec_control, vmcs12_exec_ctrl; |
fe3ef05c NHE |
10940 | |
10941 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); | |
10942 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); | |
10943 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); | |
10944 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); | |
10945 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); | |
10946 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); | |
10947 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); | |
10948 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); | |
10949 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); | |
10950 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); | |
10951 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); | |
10952 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); | |
10953 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); | |
10954 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); | |
10955 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); | |
10956 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); | |
10957 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); | |
10958 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); | |
10959 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); | |
10960 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); | |
10961 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); | |
10962 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); | |
10963 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); | |
10964 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); | |
10965 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); | |
10966 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); | |
10967 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); | |
10968 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); | |
10969 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); | |
10970 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); | |
10971 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); | |
10972 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); | |
10973 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); | |
10974 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); | |
10975 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); | |
10976 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); | |
10977 | ||
cf8b84f4 JM |
10978 | if (from_vmentry && |
10979 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { | |
2996fca0 JK |
10980 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
10981 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); | |
10982 | } else { | |
10983 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); | |
10984 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); | |
10985 | } | |
cf8b84f4 JM |
10986 | if (from_vmentry) { |
10987 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, | |
10988 | vmcs12->vm_entry_intr_info_field); | |
10989 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, | |
10990 | vmcs12->vm_entry_exception_error_code); | |
10991 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, | |
10992 | vmcs12->vm_entry_instruction_len); | |
10993 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, | |
10994 | vmcs12->guest_interruptibility_info); | |
2d6144e3 WL |
10995 | vmx->loaded_vmcs->nmi_known_unmasked = |
10996 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); | |
cf8b84f4 JM |
10997 | } else { |
10998 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); | |
10999 | } | |
fe3ef05c | 11000 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
63fbf59f | 11001 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
fe3ef05c NHE |
11002 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
11003 | vmcs12->guest_pending_dbg_exceptions); | |
11004 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); | |
11005 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); | |
11006 | ||
81dc01f7 WL |
11007 | if (nested_cpu_has_xsaves(vmcs12)) |
11008 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); | |
fe3ef05c NHE |
11009 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
11010 | ||
f4124500 | 11011 | exec_control = vmcs12->pin_based_vm_exec_control; |
9314006d PB |
11012 | |
11013 | /* Preemption timer setting is only taken from vmcs01. */ | |
705699a1 | 11014 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
9314006d PB |
11015 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
11016 | if (vmx->hv_deadline_tsc == -1) | |
11017 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; | |
705699a1 | 11018 | |
9314006d | 11019 | /* Posted interrupts setting is only taken from vmcs12. */ |
705699a1 | 11020 | if (nested_cpu_has_posted_intr(vmcs12)) { |
705699a1 WV |
11021 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
11022 | vmx->nested.pi_pending = false; | |
06a5524f | 11023 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
6beb7bd5 | 11024 | } else { |
705699a1 | 11025 | exec_control &= ~PIN_BASED_POSTED_INTR; |
6beb7bd5 | 11026 | } |
705699a1 | 11027 | |
f4124500 | 11028 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
fe3ef05c | 11029 | |
f4124500 JK |
11030 | vmx->nested.preemption_timer_expired = false; |
11031 | if (nested_cpu_has_preemption_timer(vmcs12)) | |
11032 | vmx_start_preemption_timer(vcpu); | |
0238ea91 | 11033 | |
fe3ef05c NHE |
11034 | /* |
11035 | * Whether page-faults are trapped is determined by a combination of | |
11036 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. | |
11037 | * If enable_ept, L0 doesn't care about page faults and we should | |
11038 | * set all of these to L1's desires. However, if !enable_ept, L0 does | |
11039 | * care about (at least some) page faults, and because it is not easy | |
11040 | * (if at all possible?) to merge L0 and L1's desires, we simply ask | |
11041 | * to exit on each and every L2 page fault. This is done by setting | |
11042 | * MASK=MATCH=0 and (see below) EB.PF=1. | |
11043 | * Note that below we don't need special code to set EB.PF beyond the | |
11044 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, | |
11045 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when | |
11046 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. | |
fe3ef05c NHE |
11047 | */ |
11048 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, | |
11049 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); | |
11050 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, | |
11051 | enable_ept ? vmcs12->page_fault_error_code_match : 0); | |
11052 | ||
11053 | if (cpu_has_secondary_exec_ctrls()) { | |
80154d77 | 11054 | exec_control = vmx->secondary_exec_control; |
e2821620 | 11055 | |
fe3ef05c | 11056 | /* Take the following fields only from vmcs12 */ |
696dfd95 | 11057 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
90a2db6d | 11058 | SECONDARY_EXEC_ENABLE_INVPCID | |
b3a2a907 | 11059 | SECONDARY_EXEC_RDTSCP | |
3db13480 | 11060 | SECONDARY_EXEC_XSAVES | |
696dfd95 | 11061 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
27c42a1b BD |
11062 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
11063 | SECONDARY_EXEC_ENABLE_VMFUNC); | |
fe3ef05c | 11064 | if (nested_cpu_has(vmcs12, |
03efce6f BD |
11065 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
11066 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & | |
11067 | ~SECONDARY_EXEC_ENABLE_PML; | |
11068 | exec_control |= vmcs12_exec_ctrl; | |
11069 | } | |
fe3ef05c | 11070 | |
27c42a1b BD |
11071 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
11072 | if (exec_control & SECONDARY_EXEC_ENABLE_VMFUNC) | |
11073 | vmcs_write64(VM_FUNCTION_CONTROL, 0); | |
11074 | ||
608406e2 WV |
11075 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { |
11076 | vmcs_write64(EOI_EXIT_BITMAP0, | |
11077 | vmcs12->eoi_exit_bitmap0); | |
11078 | vmcs_write64(EOI_EXIT_BITMAP1, | |
11079 | vmcs12->eoi_exit_bitmap1); | |
11080 | vmcs_write64(EOI_EXIT_BITMAP2, | |
11081 | vmcs12->eoi_exit_bitmap2); | |
11082 | vmcs_write64(EOI_EXIT_BITMAP3, | |
11083 | vmcs12->eoi_exit_bitmap3); | |
11084 | vmcs_write16(GUEST_INTR_STATUS, | |
11085 | vmcs12->guest_intr_status); | |
11086 | } | |
11087 | ||
6beb7bd5 JM |
11088 | /* |
11089 | * Write an illegal value to APIC_ACCESS_ADDR. Later, | |
11090 | * nested_get_vmcs12_pages will either fix it up or | |
11091 | * remove the VM execution control. | |
11092 | */ | |
11093 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) | |
11094 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); | |
11095 | ||
fe3ef05c NHE |
11096 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
11097 | } | |
11098 | ||
11099 | ||
11100 | /* | |
11101 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) | |
11102 | * Some constant fields are set here by vmx_set_constant_host_state(). | |
11103 | * Other fields are different per CPU, and will be set later when | |
11104 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. | |
11105 | */ | |
a547c6db | 11106 | vmx_set_constant_host_state(vmx); |
fe3ef05c | 11107 | |
83bafef1 JM |
11108 | /* |
11109 | * Set the MSR load/store lists to match L0's settings. | |
11110 | */ | |
11111 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); | |
6e3dedb6 KRW |
11112 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
11113 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); | |
11114 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); | |
11115 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); | |
83bafef1 | 11116 | |
fe3ef05c NHE |
11117 | /* |
11118 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before | |
11119 | * entry, but only if the current (host) sp changed from the value | |
11120 | * we wrote last (vmx->host_rsp). This cache is no longer relevant | |
11121 | * if we switch vmcs, and rather than hold a separate cache per vmcs, | |
11122 | * here we just force the write to happen on entry. | |
11123 | */ | |
11124 | vmx->host_rsp = 0; | |
11125 | ||
11126 | exec_control = vmx_exec_control(vmx); /* L0's desires */ | |
11127 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; | |
11128 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; | |
11129 | exec_control &= ~CPU_BASED_TPR_SHADOW; | |
11130 | exec_control |= vmcs12->cpu_based_vm_exec_control; | |
a7c0b07d | 11131 | |
6beb7bd5 JM |
11132 | /* |
11133 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if | |
11134 | * nested_get_vmcs12_pages can't fix it up, the illegal value | |
11135 | * will result in a VM entry failure. | |
11136 | */ | |
a7c0b07d | 11137 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
6beb7bd5 | 11138 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
a7c0b07d | 11139 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
51aa68e7 JM |
11140 | } else { |
11141 | #ifdef CONFIG_X86_64 | |
11142 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | | |
11143 | CPU_BASED_CR8_STORE_EXITING; | |
11144 | #endif | |
a7c0b07d WL |
11145 | } |
11146 | ||
fe3ef05c | 11147 | /* |
3af18d9c | 11148 | * Merging of IO bitmap not currently supported. |
fe3ef05c NHE |
11149 | * Rather, exit every time. |
11150 | */ | |
fe3ef05c NHE |
11151 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
11152 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; | |
11153 | ||
11154 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); | |
11155 | ||
11156 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the | |
11157 | * bitwise-or of what L1 wants to trap for L2, and what we want to | |
11158 | * trap. Note that CR0.TS also needs updating - we do this later. | |
11159 | */ | |
11160 | update_exception_bitmap(vcpu); | |
11161 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; | |
11162 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); | |
11163 | ||
8049d651 NHE |
11164 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
11165 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER | |
11166 | * bits are further modified by vmx_set_efer() below. | |
11167 | */ | |
f4124500 | 11168 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
8049d651 NHE |
11169 | |
11170 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are | |
11171 | * emulated by vmx_set_efer(), below. | |
11172 | */ | |
2961e876 | 11173 | vm_entry_controls_init(vmx, |
8049d651 NHE |
11174 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
11175 | ~VM_ENTRY_IA32E_MODE) | | |
fe3ef05c NHE |
11176 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
11177 | ||
cf8b84f4 JM |
11178 | if (from_vmentry && |
11179 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { | |
fe3ef05c | 11180 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
44811c02 | 11181 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
cf8b84f4 | 11182 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
fe3ef05c | 11183 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
cf8b84f4 | 11184 | } |
fe3ef05c NHE |
11185 | |
11186 | set_cr4_guest_host_mask(vmx); | |
11187 | ||
cf8b84f4 JM |
11188 | if (from_vmentry && |
11189 | vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) | |
36be0b9d PB |
11190 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
11191 | ||
f7f5542f KA |
11192 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
11193 | ||
c95ba92a PF |
11194 | if (kvm_has_tsc_control) |
11195 | decache_tsc_multiplier(vmx); | |
fe3ef05c | 11196 | |
4b0be90f PB |
11197 | if (cpu_has_vmx_msr_bitmap()) |
11198 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); | |
11199 | ||
fe3ef05c NHE |
11200 | if (enable_vpid) { |
11201 | /* | |
5c614b35 WL |
11202 | * There is no direct mapping between vpid02 and vpid12, the |
11203 | * vpid02 is per-vCPU for L0 and reused while the value of | |
11204 | * vpid12 is changed w/ one invvpid during nested vmentry. | |
11205 | * The vpid12 is allocated by L1 for L2, so it will not | |
11206 | * influence global bitmap(for vpid01 and vpid02 allocation) | |
11207 | * even if spawn a lot of nested vCPUs. | |
fe3ef05c | 11208 | */ |
5c614b35 WL |
11209 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
11210 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); | |
11211 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { | |
11212 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; | |
6bf41e55 | 11213 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true); |
5c614b35 WL |
11214 | } |
11215 | } else { | |
11216 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); | |
6bf41e55 | 11217 | vmx_flush_tlb(vcpu, true); |
5c614b35 WL |
11218 | } |
11219 | ||
fe3ef05c NHE |
11220 | } |
11221 | ||
1fb883bb LP |
11222 | if (enable_pml) { |
11223 | /* | |
11224 | * Conceptually we want to copy the PML address and index from | |
11225 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, | |
11226 | * since we always flush the log on each vmexit, this happens | |
11227 | * to be equivalent to simply resetting the fields in vmcs02. | |
11228 | */ | |
11229 | ASSERT(vmx->pml_pg); | |
11230 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); | |
11231 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); | |
11232 | } | |
11233 | ||
155a97a3 | 11234 | if (nested_cpu_has_ept(vmcs12)) { |
ae1e2d10 PB |
11235 | if (nested_ept_init_mmu_context(vcpu)) { |
11236 | *entry_failure_code = ENTRY_FAIL_DEFAULT; | |
11237 | return 1; | |
11238 | } | |
fb6c8198 JM |
11239 | } else if (nested_cpu_has2(vmcs12, |
11240 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
6bf41e55 | 11241 | vmx_flush_tlb(vcpu, true); |
155a97a3 NHE |
11242 | } |
11243 | ||
fe3ef05c | 11244 | /* |
bd7e5b08 PB |
11245 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
11246 | * bits which we consider mandatory enabled. | |
fe3ef05c NHE |
11247 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
11248 | * the specifications by L1; It's not enough to take | |
11249 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we | |
11250 | * have more bits than L1 expected. | |
11251 | */ | |
11252 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); | |
11253 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); | |
11254 | ||
11255 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); | |
11256 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); | |
11257 | ||
cf8b84f4 JM |
11258 | if (from_vmentry && |
11259 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) | |
5a6a9748 DM |
11260 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
11261 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) | |
11262 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); | |
11263 | else | |
11264 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
11265 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ | |
11266 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
11267 | ||
9ed38ffa | 11268 | /* Shadow page tables on either EPT or shadow page tables. */ |
7ad658b6 | 11269 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
9ed38ffa LP |
11270 | entry_failure_code)) |
11271 | return 1; | |
7ca29de2 | 11272 | |
feaf0c7d GN |
11273 | if (!enable_ept) |
11274 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; | |
11275 | ||
3633cfc3 NHE |
11276 | /* |
11277 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 | |
11278 | */ | |
11279 | if (enable_ept) { | |
11280 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); | |
11281 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); | |
11282 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); | |
11283 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); | |
11284 | } | |
11285 | ||
fe3ef05c NHE |
11286 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
11287 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); | |
ee146c1c | 11288 | return 0; |
fe3ef05c NHE |
11289 | } |
11290 | ||
ca0bde28 | 11291 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
cd232ad0 | 11292 | { |
cd232ad0 | 11293 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
7c177938 | 11294 | |
6dfacadd | 11295 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
ca0bde28 JM |
11296 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
11297 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
26539bd0 | 11298 | |
56a20510 JM |
11299 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
11300 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11301 | ||
ca0bde28 JM |
11302 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
11303 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
7c177938 | 11304 | |
76a19ac5 KS |
11305 | if (nested_vmx_check_apic_access_controls(vcpu, vmcs12)) |
11306 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11307 | ||
712b12d7 JM |
11308 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
11309 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11310 | ||
ca0bde28 JM |
11311 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
11312 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
f2b93280 | 11313 | |
ca0bde28 JM |
11314 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
11315 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
e9ac033e | 11316 | |
c5f983f6 BD |
11317 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
11318 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11319 | ||
7c177938 | 11320 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
0115f9cb | 11321 | vmx->nested.nested_vmx_procbased_ctls_low, |
b9c237bb | 11322 | vmx->nested.nested_vmx_procbased_ctls_high) || |
2e5b0bd9 JM |
11323 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
11324 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, | |
11325 | vmx->nested.nested_vmx_secondary_ctls_low, | |
11326 | vmx->nested.nested_vmx_secondary_ctls_high)) || | |
7c177938 | 11327 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
b9c237bb WV |
11328 | vmx->nested.nested_vmx_pinbased_ctls_low, |
11329 | vmx->nested.nested_vmx_pinbased_ctls_high) || | |
7c177938 | 11330 | !vmx_control_verify(vmcs12->vm_exit_controls, |
0115f9cb | 11331 | vmx->nested.nested_vmx_exit_ctls_low, |
b9c237bb | 11332 | vmx->nested.nested_vmx_exit_ctls_high) || |
7c177938 | 11333 | !vmx_control_verify(vmcs12->vm_entry_controls, |
0115f9cb | 11334 | vmx->nested.nested_vmx_entry_ctls_low, |
b9c237bb | 11335 | vmx->nested.nested_vmx_entry_ctls_high)) |
ca0bde28 | 11336 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
7c177938 | 11337 | |
41ab9372 BD |
11338 | if (nested_cpu_has_vmfunc(vmcs12)) { |
11339 | if (vmcs12->vm_function_control & | |
11340 | ~vmx->nested.nested_vmx_vmfunc_controls) | |
11341 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11342 | ||
11343 | if (nested_cpu_has_eptp_switching(vmcs12)) { | |
11344 | if (!nested_cpu_has_ept(vmcs12) || | |
11345 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) | |
11346 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11347 | } | |
11348 | } | |
27c42a1b | 11349 | |
c7c2c709 JM |
11350 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
11351 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; | |
11352 | ||
3899152c | 11353 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
1dc35dac | 11354 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
ca0bde28 JM |
11355 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
11356 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; | |
11357 | ||
11358 | return 0; | |
11359 | } | |
11360 | ||
11361 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
11362 | u32 *exit_qual) | |
11363 | { | |
11364 | bool ia32e; | |
11365 | ||
11366 | *exit_qual = ENTRY_FAIL_DEFAULT; | |
7c177938 | 11367 | |
3899152c | 11368 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
ca0bde28 | 11369 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
b428018a | 11370 | return 1; |
ca0bde28 JM |
11371 | |
11372 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) && | |
11373 | vmcs12->vmcs_link_pointer != -1ull) { | |
11374 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; | |
b428018a | 11375 | return 1; |
7c177938 NHE |
11376 | } |
11377 | ||
384bb783 | 11378 | /* |
cb0c8cda | 11379 | * If the load IA32_EFER VM-entry control is 1, the following checks |
384bb783 JK |
11380 | * are performed on the field for the IA32_EFER MSR: |
11381 | * - Bits reserved in the IA32_EFER MSR must be 0. | |
11382 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of | |
11383 | * the IA-32e mode guest VM-exit control. It must also be identical | |
11384 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to | |
11385 | * CR0.PG) is 1. | |
11386 | */ | |
ca0bde28 JM |
11387 | if (to_vmx(vcpu)->nested.nested_run_pending && |
11388 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { | |
384bb783 JK |
11389 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
11390 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || | |
11391 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || | |
11392 | ((vmcs12->guest_cr0 & X86_CR0_PG) && | |
ca0bde28 | 11393 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
b428018a | 11394 | return 1; |
384bb783 JK |
11395 | } |
11396 | ||
11397 | /* | |
11398 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the | |
11399 | * IA32_EFER MSR must be 0 in the field for that register. In addition, | |
11400 | * the values of the LMA and LME bits in the field must each be that of | |
11401 | * the host address-space size VM-exit control. | |
11402 | */ | |
11403 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { | |
11404 | ia32e = (vmcs12->vm_exit_controls & | |
11405 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; | |
11406 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || | |
11407 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || | |
ca0bde28 | 11408 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
b428018a | 11409 | return 1; |
ca0bde28 JM |
11410 | } |
11411 | ||
f1b026a3 WL |
11412 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
11413 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || | |
11414 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) | |
11415 | return 1; | |
11416 | ||
ca0bde28 JM |
11417 | return 0; |
11418 | } | |
11419 | ||
858e25c0 JM |
11420 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry) |
11421 | { | |
11422 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
11423 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
858e25c0 | 11424 | u32 exit_qual; |
f7f5542f | 11425 | int r; |
858e25c0 | 11426 | |
858e25c0 JM |
11427 | enter_guest_mode(vcpu); |
11428 | ||
11429 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) | |
11430 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
11431 | ||
8819227c | 11432 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
9ee802b1 | 11433 | vmx_segment_cache_clear(vmx); |
858e25c0 | 11434 | |
f7f5542f KA |
11435 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
11436 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; | |
11437 | ||
11438 | r = EXIT_REASON_INVALID_STATE; | |
11439 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) | |
11440 | goto fail; | |
858e25c0 JM |
11441 | |
11442 | nested_get_vmcs12_pages(vcpu, vmcs12); | |
11443 | ||
f7f5542f | 11444 | r = EXIT_REASON_MSR_LOAD_FAIL; |
4fddb9c5 JM |
11445 | exit_qual = nested_vmx_load_msr(vcpu, |
11446 | vmcs12->vm_entry_msr_load_addr, | |
11447 | vmcs12->vm_entry_msr_load_count); | |
11448 | if (exit_qual) | |
f7f5542f | 11449 | goto fail; |
858e25c0 | 11450 | |
858e25c0 JM |
11451 | /* |
11452 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point | |
11453 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet | |
11454 | * returned as far as L1 is concerned. It will only return (and set | |
11455 | * the success flag) when L2 exits (see nested_vmx_vmexit()). | |
11456 | */ | |
11457 | return 0; | |
f7f5542f KA |
11458 | |
11459 | fail: | |
11460 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) | |
11461 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; | |
11462 | leave_guest_mode(vcpu); | |
11463 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); | |
11464 | nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual); | |
11465 | return 1; | |
858e25c0 JM |
11466 | } |
11467 | ||
ca0bde28 JM |
11468 | /* |
11469 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 | |
11470 | * for running an L2 nested guest. | |
11471 | */ | |
11472 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) | |
11473 | { | |
11474 | struct vmcs12 *vmcs12; | |
11475 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
b3f1dfb6 | 11476 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
ca0bde28 JM |
11477 | u32 exit_qual; |
11478 | int ret; | |
11479 | ||
11480 | if (!nested_vmx_check_permission(vcpu)) | |
11481 | return 1; | |
11482 | ||
11483 | if (!nested_vmx_check_vmcs12(vcpu)) | |
11484 | goto out; | |
11485 | ||
11486 | vmcs12 = get_vmcs12(vcpu); | |
11487 | ||
11488 | if (enable_shadow_vmcs) | |
11489 | copy_shadow_to_vmcs12(vmx); | |
11490 | ||
11491 | /* | |
11492 | * The nested entry process starts with enforcing various prerequisites | |
11493 | * on vmcs12 as required by the Intel SDM, and act appropriately when | |
11494 | * they fail: As the SDM explains, some conditions should cause the | |
11495 | * instruction to fail, while others will cause the instruction to seem | |
11496 | * to succeed, but return an EXIT_REASON_INVALID_STATE. | |
11497 | * To speed up the normal (success) code path, we should avoid checking | |
11498 | * for misconfigurations which will anyway be caught by the processor | |
11499 | * when using the merged vmcs02. | |
11500 | */ | |
b3f1dfb6 JM |
11501 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
11502 | nested_vmx_failValid(vcpu, | |
11503 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); | |
11504 | goto out; | |
11505 | } | |
11506 | ||
ca0bde28 JM |
11507 | if (vmcs12->launch_state == launch) { |
11508 | nested_vmx_failValid(vcpu, | |
11509 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS | |
11510 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); | |
11511 | goto out; | |
11512 | } | |
11513 | ||
11514 | ret = check_vmentry_prereqs(vcpu, vmcs12); | |
11515 | if (ret) { | |
11516 | nested_vmx_failValid(vcpu, ret); | |
11517 | goto out; | |
11518 | } | |
11519 | ||
11520 | /* | |
11521 | * After this point, the trap flag no longer triggers a singlestep trap | |
11522 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. | |
11523 | * This is not 100% correct; for performance reasons, we delegate most | |
11524 | * of the checks on host state to the processor. If those fail, | |
11525 | * the singlestep trap is missed. | |
11526 | */ | |
11527 | skip_emulated_instruction(vcpu); | |
11528 | ||
11529 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); | |
11530 | if (ret) { | |
11531 | nested_vmx_entry_failure(vcpu, vmcs12, | |
11532 | EXIT_REASON_INVALID_STATE, exit_qual); | |
11533 | return 1; | |
384bb783 JK |
11534 | } |
11535 | ||
7c177938 NHE |
11536 | /* |
11537 | * We're finally done with prerequisite checking, and can start with | |
11538 | * the nested entry. | |
11539 | */ | |
11540 | ||
858e25c0 JM |
11541 | ret = enter_vmx_non_root_mode(vcpu, true); |
11542 | if (ret) | |
11543 | return ret; | |
ff651cb6 | 11544 | |
f0ace387 PB |
11545 | /* Hide L1D cache contents from the nested guest. */ |
11546 | vmx->vcpu.arch.l1tf_flush_l1d = true; | |
11547 | ||
076381c4 CG |
11548 | /* |
11549 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken | |
11550 | * by event injection, halt vcpu. | |
11551 | */ | |
11552 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && | |
11553 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) | |
5cb56059 | 11554 | return kvm_vcpu_halt(vcpu); |
6dfacadd | 11555 | |
7af40ad3 JK |
11556 | vmx->nested.nested_run_pending = 1; |
11557 | ||
cd232ad0 | 11558 | return 1; |
eb277562 KH |
11559 | |
11560 | out: | |
6affcbed | 11561 | return kvm_skip_emulated_instruction(vcpu); |
cd232ad0 NHE |
11562 | } |
11563 | ||
4704d0be NHE |
11564 | /* |
11565 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date | |
11566 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). | |
11567 | * This function returns the new value we should put in vmcs12.guest_cr0. | |
11568 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, | |
11569 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now | |
11570 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 | |
11571 | * didn't trap the bit, because if L1 did, so would L0). | |
11572 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have | |
11573 | * been modified by L2, and L1 knows it. So just leave the old value of | |
11574 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 | |
11575 | * isn't relevant, because if L0 traps this bit it can set it to anything. | |
11576 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have | |
11577 | * changed these bits, and therefore they need to be updated, but L0 | |
11578 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather | |
11579 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. | |
11580 | */ | |
11581 | static inline unsigned long | |
11582 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
11583 | { | |
11584 | return | |
11585 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | | |
11586 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | | |
11587 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | | |
11588 | vcpu->arch.cr0_guest_owned_bits)); | |
11589 | } | |
11590 | ||
11591 | static inline unsigned long | |
11592 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) | |
11593 | { | |
11594 | return | |
11595 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | | |
11596 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | | |
11597 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | | |
11598 | vcpu->arch.cr4_guest_owned_bits)); | |
11599 | } | |
11600 | ||
5f3d5799 JK |
11601 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
11602 | struct vmcs12 *vmcs12) | |
11603 | { | |
11604 | u32 idt_vectoring; | |
11605 | unsigned int nr; | |
11606 | ||
664f8e26 | 11607 | if (vcpu->arch.exception.injected) { |
5f3d5799 JK |
11608 | nr = vcpu->arch.exception.nr; |
11609 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
11610 | ||
11611 | if (kvm_exception_is_soft(nr)) { | |
11612 | vmcs12->vm_exit_instruction_len = | |
11613 | vcpu->arch.event_exit_inst_len; | |
11614 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; | |
11615 | } else | |
11616 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; | |
11617 | ||
11618 | if (vcpu->arch.exception.has_error_code) { | |
11619 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; | |
11620 | vmcs12->idt_vectoring_error_code = | |
11621 | vcpu->arch.exception.error_code; | |
11622 | } | |
11623 | ||
11624 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
cd2633c5 | 11625 | } else if (vcpu->arch.nmi_injected) { |
5f3d5799 JK |
11626 | vmcs12->idt_vectoring_info_field = |
11627 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; | |
11628 | } else if (vcpu->arch.interrupt.pending) { | |
11629 | nr = vcpu->arch.interrupt.nr; | |
11630 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; | |
11631 | ||
11632 | if (vcpu->arch.interrupt.soft) { | |
11633 | idt_vectoring |= INTR_TYPE_SOFT_INTR; | |
11634 | vmcs12->vm_entry_instruction_len = | |
11635 | vcpu->arch.event_exit_inst_len; | |
11636 | } else | |
11637 | idt_vectoring |= INTR_TYPE_EXT_INTR; | |
11638 | ||
11639 | vmcs12->idt_vectoring_info_field = idt_vectoring; | |
11640 | } | |
11641 | } | |
11642 | ||
b6b8a145 JK |
11643 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
11644 | { | |
11645 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
bfcf83b1 | 11646 | unsigned long exit_qual; |
917dc606 LA |
11647 | bool block_nested_events = |
11648 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); | |
acc9ab60 | 11649 | |
bfcf83b1 WL |
11650 | if (vcpu->arch.exception.pending && |
11651 | nested_vmx_check_exception(vcpu, &exit_qual)) { | |
917dc606 | 11652 | if (block_nested_events) |
bfcf83b1 WL |
11653 | return -EBUSY; |
11654 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); | |
bfcf83b1 WL |
11655 | return 0; |
11656 | } | |
11657 | ||
f4124500 JK |
11658 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
11659 | vmx->nested.preemption_timer_expired) { | |
917dc606 | 11660 | if (block_nested_events) |
f4124500 JK |
11661 | return -EBUSY; |
11662 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); | |
11663 | return 0; | |
11664 | } | |
11665 | ||
b6b8a145 | 11666 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
917dc606 | 11667 | if (block_nested_events) |
b6b8a145 JK |
11668 | return -EBUSY; |
11669 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, | |
11670 | NMI_VECTOR | INTR_TYPE_NMI_INTR | | |
11671 | INTR_INFO_VALID_MASK, 0); | |
11672 | /* | |
11673 | * The NMI-triggered VM exit counts as injection: | |
11674 | * clear this one and block further NMIs. | |
11675 | */ | |
11676 | vcpu->arch.nmi_pending = 0; | |
11677 | vmx_set_nmi_mask(vcpu, true); | |
11678 | return 0; | |
11679 | } | |
11680 | ||
11681 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && | |
11682 | nested_exit_on_intr(vcpu)) { | |
917dc606 | 11683 | if (block_nested_events) |
b6b8a145 JK |
11684 | return -EBUSY; |
11685 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); | |
705699a1 | 11686 | return 0; |
b6b8a145 JK |
11687 | } |
11688 | ||
6342c50a DH |
11689 | vmx_complete_nested_posted_interrupt(vcpu); |
11690 | return 0; | |
b6b8a145 JK |
11691 | } |
11692 | ||
f4124500 JK |
11693 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
11694 | { | |
11695 | ktime_t remaining = | |
11696 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); | |
11697 | u64 value; | |
11698 | ||
11699 | if (ktime_to_ns(remaining) <= 0) | |
11700 | return 0; | |
11701 | ||
11702 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; | |
11703 | do_div(value, 1000000); | |
11704 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; | |
11705 | } | |
11706 | ||
4704d0be | 11707 | /* |
cf8b84f4 JM |
11708 | * Update the guest state fields of vmcs12 to reflect changes that |
11709 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the | |
11710 | * VM-entry controls is also updated, since this is really a guest | |
11711 | * state bit.) | |
4704d0be | 11712 | */ |
cf8b84f4 | 11713 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
4704d0be | 11714 | { |
4704d0be NHE |
11715 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
11716 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); | |
11717 | ||
4704d0be NHE |
11718 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
11719 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); | |
11720 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); | |
11721 | ||
11722 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); | |
11723 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); | |
11724 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); | |
11725 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); | |
11726 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); | |
11727 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); | |
11728 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); | |
11729 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); | |
11730 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); | |
11731 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); | |
11732 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); | |
11733 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); | |
11734 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); | |
11735 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); | |
11736 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); | |
11737 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); | |
11738 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); | |
11739 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); | |
11740 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); | |
11741 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); | |
11742 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); | |
11743 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); | |
11744 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); | |
11745 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); | |
11746 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); | |
11747 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); | |
11748 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); | |
11749 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); | |
11750 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); | |
11751 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); | |
11752 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); | |
11753 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); | |
11754 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); | |
11755 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); | |
11756 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); | |
11757 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); | |
11758 | ||
4704d0be NHE |
11759 | vmcs12->guest_interruptibility_info = |
11760 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
11761 | vmcs12->guest_pending_dbg_exceptions = | |
11762 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); | |
3edf1e69 JK |
11763 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
11764 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; | |
11765 | else | |
11766 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; | |
4704d0be | 11767 | |
f4124500 JK |
11768 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
11769 | if (vmcs12->vm_exit_controls & | |
11770 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) | |
11771 | vmcs12->vmx_preemption_timer_value = | |
11772 | vmx_get_preemption_timer_value(vcpu); | |
11773 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); | |
11774 | } | |
7854cbca | 11775 | |
3633cfc3 NHE |
11776 | /* |
11777 | * In some cases (usually, nested EPT), L2 is allowed to change its | |
11778 | * own CR3 without exiting. If it has changed it, we must keep it. | |
11779 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined | |
11780 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. | |
11781 | * | |
11782 | * Additionally, restore L2's PDPTR to vmcs12. | |
11783 | */ | |
11784 | if (enable_ept) { | |
f3531054 | 11785 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
3633cfc3 NHE |
11786 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
11787 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); | |
11788 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); | |
11789 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); | |
11790 | } | |
11791 | ||
d281e13b | 11792 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
119a9c01 | 11793 | |
608406e2 WV |
11794 | if (nested_cpu_has_vid(vmcs12)) |
11795 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); | |
11796 | ||
c18911a2 JK |
11797 | vmcs12->vm_entry_controls = |
11798 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | | |
2961e876 | 11799 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
c18911a2 | 11800 | |
2996fca0 JK |
11801 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
11802 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); | |
11803 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); | |
11804 | } | |
11805 | ||
4704d0be NHE |
11806 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
11807 | * the relevant bit asks not to trap the change */ | |
b8c07d55 | 11808 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
4704d0be | 11809 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
10ba54a5 JK |
11810 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
11811 | vmcs12->guest_ia32_efer = vcpu->arch.efer; | |
4704d0be NHE |
11812 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
11813 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); | |
11814 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); | |
a87036ad | 11815 | if (kvm_mpx_supported()) |
36be0b9d | 11816 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
cf8b84f4 JM |
11817 | } |
11818 | ||
11819 | /* | |
11820 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits | |
11821 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), | |
11822 | * and this function updates it to reflect the changes to the guest state while | |
11823 | * L2 was running (and perhaps made some exits which were handled directly by L0 | |
11824 | * without going back to L1), and to reflect the exit reason. | |
11825 | * Note that we do not have to copy here all VMCS fields, just those that | |
11826 | * could have changed by the L2 guest or the exit - i.e., the guest-state and | |
11827 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, | |
11828 | * which already writes to vmcs12 directly. | |
11829 | */ | |
11830 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, | |
11831 | u32 exit_reason, u32 exit_intr_info, | |
11832 | unsigned long exit_qualification) | |
11833 | { | |
11834 | /* update guest state fields: */ | |
11835 | sync_vmcs12(vcpu, vmcs12); | |
4704d0be NHE |
11836 | |
11837 | /* update exit information fields: */ | |
11838 | ||
533558bc JK |
11839 | vmcs12->vm_exit_reason = exit_reason; |
11840 | vmcs12->exit_qualification = exit_qualification; | |
533558bc | 11841 | vmcs12->vm_exit_intr_info = exit_intr_info; |
7313c698 | 11842 | |
5f3d5799 | 11843 | vmcs12->idt_vectoring_info_field = 0; |
4704d0be NHE |
11844 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
11845 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); | |
11846 | ||
5f3d5799 | 11847 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
7cdc2d62 JM |
11848 | vmcs12->launch_state = 1; |
11849 | ||
5f3d5799 JK |
11850 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
11851 | * instead of reading the real value. */ | |
4704d0be | 11852 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
5f3d5799 JK |
11853 | |
11854 | /* | |
11855 | * Transfer the event that L0 or L1 may wanted to inject into | |
11856 | * L2 to IDT_VECTORING_INFO_FIELD. | |
11857 | */ | |
11858 | vmcs12_save_pending_event(vcpu, vmcs12); | |
11859 | } | |
11860 | ||
11861 | /* | |
11862 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is | |
11863 | * preserved above and would only end up incorrectly in L1. | |
11864 | */ | |
11865 | vcpu->arch.nmi_injected = false; | |
11866 | kvm_clear_exception_queue(vcpu); | |
11867 | kvm_clear_interrupt_queue(vcpu); | |
4704d0be NHE |
11868 | } |
11869 | ||
11870 | /* | |
11871 | * A part of what we need to when the nested L2 guest exits and we want to | |
11872 | * run its L1 parent, is to reset L1's guest state to the host state specified | |
11873 | * in vmcs12. | |
11874 | * This function is to be called not only on normal nested exit, but also on | |
11875 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry | |
11876 | * Failures During or After Loading Guest State"). | |
11877 | * This function should be called when the active VMCS is L1's (vmcs01). | |
11878 | */ | |
733568f9 JK |
11879 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
11880 | struct vmcs12 *vmcs12) | |
4704d0be | 11881 | { |
21feb4eb | 11882 | struct kvm_segment seg; |
c08567af | 11883 | u32 entry_failure_code; |
21feb4eb | 11884 | |
4704d0be NHE |
11885 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
11886 | vcpu->arch.efer = vmcs12->host_ia32_efer; | |
d1fa0352 | 11887 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
4704d0be NHE |
11888 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
11889 | else | |
11890 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); | |
11891 | vmx_set_efer(vcpu, vcpu->arch.efer); | |
11892 | ||
11893 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); | |
11894 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); | |
1adfa76a | 11895 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
4704d0be NHE |
11896 | /* |
11897 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't | |
bd7e5b08 PB |
11898 | * actually changed, because vmx_set_cr0 refers to efer set above. |
11899 | * | |
11900 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 | |
11901 | * (KVM doesn't change it); | |
4704d0be | 11902 | */ |
bd7e5b08 | 11903 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
9e3e4dbf | 11904 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
4704d0be | 11905 | |
bd7e5b08 | 11906 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
4704d0be | 11907 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
8eb3f87d | 11908 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
4704d0be | 11909 | |
c08567af SC |
11910 | nested_ept_uninit_mmu_context(vcpu); |
11911 | ||
11912 | /* | |
11913 | * Only PDPTE load can fail as the value of cr3 was checked on entry and | |
11914 | * couldn't have changed. | |
11915 | */ | |
11916 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) | |
11917 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); | |
11918 | ||
11919 | if (!enable_ept) | |
11920 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; | |
feaf0c7d | 11921 | |
4704d0be NHE |
11922 | if (enable_vpid) { |
11923 | /* | |
11924 | * Trivially support vpid by letting L2s share their parent | |
11925 | * L1's vpid. TODO: move to a more elaborate solution, giving | |
11926 | * each L2 its own vpid and exposing the vpid feature to L1. | |
11927 | */ | |
6bf41e55 | 11928 | vmx_flush_tlb(vcpu, true); |
4704d0be | 11929 | } |
06a5524f WV |
11930 | /* Restore posted intr vector. */ |
11931 | if (nested_cpu_has_posted_intr(vmcs12)) | |
11932 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); | |
4704d0be NHE |
11933 | |
11934 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); | |
11935 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); | |
11936 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); | |
11937 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); | |
11938 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); | |
21f2d551 LP |
11939 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
11940 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); | |
4704d0be | 11941 | |
36be0b9d PB |
11942 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
11943 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) | |
11944 | vmcs_write64(GUEST_BNDCFGS, 0); | |
11945 | ||
44811c02 | 11946 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
4704d0be | 11947 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
44811c02 JK |
11948 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
11949 | } | |
4704d0be NHE |
11950 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
11951 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, | |
11952 | vmcs12->host_ia32_perf_global_ctrl); | |
503cd0c5 | 11953 | |
21feb4eb ACL |
11954 | /* Set L1 segment info according to Intel SDM |
11955 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ | |
11956 | seg = (struct kvm_segment) { | |
11957 | .base = 0, | |
11958 | .limit = 0xFFFFFFFF, | |
11959 | .selector = vmcs12->host_cs_selector, | |
11960 | .type = 11, | |
11961 | .present = 1, | |
11962 | .s = 1, | |
11963 | .g = 1 | |
11964 | }; | |
11965 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) | |
11966 | seg.l = 1; | |
11967 | else | |
11968 | seg.db = 1; | |
11969 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); | |
11970 | seg = (struct kvm_segment) { | |
11971 | .base = 0, | |
11972 | .limit = 0xFFFFFFFF, | |
11973 | .type = 3, | |
11974 | .present = 1, | |
11975 | .s = 1, | |
11976 | .db = 1, | |
11977 | .g = 1 | |
11978 | }; | |
11979 | seg.selector = vmcs12->host_ds_selector; | |
11980 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); | |
11981 | seg.selector = vmcs12->host_es_selector; | |
11982 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); | |
11983 | seg.selector = vmcs12->host_ss_selector; | |
11984 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); | |
11985 | seg.selector = vmcs12->host_fs_selector; | |
11986 | seg.base = vmcs12->host_fs_base; | |
11987 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); | |
11988 | seg.selector = vmcs12->host_gs_selector; | |
11989 | seg.base = vmcs12->host_gs_base; | |
11990 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); | |
11991 | seg = (struct kvm_segment) { | |
205befd9 | 11992 | .base = vmcs12->host_tr_base, |
21feb4eb ACL |
11993 | .limit = 0x67, |
11994 | .selector = vmcs12->host_tr_selector, | |
11995 | .type = 11, | |
11996 | .present = 1 | |
11997 | }; | |
11998 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); | |
11999 | ||
503cd0c5 JK |
12000 | kvm_set_dr(vcpu, 7, 0x400); |
12001 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
ff651cb6 | 12002 | |
3af18d9c | 12003 | if (cpu_has_vmx_msr_bitmap()) |
4b0be90f | 12004 | vmx_update_msr_bitmap(vcpu); |
3af18d9c | 12005 | |
ff651cb6 WV |
12006 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
12007 | vmcs12->vm_exit_msr_load_count)) | |
12008 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); | |
4704d0be NHE |
12009 | } |
12010 | ||
c08567af SC |
12011 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
12012 | { | |
12013 | struct shared_msr_entry *efer_msr; | |
12014 | unsigned int i; | |
12015 | ||
12016 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) | |
12017 | return vmcs_read64(GUEST_IA32_EFER); | |
12018 | ||
12019 | if (cpu_has_load_ia32_efer) | |
12020 | return host_efer; | |
12021 | ||
12022 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { | |
12023 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) | |
12024 | return vmx->msr_autoload.guest.val[i].value; | |
12025 | } | |
12026 | ||
12027 | efer_msr = find_msr_entry(vmx, MSR_EFER); | |
12028 | if (efer_msr) | |
12029 | return efer_msr->data; | |
12030 | ||
12031 | return host_efer; | |
12032 | } | |
12033 | ||
12034 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) | |
12035 | { | |
12036 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); | |
12037 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
12038 | struct vmx_msr_entry g, h; | |
12039 | struct msr_data msr; | |
12040 | gpa_t gpa; | |
12041 | u32 i, j; | |
12042 | ||
12043 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); | |
12044 | ||
12045 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { | |
12046 | /* | |
12047 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set | |
12048 | * as vmcs01.GUEST_DR7 contains a userspace defined value | |
12049 | * and vcpu->arch.dr7 is not squirreled away before the | |
12050 | * nested VMENTER (not worth adding a variable in nested_vmx). | |
12051 | */ | |
12052 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) | |
12053 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); | |
12054 | else | |
12055 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); | |
12056 | } | |
12057 | ||
12058 | /* | |
12059 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they | |
12060 | * handle a variety of side effects to KVM's software model. | |
12061 | */ | |
12062 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); | |
12063 | ||
12064 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; | |
12065 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); | |
12066 | ||
12067 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); | |
12068 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); | |
12069 | ||
12070 | nested_ept_uninit_mmu_context(vcpu); | |
12071 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); | |
12072 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); | |
12073 | ||
12074 | /* | |
12075 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs | |
12076 | * from vmcs01 (if necessary). The PDPTRs are not loaded on | |
12077 | * VMFail, like everything else we just need to ensure our | |
12078 | * software model is up-to-date. | |
12079 | */ | |
12080 | ept_save_pdptrs(vcpu); | |
12081 | ||
12082 | kvm_mmu_reset_context(vcpu); | |
12083 | ||
12084 | if (cpu_has_vmx_msr_bitmap()) | |
12085 | vmx_update_msr_bitmap(vcpu); | |
12086 | ||
12087 | /* | |
12088 | * This nasty bit of open coding is a compromise between blindly | |
12089 | * loading L1's MSRs using the exit load lists (incorrect emulation | |
12090 | * of VMFail), leaving the nested VM's MSRs in the software model | |
12091 | * (incorrect behavior) and snapshotting the modified MSRs (too | |
12092 | * expensive since the lists are unbound by hardware). For each | |
12093 | * MSR that was (prematurely) loaded from the nested VMEntry load | |
12094 | * list, reload it from the exit load list if it exists and differs | |
12095 | * from the guest value. The intent is to stuff host state as | |
12096 | * silently as possible, not to fully process the exit load list. | |
12097 | */ | |
12098 | msr.host_initiated = false; | |
12099 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { | |
12100 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); | |
12101 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { | |
12102 | pr_debug_ratelimited( | |
12103 | "%s read MSR index failed (%u, 0x%08llx)\n", | |
12104 | __func__, i, gpa); | |
12105 | goto vmabort; | |
12106 | } | |
12107 | ||
12108 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { | |
12109 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); | |
12110 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { | |
12111 | pr_debug_ratelimited( | |
12112 | "%s read MSR failed (%u, 0x%08llx)\n", | |
12113 | __func__, j, gpa); | |
12114 | goto vmabort; | |
12115 | } | |
12116 | if (h.index != g.index) | |
12117 | continue; | |
12118 | if (h.value == g.value) | |
12119 | break; | |
12120 | ||
12121 | if (nested_vmx_load_msr_check(vcpu, &h)) { | |
12122 | pr_debug_ratelimited( | |
12123 | "%s check failed (%u, 0x%x, 0x%x)\n", | |
12124 | __func__, j, h.index, h.reserved); | |
12125 | goto vmabort; | |
12126 | } | |
12127 | ||
12128 | msr.index = h.index; | |
12129 | msr.data = h.value; | |
12130 | if (kvm_set_msr(vcpu, &msr)) { | |
12131 | pr_debug_ratelimited( | |
12132 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", | |
12133 | __func__, j, h.index, h.value); | |
12134 | goto vmabort; | |
12135 | } | |
12136 | } | |
12137 | } | |
12138 | ||
12139 | return; | |
12140 | ||
12141 | vmabort: | |
12142 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); | |
12143 | } | |
12144 | ||
4704d0be NHE |
12145 | /* |
12146 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 | |
12147 | * and modify vmcs12 to make it see what it would expect to see there if | |
12148 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) | |
12149 | */ | |
533558bc JK |
12150 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
12151 | u32 exit_intr_info, | |
12152 | unsigned long exit_qualification) | |
4704d0be NHE |
12153 | { |
12154 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
4704d0be NHE |
12155 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
12156 | ||
5f3d5799 JK |
12157 | /* trying to cancel vmlaunch/vmresume is a bug */ |
12158 | WARN_ON_ONCE(vmx->nested.nested_run_pending); | |
12159 | ||
4f350c6d JM |
12160 | /* |
12161 | * The only expected VM-instruction error is "VM entry with | |
12162 | * invalid control field(s)." Anything else indicates a | |
12163 | * problem with L0. | |
12164 | */ | |
12165 | WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) != | |
12166 | VMXERR_ENTRY_INVALID_CONTROL_FIELD)); | |
12167 | ||
4704d0be | 12168 | leave_guest_mode(vcpu); |
4704d0be | 12169 | |
f7f5542f KA |
12170 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
12171 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; | |
12172 | ||
4f350c6d | 12173 | if (likely(!vmx->fail)) { |
72e9cbdb LP |
12174 | if (exit_reason == -1) |
12175 | sync_vmcs12(vcpu, vmcs12); | |
12176 | else | |
12177 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, | |
12178 | exit_qualification); | |
ff651cb6 | 12179 | |
4f350c6d JM |
12180 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
12181 | vmcs12->vm_exit_msr_store_count)) | |
12182 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); | |
12183 | } | |
cf3215d9 | 12184 | |
1279a6b1 | 12185 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
9ee802b1 GKH |
12186 | vm_entry_controls_reset_shadow(vmx); |
12187 | vm_exit_controls_reset_shadow(vmx); | |
12188 | vmx_segment_cache_clear(vmx); | |
36c3cc42 | 12189 | |
9314006d | 12190 | /* Update any VMCS fields that might have changed while L2 ran */ |
6e3dedb6 KRW |
12191 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
12192 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); | |
ea26e4ec | 12193 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
9314006d PB |
12194 | if (vmx->hv_deadline_tsc == -1) |
12195 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
12196 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
12197 | else | |
12198 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
12199 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c95ba92a PF |
12200 | if (kvm_has_tsc_control) |
12201 | decache_tsc_multiplier(vmx); | |
4704d0be | 12202 | |
d3f4c0a5 JM |
12203 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
12204 | vmx->nested.change_vmcs01_virtual_apic_mode = false; | |
12205 | vmx_set_virtual_apic_mode(vcpu); | |
fb6c8198 JM |
12206 | } else if (!nested_cpu_has_ept(vmcs12) && |
12207 | nested_cpu_has2(vmcs12, | |
12208 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { | |
6bf41e55 | 12209 | vmx_flush_tlb(vcpu, true); |
dccbfcf5 | 12210 | } |
4704d0be NHE |
12211 | |
12212 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ | |
12213 | vmx->host_rsp = 0; | |
12214 | ||
12215 | /* Unpin physical memory we referred to in vmcs02 */ | |
12216 | if (vmx->nested.apic_access_page) { | |
53a70daf | 12217 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
48d89b92 | 12218 | vmx->nested.apic_access_page = NULL; |
4704d0be | 12219 | } |
a7c0b07d | 12220 | if (vmx->nested.virtual_apic_page) { |
53a70daf | 12221 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
48d89b92 | 12222 | vmx->nested.virtual_apic_page = NULL; |
a7c0b07d | 12223 | } |
705699a1 WV |
12224 | if (vmx->nested.pi_desc_page) { |
12225 | kunmap(vmx->nested.pi_desc_page); | |
53a70daf | 12226 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
705699a1 WV |
12227 | vmx->nested.pi_desc_page = NULL; |
12228 | vmx->nested.pi_desc = NULL; | |
12229 | } | |
4704d0be | 12230 | |
38b99173 TC |
12231 | /* |
12232 | * We are now running in L2, mmu_notifier will force to reload the | |
12233 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. | |
12234 | */ | |
c83b6d15 | 12235 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
38b99173 | 12236 | |
72e9cbdb | 12237 | if (enable_shadow_vmcs && exit_reason != -1) |
012f83cb | 12238 | vmx->nested.sync_shadow_vmcs = true; |
b6b8a145 JK |
12239 | |
12240 | /* in case we halted in L2 */ | |
12241 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
4f350c6d JM |
12242 | |
12243 | if (likely(!vmx->fail)) { | |
12244 | /* | |
12245 | * TODO: SDM says that with acknowledge interrupt on | |
12246 | * exit, bit 31 of the VM-exit interrupt information | |
12247 | * (valid interrupt) is always set to 1 on | |
12248 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't | |
12249 | * need kvm_cpu_has_interrupt(). See the commit | |
12250 | * message for details. | |
12251 | */ | |
12252 | if (nested_exit_intr_ack_set(vcpu) && | |
12253 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && | |
12254 | kvm_cpu_has_interrupt(vcpu)) { | |
12255 | int irq = kvm_cpu_get_interrupt(vcpu); | |
12256 | WARN_ON(irq < 0); | |
12257 | vmcs12->vm_exit_intr_info = irq | | |
12258 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; | |
12259 | } | |
12260 | ||
72e9cbdb LP |
12261 | if (exit_reason != -1) |
12262 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, | |
12263 | vmcs12->exit_qualification, | |
12264 | vmcs12->idt_vectoring_info_field, | |
12265 | vmcs12->vm_exit_intr_info, | |
12266 | vmcs12->vm_exit_intr_error_code, | |
12267 | KVM_ISA_VMX); | |
4f350c6d JM |
12268 | |
12269 | load_vmcs12_host_state(vcpu, vmcs12); | |
12270 | ||
12271 | return; | |
12272 | } | |
12273 | ||
12274 | /* | |
12275 | * After an early L2 VM-entry failure, we're now back | |
12276 | * in L1 which thinks it just finished a VMLAUNCH or | |
12277 | * VMRESUME instruction, so we need to set the failure | |
12278 | * flag and the VM-instruction error field of the VMCS | |
12279 | * accordingly. | |
12280 | */ | |
12281 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); | |
5af41573 | 12282 | |
c08567af SC |
12283 | /* |
12284 | * Restore L1's host state to KVM's software model. We're here | |
12285 | * because a consistency check was caught by hardware, which | |
12286 | * means some amount of guest state has been propagated to KVM's | |
12287 | * model and needs to be unwound to the host's state. | |
12288 | */ | |
12289 | nested_vmx_restore_host_state(vcpu); | |
5af41573 | 12290 | |
4f350c6d JM |
12291 | /* |
12292 | * The emulated instruction was already skipped in | |
12293 | * nested_vmx_run, but the updated RIP was never | |
12294 | * written back to the vmcs01. | |
12295 | */ | |
12296 | skip_emulated_instruction(vcpu); | |
12297 | vmx->fail = 0; | |
4704d0be NHE |
12298 | } |
12299 | ||
42124925 JK |
12300 | /* |
12301 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. | |
12302 | */ | |
12303 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) | |
12304 | { | |
2f707d97 WL |
12305 | if (is_guest_mode(vcpu)) { |
12306 | to_vmx(vcpu)->nested.nested_run_pending = 0; | |
533558bc | 12307 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
2f707d97 | 12308 | } |
42124925 JK |
12309 | free_nested(to_vmx(vcpu)); |
12310 | } | |
12311 | ||
7c177938 NHE |
12312 | /* |
12313 | * L1's failure to enter L2 is a subset of a normal exit, as explained in | |
12314 | * 23.7 "VM-entry failures during or after loading guest state" (this also | |
12315 | * lists the acceptable exit-reason and exit-qualification parameters). | |
12316 | * It should only be called before L2 actually succeeded to run, and when | |
12317 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). | |
12318 | */ | |
12319 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, | |
12320 | struct vmcs12 *vmcs12, | |
12321 | u32 reason, unsigned long qualification) | |
12322 | { | |
12323 | load_vmcs12_host_state(vcpu, vmcs12); | |
12324 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; | |
12325 | vmcs12->exit_qualification = qualification; | |
12326 | nested_vmx_succeed(vcpu); | |
012f83cb AG |
12327 | if (enable_shadow_vmcs) |
12328 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; | |
7c177938 NHE |
12329 | } |
12330 | ||
8a76d7f2 JR |
12331 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
12332 | struct x86_instruction_info *info, | |
12333 | enum x86_intercept_stage stage) | |
12334 | { | |
4b12b681 PB |
12335 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
12336 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; | |
12337 | ||
12338 | /* | |
12339 | * RDPID causes #UD if disabled through secondary execution controls. | |
12340 | * Because it is marked as EmulateOnUD, we need to intercept it here. | |
12341 | */ | |
12342 | if (info->intercept == x86_intercept_rdtscp && | |
12343 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { | |
12344 | ctxt->exception.vector = UD_VECTOR; | |
12345 | ctxt->exception.error_code_valid = false; | |
12346 | return X86EMUL_PROPAGATE_FAULT; | |
12347 | } | |
12348 | ||
12349 | /* TODO: check more intercepts... */ | |
8a76d7f2 JR |
12350 | return X86EMUL_CONTINUE; |
12351 | } | |
12352 | ||
64672c95 YJ |
12353 | #ifdef CONFIG_X86_64 |
12354 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ | |
12355 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, | |
12356 | u64 divisor, u64 *result) | |
12357 | { | |
12358 | u64 low = a << shift, high = a >> (64 - shift); | |
12359 | ||
12360 | /* To avoid the overflow on divq */ | |
12361 | if (high >= divisor) | |
12362 | return 1; | |
12363 | ||
12364 | /* Low hold the result, high hold rem which is discarded */ | |
12365 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : | |
12366 | "rm" (divisor), "0" (low), "1" (high)); | |
12367 | *result = low; | |
12368 | ||
12369 | return 0; | |
12370 | } | |
12371 | ||
12372 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) | |
12373 | { | |
12374 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
9175d2e9 PB |
12375 | u64 tscl = rdtsc(); |
12376 | u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); | |
12377 | u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; | |
64672c95 YJ |
12378 | |
12379 | /* Convert to host delta tsc if tsc scaling is enabled */ | |
12380 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && | |
12381 | u64_shl_div_u64(delta_tsc, | |
12382 | kvm_tsc_scaling_ratio_frac_bits, | |
12383 | vcpu->arch.tsc_scaling_ratio, | |
12384 | &delta_tsc)) | |
12385 | return -ERANGE; | |
12386 | ||
12387 | /* | |
12388 | * If the delta tsc can't fit in the 32 bit after the multi shift, | |
12389 | * we can't use the preemption timer. | |
12390 | * It's possible that it fits on later vmentries, but checking | |
12391 | * on every vmentry is costly so we just use an hrtimer. | |
12392 | */ | |
12393 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) | |
12394 | return -ERANGE; | |
12395 | ||
12396 | vmx->hv_deadline_tsc = tscl + delta_tsc; | |
12397 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, | |
12398 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
c8533544 WL |
12399 | |
12400 | return delta_tsc == 0; | |
64672c95 YJ |
12401 | } |
12402 | ||
12403 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) | |
12404 | { | |
12405 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
12406 | vmx->hv_deadline_tsc = -1; | |
12407 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, | |
12408 | PIN_BASED_VMX_PREEMPTION_TIMER); | |
12409 | } | |
12410 | #endif | |
12411 | ||
48d89b92 | 12412 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
ae97a3b8 | 12413 | { |
b4a2d31d RK |
12414 | if (ple_gap) |
12415 | shrink_ple_window(vcpu); | |
ae97a3b8 RK |
12416 | } |
12417 | ||
843e4330 KH |
12418 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
12419 | struct kvm_memory_slot *slot) | |
12420 | { | |
12421 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); | |
12422 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); | |
12423 | } | |
12424 | ||
12425 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, | |
12426 | struct kvm_memory_slot *slot) | |
12427 | { | |
12428 | kvm_mmu_slot_set_dirty(kvm, slot); | |
12429 | } | |
12430 | ||
12431 | static void vmx_flush_log_dirty(struct kvm *kvm) | |
12432 | { | |
12433 | kvm_flush_pml_buffers(kvm); | |
12434 | } | |
12435 | ||
c5f983f6 BD |
12436 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
12437 | { | |
12438 | struct vmcs12 *vmcs12; | |
12439 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
12440 | gpa_t gpa; | |
12441 | struct page *page = NULL; | |
12442 | u64 *pml_address; | |
12443 | ||
12444 | if (is_guest_mode(vcpu)) { | |
12445 | WARN_ON_ONCE(vmx->nested.pml_full); | |
12446 | ||
12447 | /* | |
12448 | * Check if PML is enabled for the nested guest. | |
12449 | * Whether eptp bit 6 is set is already checked | |
12450 | * as part of A/D emulation. | |
12451 | */ | |
12452 | vmcs12 = get_vmcs12(vcpu); | |
12453 | if (!nested_cpu_has_pml(vmcs12)) | |
12454 | return 0; | |
12455 | ||
4769886b | 12456 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
c5f983f6 BD |
12457 | vmx->nested.pml_full = true; |
12458 | return 1; | |
12459 | } | |
12460 | ||
12461 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; | |
12462 | ||
5e2f30b7 DH |
12463 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
12464 | if (is_error_page(page)) | |
c5f983f6 BD |
12465 | return 0; |
12466 | ||
12467 | pml_address = kmap(page); | |
12468 | pml_address[vmcs12->guest_pml_index--] = gpa; | |
12469 | kunmap(page); | |
53a70daf | 12470 | kvm_release_page_clean(page); |
c5f983f6 BD |
12471 | } |
12472 | ||
12473 | return 0; | |
12474 | } | |
12475 | ||
843e4330 KH |
12476 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
12477 | struct kvm_memory_slot *memslot, | |
12478 | gfn_t offset, unsigned long mask) | |
12479 | { | |
12480 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); | |
12481 | } | |
12482 | ||
cd39e117 PB |
12483 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
12484 | { | |
12485 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
12486 | struct pi_desc old, new; | |
12487 | unsigned int dest; | |
cd39e117 PB |
12488 | |
12489 | do { | |
12490 | old.control = new.control = pi_desc->control; | |
8b306e2f PB |
12491 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
12492 | "Wakeup handler not enabled while the VCPU is blocked\n"); | |
cd39e117 PB |
12493 | |
12494 | dest = cpu_physical_id(vcpu->cpu); | |
12495 | ||
12496 | if (x2apic_enabled()) | |
12497 | new.ndst = dest; | |
12498 | else | |
12499 | new.ndst = (dest << 8) & 0xFF00; | |
12500 | ||
cd39e117 PB |
12501 | /* set 'NV' to 'notification vector' */ |
12502 | new.nv = POSTED_INTR_VECTOR; | |
c0a1666b PB |
12503 | } while (cmpxchg64(&pi_desc->control, old.control, |
12504 | new.control) != old.control); | |
cd39e117 | 12505 | |
8b306e2f PB |
12506 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
12507 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
cd39e117 | 12508 | list_del(&vcpu->blocked_vcpu_list); |
8b306e2f | 12509 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
cd39e117 PB |
12510 | vcpu->pre_pcpu = -1; |
12511 | } | |
12512 | } | |
12513 | ||
bf9f6ac8 FW |
12514 | /* |
12515 | * This routine does the following things for vCPU which is going | |
12516 | * to be blocked if VT-d PI is enabled. | |
12517 | * - Store the vCPU to the wakeup list, so when interrupts happen | |
12518 | * we can find the right vCPU to wake up. | |
12519 | * - Change the Posted-interrupt descriptor as below: | |
12520 | * 'NDST' <-- vcpu->pre_pcpu | |
12521 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR | |
12522 | * - If 'ON' is set during this process, which means at least one | |
12523 | * interrupt is posted for this vCPU, we cannot block it, in | |
12524 | * this case, return 1, otherwise, return 0. | |
12525 | * | |
12526 | */ | |
bc22512b | 12527 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
bf9f6ac8 | 12528 | { |
bf9f6ac8 FW |
12529 | unsigned int dest; |
12530 | struct pi_desc old, new; | |
12531 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); | |
12532 | ||
12533 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || | |
a0052191 YZ |
12534 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
12535 | !kvm_vcpu_apicv_active(vcpu)) | |
bf9f6ac8 FW |
12536 | return 0; |
12537 | ||
8b306e2f PB |
12538 | WARN_ON(irqs_disabled()); |
12539 | local_irq_disable(); | |
12540 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { | |
12541 | vcpu->pre_pcpu = vcpu->cpu; | |
12542 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
12543 | list_add_tail(&vcpu->blocked_vcpu_list, | |
12544 | &per_cpu(blocked_vcpu_on_cpu, | |
12545 | vcpu->pre_pcpu)); | |
12546 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); | |
12547 | } | |
bf9f6ac8 FW |
12548 | |
12549 | do { | |
12550 | old.control = new.control = pi_desc->control; | |
12551 | ||
bf9f6ac8 FW |
12552 | WARN((pi_desc->sn == 1), |
12553 | "Warning: SN field of posted-interrupts " | |
12554 | "is set before blocking\n"); | |
12555 | ||
12556 | /* | |
12557 | * Since vCPU can be preempted during this process, | |
12558 | * vcpu->cpu could be different with pre_pcpu, we | |
12559 | * need to set pre_pcpu as the destination of wakeup | |
12560 | * notification event, then we can find the right vCPU | |
12561 | * to wakeup in wakeup handler if interrupts happen | |
12562 | * when the vCPU is in blocked state. | |
12563 | */ | |
12564 | dest = cpu_physical_id(vcpu->pre_pcpu); | |
12565 | ||
12566 | if (x2apic_enabled()) | |
12567 | new.ndst = dest; | |
12568 | else | |
12569 | new.ndst = (dest << 8) & 0xFF00; | |
12570 | ||
12571 | /* set 'NV' to 'wakeup vector' */ | |
12572 | new.nv = POSTED_INTR_WAKEUP_VECTOR; | |
c0a1666b PB |
12573 | } while (cmpxchg64(&pi_desc->control, old.control, |
12574 | new.control) != old.control); | |
bf9f6ac8 | 12575 | |
8b306e2f PB |
12576 | /* We should not block the vCPU if an interrupt is posted for it. */ |
12577 | if (pi_test_on(pi_desc) == 1) | |
12578 | __pi_post_block(vcpu); | |
12579 | ||
12580 | local_irq_enable(); | |
12581 | return (vcpu->pre_pcpu == -1); | |
bf9f6ac8 FW |
12582 | } |
12583 | ||
bc22512b YJ |
12584 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
12585 | { | |
12586 | if (pi_pre_block(vcpu)) | |
12587 | return 1; | |
12588 | ||
64672c95 YJ |
12589 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
12590 | kvm_lapic_switch_to_sw_timer(vcpu); | |
12591 | ||
bc22512b YJ |
12592 | return 0; |
12593 | } | |
12594 | ||
12595 | static void pi_post_block(struct kvm_vcpu *vcpu) | |
bf9f6ac8 | 12596 | { |
8b306e2f | 12597 | if (vcpu->pre_pcpu == -1) |
bf9f6ac8 FW |
12598 | return; |
12599 | ||
8b306e2f PB |
12600 | WARN_ON(irqs_disabled()); |
12601 | local_irq_disable(); | |
cd39e117 | 12602 | __pi_post_block(vcpu); |
8b306e2f | 12603 | local_irq_enable(); |
bf9f6ac8 FW |
12604 | } |
12605 | ||
bc22512b YJ |
12606 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
12607 | { | |
64672c95 YJ |
12608 | if (kvm_x86_ops->set_hv_timer) |
12609 | kvm_lapic_switch_to_hv_timer(vcpu); | |
12610 | ||
bc22512b YJ |
12611 | pi_post_block(vcpu); |
12612 | } | |
12613 | ||
efc64404 FW |
12614 | /* |
12615 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts | |
12616 | * | |
12617 | * @kvm: kvm | |
12618 | * @host_irq: host irq of the interrupt | |
12619 | * @guest_irq: gsi of the interrupt | |
12620 | * @set: set or unset PI | |
12621 | * returns 0 on success, < 0 on failure | |
12622 | */ | |
12623 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, | |
12624 | uint32_t guest_irq, bool set) | |
12625 | { | |
12626 | struct kvm_kernel_irq_routing_entry *e; | |
12627 | struct kvm_irq_routing_table *irq_rt; | |
12628 | struct kvm_lapic_irq irq; | |
12629 | struct kvm_vcpu *vcpu; | |
12630 | struct vcpu_data vcpu_info; | |
3a8b0677 | 12631 | int idx, ret = 0; |
efc64404 FW |
12632 | |
12633 | if (!kvm_arch_has_assigned_device(kvm) || | |
a0052191 YZ |
12634 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
12635 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) | |
efc64404 FW |
12636 | return 0; |
12637 | ||
12638 | idx = srcu_read_lock(&kvm->irq_srcu); | |
12639 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); | |
3a8b0677 JS |
12640 | if (guest_irq >= irq_rt->nr_rt_entries || |
12641 | hlist_empty(&irq_rt->map[guest_irq])) { | |
12642 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", | |
12643 | guest_irq, irq_rt->nr_rt_entries); | |
12644 | goto out; | |
12645 | } | |
efc64404 FW |
12646 | |
12647 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { | |
12648 | if (e->type != KVM_IRQ_ROUTING_MSI) | |
12649 | continue; | |
12650 | /* | |
12651 | * VT-d PI cannot support posting multicast/broadcast | |
12652 | * interrupts to a vCPU, we still use interrupt remapping | |
12653 | * for these kind of interrupts. | |
12654 | * | |
12655 | * For lowest-priority interrupts, we only support | |
12656 | * those with single CPU as the destination, e.g. user | |
12657 | * configures the interrupts via /proc/irq or uses | |
12658 | * irqbalance to make the interrupts single-CPU. | |
12659 | * | |
12660 | * We will support full lowest-priority interrupt later. | |
12661 | */ | |
12662 | ||
37131313 | 12663 | kvm_set_msi_irq(kvm, e, &irq); |
23a1c257 FW |
12664 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
12665 | /* | |
12666 | * Make sure the IRTE is in remapped mode if | |
12667 | * we don't handle it in posted mode. | |
12668 | */ | |
12669 | ret = irq_set_vcpu_affinity(host_irq, NULL); | |
12670 | if (ret < 0) { | |
12671 | printk(KERN_INFO | |
12672 | "failed to back to remapped mode, irq: %u\n", | |
12673 | host_irq); | |
12674 | goto out; | |
12675 | } | |
12676 | ||
efc64404 | 12677 | continue; |
23a1c257 | 12678 | } |
efc64404 FW |
12679 | |
12680 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); | |
12681 | vcpu_info.vector = irq.vector; | |
12682 | ||
4edf01c6 | 12683 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
efc64404 FW |
12684 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
12685 | ||
12686 | if (set) | |
12687 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); | |
dc91f2eb | 12688 | else |
efc64404 | 12689 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
efc64404 FW |
12690 | |
12691 | if (ret < 0) { | |
12692 | printk(KERN_INFO "%s: failed to update PI IRTE\n", | |
12693 | __func__); | |
12694 | goto out; | |
12695 | } | |
12696 | } | |
12697 | ||
12698 | ret = 0; | |
12699 | out: | |
12700 | srcu_read_unlock(&kvm->irq_srcu, idx); | |
12701 | return ret; | |
12702 | } | |
12703 | ||
c45dcc71 AR |
12704 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
12705 | { | |
12706 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) | |
12707 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= | |
12708 | FEATURE_CONTROL_LMCE; | |
12709 | else | |
12710 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= | |
12711 | ~FEATURE_CONTROL_LMCE; | |
12712 | } | |
12713 | ||
72d7b374 LP |
12714 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
12715 | { | |
72e9cbdb LP |
12716 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
12717 | if (to_vmx(vcpu)->nested.nested_run_pending) | |
12718 | return 0; | |
72d7b374 LP |
12719 | return 1; |
12720 | } | |
12721 | ||
0234bf88 LP |
12722 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
12723 | { | |
72e9cbdb LP |
12724 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
12725 | ||
12726 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); | |
12727 | if (vmx->nested.smm.guest_mode) | |
12728 | nested_vmx_vmexit(vcpu, -1, 0, 0); | |
12729 | ||
12730 | vmx->nested.smm.vmxon = vmx->nested.vmxon; | |
12731 | vmx->nested.vmxon = false; | |
0234bf88 LP |
12732 | return 0; |
12733 | } | |
12734 | ||
12735 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) | |
12736 | { | |
72e9cbdb LP |
12737 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
12738 | int ret; | |
12739 | ||
12740 | if (vmx->nested.smm.vmxon) { | |
12741 | vmx->nested.vmxon = true; | |
12742 | vmx->nested.smm.vmxon = false; | |
12743 | } | |
12744 | ||
12745 | if (vmx->nested.smm.guest_mode) { | |
12746 | vcpu->arch.hflags &= ~HF_SMM_MASK; | |
12747 | ret = enter_vmx_non_root_mode(vcpu, false); | |
12748 | vcpu->arch.hflags |= HF_SMM_MASK; | |
12749 | if (ret) | |
12750 | return ret; | |
12751 | ||
12752 | vmx->nested.smm.guest_mode = false; | |
12753 | } | |
0234bf88 LP |
12754 | return 0; |
12755 | } | |
12756 | ||
cc3d967f LP |
12757 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
12758 | { | |
12759 | return 0; | |
12760 | } | |
12761 | ||
404f6aac | 12762 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
6aa8b732 AK |
12763 | .cpu_has_kvm_support = cpu_has_kvm_support, |
12764 | .disabled_by_bios = vmx_disabled_by_bios, | |
12765 | .hardware_setup = hardware_setup, | |
12766 | .hardware_unsetup = hardware_unsetup, | |
002c7f7c | 12767 | .check_processor_compatibility = vmx_check_processor_compat, |
6aa8b732 AK |
12768 | .hardware_enable = hardware_enable, |
12769 | .hardware_disable = hardware_disable, | |
04547156 | 12770 | .cpu_has_accelerated_tpr = report_flexpriority, |
4d5c8a07 | 12771 | .has_emulated_msr = vmx_has_emulated_msr, |
6aa8b732 | 12772 | |
275b72a9 KRW |
12773 | .vm_init = vmx_vm_init, |
12774 | ||
6aa8b732 AK |
12775 | .vcpu_create = vmx_create_vcpu, |
12776 | .vcpu_free = vmx_free_vcpu, | |
04d2cc77 | 12777 | .vcpu_reset = vmx_vcpu_reset, |
6aa8b732 | 12778 | |
04d2cc77 | 12779 | .prepare_guest_switch = vmx_save_host_state, |
6aa8b732 AK |
12780 | .vcpu_load = vmx_vcpu_load, |
12781 | .vcpu_put = vmx_vcpu_put, | |
12782 | ||
a96036b8 | 12783 | .update_bp_intercept = update_exception_bitmap, |
ab1bebf8 | 12784 | .get_msr_feature = vmx_get_msr_feature, |
6aa8b732 AK |
12785 | .get_msr = vmx_get_msr, |
12786 | .set_msr = vmx_set_msr, | |
12787 | .get_segment_base = vmx_get_segment_base, | |
12788 | .get_segment = vmx_get_segment, | |
12789 | .set_segment = vmx_set_segment, | |
2e4d2653 | 12790 | .get_cpl = vmx_get_cpl, |
6aa8b732 | 12791 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
e8467fda | 12792 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
aff48baa | 12793 | .decache_cr3 = vmx_decache_cr3, |
25c4c276 | 12794 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
6aa8b732 | 12795 | .set_cr0 = vmx_set_cr0, |
6aa8b732 AK |
12796 | .set_cr3 = vmx_set_cr3, |
12797 | .set_cr4 = vmx_set_cr4, | |
6aa8b732 | 12798 | .set_efer = vmx_set_efer, |
6aa8b732 AK |
12799 | .get_idt = vmx_get_idt, |
12800 | .set_idt = vmx_set_idt, | |
12801 | .get_gdt = vmx_get_gdt, | |
12802 | .set_gdt = vmx_set_gdt, | |
73aaf249 JK |
12803 | .get_dr6 = vmx_get_dr6, |
12804 | .set_dr6 = vmx_set_dr6, | |
020df079 | 12805 | .set_dr7 = vmx_set_dr7, |
81908bf4 | 12806 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
5fdbf976 | 12807 | .cache_reg = vmx_cache_reg, |
6aa8b732 AK |
12808 | .get_rflags = vmx_get_rflags, |
12809 | .set_rflags = vmx_set_rflags, | |
be94f6b7 | 12810 | |
6aa8b732 | 12811 | .tlb_flush = vmx_flush_tlb, |
6aa8b732 | 12812 | |
6aa8b732 | 12813 | .run = vmx_vcpu_run, |
6062d012 | 12814 | .handle_exit = vmx_handle_exit, |
6aa8b732 | 12815 | .skip_emulated_instruction = skip_emulated_instruction, |
2809f5d2 GC |
12816 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
12817 | .get_interrupt_shadow = vmx_get_interrupt_shadow, | |
102d8325 | 12818 | .patch_hypercall = vmx_patch_hypercall, |
2a8067f1 | 12819 | .set_irq = vmx_inject_irq, |
95ba8273 | 12820 | .set_nmi = vmx_inject_nmi, |
298101da | 12821 | .queue_exception = vmx_queue_exception, |
b463a6f7 | 12822 | .cancel_injection = vmx_cancel_injection, |
78646121 | 12823 | .interrupt_allowed = vmx_interrupt_allowed, |
95ba8273 | 12824 | .nmi_allowed = vmx_nmi_allowed, |
3cfc3092 JK |
12825 | .get_nmi_mask = vmx_get_nmi_mask, |
12826 | .set_nmi_mask = vmx_set_nmi_mask, | |
95ba8273 GN |
12827 | .enable_nmi_window = enable_nmi_window, |
12828 | .enable_irq_window = enable_irq_window, | |
12829 | .update_cr8_intercept = update_cr8_intercept, | |
d3f4c0a5 | 12830 | .set_virtual_apic_mode = vmx_set_virtual_apic_mode, |
38b99173 | 12831 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
d62caabb AS |
12832 | .get_enable_apicv = vmx_get_enable_apicv, |
12833 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, | |
c7c9c56c | 12834 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
967235d3 | 12835 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
c7c9c56c YZ |
12836 | .hwapic_irr_update = vmx_hwapic_irr_update, |
12837 | .hwapic_isr_update = vmx_hwapic_isr_update, | |
a20ed54d YZ |
12838 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
12839 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, | |
050ee5a5 | 12840 | .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt, |
95ba8273 | 12841 | |
cbc94022 | 12842 | .set_tss_addr = vmx_set_tss_addr, |
67253af5 | 12843 | .get_tdp_level = get_ept_level, |
4b12f0de | 12844 | .get_mt_mask = vmx_get_mt_mask, |
229456fc | 12845 | |
586f9607 | 12846 | .get_exit_info = vmx_get_exit_info, |
586f9607 | 12847 | |
17cc3935 | 12848 | .get_lpage_level = vmx_get_lpage_level, |
0e851880 SY |
12849 | |
12850 | .cpuid_update = vmx_cpuid_update, | |
4e47c7a6 SY |
12851 | |
12852 | .rdtscp_supported = vmx_rdtscp_supported, | |
ad756a16 | 12853 | .invpcid_supported = vmx_invpcid_supported, |
d4330ef2 JR |
12854 | |
12855 | .set_supported_cpuid = vmx_set_supported_cpuid, | |
f5f48ee1 SY |
12856 | |
12857 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, | |
99e3e30a | 12858 | |
f7f5542f | 12859 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
7cb0f5cc | 12860 | .write_l1_tsc_offset = vmx_write_l1_tsc_offset, |
1c97f0a0 JR |
12861 | |
12862 | .set_tdp_cr3 = vmx_set_cr3, | |
8a76d7f2 JR |
12863 | |
12864 | .check_intercept = vmx_check_intercept, | |
a547c6db | 12865 | .handle_external_intr = vmx_handle_external_intr, |
da8999d3 | 12866 | .mpx_supported = vmx_mpx_supported, |
55412b2e | 12867 | .xsaves_supported = vmx_xsaves_supported, |
b6b8a145 JK |
12868 | |
12869 | .check_nested_events = vmx_check_nested_events, | |
ae97a3b8 RK |
12870 | |
12871 | .sched_in = vmx_sched_in, | |
843e4330 KH |
12872 | |
12873 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, | |
12874 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, | |
12875 | .flush_log_dirty = vmx_flush_log_dirty, | |
12876 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, | |
c5f983f6 | 12877 | .write_log_dirty = vmx_write_pml_buffer, |
25462f7f | 12878 | |
bf9f6ac8 FW |
12879 | .pre_block = vmx_pre_block, |
12880 | .post_block = vmx_post_block, | |
12881 | ||
25462f7f | 12882 | .pmu_ops = &intel_pmu_ops, |
efc64404 FW |
12883 | |
12884 | .update_pi_irte = vmx_update_pi_irte, | |
64672c95 YJ |
12885 | |
12886 | #ifdef CONFIG_X86_64 | |
12887 | .set_hv_timer = vmx_set_hv_timer, | |
12888 | .cancel_hv_timer = vmx_cancel_hv_timer, | |
12889 | #endif | |
c45dcc71 AR |
12890 | |
12891 | .setup_mce = vmx_setup_mce, | |
0234bf88 | 12892 | |
72d7b374 | 12893 | .smi_allowed = vmx_smi_allowed, |
0234bf88 LP |
12894 | .pre_enter_smm = vmx_pre_enter_smm, |
12895 | .pre_leave_smm = vmx_pre_leave_smm, | |
cc3d967f | 12896 | .enable_smi_window = enable_smi_window, |
6aa8b732 AK |
12897 | }; |
12898 | ||
b9cfedcd | 12899 | static void vmx_cleanup_l1d_flush(void) |
d665f9fc PB |
12900 | { |
12901 | if (vmx_l1d_flush_pages) { | |
12902 | free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); | |
12903 | vmx_l1d_flush_pages = NULL; | |
12904 | } | |
b9cfedcd TG |
12905 | /* Restore state so sysfs ignores VMX */ |
12906 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; | |
1749555e KRW |
12907 | } |
12908 | ||
1ead4979 TG |
12909 | static void vmx_exit(void) |
12910 | { | |
12911 | #ifdef CONFIG_KEXEC_CORE | |
12912 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); | |
12913 | synchronize_rcu(); | |
12914 | #endif | |
12915 | ||
12916 | kvm_exit(); | |
12917 | ||
12918 | vmx_cleanup_l1d_flush(); | |
12919 | } | |
12920 | module_exit(vmx_exit) | |
12921 | ||
6aa8b732 AK |
12922 | static int __init vmx_init(void) |
12923 | { | |
1749555e KRW |
12924 | int r; |
12925 | ||
1ead4979 TG |
12926 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
12927 | __alignof__(struct vcpu_vmx), THIS_MODULE); | |
d665f9fc PB |
12928 | if (r) |
12929 | return r; | |
1749555e | 12930 | |
1ead4979 | 12931 | /* |
2bcd5b01 TG |
12932 | * Must be called after kvm_init() so enable_ept is properly set |
12933 | * up. Hand the parameter mitigation value in which was stored in | |
12934 | * the pre module init parser. If no parameter was given, it will | |
12935 | * contain 'auto' which will be turned into the default 'cond' | |
12936 | * mitigation mode. | |
1ead4979 | 12937 | */ |
2bcd5b01 TG |
12938 | if (boot_cpu_has(X86_BUG_L1TF)) { |
12939 | r = vmx_setup_l1d_flush(vmentry_l1d_flush_param); | |
12940 | if (r) { | |
12941 | vmx_exit(); | |
12942 | return r; | |
12943 | } | |
d665f9fc | 12944 | } |
25c5f225 | 12945 | |
2965faa5 | 12946 | #ifdef CONFIG_KEXEC_CORE |
8f536b76 ZY |
12947 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
12948 | crash_vmclear_local_loaded_vmcss); | |
12949 | #endif | |
12950 | ||
fdef3ad1 | 12951 | return 0; |
6aa8b732 | 12952 | } |
6aa8b732 | 12953 | module_init(vmx_init) |