Lines Matching +full:fails +full:- +full:without +full:- +full:test +full:- +full:cd

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
6 * machines without emulation or binary translation.
30 #include <linux/entry-kvm.h>
47 #include <asm/spec-ctrl.h>
124 /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
172 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
175 * According to test, this time is usually smaller than 128 cycles.
188 /* Default doubles per-vcpu window every exit. */
192 /* Default resets per-vcpu window every exit to ple_window. */
200 /* Default is SYSTEM mode, 1 for host-guest mode */
279 return -ENOMEM; in vmx_setup_l1d_flush()
318 return -EINVAL; in vmentry_l1d_flush_parse()
414 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
451 vmx->segment_cache.bitmask = 0; in vmx_segment_cache_clear()
457 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
484 tmp_eptp = to_vmx(vcpu)->ept_pointer; in check_ept_pointer_match()
485 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) { in check_ept_pointer_match()
486 to_kvm_vmx(kvm)->ept_pointers_match in check_ept_pointer_match()
492 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH; in check_ept_pointer_match()
500 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn, in kvm_fill_hv_flush_list_func()
501 range->pages); in kvm_fill_hv_flush_list_func()
507 u64 ept_pointer = to_vmx(vcpu)->ept_pointer; in __hv_remote_flush_tlb_with_range()
527 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); in hv_remote_flush_tlb_with_range()
529 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK) in hv_remote_flush_tlb_with_range()
532 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) { in hv_remote_flush_tlb_with_range()
535 if (VALID_PAGE(to_vmx(vcpu)->ept_pointer)) in hv_remote_flush_tlb_with_range()
544 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); in hv_remote_flush_tlb_with_range()
556 &vcpu->kvm->arch.hyperv.hv_pa_pg; in hv_enable_direct_tlbflush()
558 * Synthetic VM-Exit is not enabled in current code and so All in hv_enable_direct_tlbflush()
565 return -ENOMEM; in hv_enable_direct_tlbflush()
567 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs; in hv_enable_direct_tlbflush()
569 evmcs->partition_assist_page = in hv_enable_direct_tlbflush()
571 evmcs->hv_vm_id = (unsigned long)vcpu->kvm; in hv_enable_direct_tlbflush()
572 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1; in hv_enable_direct_tlbflush()
580 * Comment's format: document - errata name - stepping - processor name.
585 /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
587 /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
588 /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
589 /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
591 /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
593 /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
594 /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
596 * 320767.pdf - AAP86 - B1 -
597 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
600 /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
602 /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
604 /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
606 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
607 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
608 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
610 /* Xeon E3-1220 V2 */
645 return -ENOENT; in possible_passthrough_msr_slot()
665 r = possible_passthrough_msr_slot(msr) != -ENOENT; in is_valid_passthrough_msr()
676 for (i = 0; i < vmx->nr_uret_msrs; ++i) in __vmx_find_uret_msr()
677 if (vmx_uret_msrs_list[vmx->guest_uret_msrs[i].slot] == msr) in __vmx_find_uret_msr()
679 return -1; in __vmx_find_uret_msr()
688 return &vmx->guest_uret_msrs[i]; in vmx_find_uret_msr()
697 u64 old_msr_data = msr->data; in vmx_set_guest_uret_msr()
698 msr->data = data; in vmx_set_guest_uret_msr()
699 if (msr - vmx->guest_uret_msrs < vmx->nr_active_uret_msrs) { in vmx_set_guest_uret_msr()
701 ret = kvm_set_user_return_msr(msr->slot, msr->data, msr->mask); in vmx_set_guest_uret_msr()
704 msr->data = old_msr_data; in vmx_set_guest_uret_msr()
717 vmcs_clear(v->vmcs); in crash_vmclear_local_loaded_vmcss()
726 if (loaded_vmcs->cpu != cpu) in __loaded_vmcs_clear()
728 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) in __loaded_vmcs_clear()
731 vmcs_clear(loaded_vmcs->vmcs); in __loaded_vmcs_clear()
732 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) in __loaded_vmcs_clear()
733 vmcs_clear(loaded_vmcs->shadow_vmcs); in __loaded_vmcs_clear()
735 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); in __loaded_vmcs_clear()
739 * current percpu list, complete before setting loaded_vmcs->vcpu to in __loaded_vmcs_clear()
740 * -1, otherwise a different cpu can see vcpu == -1 first and add in __loaded_vmcs_clear()
746 loaded_vmcs->cpu = -1; in __loaded_vmcs_clear()
747 loaded_vmcs->launched = 0; in __loaded_vmcs_clear()
752 int cpu = loaded_vmcs->cpu; in loaded_vmcs_clear()
754 if (cpu != -1) in loaded_vmcs_clear()
765 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) { in vmx_segment_cache_test_set()
766 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS); in vmx_segment_cache_test_set()
767 vmx->segment_cache.bitmask = 0; in vmx_segment_cache_test_set()
769 ret = vmx->segment_cache.bitmask & mask; in vmx_segment_cache_test_set()
770 vmx->segment_cache.bitmask |= mask; in vmx_segment_cache_test_set()
776 u16 *p = &vmx->segment_cache.seg[seg].selector; in vmx_read_guest_seg_selector()
785 ulong *p = &vmx->segment_cache.seg[seg].base; in vmx_read_guest_seg_base()
794 u32 *p = &vmx->segment_cache.seg[seg].limit; in vmx_read_guest_seg_limit()
803 u32 *p = &vmx->segment_cache.seg[seg].ar; in vmx_read_guest_seg_ar()
824 if ((vcpu->guest_debug & in update_exception_bitmap()
828 if (to_vmx(vcpu)->rmode.vm86_active) in update_exception_bitmap()
839 eb |= get_vmcs12(vcpu)->exception_bitmap; in update_exception_bitmap()
867 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; in msr_write_intercepted()
890 for (i = 0; i < m->nr; ++i) { in vmx_find_loadstore_msr_slot()
891 if (m->val[i].index == msr) in vmx_find_loadstore_msr_slot()
894 return -ENOENT; in vmx_find_loadstore_msr_slot()
900 struct msr_autoload *m = &vmx->msr_autoload; in clear_atomic_switch_msr()
920 i = vmx_find_loadstore_msr_slot(&m->guest, msr); in clear_atomic_switch_msr()
923 --m->guest.nr; in clear_atomic_switch_msr()
924 m->guest.val[i] = m->guest.val[m->guest.nr]; in clear_atomic_switch_msr()
925 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); in clear_atomic_switch_msr()
928 i = vmx_find_loadstore_msr_slot(&m->host, msr); in clear_atomic_switch_msr()
932 --m->host.nr; in clear_atomic_switch_msr()
933 m->host.val[i] = m->host.val[m->host.nr]; in clear_atomic_switch_msr()
934 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); in clear_atomic_switch_msr()
953 struct msr_autoload *m = &vmx->msr_autoload; in add_atomic_switch_msr()
987 i = vmx_find_loadstore_msr_slot(&m->guest, msr); in add_atomic_switch_msr()
989 j = vmx_find_loadstore_msr_slot(&m->host, msr); in add_atomic_switch_msr()
991 if ((i < 0 && m->guest.nr == MAX_NR_LOADSTORE_MSRS) || in add_atomic_switch_msr()
992 (j < 0 && m->host.nr == MAX_NR_LOADSTORE_MSRS)) { in add_atomic_switch_msr()
998 i = m->guest.nr++; in add_atomic_switch_msr()
999 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); in add_atomic_switch_msr()
1001 m->guest.val[i].index = msr; in add_atomic_switch_msr()
1002 m->guest.val[i].value = guest_val; in add_atomic_switch_msr()
1008 j = m->host.nr++; in add_atomic_switch_msr()
1009 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); in add_atomic_switch_msr()
1011 m->host.val[j].index = msr; in add_atomic_switch_msr()
1012 m->host.val[j].value = host_val; in add_atomic_switch_msr()
1017 u64 guest_efer = vmx->vcpu.arch.efer; in update_transition_efer()
1042 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { in update_transition_efer()
1062 vmx->guest_uret_msrs[i].data = guest_efer; in update_transition_efer()
1063 vmx->guest_uret_msrs[i].mask = ~ignore_bits; in update_transition_efer()
1070 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1100 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN); in pt_can_write_msr()
1105 /* The base must be 128-byte aligned and a legal physical address. */ in pt_output_base_valid()
1113 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status); in pt_load_msr()
1114 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); in pt_load_msr()
1115 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); in pt_load_msr()
1116 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); in pt_load_msr()
1118 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]); in pt_load_msr()
1119 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]); in pt_load_msr()
1127 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status); in pt_save_msr()
1128 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); in pt_save_msr()
1129 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); in pt_save_msr()
1130 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); in pt_save_msr()
1132 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]); in pt_save_msr()
1133 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]); in pt_save_msr()
1146 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); in pt_guest_enter()
1147 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { in pt_guest_enter()
1149 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range); in pt_guest_enter()
1150 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range); in pt_guest_enter()
1159 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { in pt_guest_exit()
1160 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range); in pt_guest_exit()
1161 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range); in pt_guest_exit()
1165 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); in pt_guest_exit()
1171 if (unlikely(fs_sel != host->fs_sel)) { in vmx_set_host_fs_gs()
1176 host->fs_sel = fs_sel; in vmx_set_host_fs_gs()
1178 if (unlikely(gs_sel != host->gs_sel)) { in vmx_set_host_fs_gs()
1183 host->gs_sel = gs_sel; in vmx_set_host_fs_gs()
1185 if (unlikely(fs_base != host->fs_base)) { in vmx_set_host_fs_gs()
1187 host->fs_base = fs_base; in vmx_set_host_fs_gs()
1189 if (unlikely(gs_base != host->gs_base)) { in vmx_set_host_fs_gs()
1191 host->gs_base = gs_base; in vmx_set_host_fs_gs()
1206 vmx->req_immediate_exit = false; in vmx_prepare_switch_to_guest()
1211 * to/from long-mode by setting MSR_EFER.LMA. in vmx_prepare_switch_to_guest()
1213 if (!vmx->guest_uret_msrs_loaded) { in vmx_prepare_switch_to_guest()
1214 vmx->guest_uret_msrs_loaded = true; in vmx_prepare_switch_to_guest()
1215 for (i = 0; i < vmx->nr_active_uret_msrs; ++i) in vmx_prepare_switch_to_guest()
1216 kvm_set_user_return_msr(vmx->guest_uret_msrs[i].slot, in vmx_prepare_switch_to_guest()
1217 vmx->guest_uret_msrs[i].data, in vmx_prepare_switch_to_guest()
1218 vmx->guest_uret_msrs[i].mask); in vmx_prepare_switch_to_guest()
1222 if (vmx->nested.need_vmcs12_to_shadow_sync) in vmx_prepare_switch_to_guest()
1225 if (vmx->guest_state_loaded) in vmx_prepare_switch_to_guest()
1228 host_state = &vmx->loaded_vmcs->host_state; in vmx_prepare_switch_to_guest()
1234 host_state->ldt_sel = kvm_read_ldt(); in vmx_prepare_switch_to_guest()
1237 savesegment(ds, host_state->ds_sel); in vmx_prepare_switch_to_guest()
1238 savesegment(es, host_state->es_sel); in vmx_prepare_switch_to_guest()
1241 if (likely(is_64bit_mm(current->mm))) { in vmx_prepare_switch_to_guest()
1243 fs_sel = current->thread.fsindex; in vmx_prepare_switch_to_guest()
1244 gs_sel = current->thread.gsindex; in vmx_prepare_switch_to_guest()
1245 fs_base = current->thread.fsbase; in vmx_prepare_switch_to_guest()
1246 vmx->msr_host_kernel_gs_base = current->thread.gsbase; in vmx_prepare_switch_to_guest()
1251 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE); in vmx_prepare_switch_to_guest()
1254 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); in vmx_prepare_switch_to_guest()
1263 vmx->guest_state_loaded = true; in vmx_prepare_switch_to_guest()
1270 if (!vmx->guest_state_loaded) in vmx_prepare_switch_to_host()
1273 host_state = &vmx->loaded_vmcs->host_state; in vmx_prepare_switch_to_host()
1275 ++vmx->vcpu.stat.host_state_reload; in vmx_prepare_switch_to_host()
1278 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); in vmx_prepare_switch_to_host()
1280 if (host_state->ldt_sel || (host_state->gs_sel & 7)) { in vmx_prepare_switch_to_host()
1281 kvm_load_ldt(host_state->ldt_sel); in vmx_prepare_switch_to_host()
1283 load_gs_index(host_state->gs_sel); in vmx_prepare_switch_to_host()
1285 loadsegment(gs, host_state->gs_sel); in vmx_prepare_switch_to_host()
1288 if (host_state->fs_sel & 7) in vmx_prepare_switch_to_host()
1289 loadsegment(fs, host_state->fs_sel); in vmx_prepare_switch_to_host()
1291 if (unlikely(host_state->ds_sel | host_state->es_sel)) { in vmx_prepare_switch_to_host()
1292 loadsegment(ds, host_state->ds_sel); in vmx_prepare_switch_to_host()
1293 loadsegment(es, host_state->es_sel); in vmx_prepare_switch_to_host()
1298 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); in vmx_prepare_switch_to_host()
1301 vmx->guest_state_loaded = false; in vmx_prepare_switch_to_host()
1302 vmx->guest_uret_msrs_loaded = false; in vmx_prepare_switch_to_host()
1309 if (vmx->guest_state_loaded) in vmx_read_guest_kernel_gs_base()
1310 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); in vmx_read_guest_kernel_gs_base()
1312 return vmx->msr_guest_kernel_gs_base; in vmx_read_guest_kernel_gs_base()
1318 if (vmx->guest_state_loaded) in vmx_write_guest_kernel_gs_base()
1321 vmx->msr_guest_kernel_gs_base = data; in vmx_write_guest_kernel_gs_base()
1329 bool already_loaded = vmx->loaded_vmcs->cpu == cpu; in vmx_vcpu_load_vmcs()
1333 loaded_vmcs_clear(vmx->loaded_vmcs); in vmx_vcpu_load_vmcs()
1337 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to in vmx_vcpu_load_vmcs()
1344 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, in vmx_vcpu_load_vmcs()
1350 if (prev != vmx->loaded_vmcs->vmcs) { in vmx_vcpu_load_vmcs()
1351 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; in vmx_vcpu_load_vmcs()
1352 vmcs_load(vmx->loaded_vmcs->vmcs); in vmx_vcpu_load_vmcs()
1356 * the active VMCS within a guest, e.g. on nested VM-Enter. in vmx_vcpu_load_vmcs()
1359 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev)) in vmx_vcpu_load_vmcs()
1374 * Linux uses per-cpu TSS and GDT, so set these when switching in vmx_vcpu_load_vmcs()
1378 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); in vmx_vcpu_load_vmcs()
1384 vmx->loaded_vmcs->cpu = cpu; in vmx_vcpu_load_vmcs()
1389 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) in vmx_vcpu_load_vmcs()
1405 vmx->host_debugctlmsr = get_debugctlmsr(); in vmx_vcpu_load()
1428 if (vmx->rmode.vm86_active) { in vmx_get_rflags()
1430 save_rflags = vmx->rmode.save_rflags; in vmx_get_rflags()
1433 vmx->rflags = rflags; in vmx_get_rflags()
1435 return vmx->rflags; in vmx_get_rflags()
1445 vmx->rflags = rflags; in vmx_set_rflags()
1451 vmx->rflags = rflags; in vmx_set_rflags()
1452 if (vmx->rmode.vm86_active) { in vmx_set_rflags()
1453 vmx->rmode.save_rflags = rflags; in vmx_set_rflags()
1458 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM) in vmx_set_rflags()
1459 vmx->emulation_required = emulation_required(vcpu); in vmx_set_rflags()
1500 if (data & vmx->pt_desc.ctl_bitmask) in vmx_rtit_ctl_check()
1507 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) && in vmx_rtit_ctl_check()
1508 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN)) in vmx_rtit_ctl_check()
1518 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_rtit_ctl_check()
1526 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods); in vmx_rtit_ctl_check()
1527 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) && in vmx_rtit_ctl_check()
1531 value = intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_rtit_ctl_check()
1533 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) && in vmx_rtit_ctl_check()
1537 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods); in vmx_rtit_ctl_check()
1538 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) && in vmx_rtit_ctl_check()
1548 if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2)) in vmx_rtit_ctl_check()
1551 if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2)) in vmx_rtit_ctl_check()
1554 if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2)) in vmx_rtit_ctl_check()
1557 if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2)) in vmx_rtit_ctl_check()
1577 * (namely Hyper-V) don't set it due to it being undefined behavior, in skip_emulated_instruction()
1581 to_vmx(vcpu)->exit_reason != EXIT_REASON_EPT_MISCONFIG) { in skip_emulated_instruction()
1586 * We need to mask out the high 32 bits of RIP if not in 64-bit in skip_emulated_instruction()
1587 * mode, but just finding out that we are in 64-bit mode is in skip_emulated_instruction()
1606 * Recognizes a pending MTF VM-exit and records the nested state for later
1618 * Per the SDM, MTF takes priority over debug-trap exceptions besides in vmx_update_emulated_instruction()
1619 * T-bit traps. As instruction emulation is completed (i.e. at the in vmx_update_emulated_instruction()
1621 * debug-trap. Record the pending MTF state to be delivered in in vmx_update_emulated_instruction()
1625 (!vcpu->arch.exception.pending || in vmx_update_emulated_instruction()
1626 vcpu->arch.exception.nr == DB_VECTOR)) in vmx_update_emulated_instruction()
1627 vmx->nested.mtf_pending = true; in vmx_update_emulated_instruction()
1629 vmx->nested.mtf_pending = false; in vmx_update_emulated_instruction()
1646 if (kvm_hlt_in_guest(vcpu->kvm) && in vmx_clear_hlt()
1654 unsigned nr = vcpu->arch.exception.nr; in vmx_queue_exception()
1655 bool has_error_code = vcpu->arch.exception.has_error_code; in vmx_queue_exception()
1656 u32 error_code = vcpu->arch.exception.error_code; in vmx_queue_exception()
1666 if (vmx->rmode.vm86_active) { in vmx_queue_exception()
1669 inc_eip = vcpu->arch.event_exit_inst_len; in vmx_queue_exception()
1674 WARN_ON_ONCE(vmx->emulation_required); in vmx_queue_exception()
1678 vmx->vcpu.arch.event_exit_inst_len); in vmx_queue_exception()
1696 to = vmx->nr_active_uret_msrs++; in vmx_setup_uret_msr()
1698 tmp = vmx->guest_uret_msrs[to]; in vmx_setup_uret_msr()
1699 vmx->guest_uret_msrs[to] = vmx->guest_uret_msrs[from]; in vmx_setup_uret_msr()
1700 vmx->guest_uret_msrs[from] = tmp; in vmx_setup_uret_msr()
1705 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1710 vmx->guest_uret_msrs_loaded = false; in setup_msrs()
1711 vmx->nr_active_uret_msrs = 0; in setup_msrs()
1717 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) { in setup_msrs()
1726 if (guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) in setup_msrs()
1732 vmx_update_msr_bitmap(&vmx->vcpu); in setup_msrs()
1747 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING)) in vmx_write_l1_tsc_offset()
1748 g_tsc_offset = vmcs12->tsc_offset; in vmx_write_l1_tsc_offset()
1750 trace_kvm_write_tsc_offset(vcpu->vcpu_id, in vmx_write_l1_tsc_offset()
1751 vcpu->arch.tsc_offset - g_tsc_offset, in vmx_write_l1_tsc_offset()
1771 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; in vmx_feature_control_msr_valid()
1778 switch (msr->index) { in vmx_get_msr_feature()
1782 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); in vmx_get_msr_feature()
1784 msr->data = vmx_get_perf_capabilities(); in vmx_get_msr_feature()
1793 * Returns 0 on success, non-0 otherwise.
1802 switch (msr_info->index) { in vmx_get_msr()
1805 msr_info->data = vmcs_readl(GUEST_FS_BASE); in vmx_get_msr()
1808 msr_info->data = vmcs_readl(GUEST_GS_BASE); in vmx_get_msr()
1811 msr_info->data = vmx_read_guest_kernel_gs_base(vmx); in vmx_get_msr()
1817 if (!msr_info->host_initiated && in vmx_get_msr()
1818 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR)) in vmx_get_msr()
1822 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx)) in vmx_get_msr()
1825 msr_info->data = vmx->msr_ia32_umwait_control; in vmx_get_msr()
1828 if (!msr_info->host_initiated && in vmx_get_msr()
1832 msr_info->data = to_vmx(vcpu)->spec_ctrl; in vmx_get_msr()
1835 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); in vmx_get_msr()
1838 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); in vmx_get_msr()
1841 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); in vmx_get_msr()
1845 (!msr_info->host_initiated && in vmx_get_msr()
1848 msr_info->data = vmcs_read64(GUEST_BNDCFGS); in vmx_get_msr()
1851 if (!msr_info->host_initiated && in vmx_get_msr()
1852 !(vmx->msr_ia32_feature_control & in vmx_get_msr()
1855 msr_info->data = vcpu->arch.mcg_ext_ctl; in vmx_get_msr()
1858 msr_info->data = vmx->msr_ia32_feature_control; in vmx_get_msr()
1863 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, in vmx_get_msr()
1864 &msr_info->data)) in vmx_get_msr()
1868 * Hyper-V versions are still trying to use corresponding in vmx_get_msr()
1872 if (!msr_info->host_initiated && in vmx_get_msr()
1873 vmx->nested.enlightened_vmcs_enabled) in vmx_get_msr()
1874 nested_evmcs_filter_control_msr(msr_info->index, in vmx_get_msr()
1875 &msr_info->data); in vmx_get_msr()
1880 msr_info->data = vmx->pt_desc.guest.ctl; in vmx_get_msr()
1885 msr_info->data = vmx->pt_desc.guest.status; in vmx_get_msr()
1889 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
1892 msr_info->data = vmx->pt_desc.guest.cr3_match; in vmx_get_msr()
1896 (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
1898 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
1901 msr_info->data = vmx->pt_desc.guest.output_base; in vmx_get_msr()
1905 (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
1907 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
1910 msr_info->data = vmx->pt_desc.guest.output_mask; in vmx_get_msr()
1913 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; in vmx_get_msr()
1915 (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_get_msr()
1919 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2]; in vmx_get_msr()
1921 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2]; in vmx_get_msr()
1924 if (!msr_info->host_initiated && in vmx_get_msr()
1930 msr = vmx_find_uret_msr(vmx, msr_info->index); in vmx_get_msr()
1932 msr_info->data = msr->data; in vmx_get_msr()
1953 * Returns 0 on success, non-0 otherwise.
1961 u32 msr_index = msr_info->index; in vmx_set_msr()
1962 u64 data = msr_info->data; in vmx_set_msr()
1984 get_vmcs12(vcpu)->guest_sysenter_cs = data; in vmx_set_msr()
1990 get_vmcs12(vcpu)->guest_sysenter_eip = data; in vmx_set_msr()
1997 get_vmcs12(vcpu)->guest_sysenter_esp = data; in vmx_set_msr()
2002 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls & in vmx_set_msr()
2004 get_vmcs12(vcpu)->guest_ia32_debugctl = data; in vmx_set_msr()
2011 (!msr_info->host_initiated && in vmx_set_msr()
2020 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx)) in vmx_set_msr()
2023 /* The reserved bit 1 and non-32 bit [63:32] should be zero */ in vmx_set_msr()
2027 vmx->msr_ia32_umwait_control = data; in vmx_set_msr()
2030 if (!msr_info->host_initiated && in vmx_set_msr()
2037 vmx->spec_ctrl = data; in vmx_set_msr()
2042 * For non-nested: in vmx_set_msr()
2043 * When it's written (to non-zero) for the first time, pass in vmx_set_msr()
2058 if (!msr_info->host_initiated && in vmx_set_msr()
2059 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR)) in vmx_set_msr()
2065 if (!msr_info->host_initiated && in vmx_set_msr()
2079 * For non-nested: in vmx_set_msr()
2080 * When it's written (to non-zero) for the first time, pass in vmx_set_msr()
2096 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) in vmx_set_msr()
2097 get_vmcs12(vcpu)->guest_ia32_pat = data; in vmx_set_msr()
2101 vcpu->arch.pat = data; in vmx_set_msr()
2110 if ((!msr_info->host_initiated && in vmx_set_msr()
2111 !(to_vmx(vcpu)->msr_ia32_feature_control & in vmx_set_msr()
2115 vcpu->arch.mcg_ext_ctl = data; in vmx_set_msr()
2119 (to_vmx(vcpu)->msr_ia32_feature_control & in vmx_set_msr()
2120 FEAT_CTL_LOCKED && !msr_info->host_initiated)) in vmx_set_msr()
2122 vmx->msr_ia32_feature_control = data; in vmx_set_msr()
2123 if (msr_info->host_initiated && data == 0) in vmx_set_msr()
2127 if (!msr_info->host_initiated) in vmx_set_msr()
2128 return 1; /* they are read-only */ in vmx_set_msr()
2135 vmx->nested.vmxon) in vmx_set_msr()
2138 vmx->pt_desc.guest.ctl = data; in vmx_set_msr()
2146 vmx->pt_desc.guest.status = data; in vmx_set_msr()
2151 if (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2154 vmx->pt_desc.guest.cr3_match = data; in vmx_set_msr()
2159 if (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2161 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2166 vmx->pt_desc.guest.output_base = data; in vmx_set_msr()
2171 if (!intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2173 !intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2176 vmx->pt_desc.guest.output_mask = data; in vmx_set_msr()
2181 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; in vmx_set_msr()
2182 if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps, in vmx_set_msr()
2188 vmx->pt_desc.guest.addr_b[index / 2] = data; in vmx_set_msr()
2190 vmx->pt_desc.guest.addr_a[index / 2] = data; in vmx_set_msr()
2193 if (!msr_info->host_initiated && in vmx_set_msr()
2221 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); in vmx_cache_reg()
2224 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); in vmx_cache_reg()
2231 guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; in vmx_cache_reg()
2233 vcpu->arch.cr0 &= ~guest_owned_bits; in vmx_cache_reg()
2234 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits; in vmx_cache_reg()
2239 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); in vmx_cache_reg()
2242 guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; in vmx_cache_reg()
2244 vcpu->arch.cr4 &= ~guest_owned_bits; in vmx_cache_reg()
2245 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits; in vmx_cache_reg()
2283 return -EFAULT; in kvm_cpu_vmxon()
2293 return -EBUSY; in hardware_enable()
2296 * This can happen if we hot-added a CPU but failed to allocate in hardware_enable()
2301 return -EFAULT; in hardware_enable()
2365 return -EIO; in adjust_vmx_controls()
2403 return -EIO; in setup_vmcs_config()
2438 return -EIO; in setup_vmcs_config()
2453 &vmx_cap->ept, &vmx_cap->vpid); in setup_vmcs_config()
2461 } else if (vmx_cap->ept) { in setup_vmcs_config()
2462 vmx_cap->ept = 0; in setup_vmcs_config()
2464 "1-setting enable EPT VM-execution control\n"); in setup_vmcs_config()
2467 vmx_cap->vpid) { in setup_vmcs_config()
2468 vmx_cap->vpid = 0; in setup_vmcs_config()
2470 "1-setting enable VPID VM-execution control\n"); in setup_vmcs_config()
2485 return -EIO; in setup_vmcs_config()
2492 return -EIO; in setup_vmcs_config()
2509 return -EIO; in setup_vmcs_config()
2537 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ in setup_vmcs_config()
2539 return -EIO; in setup_vmcs_config()
2542 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ in setup_vmcs_config()
2544 return -EIO; in setup_vmcs_config()
2547 /* Require Write-Back (WB) memory type for VMCS accesses. */ in setup_vmcs_config()
2549 return -EIO; in setup_vmcs_config()
2551 vmcs_conf->size = vmx_msr_high & 0x1fff; in setup_vmcs_config()
2552 vmcs_conf->order = get_order(vmcs_conf->size); in setup_vmcs_config()
2553 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; in setup_vmcs_config()
2555 vmcs_conf->revision_id = vmx_msr_low; in setup_vmcs_config()
2557 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; in setup_vmcs_config()
2558 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; in setup_vmcs_config()
2559 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; in setup_vmcs_config()
2560 vmcs_conf->vmexit_ctrl = _vmexit_control; in setup_vmcs_config()
2561 vmcs_conf->vmentry_ctrl = _vmentry_control; in setup_vmcs_config()
2585 vmcs->hdr.revision_id = KVM_EVMCS_VERSION; in alloc_vmcs_cpu()
2587 vmcs->hdr.revision_id = vmcs_config.revision_id; in alloc_vmcs_cpu()
2590 vmcs->hdr.shadow_vmcs = 1; in alloc_vmcs_cpu()
2604 if (!loaded_vmcs->vmcs) in free_loaded_vmcs()
2607 free_vmcs(loaded_vmcs->vmcs); in free_loaded_vmcs()
2608 loaded_vmcs->vmcs = NULL; in free_loaded_vmcs()
2609 if (loaded_vmcs->msr_bitmap) in free_loaded_vmcs()
2610 free_page((unsigned long)loaded_vmcs->msr_bitmap); in free_loaded_vmcs()
2611 WARN_ON(loaded_vmcs->shadow_vmcs != NULL); in free_loaded_vmcs()
2616 loaded_vmcs->vmcs = alloc_vmcs(false); in alloc_loaded_vmcs()
2617 if (!loaded_vmcs->vmcs) in alloc_loaded_vmcs()
2618 return -ENOMEM; in alloc_loaded_vmcs()
2620 vmcs_clear(loaded_vmcs->vmcs); in alloc_loaded_vmcs()
2622 loaded_vmcs->shadow_vmcs = NULL; in alloc_loaded_vmcs()
2623 loaded_vmcs->hv_timer_soft_disabled = false; in alloc_loaded_vmcs()
2624 loaded_vmcs->cpu = -1; in alloc_loaded_vmcs()
2625 loaded_vmcs->launched = 0; in alloc_loaded_vmcs()
2628 loaded_vmcs->msr_bitmap = (unsigned long *) in alloc_loaded_vmcs()
2630 if (!loaded_vmcs->msr_bitmap) in alloc_loaded_vmcs()
2632 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); in alloc_loaded_vmcs()
2638 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs; in alloc_loaded_vmcs()
2640 evmcs->hv_enlightenments_control.msr_bitmap = 1; in alloc_loaded_vmcs()
2644 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state)); in alloc_loaded_vmcs()
2645 memset(&loaded_vmcs->controls_shadow, 0, in alloc_loaded_vmcs()
2652 return -ENOMEM; in alloc_loaded_vmcs()
2675 return -ENOMEM; in alloc_kvm_area()
2680 * vmcs->revision_id to KVM_EVMCS_VERSION instead of in alloc_kvm_area()
2689 vmcs->hdr.revision_id = vmcs_config.revision_id; in alloc_kvm_area()
2708 save->selector &= ~SEGMENT_RPL_MASK; in fix_pmode_seg()
2709 save->dpl = save->selector & SEGMENT_RPL_MASK; in fix_pmode_seg()
2710 save->s = 1; in fix_pmode_seg()
2721 * Update real mode segment cache. It may be not up-to-date if sement in enter_pmode()
2724 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); in enter_pmode()
2725 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); in enter_pmode()
2726 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); in enter_pmode()
2727 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); in enter_pmode()
2728 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); in enter_pmode()
2729 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); in enter_pmode()
2731 vmx->rmode.vm86_active = 0; in enter_pmode()
2733 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); in enter_pmode()
2737 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; in enter_pmode()
2745 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); in enter_pmode()
2746 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); in enter_pmode()
2747 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); in enter_pmode()
2748 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); in enter_pmode()
2749 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); in enter_pmode()
2750 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); in enter_pmode()
2774 if (save->base & 0xf) in fix_rmode_seg()
2780 vmcs_write16(sf->selector, var.selector); in fix_rmode_seg()
2781 vmcs_writel(sf->base, var.base); in fix_rmode_seg()
2782 vmcs_write32(sf->limit, var.limit); in fix_rmode_seg()
2783 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); in fix_rmode_seg()
2790 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); in enter_rmode()
2792 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); in enter_rmode()
2793 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); in enter_rmode()
2794 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); in enter_rmode()
2795 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); in enter_rmode()
2796 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); in enter_rmode()
2797 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); in enter_rmode()
2798 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); in enter_rmode()
2800 vmx->rmode.vm86_active = 1; in enter_rmode()
2806 if (!kvm_vmx->tss_addr) in enter_rmode()
2812 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); in enter_rmode()
2813 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); in enter_rmode()
2817 vmx->rmode.save_rflags = flags; in enter_rmode()
2825 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); in enter_rmode()
2826 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); in enter_rmode()
2827 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); in enter_rmode()
2828 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); in enter_rmode()
2829 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); in enter_rmode()
2830 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); in enter_rmode()
2844 vcpu->arch.efer = efer; in vmx_set_efer()
2847 msr->data = efer; in vmx_set_efer()
2851 msr->data = efer & ~EFER_LME; in vmx_set_efer()
2873 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); in enter_lmode()
2879 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); in exit_lmode()
2890 * the CPU is not required to invalidate guest-physical mappings on in vmx_flush_tlb_all()
2891 * VM-Entry, even if VPID is disabled. Guest-physical mappings are in vmx_flush_tlb_all()
2893 * (INVVPID also isn't required to invalidate guest-physical mappings). in vmx_flush_tlb_all()
2901 vpid_sync_vcpu_single(vmx->vpid); in vmx_flush_tlb_all()
2902 vpid_sync_vcpu_single(vmx->nested.vpid02); in vmx_flush_tlb_all()
2909 struct kvm_mmu *mmu = vcpu->arch.mmu; in vmx_flush_tlb_current()
2910 u64 root_hpa = mmu->root_hpa; in vmx_flush_tlb_current()
2918 mmu->shadow_root_level)); in vmx_flush_tlb_current()
2920 vpid_sync_context(to_vmx(vcpu)->vpid); in vmx_flush_tlb_current()
2928 * vpid_sync_vcpu_addr() is a nop if vmx->vpid==0, see the comment in in vmx_flush_tlb_gva()
2931 vpid_sync_vcpu_addr(to_vmx(vcpu)->vpid, addr); in vmx_flush_tlb_gva()
2937 * vpid_sync_context() is a nop if vmx->vpid==0, e.g. if enable_vpid==0 in vmx_flush_tlb_guest()
2938 * or a vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit in vmx_flush_tlb_guest()
2939 * are required to flush GVA->{G,H}PA mappings from the TLB if vpid is in vmx_flush_tlb_guest()
2940 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed), in vmx_flush_tlb_guest()
2943 vpid_sync_context(to_vmx(vcpu)->vpid); in vmx_flush_tlb_guest()
2948 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in vmx_ept_load_pdptrs()
2954 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); in vmx_ept_load_pdptrs()
2955 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); in vmx_ept_load_pdptrs()
2956 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); in vmx_ept_load_pdptrs()
2957 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); in vmx_ept_load_pdptrs()
2963 struct kvm_mmu *mmu = vcpu->arch.walk_mmu; in ept_save_pdptrs()
2968 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); in ept_save_pdptrs()
2969 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); in ept_save_pdptrs()
2970 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); in ept_save_pdptrs()
2971 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); in ept_save_pdptrs()
2988 vcpu->arch.cr0 = cr0; in ept_update_paging_mode_cr0()
2994 vcpu->arch.cr0 = cr0; in ept_update_paging_mode_cr0()
3013 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) in vmx_set_cr0()
3016 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) in vmx_set_cr0()
3021 if (vcpu->arch.efer & EFER_LME) { in vmx_set_cr0()
3034 vcpu->arch.cr0 = cr0; in vmx_set_cr0()
3037 /* depends on vcpu->arch.cr0 to be set to a new value */ in vmx_set_cr0()
3038 vmx->emulation_required = emulation_required(vcpu); in vmx_set_cr0()
3066 struct kvm *kvm = vcpu->kvm; in vmx_load_mmu_pgd()
3076 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); in vmx_load_mmu_pgd()
3077 to_vmx(vcpu)->ept_pointer = eptp; in vmx_load_mmu_pgd()
3078 to_kvm_vmx(kvm)->ept_pointers_match in vmx_load_mmu_pgd()
3080 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); in vmx_load_mmu_pgd()
3084 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; in vmx_load_mmu_pgd()
3085 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) in vmx_load_mmu_pgd()
3086 guest_cr3 = vcpu->arch.cr3; in vmx_load_mmu_pgd()
3087 else /* vmcs01.GUEST_CR3 is already up-to-date. */ in vmx_load_mmu_pgd()
3111 else if (vmx->rmode.vm86_active) in vmx_set_cr4()
3138 if (vmx->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) in vmx_set_cr4()
3141 vcpu->arch.cr4 = cr4; in vmx_set_cr4()
3155 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in in vmx_set_cr4()
3157 * to be manually disabled when guest switches to non-paging in vmx_set_cr4()
3179 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { in vmx_get_segment()
3180 *var = vmx->rmode.segs[seg]; in vmx_get_segment()
3182 || var->selector == vmx_read_guest_seg_selector(vmx, seg)) in vmx_get_segment()
3184 var->base = vmx_read_guest_seg_base(vmx, seg); in vmx_get_segment()
3185 var->selector = vmx_read_guest_seg_selector(vmx, seg); in vmx_get_segment()
3188 var->base = vmx_read_guest_seg_base(vmx, seg); in vmx_get_segment()
3189 var->limit = vmx_read_guest_seg_limit(vmx, seg); in vmx_get_segment()
3190 var->selector = vmx_read_guest_seg_selector(vmx, seg); in vmx_get_segment()
3192 var->unusable = (ar >> 16) & 1; in vmx_get_segment()
3193 var->type = ar & 15; in vmx_get_segment()
3194 var->s = (ar >> 4) & 1; in vmx_get_segment()
3195 var->dpl = (ar >> 5) & 3; in vmx_get_segment()
3203 var->present = !var->unusable; in vmx_get_segment()
3204 var->avl = (ar >> 12) & 1; in vmx_get_segment()
3205 var->l = (ar >> 13) & 1; in vmx_get_segment()
3206 var->db = (ar >> 14) & 1; in vmx_get_segment()
3207 var->g = (ar >> 15) & 1; in vmx_get_segment()
3214 if (to_vmx(vcpu)->rmode.vm86_active) { in vmx_get_segment_base()
3225 if (unlikely(vmx->rmode.vm86_active)) in vmx_get_cpl()
3237 if (var->unusable || !var->present) in vmx_segment_access_rights()
3240 ar = var->type & 15; in vmx_segment_access_rights()
3241 ar |= (var->s & 1) << 4; in vmx_segment_access_rights()
3242 ar |= (var->dpl & 3) << 5; in vmx_segment_access_rights()
3243 ar |= (var->present & 1) << 7; in vmx_segment_access_rights()
3244 ar |= (var->avl & 1) << 12; in vmx_segment_access_rights()
3245 ar |= (var->l & 1) << 13; in vmx_segment_access_rights()
3246 ar |= (var->db & 1) << 14; in vmx_segment_access_rights()
3247 ar |= (var->g & 1) << 15; in vmx_segment_access_rights()
3260 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { in vmx_set_segment()
3261 vmx->rmode.segs[seg] = *var; in vmx_set_segment()
3263 vmcs_write16(sf->selector, var->selector); in vmx_set_segment()
3264 else if (var->s) in vmx_set_segment()
3265 fix_rmode_seg(seg, &vmx->rmode.segs[seg]); in vmx_set_segment()
3269 vmcs_writel(sf->base, var->base); in vmx_set_segment()
3270 vmcs_write32(sf->limit, var->limit); in vmx_set_segment()
3271 vmcs_write16(sf->selector, var->selector); in vmx_set_segment()
3285 var->type |= 0x1; /* Accessed */ in vmx_set_segment()
3287 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); in vmx_set_segment()
3290 vmx->emulation_required = emulation_required(vcpu); in vmx_set_segment()
3303 dt->size = vmcs_read32(GUEST_IDTR_LIMIT); in vmx_get_idt()
3304 dt->address = vmcs_readl(GUEST_IDTR_BASE); in vmx_get_idt()
3309 vmcs_write32(GUEST_IDTR_LIMIT, dt->size); in vmx_set_idt()
3310 vmcs_writel(GUEST_IDTR_BASE, dt->address); in vmx_set_idt()
3315 dt->size = vmcs_read32(GUEST_GDTR_LIMIT); in vmx_get_gdt()
3316 dt->address = vmcs_readl(GUEST_GDTR_BASE); in vmx_get_gdt()
3321 vmcs_write32(GUEST_GDTR_LIMIT, dt->size); in vmx_set_gdt()
3322 vmcs_writel(GUEST_GDTR_BASE, dt->address); in vmx_set_gdt()
3511 * - Add checks on RIP in __vmx_guest_state_valid()
3512 * - Add checks on RFLAGS in __vmx_guest_state_valid()
3524 idx = srcu_read_lock(&kvm->srcu); in init_rmode_tss()
3525 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; in init_rmode_tss()
3542 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, in init_rmode_tss()
3545 srcu_read_unlock(&kvm->srcu, idx); in init_rmode_tss()
3556 /* Protect kvm_vmx->ept_identity_pagetable_done. */ in init_rmode_identity_map()
3557 mutex_lock(&kvm->slots_lock); in init_rmode_identity_map()
3559 if (likely(kvm_vmx->ept_identity_pagetable_done)) in init_rmode_identity_map()
3562 if (!kvm_vmx->ept_identity_map_addr) in init_rmode_identity_map()
3563 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; in init_rmode_identity_map()
3564 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; in init_rmode_identity_map()
3567 kvm_vmx->ept_identity_map_addr, PAGE_SIZE); in init_rmode_identity_map()
3574 /* Set up identity-mapping pagetable for EPT in real mode */ in init_rmode_identity_map()
3583 kvm_vmx->ept_identity_pagetable_done = true; in init_rmode_identity_map()
3586 mutex_unlock(&kvm->slots_lock); in init_rmode_identity_map()
3595 vmcs_write16(sf->selector, 0); in seg_setup()
3596 vmcs_writel(sf->base, 0); in seg_setup()
3597 vmcs_write32(sf->limit, 0xffff); in seg_setup()
3602 vmcs_write32(sf->ar_bytes, ar); in seg_setup()
3610 mutex_lock(&kvm->slots_lock); in alloc_apic_access_page()
3611 if (kvm->arch.apic_access_page_done) in alloc_apic_access_page()
3620 r = -EFAULT; in alloc_apic_access_page()
3625 * Do not pin the page in memory, so that memory hot-unplug in alloc_apic_access_page()
3629 kvm->arch.apic_access_page_done = true; in alloc_apic_access_page()
3631 mutex_unlock(&kvm->slots_lock); in alloc_apic_access_page()
3704 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; in vmx_disable_intercept_for_msr()
3719 if (idx != -ENOENT) { in vmx_disable_intercept_for_msr()
3721 clear_bit(idx, vmx->shadow_msr_intercept.read); in vmx_disable_intercept_for_msr()
3723 clear_bit(idx, vmx->shadow_msr_intercept.write); in vmx_disable_intercept_for_msr()
3750 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; in vmx_enable_intercept_for_msr()
3765 if (idx != -ENOENT) { in vmx_enable_intercept_for_msr()
3767 set_bit(idx, vmx->shadow_msr_intercept.read); in vmx_enable_intercept_for_msr()
3769 set_bit(idx, vmx->shadow_msr_intercept.write); in vmx_enable_intercept_for_msr()
3806 unsigned long *msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; in vmx_reset_x2apic_msrs()
3846 u8 changed = mode ^ vmx->msr_bitmap_mode; in vmx_update_msr_bitmap()
3854 vmx->msr_bitmap_mode = mode; in vmx_update_msr_bitmap()
3860 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN); in pt_update_intercept_for_msr()
3867 for (i = 0; i < vmx->pt_desc.addr_range; i++) { in pt_update_intercept_for_msr()
3882 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn)) in vmx_guest_apic_has_interrupt()
3887 vapic_page = vmx->nested.virtual_apic_map.hva; in vmx_guest_apic_has_interrupt()
3905 bool read = test_bit(i, vmx->shadow_msr_intercept.read); in vmx_msr_filter_changed()
3906 bool write = test_bit(i, vmx->shadow_msr_intercept.write); in vmx_msr_filter_changed()
3922 if (vcpu->mode == IN_GUEST_MODE) { in kvm_vcpu_trigger_posted_interrupt()
3931 * Case 1: vcpu keeps in non-root mode. Sending a in kvm_vcpu_trigger_posted_interrupt()
3948 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); in kvm_vcpu_trigger_posted_interrupt()
3961 vector == vmx->nested.posted_intr_nv) { in vmx_deliver_nested_posted_interrupt()
3966 vmx->nested.pi_pending = true; in vmx_deliver_nested_posted_interrupt()
3973 return -1; in vmx_deliver_nested_posted_interrupt()
3977 * 1. If target vcpu is running(non-root mode), send posted interrupt
3991 if (!vcpu->arch.apicv_active) in vmx_deliver_posted_interrupt()
3992 return -1; in vmx_deliver_posted_interrupt()
3994 if (pi_test_and_set_pir(vector, &vmx->pi_desc)) in vmx_deliver_posted_interrupt()
3998 if (pi_test_and_set_on(&vmx->pi_desc)) in vmx_deliver_posted_interrupt()
4009 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4011 * Note that host-state that does change is set elsewhere. E.g., host-state
4030 vmx->loaded_vmcs->host_state.cr3 = cr3; in vmx_set_constant_host_state()
4035 vmx->loaded_vmcs->host_state.cr4 = cr4; in vmx_set_constant_host_state()
4073 struct kvm_vcpu *vcpu = &vmx->vcpu; in set_cr4_guest_host_mask()
4075 vcpu->arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS & in set_cr4_guest_host_mask()
4076 ~vcpu->arch.cr4_guest_rsvd_bits; in set_cr4_guest_host_mask()
4078 vcpu->arch.cr4_guest_owned_bits &= ~X86_CR4_PGE; in set_cr4_guest_host_mask()
4079 if (is_guest_mode(&vmx->vcpu)) in set_cr4_guest_host_mask()
4080 vcpu->arch.cr4_guest_owned_bits &= in set_cr4_guest_host_mask()
4081 ~get_vmcs12(vcpu)->cr4_guest_host_mask; in set_cr4_guest_host_mask()
4082 vmcs_writel(CR4_GUEST_HOST_MASK, ~vcpu->arch.cr4_guest_owned_bits); in set_cr4_guest_host_mask()
4089 if (!kvm_vcpu_apicv_active(&vmx->vcpu)) in vmx_pin_based_exec_ctrl()
4125 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) in vmx_exec_control()
4128 if (!cpu_need_tpr_shadow(&vmx->vcpu)) { in vmx_exec_control()
4139 if (kvm_mwait_in_guest(vmx->vcpu.kvm)) in vmx_exec_control()
4142 if (kvm_hlt_in_guest(vmx->vcpu.kvm)) in vmx_exec_control()
4157 * If the control is for an opt-in feature, clear the control if the in vmx_adjust_secondary_exec_control()
4159 * control is opt-out, i.e. an exiting control, clear the control if in vmx_adjust_secondary_exec_control()
4173 vmx->nested.msrs.secondary_ctls_high |= control; in vmx_adjust_secondary_exec_control()
4175 vmx->nested.msrs.secondary_ctls_high &= ~control; in vmx_adjust_secondary_exec_control()
4189 __enabled = guest_cpuid_has(&(vmx)->vcpu, \
4196 /* More macro magic for ENABLE_/opt-in versus _EXITING/opt-out controls. */
4205 struct kvm_vcpu *vcpu = &vmx->vcpu; in vmx_compute_secondary_exec_control()
4213 if (vmx->vpid == 0) in vmx_compute_secondary_exec_control()
4221 if (kvm_pause_in_guest(vmx->vcpu.kvm)) in vmx_compute_secondary_exec_control()
4249 vcpu->arch.xsaves_enabled = xsaves_enabled; in vmx_compute_secondary_exec_control()
4275 vmx->secondary_exec_control = exec_control; in vmx_compute_secondary_exec_control()
4282 * of an EPT paging-structure entry is 110b (write/execute). in ept_set_mmio_spte_mask()
4290 * Noting that the initialization of Guest-state Area of VMCS is in
4299 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); in init_vmcs()
4301 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ in init_vmcs()
4310 secondary_exec_controls_set(vmx, vmx->secondary_exec_control); in init_vmcs()
4313 if (kvm_vcpu_apicv_active(&vmx->vcpu)) { in init_vmcs()
4322 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); in init_vmcs()
4325 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { in init_vmcs()
4327 vmx->ple_window = ple_window; in init_vmcs()
4328 vmx->ple_window_dirty = true; in init_vmcs()
4346 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); in init_vmcs()
4348 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); in init_vmcs()
4351 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); in init_vmcs()
4358 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; in init_vmcs()
4359 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits); in init_vmcs()
4363 if (vmx->vpid != 0) in init_vmcs()
4364 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); in init_vmcs()
4370 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); in init_vmcs()
4371 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); in init_vmcs()
4375 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); in init_vmcs()
4378 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc)); in init_vmcs()
4380 vmx->pt_desc.guest.output_mask = 0x7F; in init_vmcs()
4391 vmx->rmode.vm86_active = 0; in vmx_vcpu_reset()
4392 vmx->spec_ctrl = 0; in vmx_vcpu_reset()
4394 vmx->msr_ia32_umwait_control = 0; in vmx_vcpu_reset()
4396 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); in vmx_vcpu_reset()
4397 vmx->hv_deadline_tsc = -1; in vmx_vcpu_reset()
4461 __pa(vcpu->arch.apic->regs)); in vmx_vcpu_reset()
4468 vmx->vcpu.arch.cr0 = cr0; in vmx_vcpu_reset()
4475 vpid_sync_context(vmx->vpid); in vmx_vcpu_reset()
4500 int irq = vcpu->arch.interrupt.nr; in vmx_inject_irq()
4504 ++vcpu->stat.irq_injections; in vmx_inject_irq()
4505 if (vmx->rmode.vm86_active) { in vmx_inject_irq()
4507 if (vcpu->arch.interrupt.soft) in vmx_inject_irq()
4508 inc_eip = vcpu->arch.event_exit_inst_len; in vmx_inject_irq()
4513 if (vcpu->arch.interrupt.soft) { in vmx_inject_irq()
4516 vmx->vcpu.arch.event_exit_inst_len); in vmx_inject_irq()
4530 * Tracking the NMI-blocked state in software is built upon in vmx_inject_nmi()
4532 * well-behaving guests: They have to keep IRQs disabled at in vmx_inject_nmi()
4537 vmx->loaded_vmcs->soft_vnmi_blocked = 1; in vmx_inject_nmi()
4538 vmx->loaded_vmcs->vnmi_blocked_time = 0; in vmx_inject_nmi()
4541 ++vcpu->stat.nmi_injections; in vmx_inject_nmi()
4542 vmx->loaded_vmcs->nmi_known_unmasked = false; in vmx_inject_nmi()
4544 if (vmx->rmode.vm86_active) { in vmx_inject_nmi()
4561 return vmx->loaded_vmcs->soft_vnmi_blocked; in vmx_get_nmi_mask()
4562 if (vmx->loaded_vmcs->nmi_known_unmasked) in vmx_get_nmi_mask()
4565 vmx->loaded_vmcs->nmi_known_unmasked = !masked; in vmx_get_nmi_mask()
4574 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { in vmx_set_nmi_mask()
4575 vmx->loaded_vmcs->soft_vnmi_blocked = masked; in vmx_set_nmi_mask()
4576 vmx->loaded_vmcs->vnmi_blocked_time = 0; in vmx_set_nmi_mask()
4579 vmx->loaded_vmcs->nmi_known_unmasked = !masked; in vmx_set_nmi_mask()
4594 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) in vmx_nmi_blocked()
4604 if (to_vmx(vcpu)->nested.nested_run_pending) in vmx_nmi_allowed()
4605 return -EBUSY; in vmx_nmi_allowed()
4607 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */ in vmx_nmi_allowed()
4609 return -EBUSY; in vmx_nmi_allowed()
4626 if (to_vmx(vcpu)->nested.nested_run_pending) in vmx_interrupt_allowed()
4627 return -EBUSY; in vmx_interrupt_allowed()
4630 * An IRQ must not be injected into L2 if it's supposed to VM-Exit, in vmx_interrupt_allowed()
4634 return -EBUSY; in vmx_interrupt_allowed()
4646 mutex_lock(&kvm->slots_lock); in vmx_set_tss_addr()
4649 mutex_unlock(&kvm->slots_lock); in vmx_set_tss_addr()
4653 to_kvm_vmx(kvm)->tss_addr = addr; in vmx_set_tss_addr()
4659 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; in vmx_set_identity_map_addr()
4671 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = in rmode_exception()
4673 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) in rmode_exception()
4677 return !(vcpu->guest_debug & in rmode_exception()
4701 if (vcpu->arch.halt_request) { in handle_rmode_exception()
4702 vcpu->arch.halt_request = 0; in handle_rmode_exception()
4751 * - Guest CPL == 3 (user mode)
4752 * - Guest has #AC detection enabled in CR0
4753 * - Guest EFLAGS has AC bit set
4767 struct kvm_run *kvm_run = vcpu->run; in handle_exception_nmi()
4772 vect_info = vmx->idt_vectoring_info; in handle_exception_nmi()
4785 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { in handle_exception_nmi()
4790 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero in handle_exception_nmi()
4807 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in handle_exception_nmi()
4808 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; in handle_exception_nmi()
4809 vcpu->run->internal.ndata = 4; in handle_exception_nmi()
4810 vcpu->run->internal.data[0] = vect_info; in handle_exception_nmi()
4811 vcpu->run->internal.data[1] = intr_info; in handle_exception_nmi()
4812 vcpu->run->internal.data[2] = error_code; in handle_exception_nmi()
4813 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu; in handle_exception_nmi()
4819 if (enable_ept && !vcpu->arch.apf.host_apf_flags) { in handle_exception_nmi()
4833 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) in handle_exception_nmi()
4839 if (!(vcpu->guest_debug & in handle_exception_nmi()
4847 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM; in handle_exception_nmi()
4848 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); in handle_exception_nmi()
4856 vmx->vcpu.arch.event_exit_inst_len = in handle_exception_nmi()
4858 kvm_run->exit_reason = KVM_EXIT_DEBUG; in handle_exception_nmi()
4860 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; in handle_exception_nmi()
4861 kvm_run->debug.arch.exception = ex_no; in handle_exception_nmi()
4878 kvm_run->exit_reason = KVM_EXIT_EXCEPTION; in handle_exception_nmi()
4879 kvm_run->ex.exception = ex_no; in handle_exception_nmi()
4880 kvm_run->ex.error_code = error_code; in handle_exception_nmi()
4888 ++vcpu->stat.irq_exits; in handle_external_interrupt()
4894 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; in handle_triple_fault()
4895 vcpu->mmio_needed = 0; in handle_triple_fault()
4908 ++vcpu->stat.io_exits; in handle_io()
4931 /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
4943 * hardware. It consists of the L2-owned bits from the new in handle_set_cr0()
4944 * value combined with the L1-owned bits from L1's guest_cr0. in handle_set_cr0()
4946 val = (val & ~vmcs12->cr0_guest_host_mask) | in handle_set_cr0()
4947 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); in handle_set_cr0()
4957 if (to_vmx(vcpu)->nested.vmxon && in handle_set_cr0()
4972 val = (val & ~vmcs12->cr4_guest_host_mask) | in handle_set_cr4()
4973 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); in handle_set_cr4()
4984 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); in handle_desc()
5025 * KVM_GUESTDBG_SINGLESTEP-triggered in handle_cr()
5028 vcpu->run->exit_reason = KVM_EXIT_SET_TPR; in handle_cr()
5062 vcpu->run->exit_reason = 0; in handle_cr()
5080 /* Do not handle if the CPL > 0, will trigger GP on re-entry */ in handle_dr()
5086 * As the vm-exit takes precedence over the debug trap, we in handle_dr()
5090 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { in handle_dr()
5091 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_RTM | DR6_FIXED_1; in handle_dr()
5092 vcpu->run->debug.arch.dr7 = dr7; in handle_dr()
5093 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); in handle_dr()
5094 vcpu->run->debug.arch.exception = DB_VECTOR; in handle_dr()
5095 vcpu->run->exit_reason = KVM_EXIT_DEBUG; in handle_dr()
5103 if (vcpu->guest_debug == 0) { in handle_dr()
5111 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; in handle_dr()
5131 get_debugreg(vcpu->arch.db[0], 0); in vmx_sync_dirty_debug_regs()
5132 get_debugreg(vcpu->arch.db[1], 1); in vmx_sync_dirty_debug_regs()
5133 get_debugreg(vcpu->arch.db[2], 2); in vmx_sync_dirty_debug_regs()
5134 get_debugreg(vcpu->arch.db[3], 3); in vmx_sync_dirty_debug_regs()
5135 get_debugreg(vcpu->arch.dr6, 6); in vmx_sync_dirty_debug_regs()
5136 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); in vmx_sync_dirty_debug_regs()
5138 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; in vmx_sync_dirty_debug_regs()
5159 ++vcpu->stat.irq_window_exits; in handle_interrupt_window()
5215 * not cared. So make a short-circuit here by avoiding in handle_apic_access()
5232 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ in handle_apic_eoi_induced()
5242 /* APIC-write VM exit is trap-like and thus no need to adjust IP */ in handle_apic_write()
5256 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); in handle_task_switch()
5257 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); in handle_task_switch()
5258 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); in handle_task_switch()
5266 vcpu->arch.nmi_injected = false; in handle_task_switch()
5274 if (vmx->idt_vectoring_info & in handle_task_switch()
5300 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, in handle_task_switch()
5318 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && in handle_ept_violation()
5344 vcpu->arch.exit_qualification = exit_qualification; in handle_ept_violation()
5351 * would also use advanced VM-exit information for EPT violations to in handle_ept_violation()
5382 ++vcpu->stat.nmi_window_exits; in handle_nmi_window()
5397 while (vmx->emulation_required && count-- != 0) { in handle_invalid_guest_state()
5399 return handle_interrupt_window(&vmx->vcpu); in handle_invalid_guest_state()
5407 if (vmx->emulation_required && !vmx->rmode.vm86_active && in handle_invalid_guest_state()
5408 vcpu->arch.exception.pending) { in handle_invalid_guest_state()
5409 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in handle_invalid_guest_state()
5410 vcpu->run->internal.suberror = in handle_invalid_guest_state()
5412 vcpu->run->internal.ndata = 0; in handle_invalid_guest_state()
5416 if (vcpu->arch.halt_request) { in handle_invalid_guest_state()
5417 vcpu->arch.halt_request = 0; in handle_invalid_guest_state()
5436 unsigned int old = vmx->ple_window; in grow_ple_window()
5438 vmx->ple_window = __grow_ple_window(old, ple_window, in grow_ple_window()
5442 if (vmx->ple_window != old) { in grow_ple_window()
5443 vmx->ple_window_dirty = true; in grow_ple_window()
5444 trace_kvm_ple_window_update(vcpu->vcpu_id, in grow_ple_window()
5445 vmx->ple_window, old); in grow_ple_window()
5452 unsigned int old = vmx->ple_window; in shrink_ple_window()
5454 vmx->ple_window = __shrink_ple_window(old, ple_window, in shrink_ple_window()
5458 if (vmx->ple_window != old) { in shrink_ple_window()
5459 vmx->ple_window_dirty = true; in shrink_ple_window()
5460 trace_kvm_ple_window_update(vcpu->vcpu_id, in shrink_ple_window()
5461 vmx->ple_window, old); in shrink_ple_window()
5478 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5479 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5483 if (!kvm_pause_in_guest(vcpu->kvm)) in handle_pause()
5487 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" in handle_pause()
5488 * VM-execution control is ignored if CPL > 0. OTOH, KVM in handle_pause()
5562 trace_kvm_pml_full(vcpu->vcpu_id); in handle_pml_full()
5570 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && in handle_pml_full()
5587 if (!vmx->req_immediate_exit && in handle_fastpath_preemption_timer()
5588 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) { in handle_fastpath_preemption_timer()
5690 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { in vmx_get_exit_info()
5691 *info2 = vmx->idt_vectoring_info; in vmx_get_exit_info()
5706 if (vmx->pml_pg) { in vmx_destroy_pml_buffer()
5707 __free_page(vmx->pml_pg); in vmx_destroy_pml_buffer()
5708 vmx->pml_pg = NULL; in vmx_destroy_pml_buffer()
5721 if (pml_idx == (PML_ENTITY_NUM - 1)) in vmx_flush_pml_buffer()
5730 pml_buf = page_address(vmx->pml_pg); in vmx_flush_pml_buffer()
5735 WARN_ON(gpa & (PAGE_SIZE - 1)); in vmx_flush_pml_buffer()
5740 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); in vmx_flush_pml_buffer()
5765 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), in vmx_dump_sel()
5766 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), in vmx_dump_sel()
5767 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); in vmx_dump_sel()
5774 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); in vmx_dump_dtsel()
5912 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR)); in dump_vmcs()
5913 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR)); in dump_vmcs()
5934 u32 exit_reason = vmx->exit_reason; in vmx_handle_exit()
5935 u32 vectoring_info = vmx->idt_vectoring_info; in vmx_handle_exit()
5948 * We should never reach this point with a pending nested VM-Enter, and in vmx_handle_exit()
5951 * nested VM-Enter with an invalid vmcs12. in vmx_handle_exit()
5953 WARN_ON_ONCE(vmx->nested.nested_run_pending); in vmx_handle_exit()
5956 if (vmx->emulation_required) in vmx_handle_exit()
5965 * address-translation-based dirty tracking (e.g. EPT write in vmx_handle_exit()
5979 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; in vmx_handle_exit()
5980 vcpu->run->fail_entry.hardware_entry_failure_reason in vmx_handle_exit()
5982 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; in vmx_handle_exit()
5986 if (unlikely(vmx->fail)) { in vmx_handle_exit()
5988 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; in vmx_handle_exit()
5989 vcpu->run->fail_entry.hardware_entry_failure_reason in vmx_handle_exit()
5991 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu; in vmx_handle_exit()
5999 * The vm-exit can be triggered again after return to guest that in vmx_handle_exit()
6008 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in vmx_handle_exit()
6009 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; in vmx_handle_exit()
6010 vcpu->run->internal.ndata = 3; in vmx_handle_exit()
6011 vcpu->run->internal.data[0] = vectoring_info; in vmx_handle_exit()
6012 vcpu->run->internal.data[1] = exit_reason; in vmx_handle_exit()
6013 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; in vmx_handle_exit()
6015 vcpu->run->internal.ndata++; in vmx_handle_exit()
6016 vcpu->run->internal.data[3] = in vmx_handle_exit()
6019 vcpu->run->internal.data[vcpu->run->internal.ndata++] = in vmx_handle_exit()
6020 vcpu->arch.last_vmentry_cpu; in vmx_handle_exit()
6025 vmx->loaded_vmcs->soft_vnmi_blocked)) { in vmx_handle_exit()
6027 vmx->loaded_vmcs->soft_vnmi_blocked = 0; in vmx_handle_exit()
6028 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && in vmx_handle_exit()
6029 vcpu->arch.nmi_pending) { in vmx_handle_exit()
6032 * NMI-blocked window if the guest runs with IRQs in vmx_handle_exit()
6036 printk(KERN_WARNING "%s: Breaking out of NMI-blocked " in vmx_handle_exit()
6038 __func__, vcpu->vcpu_id); in vmx_handle_exit()
6039 vmx->loaded_vmcs->soft_vnmi_blocked = 0; in vmx_handle_exit()
6073 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; in vmx_handle_exit()
6074 vcpu->run->internal.suberror = in vmx_handle_exit()
6076 vcpu->run->internal.ndata = 2; in vmx_handle_exit()
6077 vcpu->run->internal.data[0] = exit_reason; in vmx_handle_exit()
6078 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu; in vmx_handle_exit()
6104 * Clear the per-vcpu flush bit, it gets set again in vmx_l1d_flush()
6108 flush_l1d = vcpu->arch.l1tf_flush_l1d; in vmx_l1d_flush()
6109 vcpu->arch.l1tf_flush_l1d = false; in vmx_l1d_flush()
6112 * Clear the per-cpu flush bit, it gets set again from in vmx_l1d_flush()
6122 vcpu->stat.l1d_flush++; in vmx_l1d_flush()
6161 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr; in update_cr8_intercept()
6163 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold; in update_cr8_intercept()
6182 vmx->nested.change_vmcs01_virtual_apic_mode = true; in vmx_set_virtual_apic_mode()
6204 * the guest may have inserted a non-APIC mapping into in vmx_set_virtual_apic_mode()
6227 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true; in vmx_set_apic_access_page_addr()
6235 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); in vmx_set_apic_access_page_addr()
6254 if (max_isr == -1) in vmx_hwapic_isr_update()
6271 if (vector == -1) in vmx_set_rvi()
6287 * vmcs12 virtual-interrupt-delivery enabled. in vmx_hwapic_irr_update()
6289 * intercepts external-interrupts and in that case in vmx_hwapic_irr_update()
6303 WARN_ON(!vcpu->arch.apicv_active); in vmx_sync_pir_to_irr()
6304 if (pi_test_on(&vmx->pi_desc)) { in vmx_sync_pir_to_irr()
6305 pi_clear_on(&vmx->pi_desc); in vmx_sync_pir_to_irr()
6312 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); in vmx_sync_pir_to_irr()
6316 * which can be injected, we should re-evaluate in vmx_sync_pir_to_irr()
6318 * If L1 intercepts external-interrupts, we should in vmx_sync_pir_to_irr()
6350 pi_clear_on(&vmx->pi_desc); in vmx_apicv_post_state_restore()
6351 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); in vmx_apicv_post_state_restore()
6368 u32 intr_info = vmx_get_intr_info(&vmx->vcpu); in handle_exception_nmi_irqoff()
6372 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags(); in handle_exception_nmi_irqoff()
6378 handle_interrupt_nmi_irqoff(&vmx->vcpu, intr_info); in handle_exception_nmi_irqoff()
6386 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info)) in handle_external_interrupt_irqoff()
6396 if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) in vmx_handle_exit_irqoff()
6398 else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI) in vmx_handle_exit_irqoff()
6428 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; in vmx_recover_nmi_blocking()
6431 if (vmx->loaded_vmcs->nmi_known_unmasked) in vmx_recover_nmi_blocking()
6434 exit_intr_info = vmx_get_intr_info(&vmx->vcpu); in vmx_recover_nmi_blocking()
6439 * Re-set bit "block by NMI" before VM entry if vmexit caused by in vmx_recover_nmi_blocking()
6443 * If the VM exit sets the valid bit in the IDT-vectoring in vmx_recover_nmi_blocking()
6452 vmx->loaded_vmcs->nmi_known_unmasked = in vmx_recover_nmi_blocking()
6455 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) in vmx_recover_nmi_blocking()
6456 vmx->loaded_vmcs->vnmi_blocked_time += in vmx_recover_nmi_blocking()
6458 vmx->loaded_vmcs->entry_time)); in vmx_recover_nmi_blocking()
6472 vcpu->arch.nmi_injected = false; in __vmx_complete_interrupts()
6486 vcpu->arch.nmi_injected = true; in __vmx_complete_interrupts()
6495 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); in __vmx_complete_interrupts()
6505 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); in __vmx_complete_interrupts()
6517 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, in vmx_complete_interrupts()
6556 if (vmx->req_immediate_exit) { in vmx_update_hv_timer()
6558 vmx->loaded_vmcs->hv_timer_soft_disabled = false; in vmx_update_hv_timer()
6559 } else if (vmx->hv_deadline_tsc != -1) { in vmx_update_hv_timer()
6561 if (vmx->hv_deadline_tsc > tscl) in vmx_update_hv_timer()
6562 /* set_hv_timer ensures the delta fits in 32-bits */ in vmx_update_hv_timer()
6563 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> in vmx_update_hv_timer()
6569 vmx->loaded_vmcs->hv_timer_soft_disabled = false; in vmx_update_hv_timer()
6570 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) { in vmx_update_hv_timer()
6571 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1); in vmx_update_hv_timer()
6572 vmx->loaded_vmcs->hv_timer_soft_disabled = true; in vmx_update_hv_timer()
6578 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) { in vmx_update_host_rsp()
6579 vmx->loaded_vmcs->host_state.rsp = host_rsp; in vmx_update_host_rsp()
6586 switch (to_vmx(vcpu)->exit_reason) { in vmx_exit_handlers_fastpath()
6627 if (vcpu->arch.cr2 != native_read_cr2()) in vmx_vcpu_enter_exit()
6628 native_write_cr2(vcpu->arch.cr2); in vmx_vcpu_enter_exit()
6630 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, in vmx_vcpu_enter_exit()
6631 vmx->loaded_vmcs->launched); in vmx_vcpu_enter_exit()
6633 vcpu->arch.cr2 = native_read_cr2(); in vmx_vcpu_enter_exit()
6664 vmx->loaded_vmcs->soft_vnmi_blocked)) in vmx_vcpu_run()
6665 vmx->loaded_vmcs->entry_time = ktime_get(); in vmx_vcpu_run()
6669 if (vmx->emulation_required) in vmx_vcpu_run()
6672 if (vmx->ple_window_dirty) { in vmx_vcpu_run()
6673 vmx->ple_window_dirty = false; in vmx_vcpu_run()
6674 vmcs_write32(PLE_WINDOW, vmx->ple_window); in vmx_vcpu_run()
6681 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync); in vmx_vcpu_run()
6684 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); in vmx_vcpu_run()
6686 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); in vmx_vcpu_run()
6689 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { in vmx_vcpu_run()
6691 vmx->loaded_vmcs->host_state.cr3 = cr3; in vmx_vcpu_run()
6695 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { in vmx_vcpu_run()
6697 vmx->loaded_vmcs->host_state.cr4 = cr4; in vmx_vcpu_run()
6700 /* When single-stepping over STI and MOV SS, we must clear the in vmx_vcpu_run()
6702 * vmentry fails as it then expects bit 14 (BS) in pending debug in vmx_vcpu_run()
6705 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) in vmx_vcpu_run()
6721 * it's non-zero. Since vmentry is serialising on affected CPUs, there in vmx_vcpu_run()
6725 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); in vmx_vcpu_run()
6737 * For non-nested case: in vmx_vcpu_run()
6746 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); in vmx_vcpu_run()
6748 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); in vmx_vcpu_run()
6752 current_evmcs->hv_clean_fields |= in vmx_vcpu_run()
6756 current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index; in vmx_vcpu_run()
6759 if (vmx->host_debugctlmsr) in vmx_vcpu_run()
6760 update_debugctlmsr(vmx->host_debugctlmsr); in vmx_vcpu_run()
6781 vmx->nested.nested_run_pending = 0; in vmx_vcpu_run()
6782 vmx->idt_vectoring_info = 0; in vmx_vcpu_run()
6784 if (unlikely(vmx->fail)) { in vmx_vcpu_run()
6785 vmx->exit_reason = 0xdead; in vmx_vcpu_run()
6789 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); in vmx_vcpu_run()
6790 if (unlikely((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)) in vmx_vcpu_run()
6793 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX); in vmx_vcpu_run()
6795 if (unlikely(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) in vmx_vcpu_run()
6798 vmx->loaded_vmcs->launched = 1; in vmx_vcpu_run()
6799 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); in vmx_vcpu_run()
6815 if (vcpu->arch.apicv_active) in vmx_vcpu_run()
6831 free_vpid(vmx->vpid); in vmx_free_vcpu()
6833 free_loaded_vmcs(vmx->loaded_vmcs); in vmx_free_vcpu()
6844 err = -ENOMEM; in vmx_create_vcpu()
6846 vmx->vpid = allocate_vpid(); in vmx_create_vcpu()
6855 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); in vmx_create_vcpu()
6856 if (!vmx->pml_pg) in vmx_create_vcpu()
6865 int j = vmx->nr_uret_msrs; in vmx_create_vcpu()
6872 vmx->guest_uret_msrs[j].slot = i; in vmx_create_vcpu()
6873 vmx->guest_uret_msrs[j].data = 0; in vmx_create_vcpu()
6881 vmx->guest_uret_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR; in vmx_create_vcpu()
6884 vmx->guest_uret_msrs[j].mask = -1ull; in vmx_create_vcpu()
6887 ++vmx->nr_uret_msrs; in vmx_create_vcpu()
6890 err = alloc_loaded_vmcs(&vmx->vmcs01); in vmx_create_vcpu()
6895 bitmap_fill(vmx->shadow_msr_intercept.read, MAX_POSSIBLE_PASSTHROUGH_MSRS); in vmx_create_vcpu()
6896 bitmap_fill(vmx->shadow_msr_intercept.write, MAX_POSSIBLE_PASSTHROUGH_MSRS); in vmx_create_vcpu()
6905 if (kvm_cstate_in_guest(vcpu->kvm)) { in vmx_create_vcpu()
6911 vmx->msr_bitmap_mode = 0; in vmx_create_vcpu()
6913 vmx->loaded_vmcs = &vmx->vmcs01; in vmx_create_vcpu()
6916 vcpu->cpu = cpu; in vmx_create_vcpu()
6921 err = alloc_apic_access_page(vcpu->kvm); in vmx_create_vcpu()
6927 err = init_rmode_identity_map(vcpu->kvm); in vmx_create_vcpu()
6933 memcpy(&vmx->nested.msrs, &vmcs_config.nested, sizeof(vmx->nested.msrs)); in vmx_create_vcpu()
6935 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs)); in vmx_create_vcpu()
6937 vmx->nested.posted_intr_nv = -1; in vmx_create_vcpu()
6938 vmx->nested.current_vmptr = -1ull; in vmx_create_vcpu()
6940 vcpu->arch.microcode_version = 0x100000000ULL; in vmx_create_vcpu()
6941 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED; in vmx_create_vcpu()
6947 vmx->pi_desc.nv = POSTED_INTR_VECTOR; in vmx_create_vcpu()
6948 vmx->pi_desc.sn = 1; in vmx_create_vcpu()
6950 vmx->ept_pointer = INVALID_PAGE; in vmx_create_vcpu()
6955 free_loaded_vmcs(vmx->loaded_vmcs); in vmx_create_vcpu()
6959 free_vpid(vmx->vpid); in vmx_create_vcpu()
6963 …nt and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/ad…
6964 …tion disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/ad…
6968 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock); in vmx_vm_init()
6971 kvm->arch.pause_in_guest = true; in vmx_vm_init()
7008 return -EIO; in vmx_check_processor_compat()
7012 return -EIO; in vmx_check_processor_compat()
7018 return -EIO; in vmx_check_processor_compat()
7028 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in in vmx_get_mt_mask()
7032 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to in vmx_get_mt_mask()
7037 * When there is no need to deal with noncoherent DMA (e.g., no VT-d in vmx_get_mt_mask()
7038 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The in vmx_get_mt_mask()
7042 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The in vmx_get_mt_mask()
7043 * EPT memory type is used to emulate guest CD/MTRR. in vmx_get_mt_mask()
7051 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { in vmx_get_mt_mask()
7059 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) in vmx_get_mt_mask()
7086 u32 new_ctl = vmx->secondary_exec_control; in vmcs_set_secondary_exec_control()
7094 * (indicating "allowed-1") if they are supported in the guest's CPUID.
7101 vmx->nested.msrs.cr0_fixed1 = 0xffffffff; in nested_vmx_cr_fixed1_bits_update()
7102 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; in nested_vmx_cr_fixed1_bits_update()
7105 if (entry && (entry->_reg & (_cpuid_mask))) \ in nested_vmx_cr_fixed1_bits_update()
7106 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ in nested_vmx_cr_fixed1_bits_update()
7144 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; in nested_vmx_entry_exit_ctls_update()
7145 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; in nested_vmx_entry_exit_ctls_update()
7147 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS; in nested_vmx_entry_exit_ctls_update()
7148 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS; in nested_vmx_entry_exit_ctls_update()
7163 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax; in update_intel_pt_cfg()
7164 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx; in update_intel_pt_cfg()
7165 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx; in update_intel_pt_cfg()
7166 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx; in update_intel_pt_cfg()
7170 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps, in update_intel_pt_cfg()
7174 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS | in update_intel_pt_cfg()
7181 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering)) in update_intel_pt_cfg()
7182 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN; in update_intel_pt_cfg()
7188 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc)) in update_intel_pt_cfg()
7189 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC | in update_intel_pt_cfg()
7196 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc)) in update_intel_pt_cfg()
7197 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN | in update_intel_pt_cfg()
7201 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite)) in update_intel_pt_cfg()
7202 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW | in update_intel_pt_cfg()
7206 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace)) in update_intel_pt_cfg()
7207 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN; in update_intel_pt_cfg()
7210 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output)) in update_intel_pt_cfg()
7211 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA; in update_intel_pt_cfg()
7214 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys)) in update_intel_pt_cfg()
7215 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN; in update_intel_pt_cfg()
7218 for (i = 0; i < vmx->pt_desc.addr_range; i++) in update_intel_pt_cfg()
7219 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4)); in update_intel_pt_cfg()
7227 vcpu->arch.xsaves_enabled = false; in vmx_vcpu_after_set_cpuid()
7235 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= in vmx_vcpu_after_set_cpuid()
7239 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= in vmx_vcpu_after_set_cpuid()
7301 to_vmx(vcpu)->req_immediate_exit = true; in vmx_request_immediate_exit()
7312 if (info->intercept == x86_intercept_in || in vmx_check_intercept_io()
7313 info->intercept == x86_intercept_ins) { in vmx_check_intercept_io()
7314 port = info->src_val; in vmx_check_intercept_io()
7315 size = info->dst_bytes; in vmx_check_intercept_io()
7317 port = info->dst_val; in vmx_check_intercept_io()
7318 size = info->src_bytes; in vmx_check_intercept_io()
7322 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction in vmx_check_intercept_io()
7323 * VM-exits depend on the 'unconditional IO exiting' VM-execution in vmx_check_intercept_io()
7326 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps. in vmx_check_intercept_io()
7345 switch (info->intercept) { in vmx_check_intercept()
7352 exception->vector = UD_VECTOR; in vmx_check_intercept()
7353 exception->error_code_valid = false; in vmx_check_intercept()
7391 u64 low = a << shift, high = a >> (64 - shift); in u64_shl_div_u64()
7410 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer; in vmx_set_hv_timer()
7415 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; in vmx_set_hv_timer()
7417 ktimer->timer_advance_ns); in vmx_set_hv_timer()
7420 delta_tsc -= lapic_timer_advance_cycles; in vmx_set_hv_timer()
7425 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && in vmx_set_hv_timer()
7428 vcpu->arch.tsc_scaling_ratio, &delta_tsc)) in vmx_set_hv_timer()
7429 return -ERANGE; in vmx_set_hv_timer()
7438 return -ERANGE; in vmx_set_hv_timer()
7440 vmx->hv_deadline_tsc = tscl + delta_tsc; in vmx_set_hv_timer()
7447 to_vmx(vcpu)->hv_deadline_tsc = -1; in vmx_cancel_hv_timer()
7453 if (!kvm_pause_in_guest(vcpu->kvm)) in vmx_sched_in()
7504 if (vcpu->arch.mcg_cap & MCG_LMCE_P) in vmx_setup_mce()
7505 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= in vmx_setup_mce()
7508 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= in vmx_setup_mce()
7515 if (to_vmx(vcpu)->nested.nested_run_pending) in vmx_smi_allowed()
7516 return -EBUSY; in vmx_smi_allowed()
7524 vmx->nested.smm.guest_mode = is_guest_mode(vcpu); in vmx_pre_enter_smm()
7525 if (vmx->nested.smm.guest_mode) in vmx_pre_enter_smm()
7526 nested_vmx_vmexit(vcpu, -1, 0, 0); in vmx_pre_enter_smm()
7528 vmx->nested.smm.vmxon = vmx->nested.vmxon; in vmx_pre_enter_smm()
7529 vmx->nested.vmxon = false; in vmx_pre_enter_smm()
7539 if (vmx->nested.smm.vmxon) { in vmx_pre_leave_smm()
7540 vmx->nested.vmxon = true; in vmx_pre_leave_smm()
7541 vmx->nested.smm.vmxon = false; in vmx_pre_leave_smm()
7544 if (vmx->nested.smm.guest_mode) { in vmx_pre_leave_smm()
7549 vmx->nested.smm.guest_mode = false; in vmx_pre_leave_smm()
7561 return to_vmx(vcpu)->nested.vmxon; in vmx_apic_init_signal_blocked()
7567 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer; in vmx_migrate_timers()
7733 return -EIO; in hardware_setup()
7825 * and EPT A/D bit features are enabled -- PML depends on them to work. in hardware_setup()
7872 return -EINVAL; in hardware_setup()
7926 * Reset everything to support using non-enlightened VMCS in vmx_exit()
7936 vp_ap->nested_control.features.directhypercall = 0; in vmx_exit()
7937 vp_ap->current_nested_vmcs = 0; in vmx_exit()
7938 vp_ap->enlighten_vmentry = 0; in vmx_exit()
7973 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); in vmx_init()