1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/objtool.h>
4 #include <linux/percpu.h>
5
6 #include <asm/debugreg.h>
7 #include <asm/mmu_context.h>
8
9 #include "cpuid.h"
10 #include "hyperv.h"
11 #include "mmu.h"
12 #include "nested.h"
13 #include "pmu.h"
14 #include "trace.h"
15 #include "vmx.h"
16 #include "x86.h"
17
18 static bool __read_mostly enable_shadow_vmcs = 1;
19 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
20
21 static bool __read_mostly nested_early_check = 0;
22 module_param(nested_early_check, bool, S_IRUGO);
23
24 #define CC(consistency_check) \
25 ({ \
26 bool failed = (consistency_check); \
27 if (failed) \
28 trace_kvm_nested_vmenter_failed(#consistency_check, 0); \
29 failed; \
30 })
31
32 /*
33 * Hyper-V requires all of these, so mark them as supported even though
34 * they are just treated the same as all-context.
35 */
36 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
37 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
38 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
39 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
40 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
41
42 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
43
44 enum {
45 VMX_VMREAD_BITMAP,
46 VMX_VMWRITE_BITMAP,
47 VMX_BITMAP_NR
48 };
49 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
50
51 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
52 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
53
54 struct shadow_vmcs_field {
55 u16 encoding;
56 u16 offset;
57 };
58 static struct shadow_vmcs_field shadow_read_only_fields[] = {
59 #define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) },
60 #include "vmcs_shadow_fields.h"
61 };
62 static int max_shadow_read_only_fields =
63 ARRAY_SIZE(shadow_read_only_fields);
64
65 static struct shadow_vmcs_field shadow_read_write_fields[] = {
66 #define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) },
67 #include "vmcs_shadow_fields.h"
68 };
69 static int max_shadow_read_write_fields =
70 ARRAY_SIZE(shadow_read_write_fields);
71
init_vmcs_shadow_fields(void)72 static void init_vmcs_shadow_fields(void)
73 {
74 int i, j;
75
76 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
77 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
78
79 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
80 struct shadow_vmcs_field entry = shadow_read_only_fields[i];
81 u16 field = entry.encoding;
82
83 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
84 (i + 1 == max_shadow_read_only_fields ||
85 shadow_read_only_fields[i + 1].encoding != field + 1))
86 pr_err("Missing field from shadow_read_only_field %x\n",
87 field + 1);
88
89 clear_bit(field, vmx_vmread_bitmap);
90 if (field & 1)
91 #ifdef CONFIG_X86_64
92 continue;
93 #else
94 entry.offset += sizeof(u32);
95 #endif
96 shadow_read_only_fields[j++] = entry;
97 }
98 max_shadow_read_only_fields = j;
99
100 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
101 struct shadow_vmcs_field entry = shadow_read_write_fields[i];
102 u16 field = entry.encoding;
103
104 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
105 (i + 1 == max_shadow_read_write_fields ||
106 shadow_read_write_fields[i + 1].encoding != field + 1))
107 pr_err("Missing field from shadow_read_write_field %x\n",
108 field + 1);
109
110 WARN_ONCE(field >= GUEST_ES_AR_BYTES &&
111 field <= GUEST_TR_AR_BYTES,
112 "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
113
114 /*
115 * PML and the preemption timer can be emulated, but the
116 * processor cannot vmwrite to fields that don't exist
117 * on bare metal.
118 */
119 switch (field) {
120 case GUEST_PML_INDEX:
121 if (!cpu_has_vmx_pml())
122 continue;
123 break;
124 case VMX_PREEMPTION_TIMER_VALUE:
125 if (!cpu_has_vmx_preemption_timer())
126 continue;
127 break;
128 case GUEST_INTR_STATUS:
129 if (!cpu_has_vmx_apicv())
130 continue;
131 break;
132 default:
133 break;
134 }
135
136 clear_bit(field, vmx_vmwrite_bitmap);
137 clear_bit(field, vmx_vmread_bitmap);
138 if (field & 1)
139 #ifdef CONFIG_X86_64
140 continue;
141 #else
142 entry.offset += sizeof(u32);
143 #endif
144 shadow_read_write_fields[j++] = entry;
145 }
146 max_shadow_read_write_fields = j;
147 }
148
149 /*
150 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
151 * set the success or error code of an emulated VMX instruction (as specified
152 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
153 * instruction.
154 */
nested_vmx_succeed(struct kvm_vcpu * vcpu)155 static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
156 {
157 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
158 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
159 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
160 return kvm_skip_emulated_instruction(vcpu);
161 }
162
nested_vmx_failInvalid(struct kvm_vcpu * vcpu)163 static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
164 {
165 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
166 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
167 X86_EFLAGS_SF | X86_EFLAGS_OF))
168 | X86_EFLAGS_CF);
169 return kvm_skip_emulated_instruction(vcpu);
170 }
171
nested_vmx_failValid(struct kvm_vcpu * vcpu,u32 vm_instruction_error)172 static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
173 u32 vm_instruction_error)
174 {
175 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
176 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
177 X86_EFLAGS_SF | X86_EFLAGS_OF))
178 | X86_EFLAGS_ZF);
179 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
180 /*
181 * We don't need to force a shadow sync because
182 * VM_INSTRUCTION_ERROR is not shadowed
183 */
184 return kvm_skip_emulated_instruction(vcpu);
185 }
186
nested_vmx_fail(struct kvm_vcpu * vcpu,u32 vm_instruction_error)187 static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error)
188 {
189 struct vcpu_vmx *vmx = to_vmx(vcpu);
190
191 /*
192 * failValid writes the error number to the current VMCS, which
193 * can't be done if there isn't a current VMCS.
194 */
195 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
196 return nested_vmx_failInvalid(vcpu);
197
198 return nested_vmx_failValid(vcpu, vm_instruction_error);
199 }
200
nested_vmx_abort(struct kvm_vcpu * vcpu,u32 indicator)201 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
202 {
203 /* TODO: not to reset guest simply here. */
204 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
205 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
206 }
207
vmx_control_verify(u32 control,u32 low,u32 high)208 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
209 {
210 return fixed_bits_valid(control, low, high);
211 }
212
vmx_control_msr(u32 low,u32 high)213 static inline u64 vmx_control_msr(u32 low, u32 high)
214 {
215 return low | ((u64)high << 32);
216 }
217
vmx_disable_shadow_vmcs(struct vcpu_vmx * vmx)218 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
219 {
220 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
221 vmcs_write64(VMCS_LINK_POINTER, -1ull);
222 vmx->nested.need_vmcs12_to_shadow_sync = false;
223 }
224
nested_release_evmcs(struct kvm_vcpu * vcpu)225 static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
226 {
227 struct vcpu_vmx *vmx = to_vmx(vcpu);
228
229 if (!vmx->nested.hv_evmcs)
230 return;
231
232 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
233 vmx->nested.hv_evmcs_vmptr = 0;
234 vmx->nested.hv_evmcs = NULL;
235 }
236
vmx_sync_vmcs_host_state(struct vcpu_vmx * vmx,struct loaded_vmcs * prev)237 static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
238 struct loaded_vmcs *prev)
239 {
240 struct vmcs_host_state *dest, *src;
241
242 if (unlikely(!vmx->guest_state_loaded))
243 return;
244
245 src = &prev->host_state;
246 dest = &vmx->loaded_vmcs->host_state;
247
248 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
249 dest->ldt_sel = src->ldt_sel;
250 #ifdef CONFIG_X86_64
251 dest->ds_sel = src->ds_sel;
252 dest->es_sel = src->es_sel;
253 #endif
254 }
255
vmx_switch_vmcs(struct kvm_vcpu * vcpu,struct loaded_vmcs * vmcs)256 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
257 {
258 struct vcpu_vmx *vmx = to_vmx(vcpu);
259 struct loaded_vmcs *prev;
260 int cpu;
261
262 if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs))
263 return;
264
265 cpu = get_cpu();
266 prev = vmx->loaded_vmcs;
267 vmx->loaded_vmcs = vmcs;
268 vmx_vcpu_load_vmcs(vcpu, cpu, prev);
269 vmx_sync_vmcs_host_state(vmx, prev);
270 put_cpu();
271
272 vmx_register_cache_reset(vcpu);
273 }
274
275 /*
276 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
277 * just stops using VMX.
278 */
free_nested(struct kvm_vcpu * vcpu)279 static void free_nested(struct kvm_vcpu *vcpu)
280 {
281 struct vcpu_vmx *vmx = to_vmx(vcpu);
282
283 if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01))
284 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
285
286 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
287 return;
288
289 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
290
291 vmx->nested.vmxon = false;
292 vmx->nested.smm.vmxon = false;
293 free_vpid(vmx->nested.vpid02);
294 vmx->nested.posted_intr_nv = -1;
295 vmx->nested.current_vmptr = -1ull;
296 if (enable_shadow_vmcs) {
297 vmx_disable_shadow_vmcs(vmx);
298 vmcs_clear(vmx->vmcs01.shadow_vmcs);
299 free_vmcs(vmx->vmcs01.shadow_vmcs);
300 vmx->vmcs01.shadow_vmcs = NULL;
301 }
302 kfree(vmx->nested.cached_vmcs12);
303 vmx->nested.cached_vmcs12 = NULL;
304 kfree(vmx->nested.cached_shadow_vmcs12);
305 vmx->nested.cached_shadow_vmcs12 = NULL;
306 /* Unpin physical memory we referred to in the vmcs02 */
307 if (vmx->nested.apic_access_page) {
308 kvm_release_page_clean(vmx->nested.apic_access_page);
309 vmx->nested.apic_access_page = NULL;
310 }
311 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
312 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
313 vmx->nested.pi_desc = NULL;
314
315 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
316
317 nested_release_evmcs(vcpu);
318
319 free_loaded_vmcs(&vmx->nested.vmcs02);
320 }
321
322 /*
323 * Ensure that the current vmcs of the logical processor is the
324 * vmcs01 of the vcpu before calling free_nested().
325 */
nested_vmx_free_vcpu(struct kvm_vcpu * vcpu)326 void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
327 {
328 vcpu_load(vcpu);
329 vmx_leave_nested(vcpu);
330 vcpu_put(vcpu);
331 }
332
nested_ept_inject_page_fault(struct kvm_vcpu * vcpu,struct x86_exception * fault)333 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
334 struct x86_exception *fault)
335 {
336 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
337 struct vcpu_vmx *vmx = to_vmx(vcpu);
338 u32 vm_exit_reason;
339 unsigned long exit_qualification = vcpu->arch.exit_qualification;
340
341 if (vmx->nested.pml_full) {
342 vm_exit_reason = EXIT_REASON_PML_FULL;
343 vmx->nested.pml_full = false;
344 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
345 } else if (fault->error_code & PFERR_RSVD_MASK)
346 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
347 else
348 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
349
350 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
351 vmcs12->guest_physical_address = fault->address;
352 }
353
nested_ept_init_mmu_context(struct kvm_vcpu * vcpu)354 static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
355 {
356 WARN_ON(mmu_is_nested(vcpu));
357
358 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
359 kvm_init_shadow_ept_mmu(vcpu,
360 to_vmx(vcpu)->nested.msrs.ept_caps &
361 VMX_EPT_EXECUTE_ONLY_BIT,
362 nested_ept_ad_enabled(vcpu),
363 nested_ept_get_eptp(vcpu));
364 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
365 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
366 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
367
368 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
369 }
370
nested_ept_uninit_mmu_context(struct kvm_vcpu * vcpu)371 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
372 {
373 vcpu->arch.mmu = &vcpu->arch.root_mmu;
374 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
375 }
376
nested_vmx_is_page_fault_vmexit(struct vmcs12 * vmcs12,u16 error_code)377 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
378 u16 error_code)
379 {
380 bool inequality, bit;
381
382 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
383 inequality =
384 (error_code & vmcs12->page_fault_error_code_mask) !=
385 vmcs12->page_fault_error_code_match;
386 return inequality ^ bit;
387 }
388
389
390 /*
391 * KVM wants to inject page-faults which it got to the guest. This function
392 * checks whether in a nested guest, we need to inject them to L1 or L2.
393 */
nested_vmx_check_exception(struct kvm_vcpu * vcpu,unsigned long * exit_qual)394 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
395 {
396 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
397 unsigned int nr = vcpu->arch.exception.nr;
398 bool has_payload = vcpu->arch.exception.has_payload;
399 unsigned long payload = vcpu->arch.exception.payload;
400
401 if (nr == PF_VECTOR) {
402 if (vcpu->arch.exception.nested_apf) {
403 *exit_qual = vcpu->arch.apf.nested_apf_token;
404 return 1;
405 }
406 if (nested_vmx_is_page_fault_vmexit(vmcs12,
407 vcpu->arch.exception.error_code)) {
408 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
409 return 1;
410 }
411 } else if (vmcs12->exception_bitmap & (1u << nr)) {
412 if (nr == DB_VECTOR) {
413 if (!has_payload) {
414 payload = vcpu->arch.dr6;
415 payload &= ~(DR6_FIXED_1 | DR6_BT);
416 payload ^= DR6_RTM;
417 }
418 *exit_qual = payload;
419 } else
420 *exit_qual = 0;
421 return 1;
422 }
423
424 return 0;
425 }
426
427
vmx_inject_page_fault_nested(struct kvm_vcpu * vcpu,struct x86_exception * fault)428 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
429 struct x86_exception *fault)
430 {
431 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
432
433 WARN_ON(!is_guest_mode(vcpu));
434
435 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
436 !to_vmx(vcpu)->nested.nested_run_pending) {
437 vmcs12->vm_exit_intr_error_code = fault->error_code;
438 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
439 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
440 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
441 fault->address);
442 } else {
443 kvm_inject_page_fault(vcpu, fault);
444 }
445 }
446
nested_vmx_check_io_bitmap_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)447 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
448 struct vmcs12 *vmcs12)
449 {
450 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
451 return 0;
452
453 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
454 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
455 return -EINVAL;
456
457 return 0;
458 }
459
nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)460 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
461 struct vmcs12 *vmcs12)
462 {
463 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
464 return 0;
465
466 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
467 return -EINVAL;
468
469 return 0;
470 }
471
nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)472 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
473 struct vmcs12 *vmcs12)
474 {
475 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
476 return 0;
477
478 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
479 return -EINVAL;
480
481 return 0;
482 }
483
484 /*
485 * Check if MSR is intercepted for L01 MSR bitmap.
486 */
msr_write_intercepted_l01(struct kvm_vcpu * vcpu,u32 msr)487 static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
488 {
489 unsigned long *msr_bitmap;
490 int f = sizeof(unsigned long);
491
492 if (!cpu_has_vmx_msr_bitmap())
493 return true;
494
495 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
496
497 if (msr <= 0x1fff) {
498 return !!test_bit(msr, msr_bitmap + 0x800 / f);
499 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
500 msr &= 0x1fff;
501 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
502 }
503
504 return true;
505 }
506
507 /*
508 * If a msr is allowed by L0, we should check whether it is allowed by L1.
509 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
510 */
nested_vmx_disable_intercept_for_msr(unsigned long * msr_bitmap_l1,unsigned long * msr_bitmap_nested,u32 msr,int type)511 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
512 unsigned long *msr_bitmap_nested,
513 u32 msr, int type)
514 {
515 int f = sizeof(unsigned long);
516
517 /*
518 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
519 * have the write-low and read-high bitmap offsets the wrong way round.
520 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
521 */
522 if (msr <= 0x1fff) {
523 if (type & MSR_TYPE_R &&
524 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
525 /* read-low */
526 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
527
528 if (type & MSR_TYPE_W &&
529 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
530 /* write-low */
531 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
532
533 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
534 msr &= 0x1fff;
535 if (type & MSR_TYPE_R &&
536 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
537 /* read-high */
538 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
539
540 if (type & MSR_TYPE_W &&
541 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
542 /* write-high */
543 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
544
545 }
546 }
547
enable_x2apic_msr_intercepts(unsigned long * msr_bitmap)548 static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
549 {
550 int msr;
551
552 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
553 unsigned word = msr / BITS_PER_LONG;
554
555 msr_bitmap[word] = ~0;
556 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
557 }
558 }
559
560 /*
561 * Merge L0's and L1's MSR bitmap, return false to indicate that
562 * we do not use the hardware.
563 */
nested_vmx_prepare_msr_bitmap(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)564 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
565 struct vmcs12 *vmcs12)
566 {
567 int msr;
568 unsigned long *msr_bitmap_l1;
569 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
570 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
571
572 /* Nothing to do if the MSR bitmap is not in use. */
573 if (!cpu_has_vmx_msr_bitmap() ||
574 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
575 return false;
576
577 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
578 return false;
579
580 msr_bitmap_l1 = (unsigned long *)map->hva;
581
582 /*
583 * To keep the control flow simple, pay eight 8-byte writes (sixteen
584 * 4-byte writes on 32-bit systems) up front to enable intercepts for
585 * the x2APIC MSR range and selectively disable them below.
586 */
587 enable_x2apic_msr_intercepts(msr_bitmap_l0);
588
589 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
590 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
591 /*
592 * L0 need not intercept reads for MSRs between 0x800
593 * and 0x8ff, it just lets the processor take the value
594 * from the virtual-APIC page; take those 256 bits
595 * directly from the L1 bitmap.
596 */
597 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
598 unsigned word = msr / BITS_PER_LONG;
599
600 msr_bitmap_l0[word] = msr_bitmap_l1[word];
601 }
602 }
603
604 nested_vmx_disable_intercept_for_msr(
605 msr_bitmap_l1, msr_bitmap_l0,
606 X2APIC_MSR(APIC_TASKPRI),
607 MSR_TYPE_R | MSR_TYPE_W);
608
609 if (nested_cpu_has_vid(vmcs12)) {
610 nested_vmx_disable_intercept_for_msr(
611 msr_bitmap_l1, msr_bitmap_l0,
612 X2APIC_MSR(APIC_EOI),
613 MSR_TYPE_W);
614 nested_vmx_disable_intercept_for_msr(
615 msr_bitmap_l1, msr_bitmap_l0,
616 X2APIC_MSR(APIC_SELF_IPI),
617 MSR_TYPE_W);
618 }
619 }
620
621 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
622 #ifdef CONFIG_X86_64
623 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
624 MSR_FS_BASE, MSR_TYPE_RW);
625
626 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
627 MSR_GS_BASE, MSR_TYPE_RW);
628
629 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
630 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
631 #endif
632
633 /*
634 * Checking the L0->L1 bitmap is trying to verify two things:
635 *
636 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
637 * ensures that we do not accidentally generate an L02 MSR bitmap
638 * from the L12 MSR bitmap that is too permissive.
639 * 2. That L1 or L2s have actually used the MSR. This avoids
640 * unnecessarily merging of the bitmap if the MSR is unused. This
641 * works properly because we only update the L01 MSR bitmap lazily.
642 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
643 * updated to reflect this when L1 (or its L2s) actually write to
644 * the MSR.
645 */
646 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
647 nested_vmx_disable_intercept_for_msr(
648 msr_bitmap_l1, msr_bitmap_l0,
649 MSR_IA32_SPEC_CTRL,
650 MSR_TYPE_R | MSR_TYPE_W);
651
652 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
653 nested_vmx_disable_intercept_for_msr(
654 msr_bitmap_l1, msr_bitmap_l0,
655 MSR_IA32_PRED_CMD,
656 MSR_TYPE_W);
657
658 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
659
660 return true;
661 }
662
nested_cache_shadow_vmcs12(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)663 static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
664 struct vmcs12 *vmcs12)
665 {
666 struct kvm_host_map map;
667 struct vmcs12 *shadow;
668
669 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
670 vmcs12->vmcs_link_pointer == -1ull)
671 return;
672
673 shadow = get_shadow_vmcs12(vcpu);
674
675 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
676 return;
677
678 memcpy(shadow, map.hva, VMCS12_SIZE);
679 kvm_vcpu_unmap(vcpu, &map, false);
680 }
681
nested_flush_cached_shadow_vmcs12(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)682 static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
683 struct vmcs12 *vmcs12)
684 {
685 struct vcpu_vmx *vmx = to_vmx(vcpu);
686
687 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
688 vmcs12->vmcs_link_pointer == -1ull)
689 return;
690
691 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
692 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
693 }
694
695 /*
696 * In nested virtualization, check if L1 has set
697 * VM_EXIT_ACK_INTR_ON_EXIT
698 */
nested_exit_intr_ack_set(struct kvm_vcpu * vcpu)699 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
700 {
701 return get_vmcs12(vcpu)->vm_exit_controls &
702 VM_EXIT_ACK_INTR_ON_EXIT;
703 }
704
nested_vmx_check_apic_access_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)705 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
706 struct vmcs12 *vmcs12)
707 {
708 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
709 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
710 return -EINVAL;
711 else
712 return 0;
713 }
714
nested_vmx_check_apicv_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)715 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
716 struct vmcs12 *vmcs12)
717 {
718 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
719 !nested_cpu_has_apic_reg_virt(vmcs12) &&
720 !nested_cpu_has_vid(vmcs12) &&
721 !nested_cpu_has_posted_intr(vmcs12))
722 return 0;
723
724 /*
725 * If virtualize x2apic mode is enabled,
726 * virtualize apic access must be disabled.
727 */
728 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
729 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
730 return -EINVAL;
731
732 /*
733 * If virtual interrupt delivery is enabled,
734 * we must exit on external interrupts.
735 */
736 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
737 return -EINVAL;
738
739 /*
740 * bits 15:8 should be zero in posted_intr_nv,
741 * the descriptor address has been already checked
742 * in nested_get_vmcs12_pages.
743 *
744 * bits 5:0 of posted_intr_desc_addr should be zero.
745 */
746 if (nested_cpu_has_posted_intr(vmcs12) &&
747 (CC(!nested_cpu_has_vid(vmcs12)) ||
748 CC(!nested_exit_intr_ack_set(vcpu)) ||
749 CC((vmcs12->posted_intr_nv & 0xff00)) ||
750 CC((vmcs12->posted_intr_desc_addr & 0x3f)) ||
751 CC((vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu)))))
752 return -EINVAL;
753
754 /* tpr shadow is needed by all apicv features. */
755 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
756 return -EINVAL;
757
758 return 0;
759 }
760
nested_vmx_check_msr_switch(struct kvm_vcpu * vcpu,u32 count,u64 addr)761 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
762 u32 count, u64 addr)
763 {
764 int maxphyaddr;
765
766 if (count == 0)
767 return 0;
768 maxphyaddr = cpuid_maxphyaddr(vcpu);
769 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
770 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr)
771 return -EINVAL;
772
773 return 0;
774 }
775
nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)776 static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
777 struct vmcs12 *vmcs12)
778 {
779 if (CC(nested_vmx_check_msr_switch(vcpu,
780 vmcs12->vm_exit_msr_load_count,
781 vmcs12->vm_exit_msr_load_addr)) ||
782 CC(nested_vmx_check_msr_switch(vcpu,
783 vmcs12->vm_exit_msr_store_count,
784 vmcs12->vm_exit_msr_store_addr)))
785 return -EINVAL;
786
787 return 0;
788 }
789
nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)790 static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
791 struct vmcs12 *vmcs12)
792 {
793 if (CC(nested_vmx_check_msr_switch(vcpu,
794 vmcs12->vm_entry_msr_load_count,
795 vmcs12->vm_entry_msr_load_addr)))
796 return -EINVAL;
797
798 return 0;
799 }
800
nested_vmx_check_pml_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)801 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
802 struct vmcs12 *vmcs12)
803 {
804 if (!nested_cpu_has_pml(vmcs12))
805 return 0;
806
807 if (CC(!nested_cpu_has_ept(vmcs12)) ||
808 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
809 return -EINVAL;
810
811 return 0;
812 }
813
nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)814 static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
815 struct vmcs12 *vmcs12)
816 {
817 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
818 !nested_cpu_has_ept(vmcs12)))
819 return -EINVAL;
820 return 0;
821 }
822
nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)823 static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
824 struct vmcs12 *vmcs12)
825 {
826 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
827 !nested_cpu_has_ept(vmcs12)))
828 return -EINVAL;
829 return 0;
830 }
831
nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)832 static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
833 struct vmcs12 *vmcs12)
834 {
835 if (!nested_cpu_has_shadow_vmcs(vmcs12))
836 return 0;
837
838 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
839 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
840 return -EINVAL;
841
842 return 0;
843 }
844
nested_vmx_msr_check_common(struct kvm_vcpu * vcpu,struct vmx_msr_entry * e)845 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
846 struct vmx_msr_entry *e)
847 {
848 /* x2APIC MSR accesses are not allowed */
849 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
850 return -EINVAL;
851 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
852 CC(e->index == MSR_IA32_UCODE_REV))
853 return -EINVAL;
854 if (CC(e->reserved != 0))
855 return -EINVAL;
856 return 0;
857 }
858
nested_vmx_load_msr_check(struct kvm_vcpu * vcpu,struct vmx_msr_entry * e)859 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
860 struct vmx_msr_entry *e)
861 {
862 if (CC(e->index == MSR_FS_BASE) ||
863 CC(e->index == MSR_GS_BASE) ||
864 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
865 nested_vmx_msr_check_common(vcpu, e))
866 return -EINVAL;
867 return 0;
868 }
869
nested_vmx_store_msr_check(struct kvm_vcpu * vcpu,struct vmx_msr_entry * e)870 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
871 struct vmx_msr_entry *e)
872 {
873 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
874 nested_vmx_msr_check_common(vcpu, e))
875 return -EINVAL;
876 return 0;
877 }
878
nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu * vcpu)879 static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
880 {
881 struct vcpu_vmx *vmx = to_vmx(vcpu);
882 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
883 vmx->nested.msrs.misc_high);
884
885 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
886 }
887
888 /*
889 * Load guest's/host's msr at nested entry/exit.
890 * return 0 for success, entry index for failure.
891 *
892 * One of the failure modes for MSR load/store is when a list exceeds the
893 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
894 * as possible, process all valid entries before failing rather than precheck
895 * for a capacity violation.
896 */
nested_vmx_load_msr(struct kvm_vcpu * vcpu,u64 gpa,u32 count)897 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
898 {
899 u32 i;
900 struct vmx_msr_entry e;
901 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
902
903 for (i = 0; i < count; i++) {
904 if (unlikely(i >= max_msr_list_size))
905 goto fail;
906
907 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
908 &e, sizeof(e))) {
909 pr_debug_ratelimited(
910 "%s cannot read MSR entry (%u, 0x%08llx)\n",
911 __func__, i, gpa + i * sizeof(e));
912 goto fail;
913 }
914 if (nested_vmx_load_msr_check(vcpu, &e)) {
915 pr_debug_ratelimited(
916 "%s check failed (%u, 0x%x, 0x%x)\n",
917 __func__, i, e.index, e.reserved);
918 goto fail;
919 }
920 if (kvm_set_msr(vcpu, e.index, e.value)) {
921 pr_debug_ratelimited(
922 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
923 __func__, i, e.index, e.value);
924 goto fail;
925 }
926 }
927 return 0;
928 fail:
929 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
930 return i + 1;
931 }
932
nested_vmx_get_vmexit_msr_value(struct kvm_vcpu * vcpu,u32 msr_index,u64 * data)933 static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
934 u32 msr_index,
935 u64 *data)
936 {
937 struct vcpu_vmx *vmx = to_vmx(vcpu);
938
939 /*
940 * If the L0 hypervisor stored a more accurate value for the TSC that
941 * does not include the time taken for emulation of the L2->L1
942 * VM-exit in L0, use the more accurate value.
943 */
944 if (msr_index == MSR_IA32_TSC) {
945 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
946 MSR_IA32_TSC);
947
948 if (i >= 0) {
949 u64 val = vmx->msr_autostore.guest.val[i].value;
950
951 *data = kvm_read_l1_tsc(vcpu, val);
952 return true;
953 }
954 }
955
956 if (kvm_get_msr(vcpu, msr_index, data)) {
957 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
958 msr_index);
959 return false;
960 }
961 return true;
962 }
963
read_and_check_msr_entry(struct kvm_vcpu * vcpu,u64 gpa,int i,struct vmx_msr_entry * e)964 static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
965 struct vmx_msr_entry *e)
966 {
967 if (kvm_vcpu_read_guest(vcpu,
968 gpa + i * sizeof(*e),
969 e, 2 * sizeof(u32))) {
970 pr_debug_ratelimited(
971 "%s cannot read MSR entry (%u, 0x%08llx)\n",
972 __func__, i, gpa + i * sizeof(*e));
973 return false;
974 }
975 if (nested_vmx_store_msr_check(vcpu, e)) {
976 pr_debug_ratelimited(
977 "%s check failed (%u, 0x%x, 0x%x)\n",
978 __func__, i, e->index, e->reserved);
979 return false;
980 }
981 return true;
982 }
983
nested_vmx_store_msr(struct kvm_vcpu * vcpu,u64 gpa,u32 count)984 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
985 {
986 u64 data;
987 u32 i;
988 struct vmx_msr_entry e;
989 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
990
991 for (i = 0; i < count; i++) {
992 if (unlikely(i >= max_msr_list_size))
993 return -EINVAL;
994
995 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
996 return -EINVAL;
997
998 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
999 return -EINVAL;
1000
1001 if (kvm_vcpu_write_guest(vcpu,
1002 gpa + i * sizeof(e) +
1003 offsetof(struct vmx_msr_entry, value),
1004 &data, sizeof(data))) {
1005 pr_debug_ratelimited(
1006 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
1007 __func__, i, e.index, data);
1008 return -EINVAL;
1009 }
1010 }
1011 return 0;
1012 }
1013
nested_msr_store_list_has_msr(struct kvm_vcpu * vcpu,u32 msr_index)1014 static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1015 {
1016 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1017 u32 count = vmcs12->vm_exit_msr_store_count;
1018 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1019 struct vmx_msr_entry e;
1020 u32 i;
1021
1022 for (i = 0; i < count; i++) {
1023 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1024 return false;
1025
1026 if (e.index == msr_index)
1027 return true;
1028 }
1029 return false;
1030 }
1031
prepare_vmx_msr_autostore_list(struct kvm_vcpu * vcpu,u32 msr_index)1032 static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1033 u32 msr_index)
1034 {
1035 struct vcpu_vmx *vmx = to_vmx(vcpu);
1036 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1037 bool in_vmcs12_store_list;
1038 int msr_autostore_slot;
1039 bool in_autostore_list;
1040 int last;
1041
1042 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1043 in_autostore_list = msr_autostore_slot >= 0;
1044 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1045
1046 if (in_vmcs12_store_list && !in_autostore_list) {
1047 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
1048 /*
1049 * Emulated VMEntry does not fail here. Instead a less
1050 * accurate value will be returned by
1051 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1052 * instead of reading the value from the vmcs02 VMExit
1053 * MSR-store area.
1054 */
1055 pr_warn_ratelimited(
1056 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1057 msr_index);
1058 return;
1059 }
1060 last = autostore->nr++;
1061 autostore->val[last].index = msr_index;
1062 } else if (!in_vmcs12_store_list && in_autostore_list) {
1063 last = --autostore->nr;
1064 autostore->val[msr_autostore_slot] = autostore->val[last];
1065 }
1066 }
1067
nested_cr3_valid(struct kvm_vcpu * vcpu,unsigned long val)1068 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
1069 {
1070 unsigned long invalid_mask;
1071
1072 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
1073 return (val & invalid_mask) == 0;
1074 }
1075
1076 /*
1077 * Returns true if the MMU needs to be sync'd on nested VM-Enter/VM-Exit.
1078 * tl;dr: the MMU needs a sync if L0 is using shadow paging and L1 didn't
1079 * enable VPID for L2 (implying it expects a TLB flush on VMX transitions).
1080 * Here's why.
1081 *
1082 * If EPT is enabled by L0 a sync is never needed:
1083 * - if it is disabled by L1, then L0 is not shadowing L1 or L2 PTEs, there
1084 * cannot be unsync'd SPTEs for either L1 or L2.
1085 *
1086 * - if it is also enabled by L1, then L0 doesn't need to sync on VM-Enter
1087 * VM-Enter as VM-Enter isn't required to invalidate guest-physical mappings
1088 * (irrespective of VPID), i.e. L1 can't rely on the (virtual) CPU to flush
1089 * stale guest-physical mappings for L2 from the TLB. And as above, L0 isn't
1090 * shadowing L1 PTEs so there are no unsync'd SPTEs to sync on VM-Exit.
1091 *
1092 * If EPT is disabled by L0:
1093 * - if VPID is enabled by L1 (for L2), the situation is similar to when L1
1094 * enables EPT: L0 doesn't need to sync as VM-Enter and VM-Exit aren't
1095 * required to invalidate linear mappings (EPT is disabled so there are
1096 * no combined or guest-physical mappings), i.e. L1 can't rely on the
1097 * (virtual) CPU to flush stale linear mappings for either L2 or itself (L1).
1098 *
1099 * - however if VPID is disabled by L1, then a sync is needed as L1 expects all
1100 * linear mappings (EPT is disabled so there are no combined or guest-physical
1101 * mappings) to be invalidated on both VM-Enter and VM-Exit.
1102 *
1103 * Note, this logic is subtly different than nested_has_guest_tlb_tag(), which
1104 * additionally checks that L2 has been assigned a VPID (when EPT is disabled).
1105 * Whether or not L2 has been assigned a VPID by L0 is irrelevant with respect
1106 * to L1's expectations, e.g. L0 needs to invalidate hardware TLB entries if L2
1107 * doesn't have a unique VPID to prevent reusing L1's entries (assuming L1 has
1108 * been assigned a VPID), but L0 doesn't need to do a MMU sync because L1
1109 * doesn't expect stale (virtual) TLB entries to be flushed, i.e. L1 doesn't
1110 * know that L0 will flush the TLB and so L1 will do INVVPID as needed to flush
1111 * stale TLB entries, at which point L0 will sync L2's MMU.
1112 */
nested_vmx_transition_mmu_sync(struct kvm_vcpu * vcpu)1113 static bool nested_vmx_transition_mmu_sync(struct kvm_vcpu *vcpu)
1114 {
1115 return !enable_ept && !nested_cpu_has_vpid(get_vmcs12(vcpu));
1116 }
1117
1118 /*
1119 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1120 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1121 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1122 * @entry_failure_code.
1123 */
nested_vmx_load_cr3(struct kvm_vcpu * vcpu,unsigned long cr3,bool nested_ept,enum vm_entry_failure_code * entry_failure_code)1124 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
1125 enum vm_entry_failure_code *entry_failure_code)
1126 {
1127 if (CC(!nested_cr3_valid(vcpu, cr3))) {
1128 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1129 return -EINVAL;
1130 }
1131
1132 /*
1133 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1134 * must not be dereferenced.
1135 */
1136 if (!nested_ept && is_pae_paging(vcpu) &&
1137 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
1138 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1139 *entry_failure_code = ENTRY_FAIL_PDPTE;
1140 return -EINVAL;
1141 }
1142 }
1143
1144 /*
1145 * Unconditionally skip the TLB flush on fast CR3 switch, all TLB
1146 * flushes are handled by nested_vmx_transition_tlb_flush().
1147 */
1148 if (!nested_ept) {
1149 kvm_mmu_new_pgd(vcpu, cr3, true, true);
1150
1151 /*
1152 * A TLB flush on VM-Enter/VM-Exit flushes all linear mappings
1153 * across all PCIDs, i.e. all PGDs need to be synchronized.
1154 * See nested_vmx_transition_mmu_sync() for more details.
1155 */
1156 if (nested_vmx_transition_mmu_sync(vcpu))
1157 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
1158 }
1159
1160 vcpu->arch.cr3 = cr3;
1161 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
1162
1163 kvm_init_mmu(vcpu, false);
1164
1165 return 0;
1166 }
1167
1168 /*
1169 * Returns if KVM is able to config CPU to tag TLB entries
1170 * populated by L2 differently than TLB entries populated
1171 * by L1.
1172 *
1173 * If L0 uses EPT, L1 and L2 run with different EPTP because
1174 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1175 * are tagged with different EPTP.
1176 *
1177 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1178 * with different VPID (L1 entries are tagged with vmx->vpid
1179 * while L2 entries are tagged with vmx->nested.vpid02).
1180 */
nested_has_guest_tlb_tag(struct kvm_vcpu * vcpu)1181 static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1182 {
1183 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1184
1185 return enable_ept ||
1186 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1187 }
1188
nested_vmx_transition_tlb_flush(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,bool is_vmenter)1189 static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1190 struct vmcs12 *vmcs12,
1191 bool is_vmenter)
1192 {
1193 struct vcpu_vmx *vmx = to_vmx(vcpu);
1194
1195 /*
1196 * If VPID is disabled, linear and combined mappings are flushed on
1197 * VM-Enter/VM-Exit, and guest-physical mappings are valid only for
1198 * their associated EPTP.
1199 */
1200 if (!enable_vpid)
1201 return;
1202
1203 /*
1204 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1205 * for *all* contexts to be flushed on VM-Enter/VM-Exit.
1206 *
1207 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1208 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
1209 * a VPID for L2, flush the current context as the effective ASID is
1210 * common to both L1 and L2.
1211 *
1212 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1213 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1214 * redundant flushes further down the nested pipeline.
1215 *
1216 * If a TLB flush isn't required due to any of the above, and vpid12 is
1217 * changing then the new "virtual" VPID (vpid12) will reuse the same
1218 * "real" VPID (vpid02), and so needs to be sync'd. There is no direct
1219 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
1220 * all nested vCPUs.
1221 */
1222 if (!nested_cpu_has_vpid(vmcs12)) {
1223 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1224 } else if (!nested_has_guest_tlb_tag(vcpu)) {
1225 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
1226 } else if (is_vmenter &&
1227 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1228 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1229 vpid_sync_context(nested_get_vpid02(vcpu));
1230 }
1231 }
1232
is_bitwise_subset(u64 superset,u64 subset,u64 mask)1233 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1234 {
1235 superset &= mask;
1236 subset &= mask;
1237
1238 return (superset | subset) == superset;
1239 }
1240
vmx_restore_vmx_basic(struct vcpu_vmx * vmx,u64 data)1241 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1242 {
1243 const u64 feature_and_reserved =
1244 /* feature (except bit 48; see below) */
1245 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1246 /* reserved */
1247 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1248 u64 vmx_basic = vmx->nested.msrs.basic;
1249
1250 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1251 return -EINVAL;
1252
1253 /*
1254 * KVM does not emulate a version of VMX that constrains physical
1255 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1256 */
1257 if (data & BIT_ULL(48))
1258 return -EINVAL;
1259
1260 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1261 vmx_basic_vmcs_revision_id(data))
1262 return -EINVAL;
1263
1264 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1265 return -EINVAL;
1266
1267 vmx->nested.msrs.basic = data;
1268 return 0;
1269 }
1270
1271 static int
vmx_restore_control_msr(struct vcpu_vmx * vmx,u32 msr_index,u64 data)1272 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1273 {
1274 u64 supported;
1275 u32 *lowp, *highp;
1276
1277 switch (msr_index) {
1278 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1279 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1280 highp = &vmx->nested.msrs.pinbased_ctls_high;
1281 break;
1282 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1283 lowp = &vmx->nested.msrs.procbased_ctls_low;
1284 highp = &vmx->nested.msrs.procbased_ctls_high;
1285 break;
1286 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1287 lowp = &vmx->nested.msrs.exit_ctls_low;
1288 highp = &vmx->nested.msrs.exit_ctls_high;
1289 break;
1290 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1291 lowp = &vmx->nested.msrs.entry_ctls_low;
1292 highp = &vmx->nested.msrs.entry_ctls_high;
1293 break;
1294 case MSR_IA32_VMX_PROCBASED_CTLS2:
1295 lowp = &vmx->nested.msrs.secondary_ctls_low;
1296 highp = &vmx->nested.msrs.secondary_ctls_high;
1297 break;
1298 default:
1299 BUG();
1300 }
1301
1302 supported = vmx_control_msr(*lowp, *highp);
1303
1304 /* Check must-be-1 bits are still 1. */
1305 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1306 return -EINVAL;
1307
1308 /* Check must-be-0 bits are still 0. */
1309 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1310 return -EINVAL;
1311
1312 *lowp = data;
1313 *highp = data >> 32;
1314 return 0;
1315 }
1316
vmx_restore_vmx_misc(struct vcpu_vmx * vmx,u64 data)1317 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1318 {
1319 const u64 feature_and_reserved_bits =
1320 /* feature */
1321 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1322 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1323 /* reserved */
1324 GENMASK_ULL(13, 9) | BIT_ULL(31);
1325 u64 vmx_misc;
1326
1327 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1328 vmx->nested.msrs.misc_high);
1329
1330 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1331 return -EINVAL;
1332
1333 if ((vmx->nested.msrs.pinbased_ctls_high &
1334 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1335 vmx_misc_preemption_timer_rate(data) !=
1336 vmx_misc_preemption_timer_rate(vmx_misc))
1337 return -EINVAL;
1338
1339 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1340 return -EINVAL;
1341
1342 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1343 return -EINVAL;
1344
1345 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1346 return -EINVAL;
1347
1348 vmx->nested.msrs.misc_low = data;
1349 vmx->nested.msrs.misc_high = data >> 32;
1350
1351 return 0;
1352 }
1353
vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx * vmx,u64 data)1354 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1355 {
1356 u64 vmx_ept_vpid_cap;
1357
1358 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1359 vmx->nested.msrs.vpid_caps);
1360
1361 /* Every bit is either reserved or a feature bit. */
1362 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1363 return -EINVAL;
1364
1365 vmx->nested.msrs.ept_caps = data;
1366 vmx->nested.msrs.vpid_caps = data >> 32;
1367 return 0;
1368 }
1369
vmx_restore_fixed0_msr(struct vcpu_vmx * vmx,u32 msr_index,u64 data)1370 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1371 {
1372 u64 *msr;
1373
1374 switch (msr_index) {
1375 case MSR_IA32_VMX_CR0_FIXED0:
1376 msr = &vmx->nested.msrs.cr0_fixed0;
1377 break;
1378 case MSR_IA32_VMX_CR4_FIXED0:
1379 msr = &vmx->nested.msrs.cr4_fixed0;
1380 break;
1381 default:
1382 BUG();
1383 }
1384
1385 /*
1386 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1387 * must be 1 in the restored value.
1388 */
1389 if (!is_bitwise_subset(data, *msr, -1ULL))
1390 return -EINVAL;
1391
1392 *msr = data;
1393 return 0;
1394 }
1395
1396 /*
1397 * Called when userspace is restoring VMX MSRs.
1398 *
1399 * Returns 0 on success, non-0 otherwise.
1400 */
vmx_set_vmx_msr(struct kvm_vcpu * vcpu,u32 msr_index,u64 data)1401 int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1402 {
1403 struct vcpu_vmx *vmx = to_vmx(vcpu);
1404
1405 /*
1406 * Don't allow changes to the VMX capability MSRs while the vCPU
1407 * is in VMX operation.
1408 */
1409 if (vmx->nested.vmxon)
1410 return -EBUSY;
1411
1412 switch (msr_index) {
1413 case MSR_IA32_VMX_BASIC:
1414 return vmx_restore_vmx_basic(vmx, data);
1415 case MSR_IA32_VMX_PINBASED_CTLS:
1416 case MSR_IA32_VMX_PROCBASED_CTLS:
1417 case MSR_IA32_VMX_EXIT_CTLS:
1418 case MSR_IA32_VMX_ENTRY_CTLS:
1419 /*
1420 * The "non-true" VMX capability MSRs are generated from the
1421 * "true" MSRs, so we do not support restoring them directly.
1422 *
1423 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1424 * should restore the "true" MSRs with the must-be-1 bits
1425 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1426 * DEFAULT SETTINGS".
1427 */
1428 return -EINVAL;
1429 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1430 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1431 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1432 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1433 case MSR_IA32_VMX_PROCBASED_CTLS2:
1434 return vmx_restore_control_msr(vmx, msr_index, data);
1435 case MSR_IA32_VMX_MISC:
1436 return vmx_restore_vmx_misc(vmx, data);
1437 case MSR_IA32_VMX_CR0_FIXED0:
1438 case MSR_IA32_VMX_CR4_FIXED0:
1439 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1440 case MSR_IA32_VMX_CR0_FIXED1:
1441 case MSR_IA32_VMX_CR4_FIXED1:
1442 /*
1443 * These MSRs are generated based on the vCPU's CPUID, so we
1444 * do not support restoring them directly.
1445 */
1446 return -EINVAL;
1447 case MSR_IA32_VMX_EPT_VPID_CAP:
1448 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1449 case MSR_IA32_VMX_VMCS_ENUM:
1450 vmx->nested.msrs.vmcs_enum = data;
1451 return 0;
1452 case MSR_IA32_VMX_VMFUNC:
1453 if (data & ~vmx->nested.msrs.vmfunc_controls)
1454 return -EINVAL;
1455 vmx->nested.msrs.vmfunc_controls = data;
1456 return 0;
1457 default:
1458 /*
1459 * The rest of the VMX capability MSRs do not support restore.
1460 */
1461 return -EINVAL;
1462 }
1463 }
1464
1465 /* Returns 0 on success, non-0 otherwise. */
vmx_get_vmx_msr(struct nested_vmx_msrs * msrs,u32 msr_index,u64 * pdata)1466 int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1467 {
1468 switch (msr_index) {
1469 case MSR_IA32_VMX_BASIC:
1470 *pdata = msrs->basic;
1471 break;
1472 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1473 case MSR_IA32_VMX_PINBASED_CTLS:
1474 *pdata = vmx_control_msr(
1475 msrs->pinbased_ctls_low,
1476 msrs->pinbased_ctls_high);
1477 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1478 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1479 break;
1480 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1481 case MSR_IA32_VMX_PROCBASED_CTLS:
1482 *pdata = vmx_control_msr(
1483 msrs->procbased_ctls_low,
1484 msrs->procbased_ctls_high);
1485 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1486 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1487 break;
1488 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1489 case MSR_IA32_VMX_EXIT_CTLS:
1490 *pdata = vmx_control_msr(
1491 msrs->exit_ctls_low,
1492 msrs->exit_ctls_high);
1493 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1494 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1495 break;
1496 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1497 case MSR_IA32_VMX_ENTRY_CTLS:
1498 *pdata = vmx_control_msr(
1499 msrs->entry_ctls_low,
1500 msrs->entry_ctls_high);
1501 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1502 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1503 break;
1504 case MSR_IA32_VMX_MISC:
1505 *pdata = vmx_control_msr(
1506 msrs->misc_low,
1507 msrs->misc_high);
1508 break;
1509 case MSR_IA32_VMX_CR0_FIXED0:
1510 *pdata = msrs->cr0_fixed0;
1511 break;
1512 case MSR_IA32_VMX_CR0_FIXED1:
1513 *pdata = msrs->cr0_fixed1;
1514 break;
1515 case MSR_IA32_VMX_CR4_FIXED0:
1516 *pdata = msrs->cr4_fixed0;
1517 break;
1518 case MSR_IA32_VMX_CR4_FIXED1:
1519 *pdata = msrs->cr4_fixed1;
1520 break;
1521 case MSR_IA32_VMX_VMCS_ENUM:
1522 *pdata = msrs->vmcs_enum;
1523 break;
1524 case MSR_IA32_VMX_PROCBASED_CTLS2:
1525 *pdata = vmx_control_msr(
1526 msrs->secondary_ctls_low,
1527 msrs->secondary_ctls_high);
1528 break;
1529 case MSR_IA32_VMX_EPT_VPID_CAP:
1530 *pdata = msrs->ept_caps |
1531 ((u64)msrs->vpid_caps << 32);
1532 break;
1533 case MSR_IA32_VMX_VMFUNC:
1534 *pdata = msrs->vmfunc_controls;
1535 break;
1536 default:
1537 return 1;
1538 }
1539
1540 return 0;
1541 }
1542
1543 /*
1544 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1545 * been modified by the L1 guest. Note, "writable" in this context means
1546 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1547 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1548 * VM-exit information fields (which are actually writable if the vCPU is
1549 * configured to support "VMWRITE to any supported field in the VMCS").
1550 */
copy_shadow_to_vmcs12(struct vcpu_vmx * vmx)1551 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1552 {
1553 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1554 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1555 struct shadow_vmcs_field field;
1556 unsigned long val;
1557 int i;
1558
1559 if (WARN_ON(!shadow_vmcs))
1560 return;
1561
1562 preempt_disable();
1563
1564 vmcs_load(shadow_vmcs);
1565
1566 for (i = 0; i < max_shadow_read_write_fields; i++) {
1567 field = shadow_read_write_fields[i];
1568 val = __vmcs_readl(field.encoding);
1569 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
1570 }
1571
1572 vmcs_clear(shadow_vmcs);
1573 vmcs_load(vmx->loaded_vmcs->vmcs);
1574
1575 preempt_enable();
1576 }
1577
copy_vmcs12_to_shadow(struct vcpu_vmx * vmx)1578 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1579 {
1580 const struct shadow_vmcs_field *fields[] = {
1581 shadow_read_write_fields,
1582 shadow_read_only_fields
1583 };
1584 const int max_fields[] = {
1585 max_shadow_read_write_fields,
1586 max_shadow_read_only_fields
1587 };
1588 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1589 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1590 struct shadow_vmcs_field field;
1591 unsigned long val;
1592 int i, q;
1593
1594 if (WARN_ON(!shadow_vmcs))
1595 return;
1596
1597 vmcs_load(shadow_vmcs);
1598
1599 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1600 for (i = 0; i < max_fields[q]; i++) {
1601 field = fields[q][i];
1602 val = vmcs12_read_any(vmcs12, field.encoding,
1603 field.offset);
1604 __vmcs_writel(field.encoding, val);
1605 }
1606 }
1607
1608 vmcs_clear(shadow_vmcs);
1609 vmcs_load(vmx->loaded_vmcs->vmcs);
1610 }
1611
copy_enlightened_to_vmcs12(struct vcpu_vmx * vmx)1612 static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
1613 {
1614 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1615 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1616
1617 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1618 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1619 vmcs12->guest_rip = evmcs->guest_rip;
1620
1621 if (unlikely(!(evmcs->hv_clean_fields &
1622 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1623 vmcs12->guest_rsp = evmcs->guest_rsp;
1624 vmcs12->guest_rflags = evmcs->guest_rflags;
1625 vmcs12->guest_interruptibility_info =
1626 evmcs->guest_interruptibility_info;
1627 }
1628
1629 if (unlikely(!(evmcs->hv_clean_fields &
1630 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1631 vmcs12->cpu_based_vm_exec_control =
1632 evmcs->cpu_based_vm_exec_control;
1633 }
1634
1635 if (unlikely(!(evmcs->hv_clean_fields &
1636 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
1637 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1638 }
1639
1640 if (unlikely(!(evmcs->hv_clean_fields &
1641 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1642 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1643 }
1644
1645 if (unlikely(!(evmcs->hv_clean_fields &
1646 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1647 vmcs12->vm_entry_intr_info_field =
1648 evmcs->vm_entry_intr_info_field;
1649 vmcs12->vm_entry_exception_error_code =
1650 evmcs->vm_entry_exception_error_code;
1651 vmcs12->vm_entry_instruction_len =
1652 evmcs->vm_entry_instruction_len;
1653 }
1654
1655 if (unlikely(!(evmcs->hv_clean_fields &
1656 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1657 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1658 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1659 vmcs12->host_cr0 = evmcs->host_cr0;
1660 vmcs12->host_cr3 = evmcs->host_cr3;
1661 vmcs12->host_cr4 = evmcs->host_cr4;
1662 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1663 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1664 vmcs12->host_rip = evmcs->host_rip;
1665 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1666 vmcs12->host_es_selector = evmcs->host_es_selector;
1667 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1668 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1669 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1670 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1671 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1672 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1673 }
1674
1675 if (unlikely(!(evmcs->hv_clean_fields &
1676 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
1677 vmcs12->pin_based_vm_exec_control =
1678 evmcs->pin_based_vm_exec_control;
1679 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1680 vmcs12->secondary_vm_exec_control =
1681 evmcs->secondary_vm_exec_control;
1682 }
1683
1684 if (unlikely(!(evmcs->hv_clean_fields &
1685 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1686 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1687 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1688 }
1689
1690 if (unlikely(!(evmcs->hv_clean_fields &
1691 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1692 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1693 }
1694
1695 if (unlikely(!(evmcs->hv_clean_fields &
1696 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1697 vmcs12->guest_es_base = evmcs->guest_es_base;
1698 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1699 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1700 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1701 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1702 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1703 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1704 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1705 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1706 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1707 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1708 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1709 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1710 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1711 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1712 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1713 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1714 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1715 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1716 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1717 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1718 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1719 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1720 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1721 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1722 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1723 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1724 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1725 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1726 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1727 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1728 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1729 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1730 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1731 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1732 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1733 }
1734
1735 if (unlikely(!(evmcs->hv_clean_fields &
1736 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1737 vmcs12->tsc_offset = evmcs->tsc_offset;
1738 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1739 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1740 }
1741
1742 if (unlikely(!(evmcs->hv_clean_fields &
1743 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1744 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1745 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1746 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1747 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1748 vmcs12->guest_cr0 = evmcs->guest_cr0;
1749 vmcs12->guest_cr3 = evmcs->guest_cr3;
1750 vmcs12->guest_cr4 = evmcs->guest_cr4;
1751 vmcs12->guest_dr7 = evmcs->guest_dr7;
1752 }
1753
1754 if (unlikely(!(evmcs->hv_clean_fields &
1755 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1756 vmcs12->host_fs_base = evmcs->host_fs_base;
1757 vmcs12->host_gs_base = evmcs->host_gs_base;
1758 vmcs12->host_tr_base = evmcs->host_tr_base;
1759 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1760 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1761 vmcs12->host_rsp = evmcs->host_rsp;
1762 }
1763
1764 if (unlikely(!(evmcs->hv_clean_fields &
1765 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1766 vmcs12->ept_pointer = evmcs->ept_pointer;
1767 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1768 }
1769
1770 if (unlikely(!(evmcs->hv_clean_fields &
1771 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1772 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1773 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1774 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1775 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1776 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1777 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1778 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1779 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1780 vmcs12->guest_pending_dbg_exceptions =
1781 evmcs->guest_pending_dbg_exceptions;
1782 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1783 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1784 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1785 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1786 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1787 }
1788
1789 /*
1790 * Not used?
1791 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1792 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1793 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
1794 * vmcs12->page_fault_error_code_mask =
1795 * evmcs->page_fault_error_code_mask;
1796 * vmcs12->page_fault_error_code_match =
1797 * evmcs->page_fault_error_code_match;
1798 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1799 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1800 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1801 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1802 */
1803
1804 /*
1805 * Read only fields:
1806 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1807 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1808 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1809 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1810 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1811 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1812 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1813 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1814 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1815 * vmcs12->exit_qualification = evmcs->exit_qualification;
1816 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1817 *
1818 * Not present in struct vmcs12:
1819 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1820 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1821 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1822 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1823 */
1824
1825 return 0;
1826 }
1827
copy_vmcs12_to_enlightened(struct vcpu_vmx * vmx)1828 static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
1829 {
1830 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1831 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1832
1833 /*
1834 * Should not be changed by KVM:
1835 *
1836 * evmcs->host_es_selector = vmcs12->host_es_selector;
1837 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1838 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1839 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1840 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1841 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1842 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1843 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1844 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1845 * evmcs->host_cr0 = vmcs12->host_cr0;
1846 * evmcs->host_cr3 = vmcs12->host_cr3;
1847 * evmcs->host_cr4 = vmcs12->host_cr4;
1848 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1849 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1850 * evmcs->host_rip = vmcs12->host_rip;
1851 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1852 * evmcs->host_fs_base = vmcs12->host_fs_base;
1853 * evmcs->host_gs_base = vmcs12->host_gs_base;
1854 * evmcs->host_tr_base = vmcs12->host_tr_base;
1855 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1856 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1857 * evmcs->host_rsp = vmcs12->host_rsp;
1858 * sync_vmcs02_to_vmcs12() doesn't read these:
1859 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1860 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1861 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1862 * evmcs->ept_pointer = vmcs12->ept_pointer;
1863 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1864 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1865 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1866 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
1867 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1868 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1869 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1870 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1871 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1872 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1873 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1874 * evmcs->page_fault_error_code_mask =
1875 * vmcs12->page_fault_error_code_mask;
1876 * evmcs->page_fault_error_code_match =
1877 * vmcs12->page_fault_error_code_match;
1878 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1879 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1880 * evmcs->tsc_offset = vmcs12->tsc_offset;
1881 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1882 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1883 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1884 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1885 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1886 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1887 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1888 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1889 *
1890 * Not present in struct vmcs12:
1891 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1892 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1893 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1894 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1895 */
1896
1897 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1898 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1899 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1900 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1901 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1902 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1903 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1904 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1905
1906 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1907 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1908 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1909 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1910 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1911 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1912 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1913 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1914 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1915 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1916
1917 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1918 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1919 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1920 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1921 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1922 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1923 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1924 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1925
1926 evmcs->guest_es_base = vmcs12->guest_es_base;
1927 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1928 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1929 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1930 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1931 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1932 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1933 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1934 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1935 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1936
1937 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1938 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1939
1940 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1941 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1942 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1943 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1944
1945 evmcs->guest_pending_dbg_exceptions =
1946 vmcs12->guest_pending_dbg_exceptions;
1947 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1948 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1949
1950 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1951 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1952
1953 evmcs->guest_cr0 = vmcs12->guest_cr0;
1954 evmcs->guest_cr3 = vmcs12->guest_cr3;
1955 evmcs->guest_cr4 = vmcs12->guest_cr4;
1956 evmcs->guest_dr7 = vmcs12->guest_dr7;
1957
1958 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1959
1960 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1961 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1962 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1963 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1964 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1965 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1966 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1967 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1968
1969 evmcs->exit_qualification = vmcs12->exit_qualification;
1970
1971 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1972 evmcs->guest_rsp = vmcs12->guest_rsp;
1973 evmcs->guest_rflags = vmcs12->guest_rflags;
1974
1975 evmcs->guest_interruptibility_info =
1976 vmcs12->guest_interruptibility_info;
1977 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1978 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1979 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1980 evmcs->vm_entry_exception_error_code =
1981 vmcs12->vm_entry_exception_error_code;
1982 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1983
1984 evmcs->guest_rip = vmcs12->guest_rip;
1985
1986 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1987
1988 return 0;
1989 }
1990
1991 /*
1992 * This is an equivalent of the nested hypervisor executing the vmptrld
1993 * instruction.
1994 */
nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu * vcpu,bool from_launch)1995 static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1996 struct kvm_vcpu *vcpu, bool from_launch)
1997 {
1998 struct vcpu_vmx *vmx = to_vmx(vcpu);
1999 bool evmcs_gpa_changed = false;
2000 u64 evmcs_gpa;
2001
2002 if (likely(!vmx->nested.enlightened_vmcs_enabled))
2003 return EVMPTRLD_DISABLED;
2004
2005 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa))
2006 return EVMPTRLD_DISABLED;
2007
2008 if (unlikely(!vmx->nested.hv_evmcs ||
2009 evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
2010 if (!vmx->nested.hv_evmcs)
2011 vmx->nested.current_vmptr = -1ull;
2012
2013 nested_release_evmcs(vcpu);
2014
2015 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
2016 &vmx->nested.hv_evmcs_map))
2017 return EVMPTRLD_ERROR;
2018
2019 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
2020
2021 /*
2022 * Currently, KVM only supports eVMCS version 1
2023 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
2024 * value to first u32 field of eVMCS which should specify eVMCS
2025 * VersionNumber.
2026 *
2027 * Guest should be aware of supported eVMCS versions by host by
2028 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
2029 * expected to set this CPUID leaf according to the value
2030 * returned in vmcs_version from nested_enable_evmcs().
2031 *
2032 * However, it turns out that Microsoft Hyper-V fails to comply
2033 * to their own invented interface: When Hyper-V use eVMCS, it
2034 * just sets first u32 field of eVMCS to revision_id specified
2035 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
2036 * which is one of the supported versions specified in
2037 * CPUID.0x4000000A.EAX[0:15].
2038 *
2039 * To overcome Hyper-V bug, we accept here either a supported
2040 * eVMCS version or VMCS12 revision_id as valid values for first
2041 * u32 field of eVMCS.
2042 */
2043 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
2044 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
2045 nested_release_evmcs(vcpu);
2046 return EVMPTRLD_VMFAIL;
2047 }
2048
2049 vmx->nested.dirty_vmcs12 = true;
2050 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
2051
2052 evmcs_gpa_changed = true;
2053 /*
2054 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2055 * reloaded from guest's memory (read only fields, fields not
2056 * present in struct hv_enlightened_vmcs, ...). Make sure there
2057 * are no leftovers.
2058 */
2059 if (from_launch) {
2060 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2061 memset(vmcs12, 0, sizeof(*vmcs12));
2062 vmcs12->hdr.revision_id = VMCS12_REVISION;
2063 }
2064
2065 }
2066
2067 /*
2068 * Clean fields data can't be used on VMLAUNCH and when we switch
2069 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2070 */
2071 if (from_launch || evmcs_gpa_changed)
2072 vmx->nested.hv_evmcs->hv_clean_fields &=
2073 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2074
2075 return EVMPTRLD_SUCCEEDED;
2076 }
2077
nested_sync_vmcs12_to_shadow(struct kvm_vcpu * vcpu)2078 void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
2079 {
2080 struct vcpu_vmx *vmx = to_vmx(vcpu);
2081
2082 if (vmx->nested.hv_evmcs) {
2083 copy_vmcs12_to_enlightened(vmx);
2084 /* All fields are clean */
2085 vmx->nested.hv_evmcs->hv_clean_fields |=
2086 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2087 } else {
2088 copy_vmcs12_to_shadow(vmx);
2089 }
2090
2091 vmx->nested.need_vmcs12_to_shadow_sync = false;
2092 }
2093
vmx_preemption_timer_fn(struct hrtimer * timer)2094 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2095 {
2096 struct vcpu_vmx *vmx =
2097 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2098
2099 vmx->nested.preemption_timer_expired = true;
2100 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2101 kvm_vcpu_kick(&vmx->vcpu);
2102
2103 return HRTIMER_NORESTART;
2104 }
2105
vmx_calc_preemption_timer_value(struct kvm_vcpu * vcpu)2106 static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
2107 {
2108 struct vcpu_vmx *vmx = to_vmx(vcpu);
2109 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2110
2111 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2112 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2113
2114 if (!vmx->nested.has_preemption_timer_deadline) {
2115 vmx->nested.preemption_timer_deadline =
2116 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
2117 vmx->nested.has_preemption_timer_deadline = true;
2118 }
2119 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
2120 }
2121
vmx_start_preemption_timer(struct kvm_vcpu * vcpu,u64 preemption_timeout)2122 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2123 u64 preemption_timeout)
2124 {
2125 struct vcpu_vmx *vmx = to_vmx(vcpu);
2126
2127 /*
2128 * A timer value of zero is architecturally guaranteed to cause
2129 * a VMExit prior to executing any instructions in the guest.
2130 */
2131 if (preemption_timeout == 0) {
2132 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2133 return;
2134 }
2135
2136 if (vcpu->arch.virtual_tsc_khz == 0)
2137 return;
2138
2139 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2140 preemption_timeout *= 1000000;
2141 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2142 hrtimer_start(&vmx->nested.preemption_timer,
2143 ktime_add_ns(ktime_get(), preemption_timeout),
2144 HRTIMER_MODE_ABS_PINNED);
2145 }
2146
nested_vmx_calc_efer(struct vcpu_vmx * vmx,struct vmcs12 * vmcs12)2147 static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2148 {
2149 if (vmx->nested.nested_run_pending &&
2150 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2151 return vmcs12->guest_ia32_efer;
2152 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2153 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2154 else
2155 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2156 }
2157
prepare_vmcs02_constant_state(struct vcpu_vmx * vmx)2158 static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2159 {
2160 /*
2161 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2162 * according to L0's settings (vmcs12 is irrelevant here). Host
2163 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2164 * will be set as needed prior to VMLAUNCH/VMRESUME.
2165 */
2166 if (vmx->nested.vmcs02_initialized)
2167 return;
2168 vmx->nested.vmcs02_initialized = true;
2169
2170 /*
2171 * We don't care what the EPTP value is we just need to guarantee
2172 * it's valid so we don't get a false positive when doing early
2173 * consistency checks.
2174 */
2175 if (enable_ept && nested_early_check)
2176 vmcs_write64(EPT_POINTER,
2177 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
2178
2179 /* All VMFUNCs are currently emulated through L0 vmexits. */
2180 if (cpu_has_vmx_vmfunc())
2181 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2182
2183 if (cpu_has_vmx_posted_intr())
2184 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2185
2186 if (cpu_has_vmx_msr_bitmap())
2187 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2188
2189 /*
2190 * The PML address never changes, so it is constant in vmcs02.
2191 * Conceptually we want to copy the PML index from vmcs01 here,
2192 * and then back to vmcs01 on nested vmexit. But since we flush
2193 * the log and reset GUEST_PML_INDEX on each vmexit, the PML
2194 * index is also effectively constant in vmcs02.
2195 */
2196 if (enable_pml) {
2197 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
2198 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
2199 }
2200
2201 if (cpu_has_vmx_encls_vmexit())
2202 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
2203
2204 /*
2205 * Set the MSR load/store lists to match L0's settings. Only the
2206 * addresses are constant (for vmcs02), the counts can change based
2207 * on L2's behavior, e.g. switching to/from long mode.
2208 */
2209 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
2210 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2211 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2212
2213 vmx_set_constant_host_state(vmx);
2214 }
2215
prepare_vmcs02_early_rare(struct vcpu_vmx * vmx,struct vmcs12 * vmcs12)2216 static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
2217 struct vmcs12 *vmcs12)
2218 {
2219 prepare_vmcs02_constant_state(vmx);
2220
2221 vmcs_write64(VMCS_LINK_POINTER, -1ull);
2222
2223 if (enable_vpid) {
2224 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2225 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2226 else
2227 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2228 }
2229 }
2230
prepare_vmcs02_early(struct vcpu_vmx * vmx,struct vmcs12 * vmcs12)2231 static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2232 {
2233 u32 exec_control, vmcs12_exec_ctrl;
2234 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2235
2236 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
2237 prepare_vmcs02_early_rare(vmx, vmcs12);
2238
2239 /*
2240 * PIN CONTROLS
2241 */
2242 exec_control = vmx_pin_based_exec_ctrl(vmx);
2243 exec_control |= (vmcs12->pin_based_vm_exec_control &
2244 ~PIN_BASED_VMX_PREEMPTION_TIMER);
2245
2246 /* Posted interrupts setting is only taken from vmcs12. */
2247 vmx->nested.pi_pending = false;
2248 if (nested_cpu_has_posted_intr(vmcs12))
2249 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2250 else
2251 exec_control &= ~PIN_BASED_POSTED_INTR;
2252 pin_controls_set(vmx, exec_control);
2253
2254 /*
2255 * EXEC CONTROLS
2256 */
2257 exec_control = vmx_exec_control(vmx); /* L0's desires */
2258 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
2259 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
2260 exec_control &= ~CPU_BASED_TPR_SHADOW;
2261 exec_control |= vmcs12->cpu_based_vm_exec_control;
2262
2263 vmx->nested.l1_tpr_threshold = -1;
2264 if (exec_control & CPU_BASED_TPR_SHADOW)
2265 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
2266 #ifdef CONFIG_X86_64
2267 else
2268 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2269 CPU_BASED_CR8_STORE_EXITING;
2270 #endif
2271
2272 /*
2273 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2274 * for I/O port accesses.
2275 */
2276 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
2277 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2278
2279 /*
2280 * This bit will be computed in nested_get_vmcs12_pages, because
2281 * we do not have access to L1's MSR bitmap yet. For now, keep
2282 * the same bit as before, hoping to avoid multiple VMWRITEs that
2283 * only set/clear this bit.
2284 */
2285 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2286 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2287
2288 exec_controls_set(vmx, exec_control);
2289
2290 /*
2291 * SECONDARY EXEC CONTROLS
2292 */
2293 if (cpu_has_secondary_exec_ctrls()) {
2294 exec_control = vmx->secondary_exec_control;
2295
2296 /* Take the following fields only from vmcs12 */
2297 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2298 SECONDARY_EXEC_ENABLE_INVPCID |
2299 SECONDARY_EXEC_ENABLE_RDTSCP |
2300 SECONDARY_EXEC_XSAVES |
2301 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
2302 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2303 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2304 SECONDARY_EXEC_ENABLE_VMFUNC);
2305 if (nested_cpu_has(vmcs12,
2306 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
2307 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
2308 ~SECONDARY_EXEC_ENABLE_PML;
2309 exec_control |= vmcs12_exec_ctrl;
2310 }
2311
2312 /* VMCS shadowing for L2 is emulated for now */
2313 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2314
2315 /*
2316 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2317 * will not have to rewrite the controls just for this bit.
2318 */
2319 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2320 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2321 exec_control |= SECONDARY_EXEC_DESC;
2322
2323 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2324 vmcs_write16(GUEST_INTR_STATUS,
2325 vmcs12->guest_intr_status);
2326
2327 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2328 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2329
2330 secondary_exec_controls_set(vmx, exec_control);
2331 }
2332
2333 /*
2334 * ENTRY CONTROLS
2335 *
2336 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2337 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2338 * on the related bits (if supported by the CPU) in the hope that
2339 * we can avoid VMWrites during vmx_set_efer().
2340 */
2341 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2342 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2343 if (cpu_has_load_ia32_efer()) {
2344 if (guest_efer & EFER_LMA)
2345 exec_control |= VM_ENTRY_IA32E_MODE;
2346 if (guest_efer != host_efer)
2347 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2348 }
2349 vm_entry_controls_set(vmx, exec_control);
2350
2351 /*
2352 * EXIT CONTROLS
2353 *
2354 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2355 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2356 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2357 */
2358 exec_control = vmx_vmexit_ctrl();
2359 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2360 exec_control |= VM_EXIT_LOAD_IA32_EFER;
2361 vm_exit_controls_set(vmx, exec_control);
2362
2363 /*
2364 * Interrupt/Exception Fields
2365 */
2366 if (vmx->nested.nested_run_pending) {
2367 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2368 vmcs12->vm_entry_intr_info_field);
2369 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2370 vmcs12->vm_entry_exception_error_code);
2371 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2372 vmcs12->vm_entry_instruction_len);
2373 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2374 vmcs12->guest_interruptibility_info);
2375 vmx->loaded_vmcs->nmi_known_unmasked =
2376 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2377 } else {
2378 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2379 }
2380 }
2381
prepare_vmcs02_rare(struct vcpu_vmx * vmx,struct vmcs12 * vmcs12)2382 static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2383 {
2384 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2385
2386 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2387 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2388 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2389 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2390 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2391 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2392 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2393 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2394 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2395 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2396 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2397 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2398 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2399 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2400 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2401 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2402 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2403 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2404 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2405 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
2406 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2407 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
2408 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2409 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2410 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2411 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2412 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2413 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2414 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2415 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2416 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2417 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2418 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2419 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2420 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2421 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2422 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2423 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
2424
2425 vmx->segment_cache.bitmask = 0;
2426 }
2427
2428 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2429 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2430 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2431 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2432 vmcs12->guest_pending_dbg_exceptions);
2433 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2434 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2435
2436 /*
2437 * L1 may access the L2's PDPTR, so save them to construct
2438 * vmcs12
2439 */
2440 if (enable_ept) {
2441 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2442 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2443 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2444 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2445 }
2446
2447 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2448 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2449 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
2450 }
2451
2452 if (nested_cpu_has_xsaves(vmcs12))
2453 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2454
2455 /*
2456 * Whether page-faults are trapped is determined by a combination of
2457 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2458 * doesn't care about page faults then we should set all of these to
2459 * L1's desires. However, if L0 does care about (some) page faults, it
2460 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2461 * simply ask to exit on each and every L2 page fault. This is done by
2462 * setting MASK=MATCH=0 and (see below) EB.PF=1.
2463 * Note that below we don't need special code to set EB.PF beyond the
2464 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2465 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2466 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2467 */
2468 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2469 /*
2470 * TODO: if both L0 and L1 need the same MASK and MATCH,
2471 * go ahead and use it?
2472 */
2473 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2474 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2475 } else {
2476 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2477 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2478 }
2479
2480 if (cpu_has_vmx_apicv()) {
2481 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2482 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2483 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2484 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2485 }
2486
2487 /*
2488 * Make sure the msr_autostore list is up to date before we set the
2489 * count in the vmcs02.
2490 */
2491 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2492
2493 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
2494 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2495 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2496
2497 set_cr4_guest_host_mask(vmx);
2498 }
2499
2500 /*
2501 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2502 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2503 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2504 * guest in a way that will both be appropriate to L1's requests, and our
2505 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2506 * function also has additional necessary side-effects, like setting various
2507 * vcpu->arch fields.
2508 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2509 * is assigned to entry_failure_code on failure.
2510 */
prepare_vmcs02(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,enum vm_entry_failure_code * entry_failure_code)2511 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
2512 enum vm_entry_failure_code *entry_failure_code)
2513 {
2514 struct vcpu_vmx *vmx = to_vmx(vcpu);
2515 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2516 bool load_guest_pdptrs_vmcs12 = false;
2517
2518 if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
2519 prepare_vmcs02_rare(vmx, vmcs12);
2520 vmx->nested.dirty_vmcs12 = false;
2521
2522 load_guest_pdptrs_vmcs12 = !hv_evmcs ||
2523 !(hv_evmcs->hv_clean_fields &
2524 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
2525 }
2526
2527 if (vmx->nested.nested_run_pending &&
2528 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2529 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2530 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2531 } else {
2532 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2533 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2534 }
2535 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2536 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2537 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
2538 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2539
2540 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2541 * bitwise-or of what L1 wants to trap for L2, and what we want to
2542 * trap. Note that CR0.TS also needs updating - we do this later.
2543 */
2544 update_exception_bitmap(vcpu);
2545 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2546 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2547
2548 if (vmx->nested.nested_run_pending &&
2549 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2550 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2551 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2552 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2553 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2554 }
2555
2556 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
2557
2558 if (kvm_has_tsc_control)
2559 decache_tsc_multiplier(vmx);
2560
2561 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
2562
2563 if (nested_cpu_has_ept(vmcs12))
2564 nested_ept_init_mmu_context(vcpu);
2565
2566 /*
2567 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2568 * bits which we consider mandatory enabled.
2569 * The CR0_READ_SHADOW is what L2 should have expected to read given
2570 * the specifications by L1; It's not enough to take
2571 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2572 * have more bits than L1 expected.
2573 */
2574 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2575 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2576
2577 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2578 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2579
2580 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2581 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2582 vmx_set_efer(vcpu, vcpu->arch.efer);
2583
2584 /*
2585 * Guest state is invalid and unrestricted guest is disabled,
2586 * which means L1 attempted VMEntry to L2 with invalid state.
2587 * Fail the VMEntry.
2588 */
2589 if (CC(!vmx_guest_state_valid(vcpu))) {
2590 *entry_failure_code = ENTRY_FAIL_DEFAULT;
2591 return -EINVAL;
2592 }
2593
2594 /* Shadow page tables on either EPT or shadow page tables. */
2595 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
2596 entry_failure_code))
2597 return -EINVAL;
2598
2599 /*
2600 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2601 * on nested VM-Exit, which can occur without actually running L2 and
2602 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
2603 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2604 * transition to HLT instead of running L2.
2605 */
2606 if (enable_ept)
2607 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2608
2609 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2610 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2611 is_pae_paging(vcpu)) {
2612 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2613 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2614 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2615 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2616 }
2617
2618 if (!enable_ept)
2619 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2620
2621 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2622 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2623 vmcs12->guest_ia32_perf_global_ctrl)))
2624 return -EINVAL;
2625
2626 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2627 kvm_rip_write(vcpu, vmcs12->guest_rip);
2628 return 0;
2629 }
2630
nested_vmx_check_nmi_controls(struct vmcs12 * vmcs12)2631 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2632 {
2633 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2634 nested_cpu_has_virtual_nmis(vmcs12)))
2635 return -EINVAL;
2636
2637 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
2638 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
2639 return -EINVAL;
2640
2641 return 0;
2642 }
2643
nested_vmx_check_eptp(struct kvm_vcpu * vcpu,u64 new_eptp)2644 static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
2645 {
2646 struct vcpu_vmx *vmx = to_vmx(vcpu);
2647 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2648
2649 /* Check for memory type validity */
2650 switch (new_eptp & VMX_EPTP_MT_MASK) {
2651 case VMX_EPTP_MT_UC:
2652 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
2653 return false;
2654 break;
2655 case VMX_EPTP_MT_WB:
2656 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
2657 return false;
2658 break;
2659 default:
2660 return false;
2661 }
2662
2663 /* Page-walk levels validity. */
2664 switch (new_eptp & VMX_EPTP_PWL_MASK) {
2665 case VMX_EPTP_PWL_5:
2666 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2667 return false;
2668 break;
2669 case VMX_EPTP_PWL_4:
2670 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2671 return false;
2672 break;
2673 default:
2674 return false;
2675 }
2676
2677 /* Reserved bits should not be set */
2678 if (CC(new_eptp >> maxphyaddr || ((new_eptp >> 7) & 0x1f)))
2679 return false;
2680
2681 /* AD, if set, should be supported */
2682 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
2683 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
2684 return false;
2685 }
2686
2687 return true;
2688 }
2689
2690 /*
2691 * Checks related to VM-Execution Control Fields
2692 */
nested_check_vm_execution_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)2693 static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2694 struct vmcs12 *vmcs12)
2695 {
2696 struct vcpu_vmx *vmx = to_vmx(vcpu);
2697
2698 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2699 vmx->nested.msrs.pinbased_ctls_low,
2700 vmx->nested.msrs.pinbased_ctls_high)) ||
2701 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2702 vmx->nested.msrs.procbased_ctls_low,
2703 vmx->nested.msrs.procbased_ctls_high)))
2704 return -EINVAL;
2705
2706 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
2707 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2708 vmx->nested.msrs.secondary_ctls_low,
2709 vmx->nested.msrs.secondary_ctls_high)))
2710 return -EINVAL;
2711
2712 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
2713 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2714 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2715 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2716 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2717 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2718 nested_vmx_check_nmi_controls(vmcs12) ||
2719 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2720 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2721 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2722 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
2723 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
2724 return -EINVAL;
2725
2726 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2727 nested_cpu_has_save_preemption_timer(vmcs12))
2728 return -EINVAL;
2729
2730 if (nested_cpu_has_ept(vmcs12) &&
2731 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
2732 return -EINVAL;
2733
2734 if (nested_cpu_has_vmfunc(vmcs12)) {
2735 if (CC(vmcs12->vm_function_control &
2736 ~vmx->nested.msrs.vmfunc_controls))
2737 return -EINVAL;
2738
2739 if (nested_cpu_has_eptp_switching(vmcs12)) {
2740 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2741 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
2742 return -EINVAL;
2743 }
2744 }
2745
2746 return 0;
2747 }
2748
2749 /*
2750 * Checks related to VM-Exit Control Fields
2751 */
nested_check_vm_exit_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)2752 static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2753 struct vmcs12 *vmcs12)
2754 {
2755 struct vcpu_vmx *vmx = to_vmx(vcpu);
2756
2757 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2758 vmx->nested.msrs.exit_ctls_low,
2759 vmx->nested.msrs.exit_ctls_high)) ||
2760 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
2761 return -EINVAL;
2762
2763 return 0;
2764 }
2765
2766 /*
2767 * Checks related to VM-Entry Control Fields
2768 */
nested_check_vm_entry_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)2769 static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2770 struct vmcs12 *vmcs12)
2771 {
2772 struct vcpu_vmx *vmx = to_vmx(vcpu);
2773
2774 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2775 vmx->nested.msrs.entry_ctls_low,
2776 vmx->nested.msrs.entry_ctls_high)))
2777 return -EINVAL;
2778
2779 /*
2780 * From the Intel SDM, volume 3:
2781 * Fields relevant to VM-entry event injection must be set properly.
2782 * These fields are the VM-entry interruption-information field, the
2783 * VM-entry exception error code, and the VM-entry instruction length.
2784 */
2785 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2786 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2787 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2788 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2789 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2790 bool should_have_error_code;
2791 bool urg = nested_cpu_has2(vmcs12,
2792 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2793 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2794
2795 /* VM-entry interruption-info field: interruption type */
2796 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2797 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2798 !nested_cpu_supports_monitor_trap_flag(vcpu)))
2799 return -EINVAL;
2800
2801 /* VM-entry interruption-info field: vector */
2802 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2803 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2804 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
2805 return -EINVAL;
2806
2807 /* VM-entry interruption-info field: deliver error code */
2808 should_have_error_code =
2809 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2810 x86_exception_has_error_code(vector);
2811 if (CC(has_error_code != should_have_error_code))
2812 return -EINVAL;
2813
2814 /* VM-entry exception error code */
2815 if (CC(has_error_code &&
2816 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
2817 return -EINVAL;
2818
2819 /* VM-entry interruption-info field: reserved bits */
2820 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
2821 return -EINVAL;
2822
2823 /* VM-entry instruction length */
2824 switch (intr_type) {
2825 case INTR_TYPE_SOFT_EXCEPTION:
2826 case INTR_TYPE_SOFT_INTR:
2827 case INTR_TYPE_PRIV_SW_EXCEPTION:
2828 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2829 CC(vmcs12->vm_entry_instruction_len == 0 &&
2830 CC(!nested_cpu_has_zero_length_injection(vcpu))))
2831 return -EINVAL;
2832 }
2833 }
2834
2835 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2836 return -EINVAL;
2837
2838 return 0;
2839 }
2840
nested_vmx_check_controls(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)2841 static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2842 struct vmcs12 *vmcs12)
2843 {
2844 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2845 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2846 nested_check_vm_entry_controls(vcpu, vmcs12))
2847 return -EINVAL;
2848
2849 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2850 return nested_evmcs_check_controls(vmcs12);
2851
2852 return 0;
2853 }
2854
nested_vmx_check_host_state(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)2855 static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2856 struct vmcs12 *vmcs12)
2857 {
2858 bool ia32e;
2859
2860 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2861 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
2862 CC(!nested_cr3_valid(vcpu, vmcs12->host_cr3)))
2863 return -EINVAL;
2864
2865 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2866 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
2867 return -EINVAL;
2868
2869 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
2870 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
2871 return -EINVAL;
2872
2873 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2874 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2875 vmcs12->host_ia32_perf_global_ctrl)))
2876 return -EINVAL;
2877
2878 #ifdef CONFIG_X86_64
2879 ia32e = !!(vcpu->arch.efer & EFER_LMA);
2880 #else
2881 ia32e = false;
2882 #endif
2883
2884 if (ia32e) {
2885 if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) ||
2886 CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2887 return -EINVAL;
2888 } else {
2889 if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) ||
2890 CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2891 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2892 CC((vmcs12->host_rip) >> 32))
2893 return -EINVAL;
2894 }
2895
2896 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2897 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2898 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2899 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2900 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2901 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2902 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2903 CC(vmcs12->host_cs_selector == 0) ||
2904 CC(vmcs12->host_tr_selector == 0) ||
2905 CC(vmcs12->host_ss_selector == 0 && !ia32e))
2906 return -EINVAL;
2907
2908 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2909 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2910 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2911 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
2912 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2913 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
2914 return -EINVAL;
2915
2916 /*
2917 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2918 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2919 * the values of the LMA and LME bits in the field must each be that of
2920 * the host address-space size VM-exit control.
2921 */
2922 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
2923 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2924 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2925 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
2926 return -EINVAL;
2927 }
2928
2929 return 0;
2930 }
2931
nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)2932 static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2933 struct vmcs12 *vmcs12)
2934 {
2935 int r = 0;
2936 struct vmcs12 *shadow;
2937 struct kvm_host_map map;
2938
2939 if (vmcs12->vmcs_link_pointer == -1ull)
2940 return 0;
2941
2942 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
2943 return -EINVAL;
2944
2945 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
2946 return -EINVAL;
2947
2948 shadow = map.hva;
2949
2950 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2951 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
2952 r = -EINVAL;
2953
2954 kvm_vcpu_unmap(vcpu, &map, false);
2955 return r;
2956 }
2957
2958 /*
2959 * Checks related to Guest Non-register State
2960 */
nested_check_guest_non_reg_state(struct vmcs12 * vmcs12)2961 static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2962 {
2963 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
2964 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT))
2965 return -EINVAL;
2966
2967 return 0;
2968 }
2969
nested_vmx_check_guest_state(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,enum vm_entry_failure_code * entry_failure_code)2970 static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2971 struct vmcs12 *vmcs12,
2972 enum vm_entry_failure_code *entry_failure_code)
2973 {
2974 bool ia32e;
2975
2976 *entry_failure_code = ENTRY_FAIL_DEFAULT;
2977
2978 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2979 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
2980 return -EINVAL;
2981
2982 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2983 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2984 return -EINVAL;
2985
2986 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
2987 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
2988 return -EINVAL;
2989
2990 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
2991 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
2992 return -EINVAL;
2993 }
2994
2995 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2996 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2997 vmcs12->guest_ia32_perf_global_ctrl)))
2998 return -EINVAL;
2999
3000 /*
3001 * If the load IA32_EFER VM-entry control is 1, the following checks
3002 * are performed on the field for the IA32_EFER MSR:
3003 * - Bits reserved in the IA32_EFER MSR must be 0.
3004 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
3005 * the IA-32e mode guest VM-exit control. It must also be identical
3006 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
3007 * CR0.PG) is 1.
3008 */
3009 if (to_vmx(vcpu)->nested.nested_run_pending &&
3010 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
3011 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
3012 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
3013 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
3014 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
3015 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
3016 return -EINVAL;
3017 }
3018
3019 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
3020 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
3021 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
3022 return -EINVAL;
3023
3024 if (nested_check_guest_non_reg_state(vmcs12))
3025 return -EINVAL;
3026
3027 return 0;
3028 }
3029
nested_vmx_check_vmentry_hw(struct kvm_vcpu * vcpu)3030 static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
3031 {
3032 struct vcpu_vmx *vmx = to_vmx(vcpu);
3033 unsigned long cr3, cr4;
3034 bool vm_fail;
3035
3036 if (!nested_early_check)
3037 return 0;
3038
3039 if (vmx->msr_autoload.host.nr)
3040 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3041 if (vmx->msr_autoload.guest.nr)
3042 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3043
3044 preempt_disable();
3045
3046 vmx_prepare_switch_to_guest(vcpu);
3047
3048 /*
3049 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3050 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
3051 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
3052 * there is no need to preserve other bits or save/restore the field.
3053 */
3054 vmcs_writel(GUEST_RFLAGS, 0);
3055
3056 cr3 = __get_current_cr3_fast();
3057 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3058 vmcs_writel(HOST_CR3, cr3);
3059 vmx->loaded_vmcs->host_state.cr3 = cr3;
3060 }
3061
3062 cr4 = cr4_read_shadow();
3063 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3064 vmcs_writel(HOST_CR4, cr4);
3065 vmx->loaded_vmcs->host_state.cr4 = cr4;
3066 }
3067
3068 vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
3069 __vmx_vcpu_run_flags(vmx));
3070
3071 if (vmx->msr_autoload.host.nr)
3072 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3073 if (vmx->msr_autoload.guest.nr)
3074 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3075
3076 if (vm_fail) {
3077 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3078
3079 preempt_enable();
3080
3081 trace_kvm_nested_vmenter_failed(
3082 "early hardware check VM-instruction error: ", error);
3083 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3084 return 1;
3085 }
3086
3087 /*
3088 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3089 */
3090 if (hw_breakpoint_active())
3091 set_debugreg(__this_cpu_read(cpu_dr7), 7);
3092 local_irq_enable();
3093 preempt_enable();
3094
3095 /*
3096 * A non-failing VMEntry means we somehow entered guest mode with
3097 * an illegal RIP, and that's just the tip of the iceberg. There
3098 * is no telling what memory has been modified or what state has
3099 * been exposed to unknown code. Hitting this all but guarantees
3100 * a (very critical) hardware issue.
3101 */
3102 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3103 VMX_EXIT_REASONS_FAILED_VMENTRY));
3104
3105 return 0;
3106 }
3107
nested_get_evmcs_page(struct kvm_vcpu * vcpu)3108 static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
3109 {
3110 struct vcpu_vmx *vmx = to_vmx(vcpu);
3111
3112 /*
3113 * hv_evmcs may end up being not mapped after migration (when
3114 * L2 was running), map it here to make sure vmcs12 changes are
3115 * properly reflected.
3116 */
3117 if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs) {
3118 enum nested_evmptrld_status evmptrld_status =
3119 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3120
3121 if (evmptrld_status == EVMPTRLD_VMFAIL ||
3122 evmptrld_status == EVMPTRLD_ERROR)
3123 return false;
3124 }
3125
3126 return true;
3127 }
3128
nested_get_vmcs12_pages(struct kvm_vcpu * vcpu)3129 static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3130 {
3131 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3132 struct vcpu_vmx *vmx = to_vmx(vcpu);
3133 struct kvm_host_map *map;
3134 struct page *page;
3135 u64 hpa;
3136
3137 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3138 /*
3139 * Translate L1 physical address to host physical
3140 * address for vmcs02. Keep the page pinned, so this
3141 * physical address remains valid. We keep a reference
3142 * to it so we can release it later.
3143 */
3144 if (vmx->nested.apic_access_page) { /* shouldn't happen */
3145 kvm_release_page_clean(vmx->nested.apic_access_page);
3146 vmx->nested.apic_access_page = NULL;
3147 }
3148 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
3149 if (!is_error_page(page)) {
3150 vmx->nested.apic_access_page = page;
3151 hpa = page_to_phys(vmx->nested.apic_access_page);
3152 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3153 } else {
3154 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3155 __func__);
3156 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3157 vcpu->run->internal.suberror =
3158 KVM_INTERNAL_ERROR_EMULATION;
3159 vcpu->run->internal.ndata = 0;
3160 return false;
3161 }
3162 }
3163
3164 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3165 map = &vmx->nested.virtual_apic_map;
3166
3167 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3168 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
3169 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3170 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3171 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3172 /*
3173 * The processor will never use the TPR shadow, simply
3174 * clear the bit from the execution control. Such a
3175 * configuration is useless, but it happens in tests.
3176 * For any other configuration, failing the vm entry is
3177 * _not_ what the processor does but it's basically the
3178 * only possibility we have.
3179 */
3180 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
3181 } else {
3182 /*
3183 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3184 * force VM-Entry to fail.
3185 */
3186 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
3187 }
3188 }
3189
3190 if (nested_cpu_has_posted_intr(vmcs12)) {
3191 map = &vmx->nested.pi_desc_map;
3192
3193 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3194 vmx->nested.pi_desc =
3195 (struct pi_desc *)(((void *)map->hva) +
3196 offset_in_page(vmcs12->posted_intr_desc_addr));
3197 vmcs_write64(POSTED_INTR_DESC_ADDR,
3198 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
3199 }
3200 }
3201 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
3202 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
3203 else
3204 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
3205
3206 return true;
3207 }
3208
vmx_get_nested_state_pages(struct kvm_vcpu * vcpu)3209 static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3210 {
3211 if (!nested_get_evmcs_page(vcpu)) {
3212 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3213 __func__);
3214 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3215 vcpu->run->internal.suberror =
3216 KVM_INTERNAL_ERROR_EMULATION;
3217 vcpu->run->internal.ndata = 0;
3218
3219 return false;
3220 }
3221
3222 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3223 return false;
3224
3225 return true;
3226 }
3227
nested_vmx_write_pml_buffer(struct kvm_vcpu * vcpu,gpa_t gpa)3228 static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3229 {
3230 struct vmcs12 *vmcs12;
3231 struct vcpu_vmx *vmx = to_vmx(vcpu);
3232 gpa_t dst;
3233
3234 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3235 return 0;
3236
3237 if (WARN_ON_ONCE(vmx->nested.pml_full))
3238 return 1;
3239
3240 /*
3241 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3242 * set is already checked as part of A/D emulation.
3243 */
3244 vmcs12 = get_vmcs12(vcpu);
3245 if (!nested_cpu_has_pml(vmcs12))
3246 return 0;
3247
3248 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3249 vmx->nested.pml_full = true;
3250 return 1;
3251 }
3252
3253 gpa &= ~0xFFFull;
3254 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3255
3256 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3257 offset_in_page(dst), sizeof(gpa)))
3258 return 0;
3259
3260 vmcs12->guest_pml_index--;
3261
3262 return 0;
3263 }
3264
3265 /*
3266 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3267 * for running VMX instructions (except VMXON, whose prerequisites are
3268 * slightly different). It also specifies what exception to inject otherwise.
3269 * Note that many of these exceptions have priority over VM exits, so they
3270 * don't have to be checked again here.
3271 */
nested_vmx_check_permission(struct kvm_vcpu * vcpu)3272 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3273 {
3274 if (!to_vmx(vcpu)->nested.vmxon) {
3275 kvm_queue_exception(vcpu, UD_VECTOR);
3276 return 0;
3277 }
3278
3279 if (vmx_get_cpl(vcpu)) {
3280 kvm_inject_gp(vcpu, 0);
3281 return 0;
3282 }
3283
3284 return 1;
3285 }
3286
vmx_has_apicv_interrupt(struct kvm_vcpu * vcpu)3287 static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3288 {
3289 u8 rvi = vmx_get_rvi();
3290 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3291
3292 return ((rvi & 0xf0) > (vppr & 0xf0));
3293 }
3294
3295 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3296 struct vmcs12 *vmcs12);
3297
3298 /*
3299 * If from_vmentry is false, this is being called from state restore (either RSM
3300 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
3301 *
3302 * Returns:
3303 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3304 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3305 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3306 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
3307 */
nested_vmx_enter_non_root_mode(struct kvm_vcpu * vcpu,bool from_vmentry)3308 enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3309 bool from_vmentry)
3310 {
3311 struct vcpu_vmx *vmx = to_vmx(vcpu);
3312 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3313 enum vm_entry_failure_code entry_failure_code;
3314 bool evaluate_pending_interrupts;
3315 union vmx_exit_reason exit_reason = {
3316 .basic = EXIT_REASON_INVALID_STATE,
3317 .failed_vmentry = 1,
3318 };
3319 u32 failed_index;
3320
3321 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3322 kvm_vcpu_flush_tlb_current(vcpu);
3323
3324 evaluate_pending_interrupts = exec_controls_get(vmx) &
3325 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
3326 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3327 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3328
3329 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3330 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3331 if (kvm_mpx_supported() &&
3332 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3333 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3334
3335 /*
3336 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3337 * nested early checks are disabled. In the event of a "late" VM-Fail,
3338 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3339 * software model to the pre-VMEntry host state. When EPT is disabled,
3340 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3341 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3342 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3343 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3344 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3345 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3346 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3347 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3348 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3349 * path would need to manually save/restore vmcs01.GUEST_CR3.
3350 */
3351 if (!enable_ept && !nested_early_check)
3352 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3353
3354 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3355
3356 prepare_vmcs02_early(vmx, vmcs12);
3357
3358 if (from_vmentry) {
3359 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3360 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3361 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
3362 }
3363
3364 if (nested_vmx_check_vmentry_hw(vcpu)) {
3365 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3366 return NVMX_VMENTRY_VMFAIL;
3367 }
3368
3369 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3370 &entry_failure_code)) {
3371 exit_reason.basic = EXIT_REASON_INVALID_STATE;
3372 vmcs12->exit_qualification = entry_failure_code;
3373 goto vmentry_fail_vmexit;
3374 }
3375 }
3376
3377 enter_guest_mode(vcpu);
3378 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
3379 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
3380
3381 if (prepare_vmcs02(vcpu, vmcs12, &entry_failure_code)) {
3382 exit_reason.basic = EXIT_REASON_INVALID_STATE;
3383 vmcs12->exit_qualification = entry_failure_code;
3384 goto vmentry_fail_vmexit_guest_mode;
3385 }
3386
3387 if (from_vmentry) {
3388 failed_index = nested_vmx_load_msr(vcpu,
3389 vmcs12->vm_entry_msr_load_addr,
3390 vmcs12->vm_entry_msr_load_count);
3391 if (failed_index) {
3392 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
3393 vmcs12->exit_qualification = failed_index;
3394 goto vmentry_fail_vmexit_guest_mode;
3395 }
3396 } else {
3397 /*
3398 * The MMU is not initialized to point at the right entities yet and
3399 * "get pages" would need to read data from the guest (i.e. we will
3400 * need to perform gpa to hpa translation). Request a call
3401 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3402 * have already been set at vmentry time and should not be reset.
3403 */
3404 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
3405 }
3406
3407 /*
3408 * If L1 had a pending IRQ/NMI until it executed
3409 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3410 * disallowed (e.g. interrupts disabled), L0 needs to
3411 * evaluate if this pending event should cause an exit from L2
3412 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3413 * intercept EXTERNAL_INTERRUPT).
3414 *
3415 * Usually this would be handled by the processor noticing an
3416 * IRQ/NMI window request, or checking RVI during evaluation of
3417 * pending virtual interrupts. However, this setting was done
3418 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3419 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3420 */
3421 if (unlikely(evaluate_pending_interrupts))
3422 kvm_make_request(KVM_REQ_EVENT, vcpu);
3423
3424 /*
3425 * Do not start the preemption timer hrtimer until after we know
3426 * we are successful, so that only nested_vmx_vmexit needs to cancel
3427 * the timer.
3428 */
3429 vmx->nested.preemption_timer_expired = false;
3430 if (nested_cpu_has_preemption_timer(vmcs12)) {
3431 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3432 vmx_start_preemption_timer(vcpu, timer_value);
3433 }
3434
3435 /*
3436 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3437 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3438 * returned as far as L1 is concerned. It will only return (and set
3439 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3440 */
3441 return NVMX_VMENTRY_SUCCESS;
3442
3443 /*
3444 * A failed consistency check that leads to a VMExit during L1's
3445 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3446 * 26.7 "VM-entry failures during or after loading guest state".
3447 */
3448 vmentry_fail_vmexit_guest_mode:
3449 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
3450 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3451 leave_guest_mode(vcpu);
3452
3453 vmentry_fail_vmexit:
3454 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3455
3456 if (!from_vmentry)
3457 return NVMX_VMENTRY_VMEXIT;
3458
3459 load_vmcs12_host_state(vcpu, vmcs12);
3460 vmcs12->vm_exit_reason = exit_reason.full;
3461 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
3462 vmx->nested.need_vmcs12_to_shadow_sync = true;
3463 return NVMX_VMENTRY_VMEXIT;
3464 }
3465
3466 /*
3467 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3468 * for running an L2 nested guest.
3469 */
nested_vmx_run(struct kvm_vcpu * vcpu,bool launch)3470 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3471 {
3472 struct vmcs12 *vmcs12;
3473 enum nvmx_vmentry_status status;
3474 struct vcpu_vmx *vmx = to_vmx(vcpu);
3475 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
3476 enum nested_evmptrld_status evmptrld_status;
3477
3478 if (!nested_vmx_check_permission(vcpu))
3479 return 1;
3480
3481 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3482 if (evmptrld_status == EVMPTRLD_ERROR) {
3483 kvm_queue_exception(vcpu, UD_VECTOR);
3484 return 1;
3485 } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) {
3486 return nested_vmx_failInvalid(vcpu);
3487 }
3488
3489 if (CC(!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull))
3490 return nested_vmx_failInvalid(vcpu);
3491
3492 vmcs12 = get_vmcs12(vcpu);
3493
3494 /*
3495 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3496 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3497 * rather than RFLAGS.ZF, and no error number is stored to the
3498 * VM-instruction error field.
3499 */
3500 if (CC(vmcs12->hdr.shadow_vmcs))
3501 return nested_vmx_failInvalid(vcpu);
3502
3503 if (vmx->nested.hv_evmcs) {
3504 copy_enlightened_to_vmcs12(vmx);
3505 /* Enlightened VMCS doesn't have launch state */
3506 vmcs12->launch_state = !launch;
3507 } else if (enable_shadow_vmcs) {
3508 copy_shadow_to_vmcs12(vmx);
3509 }
3510
3511 /*
3512 * The nested entry process starts with enforcing various prerequisites
3513 * on vmcs12 as required by the Intel SDM, and act appropriately when
3514 * they fail: As the SDM explains, some conditions should cause the
3515 * instruction to fail, while others will cause the instruction to seem
3516 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3517 * To speed up the normal (success) code path, we should avoid checking
3518 * for misconfigurations which will anyway be caught by the processor
3519 * when using the merged vmcs02.
3520 */
3521 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
3522 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
3523
3524 if (CC(vmcs12->launch_state == launch))
3525 return nested_vmx_fail(vcpu,
3526 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3527 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3528
3529 if (nested_vmx_check_controls(vcpu, vmcs12))
3530 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3531
3532 if (nested_vmx_check_host_state(vcpu, vmcs12))
3533 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
3534
3535 /*
3536 * We're finally done with prerequisite checking, and can start with
3537 * the nested entry.
3538 */
3539 vmx->nested.nested_run_pending = 1;
3540 vmx->nested.has_preemption_timer_deadline = false;
3541 status = nested_vmx_enter_non_root_mode(vcpu, true);
3542 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3543 goto vmentry_failed;
3544
3545 /* Emulate processing of posted interrupts on VM-Enter. */
3546 if (nested_cpu_has_posted_intr(vmcs12) &&
3547 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3548 vmx->nested.pi_pending = true;
3549 kvm_make_request(KVM_REQ_EVENT, vcpu);
3550 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3551 }
3552
3553 /* Hide L1D cache contents from the nested guest. */
3554 vmx->vcpu.arch.l1tf_flush_l1d = true;
3555
3556 /*
3557 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3558 * also be used as part of restoring nVMX state for
3559 * snapshot restore (migration).
3560 *
3561 * In this flow, it is assumed that vmcs12 cache was
3562 * trasferred as part of captured nVMX state and should
3563 * therefore not be read from guest memory (which may not
3564 * exist on destination host yet).
3565 */
3566 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3567
3568 /*
3569 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3570 * awakened by event injection or by an NMI-window VM-exit or
3571 * by an interrupt-window VM-exit, halt the vcpu.
3572 */
3573 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
3574 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3575 !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_NMI_WINDOW_EXITING) &&
3576 !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_INTR_WINDOW_EXITING) &&
3577 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3578 vmx->nested.nested_run_pending = 0;
3579 return kvm_vcpu_halt(vcpu);
3580 }
3581 return 1;
3582
3583 vmentry_failed:
3584 vmx->nested.nested_run_pending = 0;
3585 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3586 return 0;
3587 if (status == NVMX_VMENTRY_VMEXIT)
3588 return 1;
3589 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
3590 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3591 }
3592
3593 /*
3594 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
3595 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
3596 * This function returns the new value we should put in vmcs12.guest_cr0.
3597 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3598 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3599 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3600 * didn't trap the bit, because if L1 did, so would L0).
3601 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3602 * been modified by L2, and L1 knows it. So just leave the old value of
3603 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3604 * isn't relevant, because if L0 traps this bit it can set it to anything.
3605 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3606 * changed these bits, and therefore they need to be updated, but L0
3607 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3608 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3609 */
3610 static inline unsigned long
vmcs12_guest_cr0(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)3611 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3612 {
3613 return
3614 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3615 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3616 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3617 vcpu->arch.cr0_guest_owned_bits));
3618 }
3619
3620 static inline unsigned long
vmcs12_guest_cr4(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)3621 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3622 {
3623 return
3624 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3625 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3626 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3627 vcpu->arch.cr4_guest_owned_bits));
3628 }
3629
vmcs12_save_pending_event(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)3630 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3631 struct vmcs12 *vmcs12)
3632 {
3633 u32 idt_vectoring;
3634 unsigned int nr;
3635
3636 if (vcpu->arch.exception.injected) {
3637 nr = vcpu->arch.exception.nr;
3638 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3639
3640 if (kvm_exception_is_soft(nr)) {
3641 vmcs12->vm_exit_instruction_len =
3642 vcpu->arch.event_exit_inst_len;
3643 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3644 } else
3645 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3646
3647 if (vcpu->arch.exception.has_error_code) {
3648 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3649 vmcs12->idt_vectoring_error_code =
3650 vcpu->arch.exception.error_code;
3651 }
3652
3653 vmcs12->idt_vectoring_info_field = idt_vectoring;
3654 } else if (vcpu->arch.nmi_injected) {
3655 vmcs12->idt_vectoring_info_field =
3656 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3657 } else if (vcpu->arch.interrupt.injected) {
3658 nr = vcpu->arch.interrupt.nr;
3659 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3660
3661 if (vcpu->arch.interrupt.soft) {
3662 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3663 vmcs12->vm_entry_instruction_len =
3664 vcpu->arch.event_exit_inst_len;
3665 } else
3666 idt_vectoring |= INTR_TYPE_EXT_INTR;
3667
3668 vmcs12->idt_vectoring_info_field = idt_vectoring;
3669 }
3670 }
3671
3672
nested_mark_vmcs12_pages_dirty(struct kvm_vcpu * vcpu)3673 void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
3674 {
3675 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3676 gfn_t gfn;
3677
3678 /*
3679 * Don't need to mark the APIC access page dirty; it is never
3680 * written to by the CPU during APIC virtualization.
3681 */
3682
3683 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3684 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3685 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3686 }
3687
3688 if (nested_cpu_has_posted_intr(vmcs12)) {
3689 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3690 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3691 }
3692 }
3693
vmx_complete_nested_posted_interrupt(struct kvm_vcpu * vcpu)3694 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
3695 {
3696 struct vcpu_vmx *vmx = to_vmx(vcpu);
3697 int max_irr;
3698 void *vapic_page;
3699 u16 status;
3700
3701 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
3702 return;
3703
3704 vmx->nested.pi_pending = false;
3705 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
3706 return;
3707
3708 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3709 if (max_irr != 256) {
3710 vapic_page = vmx->nested.virtual_apic_map.hva;
3711 if (!vapic_page)
3712 return;
3713
3714 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3715 vapic_page, &max_irr);
3716 status = vmcs_read16(GUEST_INTR_STATUS);
3717 if ((u8)max_irr > ((u8)status & 0xff)) {
3718 status &= ~0xff;
3719 status |= (u8)max_irr;
3720 vmcs_write16(GUEST_INTR_STATUS, status);
3721 }
3722 }
3723
3724 nested_mark_vmcs12_pages_dirty(vcpu);
3725 }
3726
nested_vmx_inject_exception_vmexit(struct kvm_vcpu * vcpu,unsigned long exit_qual)3727 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3728 unsigned long exit_qual)
3729 {
3730 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3731 unsigned int nr = vcpu->arch.exception.nr;
3732 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3733
3734 if (vcpu->arch.exception.has_error_code) {
3735 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3736 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3737 }
3738
3739 if (kvm_exception_is_soft(nr))
3740 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3741 else
3742 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3743
3744 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3745 vmx_get_nmi_mask(vcpu))
3746 intr_info |= INTR_INFO_UNBLOCK_NMI;
3747
3748 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3749 }
3750
3751 /*
3752 * Returns true if a debug trap is pending delivery.
3753 *
3754 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3755 * exception may be inferred from the presence of an exception payload.
3756 */
vmx_pending_dbg_trap(struct kvm_vcpu * vcpu)3757 static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3758 {
3759 return vcpu->arch.exception.pending &&
3760 vcpu->arch.exception.nr == DB_VECTOR &&
3761 vcpu->arch.exception.payload;
3762 }
3763
3764 /*
3765 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3766 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3767 * represents these debug traps with a payload that is said to be compatible
3768 * with the 'pending debug exceptions' field, write the payload to the VMCS
3769 * field if a VM-exit is delivered before the debug trap.
3770 */
nested_vmx_update_pending_dbg(struct kvm_vcpu * vcpu)3771 static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3772 {
3773 if (vmx_pending_dbg_trap(vcpu))
3774 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3775 vcpu->arch.exception.payload);
3776 }
3777
nested_vmx_preemption_timer_pending(struct kvm_vcpu * vcpu)3778 static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3779 {
3780 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3781 to_vmx(vcpu)->nested.preemption_timer_expired;
3782 }
3783
vmx_check_nested_events(struct kvm_vcpu * vcpu)3784 static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
3785 {
3786 struct vcpu_vmx *vmx = to_vmx(vcpu);
3787 unsigned long exit_qual;
3788 bool block_nested_events =
3789 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
3790 bool mtf_pending = vmx->nested.mtf_pending;
3791 struct kvm_lapic *apic = vcpu->arch.apic;
3792
3793 /*
3794 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3795 * this state is discarded.
3796 */
3797 if (!block_nested_events)
3798 vmx->nested.mtf_pending = false;
3799
3800 if (lapic_in_kernel(vcpu) &&
3801 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3802 if (block_nested_events)
3803 return -EBUSY;
3804 nested_vmx_update_pending_dbg(vcpu);
3805 clear_bit(KVM_APIC_INIT, &apic->pending_events);
3806 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3807 return 0;
3808 }
3809
3810 /*
3811 * Process any exceptions that are not debug traps before MTF.
3812 */
3813 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
3814 if (block_nested_events)
3815 return -EBUSY;
3816 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3817 goto no_vmexit;
3818 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3819 return 0;
3820 }
3821
3822 if (mtf_pending) {
3823 if (block_nested_events)
3824 return -EBUSY;
3825 nested_vmx_update_pending_dbg(vcpu);
3826 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3827 return 0;
3828 }
3829
3830 if (vcpu->arch.exception.pending) {
3831 if (block_nested_events)
3832 return -EBUSY;
3833 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3834 goto no_vmexit;
3835 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3836 return 0;
3837 }
3838
3839 if (nested_vmx_preemption_timer_pending(vcpu)) {
3840 if (block_nested_events)
3841 return -EBUSY;
3842 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3843 return 0;
3844 }
3845
3846 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3847 if (block_nested_events)
3848 return -EBUSY;
3849 goto no_vmexit;
3850 }
3851
3852 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
3853 if (block_nested_events)
3854 return -EBUSY;
3855 if (!nested_exit_on_nmi(vcpu))
3856 goto no_vmexit;
3857
3858 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3859 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3860 INTR_INFO_VALID_MASK, 0);
3861 /*
3862 * The NMI-triggered VM exit counts as injection:
3863 * clear this one and block further NMIs.
3864 */
3865 vcpu->arch.nmi_pending = 0;
3866 vmx_set_nmi_mask(vcpu, true);
3867 return 0;
3868 }
3869
3870 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
3871 if (block_nested_events)
3872 return -EBUSY;
3873 if (!nested_exit_on_intr(vcpu))
3874 goto no_vmexit;
3875 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3876 return 0;
3877 }
3878
3879 no_vmexit:
3880 vmx_complete_nested_posted_interrupt(vcpu);
3881 return 0;
3882 }
3883
vmx_get_preemption_timer_value(struct kvm_vcpu * vcpu)3884 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3885 {
3886 ktime_t remaining =
3887 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3888 u64 value;
3889
3890 if (ktime_to_ns(remaining) <= 0)
3891 return 0;
3892
3893 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3894 do_div(value, 1000000);
3895 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3896 }
3897
is_vmcs12_ext_field(unsigned long field)3898 static bool is_vmcs12_ext_field(unsigned long field)
3899 {
3900 switch (field) {
3901 case GUEST_ES_SELECTOR:
3902 case GUEST_CS_SELECTOR:
3903 case GUEST_SS_SELECTOR:
3904 case GUEST_DS_SELECTOR:
3905 case GUEST_FS_SELECTOR:
3906 case GUEST_GS_SELECTOR:
3907 case GUEST_LDTR_SELECTOR:
3908 case GUEST_TR_SELECTOR:
3909 case GUEST_ES_LIMIT:
3910 case GUEST_CS_LIMIT:
3911 case GUEST_SS_LIMIT:
3912 case GUEST_DS_LIMIT:
3913 case GUEST_FS_LIMIT:
3914 case GUEST_GS_LIMIT:
3915 case GUEST_LDTR_LIMIT:
3916 case GUEST_TR_LIMIT:
3917 case GUEST_GDTR_LIMIT:
3918 case GUEST_IDTR_LIMIT:
3919 case GUEST_ES_AR_BYTES:
3920 case GUEST_DS_AR_BYTES:
3921 case GUEST_FS_AR_BYTES:
3922 case GUEST_GS_AR_BYTES:
3923 case GUEST_LDTR_AR_BYTES:
3924 case GUEST_TR_AR_BYTES:
3925 case GUEST_ES_BASE:
3926 case GUEST_CS_BASE:
3927 case GUEST_SS_BASE:
3928 case GUEST_DS_BASE:
3929 case GUEST_FS_BASE:
3930 case GUEST_GS_BASE:
3931 case GUEST_LDTR_BASE:
3932 case GUEST_TR_BASE:
3933 case GUEST_GDTR_BASE:
3934 case GUEST_IDTR_BASE:
3935 case GUEST_PENDING_DBG_EXCEPTIONS:
3936 case GUEST_BNDCFGS:
3937 return true;
3938 default:
3939 break;
3940 }
3941
3942 return false;
3943 }
3944
sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)3945 static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3946 struct vmcs12 *vmcs12)
3947 {
3948 struct vcpu_vmx *vmx = to_vmx(vcpu);
3949
3950 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3951 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3952 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3953 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3954 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3955 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3956 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3957 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3958 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3959 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
3960 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
3961 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
3962 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
3963 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
3964 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
3965 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
3966 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
3967 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
3968 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
3969 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
3970 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
3971 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
3972 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
3973 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
3974 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
3975 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
3976 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
3977 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
3978 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
3979 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
3980 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
3981 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
3982 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
3983 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
3984 vmcs12->guest_pending_dbg_exceptions =
3985 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
3986 if (kvm_mpx_supported())
3987 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3988
3989 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
3990 }
3991
copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)3992 static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3993 struct vmcs12 *vmcs12)
3994 {
3995 struct vcpu_vmx *vmx = to_vmx(vcpu);
3996 int cpu;
3997
3998 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
3999 return;
4000
4001
4002 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4003
4004 cpu = get_cpu();
4005 vmx->loaded_vmcs = &vmx->nested.vmcs02;
4006 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
4007
4008 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4009
4010 vmx->loaded_vmcs = &vmx->vmcs01;
4011 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
4012 put_cpu();
4013 }
4014
4015 /*
4016 * Update the guest state fields of vmcs12 to reflect changes that
4017 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4018 * VM-entry controls is also updated, since this is really a guest
4019 * state bit.)
4020 */
sync_vmcs02_to_vmcs12(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)4021 static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4022 {
4023 struct vcpu_vmx *vmx = to_vmx(vcpu);
4024
4025 if (vmx->nested.hv_evmcs)
4026 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4027
4028 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = !vmx->nested.hv_evmcs;
4029
4030 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4031 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4032
4033 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4034 vmcs12->guest_rip = kvm_rip_read(vcpu);
4035 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4036
4037 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4038 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
4039
4040 vmcs12->guest_interruptibility_info =
4041 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
4042
4043 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4044 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
4045 else
4046 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4047
4048 if (nested_cpu_has_preemption_timer(vmcs12) &&
4049 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4050 !vmx->nested.nested_run_pending)
4051 vmcs12->vmx_preemption_timer_value =
4052 vmx_get_preemption_timer_value(vcpu);
4053
4054 /*
4055 * In some cases (usually, nested EPT), L2 is allowed to change its
4056 * own CR3 without exiting. If it has changed it, we must keep it.
4057 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4058 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4059 *
4060 * Additionally, restore L2's PDPTR to vmcs12.
4061 */
4062 if (enable_ept) {
4063 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
4064 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4065 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4066 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4067 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4068 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4069 }
4070 }
4071
4072 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4073
4074 if (nested_cpu_has_vid(vmcs12))
4075 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4076
4077 vmcs12->vm_entry_controls =
4078 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4079 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4080
4081 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
4082 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
4083
4084 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4085 vmcs12->guest_ia32_efer = vcpu->arch.efer;
4086 }
4087
4088 /*
4089 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4090 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4091 * and this function updates it to reflect the changes to the guest state while
4092 * L2 was running (and perhaps made some exits which were handled directly by L0
4093 * without going back to L1), and to reflect the exit reason.
4094 * Note that we do not have to copy here all VMCS fields, just those that
4095 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4096 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4097 * which already writes to vmcs12 directly.
4098 */
prepare_vmcs12(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,u32 vm_exit_reason,u32 exit_intr_info,unsigned long exit_qualification)4099 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
4100 u32 vm_exit_reason, u32 exit_intr_info,
4101 unsigned long exit_qualification)
4102 {
4103 /* update exit information fields: */
4104 vmcs12->vm_exit_reason = vm_exit_reason;
4105 vmcs12->exit_qualification = exit_qualification;
4106 vmcs12->vm_exit_intr_info = exit_intr_info;
4107
4108 vmcs12->idt_vectoring_info_field = 0;
4109 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4110 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4111
4112 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4113 vmcs12->launch_state = 1;
4114
4115 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4116 * instead of reading the real value. */
4117 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4118
4119 /*
4120 * Transfer the event that L0 or L1 may wanted to inject into
4121 * L2 to IDT_VECTORING_INFO_FIELD.
4122 */
4123 vmcs12_save_pending_event(vcpu, vmcs12);
4124
4125 /*
4126 * According to spec, there's no need to store the guest's
4127 * MSRs if the exit is due to a VM-entry failure that occurs
4128 * during or after loading the guest state. Since this exit
4129 * does not fall in that category, we need to save the MSRs.
4130 */
4131 if (nested_vmx_store_msr(vcpu,
4132 vmcs12->vm_exit_msr_store_addr,
4133 vmcs12->vm_exit_msr_store_count))
4134 nested_vmx_abort(vcpu,
4135 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
4136 }
4137
4138 /*
4139 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4140 * preserved above and would only end up incorrectly in L1.
4141 */
4142 vcpu->arch.nmi_injected = false;
4143 kvm_clear_exception_queue(vcpu);
4144 kvm_clear_interrupt_queue(vcpu);
4145 }
4146
4147 /*
4148 * A part of what we need to when the nested L2 guest exits and we want to
4149 * run its L1 parent, is to reset L1's guest state to the host state specified
4150 * in vmcs12.
4151 * This function is to be called not only on normal nested exit, but also on
4152 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4153 * Failures During or After Loading Guest State").
4154 * This function should be called when the active VMCS is L1's (vmcs01).
4155 */
load_vmcs12_host_state(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)4156 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4157 struct vmcs12 *vmcs12)
4158 {
4159 enum vm_entry_failure_code ignored;
4160 struct kvm_segment seg;
4161
4162 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4163 vcpu->arch.efer = vmcs12->host_ia32_efer;
4164 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4165 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4166 else
4167 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4168 vmx_set_efer(vcpu, vcpu->arch.efer);
4169
4170 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4171 kvm_rip_write(vcpu, vmcs12->host_rip);
4172 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4173 vmx_set_interrupt_shadow(vcpu, 0);
4174
4175 /*
4176 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4177 * actually changed, because vmx_set_cr0 refers to efer set above.
4178 *
4179 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4180 * (KVM doesn't change it);
4181 */
4182 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4183 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4184
4185 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4186 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4187 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4188
4189 nested_ept_uninit_mmu_context(vcpu);
4190
4191 /*
4192 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4193 * couldn't have changed.
4194 */
4195 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &ignored))
4196 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4197
4198 if (!enable_ept)
4199 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
4200
4201 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
4202
4203 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4204 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4205 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4206 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4207 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4208 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4209 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4210
4211 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4212 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4213 vmcs_write64(GUEST_BNDCFGS, 0);
4214
4215 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4216 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4217 vcpu->arch.pat = vmcs12->host_ia32_pat;
4218 }
4219 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
4220 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4221 vmcs12->host_ia32_perf_global_ctrl));
4222
4223 /* Set L1 segment info according to Intel SDM
4224 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4225 seg = (struct kvm_segment) {
4226 .base = 0,
4227 .limit = 0xFFFFFFFF,
4228 .selector = vmcs12->host_cs_selector,
4229 .type = 11,
4230 .present = 1,
4231 .s = 1,
4232 .g = 1
4233 };
4234 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4235 seg.l = 1;
4236 else
4237 seg.db = 1;
4238 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4239 seg = (struct kvm_segment) {
4240 .base = 0,
4241 .limit = 0xFFFFFFFF,
4242 .type = 3,
4243 .present = 1,
4244 .s = 1,
4245 .db = 1,
4246 .g = 1
4247 };
4248 seg.selector = vmcs12->host_ds_selector;
4249 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4250 seg.selector = vmcs12->host_es_selector;
4251 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4252 seg.selector = vmcs12->host_ss_selector;
4253 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4254 seg.selector = vmcs12->host_fs_selector;
4255 seg.base = vmcs12->host_fs_base;
4256 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4257 seg.selector = vmcs12->host_gs_selector;
4258 seg.base = vmcs12->host_gs_base;
4259 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4260 seg = (struct kvm_segment) {
4261 .base = vmcs12->host_tr_base,
4262 .limit = 0x67,
4263 .selector = vmcs12->host_tr_selector,
4264 .type = 11,
4265 .present = 1
4266 };
4267 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4268
4269 kvm_set_dr(vcpu, 7, 0x400);
4270 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4271
4272 if (cpu_has_vmx_msr_bitmap())
4273 vmx_update_msr_bitmap(vcpu);
4274
4275 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4276 vmcs12->vm_exit_msr_load_count))
4277 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4278 }
4279
nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx * vmx)4280 static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4281 {
4282 struct vmx_uret_msr *efer_msr;
4283 unsigned int i;
4284
4285 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4286 return vmcs_read64(GUEST_IA32_EFER);
4287
4288 if (cpu_has_load_ia32_efer())
4289 return host_efer;
4290
4291 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4292 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4293 return vmx->msr_autoload.guest.val[i].value;
4294 }
4295
4296 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
4297 if (efer_msr)
4298 return efer_msr->data;
4299
4300 return host_efer;
4301 }
4302
nested_vmx_restore_host_state(struct kvm_vcpu * vcpu)4303 static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4304 {
4305 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4306 struct vcpu_vmx *vmx = to_vmx(vcpu);
4307 struct vmx_msr_entry g, h;
4308 gpa_t gpa;
4309 u32 i, j;
4310
4311 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4312
4313 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4314 /*
4315 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4316 * as vmcs01.GUEST_DR7 contains a userspace defined value
4317 * and vcpu->arch.dr7 is not squirreled away before the
4318 * nested VMENTER (not worth adding a variable in nested_vmx).
4319 */
4320 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4321 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4322 else
4323 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4324 }
4325
4326 /*
4327 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4328 * handle a variety of side effects to KVM's software model.
4329 */
4330 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4331
4332 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4333 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4334
4335 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4336 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4337
4338 nested_ept_uninit_mmu_context(vcpu);
4339 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4340 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
4341
4342 /*
4343 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4344 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4345 * VMFail, like everything else we just need to ensure our
4346 * software model is up-to-date.
4347 */
4348 if (enable_ept && is_pae_paging(vcpu))
4349 ept_save_pdptrs(vcpu);
4350
4351 kvm_mmu_reset_context(vcpu);
4352
4353 if (cpu_has_vmx_msr_bitmap())
4354 vmx_update_msr_bitmap(vcpu);
4355
4356 /*
4357 * This nasty bit of open coding is a compromise between blindly
4358 * loading L1's MSRs using the exit load lists (incorrect emulation
4359 * of VMFail), leaving the nested VM's MSRs in the software model
4360 * (incorrect behavior) and snapshotting the modified MSRs (too
4361 * expensive since the lists are unbound by hardware). For each
4362 * MSR that was (prematurely) loaded from the nested VMEntry load
4363 * list, reload it from the exit load list if it exists and differs
4364 * from the guest value. The intent is to stuff host state as
4365 * silently as possible, not to fully process the exit load list.
4366 */
4367 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4368 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4369 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4370 pr_debug_ratelimited(
4371 "%s read MSR index failed (%u, 0x%08llx)\n",
4372 __func__, i, gpa);
4373 goto vmabort;
4374 }
4375
4376 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4377 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4378 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4379 pr_debug_ratelimited(
4380 "%s read MSR failed (%u, 0x%08llx)\n",
4381 __func__, j, gpa);
4382 goto vmabort;
4383 }
4384 if (h.index != g.index)
4385 continue;
4386 if (h.value == g.value)
4387 break;
4388
4389 if (nested_vmx_load_msr_check(vcpu, &h)) {
4390 pr_debug_ratelimited(
4391 "%s check failed (%u, 0x%x, 0x%x)\n",
4392 __func__, j, h.index, h.reserved);
4393 goto vmabort;
4394 }
4395
4396 if (kvm_set_msr(vcpu, h.index, h.value)) {
4397 pr_debug_ratelimited(
4398 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4399 __func__, j, h.index, h.value);
4400 goto vmabort;
4401 }
4402 }
4403 }
4404
4405 return;
4406
4407 vmabort:
4408 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4409 }
4410
4411 /*
4412 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4413 * and modify vmcs12 to make it see what it would expect to see there if
4414 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4415 */
nested_vmx_vmexit(struct kvm_vcpu * vcpu,u32 vm_exit_reason,u32 exit_intr_info,unsigned long exit_qualification)4416 void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
4417 u32 exit_intr_info, unsigned long exit_qualification)
4418 {
4419 struct vcpu_vmx *vmx = to_vmx(vcpu);
4420 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4421
4422 /* trying to cancel vmlaunch/vmresume is a bug */
4423 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4424
4425 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
4426 /*
4427 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map
4428 * Enlightened VMCS after migration and we still need to
4429 * do that when something is forcing L2->L1 exit prior to
4430 * the first L2 run.
4431 */
4432 (void)nested_get_evmcs_page(vcpu);
4433 }
4434
4435 /* Service the TLB flush request for L2 before switching to L1. */
4436 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4437 kvm_vcpu_flush_tlb_current(vcpu);
4438
4439 /*
4440 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4441 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4442 * up-to-date before switching to L1.
4443 */
4444 if (enable_ept && is_pae_paging(vcpu))
4445 vmx_ept_load_pdptrs(vcpu);
4446
4447 leave_guest_mode(vcpu);
4448
4449 if (nested_cpu_has_preemption_timer(vmcs12))
4450 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4451
4452 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
4453 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
4454
4455 if (likely(!vmx->fail)) {
4456 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
4457
4458 if (vm_exit_reason != -1)
4459 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4460 exit_intr_info, exit_qualification);
4461
4462 /*
4463 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
4464 * also be used to capture vmcs12 cache as part of
4465 * capturing nVMX state for snapshot (migration).
4466 *
4467 * Otherwise, this flush will dirty guest memory at a
4468 * point it is already assumed by user-space to be
4469 * immutable.
4470 */
4471 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
4472 } else {
4473 /*
4474 * The only expected VM-instruction error is "VM entry with
4475 * invalid control field(s)." Anything else indicates a
4476 * problem with L0. And we should never get here with a
4477 * VMFail of any type if early consistency checks are enabled.
4478 */
4479 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4480 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4481 WARN_ON_ONCE(nested_early_check);
4482 }
4483
4484 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4485
4486 /*
4487 * If IBRS is advertised to the vCPU, KVM must flush the indirect
4488 * branch predictors when transitioning from L2 to L1, as L1 expects
4489 * hardware (KVM in this case) to provide separate predictor modes.
4490 * Bare metal isolates VMX root (host) from VMX non-root (guest), but
4491 * doesn't isolate different VMCSs, i.e. in this case, doesn't provide
4492 * separate modes for L2 vs L1.
4493 */
4494 if (guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4495 indirect_branch_prediction_barrier();
4496
4497 /* Update any VMCS fields that might have changed while L2 ran */
4498 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4499 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4500 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
4501 if (vmx->nested.l1_tpr_threshold != -1)
4502 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
4503
4504 if (kvm_has_tsc_control)
4505 decache_tsc_multiplier(vmx);
4506
4507 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4508 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4509 vmx_set_virtual_apic_mode(vcpu);
4510 }
4511
4512 /* Unpin physical memory we referred to in vmcs02 */
4513 if (vmx->nested.apic_access_page) {
4514 kvm_release_page_clean(vmx->nested.apic_access_page);
4515 vmx->nested.apic_access_page = NULL;
4516 }
4517 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
4518 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4519 vmx->nested.pi_desc = NULL;
4520
4521 if (vmx->nested.reload_vmcs01_apic_access_page) {
4522 vmx->nested.reload_vmcs01_apic_access_page = false;
4523 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4524 }
4525
4526 if ((vm_exit_reason != -1) &&
4527 (enable_shadow_vmcs || vmx->nested.hv_evmcs))
4528 vmx->nested.need_vmcs12_to_shadow_sync = true;
4529
4530 /* in case we halted in L2 */
4531 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4532
4533 if (likely(!vmx->fail)) {
4534 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
4535 nested_exit_intr_ack_set(vcpu)) {
4536 int irq = kvm_cpu_get_interrupt(vcpu);
4537 WARN_ON(irq < 0);
4538 vmcs12->vm_exit_intr_info = irq |
4539 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4540 }
4541
4542 if (vm_exit_reason != -1)
4543 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4544 vmcs12->exit_qualification,
4545 vmcs12->idt_vectoring_info_field,
4546 vmcs12->vm_exit_intr_info,
4547 vmcs12->vm_exit_intr_error_code,
4548 KVM_ISA_VMX);
4549
4550 load_vmcs12_host_state(vcpu, vmcs12);
4551
4552 return;
4553 }
4554
4555 /*
4556 * After an early L2 VM-entry failure, we're now back
4557 * in L1 which thinks it just finished a VMLAUNCH or
4558 * VMRESUME instruction, so we need to set the failure
4559 * flag and the VM-instruction error field of the VMCS
4560 * accordingly, and skip the emulated instruction.
4561 */
4562 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4563
4564 /*
4565 * Restore L1's host state to KVM's software model. We're here
4566 * because a consistency check was caught by hardware, which
4567 * means some amount of guest state has been propagated to KVM's
4568 * model and needs to be unwound to the host's state.
4569 */
4570 nested_vmx_restore_host_state(vcpu);
4571
4572 vmx->fail = 0;
4573 }
4574
4575 /*
4576 * Decode the memory-address operand of a vmx instruction, as recorded on an
4577 * exit caused by such an instruction (run by a guest hypervisor).
4578 * On success, returns 0. When the operand is invalid, returns 1 and throws
4579 * #UD, #GP, or #SS.
4580 */
get_vmx_mem_address(struct kvm_vcpu * vcpu,unsigned long exit_qualification,u32 vmx_instruction_info,bool wr,int len,gva_t * ret)4581 int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
4582 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
4583 {
4584 gva_t off;
4585 bool exn;
4586 struct kvm_segment s;
4587
4588 /*
4589 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4590 * Execution", on an exit, vmx_instruction_info holds most of the
4591 * addressing components of the operand. Only the displacement part
4592 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4593 * For how an actual address is calculated from all these components,
4594 * refer to Vol. 1, "Operand Addressing".
4595 */
4596 int scaling = vmx_instruction_info & 3;
4597 int addr_size = (vmx_instruction_info >> 7) & 7;
4598 bool is_reg = vmx_instruction_info & (1u << 10);
4599 int seg_reg = (vmx_instruction_info >> 15) & 7;
4600 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4601 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4602 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4603 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4604
4605 if (is_reg) {
4606 kvm_queue_exception(vcpu, UD_VECTOR);
4607 return 1;
4608 }
4609
4610 /* Addr = segment_base + offset */
4611 /* offset = base + [index * scale] + displacement */
4612 off = exit_qualification; /* holds the displacement */
4613 if (addr_size == 1)
4614 off = (gva_t)sign_extend64(off, 31);
4615 else if (addr_size == 0)
4616 off = (gva_t)sign_extend64(off, 15);
4617 if (base_is_valid)
4618 off += kvm_register_readl(vcpu, base_reg);
4619 if (index_is_valid)
4620 off += kvm_register_readl(vcpu, index_reg) << scaling;
4621 vmx_get_segment(vcpu, &s, seg_reg);
4622
4623 /*
4624 * The effective address, i.e. @off, of a memory operand is truncated
4625 * based on the address size of the instruction. Note that this is
4626 * the *effective address*, i.e. the address prior to accounting for
4627 * the segment's base.
4628 */
4629 if (addr_size == 1) /* 32 bit */
4630 off &= 0xffffffff;
4631 else if (addr_size == 0) /* 16 bit */
4632 off &= 0xffff;
4633
4634 /* Checks for #GP/#SS exceptions. */
4635 exn = false;
4636 if (is_long_mode(vcpu)) {
4637 /*
4638 * The virtual/linear address is never truncated in 64-bit
4639 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4640 * address when using FS/GS with a non-zero base.
4641 */
4642 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4643 *ret = s.base + off;
4644 else
4645 *ret = off;
4646
4647 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4648 * non-canonical form. This is the only check on the memory
4649 * destination for long mode!
4650 */
4651 exn = is_noncanonical_address(*ret, vcpu);
4652 } else {
4653 /*
4654 * When not in long mode, the virtual/linear address is
4655 * unconditionally truncated to 32 bits regardless of the
4656 * address size.
4657 */
4658 *ret = (s.base + off) & 0xffffffff;
4659
4660 /* Protected mode: apply checks for segment validity in the
4661 * following order:
4662 * - segment type check (#GP(0) may be thrown)
4663 * - usability check (#GP(0)/#SS(0))
4664 * - limit check (#GP(0)/#SS(0))
4665 */
4666 if (wr)
4667 /* #GP(0) if the destination operand is located in a
4668 * read-only data segment or any code segment.
4669 */
4670 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4671 else
4672 /* #GP(0) if the source operand is located in an
4673 * execute-only code segment
4674 */
4675 exn = ((s.type & 0xa) == 8);
4676 if (exn) {
4677 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4678 return 1;
4679 }
4680 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4681 */
4682 exn = (s.unusable != 0);
4683
4684 /*
4685 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4686 * outside the segment limit. All CPUs that support VMX ignore
4687 * limit checks for flat segments, i.e. segments with base==0,
4688 * limit==0xffffffff and of type expand-up data or code.
4689 */
4690 if (!(s.base == 0 && s.limit == 0xffffffff &&
4691 ((s.type & 8) || !(s.type & 4))))
4692 exn = exn || ((u64)off + len - 1 > s.limit);
4693 }
4694 if (exn) {
4695 kvm_queue_exception_e(vcpu,
4696 seg_reg == VCPU_SREG_SS ?
4697 SS_VECTOR : GP_VECTOR,
4698 0);
4699 return 1;
4700 }
4701
4702 return 0;
4703 }
4704
nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu * vcpu)4705 void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4706 {
4707 struct vcpu_vmx *vmx;
4708
4709 if (!nested_vmx_allowed(vcpu))
4710 return;
4711
4712 vmx = to_vmx(vcpu);
4713 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
4714 vmx->nested.msrs.entry_ctls_high |=
4715 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4716 vmx->nested.msrs.exit_ctls_high |=
4717 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4718 } else {
4719 vmx->nested.msrs.entry_ctls_high &=
4720 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4721 vmx->nested.msrs.exit_ctls_high &=
4722 ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4723 }
4724 }
4725
nested_vmx_get_vmptr(struct kvm_vcpu * vcpu,gpa_t * vmpointer,int * ret)4726 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4727 int *ret)
4728 {
4729 gva_t gva;
4730 struct x86_exception e;
4731 int r;
4732
4733 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
4734 vmcs_read32(VMX_INSTRUCTION_INFO), false,
4735 sizeof(*vmpointer), &gva)) {
4736 *ret = 1;
4737 return -EINVAL;
4738 }
4739
4740 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
4741 if (r != X86EMUL_CONTINUE) {
4742 *ret = kvm_handle_memory_failure(vcpu, r, &e);
4743 return -EINVAL;
4744 }
4745
4746 return 0;
4747 }
4748
4749 /*
4750 * Allocate a shadow VMCS and associate it with the currently loaded
4751 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4752 * VMCS is also VMCLEARed, so that it is ready for use.
4753 */
alloc_shadow_vmcs(struct kvm_vcpu * vcpu)4754 static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4755 {
4756 struct vcpu_vmx *vmx = to_vmx(vcpu);
4757 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4758
4759 /*
4760 * We should allocate a shadow vmcs for vmcs01 only when L1
4761 * executes VMXON and free it when L1 executes VMXOFF.
4762 * As it is invalid to execute VMXON twice, we shouldn't reach
4763 * here when vmcs01 already have an allocated shadow vmcs.
4764 */
4765 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4766
4767 if (!loaded_vmcs->shadow_vmcs) {
4768 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4769 if (loaded_vmcs->shadow_vmcs)
4770 vmcs_clear(loaded_vmcs->shadow_vmcs);
4771 }
4772 return loaded_vmcs->shadow_vmcs;
4773 }
4774
enter_vmx_operation(struct kvm_vcpu * vcpu)4775 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4776 {
4777 struct vcpu_vmx *vmx = to_vmx(vcpu);
4778 int r;
4779
4780 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4781 if (r < 0)
4782 goto out_vmcs02;
4783
4784 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
4785 if (!vmx->nested.cached_vmcs12)
4786 goto out_cached_vmcs12;
4787
4788 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
4789 if (!vmx->nested.cached_shadow_vmcs12)
4790 goto out_cached_shadow_vmcs12;
4791
4792 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4793 goto out_shadow_vmcs;
4794
4795 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
4796 HRTIMER_MODE_ABS_PINNED);
4797 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4798
4799 vmx->nested.vpid02 = allocate_vpid();
4800
4801 vmx->nested.vmcs02_initialized = false;
4802 vmx->nested.vmxon = true;
4803
4804 if (vmx_pt_mode_is_host_guest()) {
4805 vmx->pt_desc.guest.ctl = 0;
4806 pt_update_intercept_for_msr(vcpu);
4807 }
4808
4809 return 0;
4810
4811 out_shadow_vmcs:
4812 kfree(vmx->nested.cached_shadow_vmcs12);
4813
4814 out_cached_shadow_vmcs12:
4815 kfree(vmx->nested.cached_vmcs12);
4816
4817 out_cached_vmcs12:
4818 free_loaded_vmcs(&vmx->nested.vmcs02);
4819
4820 out_vmcs02:
4821 return -ENOMEM;
4822 }
4823
4824 /*
4825 * Emulate the VMXON instruction.
4826 * Currently, we just remember that VMX is active, and do not save or even
4827 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4828 * do not currently need to store anything in that guest-allocated memory
4829 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4830 * argument is different from the VMXON pointer (which the spec says they do).
4831 */
handle_vmon(struct kvm_vcpu * vcpu)4832 static int handle_vmon(struct kvm_vcpu *vcpu)
4833 {
4834 int ret;
4835 gpa_t vmptr;
4836 uint32_t revision;
4837 struct vcpu_vmx *vmx = to_vmx(vcpu);
4838 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4839 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
4840
4841 /*
4842 * The Intel VMX Instruction Reference lists a bunch of bits that are
4843 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
4844 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
4845 * Otherwise, we should fail with #UD. But most faulting conditions
4846 * have already been checked by hardware, prior to the VM-exit for
4847 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4848 * that bit set to 1 in non-root mode.
4849 */
4850 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4851 kvm_queue_exception(vcpu, UD_VECTOR);
4852 return 1;
4853 }
4854
4855 /* CPL=0 must be checked manually. */
4856 if (vmx_get_cpl(vcpu)) {
4857 kvm_inject_gp(vcpu, 0);
4858 return 1;
4859 }
4860
4861 if (vmx->nested.vmxon)
4862 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
4863
4864 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4865 != VMXON_NEEDED_FEATURES) {
4866 kvm_inject_gp(vcpu, 0);
4867 return 1;
4868 }
4869
4870 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
4871 return ret;
4872
4873 /*
4874 * SDM 3: 24.11.5
4875 * The first 4 bytes of VMXON region contain the supported
4876 * VMCS revision identifier
4877 *
4878 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4879 * which replaces physical address width with 32
4880 */
4881 if (!page_address_valid(vcpu, vmptr))
4882 return nested_vmx_failInvalid(vcpu);
4883
4884 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4885 revision != VMCS12_REVISION)
4886 return nested_vmx_failInvalid(vcpu);
4887
4888 vmx->nested.vmxon_ptr = vmptr;
4889 ret = enter_vmx_operation(vcpu);
4890 if (ret)
4891 return ret;
4892
4893 return nested_vmx_succeed(vcpu);
4894 }
4895
nested_release_vmcs12(struct kvm_vcpu * vcpu)4896 static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4897 {
4898 struct vcpu_vmx *vmx = to_vmx(vcpu);
4899
4900 if (vmx->nested.current_vmptr == -1ull)
4901 return;
4902
4903 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4904
4905 if (enable_shadow_vmcs) {
4906 /* copy to memory all shadowed fields in case
4907 they were modified */
4908 copy_shadow_to_vmcs12(vmx);
4909 vmx_disable_shadow_vmcs(vmx);
4910 }
4911 vmx->nested.posted_intr_nv = -1;
4912
4913 /* Flush VMCS12 to guest memory */
4914 kvm_vcpu_write_guest_page(vcpu,
4915 vmx->nested.current_vmptr >> PAGE_SHIFT,
4916 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4917
4918 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4919
4920 vmx->nested.current_vmptr = -1ull;
4921 }
4922
4923 /* Emulate the VMXOFF instruction */
handle_vmoff(struct kvm_vcpu * vcpu)4924 static int handle_vmoff(struct kvm_vcpu *vcpu)
4925 {
4926 if (!nested_vmx_check_permission(vcpu))
4927 return 1;
4928
4929 free_nested(vcpu);
4930
4931 /* Process a latched INIT during time CPU was in VMX operation */
4932 kvm_make_request(KVM_REQ_EVENT, vcpu);
4933
4934 return nested_vmx_succeed(vcpu);
4935 }
4936
4937 /* Emulate the VMCLEAR instruction */
handle_vmclear(struct kvm_vcpu * vcpu)4938 static int handle_vmclear(struct kvm_vcpu *vcpu)
4939 {
4940 struct vcpu_vmx *vmx = to_vmx(vcpu);
4941 u32 zero = 0;
4942 gpa_t vmptr;
4943 u64 evmcs_gpa;
4944 int r;
4945
4946 if (!nested_vmx_check_permission(vcpu))
4947 return 1;
4948
4949 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
4950 return r;
4951
4952 if (!page_address_valid(vcpu, vmptr))
4953 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
4954
4955 if (vmptr == vmx->nested.vmxon_ptr)
4956 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
4957
4958 /*
4959 * When Enlightened VMEntry is enabled on the calling CPU we treat
4960 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
4961 * way to distinguish it from VMCS12) and we must not corrupt it by
4962 * writing to the non-existent 'launch_state' field. The area doesn't
4963 * have to be the currently active EVMCS on the calling CPU and there's
4964 * nothing KVM has to do to transition it from 'active' to 'non-active'
4965 * state. It is possible that the area will stay mapped as
4966 * vmx->nested.hv_evmcs but this shouldn't be a problem.
4967 */
4968 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
4969 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
4970 if (vmptr == vmx->nested.current_vmptr)
4971 nested_release_vmcs12(vcpu);
4972
4973 kvm_vcpu_write_guest(vcpu,
4974 vmptr + offsetof(struct vmcs12,
4975 launch_state),
4976 &zero, sizeof(zero));
4977 }
4978
4979 return nested_vmx_succeed(vcpu);
4980 }
4981
4982 /* Emulate the VMLAUNCH instruction */
handle_vmlaunch(struct kvm_vcpu * vcpu)4983 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
4984 {
4985 return nested_vmx_run(vcpu, true);
4986 }
4987
4988 /* Emulate the VMRESUME instruction */
handle_vmresume(struct kvm_vcpu * vcpu)4989 static int handle_vmresume(struct kvm_vcpu *vcpu)
4990 {
4991
4992 return nested_vmx_run(vcpu, false);
4993 }
4994
handle_vmread(struct kvm_vcpu * vcpu)4995 static int handle_vmread(struct kvm_vcpu *vcpu)
4996 {
4997 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
4998 : get_vmcs12(vcpu);
4999 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
5000 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5001 struct vcpu_vmx *vmx = to_vmx(vcpu);
5002 struct x86_exception e;
5003 unsigned long field;
5004 u64 value;
5005 gva_t gva = 0;
5006 short offset;
5007 int len, r;
5008
5009 if (!nested_vmx_check_permission(vcpu))
5010 return 1;
5011
5012 /*
5013 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5014 * any VMREAD sets the ALU flags for VMfailInvalid.
5015 */
5016 if (vmx->nested.current_vmptr == -1ull ||
5017 (is_guest_mode(vcpu) &&
5018 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
5019 return nested_vmx_failInvalid(vcpu);
5020
5021 /* Decode instruction info and find the field to read */
5022 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
5023
5024 offset = vmcs_field_to_offset(field);
5025 if (offset < 0)
5026 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5027
5028 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5029 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5030
5031 /* Read the field, zero-extended to a u64 value */
5032 value = vmcs12_read_any(vmcs12, field, offset);
5033
5034 /*
5035 * Now copy part of this value to register or memory, as requested.
5036 * Note that the number of bits actually copied is 32 or 64 depending
5037 * on the guest's mode (32 or 64 bit), not on the given field's length.
5038 */
5039 if (instr_info & BIT(10)) {
5040 kvm_register_writel(vcpu, (((instr_info) >> 3) & 0xf), value);
5041 } else {
5042 len = is_64_bit_mode(vcpu) ? 8 : 4;
5043 if (get_vmx_mem_address(vcpu, exit_qualification,
5044 instr_info, true, len, &gva))
5045 return 1;
5046 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
5047 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5048 if (r != X86EMUL_CONTINUE)
5049 return kvm_handle_memory_failure(vcpu, r, &e);
5050 }
5051
5052 return nested_vmx_succeed(vcpu);
5053 }
5054
is_shadow_field_rw(unsigned long field)5055 static bool is_shadow_field_rw(unsigned long field)
5056 {
5057 switch (field) {
5058 #define SHADOW_FIELD_RW(x, y) case x:
5059 #include "vmcs_shadow_fields.h"
5060 return true;
5061 default:
5062 break;
5063 }
5064 return false;
5065 }
5066
is_shadow_field_ro(unsigned long field)5067 static bool is_shadow_field_ro(unsigned long field)
5068 {
5069 switch (field) {
5070 #define SHADOW_FIELD_RO(x, y) case x:
5071 #include "vmcs_shadow_fields.h"
5072 return true;
5073 default:
5074 break;
5075 }
5076 return false;
5077 }
5078
handle_vmwrite(struct kvm_vcpu * vcpu)5079 static int handle_vmwrite(struct kvm_vcpu *vcpu)
5080 {
5081 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5082 : get_vmcs12(vcpu);
5083 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
5084 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5085 struct vcpu_vmx *vmx = to_vmx(vcpu);
5086 struct x86_exception e;
5087 unsigned long field;
5088 short offset;
5089 gva_t gva;
5090 int len, r;
5091
5092 /*
5093 * The value to write might be 32 or 64 bits, depending on L1's long
5094 * mode, and eventually we need to write that into a field of several
5095 * possible lengths. The code below first zero-extends the value to 64
5096 * bit (value), and then copies only the appropriate number of
5097 * bits into the vmcs12 field.
5098 */
5099 u64 value = 0;
5100
5101 if (!nested_vmx_check_permission(vcpu))
5102 return 1;
5103
5104 /*
5105 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
5106 * any VMWRITE sets the ALU flags for VMfailInvalid.
5107 */
5108 if (vmx->nested.current_vmptr == -1ull ||
5109 (is_guest_mode(vcpu) &&
5110 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
5111 return nested_vmx_failInvalid(vcpu);
5112
5113 if (instr_info & BIT(10))
5114 value = kvm_register_readl(vcpu, (((instr_info) >> 3) & 0xf));
5115 else {
5116 len = is_64_bit_mode(vcpu) ? 8 : 4;
5117 if (get_vmx_mem_address(vcpu, exit_qualification,
5118 instr_info, false, len, &gva))
5119 return 1;
5120 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5121 if (r != X86EMUL_CONTINUE)
5122 return kvm_handle_memory_failure(vcpu, r, &e);
5123 }
5124
5125 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
5126
5127 offset = vmcs_field_to_offset(field);
5128 if (offset < 0)
5129 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5130
5131 /*
5132 * If the vCPU supports "VMWRITE to any supported field in the
5133 * VMCS," then the "read-only" fields are actually read/write.
5134 */
5135 if (vmcs_field_readonly(field) &&
5136 !nested_cpu_has_vmwrite_any_field(vcpu))
5137 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
5138
5139 /*
5140 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5141 * vmcs12, else we may crush a field or consume a stale value.
5142 */
5143 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5144 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5145
5146 /*
5147 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5148 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5149 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5150 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5151 * from L1 will return a different value than VMREAD from L2 (L1 sees
5152 * the stripped down value, L2 sees the full value as stored by KVM).
5153 */
5154 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
5155 value &= 0x1f0ff;
5156
5157 vmcs12_write_any(vmcs12, field, offset, value);
5158
5159 /*
5160 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5161 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5162 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5163 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
5164 */
5165 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5166 /*
5167 * L1 can read these fields without exiting, ensure the
5168 * shadow VMCS is up-to-date.
5169 */
5170 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5171 preempt_disable();
5172 vmcs_load(vmx->vmcs01.shadow_vmcs);
5173
5174 __vmcs_writel(field, value);
5175
5176 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5177 vmcs_load(vmx->loaded_vmcs->vmcs);
5178 preempt_enable();
5179 }
5180 vmx->nested.dirty_vmcs12 = true;
5181 }
5182
5183 return nested_vmx_succeed(vcpu);
5184 }
5185
set_current_vmptr(struct vcpu_vmx * vmx,gpa_t vmptr)5186 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5187 {
5188 vmx->nested.current_vmptr = vmptr;
5189 if (enable_shadow_vmcs) {
5190 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
5191 vmcs_write64(VMCS_LINK_POINTER,
5192 __pa(vmx->vmcs01.shadow_vmcs));
5193 vmx->nested.need_vmcs12_to_shadow_sync = true;
5194 }
5195 vmx->nested.dirty_vmcs12 = true;
5196 }
5197
5198 /* Emulate the VMPTRLD instruction */
handle_vmptrld(struct kvm_vcpu * vcpu)5199 static int handle_vmptrld(struct kvm_vcpu *vcpu)
5200 {
5201 struct vcpu_vmx *vmx = to_vmx(vcpu);
5202 gpa_t vmptr;
5203 int r;
5204
5205 if (!nested_vmx_check_permission(vcpu))
5206 return 1;
5207
5208 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5209 return r;
5210
5211 if (!page_address_valid(vcpu, vmptr))
5212 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
5213
5214 if (vmptr == vmx->nested.vmxon_ptr)
5215 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
5216
5217 /* Forbid normal VMPTRLD if Enlightened version was used */
5218 if (vmx->nested.hv_evmcs)
5219 return 1;
5220
5221 if (vmx->nested.current_vmptr != vmptr) {
5222 struct kvm_host_map map;
5223 struct vmcs12 *new_vmcs12;
5224
5225 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
5226 /*
5227 * Reads from an unbacked page return all 1s,
5228 * which means that the 32 bits located at the
5229 * given physical address won't match the required
5230 * VMCS12_REVISION identifier.
5231 */
5232 return nested_vmx_fail(vcpu,
5233 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5234 }
5235
5236 new_vmcs12 = map.hva;
5237
5238 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5239 (new_vmcs12->hdr.shadow_vmcs &&
5240 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
5241 kvm_vcpu_unmap(vcpu, &map, false);
5242 return nested_vmx_fail(vcpu,
5243 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5244 }
5245
5246 nested_release_vmcs12(vcpu);
5247
5248 /*
5249 * Load VMCS12 from guest memory since it is not already
5250 * cached.
5251 */
5252 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
5253 kvm_vcpu_unmap(vcpu, &map, false);
5254
5255 set_current_vmptr(vmx, vmptr);
5256 }
5257
5258 return nested_vmx_succeed(vcpu);
5259 }
5260
5261 /* Emulate the VMPTRST instruction */
handle_vmptrst(struct kvm_vcpu * vcpu)5262 static int handle_vmptrst(struct kvm_vcpu *vcpu)
5263 {
5264 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
5265 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5266 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5267 struct x86_exception e;
5268 gva_t gva;
5269 int r;
5270
5271 if (!nested_vmx_check_permission(vcpu))
5272 return 1;
5273
5274 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
5275 return 1;
5276
5277 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5278 true, sizeof(gpa_t), &gva))
5279 return 1;
5280 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
5281 r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr,
5282 sizeof(gpa_t), &e);
5283 if (r != X86EMUL_CONTINUE)
5284 return kvm_handle_memory_failure(vcpu, r, &e);
5285
5286 return nested_vmx_succeed(vcpu);
5287 }
5288
5289 #define EPTP_PA_MASK GENMASK_ULL(51, 12)
5290
nested_ept_root_matches(hpa_t root_hpa,u64 root_eptp,u64 eptp)5291 static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
5292 {
5293 return VALID_PAGE(root_hpa) &&
5294 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
5295 }
5296
5297 /* Emulate the INVEPT instruction */
handle_invept(struct kvm_vcpu * vcpu)5298 static int handle_invept(struct kvm_vcpu *vcpu)
5299 {
5300 struct vcpu_vmx *vmx = to_vmx(vcpu);
5301 u32 vmx_instruction_info, types;
5302 unsigned long type, roots_to_free;
5303 struct kvm_mmu *mmu;
5304 gva_t gva;
5305 struct x86_exception e;
5306 struct {
5307 u64 eptp, gpa;
5308 } operand;
5309 int i, r;
5310
5311 if (!(vmx->nested.msrs.secondary_ctls_high &
5312 SECONDARY_EXEC_ENABLE_EPT) ||
5313 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5314 kvm_queue_exception(vcpu, UD_VECTOR);
5315 return 1;
5316 }
5317
5318 if (!nested_vmx_check_permission(vcpu))
5319 return 1;
5320
5321 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5322 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5323
5324 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5325
5326 if (type >= 32 || !(types & (1 << type)))
5327 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5328
5329 /* According to the Intel VMX instruction reference, the memory
5330 * operand is read even if it isn't needed (e.g., for type==global)
5331 */
5332 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
5333 vmx_instruction_info, false, sizeof(operand), &gva))
5334 return 1;
5335 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5336 if (r != X86EMUL_CONTINUE)
5337 return kvm_handle_memory_failure(vcpu, r, &e);
5338
5339 /*
5340 * Nested EPT roots are always held through guest_mmu,
5341 * not root_mmu.
5342 */
5343 mmu = &vcpu->arch.guest_mmu;
5344
5345 switch (type) {
5346 case VMX_EPT_EXTENT_CONTEXT:
5347 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
5348 return nested_vmx_fail(vcpu,
5349 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5350
5351 roots_to_free = 0;
5352 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
5353 operand.eptp))
5354 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5355
5356 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5357 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
5358 mmu->prev_roots[i].pgd,
5359 operand.eptp))
5360 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5361 }
5362 break;
5363 case VMX_EPT_EXTENT_GLOBAL:
5364 roots_to_free = KVM_MMU_ROOTS_ALL;
5365 break;
5366 default:
5367 BUG();
5368 break;
5369 }
5370
5371 if (roots_to_free)
5372 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5373
5374 return nested_vmx_succeed(vcpu);
5375 }
5376
handle_invvpid(struct kvm_vcpu * vcpu)5377 static int handle_invvpid(struct kvm_vcpu *vcpu)
5378 {
5379 struct vcpu_vmx *vmx = to_vmx(vcpu);
5380 u32 vmx_instruction_info;
5381 unsigned long type, types;
5382 gva_t gva;
5383 struct x86_exception e;
5384 struct {
5385 u64 vpid;
5386 u64 gla;
5387 } operand;
5388 u16 vpid02;
5389 int r;
5390
5391 if (!(vmx->nested.msrs.secondary_ctls_high &
5392 SECONDARY_EXEC_ENABLE_VPID) ||
5393 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5394 kvm_queue_exception(vcpu, UD_VECTOR);
5395 return 1;
5396 }
5397
5398 if (!nested_vmx_check_permission(vcpu))
5399 return 1;
5400
5401 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5402 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5403
5404 types = (vmx->nested.msrs.vpid_caps &
5405 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5406
5407 if (type >= 32 || !(types & (1 << type)))
5408 return nested_vmx_fail(vcpu,
5409 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5410
5411 /* according to the intel vmx instruction reference, the memory
5412 * operand is read even if it isn't needed (e.g., for type==global)
5413 */
5414 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
5415 vmx_instruction_info, false, sizeof(operand), &gva))
5416 return 1;
5417 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5418 if (r != X86EMUL_CONTINUE)
5419 return kvm_handle_memory_failure(vcpu, r, &e);
5420
5421 if (operand.vpid >> 16)
5422 return nested_vmx_fail(vcpu,
5423 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5424
5425 vpid02 = nested_get_vpid02(vcpu);
5426 switch (type) {
5427 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5428 if (!operand.vpid ||
5429 is_noncanonical_address(operand.gla, vcpu))
5430 return nested_vmx_fail(vcpu,
5431 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5432 vpid_sync_vcpu_addr(vpid02, operand.gla);
5433 break;
5434 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5435 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5436 if (!operand.vpid)
5437 return nested_vmx_fail(vcpu,
5438 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5439 vpid_sync_context(vpid02);
5440 break;
5441 case VMX_VPID_EXTENT_ALL_CONTEXT:
5442 vpid_sync_context(vpid02);
5443 break;
5444 default:
5445 WARN_ON_ONCE(1);
5446 return kvm_skip_emulated_instruction(vcpu);
5447 }
5448
5449 /*
5450 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
5451 * linear mappings for L2 (tagged with L2's VPID). Free all roots as
5452 * VPIDs are not tracked in the MMU role.
5453 *
5454 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5455 * an MMU when EPT is disabled.
5456 *
5457 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5458 */
5459 if (!enable_ept)
5460 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu,
5461 KVM_MMU_ROOTS_ALL);
5462
5463 return nested_vmx_succeed(vcpu);
5464 }
5465
nested_vmx_eptp_switching(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)5466 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5467 struct vmcs12 *vmcs12)
5468 {
5469 u32 index = kvm_rcx_read(vcpu);
5470 u64 new_eptp;
5471
5472 if (!nested_cpu_has_eptp_switching(vmcs12) ||
5473 !nested_cpu_has_ept(vmcs12))
5474 return 1;
5475
5476 if (index >= VMFUNC_EPTP_ENTRIES)
5477 return 1;
5478
5479 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
5480 &new_eptp, index * 8, 8))
5481 return 1;
5482
5483 /*
5484 * If the (L2) guest does a vmfunc to the currently
5485 * active ept pointer, we don't have to do anything else
5486 */
5487 if (vmcs12->ept_pointer != new_eptp) {
5488 if (!nested_vmx_check_eptp(vcpu, new_eptp))
5489 return 1;
5490
5491 vmcs12->ept_pointer = new_eptp;
5492
5493 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
5494 }
5495
5496 return 0;
5497 }
5498
handle_vmfunc(struct kvm_vcpu * vcpu)5499 static int handle_vmfunc(struct kvm_vcpu *vcpu)
5500 {
5501 struct vcpu_vmx *vmx = to_vmx(vcpu);
5502 struct vmcs12 *vmcs12;
5503 u32 function = kvm_rax_read(vcpu);
5504
5505 /*
5506 * VMFUNC is only supported for nested guests, but we always enable the
5507 * secondary control for simplicity; for non-nested mode, fake that we
5508 * didn't by injecting #UD.
5509 */
5510 if (!is_guest_mode(vcpu)) {
5511 kvm_queue_exception(vcpu, UD_VECTOR);
5512 return 1;
5513 }
5514
5515 vmcs12 = get_vmcs12(vcpu);
5516 if (!(vmcs12->vm_function_control & BIT_ULL(function)))
5517 goto fail;
5518
5519 switch (function) {
5520 case 0:
5521 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5522 goto fail;
5523 break;
5524 default:
5525 goto fail;
5526 }
5527 return kvm_skip_emulated_instruction(vcpu);
5528
5529 fail:
5530 /*
5531 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5532 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5533 * EXIT_REASON_VMFUNC as the exit reason.
5534 */
5535 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
5536 vmx_get_intr_info(vcpu),
5537 vmx_get_exit_qual(vcpu));
5538 return 1;
5539 }
5540
5541 /*
5542 * Return true if an IO instruction with the specified port and size should cause
5543 * a VM-exit into L1.
5544 */
nested_vmx_check_io_bitmaps(struct kvm_vcpu * vcpu,unsigned int port,int size)5545 bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5546 int size)
5547 {
5548 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5549 gpa_t bitmap, last_bitmap;
5550 u8 b;
5551
5552 last_bitmap = (gpa_t)-1;
5553 b = -1;
5554
5555 while (size > 0) {
5556 if (port < 0x8000)
5557 bitmap = vmcs12->io_bitmap_a;
5558 else if (port < 0x10000)
5559 bitmap = vmcs12->io_bitmap_b;
5560 else
5561 return true;
5562 bitmap += (port & 0x7fff) / 8;
5563
5564 if (last_bitmap != bitmap)
5565 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5566 return true;
5567 if (b & (1 << (port & 7)))
5568 return true;
5569
5570 port++;
5571 size--;
5572 last_bitmap = bitmap;
5573 }
5574
5575 return false;
5576 }
5577
nested_vmx_exit_handled_io(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)5578 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5579 struct vmcs12 *vmcs12)
5580 {
5581 unsigned long exit_qualification;
5582 unsigned short port;
5583 int size;
5584
5585 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5586 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5587
5588 exit_qualification = vmx_get_exit_qual(vcpu);
5589
5590 port = exit_qualification >> 16;
5591 size = (exit_qualification & 7) + 1;
5592
5593 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5594 }
5595
5596 /*
5597 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
5598 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5599 * disinterest in the current event (read or write a specific MSR) by using an
5600 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5601 */
nested_vmx_exit_handled_msr(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,union vmx_exit_reason exit_reason)5602 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
5603 struct vmcs12 *vmcs12,
5604 union vmx_exit_reason exit_reason)
5605 {
5606 u32 msr_index = kvm_rcx_read(vcpu);
5607 gpa_t bitmap;
5608
5609 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5610 return true;
5611
5612 /*
5613 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5614 * for the four combinations of read/write and low/high MSR numbers.
5615 * First we need to figure out which of the four to use:
5616 */
5617 bitmap = vmcs12->msr_bitmap;
5618 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
5619 bitmap += 2048;
5620 if (msr_index >= 0xc0000000) {
5621 msr_index -= 0xc0000000;
5622 bitmap += 1024;
5623 }
5624
5625 /* Then read the msr_index'th bit from this bitmap: */
5626 if (msr_index < 1024*8) {
5627 unsigned char b;
5628 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5629 return true;
5630 return 1 & (b >> (msr_index & 7));
5631 } else
5632 return true; /* let L1 handle the wrong parameter */
5633 }
5634
5635 /*
5636 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5637 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5638 * intercept (via guest_host_mask etc.) the current event.
5639 */
nested_vmx_exit_handled_cr(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12)5640 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5641 struct vmcs12 *vmcs12)
5642 {
5643 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
5644 int cr = exit_qualification & 15;
5645 int reg;
5646 unsigned long val;
5647
5648 switch ((exit_qualification >> 4) & 3) {
5649 case 0: /* mov to cr */
5650 reg = (exit_qualification >> 8) & 15;
5651 val = kvm_register_readl(vcpu, reg);
5652 switch (cr) {
5653 case 0:
5654 if (vmcs12->cr0_guest_host_mask &
5655 (val ^ vmcs12->cr0_read_shadow))
5656 return true;
5657 break;
5658 case 3:
5659 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5660 return true;
5661 break;
5662 case 4:
5663 if (vmcs12->cr4_guest_host_mask &
5664 (vmcs12->cr4_read_shadow ^ val))
5665 return true;
5666 break;
5667 case 8:
5668 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5669 return true;
5670 break;
5671 }
5672 break;
5673 case 2: /* clts */
5674 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5675 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5676 return true;
5677 break;
5678 case 1: /* mov from cr */
5679 switch (cr) {
5680 case 3:
5681 if (vmcs12->cpu_based_vm_exec_control &
5682 CPU_BASED_CR3_STORE_EXITING)
5683 return true;
5684 break;
5685 case 8:
5686 if (vmcs12->cpu_based_vm_exec_control &
5687 CPU_BASED_CR8_STORE_EXITING)
5688 return true;
5689 break;
5690 }
5691 break;
5692 case 3: /* lmsw */
5693 /*
5694 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5695 * cr0. Other attempted changes are ignored, with no exit.
5696 */
5697 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5698 if (vmcs12->cr0_guest_host_mask & 0xe &
5699 (val ^ vmcs12->cr0_read_shadow))
5700 return true;
5701 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5702 !(vmcs12->cr0_read_shadow & 0x1) &&
5703 (val & 0x1))
5704 return true;
5705 break;
5706 }
5707 return false;
5708 }
5709
nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu * vcpu,struct vmcs12 * vmcs12,gpa_t bitmap)5710 static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5711 struct vmcs12 *vmcs12, gpa_t bitmap)
5712 {
5713 u32 vmx_instruction_info;
5714 unsigned long field;
5715 u8 b;
5716
5717 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5718 return true;
5719
5720 /* Decode instruction info and find the field to access */
5721 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5722 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5723
5724 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5725 if (field >> 15)
5726 return true;
5727
5728 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5729 return true;
5730
5731 return 1 & (b >> (field & 7));
5732 }
5733
nested_vmx_exit_handled_mtf(struct vmcs12 * vmcs12)5734 static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5735 {
5736 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5737
5738 if (nested_cpu_has_mtf(vmcs12))
5739 return true;
5740
5741 /*
5742 * An MTF VM-exit may be injected into the guest by setting the
5743 * interruption-type to 7 (other event) and the vector field to 0. Such
5744 * is the case regardless of the 'monitor trap flag' VM-execution
5745 * control.
5746 */
5747 return entry_intr_info == (INTR_INFO_VALID_MASK
5748 | INTR_TYPE_OTHER_EVENT);
5749 }
5750
5751 /*
5752 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5753 * L1 wants the exit. Only call this when in is_guest_mode (L2).
5754 */
nested_vmx_l0_wants_exit(struct kvm_vcpu * vcpu,union vmx_exit_reason exit_reason)5755 static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
5756 union vmx_exit_reason exit_reason)
5757 {
5758 u32 intr_info;
5759
5760 switch ((u16)exit_reason.basic) {
5761 case EXIT_REASON_EXCEPTION_NMI:
5762 intr_info = vmx_get_intr_info(vcpu);
5763 if (is_nmi(intr_info))
5764 return true;
5765 else if (is_page_fault(intr_info))
5766 return vcpu->arch.apf.host_apf_flags ||
5767 vmx_need_pf_intercept(vcpu);
5768 else if (is_debug(intr_info) &&
5769 vcpu->guest_debug &
5770 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5771 return true;
5772 else if (is_breakpoint(intr_info) &&
5773 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5774 return true;
5775 else if (is_alignment_check(intr_info) &&
5776 !vmx_guest_inject_ac(vcpu))
5777 return true;
5778 return false;
5779 case EXIT_REASON_EXTERNAL_INTERRUPT:
5780 return true;
5781 case EXIT_REASON_MCE_DURING_VMENTRY:
5782 return true;
5783 case EXIT_REASON_EPT_VIOLATION:
5784 /*
5785 * L0 always deals with the EPT violation. If nested EPT is
5786 * used, and the nested mmu code discovers that the address is
5787 * missing in the guest EPT table (EPT12), the EPT violation
5788 * will be injected with nested_ept_inject_page_fault()
5789 */
5790 return true;
5791 case EXIT_REASON_EPT_MISCONFIG:
5792 /*
5793 * L2 never uses directly L1's EPT, but rather L0's own EPT
5794 * table (shadow on EPT) or a merged EPT table that L0 built
5795 * (EPT on EPT). So any problems with the structure of the
5796 * table is L0's fault.
5797 */
5798 return true;
5799 case EXIT_REASON_PREEMPTION_TIMER:
5800 return true;
5801 case EXIT_REASON_PML_FULL:
5802 /* We emulate PML support to L1. */
5803 return true;
5804 case EXIT_REASON_VMFUNC:
5805 /* VM functions are emulated through L2->L0 vmexits. */
5806 return true;
5807 case EXIT_REASON_ENCLS:
5808 /* SGX is never exposed to L1 */
5809 return true;
5810 default:
5811 break;
5812 }
5813 return false;
5814 }
5815
5816 /*
5817 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5818 * is_guest_mode (L2).
5819 */
nested_vmx_l1_wants_exit(struct kvm_vcpu * vcpu,union vmx_exit_reason exit_reason)5820 static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
5821 union vmx_exit_reason exit_reason)
5822 {
5823 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5824 u32 intr_info;
5825
5826 switch ((u16)exit_reason.basic) {
5827 case EXIT_REASON_EXCEPTION_NMI:
5828 intr_info = vmx_get_intr_info(vcpu);
5829 if (is_nmi(intr_info))
5830 return true;
5831 else if (is_page_fault(intr_info))
5832 return true;
5833 return vmcs12->exception_bitmap &
5834 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5835 case EXIT_REASON_EXTERNAL_INTERRUPT:
5836 return nested_exit_on_intr(vcpu);
5837 case EXIT_REASON_TRIPLE_FAULT:
5838 return true;
5839 case EXIT_REASON_INTERRUPT_WINDOW:
5840 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
5841 case EXIT_REASON_NMI_WINDOW:
5842 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
5843 case EXIT_REASON_TASK_SWITCH:
5844 return true;
5845 case EXIT_REASON_CPUID:
5846 return true;
5847 case EXIT_REASON_HLT:
5848 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5849 case EXIT_REASON_INVD:
5850 return true;
5851 case EXIT_REASON_INVLPG:
5852 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5853 case EXIT_REASON_RDPMC:
5854 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5855 case EXIT_REASON_RDRAND:
5856 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5857 case EXIT_REASON_RDSEED:
5858 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5859 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5860 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5861 case EXIT_REASON_VMREAD:
5862 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5863 vmcs12->vmread_bitmap);
5864 case EXIT_REASON_VMWRITE:
5865 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5866 vmcs12->vmwrite_bitmap);
5867 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5868 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5869 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5870 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5871 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5872 /*
5873 * VMX instructions trap unconditionally. This allows L1 to
5874 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5875 */
5876 return true;
5877 case EXIT_REASON_CR_ACCESS:
5878 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5879 case EXIT_REASON_DR_ACCESS:
5880 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5881 case EXIT_REASON_IO_INSTRUCTION:
5882 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5883 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5884 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5885 case EXIT_REASON_MSR_READ:
5886 case EXIT_REASON_MSR_WRITE:
5887 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5888 case EXIT_REASON_INVALID_STATE:
5889 return true;
5890 case EXIT_REASON_MWAIT_INSTRUCTION:
5891 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5892 case EXIT_REASON_MONITOR_TRAP_FLAG:
5893 return nested_vmx_exit_handled_mtf(vmcs12);
5894 case EXIT_REASON_MONITOR_INSTRUCTION:
5895 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5896 case EXIT_REASON_PAUSE_INSTRUCTION:
5897 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5898 nested_cpu_has2(vmcs12,
5899 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5900 case EXIT_REASON_MCE_DURING_VMENTRY:
5901 return true;
5902 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5903 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5904 case EXIT_REASON_APIC_ACCESS:
5905 case EXIT_REASON_APIC_WRITE:
5906 case EXIT_REASON_EOI_INDUCED:
5907 /*
5908 * The controls for "virtualize APIC accesses," "APIC-
5909 * register virtualization," and "virtual-interrupt
5910 * delivery" only come from vmcs12.
5911 */
5912 return true;
5913 case EXIT_REASON_INVPCID:
5914 return
5915 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5916 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5917 case EXIT_REASON_WBINVD:
5918 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5919 case EXIT_REASON_XSETBV:
5920 return true;
5921 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5922 /*
5923 * This should never happen, since it is not possible to
5924 * set XSS to a non-zero value---neither in L1 nor in L2.
5925 * If if it were, XSS would have to be checked against
5926 * the XSS exit bitmap in vmcs12.
5927 */
5928 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
5929 case EXIT_REASON_UMWAIT:
5930 case EXIT_REASON_TPAUSE:
5931 return nested_cpu_has2(vmcs12,
5932 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
5933 default:
5934 return true;
5935 }
5936 }
5937
5938 /*
5939 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
5940 * reflected into L1.
5941 */
nested_vmx_reflect_vmexit(struct kvm_vcpu * vcpu)5942 bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
5943 {
5944 struct vcpu_vmx *vmx = to_vmx(vcpu);
5945 union vmx_exit_reason exit_reason = vmx->exit_reason;
5946 unsigned long exit_qual;
5947 u32 exit_intr_info;
5948
5949 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5950
5951 /*
5952 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
5953 * has already loaded L2's state.
5954 */
5955 if (unlikely(vmx->fail)) {
5956 trace_kvm_nested_vmenter_failed(
5957 "hardware VM-instruction error: ",
5958 vmcs_read32(VM_INSTRUCTION_ERROR));
5959 exit_intr_info = 0;
5960 exit_qual = 0;
5961 goto reflect_vmexit;
5962 }
5963
5964 trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX);
5965
5966 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
5967 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
5968 return false;
5969
5970 /* If L1 doesn't want the exit, handle it in L0. */
5971 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
5972 return false;
5973
5974 /*
5975 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
5976 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
5977 * need to be synthesized by querying the in-kernel LAPIC, but external
5978 * interrupts are never reflected to L1 so it's a non-issue.
5979 */
5980 exit_intr_info = vmx_get_intr_info(vcpu);
5981 if (is_exception_with_error_code(exit_intr_info)) {
5982 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5983
5984 vmcs12->vm_exit_intr_error_code =
5985 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5986 }
5987 exit_qual = vmx_get_exit_qual(vcpu);
5988
5989 reflect_vmexit:
5990 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
5991 return true;
5992 }
5993
vmx_get_nested_state(struct kvm_vcpu * vcpu,struct kvm_nested_state __user * user_kvm_nested_state,u32 user_data_size)5994 static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
5995 struct kvm_nested_state __user *user_kvm_nested_state,
5996 u32 user_data_size)
5997 {
5998 struct vcpu_vmx *vmx;
5999 struct vmcs12 *vmcs12;
6000 struct kvm_nested_state kvm_state = {
6001 .flags = 0,
6002 .format = KVM_STATE_NESTED_FORMAT_VMX,
6003 .size = sizeof(kvm_state),
6004 .hdr.vmx.flags = 0,
6005 .hdr.vmx.vmxon_pa = -1ull,
6006 .hdr.vmx.vmcs12_pa = -1ull,
6007 .hdr.vmx.preemption_timer_deadline = 0,
6008 };
6009 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6010 &user_kvm_nested_state->data.vmx[0];
6011
6012 if (!vcpu)
6013 return kvm_state.size + sizeof(*user_vmx_nested_state);
6014
6015 vmx = to_vmx(vcpu);
6016 vmcs12 = get_vmcs12(vcpu);
6017
6018 if (nested_vmx_allowed(vcpu) &&
6019 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
6020 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6021 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
6022
6023 if (vmx_has_valid_vmcs12(vcpu)) {
6024 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
6025
6026 if (vmx->nested.hv_evmcs)
6027 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6028
6029 if (is_guest_mode(vcpu) &&
6030 nested_cpu_has_shadow_vmcs(vmcs12) &&
6031 vmcs12->vmcs_link_pointer != -1ull)
6032 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
6033 }
6034
6035 if (vmx->nested.smm.vmxon)
6036 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
6037
6038 if (vmx->nested.smm.guest_mode)
6039 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
6040
6041 if (is_guest_mode(vcpu)) {
6042 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6043
6044 if (vmx->nested.nested_run_pending)
6045 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
6046
6047 if (vmx->nested.mtf_pending)
6048 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
6049
6050 if (nested_cpu_has_preemption_timer(vmcs12) &&
6051 vmx->nested.has_preemption_timer_deadline) {
6052 kvm_state.hdr.vmx.flags |=
6053 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6054 kvm_state.hdr.vmx.preemption_timer_deadline =
6055 vmx->nested.preemption_timer_deadline;
6056 }
6057 }
6058 }
6059
6060 if (user_data_size < kvm_state.size)
6061 goto out;
6062
6063 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6064 return -EFAULT;
6065
6066 if (!vmx_has_valid_vmcs12(vcpu))
6067 goto out;
6068
6069 /*
6070 * When running L2, the authoritative vmcs12 state is in the
6071 * vmcs02. When running L1, the authoritative vmcs12 state is
6072 * in the shadow or enlightened vmcs linked to vmcs01, unless
6073 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
6074 * vmcs12 state is in the vmcs12 already.
6075 */
6076 if (is_guest_mode(vcpu)) {
6077 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
6078 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
6079 } else {
6080 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6081 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
6082 if (vmx->nested.hv_evmcs)
6083 copy_enlightened_to_vmcs12(vmx);
6084 else if (enable_shadow_vmcs)
6085 copy_shadow_to_vmcs12(vmx);
6086 }
6087 }
6088
6089 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6090 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6091
6092 /*
6093 * Copy over the full allocated size of vmcs12 rather than just the size
6094 * of the struct.
6095 */
6096 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
6097 return -EFAULT;
6098
6099 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6100 vmcs12->vmcs_link_pointer != -1ull) {
6101 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
6102 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
6103 return -EFAULT;
6104 }
6105 out:
6106 return kvm_state.size;
6107 }
6108
6109 /*
6110 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6111 */
vmx_leave_nested(struct kvm_vcpu * vcpu)6112 void vmx_leave_nested(struct kvm_vcpu *vcpu)
6113 {
6114 if (is_guest_mode(vcpu)) {
6115 to_vmx(vcpu)->nested.nested_run_pending = 0;
6116 nested_vmx_vmexit(vcpu, -1, 0, 0);
6117 }
6118 free_nested(vcpu);
6119 }
6120
vmx_set_nested_state(struct kvm_vcpu * vcpu,struct kvm_nested_state __user * user_kvm_nested_state,struct kvm_nested_state * kvm_state)6121 static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6122 struct kvm_nested_state __user *user_kvm_nested_state,
6123 struct kvm_nested_state *kvm_state)
6124 {
6125 struct vcpu_vmx *vmx = to_vmx(vcpu);
6126 struct vmcs12 *vmcs12;
6127 enum vm_entry_failure_code ignored;
6128 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6129 &user_kvm_nested_state->data.vmx[0];
6130 int ret;
6131
6132 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
6133 return -EINVAL;
6134
6135 if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
6136 if (kvm_state->hdr.vmx.smm.flags)
6137 return -EINVAL;
6138
6139 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
6140 return -EINVAL;
6141
6142 /*
6143 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6144 * enable eVMCS capability on vCPU. However, since then
6145 * code was changed such that flag signals vmcs12 should
6146 * be copied into eVMCS in guest memory.
6147 *
6148 * To preserve backwards compatability, allow user
6149 * to set this flag even when there is no VMXON region.
6150 */
6151 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6152 return -EINVAL;
6153 } else {
6154 if (!nested_vmx_allowed(vcpu))
6155 return -EINVAL;
6156
6157 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6158 return -EINVAL;
6159 }
6160
6161 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6162 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6163 return -EINVAL;
6164
6165 if (kvm_state->hdr.vmx.smm.flags &
6166 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6167 return -EINVAL;
6168
6169 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6170 return -EINVAL;
6171
6172 /*
6173 * SMM temporarily disables VMX, so we cannot be in guest mode,
6174 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6175 * must be zero.
6176 */
6177 if (is_smm(vcpu) ?
6178 (kvm_state->flags &
6179 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6180 : kvm_state->hdr.vmx.smm.flags)
6181 return -EINVAL;
6182
6183 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6184 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
6185 return -EINVAL;
6186
6187 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6188 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
6189 return -EINVAL;
6190
6191 vmx_leave_nested(vcpu);
6192
6193 if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
6194 return 0;
6195
6196 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
6197 ret = enter_vmx_operation(vcpu);
6198 if (ret)
6199 return ret;
6200
6201 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6202 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6203 /* See vmx_has_valid_vmcs12. */
6204 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6205 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
6206 (kvm_state->hdr.vmx.vmcs12_pa != -1ull))
6207 return -EINVAL;
6208 else
6209 return 0;
6210 }
6211
6212 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
6213 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6214 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
6215 return -EINVAL;
6216
6217 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
6218 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6219 /*
6220 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6221 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6222 * restored yet. EVMCS will be mapped from
6223 * nested_get_vmcs12_pages().
6224 */
6225 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
6226 } else {
6227 return -EINVAL;
6228 }
6229
6230 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
6231 vmx->nested.smm.vmxon = true;
6232 vmx->nested.vmxon = false;
6233
6234 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
6235 vmx->nested.smm.guest_mode = true;
6236 }
6237
6238 vmcs12 = get_vmcs12(vcpu);
6239 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
6240 return -EFAULT;
6241
6242 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6243 return -EINVAL;
6244
6245 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6246 return 0;
6247
6248 vmx->nested.nested_run_pending =
6249 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6250
6251 vmx->nested.mtf_pending =
6252 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6253
6254 ret = -EINVAL;
6255 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6256 vmcs12->vmcs_link_pointer != -1ull) {
6257 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6258
6259 if (kvm_state->size <
6260 sizeof(*kvm_state) +
6261 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
6262 goto error_guest_mode;
6263
6264 if (copy_from_user(shadow_vmcs12,
6265 user_vmx_nested_state->shadow_vmcs12,
6266 sizeof(*shadow_vmcs12))) {
6267 ret = -EFAULT;
6268 goto error_guest_mode;
6269 }
6270
6271 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6272 !shadow_vmcs12->hdr.shadow_vmcs)
6273 goto error_guest_mode;
6274 }
6275
6276 vmx->nested.has_preemption_timer_deadline = false;
6277 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6278 vmx->nested.has_preemption_timer_deadline = true;
6279 vmx->nested.preemption_timer_deadline =
6280 kvm_state->hdr.vmx.preemption_timer_deadline;
6281 }
6282
6283 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6284 nested_vmx_check_host_state(vcpu, vmcs12) ||
6285 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
6286 goto error_guest_mode;
6287
6288 vmx->nested.dirty_vmcs12 = true;
6289 ret = nested_vmx_enter_non_root_mode(vcpu, false);
6290 if (ret)
6291 goto error_guest_mode;
6292
6293 return 0;
6294
6295 error_guest_mode:
6296 vmx->nested.nested_run_pending = 0;
6297 return ret;
6298 }
6299
nested_vmx_set_vmcs_shadowing_bitmap(void)6300 void nested_vmx_set_vmcs_shadowing_bitmap(void)
6301 {
6302 if (enable_shadow_vmcs) {
6303 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
6304 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
6305 }
6306 }
6307
6308 /*
6309 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6310 * returned for the various VMX controls MSRs when nested VMX is enabled.
6311 * The same values should also be used to verify that vmcs12 control fields are
6312 * valid during nested entry from L1 to L2.
6313 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6314 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6315 * bit in the high half is on if the corresponding bit in the control field
6316 * may be on. See also vmx_control_verify().
6317 */
nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs * msrs,u32 ept_caps)6318 void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
6319 {
6320 /*
6321 * Note that as a general rule, the high half of the MSRs (bits in
6322 * the control fields which may be 1) should be initialized by the
6323 * intersection of the underlying hardware's MSR (i.e., features which
6324 * can be supported) and the list of features we want to expose -
6325 * because they are known to be properly supported in our code.
6326 * Also, usually, the low half of the MSRs (bits which must be 1) can
6327 * be set to 0, meaning that L1 may turn off any of these bits. The
6328 * reason is that if one of these bits is necessary, it will appear
6329 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6330 * fields of vmcs01 and vmcs02, will turn these bits off - and
6331 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
6332 * These rules have exceptions below.
6333 */
6334
6335 /* pin-based controls */
6336 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6337 msrs->pinbased_ctls_low,
6338 msrs->pinbased_ctls_high);
6339 msrs->pinbased_ctls_low |=
6340 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6341 msrs->pinbased_ctls_high &=
6342 PIN_BASED_EXT_INTR_MASK |
6343 PIN_BASED_NMI_EXITING |
6344 PIN_BASED_VIRTUAL_NMIS |
6345 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
6346 msrs->pinbased_ctls_high |=
6347 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6348 PIN_BASED_VMX_PREEMPTION_TIMER;
6349
6350 /* exit controls */
6351 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6352 msrs->exit_ctls_low,
6353 msrs->exit_ctls_high);
6354 msrs->exit_ctls_low =
6355 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6356
6357 msrs->exit_ctls_high &=
6358 #ifdef CONFIG_X86_64
6359 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6360 #endif
6361 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6362 VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
6363 msrs->exit_ctls_high |=
6364 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6365 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6366 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6367
6368 /* We support free control of debug control saving. */
6369 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6370
6371 /* entry controls */
6372 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6373 msrs->entry_ctls_low,
6374 msrs->entry_ctls_high);
6375 msrs->entry_ctls_low =
6376 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6377 msrs->entry_ctls_high &=
6378 #ifdef CONFIG_X86_64
6379 VM_ENTRY_IA32E_MODE |
6380 #endif
6381 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS |
6382 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
6383 msrs->entry_ctls_high |=
6384 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6385
6386 /* We support free control of debug control loading. */
6387 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6388
6389 /* cpu-based controls */
6390 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6391 msrs->procbased_ctls_low,
6392 msrs->procbased_ctls_high);
6393 msrs->procbased_ctls_low =
6394 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6395 msrs->procbased_ctls_high &=
6396 CPU_BASED_INTR_WINDOW_EXITING |
6397 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
6398 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6399 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6400 CPU_BASED_CR3_STORE_EXITING |
6401 #ifdef CONFIG_X86_64
6402 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6403 #endif
6404 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6405 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6406 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6407 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6408 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6409 /*
6410 * We can allow some features even when not supported by the
6411 * hardware. For example, L1 can specify an MSR bitmap - and we
6412 * can use it to avoid exits to L1 - even when L0 runs L2
6413 * without MSR bitmaps.
6414 */
6415 msrs->procbased_ctls_high |=
6416 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6417 CPU_BASED_USE_MSR_BITMAPS;
6418
6419 /* We support free control of CR3 access interception. */
6420 msrs->procbased_ctls_low &=
6421 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6422
6423 /*
6424 * secondary cpu-based controls. Do not include those that
6425 * depend on CPUID bits, they are added later by
6426 * vmx_vcpu_after_set_cpuid.
6427 */
6428 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6429 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6430 msrs->secondary_ctls_low,
6431 msrs->secondary_ctls_high);
6432
6433 msrs->secondary_ctls_low = 0;
6434 msrs->secondary_ctls_high &=
6435 SECONDARY_EXEC_DESC |
6436 SECONDARY_EXEC_ENABLE_RDTSCP |
6437 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
6438 SECONDARY_EXEC_WBINVD_EXITING |
6439 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6440 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
6441 SECONDARY_EXEC_RDRAND_EXITING |
6442 SECONDARY_EXEC_ENABLE_INVPCID |
6443 SECONDARY_EXEC_RDSEED_EXITING |
6444 SECONDARY_EXEC_XSAVES;
6445
6446 /*
6447 * We can emulate "VMCS shadowing," even if the hardware
6448 * doesn't support it.
6449 */
6450 msrs->secondary_ctls_high |=
6451 SECONDARY_EXEC_SHADOW_VMCS;
6452
6453 if (enable_ept) {
6454 /* nested EPT: emulate EPT also to L1 */
6455 msrs->secondary_ctls_high |=
6456 SECONDARY_EXEC_ENABLE_EPT;
6457 msrs->ept_caps =
6458 VMX_EPT_PAGE_WALK_4_BIT |
6459 VMX_EPT_PAGE_WALK_5_BIT |
6460 VMX_EPTP_WB_BIT |
6461 VMX_EPT_INVEPT_BIT |
6462 VMX_EPT_EXECUTE_ONLY_BIT;
6463
6464 msrs->ept_caps &= ept_caps;
6465 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6466 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6467 VMX_EPT_1GB_PAGE_BIT;
6468 if (enable_ept_ad_bits) {
6469 msrs->secondary_ctls_high |=
6470 SECONDARY_EXEC_ENABLE_PML;
6471 msrs->ept_caps |= VMX_EPT_AD_BIT;
6472 }
6473 }
6474
6475 if (cpu_has_vmx_vmfunc()) {
6476 msrs->secondary_ctls_high |=
6477 SECONDARY_EXEC_ENABLE_VMFUNC;
6478 /*
6479 * Advertise EPTP switching unconditionally
6480 * since we emulate it
6481 */
6482 if (enable_ept)
6483 msrs->vmfunc_controls =
6484 VMX_VMFUNC_EPTP_SWITCHING;
6485 }
6486
6487 /*
6488 * Old versions of KVM use the single-context version without
6489 * checking for support, so declare that it is supported even
6490 * though it is treated as global context. The alternative is
6491 * not failing the single-context invvpid, and it is worse.
6492 */
6493 if (enable_vpid) {
6494 msrs->secondary_ctls_high |=
6495 SECONDARY_EXEC_ENABLE_VPID;
6496 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6497 VMX_VPID_EXTENT_SUPPORTED_MASK;
6498 }
6499
6500 if (enable_unrestricted_guest)
6501 msrs->secondary_ctls_high |=
6502 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6503
6504 if (flexpriority_enabled)
6505 msrs->secondary_ctls_high |=
6506 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6507
6508 /* miscellaneous data */
6509 rdmsr(MSR_IA32_VMX_MISC,
6510 msrs->misc_low,
6511 msrs->misc_high);
6512 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6513 msrs->misc_low |=
6514 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6515 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
6516 VMX_MISC_ACTIVITY_HLT;
6517 msrs->misc_high = 0;
6518
6519 /*
6520 * This MSR reports some information about VMX support. We
6521 * should return information about the VMX we emulate for the
6522 * guest, and the VMCS structure we give it - not about the
6523 * VMX support of the underlying hardware.
6524 */
6525 msrs->basic =
6526 VMCS12_REVISION |
6527 VMX_BASIC_TRUE_CTLS |
6528 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6529 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6530
6531 if (cpu_has_vmx_basic_inout())
6532 msrs->basic |= VMX_BASIC_INOUT;
6533
6534 /*
6535 * These MSRs specify bits which the guest must keep fixed on
6536 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6537 * We picked the standard core2 setting.
6538 */
6539 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6540 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6541 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6542 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6543
6544 /* These MSRs specify bits which the guest must keep fixed off. */
6545 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6546 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6547
6548 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
6549 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
6550 }
6551
nested_vmx_hardware_unsetup(void)6552 void nested_vmx_hardware_unsetup(void)
6553 {
6554 int i;
6555
6556 if (enable_shadow_vmcs) {
6557 for (i = 0; i < VMX_BITMAP_NR; i++)
6558 free_page((unsigned long)vmx_bitmap[i]);
6559 }
6560 }
6561
nested_vmx_hardware_setup(int (* exit_handlers[])(struct kvm_vcpu *))6562 __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
6563 {
6564 int i;
6565
6566 if (!cpu_has_vmx_shadow_vmcs())
6567 enable_shadow_vmcs = 0;
6568 if (enable_shadow_vmcs) {
6569 for (i = 0; i < VMX_BITMAP_NR; i++) {
6570 /*
6571 * The vmx_bitmap is not tied to a VM and so should
6572 * not be charged to a memcg.
6573 */
6574 vmx_bitmap[i] = (unsigned long *)
6575 __get_free_page(GFP_KERNEL);
6576 if (!vmx_bitmap[i]) {
6577 nested_vmx_hardware_unsetup();
6578 return -ENOMEM;
6579 }
6580 }
6581
6582 init_vmcs_shadow_fields();
6583 }
6584
6585 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6586 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6587 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6588 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6589 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6590 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6591 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6592 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6593 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6594 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6595 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6596 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
6597
6598 return 0;
6599 }
6600
6601 struct kvm_x86_nested_ops vmx_nested_ops = {
6602 .check_events = vmx_check_nested_events,
6603 .hv_timer_pending = nested_vmx_preemption_timer_pending,
6604 .get_state = vmx_get_nested_state,
6605 .set_state = vmx_set_nested_state,
6606 .get_nested_state_pages = vmx_get_nested_state_pages,
6607 .write_log_dirty = nested_vmx_write_pml_buffer,
6608 .enable_evmcs = nested_enable_evmcs,
6609 .get_evmcs_version = nested_get_evmcs_version,
6610 };
6611