1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/frame.h>
4 #include <linux/percpu.h>
6 #include <asm/debugreg.h>
7 #include <asm/mmu_context.h>
17 static bool __read_mostly enable_shadow_vmcs = 1;
18 module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
20 static bool __read_mostly nested_early_check = 0;
21 module_param(nested_early_check, bool, S_IRUGO);
23 #define CC(consistency_check) \
25 bool failed = (consistency_check); \
27 trace_kvm_nested_vmenter_failed(#consistency_check, 0); \
32 * Hyper-V requires all of these, so mark them as supported even though
33 * they are just treated the same as all-context.
35 #define VMX_VPID_EXTENT_SUPPORTED_MASK \
36 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
37 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
38 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
39 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
41 #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
48 static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
50 #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
51 #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
53 struct shadow_vmcs_field {
57 static struct shadow_vmcs_field shadow_read_only_fields[] = {
58 #define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) },
59 #include "vmcs_shadow_fields.h"
61 static int max_shadow_read_only_fields =
62 ARRAY_SIZE(shadow_read_only_fields);
64 static struct shadow_vmcs_field shadow_read_write_fields[] = {
65 #define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) },
66 #include "vmcs_shadow_fields.h"
68 static int max_shadow_read_write_fields =
69 ARRAY_SIZE(shadow_read_write_fields);
71 static void init_vmcs_shadow_fields(void)
75 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
76 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
78 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
79 struct shadow_vmcs_field entry = shadow_read_only_fields[i];
80 u16 field = entry.encoding;
82 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
83 (i + 1 == max_shadow_read_only_fields ||
84 shadow_read_only_fields[i + 1].encoding != field + 1))
85 pr_err("Missing field from shadow_read_only_field %x\n",
88 clear_bit(field, vmx_vmread_bitmap);
93 entry.offset += sizeof(u32);
95 shadow_read_only_fields[j++] = entry;
97 max_shadow_read_only_fields = j;
99 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
100 struct shadow_vmcs_field entry = shadow_read_write_fields[i];
101 u16 field = entry.encoding;
103 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
104 (i + 1 == max_shadow_read_write_fields ||
105 shadow_read_write_fields[i + 1].encoding != field + 1))
106 pr_err("Missing field from shadow_read_write_field %x\n",
109 WARN_ONCE(field >= GUEST_ES_AR_BYTES &&
110 field <= GUEST_TR_AR_BYTES,
111 "Update vmcs12_write_any() to drop reserved bits from AR_BYTES");
114 * PML and the preemption timer can be emulated, but the
115 * processor cannot vmwrite to fields that don't exist
119 case GUEST_PML_INDEX:
120 if (!cpu_has_vmx_pml())
123 case VMX_PREEMPTION_TIMER_VALUE:
124 if (!cpu_has_vmx_preemption_timer())
127 case GUEST_INTR_STATUS:
128 if (!cpu_has_vmx_apicv())
135 clear_bit(field, vmx_vmwrite_bitmap);
136 clear_bit(field, vmx_vmread_bitmap);
141 entry.offset += sizeof(u32);
143 shadow_read_write_fields[j++] = entry;
145 max_shadow_read_write_fields = j;
149 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
150 * set the success or error code of an emulated VMX instruction (as specified
151 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
154 static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
156 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
157 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
158 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
159 return kvm_skip_emulated_instruction(vcpu);
162 static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
164 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
165 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
166 X86_EFLAGS_SF | X86_EFLAGS_OF))
168 return kvm_skip_emulated_instruction(vcpu);
171 static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
172 u32 vm_instruction_error)
174 struct vcpu_vmx *vmx = to_vmx(vcpu);
177 * failValid writes the error number to the current VMCS, which
178 * can't be done if there isn't a current VMCS.
180 if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
181 return nested_vmx_failInvalid(vcpu);
183 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
184 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
185 X86_EFLAGS_SF | X86_EFLAGS_OF))
187 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
189 * We don't need to force a shadow sync because
190 * VM_INSTRUCTION_ERROR is not shadowed
192 return kvm_skip_emulated_instruction(vcpu);
195 static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
197 /* TODO: not to reset guest simply here. */
198 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
199 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
202 static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
204 return fixed_bits_valid(control, low, high);
207 static inline u64 vmx_control_msr(u32 low, u32 high)
209 return low | ((u64)high << 32);
212 static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
214 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
215 vmcs_write64(VMCS_LINK_POINTER, -1ull);
216 vmx->nested.need_vmcs12_to_shadow_sync = false;
219 static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
221 struct vcpu_vmx *vmx = to_vmx(vcpu);
223 if (!vmx->nested.hv_evmcs)
226 kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true);
227 vmx->nested.hv_evmcs_vmptr = 0;
228 vmx->nested.hv_evmcs = NULL;
232 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
233 * just stops using VMX.
235 static void free_nested(struct kvm_vcpu *vcpu)
237 struct vcpu_vmx *vmx = to_vmx(vcpu);
239 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
242 kvm_clear_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
244 vmx->nested.vmxon = false;
245 vmx->nested.smm.vmxon = false;
246 free_vpid(vmx->nested.vpid02);
247 vmx->nested.posted_intr_nv = -1;
248 vmx->nested.current_vmptr = -1ull;
249 if (enable_shadow_vmcs) {
250 vmx_disable_shadow_vmcs(vmx);
251 vmcs_clear(vmx->vmcs01.shadow_vmcs);
252 free_vmcs(vmx->vmcs01.shadow_vmcs);
253 vmx->vmcs01.shadow_vmcs = NULL;
255 kfree(vmx->nested.cached_vmcs12);
256 vmx->nested.cached_vmcs12 = NULL;
257 kfree(vmx->nested.cached_shadow_vmcs12);
258 vmx->nested.cached_shadow_vmcs12 = NULL;
259 /* Unpin physical memory we referred to in the vmcs02 */
260 if (vmx->nested.apic_access_page) {
261 kvm_release_page_clean(vmx->nested.apic_access_page);
262 vmx->nested.apic_access_page = NULL;
264 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
265 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
266 vmx->nested.pi_desc = NULL;
268 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
270 nested_release_evmcs(vcpu);
272 free_loaded_vmcs(&vmx->nested.vmcs02);
275 static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
276 struct loaded_vmcs *prev)
278 struct vmcs_host_state *dest, *src;
280 if (unlikely(!vmx->guest_state_loaded))
283 src = &prev->host_state;
284 dest = &vmx->loaded_vmcs->host_state;
286 vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
287 dest->ldt_sel = src->ldt_sel;
289 dest->ds_sel = src->ds_sel;
290 dest->es_sel = src->es_sel;
294 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
296 struct vcpu_vmx *vmx = to_vmx(vcpu);
297 struct loaded_vmcs *prev;
300 if (vmx->loaded_vmcs == vmcs)
304 prev = vmx->loaded_vmcs;
305 vmx->loaded_vmcs = vmcs;
306 vmx_vcpu_load_vmcs(vcpu, cpu, prev);
307 vmx_sync_vmcs_host_state(vmx, prev);
310 vmx_register_cache_reset(vcpu);
314 * Ensure that the current vmcs of the logical processor is the
315 * vmcs01 of the vcpu before calling free_nested().
317 void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu)
320 vmx_leave_nested(vcpu);
321 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
326 static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
327 struct x86_exception *fault)
329 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
330 struct vcpu_vmx *vmx = to_vmx(vcpu);
332 unsigned long exit_qualification = vcpu->arch.exit_qualification;
334 if (vmx->nested.pml_full) {
335 vm_exit_reason = EXIT_REASON_PML_FULL;
336 vmx->nested.pml_full = false;
337 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
338 } else if (fault->error_code & PFERR_RSVD_MASK)
339 vm_exit_reason = EXIT_REASON_EPT_MISCONFIG;
341 vm_exit_reason = EXIT_REASON_EPT_VIOLATION;
343 nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification);
344 vmcs12->guest_physical_address = fault->address;
347 static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
349 WARN_ON(mmu_is_nested(vcpu));
351 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
352 kvm_init_shadow_ept_mmu(vcpu,
353 to_vmx(vcpu)->nested.msrs.ept_caps &
354 VMX_EPT_EXECUTE_ONLY_BIT,
355 nested_ept_ad_enabled(vcpu),
356 nested_ept_get_eptp(vcpu));
357 vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp;
358 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
359 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
361 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
364 static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
366 vcpu->arch.mmu = &vcpu->arch.root_mmu;
367 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
370 static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
373 bool inequality, bit;
375 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
377 (error_code & vmcs12->page_fault_error_code_mask) !=
378 vmcs12->page_fault_error_code_match;
379 return inequality ^ bit;
384 * KVM wants to inject page-faults which it got to the guest. This function
385 * checks whether in a nested guest, we need to inject them to L1 or L2.
387 static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
389 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
390 unsigned int nr = vcpu->arch.exception.nr;
391 bool has_payload = vcpu->arch.exception.has_payload;
392 unsigned long payload = vcpu->arch.exception.payload;
394 if (nr == PF_VECTOR) {
395 if (vcpu->arch.exception.nested_apf) {
396 *exit_qual = vcpu->arch.apf.nested_apf_token;
399 if (nested_vmx_is_page_fault_vmexit(vmcs12,
400 vcpu->arch.exception.error_code)) {
401 *exit_qual = has_payload ? payload : vcpu->arch.cr2;
404 } else if (vmcs12->exception_bitmap & (1u << nr)) {
405 if (nr == DB_VECTOR) {
407 payload = vcpu->arch.dr6;
408 payload &= ~(DR6_FIXED_1 | DR6_BT);
411 *exit_qual = payload;
421 static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
422 struct x86_exception *fault)
424 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
426 WARN_ON(!is_guest_mode(vcpu));
428 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
429 !to_vmx(vcpu)->nested.nested_run_pending) {
430 vmcs12->vm_exit_intr_error_code = fault->error_code;
431 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
432 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
433 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
436 kvm_inject_page_fault(vcpu, fault);
440 static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
442 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
445 static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
446 struct vmcs12 *vmcs12)
448 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
451 if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) ||
452 CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b)))
458 static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
459 struct vmcs12 *vmcs12)
461 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
464 if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap)))
470 static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
471 struct vmcs12 *vmcs12)
473 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
476 if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)))
483 * Check if MSR is intercepted for L01 MSR bitmap.
485 static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
487 unsigned long *msr_bitmap;
488 int f = sizeof(unsigned long);
490 if (!cpu_has_vmx_msr_bitmap())
493 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
496 return !!test_bit(msr, msr_bitmap + 0x800 / f);
497 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
499 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
506 * If a msr is allowed by L0, we should check whether it is allowed by L1.
507 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
509 static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
510 unsigned long *msr_bitmap_nested,
513 int f = sizeof(unsigned long);
516 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
517 * have the write-low and read-high bitmap offsets the wrong way round.
518 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
521 if (type & MSR_TYPE_R &&
522 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
524 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
526 if (type & MSR_TYPE_W &&
527 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
529 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
531 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
533 if (type & MSR_TYPE_R &&
534 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
536 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
538 if (type & MSR_TYPE_W &&
539 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
541 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
546 static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap)
550 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
551 unsigned word = msr / BITS_PER_LONG;
553 msr_bitmap[word] = ~0;
554 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
559 * Merge L0's and L1's MSR bitmap, return false to indicate that
560 * we do not use the hardware.
562 static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
563 struct vmcs12 *vmcs12)
566 unsigned long *msr_bitmap_l1;
567 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
568 struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map;
570 /* Nothing to do if the MSR bitmap is not in use. */
571 if (!cpu_has_vmx_msr_bitmap() ||
572 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
575 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map))
578 msr_bitmap_l1 = (unsigned long *)map->hva;
581 * To keep the control flow simple, pay eight 8-byte writes (sixteen
582 * 4-byte writes on 32-bit systems) up front to enable intercepts for
583 * the x2APIC MSR range and selectively disable them below.
585 enable_x2apic_msr_intercepts(msr_bitmap_l0);
587 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
588 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
590 * L0 need not intercept reads for MSRs between 0x800
591 * and 0x8ff, it just lets the processor take the value
592 * from the virtual-APIC page; take those 256 bits
593 * directly from the L1 bitmap.
595 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
596 unsigned word = msr / BITS_PER_LONG;
598 msr_bitmap_l0[word] = msr_bitmap_l1[word];
602 nested_vmx_disable_intercept_for_msr(
603 msr_bitmap_l1, msr_bitmap_l0,
604 X2APIC_MSR(APIC_TASKPRI),
605 MSR_TYPE_R | MSR_TYPE_W);
607 if (nested_cpu_has_vid(vmcs12)) {
608 nested_vmx_disable_intercept_for_msr(
609 msr_bitmap_l1, msr_bitmap_l0,
610 X2APIC_MSR(APIC_EOI),
612 nested_vmx_disable_intercept_for_msr(
613 msr_bitmap_l1, msr_bitmap_l0,
614 X2APIC_MSR(APIC_SELF_IPI),
619 /* KVM unconditionally exposes the FS/GS base MSRs to L1. */
620 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
621 MSR_FS_BASE, MSR_TYPE_RW);
623 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
624 MSR_GS_BASE, MSR_TYPE_RW);
626 nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0,
627 MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
630 * Checking the L0->L1 bitmap is trying to verify two things:
632 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
633 * ensures that we do not accidentally generate an L02 MSR bitmap
634 * from the L12 MSR bitmap that is too permissive.
635 * 2. That L1 or L2s have actually used the MSR. This avoids
636 * unnecessarily merging of the bitmap if the MSR is unused. This
637 * works properly because we only update the L01 MSR bitmap lazily.
638 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
639 * updated to reflect this when L1 (or its L2s) actually write to
642 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL))
643 nested_vmx_disable_intercept_for_msr(
644 msr_bitmap_l1, msr_bitmap_l0,
646 MSR_TYPE_R | MSR_TYPE_W);
648 if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD))
649 nested_vmx_disable_intercept_for_msr(
650 msr_bitmap_l1, msr_bitmap_l0,
654 kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false);
659 static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
660 struct vmcs12 *vmcs12)
662 struct kvm_host_map map;
663 struct vmcs12 *shadow;
665 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
666 vmcs12->vmcs_link_pointer == -1ull)
669 shadow = get_shadow_vmcs12(vcpu);
671 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map))
674 memcpy(shadow, map.hva, VMCS12_SIZE);
675 kvm_vcpu_unmap(vcpu, &map, false);
678 static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
679 struct vmcs12 *vmcs12)
681 struct vcpu_vmx *vmx = to_vmx(vcpu);
683 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
684 vmcs12->vmcs_link_pointer == -1ull)
687 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
688 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
692 * In nested virtualization, check if L1 has set
693 * VM_EXIT_ACK_INTR_ON_EXIT
695 static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
697 return get_vmcs12(vcpu)->vm_exit_controls &
698 VM_EXIT_ACK_INTR_ON_EXIT;
701 static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
702 struct vmcs12 *vmcs12)
704 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
705 CC(!page_address_valid(vcpu, vmcs12->apic_access_addr)))
711 static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
712 struct vmcs12 *vmcs12)
714 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
715 !nested_cpu_has_apic_reg_virt(vmcs12) &&
716 !nested_cpu_has_vid(vmcs12) &&
717 !nested_cpu_has_posted_intr(vmcs12))
721 * If virtualize x2apic mode is enabled,
722 * virtualize apic access must be disabled.
724 if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) &&
725 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)))
729 * If virtual interrupt delivery is enabled,
730 * we must exit on external interrupts.
732 if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu)))
736 * bits 15:8 should be zero in posted_intr_nv,
737 * the descriptor address has been already checked
738 * in nested_get_vmcs12_pages.
740 * bits 5:0 of posted_intr_desc_addr should be zero.
742 if (nested_cpu_has_posted_intr(vmcs12) &&
743 (CC(!nested_cpu_has_vid(vmcs12)) ||
744 CC(!nested_exit_intr_ack_set(vcpu)) ||
745 CC((vmcs12->posted_intr_nv & 0xff00)) ||
746 CC((vmcs12->posted_intr_desc_addr & 0x3f)) ||
747 CC((vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu)))))
750 /* tpr shadow is needed by all apicv features. */
751 if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)))
757 static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
764 maxphyaddr = cpuid_maxphyaddr(vcpu);
765 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
766 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr)
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 index = vmx_find_msr_index(&vmx->msr_autostore.guest,
945 u64 val = vmx->msr_autostore.guest.val[index].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_index;
1035 bool in_autostore_list;
1038 msr_autostore_index = vmx_find_msr_index(autostore, msr_index);
1039 in_autostore_list = msr_autostore_index >= 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 == 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_index] = autostore->val[last];
1064 static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
1066 unsigned long invalid_mask;
1068 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
1069 return (val & invalid_mask) == 0;
1073 * Returns true if the MMU needs to be sync'd on nested VM-Enter/VM-Exit.
1074 * tl;dr: the MMU needs a sync if L0 is using shadow paging and L1 didn't
1075 * enable VPID for L2 (implying it expects a TLB flush on VMX transitions).
1078 * If EPT is enabled by L0 a sync is never needed:
1079 * - if it is disabled by L1, then L0 is not shadowing L1 or L2 PTEs, there
1080 * cannot be unsync'd SPTEs for either L1 or L2.
1082 * - if it is also enabled by L1, then L0 doesn't need to sync on VM-Enter
1083 * VM-Enter as VM-Enter isn't required to invalidate guest-physical mappings
1084 * (irrespective of VPID), i.e. L1 can't rely on the (virtual) CPU to flush
1085 * stale guest-physical mappings for L2 from the TLB. And as above, L0 isn't
1086 * shadowing L1 PTEs so there are no unsync'd SPTEs to sync on VM-Exit.
1088 * If EPT is disabled by L0:
1089 * - if VPID is enabled by L1 (for L2), the situation is similar to when L1
1090 * enables EPT: L0 doesn't need to sync as VM-Enter and VM-Exit aren't
1091 * required to invalidate linear mappings (EPT is disabled so there are
1092 * no combined or guest-physical mappings), i.e. L1 can't rely on the
1093 * (virtual) CPU to flush stale linear mappings for either L2 or itself (L1).
1095 * - however if VPID is disabled by L1, then a sync is needed as L1 expects all
1096 * linear mappings (EPT is disabled so there are no combined or guest-physical
1097 * mappings) to be invalidated on both VM-Enter and VM-Exit.
1099 * Note, this logic is subtly different than nested_has_guest_tlb_tag(), which
1100 * additionally checks that L2 has been assigned a VPID (when EPT is disabled).
1101 * Whether or not L2 has been assigned a VPID by L0 is irrelevant with respect
1102 * to L1's expectations, e.g. L0 needs to invalidate hardware TLB entries if L2
1103 * doesn't have a unique VPID to prevent reusing L1's entries (assuming L1 has
1104 * been assigned a VPID), but L0 doesn't need to do a MMU sync because L1
1105 * doesn't expect stale (virtual) TLB entries to be flushed, i.e. L1 doesn't
1106 * know that L0 will flush the TLB and so L1 will do INVVPID as needed to flush
1107 * stale TLB entries, at which point L0 will sync L2's MMU.
1109 static bool nested_vmx_transition_mmu_sync(struct kvm_vcpu *vcpu)
1111 return !enable_ept && !nested_cpu_has_vpid(get_vmcs12(vcpu));
1115 * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are
1116 * emulating VM-Entry into a guest with EPT enabled. On failure, the expected
1117 * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to
1118 * @entry_failure_code.
1120 static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
1121 enum vm_entry_failure_code *entry_failure_code)
1123 if (CC(!nested_cr3_valid(vcpu, cr3))) {
1124 *entry_failure_code = ENTRY_FAIL_DEFAULT;
1129 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
1130 * must not be dereferenced.
1132 if (!nested_ept && is_pae_paging(vcpu) &&
1133 (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
1134 if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
1135 *entry_failure_code = ENTRY_FAIL_PDPTE;
1141 * Unconditionally skip the TLB flush on fast CR3 switch, all TLB
1142 * flushes are handled by nested_vmx_transition_tlb_flush(). See
1143 * nested_vmx_transition_mmu_sync for details on skipping the MMU sync.
1146 kvm_mmu_new_pgd(vcpu, cr3, true,
1147 !nested_vmx_transition_mmu_sync(vcpu));
1149 vcpu->arch.cr3 = cr3;
1150 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
1152 kvm_init_mmu(vcpu, false);
1158 * Returns if KVM is able to config CPU to tag TLB entries
1159 * populated by L2 differently than TLB entries populated
1162 * If L0 uses EPT, L1 and L2 run with different EPTP because
1163 * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries
1164 * are tagged with different EPTP.
1166 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
1167 * with different VPID (L1 entries are tagged with vmx->vpid
1168 * while L2 entries are tagged with vmx->nested.vpid02).
1170 static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
1172 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1174 return enable_ept ||
1175 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
1178 static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu,
1179 struct vmcs12 *vmcs12,
1182 struct vcpu_vmx *vmx = to_vmx(vcpu);
1185 * If VPID is disabled, linear and combined mappings are flushed on
1186 * VM-Enter/VM-Exit, and guest-physical mappings are valid only for
1187 * their associated EPTP.
1193 * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings
1194 * for *all* contexts to be flushed on VM-Enter/VM-Exit.
1196 * If VPID is enabled and used by vmc12, but L2 does not have a unique
1197 * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate
1198 * a VPID for L2, flush the current context as the effective ASID is
1199 * common to both L1 and L2.
1201 * Defer the flush so that it runs after vmcs02.EPTP has been set by
1202 * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid
1203 * redundant flushes further down the nested pipeline.
1205 * If a TLB flush isn't required due to any of the above, and vpid12 is
1206 * changing then the new "virtual" VPID (vpid12) will reuse the same
1207 * "real" VPID (vpid02), and so needs to be sync'd. There is no direct
1208 * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for
1211 if (!nested_cpu_has_vpid(vmcs12)) {
1212 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1213 } else if (!nested_has_guest_tlb_tag(vcpu)) {
1214 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
1215 } else if (is_vmenter &&
1216 vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
1217 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
1218 vpid_sync_context(nested_get_vpid02(vcpu));
1222 static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
1227 return (superset | subset) == superset;
1230 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
1232 const u64 feature_and_reserved =
1233 /* feature (except bit 48; see below) */
1234 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1236 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1237 u64 vmx_basic = vmx->nested.msrs.basic;
1239 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1243 * KVM does not emulate a version of VMX that constrains physical
1244 * addresses of VMX structures (e.g. VMCS) to 32-bits.
1246 if (data & BIT_ULL(48))
1249 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
1250 vmx_basic_vmcs_revision_id(data))
1253 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
1256 vmx->nested.msrs.basic = data;
1261 vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1266 switch (msr_index) {
1267 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1268 lowp = &vmx->nested.msrs.pinbased_ctls_low;
1269 highp = &vmx->nested.msrs.pinbased_ctls_high;
1271 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1272 lowp = &vmx->nested.msrs.procbased_ctls_low;
1273 highp = &vmx->nested.msrs.procbased_ctls_high;
1275 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1276 lowp = &vmx->nested.msrs.exit_ctls_low;
1277 highp = &vmx->nested.msrs.exit_ctls_high;
1279 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1280 lowp = &vmx->nested.msrs.entry_ctls_low;
1281 highp = &vmx->nested.msrs.entry_ctls_high;
1283 case MSR_IA32_VMX_PROCBASED_CTLS2:
1284 lowp = &vmx->nested.msrs.secondary_ctls_low;
1285 highp = &vmx->nested.msrs.secondary_ctls_high;
1291 supported = vmx_control_msr(*lowp, *highp);
1293 /* Check must-be-1 bits are still 1. */
1294 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
1297 /* Check must-be-0 bits are still 0. */
1298 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
1302 *highp = data >> 32;
1306 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
1308 const u64 feature_and_reserved_bits =
1310 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
1311 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
1313 GENMASK_ULL(13, 9) | BIT_ULL(31);
1316 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
1317 vmx->nested.msrs.misc_high);
1319 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
1322 if ((vmx->nested.msrs.pinbased_ctls_high &
1323 PIN_BASED_VMX_PREEMPTION_TIMER) &&
1324 vmx_misc_preemption_timer_rate(data) !=
1325 vmx_misc_preemption_timer_rate(vmx_misc))
1328 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
1331 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
1334 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
1337 vmx->nested.msrs.misc_low = data;
1338 vmx->nested.msrs.misc_high = data >> 32;
1343 static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
1345 u64 vmx_ept_vpid_cap;
1347 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
1348 vmx->nested.msrs.vpid_caps);
1350 /* Every bit is either reserved or a feature bit. */
1351 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
1354 vmx->nested.msrs.ept_caps = data;
1355 vmx->nested.msrs.vpid_caps = data >> 32;
1359 static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
1363 switch (msr_index) {
1364 case MSR_IA32_VMX_CR0_FIXED0:
1365 msr = &vmx->nested.msrs.cr0_fixed0;
1367 case MSR_IA32_VMX_CR4_FIXED0:
1368 msr = &vmx->nested.msrs.cr4_fixed0;
1375 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
1376 * must be 1 in the restored value.
1378 if (!is_bitwise_subset(data, *msr, -1ULL))
1386 * Called when userspace is restoring VMX MSRs.
1388 * Returns 0 on success, non-0 otherwise.
1390 int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1392 struct vcpu_vmx *vmx = to_vmx(vcpu);
1395 * Don't allow changes to the VMX capability MSRs while the vCPU
1396 * is in VMX operation.
1398 if (vmx->nested.vmxon)
1401 switch (msr_index) {
1402 case MSR_IA32_VMX_BASIC:
1403 return vmx_restore_vmx_basic(vmx, data);
1404 case MSR_IA32_VMX_PINBASED_CTLS:
1405 case MSR_IA32_VMX_PROCBASED_CTLS:
1406 case MSR_IA32_VMX_EXIT_CTLS:
1407 case MSR_IA32_VMX_ENTRY_CTLS:
1409 * The "non-true" VMX capability MSRs are generated from the
1410 * "true" MSRs, so we do not support restoring them directly.
1412 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
1413 * should restore the "true" MSRs with the must-be-1 bits
1414 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
1415 * DEFAULT SETTINGS".
1418 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1419 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1420 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1421 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1422 case MSR_IA32_VMX_PROCBASED_CTLS2:
1423 return vmx_restore_control_msr(vmx, msr_index, data);
1424 case MSR_IA32_VMX_MISC:
1425 return vmx_restore_vmx_misc(vmx, data);
1426 case MSR_IA32_VMX_CR0_FIXED0:
1427 case MSR_IA32_VMX_CR4_FIXED0:
1428 return vmx_restore_fixed0_msr(vmx, msr_index, data);
1429 case MSR_IA32_VMX_CR0_FIXED1:
1430 case MSR_IA32_VMX_CR4_FIXED1:
1432 * These MSRs are generated based on the vCPU's CPUID, so we
1433 * do not support restoring them directly.
1436 case MSR_IA32_VMX_EPT_VPID_CAP:
1437 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
1438 case MSR_IA32_VMX_VMCS_ENUM:
1439 vmx->nested.msrs.vmcs_enum = data;
1441 case MSR_IA32_VMX_VMFUNC:
1442 if (data & ~vmx->nested.msrs.vmfunc_controls)
1444 vmx->nested.msrs.vmfunc_controls = data;
1448 * The rest of the VMX capability MSRs do not support restore.
1454 /* Returns 0 on success, non-0 otherwise. */
1455 int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
1457 switch (msr_index) {
1458 case MSR_IA32_VMX_BASIC:
1459 *pdata = msrs->basic;
1461 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
1462 case MSR_IA32_VMX_PINBASED_CTLS:
1463 *pdata = vmx_control_msr(
1464 msrs->pinbased_ctls_low,
1465 msrs->pinbased_ctls_high);
1466 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
1467 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1469 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
1470 case MSR_IA32_VMX_PROCBASED_CTLS:
1471 *pdata = vmx_control_msr(
1472 msrs->procbased_ctls_low,
1473 msrs->procbased_ctls_high);
1474 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
1475 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
1477 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
1478 case MSR_IA32_VMX_EXIT_CTLS:
1479 *pdata = vmx_control_msr(
1480 msrs->exit_ctls_low,
1481 msrs->exit_ctls_high);
1482 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
1483 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
1485 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
1486 case MSR_IA32_VMX_ENTRY_CTLS:
1487 *pdata = vmx_control_msr(
1488 msrs->entry_ctls_low,
1489 msrs->entry_ctls_high);
1490 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
1491 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
1493 case MSR_IA32_VMX_MISC:
1494 *pdata = vmx_control_msr(
1498 case MSR_IA32_VMX_CR0_FIXED0:
1499 *pdata = msrs->cr0_fixed0;
1501 case MSR_IA32_VMX_CR0_FIXED1:
1502 *pdata = msrs->cr0_fixed1;
1504 case MSR_IA32_VMX_CR4_FIXED0:
1505 *pdata = msrs->cr4_fixed0;
1507 case MSR_IA32_VMX_CR4_FIXED1:
1508 *pdata = msrs->cr4_fixed1;
1510 case MSR_IA32_VMX_VMCS_ENUM:
1511 *pdata = msrs->vmcs_enum;
1513 case MSR_IA32_VMX_PROCBASED_CTLS2:
1514 *pdata = vmx_control_msr(
1515 msrs->secondary_ctls_low,
1516 msrs->secondary_ctls_high);
1518 case MSR_IA32_VMX_EPT_VPID_CAP:
1519 *pdata = msrs->ept_caps |
1520 ((u64)msrs->vpid_caps << 32);
1522 case MSR_IA32_VMX_VMFUNC:
1523 *pdata = msrs->vmfunc_controls;
1533 * Copy the writable VMCS shadow fields back to the VMCS12, in case they have
1534 * been modified by the L1 guest. Note, "writable" in this context means
1535 * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of
1536 * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only"
1537 * VM-exit information fields (which are actually writable if the vCPU is
1538 * configured to support "VMWRITE to any supported field in the VMCS").
1540 static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
1542 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1543 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1544 struct shadow_vmcs_field field;
1548 if (WARN_ON(!shadow_vmcs))
1553 vmcs_load(shadow_vmcs);
1555 for (i = 0; i < max_shadow_read_write_fields; i++) {
1556 field = shadow_read_write_fields[i];
1557 val = __vmcs_readl(field.encoding);
1558 vmcs12_write_any(vmcs12, field.encoding, field.offset, val);
1561 vmcs_clear(shadow_vmcs);
1562 vmcs_load(vmx->loaded_vmcs->vmcs);
1567 static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
1569 const struct shadow_vmcs_field *fields[] = {
1570 shadow_read_write_fields,
1571 shadow_read_only_fields
1573 const int max_fields[] = {
1574 max_shadow_read_write_fields,
1575 max_shadow_read_only_fields
1577 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
1578 struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu);
1579 struct shadow_vmcs_field field;
1583 if (WARN_ON(!shadow_vmcs))
1586 vmcs_load(shadow_vmcs);
1588 for (q = 0; q < ARRAY_SIZE(fields); q++) {
1589 for (i = 0; i < max_fields[q]; i++) {
1590 field = fields[q][i];
1591 val = vmcs12_read_any(vmcs12, field.encoding,
1593 __vmcs_writel(field.encoding, val);
1597 vmcs_clear(shadow_vmcs);
1598 vmcs_load(vmx->loaded_vmcs->vmcs);
1601 static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
1603 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1604 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1606 /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
1607 vmcs12->tpr_threshold = evmcs->tpr_threshold;
1608 vmcs12->guest_rip = evmcs->guest_rip;
1610 if (unlikely(!(evmcs->hv_clean_fields &
1611 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
1612 vmcs12->guest_rsp = evmcs->guest_rsp;
1613 vmcs12->guest_rflags = evmcs->guest_rflags;
1614 vmcs12->guest_interruptibility_info =
1615 evmcs->guest_interruptibility_info;
1618 if (unlikely(!(evmcs->hv_clean_fields &
1619 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
1620 vmcs12->cpu_based_vm_exec_control =
1621 evmcs->cpu_based_vm_exec_control;
1624 if (unlikely(!(evmcs->hv_clean_fields &
1625 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) {
1626 vmcs12->exception_bitmap = evmcs->exception_bitmap;
1629 if (unlikely(!(evmcs->hv_clean_fields &
1630 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
1631 vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
1634 if (unlikely(!(evmcs->hv_clean_fields &
1635 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
1636 vmcs12->vm_entry_intr_info_field =
1637 evmcs->vm_entry_intr_info_field;
1638 vmcs12->vm_entry_exception_error_code =
1639 evmcs->vm_entry_exception_error_code;
1640 vmcs12->vm_entry_instruction_len =
1641 evmcs->vm_entry_instruction_len;
1644 if (unlikely(!(evmcs->hv_clean_fields &
1645 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
1646 vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
1647 vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
1648 vmcs12->host_cr0 = evmcs->host_cr0;
1649 vmcs12->host_cr3 = evmcs->host_cr3;
1650 vmcs12->host_cr4 = evmcs->host_cr4;
1651 vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
1652 vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
1653 vmcs12->host_rip = evmcs->host_rip;
1654 vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
1655 vmcs12->host_es_selector = evmcs->host_es_selector;
1656 vmcs12->host_cs_selector = evmcs->host_cs_selector;
1657 vmcs12->host_ss_selector = evmcs->host_ss_selector;
1658 vmcs12->host_ds_selector = evmcs->host_ds_selector;
1659 vmcs12->host_fs_selector = evmcs->host_fs_selector;
1660 vmcs12->host_gs_selector = evmcs->host_gs_selector;
1661 vmcs12->host_tr_selector = evmcs->host_tr_selector;
1664 if (unlikely(!(evmcs->hv_clean_fields &
1665 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) {
1666 vmcs12->pin_based_vm_exec_control =
1667 evmcs->pin_based_vm_exec_control;
1668 vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
1669 vmcs12->secondary_vm_exec_control =
1670 evmcs->secondary_vm_exec_control;
1673 if (unlikely(!(evmcs->hv_clean_fields &
1674 HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
1675 vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
1676 vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
1679 if (unlikely(!(evmcs->hv_clean_fields &
1680 HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
1681 vmcs12->msr_bitmap = evmcs->msr_bitmap;
1684 if (unlikely(!(evmcs->hv_clean_fields &
1685 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
1686 vmcs12->guest_es_base = evmcs->guest_es_base;
1687 vmcs12->guest_cs_base = evmcs->guest_cs_base;
1688 vmcs12->guest_ss_base = evmcs->guest_ss_base;
1689 vmcs12->guest_ds_base = evmcs->guest_ds_base;
1690 vmcs12->guest_fs_base = evmcs->guest_fs_base;
1691 vmcs12->guest_gs_base = evmcs->guest_gs_base;
1692 vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
1693 vmcs12->guest_tr_base = evmcs->guest_tr_base;
1694 vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
1695 vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
1696 vmcs12->guest_es_limit = evmcs->guest_es_limit;
1697 vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
1698 vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
1699 vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
1700 vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
1701 vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
1702 vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
1703 vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
1704 vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
1705 vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
1706 vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
1707 vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
1708 vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
1709 vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
1710 vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
1711 vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
1712 vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
1713 vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
1714 vmcs12->guest_es_selector = evmcs->guest_es_selector;
1715 vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
1716 vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
1717 vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
1718 vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
1719 vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
1720 vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
1721 vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
1724 if (unlikely(!(evmcs->hv_clean_fields &
1725 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
1726 vmcs12->tsc_offset = evmcs->tsc_offset;
1727 vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
1728 vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
1731 if (unlikely(!(evmcs->hv_clean_fields &
1732 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
1733 vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
1734 vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
1735 vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
1736 vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
1737 vmcs12->guest_cr0 = evmcs->guest_cr0;
1738 vmcs12->guest_cr3 = evmcs->guest_cr3;
1739 vmcs12->guest_cr4 = evmcs->guest_cr4;
1740 vmcs12->guest_dr7 = evmcs->guest_dr7;
1743 if (unlikely(!(evmcs->hv_clean_fields &
1744 HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
1745 vmcs12->host_fs_base = evmcs->host_fs_base;
1746 vmcs12->host_gs_base = evmcs->host_gs_base;
1747 vmcs12->host_tr_base = evmcs->host_tr_base;
1748 vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
1749 vmcs12->host_idtr_base = evmcs->host_idtr_base;
1750 vmcs12->host_rsp = evmcs->host_rsp;
1753 if (unlikely(!(evmcs->hv_clean_fields &
1754 HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
1755 vmcs12->ept_pointer = evmcs->ept_pointer;
1756 vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
1759 if (unlikely(!(evmcs->hv_clean_fields &
1760 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
1761 vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
1762 vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
1763 vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
1764 vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
1765 vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
1766 vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
1767 vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
1768 vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
1769 vmcs12->guest_pending_dbg_exceptions =
1770 evmcs->guest_pending_dbg_exceptions;
1771 vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
1772 vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
1773 vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
1774 vmcs12->guest_activity_state = evmcs->guest_activity_state;
1775 vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
1780 * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
1781 * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
1782 * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
1783 * vmcs12->page_fault_error_code_mask =
1784 * evmcs->page_fault_error_code_mask;
1785 * vmcs12->page_fault_error_code_match =
1786 * evmcs->page_fault_error_code_match;
1787 * vmcs12->cr3_target_count = evmcs->cr3_target_count;
1788 * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
1789 * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
1790 * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
1795 * vmcs12->guest_physical_address = evmcs->guest_physical_address;
1796 * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
1797 * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
1798 * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
1799 * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
1800 * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
1801 * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
1802 * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
1803 * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
1804 * vmcs12->exit_qualification = evmcs->exit_qualification;
1805 * vmcs12->guest_linear_address = evmcs->guest_linear_address;
1807 * Not present in struct vmcs12:
1808 * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
1809 * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
1810 * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
1811 * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
1817 static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
1819 struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
1820 struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
1823 * Should not be changed by KVM:
1825 * evmcs->host_es_selector = vmcs12->host_es_selector;
1826 * evmcs->host_cs_selector = vmcs12->host_cs_selector;
1827 * evmcs->host_ss_selector = vmcs12->host_ss_selector;
1828 * evmcs->host_ds_selector = vmcs12->host_ds_selector;
1829 * evmcs->host_fs_selector = vmcs12->host_fs_selector;
1830 * evmcs->host_gs_selector = vmcs12->host_gs_selector;
1831 * evmcs->host_tr_selector = vmcs12->host_tr_selector;
1832 * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
1833 * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
1834 * evmcs->host_cr0 = vmcs12->host_cr0;
1835 * evmcs->host_cr3 = vmcs12->host_cr3;
1836 * evmcs->host_cr4 = vmcs12->host_cr4;
1837 * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
1838 * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
1839 * evmcs->host_rip = vmcs12->host_rip;
1840 * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
1841 * evmcs->host_fs_base = vmcs12->host_fs_base;
1842 * evmcs->host_gs_base = vmcs12->host_gs_base;
1843 * evmcs->host_tr_base = vmcs12->host_tr_base;
1844 * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
1845 * evmcs->host_idtr_base = vmcs12->host_idtr_base;
1846 * evmcs->host_rsp = vmcs12->host_rsp;
1847 * sync_vmcs02_to_vmcs12() doesn't read these:
1848 * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
1849 * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
1850 * evmcs->msr_bitmap = vmcs12->msr_bitmap;
1851 * evmcs->ept_pointer = vmcs12->ept_pointer;
1852 * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
1853 * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
1854 * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
1855 * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
1856 * evmcs->tpr_threshold = vmcs12->tpr_threshold;
1857 * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
1858 * evmcs->exception_bitmap = vmcs12->exception_bitmap;
1859 * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
1860 * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
1861 * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
1862 * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
1863 * evmcs->page_fault_error_code_mask =
1864 * vmcs12->page_fault_error_code_mask;
1865 * evmcs->page_fault_error_code_match =
1866 * vmcs12->page_fault_error_code_match;
1867 * evmcs->cr3_target_count = vmcs12->cr3_target_count;
1868 * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
1869 * evmcs->tsc_offset = vmcs12->tsc_offset;
1870 * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
1871 * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
1872 * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
1873 * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
1874 * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
1875 * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
1876 * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
1877 * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
1879 * Not present in struct vmcs12:
1880 * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
1881 * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
1882 * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
1883 * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
1886 evmcs->guest_es_selector = vmcs12->guest_es_selector;
1887 evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
1888 evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
1889 evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
1890 evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
1891 evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
1892 evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
1893 evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
1895 evmcs->guest_es_limit = vmcs12->guest_es_limit;
1896 evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
1897 evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
1898 evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
1899 evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
1900 evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
1901 evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
1902 evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
1903 evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
1904 evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
1906 evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
1907 evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
1908 evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
1909 evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
1910 evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
1911 evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
1912 evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
1913 evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
1915 evmcs->guest_es_base = vmcs12->guest_es_base;
1916 evmcs->guest_cs_base = vmcs12->guest_cs_base;
1917 evmcs->guest_ss_base = vmcs12->guest_ss_base;
1918 evmcs->guest_ds_base = vmcs12->guest_ds_base;
1919 evmcs->guest_fs_base = vmcs12->guest_fs_base;
1920 evmcs->guest_gs_base = vmcs12->guest_gs_base;
1921 evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
1922 evmcs->guest_tr_base = vmcs12->guest_tr_base;
1923 evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
1924 evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
1926 evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
1927 evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
1929 evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
1930 evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
1931 evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
1932 evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
1934 evmcs->guest_pending_dbg_exceptions =
1935 vmcs12->guest_pending_dbg_exceptions;
1936 evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
1937 evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
1939 evmcs->guest_activity_state = vmcs12->guest_activity_state;
1940 evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
1942 evmcs->guest_cr0 = vmcs12->guest_cr0;
1943 evmcs->guest_cr3 = vmcs12->guest_cr3;
1944 evmcs->guest_cr4 = vmcs12->guest_cr4;
1945 evmcs->guest_dr7 = vmcs12->guest_dr7;
1947 evmcs->guest_physical_address = vmcs12->guest_physical_address;
1949 evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
1950 evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
1951 evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
1952 evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
1953 evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
1954 evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
1955 evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
1956 evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
1958 evmcs->exit_qualification = vmcs12->exit_qualification;
1960 evmcs->guest_linear_address = vmcs12->guest_linear_address;
1961 evmcs->guest_rsp = vmcs12->guest_rsp;
1962 evmcs->guest_rflags = vmcs12->guest_rflags;
1964 evmcs->guest_interruptibility_info =
1965 vmcs12->guest_interruptibility_info;
1966 evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
1967 evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
1968 evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
1969 evmcs->vm_entry_exception_error_code =
1970 vmcs12->vm_entry_exception_error_code;
1971 evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
1973 evmcs->guest_rip = vmcs12->guest_rip;
1975 evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
1981 * This is an equivalent of the nested hypervisor executing the vmptrld
1984 static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld(
1985 struct kvm_vcpu *vcpu, bool from_launch)
1987 struct vcpu_vmx *vmx = to_vmx(vcpu);
1988 bool evmcs_gpa_changed = false;
1991 if (likely(!vmx->nested.enlightened_vmcs_enabled))
1992 return EVMPTRLD_DISABLED;
1994 if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa))
1995 return EVMPTRLD_DISABLED;
1997 if (unlikely(!vmx->nested.hv_evmcs ||
1998 evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) {
1999 if (!vmx->nested.hv_evmcs)
2000 vmx->nested.current_vmptr = -1ull;
2002 nested_release_evmcs(vcpu);
2004 if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa),
2005 &vmx->nested.hv_evmcs_map))
2006 return EVMPTRLD_ERROR;
2008 vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva;
2011 * Currently, KVM only supports eVMCS version 1
2012 * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
2013 * value to first u32 field of eVMCS which should specify eVMCS
2016 * Guest should be aware of supported eVMCS versions by host by
2017 * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
2018 * expected to set this CPUID leaf according to the value
2019 * returned in vmcs_version from nested_enable_evmcs().
2021 * However, it turns out that Microsoft Hyper-V fails to comply
2022 * to their own invented interface: When Hyper-V use eVMCS, it
2023 * just sets first u32 field of eVMCS to revision_id specified
2024 * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
2025 * which is one of the supported versions specified in
2026 * CPUID.0x4000000A.EAX[0:15].
2028 * To overcome Hyper-V bug, we accept here either a supported
2029 * eVMCS version or VMCS12 revision_id as valid values for first
2030 * u32 field of eVMCS.
2032 if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
2033 (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
2034 nested_release_evmcs(vcpu);
2035 return EVMPTRLD_VMFAIL;
2038 vmx->nested.dirty_vmcs12 = true;
2039 vmx->nested.hv_evmcs_vmptr = evmcs_gpa;
2041 evmcs_gpa_changed = true;
2043 * Unlike normal vmcs12, enlightened vmcs12 is not fully
2044 * reloaded from guest's memory (read only fields, fields not
2045 * present in struct hv_enlightened_vmcs, ...). Make sure there
2049 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2050 memset(vmcs12, 0, sizeof(*vmcs12));
2051 vmcs12->hdr.revision_id = VMCS12_REVISION;
2057 * Clean fields data can't be used on VMLAUNCH and when we switch
2058 * between different L2 guests as KVM keeps a single VMCS12 per L1.
2060 if (from_launch || evmcs_gpa_changed)
2061 vmx->nested.hv_evmcs->hv_clean_fields &=
2062 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2064 return EVMPTRLD_SUCCEEDED;
2067 void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu)
2069 struct vcpu_vmx *vmx = to_vmx(vcpu);
2071 if (vmx->nested.hv_evmcs) {
2072 copy_vmcs12_to_enlightened(vmx);
2073 /* All fields are clean */
2074 vmx->nested.hv_evmcs->hv_clean_fields |=
2075 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
2077 copy_vmcs12_to_shadow(vmx);
2080 vmx->nested.need_vmcs12_to_shadow_sync = false;
2083 static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
2085 struct vcpu_vmx *vmx =
2086 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
2088 vmx->nested.preemption_timer_expired = true;
2089 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
2090 kvm_vcpu_kick(&vmx->vcpu);
2092 return HRTIMER_NORESTART;
2095 static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
2097 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
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 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL_PINNED);
2119 static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2121 if (vmx->nested.nested_run_pending &&
2122 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
2123 return vmcs12->guest_ia32_efer;
2124 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
2125 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
2127 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
2130 static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
2133 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
2134 * according to L0's settings (vmcs12 is irrelevant here). Host
2135 * fields that come from L0 and are not constant, e.g. HOST_CR3,
2136 * will be set as needed prior to VMLAUNCH/VMRESUME.
2138 if (vmx->nested.vmcs02_initialized)
2140 vmx->nested.vmcs02_initialized = true;
2143 * We don't care what the EPTP value is we just need to guarantee
2144 * it's valid so we don't get a false positive when doing early
2145 * consistency checks.
2147 if (enable_ept && nested_early_check)
2148 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
2150 /* All VMFUNCs are currently emulated through L0 vmexits. */
2151 if (cpu_has_vmx_vmfunc())
2152 vmcs_write64(VM_FUNCTION_CONTROL, 0);
2154 if (cpu_has_vmx_posted_intr())
2155 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
2157 if (cpu_has_vmx_msr_bitmap())
2158 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
2161 * The PML address never changes, so it is constant in vmcs02.
2162 * Conceptually we want to copy the PML index from vmcs01 here,
2163 * and then back to vmcs01 on nested vmexit. But since we flush
2164 * the log and reset GUEST_PML_INDEX on each vmexit, the PML
2165 * index is also effectively constant in vmcs02.
2168 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
2169 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
2172 if (cpu_has_vmx_encls_vmexit())
2173 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
2176 * Set the MSR load/store lists to match L0's settings. Only the
2177 * addresses are constant (for vmcs02), the counts can change based
2178 * on L2's behavior, e.g. switching to/from long mode.
2180 vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val));
2181 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
2182 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
2184 vmx_set_constant_host_state(vmx);
2187 static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx,
2188 struct vmcs12 *vmcs12)
2190 prepare_vmcs02_constant_state(vmx);
2192 vmcs_write64(VMCS_LINK_POINTER, -1ull);
2195 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
2196 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
2198 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2202 static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2204 u32 exec_control, vmcs12_exec_ctrl;
2205 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
2207 if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
2208 prepare_vmcs02_early_rare(vmx, vmcs12);
2213 exec_control = vmx_pin_based_exec_ctrl(vmx);
2214 exec_control |= (vmcs12->pin_based_vm_exec_control &
2215 ~PIN_BASED_VMX_PREEMPTION_TIMER);
2217 /* Posted interrupts setting is only taken from vmcs12. */
2218 if (nested_cpu_has_posted_intr(vmcs12)) {
2219 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
2220 vmx->nested.pi_pending = false;
2222 exec_control &= ~PIN_BASED_POSTED_INTR;
2224 pin_controls_set(vmx, exec_control);
2229 exec_control = vmx_exec_control(vmx); /* L0's desires */
2230 exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING;
2231 exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING;
2232 exec_control &= ~CPU_BASED_TPR_SHADOW;
2233 exec_control |= vmcs12->cpu_based_vm_exec_control;
2235 vmx->nested.l1_tpr_threshold = -1;
2236 if (exec_control & CPU_BASED_TPR_SHADOW)
2237 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
2238 #ifdef CONFIG_X86_64
2240 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
2241 CPU_BASED_CR8_STORE_EXITING;
2245 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
2246 * for I/O port accesses.
2248 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
2249 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
2252 * This bit will be computed in nested_get_vmcs12_pages, because
2253 * we do not have access to L1's MSR bitmap yet. For now, keep
2254 * the same bit as before, hoping to avoid multiple VMWRITEs that
2255 * only set/clear this bit.
2257 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
2258 exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS;
2260 exec_controls_set(vmx, exec_control);
2263 * SECONDARY EXEC CONTROLS
2265 if (cpu_has_secondary_exec_ctrls()) {
2266 exec_control = vmx->secondary_exec_control;
2268 /* Take the following fields only from vmcs12 */
2269 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2270 SECONDARY_EXEC_ENABLE_INVPCID |
2271 SECONDARY_EXEC_RDTSCP |
2272 SECONDARY_EXEC_XSAVES |
2273 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
2274 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2275 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2276 SECONDARY_EXEC_ENABLE_VMFUNC);
2277 if (nested_cpu_has(vmcs12,
2278 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
2279 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
2280 ~SECONDARY_EXEC_ENABLE_PML;
2281 exec_control |= vmcs12_exec_ctrl;
2284 /* VMCS shadowing for L2 is emulated for now */
2285 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
2288 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
2289 * will not have to rewrite the controls just for this bit.
2291 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
2292 (vmcs12->guest_cr4 & X86_CR4_UMIP))
2293 exec_control |= SECONDARY_EXEC_DESC;
2295 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
2296 vmcs_write16(GUEST_INTR_STATUS,
2297 vmcs12->guest_intr_status);
2299 secondary_exec_controls_set(vmx, exec_control);
2305 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
2306 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
2307 * on the related bits (if supported by the CPU) in the hope that
2308 * we can avoid VMWrites during vmx_set_efer().
2310 exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) &
2311 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
2312 if (cpu_has_load_ia32_efer()) {
2313 if (guest_efer & EFER_LMA)
2314 exec_control |= VM_ENTRY_IA32E_MODE;
2315 if (guest_efer != host_efer)
2316 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
2318 vm_entry_controls_set(vmx, exec_control);
2323 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
2324 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
2325 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
2327 exec_control = vmx_vmexit_ctrl();
2328 if (cpu_has_load_ia32_efer() && guest_efer != host_efer)
2329 exec_control |= VM_EXIT_LOAD_IA32_EFER;
2330 vm_exit_controls_set(vmx, exec_control);
2333 * Interrupt/Exception Fields
2335 if (vmx->nested.nested_run_pending) {
2336 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2337 vmcs12->vm_entry_intr_info_field);
2338 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2339 vmcs12->vm_entry_exception_error_code);
2340 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2341 vmcs12->vm_entry_instruction_len);
2342 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
2343 vmcs12->guest_interruptibility_info);
2344 vmx->loaded_vmcs->nmi_known_unmasked =
2345 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
2347 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
2351 static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
2353 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2355 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2356 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
2357 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
2358 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
2359 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
2360 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
2361 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
2362 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
2363 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
2364 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
2365 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
2366 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
2367 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
2368 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
2369 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
2370 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
2371 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
2372 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
2373 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
2374 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
2375 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
2376 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
2377 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
2378 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
2379 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
2380 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
2381 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
2382 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
2383 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
2384 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
2385 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
2386 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
2387 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
2388 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
2389 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
2390 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
2391 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
2392 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
2395 if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
2396 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
2397 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
2398 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
2399 vmcs12->guest_pending_dbg_exceptions);
2400 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
2401 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
2404 * L1 may access the L2's PDPTR, so save them to construct
2408 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2409 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2410 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2411 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2414 if (kvm_mpx_supported() && vmx->nested.nested_run_pending &&
2415 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
2416 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
2419 if (nested_cpu_has_xsaves(vmcs12))
2420 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
2423 * Whether page-faults are trapped is determined by a combination of
2424 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
2425 * If enable_ept, L0 doesn't care about page faults and we should
2426 * set all of these to L1's desires. However, if !enable_ept, L0 does
2427 * care about (at least some) page faults, and because it is not easy
2428 * (if at all possible?) to merge L0 and L1's desires, we simply ask
2429 * to exit on each and every L2 page fault. This is done by setting
2430 * MASK=MATCH=0 and (see below) EB.PF=1.
2431 * Note that below we don't need special code to set EB.PF beyond the
2432 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
2433 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
2434 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
2436 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
2437 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
2438 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
2439 enable_ept ? vmcs12->page_fault_error_code_match : 0);
2441 if (cpu_has_vmx_apicv()) {
2442 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
2443 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
2444 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
2445 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
2449 * Make sure the msr_autostore list is up to date before we set the
2450 * count in the vmcs02.
2452 prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC);
2454 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr);
2455 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
2456 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
2458 set_cr4_guest_host_mask(vmx);
2462 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
2463 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
2464 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
2465 * guest in a way that will both be appropriate to L1's requests, and our
2466 * needs. In addition to modifying the active vmcs (which is vmcs02), this
2467 * function also has additional necessary side-effects, like setting various
2468 * vcpu->arch fields.
2469 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
2470 * is assigned to entry_failure_code on failure.
2472 static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
2473 enum vm_entry_failure_code *entry_failure_code)
2475 struct vcpu_vmx *vmx = to_vmx(vcpu);
2476 struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
2477 bool load_guest_pdptrs_vmcs12 = false;
2479 if (vmx->nested.dirty_vmcs12 || hv_evmcs) {
2480 prepare_vmcs02_rare(vmx, vmcs12);
2481 vmx->nested.dirty_vmcs12 = false;
2483 load_guest_pdptrs_vmcs12 = !hv_evmcs ||
2484 !(hv_evmcs->hv_clean_fields &
2485 HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1);
2488 if (vmx->nested.nested_run_pending &&
2489 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
2490 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
2491 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
2493 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
2494 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
2496 if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending ||
2497 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)))
2498 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
2499 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
2501 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
2502 * bitwise-or of what L1 wants to trap for L2, and what we want to
2503 * trap. Note that CR0.TS also needs updating - we do this later.
2505 update_exception_bitmap(vcpu);
2506 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
2507 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
2509 if (vmx->nested.nested_run_pending &&
2510 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
2511 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
2512 vcpu->arch.pat = vmcs12->guest_ia32_pat;
2513 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2514 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
2517 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
2519 if (kvm_has_tsc_control)
2520 decache_tsc_multiplier(vmx);
2522 nested_vmx_transition_tlb_flush(vcpu, vmcs12, true);
2524 if (nested_cpu_has_ept(vmcs12))
2525 nested_ept_init_mmu_context(vcpu);
2528 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
2529 * bits which we consider mandatory enabled.
2530 * The CR0_READ_SHADOW is what L2 should have expected to read given
2531 * the specifications by L1; It's not enough to take
2532 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
2533 * have more bits than L1 expected.
2535 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
2536 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2538 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
2539 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
2541 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
2542 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
2543 vmx_set_efer(vcpu, vcpu->arch.efer);
2546 * Guest state is invalid and unrestricted guest is disabled,
2547 * which means L1 attempted VMEntry to L2 with invalid state.
2550 if (vmx->emulation_required) {
2551 *entry_failure_code = ENTRY_FAIL_DEFAULT;
2555 /* Shadow page tables on either EPT or shadow page tables. */
2556 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
2557 entry_failure_code))
2561 * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12
2562 * on nested VM-Exit, which can occur without actually running L2 and
2563 * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with
2564 * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the
2565 * transition to HLT instead of running L2.
2568 vmcs_writel(GUEST_CR3, vmcs12->guest_cr3);
2570 /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */
2571 if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) &&
2572 is_pae_paging(vcpu)) {
2573 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
2574 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
2575 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
2576 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
2580 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
2582 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2583 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
2584 vmcs12->guest_ia32_perf_global_ctrl)))
2587 kvm_rsp_write(vcpu, vmcs12->guest_rsp);
2588 kvm_rip_write(vcpu, vmcs12->guest_rip);
2592 static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
2594 if (CC(!nested_cpu_has_nmi_exiting(vmcs12) &&
2595 nested_cpu_has_virtual_nmis(vmcs12)))
2598 if (CC(!nested_cpu_has_virtual_nmis(vmcs12) &&
2599 nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING)))
2605 static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp)
2607 struct vcpu_vmx *vmx = to_vmx(vcpu);
2608 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2610 /* Check for memory type validity */
2611 switch (new_eptp & VMX_EPTP_MT_MASK) {
2612 case VMX_EPTP_MT_UC:
2613 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)))
2616 case VMX_EPTP_MT_WB:
2617 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)))
2624 /* Page-walk levels validity. */
2625 switch (new_eptp & VMX_EPTP_PWL_MASK) {
2626 case VMX_EPTP_PWL_5:
2627 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT)))
2630 case VMX_EPTP_PWL_4:
2631 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT)))
2638 /* Reserved bits should not be set */
2639 if (CC(new_eptp >> maxphyaddr || ((new_eptp >> 7) & 0x1f)))
2642 /* AD, if set, should be supported */
2643 if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) {
2644 if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)))
2652 * Checks related to VM-Execution Control Fields
2654 static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
2655 struct vmcs12 *vmcs12)
2657 struct vcpu_vmx *vmx = to_vmx(vcpu);
2659 if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control,
2660 vmx->nested.msrs.pinbased_ctls_low,
2661 vmx->nested.msrs.pinbased_ctls_high)) ||
2662 CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
2663 vmx->nested.msrs.procbased_ctls_low,
2664 vmx->nested.msrs.procbased_ctls_high)))
2667 if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
2668 CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control,
2669 vmx->nested.msrs.secondary_ctls_low,
2670 vmx->nested.msrs.secondary_ctls_high)))
2673 if (CC(vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) ||
2674 nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) ||
2675 nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) ||
2676 nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) ||
2677 nested_vmx_check_apic_access_controls(vcpu, vmcs12) ||
2678 nested_vmx_check_apicv_controls(vcpu, vmcs12) ||
2679 nested_vmx_check_nmi_controls(vmcs12) ||
2680 nested_vmx_check_pml_controls(vcpu, vmcs12) ||
2681 nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) ||
2682 nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) ||
2683 nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) ||
2684 CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
2687 if (!nested_cpu_has_preemption_timer(vmcs12) &&
2688 nested_cpu_has_save_preemption_timer(vmcs12))
2691 if (nested_cpu_has_ept(vmcs12) &&
2692 CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer)))
2695 if (nested_cpu_has_vmfunc(vmcs12)) {
2696 if (CC(vmcs12->vm_function_control &
2697 ~vmx->nested.msrs.vmfunc_controls))
2700 if (nested_cpu_has_eptp_switching(vmcs12)) {
2701 if (CC(!nested_cpu_has_ept(vmcs12)) ||
2702 CC(!page_address_valid(vcpu, vmcs12->eptp_list_address)))
2711 * Checks related to VM-Exit Control Fields
2713 static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu,
2714 struct vmcs12 *vmcs12)
2716 struct vcpu_vmx *vmx = to_vmx(vcpu);
2718 if (CC(!vmx_control_verify(vmcs12->vm_exit_controls,
2719 vmx->nested.msrs.exit_ctls_low,
2720 vmx->nested.msrs.exit_ctls_high)) ||
2721 CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)))
2728 * Checks related to VM-Entry Control Fields
2730 static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,
2731 struct vmcs12 *vmcs12)
2733 struct vcpu_vmx *vmx = to_vmx(vcpu);
2735 if (CC(!vmx_control_verify(vmcs12->vm_entry_controls,
2736 vmx->nested.msrs.entry_ctls_low,
2737 vmx->nested.msrs.entry_ctls_high)))
2741 * From the Intel SDM, volume 3:
2742 * Fields relevant to VM-entry event injection must be set properly.
2743 * These fields are the VM-entry interruption-information field, the
2744 * VM-entry exception error code, and the VM-entry instruction length.
2746 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
2747 u32 intr_info = vmcs12->vm_entry_intr_info_field;
2748 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
2749 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
2750 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
2751 bool should_have_error_code;
2752 bool urg = nested_cpu_has2(vmcs12,
2753 SECONDARY_EXEC_UNRESTRICTED_GUEST);
2754 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
2756 /* VM-entry interruption-info field: interruption type */
2757 if (CC(intr_type == INTR_TYPE_RESERVED) ||
2758 CC(intr_type == INTR_TYPE_OTHER_EVENT &&
2759 !nested_cpu_supports_monitor_trap_flag(vcpu)))
2762 /* VM-entry interruption-info field: vector */
2763 if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
2764 CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
2765 CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
2768 /* VM-entry interruption-info field: deliver error code */
2769 should_have_error_code =
2770 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
2771 x86_exception_has_error_code(vector);
2772 if (CC(has_error_code != should_have_error_code))
2775 /* VM-entry exception error code */
2776 if (CC(has_error_code &&
2777 vmcs12->vm_entry_exception_error_code & GENMASK(31, 16)))
2780 /* VM-entry interruption-info field: reserved bits */
2781 if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK))
2784 /* VM-entry instruction length */
2785 switch (intr_type) {
2786 case INTR_TYPE_SOFT_EXCEPTION:
2787 case INTR_TYPE_SOFT_INTR:
2788 case INTR_TYPE_PRIV_SW_EXCEPTION:
2789 if (CC(vmcs12->vm_entry_instruction_len > 15) ||
2790 CC(vmcs12->vm_entry_instruction_len == 0 &&
2791 CC(!nested_cpu_has_zero_length_injection(vcpu))))
2796 if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12))
2802 static int nested_vmx_check_controls(struct kvm_vcpu *vcpu,
2803 struct vmcs12 *vmcs12)
2805 if (nested_check_vm_execution_controls(vcpu, vmcs12) ||
2806 nested_check_vm_exit_controls(vcpu, vmcs12) ||
2807 nested_check_vm_entry_controls(vcpu, vmcs12))
2810 if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled)
2811 return nested_evmcs_check_controls(vmcs12);
2816 static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu,
2817 struct vmcs12 *vmcs12)
2821 if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) ||
2822 CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) ||
2823 CC(!nested_cr3_valid(vcpu, vmcs12->host_cr3)))
2826 if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) ||
2827 CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu)))
2830 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
2831 CC(!kvm_pat_valid(vmcs12->host_ia32_pat)))
2834 if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2835 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2836 vmcs12->host_ia32_perf_global_ctrl)))
2839 #ifdef CONFIG_X86_64
2840 ia32e = !!(vcpu->arch.efer & EFER_LMA);
2846 if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) ||
2847 CC(!(vmcs12->host_cr4 & X86_CR4_PAE)))
2850 if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) ||
2851 CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) ||
2852 CC(vmcs12->host_cr4 & X86_CR4_PCIDE) ||
2853 CC((vmcs12->host_rip) >> 32))
2857 if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2858 CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2859 CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2860 CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2861 CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2862 CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2863 CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) ||
2864 CC(vmcs12->host_cs_selector == 0) ||
2865 CC(vmcs12->host_tr_selector == 0) ||
2866 CC(vmcs12->host_ss_selector == 0 && !ia32e))
2869 if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) ||
2870 CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) ||
2871 CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) ||
2872 CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||
2873 CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) ||
2874 CC(is_noncanonical_address(vmcs12->host_rip, vcpu)))
2878 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
2879 * IA32_EFER MSR must be 0 in the field for that register. In addition,
2880 * the values of the LMA and LME bits in the field must each be that of
2881 * the host address-space size VM-exit control.
2883 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
2884 if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) ||
2885 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) ||
2886 CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)))
2893 static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
2894 struct vmcs12 *vmcs12)
2897 struct vmcs12 *shadow;
2898 struct kvm_host_map map;
2900 if (vmcs12->vmcs_link_pointer == -1ull)
2903 if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)))
2906 if (CC(kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)))
2911 if (CC(shadow->hdr.revision_id != VMCS12_REVISION) ||
2912 CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)))
2915 kvm_vcpu_unmap(vcpu, &map, false);
2920 * Checks related to Guest Non-register State
2922 static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12)
2924 if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
2925 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT))
2931 static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
2932 struct vmcs12 *vmcs12,
2933 enum vm_entry_failure_code *entry_failure_code)
2937 *entry_failure_code = ENTRY_FAIL_DEFAULT;
2939 if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) ||
2940 CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
2943 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
2944 CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
2947 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
2948 CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
2951 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
2952 *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR;
2956 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) &&
2957 CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu),
2958 vmcs12->guest_ia32_perf_global_ctrl)))
2962 * If the load IA32_EFER VM-entry control is 1, the following checks
2963 * are performed on the field for the IA32_EFER MSR:
2964 * - Bits reserved in the IA32_EFER MSR must be 0.
2965 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
2966 * the IA-32e mode guest VM-exit control. It must also be identical
2967 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
2970 if (to_vmx(vcpu)->nested.nested_run_pending &&
2971 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
2972 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
2973 if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) ||
2974 CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) ||
2975 CC(((vmcs12->guest_cr0 & X86_CR0_PG) &&
2976 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))))
2980 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
2981 (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) ||
2982 CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))))
2985 if (nested_check_guest_non_reg_state(vmcs12))
2991 static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
2993 struct vcpu_vmx *vmx = to_vmx(vcpu);
2994 unsigned long cr3, cr4;
2997 if (!nested_early_check)
3000 if (vmx->msr_autoload.host.nr)
3001 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
3002 if (vmx->msr_autoload.guest.nr)
3003 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
3007 vmx_prepare_switch_to_guest(vcpu);
3010 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
3011 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
3012 * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e.
3013 * there is no need to preserve other bits or save/restore the field.
3015 vmcs_writel(GUEST_RFLAGS, 0);
3017 cr3 = __get_current_cr3_fast();
3018 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
3019 vmcs_writel(HOST_CR3, cr3);
3020 vmx->loaded_vmcs->host_state.cr3 = cr3;
3023 cr4 = cr4_read_shadow();
3024 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
3025 vmcs_writel(HOST_CR4, cr4);
3026 vmx->loaded_vmcs->host_state.cr4 = cr4;
3030 "sub $%c[wordsize], %%" _ASM_SP "\n\t" /* temporarily adjust RSP for CALL */
3031 "cmp %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t"
3033 __ex("vmwrite %%" _ASM_SP ", %[HOST_RSP]") "\n\t"
3034 "mov %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t"
3036 "add $%c[wordsize], %%" _ASM_SP "\n\t" /* un-adjust RSP */
3038 /* Check if vmlaunch or vmresume is needed */
3039 "cmpb $0, %c[launched](%[loaded_vmcs])\n\t"
3042 * VMLAUNCH and VMRESUME clear RFLAGS.{CF,ZF} on VM-Exit, set
3043 * RFLAGS.CF on VM-Fail Invalid and set RFLAGS.ZF on VM-Fail
3044 * Valid. vmx_vmenter() directly "returns" RFLAGS, and so the
3045 * results of VM-Enter is captured via CC_{SET,OUT} to vm_fail.
3047 "call vmx_vmenter\n\t"
3050 : ASM_CALL_CONSTRAINT, CC_OUT(be) (vm_fail)
3051 : [HOST_RSP]"r"((unsigned long)HOST_RSP),
3052 [loaded_vmcs]"r"(vmx->loaded_vmcs),
3053 [launched]"i"(offsetof(struct loaded_vmcs, launched)),
3054 [host_state_rsp]"i"(offsetof(struct loaded_vmcs, host_state.rsp)),
3055 [wordsize]"i"(sizeof(ulong))
3059 if (vmx->msr_autoload.host.nr)
3060 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
3061 if (vmx->msr_autoload.guest.nr)
3062 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
3065 u32 error = vmcs_read32(VM_INSTRUCTION_ERROR);
3069 trace_kvm_nested_vmenter_failed(
3070 "early hardware check VM-instruction error: ", error);
3071 WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3076 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
3079 if (hw_breakpoint_active())
3080 set_debugreg(__this_cpu_read(cpu_dr7), 7);
3084 * A non-failing VMEntry means we somehow entered guest mode with
3085 * an illegal RIP, and that's just the tip of the iceberg. There
3086 * is no telling what memory has been modified or what state has
3087 * been exposed to unknown code. Hitting this all but guarantees
3088 * a (very critical) hardware issue.
3090 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
3091 VMX_EXIT_REASONS_FAILED_VMENTRY));
3096 static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
3098 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3099 struct vcpu_vmx *vmx = to_vmx(vcpu);
3100 struct kvm_host_map *map;
3105 * hv_evmcs may end up being not mapped after migration (when
3106 * L2 was running), map it here to make sure vmcs12 changes are
3107 * properly reflected.
3109 if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs) {
3110 enum nested_evmptrld_status evmptrld_status =
3111 nested_vmx_handle_enlightened_vmptrld(vcpu, false);
3113 if (evmptrld_status == EVMPTRLD_VMFAIL ||
3114 evmptrld_status == EVMPTRLD_ERROR) {
3115 pr_debug_ratelimited("%s: enlightened vmptrld failed\n",
3117 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3118 vcpu->run->internal.suberror =
3119 KVM_INTERNAL_ERROR_EMULATION;
3120 vcpu->run->internal.ndata = 0;
3125 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3127 * Translate L1 physical address to host physical
3128 * address for vmcs02. Keep the page pinned, so this
3129 * physical address remains valid. We keep a reference
3130 * to it so we can release it later.
3132 if (vmx->nested.apic_access_page) { /* shouldn't happen */
3133 kvm_release_page_clean(vmx->nested.apic_access_page);
3134 vmx->nested.apic_access_page = NULL;
3136 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
3137 if (!is_error_page(page)) {
3138 vmx->nested.apic_access_page = page;
3139 hpa = page_to_phys(vmx->nested.apic_access_page);
3140 vmcs_write64(APIC_ACCESS_ADDR, hpa);
3142 pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n",
3144 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3145 vcpu->run->internal.suberror =
3146 KVM_INTERNAL_ERROR_EMULATION;
3147 vcpu->run->internal.ndata = 0;
3152 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3153 map = &vmx->nested.virtual_apic_map;
3155 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) {
3156 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn));
3157 } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) &&
3158 nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) &&
3159 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
3161 * The processor will never use the TPR shadow, simply
3162 * clear the bit from the execution control. Such a
3163 * configuration is useless, but it happens in tests.
3164 * For any other configuration, failing the vm entry is
3165 * _not_ what the processor does but it's basically the
3166 * only possibility we have.
3168 exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW);
3171 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to
3172 * force VM-Entry to fail.
3174 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
3178 if (nested_cpu_has_posted_intr(vmcs12)) {
3179 map = &vmx->nested.pi_desc_map;
3181 if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) {
3182 vmx->nested.pi_desc =
3183 (struct pi_desc *)(((void *)map->hva) +
3184 offset_in_page(vmcs12->posted_intr_desc_addr));
3185 vmcs_write64(POSTED_INTR_DESC_ADDR,
3186 pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr));
3189 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
3190 exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
3192 exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS);
3197 * Intel's VMX Instruction Reference specifies a common set of prerequisites
3198 * for running VMX instructions (except VMXON, whose prerequisites are
3199 * slightly different). It also specifies what exception to inject otherwise.
3200 * Note that many of these exceptions have priority over VM exits, so they
3201 * don't have to be checked again here.
3203 static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
3205 if (!to_vmx(vcpu)->nested.vmxon) {
3206 kvm_queue_exception(vcpu, UD_VECTOR);
3210 if (vmx_get_cpl(vcpu)) {
3211 kvm_inject_gp(vcpu, 0);
3218 static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
3220 u8 rvi = vmx_get_rvi();
3221 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
3223 return ((rvi & 0xf0) > (vppr & 0xf0));
3226 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
3227 struct vmcs12 *vmcs12);
3230 * If from_vmentry is false, this is being called from state restore (either RSM
3231 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
3234 * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode
3235 * NVMX_VMENTRY_VMFAIL: Consistency check VMFail
3236 * NVMX_VMENTRY_VMEXIT: Consistency check VMExit
3237 * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error
3239 enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
3242 struct vcpu_vmx *vmx = to_vmx(vcpu);
3243 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3244 enum vm_entry_failure_code entry_failure_code;
3245 bool evaluate_pending_interrupts;
3246 u32 exit_reason, failed_index;
3248 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
3249 kvm_vcpu_flush_tlb_current(vcpu);
3251 evaluate_pending_interrupts = exec_controls_get(vmx) &
3252 (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING);
3253 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
3254 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
3256 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
3257 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
3258 if (kvm_mpx_supported() &&
3259 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
3260 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3263 * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and*
3264 * nested early checks are disabled. In the event of a "late" VM-Fail,
3265 * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its
3266 * software model to the pre-VMEntry host state. When EPT is disabled,
3267 * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes
3268 * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing
3269 * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to
3270 * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested
3271 * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is
3272 * guaranteed to be overwritten with a shadow CR3 prior to re-entering
3273 * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as
3274 * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks
3275 * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail
3276 * path would need to manually save/restore vmcs01.GUEST_CR3.
3278 if (!enable_ept && !nested_early_check)
3279 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3281 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
3283 prepare_vmcs02_early(vmx, vmcs12);
3286 if (unlikely(!nested_get_vmcs12_pages(vcpu)))
3287 return NVMX_VMENTRY_KVM_INTERNAL_ERROR;
3289 if (nested_vmx_check_vmentry_hw(vcpu)) {
3290 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3291 return NVMX_VMENTRY_VMFAIL;
3294 if (nested_vmx_check_guest_state(vcpu, vmcs12,
3295 &entry_failure_code)) {
3296 exit_reason = EXIT_REASON_INVALID_STATE;
3297 vmcs12->exit_qualification = entry_failure_code;
3298 goto vmentry_fail_vmexit;
3302 enter_guest_mode(vcpu);
3303 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
3304 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
3306 if (prepare_vmcs02(vcpu, vmcs12, &entry_failure_code)) {
3307 exit_reason = EXIT_REASON_INVALID_STATE;
3308 vmcs12->exit_qualification = entry_failure_code;
3309 goto vmentry_fail_vmexit_guest_mode;
3313 failed_index = nested_vmx_load_msr(vcpu,
3314 vmcs12->vm_entry_msr_load_addr,
3315 vmcs12->vm_entry_msr_load_count);
3317 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
3318 vmcs12->exit_qualification = failed_index;
3319 goto vmentry_fail_vmexit_guest_mode;
3323 * The MMU is not initialized to point at the right entities yet and
3324 * "get pages" would need to read data from the guest (i.e. we will
3325 * need to perform gpa to hpa translation). Request a call
3326 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
3327 * have already been set at vmentry time and should not be reset.
3329 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
3333 * If L1 had a pending IRQ/NMI until it executed
3334 * VMLAUNCH/VMRESUME which wasn't delivered because it was
3335 * disallowed (e.g. interrupts disabled), L0 needs to
3336 * evaluate if this pending event should cause an exit from L2
3337 * to L1 or delivered directly to L2 (e.g. In case L1 don't
3338 * intercept EXTERNAL_INTERRUPT).
3340 * Usually this would be handled by the processor noticing an
3341 * IRQ/NMI window request, or checking RVI during evaluation of
3342 * pending virtual interrupts. However, this setting was done
3343 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
3344 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
3346 if (unlikely(evaluate_pending_interrupts))
3347 kvm_make_request(KVM_REQ_EVENT, vcpu);
3350 * Do not start the preemption timer hrtimer until after we know
3351 * we are successful, so that only nested_vmx_vmexit needs to cancel
3354 vmx->nested.preemption_timer_expired = false;
3355 if (nested_cpu_has_preemption_timer(vmcs12))
3356 vmx_start_preemption_timer(vcpu);
3359 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
3360 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
3361 * returned as far as L1 is concerned. It will only return (and set
3362 * the success flag) when L2 exits (see nested_vmx_vmexit()).
3364 return NVMX_VMENTRY_SUCCESS;
3367 * A failed consistency check that leads to a VMExit during L1's
3368 * VMEnter to L2 is a variation of a normal VMexit, as explained in
3369 * 26.7 "VM-entry failures during or after loading guest state".
3371 vmentry_fail_vmexit_guest_mode:
3372 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
3373 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
3374 leave_guest_mode(vcpu);
3376 vmentry_fail_vmexit:
3377 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
3380 return NVMX_VMENTRY_VMEXIT;
3382 load_vmcs12_host_state(vcpu, vmcs12);
3383 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
3384 if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
3385 vmx->nested.need_vmcs12_to_shadow_sync = true;
3386 return NVMX_VMENTRY_VMEXIT;
3390 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
3391 * for running an L2 nested guest.
3393 static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
3395 struct vmcs12 *vmcs12;
3396 enum nvmx_vmentry_status status;
3397 struct vcpu_vmx *vmx = to_vmx(vcpu);
3398 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
3399 enum nested_evmptrld_status evmptrld_status;
3401 if (!nested_vmx_check_permission(vcpu))
3404 evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch);
3405 if (evmptrld_status == EVMPTRLD_ERROR) {
3406 kvm_queue_exception(vcpu, UD_VECTOR);
3408 } else if (evmptrld_status == EVMPTRLD_VMFAIL) {
3409 return nested_vmx_failInvalid(vcpu);
3412 if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
3413 return nested_vmx_failInvalid(vcpu);
3415 vmcs12 = get_vmcs12(vcpu);
3418 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
3419 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
3420 * rather than RFLAGS.ZF, and no error number is stored to the
3421 * VM-instruction error field.
3423 if (vmcs12->hdr.shadow_vmcs)
3424 return nested_vmx_failInvalid(vcpu);
3426 if (vmx->nested.hv_evmcs) {
3427 copy_enlightened_to_vmcs12(vmx);
3428 /* Enlightened VMCS doesn't have launch state */
3429 vmcs12->launch_state = !launch;
3430 } else if (enable_shadow_vmcs) {
3431 copy_shadow_to_vmcs12(vmx);
3435 * The nested entry process starts with enforcing various prerequisites
3436 * on vmcs12 as required by the Intel SDM, and act appropriately when
3437 * they fail: As the SDM explains, some conditions should cause the
3438 * instruction to fail, while others will cause the instruction to seem
3439 * to succeed, but return an EXIT_REASON_INVALID_STATE.
3440 * To speed up the normal (success) code path, we should avoid checking
3441 * for misconfigurations which will anyway be caught by the processor
3442 * when using the merged vmcs02.
3444 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
3445 return nested_vmx_failValid(vcpu,
3446 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
3448 if (vmcs12->launch_state == launch)
3449 return nested_vmx_failValid(vcpu,
3450 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
3451 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
3453 if (nested_vmx_check_controls(vcpu, vmcs12))
3454 return nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3456 if (nested_vmx_check_host_state(vcpu, vmcs12))
3457 return nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
3460 * We're finally done with prerequisite checking, and can start with
3463 vmx->nested.nested_run_pending = 1;
3464 status = nested_vmx_enter_non_root_mode(vcpu, true);
3465 if (unlikely(status != NVMX_VMENTRY_SUCCESS))
3466 goto vmentry_failed;
3468 /* Hide L1D cache contents from the nested guest. */
3469 vmx->vcpu.arch.l1tf_flush_l1d = true;
3472 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
3473 * also be used as part of restoring nVMX state for
3474 * snapshot restore (migration).
3476 * In this flow, it is assumed that vmcs12 cache was
3477 * trasferred as part of captured nVMX state and should
3478 * therefore not be read from guest memory (which may not
3479 * exist on destination host yet).
3481 nested_cache_shadow_vmcs12(vcpu, vmcs12);
3484 * If we're entering a halted L2 vcpu and the L2 vcpu won't be
3485 * awakened by event injection or by an NMI-window VM-exit or
3486 * by an interrupt-window VM-exit, halt the vcpu.
3488 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
3489 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
3490 !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_NMI_WINDOW_EXITING) &&
3491 !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_INTR_WINDOW_EXITING) &&
3492 (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
3493 vmx->nested.nested_run_pending = 0;
3494 return kvm_vcpu_halt(vcpu);
3499 vmx->nested.nested_run_pending = 0;
3500 if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR)
3502 if (status == NVMX_VMENTRY_VMEXIT)
3504 WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL);
3505 return nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
3509 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
3510 * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK).
3511 * This function returns the new value we should put in vmcs12.guest_cr0.
3512 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
3513 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
3514 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
3515 * didn't trap the bit, because if L1 did, so would L0).
3516 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
3517 * been modified by L2, and L1 knows it. So just leave the old value of
3518 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
3519 * isn't relevant, because if L0 traps this bit it can set it to anything.
3520 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
3521 * changed these bits, and therefore they need to be updated, but L0
3522 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
3523 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
3525 static inline unsigned long
3526 vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3529 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
3530 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
3531 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
3532 vcpu->arch.cr0_guest_owned_bits));
3535 static inline unsigned long
3536 vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3539 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
3540 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
3541 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
3542 vcpu->arch.cr4_guest_owned_bits));
3545 static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
3546 struct vmcs12 *vmcs12)
3551 if (vcpu->arch.exception.injected) {
3552 nr = vcpu->arch.exception.nr;
3553 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3555 if (kvm_exception_is_soft(nr)) {
3556 vmcs12->vm_exit_instruction_len =
3557 vcpu->arch.event_exit_inst_len;
3558 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
3560 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
3562 if (vcpu->arch.exception.has_error_code) {
3563 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
3564 vmcs12->idt_vectoring_error_code =
3565 vcpu->arch.exception.error_code;
3568 vmcs12->idt_vectoring_info_field = idt_vectoring;
3569 } else if (vcpu->arch.nmi_injected) {
3570 vmcs12->idt_vectoring_info_field =
3571 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
3572 } else if (vcpu->arch.interrupt.injected) {
3573 nr = vcpu->arch.interrupt.nr;
3574 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
3576 if (vcpu->arch.interrupt.soft) {
3577 idt_vectoring |= INTR_TYPE_SOFT_INTR;
3578 vmcs12->vm_entry_instruction_len =
3579 vcpu->arch.event_exit_inst_len;
3581 idt_vectoring |= INTR_TYPE_EXT_INTR;
3583 vmcs12->idt_vectoring_info_field = idt_vectoring;
3588 void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
3590 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3594 * Don't need to mark the APIC access page dirty; it is never
3595 * written to by the CPU during APIC virtualization.
3598 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
3599 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
3600 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3603 if (nested_cpu_has_posted_intr(vmcs12)) {
3604 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
3605 kvm_vcpu_mark_page_dirty(vcpu, gfn);
3609 static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
3611 struct vcpu_vmx *vmx = to_vmx(vcpu);
3616 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
3619 vmx->nested.pi_pending = false;
3620 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
3623 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
3624 if (max_irr != 256) {
3625 vapic_page = vmx->nested.virtual_apic_map.hva;
3629 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
3630 vapic_page, &max_irr);
3631 status = vmcs_read16(GUEST_INTR_STATUS);
3632 if ((u8)max_irr > ((u8)status & 0xff)) {
3634 status |= (u8)max_irr;
3635 vmcs_write16(GUEST_INTR_STATUS, status);
3639 nested_mark_vmcs12_pages_dirty(vcpu);
3642 static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3643 unsigned long exit_qual)
3645 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3646 unsigned int nr = vcpu->arch.exception.nr;
3647 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3649 if (vcpu->arch.exception.has_error_code) {
3650 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3651 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3654 if (kvm_exception_is_soft(nr))
3655 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3657 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3659 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3660 vmx_get_nmi_mask(vcpu))
3661 intr_info |= INTR_INFO_UNBLOCK_NMI;
3663 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3667 * Returns true if a debug trap is pending delivery.
3669 * In KVM, debug traps bear an exception payload. As such, the class of a #DB
3670 * exception may be inferred from the presence of an exception payload.
3672 static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu)
3674 return vcpu->arch.exception.pending &&
3675 vcpu->arch.exception.nr == DB_VECTOR &&
3676 vcpu->arch.exception.payload;
3680 * Certain VM-exits set the 'pending debug exceptions' field to indicate a
3681 * recognized #DB (data or single-step) that has yet to be delivered. Since KVM
3682 * represents these debug traps with a payload that is said to be compatible
3683 * with the 'pending debug exceptions' field, write the payload to the VMCS
3684 * field if a VM-exit is delivered before the debug trap.
3686 static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu)
3688 if (vmx_pending_dbg_trap(vcpu))
3689 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
3690 vcpu->arch.exception.payload);
3693 static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)
3695 return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
3696 to_vmx(vcpu)->nested.preemption_timer_expired;
3699 static int vmx_check_nested_events(struct kvm_vcpu *vcpu)
3701 struct vcpu_vmx *vmx = to_vmx(vcpu);
3702 unsigned long exit_qual;
3703 bool block_nested_events =
3704 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
3705 bool mtf_pending = vmx->nested.mtf_pending;
3706 struct kvm_lapic *apic = vcpu->arch.apic;
3709 * Clear the MTF state. If a higher priority VM-exit is delivered first,
3710 * this state is discarded.
3712 if (!block_nested_events)
3713 vmx->nested.mtf_pending = false;
3715 if (lapic_in_kernel(vcpu) &&
3716 test_bit(KVM_APIC_INIT, &apic->pending_events)) {
3717 if (block_nested_events)
3719 nested_vmx_update_pending_dbg(vcpu);
3720 clear_bit(KVM_APIC_INIT, &apic->pending_events);
3721 nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0);
3726 * Process any exceptions that are not debug traps before MTF.
3728 if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) {
3729 if (block_nested_events)
3731 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3733 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3738 if (block_nested_events)
3740 nested_vmx_update_pending_dbg(vcpu);
3741 nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0);
3745 if (vcpu->arch.exception.pending) {
3746 if (block_nested_events)
3748 if (!nested_vmx_check_exception(vcpu, &exit_qual))
3750 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
3754 if (nested_vmx_preemption_timer_pending(vcpu)) {
3755 if (block_nested_events)
3757 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
3761 if (vcpu->arch.smi_pending && !is_smm(vcpu)) {
3762 if (block_nested_events)
3767 if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) {
3768 if (block_nested_events)
3770 if (!nested_exit_on_nmi(vcpu))
3773 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
3774 NMI_VECTOR | INTR_TYPE_NMI_INTR |
3775 INTR_INFO_VALID_MASK, 0);
3777 * The NMI-triggered VM exit counts as injection:
3778 * clear this one and block further NMIs.
3780 vcpu->arch.nmi_pending = 0;
3781 vmx_set_nmi_mask(vcpu, true);
3785 if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) {
3786 if (block_nested_events)
3788 if (!nested_exit_on_intr(vcpu))
3790 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
3795 vmx_complete_nested_posted_interrupt(vcpu);
3799 static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
3802 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
3805 if (ktime_to_ns(remaining) <= 0)
3808 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
3809 do_div(value, 1000000);
3810 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
3813 static bool is_vmcs12_ext_field(unsigned long field)
3816 case GUEST_ES_SELECTOR:
3817 case GUEST_CS_SELECTOR:
3818 case GUEST_SS_SELECTOR:
3819 case GUEST_DS_SELECTOR:
3820 case GUEST_FS_SELECTOR:
3821 case GUEST_GS_SELECTOR:
3822 case GUEST_LDTR_SELECTOR:
3823 case GUEST_TR_SELECTOR:
3824 case GUEST_ES_LIMIT:
3825 case GUEST_CS_LIMIT:
3826 case GUEST_SS_LIMIT:
3827 case GUEST_DS_LIMIT:
3828 case GUEST_FS_LIMIT:
3829 case GUEST_GS_LIMIT:
3830 case GUEST_LDTR_LIMIT:
3831 case GUEST_TR_LIMIT:
3832 case GUEST_GDTR_LIMIT:
3833 case GUEST_IDTR_LIMIT:
3834 case GUEST_ES_AR_BYTES:
3835 case GUEST_DS_AR_BYTES:
3836 case GUEST_FS_AR_BYTES:
3837 case GUEST_GS_AR_BYTES:
3838 case GUEST_LDTR_AR_BYTES:
3839 case GUEST_TR_AR_BYTES:
3846 case GUEST_LDTR_BASE:
3848 case GUEST_GDTR_BASE:
3849 case GUEST_IDTR_BASE:
3850 case GUEST_PENDING_DBG_EXCEPTIONS:
3860 static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3861 struct vmcs12 *vmcs12)
3863 struct vcpu_vmx *vmx = to_vmx(vcpu);
3865 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
3866 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
3867 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
3868 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
3869 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
3870 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
3871 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
3872 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
3873 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
3874 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
3875 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
3876 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
3877 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
3878 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
3879 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
3880 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
3881 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
3882 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
3883 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
3884 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
3885 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
3886 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
3887 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
3888 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
3889 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
3890 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
3891 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
3892 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
3893 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
3894 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
3895 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
3896 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
3897 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
3898 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
3899 vmcs12->guest_pending_dbg_exceptions =
3900 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
3901 if (kvm_mpx_supported())
3902 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
3904 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false;
3907 static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu,
3908 struct vmcs12 *vmcs12)
3910 struct vcpu_vmx *vmx = to_vmx(vcpu);
3913 if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare)
3917 WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01);
3920 vmx->loaded_vmcs = &vmx->nested.vmcs02;
3921 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01);
3923 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
3925 vmx->loaded_vmcs = &vmx->vmcs01;
3926 vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02);
3931 * Update the guest state fields of vmcs12 to reflect changes that
3932 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
3933 * VM-entry controls is also updated, since this is really a guest
3936 static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
3938 struct vcpu_vmx *vmx = to_vmx(vcpu);
3940 if (vmx->nested.hv_evmcs)
3941 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
3943 vmx->nested.need_sync_vmcs02_to_vmcs12_rare = !vmx->nested.hv_evmcs;
3945 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
3946 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
3948 vmcs12->guest_rsp = kvm_rsp_read(vcpu);
3949 vmcs12->guest_rip = kvm_rip_read(vcpu);
3950 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
3952 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
3953 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
3955 vmcs12->guest_interruptibility_info =
3956 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3958 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
3959 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
3961 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
3963 if (nested_cpu_has_preemption_timer(vmcs12) &&
3964 vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
3965 vmcs12->vmx_preemption_timer_value =
3966 vmx_get_preemption_timer_value(vcpu);
3969 * In some cases (usually, nested EPT), L2 is allowed to change its
3970 * own CR3 without exiting. If it has changed it, we must keep it.
3971 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
3972 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
3974 * Additionally, restore L2's PDPTR to vmcs12.
3977 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
3978 if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) {
3979 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
3980 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
3981 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
3982 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
3986 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
3988 if (nested_cpu_has_vid(vmcs12))
3989 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
3991 vmcs12->vm_entry_controls =
3992 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
3993 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
3995 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS)
3996 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
3998 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
3999 vmcs12->guest_ia32_efer = vcpu->arch.efer;
4003 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
4004 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
4005 * and this function updates it to reflect the changes to the guest state while
4006 * L2 was running (and perhaps made some exits which were handled directly by L0
4007 * without going back to L1), and to reflect the exit reason.
4008 * Note that we do not have to copy here all VMCS fields, just those that
4009 * could have changed by the L2 guest or the exit - i.e., the guest-state and
4010 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
4011 * which already writes to vmcs12 directly.
4013 static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
4014 u32 vm_exit_reason, u32 exit_intr_info,
4015 unsigned long exit_qualification)
4017 /* update exit information fields: */
4018 vmcs12->vm_exit_reason = vm_exit_reason;
4019 vmcs12->exit_qualification = exit_qualification;
4020 vmcs12->vm_exit_intr_info = exit_intr_info;
4022 vmcs12->idt_vectoring_info_field = 0;
4023 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
4024 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4026 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
4027 vmcs12->launch_state = 1;
4029 /* vm_entry_intr_info_field is cleared on exit. Emulate this
4030 * instead of reading the real value. */
4031 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
4034 * Transfer the event that L0 or L1 may wanted to inject into
4035 * L2 to IDT_VECTORING_INFO_FIELD.
4037 vmcs12_save_pending_event(vcpu, vmcs12);
4040 * According to spec, there's no need to store the guest's
4041 * MSRs if the exit is due to a VM-entry failure that occurs
4042 * during or after loading the guest state. Since this exit
4043 * does not fall in that category, we need to save the MSRs.
4045 if (nested_vmx_store_msr(vcpu,
4046 vmcs12->vm_exit_msr_store_addr,
4047 vmcs12->vm_exit_msr_store_count))
4048 nested_vmx_abort(vcpu,
4049 VMX_ABORT_SAVE_GUEST_MSR_FAIL);
4053 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
4054 * preserved above and would only end up incorrectly in L1.
4056 vcpu->arch.nmi_injected = false;
4057 kvm_clear_exception_queue(vcpu);
4058 kvm_clear_interrupt_queue(vcpu);
4062 * A part of what we need to when the nested L2 guest exits and we want to
4063 * run its L1 parent, is to reset L1's guest state to the host state specified
4065 * This function is to be called not only on normal nested exit, but also on
4066 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
4067 * Failures During or After Loading Guest State").
4068 * This function should be called when the active VMCS is L1's (vmcs01).
4070 static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
4071 struct vmcs12 *vmcs12)
4073 enum vm_entry_failure_code ignored;
4074 struct kvm_segment seg;
4076 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
4077 vcpu->arch.efer = vmcs12->host_ia32_efer;
4078 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4079 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
4081 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
4082 vmx_set_efer(vcpu, vcpu->arch.efer);
4084 kvm_rsp_write(vcpu, vmcs12->host_rsp);
4085 kvm_rip_write(vcpu, vmcs12->host_rip);
4086 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
4087 vmx_set_interrupt_shadow(vcpu, 0);
4090 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
4091 * actually changed, because vmx_set_cr0 refers to efer set above.
4093 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
4094 * (KVM doesn't change it);
4096 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
4097 vmx_set_cr0(vcpu, vmcs12->host_cr0);
4099 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
4100 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4101 vmx_set_cr4(vcpu, vmcs12->host_cr4);
4103 nested_ept_uninit_mmu_context(vcpu);
4106 * Only PDPTE load can fail as the value of cr3 was checked on entry and
4107 * couldn't have changed.
4109 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &ignored))
4110 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
4113 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
4115 nested_vmx_transition_tlb_flush(vcpu, vmcs12, false);
4117 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
4118 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
4119 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
4120 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
4121 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
4122 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
4123 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
4125 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
4126 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
4127 vmcs_write64(GUEST_BNDCFGS, 0);
4129 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
4130 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
4131 vcpu->arch.pat = vmcs12->host_ia32_pat;
4133 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
4134 WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL,
4135 vmcs12->host_ia32_perf_global_ctrl));
4137 /* Set L1 segment info according to Intel SDM
4138 27.5.2 Loading Host Segment and Descriptor-Table Registers */
4139 seg = (struct kvm_segment) {
4141 .limit = 0xFFFFFFFF,
4142 .selector = vmcs12->host_cs_selector,
4148 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
4152 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
4153 seg = (struct kvm_segment) {
4155 .limit = 0xFFFFFFFF,
4162 seg.selector = vmcs12->host_ds_selector;
4163 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
4164 seg.selector = vmcs12->host_es_selector;
4165 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
4166 seg.selector = vmcs12->host_ss_selector;
4167 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
4168 seg.selector = vmcs12->host_fs_selector;
4169 seg.base = vmcs12->host_fs_base;
4170 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
4171 seg.selector = vmcs12->host_gs_selector;
4172 seg.base = vmcs12->host_gs_base;
4173 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
4174 seg = (struct kvm_segment) {
4175 .base = vmcs12->host_tr_base,
4177 .selector = vmcs12->host_tr_selector,
4181 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
4183 kvm_set_dr(vcpu, 7, 0x400);
4184 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4186 if (cpu_has_vmx_msr_bitmap())
4187 vmx_update_msr_bitmap(vcpu);
4189 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
4190 vmcs12->vm_exit_msr_load_count))
4191 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4194 static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
4196 struct shared_msr_entry *efer_msr;
4199 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
4200 return vmcs_read64(GUEST_IA32_EFER);
4202 if (cpu_has_load_ia32_efer())
4205 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
4206 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
4207 return vmx->msr_autoload.guest.val[i].value;
4210 efer_msr = find_msr_entry(vmx, MSR_EFER);
4212 return efer_msr->data;
4217 static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
4219 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4220 struct vcpu_vmx *vmx = to_vmx(vcpu);
4221 struct vmx_msr_entry g, h;
4225 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
4227 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
4229 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
4230 * as vmcs01.GUEST_DR7 contains a userspace defined value
4231 * and vcpu->arch.dr7 is not squirreled away before the
4232 * nested VMENTER (not worth adding a variable in nested_vmx).
4234 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
4235 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
4237 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
4241 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
4242 * handle a variety of side effects to KVM's software model.
4244 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
4246 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
4247 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
4249 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
4250 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
4252 nested_ept_uninit_mmu_context(vcpu);
4253 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4254 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
4257 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
4258 * from vmcs01 (if necessary). The PDPTRs are not loaded on
4259 * VMFail, like everything else we just need to ensure our
4260 * software model is up-to-date.
4262 if (enable_ept && is_pae_paging(vcpu))
4263 ept_save_pdptrs(vcpu);
4265 kvm_mmu_reset_context(vcpu);
4267 if (cpu_has_vmx_msr_bitmap())
4268 vmx_update_msr_bitmap(vcpu);
4271 * This nasty bit of open coding is a compromise between blindly
4272 * loading L1's MSRs using the exit load lists (incorrect emulation
4273 * of VMFail), leaving the nested VM's MSRs in the software model
4274 * (incorrect behavior) and snapshotting the modified MSRs (too
4275 * expensive since the lists are unbound by hardware). For each
4276 * MSR that was (prematurely) loaded from the nested VMEntry load
4277 * list, reload it from the exit load list if it exists and differs
4278 * from the guest value. The intent is to stuff host state as
4279 * silently as possible, not to fully process the exit load list.
4281 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
4282 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
4283 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
4284 pr_debug_ratelimited(
4285 "%s read MSR index failed (%u, 0x%08llx)\n",
4290 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
4291 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
4292 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
4293 pr_debug_ratelimited(
4294 "%s read MSR failed (%u, 0x%08llx)\n",
4298 if (h.index != g.index)
4300 if (h.value == g.value)
4303 if (nested_vmx_load_msr_check(vcpu, &h)) {
4304 pr_debug_ratelimited(
4305 "%s check failed (%u, 0x%x, 0x%x)\n",
4306 __func__, j, h.index, h.reserved);
4310 if (kvm_set_msr(vcpu, h.index, h.value)) {
4311 pr_debug_ratelimited(
4312 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
4313 __func__, j, h.index, h.value);
4322 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
4326 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
4327 * and modify vmcs12 to make it see what it would expect to see there if
4328 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
4330 void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
4331 u32 exit_intr_info, unsigned long exit_qualification)
4333 struct vcpu_vmx *vmx = to_vmx(vcpu);
4334 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4336 /* trying to cancel vmlaunch/vmresume is a bug */
4337 WARN_ON_ONCE(vmx->nested.nested_run_pending);
4339 /* Service the TLB flush request for L2 before switching to L1. */
4340 if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu))
4341 kvm_vcpu_flush_tlb_current(vcpu);
4343 leave_guest_mode(vcpu);
4345 if (nested_cpu_has_preemption_timer(vmcs12))
4346 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
4348 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)
4349 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
4351 if (likely(!vmx->fail)) {
4352 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
4354 if (vm_exit_reason != -1)
4355 prepare_vmcs12(vcpu, vmcs12, vm_exit_reason,
4356 exit_intr_info, exit_qualification);
4359 * Must happen outside of sync_vmcs02_to_vmcs12() as it will
4360 * also be used to capture vmcs12 cache as part of
4361 * capturing nVMX state for snapshot (migration).
4363 * Otherwise, this flush will dirty guest memory at a
4364 * point it is already assumed by user-space to be
4367 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
4370 * The only expected VM-instruction error is "VM entry with
4371 * invalid control field(s)." Anything else indicates a
4372 * problem with L0. And we should never get here with a
4373 * VMFail of any type if early consistency checks are enabled.
4375 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
4376 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4377 WARN_ON_ONCE(nested_early_check);
4380 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
4382 /* Update any VMCS fields that might have changed while L2 ran */
4383 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
4384 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
4385 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
4386 if (vmx->nested.l1_tpr_threshold != -1)
4387 vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold);
4389 if (kvm_has_tsc_control)
4390 decache_tsc_multiplier(vmx);
4392 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
4393 vmx->nested.change_vmcs01_virtual_apic_mode = false;
4394 vmx_set_virtual_apic_mode(vcpu);
4397 /* Unpin physical memory we referred to in vmcs02 */
4398 if (vmx->nested.apic_access_page) {
4399 kvm_release_page_clean(vmx->nested.apic_access_page);
4400 vmx->nested.apic_access_page = NULL;
4402 kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true);
4403 kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true);
4404 vmx->nested.pi_desc = NULL;
4406 if (vmx->nested.reload_vmcs01_apic_access_page) {
4407 vmx->nested.reload_vmcs01_apic_access_page = false;
4408 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
4411 if ((vm_exit_reason != -1) &&
4412 (enable_shadow_vmcs || vmx->nested.hv_evmcs))
4413 vmx->nested.need_vmcs12_to_shadow_sync = true;
4415 /* in case we halted in L2 */
4416 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
4418 if (likely(!vmx->fail)) {
4419 if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
4420 nested_exit_intr_ack_set(vcpu)) {
4421 int irq = kvm_cpu_get_interrupt(vcpu);
4423 vmcs12->vm_exit_intr_info = irq |
4424 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
4427 if (vm_exit_reason != -1)
4428 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
4429 vmcs12->exit_qualification,
4430 vmcs12->idt_vectoring_info_field,
4431 vmcs12->vm_exit_intr_info,
4432 vmcs12->vm_exit_intr_error_code,
4435 load_vmcs12_host_state(vcpu, vmcs12);
4441 * After an early L2 VM-entry failure, we're now back
4442 * in L1 which thinks it just finished a VMLAUNCH or
4443 * VMRESUME instruction, so we need to set the failure
4444 * flag and the VM-instruction error field of the VMCS
4445 * accordingly, and skip the emulated instruction.
4447 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
4450 * Restore L1's host state to KVM's software model. We're here
4451 * because a consistency check was caught by hardware, which
4452 * means some amount of guest state has been propagated to KVM's
4453 * model and needs to be unwound to the host's state.
4455 nested_vmx_restore_host_state(vcpu);
4461 * Decode the memory-address operand of a vmx instruction, as recorded on an
4462 * exit caused by such an instruction (run by a guest hypervisor).
4463 * On success, returns 0. When the operand is invalid, returns 1 and throws
4466 int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
4467 u32 vmx_instruction_info, bool wr, int len, gva_t *ret)
4471 struct kvm_segment s;
4474 * According to Vol. 3B, "Information for VM Exits Due to Instruction
4475 * Execution", on an exit, vmx_instruction_info holds most of the
4476 * addressing components of the operand. Only the displacement part
4477 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4478 * For how an actual address is calculated from all these components,
4479 * refer to Vol. 1, "Operand Addressing".
4481 int scaling = vmx_instruction_info & 3;
4482 int addr_size = (vmx_instruction_info >> 7) & 7;
4483 bool is_reg = vmx_instruction_info & (1u << 10);
4484 int seg_reg = (vmx_instruction_info >> 15) & 7;
4485 int index_reg = (vmx_instruction_info >> 18) & 0xf;
4486 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4487 int base_reg = (vmx_instruction_info >> 23) & 0xf;
4488 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4491 kvm_queue_exception(vcpu, UD_VECTOR);
4495 /* Addr = segment_base + offset */
4496 /* offset = base + [index * scale] + displacement */
4497 off = exit_qualification; /* holds the displacement */
4499 off = (gva_t)sign_extend64(off, 31);
4500 else if (addr_size == 0)
4501 off = (gva_t)sign_extend64(off, 15);
4503 off += kvm_register_read(vcpu, base_reg);
4505 off += kvm_register_read(vcpu, index_reg) << scaling;
4506 vmx_get_segment(vcpu, &s, seg_reg);
4509 * The effective address, i.e. @off, of a memory operand is truncated
4510 * based on the address size of the instruction. Note that this is
4511 * the *effective address*, i.e. the address prior to accounting for
4512 * the segment's base.
4514 if (addr_size == 1) /* 32 bit */
4516 else if (addr_size == 0) /* 16 bit */
4519 /* Checks for #GP/#SS exceptions. */
4521 if (is_long_mode(vcpu)) {
4523 * The virtual/linear address is never truncated in 64-bit
4524 * mode, e.g. a 32-bit address size can yield a 64-bit virtual
4525 * address when using FS/GS with a non-zero base.
4527 if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS)
4528 *ret = s.base + off;
4532 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
4533 * non-canonical form. This is the only check on the memory
4534 * destination for long mode!
4536 exn = is_noncanonical_address(*ret, vcpu);
4539 * When not in long mode, the virtual/linear address is
4540 * unconditionally truncated to 32 bits regardless of the
4543 *ret = (s.base + off) & 0xffffffff;
4545 /* Protected mode: apply checks for segment validity in the
4547 * - segment type check (#GP(0) may be thrown)
4548 * - usability check (#GP(0)/#SS(0))
4549 * - limit check (#GP(0)/#SS(0))
4552 /* #GP(0) if the destination operand is located in a
4553 * read-only data segment or any code segment.
4555 exn = ((s.type & 0xa) == 0 || (s.type & 8));
4557 /* #GP(0) if the source operand is located in an
4558 * execute-only code segment
4560 exn = ((s.type & 0xa) == 8);
4562 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
4565 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
4567 exn = (s.unusable != 0);
4570 * Protected mode: #GP(0)/#SS(0) if the memory operand is
4571 * outside the segment limit. All CPUs that support VMX ignore
4572 * limit checks for flat segments, i.e. segments with base==0,
4573 * limit==0xffffffff and of type expand-up data or code.
4575 if (!(s.base == 0 && s.limit == 0xffffffff &&
4576 ((s.type & 8) || !(s.type & 4))))
4577 exn = exn || ((u64)off + len - 1 > s.limit);
4580 kvm_queue_exception_e(vcpu,
4581 seg_reg == VCPU_SREG_SS ?
4582 SS_VECTOR : GP_VECTOR,
4590 void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
4592 struct vcpu_vmx *vmx;
4594 if (!nested_vmx_allowed(vcpu))
4598 if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) {
4599 vmx->nested.msrs.entry_ctls_high |=
4600 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4601 vmx->nested.msrs.exit_ctls_high |=
4602 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
4604 vmx->nested.msrs.entry_ctls_high &=
4605 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4606 vmx->nested.msrs.exit_ctls_high &=
4607 ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
4611 static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
4614 struct x86_exception e;
4616 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
4617 vmcs_read32(VMX_INSTRUCTION_INFO), false,
4618 sizeof(*vmpointer), &gva))
4621 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
4622 kvm_inject_emulated_page_fault(vcpu, &e);
4630 * Allocate a shadow VMCS and associate it with the currently loaded
4631 * VMCS, unless such a shadow VMCS already exists. The newly allocated
4632 * VMCS is also VMCLEARed, so that it is ready for use.
4634 static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
4636 struct vcpu_vmx *vmx = to_vmx(vcpu);
4637 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
4640 * We should allocate a shadow vmcs for vmcs01 only when L1
4641 * executes VMXON and free it when L1 executes VMXOFF.
4642 * As it is invalid to execute VMXON twice, we shouldn't reach
4643 * here when vmcs01 already have an allocated shadow vmcs.
4645 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
4647 if (!loaded_vmcs->shadow_vmcs) {
4648 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
4649 if (loaded_vmcs->shadow_vmcs)
4650 vmcs_clear(loaded_vmcs->shadow_vmcs);
4652 return loaded_vmcs->shadow_vmcs;
4655 static int enter_vmx_operation(struct kvm_vcpu *vcpu)
4657 struct vcpu_vmx *vmx = to_vmx(vcpu);
4660 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
4664 vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
4665 if (!vmx->nested.cached_vmcs12)
4666 goto out_cached_vmcs12;
4668 vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT);
4669 if (!vmx->nested.cached_shadow_vmcs12)
4670 goto out_cached_shadow_vmcs12;
4672 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
4673 goto out_shadow_vmcs;
4675 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
4676 HRTIMER_MODE_REL_PINNED);
4677 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
4679 vmx->nested.vpid02 = allocate_vpid();
4681 vmx->nested.vmcs02_initialized = false;
4682 vmx->nested.vmxon = true;
4684 if (vmx_pt_mode_is_host_guest()) {
4685 vmx->pt_desc.guest.ctl = 0;
4686 pt_update_intercept_for_msr(vmx);
4692 kfree(vmx->nested.cached_shadow_vmcs12);
4694 out_cached_shadow_vmcs12:
4695 kfree(vmx->nested.cached_vmcs12);
4698 free_loaded_vmcs(&vmx->nested.vmcs02);
4705 * Emulate the VMXON instruction.
4706 * Currently, we just remember that VMX is active, and do not save or even
4707 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
4708 * do not currently need to store anything in that guest-allocated memory
4709 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
4710 * argument is different from the VMXON pointer (which the spec says they do).
4712 static int handle_vmon(struct kvm_vcpu *vcpu)
4717 struct vcpu_vmx *vmx = to_vmx(vcpu);
4718 const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED
4719 | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
4722 * The Intel VMX Instruction Reference lists a bunch of bits that are
4723 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
4724 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
4725 * Otherwise, we should fail with #UD. But most faulting conditions
4726 * have already been checked by hardware, prior to the VM-exit for
4727 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
4728 * that bit set to 1 in non-root mode.
4730 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
4731 kvm_queue_exception(vcpu, UD_VECTOR);
4735 /* CPL=0 must be checked manually. */
4736 if (vmx_get_cpl(vcpu)) {
4737 kvm_inject_gp(vcpu, 0);
4741 if (vmx->nested.vmxon)
4742 return nested_vmx_failValid(vcpu,
4743 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
4745 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
4746 != VMXON_NEEDED_FEATURES) {
4747 kvm_inject_gp(vcpu, 0);
4751 if (nested_vmx_get_vmptr(vcpu, &vmptr))
4756 * The first 4 bytes of VMXON region contain the supported
4757 * VMCS revision identifier
4759 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
4760 * which replaces physical address width with 32
4762 if (!page_address_valid(vcpu, vmptr))
4763 return nested_vmx_failInvalid(vcpu);
4765 if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) ||
4766 revision != VMCS12_REVISION)
4767 return nested_vmx_failInvalid(vcpu);
4769 vmx->nested.vmxon_ptr = vmptr;
4770 ret = enter_vmx_operation(vcpu);
4774 return nested_vmx_succeed(vcpu);
4777 static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
4779 struct vcpu_vmx *vmx = to_vmx(vcpu);
4781 if (vmx->nested.current_vmptr == -1ull)
4784 copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu));
4786 if (enable_shadow_vmcs) {
4787 /* copy to memory all shadowed fields in case
4788 they were modified */
4789 copy_shadow_to_vmcs12(vmx);
4790 vmx_disable_shadow_vmcs(vmx);
4792 vmx->nested.posted_intr_nv = -1;
4794 /* Flush VMCS12 to guest memory */
4795 kvm_vcpu_write_guest_page(vcpu,
4796 vmx->nested.current_vmptr >> PAGE_SHIFT,
4797 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
4799 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
4801 vmx->nested.current_vmptr = -1ull;
4804 /* Emulate the VMXOFF instruction */
4805 static int handle_vmoff(struct kvm_vcpu *vcpu)
4807 if (!nested_vmx_check_permission(vcpu))
4812 /* Process a latched INIT during time CPU was in VMX operation */
4813 kvm_make_request(KVM_REQ_EVENT, vcpu);
4815 return nested_vmx_succeed(vcpu);
4818 /* Emulate the VMCLEAR instruction */
4819 static int handle_vmclear(struct kvm_vcpu *vcpu)
4821 struct vcpu_vmx *vmx = to_vmx(vcpu);
4826 if (!nested_vmx_check_permission(vcpu))
4829 if (nested_vmx_get_vmptr(vcpu, &vmptr))
4832 if (!page_address_valid(vcpu, vmptr))
4833 return nested_vmx_failValid(vcpu,
4834 VMXERR_VMCLEAR_INVALID_ADDRESS);
4836 if (vmptr == vmx->nested.vmxon_ptr)
4837 return nested_vmx_failValid(vcpu,
4838 VMXERR_VMCLEAR_VMXON_POINTER);
4841 * When Enlightened VMEntry is enabled on the calling CPU we treat
4842 * memory area pointer by vmptr as Enlightened VMCS (as there's no good
4843 * way to distinguish it from VMCS12) and we must not corrupt it by
4844 * writing to the non-existent 'launch_state' field. The area doesn't
4845 * have to be the currently active EVMCS on the calling CPU and there's
4846 * nothing KVM has to do to transition it from 'active' to 'non-active'
4847 * state. It is possible that the area will stay mapped as
4848 * vmx->nested.hv_evmcs but this shouldn't be a problem.
4850 if (likely(!vmx->nested.enlightened_vmcs_enabled ||
4851 !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) {
4852 if (vmptr == vmx->nested.current_vmptr)
4853 nested_release_vmcs12(vcpu);
4855 kvm_vcpu_write_guest(vcpu,
4856 vmptr + offsetof(struct vmcs12,
4858 &zero, sizeof(zero));
4861 return nested_vmx_succeed(vcpu);
4864 /* Emulate the VMLAUNCH instruction */
4865 static int handle_vmlaunch(struct kvm_vcpu *vcpu)
4867 return nested_vmx_run(vcpu, true);
4870 /* Emulate the VMRESUME instruction */
4871 static int handle_vmresume(struct kvm_vcpu *vcpu)
4874 return nested_vmx_run(vcpu, false);
4877 static int handle_vmread(struct kvm_vcpu *vcpu)
4879 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
4881 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
4882 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4883 struct vcpu_vmx *vmx = to_vmx(vcpu);
4884 struct x86_exception e;
4885 unsigned long field;
4891 if (!nested_vmx_check_permission(vcpu))
4895 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
4896 * any VMREAD sets the ALU flags for VMfailInvalid.
4898 if (vmx->nested.current_vmptr == -1ull ||
4899 (is_guest_mode(vcpu) &&
4900 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
4901 return nested_vmx_failInvalid(vcpu);
4903 /* Decode instruction info and find the field to read */
4904 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
4906 offset = vmcs_field_to_offset(field);
4908 return nested_vmx_failValid(vcpu,
4909 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
4911 if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field))
4912 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
4914 /* Read the field, zero-extended to a u64 value */
4915 value = vmcs12_read_any(vmcs12, field, offset);
4918 * Now copy part of this value to register or memory, as requested.
4919 * Note that the number of bits actually copied is 32 or 64 depending
4920 * on the guest's mode (32 or 64 bit), not on the given field's length.
4922 if (instr_info & BIT(10)) {
4923 kvm_register_writel(vcpu, (((instr_info) >> 3) & 0xf), value);
4925 len = is_64_bit_mode(vcpu) ? 8 : 4;
4926 if (get_vmx_mem_address(vcpu, exit_qualification,
4927 instr_info, true, len, &gva))
4929 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
4930 if (kvm_write_guest_virt_system(vcpu, gva, &value, len, &e)) {
4931 kvm_inject_emulated_page_fault(vcpu, &e);
4936 return nested_vmx_succeed(vcpu);
4939 static bool is_shadow_field_rw(unsigned long field)
4942 #define SHADOW_FIELD_RW(x, y) case x:
4943 #include "vmcs_shadow_fields.h"
4951 static bool is_shadow_field_ro(unsigned long field)
4954 #define SHADOW_FIELD_RO(x, y) case x:
4955 #include "vmcs_shadow_fields.h"
4963 static int handle_vmwrite(struct kvm_vcpu *vcpu)
4965 struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu)
4967 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
4968 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
4969 struct vcpu_vmx *vmx = to_vmx(vcpu);
4970 struct x86_exception e;
4971 unsigned long field;
4977 * The value to write might be 32 or 64 bits, depending on L1's long
4978 * mode, and eventually we need to write that into a field of several
4979 * possible lengths. The code below first zero-extends the value to 64
4980 * bit (value), and then copies only the appropriate number of
4981 * bits into the vmcs12 field.
4985 if (!nested_vmx_check_permission(vcpu))
4989 * In VMX non-root operation, when the VMCS-link pointer is -1ull,
4990 * any VMWRITE sets the ALU flags for VMfailInvalid.
4992 if (vmx->nested.current_vmptr == -1ull ||
4993 (is_guest_mode(vcpu) &&
4994 get_vmcs12(vcpu)->vmcs_link_pointer == -1ull))
4995 return nested_vmx_failInvalid(vcpu);
4997 if (instr_info & BIT(10))
4998 value = kvm_register_readl(vcpu, (((instr_info) >> 3) & 0xf));
5000 len = is_64_bit_mode(vcpu) ? 8 : 4;
5001 if (get_vmx_mem_address(vcpu, exit_qualification,
5002 instr_info, false, len, &gva))
5004 if (kvm_read_guest_virt(vcpu, gva, &value, len, &e)) {
5005 kvm_inject_emulated_page_fault(vcpu, &e);
5010 field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
5012 offset = vmcs_field_to_offset(field);
5014 return nested_vmx_failValid(vcpu,
5015 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5018 * If the vCPU supports "VMWRITE to any supported field in the
5019 * VMCS," then the "read-only" fields are actually read/write.
5021 if (vmcs_field_readonly(field) &&
5022 !nested_cpu_has_vmwrite_any_field(vcpu))
5023 return nested_vmx_failValid(vcpu,
5024 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
5027 * Ensure vmcs12 is up-to-date before any VMWRITE that dirties
5028 * vmcs12, else we may crush a field or consume a stale value.
5030 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field))
5031 copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5034 * Some Intel CPUs intentionally drop the reserved bits of the AR byte
5035 * fields on VMWRITE. Emulate this behavior to ensure consistent KVM
5036 * behavior regardless of the underlying hardware, e.g. if an AR_BYTE
5037 * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD
5038 * from L1 will return a different value than VMREAD from L2 (L1 sees
5039 * the stripped down value, L2 sees the full value as stored by KVM).
5041 if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES)
5044 vmcs12_write_any(vmcs12, field, offset, value);
5047 * Do not track vmcs12 dirty-state if in guest-mode as we actually
5048 * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated
5049 * by L1 without a vmexit are always updated in the vmcs02, i.e. don't
5050 * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path.
5052 if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) {
5054 * L1 can read these fields without exiting, ensure the
5055 * shadow VMCS is up-to-date.
5057 if (enable_shadow_vmcs && is_shadow_field_ro(field)) {
5059 vmcs_load(vmx->vmcs01.shadow_vmcs);
5061 __vmcs_writel(field, value);
5063 vmcs_clear(vmx->vmcs01.shadow_vmcs);
5064 vmcs_load(vmx->loaded_vmcs->vmcs);
5067 vmx->nested.dirty_vmcs12 = true;
5070 return nested_vmx_succeed(vcpu);
5073 static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
5075 vmx->nested.current_vmptr = vmptr;
5076 if (enable_shadow_vmcs) {
5077 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS);
5078 vmcs_write64(VMCS_LINK_POINTER,
5079 __pa(vmx->vmcs01.shadow_vmcs));
5080 vmx->nested.need_vmcs12_to_shadow_sync = true;
5082 vmx->nested.dirty_vmcs12 = true;
5085 /* Emulate the VMPTRLD instruction */
5086 static int handle_vmptrld(struct kvm_vcpu *vcpu)
5088 struct vcpu_vmx *vmx = to_vmx(vcpu);
5091 if (!nested_vmx_check_permission(vcpu))
5094 if (nested_vmx_get_vmptr(vcpu, &vmptr))
5097 if (!page_address_valid(vcpu, vmptr))
5098 return nested_vmx_failValid(vcpu,
5099 VMXERR_VMPTRLD_INVALID_ADDRESS);
5101 if (vmptr == vmx->nested.vmxon_ptr)
5102 return nested_vmx_failValid(vcpu,
5103 VMXERR_VMPTRLD_VMXON_POINTER);
5105 /* Forbid normal VMPTRLD if Enlightened version was used */
5106 if (vmx->nested.hv_evmcs)
5109 if (vmx->nested.current_vmptr != vmptr) {
5110 struct kvm_host_map map;
5111 struct vmcs12 *new_vmcs12;
5113 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) {
5115 * Reads from an unbacked page return all 1s,
5116 * which means that the 32 bits located at the
5117 * given physical address won't match the required
5118 * VMCS12_REVISION identifier.
5120 return nested_vmx_failValid(vcpu,
5121 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5124 new_vmcs12 = map.hva;
5126 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
5127 (new_vmcs12->hdr.shadow_vmcs &&
5128 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
5129 kvm_vcpu_unmap(vcpu, &map, false);
5130 return nested_vmx_failValid(vcpu,
5131 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5134 nested_release_vmcs12(vcpu);
5137 * Load VMCS12 from guest memory since it is not already
5140 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
5141 kvm_vcpu_unmap(vcpu, &map, false);
5143 set_current_vmptr(vmx, vmptr);
5146 return nested_vmx_succeed(vcpu);
5149 /* Emulate the VMPTRST instruction */
5150 static int handle_vmptrst(struct kvm_vcpu *vcpu)
5152 unsigned long exit_qual = vmx_get_exit_qual(vcpu);
5153 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5154 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
5155 struct x86_exception e;
5158 if (!nested_vmx_check_permission(vcpu))
5161 if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
5164 if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
5165 true, sizeof(gpa_t), &gva))
5167 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
5168 if (kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr,
5169 sizeof(gpa_t), &e)) {
5170 kvm_inject_emulated_page_fault(vcpu, &e);
5173 return nested_vmx_succeed(vcpu);
5176 #define EPTP_PA_MASK GENMASK_ULL(51, 12)
5178 static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp)
5180 return VALID_PAGE(root_hpa) &&
5181 ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK));
5184 /* Emulate the INVEPT instruction */
5185 static int handle_invept(struct kvm_vcpu *vcpu)
5187 struct vcpu_vmx *vmx = to_vmx(vcpu);
5188 u32 vmx_instruction_info, types;
5189 unsigned long type, roots_to_free;
5190 struct kvm_mmu *mmu;
5192 struct x86_exception e;
5198 if (!(vmx->nested.msrs.secondary_ctls_high &
5199 SECONDARY_EXEC_ENABLE_EPT) ||
5200 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
5201 kvm_queue_exception(vcpu, UD_VECTOR);
5205 if (!nested_vmx_check_permission(vcpu))
5208 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5209 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5211 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
5213 if (type >= 32 || !(types & (1 << type)))
5214 return nested_vmx_failValid(vcpu,
5215 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5217 /* According to the Intel VMX instruction reference, the memory
5218 * operand is read even if it isn't needed (e.g., for type==global)
5220 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
5221 vmx_instruction_info, false, sizeof(operand), &gva))
5223 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
5224 kvm_inject_emulated_page_fault(vcpu, &e);
5229 * Nested EPT roots are always held through guest_mmu,
5232 mmu = &vcpu->arch.guest_mmu;
5235 case VMX_EPT_EXTENT_CONTEXT:
5236 if (!nested_vmx_check_eptp(vcpu, operand.eptp))
5237 return nested_vmx_failValid(vcpu,
5238 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5241 if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd,
5243 roots_to_free |= KVM_MMU_ROOT_CURRENT;
5245 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
5246 if (nested_ept_root_matches(mmu->prev_roots[i].hpa,
5247 mmu->prev_roots[i].pgd,
5249 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
5252 case VMX_EPT_EXTENT_GLOBAL:
5253 roots_to_free = KVM_MMU_ROOTS_ALL;
5261 kvm_mmu_free_roots(vcpu, mmu, roots_to_free);
5263 return nested_vmx_succeed(vcpu);
5266 static int handle_invvpid(struct kvm_vcpu *vcpu)
5268 struct vcpu_vmx *vmx = to_vmx(vcpu);
5269 u32 vmx_instruction_info;
5270 unsigned long type, types;
5272 struct x86_exception e;
5279 if (!(vmx->nested.msrs.secondary_ctls_high &
5280 SECONDARY_EXEC_ENABLE_VPID) ||
5281 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
5282 kvm_queue_exception(vcpu, UD_VECTOR);
5286 if (!nested_vmx_check_permission(vcpu))
5289 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5290 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5292 types = (vmx->nested.msrs.vpid_caps &
5293 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
5295 if (type >= 32 || !(types & (1 << type)))
5296 return nested_vmx_failValid(vcpu,
5297 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5299 /* according to the intel vmx instruction reference, the memory
5300 * operand is read even if it isn't needed (e.g., for type==global)
5302 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
5303 vmx_instruction_info, false, sizeof(operand), &gva))
5305 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
5306 kvm_inject_emulated_page_fault(vcpu, &e);
5309 if (operand.vpid >> 16)
5310 return nested_vmx_failValid(vcpu,
5311 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5313 vpid02 = nested_get_vpid02(vcpu);
5315 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
5316 if (!operand.vpid ||
5317 is_noncanonical_address(operand.gla, vcpu))
5318 return nested_vmx_failValid(vcpu,
5319 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5320 vpid_sync_vcpu_addr(vpid02, operand.gla);
5322 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
5323 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
5325 return nested_vmx_failValid(vcpu,
5326 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
5327 vpid_sync_context(vpid02);
5329 case VMX_VPID_EXTENT_ALL_CONTEXT:
5330 vpid_sync_context(vpid02);
5334 return kvm_skip_emulated_instruction(vcpu);
5338 * Sync the shadow page tables if EPT is disabled, L1 is invalidating
5339 * linear mappings for L2 (tagged with L2's VPID). Free all roots as
5340 * VPIDs are not tracked in the MMU role.
5342 * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share
5343 * an MMU when EPT is disabled.
5345 * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR.
5348 kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu,
5351 return nested_vmx_succeed(vcpu);
5354 static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
5355 struct vmcs12 *vmcs12)
5357 u32 index = kvm_rcx_read(vcpu);
5359 bool accessed_dirty;
5360 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5362 if (!nested_cpu_has_eptp_switching(vmcs12) ||
5363 !nested_cpu_has_ept(vmcs12))
5366 if (index >= VMFUNC_EPTP_ENTRIES)
5370 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
5371 &new_eptp, index * 8, 8))
5374 accessed_dirty = !!(new_eptp & VMX_EPTP_AD_ENABLE_BIT);
5377 * If the (L2) guest does a vmfunc to the currently
5378 * active ept pointer, we don't have to do anything else
5380 if (vmcs12->ept_pointer != new_eptp) {
5381 if (!nested_vmx_check_eptp(vcpu, new_eptp))
5384 kvm_mmu_unload(vcpu);
5385 mmu->ept_ad = accessed_dirty;
5386 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
5387 vmcs12->ept_pointer = new_eptp;
5389 * TODO: Check what's the correct approach in case
5390 * mmu reload fails. Currently, we just let the next
5391 * reload potentially fail
5393 kvm_mmu_reload(vcpu);
5399 static int handle_vmfunc(struct kvm_vcpu *vcpu)
5401 struct vcpu_vmx *vmx = to_vmx(vcpu);
5402 struct vmcs12 *vmcs12;
5403 u32 function = kvm_rax_read(vcpu);
5406 * VMFUNC is only supported for nested guests, but we always enable the
5407 * secondary control for simplicity; for non-nested mode, fake that we
5408 * didn't by injecting #UD.
5410 if (!is_guest_mode(vcpu)) {
5411 kvm_queue_exception(vcpu, UD_VECTOR);
5415 vmcs12 = get_vmcs12(vcpu);
5416 if ((vmcs12->vm_function_control & (1 << function)) == 0)
5421 if (nested_vmx_eptp_switching(vcpu, vmcs12))
5427 return kvm_skip_emulated_instruction(vcpu);
5430 nested_vmx_vmexit(vcpu, vmx->exit_reason,
5431 vmx_get_intr_info(vcpu),
5432 vmx_get_exit_qual(vcpu));
5437 * Return true if an IO instruction with the specified port and size should cause
5438 * a VM-exit into L1.
5440 bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
5443 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5444 gpa_t bitmap, last_bitmap;
5447 last_bitmap = (gpa_t)-1;
5452 bitmap = vmcs12->io_bitmap_a;
5453 else if (port < 0x10000)
5454 bitmap = vmcs12->io_bitmap_b;
5457 bitmap += (port & 0x7fff) / 8;
5459 if (last_bitmap != bitmap)
5460 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
5462 if (b & (1 << (port & 7)))
5467 last_bitmap = bitmap;
5473 static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
5474 struct vmcs12 *vmcs12)
5476 unsigned long exit_qualification;
5477 unsigned short port;
5480 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
5481 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
5483 exit_qualification = vmx_get_exit_qual(vcpu);
5485 port = exit_qualification >> 16;
5486 size = (exit_qualification & 7) + 1;
5488 return nested_vmx_check_io_bitmaps(vcpu, port, size);
5492 * Return 1 if we should exit from L2 to L1 to handle an MSR access,
5493 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5494 * disinterest in the current event (read or write a specific MSR) by using an
5495 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5497 static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
5498 struct vmcs12 *vmcs12, u32 exit_reason)
5500 u32 msr_index = kvm_rcx_read(vcpu);
5503 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
5507 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5508 * for the four combinations of read/write and low/high MSR numbers.
5509 * First we need to figure out which of the four to use:
5511 bitmap = vmcs12->msr_bitmap;
5512 if (exit_reason == EXIT_REASON_MSR_WRITE)
5514 if (msr_index >= 0xc0000000) {
5515 msr_index -= 0xc0000000;
5519 /* Then read the msr_index'th bit from this bitmap: */
5520 if (msr_index < 1024*8) {
5522 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
5524 return 1 & (b >> (msr_index & 7));
5526 return true; /* let L1 handle the wrong parameter */
5530 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5531 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5532 * intercept (via guest_host_mask etc.) the current event.
5534 static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5535 struct vmcs12 *vmcs12)
5537 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
5538 int cr = exit_qualification & 15;
5542 switch ((exit_qualification >> 4) & 3) {
5543 case 0: /* mov to cr */
5544 reg = (exit_qualification >> 8) & 15;
5545 val = kvm_register_readl(vcpu, reg);
5548 if (vmcs12->cr0_guest_host_mask &
5549 (val ^ vmcs12->cr0_read_shadow))
5553 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5557 if (vmcs12->cr4_guest_host_mask &
5558 (vmcs12->cr4_read_shadow ^ val))
5562 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5568 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5569 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5572 case 1: /* mov from cr */
5575 if (vmcs12->cpu_based_vm_exec_control &
5576 CPU_BASED_CR3_STORE_EXITING)
5580 if (vmcs12->cpu_based_vm_exec_control &
5581 CPU_BASED_CR8_STORE_EXITING)
5588 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5589 * cr0. Other attempted changes are ignored, with no exit.
5591 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5592 if (vmcs12->cr0_guest_host_mask & 0xe &
5593 (val ^ vmcs12->cr0_read_shadow))
5595 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5596 !(vmcs12->cr0_read_shadow & 0x1) &&
5604 static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
5605 struct vmcs12 *vmcs12, gpa_t bitmap)
5607 u32 vmx_instruction_info;
5608 unsigned long field;
5611 if (!nested_cpu_has_shadow_vmcs(vmcs12))
5614 /* Decode instruction info and find the field to access */
5615 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5616 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5618 /* Out-of-range fields always cause a VM exit from L2 to L1 */
5622 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
5625 return 1 & (b >> (field & 7));
5628 static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12)
5630 u32 entry_intr_info = vmcs12->vm_entry_intr_info_field;
5632 if (nested_cpu_has_mtf(vmcs12))
5636 * An MTF VM-exit may be injected into the guest by setting the
5637 * interruption-type to 7 (other event) and the vector field to 0. Such
5638 * is the case regardless of the 'monitor trap flag' VM-execution
5641 return entry_intr_info == (INTR_INFO_VALID_MASK
5642 | INTR_TYPE_OTHER_EVENT);
5646 * Return true if L0 wants to handle an exit from L2 regardless of whether or not
5647 * L1 wants the exit. Only call this when in is_guest_mode (L2).
5649 static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, u32 exit_reason)
5653 switch (exit_reason) {
5654 case EXIT_REASON_EXCEPTION_NMI:
5655 intr_info = vmx_get_intr_info(vcpu);
5656 if (is_nmi(intr_info))
5658 else if (is_page_fault(intr_info))
5659 return vcpu->arch.apf.host_apf_reason || !enable_ept;
5660 else if (is_debug(intr_info) &&
5662 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5664 else if (is_breakpoint(intr_info) &&
5665 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5668 case EXIT_REASON_EXTERNAL_INTERRUPT:
5670 case EXIT_REASON_MCE_DURING_VMENTRY:
5672 case EXIT_REASON_EPT_VIOLATION:
5674 * L0 always deals with the EPT violation. If nested EPT is
5675 * used, and the nested mmu code discovers that the address is
5676 * missing in the guest EPT table (EPT12), the EPT violation
5677 * will be injected with nested_ept_inject_page_fault()
5680 case EXIT_REASON_EPT_MISCONFIG:
5682 * L2 never uses directly L1's EPT, but rather L0's own EPT
5683 * table (shadow on EPT) or a merged EPT table that L0 built
5684 * (EPT on EPT). So any problems with the structure of the
5685 * table is L0's fault.
5688 case EXIT_REASON_PREEMPTION_TIMER:
5690 case EXIT_REASON_PML_FULL:
5691 /* We emulate PML support to L1. */
5693 case EXIT_REASON_VMFUNC:
5694 /* VM functions are emulated through L2->L0 vmexits. */
5696 case EXIT_REASON_ENCLS:
5697 /* SGX is never exposed to L1 */
5706 * Return 1 if L1 wants to intercept an exit from L2. Only call this when in
5707 * is_guest_mode (L2).
5709 static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, u32 exit_reason)
5711 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5714 switch (exit_reason) {
5715 case EXIT_REASON_EXCEPTION_NMI:
5716 intr_info = vmx_get_intr_info(vcpu);
5717 if (is_nmi(intr_info))
5719 else if (is_page_fault(intr_info))
5721 return vmcs12->exception_bitmap &
5722 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5723 case EXIT_REASON_EXTERNAL_INTERRUPT:
5724 return nested_exit_on_intr(vcpu);
5725 case EXIT_REASON_TRIPLE_FAULT:
5727 case EXIT_REASON_INTERRUPT_WINDOW:
5728 return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING);
5729 case EXIT_REASON_NMI_WINDOW:
5730 return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING);
5731 case EXIT_REASON_TASK_SWITCH:
5733 case EXIT_REASON_CPUID:
5735 case EXIT_REASON_HLT:
5736 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5737 case EXIT_REASON_INVD:
5739 case EXIT_REASON_INVLPG:
5740 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5741 case EXIT_REASON_RDPMC:
5742 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5743 case EXIT_REASON_RDRAND:
5744 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
5745 case EXIT_REASON_RDSEED:
5746 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
5747 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
5748 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5749 case EXIT_REASON_VMREAD:
5750 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5751 vmcs12->vmread_bitmap);
5752 case EXIT_REASON_VMWRITE:
5753 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
5754 vmcs12->vmwrite_bitmap);
5755 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5756 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5757 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
5758 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5759 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
5761 * VMX instructions trap unconditionally. This allows L1 to
5762 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5765 case EXIT_REASON_CR_ACCESS:
5766 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5767 case EXIT_REASON_DR_ACCESS:
5768 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5769 case EXIT_REASON_IO_INSTRUCTION:
5770 return nested_vmx_exit_handled_io(vcpu, vmcs12);
5771 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
5772 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
5773 case EXIT_REASON_MSR_READ:
5774 case EXIT_REASON_MSR_WRITE:
5775 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5776 case EXIT_REASON_INVALID_STATE:
5778 case EXIT_REASON_MWAIT_INSTRUCTION:
5779 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5780 case EXIT_REASON_MONITOR_TRAP_FLAG:
5781 return nested_vmx_exit_handled_mtf(vmcs12);
5782 case EXIT_REASON_MONITOR_INSTRUCTION:
5783 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5784 case EXIT_REASON_PAUSE_INSTRUCTION:
5785 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5786 nested_cpu_has2(vmcs12,
5787 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5788 case EXIT_REASON_MCE_DURING_VMENTRY:
5790 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5791 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
5792 case EXIT_REASON_APIC_ACCESS:
5793 case EXIT_REASON_APIC_WRITE:
5794 case EXIT_REASON_EOI_INDUCED:
5796 * The controls for "virtualize APIC accesses," "APIC-
5797 * register virtualization," and "virtual-interrupt
5798 * delivery" only come from vmcs12.
5801 case EXIT_REASON_INVPCID:
5803 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
5804 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5805 case EXIT_REASON_WBINVD:
5806 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5807 case EXIT_REASON_XSETBV:
5809 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
5811 * This should never happen, since it is not possible to
5812 * set XSS to a non-zero value---neither in L1 nor in L2.
5813 * If if it were, XSS would have to be checked against
5814 * the XSS exit bitmap in vmcs12.
5816 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
5817 case EXIT_REASON_UMWAIT:
5818 case EXIT_REASON_TPAUSE:
5819 return nested_cpu_has2(vmcs12,
5820 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE);
5827 * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was
5828 * reflected into L1.
5830 bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu)
5832 struct vcpu_vmx *vmx = to_vmx(vcpu);
5833 u32 exit_reason = vmx->exit_reason;
5834 unsigned long exit_qual;
5837 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5840 * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM
5841 * has already loaded L2's state.
5843 if (unlikely(vmx->fail)) {
5844 trace_kvm_nested_vmenter_failed(
5845 "hardware VM-instruction error: ",
5846 vmcs_read32(VM_INSTRUCTION_ERROR));
5849 goto reflect_vmexit;
5852 exit_intr_info = vmx_get_intr_info(vcpu);
5853 exit_qual = vmx_get_exit_qual(vcpu);
5855 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, exit_qual,
5856 vmx->idt_vectoring_info, exit_intr_info,
5857 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5860 /* If L0 (KVM) wants the exit, it trumps L1's desires. */
5861 if (nested_vmx_l0_wants_exit(vcpu, exit_reason))
5864 /* If L1 doesn't want the exit, handle it in L0. */
5865 if (!nested_vmx_l1_wants_exit(vcpu, exit_reason))
5869 * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For
5870 * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would
5871 * need to be synthesized by querying the in-kernel LAPIC, but external
5872 * interrupts are never reflected to L1 so it's a non-issue.
5874 if ((exit_intr_info &
5875 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
5876 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
5877 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5879 vmcs12->vm_exit_intr_error_code =
5880 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5884 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, exit_qual);
5888 static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
5889 struct kvm_nested_state __user *user_kvm_nested_state,
5892 struct vcpu_vmx *vmx;
5893 struct vmcs12 *vmcs12;
5894 struct kvm_nested_state kvm_state = {
5896 .format = KVM_STATE_NESTED_FORMAT_VMX,
5897 .size = sizeof(kvm_state),
5898 .hdr.vmx.vmxon_pa = -1ull,
5899 .hdr.vmx.vmcs12_pa = -1ull,
5901 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
5902 &user_kvm_nested_state->data.vmx[0];
5905 return kvm_state.size + sizeof(*user_vmx_nested_state);
5908 vmcs12 = get_vmcs12(vcpu);
5910 if (nested_vmx_allowed(vcpu) &&
5911 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
5912 kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
5913 kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr;
5915 if (vmx_has_valid_vmcs12(vcpu)) {
5916 kvm_state.size += sizeof(user_vmx_nested_state->vmcs12);
5918 if (vmx->nested.hv_evmcs)
5919 kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
5921 if (is_guest_mode(vcpu) &&
5922 nested_cpu_has_shadow_vmcs(vmcs12) &&
5923 vmcs12->vmcs_link_pointer != -1ull)
5924 kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12);
5927 if (vmx->nested.smm.vmxon)
5928 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
5930 if (vmx->nested.smm.guest_mode)
5931 kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
5933 if (is_guest_mode(vcpu)) {
5934 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
5936 if (vmx->nested.nested_run_pending)
5937 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
5939 if (vmx->nested.mtf_pending)
5940 kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING;
5944 if (user_data_size < kvm_state.size)
5947 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
5950 if (!vmx_has_valid_vmcs12(vcpu))
5954 * When running L2, the authoritative vmcs12 state is in the
5955 * vmcs02. When running L1, the authoritative vmcs12 state is
5956 * in the shadow or enlightened vmcs linked to vmcs01, unless
5957 * need_vmcs12_to_shadow_sync is set, in which case, the authoritative
5958 * vmcs12 state is in the vmcs12 already.
5960 if (is_guest_mode(vcpu)) {
5961 sync_vmcs02_to_vmcs12(vcpu, vmcs12);
5962 sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12);
5963 } else if (!vmx->nested.need_vmcs12_to_shadow_sync) {
5964 if (vmx->nested.hv_evmcs)
5965 copy_enlightened_to_vmcs12(vmx);
5966 else if (enable_shadow_vmcs)
5967 copy_shadow_to_vmcs12(vmx);
5970 BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE);
5971 BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE);
5974 * Copy over the full allocated size of vmcs12 rather than just the size
5977 if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE))
5980 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
5981 vmcs12->vmcs_link_pointer != -1ull) {
5982 if (copy_to_user(user_vmx_nested_state->shadow_vmcs12,
5983 get_shadow_vmcs12(vcpu), VMCS12_SIZE))
5988 return kvm_state.size;
5992 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
5994 void vmx_leave_nested(struct kvm_vcpu *vcpu)
5996 if (is_guest_mode(vcpu)) {
5997 to_vmx(vcpu)->nested.nested_run_pending = 0;
5998 nested_vmx_vmexit(vcpu, -1, 0, 0);
6003 static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
6004 struct kvm_nested_state __user *user_kvm_nested_state,
6005 struct kvm_nested_state *kvm_state)
6007 struct vcpu_vmx *vmx = to_vmx(vcpu);
6008 struct vmcs12 *vmcs12;
6009 enum vm_entry_failure_code ignored;
6010 struct kvm_vmx_nested_state_data __user *user_vmx_nested_state =
6011 &user_kvm_nested_state->data.vmx[0];
6014 if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX)
6017 if (kvm_state->hdr.vmx.vmxon_pa == -1ull) {
6018 if (kvm_state->hdr.vmx.smm.flags)
6021 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull)
6025 * KVM_STATE_NESTED_EVMCS used to signal that KVM should
6026 * enable eVMCS capability on vCPU. However, since then
6027 * code was changed such that flag signals vmcs12 should
6028 * be copied into eVMCS in guest memory.
6030 * To preserve backwards compatability, allow user
6031 * to set this flag even when there is no VMXON region.
6033 if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS)
6036 if (!nested_vmx_allowed(vcpu))
6039 if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa))
6043 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6044 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6047 if (kvm_state->hdr.vmx.smm.flags &
6048 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
6052 * SMM temporarily disables VMX, so we cannot be in guest mode,
6053 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
6058 (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING))
6059 : kvm_state->hdr.vmx.smm.flags)
6062 if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
6063 !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
6066 if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) &&
6067 (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled))
6070 vmx_leave_nested(vcpu);
6072 if (kvm_state->hdr.vmx.vmxon_pa == -1ull)
6075 vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa;
6076 ret = enter_vmx_operation(vcpu);
6080 /* Empty 'VMXON' state is permitted */
6081 if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12))
6084 if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) {
6085 if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa ||
6086 !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa))
6089 set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa);
6090 } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
6092 * nested_vmx_handle_enlightened_vmptrld() cannot be called
6093 * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be
6094 * restored yet. EVMCS will be mapped from
6095 * nested_get_vmcs12_pages().
6097 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
6102 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
6103 vmx->nested.smm.vmxon = true;
6104 vmx->nested.vmxon = false;
6106 if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
6107 vmx->nested.smm.guest_mode = true;
6110 vmcs12 = get_vmcs12(vcpu);
6111 if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12)))
6114 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
6117 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
6120 vmx->nested.nested_run_pending =
6121 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
6123 vmx->nested.mtf_pending =
6124 !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING);
6127 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
6128 vmcs12->vmcs_link_pointer != -1ull) {
6129 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
6131 if (kvm_state->size <
6132 sizeof(*kvm_state) +
6133 sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12))
6134 goto error_guest_mode;
6136 if (copy_from_user(shadow_vmcs12,
6137 user_vmx_nested_state->shadow_vmcs12,
6138 sizeof(*shadow_vmcs12))) {
6140 goto error_guest_mode;
6143 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
6144 !shadow_vmcs12->hdr.shadow_vmcs)
6145 goto error_guest_mode;
6148 if (nested_vmx_check_controls(vcpu, vmcs12) ||
6149 nested_vmx_check_host_state(vcpu, vmcs12) ||
6150 nested_vmx_check_guest_state(vcpu, vmcs12, &ignored))
6151 goto error_guest_mode;
6153 vmx->nested.dirty_vmcs12 = true;
6154 ret = nested_vmx_enter_non_root_mode(vcpu, false);
6156 goto error_guest_mode;
6161 vmx->nested.nested_run_pending = 0;
6165 void nested_vmx_set_vmcs_shadowing_bitmap(void)
6167 if (enable_shadow_vmcs) {
6168 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
6169 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
6174 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
6175 * returned for the various VMX controls MSRs when nested VMX is enabled.
6176 * The same values should also be used to verify that vmcs12 control fields are
6177 * valid during nested entry from L1 to L2.
6178 * Each of these control msrs has a low and high 32-bit half: A low bit is on
6179 * if the corresponding bit in the (32-bit) control field *must* be on, and a
6180 * bit in the high half is on if the corresponding bit in the control field
6181 * may be on. See also vmx_control_verify().
6183 void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)
6186 * Note that as a general rule, the high half of the MSRs (bits in
6187 * the control fields which may be 1) should be initialized by the
6188 * intersection of the underlying hardware's MSR (i.e., features which
6189 * can be supported) and the list of features we want to expose -
6190 * because they are known to be properly supported in our code.
6191 * Also, usually, the low half of the MSRs (bits which must be 1) can
6192 * be set to 0, meaning that L1 may turn off any of these bits. The
6193 * reason is that if one of these bits is necessary, it will appear
6194 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
6195 * fields of vmcs01 and vmcs02, will turn these bits off - and
6196 * nested_vmx_l1_wants_exit() will not pass related exits to L1.
6197 * These rules have exceptions below.
6200 /* pin-based controls */
6201 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
6202 msrs->pinbased_ctls_low,
6203 msrs->pinbased_ctls_high);
6204 msrs->pinbased_ctls_low |=
6205 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6206 msrs->pinbased_ctls_high &=
6207 PIN_BASED_EXT_INTR_MASK |
6208 PIN_BASED_NMI_EXITING |
6209 PIN_BASED_VIRTUAL_NMIS |
6210 (enable_apicv ? PIN_BASED_POSTED_INTR : 0);
6211 msrs->pinbased_ctls_high |=
6212 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6213 PIN_BASED_VMX_PREEMPTION_TIMER;
6216 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
6217 msrs->exit_ctls_low,
6218 msrs->exit_ctls_high);
6219 msrs->exit_ctls_low =
6220 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
6222 msrs->exit_ctls_high &=
6223 #ifdef CONFIG_X86_64
6224 VM_EXIT_HOST_ADDR_SPACE_SIZE |
6226 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
6227 msrs->exit_ctls_high |=
6228 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
6229 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
6230 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
6232 /* We support free control of debug control saving. */
6233 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
6235 /* entry controls */
6236 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
6237 msrs->entry_ctls_low,
6238 msrs->entry_ctls_high);
6239 msrs->entry_ctls_low =
6240 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
6241 msrs->entry_ctls_high &=
6242 #ifdef CONFIG_X86_64
6243 VM_ENTRY_IA32E_MODE |
6245 VM_ENTRY_LOAD_IA32_PAT;
6246 msrs->entry_ctls_high |=
6247 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
6249 /* We support free control of debug control loading. */
6250 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
6252 /* cpu-based controls */
6253 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
6254 msrs->procbased_ctls_low,
6255 msrs->procbased_ctls_high);
6256 msrs->procbased_ctls_low =
6257 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
6258 msrs->procbased_ctls_high &=
6259 CPU_BASED_INTR_WINDOW_EXITING |
6260 CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING |
6261 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
6262 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
6263 CPU_BASED_CR3_STORE_EXITING |
6264 #ifdef CONFIG_X86_64
6265 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
6267 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
6268 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
6269 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
6270 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
6271 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
6273 * We can allow some features even when not supported by the
6274 * hardware. For example, L1 can specify an MSR bitmap - and we
6275 * can use it to avoid exits to L1 - even when L0 runs L2
6276 * without MSR bitmaps.
6278 msrs->procbased_ctls_high |=
6279 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
6280 CPU_BASED_USE_MSR_BITMAPS;
6282 /* We support free control of CR3 access interception. */
6283 msrs->procbased_ctls_low &=
6284 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
6287 * secondary cpu-based controls. Do not include those that
6288 * depend on CPUID bits, they are added later by vmx_cpuid_update.
6290 if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
6291 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
6292 msrs->secondary_ctls_low,
6293 msrs->secondary_ctls_high);
6295 msrs->secondary_ctls_low = 0;
6296 msrs->secondary_ctls_high &=
6297 SECONDARY_EXEC_DESC |
6298 SECONDARY_EXEC_RDTSCP |
6299 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
6300 SECONDARY_EXEC_WBINVD_EXITING |
6301 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6302 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
6303 SECONDARY_EXEC_RDRAND_EXITING |
6304 SECONDARY_EXEC_ENABLE_INVPCID |
6305 SECONDARY_EXEC_RDSEED_EXITING |
6306 SECONDARY_EXEC_XSAVES;
6309 * We can emulate "VMCS shadowing," even if the hardware
6310 * doesn't support it.
6312 msrs->secondary_ctls_high |=
6313 SECONDARY_EXEC_SHADOW_VMCS;
6316 /* nested EPT: emulate EPT also to L1 */
6317 msrs->secondary_ctls_high |=
6318 SECONDARY_EXEC_ENABLE_EPT;
6320 VMX_EPT_PAGE_WALK_4_BIT |
6321 VMX_EPT_PAGE_WALK_5_BIT |
6323 VMX_EPT_INVEPT_BIT |
6324 VMX_EPT_EXECUTE_ONLY_BIT;
6326 msrs->ept_caps &= ept_caps;
6327 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
6328 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
6329 VMX_EPT_1GB_PAGE_BIT;
6330 if (enable_ept_ad_bits) {
6331 msrs->secondary_ctls_high |=
6332 SECONDARY_EXEC_ENABLE_PML;
6333 msrs->ept_caps |= VMX_EPT_AD_BIT;
6337 if (cpu_has_vmx_vmfunc()) {
6338 msrs->secondary_ctls_high |=
6339 SECONDARY_EXEC_ENABLE_VMFUNC;
6341 * Advertise EPTP switching unconditionally
6342 * since we emulate it
6345 msrs->vmfunc_controls =
6346 VMX_VMFUNC_EPTP_SWITCHING;
6350 * Old versions of KVM use the single-context version without
6351 * checking for support, so declare that it is supported even
6352 * though it is treated as global context. The alternative is
6353 * not failing the single-context invvpid, and it is worse.
6356 msrs->secondary_ctls_high |=
6357 SECONDARY_EXEC_ENABLE_VPID;
6358 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
6359 VMX_VPID_EXTENT_SUPPORTED_MASK;
6362 if (enable_unrestricted_guest)
6363 msrs->secondary_ctls_high |=
6364 SECONDARY_EXEC_UNRESTRICTED_GUEST;
6366 if (flexpriority_enabled)
6367 msrs->secondary_ctls_high |=
6368 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6370 /* miscellaneous data */
6371 rdmsr(MSR_IA32_VMX_MISC,
6374 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
6376 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
6377 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
6378 VMX_MISC_ACTIVITY_HLT;
6379 msrs->misc_high = 0;
6382 * This MSR reports some information about VMX support. We
6383 * should return information about the VMX we emulate for the
6384 * guest, and the VMCS structure we give it - not about the
6385 * VMX support of the underlying hardware.
6389 VMX_BASIC_TRUE_CTLS |
6390 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
6391 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
6393 if (cpu_has_vmx_basic_inout())
6394 msrs->basic |= VMX_BASIC_INOUT;
6397 * These MSRs specify bits which the guest must keep fixed on
6398 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
6399 * We picked the standard core2 setting.
6401 #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
6402 #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
6403 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
6404 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
6406 /* These MSRs specify bits which the guest must keep fixed off. */
6407 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
6408 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
6410 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
6411 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
6414 void nested_vmx_hardware_unsetup(void)
6418 if (enable_shadow_vmcs) {
6419 for (i = 0; i < VMX_BITMAP_NR; i++)
6420 free_page((unsigned long)vmx_bitmap[i]);
6424 __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
6428 if (!cpu_has_vmx_shadow_vmcs())
6429 enable_shadow_vmcs = 0;
6430 if (enable_shadow_vmcs) {
6431 for (i = 0; i < VMX_BITMAP_NR; i++) {
6433 * The vmx_bitmap is not tied to a VM and so should
6434 * not be charged to a memcg.
6436 vmx_bitmap[i] = (unsigned long *)
6437 __get_free_page(GFP_KERNEL);
6438 if (!vmx_bitmap[i]) {
6439 nested_vmx_hardware_unsetup();
6444 init_vmcs_shadow_fields();
6447 exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear;
6448 exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch;
6449 exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld;
6450 exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst;
6451 exit_handlers[EXIT_REASON_VMREAD] = handle_vmread;
6452 exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume;
6453 exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite;
6454 exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff;
6455 exit_handlers[EXIT_REASON_VMON] = handle_vmon;
6456 exit_handlers[EXIT_REASON_INVEPT] = handle_invept;
6457 exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid;
6458 exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc;
6463 struct kvm_x86_nested_ops vmx_nested_ops = {
6464 .check_events = vmx_check_nested_events,
6465 .hv_timer_pending = nested_vmx_preemption_timer_pending,
6466 .get_state = vmx_get_nested_state,
6467 .set_state = vmx_set_nested_state,
6468 .get_vmcs12_pages = nested_get_vmcs12_pages,
6469 .enable_evmcs = nested_enable_evmcs,
6470 .get_evmcs_version = nested_get_evmcs_version,