1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/objtool.h>
4 #include <linux/percpu.h>
6 #include <asm/debugreg.h>
7 #include <asm/mmu_context.h>
20 static bool __read_mostly enable_shadow_vmcs = 1;
21 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
23 static bool __read_mostly nested_early_check = 0;
24 module_param(nested_early_check, bool, S_IRUGO);
26 #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
29 * Hyper-V requires all of these, so mark them as supported even though
30 * they are just treated the same as all-context.
32 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
33 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
34 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
35 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
36 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
38 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
45 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
47 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
48 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
50 struct shadow_vmcs_field {
54 static struct shadow_vmcs_field shadow_read_only_fields[] = {
55 #define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) },
56 #include "vmcs_shadow_fields.h"
58 static int max_shadow_read_only_fields =
59 ARRAY_SIZE(shadow_read_only_fields);
61 static struct shadow_vmcs_field shadow_read_write_fields[] = {
62 #define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) },
63 #include "vmcs_shadow_fields.h"
65 static int max_shadow_read_write_fields =
66 ARRAY_SIZE(shadow_read_write_fields);
68 static void init_vmcs_shadow_fields(void)
72 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
73 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
75 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
76 struct shadow_vmcs_field entry = shadow_read_only_fields[i];
77 u16 field = entry.encoding;
79 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
80 (i + 1 == max_shadow_read_only_fields ||
81 shadow_read_only_fields[i + 1].encoding != field + 1))
82 pr_err("Missing field from shadow_read_only_field %x\n",
85 clear_bit(field, vmx_vmread_bitmap);
90 entry.offset += sizeof(u32);
92 shadow_read_only_fields[j++] = entry;
94 max_shadow_read_only_fields = j;
96 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
97 struct shadow_vmcs_field entry = shadow_read_write_fields[i];
98 u16 field = entry.encoding;
100 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
101 (i + 1 == max_shadow_read_write_fields ||
102 shadow_read_write_fields[i + 1].encoding != field + 1))
103 pr_err("Missing field from shadow_read_write_field %x\n",
106 WARN_ONCE(field >= GUEST_ES_AR_BYTES &&
107 field <= GUEST_TR_AR_BYTES,
108 "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
111 * PML and the preemption timer can be emulated, but the
112 * processor cannot vmwrite to fields that don't exist
116 case GUEST_PML_INDEX:
117 if (!cpu_has_vmx_pml())
120 case VMX_PREEMPTION_TIMER_VALUE:
121 if (!cpu_has_vmx_preemption_timer())
124 case GUEST_INTR_STATUS:
125 if (!cpu_has_vmx_apicv())
132 clear_bit(field, vmx_vmwrite_bitmap);
133 clear_bit(field, vmx_vmread_bitmap);
138 entry.offset += sizeof(u32);
140 shadow_read_write_fields[j++] = entry;
142 max_shadow_read_write_fields = j;
146 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
147 * set the success or error code of an emulated VMX instruction (as specified
148 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
151 static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
153 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
154 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
155 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
156 return kvm_skip_emulated_instruction(vcpu);
159 static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
161 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
162 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
163 X86_EFLAGS_SF | X86_EFLAGS_OF))
165 return kvm_skip_emulated_instruction(vcpu);
168 static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
169 u32 vm_instruction_error)
171 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
172 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
173 X86_EFLAGS_SF | X86_EFLAGS_OF))
175 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
177 * We don't need to force sync to shadow VMCS because
178 * VM_INSTRUCTION_ERROR is not shadowed. Enlightened VMCS 'shadows' all
179 * fields and thus must be synced.
181 if (to_vmx(vcpu)->nested.hv_evmcs_vmptr != EVMPTR_INVALID)
182 to_vmx(vcpu)->nested.need_vmcs12_to_shadow_sync = true;
184 return kvm_skip_emulated_instruction(vcpu);
187 static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error)
189 struct vcpu_vmx *vmx = to_vmx(vcpu);
192 * failValid writes the error number to the current VMCS, which
193 * can't be done if there isn't a current VMCS.
195 if (vmx->nested.current_vmptr == INVALID_GPA &&
196 !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
197 return nested_vmx_failInvalid(vcpu);
199 return nested_vmx_failValid(vcpu, vm_instruction_error);
202 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
204 /* TODO: not to reset guest simply here. */
205 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
206 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
209 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
211 return fixed_bits_valid(control, low, high);
214 static inline u64 vmx_control_msr(u32 low, u32 high)
216 return low | ((u64)high << 32);
219 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
221 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
222 vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA);
223 vmx->nested.need_vmcs12_to_shadow_sync = false;
226 static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
228 struct vcpu_vmx *vmx = to_vmx(vcpu);
230 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
231 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
232 vmx->nested.hv_evmcs = NULL;
235 vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID;
238 static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
239 struct loaded_vmcs *prev)
241 struct vmcs_host_state *dest, *src;
243 if (unlikely(!vmx->guest_state_loaded))
246 src = &prev->host_state;
247 dest = &vmx->loaded_vmcs->host_state;
249 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
250 dest->ldt_sel = src->ldt_sel;
252 dest->ds_sel = src->ds_sel;
253 dest->es_sel = src->es_sel;
257 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
259 struct vcpu_vmx *vmx = to_vmx(vcpu);
260 struct loaded_vmcs *prev;
263 if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs))
267 prev = vmx->loaded_vmcs;
268 vmx->loaded_vmcs = vmcs;
269 vmx_vcpu_load_vmcs(vcpu, cpu, prev);
270 vmx_sync_vmcs_host_state(vmx, prev);
273 vcpu->arch.regs_avail = ~VMX_REGS_LAZY_LOAD_SET;
276 * All lazily updated registers will be reloaded from VMCS12 on both
277 * vmentry and vmexit.
279 vcpu->arch.regs_dirty = 0;
283 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
284 * just stops using VMX.
286 static void free_nested(struct kvm_vcpu *vcpu)
288 struct vcpu_vmx *vmx = to_vmx(vcpu);
290 if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01))
291 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
293 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
296 kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
298 vmx->nested.vmxon = false;
299 vmx->nested.smm.vmxon = false;
300 vmx->nested.vmxon_ptr = INVALID_GPA;
301 free_vpid(vmx->nested.vpid02);
302 vmx->nested.posted_intr_nv = -1;
303 vmx->nested.current_vmptr = INVALID_GPA;
304 if (enable_shadow_vmcs) {
305 vmx_disable_shadow_vmcs(vmx);
306 vmcs_clear(vmx->vmcs01.shadow_vmcs);
307 free_vmcs(vmx->vmcs01.shadow_vmcs);
308 vmx->vmcs01.shadow_vmcs = NULL;
310 kfree(vmx->nested.cached_vmcs12);
311 vmx->nested.cached_vmcs12 = NULL;
312 kfree(vmx->nested.cached_shadow_vmcs12);
313 vmx->nested.cached_shadow_vmcs12 = NULL;
315 * Unpin physical memory we referred to in the vmcs02. The APIC access
316 * page's backing page (yeah, confusing) shouldn't actually be accessed,
317 * and if it is written, the contents are irrelevant.
319 kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map, false);
320 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
321 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
322 vmx->nested.pi_desc = NULL;
324 kvm_mmu_free_roots(vcpu->kvm, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
326 nested_release_evmcs(vcpu);
328 free_loaded_vmcs(&vmx->nested.vmcs02);
332 * Ensure that the current vmcs of the logical processor is the
333 * vmcs01 of the vcpu before calling free_nested().
335 void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
338 vmx_leave_nested(vcpu);
342 #define EPTP_PA_MASK GENMASK_ULL(51, 12)
344 static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
346 return VALID_PAGE(root_hpa) &&
347 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
350 static void nested_ept_invalidate_addr(struct kvm_vcpu *vcpu, gpa_t eptp,
354 struct kvm_mmu_root_info *cached_root;
356 WARN_ON_ONCE(!mmu_is_nested(vcpu));
358 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
359 cached_root = &vcpu->arch.mmu->prev_roots[i];
361 if (nested_ept_root_matches(cached_root->hpa, cached_root->pgd,
363 vcpu->arch.mmu->invlpg(vcpu, addr, cached_root->hpa);
367 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
368 struct x86_exception *fault)
370 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
371 struct vcpu_vmx *vmx = to_vmx(vcpu);
373 unsigned long exit_qualification = vcpu->arch.exit_qualification;
375 if (vmx->nested.pml_full) {
376 vm_exit_reason = EXIT_REASON_PML_FULL;
377 vmx->nested.pml_full = false;
378 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
380 if (fault->error_code & PFERR_RSVD_MASK)
381 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
383 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
386 * Although the caller (kvm_inject_emulated_page_fault) would
387 * have already synced the faulting address in the shadow EPT
388 * tables for the current EPTP12, we also need to sync it for
389 * any other cached EPTP02s based on the same EP4TA, since the
390 * TLB associates mappings to the EP4TA rather than the full EPTP.
392 nested_ept_invalidate_addr(vcpu, vmcs12->ept_pointer,
396 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
397 vmcs12->guest_physical_address = fault->address;
400 static void nested_ept_new_eptp(struct kvm_vcpu *vcpu)
402 struct vcpu_vmx *vmx = to_vmx(vcpu);
403 bool execonly = vmx->nested.msrs.ept_caps & VMX_EPT_EXECUTE_ONLY_BIT;
404 int ept_lpage_level = ept_caps_to_lpage_level(vmx->nested.msrs.ept_caps);
406 kvm_init_shadow_ept_mmu(vcpu, execonly, ept_lpage_level,
407 nested_ept_ad_enabled(vcpu),
408 nested_ept_get_eptp(vcpu));
411 static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
413 WARN_ON(mmu_is_nested(vcpu));
415 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
416 nested_ept_new_eptp(vcpu);
417 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
418 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
419 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
421 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
424 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
426 vcpu->arch.mmu = &vcpu->arch.root_mmu;
427 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
430 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
433 bool inequality, bit;
435 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
437 (error_code & vmcs12->page_fault_error_code_mask) !=
438 vmcs12->page_fault_error_code_match;
439 return inequality ^ bit;
442 static bool nested_vmx_is_exception_vmexit(struct kvm_vcpu *vcpu, u8 vector,
445 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
448 * Drop bits 31:16 of the error code when performing the #PF mask+match
449 * check. All VMCS fields involved are 32 bits, but Intel CPUs never
450 * set bits 31:16 and VMX disallows setting bits 31:16 in the injected
451 * error code. Including the to-be-dropped bits in the check might
452 * result in an "impossible" or missed exit from L1's perspective.
454 if (vector == PF_VECTOR)
455 return nested_vmx_is_page_fault_vmexit(vmcs12, (u16)error_code);
457 return (vmcs12->exception_bitmap & (1u << vector));
460 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
461 struct vmcs12 *vmcs12)
463 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
466 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
467 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
473 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
474 struct vmcs12 *vmcs12)
476 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
479 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
485 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
486 struct vmcs12 *vmcs12)
488 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
491 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
498 * For x2APIC MSRs, ignore the vmcs01 bitmap. L1 can enable x2APIC without L1
499 * itself utilizing x2APIC. All MSRs were previously set to be intercepted,
500 * only the "disable intercept" case needs to be handled.
502 static void nested_vmx_disable_intercept_for_x2apic_msr(unsigned long *msr_bitmap_l1,
503 unsigned long *msr_bitmap_l0,
506 if (type & MSR_TYPE_R && !vmx_test_msr_bitmap_read(msr_bitmap_l1, msr))
507 vmx_clear_msr_bitmap_read(msr_bitmap_l0, msr);
509 if (type & MSR_TYPE_W && !vmx_test_msr_bitmap_write(msr_bitmap_l1, msr))
510 vmx_clear_msr_bitmap_write(msr_bitmap_l0, msr);
513 static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
517 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
518 unsigned word = msr / BITS_PER_LONG;
520 msr_bitmap[word] = ~0;
521 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
525 #define BUILD_NVMX_MSR_INTERCEPT_HELPER(rw) \
527 void nested_vmx_set_msr_##rw##_intercept(struct vcpu_vmx *vmx, \
528 unsigned long *msr_bitmap_l1, \
529 unsigned long *msr_bitmap_l0, u32 msr) \
531 if (vmx_test_msr_bitmap_##rw(vmx->vmcs01.msr_bitmap, msr) || \
532 vmx_test_msr_bitmap_##rw(msr_bitmap_l1, msr)) \
533 vmx_set_msr_bitmap_##rw(msr_bitmap_l0, msr); \
535 vmx_clear_msr_bitmap_##rw(msr_bitmap_l0, msr); \
537 BUILD_NVMX_MSR_INTERCEPT_HELPER(read)
538 BUILD_NVMX_MSR_INTERCEPT_HELPER(write)
540 static inline void nested_vmx_set_intercept_for_msr(struct vcpu_vmx *vmx,
541 unsigned long *msr_bitmap_l1,
542 unsigned long *msr_bitmap_l0,
545 if (types & MSR_TYPE_R)
546 nested_vmx_set_msr_read_intercept(vmx, msr_bitmap_l1,
548 if (types & MSR_TYPE_W)
549 nested_vmx_set_msr_write_intercept(vmx, msr_bitmap_l1,
554 * Merge L0's and L1's MSR bitmap, return false to indicate that
555 * we do not use the hardware.
557 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
558 struct vmcs12 *vmcs12)
560 struct vcpu_vmx *vmx = to_vmx(vcpu);
562 unsigned long *msr_bitmap_l1;
563 unsigned long *msr_bitmap_l0 = vmx->nested.vmcs02.msr_bitmap;
564 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
565 struct kvm_host_map *map = &vmx->nested.msr_bitmap_map;
567 /* Nothing to do if the MSR bitmap is not in use. */
568 if (!cpu_has_vmx_msr_bitmap() ||
569 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
573 * MSR bitmap update can be skipped when:
574 * - MSR bitmap for L1 hasn't changed.
575 * - Nested hypervisor (L1) is attempting to launch the same L2 as
577 * - Nested hypervisor (L1) has enabled 'Enlightened MSR Bitmap' feature
578 * and tells KVM (L0) there were no changes in MSR bitmap for L2.
580 if (!vmx->nested.force_msr_bitmap_recalc && evmcs &&
581 evmcs->hv_enlightenments_control.msr_bitmap &&
582 evmcs->hv_clean_fields & HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP)
585 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
588 msr_bitmap_l1 = (unsigned long *)map->hva;
591 * To keep the control flow simple, pay eight 8-byte writes (sixteen
592 * 4-byte writes on 32-bit systems) up front to enable intercepts for
593 * the x2APIC MSR range and selectively toggle those relevant to L2.
595 enable_x2apic_msr_intercepts(msr_bitmap_l0);
597 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
598 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
600 * L0 need not intercept reads for MSRs between 0x800
601 * and 0x8ff, it just lets the processor take the value
602 * from the virtual-APIC page; take those 256 bits
603 * directly from the L1 bitmap.
605 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
606 unsigned word = msr / BITS_PER_LONG;
608 msr_bitmap_l0[word] = msr_bitmap_l1[word];
612 nested_vmx_disable_intercept_for_x2apic_msr(
613 msr_bitmap_l1, msr_bitmap_l0,
614 X2APIC_MSR(APIC_TASKPRI),
615 MSR_TYPE_R | MSR_TYPE_W);
617 if (nested_cpu_has_vid(vmcs12)) {
618 nested_vmx_disable_intercept_for_x2apic_msr(
619 msr_bitmap_l1, msr_bitmap_l0,
620 X2APIC_MSR(APIC_EOI),
622 nested_vmx_disable_intercept_for_x2apic_msr(
623 msr_bitmap_l1, msr_bitmap_l0,
624 X2APIC_MSR(APIC_SELF_IPI),
630 * Always check vmcs01's bitmap to honor userspace MSR filters and any
631 * other runtime changes to vmcs01's bitmap, e.g. dynamic pass-through.
634 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
635 MSR_FS_BASE, MSR_TYPE_RW);
637 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
638 MSR_GS_BASE, MSR_TYPE_RW);
640 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
641 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
643 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
644 MSR_IA32_SPEC_CTRL, MSR_TYPE_RW);
646 nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0,
647 MSR_IA32_PRED_CMD, MSR_TYPE_W);
649 kvm_vcpu_unmap(vcpu, &vmx->nested.msr_bitmap_map, false);
651 vmx->nested.force_msr_bitmap_recalc = false;
656 static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
657 struct vmcs12 *vmcs12)
659 struct vcpu_vmx *vmx = to_vmx(vcpu);
660 struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache;
662 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
663 vmcs12->vmcs_link_pointer == INVALID_GPA)
666 if (ghc->gpa != vmcs12->vmcs_link_pointer &&
667 kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc,
668 vmcs12->vmcs_link_pointer, VMCS12_SIZE))
671 kvm_read_guest_cached(vmx->vcpu.kvm, ghc, get_shadow_vmcs12(vcpu),
675 static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
676 struct vmcs12 *vmcs12)
678 struct vcpu_vmx *vmx = to_vmx(vcpu);
679 struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache;
681 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
682 vmcs12->vmcs_link_pointer == INVALID_GPA)
685 if (ghc->gpa != vmcs12->vmcs_link_pointer &&
686 kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc,
687 vmcs12->vmcs_link_pointer, VMCS12_SIZE))
690 kvm_write_guest_cached(vmx->vcpu.kvm, ghc, get_shadow_vmcs12(vcpu),
695 * In nested virtualization, check if L1 has set
696 * VM_EXIT_ACK_INTR_ON_EXIT
698 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
700 return get_vmcs12(vcpu)->vm_exit_controls &
701 VM_EXIT_ACK_INTR_ON_EXIT;
704 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
705 struct vmcs12 *vmcs12)
707 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
708 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
714 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
715 struct vmcs12 *vmcs12)
717 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
718 !nested_cpu_has_apic_reg_virt(vmcs12) &&
719 !nested_cpu_has_vid(vmcs12) &&
720 !nested_cpu_has_posted_intr(vmcs12))
724 * If virtualize x2apic mode is enabled,
725 * virtualize apic access must be disabled.
727 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
728 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
732 * If virtual interrupt delivery is enabled,
733 * we must exit on external interrupts.
735 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
739 * bits 15:8 should be zero in posted_intr_nv,
740 * the descriptor address has been already checked
741 * in nested_get_vmcs12_pages.
743 * bits 5:0 of posted_intr_desc_addr should be zero.
745 if (nested_cpu_has_posted_intr(vmcs12) &&
746 (CC(!nested_cpu_has_vid(vmcs12)) ||
747 CC(!nested_exit_intr_ack_set(vcpu)) ||
748 CC((vmcs12->posted_intr_nv & 0xff00)) ||
749 CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64))))
752 /* tpr shadow is needed by all apicv features. */
753 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
759 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
765 if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) ||
766 !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1)))
772 static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu,
773 struct vmcs12 *vmcs12)
775 if (CC(nested_vmx_check_msr_switch(vcpu,
776 vmcs12->vm_exit_msr_load_count,
777 vmcs12->vm_exit_msr_load_addr)) ||
778 CC(nested_vmx_check_msr_switch(vcpu,
779 vmcs12->vm_exit_msr_store_count,
780 vmcs12->vm_exit_msr_store_addr)))
786 static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu,
787 struct vmcs12 *vmcs12)
789 if (CC(nested_vmx_check_msr_switch(vcpu,
790 vmcs12->vm_entry_msr_load_count,
791 vmcs12->vm_entry_msr_load_addr)))
797 static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
798 struct vmcs12 *vmcs12)
800 if (!nested_cpu_has_pml(vmcs12))
803 if (CC(!nested_cpu_has_ept(vmcs12)) ||
804 CC(!page_address_valid(vcpu, vmcs12->pml_address)))
810 static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu,
811 struct vmcs12 *vmcs12)
813 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) &&
814 !nested_cpu_has_ept(vmcs12)))
819 static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu,
820 struct vmcs12 *vmcs12)
822 if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) &&
823 !nested_cpu_has_ept(vmcs12)))
828 static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
829 struct vmcs12 *vmcs12)
831 if (!nested_cpu_has_shadow_vmcs(vmcs12))
834 if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) ||
835 CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap)))
841 static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
842 struct vmx_msr_entry *e)
844 /* x2APIC MSR accesses are not allowed */
845 if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8))
847 if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */
848 CC(e->index == MSR_IA32_UCODE_REV))
850 if (CC(e->reserved != 0))
855 static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
856 struct vmx_msr_entry *e)
858 if (CC(e->index == MSR_FS_BASE) ||
859 CC(e->index == MSR_GS_BASE) ||
860 CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */
861 nested_vmx_msr_check_common(vcpu, e))
866 static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
867 struct vmx_msr_entry *e)
869 if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */
870 nested_vmx_msr_check_common(vcpu, e))
875 static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu)
877 struct vcpu_vmx *vmx = to_vmx(vcpu);
878 u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
879 vmx->nested.msrs.misc_high);
881 return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER;
885 * Load guest's/host's msr at nested entry/exit.
886 * return 0 for success, entry index for failure.
888 * One of the failure modes for MSR load/store is when a list exceeds the
889 * virtual hardware's capacity. To maintain compatibility with hardware inasmuch
890 * as possible, process all valid entries before failing rather than precheck
891 * for a capacity violation.
893 static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
896 struct vmx_msr_entry e;
897 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
899 for (i = 0; i < count; i++) {
900 if (unlikely(i >= max_msr_list_size))
903 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
905 pr_debug_ratelimited(
906 "%s cannot read MSR entry (%u, 0x%08llx)\n",
907 __func__, i, gpa + i * sizeof(e));
910 if (nested_vmx_load_msr_check(vcpu, &e)) {
911 pr_debug_ratelimited(
912 "%s check failed (%u, 0x%x, 0x%x)\n",
913 __func__, i, e.index, e.reserved);
916 if (kvm_set_msr(vcpu, e.index, e.value)) {
917 pr_debug_ratelimited(
918 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
919 __func__, i, e.index, e.value);
925 /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */
929 static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu,
933 struct vcpu_vmx *vmx = to_vmx(vcpu);
936 * If the L0 hypervisor stored a more accurate value for the TSC that
937 * does not include the time taken for emulation of the L2->L1
938 * VM-exit in L0, use the more accurate value.
940 if (msr_index == MSR_IA32_TSC) {
941 int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest,
945 u64 val = vmx->msr_autostore.guest.val[i].value;
947 *data = kvm_read_l1_tsc(vcpu, val);
952 if (kvm_get_msr(vcpu, msr_index, data)) {
953 pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__,
960 static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i,
961 struct vmx_msr_entry *e)
963 if (kvm_vcpu_read_guest(vcpu,
964 gpa + i * sizeof(*e),
965 e, 2 * sizeof(u32))) {
966 pr_debug_ratelimited(
967 "%s cannot read MSR entry (%u, 0x%08llx)\n",
968 __func__, i, gpa + i * sizeof(*e));
971 if (nested_vmx_store_msr_check(vcpu, e)) {
972 pr_debug_ratelimited(
973 "%s check failed (%u, 0x%x, 0x%x)\n",
974 __func__, i, e->index, e->reserved);
980 static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
984 struct vmx_msr_entry e;
985 u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu);
987 for (i = 0; i < count; i++) {
988 if (unlikely(i >= max_msr_list_size))
991 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
994 if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data))
997 if (kvm_vcpu_write_guest(vcpu,
998 gpa + i * sizeof(e) +
999 offsetof(struct vmx_msr_entry, value),
1000 &data, sizeof(data))) {
1001 pr_debug_ratelimited(
1002 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
1003 __func__, i, e.index, data);
1010 static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index)
1012 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1013 u32 count = vmcs12->vm_exit_msr_store_count;
1014 u64 gpa = vmcs12->vm_exit_msr_store_addr;
1015 struct vmx_msr_entry e;
1018 for (i = 0; i < count; i++) {
1019 if (!read_and_check_msr_entry(vcpu, gpa, i, &e))
1022 if (e.index == msr_index)
1028 static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu,
1031 struct vcpu_vmx *vmx = to_vmx(vcpu);
1032 struct vmx_msrs *autostore = &vmx->msr_autostore.guest;
1033 bool in_vmcs12_store_list;
1034 int msr_autostore_slot;
1035 bool in_autostore_list;
1038 msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index);
1039 in_autostore_list = msr_autostore_slot >= 0;
1040 in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index);
1042 if (in_vmcs12_store_list && !in_autostore_list) {
1043 if (autostore->nr == MAX_NR_LOADSTORE_MSRS) {
1045 * Emulated VMEntry does not fail here. Instead a less
1046 * accurate value will be returned by
1047 * nested_vmx_get_vmexit_msr_value() using kvm_get_msr()
1048 * instead of reading the value from the vmcs02 VMExit
1051 pr_warn_ratelimited(
1052 "Not enough msr entries in msr_autostore. Can't add msr %x\n",
1056 last = autostore->nr++;
1057 autostore->val[last].index = msr_index;
1058 } else if (!in_vmcs12_store_list && in_autostore_list) {
1059 last = --autostore->nr;
1060 autostore->val[msr_autostore_slot] = autostore->val[last];
1065 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1066 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1067 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1068 * @entry_failure_code.
1070 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
1071 bool nested_ept, bool reload_pdptrs,
1072 enum vm_entry_failure_code *entry_failure_code)
1074 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) {
1075 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1080 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1081 * must not be dereferenced.
1083 if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) &&
1084 CC(!load_pdptrs(vcpu, cr3))) {
1085 *entry_failure_code = ENTRY_FAIL_PDPTE;
1089 vcpu->arch.cr3 = cr3;
1090 kvm_register_mark_dirty(vcpu, VCPU_EXREG_CR3);
1092 /* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
1096 kvm_mmu_new_pgd(vcpu, cr3);
1102 * Returns if KVM is able to config CPU to tag TLB entries
1103 * populated by L2 differently than TLB entries populated
1106 * If L0 uses EPT, L1 and L2 run with different EPTP because
1107 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1108 * are tagged with different EPTP.
1110 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1111 * with different VPID (L1 entries are tagged with vmx->vpid
1112 * while L2 entries are tagged with vmx->nested.vpid02).
1114 static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1116 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1118 return enable_ept ||
1119 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1122 static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1123 struct vmcs12 *vmcs12,
1126 struct vcpu_vmx *vmx = to_vmx(vcpu);
1129 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1130 * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a
1131 * full TLB flush from the guest's perspective. This is required even
1132 * if VPID is disabled in the host as KVM may need to synchronize the
1133 * MMU in response to the guest TLB flush.
1135 * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use.
1136 * EPT is a special snowflake, as guest-physical mappings aren't
1137 * flushed on VPID invalidations, including VM-Enter or VM-Exit with
1138 * VPID disabled. As a result, KVM _never_ needs to sync nEPT
1139 * entries on VM-Enter because L1 can't rely on VM-Enter to flush
1142 if (!nested_cpu_has_vpid(vmcs12)) {
1143 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
1147 /* L2 should never have a VPID if VPID is disabled. */
1148 WARN_ON(!enable_vpid);
1151 * VPID is enabled and in use by vmcs12. If vpid12 is changing, then
1152 * emulate a guest TLB flush as KVM does not track vpid12 history nor
1153 * is the VPID incorporated into the MMU context. I.e. KVM must assume
1154 * that the new vpid12 has never been used and thus represents a new
1155 * guest ASID that cannot have entries in the TLB.
1157 if (is_vmenter && vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1158 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1159 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
1164 * If VPID is enabled, used by vmc12, and vpid12 is not changing but
1165 * does not have a unique TLB tag (ASID), i.e. EPT is disabled and
1166 * KVM was unable to allocate a VPID for L2, flush the current context
1167 * as the effective ASID is common to both L1 and L2.
1169 if (!nested_has_guest_tlb_tag(vcpu))
1170 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
1173 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1178 return (superset | subset) == superset;
1181 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1183 const u64 feature_and_reserved =
1184 /* feature (except bit 48; see below) */
1185 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1187 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1188 u64 vmx_basic = vmcs_config.nested.basic;
1190 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1194 * KVM does not emulate a version of VMX that constrains physical
1195 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1197 if (data & BIT_ULL(48))
1200 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1201 vmx_basic_vmcs_revision_id(data))
1204 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1207 vmx->nested.msrs.basic = data;
1211 static void vmx_get_control_msr(struct nested_vmx_msrs *msrs, u32 msr_index,
1212 u32 **low, u32 **high)
1214 switch (msr_index) {
1215 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1216 *low = &msrs->pinbased_ctls_low;
1217 *high = &msrs->pinbased_ctls_high;
1219 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1220 *low = &msrs->procbased_ctls_low;
1221 *high = &msrs->procbased_ctls_high;
1223 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1224 *low = &msrs->exit_ctls_low;
1225 *high = &msrs->exit_ctls_high;
1227 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1228 *low = &msrs->entry_ctls_low;
1229 *high = &msrs->entry_ctls_high;
1231 case MSR_IA32_VMX_PROCBASED_CTLS2:
1232 *low = &msrs->secondary_ctls_low;
1233 *high = &msrs->secondary_ctls_high;
1241 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1246 vmx_get_control_msr(&vmcs_config.nested, msr_index, &lowp, &highp);
1248 supported = vmx_control_msr(*lowp, *highp);
1250 /* Check must-be-1 bits are still 1. */
1251 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1254 /* Check must-be-0 bits are still 0. */
1255 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1258 vmx_get_control_msr(&vmx->nested.msrs, msr_index, &lowp, &highp);
1260 *highp = data >> 32;
1264 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1266 const u64 feature_and_reserved_bits =
1268 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1269 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1271 GENMASK_ULL(13, 9) | BIT_ULL(31);
1272 u64 vmx_misc = vmx_control_msr(vmcs_config.nested.misc_low,
1273 vmcs_config.nested.misc_high);
1275 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1278 if ((vmx->nested.msrs.pinbased_ctls_high &
1279 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1280 vmx_misc_preemption_timer_rate(data) !=
1281 vmx_misc_preemption_timer_rate(vmx_misc))
1284 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1287 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1290 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1293 vmx->nested.msrs.misc_low = data;
1294 vmx->nested.msrs.misc_high = data >> 32;
1299 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1301 u64 vmx_ept_vpid_cap = vmx_control_msr(vmcs_config.nested.ept_caps,
1302 vmcs_config.nested.vpid_caps);
1304 /* Every bit is either reserved or a feature bit. */
1305 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1308 vmx->nested.msrs.ept_caps = data;
1309 vmx->nested.msrs.vpid_caps = data >> 32;
1313 static u64 *vmx_get_fixed0_msr(struct nested_vmx_msrs *msrs, u32 msr_index)
1315 switch (msr_index) {
1316 case MSR_IA32_VMX_CR0_FIXED0:
1317 return &msrs->cr0_fixed0;
1318 case MSR_IA32_VMX_CR4_FIXED0:
1319 return &msrs->cr4_fixed0;
1325 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1327 const u64 *msr = vmx_get_fixed0_msr(&vmcs_config.nested, msr_index);
1330 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1331 * must be 1 in the restored value.
1333 if (!is_bitwise_subset(data, *msr, -1ULL))
1336 *vmx_get_fixed0_msr(&vmx->nested.msrs, msr_index) = data;
1341 * Called when userspace is restoring VMX MSRs.
1343 * Returns 0 on success, non-0 otherwise.
1345 int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1347 struct vcpu_vmx *vmx = to_vmx(vcpu);
1350 * Don't allow changes to the VMX capability MSRs while the vCPU
1351 * is in VMX operation.
1353 if (vmx->nested.vmxon)
1356 switch (msr_index) {
1357 case MSR_IA32_VMX_BASIC:
1358 return vmx_restore_vmx_basic(vmx, data);
1359 case MSR_IA32_VMX_PINBASED_CTLS:
1360 case MSR_IA32_VMX_PROCBASED_CTLS:
1361 case MSR_IA32_VMX_EXIT_CTLS:
1362 case MSR_IA32_VMX_ENTRY_CTLS:
1364 * The "non-true" VMX capability MSRs are generated from the
1365 * "true" MSRs, so we do not support restoring them directly.
1367 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1368 * should restore the "true" MSRs with the must-be-1 bits
1369 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1370 * DEFAULT SETTINGS".
1373 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1374 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1375 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1376 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1377 case MSR_IA32_VMX_PROCBASED_CTLS2:
1378 return vmx_restore_control_msr(vmx, msr_index, data);
1379 case MSR_IA32_VMX_MISC:
1380 return vmx_restore_vmx_misc(vmx, data);
1381 case MSR_IA32_VMX_CR0_FIXED0:
1382 case MSR_IA32_VMX_CR4_FIXED0:
1383 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1384 case MSR_IA32_VMX_CR0_FIXED1:
1385 case MSR_IA32_VMX_CR4_FIXED1:
1387 * These MSRs are generated based on the vCPU's CPUID, so we
1388 * do not support restoring them directly.
1391 case MSR_IA32_VMX_EPT_VPID_CAP:
1392 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1393 case MSR_IA32_VMX_VMCS_ENUM:
1394 vmx->nested.msrs.vmcs_enum = data;
1396 case MSR_IA32_VMX_VMFUNC:
1397 if (data & ~vmcs_config.nested.vmfunc_controls)
1399 vmx->nested.msrs.vmfunc_controls = data;
1403 * The rest of the VMX capability MSRs do not support restore.
1409 /* Returns 0 on success, non-0 otherwise. */
1410 int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1412 switch (msr_index) {
1413 case MSR_IA32_VMX_BASIC:
1414 *pdata = msrs->basic;
1416 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1417 case MSR_IA32_VMX_PINBASED_CTLS:
1418 *pdata = vmx_control_msr(
1419 msrs->pinbased_ctls_low,
1420 msrs->pinbased_ctls_high);
1421 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1422 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1424 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1425 case MSR_IA32_VMX_PROCBASED_CTLS:
1426 *pdata = vmx_control_msr(
1427 msrs->procbased_ctls_low,
1428 msrs->procbased_ctls_high);
1429 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1430 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1432 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1433 case MSR_IA32_VMX_EXIT_CTLS:
1434 *pdata = vmx_control_msr(
1435 msrs->exit_ctls_low,
1436 msrs->exit_ctls_high);
1437 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1438 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1440 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1441 case MSR_IA32_VMX_ENTRY_CTLS:
1442 *pdata = vmx_control_msr(
1443 msrs->entry_ctls_low,
1444 msrs->entry_ctls_high);
1445 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1446 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1448 case MSR_IA32_VMX_MISC:
1449 *pdata = vmx_control_msr(
1453 case MSR_IA32_VMX_CR0_FIXED0:
1454 *pdata = msrs->cr0_fixed0;
1456 case MSR_IA32_VMX_CR0_FIXED1:
1457 *pdata = msrs->cr0_fixed1;
1459 case MSR_IA32_VMX_CR4_FIXED0:
1460 *pdata = msrs->cr4_fixed0;
1462 case MSR_IA32_VMX_CR4_FIXED1:
1463 *pdata = msrs->cr4_fixed1;
1465 case MSR_IA32_VMX_VMCS_ENUM:
1466 *pdata = msrs->vmcs_enum;
1468 case MSR_IA32_VMX_PROCBASED_CTLS2:
1469 *pdata = vmx_control_msr(
1470 msrs->secondary_ctls_low,
1471 msrs->secondary_ctls_high);
1473 case MSR_IA32_VMX_EPT_VPID_CAP:
1474 *pdata = msrs->ept_caps |
1475 ((u64)msrs->vpid_caps << 32);
1477 case MSR_IA32_VMX_VMFUNC:
1478 *pdata = msrs->vmfunc_controls;
1488 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1489 * been modified by the L1 guest. Note, "writable" in this context means
1490 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1491 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1492 * VM-exit information fields (which are actually writable if the vCPU is
1493 * configured to support "VMWRITE to any supported field in the VMCS").
1495 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1497 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1498 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1499 struct shadow_vmcs_field field;
1503 if (WARN_ON(!shadow_vmcs))
1508 vmcs_load(shadow_vmcs);
1510 for (i = 0; i < max_shadow_read_write_fields; i++) {
1511 field = shadow_read_write_fields[i];
1512 val = __vmcs_readl(field.encoding);
1513 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
1516 vmcs_clear(shadow_vmcs);
1517 vmcs_load(vmx->loaded_vmcs->vmcs);
1522 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1524 const struct shadow_vmcs_field *fields[] = {
1525 shadow_read_write_fields,
1526 shadow_read_only_fields
1528 const int max_fields[] = {
1529 max_shadow_read_write_fields,
1530 max_shadow_read_only_fields
1532 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1533 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1534 struct shadow_vmcs_field field;
1538 if (WARN_ON(!shadow_vmcs))
1541 vmcs_load(shadow_vmcs);
1543 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1544 for (i = 0; i < max_fields[q]; i++) {
1545 field = fields[q][i];
1546 val = vmcs12_read_any(vmcs12, field.encoding,
1548 __vmcs_writel(field.encoding, val);
1552 vmcs_clear(shadow_vmcs);
1553 vmcs_load(vmx->loaded_vmcs->vmcs);
1556 static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields)
1558 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1559 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1561 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1562 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1563 vmcs12->guest_rip = evmcs->guest_rip;
1565 if (unlikely(!(hv_clean_fields &
1566 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1567 vmcs12->guest_rsp = evmcs->guest_rsp;
1568 vmcs12->guest_rflags = evmcs->guest_rflags;
1569 vmcs12->guest_interruptibility_info =
1570 evmcs->guest_interruptibility_info;
1572 * Not present in struct vmcs12:
1573 * vmcs12->guest_ssp = evmcs->guest_ssp;
1577 if (unlikely(!(hv_clean_fields &
1578 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1579 vmcs12->cpu_based_vm_exec_control =
1580 evmcs->cpu_based_vm_exec_control;
1583 if (unlikely(!(hv_clean_fields &
1584 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
1585 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1588 if (unlikely(!(hv_clean_fields &
1589 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1590 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1593 if (unlikely(!(hv_clean_fields &
1594 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1595 vmcs12->vm_entry_intr_info_field =
1596 evmcs->vm_entry_intr_info_field;
1597 vmcs12->vm_entry_exception_error_code =
1598 evmcs->vm_entry_exception_error_code;
1599 vmcs12->vm_entry_instruction_len =
1600 evmcs->vm_entry_instruction_len;
1603 if (unlikely(!(hv_clean_fields &
1604 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1605 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1606 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1607 vmcs12->host_cr0 = evmcs->host_cr0;
1608 vmcs12->host_cr3 = evmcs->host_cr3;
1609 vmcs12->host_cr4 = evmcs->host_cr4;
1610 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1611 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1612 vmcs12->host_rip = evmcs->host_rip;
1613 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1614 vmcs12->host_es_selector = evmcs->host_es_selector;
1615 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1616 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1617 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1618 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1619 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1620 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1621 vmcs12->host_ia32_perf_global_ctrl = evmcs->host_ia32_perf_global_ctrl;
1623 * Not present in struct vmcs12:
1624 * vmcs12->host_ia32_s_cet = evmcs->host_ia32_s_cet;
1625 * vmcs12->host_ssp = evmcs->host_ssp;
1626 * vmcs12->host_ia32_int_ssp_table_addr = evmcs->host_ia32_int_ssp_table_addr;
1630 if (unlikely(!(hv_clean_fields &
1631 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
1632 vmcs12->pin_based_vm_exec_control =
1633 evmcs->pin_based_vm_exec_control;
1634 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1635 vmcs12->secondary_vm_exec_control =
1636 evmcs->secondary_vm_exec_control;
1639 if (unlikely(!(hv_clean_fields &
1640 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1641 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1642 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1645 if (unlikely(!(hv_clean_fields &
1646 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1647 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1650 if (unlikely(!(hv_clean_fields &
1651 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1652 vmcs12->guest_es_base = evmcs->guest_es_base;
1653 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1654 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1655 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1656 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1657 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1658 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1659 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1660 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1661 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1662 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1663 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1664 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1665 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1666 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1667 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1668 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1669 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1670 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1671 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1672 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1673 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1674 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1675 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1676 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1677 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1678 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1679 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1680 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1681 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1682 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1683 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1684 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1685 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1686 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1687 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1690 if (unlikely(!(hv_clean_fields &
1691 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1692 vmcs12->tsc_offset = evmcs->tsc_offset;
1693 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1694 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1695 vmcs12->encls_exiting_bitmap = evmcs->encls_exiting_bitmap;
1696 vmcs12->tsc_multiplier = evmcs->tsc_multiplier;
1699 if (unlikely(!(hv_clean_fields &
1700 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1701 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1702 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1703 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1704 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1705 vmcs12->guest_cr0 = evmcs->guest_cr0;
1706 vmcs12->guest_cr3 = evmcs->guest_cr3;
1707 vmcs12->guest_cr4 = evmcs->guest_cr4;
1708 vmcs12->guest_dr7 = evmcs->guest_dr7;
1711 if (unlikely(!(hv_clean_fields &
1712 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1713 vmcs12->host_fs_base = evmcs->host_fs_base;
1714 vmcs12->host_gs_base = evmcs->host_gs_base;
1715 vmcs12->host_tr_base = evmcs->host_tr_base;
1716 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1717 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1718 vmcs12->host_rsp = evmcs->host_rsp;
1721 if (unlikely(!(hv_clean_fields &
1722 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1723 vmcs12->ept_pointer = evmcs->ept_pointer;
1724 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1727 if (unlikely(!(hv_clean_fields &
1728 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1729 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1730 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1731 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1732 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1733 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1734 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1735 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1736 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1737 vmcs12->guest_pending_dbg_exceptions =
1738 evmcs->guest_pending_dbg_exceptions;
1739 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1740 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1741 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1742 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1743 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1744 vmcs12->guest_ia32_perf_global_ctrl = evmcs->guest_ia32_perf_global_ctrl;
1746 * Not present in struct vmcs12:
1747 * vmcs12->guest_ia32_s_cet = evmcs->guest_ia32_s_cet;
1748 * vmcs12->guest_ia32_lbr_ctl = evmcs->guest_ia32_lbr_ctl;
1749 * vmcs12->guest_ia32_int_ssp_table_addr = evmcs->guest_ia32_int_ssp_table_addr;
1755 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1756 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1757 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
1758 * vmcs12->page_fault_error_code_mask =
1759 * evmcs->page_fault_error_code_mask;
1760 * vmcs12->page_fault_error_code_match =
1761 * evmcs->page_fault_error_code_match;
1762 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1763 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1764 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1765 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1770 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1771 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1772 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1773 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1774 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1775 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1776 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1777 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1778 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1779 * vmcs12->exit_qualification = evmcs->exit_qualification;
1780 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1782 * Not present in struct vmcs12:
1783 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1784 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1785 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1786 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1792 static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
1794 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1795 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1798 * Should not be changed by KVM:
1800 * evmcs->host_es_selector = vmcs12->host_es_selector;
1801 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1802 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1803 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1804 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1805 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1806 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1807 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1808 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1809 * evmcs->host_cr0 = vmcs12->host_cr0;
1810 * evmcs->host_cr3 = vmcs12->host_cr3;
1811 * evmcs->host_cr4 = vmcs12->host_cr4;
1812 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1813 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1814 * evmcs->host_rip = vmcs12->host_rip;
1815 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1816 * evmcs->host_fs_base = vmcs12->host_fs_base;
1817 * evmcs->host_gs_base = vmcs12->host_gs_base;
1818 * evmcs->host_tr_base = vmcs12->host_tr_base;
1819 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1820 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1821 * evmcs->host_rsp = vmcs12->host_rsp;
1822 * sync_vmcs02_to_vmcs12() doesn't read these:
1823 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1824 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1825 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1826 * evmcs->ept_pointer = vmcs12->ept_pointer;
1827 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1828 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1829 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1830 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
1831 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1832 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1833 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1834 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1835 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1836 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1837 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1838 * evmcs->page_fault_error_code_mask =
1839 * vmcs12->page_fault_error_code_mask;
1840 * evmcs->page_fault_error_code_match =
1841 * vmcs12->page_fault_error_code_match;
1842 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1843 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1844 * evmcs->tsc_offset = vmcs12->tsc_offset;
1845 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1846 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1847 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1848 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1849 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1850 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1851 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1852 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1853 * evmcs->guest_ia32_perf_global_ctrl = vmcs12->guest_ia32_perf_global_ctrl;
1854 * evmcs->host_ia32_perf_global_ctrl = vmcs12->host_ia32_perf_global_ctrl;
1855 * evmcs->encls_exiting_bitmap = vmcs12->encls_exiting_bitmap;
1856 * evmcs->tsc_multiplier = vmcs12->tsc_multiplier;
1858 * Not present in struct vmcs12:
1859 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1860 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1861 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1862 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1863 * evmcs->host_ia32_s_cet = vmcs12->host_ia32_s_cet;
1864 * evmcs->host_ssp = vmcs12->host_ssp;
1865 * evmcs->host_ia32_int_ssp_table_addr = vmcs12->host_ia32_int_ssp_table_addr;
1866 * evmcs->guest_ia32_s_cet = vmcs12->guest_ia32_s_cet;
1867 * evmcs->guest_ia32_lbr_ctl = vmcs12->guest_ia32_lbr_ctl;
1868 * evmcs->guest_ia32_int_ssp_table_addr = vmcs12->guest_ia32_int_ssp_table_addr;
1869 * evmcs->guest_ssp = vmcs12->guest_ssp;
1872 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1873 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1874 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1875 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1876 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1877 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1878 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1879 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1881 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1882 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1883 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1884 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1885 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1886 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1887 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1888 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1889 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1890 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1892 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1893 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1894 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1895 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1896 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1897 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1898 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1899 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1901 evmcs->guest_es_base = vmcs12->guest_es_base;
1902 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1903 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1904 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1905 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1906 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1907 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1908 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1909 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1910 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1912 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1913 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1915 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1916 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1917 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1918 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1920 evmcs->guest_pending_dbg_exceptions =
1921 vmcs12->guest_pending_dbg_exceptions;
1922 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1923 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1925 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1926 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1928 evmcs->guest_cr0 = vmcs12->guest_cr0;
1929 evmcs->guest_cr3 = vmcs12->guest_cr3;
1930 evmcs->guest_cr4 = vmcs12->guest_cr4;
1931 evmcs->guest_dr7 = vmcs12->guest_dr7;
1933 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1935 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1936 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1937 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1938 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1939 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1940 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1941 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1942 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1944 evmcs->exit_qualification = vmcs12->exit_qualification;
1946 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1947 evmcs->guest_rsp = vmcs12->guest_rsp;
1948 evmcs->guest_rflags = vmcs12->guest_rflags;
1950 evmcs->guest_interruptibility_info =
1951 vmcs12->guest_interruptibility_info;
1952 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1953 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1954 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1955 evmcs->vm_entry_exception_error_code =
1956 vmcs12->vm_entry_exception_error_code;
1957 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1959 evmcs->guest_rip = vmcs12->guest_rip;
1961 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1967 * This is an equivalent of the nested hypervisor executing the vmptrld
1970 static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1971 struct kvm_vcpu *vcpu, bool from_launch)
1973 struct vcpu_vmx *vmx = to_vmx(vcpu);
1974 bool evmcs_gpa_changed = false;
1977 if (likely(!guest_cpuid_has_evmcs(vcpu)))
1978 return EVMPTRLD_DISABLED;
1980 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) {
1981 nested_release_evmcs(vcpu);
1982 return EVMPTRLD_DISABLED;
1985 if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
1986 vmx->nested.current_vmptr = INVALID_GPA;
1988 nested_release_evmcs(vcpu);
1990 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
1991 &vmx->nested.hv_evmcs_map))
1992 return EVMPTRLD_ERROR;
1994 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
1997 * Currently, KVM only supports eVMCS version 1
1998 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
1999 * value to first u32 field of eVMCS which should specify eVMCS
2002 * Guest should be aware of supported eVMCS versions by host by
2003 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
2004 * expected to set this CPUID leaf according to the value
2005 * returned in vmcs_version from nested_enable_evmcs().
2007 * However, it turns out that Microsoft Hyper-V fails to comply
2008 * to their own invented interface: When Hyper-V use eVMCS, it
2009 * just sets first u32 field of eVMCS to revision_id specified
2010 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
2011 * which is one of the supported versions specified in
2012 * CPUID.0x4000000A.EAX[0:15].
2014 * To overcome Hyper-V bug, we accept here either a supported
2015 * eVMCS version or VMCS12 revision_id as valid values for first
2016 * u32 field of eVMCS.
2018 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
2019 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
2020 nested_release_evmcs(vcpu);
2021 return EVMPTRLD_VMFAIL;
2024 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
2026 evmcs_gpa_changed = true;
2028 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2029 * reloaded from guest's memory (read only fields, fields not
2030 * present in struct hv_enlightened_vmcs, ...). Make sure there
2034 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2035 memset(vmcs12, 0, sizeof(*vmcs12));
2036 vmcs12->hdr.revision_id = VMCS12_REVISION;
2042 * Clean fields data can't be used on VMLAUNCH and when we switch
2043 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2045 if (from_launch || evmcs_gpa_changed) {
2046 vmx->nested.hv_evmcs->hv_clean_fields &=
2047 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2049 vmx->nested.force_msr_bitmap_recalc = true;
2052 return EVMPTRLD_SUCCEEDED;
2055 void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
2057 struct vcpu_vmx *vmx = to_vmx(vcpu);
2059 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
2060 copy_vmcs12_to_enlightened(vmx);
2062 copy_vmcs12_to_shadow(vmx);
2064 vmx->nested.need_vmcs12_to_shadow_sync = false;
2067 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2069 struct vcpu_vmx *vmx =
2070 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2072 vmx->nested.preemption_timer_expired = true;
2073 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2074 kvm_vcpu_kick(&vmx->vcpu);
2076 return HRTIMER_NORESTART;
2079 static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu)
2081 struct vcpu_vmx *vmx = to_vmx(vcpu);
2082 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2084 u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >>
2085 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2087 if (!vmx->nested.has_preemption_timer_deadline) {
2088 vmx->nested.preemption_timer_deadline =
2089 vmcs12->vmx_preemption_timer_value + l1_scaled_tsc;
2090 vmx->nested.has_preemption_timer_deadline = true;
2092 return vmx->nested.preemption_timer_deadline - l1_scaled_tsc;
2095 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu,
2096 u64 preemption_timeout)
2098 struct vcpu_vmx *vmx = to_vmx(vcpu);
2101 * A timer value of zero is architecturally guaranteed to cause
2102 * a VMExit prior to executing any instructions in the guest.
2104 if (preemption_timeout == 0) {
2105 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
2109 if (vcpu->arch.virtual_tsc_khz == 0)
2112 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
2113 preemption_timeout *= 1000000;
2114 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
2115 hrtimer_start(&vmx->nested.preemption_timer,
2116 ktime_add_ns(ktime_get(), preemption_timeout),
2117 HRTIMER_MODE_ABS_PINNED);
2120 static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2122 if (vmx->nested.nested_run_pending &&
2123 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2124 return vmcs12->guest_ia32_efer;
2125 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2126 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2128 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2131 static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2133 struct kvm *kvm = vmx->vcpu.kvm;
2136 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2137 * according to L0's settings (vmcs12 is irrelevant here). Host
2138 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2139 * will be set as needed prior to VMLAUNCH/VMRESUME.
2141 if (vmx->nested.vmcs02_initialized)
2143 vmx->nested.vmcs02_initialized = true;
2146 * We don't care what the EPTP value is we just need to guarantee
2147 * it's valid so we don't get a false positive when doing early
2148 * consistency checks.
2150 if (enable_ept && nested_early_check)
2151 vmcs_write64(EPT_POINTER,
2152 construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
2154 /* All VMFUNCs are currently emulated through L0 vmexits. */
2155 if (cpu_has_vmx_vmfunc())
2156 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2158 if (cpu_has_vmx_posted_intr())
2159 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2161 if (cpu_has_vmx_msr_bitmap())
2162 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2165 * PML is emulated for L2, but never enabled in hardware as the MMU
2166 * handles A/D emulation. Disabling PML for L2 also avoids having to
2167 * deal with filtering out L2 GPAs from the buffer.
2170 vmcs_write64(PML_ADDRESS, 0);
2171 vmcs_write16(GUEST_PML_INDEX, -1);
2174 if (cpu_has_vmx_encls_vmexit())
2175 vmcs_write64(ENCLS_EXITING_BITMAP, INVALID_GPA);
2177 if (kvm_notify_vmexit_enabled(kvm))
2178 vmcs_write32(NOTIFY_WINDOW, kvm->arch.notify_window);
2181 * Set the MSR load/store lists to match L0's settings. Only the
2182 * addresses are constant (for vmcs02), the counts can change based
2183 * on L2's behavior, e.g. switching to/from long mode.
2185 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
2186 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2187 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2189 vmx_set_constant_host_state(vmx);
2192 static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
2193 struct vmcs12 *vmcs12)
2195 prepare_vmcs02_constant_state(vmx);
2197 vmcs_write64(VMCS_LINK_POINTER, INVALID_GPA);
2200 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2201 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2203 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2207 static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs01,
2208 struct vmcs12 *vmcs12)
2211 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2213 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
2214 prepare_vmcs02_early_rare(vmx, vmcs12);
2219 exec_control = __pin_controls_get(vmcs01);
2220 exec_control |= (vmcs12->pin_based_vm_exec_control &
2221 ~PIN_BASED_VMX_PREEMPTION_TIMER);
2223 /* Posted interrupts setting is only taken from vmcs12. */
2224 vmx->nested.pi_pending = false;
2225 if (nested_cpu_has_posted_intr(vmcs12))
2226 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2228 exec_control &= ~PIN_BASED_POSTED_INTR;
2229 pin_controls_set(vmx, exec_control);
2234 exec_control = __exec_controls_get(vmcs01); /* L0's desires */
2235 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
2236 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
2237 exec_control &= ~CPU_BASED_TPR_SHADOW;
2238 exec_control |= vmcs12->cpu_based_vm_exec_control;
2240 vmx->nested.l1_tpr_threshold = -1;
2241 if (exec_control & CPU_BASED_TPR_SHADOW)
2242 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
2243 #ifdef CONFIG_X86_64
2245 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2246 CPU_BASED_CR8_STORE_EXITING;
2250 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2251 * for I/O port accesses.
2253 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
2254 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2257 * This bit will be computed in nested_get_vmcs12_pages, because
2258 * we do not have access to L1's MSR bitmap yet. For now, keep
2259 * the same bit as before, hoping to avoid multiple VMWRITEs that
2260 * only set/clear this bit.
2262 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2263 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2265 exec_controls_set(vmx, exec_control);
2268 * SECONDARY EXEC CONTROLS
2270 if (cpu_has_secondary_exec_ctrls()) {
2271 exec_control = __secondary_exec_controls_get(vmcs01);
2273 /* Take the following fields only from vmcs12 */
2274 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2275 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2276 SECONDARY_EXEC_ENABLE_INVPCID |
2277 SECONDARY_EXEC_ENABLE_RDTSCP |
2278 SECONDARY_EXEC_XSAVES |
2279 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
2280 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2281 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2282 SECONDARY_EXEC_ENABLE_VMFUNC |
2283 SECONDARY_EXEC_DESC);
2285 if (nested_cpu_has(vmcs12,
2286 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
2287 exec_control |= vmcs12->secondary_vm_exec_control;
2289 /* PML is emulated and never enabled in hardware for L2. */
2290 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
2292 /* VMCS shadowing for L2 is emulated for now */
2293 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2296 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2297 * will not have to rewrite the controls just for this bit.
2299 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2300 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2301 exec_control |= SECONDARY_EXEC_DESC;
2303 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2304 vmcs_write16(GUEST_INTR_STATUS,
2305 vmcs12->guest_intr_status);
2307 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
2308 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
2310 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
2311 vmx_write_encls_bitmap(&vmx->vcpu, vmcs12);
2313 secondary_exec_controls_set(vmx, exec_control);
2319 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2320 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2321 * on the related bits (if supported by the CPU) in the hope that
2322 * we can avoid VMWrites during vmx_set_efer().
2324 * Similarly, take vmcs01's PERF_GLOBAL_CTRL in the hope that if KVM is
2325 * loading PERF_GLOBAL_CTRL via the VMCS for L1, then KVM will want to
2326 * do the same for L2.
2328 exec_control = __vm_entry_controls_get(vmcs01);
2329 exec_control |= (vmcs12->vm_entry_controls &
2330 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL);
2331 exec_control &= ~(VM_ENTRY_IA32E_MODE | VM_ENTRY_LOAD_IA32_EFER);
2332 if (cpu_has_load_ia32_efer()) {
2333 if (guest_efer & EFER_LMA)
2334 exec_control |= VM_ENTRY_IA32E_MODE;
2335 if (guest_efer != host_efer)
2336 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2338 vm_entry_controls_set(vmx, exec_control);
2343 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2344 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2345 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2347 exec_control = __vm_exit_controls_get(vmcs01);
2348 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2349 exec_control |= VM_EXIT_LOAD_IA32_EFER;
2351 exec_control &= ~VM_EXIT_LOAD_IA32_EFER;
2352 vm_exit_controls_set(vmx, exec_control);
2355 * Interrupt/Exception Fields
2357 if (vmx->nested.nested_run_pending) {
2358 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2359 vmcs12->vm_entry_intr_info_field);
2360 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2361 vmcs12->vm_entry_exception_error_code);
2362 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2363 vmcs12->vm_entry_instruction_len);
2364 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2365 vmcs12->guest_interruptibility_info);
2366 vmx->loaded_vmcs->nmi_known_unmasked =
2367 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2369 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2373 static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2375 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2377 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2378 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2379 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2380 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2381 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2382 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2383 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2384 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2385 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2386 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2387 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2388 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2389 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2390 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2391 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2392 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2393 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2394 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2395 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2396 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
2397 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2398 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
2399 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2400 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2401 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2402 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2403 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2404 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2405 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2406 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2407 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2408 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2409 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2410 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2411 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2412 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2413 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2414 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
2416 vmx->segment_cache.bitmask = 0;
2419 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2420 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2421 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2422 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2423 vmcs12->guest_pending_dbg_exceptions);
2424 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2425 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2428 * L1 may access the L2's PDPTR, so save them to construct
2432 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2433 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2434 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2435 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2438 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2439 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2440 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
2443 if (nested_cpu_has_xsaves(vmcs12))
2444 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2447 * Whether page-faults are trapped is determined by a combination of
2448 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0
2449 * doesn't care about page faults then we should set all of these to
2450 * L1's desires. However, if L0 does care about (some) page faults, it
2451 * is not easy (if at all possible?) to merge L0 and L1's desires, we
2452 * simply ask to exit on each and every L2 page fault. This is done by
2453 * setting MASK=MATCH=0 and (see below) EB.PF=1.
2454 * Note that below we don't need special code to set EB.PF beyond the
2455 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2456 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2457 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2459 if (vmx_need_pf_intercept(&vmx->vcpu)) {
2461 * TODO: if both L0 and L1 need the same MASK and MATCH,
2462 * go ahead and use it?
2464 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
2465 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
2467 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask);
2468 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match);
2471 if (cpu_has_vmx_apicv()) {
2472 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2473 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2474 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2475 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2479 * Make sure the msr_autostore list is up to date before we set the
2480 * count in the vmcs02.
2482 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2484 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
2485 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2486 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2488 set_cr4_guest_host_mask(vmx);
2492 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2493 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2494 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2495 * guest in a way that will both be appropriate to L1's requests, and our
2496 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2497 * function also has additional necessary side-effects, like setting various
2498 * vcpu->arch fields.
2499 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2500 * is assigned to entry_failure_code on failure.
2502 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
2504 enum vm_entry_failure_code *entry_failure_code)
2506 struct vcpu_vmx *vmx = to_vmx(vcpu);
2507 bool load_guest_pdptrs_vmcs12 = false;
2509 if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
2510 prepare_vmcs02_rare(vmx, vmcs12);
2511 vmx->nested.dirty_vmcs12 = false;
2513 load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) ||
2514 !(vmx->nested.hv_evmcs->hv_clean_fields &
2515 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
2518 if (vmx->nested.nested_run_pending &&
2519 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2520 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2521 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2523 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2524 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.pre_vmenter_debugctl);
2526 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2527 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2528 vmcs_write64(GUEST_BNDCFGS, vmx->nested.pre_vmenter_bndcfgs);
2529 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2531 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2532 * bitwise-or of what L1 wants to trap for L2, and what we want to
2533 * trap. Note that CR0.TS also needs updating - we do this later.
2535 vmx_update_exception_bitmap(vcpu);
2536 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2537 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2539 if (vmx->nested.nested_run_pending &&
2540 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2541 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2542 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2543 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2544 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2547 vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset(
2548 vcpu->arch.l1_tsc_offset,
2549 vmx_get_l2_tsc_offset(vcpu),
2550 vmx_get_l2_tsc_multiplier(vcpu));
2552 vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier(
2553 vcpu->arch.l1_tsc_scaling_ratio,
2554 vmx_get_l2_tsc_multiplier(vcpu));
2556 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
2557 if (kvm_caps.has_tsc_control)
2558 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
2560 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
2562 if (nested_cpu_has_ept(vmcs12))
2563 nested_ept_init_mmu_context(vcpu);
2566 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2567 * bits which we consider mandatory enabled.
2568 * The CR0_READ_SHADOW is what L2 should have expected to read given
2569 * the specifications by L1; It's not enough to take
2570 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we
2571 * have more bits than L1 expected.
2573 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2574 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2576 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2577 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2579 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2580 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2581 vmx_set_efer(vcpu, vcpu->arch.efer);
2584 * Guest state is invalid and unrestricted guest is disabled,
2585 * which means L1 attempted VMEntry to L2 with invalid state.
2588 * However when force loading the guest state (SMM exit or
2589 * loading nested state after migration, it is possible to
2590 * have invalid guest state now, which will be later fixed by
2591 * restoring L2 register state
2593 if (CC(from_vmentry && !vmx_guest_state_valid(vcpu))) {
2594 *entry_failure_code = ENTRY_FAIL_DEFAULT;
2598 /* Shadow page tables on either EPT or shadow page tables. */
2599 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
2600 from_vmentry, entry_failure_code))
2604 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2605 * on nested VM-Exit, which can occur without actually running L2 and
2606 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
2607 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2608 * transition to HLT instead of running L2.
2611 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2613 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2614 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2615 is_pae_paging(vcpu)) {
2616 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2617 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2618 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2619 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2622 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2623 intel_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu)) &&
2624 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2625 vmcs12->guest_ia32_perf_global_ctrl))) {
2626 *entry_failure_code = ENTRY_FAIL_DEFAULT;
2630 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2631 kvm_rip_write(vcpu, vmcs12->guest_rip);
2634 * It was observed that genuine Hyper-V running in L1 doesn't reset
2635 * 'hv_clean_fields' by itself, it only sets the corresponding dirty
2636 * bits when it changes a field in eVMCS. Mark all fields as clean
2639 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
2640 vmx->nested.hv_evmcs->hv_clean_fields |=
2641 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2646 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2648 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2649 nested_cpu_has_virtual_nmis(vmcs12)))
2652 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
2653 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
2659 static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
2661 struct vcpu_vmx *vmx = to_vmx(vcpu);
2663 /* Check for memory type validity */
2664 switch (new_eptp & VMX_EPTP_MT_MASK) {
2665 case VMX_EPTP_MT_UC:
2666 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
2669 case VMX_EPTP_MT_WB:
2670 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
2677 /* Page-walk levels validity. */
2678 switch (new_eptp & VMX_EPTP_PWL_MASK) {
2679 case VMX_EPTP_PWL_5:
2680 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2683 case VMX_EPTP_PWL_4:
2684 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2691 /* Reserved bits should not be set */
2692 if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f)))
2695 /* AD, if set, should be supported */
2696 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
2697 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
2705 * Checks related to VM-Execution Control Fields
2707 static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2708 struct vmcs12 *vmcs12)
2710 struct vcpu_vmx *vmx = to_vmx(vcpu);
2712 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2713 vmx->nested.msrs.pinbased_ctls_low,
2714 vmx->nested.msrs.pinbased_ctls_high)) ||
2715 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2716 vmx->nested.msrs.procbased_ctls_low,
2717 vmx->nested.msrs.procbased_ctls_high)))
2720 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
2721 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2722 vmx->nested.msrs.secondary_ctls_low,
2723 vmx->nested.msrs.secondary_ctls_high)))
2726 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
2727 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2728 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2729 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2730 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2731 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2732 nested_vmx_check_nmi_controls(vmcs12) ||
2733 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2734 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2735 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2736 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
2737 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
2740 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2741 nested_cpu_has_save_preemption_timer(vmcs12))
2744 if (nested_cpu_has_ept(vmcs12) &&
2745 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
2748 if (nested_cpu_has_vmfunc(vmcs12)) {
2749 if (CC(vmcs12->vm_function_control &
2750 ~vmx->nested.msrs.vmfunc_controls))
2753 if (nested_cpu_has_eptp_switching(vmcs12)) {
2754 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2755 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
2764 * Checks related to VM-Exit Control Fields
2766 static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2767 struct vmcs12 *vmcs12)
2769 struct vcpu_vmx *vmx = to_vmx(vcpu);
2771 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2772 vmx->nested.msrs.exit_ctls_low,
2773 vmx->nested.msrs.exit_ctls_high)) ||
2774 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
2781 * Checks related to VM-Entry Control Fields
2783 static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2784 struct vmcs12 *vmcs12)
2786 struct vcpu_vmx *vmx = to_vmx(vcpu);
2788 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2789 vmx->nested.msrs.entry_ctls_low,
2790 vmx->nested.msrs.entry_ctls_high)))
2794 * From the Intel SDM, volume 3:
2795 * Fields relevant to VM-entry event injection must be set properly.
2796 * These fields are the VM-entry interruption-information field, the
2797 * VM-entry exception error code, and the VM-entry instruction length.
2799 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2800 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2801 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2802 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2803 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2804 bool should_have_error_code;
2805 bool urg = nested_cpu_has2(vmcs12,
2806 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2807 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2809 /* VM-entry interruption-info field: interruption type */
2810 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2811 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2812 !nested_cpu_supports_monitor_trap_flag(vcpu)))
2815 /* VM-entry interruption-info field: vector */
2816 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2817 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2818 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
2821 /* VM-entry interruption-info field: deliver error code */
2822 should_have_error_code =
2823 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2824 x86_exception_has_error_code(vector);
2825 if (CC(has_error_code != should_have_error_code))
2828 /* VM-entry exception error code */
2829 if (CC(has_error_code &&
2830 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
2833 /* VM-entry interruption-info field: reserved bits */
2834 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
2837 /* VM-entry instruction length */
2838 switch (intr_type) {
2839 case INTR_TYPE_SOFT_EXCEPTION:
2840 case INTR_TYPE_SOFT_INTR:
2841 case INTR_TYPE_PRIV_SW_EXCEPTION:
2842 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2843 CC(vmcs12->vm_entry_instruction_len == 0 &&
2844 CC(!nested_cpu_has_zero_length_injection(vcpu))))
2849 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2855 static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2856 struct vmcs12 *vmcs12)
2858 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2859 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2860 nested_check_vm_entry_controls(vcpu, vmcs12))
2863 if (guest_cpuid_has_evmcs(vcpu))
2864 return nested_evmcs_check_controls(vmcs12);
2869 static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu,
2870 struct vmcs12 *vmcs12)
2872 #ifdef CONFIG_X86_64
2873 if (CC(!!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) !=
2874 !!(vcpu->arch.efer & EFER_LMA)))
2880 static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2881 struct vmcs12 *vmcs12)
2885 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2886 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
2887 CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3)))
2890 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2891 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
2894 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
2895 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
2898 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2899 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2900 vmcs12->host_ia32_perf_global_ctrl)))
2903 #ifdef CONFIG_X86_64
2904 ia32e = !!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE);
2910 if (CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2913 if (CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2914 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2915 CC((vmcs12->host_rip) >> 32))
2919 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2920 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2921 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2922 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2923 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2924 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2925 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2926 CC(vmcs12->host_cs_selector == 0) ||
2927 CC(vmcs12->host_tr_selector == 0) ||
2928 CC(vmcs12->host_ss_selector == 0 && !ia32e))
2931 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2932 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2933 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2934 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
2935 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2936 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
2940 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2941 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2942 * the values of the LMA and LME bits in the field must each be that of
2943 * the host address-space size VM-exit control.
2945 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
2946 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2947 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2948 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
2955 static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2956 struct vmcs12 *vmcs12)
2958 struct vcpu_vmx *vmx = to_vmx(vcpu);
2959 struct gfn_to_hva_cache *ghc = &vmx->nested.shadow_vmcs12_cache;
2960 struct vmcs_hdr hdr;
2962 if (vmcs12->vmcs_link_pointer == INVALID_GPA)
2965 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
2968 if (ghc->gpa != vmcs12->vmcs_link_pointer &&
2969 CC(kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc,
2970 vmcs12->vmcs_link_pointer, VMCS12_SIZE)))
2973 if (CC(kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr,
2974 offsetof(struct vmcs12, hdr),
2978 if (CC(hdr.revision_id != VMCS12_REVISION) ||
2979 CC(hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
2986 * Checks related to Guest Non-register State
2988 static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2990 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
2991 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT &&
2992 vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI))
2998 static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2999 struct vmcs12 *vmcs12,
3000 enum vm_entry_failure_code *entry_failure_code)
3004 *entry_failure_code = ENTRY_FAIL_DEFAULT;
3006 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
3007 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
3010 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
3011 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
3014 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
3015 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
3018 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
3019 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
3023 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
3024 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
3025 vmcs12->guest_ia32_perf_global_ctrl)))
3029 * If the load IA32_EFER VM-entry control is 1, the following checks
3030 * are performed on the field for the IA32_EFER MSR:
3031 * - Bits reserved in the IA32_EFER MSR must be 0.
3032 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
3033 * the IA-32e mode guest VM-exit control. It must also be identical
3034 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
3037 if (to_vmx(vcpu)->nested.nested_run_pending &&
3038 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
3039 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
3040 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
3041 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
3042 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
3043 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
3047 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
3048 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
3049 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
3052 if (nested_check_guest_non_reg_state(vmcs12))
3058 static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
3060 struct vcpu_vmx *vmx = to_vmx(vcpu);
3061 unsigned long cr3, cr4;
3064 if (!nested_early_check)
3067 if (vmx->msr_autoload.host.nr)
3068 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3069 if (vmx->msr_autoload.guest.nr)
3070 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3074 vmx_prepare_switch_to_guest(vcpu);
3077 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3078 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
3079 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
3080 * there is no need to preserve other bits or save/restore the field.
3082 vmcs_writel(GUEST_RFLAGS, 0);
3084 cr3 = __get_current_cr3_fast();
3085 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3086 vmcs_writel(HOST_CR3, cr3);
3087 vmx->loaded_vmcs->host_state.cr3 = cr3;
3090 cr4 = cr4_read_shadow();
3091 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3092 vmcs_writel(HOST_CR4, cr4);
3093 vmx->loaded_vmcs->host_state.cr4 = cr4;
3096 vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
3097 __vmx_vcpu_run_flags(vmx));
3099 if (vmx->msr_autoload.host.nr)
3100 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3101 if (vmx->msr_autoload.guest.nr)
3102 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3105 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3109 trace_kvm_nested_vmenter_failed(
3110 "early hardware check VM-instruction error: ", error);
3111 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3116 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3118 if (hw_breakpoint_active())
3119 set_debugreg(__this_cpu_read(cpu_dr7), 7);
3124 * A non-failing VMEntry means we somehow entered guest mode with
3125 * an illegal RIP, and that's just the tip of the iceberg. There
3126 * is no telling what memory has been modified or what state has
3127 * been exposed to unknown code. Hitting this all but guarantees
3128 * a (very critical) hardware issue.
3130 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3131 VMX_EXIT_REASONS_FAILED_VMENTRY));
3136 static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu)
3138 struct vcpu_vmx *vmx = to_vmx(vcpu);
3141 * hv_evmcs may end up being not mapped after migration (when
3142 * L2 was running), map it here to make sure vmcs12 changes are
3143 * properly reflected.
3145 if (guest_cpuid_has_evmcs(vcpu) &&
3146 vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) {
3147 enum nested_evmptrld_status evmptrld_status =
3148 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3150 if (evmptrld_status == EVMPTRLD_VMFAIL ||
3151 evmptrld_status == EVMPTRLD_ERROR)
3155 * Post migration VMCS12 always provides the most actual
3156 * information, copy it to eVMCS upon entry.
3158 vmx->nested.need_vmcs12_to_shadow_sync = true;
3164 static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3166 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3167 struct vcpu_vmx *vmx = to_vmx(vcpu);
3168 struct kvm_host_map *map;
3170 if (!vcpu->arch.pdptrs_from_userspace &&
3171 !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
3173 * Reload the guest's PDPTRs since after a migration
3174 * the guest CR3 might be restored prior to setting the nested
3175 * state which can lead to a load of wrong PDPTRs.
3177 if (CC(!load_pdptrs(vcpu, vcpu->arch.cr3)))
3182 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3183 map = &vmx->nested.apic_access_page_map;
3185 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->apic_access_addr), map)) {
3186 vmcs_write64(APIC_ACCESS_ADDR, pfn_to_hpa(map->pfn));
3188 pr_debug_ratelimited("%s: no backing for APIC-access address in vmcs12\n",
3190 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3191 vcpu->run->internal.suberror =
3192 KVM_INTERNAL_ERROR_EMULATION;
3193 vcpu->run->internal.ndata = 0;
3198 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3199 map = &vmx->nested.virtual_apic_map;
3201 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3202 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
3203 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3204 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3205 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3207 * The processor will never use the TPR shadow, simply
3208 * clear the bit from the execution control. Such a
3209 * configuration is useless, but it happens in tests.
3210 * For any other configuration, failing the vm entry is
3211 * _not_ what the processor does but it's basically the
3212 * only possibility we have.
3214 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
3217 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3218 * force VM-Entry to fail.
3220 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, INVALID_GPA);
3224 if (nested_cpu_has_posted_intr(vmcs12)) {
3225 map = &vmx->nested.pi_desc_map;
3227 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3228 vmx->nested.pi_desc =
3229 (struct pi_desc *)(((void *)map->hva) +
3230 offset_in_page(vmcs12->posted_intr_desc_addr));
3231 vmcs_write64(POSTED_INTR_DESC_ADDR,
3232 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
3235 * Defer the KVM_INTERNAL_EXIT until KVM tries to
3236 * access the contents of the VMCS12 posted interrupt
3237 * descriptor. (Note that KVM may do this when it
3238 * should not, per the architectural specification.)
3240 vmx->nested.pi_desc = NULL;
3241 pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR);
3244 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
3245 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
3247 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
3252 static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)
3254 if (!nested_get_evmcs_page(vcpu)) {
3255 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3257 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3258 vcpu->run->internal.suberror =
3259 KVM_INTERNAL_ERROR_EMULATION;
3260 vcpu->run->internal.ndata = 0;
3265 if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu))
3271 static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa)
3273 struct vmcs12 *vmcs12;
3274 struct vcpu_vmx *vmx = to_vmx(vcpu);
3277 if (WARN_ON_ONCE(!is_guest_mode(vcpu)))
3280 if (WARN_ON_ONCE(vmx->nested.pml_full))
3284 * Check if PML is enabled for the nested guest. Whether eptp bit 6 is
3285 * set is already checked as part of A/D emulation.
3287 vmcs12 = get_vmcs12(vcpu);
3288 if (!nested_cpu_has_pml(vmcs12))
3291 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
3292 vmx->nested.pml_full = true;
3297 dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
3299 if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
3300 offset_in_page(dst), sizeof(gpa)))
3303 vmcs12->guest_pml_index--;
3309 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3310 * for running VMX instructions (except VMXON, whose prerequisites are
3311 * slightly different). It also specifies what exception to inject otherwise.
3312 * Note that many of these exceptions have priority over VM exits, so they
3313 * don't have to be checked again here.
3315 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3317 if (!to_vmx(vcpu)->nested.vmxon) {
3318 kvm_queue_exception(vcpu, UD_VECTOR);
3322 if (vmx_get_cpl(vcpu)) {
3323 kvm_inject_gp(vcpu, 0);
3330 static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3332 u8 rvi = vmx_get_rvi();
3333 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3335 return ((rvi & 0xf0) > (vppr & 0xf0));
3338 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3339 struct vmcs12 *vmcs12);
3342 * If from_vmentry is false, this is being called from state restore (either RSM
3343 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
3346 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3347 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3348 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3349 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
3351 enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3354 struct vcpu_vmx *vmx = to_vmx(vcpu);
3355 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3356 enum vm_entry_failure_code entry_failure_code;
3357 bool evaluate_pending_interrupts;
3358 union vmx_exit_reason exit_reason = {
3359 .basic = EXIT_REASON_INVALID_STATE,
3360 .failed_vmentry = 1,
3364 trace_kvm_nested_vmenter(kvm_rip_read(vcpu),
3365 vmx->nested.current_vmptr,
3367 vmcs12->guest_intr_status,
3368 vmcs12->vm_entry_intr_info_field,
3369 vmcs12->secondary_vm_exec_control & SECONDARY_EXEC_ENABLE_EPT,
3370 vmcs12->ept_pointer,
3374 kvm_service_local_tlb_flush_requests(vcpu);
3376 evaluate_pending_interrupts = exec_controls_get(vmx) &
3377 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
3378 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3379 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3380 if (!evaluate_pending_interrupts)
3381 evaluate_pending_interrupts |= kvm_apic_has_pending_init_or_sipi(vcpu);
3383 if (!vmx->nested.nested_run_pending ||
3384 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3385 vmx->nested.pre_vmenter_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3386 if (kvm_mpx_supported() &&
3387 (!vmx->nested.nested_run_pending ||
3388 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
3389 vmx->nested.pre_vmenter_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3392 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3393 * nested early checks are disabled. In the event of a "late" VM-Fail,
3394 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3395 * software model to the pre-VMEntry host state. When EPT is disabled,
3396 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3397 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3398 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3399 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3400 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3401 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3402 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3403 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3404 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3405 * path would need to manually save/restore vmcs01.GUEST_CR3.
3407 if (!enable_ept && !nested_early_check)
3408 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3410 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3412 prepare_vmcs02_early(vmx, &vmx->vmcs01, vmcs12);
3415 if (unlikely(!nested_get_vmcs12_pages(vcpu))) {
3416 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3417 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
3420 if (nested_vmx_check_vmentry_hw(vcpu)) {
3421 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3422 return NVMX_VMENTRY_VMFAIL;
3425 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3426 &entry_failure_code)) {
3427 exit_reason.basic = EXIT_REASON_INVALID_STATE;
3428 vmcs12->exit_qualification = entry_failure_code;
3429 goto vmentry_fail_vmexit;
3433 enter_guest_mode(vcpu);
3435 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) {
3436 exit_reason.basic = EXIT_REASON_INVALID_STATE;
3437 vmcs12->exit_qualification = entry_failure_code;
3438 goto vmentry_fail_vmexit_guest_mode;
3442 failed_index = nested_vmx_load_msr(vcpu,
3443 vmcs12->vm_entry_msr_load_addr,
3444 vmcs12->vm_entry_msr_load_count);
3446 exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL;
3447 vmcs12->exit_qualification = failed_index;
3448 goto vmentry_fail_vmexit_guest_mode;
3452 * The MMU is not initialized to point at the right entities yet and
3453 * "get pages" would need to read data from the guest (i.e. we will
3454 * need to perform gpa to hpa translation). Request a call
3455 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3456 * have already been set at vmentry time and should not be reset.
3458 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
3462 * Re-evaluate pending events if L1 had a pending IRQ/NMI/INIT/SIPI
3463 * when it executed VMLAUNCH/VMRESUME, as entering non-root mode can
3464 * effectively unblock various events, e.g. INIT/SIPI cause VM-Exit
3467 if (unlikely(evaluate_pending_interrupts))
3468 kvm_make_request(KVM_REQ_EVENT, vcpu);
3471 * Do not start the preemption timer hrtimer until after we know
3472 * we are successful, so that only nested_vmx_vmexit needs to cancel
3475 vmx->nested.preemption_timer_expired = false;
3476 if (nested_cpu_has_preemption_timer(vmcs12)) {
3477 u64 timer_value = vmx_calc_preemption_timer_value(vcpu);
3478 vmx_start_preemption_timer(vcpu, timer_value);
3482 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3483 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3484 * returned as far as L1 is concerned. It will only return (and set
3485 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3487 return NVMX_VMENTRY_SUCCESS;
3490 * A failed consistency check that leads to a VMExit during L1's
3491 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3492 * 26.7 "VM-entry failures during or after loading guest state".
3494 vmentry_fail_vmexit_guest_mode:
3495 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
3496 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3497 leave_guest_mode(vcpu);
3499 vmentry_fail_vmexit:
3500 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3503 return NVMX_VMENTRY_VMEXIT;
3505 load_vmcs12_host_state(vcpu, vmcs12);
3506 vmcs12->vm_exit_reason = exit_reason.full;
3507 if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
3508 vmx->nested.need_vmcs12_to_shadow_sync = true;
3509 return NVMX_VMENTRY_VMEXIT;
3513 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3514 * for running an L2 nested guest.
3516 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3518 struct vmcs12 *vmcs12;
3519 enum nvmx_vmentry_status status;
3520 struct vcpu_vmx *vmx = to_vmx(vcpu);
3521 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
3522 enum nested_evmptrld_status evmptrld_status;
3524 if (!nested_vmx_check_permission(vcpu))
3527 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3528 if (evmptrld_status == EVMPTRLD_ERROR) {
3529 kvm_queue_exception(vcpu, UD_VECTOR);
3533 kvm_pmu_trigger_event(vcpu, PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
3535 if (CC(evmptrld_status == EVMPTRLD_VMFAIL))
3536 return nested_vmx_failInvalid(vcpu);
3538 if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) &&
3539 vmx->nested.current_vmptr == INVALID_GPA))
3540 return nested_vmx_failInvalid(vcpu);
3542 vmcs12 = get_vmcs12(vcpu);
3545 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3546 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3547 * rather than RFLAGS.ZF, and no error number is stored to the
3548 * VM-instruction error field.
3550 if (CC(vmcs12->hdr.shadow_vmcs))
3551 return nested_vmx_failInvalid(vcpu);
3553 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
3554 copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields);
3555 /* Enlightened VMCS doesn't have launch state */
3556 vmcs12->launch_state = !launch;
3557 } else if (enable_shadow_vmcs) {
3558 copy_shadow_to_vmcs12(vmx);
3562 * The nested entry process starts with enforcing various prerequisites
3563 * on vmcs12 as required by the Intel SDM, and act appropriately when
3564 * they fail: As the SDM explains, some conditions should cause the
3565 * instruction to fail, while others will cause the instruction to seem
3566 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3567 * To speed up the normal (success) code path, we should avoid checking
3568 * for misconfigurations which will anyway be caught by the processor
3569 * when using the merged vmcs02.
3571 if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS))
3572 return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
3574 if (CC(vmcs12->launch_state == launch))
3575 return nested_vmx_fail(vcpu,
3576 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3577 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3579 if (nested_vmx_check_controls(vcpu, vmcs12))
3580 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3582 if (nested_vmx_check_address_space_size(vcpu, vmcs12))
3583 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
3585 if (nested_vmx_check_host_state(vcpu, vmcs12))
3586 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
3589 * We're finally done with prerequisite checking, and can start with
3592 vmx->nested.nested_run_pending = 1;
3593 vmx->nested.has_preemption_timer_deadline = false;
3594 status = nested_vmx_enter_non_root_mode(vcpu, true);
3595 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3596 goto vmentry_failed;
3598 /* Emulate processing of posted interrupts on VM-Enter. */
3599 if (nested_cpu_has_posted_intr(vmcs12) &&
3600 kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) {
3601 vmx->nested.pi_pending = true;
3602 kvm_make_request(KVM_REQ_EVENT, vcpu);
3603 kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv);
3606 /* Hide L1D cache contents from the nested guest. */
3607 vmx->vcpu.arch.l1tf_flush_l1d = true;
3610 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3611 * also be used as part of restoring nVMX state for
3612 * snapshot restore (migration).
3614 * In this flow, it is assumed that vmcs12 cache was
3615 * transferred as part of captured nVMX state and should
3616 * therefore not be read from guest memory (which may not
3617 * exist on destination host yet).
3619 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3621 switch (vmcs12->guest_activity_state) {
3622 case GUEST_ACTIVITY_HLT:
3624 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3625 * awakened by event injection or by an NMI-window VM-exit or
3626 * by an interrupt-window VM-exit, halt the vcpu.
3628 if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3629 !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) &&
3630 !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) &&
3631 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3632 vmx->nested.nested_run_pending = 0;
3633 return kvm_emulate_halt_noskip(vcpu);
3636 case GUEST_ACTIVITY_WAIT_SIPI:
3637 vmx->nested.nested_run_pending = 0;
3638 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3647 vmx->nested.nested_run_pending = 0;
3648 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3650 if (status == NVMX_VMENTRY_VMEXIT)
3652 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
3653 return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3657 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
3658 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
3659 * This function returns the new value we should put in vmcs12.guest_cr0.
3660 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3661 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3662 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3663 * didn't trap the bit, because if L1 did, so would L0).
3664 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3665 * been modified by L2, and L1 knows it. So just leave the old value of
3666 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3667 * isn't relevant, because if L0 traps this bit it can set it to anything.
3668 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3669 * changed these bits, and therefore they need to be updated, but L0
3670 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3671 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3673 static inline unsigned long
3674 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3677 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3678 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3679 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3680 vcpu->arch.cr0_guest_owned_bits));
3683 static inline unsigned long
3684 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3687 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3688 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3689 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3690 vcpu->arch.cr4_guest_owned_bits));
3693 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3694 struct vmcs12 *vmcs12,
3695 u32 vm_exit_reason, u32 exit_intr_info)
3701 * Per the SDM, VM-Exits due to double and triple faults are never
3702 * considered to occur during event delivery, even if the double/triple
3703 * fault is the result of an escalating vectoring issue.
3705 * Note, the SDM qualifies the double fault behavior with "The original
3706 * event results in a double-fault exception". It's unclear why the
3707 * qualification exists since exits due to double fault can occur only
3708 * while vectoring a different exception (injected events are never
3709 * subject to interception), i.e. there's _always_ an original event.
3711 * The SDM also uses NMI as a confusing example for the "original event
3712 * causes the VM exit directly" clause. NMI isn't special in any way,
3713 * the same rule applies to all events that cause an exit directly.
3714 * NMI is an odd choice for the example because NMIs can only occur on
3715 * instruction boundaries, i.e. they _can't_ occur during vectoring.
3717 if ((u16)vm_exit_reason == EXIT_REASON_TRIPLE_FAULT ||
3718 ((u16)vm_exit_reason == EXIT_REASON_EXCEPTION_NMI &&
3719 is_double_fault(exit_intr_info))) {
3720 vmcs12->idt_vectoring_info_field = 0;
3721 } else if (vcpu->arch.exception.injected) {
3722 nr = vcpu->arch.exception.vector;
3723 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3725 if (kvm_exception_is_soft(nr)) {
3726 vmcs12->vm_exit_instruction_len =
3727 vcpu->arch.event_exit_inst_len;
3728 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3730 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3732 if (vcpu->arch.exception.has_error_code) {
3733 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3734 vmcs12->idt_vectoring_error_code =
3735 vcpu->arch.exception.error_code;
3738 vmcs12->idt_vectoring_info_field = idt_vectoring;
3739 } else if (vcpu->arch.nmi_injected) {
3740 vmcs12->idt_vectoring_info_field =
3741 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3742 } else if (vcpu->arch.interrupt.injected) {
3743 nr = vcpu->arch.interrupt.nr;
3744 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3746 if (vcpu->arch.interrupt.soft) {
3747 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3748 vmcs12->vm_entry_instruction_len =
3749 vcpu->arch.event_exit_inst_len;
3751 idt_vectoring |= INTR_TYPE_EXT_INTR;
3753 vmcs12->idt_vectoring_info_field = idt_vectoring;
3755 vmcs12->idt_vectoring_info_field = 0;
3760 void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
3762 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3766 * Don't need to mark the APIC access page dirty; it is never
3767 * written to by the CPU during APIC virtualization.
3770 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3771 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3772 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3775 if (nested_cpu_has_posted_intr(vmcs12)) {
3776 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3777 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3781 static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
3783 struct vcpu_vmx *vmx = to_vmx(vcpu);
3788 if (!vmx->nested.pi_pending)
3791 if (!vmx->nested.pi_desc)
3794 vmx->nested.pi_pending = false;
3796 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
3799 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3800 if (max_irr != 256) {
3801 vapic_page = vmx->nested.virtual_apic_map.hva;
3805 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3806 vapic_page, &max_irr);
3807 status = vmcs_read16(GUEST_INTR_STATUS);
3808 if ((u8)max_irr > ((u8)status & 0xff)) {
3810 status |= (u8)max_irr;
3811 vmcs_write16(GUEST_INTR_STATUS, status);
3815 nested_mark_vmcs12_pages_dirty(vcpu);
3819 kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL);
3823 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu)
3825 struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
3826 u32 intr_info = ex->vector | INTR_INFO_VALID_MASK;
3827 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3828 unsigned long exit_qual;
3830 if (ex->has_payload) {
3831 exit_qual = ex->payload;
3832 } else if (ex->vector == PF_VECTOR) {
3833 exit_qual = vcpu->arch.cr2;
3834 } else if (ex->vector == DB_VECTOR) {
3835 exit_qual = vcpu->arch.dr6;
3836 exit_qual &= ~DR6_BT;
3837 exit_qual ^= DR6_ACTIVE_LOW;
3842 if (ex->has_error_code) {
3844 * Intel CPUs do not generate error codes with bits 31:16 set,
3845 * and more importantly VMX disallows setting bits 31:16 in the
3846 * injected error code for VM-Entry. Drop the bits to mimic
3847 * hardware and avoid inducing failure on nested VM-Entry if L1
3848 * chooses to inject the exception back to L2. AMD CPUs _do_
3849 * generate "full" 32-bit error codes, so KVM allows userspace
3850 * to inject exception error codes with bits 31:16 set.
3852 vmcs12->vm_exit_intr_error_code = (u16)ex->error_code;
3853 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3856 if (kvm_exception_is_soft(ex->vector))
3857 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3859 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3861 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3862 vmx_get_nmi_mask(vcpu))
3863 intr_info |= INTR_INFO_UNBLOCK_NMI;
3865 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3869 * Returns true if a debug trap is (likely) pending delivery. Infer the class
3870 * of a #DB (trap-like vs. fault-like) from the exception payload (to-be-DR6).
3871 * Using the payload is flawed because code breakpoints (fault-like) and data
3872 * breakpoints (trap-like) set the same bits in DR6 (breakpoint detected), i.e.
3873 * this will return false positives if a to-be-injected code breakpoint #DB is
3874 * pending (from KVM's perspective, but not "pending" across an instruction
3875 * boundary). ICEBP, a.k.a. INT1, is also not reflected here even though it
3878 * KVM "works" despite these flaws as ICEBP isn't currently supported by the
3879 * emulator, Monitor Trap Flag is not marked pending on intercepted #DBs (the
3880 * #DB has already happened), and MTF isn't marked pending on code breakpoints
3881 * from the emulator (because such #DBs are fault-like and thus don't trigger
3882 * actions that fire on instruction retire).
3884 static unsigned long vmx_get_pending_dbg_trap(struct kvm_queued_exception *ex)
3886 if (!ex->pending || ex->vector != DB_VECTOR)
3889 /* General Detect #DBs are always fault-like. */
3890 return ex->payload & ~DR6_BD;
3894 * Returns true if there's a pending #DB exception that is lower priority than
3895 * a pending Monitor Trap Flag VM-Exit. TSS T-flag #DBs are not emulated by
3896 * KVM, but could theoretically be injected by userspace. Note, this code is
3897 * imperfect, see above.
3899 static bool vmx_is_low_priority_db_trap(struct kvm_queued_exception *ex)
3901 return vmx_get_pending_dbg_trap(ex) & ~DR6_BT;
3905 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3906 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3907 * represents these debug traps with a payload that is said to be compatible
3908 * with the 'pending debug exceptions' field, write the payload to the VMCS
3909 * field if a VM-exit is delivered before the debug trap.
3911 static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3913 unsigned long pending_dbg;
3915 pending_dbg = vmx_get_pending_dbg_trap(&vcpu->arch.exception);
3917 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, pending_dbg);
3920 static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3922 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3923 to_vmx(vcpu)->nested.preemption_timer_expired;
3926 static bool vmx_has_nested_events(struct kvm_vcpu *vcpu)
3928 return nested_vmx_preemption_timer_pending(vcpu) ||
3929 to_vmx(vcpu)->nested.mtf_pending;
3933 * Per the Intel SDM's table "Priority Among Concurrent Events", with minor
3934 * edits to fill in missing examples, e.g. #DB due to split-lock accesses,
3935 * and less minor edits to splice in the priority of VMX Non-Root specific
3936 * events, e.g. MTF and NMI/INTR-window exiting.
3938 * 1 Hardware Reset and Machine Checks
3942 * 2 Trap on Task Switch
3943 * - T flag in TSS is set (on task switch)
3945 * 3 External Hardware Interventions
3951 * 3.5 Monitor Trap Flag (MTF) VM-exit[1]
3953 * 4 Traps on Previous Instruction
3955 * - Trap-class Debug Exceptions (#DB due to TF flag set, data/I-O
3956 * breakpoint, or #DB due to a split-lock access)
3958 * 4.3 VMX-preemption timer expired VM-exit
3960 * 4.6 NMI-window exiting VM-exit[2]
3962 * 5 Nonmaskable Interrupts (NMI)
3964 * 5.5 Interrupt-window exiting VM-exit and Virtual-interrupt delivery
3966 * 6 Maskable Hardware Interrupts
3968 * 7 Code Breakpoint Fault
3970 * 8 Faults from Fetching Next Instruction
3971 * - Code-Segment Limit Violation
3973 * - Control protection exception (missing ENDBRANCH at target of indirect
3976 * 9 Faults from Decoding Next Instruction
3977 * - Instruction length > 15 bytes
3979 * - Coprocessor Not Available
3981 *10 Faults on Executing Instruction
3985 * - Segment Not Present
3987 * - General Protection
3990 * - x86 FPU Floating-point exception
3991 * - SIMD floating-point exception
3992 * - Virtualization exception
3993 * - Control protection exception
3995 * [1] Per the "Monitor Trap Flag" section: System-management interrupts (SMIs),
3996 * INIT signals, and higher priority events take priority over MTF VM exits.
3997 * MTF VM exits take priority over debug-trap exceptions and lower priority
4000 * [2] Debug-trap exceptions and higher priority events take priority over VM exits
4001 * caused by the VMX-preemption timer. VM exits caused by the VMX-preemption
4002 * timer take priority over VM exits caused by the "NMI-window exiting"
4003 * VM-execution control and lower priority events.
4005 * [3] Debug-trap exceptions and higher priority events take priority over VM exits
4006 * caused by "NMI-window exiting". VM exits caused by this control take
4007 * priority over non-maskable interrupts (NMIs) and lower priority events.
4009 * [4] Virtual-interrupt delivery has the same priority as that of VM exits due to
4010 * the 1-setting of the "interrupt-window exiting" VM-execution control. Thus,
4011 * non-maskable interrupts (NMIs) and higher priority events take priority over
4012 * delivery of a virtual interrupt; delivery of a virtual interrupt takes
4013 * priority over external interrupts and lower priority events.
4015 static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
4017 struct kvm_lapic *apic = vcpu->arch.apic;
4018 struct vcpu_vmx *vmx = to_vmx(vcpu);
4020 * Only a pending nested run blocks a pending exception. If there is a
4021 * previously injected event, the pending exception occurred while said
4022 * event was being delivered and thus needs to be handled.
4024 bool block_nested_exceptions = vmx->nested.nested_run_pending;
4026 * New events (not exceptions) are only recognized at instruction
4027 * boundaries. If an event needs reinjection, then KVM is handling a
4028 * VM-Exit that occurred _during_ instruction execution; new events are
4029 * blocked until the instruction completes.
4031 bool block_nested_events = block_nested_exceptions ||
4032 kvm_event_needs_reinjection(vcpu);
4034 if (lapic_in_kernel(vcpu) &&
4035 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
4036 if (block_nested_events)
4038 nested_vmx_update_pending_dbg(vcpu);
4039 clear_bit(KVM_APIC_INIT, &apic->pending_events);
4040 if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED)
4041 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
4043 /* MTF is discarded if the vCPU is in WFS. */
4044 vmx->nested.mtf_pending = false;
4048 if (lapic_in_kernel(vcpu) &&
4049 test_bit(KVM_APIC_SIPI, &apic->pending_events)) {
4050 if (block_nested_events)
4053 clear_bit(KVM_APIC_SIPI, &apic->pending_events);
4054 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
4055 nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0,
4056 apic->sipi_vector & 0xFFUL);
4059 /* Fallthrough, the SIPI is completely ignored. */
4063 * Process exceptions that are higher priority than Monitor Trap Flag:
4064 * fault-like exceptions, TSS T flag #DB (not emulated by KVM, but
4065 * could theoretically come in from userspace), and ICEBP (INT1).
4067 * TODO: SMIs have higher priority than MTF and trap-like #DBs (except
4068 * for TSS T flag #DBs). KVM also doesn't save/restore pending MTF
4069 * across SMI/RSM as it should; that needs to be addressed in order to
4070 * prioritize SMI over MTF and trap-like #DBs.
4072 if (vcpu->arch.exception_vmexit.pending &&
4073 !vmx_is_low_priority_db_trap(&vcpu->arch.exception_vmexit)) {
4074 if (block_nested_exceptions)
4077 nested_vmx_inject_exception_vmexit(vcpu);
4081 if (vcpu->arch.exception.pending &&
4082 !vmx_is_low_priority_db_trap(&vcpu->arch.exception)) {
4083 if (block_nested_exceptions)
4088 if (vmx->nested.mtf_pending) {
4089 if (block_nested_events)
4091 nested_vmx_update_pending_dbg(vcpu);
4092 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
4096 if (vcpu->arch.exception_vmexit.pending) {
4097 if (block_nested_exceptions)
4100 nested_vmx_inject_exception_vmexit(vcpu);
4104 if (vcpu->arch.exception.pending) {
4105 if (block_nested_exceptions)
4110 if (nested_vmx_preemption_timer_pending(vcpu)) {
4111 if (block_nested_events)
4113 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
4117 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
4118 if (block_nested_events)
4123 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
4124 if (block_nested_events)
4126 if (!nested_exit_on_nmi(vcpu))
4129 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
4130 NMI_VECTOR | INTR_TYPE_NMI_INTR |
4131 INTR_INFO_VALID_MASK, 0);
4133 * The NMI-triggered VM exit counts as injection:
4134 * clear this one and block further NMIs.
4136 vcpu->arch.nmi_pending = 0;
4137 vmx_set_nmi_mask(vcpu, true);
4141 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
4142 if (block_nested_events)
4144 if (!nested_exit_on_intr(vcpu))
4146 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
4151 return vmx_complete_nested_posted_interrupt(vcpu);
4154 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
4157 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
4160 if (ktime_to_ns(remaining) <= 0)
4163 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
4164 do_div(value, 1000000);
4165 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
4168 static bool is_vmcs12_ext_field(unsigned long field)
4171 case GUEST_ES_SELECTOR:
4172 case GUEST_CS_SELECTOR:
4173 case GUEST_SS_SELECTOR:
4174 case GUEST_DS_SELECTOR:
4175 case GUEST_FS_SELECTOR:
4176 case GUEST_GS_SELECTOR:
4177 case GUEST_LDTR_SELECTOR:
4178 case GUEST_TR_SELECTOR:
4179 case GUEST_ES_LIMIT:
4180 case GUEST_CS_LIMIT:
4181 case GUEST_SS_LIMIT:
4182 case GUEST_DS_LIMIT:
4183 case GUEST_FS_LIMIT:
4184 case GUEST_GS_LIMIT:
4185 case GUEST_LDTR_LIMIT:
4186 case GUEST_TR_LIMIT:
4187 case GUEST_GDTR_LIMIT:
4188 case GUEST_IDTR_LIMIT:
4189 case GUEST_ES_AR_BYTES:
4190 case GUEST_DS_AR_BYTES:
4191 case GUEST_FS_AR_BYTES:
4192 case GUEST_GS_AR_BYTES:
4193 case GUEST_LDTR_AR_BYTES:
4194 case GUEST_TR_AR_BYTES:
4201 case GUEST_LDTR_BASE:
4203 case GUEST_GDTR_BASE:
4204 case GUEST_IDTR_BASE:
4205 case GUEST_PENDING_DBG_EXCEPTIONS:
4215 static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4216 struct vmcs12 *vmcs12)
4218 struct vcpu_vmx *vmx = to_vmx(vcpu);
4220 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
4221 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
4222 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
4223 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
4224 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
4225 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
4226 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
4227 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
4228 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
4229 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
4230 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
4231 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
4232 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
4233 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
4234 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
4235 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
4236 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
4237 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
4238 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
4239 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
4240 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
4241 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
4242 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
4243 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
4244 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
4245 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
4246 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
4247 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
4248 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
4249 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
4250 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
4251 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
4252 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
4253 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
4254 vmcs12->guest_pending_dbg_exceptions =
4255 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
4257 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
4260 static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
4261 struct vmcs12 *vmcs12)
4263 struct vcpu_vmx *vmx = to_vmx(vcpu);
4266 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
4270 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
4273 vmx->loaded_vmcs = &vmx->nested.vmcs02;
4274 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
4276 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4278 vmx->loaded_vmcs = &vmx->vmcs01;
4279 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
4284 * Update the guest state fields of vmcs12 to reflect changes that
4285 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
4286 * VM-entry controls is also updated, since this is really a guest
4289 static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
4291 struct vcpu_vmx *vmx = to_vmx(vcpu);
4293 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
4294 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4296 vmx->nested.need_sync_vmcs02_to_vmcs12_rare =
4297 !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr);
4299 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
4300 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
4302 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
4303 vmcs12->guest_rip = kvm_rip_read(vcpu);
4304 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
4306 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
4307 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
4309 vmcs12->guest_interruptibility_info =
4310 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
4312 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
4313 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
4314 else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
4315 vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI;
4317 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
4319 if (nested_cpu_has_preemption_timer(vmcs12) &&
4320 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER &&
4321 !vmx->nested.nested_run_pending)
4322 vmcs12->vmx_preemption_timer_value =
4323 vmx_get_preemption_timer_value(vcpu);
4326 * In some cases (usually, nested EPT), L2 is allowed to change its
4327 * own CR3 without exiting. If it has changed it, we must keep it.
4328 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
4329 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
4331 * Additionally, restore L2's PDPTR to vmcs12.
4334 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
4335 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
4336 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
4337 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
4338 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
4339 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
4343 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
4345 if (nested_cpu_has_vid(vmcs12))
4346 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
4348 vmcs12->vm_entry_controls =
4349 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
4350 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
4352 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
4353 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
4355 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
4356 vmcs12->guest_ia32_efer = vcpu->arch.efer;
4360 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4361 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4362 * and this function updates it to reflect the changes to the guest state while
4363 * L2 was running (and perhaps made some exits which were handled directly by L0
4364 * without going back to L1), and to reflect the exit reason.
4365 * Note that we do not have to copy here all VMCS fields, just those that
4366 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4367 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4368 * which already writes to vmcs12 directly.
4370 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
4371 u32 vm_exit_reason, u32 exit_intr_info,
4372 unsigned long exit_qualification)
4374 /* update exit information fields: */
4375 vmcs12->vm_exit_reason = vm_exit_reason;
4376 if (to_vmx(vcpu)->exit_reason.enclave_mode)
4377 vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE;
4378 vmcs12->exit_qualification = exit_qualification;
4381 * On VM-Exit due to a failed VM-Entry, the VMCS isn't marked launched
4382 * and only EXIT_REASON and EXIT_QUALIFICATION are updated, all other
4383 * exit info fields are unmodified.
4385 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4386 vmcs12->launch_state = 1;
4388 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4389 * instead of reading the real value. */
4390 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4393 * Transfer the event that L0 or L1 may wanted to inject into
4394 * L2 to IDT_VECTORING_INFO_FIELD.
4396 vmcs12_save_pending_event(vcpu, vmcs12,
4397 vm_exit_reason, exit_intr_info);
4399 vmcs12->vm_exit_intr_info = exit_intr_info;
4400 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4401 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4404 * According to spec, there's no need to store the guest's
4405 * MSRs if the exit is due to a VM-entry failure that occurs
4406 * during or after loading the guest state. Since this exit
4407 * does not fall in that category, we need to save the MSRs.
4409 if (nested_vmx_store_msr(vcpu,
4410 vmcs12->vm_exit_msr_store_addr,
4411 vmcs12->vm_exit_msr_store_count))
4412 nested_vmx_abort(vcpu,
4413 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
4418 * A part of what we need to when the nested L2 guest exits and we want to
4419 * run its L1 parent, is to reset L1's guest state to the host state specified
4421 * This function is to be called not only on normal nested exit, but also on
4422 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4423 * Failures During or After Loading Guest State").
4424 * This function should be called when the active VMCS is L1's (vmcs01).
4426 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4427 struct vmcs12 *vmcs12)
4429 enum vm_entry_failure_code ignored;
4430 struct kvm_segment seg;
4432 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4433 vcpu->arch.efer = vmcs12->host_ia32_efer;
4434 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4435 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4437 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4438 vmx_set_efer(vcpu, vcpu->arch.efer);
4440 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4441 kvm_rip_write(vcpu, vmcs12->host_rip);
4442 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4443 vmx_set_interrupt_shadow(vcpu, 0);
4446 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4447 * actually changed, because vmx_set_cr0 refers to efer set above.
4449 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4450 * (KVM doesn't change it);
4452 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4453 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4455 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4456 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4457 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4459 nested_ept_uninit_mmu_context(vcpu);
4462 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4463 * couldn't have changed.
4465 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored))
4466 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4468 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
4470 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4471 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4472 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4473 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4474 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4475 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4476 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4478 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4479 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4480 vmcs_write64(GUEST_BNDCFGS, 0);
4482 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4483 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4484 vcpu->arch.pat = vmcs12->host_ia32_pat;
4486 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
4487 intel_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu)))
4488 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4489 vmcs12->host_ia32_perf_global_ctrl));
4491 /* Set L1 segment info according to Intel SDM
4492 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4493 seg = (struct kvm_segment) {
4495 .limit = 0xFFFFFFFF,
4496 .selector = vmcs12->host_cs_selector,
4502 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4506 __vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4507 seg = (struct kvm_segment) {
4509 .limit = 0xFFFFFFFF,
4516 seg.selector = vmcs12->host_ds_selector;
4517 __vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4518 seg.selector = vmcs12->host_es_selector;
4519 __vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4520 seg.selector = vmcs12->host_ss_selector;
4521 __vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4522 seg.selector = vmcs12->host_fs_selector;
4523 seg.base = vmcs12->host_fs_base;
4524 __vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4525 seg.selector = vmcs12->host_gs_selector;
4526 seg.base = vmcs12->host_gs_base;
4527 __vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4528 seg = (struct kvm_segment) {
4529 .base = vmcs12->host_tr_base,
4531 .selector = vmcs12->host_tr_selector,
4535 __vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4537 memset(&seg, 0, sizeof(seg));
4539 __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR);
4541 kvm_set_dr(vcpu, 7, 0x400);
4542 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4544 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4545 vmcs12->vm_exit_msr_load_count))
4546 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4548 to_vmx(vcpu)->emulation_required = vmx_emulation_required(vcpu);
4551 static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4553 struct vmx_uret_msr *efer_msr;
4556 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4557 return vmcs_read64(GUEST_IA32_EFER);
4559 if (cpu_has_load_ia32_efer())
4562 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4563 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4564 return vmx->msr_autoload.guest.val[i].value;
4567 efer_msr = vmx_find_uret_msr(vmx, MSR_EFER);
4569 return efer_msr->data;
4574 static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4576 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4577 struct vcpu_vmx *vmx = to_vmx(vcpu);
4578 struct vmx_msr_entry g, h;
4582 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4584 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4586 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4587 * as vmcs01.GUEST_DR7 contains a userspace defined value
4588 * and vcpu->arch.dr7 is not squirreled away before the
4589 * nested VMENTER (not worth adding a variable in nested_vmx).
4591 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4592 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4594 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4598 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4599 * handle a variety of side effects to KVM's software model.
4601 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4603 vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4604 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4606 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4607 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4609 nested_ept_uninit_mmu_context(vcpu);
4610 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4611 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
4614 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4615 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4616 * VMFail, like everything else we just need to ensure our
4617 * software model is up-to-date.
4619 if (enable_ept && is_pae_paging(vcpu))
4620 ept_save_pdptrs(vcpu);
4622 kvm_mmu_reset_context(vcpu);
4625 * This nasty bit of open coding is a compromise between blindly
4626 * loading L1's MSRs using the exit load lists (incorrect emulation
4627 * of VMFail), leaving the nested VM's MSRs in the software model
4628 * (incorrect behavior) and snapshotting the modified MSRs (too
4629 * expensive since the lists are unbound by hardware). For each
4630 * MSR that was (prematurely) loaded from the nested VMEntry load
4631 * list, reload it from the exit load list if it exists and differs
4632 * from the guest value. The intent is to stuff host state as
4633 * silently as possible, not to fully process the exit load list.
4635 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4636 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4637 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4638 pr_debug_ratelimited(
4639 "%s read MSR index failed (%u, 0x%08llx)\n",
4644 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4645 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4646 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4647 pr_debug_ratelimited(
4648 "%s read MSR failed (%u, 0x%08llx)\n",
4652 if (h.index != g.index)
4654 if (h.value == g.value)
4657 if (nested_vmx_load_msr_check(vcpu, &h)) {
4658 pr_debug_ratelimited(
4659 "%s check failed (%u, 0x%x, 0x%x)\n",
4660 __func__, j, h.index, h.reserved);
4664 if (kvm_set_msr(vcpu, h.index, h.value)) {
4665 pr_debug_ratelimited(
4666 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4667 __func__, j, h.index, h.value);
4676 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4680 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4681 * and modify vmcs12 to make it see what it would expect to see there if
4682 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4684 void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
4685 u32 exit_intr_info, unsigned long exit_qualification)
4687 struct vcpu_vmx *vmx = to_vmx(vcpu);
4688 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4690 /* Pending MTF traps are discarded on VM-Exit. */
4691 vmx->nested.mtf_pending = false;
4693 /* trying to cancel vmlaunch/vmresume is a bug */
4694 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4696 if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) {
4698 * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map
4699 * Enlightened VMCS after migration and we still need to
4700 * do that when something is forcing L2->L1 exit prior to
4703 (void)nested_get_evmcs_page(vcpu);
4706 /* Service pending TLB flush requests for L2 before switching to L1. */
4707 kvm_service_local_tlb_flush_requests(vcpu);
4710 * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between
4711 * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are
4712 * up-to-date before switching to L1.
4714 if (enable_ept && is_pae_paging(vcpu))
4715 vmx_ept_load_pdptrs(vcpu);
4717 leave_guest_mode(vcpu);
4719 if (nested_cpu_has_preemption_timer(vmcs12))
4720 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4722 if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) {
4723 vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset;
4724 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING))
4725 vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio;
4728 if (likely(!vmx->fail)) {
4729 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
4731 if (vm_exit_reason != -1)
4732 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4733 exit_intr_info, exit_qualification);
4736 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
4737 * also be used to capture vmcs12 cache as part of
4738 * capturing nVMX state for snapshot (migration).
4740 * Otherwise, this flush will dirty guest memory at a
4741 * point it is already assumed by user-space to be
4744 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
4747 * The only expected VM-instruction error is "VM entry with
4748 * invalid control field(s)." Anything else indicates a
4749 * problem with L0. And we should never get here with a
4750 * VMFail of any type if early consistency checks are enabled.
4752 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4753 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4754 WARN_ON_ONCE(nested_early_check);
4758 * Drop events/exceptions that were queued for re-injection to L2
4759 * (picked up via vmx_complete_interrupts()), as well as exceptions
4760 * that were pending for L2. Note, this must NOT be hoisted above
4761 * prepare_vmcs12(), events/exceptions queued for re-injection need to
4762 * be captured in vmcs12 (see vmcs12_save_pending_event()).
4764 vcpu->arch.nmi_injected = false;
4765 kvm_clear_exception_queue(vcpu);
4766 kvm_clear_interrupt_queue(vcpu);
4768 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4770 /* Update any VMCS fields that might have changed while L2 ran */
4771 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4772 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4773 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
4774 if (kvm_caps.has_tsc_control)
4775 vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio);
4777 if (vmx->nested.l1_tpr_threshold != -1)
4778 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
4780 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4781 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4782 vmx_set_virtual_apic_mode(vcpu);
4785 if (vmx->nested.update_vmcs01_cpu_dirty_logging) {
4786 vmx->nested.update_vmcs01_cpu_dirty_logging = false;
4787 vmx_update_cpu_dirty_logging(vcpu);
4790 /* Unpin physical memory we referred to in vmcs02 */
4791 kvm_vcpu_unmap(vcpu, &vmx->nested.apic_access_page_map, false);
4792 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
4793 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4794 vmx->nested.pi_desc = NULL;
4796 if (vmx->nested.reload_vmcs01_apic_access_page) {
4797 vmx->nested.reload_vmcs01_apic_access_page = false;
4798 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4801 if (vmx->nested.update_vmcs01_apicv_status) {
4802 vmx->nested.update_vmcs01_apicv_status = false;
4803 kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
4806 if ((vm_exit_reason != -1) &&
4807 (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)))
4808 vmx->nested.need_vmcs12_to_shadow_sync = true;
4810 /* in case we halted in L2 */
4811 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4813 if (likely(!vmx->fail)) {
4814 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
4815 nested_exit_intr_ack_set(vcpu)) {
4816 int irq = kvm_cpu_get_interrupt(vcpu);
4818 vmcs12->vm_exit_intr_info = irq |
4819 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4822 if (vm_exit_reason != -1)
4823 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4824 vmcs12->exit_qualification,
4825 vmcs12->idt_vectoring_info_field,
4826 vmcs12->vm_exit_intr_info,
4827 vmcs12->vm_exit_intr_error_code,
4830 load_vmcs12_host_state(vcpu, vmcs12);
4836 * After an early L2 VM-entry failure, we're now back
4837 * in L1 which thinks it just finished a VMLAUNCH or
4838 * VMRESUME instruction, so we need to set the failure
4839 * flag and the VM-instruction error field of the VMCS
4840 * accordingly, and skip the emulated instruction.
4842 (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4845 * Restore L1's host state to KVM's software model. We're here
4846 * because a consistency check was caught by hardware, which
4847 * means some amount of guest state has been propagated to KVM's
4848 * model and needs to be unwound to the host's state.
4850 nested_vmx_restore_host_state(vcpu);
4855 static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu)
4857 nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0);
4861 * Decode the memory-address operand of a vmx instruction, as recorded on an
4862 * exit caused by such an instruction (run by a guest hypervisor).
4863 * On success, returns 0. When the operand is invalid, returns 1 and throws
4866 int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
4867 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
4871 struct kvm_segment s;
4874 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4875 * Execution", on an exit, vmx_instruction_info holds most of the
4876 * addressing components of the operand. Only the displacement part
4877 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4878 * For how an actual address is calculated from all these components,
4879 * refer to Vol. 1, "Operand Addressing".
4881 int scaling = vmx_instruction_info & 3;
4882 int addr_size = (vmx_instruction_info >> 7) & 7;
4883 bool is_reg = vmx_instruction_info & (1u << 10);
4884 int seg_reg = (vmx_instruction_info >> 15) & 7;
4885 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4886 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4887 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4888 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4891 kvm_queue_exception(vcpu, UD_VECTOR);
4895 /* Addr = segment_base + offset */
4896 /* offset = base + [index * scale] + displacement */
4897 off = exit_qualification; /* holds the displacement */
4899 off = (gva_t)sign_extend64(off, 31);
4900 else if (addr_size == 0)
4901 off = (gva_t)sign_extend64(off, 15);
4903 off += kvm_register_read(vcpu, base_reg);
4905 off += kvm_register_read(vcpu, index_reg) << scaling;
4906 vmx_get_segment(vcpu, &s, seg_reg);
4909 * The effective address, i.e. @off, of a memory operand is truncated
4910 * based on the address size of the instruction. Note that this is
4911 * the *effective address*, i.e. the address prior to accounting for
4912 * the segment's base.
4914 if (addr_size == 1) /* 32 bit */
4916 else if (addr_size == 0) /* 16 bit */
4919 /* Checks for #GP/#SS exceptions. */
4921 if (is_long_mode(vcpu)) {
4923 * The virtual/linear address is never truncated in 64-bit
4924 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4925 * address when using FS/GS with a non-zero base.
4927 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4928 *ret = s.base + off;
4932 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4933 * non-canonical form. This is the only check on the memory
4934 * destination for long mode!
4936 exn = is_noncanonical_address(*ret, vcpu);
4939 * When not in long mode, the virtual/linear address is
4940 * unconditionally truncated to 32 bits regardless of the
4943 *ret = (s.base + off) & 0xffffffff;
4945 /* Protected mode: apply checks for segment validity in the
4947 * - segment type check (#GP(0) may be thrown)
4948 * - usability check (#GP(0)/#SS(0))
4949 * - limit check (#GP(0)/#SS(0))
4952 /* #GP(0) if the destination operand is located in a
4953 * read-only data segment or any code segment.
4955 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4957 /* #GP(0) if the source operand is located in an
4958 * execute-only code segment
4960 exn = ((s.type & 0xa) == 8);
4962 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4965 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4967 exn = (s.unusable != 0);
4970 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4971 * outside the segment limit. All CPUs that support VMX ignore
4972 * limit checks for flat segments, i.e. segments with base==0,
4973 * limit==0xffffffff and of type expand-up data or code.
4975 if (!(s.base == 0 && s.limit == 0xffffffff &&
4976 ((s.type & 8) || !(s.type & 4))))
4977 exn = exn || ((u64)off + len - 1 > s.limit);
4980 kvm_queue_exception_e(vcpu,
4981 seg_reg == VCPU_SREG_SS ?
4982 SS_VECTOR : GP_VECTOR,
4990 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer,
4994 struct x86_exception e;
4997 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
4998 vmcs_read32(VMX_INSTRUCTION_INFO), false,
4999 sizeof(*vmpointer), &gva)) {
5004 r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e);
5005 if (r != X86EMUL_CONTINUE) {
5006 *ret = kvm_handle_memory_failure(vcpu, r, &e);
5014 * Allocate a shadow VMCS and associate it with the currently loaded
5015 * VMCS, unless such a shadow VMCS already exists. The newly allocated
5016 * VMCS is also VMCLEARed, so that it is ready for use.
5018 static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
5020 struct vcpu_vmx *vmx = to_vmx(vcpu);
5021 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
5024 * KVM allocates a shadow VMCS only when L1 executes VMXON and frees it
5025 * when L1 executes VMXOFF or the vCPU is forced out of nested
5026 * operation. VMXON faults if the CPU is already post-VMXON, so it
5027 * should be impossible to already have an allocated shadow VMCS. KVM
5028 * doesn't support virtualization of VMCS shadowing, so vmcs01 should
5029 * always be the loaded VMCS.
5031 if (WARN_ON(loaded_vmcs != &vmx->vmcs01 || loaded_vmcs->shadow_vmcs))
5032 return loaded_vmcs->shadow_vmcs;
5034 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
5035 if (loaded_vmcs->shadow_vmcs)
5036 vmcs_clear(loaded_vmcs->shadow_vmcs);
5038 return loaded_vmcs->shadow_vmcs;
5041 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
5043 struct vcpu_vmx *vmx = to_vmx(vcpu);
5046 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
5050 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
5051 if (!vmx->nested.cached_vmcs12)
5052 goto out_cached_vmcs12;
5054 vmx->nested.shadow_vmcs12_cache.gpa = INVALID_GPA;
5055 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
5056 if (!vmx->nested.cached_shadow_vmcs12)
5057 goto out_cached_shadow_vmcs12;
5059 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
5060 goto out_shadow_vmcs;
5062 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
5063 HRTIMER_MODE_ABS_PINNED);
5064 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
5066 vmx->nested.vpid02 = allocate_vpid();
5068 vmx->nested.vmcs02_initialized = false;
5069 vmx->nested.vmxon = true;
5071 if (vmx_pt_mode_is_host_guest()) {
5072 vmx->pt_desc.guest.ctl = 0;
5073 pt_update_intercept_for_msr(vcpu);
5079 kfree(vmx->nested.cached_shadow_vmcs12);
5081 out_cached_shadow_vmcs12:
5082 kfree(vmx->nested.cached_vmcs12);
5085 free_loaded_vmcs(&vmx->nested.vmcs02);
5091 /* Emulate the VMXON instruction. */
5092 static int handle_vmxon(struct kvm_vcpu *vcpu)
5097 struct vcpu_vmx *vmx = to_vmx(vcpu);
5098 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
5099 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
5102 * Note, KVM cannot rely on hardware to perform the CR0/CR4 #UD checks
5103 * that have higher priority than VM-Exit (see Intel SDM's pseudocode
5104 * for VMXON), as KVM must load valid CR0/CR4 values into hardware while
5105 * running the guest, i.e. KVM needs to check the _guest_ values.
5107 * Rely on hardware for the other two pre-VM-Exit checks, !VM86 and
5108 * !COMPATIBILITY modes. KVM may run the guest in VM86 to emulate Real
5109 * Mode, but KVM will never take the guest out of those modes.
5111 if (!nested_host_cr0_valid(vcpu, kvm_read_cr0(vcpu)) ||
5112 !nested_host_cr4_valid(vcpu, kvm_read_cr4(vcpu))) {
5113 kvm_queue_exception(vcpu, UD_VECTOR);
5118 * CPL=0 and all other checks that are lower priority than VM-Exit must
5119 * be checked manually.
5121 if (vmx_get_cpl(vcpu)) {
5122 kvm_inject_gp(vcpu, 0);
5126 if (vmx->nested.vmxon)
5127 return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
5129 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
5130 != VMXON_NEEDED_FEATURES) {
5131 kvm_inject_gp(vcpu, 0);
5135 if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret))
5140 * The first 4 bytes of VMXON region contain the supported
5141 * VMCS revision identifier
5143 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
5144 * which replaces physical address width with 32
5146 if (!page_address_valid(vcpu, vmptr))
5147 return nested_vmx_failInvalid(vcpu);
5149 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
5150 revision != VMCS12_REVISION)
5151 return nested_vmx_failInvalid(vcpu);
5153 vmx->nested.vmxon_ptr = vmptr;
5154 ret = enter_vmx_operation(vcpu);
5158 return nested_vmx_succeed(vcpu);
5161 static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
5163 struct vcpu_vmx *vmx = to_vmx(vcpu);
5165 if (vmx->nested.current_vmptr == INVALID_GPA)
5168 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
5170 if (enable_shadow_vmcs) {
5171 /* copy to memory all shadowed fields in case
5172 they were modified */
5173 copy_shadow_to_vmcs12(vmx);
5174 vmx_disable_shadow_vmcs(vmx);
5176 vmx->nested.posted_intr_nv = -1;
5178 /* Flush VMCS12 to guest memory */
5179 kvm_vcpu_write_guest_page(vcpu,
5180 vmx->nested.current_vmptr >> PAGE_SHIFT,
5181 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
5183 kvm_mmu_free_roots(vcpu->kvm, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
5185 vmx->nested.current_vmptr = INVALID_GPA;
5188 /* Emulate the VMXOFF instruction */
5189 static int handle_vmxoff(struct kvm_vcpu *vcpu)
5191 if (!nested_vmx_check_permission(vcpu))
5196 if (kvm_apic_has_pending_init_or_sipi(vcpu))
5197 kvm_make_request(KVM_REQ_EVENT, vcpu);
5199 return nested_vmx_succeed(vcpu);
5202 /* Emulate the VMCLEAR instruction */
5203 static int handle_vmclear(struct kvm_vcpu *vcpu)
5205 struct vcpu_vmx *vmx = to_vmx(vcpu);
5211 if (!nested_vmx_check_permission(vcpu))
5214 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5217 if (!page_address_valid(vcpu, vmptr))
5218 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
5220 if (vmptr == vmx->nested.vmxon_ptr)
5221 return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
5224 * When Enlightened VMEntry is enabled on the calling CPU we treat
5225 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
5226 * way to distinguish it from VMCS12) and we must not corrupt it by
5227 * writing to the non-existent 'launch_state' field. The area doesn't
5228 * have to be the currently active EVMCS on the calling CPU and there's
5229 * nothing KVM has to do to transition it from 'active' to 'non-active'
5230 * state. It is possible that the area will stay mapped as
5231 * vmx->nested.hv_evmcs but this shouldn't be a problem.
5233 if (likely(!guest_cpuid_has_evmcs(vcpu) ||
5234 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
5235 if (vmptr == vmx->nested.current_vmptr)
5236 nested_release_vmcs12(vcpu);
5238 kvm_vcpu_write_guest(vcpu,
5239 vmptr + offsetof(struct vmcs12,
5241 &zero, sizeof(zero));
5242 } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) {
5243 nested_release_evmcs(vcpu);
5246 return nested_vmx_succeed(vcpu);
5249 /* Emulate the VMLAUNCH instruction */
5250 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5252 return nested_vmx_run(vcpu, true);
5255 /* Emulate the VMRESUME instruction */
5256 static int handle_vmresume(struct kvm_vcpu *vcpu)
5259 return nested_vmx_run(vcpu, false);
5262 static int handle_vmread(struct kvm_vcpu *vcpu)
5264 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5266 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
5267 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5268 struct vcpu_vmx *vmx = to_vmx(vcpu);
5269 struct x86_exception e;
5270 unsigned long field;
5276 if (!nested_vmx_check_permission(vcpu))
5279 /* Decode instruction info and find the field to read */
5280 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
5282 if (!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) {
5284 * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA,
5285 * any VMREAD sets the ALU flags for VMfailInvalid.
5287 if (vmx->nested.current_vmptr == INVALID_GPA ||
5288 (is_guest_mode(vcpu) &&
5289 get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA))
5290 return nested_vmx_failInvalid(vcpu);
5292 offset = get_vmcs12_field_offset(field);
5294 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5296 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
5297 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5299 /* Read the field, zero-extended to a u64 value */
5300 value = vmcs12_read_any(vmcs12, field, offset);
5303 * Hyper-V TLFS (as of 6.0b) explicitly states, that while an
5304 * enlightened VMCS is active VMREAD/VMWRITE instructions are
5305 * unsupported. Unfortunately, certain versions of Windows 11
5306 * don't comply with this requirement which is not enforced in
5307 * genuine Hyper-V. Allow VMREAD from an enlightened VMCS as a
5308 * workaround, as misbehaving guests will panic on VM-Fail.
5309 * Note, enlightened VMCS is incompatible with shadow VMCS so
5310 * all VMREADs from L2 should go to L1.
5312 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
5313 return nested_vmx_failInvalid(vcpu);
5315 offset = evmcs_field_offset(field, NULL);
5317 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5319 /* Read the field, zero-extended to a u64 value */
5320 value = evmcs_read_any(vmx->nested.hv_evmcs, field, offset);
5324 * Now copy part of this value to register or memory, as requested.
5325 * Note that the number of bits actually copied is 32 or 64 depending
5326 * on the guest's mode (32 or 64 bit), not on the given field's length.
5328 if (instr_info & BIT(10)) {
5329 kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value);
5331 len = is_64_bit_mode(vcpu) ? 8 : 4;
5332 if (get_vmx_mem_address(vcpu, exit_qualification,
5333 instr_info, true, len, &gva))
5335 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
5336 r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e);
5337 if (r != X86EMUL_CONTINUE)
5338 return kvm_handle_memory_failure(vcpu, r, &e);
5341 return nested_vmx_succeed(vcpu);
5344 static bool is_shadow_field_rw(unsigned long field)
5347 #define SHADOW_FIELD_RW(x, y) case x:
5348 #include "vmcs_shadow_fields.h"
5356 static bool is_shadow_field_ro(unsigned long field)
5359 #define SHADOW_FIELD_RO(x, y) case x:
5360 #include "vmcs_shadow_fields.h"
5368 static int handle_vmwrite(struct kvm_vcpu *vcpu)
5370 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
5372 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
5373 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5374 struct vcpu_vmx *vmx = to_vmx(vcpu);
5375 struct x86_exception e;
5376 unsigned long field;
5382 * The value to write might be 32 or 64 bits, depending on L1's long
5383 * mode, and eventually we need to write that into a field of several
5384 * possible lengths. The code below first zero-extends the value to 64
5385 * bit (value), and then copies only the appropriate number of
5386 * bits into the vmcs12 field.
5390 if (!nested_vmx_check_permission(vcpu))
5394 * In VMX non-root operation, when the VMCS-link pointer is INVALID_GPA,
5395 * any VMWRITE sets the ALU flags for VMfailInvalid.
5397 if (vmx->nested.current_vmptr == INVALID_GPA ||
5398 (is_guest_mode(vcpu) &&
5399 get_vmcs12(vcpu)->vmcs_link_pointer == INVALID_GPA))
5400 return nested_vmx_failInvalid(vcpu);
5402 if (instr_info & BIT(10))
5403 value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf));
5405 len = is_64_bit_mode(vcpu) ? 8 : 4;
5406 if (get_vmx_mem_address(vcpu, exit_qualification,
5407 instr_info, false, len, &gva))
5409 r = kvm_read_guest_virt(vcpu, gva, &value, len, &e);
5410 if (r != X86EMUL_CONTINUE)
5411 return kvm_handle_memory_failure(vcpu, r, &e);
5414 field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf));
5416 offset = get_vmcs12_field_offset(field);
5418 return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5421 * If the vCPU supports "VMWRITE to any supported field in the
5422 * VMCS," then the "read-only" fields are actually read/write.
5424 if (vmcs_field_readonly(field) &&
5425 !nested_cpu_has_vmwrite_any_field(vcpu))
5426 return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
5429 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5430 * vmcs12, else we may crush a field or consume a stale value.
5432 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5433 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5436 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5437 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5438 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5439 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5440 * from L1 will return a different value than VMREAD from L2 (L1 sees
5441 * the stripped down value, L2 sees the full value as stored by KVM).
5443 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
5446 vmcs12_write_any(vmcs12, field, offset, value);
5449 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5450 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5451 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5452 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
5454 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5456 * L1 can read these fields without exiting, ensure the
5457 * shadow VMCS is up-to-date.
5459 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5461 vmcs_load(vmx->vmcs01.shadow_vmcs);
5463 __vmcs_writel(field, value);
5465 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5466 vmcs_load(vmx->loaded_vmcs->vmcs);
5469 vmx->nested.dirty_vmcs12 = true;
5472 return nested_vmx_succeed(vcpu);
5475 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5477 vmx->nested.current_vmptr = vmptr;
5478 if (enable_shadow_vmcs) {
5479 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
5480 vmcs_write64(VMCS_LINK_POINTER,
5481 __pa(vmx->vmcs01.shadow_vmcs));
5482 vmx->nested.need_vmcs12_to_shadow_sync = true;
5484 vmx->nested.dirty_vmcs12 = true;
5485 vmx->nested.force_msr_bitmap_recalc = true;
5488 /* Emulate the VMPTRLD instruction */
5489 static int handle_vmptrld(struct kvm_vcpu *vcpu)
5491 struct vcpu_vmx *vmx = to_vmx(vcpu);
5495 if (!nested_vmx_check_permission(vcpu))
5498 if (nested_vmx_get_vmptr(vcpu, &vmptr, &r))
5501 if (!page_address_valid(vcpu, vmptr))
5502 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
5504 if (vmptr == vmx->nested.vmxon_ptr)
5505 return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
5507 /* Forbid normal VMPTRLD if Enlightened version was used */
5508 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
5511 if (vmx->nested.current_vmptr != vmptr) {
5512 struct gfn_to_hva_cache *ghc = &vmx->nested.vmcs12_cache;
5513 struct vmcs_hdr hdr;
5515 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, vmptr, VMCS12_SIZE)) {
5517 * Reads from an unbacked page return all 1s,
5518 * which means that the 32 bits located at the
5519 * given physical address won't match the required
5520 * VMCS12_REVISION identifier.
5522 return nested_vmx_fail(vcpu,
5523 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5526 if (kvm_read_guest_offset_cached(vcpu->kvm, ghc, &hdr,
5527 offsetof(struct vmcs12, hdr),
5529 return nested_vmx_fail(vcpu,
5530 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5533 if (hdr.revision_id != VMCS12_REVISION ||
5535 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
5536 return nested_vmx_fail(vcpu,
5537 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5540 nested_release_vmcs12(vcpu);
5543 * Load VMCS12 from guest memory since it is not already
5546 if (kvm_read_guest_cached(vcpu->kvm, ghc, vmx->nested.cached_vmcs12,
5548 return nested_vmx_fail(vcpu,
5549 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5552 set_current_vmptr(vmx, vmptr);
5555 return nested_vmx_succeed(vcpu);
5558 /* Emulate the VMPTRST instruction */
5559 static int handle_vmptrst(struct kvm_vcpu *vcpu)
5561 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
5562 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5563 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5564 struct x86_exception e;
5568 if (!nested_vmx_check_permission(vcpu))
5571 if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr)))
5574 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5575 true, sizeof(gpa_t), &gva))
5577 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
5578 r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr,
5580 if (r != X86EMUL_CONTINUE)
5581 return kvm_handle_memory_failure(vcpu, r, &e);
5583 return nested_vmx_succeed(vcpu);
5586 /* Emulate the INVEPT instruction */
5587 static int handle_invept(struct kvm_vcpu *vcpu)
5589 struct vcpu_vmx *vmx = to_vmx(vcpu);
5590 u32 vmx_instruction_info, types;
5591 unsigned long type, roots_to_free;
5592 struct kvm_mmu *mmu;
5594 struct x86_exception e;
5598 int i, r, gpr_index;
5600 if (!(vmx->nested.msrs.secondary_ctls_high &
5601 SECONDARY_EXEC_ENABLE_EPT) ||
5602 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5603 kvm_queue_exception(vcpu, UD_VECTOR);
5607 if (!nested_vmx_check_permission(vcpu))
5610 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5611 gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info);
5612 type = kvm_register_read(vcpu, gpr_index);
5614 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5616 if (type >= 32 || !(types & (1 << type)))
5617 return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5619 /* According to the Intel VMX instruction reference, the memory
5620 * operand is read even if it isn't needed (e.g., for type==global)
5622 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
5623 vmx_instruction_info, false, sizeof(operand), &gva))
5625 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5626 if (r != X86EMUL_CONTINUE)
5627 return kvm_handle_memory_failure(vcpu, r, &e);
5630 * Nested EPT roots are always held through guest_mmu,
5633 mmu = &vcpu->arch.guest_mmu;
5636 case VMX_EPT_EXTENT_CONTEXT:
5637 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
5638 return nested_vmx_fail(vcpu,
5639 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5642 if (nested_ept_root_matches(mmu->root.hpa, mmu->root.pgd,
5644 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5646 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5647 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
5648 mmu->prev_roots[i].pgd,
5650 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5653 case VMX_EPT_EXTENT_GLOBAL:
5654 roots_to_free = KVM_MMU_ROOTS_ALL;
5662 kvm_mmu_free_roots(vcpu->kvm, mmu, roots_to_free);
5664 return nested_vmx_succeed(vcpu);
5667 static int handle_invvpid(struct kvm_vcpu *vcpu)
5669 struct vcpu_vmx *vmx = to_vmx(vcpu);
5670 u32 vmx_instruction_info;
5671 unsigned long type, types;
5673 struct x86_exception e;
5681 if (!(vmx->nested.msrs.secondary_ctls_high &
5682 SECONDARY_EXEC_ENABLE_VPID) ||
5683 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5684 kvm_queue_exception(vcpu, UD_VECTOR);
5688 if (!nested_vmx_check_permission(vcpu))
5691 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5692 gpr_index = vmx_get_instr_info_reg2(vmx_instruction_info);
5693 type = kvm_register_read(vcpu, gpr_index);
5695 types = (vmx->nested.msrs.vpid_caps &
5696 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5698 if (type >= 32 || !(types & (1 << type)))
5699 return nested_vmx_fail(vcpu,
5700 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5702 /* according to the intel vmx instruction reference, the memory
5703 * operand is read even if it isn't needed (e.g., for type==global)
5705 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
5706 vmx_instruction_info, false, sizeof(operand), &gva))
5708 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5709 if (r != X86EMUL_CONTINUE)
5710 return kvm_handle_memory_failure(vcpu, r, &e);
5712 if (operand.vpid >> 16)
5713 return nested_vmx_fail(vcpu,
5714 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5716 vpid02 = nested_get_vpid02(vcpu);
5718 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5719 if (!operand.vpid ||
5720 is_noncanonical_address(operand.gla, vcpu))
5721 return nested_vmx_fail(vcpu,
5722 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5723 vpid_sync_vcpu_addr(vpid02, operand.gla);
5725 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5726 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5728 return nested_vmx_fail(vcpu,
5729 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5730 vpid_sync_context(vpid02);
5732 case VMX_VPID_EXTENT_ALL_CONTEXT:
5733 vpid_sync_context(vpid02);
5737 return kvm_skip_emulated_instruction(vcpu);
5741 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
5742 * linear mappings for L2 (tagged with L2's VPID). Free all guest
5743 * roots as VPIDs are not tracked in the MMU role.
5745 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5746 * an MMU when EPT is disabled.
5748 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5751 kvm_mmu_free_guest_mode_roots(vcpu->kvm, &vcpu->arch.root_mmu);
5753 return nested_vmx_succeed(vcpu);
5756 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5757 struct vmcs12 *vmcs12)
5759 u32 index = kvm_rcx_read(vcpu);
5762 if (WARN_ON_ONCE(!nested_cpu_has_ept(vmcs12)))
5764 if (index >= VMFUNC_EPTP_ENTRIES)
5767 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
5768 &new_eptp, index * 8, 8))
5772 * If the (L2) guest does a vmfunc to the currently
5773 * active ept pointer, we don't have to do anything else
5775 if (vmcs12->ept_pointer != new_eptp) {
5776 if (!nested_vmx_check_eptp(vcpu, new_eptp))
5779 vmcs12->ept_pointer = new_eptp;
5780 nested_ept_new_eptp(vcpu);
5782 if (!nested_cpu_has_vpid(vmcs12))
5783 kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
5789 static int handle_vmfunc(struct kvm_vcpu *vcpu)
5791 struct vcpu_vmx *vmx = to_vmx(vcpu);
5792 struct vmcs12 *vmcs12;
5793 u32 function = kvm_rax_read(vcpu);
5796 * VMFUNC is only supported for nested guests, but we always enable the
5797 * secondary control for simplicity; for non-nested mode, fake that we
5798 * didn't by injecting #UD.
5800 if (!is_guest_mode(vcpu)) {
5801 kvm_queue_exception(vcpu, UD_VECTOR);
5805 vmcs12 = get_vmcs12(vcpu);
5808 * #UD on out-of-bounds function has priority over VM-Exit, and VMFUNC
5809 * is enabled in vmcs02 if and only if it's enabled in vmcs12.
5811 if (WARN_ON_ONCE((function > 63) || !nested_cpu_has_vmfunc(vmcs12))) {
5812 kvm_queue_exception(vcpu, UD_VECTOR);
5816 if (!(vmcs12->vm_function_control & BIT_ULL(function)))
5821 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5827 return kvm_skip_emulated_instruction(vcpu);
5831 * This is effectively a reflected VM-Exit, as opposed to a synthesized
5832 * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode
5833 * EXIT_REASON_VMFUNC as the exit reason.
5835 nested_vmx_vmexit(vcpu, vmx->exit_reason.full,
5836 vmx_get_intr_info(vcpu),
5837 vmx_get_exit_qual(vcpu));
5842 * Return true if an IO instruction with the specified port and size should cause
5843 * a VM-exit into L1.
5845 bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5848 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5849 gpa_t bitmap, last_bitmap;
5852 last_bitmap = INVALID_GPA;
5857 bitmap = vmcs12->io_bitmap_a;
5858 else if (port < 0x10000)
5859 bitmap = vmcs12->io_bitmap_b;
5862 bitmap += (port & 0x7fff) / 8;
5864 if (last_bitmap != bitmap)
5865 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5867 if (b & (1 << (port & 7)))
5872 last_bitmap = bitmap;
5878 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5879 struct vmcs12 *vmcs12)
5881 unsigned long exit_qualification;
5882 unsigned short port;
5885 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5886 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5888 exit_qualification = vmx_get_exit_qual(vcpu);
5890 port = exit_qualification >> 16;
5891 size = (exit_qualification & 7) + 1;
5893 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5897 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
5898 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5899 * disinterest in the current event (read or write a specific MSR) by using an
5900 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5902 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
5903 struct vmcs12 *vmcs12,
5904 union vmx_exit_reason exit_reason)
5906 u32 msr_index = kvm_rcx_read(vcpu);
5909 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5913 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5914 * for the four combinations of read/write and low/high MSR numbers.
5915 * First we need to figure out which of the four to use:
5917 bitmap = vmcs12->msr_bitmap;
5918 if (exit_reason.basic == EXIT_REASON_MSR_WRITE)
5920 if (msr_index >= 0xc0000000) {
5921 msr_index -= 0xc0000000;
5925 /* Then read the msr_index'th bit from this bitmap: */
5926 if (msr_index < 1024*8) {
5928 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5930 return 1 & (b >> (msr_index & 7));
5932 return true; /* let L1 handle the wrong parameter */
5936 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5937 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5938 * intercept (via guest_host_mask etc.) the current event.
5940 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5941 struct vmcs12 *vmcs12)
5943 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
5944 int cr = exit_qualification & 15;
5948 switch ((exit_qualification >> 4) & 3) {
5949 case 0: /* mov to cr */
5950 reg = (exit_qualification >> 8) & 15;
5951 val = kvm_register_read(vcpu, reg);
5954 if (vmcs12->cr0_guest_host_mask &
5955 (val ^ vmcs12->cr0_read_shadow))
5959 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5963 if (vmcs12->cr4_guest_host_mask &
5964 (vmcs12->cr4_read_shadow ^ val))
5968 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5974 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5975 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5978 case 1: /* mov from cr */
5981 if (vmcs12->cpu_based_vm_exec_control &
5982 CPU_BASED_CR3_STORE_EXITING)
5986 if (vmcs12->cpu_based_vm_exec_control &
5987 CPU_BASED_CR8_STORE_EXITING)
5994 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5995 * cr0. Other attempted changes are ignored, with no exit.
5997 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5998 if (vmcs12->cr0_guest_host_mask & 0xe &
5999 (val ^ vmcs12->cr0_read_shadow))
6001 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
6002 !(vmcs12->cr0_read_shadow & 0x1) &&
6010 static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu,
6011 struct vmcs12 *vmcs12)
6015 if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
6016 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING))
6019 encls_leaf = kvm_rax_read(vcpu);
6020 if (encls_leaf > 62)
6022 return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf);
6025 static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
6026 struct vmcs12 *vmcs12, gpa_t bitmap)
6028 u32 vmx_instruction_info;
6029 unsigned long field;
6032 if (!nested_cpu_has_shadow_vmcs(vmcs12))
6035 /* Decode instruction info and find the field to access */
6036 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6037 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
6039 /* Out-of-range fields always cause a VM exit from L2 to L1 */
6043 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
6046 return 1 & (b >> (field & 7));
6049 static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
6051 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
6053 if (nested_cpu_has_mtf(vmcs12))
6057 * An MTF VM-exit may be injected into the guest by setting the
6058 * interruption-type to 7 (other event) and the vector field to 0. Such
6059 * is the case regardless of the 'monitor trap flag' VM-execution
6062 return entry_intr_info == (INTR_INFO_VALID_MASK
6063 | INTR_TYPE_OTHER_EVENT);
6067 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
6068 * L1 wants the exit. Only call this when in is_guest_mode (L2).
6070 static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
6071 union vmx_exit_reason exit_reason)
6075 switch ((u16)exit_reason.basic) {
6076 case EXIT_REASON_EXCEPTION_NMI:
6077 intr_info = vmx_get_intr_info(vcpu);
6078 if (is_nmi(intr_info))
6080 else if (is_page_fault(intr_info))
6081 return vcpu->arch.apf.host_apf_flags ||
6082 vmx_need_pf_intercept(vcpu);
6083 else if (is_debug(intr_info) &&
6085 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6087 else if (is_breakpoint(intr_info) &&
6088 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
6090 else if (is_alignment_check(intr_info) &&
6091 !vmx_guest_inject_ac(vcpu))
6094 case EXIT_REASON_EXTERNAL_INTERRUPT:
6096 case EXIT_REASON_MCE_DURING_VMENTRY:
6098 case EXIT_REASON_EPT_VIOLATION:
6100 * L0 always deals with the EPT violation. If nested EPT is
6101 * used, and the nested mmu code discovers that the address is
6102 * missing in the guest EPT table (EPT12), the EPT violation
6103 * will be injected with nested_ept_inject_page_fault()
6106 case EXIT_REASON_EPT_MISCONFIG:
6108 * L2 never uses directly L1's EPT, but rather L0's own EPT
6109 * table (shadow on EPT) or a merged EPT table that L0 built
6110 * (EPT on EPT). So any problems with the structure of the
6111 * table is L0's fault.
6114 case EXIT_REASON_PREEMPTION_TIMER:
6116 case EXIT_REASON_PML_FULL:
6118 * PML is emulated for an L1 VMM and should never be enabled in
6119 * vmcs02, always "handle" PML_FULL by exiting to userspace.
6122 case EXIT_REASON_VMFUNC:
6123 /* VM functions are emulated through L2->L0 vmexits. */
6125 case EXIT_REASON_BUS_LOCK:
6127 * At present, bus lock VM exit is never exposed to L1.
6128 * Handle L2's bus locks in L0 directly.
6138 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
6139 * is_guest_mode (L2).
6141 static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu,
6142 union vmx_exit_reason exit_reason)
6144 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6147 switch ((u16)exit_reason.basic) {
6148 case EXIT_REASON_EXCEPTION_NMI:
6149 intr_info = vmx_get_intr_info(vcpu);
6150 if (is_nmi(intr_info))
6152 else if (is_page_fault(intr_info))
6154 return vmcs12->exception_bitmap &
6155 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
6156 case EXIT_REASON_EXTERNAL_INTERRUPT:
6157 return nested_exit_on_intr(vcpu);
6158 case EXIT_REASON_TRIPLE_FAULT:
6160 case EXIT_REASON_INTERRUPT_WINDOW:
6161 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
6162 case EXIT_REASON_NMI_WINDOW:
6163 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
6164 case EXIT_REASON_TASK_SWITCH:
6166 case EXIT_REASON_CPUID:
6168 case EXIT_REASON_HLT:
6169 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
6170 case EXIT_REASON_INVD:
6172 case EXIT_REASON_INVLPG:
6173 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6174 case EXIT_REASON_RDPMC:
6175 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
6176 case EXIT_REASON_RDRAND:
6177 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
6178 case EXIT_REASON_RDSEED:
6179 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
6180 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
6181 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
6182 case EXIT_REASON_VMREAD:
6183 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
6184 vmcs12->vmread_bitmap);
6185 case EXIT_REASON_VMWRITE:
6186 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
6187 vmcs12->vmwrite_bitmap);
6188 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
6189 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
6190 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
6191 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
6192 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
6194 * VMX instructions trap unconditionally. This allows L1 to
6195 * emulate them for its L2 guest, i.e., allows 3-level nesting!
6198 case EXIT_REASON_CR_ACCESS:
6199 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
6200 case EXIT_REASON_DR_ACCESS:
6201 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
6202 case EXIT_REASON_IO_INSTRUCTION:
6203 return nested_vmx_exit_handled_io(vcpu, vmcs12);
6204 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
6205 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
6206 case EXIT_REASON_MSR_READ:
6207 case EXIT_REASON_MSR_WRITE:
6208 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
6209 case EXIT_REASON_INVALID_STATE:
6211 case EXIT_REASON_MWAIT_INSTRUCTION:
6212 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
6213 case EXIT_REASON_MONITOR_TRAP_FLAG:
6214 return nested_vmx_exit_handled_mtf(vmcs12);
6215 case EXIT_REASON_MONITOR_INSTRUCTION:
6216 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
6217 case EXIT_REASON_PAUSE_INSTRUCTION:
6218 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
6219 nested_cpu_has2(vmcs12,
6220 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
6221 case EXIT_REASON_MCE_DURING_VMENTRY:
6223 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6224 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
6225 case EXIT_REASON_APIC_ACCESS:
6226 case EXIT_REASON_APIC_WRITE:
6227 case EXIT_REASON_EOI_INDUCED:
6229 * The controls for "virtualize APIC accesses," "APIC-
6230 * register virtualization," and "virtual-interrupt
6231 * delivery" only come from vmcs12.
6234 case EXIT_REASON_INVPCID:
6236 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
6237 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6238 case EXIT_REASON_WBINVD:
6239 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6240 case EXIT_REASON_XSETBV:
6242 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
6244 * This should never happen, since it is not possible to
6245 * set XSS to a non-zero value---neither in L1 nor in L2.
6246 * If if it were, XSS would have to be checked against
6247 * the XSS exit bitmap in vmcs12.
6249 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
6250 case EXIT_REASON_UMWAIT:
6251 case EXIT_REASON_TPAUSE:
6252 return nested_cpu_has2(vmcs12,
6253 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
6254 case EXIT_REASON_ENCLS:
6255 return nested_vmx_exit_handled_encls(vcpu, vmcs12);
6256 case EXIT_REASON_NOTIFY:
6257 /* Notify VM exit is not exposed to L1 */
6265 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
6266 * reflected into L1.
6268 bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
6270 struct vcpu_vmx *vmx = to_vmx(vcpu);
6271 union vmx_exit_reason exit_reason = vmx->exit_reason;
6272 unsigned long exit_qual;
6275 WARN_ON_ONCE(vmx->nested.nested_run_pending);
6278 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
6279 * has already loaded L2's state.
6281 if (unlikely(vmx->fail)) {
6282 trace_kvm_nested_vmenter_failed(
6283 "hardware VM-instruction error: ",
6284 vmcs_read32(VM_INSTRUCTION_ERROR));
6287 goto reflect_vmexit;
6290 trace_kvm_nested_vmexit(vcpu, KVM_ISA_VMX);
6292 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
6293 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
6296 /* If L1 doesn't want the exit, handle it in L0. */
6297 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
6301 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
6302 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
6303 * need to be synthesized by querying the in-kernel LAPIC, but external
6304 * interrupts are never reflected to L1 so it's a non-issue.
6306 exit_intr_info = vmx_get_intr_info(vcpu);
6307 if (is_exception_with_error_code(exit_intr_info)) {
6308 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6310 vmcs12->vm_exit_intr_error_code =
6311 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6313 exit_qual = vmx_get_exit_qual(vcpu);
6316 nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual);
6320 static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
6321 struct kvm_nested_state __user *user_kvm_nested_state,
6324 struct vcpu_vmx *vmx;
6325 struct vmcs12 *vmcs12;
6326 struct kvm_nested_state kvm_state = {
6328 .format = KVM_STATE_NESTED_FORMAT_VMX,
6329 .size = sizeof(kvm_state),
6331 .hdr.vmx.vmxon_pa = INVALID_GPA,
6332 .hdr.vmx.vmcs12_pa = INVALID_GPA,
6333 .hdr.vmx.preemption_timer_deadline = 0,
6335 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6336 &user_kvm_nested_state->data.vmx[0];
6339 return kvm_state.size + sizeof(*user_vmx_nested_state);
6342 vmcs12 = get_vmcs12(vcpu);
6344 if (nested_vmx_allowed(vcpu) &&
6345 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
6346 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
6347 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
6349 if (vmx_has_valid_vmcs12(vcpu)) {
6350 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
6352 /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */
6353 if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID)
6354 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
6356 if (is_guest_mode(vcpu) &&
6357 nested_cpu_has_shadow_vmcs(vmcs12) &&
6358 vmcs12->vmcs_link_pointer != INVALID_GPA)
6359 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
6362 if (vmx->nested.smm.vmxon)
6363 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
6365 if (vmx->nested.smm.guest_mode)
6366 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
6368 if (is_guest_mode(vcpu)) {
6369 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
6371 if (vmx->nested.nested_run_pending)
6372 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
6374 if (vmx->nested.mtf_pending)
6375 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
6377 if (nested_cpu_has_preemption_timer(vmcs12) &&
6378 vmx->nested.has_preemption_timer_deadline) {
6379 kvm_state.hdr.vmx.flags |=
6380 KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE;
6381 kvm_state.hdr.vmx.preemption_timer_deadline =
6382 vmx->nested.preemption_timer_deadline;
6387 if (user_data_size < kvm_state.size)
6390 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
6393 if (!vmx_has_valid_vmcs12(vcpu))
6397 * When running L2, the authoritative vmcs12 state is in the
6398 * vmcs02. When running L1, the authoritative vmcs12 state is
6399 * in the shadow or enlightened vmcs linked to vmcs01, unless
6400 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
6401 * vmcs12 state is in the vmcs12 already.
6403 if (is_guest_mode(vcpu)) {
6404 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
6405 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
6407 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
6408 if (!vmx->nested.need_vmcs12_to_shadow_sync) {
6409 if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))
6411 * L1 hypervisor is not obliged to keep eVMCS
6412 * clean fields data always up-to-date while
6413 * not in guest mode, 'hv_clean_fields' is only
6414 * supposed to be actual upon vmentry so we need
6415 * to ignore it here and do full copy.
6417 copy_enlightened_to_vmcs12(vmx, 0);
6418 else if (enable_shadow_vmcs)
6419 copy_shadow_to_vmcs12(vmx);
6423 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
6424 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
6427 * Copy over the full allocated size of vmcs12 rather than just the size
6430 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
6433 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6434 vmcs12->vmcs_link_pointer != INVALID_GPA) {
6435 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
6436 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
6440 return kvm_state.size;
6444 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
6446 void vmx_leave_nested(struct kvm_vcpu *vcpu)
6448 if (is_guest_mode(vcpu)) {
6449 to_vmx(vcpu)->nested.nested_run_pending = 0;
6450 nested_vmx_vmexit(vcpu, -1, 0, 0);
6455 static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6456 struct kvm_nested_state __user *user_kvm_nested_state,
6457 struct kvm_nested_state *kvm_state)
6459 struct vcpu_vmx *vmx = to_vmx(vcpu);
6460 struct vmcs12 *vmcs12;
6461 enum vm_entry_failure_code ignored;
6462 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6463 &user_kvm_nested_state->data.vmx[0];
6466 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
6469 if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA) {
6470 if (kvm_state->hdr.vmx.smm.flags)
6473 if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA)
6477 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6478 * enable eVMCS capability on vCPU. However, since then
6479 * code was changed such that flag signals vmcs12 should
6480 * be copied into eVMCS in guest memory.
6482 * To preserve backwards compatability, allow user
6483 * to set this flag even when there is no VMXON region.
6485 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6488 if (!nested_vmx_allowed(vcpu))
6491 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6495 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6496 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6499 if (kvm_state->hdr.vmx.smm.flags &
6500 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6503 if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE)
6507 * SMM temporarily disables VMX, so we cannot be in guest mode,
6508 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6513 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6514 : kvm_state->hdr.vmx.smm.flags)
6517 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6518 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
6521 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6522 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
6525 vmx_leave_nested(vcpu);
6527 if (kvm_state->hdr.vmx.vmxon_pa == INVALID_GPA)
6530 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
6531 ret = enter_vmx_operation(vcpu);
6535 /* Empty 'VMXON' state is permitted if no VMCS loaded */
6536 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) {
6537 /* See vmx_has_valid_vmcs12. */
6538 if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) ||
6539 (kvm_state->flags & KVM_STATE_NESTED_EVMCS) ||
6540 (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA))
6546 if (kvm_state->hdr.vmx.vmcs12_pa != INVALID_GPA) {
6547 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6548 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
6551 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
6552 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6554 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6555 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6556 * restored yet. EVMCS will be mapped from
6557 * nested_get_vmcs12_pages().
6559 vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING;
6560 kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
6565 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
6566 vmx->nested.smm.vmxon = true;
6567 vmx->nested.vmxon = false;
6569 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
6570 vmx->nested.smm.guest_mode = true;
6573 vmcs12 = get_vmcs12(vcpu);
6574 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
6577 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6580 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6583 vmx->nested.nested_run_pending =
6584 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6586 vmx->nested.mtf_pending =
6587 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6590 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6591 vmcs12->vmcs_link_pointer != INVALID_GPA) {
6592 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6594 if (kvm_state->size <
6595 sizeof(*kvm_state) +
6596 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
6597 goto error_guest_mode;
6599 if (copy_from_user(shadow_vmcs12,
6600 user_vmx_nested_state->shadow_vmcs12,
6601 sizeof(*shadow_vmcs12))) {
6603 goto error_guest_mode;
6606 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6607 !shadow_vmcs12->hdr.shadow_vmcs)
6608 goto error_guest_mode;
6611 vmx->nested.has_preemption_timer_deadline = false;
6612 if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) {
6613 vmx->nested.has_preemption_timer_deadline = true;
6614 vmx->nested.preemption_timer_deadline =
6615 kvm_state->hdr.vmx.preemption_timer_deadline;
6618 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6619 nested_vmx_check_host_state(vcpu, vmcs12) ||
6620 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
6621 goto error_guest_mode;
6623 vmx->nested.dirty_vmcs12 = true;
6624 vmx->nested.force_msr_bitmap_recalc = true;
6625 ret = nested_vmx_enter_non_root_mode(vcpu, false);
6627 goto error_guest_mode;
6629 if (vmx->nested.mtf_pending)
6630 kvm_make_request(KVM_REQ_EVENT, vcpu);
6635 vmx->nested.nested_run_pending = 0;
6639 void nested_vmx_set_vmcs_shadowing_bitmap(void)
6641 if (enable_shadow_vmcs) {
6642 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
6643 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
6648 * Indexing into the vmcs12 uses the VMCS encoding rotated left by 6. Undo
6649 * that madness to get the encoding for comparison.
6651 #define VMCS12_IDX_TO_ENC(idx) ((u16)(((u16)(idx) >> 6) | ((u16)(idx) << 10)))
6653 static u64 nested_vmx_calc_vmcs_enum_msr(void)
6656 * Note these are the so called "index" of the VMCS field encoding, not
6657 * the index into vmcs12.
6659 unsigned int max_idx, idx;
6663 * For better or worse, KVM allows VMREAD/VMWRITE to all fields in
6664 * vmcs12, regardless of whether or not the associated feature is
6665 * exposed to L1. Simply find the field with the highest index.
6668 for (i = 0; i < nr_vmcs12_fields; i++) {
6669 /* The vmcs12 table is very, very sparsely populated. */
6670 if (!vmcs12_field_offsets[i])
6673 idx = vmcs_field_index(VMCS12_IDX_TO_ENC(i));
6678 return (u64)max_idx << VMCS_FIELD_INDEX_SHIFT;
6682 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6683 * returned for the various VMX controls MSRs when nested VMX is enabled.
6684 * The same values should also be used to verify that vmcs12 control fields are
6685 * valid during nested entry from L1 to L2.
6686 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6687 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6688 * bit in the high half is on if the corresponding bit in the control field
6689 * may be on. See also vmx_control_verify().
6691 void nested_vmx_setup_ctls_msrs(struct vmcs_config *vmcs_conf, u32 ept_caps)
6693 struct nested_vmx_msrs *msrs = &vmcs_conf->nested;
6696 * Note that as a general rule, the high half of the MSRs (bits in
6697 * the control fields which may be 1) should be initialized by the
6698 * intersection of the underlying hardware's MSR (i.e., features which
6699 * can be supported) and the list of features we want to expose -
6700 * because they are known to be properly supported in our code.
6701 * Also, usually, the low half of the MSRs (bits which must be 1) can
6702 * be set to 0, meaning that L1 may turn off any of these bits. The
6703 * reason is that if one of these bits is necessary, it will appear
6704 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6705 * fields of vmcs01 and vmcs02, will turn these bits off - and
6706 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
6707 * These rules have exceptions below.
6710 /* pin-based controls */
6711 msrs->pinbased_ctls_low =
6712 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6714 msrs->pinbased_ctls_high = vmcs_conf->pin_based_exec_ctrl;
6715 msrs->pinbased_ctls_high &=
6716 PIN_BASED_EXT_INTR_MASK |
6717 PIN_BASED_NMI_EXITING |
6718 PIN_BASED_VIRTUAL_NMIS |
6719 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
6720 msrs->pinbased_ctls_high |=
6721 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6722 PIN_BASED_VMX_PREEMPTION_TIMER;
6725 msrs->exit_ctls_low =
6726 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6728 msrs->exit_ctls_high = vmcs_conf->vmexit_ctrl;
6729 msrs->exit_ctls_high &=
6730 #ifdef CONFIG_X86_64
6731 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6733 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT |
6734 VM_EXIT_CLEAR_BNDCFGS;
6735 msrs->exit_ctls_high |=
6736 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6737 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6738 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT |
6739 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
6741 /* We support free control of debug control saving. */
6742 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6744 /* entry controls */
6745 msrs->entry_ctls_low =
6746 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6748 msrs->entry_ctls_high = vmcs_conf->vmentry_ctrl;
6749 msrs->entry_ctls_high &=
6750 #ifdef CONFIG_X86_64
6751 VM_ENTRY_IA32E_MODE |
6753 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
6754 msrs->entry_ctls_high |=
6755 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER |
6756 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL);
6758 /* We support free control of debug control loading. */
6759 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6761 /* cpu-based controls */
6762 msrs->procbased_ctls_low =
6763 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6765 msrs->procbased_ctls_high = vmcs_conf->cpu_based_exec_ctrl;
6766 msrs->procbased_ctls_high &=
6767 CPU_BASED_INTR_WINDOW_EXITING |
6768 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
6769 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6770 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6771 CPU_BASED_CR3_STORE_EXITING |
6772 #ifdef CONFIG_X86_64
6773 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6775 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6776 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6777 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6778 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6779 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6781 * We can allow some features even when not supported by the
6782 * hardware. For example, L1 can specify an MSR bitmap - and we
6783 * can use it to avoid exits to L1 - even when L0 runs L2
6784 * without MSR bitmaps.
6786 msrs->procbased_ctls_high |=
6787 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6788 CPU_BASED_USE_MSR_BITMAPS;
6790 /* We support free control of CR3 access interception. */
6791 msrs->procbased_ctls_low &=
6792 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6795 * secondary cpu-based controls. Do not include those that
6796 * depend on CPUID bits, they are added later by
6797 * vmx_vcpu_after_set_cpuid.
6799 msrs->secondary_ctls_low = 0;
6801 msrs->secondary_ctls_high = vmcs_conf->cpu_based_2nd_exec_ctrl;
6802 msrs->secondary_ctls_high &=
6803 SECONDARY_EXEC_DESC |
6804 SECONDARY_EXEC_ENABLE_RDTSCP |
6805 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
6806 SECONDARY_EXEC_WBINVD_EXITING |
6807 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6808 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
6809 SECONDARY_EXEC_RDRAND_EXITING |
6810 SECONDARY_EXEC_ENABLE_INVPCID |
6811 SECONDARY_EXEC_RDSEED_EXITING |
6812 SECONDARY_EXEC_XSAVES |
6813 SECONDARY_EXEC_TSC_SCALING;
6816 * We can emulate "VMCS shadowing," even if the hardware
6817 * doesn't support it.
6819 msrs->secondary_ctls_high |=
6820 SECONDARY_EXEC_SHADOW_VMCS;
6823 /* nested EPT: emulate EPT also to L1 */
6824 msrs->secondary_ctls_high |=
6825 SECONDARY_EXEC_ENABLE_EPT;
6827 VMX_EPT_PAGE_WALK_4_BIT |
6828 VMX_EPT_PAGE_WALK_5_BIT |
6830 VMX_EPT_INVEPT_BIT |
6831 VMX_EPT_EXECUTE_ONLY_BIT;
6833 msrs->ept_caps &= ept_caps;
6834 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6835 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6836 VMX_EPT_1GB_PAGE_BIT;
6837 if (enable_ept_ad_bits) {
6838 msrs->secondary_ctls_high |=
6839 SECONDARY_EXEC_ENABLE_PML;
6840 msrs->ept_caps |= VMX_EPT_AD_BIT;
6844 if (cpu_has_vmx_vmfunc()) {
6845 msrs->secondary_ctls_high |=
6846 SECONDARY_EXEC_ENABLE_VMFUNC;
6848 * Advertise EPTP switching unconditionally
6849 * since we emulate it
6852 msrs->vmfunc_controls =
6853 VMX_VMFUNC_EPTP_SWITCHING;
6857 * Old versions of KVM use the single-context version without
6858 * checking for support, so declare that it is supported even
6859 * though it is treated as global context. The alternative is
6860 * not failing the single-context invvpid, and it is worse.
6863 msrs->secondary_ctls_high |=
6864 SECONDARY_EXEC_ENABLE_VPID;
6865 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6866 VMX_VPID_EXTENT_SUPPORTED_MASK;
6869 if (enable_unrestricted_guest)
6870 msrs->secondary_ctls_high |=
6871 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6873 if (flexpriority_enabled)
6874 msrs->secondary_ctls_high |=
6875 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6878 msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING;
6880 /* miscellaneous data */
6881 msrs->misc_low = (u32)vmcs_conf->misc & VMX_MISC_SAVE_EFER_LMA;
6883 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6884 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
6885 VMX_MISC_ACTIVITY_HLT |
6886 VMX_MISC_ACTIVITY_WAIT_SIPI;
6887 msrs->misc_high = 0;
6890 * This MSR reports some information about VMX support. We
6891 * should return information about the VMX we emulate for the
6892 * guest, and the VMCS structure we give it - not about the
6893 * VMX support of the underlying hardware.
6897 VMX_BASIC_TRUE_CTLS |
6898 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6899 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6901 if (cpu_has_vmx_basic_inout())
6902 msrs->basic |= VMX_BASIC_INOUT;
6905 * These MSRs specify bits which the guest must keep fixed on
6906 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6907 * We picked the standard core2 setting.
6909 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6910 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6911 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6912 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6914 /* These MSRs specify bits which the guest must keep fixed off. */
6915 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6916 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6918 if (vmx_umip_emulated())
6919 msrs->cr4_fixed1 |= X86_CR4_UMIP;
6921 msrs->vmcs_enum = nested_vmx_calc_vmcs_enum_msr();
6924 void nested_vmx_hardware_unsetup(void)
6928 if (enable_shadow_vmcs) {
6929 for (i = 0; i < VMX_BITMAP_NR; i++)
6930 free_page((unsigned long)vmx_bitmap[i]);
6934 __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
6938 if (!cpu_has_vmx_shadow_vmcs())
6939 enable_shadow_vmcs = 0;
6940 if (enable_shadow_vmcs) {
6941 for (i = 0; i < VMX_BITMAP_NR; i++) {
6943 * The vmx_bitmap is not tied to a VM and so should
6944 * not be charged to a memcg.
6946 vmx_bitmap[i] = (unsigned long *)
6947 __get_free_page(GFP_KERNEL);
6948 if (!vmx_bitmap[i]) {
6949 nested_vmx_hardware_unsetup();
6954 init_vmcs_shadow_fields();
6957 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6958 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6959 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6960 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6961 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6962 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6963 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6964 exit_handlers[EXIT_REASON_VMOFF] = handle_vmxoff;
6965 exit_handlers[EXIT_REASON_VMON] = handle_vmxon;
6966 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6967 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6968 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
6973 struct kvm_x86_nested_ops vmx_nested_ops = {
6974 .leave_nested = vmx_leave_nested,
6975 .is_exception_vmexit = nested_vmx_is_exception_vmexit,
6976 .check_events = vmx_check_nested_events,
6977 .has_events = vmx_has_nested_events,
6978 .triple_fault = nested_vmx_triple_fault,
6979 .get_state = vmx_get_nested_state,
6980 .set_state = vmx_set_nested_state,
6981 .get_nested_state_pages = vmx_get_nested_state_pages,
6982 .write_log_dirty = nested_vmx_write_pml_buffer,
6983 .enable_evmcs = nested_enable_evmcs,
6984 .get_evmcs_version = nested_get_evmcs_version,