• Home
  • Raw
  • Download

Lines Matching +full:dma +full:- +full:noncoherent

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
5 * This module enables machines with Intel VT-x extensions to run virtual
30 #include <linux/entry-kvm.h>
48 #include <asm/spec-ctrl.h>
125 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
174 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
190 /* Default doubles per-vcpu window every exit. */
194 /* Default resets per-vcpu window every exit to ple_window. */
202 /* Default is SYSTEM mode, 1 for host-guest mode */
284 return -ENOMEM; in vmx_setup_l1d_flush()
323 return -EINVAL; in vmentry_l1d_flush_parse()
379 if (!vmx->disable_fb_clear) in vmx_disable_fb_clear()
386 vmx->msr_ia32_mcu_opt_ctrl = msr; in vmx_disable_fb_clear()
391 if (!vmx->disable_fb_clear) in vmx_enable_fb_clear()
394 vmx->msr_ia32_mcu_opt_ctrl &= ~FB_CLEAR_DIS; in vmx_enable_fb_clear()
395 native_wrmsrl(MSR_IA32_MCU_OPT_CTRL, vmx->msr_ia32_mcu_opt_ctrl); in vmx_enable_fb_clear()
400 vmx->disable_fb_clear = vmx_fb_clear_ctrl_available; in vmx_update_fb_clear_dis()
407 if ((vcpu->arch.arch_capabilities & ARCH_CAP_FB_CLEAR) || in vmx_update_fb_clear_dis()
408 ((vcpu->arch.arch_capabilities & ARCH_CAP_MDS_NO) && in vmx_update_fb_clear_dis()
409 (vcpu->arch.arch_capabilities & ARCH_CAP_TAA_NO) && in vmx_update_fb_clear_dis()
410 (vcpu->arch.arch_capabilities & ARCH_CAP_PSDP_NO) && in vmx_update_fb_clear_dis()
411 (vcpu->arch.arch_capabilities & ARCH_CAP_FBSDP_NO) && in vmx_update_fb_clear_dis()
412 (vcpu->arch.arch_capabilities & ARCH_CAP_SBDR_SSDP_NO))) in vmx_update_fb_clear_dis()
413 vmx->disable_fb_clear = false; in vmx_update_fb_clear_dis()
473 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
510 vmx->segment_cache.bitmask = 0; in vmx_segment_cache_clear()
516 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
543 tmp_eptp = to_vmx(vcpu)->ept_pointer; in check_ept_pointer_match()
544 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) { in check_ept_pointer_match()
545 to_kvm_vmx(kvm)->ept_pointers_match in check_ept_pointer_match()
551 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH; in check_ept_pointer_match()
559 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn, in kvm_fill_hv_flush_list_func()
560 range->pages); in kvm_fill_hv_flush_list_func()
566 u64 ept_pointer = to_vmx(vcpu)->ept_pointer; in __hv_remote_flush_tlb_with_range()
586 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); in hv_remote_flush_tlb_with_range()
588 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK) in hv_remote_flush_tlb_with_range()
591 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) { in hv_remote_flush_tlb_with_range()
594 if (VALID_PAGE(to_vmx(vcpu)->ept_pointer)) in hv_remote_flush_tlb_with_range()
603 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); in hv_remote_flush_tlb_with_range()
615 &vcpu->kvm->arch.hyperv.hv_pa_pg; in hv_enable_direct_tlbflush()
617 * Synthetic VM-Exit is not enabled in current code and so All in hv_enable_direct_tlbflush()
624 return -ENOMEM; in hv_enable_direct_tlbflush()
626 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs; in hv_enable_direct_tlbflush()
628 evmcs->partition_assist_page = in hv_enable_direct_tlbflush()
630 evmcs->hv_vm_id = (unsigned long)vcpu->kvm; in hv_enable_direct_tlbflush()
631 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1; in hv_enable_direct_tlbflush()
639 * Comment's format: document - errata name - stepping - processor name.
644 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
646 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
647 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
648 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
650 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
652 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
653 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
655 * 320767.pdf - AAP86 - B1 -
656 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
659 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
661 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
663 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
665 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
666 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
667 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
669 /* Xeon E3-1220 V2 */
704 return -ENOENT; in possible_passthrough_msr_slot()
724 r = possible_passthrough_msr_slot(msr) != -ENOENT; in is_valid_passthrough_msr()
735 for (i = 0; i < vmx->nr_uret_msrs; ++i) in __vmx_find_uret_msr()
736 if (vmx_uret_msrs_list[vmx->guest_uret_msrs[i].slot] == msr) in __vmx_find_uret_msr()
738 return -1; in __vmx_find_uret_msr()
747 return &vmx->guest_uret_msrs[i]; in vmx_find_uret_msr()
756 u64 old_msr_data = msr->data; in vmx_set_guest_uret_msr()
757 msr->data = data; in vmx_set_guest_uret_msr()
758 if (msr - vmx->guest_uret_msrs < vmx->nr_active_uret_msrs) { in vmx_set_guest_uret_msr()
760 ret = kvm_set_user_return_msr(msr->slot, msr->data, msr->mask); in vmx_set_guest_uret_msr()
763 msr->data = old_msr_data; in vmx_set_guest_uret_msr()
776 vmcs_clear(v->vmcs); in crash_vmclear_local_loaded_vmcss()
785 if (loaded_vmcs->cpu != cpu) in __loaded_vmcs_clear()
787 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) in __loaded_vmcs_clear()
790 vmcs_clear(loaded_vmcs->vmcs); in __loaded_vmcs_clear()
791 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) in __loaded_vmcs_clear()
792 vmcs_clear(loaded_vmcs->shadow_vmcs); in __loaded_vmcs_clear()
794 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); in __loaded_vmcs_clear()
798 * current percpu list, complete before setting loaded_vmcs->vcpu to in __loaded_vmcs_clear()
799 * -1, otherwise a different cpu can see vcpu == -1 first and add in __loaded_vmcs_clear()
805 loaded_vmcs->cpu = -1; in __loaded_vmcs_clear()
806 loaded_vmcs->launched = 0; in __loaded_vmcs_clear()
811 int cpu = loaded_vmcs->cpu; in loaded_vmcs_clear()
813 if (cpu != -1) in loaded_vmcs_clear()
824 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) { in vmx_segment_cache_test_set()
825 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS); in vmx_segment_cache_test_set()
826 vmx->segment_cache.bitmask = 0; in vmx_segment_cache_test_set()
828 ret = vmx->segment_cache.bitmask & mask; in vmx_segment_cache_test_set()
829 vmx->segment_cache.bitmask |= mask; in vmx_segment_cache_test_set()
835 u16 *p = &vmx->segment_cache.seg[seg].selector; in vmx_read_guest_seg_selector()
844 ulong *p = &vmx->segment_cache.seg[seg].base; in vmx_read_guest_seg_base()
853 u32 *p = &vmx->segment_cache.seg[seg].limit; in vmx_read_guest_seg_limit()
862 u32 *p = &vmx->segment_cache.seg[seg].ar; in vmx_read_guest_seg_ar()
883 if ((vcpu->guest_debug & in update_exception_bitmap()
887 if (to_vmx(vcpu)->rmode.vm86_active) in update_exception_bitmap()
898 eb |= get_vmcs12(vcpu)->exception_bitmap; in update_exception_bitmap()
926 msr_bitmap = vmx->loaded_vmcs->msr_bitmap; in msr_write_intercepted()
942 if (vmx->loaded_vmcs->launched) in __vmx_vcpu_run_flags()
948 * it after vmexit and store it in vmx->spec_ctrl. in __vmx_vcpu_run_flags()
967 for (i = 0; i < m->nr; ++i) { in vmx_find_loadstore_msr_slot()
968 if (m->val[i].index == msr) in vmx_find_loadstore_msr_slot()
971 return -ENOENT; in vmx_find_loadstore_msr_slot()
977 struct msr_autoload *m = &vmx->msr_autoload; in clear_atomic_switch_msr()
997 i = vmx_find_loadstore_msr_slot(&m->guest, msr); in clear_atomic_switch_msr()
1000 --m->guest.nr; in clear_atomic_switch_msr()
1001 m->guest.val[i] = m->guest.val[m->guest.nr]; in clear_atomic_switch_msr()
1002 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); in clear_atomic_switch_msr()
1005 i = vmx_find_loadstore_msr_slot(&m->host, msr); in clear_atomic_switch_msr()
1009 --m->host.nr; in clear_atomic_switch_msr()
1010 m->host.val[i] = m->host.val[m->host.nr]; in clear_atomic_switch_msr()
1011 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); in clear_atomic_switch_msr()
1030 struct msr_autoload *m = &vmx->msr_autoload; in add_atomic_switch_msr()
1064 i = vmx_find_loadstore_msr_slot(&m->guest, msr); in add_atomic_switch_msr()
1066 j = vmx_find_loadstore_msr_slot(&m->host, msr); in add_atomic_switch_msr()
1068 if ((i < 0 && m->guest.nr == MAX_NR_LOADSTORE_MSRS) || in add_atomic_switch_msr()
1069 (j < 0 && m->host.nr == MAX_NR_LOADSTORE_MSRS)) { in add_atomic_switch_msr()
1075 i = m->guest.nr++; in add_atomic_switch_msr()
1076 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); in add_atomic_switch_msr()
1078 m->guest.val[i].index = msr; in add_atomic_switch_msr()
1079 m->guest.val[i].value = guest_val; in add_atomic_switch_msr()
1085 j = m->host.nr++; in add_atomic_switch_msr()
1086 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); in add_atomic_switch_msr()
1088 m->host.val[j].index = msr; in add_atomic_switch_msr()
1089 m->host.val[j].value = host_val; in add_atomic_switch_msr()
1094 u64 guest_efer = vmx->vcpu.arch.efer; in update_transition_efer()
1119 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { in update_transition_efer()
1139 vmx->guest_uret_msrs[i].data = guest_efer; in update_transition_efer()
1140 vmx->guest_uret_msrs[i].mask = ~ignore_bits; in update_transition_efer()
1147 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1177 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN); in pt_can_write_msr()
1182 /* The base must be 128-byte aligned and a legal physical address. */ in pt_output_base_valid()
1190 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status); in pt_load_msr()
1191 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); in pt_load_msr()
1192 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); in pt_load_msr()
1193 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); in pt_load_msr()
1195 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]); in pt_load_msr()
1196 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]); in pt_load_msr()
1204 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status); in pt_save_msr()
1205 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); in pt_save_msr()
1206 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); in pt_save_msr()
1207 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); in pt_save_msr()
1209 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]); in pt_save_msr()
1210 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]); in pt_save_msr()
1223 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); in pt_guest_enter()
1224 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { in pt_guest_enter()
1226 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range); in pt_guest_enter()
1227 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range); in pt_guest_enter()
1236 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { in pt_guest_exit()
1237 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range); in pt_guest_exit()
1238 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range); in pt_guest_exit()
1242 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); in pt_guest_exit()
1248 if (unlikely(fs_sel != host->fs_sel)) { in vmx_set_host_fs_gs()
1253 host->fs_sel = fs_sel; in vmx_set_host_fs_gs()
1255 if (unlikely(gs_sel != host->gs_sel)) { in vmx_set_host_fs_gs()
1260 host->gs_sel = gs_sel; in vmx_set_host_fs_gs()
1262 if (unlikely(fs_base != host->fs_base)) { in vmx_set_host_fs_gs()
1264 host->fs_base = fs_base; in vmx_set_host_fs_gs()
1266 if (unlikely(gs_base != host->gs_base)) { in vmx_set_host_fs_gs()
1268 host->gs_base = gs_base; in vmx_set_host_fs_gs()
1283 vmx->req_immediate_exit = false; in vmx_prepare_switch_to_guest()
1288 * to/from long-mode by setting MSR_EFER.LMA. in vmx_prepare_switch_to_guest()
1290 if (!vmx->guest_uret_msrs_loaded) { in vmx_prepare_switch_to_guest()
1291 vmx->guest_uret_msrs_loaded = true; in vmx_prepare_switch_to_guest()
1292 for (i = 0; i < vmx->nr_active_uret_msrs; ++i) in vmx_prepare_switch_to_guest()
1293 kvm_set_user_return_msr(vmx->guest_uret_msrs[i].slot, in vmx_prepare_switch_to_guest()
1294 vmx->guest_uret_msrs[i].data, in vmx_prepare_switch_to_guest()
1295 vmx->guest_uret_msrs[i].mask); in vmx_prepare_switch_to_guest()
1299 if (vmx->nested.need_vmcs12_to_shadow_sync) in vmx_prepare_switch_to_guest()
1302 if (vmx->guest_state_loaded) in vmx_prepare_switch_to_guest()
1305 host_state = &vmx->loaded_vmcs->host_state; in vmx_prepare_switch_to_guest()
1311 host_state->ldt_sel = kvm_read_ldt(); in vmx_prepare_switch_to_guest()
1314 savesegment(ds, host_state->ds_sel); in vmx_prepare_switch_to_guest()
1315 savesegment(es, host_state->es_sel); in vmx_prepare_switch_to_guest()
1318 if (likely(is_64bit_mm(current->mm))) { in vmx_prepare_switch_to_guest()
1320 fs_sel = current->thread.fsindex; in vmx_prepare_switch_to_guest()
1321 gs_sel = current->thread.gsindex; in vmx_prepare_switch_to_guest()
1322 fs_base = current->thread.fsbase; in vmx_prepare_switch_to_guest()
1323 vmx->msr_host_kernel_gs_base = current->thread.gsbase; in vmx_prepare_switch_to_guest()
1328 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE); in vmx_prepare_switch_to_guest()
1331 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); in vmx_prepare_switch_to_guest()
1340 vmx->guest_state_loaded = true; in vmx_prepare_switch_to_guest()
1347 if (!vmx->guest_state_loaded) in vmx_prepare_switch_to_host()
1350 host_state = &vmx->loaded_vmcs->host_state; in vmx_prepare_switch_to_host()
1352 ++vmx->vcpu.stat.host_state_reload; in vmx_prepare_switch_to_host()
1355 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); in vmx_prepare_switch_to_host()
1357 if (host_state->ldt_sel || (host_state->gs_sel & 7)) { in vmx_prepare_switch_to_host()
1358 kvm_load_ldt(host_state->ldt_sel); in vmx_prepare_switch_to_host()
1360 load_gs_index(host_state->gs_sel); in vmx_prepare_switch_to_host()
1362 loadsegment(gs, host_state->gs_sel); in vmx_prepare_switch_to_host()
1365 if (host_state->fs_sel & 7) in vmx_prepare_switch_to_host()
1366 loadsegment(fs, host_state->fs_sel); in vmx_prepare_switch_to_host()
1368 if (unlikely(host_state->ds_sel | host_state->es_sel)) { in vmx_prepare_switch_to_host()
1369 loadsegment(ds, host_state->ds_sel); in vmx_prepare_switch_to_host()
1370 loadsegment(es, host_state->es_sel); in vmx_prepare_switch_to_host()
1375 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); in vmx_prepare_switch_to_host()
1378 vmx->guest_state_loaded = false; in vmx_prepare_switch_to_host()
1379 vmx->guest_uret_msrs_loaded = false; in vmx_prepare_switch_to_host()
1386 if (vmx->guest_state_loaded) in vmx_read_guest_kernel_gs_base()
1387 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); in vmx_read_guest_kernel_gs_base()
1389 return vmx->msr_guest_kernel_gs_base; in vmx_read_guest_kernel_gs_base()
1395 if (vmx->guest_state_loaded) in vmx_write_guest_kernel_gs_base()
1398 vmx->msr_guest_kernel_gs_base = data; in vmx_write_guest_kernel_gs_base()
1406 bool already_loaded = vmx->loaded_vmcs->cpu == cpu; in vmx_vcpu_load_vmcs()
1410 loaded_vmcs_clear(vmx->loaded_vmcs); in vmx_vcpu_load_vmcs()
1414 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to in vmx_vcpu_load_vmcs()
1421 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, in vmx_vcpu_load_vmcs()
1427 if (prev != vmx->loaded_vmcs->vmcs) { in vmx_vcpu_load_vmcs()
1428 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; in vmx_vcpu_load_vmcs()
1429 vmcs_load(vmx->loaded_vmcs->vmcs); in vmx_vcpu_load_vmcs()
1435 * performs IBPB on nested VM-Exit (a single nested transition in vmx_vcpu_load_vmcs()
1438 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev)) in vmx_vcpu_load_vmcs()
1453 * Linux uses per-cpu TSS and GDT, so set these when switching in vmx_vcpu_load_vmcs()
1457 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); in vmx_vcpu_load_vmcs()
1463 vmx->loaded_vmcs->cpu = cpu; in vmx_vcpu_load_vmcs()
1468 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) in vmx_vcpu_load_vmcs()
1484 vmx->host_debugctlmsr = get_debugctlmsr(); in vmx_vcpu_load()
1507 if (vmx->rmode.vm86_active) { in vmx_get_rflags()
1509 save_rflags = vmx->rmode.save_rflags; in vmx_get_rflags()
1512 vmx->rflags = rflags; in vmx_get_rflags()
1514 return vmx->rflags; in vmx_get_rflags()
1529 vmx->rflags = rflags; in vmx_set_rflags()
1535 vmx->rflags = rflags; in vmx_set_rflags()
1536 if (vmx->rmode.vm86_active) { in vmx_set_rflags()
1537 vmx->rmode.save_rflags = rflags; in vmx_set_rflags()
1542 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM) in vmx_set_rflags()
1543 vmx->emulation_required = emulation_required(vcpu); in vmx_set_rflags()
1584 if (data & vmx->pt_desc.ctl_bitmask) in vmx_rtit_ctl_check()
1591 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) && in vmx_rtit_ctl_check()
1592 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN)) in vmx_rtit_ctl_check()
1602 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_rtit_ctl_check()
1610 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods); in vmx_rtit_ctl_check()
1611 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) && in vmx_rtit_ctl_check()
1615 value = intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_rtit_ctl_check()
1617 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) && in vmx_rtit_ctl_check()
1621 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods); in vmx_rtit_ctl_check()
1622 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) && in vmx_rtit_ctl_check()
1632 if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2)) in vmx_rtit_ctl_check()
1635 if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2)) in vmx_rtit_ctl_check()
1638 if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2)) in vmx_rtit_ctl_check()
1641 if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2)) in vmx_rtit_ctl_check()
1661 * (namely Hyper-V) don't set it due to it being undefined behavior, in skip_emulated_instruction()
1665 to_vmx(vcpu)->exit_reason.basic != EXIT_REASON_EPT_MISCONFIG) { in skip_emulated_instruction()
1670 * We need to mask out the high 32 bits of RIP if not in 64-bit in skip_emulated_instruction()
1671 * mode, but just finding out that we are in 64-bit mode is in skip_emulated_instruction()
1690 * Recognizes a pending MTF VM-exit and records the nested state for later
1702 * Per the SDM, MTF takes priority over debug-trap exceptions besides in vmx_update_emulated_instruction()
1703 * T-bit traps. As instruction emulation is completed (i.e. at the in vmx_update_emulated_instruction()
1705 * debug-trap. Record the pending MTF state to be delivered in in vmx_update_emulated_instruction()
1709 (!vcpu->arch.exception.pending || in vmx_update_emulated_instruction()
1710 vcpu->arch.exception.nr == DB_VECTOR)) in vmx_update_emulated_instruction()
1711 vmx->nested.mtf_pending = true; in vmx_update_emulated_instruction()
1713 vmx->nested.mtf_pending = false; in vmx_update_emulated_instruction()
1730 if (kvm_hlt_in_guest(vcpu->kvm) && in vmx_clear_hlt()
1738 unsigned nr = vcpu->arch.exception.nr; in vmx_queue_exception()
1739 bool has_error_code = vcpu->arch.exception.has_error_code; in vmx_queue_exception()
1740 u32 error_code = vcpu->arch.exception.error_code; in vmx_queue_exception()
1752 * ABI lets userspace shove in arbitrary 32-bit values. Drop in vmx_queue_exception()
1753 * the upper bits to avoid VM-Fail, losing information that in vmx_queue_exception()
1760 if (vmx->rmode.vm86_active) { in vmx_queue_exception()
1763 inc_eip = vcpu->arch.event_exit_inst_len; in vmx_queue_exception()
1768 WARN_ON_ONCE(vmx->emulation_required); in vmx_queue_exception()
1772 vmx->vcpu.arch.event_exit_inst_len); in vmx_queue_exception()
1790 to = vmx->nr_active_uret_msrs++; in vmx_setup_uret_msr()
1792 tmp = vmx->guest_uret_msrs[to]; in vmx_setup_uret_msr()
1793 vmx->guest_uret_msrs[to] = vmx->guest_uret_msrs[from]; in vmx_setup_uret_msr()
1794 vmx->guest_uret_msrs[from] = tmp; in vmx_setup_uret_msr()
1799 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1804 vmx->guest_uret_msrs_loaded = false; in setup_msrs()
1805 vmx->nr_active_uret_msrs = 0; in setup_msrs()
1811 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) { in setup_msrs()
1820 if (guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) in setup_msrs()
1826 vmx_update_msr_bitmap(&vmx->vcpu); in setup_msrs()
1841 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)) in vmx_write_l1_tsc_offset()
1842 g_tsc_offset = vmcs12->tsc_offset; in vmx_write_l1_tsc_offset()
1844 trace_kvm_write_tsc_offset(vcpu->vcpu_id, in vmx_write_l1_tsc_offset()
1845 vcpu->arch.tsc_offset - g_tsc_offset, in vmx_write_l1_tsc_offset()
1865 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; in vmx_feature_control_msr_valid()
1872 switch (msr->index) { in vmx_get_msr_feature()
1876 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); in vmx_get_msr_feature()
1878 msr->data = vmx_get_perf_capabilities(); in vmx_get_msr_feature()
1887 * Returns 0 on success, non-0 otherwise.
1896 switch (msr_info->index) { in vmx_get_msr()
1899 msr_info->data = vmcs_readl(GUEST_FS_BASE); in vmx_get_msr()
1902 msr_info->data = vmcs_readl(GUEST_GS_BASE); in vmx_get_msr()
1905 msr_info->data = vmx_read_guest_kernel_gs_base(vmx); in vmx_get_msr()
1911 if (!msr_info->host_initiated && in vmx_get_msr()
1912 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR)) in vmx_get_msr()
1916 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx)) in vmx_get_msr()
1919 msr_info->data = vmx->msr_ia32_umwait_control; in vmx_get_msr()
1922 if (!msr_info->host_initiated && in vmx_get_msr()
1926 msr_info->data = to_vmx(vcpu)->spec_ctrl; in vmx_get_msr()
1929 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); in vmx_get_msr()
1932 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); in vmx_get_msr()
1935 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); in vmx_get_msr()
1939 (!msr_info->host_initiated && in vmx_get_msr()
1942 msr_info->data = vmcs_read64(GUEST_BNDCFGS); in vmx_get_msr()
1945 if (!msr_info->host_initiated && in vmx_get_msr()
1946 !(vmx->msr_ia32_feature_control & in vmx_get_msr()
1949 msr_info->data = vcpu->arch.mcg_ext_ctl; in vmx_get_msr()
1952 msr_info->data = vmx->msr_ia32_feature_control; in vmx_get_msr()
1957 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, in vmx_get_msr()
1958 &msr_info->data)) in vmx_get_msr()
1962 * instead of just ignoring the features, different Hyper-V in vmx_get_msr()
1967 if (!msr_info->host_initiated && in vmx_get_msr()
1968 vmx->nested.enlightened_vmcs_enabled) in vmx_get_msr()
1969 nested_evmcs_filter_control_msr(msr_info->index, in vmx_get_msr()
1970 &msr_info->data); in vmx_get_msr()
1975 msr_info->data = vmx->pt_desc.guest.ctl; in vmx_get_msr()
1980 msr_info->data = vmx->pt_desc.guest.status; in vmx_get_msr()
1984 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
1987 msr_info->data = vmx->pt_desc.guest.cr3_match; in vmx_get_msr()
1991 (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
1993 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
1996 msr_info->data = vmx->pt_desc.guest.output_base; in vmx_get_msr()
2000 (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
2002 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
2005 msr_info->data = vmx->pt_desc.guest.output_mask; in vmx_get_msr()
2008 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; in vmx_get_msr()
2010 (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
2014 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2]; in vmx_get_msr()
2016 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2]; in vmx_get_msr()
2019 if (!msr_info->host_initiated && in vmx_get_msr()
2025 msr = vmx_find_uret_msr(vmx, msr_info->index); in vmx_get_msr()
2027 msr_info->data = msr->data; in vmx_get_msr()
2048 * Returns 0 on success, non-0 otherwise.
2056 u32 msr_index = msr_info->index; in vmx_set_msr()
2057 u64 data = msr_info->data; in vmx_set_msr()
2079 get_vmcs12(vcpu)->guest_sysenter_cs = data; in vmx_set_msr()
2085 get_vmcs12(vcpu)->guest_sysenter_eip = data; in vmx_set_msr()
2092 get_vmcs12(vcpu)->guest_sysenter_esp = data; in vmx_set_msr()
2097 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls & in vmx_set_msr()
2099 get_vmcs12(vcpu)->guest_ia32_debugctl = data; in vmx_set_msr()
2106 (!msr_info->host_initiated && in vmx_set_msr()
2115 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx)) in vmx_set_msr()
2118 /* The reserved bit 1 and non-32 bit [63:32] should be zero */ in vmx_set_msr()
2122 vmx->msr_ia32_umwait_control = data; in vmx_set_msr()
2125 if (!msr_info->host_initiated && in vmx_set_msr()
2132 vmx->spec_ctrl = data; in vmx_set_msr()
2137 * For non-nested: in vmx_set_msr()
2138 * When it's written (to non-zero) for the first time, pass in vmx_set_msr()
2153 if (!msr_info->host_initiated && in vmx_set_msr()
2154 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR)) in vmx_set_msr()
2160 if (!msr_info->host_initiated && in vmx_set_msr()
2174 * For non-nested: in vmx_set_msr()
2175 * When it's written (to non-zero) for the first time, pass in vmx_set_msr()
2191 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) in vmx_set_msr()
2192 get_vmcs12(vcpu)->guest_ia32_pat = data; in vmx_set_msr()
2196 vcpu->arch.pat = data; in vmx_set_msr()
2205 if ((!msr_info->host_initiated && in vmx_set_msr()
2206 !(to_vmx(vcpu)->msr_ia32_feature_control & in vmx_set_msr()
2210 vcpu->arch.mcg_ext_ctl = data; in vmx_set_msr()
2214 (to_vmx(vcpu)->msr_ia32_feature_control & in vmx_set_msr()
2215 FEAT_CTL_LOCKED && !msr_info->host_initiated)) in vmx_set_msr()
2217 vmx->msr_ia32_feature_control = data; in vmx_set_msr()
2218 if (msr_info->host_initiated && data == 0) in vmx_set_msr()
2222 if (!msr_info->host_initiated) in vmx_set_msr()
2223 return 1; /* they are read-only */ in vmx_set_msr()
2230 vmx->nested.vmxon) in vmx_set_msr()
2233 vmx->pt_desc.guest.ctl = data; in vmx_set_msr()
2241 vmx->pt_desc.guest.status = data; in vmx_set_msr()
2246 if (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2249 vmx->pt_desc.guest.cr3_match = data; in vmx_set_msr()
2254 if (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2256 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2261 vmx->pt_desc.guest.output_base = data; in vmx_set_msr()
2266 if (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2268 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2271 vmx->pt_desc.guest.output_mask = data; in vmx_set_msr()
2276 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; in vmx_set_msr()
2277 if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2283 vmx->pt_desc.guest.addr_b[index / 2] = data; in vmx_set_msr()
2285 vmx->pt_desc.guest.addr_a[index / 2] = data; in vmx_set_msr()
2288 if (!msr_info->host_initiated && in vmx_set_msr()
2320 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); in vmx_cache_reg()
2323 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); in vmx_cache_reg()
2330 guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; in vmx_cache_reg()
2332 vcpu->arch.cr0 &= ~guest_owned_bits; in vmx_cache_reg()
2333 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits; in vmx_cache_reg()
2338 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); in vmx_cache_reg()
2341 guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; in vmx_cache_reg()
2343 vcpu->arch.cr4 &= ~guest_owned_bits; in vmx_cache_reg()
2344 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits; in vmx_cache_reg()
2382 return -EFAULT; in kvm_cpu_vmxon()
2392 return -EBUSY; in hardware_enable()
2395 * This can happen if we hot-added a CPU but failed to allocate in hardware_enable()
2400 return -EFAULT; in hardware_enable()
2464 return -EIO; in adjust_vmx_controls()
2502 return -EIO; in setup_vmcs_config()
2537 return -EIO; in setup_vmcs_config()
2552 &vmx_cap->ept, &vmx_cap->vpid); in setup_vmcs_config()
2560 } else if (vmx_cap->ept) { in setup_vmcs_config()
2561 vmx_cap->ept = 0; in setup_vmcs_config()
2563 "1-setting enable EPT VM-execution control\n"); in setup_vmcs_config()
2566 vmx_cap->vpid) { in setup_vmcs_config()
2567 vmx_cap->vpid = 0; in setup_vmcs_config()
2569 "1-setting enable VPID VM-execution control\n"); in setup_vmcs_config()
2584 return -EIO; in setup_vmcs_config()
2591 return -EIO; in setup_vmcs_config()
2608 return -EIO; in setup_vmcs_config()
2636 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ in setup_vmcs_config()
2638 return -EIO; in setup_vmcs_config()
2641 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ in setup_vmcs_config()
2643 return -EIO; in setup_vmcs_config()
2646 /* Require Write-Back (WB) memory type for VMCS accesses. */ in setup_vmcs_config()
2648 return -EIO; in setup_vmcs_config()
2650 vmcs_conf->size = vmx_msr_high & 0x1fff; in setup_vmcs_config()
2651 vmcs_conf->order = get_order(vmcs_conf->size); in setup_vmcs_config()
2652 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; in setup_vmcs_config()
2654 vmcs_conf->revision_id = vmx_msr_low; in setup_vmcs_config()
2656 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; in setup_vmcs_config()
2657 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; in setup_vmcs_config()
2658 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; in setup_vmcs_config()
2659 vmcs_conf->vmexit_ctrl = _vmexit_control; in setup_vmcs_config()
2660 vmcs_conf->vmentry_ctrl = _vmentry_control; in setup_vmcs_config()
2684 vmcs->hdr.revision_id = KVM_EVMCS_VERSION; in alloc_vmcs_cpu()
2686 vmcs->hdr.revision_id = vmcs_config.revision_id; in alloc_vmcs_cpu()
2689 vmcs->hdr.shadow_vmcs = 1; in alloc_vmcs_cpu()
2703 if (!loaded_vmcs->vmcs) in free_loaded_vmcs()
2706 free_vmcs(loaded_vmcs->vmcs); in free_loaded_vmcs()
2707 loaded_vmcs->vmcs = NULL; in free_loaded_vmcs()
2708 if (loaded_vmcs->msr_bitmap) in free_loaded_vmcs()
2709 free_page((unsigned long)loaded_vmcs->msr_bitmap); in free_loaded_vmcs()
2710 WARN_ON(loaded_vmcs->shadow_vmcs != NULL); in free_loaded_vmcs()
2715 loaded_vmcs->vmcs = alloc_vmcs(false); in alloc_loaded_vmcs()
2716 if (!loaded_vmcs->vmcs) in alloc_loaded_vmcs()
2717 return -ENOMEM; in alloc_loaded_vmcs()
2719 vmcs_clear(loaded_vmcs->vmcs); in alloc_loaded_vmcs()
2721 loaded_vmcs->shadow_vmcs = NULL; in alloc_loaded_vmcs()
2722 loaded_vmcs->hv_timer_soft_disabled = false; in alloc_loaded_vmcs()
2723 loaded_vmcs->cpu = -1; in alloc_loaded_vmcs()
2724 loaded_vmcs->launched = 0; in alloc_loaded_vmcs()
2727 loaded_vmcs->msr_bitmap = (unsigned long *) in alloc_loaded_vmcs()
2729 if (!loaded_vmcs->msr_bitmap) in alloc_loaded_vmcs()
2731 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); in alloc_loaded_vmcs()
2734 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state)); in alloc_loaded_vmcs()
2735 memset(&loaded_vmcs->controls_shadow, 0, in alloc_loaded_vmcs()
2742 return -ENOMEM; in alloc_loaded_vmcs()
2765 return -ENOMEM; in alloc_kvm_area()
2770 * vmcs->revision_id to KVM_EVMCS_VERSION instead of in alloc_kvm_area()
2779 vmcs->hdr.revision_id = vmcs_config.revision_id; in alloc_kvm_area()
2798 save->selector &= ~SEGMENT_RPL_MASK; in fix_pmode_seg()
2799 save->dpl = save->selector & SEGMENT_RPL_MASK; in fix_pmode_seg()
2800 save->s = 1; in fix_pmode_seg()
2811 * Update real mode segment cache. It may be not up-to-date if sement in enter_pmode()
2814 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); in enter_pmode()
2815 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); in enter_pmode()
2816 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); in enter_pmode()
2817 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); in enter_pmode()
2818 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); in enter_pmode()
2819 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); in enter_pmode()
2821 vmx->rmode.vm86_active = 0; in enter_pmode()
2823 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); in enter_pmode()
2827 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; in enter_pmode()
2835 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); in enter_pmode()
2836 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); in enter_pmode()
2837 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); in enter_pmode()
2838 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); in enter_pmode()
2839 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); in enter_pmode()
2840 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); in enter_pmode()
2864 if (save->base & 0xf) in fix_rmode_seg()
2870 vmcs_write16(sf->selector, var.selector); in fix_rmode_seg()
2871 vmcs_writel(sf->base, var.base); in fix_rmode_seg()
2872 vmcs_write32(sf->limit, var.limit); in fix_rmode_seg()
2873 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); in fix_rmode_seg()
2880 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); in enter_rmode()
2882 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); in enter_rmode()
2883 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); in enter_rmode()
2884 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); in enter_rmode()
2885 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); in enter_rmode()
2886 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); in enter_rmode()
2887 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); in enter_rmode()
2888 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); in enter_rmode()
2890 vmx->rmode.vm86_active = 1; in enter_rmode()
2896 if (!kvm_vmx->tss_addr) in enter_rmode()
2902 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); in enter_rmode()
2903 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); in enter_rmode()
2907 vmx->rmode.save_rflags = flags; in enter_rmode()
2915 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); in enter_rmode()
2916 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); in enter_rmode()
2917 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); in enter_rmode()
2918 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); in enter_rmode()
2919 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); in enter_rmode()
2920 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); in enter_rmode()
2934 vcpu->arch.efer = efer; in vmx_set_efer()
2937 msr->data = efer; in vmx_set_efer()
2941 msr->data = efer & ~EFER_LME; in vmx_set_efer()
2963 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); in enter_lmode()
2969 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); in exit_lmode()
2980 * the CPU is not required to invalidate guest-physical mappings on in vmx_flush_tlb_all()
2981 * VM-Entry, even if VPID is disabled. Guest-physical mappings are in vmx_flush_tlb_all()
2983 * (INVVPID also isn't required to invalidate guest-physical mappings). in vmx_flush_tlb_all()
2991 vpid_sync_vcpu_single(vmx->vpid); in vmx_flush_tlb_all()
2992 vpid_sync_vcpu_single(vmx->nested.vpid02); in vmx_flush_tlb_all()
3001 return to_vmx(vcpu)->vpid; in vmx_get_current_vpid()
3006 struct kvm_mmu *mmu = vcpu->arch.mmu; in vmx_flush_tlb_current()
3007 u64 root_hpa = mmu->root_hpa; in vmx_flush_tlb_current()
3015 mmu->shadow_root_level)); in vmx_flush_tlb_current()
3033 * vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit are in vmx_flush_tlb_guest()
3034 * required to flush GVA->{G,H}PA mappings from the TLB if vpid is in vmx_flush_tlb_guest()
3035 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed), in vmx_flush_tlb_guest()
3043 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vmx_ept_load_pdptrs()
3049 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); in vmx_ept_load_pdptrs()
3050 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); in vmx_ept_load_pdptrs()
3051 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); in vmx_ept_load_pdptrs()
3052 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); in vmx_ept_load_pdptrs()
3058 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in ept_save_pdptrs()
3063 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); in ept_save_pdptrs()
3064 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); in ept_save_pdptrs()
3065 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); in ept_save_pdptrs()
3066 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); in ept_save_pdptrs()
3088 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) in vmx_set_cr0()
3091 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) in vmx_set_cr0()
3096 if (vcpu->arch.efer & EFER_LME) { in vmx_set_cr0()
3106 * Ensure KVM has an up-to-date snapshot of the guest's CR3. If in vmx_set_cr0()
3123 * e.g. after nested VM-Enter. in vmx_set_cr0()
3136 tmp |= get_vmcs12(vcpu)->cpu_based_vm_exec_control & CR3_EXITING_BITS; in vmx_set_cr0()
3141 vcpu->arch.cr0 = cr0; in vmx_set_cr0()
3148 vcpu->arch.cr0 = cr0; in vmx_set_cr0()
3151 /* depends on vcpu->arch.cr0 to be set to a new value */ in vmx_set_cr0()
3152 vmx->emulation_required = emulation_required(vcpu); in vmx_set_cr0()
3180 struct kvm *kvm = vcpu->kvm; in vmx_load_mmu_pgd()
3190 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); in vmx_load_mmu_pgd()
3191 to_vmx(vcpu)->ept_pointer = eptp; in vmx_load_mmu_pgd()
3192 to_kvm_vmx(kvm)->ept_pointers_match in vmx_load_mmu_pgd()
3194 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); in vmx_load_mmu_pgd()
3198 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; in vmx_load_mmu_pgd()
3199 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) in vmx_load_mmu_pgd()
3200 guest_cr3 = vcpu->arch.cr3; in vmx_load_mmu_pgd()
3201 else /* vmcs01.GUEST_CR3 is already up-to-date. */ in vmx_load_mmu_pgd()
3222 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) in vmx_is_valid_cr4()
3241 else if (vmx->rmode.vm86_active) in vmx_set_cr4()
3256 vcpu->arch.cr4 = cr4; in vmx_set_cr4()
3270 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in in vmx_set_cr4()
3272 * to be manually disabled when guest switches to non-paging in vmx_set_cr4()
3293 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { in vmx_get_segment()
3294 *var = vmx->rmode.segs[seg]; in vmx_get_segment()
3296 || var->selector == vmx_read_guest_seg_selector(vmx, seg)) in vmx_get_segment()
3298 var->base = vmx_read_guest_seg_base(vmx, seg); in vmx_get_segment()
3299 var->selector = vmx_read_guest_seg_selector(vmx, seg); in vmx_get_segment()
3302 var->base = vmx_read_guest_seg_base(vmx, seg); in vmx_get_segment()
3303 var->limit = vmx_read_guest_seg_limit(vmx, seg); in vmx_get_segment()
3304 var->selector = vmx_read_guest_seg_selector(vmx, seg); in vmx_get_segment()
3306 var->unusable = (ar >> 16) & 1; in vmx_get_segment()
3307 var->type = ar & 15; in vmx_get_segment()
3308 var->s = (ar >> 4) & 1; in vmx_get_segment()
3309 var->dpl = (ar >> 5) & 3; in vmx_get_segment()
3317 var->present = !var->unusable; in vmx_get_segment()
3318 var->avl = (ar >> 12) & 1; in vmx_get_segment()
3319 var->l = (ar >> 13) & 1; in vmx_get_segment()
3320 var->db = (ar >> 14) & 1; in vmx_get_segment()
3321 var->g = (ar >> 15) & 1; in vmx_get_segment()
3328 if (to_vmx(vcpu)->rmode.vm86_active) { in vmx_get_segment_base()
3339 if (unlikely(vmx->rmode.vm86_active)) in vmx_get_cpl()
3351 ar = var->type & 15; in vmx_segment_access_rights()
3352 ar |= (var->s & 1) << 4; in vmx_segment_access_rights()
3353 ar |= (var->dpl & 3) << 5; in vmx_segment_access_rights()
3354 ar |= (var->present & 1) << 7; in vmx_segment_access_rights()
3355 ar |= (var->avl & 1) << 12; in vmx_segment_access_rights()
3356 ar |= (var->l & 1) << 13; in vmx_segment_access_rights()
3357 ar |= (var->db & 1) << 14; in vmx_segment_access_rights()
3358 ar |= (var->g & 1) << 15; in vmx_segment_access_rights()
3359 ar |= (var->unusable || !var->present) << 16; in vmx_segment_access_rights()
3371 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { in vmx_set_segment()
3372 vmx->rmode.segs[seg] = *var; in vmx_set_segment()
3374 vmcs_write16(sf->selector, var->selector); in vmx_set_segment()
3375 else if (var->s) in vmx_set_segment()
3376 fix_rmode_seg(seg, &vmx->rmode.segs[seg]); in vmx_set_segment()
3380 vmcs_writel(sf->base, var->base); in vmx_set_segment()
3381 vmcs_write32(sf->limit, var->limit); in vmx_set_segment()
3382 vmcs_write16(sf->selector, var->selector); in vmx_set_segment()
3396 var->type |= 0x1; /* Accessed */ in vmx_set_segment()
3398 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); in vmx_set_segment()
3401 vmx->emulation_required = emulation_required(vcpu); in vmx_set_segment()
3414 dt->size = vmcs_read32(GUEST_IDTR_LIMIT); in vmx_get_idt()
3415 dt->address = vmcs_readl(GUEST_IDTR_BASE); in vmx_get_idt()
3420 vmcs_write32(GUEST_IDTR_LIMIT, dt->size); in vmx_set_idt()
3421 vmcs_writel(GUEST_IDTR_BASE, dt->address); in vmx_set_idt()
3426 dt->size = vmcs_read32(GUEST_GDTR_LIMIT); in vmx_get_gdt()
3427 dt->address = vmcs_readl(GUEST_GDTR_BASE); in vmx_get_gdt()
3432 vmcs_write32(GUEST_GDTR_LIMIT, dt->size); in vmx_set_gdt()
3433 vmcs_writel(GUEST_GDTR_BASE, dt->address); in vmx_set_gdt()
3622 * - Add checks on RIP in __vmx_guest_state_valid()
3623 * - Add checks on RFLAGS in __vmx_guest_state_valid()
3635 idx = srcu_read_lock(&kvm->srcu); in init_rmode_tss()
3636 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; in init_rmode_tss()
3653 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, in init_rmode_tss()
3656 srcu_read_unlock(&kvm->srcu, idx); in init_rmode_tss()
3667 /* Protect kvm_vmx->ept_identity_pagetable_done. */ in init_rmode_identity_map()
3668 mutex_lock(&kvm->slots_lock); in init_rmode_identity_map()
3670 if (likely(kvm_vmx->ept_identity_pagetable_done)) in init_rmode_identity_map()
3673 if (!kvm_vmx->ept_identity_map_addr) in init_rmode_identity_map()
3674 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; in init_rmode_identity_map()
3675 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; in init_rmode_identity_map()
3678 kvm_vmx->ept_identity_map_addr, PAGE_SIZE); in init_rmode_identity_map()
3685 /* Set up identity-mapping pagetable for EPT in real mode */ in init_rmode_identity_map()
3694 kvm_vmx->ept_identity_pagetable_done = true; in init_rmode_identity_map()
3697 mutex_unlock(&kvm->slots_lock); in init_rmode_identity_map()
3706 vmcs_write16(sf->selector, 0); in seg_setup()
3707 vmcs_writel(sf->base, 0); in seg_setup()
3708 vmcs_write32(sf->limit, 0xffff); in seg_setup()
3713 vmcs_write32(sf->ar_bytes, ar); in seg_setup()
3721 mutex_lock(&kvm->slots_lock); in alloc_apic_access_page()
3722 if (kvm->arch.apic_access_page_done) in alloc_apic_access_page()
3731 r = -EFAULT; in alloc_apic_access_page()
3736 * Do not pin the page in memory, so that memory hot-unplug in alloc_apic_access_page()
3740 kvm->arch.apic_access_page_done = true; in alloc_apic_access_page()
3742 mutex_unlock(&kvm->slots_lock); in alloc_apic_access_page()
3814 * When KVM is a nested hypervisor on top of Hyper-V and uses in vmx_msr_bitmap_l01_changed()
3819 struct hv_enlightened_vmcs *evmcs = (void *)vmx->vmcs01.vmcs; in vmx_msr_bitmap_l01_changed()
3821 if (evmcs->hv_enlightenments_control.msr_bitmap) in vmx_msr_bitmap_l01_changed()
3822 evmcs->hv_clean_fields &= in vmx_msr_bitmap_l01_changed()
3831 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; in vmx_disable_intercept_for_msr()
3845 if (idx != -ENOENT) { in vmx_disable_intercept_for_msr()
3847 clear_bit(idx, vmx->shadow_msr_intercept.read); in vmx_disable_intercept_for_msr()
3849 clear_bit(idx, vmx->shadow_msr_intercept.write); in vmx_disable_intercept_for_msr()
3876 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; in vmx_enable_intercept_for_msr()
3890 if (idx != -ENOENT) { in vmx_enable_intercept_for_msr()
3892 set_bit(idx, vmx->shadow_msr_intercept.read); in vmx_enable_intercept_for_msr()
3894 set_bit(idx, vmx->shadow_msr_intercept.write); in vmx_enable_intercept_for_msr()
3931 unsigned long *msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; in vmx_reset_x2apic_msrs()
3971 u8 changed = mode ^ vmx->msr_bitmap_mode; in vmx_update_msr_bitmap()
3979 vmx->msr_bitmap_mode = mode; in vmx_update_msr_bitmap()
3985 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN); in pt_update_intercept_for_msr()
3992 for (i = 0; i < vmx->pt_desc.addr_range; i++) { in pt_update_intercept_for_msr()
4007 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn)) in vmx_guest_apic_has_interrupt()
4012 vapic_page = vmx->nested.virtual_apic_map.hva; in vmx_guest_apic_has_interrupt()
4030 bool read = test_bit(i, vmx->shadow_msr_intercept.read); in vmx_msr_filter_changed()
4031 bool write = test_bit(i, vmx->shadow_msr_intercept.write); in vmx_msr_filter_changed()
4047 if (vcpu->mode == IN_GUEST_MODE) { in kvm_vcpu_trigger_posted_interrupt()
4056 * Case 1: vcpu keeps in non-root mode. Sending a in kvm_vcpu_trigger_posted_interrupt()
4073 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); in kvm_vcpu_trigger_posted_interrupt()
4086 vector == vmx->nested.posted_intr_nv) { in vmx_deliver_nested_posted_interrupt()
4091 vmx->nested.pi_pending = true; in vmx_deliver_nested_posted_interrupt()
4098 return -1; in vmx_deliver_nested_posted_interrupt()
4102 * 1. If target vcpu is running(non-root mode), send posted interrupt
4116 if (!vcpu->arch.apicv_active) in vmx_deliver_posted_interrupt()
4117 return -1; in vmx_deliver_posted_interrupt()
4119 if (pi_test_and_set_pir(vector, &vmx->pi_desc)) in vmx_deliver_posted_interrupt()
4123 if (pi_test_and_set_on(&vmx->pi_desc)) in vmx_deliver_posted_interrupt()
4133 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4135 * Note that host-state that does change is set elsewhere. E.g., host-state
4154 vmx->loaded_vmcs->host_state.cr3 = cr3; in vmx_set_constant_host_state()
4159 vmx->loaded_vmcs->host_state.cr4 = cr4; in vmx_set_constant_host_state()
4197 struct kvm_vcpu *vcpu = &vmx->vcpu; in set_cr4_guest_host_mask()
4199 vcpu->arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS & in set_cr4_guest_host_mask()
4200 ~vcpu->arch.cr4_guest_rsvd_bits; in set_cr4_guest_host_mask()
4202 vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_PGE; in set_cr4_guest_host_mask()
4203 if (is_guest_mode(&vmx->vcpu)) in set_cr4_guest_host_mask()
4204 vcpu->arch.cr4_guest_owned_bits &= in set_cr4_guest_host_mask()
4205 ~get_vmcs12(vcpu)->cr4_guest_host_mask; in set_cr4_guest_host_mask()
4206 vmcs_writel(CR4_GUEST_HOST_MASK, ~vcpu->arch.cr4_guest_owned_bits); in set_cr4_guest_host_mask()
4213 if (!kvm_vcpu_apicv_active(&vmx->vcpu)) in vmx_pin_based_exec_ctrl()
4249 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) in vmx_exec_control()
4252 if (!cpu_need_tpr_shadow(&vmx->vcpu)) { in vmx_exec_control()
4263 if (kvm_mwait_in_guest(vmx->vcpu.kvm)) in vmx_exec_control()
4266 if (kvm_hlt_in_guest(vmx->vcpu.kvm)) in vmx_exec_control()
4281 * If the control is for an opt-in feature, clear the control if the in vmx_adjust_secondary_exec_control()
4283 * control is opt-out, i.e. an exiting control, clear the control if in vmx_adjust_secondary_exec_control()
4297 vmx->nested.msrs.secondary_ctls_high |= control; in vmx_adjust_secondary_exec_control()
4299 vmx->nested.msrs.secondary_ctls_high &= ~control; in vmx_adjust_secondary_exec_control()
4313 __enabled = guest_cpuid_has(&(vmx)->vcpu, \
4320 /* More macro magic for ENABLE_/opt-in versus _EXITING/opt-out controls. */
4329 struct kvm_vcpu *vcpu = &vmx->vcpu; in vmx_compute_secondary_exec_control()
4337 if (vmx->vpid == 0) in vmx_compute_secondary_exec_control()
4345 if (kvm_pause_in_guest(vmx->vcpu.kvm)) in vmx_compute_secondary_exec_control()
4373 vcpu->arch.xsaves_enabled = xsaves_enabled; in vmx_compute_secondary_exec_control()
4399 vmx->secondary_exec_control = exec_control; in vmx_compute_secondary_exec_control()
4406 * of an EPT paging-structure entry is 110b (write/execute). in ept_set_mmio_spte_mask()
4414 * Noting that the initialization of Guest-state Area of VMCS is in
4423 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); in init_vmcs()
4425 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ in init_vmcs()
4434 secondary_exec_controls_set(vmx, vmx->secondary_exec_control); in init_vmcs()
4437 if (kvm_vcpu_apicv_active(&vmx->vcpu)) { in init_vmcs()
4446 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); in init_vmcs()
4449 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { in init_vmcs()
4451 vmx->ple_window = ple_window; in init_vmcs()
4452 vmx->ple_window_dirty = true; in init_vmcs()
4470 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); in init_vmcs()
4472 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); in init_vmcs()
4475 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); in init_vmcs()
4482 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; in init_vmcs()
4483 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits); in init_vmcs()
4487 if (vmx->vpid != 0) in init_vmcs()
4488 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); in init_vmcs()
4494 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); in init_vmcs()
4495 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); in init_vmcs()
4499 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); in init_vmcs()
4502 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc)); in init_vmcs()
4504 vmx->pt_desc.guest.output_mask = 0x7F; in init_vmcs()
4515 vmx->rmode.vm86_active = 0; in vmx_vcpu_reset()
4516 vmx->spec_ctrl = 0; in vmx_vcpu_reset()
4518 vmx->msr_ia32_umwait_control = 0; in vmx_vcpu_reset()
4520 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); in vmx_vcpu_reset()
4521 vmx->hv_deadline_tsc = -1; in vmx_vcpu_reset()
4585 __pa(vcpu->arch.apic->regs)); in vmx_vcpu_reset()
4592 vmx->vcpu.arch.cr0 = cr0; in vmx_vcpu_reset()
4599 vpid_sync_context(vmx->vpid); in vmx_vcpu_reset()
4626 int irq = vcpu->arch.interrupt.nr; in vmx_inject_irq()
4630 ++vcpu->stat.irq_injections; in vmx_inject_irq()
4631 if (vmx->rmode.vm86_active) { in vmx_inject_irq()
4633 if (vcpu->arch.interrupt.soft) in vmx_inject_irq()
4634 inc_eip = vcpu->arch.event_exit_inst_len; in vmx_inject_irq()
4639 if (vcpu->arch.interrupt.soft) { in vmx_inject_irq()
4642 vmx->vcpu.arch.event_exit_inst_len); in vmx_inject_irq()
4656 * Tracking the NMI-blocked state in software is built upon in vmx_inject_nmi()
4658 * well-behaving guests: They have to keep IRQs disabled at in vmx_inject_nmi()
4663 vmx->loaded_vmcs->soft_vnmi_blocked = 1; in vmx_inject_nmi()
4664 vmx->loaded_vmcs->vnmi_blocked_time = 0; in vmx_inject_nmi()
4667 ++vcpu->stat.nmi_injections; in vmx_inject_nmi()
4668 vmx->loaded_vmcs->nmi_known_unmasked = false; in vmx_inject_nmi()
4670 if (vmx->rmode.vm86_active) { in vmx_inject_nmi()
4687 return vmx->loaded_vmcs->soft_vnmi_blocked; in vmx_get_nmi_mask()
4688 if (vmx->loaded_vmcs->nmi_known_unmasked) in vmx_get_nmi_mask()
4691 vmx->loaded_vmcs->nmi_known_unmasked = !masked; in vmx_get_nmi_mask()
4700 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { in vmx_set_nmi_mask()
4701 vmx->loaded_vmcs->soft_vnmi_blocked = masked; in vmx_set_nmi_mask()
4702 vmx->loaded_vmcs->vnmi_blocked_time = 0; in vmx_set_nmi_mask()
4705 vmx->loaded_vmcs->nmi_known_unmasked = !masked; in vmx_set_nmi_mask()
4720 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) in vmx_nmi_blocked()
4730 if (to_vmx(vcpu)->nested.nested_run_pending) in vmx_nmi_allowed()
4731 return -EBUSY; in vmx_nmi_allowed()
4733 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */ in vmx_nmi_allowed()
4735 return -EBUSY; in vmx_nmi_allowed()
4752 if (to_vmx(vcpu)->nested.nested_run_pending) in vmx_interrupt_allowed()
4753 return -EBUSY; in vmx_interrupt_allowed()
4756 * An IRQ must not be injected into L2 if it's supposed to VM-Exit, in vmx_interrupt_allowed()
4760 return -EBUSY; in vmx_interrupt_allowed()
4772 mutex_lock(&kvm->slots_lock); in vmx_set_tss_addr()
4775 mutex_unlock(&kvm->slots_lock); in vmx_set_tss_addr()
4779 to_kvm_vmx(kvm)->tss_addr = addr; in vmx_set_tss_addr()
4785 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; in vmx_set_identity_map_addr()
4797 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = in rmode_exception()
4799 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) in rmode_exception()
4803 return !(vcpu->guest_debug & in rmode_exception()
4827 if (vcpu->arch.halt_request) { in handle_rmode_exception()
4828 vcpu->arch.halt_request = 0; in handle_rmode_exception()
4877 * - Guest CPL == 3 (user mode)
4878 * - Guest has #AC detection enabled in CR0
4879 * - Guest EFLAGS has AC bit set
4893 struct kvm_run *kvm_run = vcpu->run; in handle_exception_nmi()
4898 vect_info = vmx->idt_vectoring_info; in handle_exception_nmi()
4911 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { in handle_exception_nmi()
4916 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero in handle_exception_nmi()
4933 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in handle_exception_nmi()
4934 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; in handle_exception_nmi()
4935 vcpu->run->internal.ndata = 4; in handle_exception_nmi()
4936 vcpu->run->internal.data[0] = vect_info; in handle_exception_nmi()
4937 vcpu->run->internal.data[1] = intr_info; in handle_exception_nmi()
4938 vcpu->run->internal.data[2] = error_code; in handle_exception_nmi()
4939 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu; in handle_exception_nmi()
4945 if (enable_ept && !vcpu->arch.apf.host_apf_flags) { in handle_exception_nmi()
4959 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) in handle_exception_nmi()
4965 if (!(vcpu->guest_debug & in handle_exception_nmi()
4969 * instruction. ICEBP generates a trap-like #DB, but in handle_exception_nmi()
4971 * is an instruction intercept, i.e. the VM-Exit occurs in handle_exception_nmi()
4976 * if single-step is enabled in RFLAGS and STI or MOVSS in handle_exception_nmi()
4978 * on VM-Exit due to #DB interception. VM-Entry has a in handle_exception_nmi()
4979 * consistency check that a single-step #DB is pending in handle_exception_nmi()
4983 * delay when activating single-step breakpoints must in handle_exception_nmi()
4985 * as appropriate for all other VM-Exits types. in handle_exception_nmi()
4998 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM; in handle_exception_nmi()
4999 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); in handle_exception_nmi()
5007 vmx->vcpu.arch.event_exit_inst_len = in handle_exception_nmi()
5009 kvm_run->exit_reason = KVM_EXIT_DEBUG; in handle_exception_nmi()
5011 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; in handle_exception_nmi()
5012 kvm_run->debug.arch.exception = ex_no; in handle_exception_nmi()
5029 kvm_run->exit_reason = KVM_EXIT_EXCEPTION; in handle_exception_nmi()
5030 kvm_run->ex.exception = ex_no; in handle_exception_nmi()
5031 kvm_run->ex.error_code = error_code; in handle_exception_nmi()
5039 ++vcpu->stat.irq_exits; in handle_external_interrupt()
5045 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in handle_triple_fault()
5046 vcpu->mmio_needed = 0; in handle_triple_fault()
5059 ++vcpu->stat.io_exits; in handle_io()
5082 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
5094 * hardware. It consists of the L2-owned bits from the new in handle_set_cr0()
5095 * value combined with the L1-owned bits from L1's guest_cr0. in handle_set_cr0()
5097 val = (val & ~vmcs12->cr0_guest_host_mask) | in handle_set_cr0()
5098 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); in handle_set_cr0()
5108 if (to_vmx(vcpu)->nested.vmxon && in handle_set_cr0()
5123 val = (val & ~vmcs12->cr4_guest_host_mask) | in handle_set_cr4()
5124 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); in handle_set_cr4()
5135 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); in handle_desc()
5176 * KVM_GUESTDBG_SINGLESTEP-triggered in handle_cr()
5179 vcpu->run->exit_reason = KVM_EXIT_SET_TPR; in handle_cr()
5213 vcpu->run->exit_reason = 0; in handle_cr()
5231 /* Do not handle if the CPL > 0, will trigger GP on re-entry */ in handle_dr()
5237 * As the vm-exit takes precedence over the debug trap, we in handle_dr()
5241 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in handle_dr()
5242 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_RTM | DR6_FIXED_1; in handle_dr()
5243 vcpu->run->debug.arch.dr7 = dr7; in handle_dr()
5244 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); in handle_dr()
5245 vcpu->run->debug.arch.exception = DB_VECTOR; in handle_dr()
5246 vcpu->run->exit_reason = KVM_EXIT_DEBUG; in handle_dr()
5254 if (vcpu->guest_debug == 0) { in handle_dr()
5262 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; in handle_dr()
5282 get_debugreg(vcpu->arch.db[0], 0); in vmx_sync_dirty_debug_regs()
5283 get_debugreg(vcpu->arch.db[1], 1); in vmx_sync_dirty_debug_regs()
5284 get_debugreg(vcpu->arch.db[2], 2); in vmx_sync_dirty_debug_regs()
5285 get_debugreg(vcpu->arch.db[3], 3); in vmx_sync_dirty_debug_regs()
5286 get_debugreg(vcpu->arch.dr6, 6); in vmx_sync_dirty_debug_regs()
5287 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); in vmx_sync_dirty_debug_regs()
5289 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; in vmx_sync_dirty_debug_regs()
5310 ++vcpu->stat.irq_window_exits; in handle_interrupt_window()
5366 * not cared. So make a short-circuit here by avoiding in handle_apic_access()
5383 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ in handle_apic_eoi_induced()
5393 /* APIC-write VM exit is trap-like and thus no need to adjust IP */ in handle_apic_write()
5407 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); in handle_task_switch()
5408 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); in handle_task_switch()
5409 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); in handle_task_switch()
5417 vcpu->arch.nmi_injected = false; in handle_task_switch()
5425 if (vmx->idt_vectoring_info & in handle_task_switch()
5451 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, in handle_task_switch()
5469 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && in handle_ept_violation()
5495 vcpu->arch.exit_qualification = exit_qualification; in handle_ept_violation()
5502 * would also use advanced VM-exit information for EPT violations to in handle_ept_violation()
5533 ++vcpu->stat.nmi_window_exits; in handle_nmi_window()
5548 while (vmx->emulation_required && count-- != 0) { in handle_invalid_guest_state()
5550 return handle_interrupt_window(&vmx->vcpu); in handle_invalid_guest_state()
5558 if (vmx->emulation_required && !vmx->rmode.vm86_active && in handle_invalid_guest_state()
5559 vcpu->arch.exception.pending) { in handle_invalid_guest_state()
5560 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in handle_invalid_guest_state()
5561 vcpu->run->internal.suberror = in handle_invalid_guest_state()
5563 vcpu->run->internal.ndata = 0; in handle_invalid_guest_state()
5567 if (vcpu->arch.halt_request) { in handle_invalid_guest_state()
5568 vcpu->arch.halt_request = 0; in handle_invalid_guest_state()
5587 unsigned int old = vmx->ple_window; in grow_ple_window()
5589 vmx->ple_window = __grow_ple_window(old, ple_window, in grow_ple_window()
5593 if (vmx->ple_window != old) { in grow_ple_window()
5594 vmx->ple_window_dirty = true; in grow_ple_window()
5595 trace_kvm_ple_window_update(vcpu->vcpu_id, in grow_ple_window()
5596 vmx->ple_window, old); in grow_ple_window()
5603 unsigned int old = vmx->ple_window; in shrink_ple_window()
5605 vmx->ple_window = __shrink_ple_window(old, ple_window, in shrink_ple_window()
5609 if (vmx->ple_window != old) { in shrink_ple_window()
5610 vmx->ple_window_dirty = true; in shrink_ple_window()
5611 trace_kvm_ple_window_update(vcpu->vcpu_id, in shrink_ple_window()
5612 vmx->ple_window, old); in shrink_ple_window()
5629 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5630 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5634 if (!kvm_pause_in_guest(vcpu->kvm)) in handle_pause()
5638 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" in handle_pause()
5639 * VM-execution control is ignored if CPL > 0. OTOH, KVM in handle_pause()
5713 trace_kvm_pml_full(vcpu->vcpu_id); in handle_pml_full()
5721 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && in handle_pml_full()
5738 if (!vmx->req_immediate_exit && in handle_fastpath_preemption_timer()
5739 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) { in handle_fastpath_preemption_timer()
5841 if (!(vmx->exit_reason.failed_vmentry)) { in vmx_get_exit_info()
5842 *info2 = vmx->idt_vectoring_info; in vmx_get_exit_info()
5857 if (vmx->pml_pg) { in vmx_destroy_pml_buffer()
5858 __free_page(vmx->pml_pg); in vmx_destroy_pml_buffer()
5859 vmx->pml_pg = NULL; in vmx_destroy_pml_buffer()
5872 if (pml_idx == (PML_ENTITY_NUM - 1)) in vmx_flush_pml_buffer()
5881 pml_buf = page_address(vmx->pml_pg); in vmx_flush_pml_buffer()
5886 WARN_ON(gpa & (PAGE_SIZE - 1)); in vmx_flush_pml_buffer()
5891 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); in vmx_flush_pml_buffer()
5916 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), in vmx_dump_sel()
5917 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), in vmx_dump_sel()
5918 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); in vmx_dump_sel()
5925 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); in vmx_dump_dtsel()
6060 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR)); in dump_vmcs()
6061 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR)); in dump_vmcs()
6082 union vmx_exit_reason exit_reason = vmx->exit_reason; in vmx_handle_exit()
6083 u32 vectoring_info = vmx->idt_vectoring_info; in vmx_handle_exit()
6097 * We should never reach this point with a pending nested VM-Enter, and in vmx_handle_exit()
6100 * nested VM-Enter with an invalid vmcs12. in vmx_handle_exit()
6102 WARN_ON_ONCE(vmx->nested.nested_run_pending); in vmx_handle_exit()
6105 if (vmx->emulation_required) in vmx_handle_exit()
6114 * address-translation-based dirty tracking (e.g. EPT write in vmx_handle_exit()
6128 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; in vmx_handle_exit()
6129 vcpu->run->fail_entry.hardware_entry_failure_reason in vmx_handle_exit()
6131 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; in vmx_handle_exit()
6135 if (unlikely(vmx->fail)) { in vmx_handle_exit()
6137 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; in vmx_handle_exit()
6138 vcpu->run->fail_entry.hardware_entry_failure_reason in vmx_handle_exit()
6140 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; in vmx_handle_exit()
6148 * The vm-exit can be triggered again after return to guest that in vmx_handle_exit()
6159 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in vmx_handle_exit()
6160 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; in vmx_handle_exit()
6161 vcpu->run->internal.data[0] = vectoring_info; in vmx_handle_exit()
6162 vcpu->run->internal.data[1] = exit_reason.full; in vmx_handle_exit()
6163 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; in vmx_handle_exit()
6165 vcpu->run->internal.data[ndata++] = in vmx_handle_exit()
6168 vcpu->run->internal.data[ndata++] = vcpu->arch.last_vmentry_cpu; in vmx_handle_exit()
6169 vcpu->run->internal.ndata = ndata; in vmx_handle_exit()
6174 vmx->loaded_vmcs->soft_vnmi_blocked)) { in vmx_handle_exit()
6176 vmx->loaded_vmcs->soft_vnmi_blocked = 0; in vmx_handle_exit()
6177 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && in vmx_handle_exit()
6178 vcpu->arch.nmi_pending) { in vmx_handle_exit()
6181 * NMI-blocked window if the guest runs with IRQs in vmx_handle_exit()
6185 printk(KERN_WARNING "%s: Breaking out of NMI-blocked " in vmx_handle_exit()
6187 __func__, vcpu->vcpu_id); in vmx_handle_exit()
6188 vmx->loaded_vmcs->soft_vnmi_blocked = 0; in vmx_handle_exit()
6223 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in vmx_handle_exit()
6224 vcpu->run->internal.suberror = in vmx_handle_exit()
6226 vcpu->run->internal.ndata = 2; in vmx_handle_exit()
6227 vcpu->run->internal.data[0] = exit_reason.full; in vmx_handle_exit()
6228 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; in vmx_handle_exit()
6254 * Clear the per-vcpu flush bit, it gets set again in vmx_l1d_flush()
6258 flush_l1d = vcpu->arch.l1tf_flush_l1d; in vmx_l1d_flush()
6259 vcpu->arch.l1tf_flush_l1d = false; in vmx_l1d_flush()
6262 * Clear the per-cpu flush bit, it gets set again from in vmx_l1d_flush()
6272 vcpu->stat.l1d_flush++; in vmx_l1d_flush()
6311 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr; in update_cr8_intercept()
6313 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold; in update_cr8_intercept()
6332 vmx->nested.change_vmcs01_virtual_apic_mode = true; in vmx_set_virtual_apic_mode()
6354 * the guest may have inserted a non-APIC mapping into in vmx_set_virtual_apic_mode()
6377 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true; in vmx_set_apic_access_page_addr()
6385 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); in vmx_set_apic_access_page_addr()
6404 if (max_isr == -1) in vmx_hwapic_isr_update()
6421 if (vector == -1) in vmx_set_rvi()
6437 * vmcs12 virtual-interrupt-delivery enabled. in vmx_hwapic_irr_update()
6439 * intercepts external-interrupts and in that case in vmx_hwapic_irr_update()
6453 WARN_ON(!vcpu->arch.apicv_active); in vmx_sync_pir_to_irr()
6454 if (pi_test_on(&vmx->pi_desc)) { in vmx_sync_pir_to_irr()
6455 pi_clear_on(&vmx->pi_desc); in vmx_sync_pir_to_irr()
6462 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); in vmx_sync_pir_to_irr()
6495 pi_clear_on(&vmx->pi_desc); in vmx_apicv_post_state_restore()
6496 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); in vmx_apicv_post_state_restore()
6512 u32 intr_info = vmx_get_intr_info(&vmx->vcpu); in handle_exception_nmi_irqoff()
6516 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags(); in handle_exception_nmi_irqoff()
6522 handle_interrupt_nmi_irqoff(&vmx->vcpu, nmi_entry); in handle_exception_nmi_irqoff()
6532 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info)) in handle_external_interrupt_irqoff()
6536 vcpu->arch.at_instruction_boundary = true; in handle_external_interrupt_irqoff()
6543 if (vmx->emulation_required) in vmx_handle_exit_irqoff()
6546 if (vmx->exit_reason.basic == EXIT_REASON_EXTERNAL_INTERRUPT) in vmx_handle_exit_irqoff()
6548 else if (vmx->exit_reason.basic == EXIT_REASON_EXCEPTION_NMI) in vmx_handle_exit_irqoff()
6578 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; in vmx_recover_nmi_blocking()
6581 if (vmx->loaded_vmcs->nmi_known_unmasked) in vmx_recover_nmi_blocking()
6584 exit_intr_info = vmx_get_intr_info(&vmx->vcpu); in vmx_recover_nmi_blocking()
6589 * Re-set bit "block by NMI" before VM entry if vmexit caused by in vmx_recover_nmi_blocking()
6593 * If the VM exit sets the valid bit in the IDT-vectoring in vmx_recover_nmi_blocking()
6602 vmx->loaded_vmcs->nmi_known_unmasked = in vmx_recover_nmi_blocking()
6605 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) in vmx_recover_nmi_blocking()
6606 vmx->loaded_vmcs->vnmi_blocked_time += in vmx_recover_nmi_blocking()
6608 vmx->loaded_vmcs->entry_time)); in vmx_recover_nmi_blocking()
6622 vcpu->arch.nmi_injected = false; in __vmx_complete_interrupts()
6636 vcpu->arch.nmi_injected = true; in __vmx_complete_interrupts()
6645 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); in __vmx_complete_interrupts()
6655 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); in __vmx_complete_interrupts()
6667 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, in vmx_complete_interrupts()
6706 if (vmx->req_immediate_exit) { in vmx_update_hv_timer()
6708 vmx->loaded_vmcs->hv_timer_soft_disabled = false; in vmx_update_hv_timer()
6709 } else if (vmx->hv_deadline_tsc != -1) { in vmx_update_hv_timer()
6711 if (vmx->hv_deadline_tsc > tscl) in vmx_update_hv_timer()
6712 /* set_hv_timer ensures the delta fits in 32-bits */ in vmx_update_hv_timer()
6713 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> in vmx_update_hv_timer()
6719 vmx->loaded_vmcs->hv_timer_soft_disabled = false; in vmx_update_hv_timer()
6720 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) { in vmx_update_hv_timer()
6721 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1); in vmx_update_hv_timer()
6722 vmx->loaded_vmcs->hv_timer_soft_disabled = true; in vmx_update_hv_timer()
6728 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) { in vmx_update_host_rsp()
6729 vmx->loaded_vmcs->host_state.rsp = host_rsp; in vmx_update_host_rsp()
6743 vmx->spec_ctrl = __rdmsr(MSR_IA32_SPEC_CTRL); in vmx_spec_ctrl_restore_host()
6753 vmx->spec_ctrl != hostval) in vmx_spec_ctrl_restore_host()
6761 switch (to_vmx(vcpu)->exit_reason.basic) { in vmx_exit_handlers_fastpath()
6801 kvm_arch_has_assigned_device(vcpu->kvm)) in vmx_vcpu_enter_exit()
6806 if (vcpu->arch.cr2 != native_read_cr2()) in vmx_vcpu_enter_exit()
6807 native_write_cr2(vcpu->arch.cr2); in vmx_vcpu_enter_exit()
6809 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, in vmx_vcpu_enter_exit()
6812 vcpu->arch.cr2 = native_read_cr2(); in vmx_vcpu_enter_exit()
6845 vmx->loaded_vmcs->soft_vnmi_blocked)) in vmx_vcpu_run()
6846 vmx->loaded_vmcs->entry_time = ktime_get(); in vmx_vcpu_run()
6850 if (vmx->emulation_required) in vmx_vcpu_run()
6853 if (vmx->ple_window_dirty) { in vmx_vcpu_run()
6854 vmx->ple_window_dirty = false; in vmx_vcpu_run()
6855 vmcs_write32(PLE_WINDOW, vmx->ple_window); in vmx_vcpu_run()
6862 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync); in vmx_vcpu_run()
6865 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); in vmx_vcpu_run()
6867 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); in vmx_vcpu_run()
6870 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { in vmx_vcpu_run()
6872 vmx->loaded_vmcs->host_state.cr3 = cr3; in vmx_vcpu_run()
6876 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { in vmx_vcpu_run()
6878 vmx->loaded_vmcs->host_state.cr4 = cr4; in vmx_vcpu_run()
6881 /* When single-stepping over STI and MOV SS, we must clear the in vmx_vcpu_run()
6886 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in vmx_vcpu_run()
6902 * it's non-zero. Since vmentry is serialising on affected CPUs, there in vmx_vcpu_run()
6906 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); in vmx_vcpu_run()
6913 current_evmcs->hv_clean_fields |= in vmx_vcpu_run()
6917 current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index; in vmx_vcpu_run()
6920 if (vmx->host_debugctlmsr) in vmx_vcpu_run()
6921 update_debugctlmsr(vmx->host_debugctlmsr); in vmx_vcpu_run()
6942 vmx->nested.nested_run_pending = 0; in vmx_vcpu_run()
6943 vmx->idt_vectoring_info = 0; in vmx_vcpu_run()
6945 if (unlikely(vmx->fail)) { in vmx_vcpu_run()
6946 vmx->exit_reason.full = 0xdead; in vmx_vcpu_run()
6950 vmx->exit_reason.full = vmcs_read32(VM_EXIT_REASON); in vmx_vcpu_run()
6951 if (unlikely((u16)vmx->exit_reason.basic == EXIT_REASON_MCE_DURING_VMENTRY)) in vmx_vcpu_run()
6954 trace_kvm_exit(vmx->exit_reason.full, vcpu, KVM_ISA_VMX); in vmx_vcpu_run()
6956 if (unlikely(vmx->exit_reason.failed_vmentry)) in vmx_vcpu_run()
6959 vmx->loaded_vmcs->launched = 1; in vmx_vcpu_run()
6960 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); in vmx_vcpu_run()
6976 if (vcpu->arch.apicv_active) in vmx_vcpu_run()
6992 free_vpid(vmx->vpid); in vmx_free_vcpu()
6994 free_loaded_vmcs(vmx->loaded_vmcs); in vmx_free_vcpu()
7005 err = -ENOMEM; in vmx_create_vcpu()
7007 vmx->vpid = allocate_vpid(); in vmx_create_vcpu()
7016 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); in vmx_create_vcpu()
7017 if (!vmx->pml_pg) in vmx_create_vcpu()
7025 int j = vmx->nr_uret_msrs; in vmx_create_vcpu()
7030 vmx->guest_uret_msrs[j].slot = i; in vmx_create_vcpu()
7031 vmx->guest_uret_msrs[j].data = 0; in vmx_create_vcpu()
7046 vmx->guest_uret_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR; in vmx_create_vcpu()
7048 vmx->guest_uret_msrs[j].mask = 0; in vmx_create_vcpu()
7051 vmx->guest_uret_msrs[j].mask = -1ull; in vmx_create_vcpu()
7054 ++vmx->nr_uret_msrs; in vmx_create_vcpu()
7057 err = alloc_loaded_vmcs(&vmx->vmcs01); in vmx_create_vcpu()
7062 * Use Hyper-V 'Enlightened MSR Bitmap' feature when KVM runs as a in vmx_create_vcpu()
7063 * nested (L1) hypervisor and Hyper-V in L0 supports it. Enable the in vmx_create_vcpu()
7069 struct hv_enlightened_vmcs *evmcs = (void *)vmx->vmcs01.vmcs; in vmx_create_vcpu()
7071 evmcs->hv_enlightenments_control.msr_bitmap = 1; in vmx_create_vcpu()
7075 bitmap_fill(vmx->shadow_msr_intercept.read, MAX_POSSIBLE_PASSTHROUGH_MSRS); in vmx_create_vcpu()
7076 bitmap_fill(vmx->shadow_msr_intercept.write, MAX_POSSIBLE_PASSTHROUGH_MSRS); in vmx_create_vcpu()
7087 if (kvm_cstate_in_guest(vcpu->kvm)) { in vmx_create_vcpu()
7093 vmx->msr_bitmap_mode = 0; in vmx_create_vcpu()
7095 vmx->loaded_vmcs = &vmx->vmcs01; in vmx_create_vcpu()
7098 vcpu->cpu = cpu; in vmx_create_vcpu()
7103 err = alloc_apic_access_page(vcpu->kvm); in vmx_create_vcpu()
7109 err = init_rmode_identity_map(vcpu->kvm); in vmx_create_vcpu()
7115 memcpy(&vmx->nested.msrs, &vmcs_config.nested, sizeof(vmx->nested.msrs)); in vmx_create_vcpu()
7117 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs)); in vmx_create_vcpu()
7119 vmx->nested.posted_intr_nv = -1; in vmx_create_vcpu()
7120 vmx->nested.current_vmptr = -1ull; in vmx_create_vcpu()
7122 vcpu->arch.microcode_version = 0x100000000ULL; in vmx_create_vcpu()
7123 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED; in vmx_create_vcpu()
7129 vmx->pi_desc.nv = POSTED_INTR_VECTOR; in vmx_create_vcpu()
7130 vmx->pi_desc.sn = 1; in vmx_create_vcpu()
7132 vmx->ept_pointer = INVALID_PAGE; in vmx_create_vcpu()
7137 free_loaded_vmcs(vmx->loaded_vmcs); in vmx_create_vcpu()
7141 free_vpid(vmx->vpid); in vmx_create_vcpu()
7145 …nt and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/ad…
7146 …tion disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/ad…
7150 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock); in vmx_vm_init()
7153 kvm->arch.pause_in_guest = true; in vmx_vm_init()
7190 return -EIO; in vmx_check_processor_compat()
7194 return -EIO; in vmx_check_processor_compat()
7200 return -EIO; in vmx_check_processor_compat()
7219 * When there is no need to deal with noncoherent DMA (e.g., no VT-d in vmx_get_mt_mask()
7220 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The in vmx_get_mt_mask()
7233 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { in vmx_get_mt_mask()
7241 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) in vmx_get_mt_mask()
7268 u32 new_ctl = vmx->secondary_exec_control; in vmcs_set_secondary_exec_control()
7276 * (indicating "allowed-1") if they are supported in the guest's CPUID.
7283 vmx->nested.msrs.cr0_fixed1 = 0xffffffff; in nested_vmx_cr_fixed1_bits_update()
7284 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; in nested_vmx_cr_fixed1_bits_update()
7287 if (entry && (entry->_reg & (_cpuid_mask))) \ in nested_vmx_cr_fixed1_bits_update()
7288 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ in nested_vmx_cr_fixed1_bits_update()
7326 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; in nested_vmx_entry_exit_ctls_update()
7327 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; in nested_vmx_entry_exit_ctls_update()
7329 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS; in nested_vmx_entry_exit_ctls_update()
7330 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS; in nested_vmx_entry_exit_ctls_update()
7345 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax; in update_intel_pt_cfg()
7346 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx; in update_intel_pt_cfg()
7347 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx; in update_intel_pt_cfg()
7348 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx; in update_intel_pt_cfg()
7352 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps, in update_intel_pt_cfg()
7356 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS | in update_intel_pt_cfg()
7363 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering)) in update_intel_pt_cfg()
7364 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN; in update_intel_pt_cfg()
7370 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc)) in update_intel_pt_cfg()
7371 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC | in update_intel_pt_cfg()
7378 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc)) in update_intel_pt_cfg()
7379 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN | in update_intel_pt_cfg()
7383 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite)) in update_intel_pt_cfg()
7384 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW | in update_intel_pt_cfg()
7388 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace)) in update_intel_pt_cfg()
7389 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN; in update_intel_pt_cfg()
7392 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output)) in update_intel_pt_cfg()
7393 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA; in update_intel_pt_cfg()
7396 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys)) in update_intel_pt_cfg()
7397 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN; in update_intel_pt_cfg()
7400 for (i = 0; i < vmx->pt_desc.addr_range; i++) in update_intel_pt_cfg()
7401 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4)); in update_intel_pt_cfg()
7409 vcpu->arch.xsaves_enabled = false; in vmx_vcpu_after_set_cpuid()
7417 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= in vmx_vcpu_after_set_cpuid()
7421 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= in vmx_vcpu_after_set_cpuid()
7485 to_vmx(vcpu)->req_immediate_exit = true; in vmx_request_immediate_exit()
7496 if (info->intercept == x86_intercept_in || in vmx_check_intercept_io()
7497 info->intercept == x86_intercept_ins) { in vmx_check_intercept_io()
7498 port = info->src_val; in vmx_check_intercept_io()
7499 size = info->dst_bytes; in vmx_check_intercept_io()
7501 port = info->dst_val; in vmx_check_intercept_io()
7502 size = info->src_bytes; in vmx_check_intercept_io()
7506 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction in vmx_check_intercept_io()
7507 * VM-exits depend on the 'unconditional IO exiting' VM-execution in vmx_check_intercept_io()
7510 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps. in vmx_check_intercept_io()
7529 switch (info->intercept) { in vmx_check_intercept()
7537 exception->vector = UD_VECTOR; in vmx_check_intercept()
7538 exception->error_code_valid = false; in vmx_check_intercept()
7565 * PAUSE is a single-byte NOP with a REPE prefix, i.e. collides in vmx_check_intercept()
7568 * PAUSE-loop-exiting, software can't expect a given PAUSE to in vmx_check_intercept()
7572 if ((info->rep_prefix != REPE_PREFIX) || in vmx_check_intercept()
7591 u64 low = a << shift, high = a >> (64 - shift); in u64_shl_div_u64()
7610 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer; in vmx_set_hv_timer()
7615 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; in vmx_set_hv_timer()
7617 ktimer->timer_advance_ns); in vmx_set_hv_timer()
7620 delta_tsc -= lapic_timer_advance_cycles; in vmx_set_hv_timer()
7625 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && in vmx_set_hv_timer()
7628 vcpu->arch.tsc_scaling_ratio, &delta_tsc)) in vmx_set_hv_timer()
7629 return -ERANGE; in vmx_set_hv_timer()
7638 return -ERANGE; in vmx_set_hv_timer()
7640 vmx->hv_deadline_tsc = tscl + delta_tsc; in vmx_set_hv_timer()
7647 to_vmx(vcpu)->hv_deadline_tsc = -1; in vmx_cancel_hv_timer()
7653 if (!kvm_pause_in_guest(vcpu->kvm)) in vmx_sched_in()
7704 if (vcpu->arch.mcg_cap & MCG_LMCE_P) in vmx_setup_mce()
7705 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= in vmx_setup_mce()
7708 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= in vmx_setup_mce()
7715 if (to_vmx(vcpu)->nested.nested_run_pending) in vmx_smi_allowed()
7716 return -EBUSY; in vmx_smi_allowed()
7724 vmx->nested.smm.guest_mode = is_guest_mode(vcpu); in vmx_pre_enter_smm()
7725 if (vmx->nested.smm.guest_mode) in vmx_pre_enter_smm()
7726 nested_vmx_vmexit(vcpu, -1, 0, 0); in vmx_pre_enter_smm()
7728 vmx->nested.smm.vmxon = vmx->nested.vmxon; in vmx_pre_enter_smm()
7729 vmx->nested.vmxon = false; in vmx_pre_enter_smm()
7739 if (vmx->nested.smm.vmxon) { in vmx_pre_leave_smm()
7740 vmx->nested.vmxon = true; in vmx_pre_leave_smm()
7741 vmx->nested.smm.vmxon = false; in vmx_pre_leave_smm()
7744 if (vmx->nested.smm.guest_mode) { in vmx_pre_leave_smm()
7749 vmx->nested.smm.guest_mode = false; in vmx_pre_leave_smm()
7761 return to_vmx(vcpu)->nested.vmxon; in vmx_apic_init_signal_blocked()
7767 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer; in vmx_migrate_timers()
7936 return -EIO; in hardware_setup()
8028 * and EPT A/D bit features are enabled -- PML depends on them to work. in hardware_setup()
8073 return -EINVAL; in hardware_setup()
8131 * Reset everything to support using non-enlightened VMCS in vmx_exit()
8141 vp_ap->nested_control.features.directhypercall = 0; in vmx_exit()
8142 vp_ap->current_nested_vmcs = 0; in vmx_exit()
8143 vp_ap->enlighten_vmentry = 0; in vmx_exit()
8178 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); in vmx_init()